Skip to content

Commit

Permalink
Support BGP policy's set-next-hop and next-hop-in leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus committed Jun 14, 2024
1 parent 8f9b06f commit 8e263de
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 11 deletions.
12 changes: 8 additions & 4 deletions bgp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
v4Stmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetOriginEq(oc.BgpTypes_BgpOriginAttrType_EGP)
v4Stmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetRouteOrigin(oc.BgpTypes_BgpOriginAttrType_IGP)

// set-next-hop
v4Stmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetNextHopIn([]string{"111.111.111.111"})
v4Stmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetNextHop(oc.BgpPolicy_BgpNextHopType_Enum_SELF)

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 @@ -214,7 +218,7 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
},
MedEq: 0x0,
OriginEq: "egp",
NextHopInList: []string(nil),
NextHopInList: []string{"111.111.111.111"},
AfiSafiInList: []gobgpoc.AfiSafiType(nil),
LocalPrefEq: 0x0,
CommunityCount: gobgpoc.CommunityCount{Operator: "", Value: 0x0},
Expand All @@ -235,7 +239,7 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
SetExtCommunity: gobgpoc.SetExtCommunity{SetExtCommunityMethod: gobgpoc.SetExtCommunityMethod{CommunitiesList: []string(nil), ExtCommunitySetRef: ""}, Options: ""},
SetRouteOrigin: "igp",
SetLocalPref: 0x0,
SetNextHop: "",
SetNextHop: "self",
SetMed: "",
SetLargeCommunity: gobgpoc.SetLargeCommunity{SetLargeCommunityMethod: gobgpoc.SetLargeCommunityMethod{CommunitiesList: []string(nil)}, Options: ""},
},
Expand Down Expand Up @@ -334,7 +338,7 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
},
MedEq: 0x0,
OriginEq: "egp",
NextHopInList: []string(nil),
NextHopInList: []string{"111.111.111.111"},
AfiSafiInList: []gobgpoc.AfiSafiType(nil),
LocalPrefEq: 0x0,
CommunityCount: gobgpoc.CommunityCount{Operator: "", Value: 0x0},
Expand All @@ -355,7 +359,7 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
SetExtCommunity: gobgpoc.SetExtCommunity{SetExtCommunityMethod: gobgpoc.SetExtCommunityMethod{CommunitiesList: []string(nil), ExtCommunitySetRef: ""}, Options: ""},
SetRouteOrigin: "igp",
SetLocalPref: 0x0,
SetNextHop: "",
SetNextHop: "self",
SetMed: "",
SetLargeCommunity: gobgpoc.SetLargeCommunity{SetLargeCommunityMethod: gobgpoc.SetLargeCommunityMethod{CommunitiesList: []string(nil)}, Options: ""},
},
Expand Down
28 changes: 26 additions & 2 deletions bgp/ocgobgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func convertPolicyDefinition(policy *oc.RoutingPolicy_PolicyDefinition, neighAdd
if err != nil {
log.Errorf("MED value not supported: %v", err)
}
setNextHop, err := convertNextHop(statement.GetActions().GetBgpActions().GetSetNextHop())
if err != nil {
log.Error(err)
}
statements = append(statements, gobgpoc.Statement{
// In GoBGP, statements must have globally-unique names.
// Ensure uniqueness by qualifying each one with the name of the converted policy.
Expand All @@ -112,8 +116,9 @@ func convertPolicyDefinition(policy *oc.RoutingPolicy_PolicyDefinition, neighAdd
AsPathSet: statement.Conditions.GetBgpConditions().GetMatchAsPathSet().GetAsPathSet(),
MatchSetOptions: convertMatchSetOptionsType(statement.GetConditions().GetBgpConditions().GetMatchAsPathSet().GetMatchSetOptions()),
},
RouteType: convertRouteType(statement.GetConditions().GetBgpConditions().GetRouteType()),
OriginEq: convertOrigin(statement.GetConditions().GetBgpConditions().GetOriginEq()),
RouteType: convertRouteType(statement.GetConditions().GetBgpConditions().GetRouteType()),
OriginEq: convertOrigin(statement.GetConditions().GetBgpConditions().GetOriginEq()),
NextHopInList: statement.GetConditions().GetBgpConditions().GetNextHopIn(),
},
},
Actions: gobgpoc.Actions{
Expand All @@ -132,6 +137,7 @@ func convertPolicyDefinition(policy *oc.RoutingPolicy_PolicyDefinition, neighAdd
As: strconv.FormatUint(uint64(statement.GetActions().GetBgpActions().GetSetAsPathPrepend().GetAsn()), 10),
},
SetRouteOrigin: convertOrigin(statement.GetActions().GetBgpActions().GetSetRouteOrigin()),
SetNextHop: setNextHop,
},
},
})
Expand Down Expand Up @@ -380,3 +386,21 @@ func convertOrigin(origin oc.E_BgpTypes_BgpOriginAttrType) gobgpoc.BgpOriginAttr
return ""
}
}

