libmemif: version 2

Change-Id: Ia2532695aa9199d2a7b684aebef43df0b8235531
Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
diff --git a/extras/libmemif/examples/icmp_responder-epoll/main.c b/extras/libmemif/examples/icmp_responder-epoll/main.c
index 634e180..6b977a4 100644
--- a/extras/libmemif/examples/icmp_responder-epoll/main.c
+++ b/extras/libmemif/examples/icmp_responder-epoll/main.c
@@ -319,7 +319,7 @@
   memif_connection_t *c = &memif_connection[index];
   int err;
   /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */
-  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 0);
+  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 128);
   if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
     {
       INFO ("memif_buffer_alloc: %s", memif_strerror (err));
@@ -361,10 +361,10 @@
     }
 
   int err = MEMIF_ERR_SUCCESS, ret_val;
-  uint16_t rx, tx;
-  uint16_t fb;
-  int i;			/* rx buffer iterator */
-  int j;			/* tx bufferiterator */
+  uint16_t rx = 0, tx = 0;
+  uint16_t fb = 0;
+  int i = 0;			/* rx buffer iterator */
+  int j = 0;			/* tx bufferiterator */
 
   /* loop while there are packets in shm */
   do
@@ -380,54 +380,34 @@
 	}
 
       i = 0;
-
-      /* try to alloc required number of buffers buffers */
-      err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0);
+      err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
       if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
 	{
 	  INFO ("memif_buffer_alloc: %s", memif_strerror (err));
 	  goto error;
 	}
       j = 0;
-      c->tx_err_counter = rx - tx;
+      c->tx_err_counter += rx - tx;
 
-      /* process bufers */
       while (tx)
 	{
-	  while (tx > 2)
-	    {
-	      resolve_packet ((void *) (c->rx_bufs + i)->data,
-			      (c->rx_bufs + i)->data_len,
-			      (void *) (c->tx_bufs + j)->data,
-			      &(c->tx_bufs + j)->data_len, c->ip_addr);
-	      resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
-			      (c->rx_bufs + i + 1)->data_len,
-			      (void *) (c->tx_bufs + j + 1)->data,
-			      &(c->tx_bufs + j + 1)->data_len, c->ip_addr);
-
-	      i += 2;
-	      j += 2;
-	      tx -= 2;
-	    }
 	  resolve_packet ((void *) (c->rx_bufs + i)->data,
-			  (c->rx_bufs + i)->data_len,
+			  (c->rx_bufs + i)->len,
 			  (void *) (c->tx_bufs + j)->data,
-			  &(c->tx_bufs + j)->data_len, c->ip_addr);
+			  &(c->tx_bufs + j)->len, c->ip_addr);
 	  i++;
 	  j++;
 	  tx--;
 	}
-      /* mark memif buffers and shared memory buffers as free */
-      /* free processed buffers */
-      err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+
+      err = memif_refill_queue (c->conn, qid, rx);
       if (err != MEMIF_ERR_SUCCESS)
 	INFO ("memif_buffer_free: %s", memif_strerror (err));
-      rx -= fb;
+      rx -= rx;
 
       DBG ("freed %d buffers. %u/%u alloc/free buffers",
-	   fb, rx, MAX_MEMIF_BUFS - rx);
+	   rx, rx, MAX_MEMIF_BUFS - rx);
 
-      /* transmit allocated buffers */
       err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx);
       if (err != MEMIF_ERR_SUCCESS)
 	{
@@ -442,10 +422,10 @@
   return 0;
 
 error:
-  err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+  err = memif_refill_queue (c->conn, qid, rx);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
-  c->rx_buf_num -= fb;
+  c->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
        fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
   return 0;
@@ -466,7 +446,7 @@
     }
 
   int err = MEMIF_ERR_SUCCESS, ret_val;
-  uint16_t rx, tx;
+  uint16_t rx = 0, tx = 0;
   uint16_t fb;
   int i;			/* rx buffer iterator */
   int j;			/* tx bufferiterator */
