vcl: support for eventfd mq signaling
[vpp.git] / src / svm / message_queue.c
index 1b2d2e1..d6a77e7 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <svm/message_queue.h>
 #include <vppinfra/mem.h>
+#include <sys/eventfd.h>
 
 static inline svm_msg_q_ring_t *
 svm_msg_q_ring_inline (svm_msg_q_t * mq, u32 ring_index)
@@ -39,12 +40,12 @@ svm_msg_q_t *
 svm_msg_q_alloc (svm_msg_q_cfg_t * cfg)
 {
   svm_msg_q_ring_cfg_t *ring_cfg;
+  uword rings_sz = 0, mq_sz;
   svm_msg_q_ring_t *ring;
   u8 *base, *rings_ptr;
-  uword rings_sz = 0;
   vec_header_t *vh;
+  u32 vec_sz, q_sz;
   svm_msg_q_t *mq;
-  u32 vec_sz;
   int i;
 
   ASSERT (cfg);
@@ -58,13 +59,17 @@ svm_msg_q_alloc (svm_msg_q_cfg_t * cfg)
       rings_sz += (uword) ring_cfg->nitems * ring_cfg->elsize;
     }
 
-  base = clib_mem_alloc_aligned (sizeof (svm_msg_q_t) + vec_sz + rings_sz,
-                                CLIB_CACHE_LINE_BYTES);
+  q_sz = sizeof (svm_queue_t) + cfg->q_nitems * sizeof (svm_msg_q_msg_t);
+  mq_sz = sizeof (svm_msg_q_t) + vec_sz + rings_sz + q_sz;
+  base = clib_mem_alloc_aligned (mq_sz, CLIB_CACHE_LINE_BYTES);
   if (!base)
     return 0;
 
   mq = (svm_msg_q_t *) base;
-  vh = (vec_header_t *) (base + sizeof (svm_msg_q_t));
+  mq->q = svm_queue_init (base + sizeof (svm_msg_q_t), cfg->q_nitems,
+                         sizeof (svm_msg_q_msg_t));
+  mq->q->consumer_pid = cfg->consumer_pid;
+  vh = (vec_header_t *) ((u8 *) mq->q + q_sz);
   vh->len = cfg->n_rings;
   mq->rings = (svm_msg_q_ring_t *) (vh + 1);
   rings_ptr = (u8 *) mq->rings + vec_sz;
@@ -82,8 +87,6 @@ svm_msg_q_alloc (svm_msg_q_cfg_t * cfg)
          rings_ptr += (uword) ring->nitems * ring->elsize;
        }
     }
-  mq->q = svm_queue_init (cfg->q_nitems, sizeof (svm_msg_q_msg_t),
-                         cfg->consumer_pid, 0);
 
   return mq;
 }
@@ -233,6 +236,38 @@ svm_msg_q_sub_w_lock (svm_msg_q_t * mq, svm_msg_q_msg_t * msg)
   svm_queue_sub_raw (mq->q, (u8 *) msg);
 }
 
+void
+svm_msg_q_set_consumer_eventfd (svm_msg_q_t * mq, int fd)
+{
+  mq->q->consumer_evtfd = fd;
+}
+
+void
+svm_msg_q_set_producer_eventfd (svm_msg_q_t * mq, int fd)
+{
+  mq->q->producer_evtfd = fd;
+}
+
+int
+svm_msg_q_alloc_consumer_eventfd (svm_msg_q_t * mq)
+{
+  int fd;
+  if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
+    return -1;
+  svm_msg_q_set_consumer_eventfd (mq, fd);
+  return 0;
+}
+
+int
+svm_msg_q_alloc_producer_eventfd (svm_msg_q_t * mq)
+{
+  int fd;
+  if ((fd = eventfd (0, EFD_NONBLOCK)) < 0)
+    return -1;
+  svm_msg_q_set_producer_eventfd (mq, fd);
+  return 0;
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *