Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support BGP policy's origin attributes #434

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions bgp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
v4Stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().SetOptions(oc.BgpPolicy_BgpSetCommunityOptionType_REPLACE)
v4Stmt.GetOrCreateActions().SetPolicyResult(oc.RoutingPolicy_PolicyResultType_ACCEPT_ROUTE)

// origin
v4Stmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetOriginEq(oc.BgpTypes_BgpOriginAttrType_EGP)
v4Stmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetRouteOrigin(oc.BgpTypes_BgpOriginAttrType_IGP)

bgpoc.GetOrCreateNeighbor("1.1.1.1").GetOrCreateApplyPolicy().SetExportPolicy([]string{policyName})
bgpoc.GetOrCreateNeighbor("1.1.1.1").GetOrCreateApplyPolicy().SetImportPolicy([]string{policyName})
bgpoc.GetOrCreateNeighbor("1.1.1.1").GetOrCreateApplyPolicy().SetDefaultImportPolicy(oc.RoutingPolicy_DefaultPolicyType_ACCEPT_ROUTE)
Expand Down Expand Up @@ -209,7 +213,7 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
AsPathSet: "", MatchSetOptions: "any",
},
MedEq: 0x0,
OriginEq: "",
OriginEq: "egp",
NextHopInList: []string(nil),
AfiSafiInList: []gobgpoc.AfiSafiType(nil),
LocalPrefEq: 0x0,
Expand All @@ -229,7 +233,7 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
BgpActions: gobgpoc.BgpActions{SetAsPathPrepend: gobgpoc.SetAsPathPrepend{RepeatN: 0x0, As: "0"},
SetCommunity: gobgpoc.SetCommunity{SetCommunityMethod: gobgpoc.SetCommunityMethod{CommunitiesList: []string(nil), CommunitySetRef: ""}, Options: "replace"},
SetExtCommunity: gobgpoc.SetExtCommunity{SetExtCommunityMethod: gobgpoc.SetExtCommunityMethod{CommunitiesList: []string(nil), ExtCommunitySetRef: ""}, Options: ""},
SetRouteOrigin: "",
SetRouteOrigin: "igp",
SetLocalPref: 0x0,
SetNextHop: "",
SetMed: "",
Expand Down Expand Up @@ -329,7 +333,7 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
AsPathSet: "", MatchSetOptions: "any",
},
MedEq: 0x0,
OriginEq: "",
OriginEq: "egp",
NextHopInList: []string(nil),
AfiSafiInList: []gobgpoc.AfiSafiType(nil),
LocalPrefEq: 0x0,
Expand All @@ -349,7 +353,7 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
BgpActions: gobgpoc.BgpActions{SetAsPathPrepend: gobgpoc.SetAsPathPrepend{RepeatN: 0x0, As: "0"},
SetCommunity: gobgpoc.SetCommunity{SetCommunityMethod: gobgpoc.SetCommunityMethod{CommunitiesList: []string(nil), CommunitySetRef: ""}, Options: "replace"},
SetExtCommunity: gobgpoc.SetExtCommunity{SetExtCommunityMethod: gobgpoc.SetExtCommunityMethod{CommunitiesList: []string(nil), ExtCommunitySetRef: ""}, Options: ""},
SetRouteOrigin: "",
SetRouteOrigin: "igp",
SetLocalPref: 0x0,
SetNextHop: "",
SetMed: "",
Expand Down
8 changes: 4 additions & 4 deletions bgp/gobgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,12 @@ func (t *bgpTask) populateRIBAttrs(path *api.Path, rib *oc.NetworkInstance_Proto
}
case *api.OriginAttribute:
hasOrigin = true
switch origin := m.GetOrigin(); origin {
case 0:
switch origin := m.GetOrigin(); int(origin) {
case gobgpoc.BGP_ORIGIN_ATTR_TYPE_IGP.ToInt():
attrSet.origin = oc.BgpTypes_BgpOriginAttrType_IGP
case 1:
case gobgpoc.BGP_ORIGIN_ATTR_TYPE_EGP.ToInt():
attrSet.origin = oc.BgpTypes_BgpOriginAttrType_EGP
case 2:
case gobgpoc.BGP_ORIGIN_ATTR_TYPE_INCOMPLETE.ToInt():
attrSet.origin = oc.BgpTypes_BgpOriginAttrType_INCOMPLETE
default:
log.Errorf("BGP: Unrecognized origin attribute value: %v", origin)
Expand Down
15 changes: 15 additions & 0 deletions bgp/ocgobgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func convertPolicyDefinition(policy *oc.RoutingPolicy_PolicyDefinition, neighAdd
MatchSetOptions: convertMatchSetOptionsType(statement.GetConditions().GetBgpConditions().GetMatchAsPathSet().GetMatchSetOptions()),
},
RouteType: convertRouteType(statement.GetConditions().GetBgpConditions().GetRouteType()),
OriginEq: convertOrigin(statement.GetConditions().GetBgpConditions().GetOriginEq()),
},
},
Actions: gobgpoc.Actions{
Expand All @@ -130,6 +131,7 @@ func convertPolicyDefinition(policy *oc.RoutingPolicy_PolicyDefinition, neighAdd
RepeatN: statement.GetActions().GetBgpActions().GetSetAsPathPrepend().GetRepeatN(),
As: strconv.FormatUint(uint64(statement.GetActions().GetBgpActions().GetSetAsPathPrepend().GetAsn()), 10),
},
SetRouteOrigin: convertOrigin(statement.GetActions().GetBgpActions().GetSetRouteOrigin()),
},
},
})
Expand Down Expand Up @@ -365,3 +367,16 @@ func convertSegmentTypeToOC(segmentType api.AsSegment_Type) oc.E_BgpTypes_AsPath
return oc.BgpTypes_AsPathSegmentType_UNSET
}
}

