libmemif: fix clang compilation errors/warnings
[vpp.git] / extras / libmemif / src / main.c
index b1176c6..d0c1087 100644 (file)
@@ -54,7 +54,7 @@
 /* private structs and functions */
 #include <memif_private.h>
 
-#define ERRLIST_LEN 37
+#define ERRLIST_LEN 39
 #define MAX_ERRBUF_LEN 256
 
 #if __x86_x64__
@@ -142,7 +142,11 @@ const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */
   /* MEMIF_ERR_UNKNOWN_MSG */
   "Unknown message type received on control channel. (internal error)",
   /* MEMIF_ERR_POLL_CANCEL */
-  "Memif event polling was canceled."
+  "Memif event polling was canceled.",
+  /* MEMIF_ERR_MAX_RING */
+  "Maximum log2 ring size is 15",
+  /* MEMIF_ERR_PRIVHDR */
+  "Private headers not supported."
 };
 
 #define MEMIF_ERR_UNDEFINED "undefined error"
@@ -164,6 +168,12 @@ memif_strerror (int err_code)
   return memif_buf;
 }
 
+uint16_t
+memif_get_version ()
+{
+  return MEMIF_VERSION;
+}
+
 #define DBG_TX_BUF (0)
 #define DBG_RX_BUF (1)
 
@@ -189,7 +199,7 @@ print_bytes (void *data, uint16_t len, uint8_t q)
 int
 memif_syscall_error_handler (int err_code)
 {
-  DBG_UNIX ("%s", strerror (err_code));
+  DBG ("%s", strerror (err_code));
 
   if (err_code == 0)
     return MEMIF_ERR_SUCCESS;
@@ -201,7 +211,7 @@ memif_syscall_error_handler (int err_code)
     return MEMIF_ERR_PROC_FILE_LIMIT;
   if (err_code == ENOMEM)
     return MEMIF_ERR_NOMEM;
-/* connection refused if master dows not exist
+/* connection refused if master does not exist
     this error would spam the user until master was created */
   if (err_code == ECONNREFUSED)
     return MEMIF_ERR_SUCCESS;
@@ -399,22 +409,48 @@ memif_control_fd_update_register (memif_control_fd_update_t * cb)
   lm->control_fd_update = cb;
 }
 
+static void
+memif_alloc_register (memif_alloc_t * ma)
+{
+  libmemif_main_t *lm = &libmemif_main;
+  lm->alloc = ma;
+}
+
+static void
+memif_free_register (memif_free_t * mf)
+{
+  libmemif_main_t *lm = &libmemif_main;
+  lm->free = mf;
+}
+
 int
-memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
+memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
+           memif_alloc_t * memif_alloc, memif_free_t * memif_free)
 {
   int err = MEMIF_ERR_SUCCESS; /* 0 */
   libmemif_main_t *lm = &libmemif_main;
+  memset (lm, 0, sizeof (libmemif_main_t));
+
+  if (memif_alloc != NULL)
+    {
+      memif_alloc_register (memif_alloc);
+    }
+  else
+    memif_alloc_register (malloc);
+
+  if (memif_free != NULL)
+    memif_free_register (memif_free);
+  else
+    memif_free_register (free);
 
-  if (app_name)
+  if (app_name != NULL)
     {
-      lm->app_name = malloc (strlen (app_name) + sizeof (char));
-      memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
-      strncpy ((char *) lm->app_name, app_name, strlen (app_name));
+      uint8_t len = (strlen (app_name) > MEMIF_NAME_LEN)
+       ? strlen (app_name) : MEMIF_NAME_LEN;
+      strncpy ((char *) lm->app_name, app_name, len);
     }
   else
     {
-      lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME) + sizeof (char));
-      memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
       strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
               strlen (MEMIF_DEFAULT_APP_NAME));
     }
@@ -436,21 +472,39 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
       DBG ("libmemif event polling initialized");
     }
 
-  memset (&lm->ms, 0, sizeof (memif_socket_t));
-
   lm->control_list_len = 2;
   lm->interrupt_list_len = 2;
   lm->listener_list_len = 1;
   lm->pending_list_len = 1;
 
   lm->control_list =
-    malloc (sizeof (memif_list_elt_t) * lm->control_list_len);
+    lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len);
+  if (lm->control_list == NULL)
+    {
+      err = MEMIF_ERR_NOMEM;
+      goto error;
+    }
   lm->interrupt_list =
-    malloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
+    lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
+  if (lm->interrupt_list == NULL)
+    {
+      err = MEMIF_ERR_NOMEM;
+      goto error;
+    }
   lm->listener_list =
-    malloc (sizeof (memif_list_elt_t) * lm->listener_list_len);
+    lm->alloc (sizeof (memif_list_elt_t) * lm->listener_list_len);
+  if (lm->listener_list == NULL)
+    {
+      err = MEMIF_ERR_NOMEM;
+      goto error;
+    }
   lm->pending_list =
-    malloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
+    lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
+  if (lm->pending_list == NULL)
+    {
+      err = MEMIF_ERR_NOMEM;
+      goto error;
+    }
 
   int i;
   for (i = 0; i < lm->control_list_len; i++)
@@ -479,24 +533,27 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name)
   lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
   if (lm->timerfd < 0)
     {
-      err = errno;
-      DBG ("timerfd: %s", strerror (err));
-      return memif_syscall_error_handler (err);
+      err = memif_syscall_error_handler (errno);
+      goto error;
     }
 
   lm->arm.it_value.tv_sec = 2;
   lm->arm.it_value.tv_nsec = 0;
   lm->arm.it_interval.tv_sec = 2;
   lm->arm.it_interval.tv_nsec = 0;
-  memset (&lm->disarm, 0, sizeof (lm->disarm));
 
   if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0)
     {
       DBG ("callback type memif_control_fd_update_t error!");
-      return MEMIF_ERR_CB_FDUPDATE;
+      err = MEMIF_ERR_CB_FDUPDATE;
+      goto error;
     }
 
