avf: avoid placeholder buffer alloc in datapath

Type: improvement
Change-Id: I0ad0fa42f056b5797ba71d6972a44273c13bb97e
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/src/plugins/avf/avf.h b/src/plugins/avf/avf.h
index f7ea407..ea931dc 100644
--- a/src/plugins/avf/avf.h
+++ b/src/plugins/avf/avf.h
@@ -182,7 +182,7 @@
   volatile u32 *qtx_tail;
   u16 next;
   u16 size;
-  u32 ctx_desc_placeholder_bi;
+  u32 *ph_bufs;
   clib_spinlock_t lock;
   avf_tx_desc_t *descs;
   u32 *bufs;
diff --git a/src/plugins/avf/device.c b/src/plugins/avf/device.c
index e0c3c99..70ea446 100644
--- a/src/plugins/avf/device.c
+++ b/src/plugins/avf/device.c
@@ -295,6 +295,7 @@
 {
   clib_error_t *err;
   avf_txq_t *txq;
+  u16 n;
   u8 bpi = vlib_buffer_pool_get_default_for_numa (vm,
 						  ad->numa_node);
 
@@ -313,11 +314,15 @@
   txq->size = txq_size;
   txq->next = 0;
 
-  /* Prepare a placeholder buffer to maintain a 1-1
-     relationship between bufs and descs when a context
-     descriptor is added in descs */
-  if (!vlib_buffer_alloc_from_pool
-      (vm, &txq->ctx_desc_placeholder_bi, 1, bpi))
+  /* Prepare a placeholder buffer(s) to maintain a 1-1 relationship between
+   * bufs and descs when a context descriptor is added in descs. Worst case
+   * every second descriptor is context descriptor and due to b->ref_count
+   * being u8 we need one for each block of 510 descriptors */
+
+  n = (txq->size / 510) + 1;
+  vec_validate_aligned (txq->ph_bufs, n, CLIB_CACHE_LINE_BYTES);
+
+  if (!vlib_buffer_alloc_from_pool (vm, txq->ph_bufs, n, bpi))
     return clib_error_return (0, "buffer allocation error");
 
   txq->descs = vlib_physmem_alloc_aligned_on_numa (vm, txq->size *
@@ -1518,7 +1523,8 @@
 				      txq->n_enqueued);
 	}
       /* Free the placeholder buffer */
-      vlib_buffer_free_one(vm, txq->ctx_desc_placeholder_bi);
+      vlib_buffer_free (vm, txq->ph_bufs, vec_len (txq->ph_bufs));
+      vec_free (txq->ph_bufs);
       vec_free (txq->bufs);
       clib_ring_free (txq->rs_slots);
       vec_free (txq->tmp_bufs);
diff --git a/src/plugins/avf/output.c b/src/plugins/avf/output.c
index 3f361f0..8c932cc 100644
--- a/src/plugins/avf/output.c
+++ b/src/plugins/avf/output.c
@@ -129,28 +129,19 @@
   return flags;
 }
 
-static_always_inline int
-avf_tx_fill_ctx_desc (vlib_main_t * vm, avf_txq_t * txq, avf_tx_desc_t * d,
-		      vlib_buffer_t * b)
+static_always_inline u32
+avf_tx_fill_ctx_desc (vlib_main_t *vm, avf_txq_t *txq, avf_tx_desc_t *d,
+		      vlib_buffer_t *b)
 {
-  vlib_buffer_t *ctx_ph = vlib_get_buffer (vm, txq->ctx_desc_placeholder_bi);
+  vlib_buffer_t *ctx_ph;
+  u32 *bi = txq->ph_bufs;
+
+next:
+  ctx_ph = vlib_get_buffer (vm, bi[0]);
   if (PREDICT_FALSE (ctx_ph->ref_count == 255))
     {
-      /* We need a new placeholder buffer */
-      u32 new_bi;
-      u8 bpi = vlib_buffer_pool_get_default_for_numa (vm, vm->numa_node);
-      if (PREDICT_TRUE
-	  (vlib_buffer_alloc_from_pool (vm, &new_bi, 1, bpi) == 1))
-	{
-	  /* Remove our own reference on the current placeholder buffer */
-	  ctx_ph->ref_count--;
-	  /* Replace with the new placeholder buffer */
-	  txq->ctx_desc_placeholder_bi = new_bi;
-	  ctx_ph = vlib_get_buffer (vm, new_bi);
-	}
-      else
-	/* Impossible to enqueue a ctx descriptor, fail */
-	return 1;
+      bi++;
+      goto next;
     }
 
   /* Acquire a reference on the placeholder buffer */
@@ -163,7 +154,7 @@
   d[0].qword[1] = AVF_TXD_DTYP_CTX | AVF_TXD_CTX_CMD_TSO
     | AVF_TXD_CTX_SEG_MSS (vnet_buffer2 (b)->gso_size) |
     AVF_TXD_CTX_SEG_TLEN (tlen);
-  return 0;
+  return bi[0];
 }
 
 static_always_inline void
@@ -334,11 +325,8 @@
 	  /* Enqueue a context descriptor if needed */
 	  if (PREDICT_FALSE (is_tso))
 	    {
-	      if (avf_tx_fill_ctx_desc (vm, txq, d, b[0]))
-		/* Failure to acquire ref on ctx placeholder */
-		break;
 	      tb[1] = tb[0];
-	      tb[0] = txq->ctx_desc_placeholder_bi;
+	      tb[0] = avf_tx_fill_ctx_desc (vm, txq, d, b[0]);
 	      n_desc += 1;
 	      n_desc_left -= 1;
 	      d += 1;