libmemif: buffer enqueue refactor
[vpp.git] / extras / libmemif / src / main.c
index 870120d..f5b1b5d 100644 (file)
@@ -76,7 +76,7 @@ const char *memif_errlist[ERRLIST_LEN] = {    /* MEMIF_ERR_SUCCESS */
   /* MEMIF_ERR_CONNREFUSED */
   "Connection refused",
   /* MEMIF_ERR_ACCES */
-  "Permission to resoure denied.",
+  "Permission to resource denied.",
   /* MEMIF_ERR_NO_FILE */
   "Socket file does not exist",
   /* MEMIF_ERR_FILE_LIMIT */
@@ -94,7 +94,7 @@ const char *memif_errlist[ERRLIST_LEN] = {    /* MEMIF_ERR_SUCCESS */
   /* MEMIF_ERR_INVAL_ARG */
   "Invalid argument.",
   /* MEMIF_ERR_NOCONN */
-  "Memif connection handle does not point to existing conenction",
+  "Memif connection handle does not point to existing connection",
   /* MEMIF_ERR_CONN */
   "Memif connection handle points to existing connection",
   /* MEMIF_ERR_CB_FDUPDATE */
@@ -110,7 +110,7 @@ const char *memif_errlist[ERRLIST_LEN] = {  /* MEMIF_ERR_SUCCESS */
   /* MEMIF_ERR_NOBUF */
   "Not enough memif buffers. There are unreceived data in shared memory.",
   /* MEMIF_ERR_NOBUF_DET */
-  "Not enough space for memif details in suplied buffer. String data might be malformed.",
+  "Not enough space for memif details in supplied buffer. String data might be malformed.",
   /* MEMIF_ERR_INT_WRITE */
   "Send interrupt error.",
   /* MEMIF_ERR_MFMSG */
@@ -122,7 +122,7 @@ const char *memif_errlist[ERRLIST_LEN] = {  /* MEMIF_ERR_SUCCESS */
   /* MEMIF_ERR_ID */
   "Unmatched interface id.",
   /* MEMIF_ERR_ACCSLAVE */
-  "Slave cannot accept connection reqest.",
+  "Slave cannot accept connection request.",
   /* MEMIF_ERR_ALRCONN */
   "Interface is already connected.",
   /* MEMIF_ERR_MODE */
@@ -281,7 +281,7 @@ memif_mod_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events)
       DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
       return -1;
     }
-  DBG ("fd %d moddified on epoll", fd);
+  DBG ("fd %d modified on epoll", fd);
   return 0;
 }
 
@@ -309,10 +309,7 @@ memif_control_fd_update (int fd, uint8_t events, void *private_ctx)
 {
   libmemif_main_t *lm;
 
-  if (private_ctx == NULL)
-    return MEMIF_ERR_INVAL_ARG;
-
-  lm = (libmemif_main_t *) private_ctx;
+  lm = (private_ctx == NULL) ? &libmemif_main : (libmemif_main_t *) private_ctx;
 
   if (events & MEMIF_FD_EVENT_DEL)
     return memif_del_epoll_fd (lm, fd);
@@ -559,7 +556,7 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
          DBG ("eventfd: %s", strerror (err));
          return memif_syscall_error_handler (err);
        }
-      lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ, NULL);
+      lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ, lm->private_ctx);
       DBG ("libmemif event polling initialized");
     }
 
@@ -633,7 +630,7 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
   lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
   lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
 
