libmemif: unmask head/tail pointers fix, additional ring info in memif_queue_details_t

Change-Id: I1dbf91c2a1575d5a82af70bfccdd990310323a8c
Signed-off-by: Jakub Grajciar <Jakub.Grajciar@pantheon.tech>
diff --git a/extras/libmemif/examples/icmp_responder-epoll/main.c b/extras/libmemif/examples/icmp_responder-epoll/main.c
index 80a7f7e..634e180 100644
--- a/extras/libmemif/examples/icmp_responder-epoll/main.c
+++ b/extras/libmemif/examples/icmp_responder-epoll/main.c
@@ -185,6 +185,10 @@
 	{
 	  printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid);
 	  printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size);
+	  printf ("\t\tring rx mode: %s\n",
+		  md.rx_queues[e].flags ? "polling" : "interrupt");
+	  printf ("\t\tring head: %u\n", md.rx_queues[e].head);
+	  printf ("\t\tring tail: %u\n", md.rx_queues[e].tail);
 	  printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size);
 	}
       printf ("\ttx queues:\n");
@@ -192,6 +196,10 @@
 	{
 	  printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid);
 	  printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size);
+	  printf ("\t\tring rx mode: %s\n",
+		  md.tx_queues[e].flags ? "polling" : "interrupt");
+	  printf ("\t\tring head: %u\n", md.tx_queues[e].head);
+	  printf ("\t\tring tail: %u\n", md.tx_queues[e].tail);
 	  printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size);
 	}
       printf ("\tlink: ");
@@ -1213,7 +1221,7 @@
     }
   else
     {
-      DBG ("unknown command: %s", ui);
+      INFO ("unknown command: %s", ui);
       goto done;
     }
 
diff --git a/extras/libmemif/src/libmemif.h b/extras/libmemif/src/libmemif.h
index 3fc0648..796d121 100644
--- a/extras/libmemif/src/libmemif.h
+++ b/extras/libmemif/src/libmemif.h
@@ -200,14 +200,21 @@
 /** \brief Memif queue details
     @param qid - queue id
     @param ring_size - size of ring buffer in sharem memory
+    @param flags - ring flags
+    @param head - ring head pointer
+    @param tail - ring tail pointer
     @param buffer_size - buffer size on sharem memory
 */
 typedef struct
 {
   uint8_t qid;
   uint32_t ring_size;
+/** if set queue is in polling mode, else in interrupt mode */
+#define MEMIF_QUEUE_FLAG_POLLING 1
+  uint16_t flags;
+  uint16_t head;
+  uint16_t tail;
   uint16_t buffer_size;
-  /* add ring information */
 } memif_queue_details_t;
 
 /** \brief Memif details
diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c
index 2e2602e..9b5bdd2 100644
--- a/extras/libmemif/src/main.c
+++ b/extras/libmemif/src/main.c
@@ -524,8 +524,8 @@
   if (conn == NULL)
     return MEMIF_ERR_NOCONN;
   uint8_t num =
-    (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args.
-    num_m2s_rings;
+    (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->
+    run_args.num_m2s_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
 
@@ -859,21 +859,20 @@
 	  if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
 	    {
 	      num =
-		(((memif_connection_t *) e->data_struct)->args.
-		 is_master) ? ((memif_connection_t *) e->data_struct)->
-		run_args.num_s2m_rings : ((memif_connection_t *) e->
-					  data_struct)->run_args.
-		num_m2s_rings;
+		(((memif_connection_t *) e->data_struct)->
+		 args.is_master) ? ((memif_connection_t *) e->
+				    data_struct)->run_args.
+		num_s2m_rings : ((memif_connection_t *) e->data_struct)->
+		run_args.num_m2s_rings;
 	      for (i = 0; i < num; i++)
 		{
-		  if (((memif_connection_t *) e->data_struct)->rx_queues[i].
-		      int_fd == fd)
+		  if (((memif_connection_t *) e->data_struct)->
+		      rx_queues[i].int_fd == fd)
 		    {
-		      ((memif_connection_t *) e->
-		       data_struct)->on_interrupt ((void *) e->data_struct,
-						   ((memif_connection_t *)
-						    e->data_struct)->
-						   private_ctx, i);
+		      ((memif_connection_t *) e->data_struct)->
+			on_interrupt ((void *) e->data_struct,
+				      ((memif_connection_t *) e->
+				       data_struct)->private_ctx, i);
 		      return MEMIF_ERR_SUCCESS;
 		    }
 		}
@@ -900,24 +899,24 @@
 	  if (events & MEMIF_FD_EVENT_READ)
 	    {
 	      err =
-		((memif_connection_t *) e->data_struct)->read_fn (e->
-								  data_struct);
+		((memif_connection_t *) e->data_struct)->
+		read_fn (e->data_struct);
 	      if (err != MEMIF_ERR_SUCCESS)
 		return err;
 	    }
 	  if (events & MEMIF_FD_EVENT_WRITE)
 	    {
 	      err =
-		((memif_connection_t *) e->data_struct)->write_fn (e->
-								   data_struct);
+		((memif_connection_t *) e->data_struct)->
+		write_fn (e->data_struct);
 	      if (err != MEMIF_ERR_SUCCESS)
 		return err;
 	    }
 	  if (events & MEMIF_FD_EVENT_ERROR)
 	    {
 	      err =
-		((memif_connection_t *) e->data_struct)->error_fn (e->
-								   data_struct);
+		((memif_connection_t *) e->data_struct)->
+		error_fn (e->data_struct);
 	      if (err != MEMIF_ERR_SUCCESS)
 		return err;
 	    }
@@ -1034,8 +1033,8 @@
   if (c->tx_queues != NULL)
     {
       num =
-	(c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
-	num_s2m_rings;
+	(c->args.is_master) ? c->run_args.num_m2s_rings : c->
+	run_args.num_s2m_rings;
       for (i = 0; i < num; i++)
 	{
 	  mq = &c->tx_queues[i];
@@ -1055,8 +1054,8 @@
   if (c->rx_queues != NULL)
     {
       num =
-	(c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-	num_m2s_rings;
+	(c->args.is_master) ? c->run_args.num_s2m_rings : c->
+	run_args.num_m2s_rings;
       for (i = 0; i < num; i++)
 	{
 	  mq = &c->rx_queues[i];
@@ -1208,8 +1207,8 @@
     }
 
   num =
-    (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
-    num_s2m_rings;
+    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+    run_args.num_s2m_rings;
   for (i = 0; i < num; i++)
     {
       mq = &c->tx_queues[i];
@@ -1226,8 +1225,8 @@
 	}
     }
   num =
-    (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-    num_m2s_rings;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
   for (i = 0; i < num; i++)
     {
       mq = &c->rx_queues[i];
@@ -1388,8 +1387,8 @@
   if (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;
+    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+    run_args.num_s2m_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
   memif_queue_t *mq = &c->tx_queues[qid];
@@ -1408,8 +1407,8 @@
   /* calculate number of chain buffers */
   if (size > ring->desc[0].buffer_length)
     {
-      chain_buf = size / ring->desc[s0].buffer_length;
-      if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
+      chain_buf = size / ring->desc[0].buffer_length;
+      if (((size % ring->desc[0].buffer_length) != 0) || (size == 0))
 	chain_buf++;
     }
 
