libmemif: Fix for memif_buffer_alloc rewind logic 65/40065/10
authormbly <mbly@ciena.com>
Tue, 5 Dec 2023 17:36:49 +0000 (09:36 -0800)
committerDave Wallace <dwallacelf@gmail.com>
Fri, 12 Jan 2024 03:22:06 +0000 (03:22 +0000)
Rewind logic was not supporting count = 1, where size was > memif_buffer_size and a rewind is required.
Fixed slot-->next_buf bug for !master as well.

Type: fix
Change-Id: I65cf0d3d0c105f37125412a613e5ff8c5da9a3a2
Signed-off-by: mbly <mbly@ciena.com>
extras/libmemif/src/main.c

index e87d4c5..cb07bf6 100644 (file)
@@ -1550,9 +1550,8 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
   uint16_t ns;
   int err = MEMIF_ERR_SUCCESS; /* 0 */
   uint16_t dst_left, src_left;
-  uint16_t saved_count;
+  uint16_t saved_count_out, delta_count;
   uint16_t saved_next_buf;
-  uint16_t slot;
   memif_buffer_t *saved_b;
   *count_out = 0;
 
@@ -1568,7 +1567,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
       b0 = (bufs + *count_out);
 
       saved_b = b0;
-      saved_count = count;
+      saved_count_out = *count_out;
       saved_next_buf = mq->next_buf;
 
       b0->desc_index = mq->next_buf;
@@ -1584,12 +1583,8 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
        {
          if (EXPECT_FALSE (dst_left == 0))
            {
-             if (count && ns)
+             if (ns)
                {
-                 *count_out += 1;
-                 mq->next_buf++;
-                 ns--;
-
                  ring->desc[b0->desc_index & mask].flags |=
                    MEMIF_DESC_FLAG_NEXT;
                  b0->flags |= MEMIF_BUFFER_FLAG_NEXT;
@@ -1600,13 +1595,14 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
                               ring->desc[mq->next_buf & mask].length :
                               c->run_args.buffer_size;
                  ring->desc[mq->next_buf & mask].flags = 0;
+                 b0->flags = 0;
                }
              else
                {
                  /* rollback allocated chain buffers */
-                 memset (saved_b, 0, sizeof (memif_buffer_t)
-                         * (saved_count - count + 1));
-                 *count_out -= saved_count - count;
+                 delta_count = *count_out - saved_count_out;
+                 memset (saved_b, 0, sizeof (memif_buffer_t) * delta_count);
+                 *count_out -= delta_count;
                  mq->next_buf = saved_next_buf;
                  goto no_ns;
                }
@@ -1616,7 +1612,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
          /* slave resets buffer offset */
          if (c->args.is_master == 0)
            {
-             memif_desc_t *d = &ring->desc[slot & mask];
+             memif_desc_t *d = &ring->desc[mq->next_buf & mask];
              if (ms->get_external_buffer_offset)
                d->offset = ms->get_external_buffer_offset (c->private_ctx);
              else
@@ -1626,18 +1622,17 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
 
          src_left -= b0->len;
          dst_left -= b0->len;
+         *count_out += 1;
+         mq->next_buf++;
+         ns--;
        }
-
-      *count_out += 1;
-      mq->next_buf++;
-      ns--;
       count--;
     }
 
 no_ns:
 
-  DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count,
-       mq->next_buf);
+  DBG ("allocated: %u/%u bufs, size: %u. Next buffer pointer %d", *count_out,
+       count, size, mq->next_buf);
 
   if (count)
     {