-  return 0;
+  return err;
+
+error:
+  memif_cleanup ();
+  return err;
 }
 
 static inline memif_ring_t *
@@ -538,6 +595,7 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
              memif_connection_update_t * on_disconnect,
              memif_interrupt_t * on_interrupt, void *private_ctx)
 {
+  libmemif_main_t *lm = &libmemif_main;
   int err, i, index, sockfd = -1;
   memif_list_elt_t list_elt;
   memif_connection_t *conn = (memif_connection_t *) * c;
@@ -546,20 +604,23 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
       DBG ("This handle already points to existing memif.");
       return MEMIF_ERR_CONN;
     }
-  conn = (memif_connection_t *) malloc (sizeof (memif_connection_t));
+  conn = (memif_connection_t *) lm->alloc (sizeof (memif_connection_t));
   if (conn == NULL)
     {
-      err = memif_syscall_error_handler (errno);
+      err = MEMIF_ERR_NOMEM;
       goto error;
     }
   memset (conn, 0, sizeof (memif_connection_t));
 
-  libmemif_main_t *lm = &libmemif_main;
-
   conn->args.interface_id = args->interface_id;
 
   if (args->log2_ring_size == 0)
     args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE;
+  else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE)
+    {
+      err = MEMIF_ERR_MAX_RING;
+      goto error;
+    }
   if (args->buffer_size == 0)
     args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE;
   if (args->num_s2m_rings == 0)
@@ -588,13 +649,14 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
   strncpy ((char *) conn->args.interface_name, (char *) args->interface_name,
           l);
 
-  l = strlen ((char *) args->instance_name);
-  strncpy ((char *) conn->args.instance_name, (char *) args->instance_name,
-          l);
-
   /* allocate and initialize socket_filename so it can be copyed to sun_path
      without memory leaks */
-  conn->args.socket_filename = malloc (sizeof (char *) * 108);
+  conn->args.socket_filename = lm->alloc (sizeof (char *) * 108);
+  if (conn->args.socket_filename == NULL)
+    {
+      err = MEMIF_ERR_NOMEM;
+      goto error;
+    }
   memset (conn->args.socket_filename, 0, 108 * sizeof (char *));
 
   if (args->socket_filename)
@@ -624,9 +686,8 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
               MEMIF_DEFAULT_SOCKET_FILENAME, sfl);
     }
 
-  if (args->secret)
+  if ((l = strlen ((char *) args->secret)) > 0)
     {
-      l = strlen ((char *) args->secret);
       strncpy ((char *) conn->args.secret, (char *) args->secret, l);
     }
 
@@ -665,10 +726,20 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
                    return memif_syscall_error_handler (errno);
                }
              DBG ("creating socket file");
-             ms = malloc (sizeof (memif_socket_t));
+             ms = lm->alloc (sizeof (memif_socket_t));
+             if (ms == NULL)
+               {
+                 err = MEMIF_ERR_NOMEM;
+                 goto error;
+               }
              ms->filename =
-               malloc (strlen ((char *) conn->args.socket_filename) +
-                       sizeof (char));
+               lm->alloc (strlen ((char *) conn->args.socket_filename) +
+                          sizeof (char));
+             if (ms->filename == NULL)
+               {
+                 err = MEMIF_ERR_NOMEM;
+                 goto error;
+               }
              memset (ms->filename, 0,
                      strlen ((char *) conn->args.socket_filename) +
                      sizeof (char));
@@ -677,7 +748,13 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
                       strlen ((char *) conn->args.socket_filename));
              ms->interface_list_len = 1;
              ms->interface_list =
-               malloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
+               lm->alloc (sizeof (memif_list_elt_t) *
+                          ms->interface_list_len);
+             if (ms->interface_list == NULL)
+               {
+                 err = MEMIF_ERR_NOMEM;
+                 goto error;
+               }
              ms->interface_list[0].key = -1;
              ms->interface_list[0].data_struct = NULL;
              struct sockaddr_un un = { 0 };
@@ -769,9 +846,9 @@ error:
     close (sockfd);
   sockfd = -1;
   if (conn->args.socket_filename)
-    free (conn->args.socket_filename);
+    lm->free (conn->args.socket_filename);
   if (conn != NULL)
-    free (conn);
+    lm->free (conn);
   *c = conn = NULL;
   return err;
 }
@@ -808,7 +885,7 @@ memif_control_fd_handler (int fd, uint8_t events)
 
              sun.sun_family = AF_UNIX;
 
-             strncpy (sun.sun_path, conn->args.socket_filename,
+             strncpy (sun.sun_path, (char*) conn->args.socket_filename,
                       sizeof (sun.sun_path) - 1);
 
              if (connect (sockfd, (struct sockaddr *) &sun,
@@ -983,12 +1060,12 @@ memif_cancel_poll_event ()
 }
 
 static void
-memif_msg_queue_free (memif_msg_queue_elt_t ** e)
+memif_msg_queue_free (libmemif_main_t * lm, memif_msg_queue_elt_t ** e)
 {
   if (*e == NULL)
     return;
-  memif_msg_queue_free (&(*e)->next);
-  free (*e);
+  memif_msg_queue_free (lm, &(*e)->next);
+  lm->free (*e);
   *e = NULL;
   return;
 }
@@ -1012,7 +1089,7 @@ memif_disconnect_internal (memif_connection_t * c)
 
   if (c->fd > 0)
     {
-      memif_msg_send_disconnect (c->fd, "interface deleted", 0);
+      memif_msg_send_disconnect (c->fd, (uint8_t *) "interface deleted", 0);
       lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL);
       close (c->fd);
     }
@@ -1041,7 +1118,7 @@ memif_disconnect_internal (memif_connection_t * c)
              mq->int_fd = -1;
            }
        }
-      free (c->tx_queues);
+      lm->free (c->tx_queues);
       c->tx_queues = NULL;
     }
 
@@ -1066,7 +1143,7 @@ memif_disconnect_internal (memif_connection_t * c)
              mq->int_fd = -1;
            }
        }
-      free (c->rx_queues);
+      lm->free (c->rx_queues);
       c->rx_queues = NULL;
     }
 