@@ -492,7 +472,7 @@
 	{
 
 	  /* try to alloc required number of buffers buffers */
-	  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0);
+	  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 128);
 	  if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
 	    {
 	      INFO ("memif_buffer_alloc: %s", memif_strerror (err));
@@ -506,33 +486,32 @@
 	      while (tx > 2)
 		{
 		  resolve_packet ((void *) (c->rx_bufs + i)->data,
-				  (c->rx_bufs + i)->data_len,
+				  (c->rx_bufs + i)->len,
 				  (void *) (c->tx_bufs + j)->data,
-				  &(c->tx_bufs + j)->data_len, c->ip_addr);
+				  &(c->tx_bufs + j)->len, c->ip_addr);
 		  resolve_packet ((void *) (c->rx_bufs + i + 1)->data,
-				  (c->rx_bufs + i + 1)->data_len,
+				  (c->rx_bufs + i + 1)->len,
 				  (void *) (c->tx_bufs + j + 1)->data,
-				  &(c->tx_bufs + j + 1)->data_len,
-				  c->ip_addr);
+				  &(c->tx_bufs + j + 1)->len, c->ip_addr);
 
 		  i += 2;
 		  j += 2;
 		  tx -= 2;
 		}
 	      resolve_packet ((void *) (c->rx_bufs + i)->data,
-			      (c->rx_bufs + i)->data_len,
+			      (c->rx_bufs + i)->len,
 			      (void *) (c->tx_bufs + j)->data,
-			      &(c->tx_bufs + j)->data_len, c->ip_addr);
+			      &(c->tx_bufs + j)->len, c->ip_addr);
 	      i++;
 	      j++;
 	      tx--;
 	    }
 	  /* mark memif buffers and shared memory buffers as free */
 	  /* free processed buffers */
-	  err = memif_buffer_free (c->conn, qid, c->rx_bufs + prev_i, j, &fb);
+	  err = memif_refill_queue (c->conn, qid, j);
 	  if (err != MEMIF_ERR_SUCCESS)
 	    INFO ("memif_buffer_free: %s", memif_strerror (err));
-	  rx -= fb;
+	  rx -= j;
 	  prev_i = i;
 
 	  DBG ("freed %d buffers. %u/%u alloc/free buffers",
@@ -555,10 +534,10 @@
   return 0;
 
 error:
-  err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+  err = memif_refill_queue (c->conn, qid, rx);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
-  c->rx_buf_num -= fb;
+  c->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
        fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
   return 0;
@@ -605,17 +584,17 @@
 	      if (icmpr_buffer_alloc (c->index, 1, &tx, i, qid) < 0)
 		break;
 	      resolve_packet ((void *) (c->rx_bufs + i)->data,
-			      (c->rx_bufs + i)->data_len,
+			      (c->rx_bufs + i)->len,
 			      (void *) (c->tx_bufs + i)->data,
-			      &(c->tx_bufs + i)->data_len, c->ip_addr);
+			      &(c->tx_bufs + i)->len, c->ip_addr);
 	      icmpr_tx_burst (c->index, qid);
 	    }
 	}
 
-      err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+      err = memif_refill_queue (c->conn, qid, rx);
       if (err != MEMIF_ERR_SUCCESS)
 	INFO ("memif_buffer_free: %s", memif_strerror (err));
-      c->rx_buf_num -= fb;
+      c->rx_buf_num -= rx;
 
 
     }
@@ -624,10 +603,10 @@
   return 0;
 
 error:
-  err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+  err = memif_refill_queue (c->conn, qid, rx);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
-  c->rx_buf_num -= fb;
+  c->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
        fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
   return 0;
@@ -655,10 +634,9 @@
   args.is_master = mode;
   args.log2_ring_size = 11;
   args.buffer_size = 2048;
-  args.num_s2m_rings = 2;
-  args.num_m2s_rings = 2;
+  args.num_s2m_rings = 1;
+  args.num_m2s_rings = 1;
   strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
-  strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME));
   args.mode = 0;
   /* socket filename is not specified, because this app is supposed to
      connect to VPP over memif. so default socket filename will be used */
@@ -779,7 +757,7 @@
   printf (" (debug)");
 #endif
   printf ("\n");
-  printf ("memif version: %d\n", MEMIF_VERSION);
+  printf ("memif version: %d\n", memif_get_version ());
   printf ("commands:\n");
   printf ("\thelp - prints this help\n");
   printf ("\texit - exit app\n");