-  if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ, NULL) < 0)
+  if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ, lm->private_ctx) < 0)
     {
       DBG ("callback type memif_control_fd_update_t error!");
       err = MEMIF_ERR_CB_FDUPDATE;
@@ -718,6 +715,10 @@ memif_per_thread_init (memif_per_thread_main_handle_t * pt_main,
     memif_control_fd_update_register (lm, on_control_fd_update);
   else
     {
+      /* private_ctx only used internally by memif_control_fd_update
+       * pointer to this libmemif main
+       */
+      lm->private_ctx = lm;
       lm->epfd = epoll_create (1);
       memif_control_fd_update_register (lm, memif_control_fd_update);
       if ((lm->poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
@@ -911,7 +912,8 @@ memif_socket_start_listening (memif_socket_t * ms)
   elt.key = ms->fd;
   elt.data_struct = ms;
   add_list_elt (lm, &elt, &lm->socket_list, &lm->socket_list_len);
-  lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ, ms->private_ctx);
+  /* if lm->private_ctx == lm event polling is done by libmemif */
+  lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ, lm->private_ctx);
 
   ms->type = MEMIF_SOCKET_TYPE_LISTENER;
 
@@ -1202,7 +1204,7 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
 
       conn->index = index;
 
-      /* try connectiong to master */
+      /* try connecting to master */
       err = memif_request_connection (conn);
       if ((err != MEMIF_ERR_SUCCESS) && (lm->disconn_slaves == 0))
        {
@@ -1269,10 +1271,9 @@ memif_request_connection (memif_conn_handle_t c)
       conn->error_fn = memif_conn_fd_error;
 
       lm->control_list[conn->index].key = conn->fd;
-
       lm->control_fd_update (sockfd,
                             MEMIF_FD_EVENT_READ |
-                            MEMIF_FD_EVENT_WRITE, conn->private_ctx);
+                            MEMIF_FD_EVENT_WRITE, lm->private_ctx);
 
       lm->disconn_slaves--;
       if (lm->disconn_slaves == 0)
@@ -1663,7 +1664,6 @@ memif_msg_queue_free (libmemif_main_t * lm, memif_msg_queue_elt_t ** e)
 int
 memif_disconnect_internal (memif_connection_t * c)
 {
-  uint16_t num;
   int err = MEMIF_ERR_SUCCESS, i;      /* 0 */
   memif_queue_t *mq;
   libmemif_main_t *lm;
@@ -1682,7 +1682,7 @@ memif_disconnect_internal (memif_connection_t * c)
   if (c->fd > 0)
     {
       memif_msg_send_disconnect (c->fd, (uint8_t *) "interface deleted", 0);
-      lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL, c->private_ctx);
+      lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL, lm->private_ctx);
       close (c->fd);
     }
   get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd);
@@ -1695,10 +1695,7 @@ memif_disconnect_internal (memif_connection_t * c)
 
   if (c->tx_queues != NULL)
     {
-      num =
-       (c->args.is_master) ? c->run_args.num_m2s_rings : c->
-       run_args.num_s2m_rings;
-      for (i = 0; i < num; i++)
+      for (i = 0; i < c->tx_queues_num; i++)
        {
          mq = &c->tx_queues[i];
          if (mq != NULL)
@@ -1713,13 +1710,11 @@ memif_disconnect_internal (memif_connection_t * c)
       lm->free (c->tx_queues);
       c->tx_queues = NULL;
     }
+  c->tx_queues_num = 0;
 
   if (c->rx_queues != NULL)
     {
-      num =
-       (c->args.is_master) ? c->run_args.num_s2m_rings : c->
-       run_args.num_m2s_rings;
-      for (i = 0; i < num; i++)
+      for (i = 0; i < c->rx_queues_num; i++)
        {
          mq = &c->rx_queues[i];
          if (mq != NULL)
@@ -1728,7 +1723,7 @@ memif_disconnect_internal (memif_connection_t * c)
                {
                  if (c->on_interrupt != NULL)
                    lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL,
-                                          c->private_ctx);
+                                          lm->private_ctx);
                  close (mq->int_fd);
                }
              free_list_elt (lm->interrupt_list, lm->interrupt_list_len,
@@ -1739,6 +1734,7 @@ memif_disconnect_internal (memif_connection_t * c)
       lm->free (c->rx_queues);
       c->rx_queues = NULL;
     }
+  c->rx_queues_num = 0;
 
   for (i = 0; i < c->regions_num; i++)
     {
@@ -1851,7 +1847,7 @@ memif_delete (memif_conn_handle_t * conn)
       /* stop listening on this socket */
       if (ms->type == MEMIF_SOCKET_TYPE_LISTENER)
        {
-         lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_DEL, ms->private_ctx);
+         lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_DEL, lm->private_ctx);
          free_list_elt (lm->socket_list, lm->socket_list_len, ms->fd);
          close (ms->fd);
          ms->fd = -1;
@@ -1935,8 +1931,7 @@ memif_connect1 (memif_connection_t * c)
              DBG ("wrong cookie on rx ring %u", i);
              return MEMIF_ERR_COOKIE;
            }
-         mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
-           0;
+         mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0;
        }
     }
 
@@ -1951,13 +1946,12 @@ memif_connect1 (memif_connection_t * c)
              DBG ("wrong cookie on tx ring %u", i);
              return MEMIF_ERR_COOKIE;
            }
-         mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs =
-           0;
+         mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0;
        }
     }
 
   lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD,
-                        c->private_ctx);
+                        lm->private_ctx);
 
   return 0;
 }
