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

bio-rd BGP configuration refactoring #455

Merged
merged 22 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b109345
add dockerfile
czerwonk Nov 29, 2023
5d8f84d
add dockerfile
czerwonk Dec 4, 2023
0a2329d
prepare cleanup of daemon config
czerwonk Dec 4, 2023
bdcc3a0
improve readability in bgp.go
czerwonk Dec 5, 2023
1330851
add AFI configuration following the internal structure
czerwonk Dec 5, 2023
8031d54
do not assume legacy IP session
czerwonk Dec 5, 2023
1cf35b8
improve readability of filter replacements
czerwonk Dec 5, 2023
00111ea
extract magic figure DefaultReconnectInterval
czerwonk Dec 6, 2023
c7e388b
support AdvertiseIPv4MultiProtocol setting in config
czerwonk Dec 6, 2023
8b1ccf3
simplify BGP session configuration code, respect configured VRF
czerwonk Dec 7, 2023
6b09b9a
improve configuration error messages
czerwonk Dec 7, 2023
fc7c203
inherit multipath and routing instance
czerwonk Dec 7, 2023
310eaa3
move responsibility to inherit filter chain to config package, add in…
czerwonk Dec 7, 2023
d50607a
run unprivileged
czerwonk Dec 7, 2023
291c87f
add missing configuration for route reflection
czerwonk Dec 7, 2023
9e8742c
add tests for loading group
czerwonk Dec 7, 2023
87853b5
parse BGP instead of BGPGroup in test to increase coverage
czerwonk Dec 8, 2023
4105284
cover add_path configuration
czerwonk Dec 8, 2023
dbd29e3
cover disabled
czerwonk Dec 8, 2023
0b33ddd
improve readability for deconfigureRemovedSessions
czerwonk Dec 8, 2023
5cc0ae0
move deconfigure to last step
czerwonk Dec 8, 2023
fe6445c
add cluster id to group, test overriding group cluster id
czerwonk Dec 10, 2023
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
222 changes: 222 additions & 0 deletions cmd/bio-rd/bgp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package main

import (
"fmt"
"time"

"github.com/bio-routing/bio-rd/cmd/bio-rd/config"
bgpserver "github.com/bio-routing/bio-rd/protocols/bgp/server"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/vrf"
)

const (
DefaultReconnectInterval = time.Second * 15
)

type bgpConfigurator struct {
srv bgpserver.BGPServer
vrfReg *vrf.VRFRegistry
}

func (c *bgpConfigurator) configure(cfg *config.BGP) error {
for _, bg := range cfg.Groups {
for _, bn := range bg.Neighbors {
err := c.configureSession(bn, bg)
if err != nil {
return fmt.Errorf("could not configure session for neighbor %s: %w", bn.PeerAddress, err)
}
}
}

c.deconfigureRemovedSessions(cfg)

return nil
}

func (c *bgpConfigurator) configureSession(bn *config.BGPNeighbor, bg *config.BGPGroup) error {
v, err := c.determineVRF(bn, bg)
if err != nil {
return fmt.Errorf("could not determine VRF: %w", err)
}

newCfg := c.newPeerConfig(bn, bg, v)
oldCfg := c.srv.GetPeerConfig(v, bn.PeerAddressIP)
if oldCfg != nil {
return c.reconfigureModifiedSession(bn, bg, newCfg, oldCfg)
}

err = c.srv.AddPeer(*newCfg)
if err != nil {
return fmt.Errorf("unable to add BGP peer: %w", err)
}

return nil
}

func (c *bgpConfigurator) deconfigureRemovedSessions(cfg *config.BGP) {
for _, p := range c.srv.GetPeers() {
if !c.peerExistsInConfig(cfg, p) {
c.srv.DisposePeer(p.VRF(), p.Addr())
}
}
}

func (c *bgpConfigurator) peerExistsInConfig(cfg *config.BGP, p bgpserver.PeerKey) bool {
for _, bg := range cfg.Groups {
for _, bn := range bg.Neighbors {
v, _ := c.determineVRF(bn, bg)
if bn.PeerAddressIP == p.Addr() && p.VRF() == v {
return true
}
}
}

return false
}

func (c *bgpConfigurator) reconfigureModifiedSession(bn *config.BGPNeighbor, bg *config.BGPGroup, newCfg, oldCfg *bgpserver.PeerConfig) error {
if oldCfg.NeedsRestart(newCfg) {
return c.replaceSession(newCfg, oldCfg)
}

err := c.srv.ReplaceImportFilterChain(
newCfg.VRF,
bn.PeerAddressIP,
bn.ImportFilterChain)
if err != nil {
return fmt.Errorf("could not replace import filter: %w", err)
}

err = c.srv.ReplaceExportFilterChain(
newCfg.VRF,
bn.PeerAddressIP,
bn.ExportFilterChain)
if err != nil {
return fmt.Errorf("could not replace export filter: %w", err)
}

return nil
}

