Files
gocast/controller/bgp.go
Ben Roberts 567a84095e Implement support for multiple BGP peers
The BGP controller now supports announcing routes to multiple BGP peers for redundancy and resilience. If one peer fails, route announcements continue to succeed for other healthy peers.

```yaml
bgp:
  local_as: 12345
  local_ip: 192.168.1.100  # optional
  peers:
    - peer_ip: 10.10.10.1
      peer_as: 6789
      communities:        # per-peer communities (optional)
        - 100:100
    - peer_ip: 10.10.10.2
      peer_as: 6789
      communities:
        - 100:101
      multi_hop: true     # optional, defaults to true for eBGP
  communities:            # global communities applied to all peers
    - 1000:1000
  origin: igp
```

```yaml
bgp:
  local_as: 12345
  peer_as: 6789
  peer_ip: 10.10.10.1
  communities:
    - 100:100
  origin: igp
```

Legacy configurations are automatically converted to the new format internally, ensuring backward compatibility.

Routes are announced to all configured peers. If announcement to one peer fails, the operation continues for other peers. Errors are aggregated and returned, but partial success is allowed.

Communities are merged in the following order:
1. **Global communities** (defined at `bgp.communities`)
2. **Per-peer communities** (defined at `bgp.peers[].communities`)
3. **Per-route communities** (defined at `apps[].vip_config.bgp_communities`)

Example: If global communities are `[1000:1000]`, peer communities are `[100:100]`, and route communities are `[5000:5000]`, the announced route will have all three: `[1000:1000, 100:100, 5000:5000]`.

- **Default behavior**: Multi-hop is disabled by default
- **Enable**: Set `multi_hop: true` per peer to explicitly enable multi-hop BGP

The `/info` endpoint now returns an array of peer information instead of a single peer object:

**Before:**
```json
{
  "conf": {
    "neighbor_address": "10.10.10.1",
    "peer_as": 6789
  },
  "state": {...}
}
```

**After:**
```json
[
  {
    "conf": {
      "neighbor_address": "10.10.10.1",
      "peer_as": 6789
    },
    "state": {...}
  },
  {
    "conf": {
      "neighbor_address": "10.10.10.2",
      "peer_as": 6789
    },
    "state": {...}
  }
]
```

- `config/config.go`: Added `PeerConfig` struct and `Peers` slice to `BgpConfig`
- `controller/bgp.go`: Refactored to support multiple peers with best-effort semantics
- `controller/monitor.go`: Updated `GetInfo()` to return slice of peers
- `server/server.go`: Updated info handler to return array of peers

1. **Controller struct** now stores `[]PeerConfig` instead of single peer fields
2. **Announce/Withdraw** methods loop through all peers with error aggregation
3. **getApiPath** accepts a `PeerConfig` parameter for per-peer community merging
4. **addPeer** determines multi-hop settings per peer
5. **PeerInfo** returns information for all configured peers
6. **Shutdown** gracefully shuts down all peer sessions

The implementation includes comprehensive test coverage:

1. **TestLegacyConfigConversion** - Verifies backward compatibility by testing that legacy single-peer configs are automatically converted to multi-peer format
2. **TestMultiPeerConfig** - Tests that new multi-peer configurations are properly loaded with multiple peers
3. **TestNoPeersConfigError** - Ensures proper error handling when no peers are configured
4. **TestCommunityMerging** - Validates that global, per-peer, and per-route communities are correctly merged in order
5. **TestMultiHopConfiguration** - Tests multi-hop BGP settings with various scenarios:
   - Default behavior (multi-hop disabled)
   - Explicit multi-hop disable
   - Explicit multi-hop enable
6. **TestBestEffortAnnouncement** - Verifies that announcements succeed even when individual peers may have issues
7. **TestWithdrawMultiplePeers** - Tests route withdrawal across multiple peers
8. **TestPeerInfoMultiplePeers** - Validates that peer information is correctly returned for all configured peers

- **TestBgpNew** - Full integration test with actual BGP listeners (requires root, skipped in CI)
- **TestMultiPeerAnnouncement** - Tests actual route announcements to multiple BGP listeners (requires root, skipped in CI)

Existing configurations using `peer_ip` and `peer_as` continue to work without modification.