@@ -2079,9 +2073,10 @@ memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn)
       mq[x].offset =
        (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
       mq[x].last_head = mq[x].last_tail = 0;
-      mq[x].alloc_bufs = 0;
+      mq[x].next_buf = 0;
     }
   conn->tx_queues = mq;
+  conn->tx_queues_num = conn->run_args.num_s2m_rings;
 
   mq =
     (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
@@ -2104,9 +2099,10 @@ memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn)
       mq[x].offset =
        (void *) mq[x].ring - (void *) conn->regions[mq->region].addr;
       mq[x].last_head = mq[x].last_tail = 0;
-      mq[x].alloc_bufs = 0;
+      mq[x].next_buf = 0;
     }
   conn->rx_queues = mq;
+  conn->rx_queues_num = conn->run_args.num_m2s_rings;
 
   return MEMIF_ERR_SUCCESS;
 }
@@ -2155,6 +2151,57 @@ memif_init_regions_and_queues (memif_connection_t * conn)
   return 0;
 }
 
+static void
+memif_buffer_enq_at_idx_internal (memif_queue_t *from_q, memif_queue_t *to_q,
+                                 memif_buffer_t *buf, uint16_t slot)
+{
+  uint16_t from_mask = (1 << from_q->log2_ring_size) - 1;
+  uint16_t to_mask = (1 << to_q->log2_ring_size) - 1;
+  memif_desc_t *from_d, *to_d, tmp_d;
+
+  /* Get the descriptors */
+  from_d = &from_q->ring->desc[buf->desc_index & from_mask];
+  to_d = &to_q->ring->desc[slot & to_mask];
+
+  /* Swap descriptors */
+  tmp_d = *from_d;
+  *from_d = *to_d;
+  *to_d = tmp_d;
+
+  /* Update descriptor index and queue for clients buffer */
+  buf->desc_index = slot;
+  buf->queue = to_q;
+}
+
+int
+memif_buffer_requeue (memif_conn_handle_t conn, memif_buffer_t *buf_a,
+                     memif_buffer_t *buf_b)
+{
+  memif_connection_t *c = (memif_connection_t *) conn;
+  if (EXPECT_FALSE (c == NULL))
+    return MEMIF_ERR_NOCONN;
+  if (EXPECT_FALSE (c->args.is_master))
+    return MEMIF_ERR_INVAL_ARG;
+  if ((buf_a == NULL) || (buf_b == NULL))
+    return MEMIF_ERR_INVAL_ARG;
+
+  int err;
+  /* store buf_a information */
+  uint16_t index_a = buf_a->desc_index;
+  memif_queue_t *mq_a = buf_a->queue;
+
+  /* swap buffers, buf_a was updated with new desc_index and queue */
+  memif_buffer_enq_at_idx_internal ((memif_queue_t *) buf_a->queue,
+                                   (memif_queue_t *) buf_b->queue, buf_a,
+                                   buf_b->desc_index);
+
+  /* update buf_b desc_index and queue */
+  buf_b->desc_index = index_a;
+  buf_b->queue = mq_a;
+
+  return MEMIF_ERR_SUCCESS;
+}
+
 int
 memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
                     memif_buffer_t * bufs, uint16_t count,
@@ -2165,10 +2212,7 @@ memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
     return MEMIF_ERR_NOCONN;
   if (EXPECT_FALSE (c->fd < 0))
     return MEMIF_ERR_DISCONNECTED;
-  uint8_t num =
-    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
-    run_args.num_s2m_rings;
-  if (EXPECT_FALSE (qid >= num))
+  if (EXPECT_FALSE (qid >= c->tx_queues_num))
     return MEMIF_ERR_QID;
   if (EXPECT_FALSE (!count_out))
     return MEMIF_ERR_INVAL_ARG;
@@ -2180,51 +2224,35 @@ memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
   memif_buffer_t *b0;
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
   uint16_t ring_size;
-  uint16_t slot, ns;
+  uint16_t ns;
+  memif_queue_t *bmq;
   int err = MEMIF_ERR_SUCCESS; /* 0 */
   *count_out = 0;
 
   ring_size = (1 << mq->log2_ring_size);
-  slot = (c->args.is_master) ? ring->tail : ring->head;
-  slot += mq->alloc_bufs;
 
   /* can only be called by slave */
