From 898b4123fbcad646de1aad2beab16de4819d027f Mon Sep 17 00:00:00 2001 From: Nate Sales Date: Sun, 4 Jul 2021 18:37:21 -0700 Subject: [PATCH] add remove-communities attribute --- config.go | 22 ++++++++++++++++++++++ templates/peer.tmpl | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/config.go b/config.go index c15c8c47..55cdb2a5 100644 --- a/config.go +++ b/config.go @@ -63,6 +63,7 @@ type peer struct { ImportCommunities *[]string `yaml:"import-communities" description:"List of communities to add to all imported routes" default:"-"` ExportCommunities *[]string `yaml:"export-communities" description:"List of communities to add to all exported routes" default:"-"` AnnounceCommunities *[]string `yaml:"announce-communities" description:"Announce all routes matching these communities to the peer" default:"-"` + RemoveCommunities *[]string `yaml:"remove-communities" description:"List of communities to remove before from routes announced by this peer" default:"-"` // Filtering ASSet *string `yaml:"as-set" description:"Peer's as-set for filtering" default:"-"` @@ -115,6 +116,8 @@ type peer struct { ExportLargeCommunities *[]string `yaml:"-" description:"-" default:"-"` AnnounceStandardCommunities *[]string `yaml:"-" description:"-" default:"-"` AnnounceLargeCommunities *[]string `yaml:"-" description:"-" default:"-"` + RemoveStandardCommunities *[]string `yaml:"-" description:"-" default:"-"` + RemoveLargeCommunities *[]string `yaml:"-" description:"-" default:"-"` } type vrrpInstance struct { @@ -429,6 +432,25 @@ func loadConfig(configBlob []byte) (*config, error) { } } } + if peerData.RemoveCommunities != nil { + for _, community := range *peerData.RemoveCommunities { + communityType := categorizeCommunity(community) + + if communityType == "standard" { + if peerData.RemoveStandardCommunities == nil { + peerData.RemoveStandardCommunities = &[]string{} + } + *peerData.RemoveStandardCommunities = append(*peerData.RemoveStandardCommunities, community) + } else if communityType == "large" { + if peerData.RemoveLargeCommunities == nil { + peerData.RemoveLargeCommunities = &[]string{} + } + *peerData.RemoveLargeCommunities = append(*peerData.RemoveLargeCommunities, strings.ReplaceAll(community, ":", ",")) + } else { + return nil, errors.New("Invalid remove community: " + community) + } + } + } // Check for no originated prefixes but announce-originated enabled if len(c.Prefixes) < 1 && *peerData.AnnounceOriginated { diff --git a/templates/peer.tmpl b/templates/peer.tmpl index bad7d332..9e4b36fa 100644 --- a/templates/peer.tmpl +++ b/templates/peer.tmpl @@ -55,6 +55,13 @@ protocol bgp {{ UniqueProtocolName $peer.ProtocolName $af }} { {{ if BoolDeref $peer.EnforcePeerNexthop }}enforce_peer_nexthop({{ $neighbor }});{{ end }} {{ if BoolDeref $peer.FilterTransitASNs }}reject_transit_paths();{{ end }} + {{ range $i, $pattern := StringSliceIter $peer.RemoveStandardCommunities }} + bgp_community.delete([({{ $pattern }})]); + {{ end }} + {{ range $i, $pattern := StringSliceIter $peer.RemoveLargeCommunities }} + bgp_large_community.delete([({{ $pattern }})]); + {{ end }} + {{ if BoolDeref $peer.AllowBlackholeCommunity }}process_blackholes();{{ end }} bgp_local_pref = {{ $peer.LocalPref }};