func convertOrigin(origin oc.E_BgpTypes_BgpOriginAttrType) gobgpoc.BgpOriginAttrType {
switch origin {
case oc.BgpTypes_BgpOriginAttrType_EGP:
return gobgpoc.BGP_ORIGIN_ATTR_TYPE_EGP
case oc.BgpTypes_BgpOriginAttrType_IGP:
return gobgpoc.BGP_ORIGIN_ATTR_TYPE_IGP
case oc.BgpTypes_BgpOriginAttrType_INCOMPLETE:
return gobgpoc.BGP_ORIGIN_ATTR_TYPE_INCOMPLETE
default:
return ""
}
}
6 changes: 6 additions & 0 deletions bgp/tests/local_tests/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ func testPolicyAux(t *testing.T, testspec *PolicyTestCase, dut1, dut2, dut3, dut
}

for _, routeTest := range testspec.routeTests {
if routeTest.Input.ReachPrefix == "" {
continue
}
if routeTest.RouteTest != nil {
// Install regular test route into DUT1.
route := &oc.NetworkInstance_Protocol_Static{
Expand Down Expand Up @@ -257,6 +260,9 @@ func testPolicyAux(t *testing.T, testspec *PolicyTestCase, dut1, dut2, dut3, dut
}

for _, routeTest := range testspec.routeTests {
if routeTest.Input.ReachPrefix == "" {
continue
}
if routeTest.RouteTest != nil {
testPropagation(t, routeTest.Input, routeTest.RouteTest, dut1, dut2, dut3)
testCommunities(t, routeTest.Input, routeTest.RouteTest, dut1, dut2, dut3)
Expand Down
140 changes: 132 additions & 8 deletions bgp/tests/local_tests/set_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ func singletonPrefixSetName(route string) string {
// TestSetAttributes tests setting BGP attributes.
func TestSetAttributes(t *testing.T) {
routesUnderTest := map[int]string{
0: "10.1.0.0/16",
1: "10.2.0.0/16",
2: "10.10.0.0/16",
3: "10.11.0.0/16",
4: "10.12.0.0/16",
5: "10.13.0.0/16",
6: "10.14.0.0/16",
7: "10.15.0.0/16",
0: "10.1.0.0/16",
1: "10.2.0.0/16",
2: "10.10.0.0/16",
3: "10.11.0.0/16",
4: "10.12.0.0/16",
5: "10.13.0.0/16",
6: "10.14.0.0/16",
7: "10.15.0.0/16",
30: "10.30.0.0/16",
31: "10.31.0.0/16",
32: "10.32.0.0/16",
33: "10.33.0.0/16",
}

installDefinedSets := func(t *testing.T, dut1, dut2, dut5 *Device) {
Expand Down Expand Up @@ -680,6 +684,98 @@ func TestSetAttributes(t *testing.T) {
},
},
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[30],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Rejected route due to Origin Match",
ExpectedResult: policytest.RouteDiscarded,
PrevAdjRibOutPreAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_IGP,
},
PrevAdjRibOutPostAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
},
AdjRibInPreAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
},
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[31],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Rejected route due to Origin Match",
ExpectedResult: policytest.RouteDiscarded,
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[32],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Rejected route due to Origin Match",
ExpectedResult: policytest.RouteDiscarded,
PrevAdjRibOutPreAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_IGP,
},
PrevAdjRibOutPostAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_INCOMPLETE,
},
AdjRibInPreAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_INCOMPLETE,
},
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[33],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Accepted route due to Origin Mismatch",
ExpectedResult: policytest.RouteAccepted,
PrevAdjRibOutPreAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_IGP,
},
PrevAdjRibOutPostAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
},
AdjRibInPreAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
},
AdjRibInPostAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
},
LocalRibAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
},
AdjRibOutPreAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
},
AdjRibOutPostAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
AsSegment: map[uint32]*oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet_AsSegment{
0: {Index: ygot.Uint32(0), Member: []uint32{64500}, Type: oc.BgpTypes_AsPathSegmentType_AS_SEQ},
},
},
NextAdjRibInPreAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
AsSegment: map[uint32]*oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet_AsSegment{
0: {Index: ygot.Uint32(0), Member: []uint32{64500}, Type: oc.BgpTypes_AsPathSegmentType_AS_SEQ},
},
},
NextAdjRibInPostAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
AsSegment: map[uint32]*oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet_AsSegment{
0: {Index: ygot.Uint32(0), Member: []uint32{64500}, Type: oc.BgpTypes_AsPathSegmentType_AS_SEQ},
},
},
NextLocalRibAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
Origin: oc.BgpTypes_BgpOriginAttrType_EGP,
AsSegment: map[uint32]*oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet_AsSegment{
0: {Index: ygot.Uint32(0), Member: []uint32{64500}, Type: oc.BgpTypes_AsPathSegmentType_AS_SEQ},
},
},
},
}}

