session: cleanup session tx function
[vpp.git] / src / svm / svm_fifo.c
index fc2189c..dbdb813 100644 (file)
@@ -53,10 +53,12 @@ ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
 u8 *
 format_ooo_segment (u8 * s, va_list * args)
 {
+  svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
   ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
-
-  s = format (s, "pos %u, len %u, next %d, prev %d",
-             seg->start, seg->length, seg->next, seg->prev);
+  u32 normalized_start = (seg->start + f->nitems - f->tail) % f->nitems;
+  s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
+             (normalized_start + seg->length) % f->nitems, seg->length,
+             seg->next, seg->prev);
   return s;
 }
 
@@ -154,7 +156,7 @@ format_ooo_list (u8 * s, va_list * args)
   while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
     {
       seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
-      s = format (s, "  %U\n", format_ooo_segment, seg);
+      s = format (s, "  %U\n", format_ooo_segment, f, seg);
       ooo_segment_index = seg->next;
     }
 
@@ -167,6 +169,9 @@ format_svm_fifo (u8 * s, va_list * args)
   svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
   int verbose = va_arg (*args, int);
 
+  if (!s)
+    return s;
+
   s = format (s, "cursize %u nitems %u has_event %d\n",
              f->cursize, f->nitems, f->has_event);
   s = format (s, " head %d tail %d\n", f->head, f->tail);
@@ -192,8 +197,11 @@ svm_fifo_t *
 svm_fifo_create (u32 data_size_in_bytes)
 {
   svm_fifo_t *f;
+  u32 rounded_data_size;
 
-  f = clib_mem_alloc_aligned_or_null (sizeof (*f) + data_size_in_bytes,
+  /* always round fifo data size to the next highest power-of-two */
+  rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
+  f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
                                      CLIB_CACHE_LINE_BYTES);
   if (f == 0)
     return 0;
@@ -201,14 +209,20 @@ svm_fifo_create (u32 data_size_in_bytes)
   memset (f, 0, sizeof (*f));
   f->nitems = data_size_in_bytes;
   f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
+  f->refcnt = 1;
   return (f);
 }
 
 void
 svm_fifo_free (svm_fifo_t * f)
 {
-  pool_free (f->ooo_segments);
-  clib_mem_free (f);
+  ASSERT (f->refcnt > 0);
+
+  if (--f->refcnt == 0)
+    {
+      pool_free (f->ooo_segments);
+      clib_mem_free (f);
+    }
 }
 
 always_inline ooo_segment_t *
@@ -432,12 +446,13 @@ ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued)
        }
     }
 
-  ASSERT (bytes >= 0 && bytes <= f->nitems);
+  ASSERT (bytes <= f->nitems);
   return bytes;
 }
 
 static int
-svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
+svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes,
+                          const u8 * copy_from_here)
 {
   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
   u32 cursize, nitems;
@@ -447,7 +462,7 @@ svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
 
   if (PREDICT_FALSE (cursize == f->nitems))
-    return -2;                 /* fifo stuffed */
+    return SVM_FIFO_FULL;
 
   nitems = f->nitems;
 
@@ -509,7 +524,7 @@ svm_fifo_enqueue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
 
 static int
 svm_fifo_enqueue_nowait_ma (svm_fifo_t * f, u32 max_bytes,
-                           u8 * copy_from_here)
+                           const u8 * copy_from_here)
 {
   return svm_fifo_enqueue_internal (f, max_bytes, copy_from_here);
 }
@@ -519,12 +534,13 @@ foreach_march_variant (SVM_ENQUEUE_CLONE_TEMPLATE,
 CLIB_MULTIARCH_SELECT_FN (svm_fifo_enqueue_nowait_ma);
 
 int
-svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_from_here)
+svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
+                        const u8 * copy_from_here)
 {
 #if CLIB_DEBUG > 0
   return svm_fifo_enqueue_nowait_ma (f, max_bytes, copy_from_here);
 #else
-  static int (*fp) (svm_fifo_t *, u32, u8 *);
+  static int (*fp) (svm_fifo_t *, u32, const u8 *);
 
   if (PREDICT_FALSE (fp == 0))
     fp = (void *) svm_fifo_enqueue_nowait_ma_multiarch_select ();
@@ -548,7 +564,6 @@ svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
 {
   u32 total_copy_bytes, first_copy_bytes, second_copy_bytes;
   u32 cursize, nitems, normalized_offset;
-  u32 offset_from_tail;
 
   f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
 
@@ -561,8 +576,7 @@ svm_fifo_enqueue_with_offset_internal (svm_fifo_t * f,
   normalized_offset = (f->tail + offset) % nitems;
 
   /* Will this request fit? */
-  offset_from_tail = (nitems + normalized_offset - f->tail) % nitems;
-  if ((required_bytes + offset_from_tail) > (nitems - cursize))
+  if ((required_bytes + offset) > (nitems - cursize))
     return -1;
 
   svm_fifo_trace_add (f, offset, required_bytes, 1);
@@ -604,6 +618,20 @@ svm_fifo_enqueue_with_offset (svm_fifo_t * f,
                                                copy_from_here);
 }
 
+void
+svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len)
+{
+  u32 first_chunk;
+  first_chunk = f->nitems - f->head;
+  ASSERT (len <= f->nitems);
+  if (len <= first_chunk)
+    clib_memcpy (&f->data[f->head], data, len);
+  else
+    {
+      clib_memcpy (&f->data[f->head], data, first_chunk);
+      clib_memcpy (&f->data[0], data + first_chunk, len - first_chunk);
+    }
+}
 
 static int
 svm_fifo_dequeue_internal (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)