title: recvmmsg/recv/recvfrom blocks under high load
body: <p>We have an application on Centos 6 that calls <code>recvmmsg()</code> on a multicast address to read 1024 UDP packets at a time.  When we run multiple instances of this application on the same box (all listening to the same traffic), sometimes this call will block for multiple seconds, despite the socket being non-blocking, and passing in <code>MSG_DONTWAIT</code>.  It works fine in all other circumstances, but will freeze under high load (50MB/s).  When the application blocks, we fall behind on the UDP traffic and can't recover.  The process is running using the RR scheduler as high priority to avoid interference from other processes.  We've tried switching to <code>recvfrom()</code> and <code>recv()</code> in a for loop as well with the same results.</p>
  
  <p>The only thing that we can see in the kernel source that could block this is <code>spin_lock_irqsave()</code> on the queue lock in <code>__skb_try_recv_datagram()</code>.  But I don't know under what circumstances that would be a problem, or what to do about it to prevent blocking, or if that's really the issue.</p>
  
  <p>I'm not sure where to look next, so any pointers would be appreciated.</p>
  
  <p>Created a very simple program that can replicate this on one of the servers where we see this (didn't paste the interface retrieval function, but that shouldn't be relevant here, let me know if you need it).</p>
  
  <p><strong>recv() example:</strong></p>
  
  <pre><code>int main(){
      int fd = socket(AF_INET,SOCK_DGRAM,0);
      int flags = fcntl(fd,F_GETFL,0);
      fcntl(fd,F_SETFL, flags | O_NONBLOCK);
      int reuse = 1;
      setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&amp;reuse,sizeof(reuse));
      struct sockaddr_in sockaddr;
      sockaddr.sin_port = htons(4755);
      sockaddr.sin_family = AF_INET;
      sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
      if(bind(fd,(struct sockaddr*)&amp;sockaddr,sizeof(sockaddr)) &lt; 0){ 
        printf("Failed to bind.\n");
        return 1;
      }
  
      in_addr_t interface;
      if(!getInterface("192.168.15.255",&amp;interface)){
        printf("Failed to get interface.\n");
        return 1;
      }
      struct ip_mreq imr;
      memset(&amp;imr,0,sizeof(imr));
      imr.imr_multiaddr.s_addr = inet_addr("239.255.61.255");
      imr.imr_interface.s_addr = interface;
      if(!IN_MULTICAST(htonl(imr.imr_multiaddr.s_addr))){
        printf("Group not in multicast.");
        return 1;
      }
      if(setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP, (char*)&amp;imr, sizeof(imr))    &lt; 0){ 
        printf("Failed to add membership, errno: %d.\n",errno);
        return 1;
      }
  
      int epollInstance = epoll_create1(0);
      struct epoll_event* epollEvents = (struct epoll_event*)malloc(sizeof(struct epoll_event)*8192);
      epollEvents[0].events = EPOLLIN;
      epoll_ctl(epollInstance,EPOLL_CTL_ADD,fd,&amp;epollEvents[0]);
  
      const int PACKETS_TO_READ = 1024;
      static char receiveBuffer[PACKETS_TO_READ][USHRT_MAX];
      static struct iovec iovecs[PACKETS_TO_READ];
      static struct mmsghdr msgs[PACKETS_TO_READ];
      static struct sockaddr_in sockFrom[PACKETS_TO_READ];
      for (int i = 0; i &lt; PACKETS_TO_READ; i++) {
        iovecs[i].iov_base         = receiveBuffer[i];
        iovecs[i].iov_len          = USHRT_MAX;
        msgs[i].msg_hdr.msg_iov    = &amp;iovecs[i];
        msgs[i].msg_hdr.msg_iovlen = 1;
        msgs[i].msg_hdr.msg_name = &amp;sockFrom[i];
        msgs[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_in );
      }
  
      struct timeval start;
      struct timeval end;  
  
      while(1){
        int selected = epoll_wait(epollInstance,epollEvents,8192,10);
        if(selected &gt; 0){ 
          gettimeofday(&amp;start,NULL);
          // uncomment this line and comment out the below for loop to switch to recvmmsg, both show the issue
          //  int numPackets = recvmmsg(fd,msgs,PACKETS_TO_READ,MSG_DONTWAIT,0);
          int numPackets = 0;
          for(int i = 0; i &lt; PACKETS_TO_READ; i++){
          int result = recv(fd,receiveBuffer[0],USHRT_MAX,MSG_DONTWAIT);
          if(result == EAGAIN) break;
            numPackets++;
          }
          gettimeofday(&amp;end,NULL);
          printf("Got %d packets in %lu microseconds\n",numPackets, (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec);
        }   
      }
  }
  </code></pre>
  
  <p><strong>recvmmsg() example:</strong></p>
  
  <pre><code>int main(){
      int fd = socket(AF_INET,SOCK_DGRAM,0);
      int flags = fcntl(fd,F_GETFL,0);
      fcntl(fd,F_SETFL, flags | O_NONBLOCK);
      int reuse = 1;
      setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*)&amp;reuse,sizeof(reuse));
      struct sockaddr_in sockaddr;
      sockaddr.sin_port = htons(4755);
      sockaddr.sin_family = AF_INET;
      sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
      if(bind(fd,(struct sockaddr*)&amp;sockaddr,sizeof(sockaddr)) &lt; 0){ 
        printf("Failed to bind.\n");
        return 1;
      }
  
      in_addr_t interface;
      if(!getInterface("192.168.15.255",&amp;interface)){
        printf("Failed to get interface.\n");
        return 1;
      }
      struct ip_mreq imr;
      memset(&amp;imr,0,sizeof(imr));
      imr.imr_multiaddr.s_addr = inet_addr("239.255.61.255");
      imr.imr_interface.s_addr = interface;
      if(!IN_MULTICAST(htonl(imr.imr_multiaddr.s_addr))){
        printf("Group not in multicast.");
        return 1;
      }
      if(setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP, (char*)&amp;imr, sizeof(imr))    &lt; 0){ 
        printf("Failed to add membership, errno: %d.\n",errno);
        return 1;
      }
  
      int epollInstance = epoll_create1(0);
      struct epoll_event* epollEvents = (struct epoll_event*)malloc(sizeof(struct epoll_event)*8192);
      epollEvents[0].events = EPOLLIN;
      epoll_ctl(epollInstance,EPOLL_CTL_ADD,fd,&amp;epollEvents[0]);
  
      const int PACKETS_TO_READ = 1024;
      static char receiveBuffer[PACKETS_TO_READ][USHRT_MAX];
      static struct iovec iovecs[PACKETS_TO_READ];
      static struct mmsghdr msgs[PACKETS_TO_READ];
      static struct sockaddr_in sockFrom[PACKETS_TO_READ];
      for (int i = 0; i &lt; PACKETS_TO_READ; i++) {
        iovecs[i].iov_base         = receiveBuffer[i];
        iovecs[i].iov_len          = USHRT_MAX;
        msgs[i].msg_hdr.msg_iov    = &amp;iovecs[i];
        msgs[i].msg_hdr.msg_iovlen = 1;
        msgs[i].msg_hdr.msg_name = &amp;sockFrom[i];
        msgs[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_in );
      }
  
      struct timeval start;
      struct timeval end;  
  
      while(1){
        int selected = epoll_wait(epollInstance,epollEvents,8192,10);
        if(selected &gt; 0){ 
          gettimeofday(&amp;start,NULL);
          // uncomment this line and comment out the below for loop to switch to recvmmsg, both show the issue
          int numPackets = recvmmsg(fd,msgs,PACKETS_TO_READ,MSG_DONTWAIT,0);
          gettimeofday(&amp;end,NULL);
          printf("Got %d packets in %lu microseconds\n",numPackets, (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec);
        }   
      }
  }
  </code></pre>