@@ -1499,8 +1498,8 @@
   if (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;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
   libmemif_main_t *lm = &libmemif_main;
@@ -1522,14 +1521,14 @@
 	  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)
+	    b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+	  if ((b0->buffer_len %
+	       ring->desc[b0->desc_index & mask].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)
+	    b1->buffer_len / ring->desc[b1->desc_index & mask].buffer_length;
+	  if ((b1->buffer_len %
+	       ring->desc[b1->desc_index & mask].buffer_length) != 0)
 	    chain_buf1++;
 	  tail = b1->desc_index + chain_buf1;
 	  b0->data = NULL;
@@ -1540,8 +1539,10 @@
 	  mq->alloc_bufs -= chain_buf0 + chain_buf1;
 	}
       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 =
+	b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+      if ((b0->buffer_len %
+	   ring->desc[b0->desc_index & mask].buffer_length) != 0)
 	chain_buf0++;
       tail = b0->desc_index + chain_buf0;
       b0->data = NULL;
@@ -1567,8 +1568,8 @@
   if (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;
+    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+    run_args.num_s2m_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
   memif_queue_t *mq = &c->tx_queues[qid];
@@ -1588,15 +1589,15 @@
 	  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)
+	    b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+	  if ((b0->buffer_len %
+	       ring->desc[b0->desc_index & mask].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)
+	    b1->buffer_len / ring->desc[b1->desc_index & mask].buffer_length;
+	  if ((b1->buffer_len %
+	       ring->desc[b1->desc_index & mask].buffer_length) != 0)
 	    chain_buf1++;
 
 	  for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++)
@@ -1702,7 +1703,7 @@
 		}
 	    }
 
-	  head = (b1->desc_index + chain_buf1) & mask;
+	  head = b1->desc_index + chain_buf1;
 
 	  b0->data = NULL;
 #ifdef MEMIF_DBG
@@ -1721,8 +1722,10 @@
 	}
 
       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 =
