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

Enable SO_REUSEPORT #464

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
Loading