Skip to content

Commit

Permalink
Make reuseport optional
Browse files Browse the repository at this point in the history
  • Loading branch information
netixx committed Apr 18, 2024
1 parent d4f72cd commit e9d95a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
15 changes: 8 additions & 7 deletions net/tcp/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type ListenerFactoryI interface {
NewListener(v *vrf.VRF, laddr *net.TCPAddr, ttl uint8) (ListenerI, error)
NewListener(v *vrf.VRF, laddr *net.TCPAddr, ttl uint8, reuseport bool) (ListenerI, error)
}

type ListenerFactory struct{}
Expand All @@ -30,7 +30,7 @@ type Listener struct {
}

// NewListener starts a TCPListener
func (lf *ListenerFactory) NewListener(v *vrf.VRF, laddr *net.TCPAddr, ttl uint8) (ListenerI, error) {
func (lf *ListenerFactory) NewListener(v *vrf.VRF, laddr *net.TCPAddr, ttl uint8, reuseport bool) (ListenerI, error) {
l := &Listener{
laddr: laddr,
}
Expand All @@ -57,13 +57,14 @@ func (lf *ListenerFactory) NewListener(v *vrf.VRF, laddr *net.TCPAddr, ttl uint8
err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
if err != nil {
unix.Close(fd)
return nil, fmt.Errorf("unable to get SO_REUSEADDR %w", err)
return nil, fmt.Errorf("unable to set SO_REUSEADDR %w", err)
}

err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
if err != nil {
unix.Close(fd)
return nil, fmt.Errorf("unable to get SO_REUSEPORT %w", err)
if reuseport {
if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
unix.Close(fd)
return nil, fmt.Errorf("unable to set SO_REUSEPORT %w", err)
}
}

if ttl != 0 {
Expand Down
6 changes: 4 additions & 2 deletions net/tcp/listener_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ type ListenerManager struct {
listenersByVRFmu sync.RWMutex
acceptCh chan ConnWithVRF
listenerFactory ListenerFactoryI
reusePort bool
}

func NewListenerManager(listenAddrsByVRF map[string][]string) *ListenerManager {
func NewListenerManager(listenAddrsByVRF map[string][]string, reusePort bool) *ListenerManager {
return &ListenerManager{
listenAddrsByVRF: listenAddrsByVRF,
listenersByVRF: make(map[string][]ListenerI),
listenerFactory: NewListenerFactory(),
acceptCh: make(chan ConnWithVRF),
reusePort: reusePort,
}
}

Expand Down Expand Up @@ -93,7 +95,7 @@ func (lm *ListenerManager) _addListener(vrf *vrf.VRF, addr string, ch chan ConnW
}

log.Infof("Listener manager: Starting TCP listener on %s in VRF %s", addr, vrf.Name())
l, err := lm.listenerFactory.NewListener(vrf, tcpaddr, 255)
l, err := lm.listenerFactory.NewListener(vrf, tcpaddr, 255, lm.reusePort)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion protocols/bgp/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type BGPServerConfig struct {

// Optional attributes
DefaultLocalPreference *uint32
ReusePort bool
}

type bgpServer struct {
Expand Down Expand Up @@ -68,7 +69,7 @@ func newBGPServer(config BGPServerConfig) *bgpServer {
server := &bgpServer{
config: config,
peers: newPeerManager(),
listenerManager: tcp.NewListenerManager(config.ListenAddrsByVRF),
listenerManager: tcp.NewListenerManager(config.ListenAddrsByVRF, config.ReusePort),
}

server.metrics = &metricsService{server}
Expand Down

0 comments on commit e9d95a5

Please sign in to comment.