@@ -968,7 +946,7 @@
       i = 0;
       err = memif_buffer_alloc (c->conn, 0, c->tx_bufs,
 				MAX_MEMIF_BUFS >
-				count ? count : MAX_MEMIF_BUFS, &tx, 0);
+				count ? count : MAX_MEMIF_BUFS, &tx, 128);
       if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING))
 	{
 	  INFO ("memif_buffer_alloc: %s Thread exiting...\n",
@@ -982,16 +960,16 @@
 	  while (tx > 2)
 	    {
 	      generate_packet ((void *) c->tx_bufs[i].data,
-			       &c->tx_bufs[i].data_len, c->ip_addr,
+			       &c->tx_bufs[i].len, c->ip_addr,
 			       d->ip_daddr, d->hw_daddr, seq++);
 	      generate_packet ((void *) c->tx_bufs[i + 1].data,
-			       &c->tx_bufs[i + 1].data_len, c->ip_addr,
+			       &c->tx_bufs[i + 1].len, c->ip_addr,
 			       d->ip_daddr, d->hw_daddr, seq++);
 	      i += 2;
 	      tx -= 2;
 	    }
 	  generate_packet ((void *) c->tx_bufs[i].data,
-			   &c->tx_bufs[i].data_len, c->ip_addr,
+			   &c->tx_bufs[i].len, c->ip_addr,
 			   d->ip_daddr, d->hw_daddr, seq++);
 	  i++;
 	  tx--;
@@ -1313,13 +1291,17 @@
   /* if valid callback is passed as argument, fd event polling will be done by user
      all file descriptors and events will be passed to user in this callback */
   /* if callback is set to NULL libmemif will handle fd event polling */
-  err = memif_init (control_fd_update, APP_NAME);
+  err = memif_init (control_fd_update, APP_NAME, NULL, NULL);
   if (err != MEMIF_ERR_SUCCESS)
-    INFO ("memif_init: %s", memif_strerror (err));
+    {
+      INFO ("memif_init: %s", memif_strerror (err));
+      icmpr_free ();
+      exit (-1);
+    }
 
   for (i = 0; i < MAX_CONNS; i++)
     {
-      memif_connection[i].conn = NULL;
+      memset (&memif_connection[i], 0, sizeof (memif_connection_t));
       ctx[i] = i;
     }
 
diff --git a/extras/libmemif/examples/icmp_responder-mt/main.c b/extras/libmemif/examples/icmp_responder-mt/main.c
index c47fc53..4f7733b 100644
--- a/extras/libmemif/examples/icmp_responder-mt/main.c
+++ b/extras/libmemif/examples/icmp_responder-mt/main.c
@@ -299,6 +299,10 @@
       data->rx_buf_num += rx;
       if (rx == 0)
 	{
+	  /* if queue is in polling mode, it's not refilled */
+	  err = memif_refill_queue (c->conn, data->qid, -1);
+	  if (err != MEMIF_ERR_SUCCESS)
+	    INFO ("memif_buffer_free: %s", memif_strerror (err));
 	  continue;
 	}
 
@@ -309,7 +313,7 @@
 
       err =
 	memif_buffer_alloc (c->conn, data->qid, data->tx_bufs,
-			    data->rx_buf_num, &tx, 0);
+			    data->rx_buf_num, &tx, 128);
       if (err != MEMIF_ERR_SUCCESS)
 	{
 	  INFO ("memif_buffer_alloc: %s", memif_strerror (err));
@@ -324,16 +328,16 @@
       for (i = 0; i < rx; i++)
 	{
 	  resolve_packet ((void *) (data->rx_bufs + i)->data,
-			  (data->rx_bufs + i)->data_len,
+			  (data->rx_bufs + i)->len,
 			  (void *) (data->tx_bufs + i)->data,
-			  &(data->tx_bufs + i)->data_len, c->ip_addr);
+			  &(data->tx_bufs + i)->len, c->ip_addr);
 	}
 
       /* mark memif buffers and shared memory buffers as free */
-      err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb);
+      err = memif_refill_queue (c->conn, data->qid, -1);
       if (err != MEMIF_ERR_SUCCESS)
 	INFO ("memif_buffer_free: %s", memif_strerror (err));
-      data->rx_buf_num -= fb;
+      data->rx_buf_num -= rx;
 
       DBG ("freed %d buffers. %u/%u alloc/free buffers",
 	   fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
@@ -355,10 +359,10 @@
   goto close;
 
 close:
-  err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb);
+  err = memif_refill_queue (c->conn, data->qid, -1);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
-  data->rx_buf_num -= fb;
+  data->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
        fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
   free (data->rx_bufs);
@@ -439,7 +443,7 @@
 
 	  err =
 	    memif_buffer_alloc (c->conn, data->qid, data->tx_bufs,
-				data->rx_buf_num, &tx, 0);
+				data->rx_buf_num, &tx, 128);
 	  if (err != MEMIF_ERR_SUCCESS)
 	    {
 	      INFO ("memif_buffer_alloc: %s", memif_strerror (err));
@@ -454,17 +458,16 @@
 	  for (i = 0; i < rx; i++)
 	    {
 	      resolve_packet ((void *) (data->rx_bufs + i)->data,
-			      (data->rx_bufs + i)->data_len,
+			      (data->rx_bufs + i)->len,
 			      (void *) (data->tx_bufs + i)->data,
-			      &(data->tx_bufs + i)->data_len, c->ip_addr);
+			      &(data->tx_bufs + i)->len, c->ip_addr);
 	    }
 
 	  /* mark memif buffers and shared memory buffers as free */
