Skip to content

Commit

Permalink
grpc/bgp listen in netns
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Peim committed Oct 3, 2024
1 parent 313096d commit e361597
Show file tree
Hide file tree
Showing 13 changed files with 1,213 additions and 875 deletions.
2 changes: 1 addition & 1 deletion api/attribute.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/capability.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,733 changes: 876 additions & 857 deletions api/gobgp.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/gobgp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ message Transport {
uint32 remote_port = 6;
uint32 tcp_mss = 7;
string bind_interface = 8;
string netns = 9;
}

message RouteServer {
Expand Down Expand Up @@ -1108,6 +1109,7 @@ message Global {
GracefulRestart graceful_restart = 10;
ApplyPolicy apply_policy = 11;
string bind_to_device = 12;
string netns = 13;
}

message Confederation {
Expand Down
4 changes: 3 additions & 1 deletion cmd/gobgp/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,7 @@ func showGlobalConfig() error {
fmt.Println("AS: ", g.Asn)
fmt.Println("Router-ID:", g.RouterId)
if len(g.ListenAddresses) > 0 {
fmt.Printf("Listening Port: %d, Addresses: %s\n", g.ListenPort, strings.Join(g.ListenAddresses, ", "))
fmt.Printf("Listening Port: %d, Addresses: %s, NetNs: %s\n", g.ListenPort, strings.Join(g.ListenAddresses, ", "), g.Netns)
}
if g.UseMultiplePaths {
fmt.Printf("Multipath: enabled")
Expand All @@ -2263,6 +2263,7 @@ func modGlobalConfig(args []string) error {
"router-id": paramSingle,
"listen-port": paramSingle,
"listen-addresses": paramList,
"netns": paramSingle,
"use-multipath": paramFlag})
if err != nil || len(m["as"]) != 1 || len(m["router-id"]) != 1 {
return fmt.Errorf("usage: gobgp global as <VALUE> router-id <VALUE> [use-multipath] [listen-port <VALUE>] [listen-addresses <VALUE>...]")
Expand Down Expand Up @@ -2294,6 +2295,7 @@ func modGlobalConfig(args []string) error {
RouterId: id.String(),
ListenPort: int32(port),
ListenAddresses: m["listen-addresses"],
Netns: m["netns"][0],
UseMultiplePaths: useMultipath,
},
})
Expand Down
6 changes: 5 additions & 1 deletion cmd/gobgp/neighbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,12 @@ func showNeighbor(args []string) error {
} else {
fmt.Print("\n")
}
netns := "default"
if p.Transport.Netns != "" {
netns = p.Transport.Netns
}
fmt.Printf(" BGP OutQ = %d, Flops = %d\n", p.State.Queues.Output, p.State.Flops)
fmt.Printf(" Local address is %s, local ASN: %s\n", p.Transport.LocalAddress, getLocalASN(p))
fmt.Printf(" Local address is %s in %s netns, local ASN: %s\n", p.Transport.LocalAddress, netns, getLocalASN(p))
fmt.Printf(" Hold time is %d, keepalive interval is %d seconds\n", int(p.Timers.State.NegotiatedHoldTime), int(p.Timers.State.KeepaliveInterval))
fmt.Printf(" Configured hold time is %d, keepalive interval is %d seconds\n", int(p.Timers.Config.HoldTime), int(p.Timers.Config.KeepaliveInterval))

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
github.com/vishvananda/netlink v1.2.1
github.com/vishvananda/netns v0.0.4
golang.org/x/sys v0.25.0
golang.org/x/text v0.14.0
golang.org/x/time v0.3.0
Expand Down Expand Up @@ -49,7 +50,6 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/x/net v0.23.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/oc/bgp_configs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 32 additions & 11 deletions pkg/server/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func (h *fsmHandler) connectLoop(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
fsm := h.fsm

retry, addr, port, password, ttl, ttlMin, mss, localAddress, localPort, bindInterface := func() (int, string, int, string, uint8, uint8, uint16, string, int, string) {
retry, addr, port, password, ttl, ttlMin, mss, localAddress, localPort, netNs, bindInterface := func() (int, string, int, string, uint8, uint8, uint16, string, int, string, string) {
fsm.lock.RLock()
defer fsm.lock.RUnlock()

Expand All @@ -521,7 +521,7 @@ func (h *fsmHandler) connectLoop(ctx context.Context, wg *sync.WaitGroup) {
ttl = fsm.pConf.EbgpMultihop.Config.MultihopTtl
}
}
return tick, addr, port, password, ttl, ttlMin, fsm.pConf.Transport.Config.TcpMss, fsm.pConf.Transport.Config.LocalAddress, int(fsm.pConf.Transport.Config.LocalPort), fsm.pConf.Transport.Config.BindInterface
return tick, addr, port, password, ttl, ttlMin, fsm.pConf.Transport.Config.TcpMss, fsm.pConf.Transport.Config.LocalAddress, int(fsm.pConf.Transport.Config.LocalPort), fsm.gConf.Config.Netns, fsm.pConf.Transport.Config.BindInterface
}()

tick := minConnectRetryInterval
Expand All @@ -545,15 +545,36 @@ func (h *fsmHandler) connectLoop(ctx context.Context, wg *sync.WaitGroup) {
}
}

laddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(localAddress, strconv.Itoa(localPort)))
if err != nil {
fsm.logger.Warn("failed to resolve local address",
func() {
cleanNs, err := NsEnter(netNs)
if err != nil {
fsm.logger.Warn("failed to enter netns",
log.Fields{
"Topic": "Peer",
"Key": addr,
"Namspace": netNs,
"Error": err})
tick = retry
return
}
defer cleanNs()

laddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(localAddress, strconv.Itoa(localPort)))
if err != nil {
fsm.logger.Warn("failed to resolve local address",
log.Fields{
"Topic": "Peer",
"Key": addr})
tick = retry
return
}

fsm.logger.Warn("dialing",
log.Fields{
"Topic": "Peer",
"Key": addr})
}
"Topic": "Peer",
"Key": addr,
"LocalAddress": laddr.String()})

if err == nil {
d := net.Dialer{
LocalAddr: laddr,
Timeout: time.Duration(tick-1) * time.Second,
Expand Down Expand Up @@ -593,8 +614,8 @@ func (h *fsmHandler) connectLoop(ctx context.Context, wg *sync.WaitGroup) {
"Error": err})
}
}
}
tick = retry
tick = retry
}()
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/server/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ func newNeighborFromAPIStruct(a *api.Peer) (*oc.Neighbor, error) {
pconf.Transport.Config.LocalPort = uint16(a.Transport.LocalPort)
pconf.Transport.Config.BindInterface = a.Transport.BindInterface
pconf.Transport.Config.TcpMss = uint16(a.Transport.TcpMss)
pconf.Transport.Config.Netns = a.Transport.Netns
}
if a.EbgpMultihop != nil {
pconf.EbgpMultihop.Config.Enabled = a.EbgpMultihop.Enabled
Expand Down Expand Up @@ -1908,6 +1909,7 @@ func newGlobalFromAPIStruct(a *api.Global) *oc.Global {
RouterId: a.RouterId,
Port: a.ListenPort,
LocalAddressList: a.ListenAddresses,
Netns: a.Netns,
},
ApplyPolicy: *applyPolicy,
AfiSafis: families,
Expand Down
Loading

0 comments on commit e361597

Please sign in to comment.