func (c *bgpConfigurator) replaceSession(newCfg, oldCfg *bgpserver.PeerConfig) error {
c.srv.DisposePeer(oldCfg.VRF, oldCfg.PeerAddress)
err := c.srv.AddPeer(*newCfg)
if err != nil {
return fmt.Errorf("unable to reconfigure BGP peer: %w", err)
}

return nil
}

func (c *bgpConfigurator) determineVRF(bn *config.BGPNeighbor, bg *config.BGPGroup) (*vrf.VRF, error) {
if len(bn.RoutingInstance) > 0 {
return c.vrfByName(bn.RoutingInstance)
}

if len(bg.RoutingInstance) > 0 {
return c.vrfByName(bg.RoutingInstance)
}

return bgpSrv.GetDefaultVRF(), nil
}

func (c *bgpConfigurator) vrfByName(name string) (*vrf.VRF, error) {
v := c.vrfReg.GetVRFByName(name)
if v == nil {
return nil, fmt.Errorf("could not find VRF for name %s", name)
}

return v, nil
}

func (c *bgpConfigurator) newPeerConfig(bn *config.BGPNeighbor, bg *config.BGPGroup, vrf *vrf.VRF) *bgpserver.PeerConfig {
p := &bgpserver.PeerConfig{
AdminEnabled: !bn.Disabled,
AuthenticationKey: bn.AuthenticationKey,
LocalAS: bn.LocalAS,
PeerAS: bn.PeerAS,
PeerAddress: bn.PeerAddressIP,
LocalAddress: bn.LocalAddressIP,
TTL: bn.TTL,
ReconnectInterval: DefaultReconnectInterval,
HoldTime: bn.HoldTimeDuration,
KeepAlive: bn.HoldTimeDuration / 3,
RouterID: c.srv.RouterID(),
VRF: vrf,
AdvertiseIPv4MultiProtocol: bn.AdvertiseIPv4MultiProtocol,
}

c.configureIPv4(bn, bg, p)
c.configureIPv6(bn, bg, p)

if bn.Passive != nil {
p.Passive = *bn.Passive
}

if bn.RouteServerClient != nil {
p.RouteServerClient = *bn.RouteServerClient
}

if bn.RouteReflectorClient != nil {
p.RouteReflectorClient = *bn.RouteReflectorClient
}

if bn.ClusterIDIP != nil {
p.RouteReflectorClusterID = bn.ClusterIDIP.ToUint32()
}

return p
}

func (c *bgpConfigurator) configureIPv4(bn *config.BGPNeighbor, bg *config.BGPGroup, p *bgpserver.PeerConfig) {
if !bn.PeerAddressIP.IsIPv4() && bn.IPv4 == nil {
return
}

p.IPv4 = c.newAFIConfig(bn, bg)

if bn.IPv4 != nil {
c.configureAddressFamily(bn.IPv4, p.IPv4)
}
}

func (c *bgpConfigurator) configureIPv6(bn *config.BGPNeighbor, bg *config.BGPGroup, p *bgpserver.PeerConfig) {
if bn.PeerAddressIP.IsIPv4() && bn.IPv6 == nil {
return
}

p.IPv6 = c.newAFIConfig(bn, bg)

if bn.IPv6 != nil {
c.configureAddressFamily(bn.IPv6, p.IPv6)
}
}

func (c *bgpConfigurator) newAFIConfig(bn *config.BGPNeighbor, bg *config.BGPGroup) *bgpserver.AddressFamilyConfig {
return &bgpserver.AddressFamilyConfig{
ImportFilterChain: bn.ImportFilterChain,
ExportFilterChain: bn.ExportFilterChain,
AddPathSend: routingtable.ClientOptions{
BestOnly: true,
},
AddPathRecv: false,
}
}

func (c *bgpConfigurator) configureAddressFamily(baf *config.AddressFamilyConfig, af *bgpserver.AddressFamilyConfig) {
if baf.AddPath != nil {
c.configureAddPath(baf.AddPath, af)
}
}

func (c *bgpConfigurator) configureAddPath(bac *config.AddPathConfig, af *bgpserver.AddressFamilyConfig) {
af.AddPathRecv = bac.Receive

if bac.Send == nil {
return
}

af.AddPathSend.BestOnly = !bac.Send.Multipath
af.AddPathSend.MaxPaths = uint(bac.Send.PathCount)
}
Loading
Loading