To add a second peer for resilience:

```yaml
bgp:
  local_as: 12345
  # Keep existing config for backward compatibility, or remove these lines
  # peer_as: 6789
  # peer_ip: 10.10.10.1

  # Add new multi-peer config
  peers:
    - peer_ip: 10.10.10.1
      peer_as: 6789
    - peer_ip: 10.10.10.2  # redundant peer
      peer_as: 6789
  communities:
    - 100:100
  origin: igp
```

All operations (Announce, Withdraw, Shutdown) use best-effort error handling:
- Operations continue even if individual peers fail
- Errors are collected and returned as aggregated error messages
- Format: `"announcement errors: [peer 10.10.10.1: error message, peer 10.10.10.2: error message]"`

These changes were authored via AI LLM.

Authored-By: Claude Code (Sonnet 4.5)
2026-06-17 15:52:43 +01:00

274 lines
6.6 KiB
Go

package controller
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
c "github.com/mayuresh82/gocast/config"
api "github.com/osrg/gobgp/api"
gobgp "github.com/osrg/gobgp/pkg/server"
)
type Route struct {
Net *net.IPNet
Communities []string
}
type Controller struct {
localAS int
localIP net.IP
peers []c.PeerConfig
communities []string
origin uint32
s *gobgp.BgpServer
}
func NewController(config c.BgpConfig) (*Controller, error) {
ctrl := &Controller{}
var gw net.IP
var err error
// Normalize config: convert legacy single-peer to new multi-peer format
peers := config.Peers
if len(peers) == 0 {
// Backward compatibility: convert legacy config
if config.PeerIP != "" {
// Explicit peer IP configured
peers = []c.PeerConfig{{
PeerIP: config.PeerIP,
PeerAS: config.PeerAS,
}}
} else {
// No peer IP configured - use default gateway
gw, err = gateway()
if err != nil {
return nil, fmt.Errorf("Unable to get gateway ip: %v", err)
}
peers = []c.PeerConfig{{
PeerIP: gw.String(),
PeerAS: config.PeerAS,
}}
}
}
// Determine local IP
if config.LocalIP == "" {
// Use first peer to determine local IP
firstPeerIP := net.ParseIP(peers[0].PeerIP)
if firstPeerIP == nil {
gw, err = gateway()
if err != nil {
return nil, fmt.Errorf("Unable to get gw ip: %v", err)
}
firstPeerIP = gw
}
gw, err = via(firstPeerIP)
if err != nil {
return nil, fmt.Errorf("Unable to get gw ip: %v", err)
}
ctrl.localIP, err = localAddress(gw)
if err != nil {
return nil, err
}
} else {
ctrl.localIP = net.ParseIP(config.LocalIP)
}
ctrl.localAS = config.LocalAS
ctrl.peers = peers
ctrl.communities = config.Communities
switch config.Origin {
case "igp":
ctrl.origin = 0
case "egp":
ctrl.origin = 1
case "unknown":
ctrl.origin = 2
}
s := gobgp.NewBgpServer()
go s.Serve()
if err := s.StartBgp(context.Background(), &api.StartBgpRequest{
Global: &api.Global{
As: uint32(config.LocalAS),
RouterId: ctrl.localIP.String(),
ListenPort: -1, // gobgp won't listen on tcp:179
},
}); err != nil {
return nil, fmt.Errorf("Unable to start bgp: %v", err)
}
ctrl.s = s
return ctrl, nil
}
func (c *Controller) addPeer(peer *c.PeerConfig) error {
n := &api.Peer{
Conf: &api.PeerConf{
NeighborAddress: peer.PeerIP,
PeerAs: uint32(peer.PeerAS),
},
}
// Enable multihop only if explicitly configured
if peer.MultiHop != nil && *peer.MultiHop {
n.EbgpMultihop = &api.EbgpMultihop{Enabled: true, MultihopTtl: uint32(255)}
}
return c.s.AddPeer(context.Background(), &api.AddPeerRequest{Peer: n})
}
func (c *Controller) getApiPath(route *Route, peer *c.PeerConfig) *api.Path {
afi := api.Family_AFI_IP
if route.Net.IP.To4() == nil {
afi = api.Family_AFI_IP6
}
prefixlen, _ := route.Net.Mask.Size()
nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{
Prefix: route.Net.IP.String(),
PrefixLen: uint32(prefixlen),
})
a1, _ := ptypes.MarshalAny(&api.OriginAttribute{
Origin: c.origin,
})
a2, _ := ptypes.MarshalAny(&api.NextHopAttribute{
NextHop: c.localIP.String(),
})
// Merge communities: global + per-peer + per-route
var allCommunities []string
allCommunities = append(allCommunities, c.communities...)
allCommunities = append(allCommunities, peer.Communities...)
allCommunities = append(allCommunities, route.Communities...)
var communities []uint32
for _, comm := range allCommunities {
communities = append(communities, convertCommunity(comm))
}
a3, _ := ptypes.MarshalAny(&api.CommunitiesAttribute{
Communities: communities,
})
attrs := []*any.Any{a1, a2, a3}
return &api.Path{
Family: &api.Family{Afi: afi, Safi: api.Family_SAFI_UNICAST},
Nlri: nlri,
Pattrs: attrs,
}
}
func (c *Controller) Announce(route *Route) error {
var errs []error
for i := range c.peers {
peer := &c.peers[i]
// Check if peer exists
var found bool
err := c.s.ListPeer(context.Background(), &api.ListPeerRequest{}, func(p *api.Peer) {
if p.Conf.NeighborAddress == peer.PeerIP {
found = true
}
})
if err != nil {
errs = append(errs, fmt.Errorf("peer %s: list error: %v", peer.PeerIP, err))
continue
}
// Add peer if not found
if !found {
if err := c.addPeer(peer); err != nil {
errs = append(errs, fmt.Errorf("peer %s: add error: %v", peer.PeerIP, err))
continue
}
}
// Announce route to this peer
path := c.getApiPath(route, peer)
if _, err := c.s.AddPath(context.Background(), &api.AddPathRequest{Path: path}); err != nil {
errs = append(errs, fmt.Errorf("peer %s: announce error: %v", peer.PeerIP, err))
continue
}
}
// Return aggregated errors if any peer failed
if len(errs) > 0 {
return fmt.Errorf("announcement errors: %v", errs)
}
return nil
}
func (c *Controller) Withdraw(route *Route) error {
var errs []error
for i := range c.peers {
peer := &c.peers[i]
path := c.getApiPath(route, peer)
if err := c.s.DeletePath(context.Background(), &api.DeletePathRequest{Path: path}); err != nil {
errs = append(errs, fmt.Errorf("peer %s: withdraw error: %v", peer.PeerIP, err))
continue
}
}
// Return aggregated errors if any peer failed
if len(errs) > 0 {
return fmt.Errorf("withdrawal errors: %v", errs)
}
return nil
}
func (c *Controller) PeerInfo() ([]*api.Peer, error) {
var peers []*api.Peer
peerMap := make(map[string]bool)
// Build map of configured peer IPs
for _, peer := range c.peers {
peerMap[peer.PeerIP] = true
}
// Collect info for all configured peers
err := c.s.ListPeer(context.Background(), &api.ListPeerRequest{}, func(p *api.Peer) {
if peerMap[p.Conf.NeighborAddress] {
peers = append(peers, p)
}
})
if err != nil {
return nil, err
}
return peers, nil
}
func (c *Controller) Shutdown() error {
var errs []error
// Shutdown all peer sessions
for _, peer := range c.peers {
if err := c.s.ShutdownPeer(context.Background(), &api.ShutdownPeerRequest{
Address: peer.PeerIP,
}); err != nil {
errs = append(errs, fmt.Errorf("peer %s: shutdown error: %v", peer.PeerIP, err))
}
}
// Stop BGP server
if err := c.s.StopBgp(context.Background(), &api.StopBgpRequest{}); err != nil {
errs = append(errs, fmt.Errorf("stop bgp error: %v", err))
}
if len(errs) > 0 {
return fmt.Errorf("shutdown errors: %v", errs)
}
return nil
}
func convertCommunity(comm string) uint32 {
parts := strings.Split(comm, ":")
first, _ := strconv.ParseUint(parts[0], 10, 32)
second, _ := strconv.ParseUint(parts[1], 10, 32)
return uint32(first)<<16 | uint32(second)
}