From e965df174205e68fc3dd9c403310de192d7a58cc Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Fri, 4 Aug 2023 15:52:34 +0530 Subject: [PATCH 1/5] feat: Add support for SO_REUSEPORT_LB option as per freeBSD --- control_unix.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/control_unix.go b/control_unix.go index 4197d1f..f3cd60a 100644 --- a/control_unix.go +++ b/control_unix.go @@ -8,6 +8,9 @@ import ( "golang.org/x/sys/unix" ) +// This value has been taken from https://reviews.freebsd.org/D11003 since this is not yet provided in golang. +const FREEBSD_SO_REUSEPORT_LB = 0x00010000 + func Control(network, address string, c syscall.RawConn) (err error) { controlErr := c.Control(func(fd uintptr) { err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1) @@ -15,6 +18,10 @@ func Control(network, address string, c syscall.RawConn) (err error) { return } err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1) + if err != nil { + return + } + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, FREEBSD_SO_REUSEPORT_LB, 1) }) if controlErr != nil { err = controlErr From b9a6708d7ec7e524d39317eef91d4410d8167738 Mon Sep 17 00:00:00 2001 From: prem prathi Date: Wed, 16 Aug 2023 18:47:57 +0530 Subject: [PATCH 2/5] fix: address review comments --- control_unix.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/control_unix.go b/control_unix.go index f3cd60a..75e34b6 100644 --- a/control_unix.go +++ b/control_unix.go @@ -4,12 +4,10 @@ package reuseport import ( "syscall" - + "runtime" "golang.org/x/sys/unix" ) -// This value has been taken from https://reviews.freebsd.org/D11003 since this is not yet provided in golang. -const FREEBSD_SO_REUSEPORT_LB = 0x00010000 func Control(network, address string, c syscall.RawConn) (err error) { controlErr := c.Control(func(fd uintptr) { @@ -21,7 +19,9 @@ func Control(network, address string, c syscall.RawConn) (err error) { if err != nil { return } - err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, FREEBSD_SO_REUSEPORT_LB, 1) + if runtime.GOOS == "freebsd" { + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT_LB, 1) + } }) if controlErr != nil { err = controlErr From 2f99b77039c276c6d7aecb66195c7ba60ec1d858 Mon Sep 17 00:00:00 2001 From: prem prathi Date: Wed, 16 Aug 2023 19:19:42 +0530 Subject: [PATCH 3/5] chore: updated to use build flags --- control_freebsd.go | 27 +++++++++++++++++++++++++++ control_unix.go | 6 +----- 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 control_freebsd.go diff --git a/control_freebsd.go b/control_freebsd.go new file mode 100644 index 0000000..71caca4 --- /dev/null +++ b/control_freebsd.go @@ -0,0 +1,27 @@ +//go:build freebsd + +package reuseport + +import ( + "syscall" + "golang.org/x/sys/unix" +) + + +func Control(network, address string, c syscall.RawConn) (err error) { + controlErr := c.Control(func(fd uintptr) { + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1) + if err != nil { + return + } + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1) + if err != nil { + return + } + err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT_LB, 1) + }) + if controlErr != nil { + err = controlErr + } + return +} diff --git a/control_unix.go b/control_unix.go index 75e34b6..8b48ebc 100644 --- a/control_unix.go +++ b/control_unix.go @@ -1,10 +1,9 @@ -//go:build !plan9 && !windows && !wasm +//go:build !plan9 && !windows && !wasm && !freebsd package reuseport import ( "syscall" - "runtime" "golang.org/x/sys/unix" ) @@ -19,9 +18,6 @@ func Control(network, address string, c syscall.RawConn) (err error) { if err != nil { return } - if runtime.GOOS == "freebsd" { - err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT_LB, 1) - } }) if controlErr != nil { err = controlErr From 82f33bebb027a4418b18712b5ad69aea609845cb Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Thu, 17 Aug 2023 10:44:37 +0530 Subject: [PATCH 4/5] chore: remove unnecessary code --- control_unix.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/control_unix.go b/control_unix.go index 8b48ebc..e80688b 100644 --- a/control_unix.go +++ b/control_unix.go @@ -4,10 +4,10 @@ package reuseport import ( "syscall" + "golang.org/x/sys/unix" ) - func Control(network, address string, c syscall.RawConn) (err error) { controlErr := c.Control(func(fd uintptr) { err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1) @@ -15,9 +15,6 @@ func Control(network, address string, c syscall.RawConn) (err error) { return } err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1) - if err != nil { - return - } }) if controlErr != nil { err = controlErr From 2f2c5e419dbc33c2cbf8806a19c666a1b69b9ecc Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Thu, 17 Aug 2023 11:04:06 +0530 Subject: [PATCH 5/5] chore: fix gofmt error --- control_freebsd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/control_freebsd.go b/control_freebsd.go index 71caca4..cec1b11 100644 --- a/control_freebsd.go +++ b/control_freebsd.go @@ -4,10 +4,10 @@ package reuseport import ( "syscall" + "golang.org/x/sys/unix" ) - func Control(network, address string, c syscall.RawConn) (err error) { controlErr := c.Control(func(fd uintptr) { err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)