libmemif: Jumbo frames data/buffer length fix

Change-Id: Icadf1c28b4ab712a210a8e037200ab29d6c53fe4
Signed-off-by: Jakub Grajciar <grajciar.jakub@gmail.com>
diff --git a/extras/libmemif/dockerfile b/extras/libmemif/dockerfile
index 92b2d2a..5475c6b 100644
--- a/extras/libmemif/dockerfile
+++ b/extras/libmemif/dockerfile
@@ -6,7 +6,7 @@
 
 RUN git clone https://github.com/JakubGrajciar/libmemif.git /libmemif
 WORKDIR /libmemif
-RUN git checkout dev
+RUN git checkout master
 RUN ./bootstrap
 RUN ./configure
 RUN make
diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c
index 49bf50c..4942767 100644
--- a/extras/libmemif/src/main.c
+++ b/extras/libmemif/src/main.c
@@ -181,7 +181,7 @@
     }
   printf ("\n\n");
 }
-#endif /* MEMIF_DBG */
+#endif /* MEMIF_DBG_SHM */
 
 int
 memif_syscall_error_handler (int err_code)
@@ -512,8 +512,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;
 
@@ -842,20 +842,21 @@
 	  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;
 		    }
 		}
@@ -882,24 +883,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;
 	    }
@@ -993,8 +994,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];
@@ -1014,8 +1015,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];
@@ -1167,8 +1168,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];
@@ -1185,8 +1186,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];
@@ -1347,8 +1348,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];
@@ -1377,7 +1378,7 @@
     {
       while ((count > 2) && (ns > 2))
 	{
-	  s0 = (ring->head + mq->alloc_bufs + *count_out) & mask;
+	  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++;
@@ -1385,7 +1386,7 @@
 	  if (chain_buf0 > ns)
 	    break;
 
-	  s1 = (ring->head + mq->alloc_bufs + *count_out + chain_buf0) & mask;
+	  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++;
@@ -1398,6 +1399,8 @@
 
 	  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 */
@@ -1429,7 +1432,7 @@
 	  ns -= chain_buf0 + chain_buf1;
 	  *count_out += 2;
 	}
-      s0 = (ring->head + mq->alloc_bufs + *count_out) & mask;
+      s0 = (ring->head + mq->alloc_bufs) & mask;
 
       b0 = (bufs + *count_out);
 
@@ -1441,6 +1444,7 @@
 	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;
 
@@ -1481,8 +1485,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;
@@ -1513,7 +1517,6 @@
 	  if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) !=
 	      0)
 	    chain_buf1++;
-	  tail = (b0->desc_index + chain_buf0) & mask;
 	  tail = (b1->desc_index + chain_buf1) & mask;
 	  b0->data = NULL;
 	  b1->data = NULL;
@@ -1535,6 +1538,7 @@
     }
   MEMIF_MEORY_BARRIER ();
   ring->tail = tail;
+  DBG ("tail: %u", ring->tail);
 
   return MEMIF_ERR_SUCCESS;	/* 0 */
 }
@@ -1549,8 +1553,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];
@@ -1583,8 +1587,34 @@
 
 	  for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++)
 	    {
-	      ring->desc[(b0->desc_index + i) & mask].length = b0->data_len;
-	      ring->desc[(b1->desc_index + i) & mask].length = b1->data_len;
+	      /* 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 +
@@ -1598,15 +1628,26 @@
 			   (chain_buf1 - 1),
 			   ring->desc[(b1->desc_index +
 				       i) & mask].buffer_length, DBG_TX_BUF);
-#endif
+#endif /* MEMIF_DBG_SHM */
 	    }
 
 	  if (chain_buf0 > chain_buf1)
 	    {
 	      for (; i < chain_buf0; i++)
 		{
-		  ring->desc[(b0->desc_index + i) & mask].length =
-		    b0->data_len;
+		  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 +
@@ -1615,14 +1656,26 @@
 			       ring->desc[(b0->desc_index +
 					   i) & mask].buffer_length,
 			       DBG_TX_BUF);
-#endif
+#endif /* MEMIF_DBG_SHM */
 		}
 	    }
 	  else
 	    {
 	      for (; i < chain_buf1; i++)
 		{
-		  ring->desc[b1->desc_index + i].length = b1->data_len;
+		  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 +
@@ -1631,44 +1684,62 @@
 			       ring->desc[(b1->desc_index +
 					   i) & mask].buffer_length,
 			       DBG_TX_BUF);
-#endif
+#endif /* MEMIF_DBG_SHM */
 		}
 	    }
 
-	  head = (b0->desc_index + chain_buf0) & mask;
 	  head = (b1->desc_index + chain_buf1) & mask;
 
 	  b0->data = NULL;
-	  b0->data_len = 0;
+#ifdef MEMIF_DBG
+	  if (b0->data_len != 0)
+	    DBG ("invalid b0 data length!");
+#endif /* MEMIF_DBG */
 	  b1->data = NULL;
-	  b1->data_len = 0;
+#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;
 	}
 
-      b0 = (bufs + *tx);
+      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++;
 
       for (i = 0; i < chain_buf0; i++)
 	{
-	  ring->desc[(b0->desc_index + i) & mask].length = b0->data_len;
+	  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
+#endif /* MEMIF_DBG_SHM */
 	}
 
       head = (b0->desc_index + chain_buf0) & mask;
 
       b0->data = NULL;
-      b0->data_len = 0;
+#ifdef MEMIF_DBG
+      if (b0->data_len != 0)
+	DBG ("invalid b0 data length!");
+#endif /* MEMIF_DBG */
 
       count--;
       *tx += chain_buf0;
@@ -1703,8 +1774,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];
@@ -1732,6 +1803,7 @@
 
   while (ns && count)
     {
+      DBG ("ns: %u, count: %u", ns, count);
       while ((ns > 2) && (count > 2))
 	{
 	  b0 = (bufs + curr_buf);
@@ -1739,41 +1811,67 @@
 
 	  b0->desc_index = mq->last_head;
 	  i = 0;
-	  do
+	  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)
 	    {
-	      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;
+	      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
-	      mq->last_head = (mq->last_head + 1) & mask;
+#endif /* MEMIF_DBG_SHM */
 	      ns--;
 	      *rx += 1;
 	    }
-	  while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT);
+	  mq->last_head = (mq->last_head + 1) & mask;
 
 	  b1->desc_index = mq->last_head;
 	  i = 0;
-	  do
+	  b0->data_len = 0;
+	  b0->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)
 	    {
-	      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;
+	      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
-	      mq->last_head = (mq->last_head + 1) & mask;
+#endif /* MEMIF_DBG_SHM */
 	      ns--;
 	      *rx += 1;
 	    }
-	  while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT);
+	  mq->last_head = (mq->last_head + 1) & mask;
 
 	  count -= 2;
 	  curr_buf += 2;
@@ -1782,21 +1880,35 @@
 
       b0->desc_index = mq->last_head;
       i = 0;
-      do
+      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)
 	{
-	  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;
+	  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
-	  mq->last_head = (mq->last_head + 1) & mask;
+#endif /* MEMIF_DBG_SHM */
 	  ns--;
 	  *rx += 1;
 	}
-      while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT);
+      mq->last_head = (mq->last_head + 1) & mask;
 
       count--;
       curr_buf++;
@@ -1895,8 +2007,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)
@@ -1915,8 +2027,8 @@
     }
 
   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)
@@ -1949,8 +2061,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;