-  ns = ring_size - (ring->head + mq->alloc_bufs) + ring->tail;
+  ns = ring_size - mq->next_buf + ring->tail;
 
   b0 = bufs;
 
   while (count && ns)
     {
-      if (EXPECT_FALSE ((b0->flags & MEMIF_BUFFER_FLAG_RX) == 0))
-       {
-         /* not a valid buffer */
-         count--;
-         continue;
-       }
-      b0->flags &= ~MEMIF_BUFFER_FLAG_RX;
-
-      ((memif_ring_t *) b0->ring)->desc[b0->desc_index & mask].offset = ring->desc[slot & mask].offset;        /* put free buffer on rx ring */
-
-      ring->desc[slot & mask].offset =
-       (uint32_t) (b0->data -
-                   c->regions[ring->desc[slot & mask].region].addr);
-      ring->desc[slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
-      ring->desc[slot & mask].flags |=
-       (b0->flags & MEMIF_BUFFER_FLAG_NEXT) ? MEMIF_DESC_FLAG_NEXT : 0;
+      /* Swaps the descriptors, updates next_buf pointer and updates client
+       * memif buffer */
 
-      b0->desc_index = slot;
-
-      mq->alloc_bufs++;
-      slot++;
+      memif_buffer_enq_at_idx_internal ((memif_queue_t *) b0->queue, mq, b0,
+                                       mq->next_buf);
 
+      mq->next_buf++; /* mark the buffer as allocated */
       count--;
       ns--;
       b0++;
       *count_out += 1;
     }
 
-  DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
-       mq->alloc_bufs);
+  DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count,
+       mq->next_buf);
 
   if (count)
     {
@@ -2260,21 +2288,20 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
   uint32_t offset_mask = c->run_args.buffer_size - 1;
   uint16_t ring_size;
-  uint16_t slot, ns;
+  uint16_t ns;
   int err = MEMIF_ERR_SUCCESS; /* 0 */
   uint16_t dst_left, src_left;
   uint16_t saved_count;
+  uint16_t saved_next_buf;
   memif_buffer_t *saved_b;
   *count_out = 0;
 
   ring_size = (1 << mq->log2_ring_size);
-  slot = (c->args.is_master) ? ring->tail : ring->head;
-  slot += mq->alloc_bufs;
 
   if (c->args.is_master)
-    ns = ring->head - (ring->tail + mq->alloc_bufs);
+    ns = ring->head - mq->next_buf;
   else
-    ns = ring_size - (ring->head + mq->alloc_bufs) + ring->tail;
+    ns = ring_size - mq->next_buf + ring->tail;
 
   while (count && ns)
     {
@@ -2282,13 +2309,14 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
 
       saved_b = b0;
       saved_count = count;
+      saved_next_buf = mq->next_buf;
 
-      b0->desc_index = slot;
-      ring->desc[slot & mask].flags = 0;
+      b0->desc_index = mq->next_buf;
+      ring->desc[mq->next_buf & mask].flags = 0;
 
       /* slave can produce buffer with original length */
-      dst_left = (c->args.is_master) ? ring->desc[slot & mask].length :
-       c->run_args.buffer_size;
+      dst_left = (c->args.is_master) ? ring->desc[mq->next_buf & mask].length :
+                                      c->run_args.buffer_size;
       src_left = size;
 
       while (src_left)
@@ -2297,9 +2325,8 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
            {
              if (count && ns)
                {
-                 slot++;
                  *count_out += 1;
-                 mq->alloc_bufs++;
+                 mq->next_buf++;
                  ns--;
 
                  ring->desc[b0->desc_index & mask].flags |=
@@ -2307,11 +2334,11 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
                  b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
 
                  b0 = (bufs + *count_out);
-                 b0->desc_index = slot;
-                 dst_left =
-                   (c->args.is_master) ? ring->desc[slot & mask].
-                   length : c->run_args.buffer_size;
-                 ring->desc[slot & mask].flags = 0;
+                 b0->desc_index = mq->next_buf;
+                 dst_left = (c->args.is_master) ?
+                              ring->desc[mq->next_buf & mask].length :
+                              c->run_args.buffer_size;
+                 ring->desc[mq->next_buf & mask].flags = 0;
                }
              else
                {
@@ -2319,7 +2346,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
                  memset (saved_b, 0, sizeof (memif_buffer_t)
                          * (saved_count - count + 1));
                  *count_out -= saved_count - count;
-                 mq->alloc_bufs = saved_count - count;
+                 mq->next_buf = saved_next_buf;
                  goto no_ns;
                }
            }
@@ -2328,29 +2355,28 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
          /* slave resets buffer offset */
          if (c->args.is_master == 0)
            {
-             memif_desc_t *d = &ring->desc[slot & mask];
+             memif_desc_t *d = &ring->desc[mq->next_buf & mask];
              if (lm->get_external_buffer_offset)
                d->offset = lm->get_external_buffer_offset (c->private_ctx);
              else
                d->offset = d->offset - (d->offset & offset_mask);
            }
-         b0->data = memif_get_buffer (c, ring, slot & mask);
+         b0->data = memif_get_buffer (c, ring, mq->next_buf & mask);
 
          src_left -= b0->len;
          dst_left -= b0->len;
        }
 
-      slot++;
       *count_out += 1;
-      mq->alloc_bufs++;
+      mq->next_buf++;
       ns--;
       count--;
     }
 
 no_ns:
 