@@ -1077,13 +1154,13 @@ memif_disconnect_internal (memif_connection_t * c)
       if (c->regions[0].fd > 0)
        close (c->regions[0].fd);
       c->regions[0].fd = -1;
-      free (c->regions);
+      lm->free (c->regions);
       c->regions = NULL;
     }
 
   memset (&c->run_args, 0, sizeof (memif_conn_run_args_t));
 
-  memif_msg_queue_free (&c->msg_queue);
+  memif_msg_queue_free (lm, &c->msg_queue);
 
   if (!(c->args.is_master))
     {
@@ -1092,7 +1169,7 @@ memif_disconnect_internal (memif_connection_t * c)
          if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
            {
              err = memif_syscall_error_handler (errno);
-             DBG_UNIX ("timerfd_settime: arm");
+             DBG ("timerfd_settime: arm");
            }
        }
       lm->disconn_slaves++;
@@ -1143,11 +1220,11 @@ memif_delete (memif_conn_handle_t * conn)
                             c->listener_fd);
              close (c->listener_fd);
              c->listener_fd = ms->fd = -1;
-             free (ms->interface_list);
+             lm->free (ms->interface_list);
              ms->interface_list = NULL;
-             free (ms->filename);
+             lm->free (ms->filename);
              ms->filename = NULL;
-             free (ms);
+             lm->free (ms);
              ms = NULL;
            }
        }
@@ -1166,10 +1243,10 @@ memif_delete (memif_conn_handle_t * conn)
     }
 
   if (c->args.socket_filename)
-    free (c->args.socket_filename);
+    lm->free (c->args.socket_filename);
   c->args.socket_filename = NULL;
 
-  free (c);
+  lm->free (c);
   c = NULL;
 
   *conn = c;
@@ -1246,33 +1323,34 @@ int
 memif_init_regions_and_queues (memif_connection_t * conn)
 {
   memif_ring_t *ring = NULL;
-  uint64_t buffer_offset;
   memif_region_t *r;
   int i, j;
   libmemif_main_t *lm = &libmemif_main;
   memif_list_elt_t e;
 
-  conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t));
+  conn->regions = (memif_region_t *) lm->alloc (sizeof (memif_region_t));
   if (conn->regions == NULL)
-    return memif_syscall_error_handler (errno);
+    return MEMIF_ERR_NOMEM;
   r = conn->regions;
 
-  buffer_offset =
+  r->buffer_offset =
     (conn->run_args.num_s2m_rings +
      conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) +
                                      sizeof (memif_desc_t) *
                                      (1 << conn->run_args.log2_ring_size));
 
-  r->region_size = buffer_offset +
+  r->region_size = r->buffer_offset +
     conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) *
     (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings);
 
+  if ((r->fd =
+       memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
   if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1)
     return memif_syscall_error_handler (errno);
-/*
-    if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
-        return memif_syscall_error_handler (errno);
-*/
+
+  if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
+    return memif_syscall_error_handler (errno);
+
   if ((ftruncate (r->fd, r->region_size)) == -1)
     return memif_syscall_error_handler (errno);
 
@@ -1291,9 +1369,9 @@ memif_init_regions_and_queues (memif_connection_t * conn)
        {
          uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j;
          ring->desc[j].region = 0;
-         ring->desc[j].offset = buffer_offset +
+         ring->desc[j].offset = r->buffer_offset +
            (uint32_t) (slot * conn->run_args.buffer_size);
-         ring->desc[j].buffer_length = conn->run_args.buffer_size;
+         ring->desc[j].length = conn->run_args.buffer_size;
        }
     }
   for (i = 0; i < conn->run_args.num_m2s_rings; i++)
@@ -1310,17 +1388,17 @@ memif_init_regions_and_queues (memif_connection_t * conn)
             conn->run_args.num_s2m_rings) *
            (1 << conn->run_args.log2_ring_size) + j;
          ring->desc[j].region = 0;
-         ring->desc[j].offset = buffer_offset +
+         ring->desc[j].offset = r->buffer_offset +
            (uint32_t) (slot * conn->run_args.buffer_size);
-         ring->desc[j].buffer_length = conn->run_args.buffer_size;
+         ring->desc[j].length = conn->run_args.buffer_size;
        }
     }
   memif_queue_t *mq;
   mq =
-    (memif_queue_t *) malloc (sizeof (memif_queue_t) *
-                             conn->run_args.num_s2m_rings);
+    (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
+                                conn->run_args.num_s2m_rings);
   if (mq == NULL)
-    return memif_syscall_error_handler (errno);
+    return MEMIF_ERR_NOMEM;
   int x;
   for (x = 0; x < conn->run_args.num_s2m_rings; x++)
     {
@@ -1343,10 +1421,10 @@ memif_init_regions_and_queues (memif_connection_t * conn)
   conn->tx_queues = mq;
 
   mq =
-    (memif_queue_t *) malloc (sizeof (memif_queue_t) *
-                             conn->run_args.num_m2s_rings);
+    (memif_queue_t *) lm->alloc (sizeof (memif_queue_t) *
+                                conn->run_args.num_m2s_rings);
   if (mq == NULL)
-    return memif_syscall_error_handler (errno);
+    return MEMIF_ERR_NOMEM;
   for (x = 0; x < conn->run_args.num_m2s_rings; x++)
     {
       if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0)
@@ -1370,131 +1448,203 @@ memif_init_regions_and_queues (memif_connection_t * conn)
   return 0;
 }
 