func convertNextHop(nexthop oc.RoutingPolicy_PolicyDefinition_Statement_Actions_BgpActions_SetNextHop_Union) (gobgpoc.BgpNextHopType, error) {
if nexthop == nil {
return "", nil
}
switch nh := nexthop.(type) {
case oc.UnionString:
return gobgpoc.BgpNextHopType(nh), nil
case oc.E_BgpPolicy_BgpNextHopType_Enum:
switch nh {
case oc.BgpPolicy_BgpNextHopType_Enum_SELF:
return gobgpoc.BgpNextHopType("self"), nil
case oc.BgpPolicy_BgpNextHopType_Enum_PEER_ADDRESS:
return gobgpoc.BgpNextHopType("peer-address"), nil
}
}
return "", fmt.Errorf("unrecognized value for SetNextHop: (%T, %v)", nexthop, nexthop)
}
118 changes: 118 additions & 0 deletions bgp/tests/local_tests/set_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func singletonPrefixSetName(route string) string {

// TestSetAttributes tests setting BGP attributes.
func TestSetAttributes(t *testing.T) {
// For debugging: just comment out the ones you don't want to run.
routesUnderTest := map[int]string{
0: "10.1.0.0/16",
1: "10.2.0.0/16",
Expand All @@ -56,6 +57,11 @@ func TestSetAttributes(t *testing.T) {
5: "10.13.0.0/16",
6: "10.14.0.0/16",
7: "10.15.0.0/16",
20: "10.20.0.0/16",
21: "10.21.0.0/16",
22: "10.22.0.0/16",
23: "10.23.0.0/16",
24: "10.24.0.0/16",
30: "10.30.0.0/16",
31: "10.31.0.0/16",
32: "10.32.0.0/16",
Expand Down Expand Up @@ -684,6 +690,86 @@ func TestSetAttributes(t *testing.T) {
},
},
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[20],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Rejected route due to NextHop Match",
ExpectedResult: policytest.RouteDiscarded,
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[21],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Accepted route due to NextHop Mismatch",
ExpectedResult: policytest.RouteAccepted,
AdjRibOutPostAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
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{
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{
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{
AsSegment: map[uint32]*oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet_AsSegment{
0: {Index: ygot.Uint32(0), Member: []uint32{64500}, Type: oc.BgpTypes_AsPathSegmentType_AS_SEQ},
},
},
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[22],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Rejected route due to NextHop Match",
ExpectedResult: policytest.RouteDiscarded,
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[23],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Accepted route due to NextHop Mismatch",
ExpectedResult: policytest.RouteAccepted,
AdjRibOutPostAttrs: &oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet{
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{
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{
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{
AsSegment: map[uint32]*oc.NetworkInstance_Protocol_Bgp_Rib_AttrSet_AsSegment{
0: {Index: ygot.Uint32(0), Member: []uint32{64500}, Type: oc.BgpTypes_AsPathSegmentType_AS_SEQ},
},
},
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[24],
},
RouteTest: &policytest.RoutePathTestCase{
Description: "Rejected route due to NextHop Mismatch",
ExpectedResult: policytest.RouteDiscarded,
},
}, {
Input: policytest.TestRoute{
ReachPrefix: routesUnderTest[30],
Expand Down Expand Up @@ -892,6 +978,38 @@ func TestSetAttributes(t *testing.T) {
installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().GetOrCreateMatchAsPathSet().SetAsPathSet(rejectASPathSetName2)
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().GetOrCreateMatchAsPathSet().SetMatchSetOptions(oc.PolicyTypes_MatchSetOptionsType_ANY)
case 20:
// Set NextHop SELF and then match on it for rejection.
installDut1ExportStmt = true
dut1ExportStmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetNextHop(oc.BgpPolicy_BgpNextHopType_Enum_SELF)

installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetNextHopIn([]string{dut1.RouterID})
case 21:
// Don't set NextHop but still match on it.
installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetNextHopIn([]string{dut1.RouterID})
case 22:
// Set NextHop to custom and then match on it for rejection.
installDut1ExportStmt = true
dut1ExportStmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetNextHop(oc.UnionString("1.2.3.4"))

installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetNextHopIn([]string{"1.2.3.4"})
case 23:
// Set NextHop to custom and then match on a different one.
installDut1ExportStmt = true
dut1ExportStmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetNextHop(oc.UnionString("1.2.3.4"))

installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetNextHopIn([]string{dut1.RouterID})
case 24:
// Set NextHop to custom and then match on a different one.
installDut1ExportStmt = true
dut1ExportStmt.GetOrCreateActions().GetOrCreateBgpActions().SetSetNextHop(oc.BgpPolicy_BgpNextHopType_Enum_PEER_ADDRESS)

installDut1ImportStmt = true
dut1ImportStmt.GetOrCreateConditions().GetOrCreateBgpConditions().SetNextHopIn([]string{dut2.RouterID})
case 30:
// Set Route Origin and then match on it for rejection.
installDut1ExportStmt = true
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.20240613095719-0c8d2054ef43
github.com/osrg/gobgp/v3 v3.27.1-0.20240614010451-0148e2d22dcf
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.20240613095719-0c8d2054ef43 h1:HamaGNtGjbXR3iqXr2f5J4b9YXgLEz3DtItZuIVn0vk=
github.com/osrg/gobgp/v3 v3.27.1-0.20240613095719-0c8d2054ef43/go.mod h1:ZGeSti9mURR/o5hf5R6T1FM5g1yiEBZbhP+TuqYJUpI=
github.com/osrg/gobgp/v3 v3.27.1-0.20240614010451-0148e2d22dcf h1:KrVLbjNucHf+LrrGcwrH6hN0RyfmbPx9Vk5/iBsFfYY=
github.com/osrg/gobgp/v3 v3.27.1-0.20240614010451-0148e2d22dcf/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:HamaGNtGjbXR3iqXr2f5J4b9YXgLEz3DtItZuIVn0vk=",
version = "v3.27.1-0.20240613095719-0c8d2054ef43",
sum = "h1:KrVLbjNucHf+LrrGcwrH6hN0RyfmbPx9Vk5/iBsFfYY=",
version = "v3.27.1-0.20240614010451-0148e2d22dcf",
)
go_repository(
name = "com_github_patrickmn_go_cache",
Expand Down

0 comments on commit 8e263de

Please sign in to comment.