-  DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
-       mq->alloc_bufs);
+  DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count,
+       mq->next_buf);
 
   if (count)
     {
@@ -2437,16 +2463,27 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
   memif_buffer_t *b0;
   *tx = 0;
-
-  if (count > mq->alloc_bufs)
-    count = mq->alloc_bufs;
+  int err = MEMIF_ERR_SUCCESS;
 
   if (EXPECT_FALSE (count == 0))
     return MEMIF_ERR_SUCCESS;
 
+  uint16_t index;
+  if (c->args.is_master)
+    index = ring->tail;
+  else
+    index = ring->head;
+
   while (count)
     {
       b0 = (bufs + *tx);
+      /* set error to MEMIF_ERR_INVAL_ARG and finish the sending process
+       */
+      if ((b0->desc_index & mask) != (index & mask))
+       {
+         err = MEMIF_ERR_INVAL_ARG;
+         goto done;
+       }
       ring->desc[b0->desc_index & mask].length = b0->len;
 
 #ifdef MEMIF_DBG_SHM
@@ -2460,17 +2497,16 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
 
       *tx += 1;
       count--;
+      index++;
     }
 
-
+done:
   MEMIF_MEMORY_BARRIER ();
   if (c->args.is_master)
     ring->tail = b0->desc_index + 1;
   else
     ring->head = b0->desc_index + 1;
 
-  mq->alloc_bufs -= *tx;
-
   if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
     {
       uint64_t a = 1;
@@ -2479,7 +2515,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
        return MEMIF_ERR_INT_WRITE;
     }
 
-  return MEMIF_ERR_SUCCESS;    /* 0 */
+  return err;
 }
 
 int
@@ -2508,14 +2544,18 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
   *rx = 0;
 
   uint64_t b;
-  ssize_t r = read (mq->int_fd, &b, sizeof (b));
-  if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
-    return memif_syscall_error_handler (errno);
+  ssize_t r;
 
   cur_slot = (c->args.is_master) ? mq->last_head : mq->last_tail;
   last_slot = (c->args.is_master) ? ring->head : ring->tail;
   if (cur_slot == last_slot)
-    return MEMIF_ERR_SUCCESS;
+    {
+      r = read (mq->int_fd, &b, sizeof (b));
+      if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
+                       return memif_syscall_error_handler (errno);
+
+      return MEMIF_ERR_SUCCESS;
+    }
 
   ns = last_slot - cur_slot;
 
@@ -2532,18 +2572,17 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
          ring->desc[cur_slot & mask].length = c->run_args.buffer_size;
        }
 
-      b0->flags = MEMIF_BUFFER_FLAG_RX;
       if (ring->desc[cur_slot & mask].flags & MEMIF_DESC_FLAG_NEXT)
        {
          b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
          ring->desc[cur_slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
        }
 /*      b0->offset = ring->desc[cur_slot & mask].offset;*/
-      b0->ring = ring;
+      b0->queue = mq;
 #ifdef MEMIF_DBG_SHM
       printf ("data: %p\n", b0->data);
       printf ("index: %u\n", b0->desc_index);
-      printf ("ring: %p\n", b0->ring);
+      printf ("queue: %p\n", b0->queue);
       print_bytes (b0->data, b0->len, DBG_RX_BUF);
 #endif /* MEMIF_DBG_SHM */
       ns--;
@@ -2564,6 +2603,10 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
       return MEMIF_ERR_NOBUF;
     }
 
+  r = read (mq->int_fd, &b, sizeof (b));
+  if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
+    return memif_syscall_error_handler (errno);
+
   return MEMIF_ERR_SUCCESS;    /* 0 */
 }