+int
+memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid,
+                    memif_buffer_t * bufs, uint16_t count,
+                    uint16_t * count_out)
+{
+  memif_connection_t *c = (memif_connection_t *) conn;
+  if (EXPECT_FALSE (c == NULL))
+    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))
+    return MEMIF_ERR_QID;
+  if (EXPECT_FALSE (!count_out))
+    return MEMIF_ERR_INVAL_ARG;
+  if (EXPECT_FALSE (c->args.is_master))
+    return MEMIF_ERR_INVAL_ARG;
+
+  memif_queue_t *mq = &c->tx_queues[qid];
+  memif_ring_t *ring = mq->ring;
+  memif_buffer_t *b0;
+  uint16_t mask = (1 << mq->log2_ring_size) - 1;
+  uint16_t ring_size;
+  uint16_t slot, ns;
+  int i, err = MEMIF_ERR_SUCCESS;      /* 0 */
+  *count_out = 0;
+
+  ring_size = (1 << mq->log2_ring_size);
+  ns = ring->tail - mq->last_tail;
+  mq->last_tail += ns;
+  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 + mq->last_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->shm);
+      ring->desc[slot & mask].flags |=
+       (b0->flags & MEMIF_BUFFER_FLAG_NEXT) ? MEMIF_DESC_FLAG_NEXT : 0;
+
+      b0->desc_index = slot;
+
+      mq->alloc_bufs++;
+      slot++;
+
+      count--;
+      ns--;
+      b0++;
+      *count_out += 1;
+    }
+
+  DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
+       mq->alloc_bufs);
+
+  if (count)
+    {
+      DBG ("ring buffer full! qid: %u", qid);
+      err = MEMIF_ERR_NOBUF_RING;
+    }
+
+error:
+  return err;
+}
+
 int
 memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
                    memif_buffer_t * bufs, uint16_t count,
                    uint16_t * count_out, uint16_t size)
 {
   memif_connection_t *c = (memif_connection_t *) conn;
-  if (c == NULL)
+  if (EXPECT_FALSE (c == NULL))
     return MEMIF_ERR_NOCONN;
-  if (c->fd < 0)
+  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 (qid >= num)
+  if (EXPECT_FALSE (qid >= num))
     return MEMIF_ERR_QID;
+  if (EXPECT_FALSE (!count_out))
+    return MEMIF_ERR_INVAL_ARG;
+
   memif_queue_t *mq = &c->tx_queues[qid];
   memif_ring_t *ring = mq->ring;
   memif_buffer_t *b0, *b1;
-  uint8_t chain_buf0, chain_buf1;
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
-  uint16_t s0, s1, ns;
-  *count_out = 0;
+  uint16_t ring_size;
+  uint16_t slot, ns;
   int i, err = MEMIF_ERR_SUCCESS;      /* 0 */
+  uint16_t dst_left, src_left;
+  uint16_t saved_count;
+  memif_buffer_t *saved_b;
+  *count_out = 0;
 
-  if (ring->tail != ring->head)
-    {
-      if (ring->head > ring->tail)
-       ns = (1 << mq->log2_ring_size) - ring->head + ring->tail;
-      else
-       ns = ring->tail - ring->head;
-    }
-  else
-    ns = (1 << mq->log2_ring_size);
+  ring_size = (1 << mq->log2_ring_size);
+  ns = ring->tail - mq->last_tail;
+  mq->last_tail += ns;
+  slot = (c->args.is_master) ? ring->tail : ring->head;
+  slot += mq->alloc_bufs;
 
-  /* (head == tail) ? receive function will asume that no packets are available */
-  ns -= 1;
+  if (c->args.is_master)
+    ns = ring->head + mq->alloc_bufs - ring->tail;
+  else
+    ns = ring_size - ring->head + mq->alloc_bufs + mq->last_tail;
 
   while (count && ns)
     {
-      while ((count > 2) && (ns > 2))
+      b0 = (bufs + *count_out);
+
+      saved_b = b0;
+      saved_count = count;
+
+      b0->desc_index = slot;
+      ring->desc[slot & 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;
+      src_left = size;
+
+      while (src_left)
        {
-         s0 = (ring->head + mq->alloc_bufs) & mask;
-         chain_buf0 = size / ring->desc[s0].buffer_length;
-         if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
-           chain_buf0++;
-
-         if (chain_buf0 > ns)
-           break;
-
-         s1 = (ring->head + mq->alloc_bufs + chain_buf0) & mask;
-         chain_buf1 = size / ring->desc[s1].buffer_length;
-         if (((size % ring->desc[s1].buffer_length) != 0) || (size == 0))
-           chain_buf1++;
-
-         if ((chain_buf0 + chain_buf1) > ns)
-           break;
-
-         b0 = (bufs + *count_out);
-         b1 = (bufs + *count_out + 1);
-
-         b0->desc_index = s0;
-         b1->desc_index = s1;
-         ring->desc[s0].flags = 0;
-         ring->desc[s1].flags = 0;
-         b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0;
-         b1->buffer_len = ring->desc[s1].buffer_length * chain_buf1;
-         /* TODO: support multiple regions -> ring descriptor contains region index */
-         b0->data = c->regions->shm + ring->desc[s0].offset;
-         b1->data = c->regions->shm + ring->desc[s1].offset;
-
-         for (i = 0; i < (memif_min (chain_buf0, chain_buf1) - 1); i++)
+         if (EXPECT_FALSE (dst_left == 0))
            {
-             ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
-             ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
-             DBG ("allocating chained buffers");
+             if (count && ns)
+               {
+                 slot++;
+                 *count_out += 1;
+                 mq->alloc_bufs++;
+                 ns--;
+                 count--;
+
+                 ring->desc[b0->desc_index & mask].flags |=
+                   MEMIF_DESC_FLAG_NEXT;
+                 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;
+               }
+             else
+               {
+                 /* rollback allocated chain buffers */
+                 memset (saved_b, 0, sizeof (memif_buffer_t)
+                         * (saved_count - count + 1));
+                 *count_out -= saved_count - count;
+                 mq->alloc_bufs = saved_count - count;
+                 goto no_ns;
+               }
            }
+         b0->len = memif_min (dst_left, src_left);
 
-         if (chain_buf0 > chain_buf1)
+         /* slave resets buffer offset */
+         if (c->args.is_master == 0)
            {
-             for (; i < (chain_buf0 - 1); i++)
-               ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
+             uint16_t x =
+               (ring->desc[slot & mask].offset -
+                c->regions->buffer_offset) / c->run_args.buffer_size;
+             ring->desc[slot & mask].offset =
+               c->regions->buffer_offset + (x * c->run_args.buffer_size);
            }
-         else
-           {
-             for (; i < (chain_buf1 - 1); i++)
-               ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
-           }
-
-         mq->alloc_bufs += chain_buf0 + chain_buf1;
-
-         DBG ("allocated ring slots %u, %u", s0, s1);
-         count -= 2;
-         ns -= chain_buf0 + chain_buf1;
-         *count_out += 2;
-       }
-      s0 = (ring->head + mq->alloc_bufs) & mask;
-
-      b0 = (bufs + *count_out);
-
-      chain_buf0 = size / ring->desc[s0].buffer_length;
-      if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
-       chain_buf0++;
-
-      if (chain_buf0 > ns)
-       break;
 
-      b0->desc_index = s0;
-      ring->desc[s0].flags = 0;
-      b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0;
-      b0->data = c->regions->shm + ring->desc[s0].offset;
+         b0->data = c->regions->shm + ring->desc[slot & mask].offset;
 
-      for (i = 0; i < (chain_buf0 - 1); i++)
-       {
-         ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
-         DBG ("allocating chained buffers");
+         src_left -= b0->len;
+         dst_left -= b0->len;
        }
 
-      mq->alloc_bufs += chain_buf0;
-
-      DBG ("allocated ring slot %u", s0);
-      count--;
-      ns -= chain_buf0;
+      slot++;
       *count_out += 1;
+      mq->alloc_bufs++;
+      ns--;
+      count--;
     }
 
+no_ns:
+
   DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
        mq->alloc_bufs);
 
@@ -1504,74 +1654,61 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
       err = MEMIF_ERR_NOBUF_RING;
     }
 
