svm: support for multi-segment enqueues
[vpp.git] / src / svm / svm_fifo.c
index 0168c08..f1ac8d4 100644 (file)
@@ -70,6 +70,8 @@ CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
       c = c->next;
       while ((to_copy -= n_chunk))
        {
+         CLIB_MEM_UNPOISON (c, sizeof (*c));
+         CLIB_MEM_UNPOISON (c->data, c->length);
          n_chunk = clib_min (c->length, to_copy);
          clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
          c = c->length <= to_copy ? c->next : c;
@@ -368,7 +370,7 @@ void
 svm_fifo_init (svm_fifo_t * f, u32 size)
 {
   svm_fifo_chunk_t *c, *prev;
-  u32 first_chunk, min_alloc;
+  u32 min_alloc;
 
   f->size = size;
   f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
@@ -378,9 +380,8 @@ svm_fifo_init (svm_fifo_t * f, u32 size)
   f->head_chunk = f->tail_chunk = f->start_chunk;
   f->ooo_deq = f->ooo_enq = 0;
 
-  first_chunk = f->start_chunk->length;
-  min_alloc = first_chunk > 16 << 10 ? first_chunk >> 2 : 4096;
-  min_alloc = clib_min (first_chunk, 64 << 10);
+  min_alloc = size > 32 << 10 ? size >> 3 : 4096;
+  min_alloc = clib_min (min_alloc, 64 << 10);
   f->min_alloc = min_alloc;
 
   /*
@@ -388,11 +389,13 @@ svm_fifo_init (svm_fifo_t * f, u32 size)
    */
   f->start_chunk->start_byte = 0;
   prev = f->start_chunk;
+  prev->enq_rb_index = prev->deq_rb_index = RBTREE_TNIL_INDEX;
   c = prev->next;
 
   while (c)
     {
       c->start_byte = prev->start_byte + prev->length;
+      c->enq_rb_index = c->deq_rb_index = RBTREE_TNIL_INDEX;
       prev = c;
       c = c->next;
     }
@@ -748,34 +751,6 @@ f_lookup_clear_deq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
   return c;
 }
 
-void
-svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c)
-{
-  svm_fifo_chunk_t *cur, *prev;
-
-  cur = c;
-  prev = f->end_chunk;
-
-  while (cur)
-    {
-      cur->start_byte = prev->start_byte + prev->length;
-      cur->enq_rb_index = RBTREE_TNIL_INDEX;
-      cur->deq_rb_index = RBTREE_TNIL_INDEX;
-
-      prev = cur;
-      cur = cur->next;
-    }
-
-  prev->next = 0;
-  f->end_chunk->next = c;
-  f->end_chunk = prev;
-
-  if (!f->tail_chunk)
-    f->tail_chunk = c;
-
-  return;
-}
-
 void
 svm_fifo_free_chunk_lookup (svm_fifo_t * f)
 {
@@ -824,13 +799,12 @@ svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len)
 }
 
 static int
-f_try_grow (svm_fifo_t * f, u32 head, u32 tail, u32 len)
+f_try_chunk_alloc (svm_fifo_t * f, u32 head, u32 tail, u32 len)
 {
-  svm_fifo_chunk_t *c;
+  svm_fifo_chunk_t *c, *cur, *prev;
   u32 alloc_size, free_alloced;
 
   free_alloced = f_chunk_end (f->end_chunk) - tail;
-  ASSERT (free_alloced < len);
 
   alloc_size = clib_min (f->min_alloc, f->size - (tail - head));
   alloc_size = clib_max (alloc_size, len - free_alloced);
@@ -839,7 +813,26 @@ f_try_grow (svm_fifo_t * f, u32 head, u32 tail, u32 len)
   if (PREDICT_FALSE (!c))
     return -1;
 
-  svm_fifo_add_chunk (f, c);
+  cur = c;
+  prev = f->end_chunk;
+
+  while (cur)
+    {
+      cur->start_byte = prev->start_byte + prev->length;
+      cur->enq_rb_index = RBTREE_TNIL_INDEX;
+      cur->deq_rb_index = RBTREE_TNIL_INDEX;
+
+      prev = cur;
+      cur = cur->next;
+    }
+
+  prev->next = 0;
+  f->end_chunk->next = c;
+  f->end_chunk = prev;
+
+  if (!f->tail_chunk)
+    f->tail_chunk = c;
+
   return 0;
 }
 
