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

[DO NOT SUBMIT] Demonstrative Tests on ListPath Usage #2764

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2614,13 +2614,18 @@ func (s *BgpServer) getAdjRib(addr string, family bgp.RouteFamily, in bool, enab
if in {
adjRib = peer.adjRibIn
if enableFiltered {
//adjRib = table.NewAdjRib(s.logger, peer.configuredRFlist())
//for _, path := range peer.adjRibIn.PathList([]bgp.RouteFamily{family}, true) {
//}

for _, path := range peer.adjRibIn.PathList([]bgp.RouteFamily{family}, true) {
options := &table.PolicyOptions{
Validate: s.roaTable.Validate,
}
if s.policy.ApplyPolicy(peer.TableID(), table.POLICY_DIRECTION_IMPORT, path, options) == nil {
filtered[path.GetNlri().String()] = path
}
//adjRib.UpdateAdjRibOut([]*table.Path{path})
}
}
} else {
Expand Down
200 changes: 199 additions & 1 deletion pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,77 @@ func TestListPathEnableFiltered(test *testing.T) {
})
attrs := []*apb.Any{a1, a2}

wantCommunities := []uint32{100<<16 | 100}
wantCommunities2 := []uint32{100<<16 | 100, 200<<16 | 200}

commSet, _ := table.NewCommunitySet(oc.CommunitySet{
CommunitySetName: "comset1",
CommunityList: []string{"100:100"},
})
t.policy.AddDefinedSet(commSet, false)

// EXPORT
statement := oc.Statement{
Name: "stmt1",
Actions: oc.Actions{
BgpActions: oc.BgpActions{
SetCommunity: oc.SetCommunity{
SetCommunityMethod: oc.SetCommunityMethod{
CommunitiesList: []string{"100:100"},
},
Options: string(oc.BGP_SET_COMMUNITY_OPTION_TYPE_ADD),
},
},
RouteDisposition: oc.ROUTE_DISPOSITION_ACCEPT_ROUTE,
},
}
policy := oc.PolicyDefinition{
Name: "policy1",
Statements: []oc.Statement{statement},
}
p, err := table.NewPolicy(policy)
if err != nil {
test.Fatalf("cannot create new policy: %v", err)
}
t.policy.AddPolicy(p, false)
policies := []*oc.PolicyDefinition{
{
Name: "policy1",
},
}
t.policy.AddPolicyAssignment(table.GLOBAL_RIB_NAME, table.POLICY_DIRECTION_EXPORT, policies, table.ROUTE_TYPE_REJECT)

// IMPORT
statement = oc.Statement{
Name: "stmt1",
Actions: oc.Actions{
BgpActions: oc.BgpActions{
SetCommunity: oc.SetCommunity{
SetCommunityMethod: oc.SetCommunityMethod{
CommunitiesList: []string{"200:200"},
},
Options: string(oc.BGP_SET_COMMUNITY_OPTION_TYPE_ADD),
},
},
RouteDisposition: oc.ROUTE_DISPOSITION_ACCEPT_ROUTE,
},
}
policy = oc.PolicyDefinition{
Name: "policy1",
Statements: []oc.Statement{statement},
}
p, err = table.NewPolicy(policy)
if err != nil {
test.Fatalf("cannot create new policy: %v", err)
}
s.policy.AddPolicy(p, false)
policies = []*oc.PolicyDefinition{
{
Name: "policy1",
},
}
s.policy.AddPolicyAssignment(table.GLOBAL_RIB_NAME, table.POLICY_DIRECTION_IMPORT, policies, table.ROUTE_TYPE_REJECT)

t.AddPath(context.Background(), &api.AddPathRequest{
TableType: api.TableType_GLOBAL,
Path: &api.Path{
Expand Down Expand Up @@ -378,8 +449,135 @@ func TestListPathEnableFiltered(test *testing.T) {

for {
count := 0
s.ListPath(context.Background(), &api.ListPathRequest{TableType: api.TableType_ADJ_IN, Family: family, Name: "127.0.0.1"}, func(d *api.Destination) {
t.ListPath(context.Background(), &api.ListPathRequest{
TableType: api.TableType_ADJ_OUT,
Family: family, Name: "127.0.0.1",
EnableFiltered: true,
}, func(d *api.Destination) {
count++
for _, path := range d.Paths {
var comms []uint32
for _, attr := range path.GetPattrs() {
m, err := attr.UnmarshalNew()
if err != nil {
test.Fatalf("Unable to unmarshal a GoBGP path attribute: %v", err)
continue
}
switch m := m.(type) {
case *api.CommunitiesAttribute:
comms = m.GetCommunities()
}
}
if diff := cmp.Diff([]uint32(nil), comms); diff != "" {
test.Errorf("AdjRibOutPre communities for %v (-want, +got):\n%s", d.GetPrefix(), diff)
} else {
test.Logf("Got expected communities for %v: %v", d.GetPrefix(), comms)
}
}
})
if count == 2 {
break
}
}
for {
count := 0
t.ListPath(context.Background(), &api.ListPathRequest{
TableType: api.TableType_ADJ_OUT,
Family: family, Name: "127.0.0.1",
EnableFiltered: false,
}, func(d *api.Destination) {
count++
for _, path := range d.Paths {
if path.Filtered {
continue
}
var comms []uint32
for _, attr := range path.GetPattrs() {
m, err := attr.UnmarshalNew()
if err != nil {
test.Fatalf("Unable to unmarshal a GoBGP path attribute: %v", err)
continue
}
switch m := m.(type) {
case *api.CommunitiesAttribute:
comms = m.GetCommunities()
}
}
if diff := cmp.Diff(wantCommunities, comms); diff != "" {
test.Errorf("AdjRibOutPost communities for %v (-want, +got):\n%s", d.GetPrefix(), diff)
} else {
test.Logf("Got expected communities for %v: %v", d.GetPrefix(), comms)
}
}
})
if count == 2 {
break
}
}
for {
count := 0
s.ListPath(context.Background(), &api.ListPathRequest{
TableType: api.TableType_ADJ_IN,
Family: family,
Name: "127.0.0.1",
EnableFiltered: false,
}, func(d *api.Destination) {
count++
for _, path := range d.Paths {
var comms []uint32
for _, attr := range path.GetPattrs() {
m, err := attr.UnmarshalNew()
if err != nil {
test.Fatalf("Unable to unmarshal a GoBGP path attribute: %v", err)
continue
}
switch m := m.(type) {
case *api.CommunitiesAttribute:
comms = m.GetCommunities()
}
}
if diff := cmp.Diff(wantCommunities, comms); diff != "" {
test.Errorf("AdjRibInPre communities for %v (-want, +got):\n%s", d.GetPrefix(), diff)
} else {
test.Logf("Got expected communities for %v: %v", d.GetPrefix(), comms)
}
}
})
if count == 2 {
break
}
}
for {
count := 0
s.ListPath(context.Background(), &api.ListPathRequest{
TableType: api.TableType_ADJ_IN,
Family: family,
Name: "127.0.0.1",
EnableFiltered: true,
}, func(d *api.Destination) {
count++
for _, path := range d.Paths {
if path.Filtered {
continue
}
var comms []uint32
for _, attr := range path.GetPattrs() {
m, err := attr.UnmarshalNew()
if err != nil {
test.Fatalf("Unable to unmarshal a GoBGP path attribute: %v", err)
continue
}
switch m := m.(type) {
case *api.CommunitiesAttribute:
comms = m.GetCommunities()
}
}
if diff := cmp.Diff(wantCommunities2, comms); diff != "" {
test.Errorf("AdjRibInPost communities for %v (-want, +got):\n%s", d.GetPrefix(), diff)
} else {
test.Logf("Got expected communities for %v: %v", d.GetPrefix(), comms)
}
}
})
if count == 2 {
break
Expand Down
Loading