From f3f640b6bcf10ba463fc81cec97cfa212c520cfe Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Wed, 28 Oct 2026 17:08:17 +0100 Subject: Performance improvement, where it works: Let different udp worker threads have their own socket --- ot_socket.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'ot_socket.c') diff --git a/ot_socket.c b/ot_socket.c index 576087f..158459f 100644 --- a/ot_socket.c +++ b/ot_socket.c @@ -75,13 +75,37 @@ static int socket_v6(int type) { return s; } +/* SO_REUSEPORT means three different things. Linux spreads datagrams over + the group. FreeBSD permits the bind and spreads nothing, which is what + SO_REUSEPORT_LB was added for. Darwin hands every datagram to whichever + socket bound last, so a group there starves all but one reader. */ +#if defined(SO_REUSEPORT_LB) +#define OT_BALANCE_OPTION SO_REUSEPORT_LB +#elif defined(SO_REUSEPORT) && !defined(__APPLE__) +#define OT_BALANCE_OPTION SO_REUSEPORT +#endif + +int socket_can_balance(void) { +#ifdef OT_BALANCE_OPTION + return 1; +#else + return 0; +#endif +} + /* The only place in the tree that needs to know what a kernel calls this */ static void socket_set_sharing(int s, socket_sharing sharing) { int one = 1; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); +#ifdef OT_BALANCE_OPTION + if (sharing == SOCKET_BALANCED) { + setsockopt(s, SOL_SOCKET, OT_BALANCE_OPTION, &one, sizeof(one)); + return; + } +#endif #ifdef SO_REUSEPORT - if (sharing == SOCKET_SHARED) + if (sharing != SOCKET_EXCLUSIVE) setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); #endif } -- cgit v1.3