]> git.puffer.fish Git - mirror/frr.git/commitdiff
pimd: Modify mroute_read to be non-blocking
authorDonald Sharp <sharpd@cumulusnetworks.com>
Thu, 17 Nov 2016 16:18:06 +0000 (11:18 -0500)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Thu, 22 Dec 2016 01:26:16 +0000 (20:26 -0500)
Modify mroute_read to be non-blocking and
then to read in up to 3 packets at a time
to be handled.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
pimd/pim_mroute.c

index d58cfaa9a2ada6749d7a6d752e215c3de5545da8..34fc24c5061b65c35b20f65d2b3c49be9416b355 100644 (file)
@@ -54,6 +54,7 @@ static int pim_mroute_set(int fd, int enable)
   int opt = enable ? MRT_INIT : MRT_DONE;
   socklen_t opt_len = sizeof(opt);
   int rcvbuf = 1024 * 1024 * 8;
+  long flags;
 
   err = setsockopt(fd, IPPROTO_IP, opt, &opt, opt_len);
   if (err) {
@@ -69,6 +70,22 @@ static int pim_mroute_set(int fd, int enable)
              __PRETTY_FUNCTION__, fd, rcvbuf, errno, safe_strerror(errno));
   }
 
+  flags = fcntl(fd, F_GETFL, 0);
+  if (flags < 0)
+    {
+      zlog_warn("Could not get flags on socket fd:%d %d %s",
+               fd, errno, safe_strerror(errno));
+      close (fd);
+      return -1;
+    }
+  if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
+    {
+      zlog_warn("Could not set O_NONBLOCK on socket fd:%d %d %s",
+               fd, errno, safe_strerror(errno));
+      close(fd);
+      return -1;
+    }
+
   if (enable)
     {
       int upcalls = IGMPMSG_WRVIFWHOLE;
@@ -530,32 +547,42 @@ int pim_mroute_msg(int fd, const char *buf, int buf_size)
   return 0;
 }
 
-static int mroute_read_msg(int fd)
-{
-  char buf[10000];
-  int rd;
-
-  rd = read(fd, buf, sizeof(buf));
-  if (rd < 0) {
-    zlog_warn("%s: failure reading fd=%d: errno=%d: %s",
-             __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
-    return -1;
-  }
-
-  return pim_mroute_msg(fd, buf, rd);
-}
-
 static int mroute_read(struct thread *t)
 {
+  static long long count;
+  char buf[10000];
+  int result = 0;
+  int cont = 1;
   int fd;
-  int result;
-
+  int rd;
 
   fd = THREAD_FD(t);
 
-  result = mroute_read_msg(fd);
+  while (cont)
+    {
+      rd = read(fd, buf, sizeof(buf));
+      if (rd < 0) {
+       if (errno == EINTR)
+         continue;
+       if (errno == EWOULDBLOCK || errno == EAGAIN)
+         {
+           cont = 0;
+           break;
+         }
+       if (PIM_DEBUG_MROUTE)
+         zlog_warn("%s: failure reading fd=%d: errno=%d: %s",
+                   __PRETTY_FUNCTION__, fd, errno, safe_strerror(errno));
+       goto done;
+      }
 
+      result = pim_mroute_msg(fd, buf, rd);
+
+      count++;
+      if (count % 3 == 0)
+       cont = 0;
+    }
   /* Keep reading */
+ done:
   qpim_mroute_socket_reader = NULL;
   mroute_read_on();