From: Willy Tarreau Date: Sat, 22 Apr 2023 18:12:59 +0000 (+0200) Subject: BUG/MINOR: sock_inet: use SO_REUSEPORT_LB where available X-Git-Tag: v2.8-dev8~19 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9f53b7b41ac7bb8b415550ca626a635b2808cc3b;p=thirdparty%2Fhaproxy.git BUG/MINOR: sock_inet: use SO_REUSEPORT_LB where available On FreeBSD 13.1 I noticed that thread balancing using shards was not always working. Sometimes several threads would work, but most of the time a single one was taking all the traffic. This is related to how SO_REUSEPORT works on FreeBSD since version 12, as it seems there is no guarantee that multiple sockets will receive the traffic. However there is SO_REUSEPORT_LB that is designed exactly for this, so we'd rather use it when available. This patch may possibly be backported, but nobody complained and it's not sure that many users rely on shards. So better wait for some feedback before backporting this. --- diff --git a/src/sock_inet.c b/src/sock_inet.c index b7861f7885..d225a9dc94 100644 --- a/src/sock_inet.c +++ b/src/sock_inet.c @@ -380,6 +380,14 @@ int sock_inet_bind_receiver(struct receiver *rx, char **errmsg) setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); #endif +#ifdef SO_REUSEPORT_LB + /* FreeBSD 12 and above use this to load-balance incoming connections. + * This is limited to 256 listeners per group however. + */ + if (!ext && (global.tune.options & GTUNE_USE_REUSEPORT)) + setsockopt(fd, SOL_SOCKET, SO_REUSEPORT_LB, &one, sizeof(one)); +#endif + if (!ext && (rx->settings->options & RX_O_FOREIGN)) { switch (addr_inet.ss_family) { case AF_INET: