Can SO_REUSEPORT be used on Unix domain sockets?

Linux kernels >= 3.9 allow sharing of sockets between processes with in-kernel load-balancing by setting SO_REUSEPORT:

How can this be used for sockets of type **AF_UNIX****?**

It seems, it only works with TCP, not Unix domain sockets.

Here is a Python test program:

import os
import socket

if not hasattr(socket, 'SO_REUSEPORT'):
   socket.SO_REUSEPORT = 15

if True:
   # using TCP sockets
   # works. test with: "echo data | nc localhost 8888"
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
   s.bind(('', 8888))
   # using Unix domain sockets
   # does NOT work. test with: "echo data | nc -U /tmp/socket1"
   s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
   s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

while True:
   conn, addr = s.accept()
   print('Connected to {}'.format(os.getpid()))
   data = conn.recv(1024)

Start 2 instances, and test by running the following multiple times:

  • echo data | nc localhost 8888 for TCP
  • echo data | nc -U /tmp/socket1 for Unix domain sockets

When using TCP, the incoming clients will get balanced to both servers. With Unix domain sockets, the incoming clients all get connected to the last started server.

Best answer

This specific kernel patch is documented here:

As you can see from the list of patched files, the patch only affected the net/ipv4 and net/ipv6 sockets. Unix domain sockets are implemented in net/unix. So, the answer is: no, SO_REUSEPORT will not work with sockets of type AF_UNIX.