Skip to content

Commit

Permalink
BGP Policy conversion unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus committed Oct 3, 2023
1 parent 07a2d40 commit d4ff9c1
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 27 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ http_archive(

http_archive(
name = "com_github_opencomputeproject_sai",
patch_args = ["-p1"],
patches = ["//patches:sai.patch"],
build_file_content = """
cc_library(
name = "sai",
Expand All @@ -141,6 +139,8 @@ cc_library(
visibility = ["//visibility:public"],
)
""",
patch_args = ["-p1"],
patches = ["//patches:sai.patch"],
sha256 = "240d0211bbea2758faabfdbfa5e5488d837a47d42839bfe99b4bfbff52ab6c11",
strip_prefix = "SAI-1.11.0",
urls = ["https://github.com/opencomputeproject/SAI/archive/refs/tags/v1.11.0.tar.gz"],
Expand Down
10 changes: 9 additions & 1 deletion bgp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ go_library(
"@com_github_wenovus_gobgp_v3//pkg/config/gobgp",
"@com_github_wenovus_gobgp_v3//pkg/server",
"@com_github_wenovus_gobgp_v3//pkg/zebra",
"@org_golang_x_exp//maps",
"@org_golang_x_exp//slices",
],
)

go_test(
name = "bgp_test",
srcs = ["gobgp_test.go"],
srcs = [
"config_test.go",
"gobgp_test.go",
],
embed = [":bgp"],
deps = [
"//gnmi/fakedevice",
"//gnmi/oc",
"@com_github_google_go_cmp//cmp",
"@com_github_openconfig_ygot//ygot",
"@com_github_wenovus_gobgp_v3//pkg/config/gobgp",
],
)
28 changes: 8 additions & 20 deletions bgp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package bgp
import (
"net/netip"

"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

log "github.com/golang/glog"
"github.com/openconfig/lemming/gnmi/oc"
"github.com/wenovus/gobgp/v3/pkg/config/gobgp"
Expand Down Expand Up @@ -45,10 +48,6 @@ func intendedToGoBGP(bgpoc *oc.NetworkInstance_Protocol_Bgp, policyoc *oc.Routin
}

for neighAddr, neigh := range bgpoc.Neighbor {
applyPolicy := convertNeighborApplyPolicy(neigh)
applyPolicy.Config.ImportPolicyList = convertPolicyNames(neighAddr, applyPolicy.Config.ImportPolicyList)
applyPolicy.Config.ExportPolicyList = convertPolicyNames(neighAddr, applyPolicy.Config.ExportPolicyList)

// Add neighbour config.
bgpConfig.Neighbors = append(bgpConfig.Neighbors, gobgp.Neighbor{
Config: gobgp.NeighborConfig{
Expand All @@ -68,18 +67,6 @@ func intendedToGoBGP(bgpoc *oc.NetworkInstance_Protocol_Bgp, policyoc *oc.Routin
RemotePort: neigh.GetNeighborPort(),
},
},
// NOTE: From reading GoBGP's source code these are not used for filtering
// routes (the global ApplyPolicy list is used instead) unless the neighbour
// is a route server client.
//
// However, testing shows that when a REJECT policy is installed in the
// presence of routes, they are not withdrawn UNLESS this configuration is
// populated. Therefore it's possible this is a bug in GoBGP where the
// global apply policy list is not used for computing route withdrawals.
//
// As such this configuration is kept to get the withdraw behaviour, but how
// this works is not well-understood and needs more work.
ApplyPolicy: applyPolicy,
})
}

Expand All @@ -101,7 +88,6 @@ func intendedToGoBGP(bgpoc *oc.NetworkInstance_Protocol_Bgp, policyoc *oc.Routin
}

// intendedToGoBGPPolicies populates bgpConfig's policies from the OC configuration.
// TODO: applied state
func intendedToGoBGPPolicies(bgpoc *oc.NetworkInstance_Protocol_Bgp, policyoc *oc.RoutingPolicy, bgpConfig *gobgp.BgpConfigSet) {
var communitySetIndexMap map[string]int
// community sets
Expand All @@ -111,8 +97,11 @@ func intendedToGoBGPPolicies(bgpoc *oc.NetworkInstance_Protocol_Bgp, policyoc *o
// AS Path Sets
bgpConfig.DefinedSets.BgpDefinedSets.AsPathSets = convertASPathSets(policyoc.GetOrCreateDefinedSets().GetOrCreateBgpDefinedSets().AsPathSet)

neighAddrs := maps.Keys(bgpoc.Neighbor)
slices.Sort(neighAddrs)

// Neighbours, global policy definitions, and global apply policy list.
for neighAddr, neigh := range bgpoc.Neighbor {
for _, neighAddr := range neighAddrs {
// Ideally a simple conversion of apply-policy is sufficient, but due to GoBGP using
// a global set of apply-policy instead of per-neighbour policies, we need to create
// neighbour sets and modify input policy statements so that we retain the same
Expand All @@ -134,7 +123,7 @@ func intendedToGoBGPPolicies(bgpoc *oc.NetworkInstance_Protocol_Bgp, policyoc *o
NeighborInfoList: []string{neighAddr},
})

applyPolicy := convertNeighborApplyPolicy(neigh)
applyPolicy := convertNeighborApplyPolicy(bgpoc.Neighbor[neighAddr])

// populatePolicies populates the global policy definitions and the ApplyPolicy
// list, and returns the list of converted policies' names.
Expand All @@ -148,7 +137,6 @@ func intendedToGoBGPPolicies(bgpoc *oc.NetworkInstance_Protocol_Bgp, policyoc *o
applyPolicyList = append(applyPolicyList, convertedPolicyName)
continue
}
// TODO(wenbli): Add unit tests for BGP policy conversion.
policies[policyName] = true
policy, ok := policyoc.PolicyDefinition[policyName]
if !ok {
Expand Down
Loading

0 comments on commit d4ff9c1

Please sign in to comment.