SO_REUSEPORT: strange kernel balancer behaviour

trafdev trafdev at
Sat Jul 13 05:18:29 UTC 2013


Could someone help with following problem of SO_REUSEPORT.

Created server:

int sockd_acceptor_;

if ((sockd_acceptor_ = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
         LOG4CXX_ERROR_ERRNO(kLogger, "socket");
         return false;
     struct sockaddr_in sa_in;
     memset(&sa_in, 0, sizeof(sa_in));
     sa_in.sin_family = AF_INET;
     sa_in.sin_port = htons(port);
     sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
     int yes = 1;
     if (setsockopt(sockd_acceptor_, SOL_SOCKET, SO_REUSEPORT, &yes, 
sizeof (yes)) == -1) {
         LOG4CXX_ERROR_ERRNO(kLogger, "setsockopt");
         return false;
     if (bind(sockd_acceptor_, (const struct sockaddr*)&sa_in, 
sizeof(sa_in)) == -1) {
         LOG4CXX_ERROR_ERRNO(kLogger, "bind");
         return false;

     if (listen(sockd_acceptor_, listen_backlog) == -1) {
         LOG4CXX_ERROR_ERRNO(kLogger, "socket listen");
         return false;
     if (!fcntl_set(sockd_acceptor_, O_NONBLOCK))
         return false;

Then libev is used as async dispatcher.

Server process 1 started, server process 2 started.
Everything is good so far, no bind errors.

Client started.

Server process 1 starts to reply, process 2 gets no requests yet.
This happens until process 1 is killed. Immediately after that process 2 
starts to respond (gets requests from client).

So it looks like there is a deque of processes sharing same port 
(SO_REUSEPORT) and only one process may respond.
Is this an expected behavior?

More information about the freebsd-net mailing list