Skip to content

Commit

Permalink
Enable SO_REUSEPORT (#464)
Browse files Browse the repository at this point in the history
* Enable SO_REUSEPORT

Signed-off-by: netixx <[email protected]>

* Make reuseport optional

---------

Signed-off-by: netixx <[email protected]>
  • Loading branch information
netixx authored Jul 2, 2024
1 parent 3fbc7fd commit 63eef2b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
15 changes: 11 additions & 4 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,7 +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)
}

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 Expand Up @@ -147,7 +154,7 @@ type MockListener struct {
connCh chan *MockConn
}

func (lf *MockListenerFactory) NewListener(v *vrf.VRF, laddr *net.TCPAddr, ttl uint8) (ListenerI, error) {
func (lf *MockListenerFactory) NewListener(v *vrf.VRF, laddr *net.TCPAddr, ttl uint8, reusePort bool) (ListenerI, error) {
return &MockListener{
localAddr: laddr.IP,
localPort: uint16(laddr.Port),
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
2 changes: 1 addition & 1 deletion tests/bgp_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestBGP(t *testing.T) {
"main": {
"0.0.0.0:179",
},
})
}, false)

lm.SetListenerFactory(tcp.NewMockListenerFactory())
b.SetListenerManager(lm)
Expand Down

0 comments on commit 63eef2b

Please sign in to comment.