+ if (PREDICT_FALSE (f->ooo_deq != 0))
+ f->ooo_deq = 0;
+ }
+
+ /* Avoid unlinking the last chunk */
+ if (!prev)
+ return 0;
+
+ prev->next = 0;
+ start = f->start_chunk;
+ f->start_chunk = c;
+
+ return start;
+}
+
+int
+svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
+{
+ u32 tail, head, cursize;
+
+ f_load_head_tail_cons (f, &head, &tail);
+
+ /* current size of fifo can only increase during dequeue: SPSC */
+ cursize = f_cursize (f, head, tail);
+
+ if (PREDICT_FALSE (cursize == 0))
+ return SVM_FIFO_EEMPTY;
+
+ len = clib_min (cursize, len);
+
+ if (!f->head_chunk)
+ f->head_chunk = svm_fifo_find_chunk (f, head);
+
+ 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));
+
+ /* store-rel: consumer owned index (paired with load-acq in producer) */
+ clib_atomic_store_rel_n (&f->head, head);
+
+ return len;
+}
+
+int
+svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
+{
+ u32 tail, head, cursize, head_idx;
+
+ f_load_head_tail_cons (f, &head, &tail);
+
+ /* current size of fifo can only increase during peek: SPSC */
+ cursize = f_cursize (f, head, tail);
+
+ if (PREDICT_FALSE (cursize < offset))
+ return SVM_FIFO_EEMPTY;
+
+ 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);
+
+ svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
+ return len;
+}
+
+int
+svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
+{
+ u32 total_drop_bytes, tail, head, cursize;
+
+ f_load_head_tail_cons (f, &head, &tail);
+
+ /* number of bytes available */
+ cursize = f_cursize (f, head, tail);
+ if (PREDICT_FALSE (cursize == 0))
+ return SVM_FIFO_EEMPTY;
+
+ /* number of bytes we're going to drop */
+ total_drop_bytes = clib_min (cursize, len);
+
+ svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
+
+ /* move head */
+ head = head + total_drop_bytes;
+
+ 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, 1));
+ f->head_chunk =
+ f_chunk_includes_pos (f->start_chunk, head) ? f->start_chunk : 0;
+ }
+
+ /* store-rel: consumer owned index (paired with load-acq in producer) */
+ clib_atomic_store_rel_n (&f->head, head);
+
+ return total_drop_bytes;
+}
+
+/**
+ * Drop all data from fifo
+ *
+ */
+void
+svm_fifo_dequeue_drop_all (svm_fifo_t * f)
+{
+ u32 head, tail;
+
+ f_load_head_tail_all_acq (f, &head, &tail);
+
+ if (!f->head_chunk || !f_chunk_includes_pos (f->head_chunk, head))
+ f->head_chunk = svm_fifo_find_chunk (f, head);
+
+ f->head_chunk = f_lookup_clear_deq_chunks (f, f->head_chunk, tail);
+
+ if (f_pos_geq (tail, f_chunk_end (f->start_chunk)))
+ fsh_collect_chunks (f->fs_hdr, f->slice_index,
+ f_unlink_chunks (f, tail, 0));
+
+ /* store-rel: consumer owned index (paired with load-acq in producer) */
+ clib_atomic_store_rel_n (&f->head, tail);
+}
+
+int
+svm_fifo_fill_chunk_list (svm_fifo_t * f)
+{
+ u32 head, tail;
+
+ f_load_head_tail_prod (f, &head, &tail);
+
+ if (f_chunk_end (f->end_chunk) - head >= f->size)
+ return 0;
+
+ 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, u32 offset, svm_fifo_seg_t * fs,
+ u32 n_segs, u32 max_bytes)
+{
+ 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);
+
+ /* consumer function, cursize can only increase while we're working */
+ cursize = f_cursize (f, head, tail);
+
+ if (PREDICT_FALSE (cursize == 0))
+ return SVM_FIFO_EEMPTY;
+
+ if (offset >= cursize)
+ return SVM_FIFO_EEMPTY;
+
+ to_read = clib_min (cursize - offset, max_bytes);
+ start = head + offset;
+
+ if (!f->head_chunk)
+ f->head_chunk = svm_fifo_find_chunk (f, head);
+
+ c = f->head_chunk;
+
+ 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;