From 4df9a4b4e598de38f9e5ea79f19b51f34de1bb07 Mon Sep 17 00:00:00 2001 From: Daniel Czerwonk <daniel@dan-nrw.de> Date: Mon, 25 Jun 2018 18:53:12 +0200 Subject: [PATCH] bgp communities -> []uint32 --- protocols/bgp/packet/path_attributes.go | 18 ------------------ protocols/bgp/server/fsm_established.go | 2 +- protocols/bgp/server/update_helper.go | 7 +++---- route/bgp.go | 2 +- .../filter/actions/add_community_action.go | 6 +----- routingtable/filter/community_filter.go | 16 ++++++++-------- routingtable/update_helper.go | 15 +-------------- 7 files changed, 15 insertions(+), 51 deletions(-) diff --git a/protocols/bgp/packet/path_attributes.go b/protocols/bgp/packet/path_attributes.go index acd5c78a..b62aacda 100644 --- a/protocols/bgp/packet/path_attributes.go +++ b/protocols/bgp/packet/path_attributes.go @@ -690,24 +690,6 @@ func LargeCommunityAttributeForString(s string) (*PathAttribute, error) { }, nil } -func CommunityAttributeForString(s string) (*PathAttribute, error) { - strs := strings.Split(s, " ") - coms := make([]uint32, len(strs)) - - var err error - for i, str := range strs { - coms[i], err = ParseCommunityString(str) - if err != nil { - return nil, err - } - } - - return &PathAttribute{ - TypeCode: CommunitiesAttr, - Value: coms, - }, nil -} - func isBeginOfASSet(asPathPart string) bool { return strings.Contains(asPathPart, "(") } diff --git a/protocols/bgp/server/fsm_established.go b/protocols/bgp/server/fsm_established.go index 66480407..c1507eb5 100644 --- a/protocols/bgp/server/fsm_established.go +++ b/protocols/bgp/server/fsm_established.go @@ -233,7 +233,7 @@ func (s *establishedState) updates(u *packet.BGPUpdate) { path.BGPPath.ASPath = pa.ASPathString() path.BGPPath.ASPathLen = pa.ASPathLen() case packet.CommunitiesAttr: - path.BGPPath.Communities = pa.CommunityString() + path.BGPPath.Communities = pa.Value.([]uint32) case packet.LargeCommunitiesAttr: path.BGPPath.LargeCommunities = pa.LargeCommunityString() } diff --git a/protocols/bgp/server/update_helper.go b/protocols/bgp/server/update_helper.go index c4518180..393df4e9 100644 --- a/protocols/bgp/server/update_helper.go +++ b/protocols/bgp/server/update_helper.go @@ -49,11 +49,10 @@ func addOptionalPathAttribues(p *route.Path, parent *packet.PathAttribute) error current := parent if len(p.BGPPath.Communities) > 0 { - communities, err := packet.CommunityAttributeForString(p.BGPPath.Communities) - if err != nil { - return fmt.Errorf("Could not create communities attribute: %v", err) + communities := &packet.PathAttribute{ + TypeCode: packet.CommunitiesAttr, + Value: p.BGPPath.Communities, } - current.Next = communities current = communities } diff --git a/route/bgp.go b/route/bgp.go index 80aebdfe..3beb6b56 100644 --- a/route/bgp.go +++ b/route/bgp.go @@ -21,7 +21,7 @@ type BGPPath struct { EBGP bool BGPIdentifier uint32 Source uint32 - Communities string + Communities []uint32 LargeCommunities string } diff --git a/routingtable/filter/actions/add_community_action.go b/routingtable/filter/actions/add_community_action.go index 01ad05f6..c24107e8 100644 --- a/routingtable/filter/actions/add_community_action.go +++ b/routingtable/filter/actions/add_community_action.go @@ -1,10 +1,7 @@ package actions import ( - "strings" - "github.com/bio-routing/bio-rd/net" - "github.com/bio-routing/bio-rd/protocols/bgp/packet" "github.com/bio-routing/bio-rd/route" ) @@ -26,9 +23,8 @@ func (a *AddCommunityAction) Do(p net.Prefix, pa *route.Path) (modPath *route.Pa modified := pa.Copy() for _, com := range a.communities { - modified.BGPPath.Communities = modified.BGPPath.Communities + " " + packet.CommunityStringForUint32(com) + modified.BGPPath.Communities = append(modified.BGPPath.Communities, com) } - modified.BGPPath.Communities = strings.TrimLeft(modified.BGPPath.Communities, " ") return modified, false } diff --git a/routingtable/filter/community_filter.go b/routingtable/filter/community_filter.go index 9d384e54..b07d4b33 100644 --- a/routingtable/filter/community_filter.go +++ b/routingtable/filter/community_filter.go @@ -1,15 +1,15 @@ package filter -import ( - "strings" - - "github.com/bio-routing/bio-rd/protocols/bgp/packet" -) - type CommunityFilter struct { community uint32 } -func (f *CommunityFilter) Matches(communityString string) bool { - return strings.Contains(communityString, packet.CommunityStringForUint32(f.community)) +func (f *CommunityFilter) Matches(coms []uint32) bool { + for _, com := range coms { + if com == f.community { + return true + } + } + + return false } diff --git a/routingtable/update_helper.go b/routingtable/update_helper.go index c18936f3..6709bd43 100644 --- a/routingtable/update_helper.go +++ b/routingtable/update_helper.go @@ -1,12 +1,9 @@ package routingtable import ( - "strings" - "github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/protocols/bgp/packet" "github.com/bio-routing/bio-rd/route" - log "github.com/sirupsen/logrus" ) // ShouldPropagateUpdate performs some default checks and returns if an route update should be propagated to a neighbor @@ -32,17 +29,7 @@ func isDisallowedByCommunity(p *route.Path, n *Neighbor) bool { return false } - strs := strings.Split(p.BGPPath.Communities, " ") - for _, str := range strs { - com, err := packet.ParseCommunityString(str) - if err != nil { - log.WithField("Sender", "routingtable.ShouldAnnounce()"). - WithField("community", str). - WithError(err). - Error("Could not parse community") - continue - } - + for _, com := range p.BGPPath.Communities { if (com == packet.WellKnownCommunityNoExport && !n.IBGP) || com == packet.WellKnownCommunityNoAdvertise { return true } -- GitLab