+error:
   return err;
 }
 
 int
-memif_buffer_free (memif_conn_handle_t conn, uint16_t qid,
-                  memif_buffer_t * bufs, uint16_t count,
-                  uint16_t * count_out)
+memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
+                   uint16_t headroom)
 {
   memif_connection_t *c = (memif_connection_t *) conn;
-  if (c == NULL)
+  if (EXPECT_FALSE (c == NULL))
     return MEMIF_ERR_NOCONN;
-  if (c->fd < 0)
+  if (EXPECT_FALSE (c->fd < 0))
     return MEMIF_ERR_DISCONNECTED;
   uint8_t num =
     (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
     num_m2s_rings;
-  if (qid >= num)
+  if (EXPECT_FALSE (qid >= num))
     return MEMIF_ERR_QID;
   libmemif_main_t *lm = &libmemif_main;
   memif_queue_t *mq = &c->rx_queues[qid];
   memif_ring_t *ring = mq->ring;
-  uint16_t tail = ring->tail;
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
-  uint8_t chain_buf0, chain_buf1;
-  memif_buffer_t *b0, *b1;
-  *count_out = 0;
+  uint16_t slot;
 
-  if (mq->alloc_bufs < count)
-    count = mq->alloc_bufs;
+  if (c->args.is_master)
+    {
+      MEMIF_MEMORY_BARRIER ();
+      ring->tail =
+       (ring->tail + count <=
+        mq->last_head) ? ring->tail + count : mq->last_head;
+      return MEMIF_ERR_SUCCESS;
+    }
 
-  while (count)
+  uint16_t head = ring->head;
+  uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail;
+  head += (count < ns) ? count : ns;
+
+  if (headroom)
     {
-      while (count > 2)
+      slot = (c->args.is_master) ? ring->head : ring->tail;
+      while (slot < head)
        {
-         b0 = (bufs + *count_out);
-         b1 = (bufs + *count_out + 1);
-         chain_buf0 =
-           b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
-         if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
-             0)
-           chain_buf0++;
-         chain_buf1 =
-           b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
-         if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
-             0)
-           chain_buf1++;
-         tail = (b1->desc_index + chain_buf1) & mask;
-         b0->data = NULL;
-         b1->data = NULL;
-
-         count -= 2;
-         *count_out += 2;
-         mq->alloc_bufs -= chain_buf0 + chain_buf1;
+         uint16_t x =
+           (ring->desc[slot & mask].offset -
+            c->regions->buffer_offset) / c->run_args.buffer_size;
+         ring->desc[slot & mask].offset =
+           c->regions->buffer_offset + (x * c->run_args.buffer_size) +
+           headroom;
+
+         slot++;
        }
-      b0 = (bufs + *count_out);
-      chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
-      if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
-       chain_buf0++;
-      tail = (b0->desc_index + chain_buf0) & mask;
-      b0->data = NULL;
-
-      count--;
-      *count_out += 1;
-      mq->alloc_bufs -= chain_buf0;
     }
+
   MEMIF_MEMORY_BARRIER ();
-  ring->tail = tail;
-  DBG ("tail: %u", ring->tail);
+  ring->head = head;
 
   return MEMIF_ERR_SUCCESS;    /* 0 */
 }