@@ -864,7 +857,7 @@ svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src)
 
   if (f_pos_gt (tail + len, f_chunk_end (f->end_chunk)))
     {
-      if (PREDICT_FALSE (f_try_grow (f, head, tail, len)))
+      if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
        {
          len = f_chunk_end (f->end_chunk) - tail;
          if (!len)
@@ -920,7 +913,7 @@ svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
 
   if (f_pos_gt (enq_pos + len, f_chunk_end (f->end_chunk)))
     {
-      if (PREDICT_FALSE (f_try_grow (f, head, tail, offset + len)))
+      if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, offset + len)))
        return SVM_FIFO_EGROW;
     }
 
@@ -962,6 +955,87 @@ svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
   clib_atomic_store_rel_n (&f->tail, tail);
 }
 
+int
+svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[],
+                          u32 n_segs, u8 allow_partial)
+{
+  u32 tail, head, free_count, len = 0, i;
+  svm_fifo_chunk_t *old_tail_c;
+
+  f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
+
+  f_load_head_tail_prod (f, &head, &tail);
+
+  /* free space in fifo can only increase during enqueue: SPSC */
+  free_count = f_free_count (f, head, tail);
+
+  if (PREDICT_FALSE (free_count == 0))
+    return SVM_FIFO_EFULL;
+
+  for (i = 0; i < n_segs; i++)
+    len += segs[i].len;
+
+  old_tail_c = f->tail_chunk;
+
+  if (!allow_partial)
+    {
+      if (PREDICT_FALSE (free_count < len))
+       return SVM_FIFO_EFULL;
+
+      if (f_pos_gt (tail + len, f_chunk_end (f->end_chunk)))
+       {
+         if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
+           return SVM_FIFO_EGROW;
+       }
+
+      for (i = 0; i < n_segs; i++)
+       {
+         svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, segs[i].data,
+                                 segs[i].len, &f->tail_chunk);
+         tail += segs[i].len;
+       }
+    }
+  else
+    {
+      len = clib_min (free_count, len);
+
+      if (f_pos_gt (tail + len, f_chunk_end (f->end_chunk)))
+       {
+         if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
+           {
+             len = f_chunk_end (f->end_chunk) - tail;
+             if (!len)
+               return SVM_FIFO_EGROW;
+           }
+       }
+
+      i = 0;
+      while (len)
+       {
+         u32 to_copy = clib_min (segs[i].len, len);
+         svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, segs[i].data,
+                                 to_copy, &f->tail_chunk);
+         len -= to_copy;
+         tail += to_copy;
+         i++;
+       }
+    }
+
+  /* collect out-of-order segments */
+  if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
+    {
+      len += ooo_segment_try_collect (f, len, &tail);
+      /* Tail chunk might've changed even if nothing was collected */
+      f->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
+      f->ooo_enq = 0;
+    }
+
+  /* store-rel: producer owned index (paired with load-acq in consumer) */
+  clib_atomic_store_rel_n (&f->tail, tail);
+
+  return len;
+}
+
 always_inline svm_fifo_chunk_t *
 f_unlink_chunks (svm_fifo_t * f, u32 end_pos, u8 maybe_ooo)
 {
@@ -1035,6 +1109,10 @@ svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
   svm_fifo_copy_from_chunk (f, f->head_chunk, head, dst, len, &f->head_chunk);
   head = head + len;
 
+  /* In order dequeues are not supported in combination with ooo peeking.
+   * Use svm_fifo_dequeue_drop instead. */
+  ASSERT (rb_tree_n_nodes (&f->ooo_deq_lookup) <= 1);
+
   if (f_pos_geq (head, f_chunk_end (f->start_chunk)))
     fsh_collect_chunks (f->fs_hdr, f->slice_index,
                        f_unlink_chunks (f, head, 0));
@@ -1061,6 +1139,7 @@ svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
   len = clib_min (cursize - offset, len);
   head_idx = head + offset;
 
+  CLIB_MEM_UNPOISON (f->ooo_deq, sizeof (*f->ooo_deq));
   if (!f->ooo_deq || !f_chunk_includes_pos (f->ooo_deq, head_idx))
     f_update_ooo_deq (f, head_idx, head_idx + len);
 
@@ -1105,7 +1184,6 @@ svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
 /**
  * Drop all data from fifo
  *
- * Should be called only from vpp side because of lookup cleanup
  */
 void
 svm_fifo_dequeue_drop_all (svm_fifo_t * f)
@@ -1137,16 +1215,19 @@ svm_fifo_fill_chunk_list (svm_fifo_t * f)
   if (f_chunk_end (f->end_chunk) - head >= f->size)
     return 0;
 
-  if (f_try_grow (f, head, tail, f->size - (tail - head)))
+  if (f_try_chunk_alloc (f, head, tail, f->size - (tail - head)))
     return SVM_FIFO_EGROW;
 
   return 0;
 }
 
 int
-svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs)
+svm_fifo_segments (svm_fifo_t * f, u32 offset, svm_fifo_seg_t * fs,
+                  u32 n_segs, u32 max_bytes)
 {
-  u32 cursize, head, tail, head_idx;
+  u32 cursize, to_read, head, tail, fs_index = 1;
+  u32 n_bytes, head_pos, len, start;
+  svm_fifo_chunk_t *c;
 
   f_load_head_tail_cons (f, &head, &tail);
 
@@ -1156,37 +1237,36 @@ svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs)
   if (PREDICT_FALSE (cursize == 0))
     return SVM_FIFO_EEMPTY;
 
-  head_idx = head;
+  if (offset >= cursize)
+    return SVM_FIFO_EEMPTY;
 
-  if (tail < head)
-    {
-      fs[0].len = f->size - head_idx;
-      fs[0].data = f->head_chunk->data + head_idx;
-      fs[1].len = cursize - fs[0].len;
-      fs[1].data = f->head_chunk->data;
-    }
-  else
-    {
-      fs[0].len = cursize;
-      fs[0].data = f->head_chunk->data + head_idx;
-      fs[1].len = 0;
-      fs[1].data = 0;
-    }
-  return cursize;
-}
+  to_read = clib_min (cursize - offset, max_bytes);
+  start = head + offset;
 
-void
-svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs)
-{
-  u32 head;
+  if (!f->head_chunk)
+    f->head_chunk = svm_fifo_find_chunk (f, head);
 
-  /* consumer owned index */
-  head = f->head;
+  c = f->head_chunk;
 
-  ASSERT (fs[0].data == f->head_chunk->data + head);
-  head = (head + fs[0].len + fs[1].len);
-  /* store-rel: consumer owned index (paired with load-acq in producer) */
-  clib_atomic_store_rel_n (&f->head, head);
+  while (!f_chunk_includes_pos (c, start))
+    c = c->next;
+
+  head_pos = start - c->start_byte;
+  fs[0].data = c->data + head_pos;
+  fs[0].len = clib_min (c->length - head_pos, cursize - offset);
+  n_bytes = fs[0].len;
+
+  while (n_bytes < to_read && fs_index < n_segs)
+    {
+      c = c->next;
+      len = clib_min (c->length, to_read - n_bytes);
+      fs[fs_index].data = c->data;
+      fs[fs_index].len = len;
+      n_bytes += len;
+      fs_index += 1;
+    }
+
+  return n_bytes;
 }
 
 /**
@@ -1415,7 +1495,7 @@ svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
   u8 *data = 0;
   svm_fifo_trace_elem_t *trace;
   u32 offset;
-  svm_fifo_t *dummy_fifo;
+  svm_fifo_t *placeholder_fifo;
 
   if (!f)
     return s;
@@ -1428,7 +1508,7 @@ svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
   trace_len = 0;
 #endif
 
-  dummy_fifo = svm_fifo_alloc (f->size);
+  placeholder_fifo = svm_fifo_alloc (f->size);
   svm_fifo_init (f, f->size);
   clib_memset (f->head_chunk->data, 0xFF, f->size);
   vec_validate (data, f->size);
@@ -1443,26 +1523,26 @@ svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
          if (verbose)
            s = format (s, "adding [%u, %u]:", trace[i].offset,
                        (trace[i].offset + trace[i].len));
-         svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
+         svm_fifo_enqueue_with_offset (placeholder_fifo, trace[i].offset,
                                        trace[i].len, &data[offset]);
        }
       else if (trace[i].action == 2)
        {
          if (verbose)
            s = format (s, "adding [%u, %u]:", 0, trace[i].len);
-         svm_fifo_enqueue (dummy_fifo, trace[i].len, &data[offset]);
+         svm_fifo_enqueue (placeholder_fifo, trace[i].len, &data[offset]);
        }
       else if (!no_read)
        {
          if (verbose)
            s = format (s, "read: %u", trace[i].len);
-         svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
+         svm_fifo_dequeue_drop (placeholder_fifo, trace[i].len);
        }
       if (verbose)
-       s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
+       s = format (s, "%U", format_svm_fifo, placeholder_fifo, 1);
     }
 
-  s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
+  s = format (s, "result: %U", format_svm_fifo, placeholder_fifo, 1);
 
   return s;
 }