+	b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+      if ((b0->buffer_len %
+	   ring->desc[b0->desc_index & mask].buffer_length) != 0)
 	chain_buf0++;
 
       for (i = 0; i < chain_buf0; i++)
@@ -1747,7 +1750,7 @@
 #endif /* MEMIF_DBG_SHM */
 	}
 
-      head = (b0->desc_index + chain_buf0) & mask;
+      head = b0->desc_index + chain_buf0;
 
       b0->data = NULL;
 #ifdef MEMIF_DBG
@@ -1788,8 +1791,8 @@
   if (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;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;
   memif_queue_t *mq = &c->rx_queues[qid];
@@ -1829,19 +1832,23 @@
 	  i = 0;
 	  print_bytes (b0->data +
 		       ring->desc[b0->desc_index & mask].buffer_length * i++,
-		       ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
+		       ring->desc[b0->desc_index & mask].buffer_length,
+		       DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
 	  ns--;
 	  *rx += 1;
-	  while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
+	  while (ring->desc[mq->last_head & mask].
+		 flags & MEMIF_DESC_FLAG_NEXT)
 	    {
 	      ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
 	      mq->last_head++;
 	      b0->data_len += ring->desc[mq->last_head & mask].length;
-	      b0->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
+	      b0->buffer_len +=
+		ring->desc[mq->last_head & mask].buffer_length;
 #ifdef MEMIF_DBG_SHM
 	      print_bytes (b0->data +
-			   ring->desc[b0->desc_index & mask].buffer_length * i++,
+			   ring->desc[b0->desc_index & mask].buffer_length *
+			   i++,
 			   ring->desc[b0->desc_index & mask].buffer_length,
 			   DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
@@ -1858,19 +1865,23 @@
 	  i = 0;
 	  print_bytes (b1->data +
 		       ring->desc[b1->desc_index & mask].buffer_length * i++,
-		       ring->desc[b1->desc_index & mask].buffer_length, DBG_TX_BUF);
+		       ring->desc[b1->desc_index & mask].buffer_length,
+		       DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
 	  ns--;
 	  *rx += 1;
-	  while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
+	  while (ring->desc[mq->last_head & mask].
+		 flags & MEMIF_DESC_FLAG_NEXT)
 	    {
 	      ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
 	      mq->last_head++;
 	      b1->data_len += ring->desc[mq->last_head & mask].length;
-	      b1->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
+	      b1->buffer_len +=
+		ring->desc[mq->last_head & mask].buffer_length;
 #ifdef MEMIF_DBG_SHM
 	      print_bytes (b1->data +
-			   ring->desc[b1->desc_index & mask].buffer_length * i++,
+			   ring->desc[b1->desc_index & mask].buffer_length *
+			   i++,
 			   ring->desc[b1->desc_index & mask].buffer_length,
 			   DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
@@ -1889,10 +1900,11 @@
       b0->data_len = ring->desc[mq->last_head & mask].length;
       b0->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
 #ifdef MEMIF_DBG_SHM
-	  i = 0;
+      i = 0;
       print_bytes (b0->data +
 		   ring->desc[b0->desc_index & mask].buffer_length * i++,
-		   ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
+		   ring->desc[b0->desc_index & mask].buffer_length,
+		   DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
       ns--;
       *rx += 1;
@@ -1906,7 +1918,8 @@
 #ifdef MEMIF_DBG_SHM
 	  print_bytes (b0->data +
 		       ring->desc[b0->desc_index & mask].buffer_length * i++,
-		       ring->desc[b0->desc_index & mask].buffer_length, DBG_TX_BUF);
+		       ring->desc[b0->desc_index & mask].buffer_length,
+		       DBG_TX_BUF);
 #endif /* MEMIF_DBG_SHM */
 	  ns--;
 	  *rx += 1;
@@ -2007,8 +2020,8 @@
     err = MEMIF_ERR_NOBUF_DET;
 
   md->rx_queues_num =
-    (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
-    num_m2s_rings;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
 
   l1 = sizeof (memif_queue_details_t) * md->rx_queues_num;
   if (l0 + l1 <= buflen)
@@ -2023,12 +2036,15 @@
     {
       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;
     }
 
   md->tx_queues_num =
-    (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args.
-    num_s2m_rings;
+    (c->args.is_master) ? c->run_args.num_m2s_rings : c->
+    run_args.num_s2m_rings;
 
   l1 = sizeof (memif_queue_details_t) * md->tx_queues_num;
   if (l0 + l1 <= buflen)
@@ -2043,6 +2059,9 @@
     {
       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;
     }
 
@@ -2061,8 +2080,8 @@
   if (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;
+    (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+    run_args.num_m2s_rings;
   if (qid >= num)
     return MEMIF_ERR_QID;