@@ -1581,211 +1718,57 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
                memif_buffer_t * bufs, uint16_t count, uint16_t * tx)
 {
   memif_connection_t *c = (memif_connection_t *) conn;
-  if (c == NULL)
+  if (EXPECT_FALSE (c == NULL))
     return MEMIF_ERR_NOCONN;
-  if (c->fd < 0)
+  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 (qid >= num)
+  if (EXPECT_FALSE (qid >= num))
     return MEMIF_ERR_QID;
+  if (EXPECT_FALSE (!tx))
+    return MEMIF_ERR_INVAL_ARG;
+
   memif_queue_t *mq = &c->tx_queues[qid];
   memif_ring_t *ring = mq->ring;
-  uint16_t head = ring->head;
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
-  uint8_t chain_buf0, chain_buf1;
+  memif_buffer_t *b0;
   *tx = 0;
-  uint16_t curr_buf = 0;
-  memif_buffer_t *b0, *b1;
-  int i;
 
-  while (count)
-    {
-      while (count > 2)
-       {
-         b0 = (bufs + curr_buf);
-         b1 = (bufs + curr_buf + 1);
-         chain_buf0 =
-           b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
-         if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) !=
-             0)
-           chain_buf0++;
-
-         chain_buf1 =
-           b1->buffer_len / ring->desc[b1->desc_index].buffer_length;
-         if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
-             0)
-           chain_buf1++;
-
-         for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++)
-           {
-             /* b0 */
-             if (b0->data_len >
-                 ring->desc[(b0->desc_index + i) & mask].buffer_length)
-               {
-                 b0->data_len -=
-                   ring->desc[(b0->desc_index + i) & mask].length =
-                   ring->desc[(b0->desc_index + i) & mask].buffer_length;
-               }
-             else
-               {
-                 ring->desc[(b0->desc_index + i) & mask].length =
-                   b0->data_len;
-                 b0->data_len = 0;
-               }
-             /* b1 */
-             if (b1->data_len >
-                 ring->desc[(b1->desc_index + i) & mask].buffer_length)
-               {
-                 b1->data_len -=
-                   ring->desc[(b1->desc_index + i) & mask].length =
-                   ring->desc[(b1->desc_index + i) & mask].buffer_length;
-               }
-             else
-               {
-                 ring->desc[(b1->desc_index + i) & mask].length =
-                   b1->data_len;
-                 b1->data_len = 0;
-               }
-#ifdef MEMIF_DBG_SHM
-             print_bytes (b0->data +
-                          ring->desc[(b0->desc_index +
-                                      i) & mask].buffer_length *
-                          (chain_buf0 - 1),
-                          ring->desc[(b0->desc_index +
-                                      i) & mask].buffer_length, DBG_TX_BUF);
-             print_bytes (b1->data +
-                          ring->desc[(b1->desc_index +
-                                      i) & mask].buffer_length *
-                          (chain_buf1 - 1),
-                          ring->desc[(b1->desc_index +
-                                      i) & mask].buffer_length, DBG_TX_BUF);
-#endif /* MEMIF_DBG_SHM */
-           }
-
-         if (chain_buf0 > chain_buf1)
-           {
-             for (; i < chain_buf0; i++)
-               {
-                 if (b0->data_len >
-                     ring->desc[(b0->desc_index + i) & mask].buffer_length)
-                   {
-                     b0->data_len -=
-                       ring->desc[(b0->desc_index + i) & mask].length =
-                       ring->desc[(b0->desc_index + i) & mask].buffer_length;
-                   }
-                 else
-                   {
-                     ring->desc[(b0->desc_index + i) & mask].length =
-                       b0->data_len;
-                     b0->data_len = 0;
-                   }
-#ifdef MEMIF_DBG_SHM
-                 print_bytes (b0->data +
-                              ring->desc[(b0->desc_index +
-                                          i) & mask].buffer_length *
-                              (chain_buf0 - 1),
-                              ring->desc[(b0->desc_index +
-                                          i) & mask].buffer_length,
-                              DBG_TX_BUF);
-#endif /* MEMIF_DBG_SHM */
-               }
-           }
-         else
-           {
-             for (; i < chain_buf1; i++)
-               {
-                 if (b1->data_len >
-                     ring->desc[(b1->desc_index + i) & mask].buffer_length)
-                   {
-                     b1->data_len -=
-                       ring->desc[(b1->desc_index + i) & mask].length =
-                       ring->desc[(b1->desc_index + i) & mask].buffer_length;
-                   }
-                 else
-                   {
-                     ring->desc[(b1->desc_index + i) & mask].length =
-                       b1->data_len;
-                     b1->data_len = 0;
-                   }
-#ifdef MEMIF_DBG_SHM
-                 print_bytes (b1->data +
-                              ring->desc[(b1->desc_index +
-                                          i) & mask].buffer_length *
-                              (chain_buf1 - 1),
-                              ring->desc[(b1->desc_index +
-                                          i) & mask].buffer_length,
-                              DBG_TX_BUF);
-#endif /* MEMIF_DBG_SHM */
-               }
-           }
+  if (count > mq->alloc_bufs)
+    count = mq->alloc_bufs;
 
-         head = (b1->desc_index + chain_buf1) & mask;
-
-         b0->data = NULL;
-#ifdef MEMIF_DBG
-         if (b0->data_len != 0)
-           DBG ("invalid b0 data length!");
-#endif /* MEMIF_DBG */
-         b1->data = NULL;
-#ifdef MEMIF_DBG
-         if (b1->data_len != 0)
-           DBG ("invalid b1 data length!");
-#endif /* MEMIF_DBG */
-
-         count -= 2;
-         *tx += chain_buf0 + chain_buf1;
-         curr_buf += 2;
-       }
+  if (EXPECT_FALSE (count == 0))
+    return MEMIF_ERR_SUCCESS;
 