-	  err =
-	    memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb);
+	  err = memif_refill_queue (c->conn, data->qid, rx);
 	  if (err != MEMIF_ERR_SUCCESS)
 	    INFO ("memif_buffer_free: %s", memif_strerror (err));
-	  data->rx_buf_num -= fb;
+	  data->rx_buf_num -= rx;
 
 	  DBG ("freed %d buffers. %u/%u alloc/free buffers",
 	       fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
@@ -487,10 +490,10 @@
   goto close;
 
 close:
-  err = memif_buffer_free (c->conn, data->qid, data->rx_bufs, rx, &fb);
+  err = memif_refill_queue (c->conn, data->qid, rx);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
-  data->rx_buf_num -= fb;
+  data->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
        fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num);
   free (data->rx_bufs);
@@ -611,7 +614,6 @@
   args.num_s2m_rings = 2;
   args.num_m2s_rings = 2;
   strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
-  strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME));
   args.mode = 0;
   /* socket filename is not specified, because this app is supposed to
      connect to VPP over memif. so default socket filename will be used */
@@ -675,7 +677,7 @@
   printf (" (debug)");
 #endif
   printf ("\n");
-  printf ("memif version: %d\n", MEMIF_VERSION);
+  printf ("memif version: %d\n", memif_get_version ());
   printf ("commands:\n");
   printf ("\thelp - prints this help\n");
   printf ("\texit - exit app\n");
@@ -896,7 +898,7 @@
   /* if valid callback is passed as argument, fd event polling will be done by user
      all file descriptors and events will be passed to user in this callback */
   /* if callback is set to NULL libmemif will handle fd event polling */
-  err = memif_init (control_fd_update, APP_NAME);
+  err = memif_init (control_fd_update, APP_NAME, NULL, NULL);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_init: %s", memif_strerror (err));
 
diff --git a/extras/libmemif/examples/icmp_responder/main.c b/extras/libmemif/examples/icmp_responder/main.c
index 9e49771..b820ad3 100644
--- a/extras/libmemif/examples/icmp_responder/main.c
+++ b/extras/libmemif/examples/icmp_responder/main.c
@@ -214,7 +214,7 @@
   printf (" (debug)");
 #endif
   printf ("\n");
-  printf ("memif version: %d\n", MEMIF_VERSION);
+  printf ("memif version: %d\n", memif_get_version ());
   printf ("\tuse CTRL+C to exit\n");
 }
 
@@ -225,7 +225,7 @@
   int err;
   uint16_t r;
   /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */
-  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 0);
+  err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, n, &r, 128);
   if (err != MEMIF_ERR_SUCCESS)
     {
       INFO ("memif_buffer_alloc: %s", memif_strerror (err));
@@ -306,30 +306,29 @@
   for (i = 0; i < rx; i++)
     {
       resolve_packet ((void *) (c->rx_bufs + i)->data,
-		      (c->rx_bufs + i)->data_len,
+		      (c->rx_bufs + i)->len,
 		      (void *) (c->tx_bufs + i)->data,
-		      &(c->tx_bufs + i)->data_len, c->ip_addr);
+		      &(c->tx_bufs + i)->len, c->ip_addr);
     }
 
-  uint16_t fb;
   /* mark memif buffers and shared memory buffers as free */
-  err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
-  c->rx_buf_num -= fb;
+  err = memif_refill_queue (c->conn, qid, rx);
+  c->rx_buf_num -= rx;
 
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
-       fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
+       rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
 
   icmpr_tx_burst (c->tx_qid);
 
   return 0;
 
 error:
-  err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb);
+  err = memif_refill_queue (c->conn, qid, rx);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_buffer_free: %s", memif_strerror (err));
-  c->rx_buf_num -= fb;
+  c->rx_buf_num -= rx;
   DBG ("freed %d buffers. %u/%u alloc/free buffers",
-       fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
+       rx, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num);
   return 0;
 }
 
@@ -346,7 +345,6 @@
   args.num_s2m_rings = 2;
   args.num_m2s_rings = 2;
   strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME));
-  strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME));
   args.mode = 0;
   /* socket filename is not specified, because this app is supposed to
      connect to VPP over memif. so default socket filename will be used */
@@ -396,7 +394,7 @@
   /* if valid callback is passed as argument, fd event polling will be done by user
      all file descriptors and events will be passed to user in this callback */
   /* if callback is set to NULL libmemif will handle fd event polling */
-  err = memif_init (NULL, APP_NAME);
+  err = memif_init (NULL, APP_NAME, NULL, NULL);
   if (err != MEMIF_ERR_SUCCESS)
     INFO ("memif_init: %s", memif_strerror (err));