Linux TCP So_reuseport: Usage and Implementation
linuxjournal.rubdos.be
linuxjournal.rubdos.be
#if defined SO_REUSEPORT_LB
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT_LB, &(int) { 1 }, sizeof(int));
#elif defined SO_REUSEPORT
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &(int) { 1 }, sizeof(int));
#else
[do something to avoid reusing sockets]
#endif
when I want to use this feature. SO_REUSEPORT enables duplicate address and port bindings
SO_REUSEPORT_LB enables duplicate address and port bindings
with load balancing
* https://man.freebsd.org/cgi/man.cgi?setsockopt(2)The LB variant was added in only 2017:
* https://reviews.freebsd.org/D11003
SO_REUSEPORT has been around since 4.4BSD:
* https://man.freebsd.org/cgi/man.cgi?query=setsockopt&sektion...
SO_REUSEPORT was added to Linux in ~2013:
* https://lists.dragonflybsd.org/pipermail/users/2013-July/053...
Unfortunately, this behavior was never documented in the manual pages; only the load balancing-like UDP semantics were documented. Previously Linux didn't support SO_REUSEPORT at all, and unfortunately whomever decided to make use of SO_REUSEPORT on Linux either didn't check or didn't care what the actual behavior was for TCP connections on the BSDs. AFAIU, on the Linux side the original problem being solved was the infamous stampeding herd issue when multiple processes or threads were polling on listening TCP sockets, as can occur with non-blocking/asynchronous I/O frameworks that utilize multiple processes or threads, each with its own event loop polling (i.e. poll/epoll_wait/kqueue) for incoming connections. The semantics they chose were largely compatible with how the BSDs supported this for UDP, but not how it worked for TCP.
Arguably, at least regards TCP, SO_REUSEPORT wasn't added to Linux so much as an entirely different feature was created and the implementation decided to squat the pre-existing macro definition out of convenience. (SO_REUSEPORT had always been defined, but, IIRC, simply ignored by Linux, similar to SO_RCVLOWAT.) That's not charitable, but neither was the decision to either not investigate the actual BSD behavior, or if they did to ignore or discount it.
I think the confusing thing was that Linux added an option and named it "SO_REUSEPORT" even though that name had meant something completely different for decades.