-      b0 = (bufs + curr_buf);
-      chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length;
-      if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0)
-       chain_buf0++;
+  while (count)
+    {
+      b0 = (bufs + *tx);
+      ring->desc[b0->desc_index & mask].length = b0->len;
 
-      for (i = 0; i < chain_buf0; i++)
-       {
-         if (b0->data_len >
-             ring->desc[(b0->desc_index + i) & mask].buffer_length)
-           {
-             b0->data_len -= ring->desc[(b0->desc_index + i) & mask].length =
-               ring->desc[(b0->desc_index + i) & mask].buffer_length;
-           }
-         else
-           {
-             ring->desc[(b0->desc_index + i) & mask].length = b0->data_len;
-             b0->data_len = 0;
-           }
 #ifdef MEMIF_DBG_SHM
-         print_bytes (b0->data +
-                      ring->desc[(b0->desc_index + i) & mask].buffer_length *
-                      (chain_buf0 - 1),
-                      ring->desc[(b0->desc_index + i) & mask].buffer_length,
-                      DBG_TX_BUF);
+      printf ("offset: %-6d\n", ring->desc[b0->desc_index & mask].offset);
+      printf ("data: %p\n",
+             memif_get_buffer (c, ring, b0->desc_index & mask));
+      printf ("index: %u\n", b0->desc_index);
+      print_bytes (memif_get_buffer (c, ring, b0->desc_index & mask),
+                  ring->desc[b0->desc_index & mask].length, DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
-       }
-
-      head = (b0->desc_index + chain_buf0) & mask;
-
-      b0->data = NULL;
-#ifdef MEMIF_DBG
-      if (b0->data_len != 0)
-       DBG ("invalid b0 data length!");
-#endif /* MEMIF_DBG */
 
+      *tx += 1;
       count--;
-      *tx += chain_buf0;
-      curr_buf++;
     }
+
+
   MEMIF_MEMORY_BARRIER ();
-  ring->head = head;
+  if (c->args.is_master)
+    ring->tail = b0->desc_index + 1;
+  else
+    ring->head = b0->desc_index + 1;
 
   mq->alloc_bufs -= *tx;
 
-  /* TODO: return num of buffers and packets */
-  *tx = curr_buf;
-
   if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
     {
       uint64_t a = 1;
@@ -1802,155 +1785,76 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
                memif_buffer_t * bufs, uint16_t count, uint16_t * rx)
 {
   memif_connection_t *c = (memif_connection_t *) conn;
-  if (c == NULL)
+  if (EXPECT_FALSE (c == NULL))
     return MEMIF_ERR_NOCONN;
-  if (c->fd < 0)
+  if (EXPECT_FALSE (c->fd < 0))
     return MEMIF_ERR_DISCONNECTED;
   uint8_t num =
     (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
     num_m2s_rings;
-  if (qid >= num)
+  if (EXPECT_FALSE (qid >= num))
     return MEMIF_ERR_QID;
+  if (EXPECT_FALSE (!rx))
+    return MEMIF_ERR_INVAL_ARG;
+
   memif_queue_t *mq = &c->rx_queues[qid];
   memif_ring_t *ring = mq->ring;
-  uint16_t head = ring->head;
+  uint16_t cur_slot, last_slot;
   uint16_t ns;
   uint16_t mask = (1 << mq->log2_ring_size) - 1;
   memif_buffer_t *b0, *b1;
-  uint16_t curr_buf = 0;
   *rx = 0;
-  int i;
 
   uint64_t b;
   ssize_t r = read (mq->int_fd, &b, sizeof (b));
-  if ((r == -1) && (errno != EAGAIN))
+  if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN)))
     return memif_syscall_error_handler (errno);
 
-  if (head == mq->last_head)
-    return 0;
+  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;
 