testPolicy(t, &PolicyTestCase{
Expand Down Expand Up @@ -796,6 +892,34 @@ func TestSetAttributes(t *testing.T) {
installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().GetOrCreateMatchAsPathSet().SetAsPathSet(rejectASPathSetName2)
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().GetOrCreateMatchAsPathSet().SetMatchSetOptions(oc.PolicyTypes_MatchSetOptionsType_ANY)
case 30:
// Set Route Origin and then match on it for rejection.
installDut1ExportStmt = true
dut1ExportStmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetRouteOrigin(oc.BgpTypes_BgpOriginAttrType_EGP)

installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetOriginEq(oc.BgpTypes_BgpOriginAttrType_EGP)
case 31:
// Set Route Origin and then match on it for rejection.
installDut1ExportStmt = true
dut1ExportStmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetRouteOrigin(oc.BgpTypes_BgpOriginAttrType_IGP)

installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetOriginEq(oc.BgpTypes_BgpOriginAttrType_IGP)
case 32:
// Set Route Origin and then match on it for rejection.
installDut1ExportStmt = true
dut1ExportStmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetRouteOrigin(oc.BgpTypes_BgpOriginAttrType_INCOMPLETE)

installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetOriginEq(oc.BgpTypes_BgpOriginAttrType_INCOMPLETE)
case 33:
// Set Route Origin and then match on a different one for rejection.
installDut1ExportStmt = true
dut1ExportStmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetRouteOrigin(oc.BgpTypes_BgpOriginAttrType_EGP)

installDut1ImportStmt = false
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetOriginEq(oc.BgpTypes_BgpOriginAttrType_IGP)
}
if installDut1ExportStmt {
dut1ExportStmt.GetOrCreateActions().SetPolicyResult(oc.RoutingPolicy_PolicyResultType_ACCEPT_ROUTE)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/openconfig/testt v0.0.0-20220311054427-efbb1a32ec07
github.com/openconfig/ygnmi v0.11.1
github.com/openconfig/ygot v0.29.19
github.com/osrg/gobgp/v3 v3.27.1-0.20240606071546-dace87570846
github.com/osrg/gobgp/v3 v3.27.1-0.20240613095719-0c8d2054ef43
github.com/p4lang/p4runtime v1.4.0-rc.5.0.20220728214547-13f0d02a521e
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1797,8 +1797,8 @@ github.com/openconfig/ygot v0.10.4/go.mod h1:oCQNdXnv7dWc8scTDgoFkauv1wwplJn5Hsp
github.com/openconfig/ygot v0.13.2/go.mod h1:kJN0yCXIH07dOXvNBEFm3XxXdnDD5NI6K99tnD5x49c=
github.com/openconfig/ygot v0.29.19 h1:3bbAWbCBVjyjHgeROvT38LQ7pAxcjtm7C2vNVj/rvEE=
github.com/openconfig/ygot v0.29.19/go.mod h1:8/FXt4tc5wSfYDEJbGGumxmxwJ55Xuv12oO/jCyEins=
github.com/osrg/gobgp/v3 v3.27.1-0.20240606071546-dace87570846 h1:lhc6n8L5Yqm1ZNca+21TQjpXBrl+O3sNwDgU0/407Ms=
github.com/osrg/gobgp/v3 v3.27.1-0.20240606071546-dace87570846/go.mod h1:ZGeSti9mURR/o5hf5R6T1FM5g1yiEBZbhP+TuqYJUpI=
github.com/osrg/gobgp/v3 v3.27.1-0.20240613095719-0c8d2054ef43 h1:HamaGNtGjbXR3iqXr2f5J4b9YXgLEz3DtItZuIVn0vk=
github.com/osrg/gobgp/v3 v3.27.1-0.20240613095719-0c8d2054ef43/go.mod h1:ZGeSti9mURR/o5hf5R6T1FM5g1yiEBZbhP+TuqYJUpI=
github.com/p4lang/p4runtime v1.4.0-rc.5.0.20220728214547-13f0d02a521e h1:AfZKoikDXbZ7zWvO/lvCRzLo7i6lM+gNleYVMxPiWyQ=
github.com/p4lang/p4runtime v1.4.0-rc.5.0.20220728214547-13f0d02a521e/go.mod h1:m9laObIMXM9N1ElGXijc66/MSM5eheZJLRLxg/TG+fU=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
Expand Down
4 changes: 2 additions & 2 deletions repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1512,8 +1512,8 @@ def go_repositories():
name = "com_github_osrg_gobgp_v3",
build_file_proto_mode = "disable",
importpath = "github.com/osrg/gobgp/v3",
sum = "h1:lhc6n8L5Yqm1ZNca+21TQjpXBrl+O3sNwDgU0/407Ms=",
version = "v3.27.1-0.20240606071546-dace87570846",
sum = "h1:HamaGNtGjbXR3iqXr2f5J4b9YXgLEz3DtItZuIVn0vk=",
version = "v3.27.1-0.20240613095719-0c8d2054ef43",
)
go_repository(
name = "com_github_patrickmn_go_cache",
Expand Down
Loading