diff --git a/routingtable/filter/term_condition.go b/routingtable/filter/term_condition.go index a7e0a4330d21e1678d8aacc03e429632506cf61b..df6b866a1d2f0e5482f25f9003e4c73cd9feb5f1 100644 --- a/routingtable/filter/term_condition.go +++ b/routingtable/filter/term_condition.go @@ -20,13 +20,17 @@ func NewTermCondition(prefixLists []*PrefixList, routeFilters []*RouteFilter) *T } func (f *TermCondition) Matches(p net.Prefix, pa *route.Path) bool { - return f.matchesAnyPrefixList(p) || - f.machtchesAnyRouteFilter(p) || - f.machtchesAnyLageCommunityFilter(pa) || - f.machtchesAnyCommunityFilter(pa) + return f.matchesPrefixListFilters(p) && + f.machtchesRouteFilters(p) && + f.machtchesCommunityFilters(pa) && + f.machtchesLageCommunityFilters(pa) } -func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool { +func (t *TermCondition) matchesPrefixListFilters(p net.Prefix) bool { + if len(t.prefixLists) == 0 { + return true + } + for _, l := range t.prefixLists { if l.Matches(p) { return true @@ -36,7 +40,11 @@ func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool { return false } -func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool { +func (t *TermCondition) machtchesRouteFilters(p net.Prefix) bool { + if len(t.routeFilters) == 0 { + return true + } + for _, l := range t.routeFilters { if l.Matches(p) { return true @@ -46,7 +54,11 @@ func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool { return false } -func (t *TermCondition) machtchesAnyCommunityFilter(pa *route.Path) bool { +func (t *TermCondition) machtchesCommunityFilters(pa *route.Path) bool { + if len(t.communityFilters) == 0 { + return true + } + if pa.BGPPath == nil { return false } @@ -60,7 +72,11 @@ func (t *TermCondition) machtchesAnyCommunityFilter(pa *route.Path) bool { return false } -func (t *TermCondition) machtchesAnyLageCommunityFilter(pa *route.Path) bool { +func (t *TermCondition) machtchesLageCommunityFilters(pa *route.Path) bool { + if len(t.largeCommunityFilters) == 0 { + return true + } + if pa.BGPPath == nil { return false } diff --git a/routingtable/filter/term_condition_test.go b/routingtable/filter/term_condition_test.go index 3d8558ab2d47abf957c5f8e294edccf06ab6fbb4..cca90950aa5a443c042c8061f77a08d3f4f7a0c4 100644 --- a/routingtable/filter/term_condition_test.go +++ b/routingtable/filter/term_condition_test.go @@ -26,8 +26,7 @@ func TestMatches(t *testing.T) { prefixLists: []*PrefixList{ NewPrefixList(net.NewPfx(strAddr("127.0.0.1"), 8)), }, - routeFilters: []*RouteFilter{}, - expected: true, + expected: true, }, { name: "one prefix in prefix list and no match, no route filters set", @@ -35,8 +34,7 @@ func TestMatches(t *testing.T) { prefixLists: []*PrefixList{ NewPrefixList(net.NewPfx(0, 32)), }, - routeFilters: []*RouteFilter{}, - expected: false, + expected: false, }, { name: "one prefix of 2 matches in prefix list, no route filters set", @@ -45,22 +43,19 @@ func TestMatches(t *testing.T) { NewPrefixList(net.NewPfx(strAddr("10.0.0.0"), 8)), NewPrefixList(net.NewPfx(strAddr("127.0.0.1"), 8)), }, - routeFilters: []*RouteFilter{}, - expected: true, + expected: true, }, { - name: "no prefixes in prefix list, only route filter matches", - prefix: net.NewPfx(strAddr("10.0.0.0"), 24), - prefixLists: []*PrefixList{}, + name: "no prefixes in prefix list, only route filter matches", + prefix: net.NewPfx(strAddr("10.0.0.0"), 24), routeFilters: []*RouteFilter{ NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), }, expected: true, }, { - name: "no prefixes in prefix list, one route filter matches", - prefix: net.NewPfx(strAddr("10.0.0.0"), 24), - prefixLists: []*PrefixList{}, + name: "no prefixes in prefix list, one route filter matches", + prefix: net.NewPfx(strAddr("10.0.0.0"), 24), routeFilters: []*RouteFilter{ NewRouteFilter(net.NewPfx(strAddr("8.0.0.0"), 8), Longer()), NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), @@ -68,9 +63,8 @@ func TestMatches(t *testing.T) { expected: true, }, { - name: "no prefixes in prefix list, one of many route filters matches", - prefix: net.NewPfx(strAddr("127.0.0.1"), 8), - prefixLists: []*PrefixList{}, + name: "no prefixes in prefix list, one of many route filters matches", + prefix: net.NewPfx(strAddr("127.0.0.1"), 8), routeFilters: []*RouteFilter{ NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), }, @@ -88,7 +82,7 @@ func TestMatches(t *testing.T) { expected: false, }, { - name: "one prefix in prefixlist, one route fitler, only prefix list matches", + name: "one prefix in prefixlist, one route filter, only prefix list matches", prefix: net.NewPfx(strAddr("8.8.8.0"), 24), prefixLists: []*PrefixList{ NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)), @@ -96,10 +90,10 @@ func TestMatches(t *testing.T) { routeFilters: []*RouteFilter{ NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), }, - expected: true, + expected: false, }, { - name: "one prefix in prefixlist, one route fitler, only route filter matches", + name: "one prefix in prefixlist, one route filter, only route filter matches", prefix: net.NewPfx(strAddr("10.0.0.0"), 24), prefixLists: []*PrefixList{ NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)), @@ -107,7 +101,7 @@ func TestMatches(t *testing.T) { routeFilters: []*RouteFilter{ NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), }, - expected: true, + expected: false, }, { name: "community matches", @@ -123,6 +117,17 @@ func TestMatches(t *testing.T) { { name: "community does not match", prefix: net.NewPfx(strAddr("10.0.0.0"), 24), + bgpPath: &route.BGPPath{ + Communities: "(1,2) (3,4) (5,6)", + }, + communityFilters: []*CommunityFilter{ + &CommunityFilter{196608}, // (3,0) + }, + expected: false, + }, + { + name: "community filter, bgp path is nil", + prefix: net.NewPfx(strAddr("10.0.0.0"), 24), communityFilters: []*CommunityFilter{ &CommunityFilter{196608}, // (3,0) }, @@ -160,6 +165,20 @@ func TestMatches(t *testing.T) { }, expected: false, }, + { + name: "large community filter, bgp path is nil", + prefix: net.NewPfx(strAddr("10.0.0.0"), 24), + largeCommunityFilters: []*LargeCommunityFilter{ + { + &packet.LargeCommunity{ + GlobalAdministrator: 1, + DataPart1: 2, + DataPart2: 3, + }, + }, + }, + expected: false, + }, } for _, test := range tests {