svm: mq use poll instead of SO_RCVTIMEO 02/41802/3
authorFlorin Coras <[email protected]>
Tue, 5 Nov 2024 05:47:09 +0000 (21:47 -0800)
committerFlorin Coras <[email protected]>
Tue, 5 Nov 2024 05:31:36 +0000 (05:31 +0000)
setsockopt does not work on eventfds

Type: fix

Signed-off-by: Florin Coras <[email protected]>
Change-Id: I652a2b78160abe1bc15879fd8bc320ba4ef38e63

src/svm/message_queue.c

index ab0d230..f285653 100644 (file)
@@ -18,7 +18,7 @@
 #include <vppinfra/format.h>
 #include <vppinfra/time.h>
 #include <sys/eventfd.h>
-#include <sys/socket.h>
+#include <poll.h>
 
 static inline svm_msg_q_ring_t *
 svm_msg_q_ring_inline (svm_msg_q_t * mq, u32 ring_index)
@@ -629,25 +629,29 @@ svm_msg_q_timedwait (svm_msg_q_t *mq, double timeout)
     }
   else
     {
-      struct timeval tv;
+      struct pollfd fds = { .fd = mq->q.evtfd, .events = POLLIN };
       u64 buf;
       int rv;
 
-      tv.tv_sec = (u64) timeout;
-      tv.tv_usec = ((u64) timeout - (u64) timeout) * 1e9;
-      rv = setsockopt (mq->q.evtfd, SOL_SOCKET, SO_RCVTIMEO,
-                      (const char *) &tv, sizeof tv);
+      rv = poll (&fds, 1, timeout * 1e3 /* ms */);
       if (rv < 0)
        {
-         clib_unix_warning ("setsockopt");
+         clib_unix_warning ("poll");
          return -1;
        }
+      else if (rv == 0)
+       {
+         /* timeout occured */
+         return 0;
+       }
 
       rv = read (mq->q.evtfd, &buf, sizeof (buf));
-      if (rv < 0)
-       clib_warning ("read %u", errno);
-
-      return rv < 0 ? errno : 0;
+      if (rv < 0 && errno != EAGAIN)
+       {
+         clib_warning ("read %u", errno);
+         return -2;
+       }
+      return 0;
     }
 }