From 2b6ebfa53131cb77553cfa616824aaf61773f771 Mon Sep 17 00:00:00 2001 From: Daniel Czerwonk <daniel@dan-nrw.de> Date: Sun, 20 May 2018 00:42:55 +0200 Subject: [PATCH] added large community filter --- routingtable/filter/large_community_filter.go | 15 +++++ routingtable/filter/term_condition.go | 21 ++++++- routingtable/filter/term_condition_test.go | 56 ++++++++++++++++--- 3 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 routingtable/filter/large_community_filter.go diff --git a/routingtable/filter/large_community_filter.go b/routingtable/filter/large_community_filter.go new file mode 100644 index 00000000..0f9d474e --- /dev/null +++ b/routingtable/filter/large_community_filter.go @@ -0,0 +1,15 @@ +package filter + +import ( + "strings" + + "github.com/bio-routing/bio-rd/protocols/bgp/packet" +) + +type LargeCommunityFilter struct { + community *packet.LargeCommunity +} + +func (f *LargeCommunityFilter) Matches(communityString string) bool { + return strings.Contains(communityString, f.community.String()) +} diff --git a/routingtable/filter/term_condition.go b/routingtable/filter/term_condition.go index 92c3616a..946a04e8 100644 --- a/routingtable/filter/term_condition.go +++ b/routingtable/filter/term_condition.go @@ -6,12 +6,13 @@ import ( ) type TermCondition struct { - prefixLists []*PrefixList - routeFilters []*RouteFilter + prefixLists []*PrefixList + routeFilters []*RouteFilter + largeCommunityFilters []*LargeCommunityFilter } func (f *TermCondition) Matches(p net.Prefix, pa *route.Path) bool { - return f.matchesAnyPrefixList(p) || f.machtchesAnyRouteFilter(p) + return f.matchesAnyPrefixList(p) || f.machtchesAnyRouteFilter(p) || f.machtchesAnyLageCommunityFilter(pa) } func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool { @@ -33,3 +34,17 @@ func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool { return false } + +func (t *TermCondition) machtchesAnyLageCommunityFilter(pa *route.Path) bool { + if pa.BGPPath == nil { + return false + } + + for _, l := range t.largeCommunityFilters { + if l.Matches(pa.BGPPath.LargeCommunities) { + return true + } + } + + return false +} diff --git a/routingtable/filter/term_condition_test.go b/routingtable/filter/term_condition_test.go index 876746a2..307cd5e0 100644 --- a/routingtable/filter/term_condition_test.go +++ b/routingtable/filter/term_condition_test.go @@ -4,17 +4,20 @@ import ( "testing" "github.com/bio-routing/bio-rd/net" + "github.com/bio-routing/bio-rd/protocols/bgp/packet" "github.com/bio-routing/bio-rd/route" "github.com/stretchr/testify/assert" ) func TestMatches(t *testing.T) { tests := []struct { - name string - prefix net.Prefix - prefixLists []*PrefixList - routeFilters []*RouteFilter - expected bool + name string + prefix net.Prefix + bgpPath *route.BGPPath + prefixLists []*PrefixList + routeFilters []*RouteFilter + largeCommunityFilters []*LargeCommunityFilter + expected bool }{ { name: "one prefix matches in prefix list, no route filters set", @@ -105,16 +108,53 @@ func TestMatches(t *testing.T) { }, expected: true, }, + { + name: "large community matches", + prefix: net.NewPfx(strAddr("10.0.0.0"), 24), + bgpPath: &route.BGPPath{ + LargeCommunities: "(1,2,0) (1,2,3)", + }, + largeCommunityFilters: []*LargeCommunityFilter{ + { + &packet.LargeCommunity{ + GlobalAdministrator: 1, + DataPart1: 2, + DataPart2: 3, + }, + }, + }, + expected: true, + }, + { + name: "large community does not match", + prefix: net.NewPfx(strAddr("10.0.0.0"), 24), + bgpPath: &route.BGPPath{}, + largeCommunityFilters: []*LargeCommunityFilter{ + { + &packet.LargeCommunity{ + GlobalAdministrator: 1, + DataPart1: 2, + DataPart2: 3, + }, + }, + }, + expected: false, + }, } for _, test := range tests { t.Run(test.name, func(te *testing.T) { f := &TermCondition{ - prefixLists: test.prefixLists, - routeFilters: test.routeFilters, + prefixLists: test.prefixLists, + routeFilters: test.routeFilters, + largeCommunityFilters: test.largeCommunityFilters, + } + + pa := &route.Path{ + BGPPath: test.bgpPath, } - assert.Equal(te, test.expected, f.Matches(test.prefix, &route.Path{})) + assert.Equal(te, test.expected, f.Matches(test.prefix, pa)) }) } } -- GitLab