-  if (head > mq->last_head)
-    ns = head - mq->last_head;
-  else
-    ns = (1 << mq->log2_ring_size) - mq->last_head + head;
+  ns = last_slot - cur_slot;
 
   while (ns && count)
     {
-      DBG ("ns: %u, count: %u", ns, count);
-      while ((ns > 2) && (count > 2))
-       {
-         b0 = (bufs + curr_buf);
-         b1 = (bufs + curr_buf + 1);
-
-         b0->desc_index = mq->last_head;
-         i = 0;
-         b0->data_len = 0;
-         b0->buffer_len = 0;
-
-         b0->data = memif_get_buffer (conn, ring, mq->last_head);
-         b0->data_len += ring->desc[mq->last_head].length;
-         b0->buffer_len += ring->desc[mq->last_head].buffer_length;
-#ifdef MEMIF_DBG_SHM
-         print_bytes (b0->data +
-                      ring->desc[b0->desc_index].buffer_length * i++,
-                      ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
-#endif /* MEMIF_DBG_SHM */
-         ns--;
-         *rx += 1;
-         while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
-           {
-             ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
-             mq->last_head = (mq->last_head + 1) & mask;
-             b0->data_len += ring->desc[mq->last_head].length;
-             b0->buffer_len += ring->desc[mq->last_head].buffer_length;
-#ifdef MEMIF_DBG_SHM
-             print_bytes (b0->data +
-                          ring->desc[b0->desc_index].buffer_length * i++,
-                          ring->desc[b0->desc_index].buffer_length,
-                          DBG_TX_BUF);
-#endif /* MEMIF_DBG_SHM */
-             ns--;
-             *rx += 1;
-           }
-         mq->last_head = (mq->last_head + 1) & mask;
+      b0 = (bufs + *rx);
 
-         b1->desc_index = mq->last_head;
-         i = 0;
-         b1->data_len = 0;
-         b1->buffer_len = 0;
-
-         b1->data = memif_get_buffer (conn, ring, mq->last_head);
-         b1->data_len += ring->desc[mq->last_head].length;
-         b1->buffer_len += ring->desc[mq->last_head].buffer_length;
-#ifdef MEMIF_DBG_SHM
-         print_bytes (b1->data +
-                      ring->desc[b1->desc_index].buffer_length * i++,
-                      ring->desc[b1->desc_index].buffer_length, DBG_TX_BUF);
-#endif /* MEMIF_DBG_SHM */
-         ns--;
-         *rx += 1;
-         while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
-           {
-             ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
-             mq->last_head = (mq->last_head + 1) & mask;
-             b1->data_len += ring->desc[mq->last_head].length;
-             b1->buffer_len += ring->desc[mq->last_head].buffer_length;
-#ifdef MEMIF_DBG_SHM
-             print_bytes (b1->data +
-                          ring->desc[b1->desc_index].buffer_length * i++,
-                          ring->desc[b1->desc_index].buffer_length,
-                          DBG_TX_BUF);
-#endif /* MEMIF_DBG_SHM */
-             ns--;
-             *rx += 1;
-           }
-         mq->last_head = (mq->last_head + 1) & mask;
-
-         count -= 2;
-         curr_buf += 2;
+      b0->desc_index = cur_slot;
+      b0->data = memif_get_buffer (c, ring, cur_slot & mask);
+      b0->len = ring->desc[cur_slot & mask].length;
+      /* slave resets buffer length */
+      if (c->args.is_master == 0)
+       {
+         ring->desc[cur_slot & mask].length = c->run_args.buffer_size;
        }
-      b0 = (bufs + curr_buf);
 
-      b0->desc_index = mq->last_head;
-      i = 0;
-      b0->data_len = 0;
-      b0->buffer_len = 0;
-
-      b0->data = memif_get_buffer (conn, ring, mq->last_head);
-      b0->data_len += ring->desc[mq->last_head].length;
-      b0->buffer_len += ring->desc[mq->last_head].buffer_length;
+      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;
 #ifdef MEMIF_DBG_SHM
-      print_bytes (b0->data +
-                  ring->desc[b0->desc_index].buffer_length * i++,
-                  ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
+      printf ("data: %p\n", b0->data);
+      printf ("index: %u\n", b0->desc_index);
+      printf ("ring: %p\n", b0->ring);
+      print_bytes (b0->data, b0->len, DBG_RX_BUF);
 #endif /* MEMIF_DBG_SHM */
       ns--;
       *rx += 1;
 
-      while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT)
-       {
-         ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT;
-         mq->last_head = (mq->last_head + 1) & mask;
-         b0->data_len += ring->desc[mq->last_head].length;
-         b0->buffer_len += ring->desc[mq->last_head].buffer_length;
-#ifdef MEMIF_DBG_SHM
-         print_bytes (b0->data +
-                      ring->desc[b0->desc_index].buffer_length * i++,
-                      ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
-#endif /* MEMIF_DBG_SHM */
-         ns--;
-         *rx += 1;
-       }
-      mq->last_head = (mq->last_head + 1) & mask;
-
       count--;
-      curr_buf++;
+      cur_slot++;
     }
 
-  mq->alloc_bufs += *rx;
-
-  /* TODO: return num of buffers and packets */
-  *rx = curr_buf;
+  if (c->args.is_master)
+    mq->last_head = cur_slot;
+  else
+    mq->last_tail = cur_slot;
 
   if (ns)
     {
@@ -1965,6 +1869,7 @@ int
 memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
                   char *buf, ssize_t buflen)
 {
+  libmemif_main_t *lm = &libmemif_main;
   memif_connection_t *c = (memif_connection_t *) conn;
   if (c == NULL)
     return MEMIF_ERR_NOCONN;
@@ -1976,16 +1881,16 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
   l1 = strlen ((char *) c->args.interface_name);
   if (l0 + l1 < buflen)
     {
-      md->if_name = strcpy (buf + l0, (char *) c->args.interface_name);
+      md->if_name = (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name);
       l0 += l1 + 1;
     }
   else
     err = MEMIF_ERR_NOBUF_DET;
 
-  l1 = strlen ((char *) c->args.instance_name);
+  l1 = strlen ((char *) lm->app_name);
   if (l0 + l1 < buflen)
     {
-      md->inst_name = strcpy (buf + l0, (char *) c->args.instance_name);
+      md->inst_name = (uint8_t *) strcpy (buf + l0, (char *) lm->app_name);
       l0 += l1 + 1;
     }
   else
@@ -1994,7 +1899,7 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
   l1 = strlen ((char *) c->remote_if_name);
   if (l0 + l1 < buflen)
     {
-      md->remote_if_name = strcpy (buf + l0, (char *) c->remote_if_name);
+      md->remote_if_name = (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name);
       l0 += l1 + 1;
     }
   else
@@ -2003,7 +1908,7 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
   l1 = strlen ((char *) c->remote_name);
   if (l0 + l1 < buflen)
     {
-      md->remote_inst_name = strcpy (buf + l0, (char *) c->remote_name);
+      md->remote_inst_name = (uint8_t *) strcpy (buf + l0, (char *) c->remote_name);
       l0 += l1 + 1;
     }
   else
@@ -2011,12 +1916,12 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
 
   md->id = c->args.interface_id;
 
-  if (c->args.secret)
+  if (strlen((char *) c->args.secret) > 0)
     {
       l1 = strlen ((char *) c->args.secret);
       if (l0 + l1 < buflen)
        {
-         md->secret = strcpy (buf + l0, (char *) c->args.secret);
+         md->secret = (uint8_t *) strcpy (buf + l0, (char *) c->args.secret);
          l0 += l1 + 1;
        }
       else
@@ -2030,7 +1935,7 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
   if (l0 + l1 < buflen)
     {
       md->socket_filename =
-       strcpy (buf + l0, (char *) c->args.socket_filename);
+       (uint8_t *) strcpy (buf + l0, (char *) c->args.socket_filename);
       l0 += l1 + 1;
     }
   else
@@ -2053,6 +1958,9 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
     {
       md->rx_queues[i].qid = i;
       md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size);
+      md->rx_queues[i].flags = c->rx_queues[i].ring->flags;
+      md->rx_queues[i].head = c->rx_queues[i].ring->head;
+      md->rx_queues[i].tail = c->rx_queues[i].ring->tail;
       md->rx_queues[i].buffer_size = c->run_args.buffer_size;
     }
 
@@ -2073,6 +1981,9 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
     {
       md->tx_queues[i].qid = i;
       md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size);
+      md->tx_queues[i].flags = c->tx_queues[i].ring->flags;
+      md->tx_queues[i].head = c->tx_queues[i].ring->head;
+      md->tx_queues[i].tail = c->tx_queues[i].ring->tail;
       md->tx_queues[i].buffer_size = c->run_args.buffer_size;
     }
 
@@ -2105,20 +2016,17 @@ int
 memif_cleanup ()
 {
   libmemif_main_t *lm = &libmemif_main;
-  if (lm->app_name)
-    free (lm->app_name);
-  lm->app_name = NULL;
   if (lm->control_list)
-    free (lm->control_list);
+    lm->free (lm->control_list);
   lm->control_list = NULL;
   if (lm->interrupt_list)
-    free (lm->interrupt_list);
+    lm->free (lm->interrupt_list);
   lm->interrupt_list = NULL;
   if (lm->listener_list)
-    free (lm->listener_list);
+    lm->free (lm->listener_list);
   lm->listener_list = NULL;
   if (lm->pending_list)
-    free (lm->pending_list);
+    lm->free (lm->pending_list);
   lm->pending_list = NULL;
   if (poll_cancel_fd != -1)
     close (poll_cancel_fd);