2 * Copyright (c) 2016-2019 Cisco and/or its affiliates.
3 * Copyright (c) 2019 Arm Limited
4 * Copyright (c) 2010-2017 Intel Corporation and/or its affiliates.
5 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
6 * Inspired from DPDK rte_ring.h (SPSC only) (derived from freebsd bufring.h).
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <svm/svm_fifo.h>
21 #include <vppinfra/cpu.h>
23 CLIB_MARCH_FN (svm_fifo_copy_to_chunk, void, svm_fifo_t * f,
24 svm_fifo_chunk_t * c, u32 tail_idx, const u8 * src, u32 len,
25 svm_fifo_chunk_t ** last)
29 ASSERT (tail_idx >= c->start_byte && tail_idx < c->start_byte + c->length);
31 tail_idx -= c->start_byte;
32 n_chunk = c->length - tail_idx;
36 clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
38 while ((to_copy -= n_chunk))
40 n_chunk = clib_min (c->length, to_copy);
41 clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
42 c = c->length <= to_copy ? c->next : c;
49 clib_memcpy_fast (&c->data[tail_idx], src, len);
53 CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
54 svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len,
55 svm_fifo_chunk_t ** last)
59 ASSERT (head_idx >= c->start_byte && head_idx < c->start_byte + c->length);
61 head_idx -= c->start_byte;
62 n_chunk = c->length - head_idx;
66 clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
68 while ((to_copy -= n_chunk))
70 n_chunk = clib_min (c->length, to_copy);
71 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
72 c = c->length <= to_copy ? c->next : c;
79 clib_memcpy_fast (dst, &c->data[head_idx], len);
83 #ifndef CLIB_MARCH_VARIANT
86 svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
87 const u8 * src, u32 len, svm_fifo_chunk_t ** last)
89 CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
94 svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
95 u8 * dst, u32 len, svm_fifo_chunk_t ** last)
97 CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
102 position_lt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
104 return (f_distance_to (f, a, tail) < f_distance_to (f, b, tail));
108 position_leq (svm_fifo_t * f, u32 a, u32 b, u32 tail)
110 return (f_distance_to (f, a, tail) <= f_distance_to (f, b, tail));
114 position_gt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
116 return (f_distance_to (f, a, tail) > f_distance_to (f, b, tail));
120 position_diff (svm_fifo_t * f, u32 a, u32 b, u32 tail)
122 return f_distance_to (f, a, tail) - f_distance_to (f, b, tail);
126 ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
128 return (s->start + s->length) % f->size;
132 svm_fifo_free_ooo_data (svm_fifo_t * f)
134 pool_free (f->ooo_segments);
137 static inline ooo_segment_t *
138 ooo_segment_prev (svm_fifo_t * f, ooo_segment_t * s)
140 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
142 return pool_elt_at_index (f->ooo_segments, s->prev);
145 static inline ooo_segment_t *
146 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
148 if (s->next == OOO_SEGMENT_INVALID_INDEX)
150 return pool_elt_at_index (f->ooo_segments, s->next);
153 static inline ooo_segment_t *
154 ooo_segment_alloc (svm_fifo_t * f, u32 start, u32 length)
158 pool_get (f->ooo_segments, s);
162 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
168 ooo_segment_free (svm_fifo_t * f, u32 index)
170 ooo_segment_t *cur, *prev = 0, *next = 0;
171 cur = pool_elt_at_index (f->ooo_segments, index);
173 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
175 next = pool_elt_at_index (f->ooo_segments, cur->next);
176 next->prev = cur->prev;
179 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
181 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
182 prev->next = cur->next;
186 f->ooos_list_head = cur->next;
189 pool_put (f->ooo_segments, cur);
193 * Add segment to fifo's out-of-order segment list. Takes care of merging
194 * adjacent segments and removing overlapping ones.
197 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
199 ooo_segment_t *s, *new_s, *prev, *next, *it;
200 u32 new_index, s_end_pos, s_index;
201 u32 offset_pos, offset_end_pos;
203 ASSERT (offset + length <= f_distance_to (f, head, tail) || head == tail);
205 offset_pos = (tail + offset) % f->size;
206 offset_end_pos = (tail + offset + length) % f->size;
208 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
210 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
212 s = ooo_segment_alloc (f, offset_pos, length);
213 f->ooos_list_head = s - f->ooo_segments;
214 f->ooos_newest = f->ooos_list_head;
218 /* Find first segment that starts after new segment */
219 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
220 while (s->next != OOO_SEGMENT_INVALID_INDEX
221 && position_lt (f, s->start, offset_pos, tail))
222 s = pool_elt_at_index (f->ooo_segments, s->next);
224 /* If we have a previous and we overlap it, use it as starting point */
225 prev = ooo_segment_prev (f, s);
227 && position_leq (f, offset_pos, ooo_segment_end_pos (f, prev), tail))
230 s_end_pos = ooo_segment_end_pos (f, s);
232 /* Since we have previous, offset start position cannot be smaller
233 * than prev->start. Check tail */
234 ASSERT (position_lt (f, s->start, offset_pos, tail));
238 s_index = s - f->ooo_segments;
239 s_end_pos = ooo_segment_end_pos (f, s);
241 /* No overlap, add before current segment */
242 if (position_lt (f, offset_end_pos, s->start, tail))
244 new_s = ooo_segment_alloc (f, offset_pos, length);
245 new_index = new_s - f->ooo_segments;
247 /* Pool might've moved, get segment again */
248 s = pool_elt_at_index (f->ooo_segments, s_index);
249 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
251 new_s->prev = s->prev;
252 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
253 prev->next = new_index;
258 f->ooos_list_head = new_index;
261 new_s->next = s_index;
263 f->ooos_newest = new_index;
266 /* No overlap, add after current segment */
267 else if (position_gt (f, offset_pos, s_end_pos, tail))
269 new_s = ooo_segment_alloc (f, offset_pos, length);
270 new_index = new_s - f->ooo_segments;
272 /* Pool might've moved, get segment again */
273 s = pool_elt_at_index (f->ooo_segments, s_index);
275 /* Needs to be last */
276 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
278 new_s->prev = s_index;
280 f->ooos_newest = new_index;
290 if (position_lt (f, offset_pos, s->start, tail))
292 s->start = offset_pos;
293 s->length = position_diff (f, s_end_pos, s->start, tail);
294 f->ooos_newest = s - f->ooo_segments;
299 /* Overlapping tail */
300 if (position_gt (f, offset_end_pos, s_end_pos, tail))
302 s->length = position_diff (f, offset_end_pos, s->start, tail);
304 /* Remove the completely overlapped segments in the tail */
305 it = ooo_segment_next (f, s);
306 while (it && position_leq (f, ooo_segment_end_pos (f, it),
307 offset_end_pos, tail))
309 next = ooo_segment_next (f, it);
310 ooo_segment_free (f, it - f->ooo_segments);
314 /* If partial overlap with last, merge */
315 if (it && position_leq (f, it->start, offset_end_pos, tail))
317 s->length = position_diff (f, ooo_segment_end_pos (f, it),
319 ooo_segment_free (f, it - f->ooo_segments);
321 f->ooos_newest = s - f->ooo_segments;
326 * Removes segments that can now be enqueued because the fifo's tail has
327 * advanced. Returns the number of bytes added to tail.
330 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
332 u32 s_index, bytes = 0;
336 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
337 diff = f_distance_from (f, s->start, *tail);
339 ASSERT (diff != n_bytes_enqueued);
341 if (diff > n_bytes_enqueued)
344 /* If last tail update overlaps one/multiple ooo segments, remove them */
345 while (0 <= diff && diff < n_bytes_enqueued)
347 s_index = s - f->ooo_segments;
349 /* Segment end is beyond the tail. Advance tail and remove segment */
350 if (s->length > diff)
352 bytes = s->length - diff;
353 *tail = (*tail + bytes) % f->size;
354 ooo_segment_free (f, s_index);
358 /* If we have next go on */
359 if (s->next != OOO_SEGMENT_INVALID_INDEX)
361 s = pool_elt_at_index (f->ooo_segments, s->next);
362 diff = f_distance_from (f, s->start, *tail);
363 ooo_segment_free (f, s_index);
368 ooo_segment_free (f, s_index);
373 ASSERT (bytes <= f->nitems);
377 static ooo_segment_t *
378 ooo_segment_last (svm_fifo_t * f)
382 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
385 s = svm_fifo_first_ooo_segment (f);
386 while (s->next != OOO_SEGMENT_INVALID_INDEX)
387 s = pool_elt_at_index (f->ooo_segments, s->next);
392 svm_fifo_init (svm_fifo_t * f, u32 size)
396 * usable size of the fifo set to rounded_data_size - 1
397 * to differentiate between free fifo and empty fifo.
399 f->nitems = f->size - 1;
400 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
401 f->segment_index = SVM_FIFO_INVALID_INDEX;
403 f->default_chunk.start_byte = 0;
404 f->default_chunk.length = f->size;
405 f->default_chunk.next = f->start_chunk = f->end_chunk = &f->default_chunk;
406 f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = f->start_chunk;
410 * Creates a fifo in the current heap. Fails vs blow up the process
413 svm_fifo_create (u32 data_size_in_bytes)
416 u32 rounded_data_size;
418 /* always round fifo data size to the next highest power-of-two */
419 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
420 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
421 CLIB_CACHE_LINE_BYTES);
425 clib_memset (f, 0, sizeof (*f));
426 svm_fifo_init (f, data_size_in_bytes);
431 * Creates a fifo chunk in the current heap
434 svm_fifo_chunk_alloc (u32 size)
439 /* round chunk size to the next highest power-of-two */
440 rounded_size = (1 << (max_log2 (size)));
441 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_size,
442 CLIB_CACHE_LINE_BYTES);
446 clib_memset (c, 0, sizeof (*c));
447 c->length = rounded_size;
452 svm_fifo_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos)
454 return (pos >= c->start_byte && pos < c->start_byte + c->length);
458 * Find chunk for given byte position
461 * @param pos normalized position in fifo
463 * @return chunk that includes given position or 0
465 static svm_fifo_chunk_t *
466 svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
468 rb_tree_t *rt = &f->chunk_lookup;
469 rb_node_t *cur, *prev;
472 cur = rb_node (rt, rt->root);
473 while (pos != cur->key)
477 cur = rb_node_left (rt, cur);
479 cur = rb_node_right (rt, cur);
481 if (rb_node_is_tnil (rt, cur))
483 /* Hit tnil as a left child. Find predecessor */
486 cur = rb_tree_predecessor (rt, prev);
487 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
488 if (svm_fifo_chunk_includes_pos (c, pos))
492 /* Hit tnil as a right child. Check if this is the one */
493 c = uword_to_pointer (prev->opaque, svm_fifo_chunk_t *);
494 if (svm_fifo_chunk_includes_pos (c, pos))
501 if (!rb_node_is_tnil (rt, cur))
502 return uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
507 svm_fifo_grow (svm_fifo_t * f, svm_fifo_chunk_t * c)
509 svm_fifo_chunk_t *prev;
515 f->end_chunk->next = c;
518 add_bytes += c->length;
523 prev->next = f->start_chunk;
524 f->size += add_bytes;
525 f->nitems = f->size - 1;
530 svm_fifo_try_grow (svm_fifo_t * f, u32 new_head)
532 if (new_head > f->tail)
535 svm_fifo_grow (f, f->new_chunks);
536 f->flags &= ~SVM_FIFO_F_GROW;
540 svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c)
542 svm_fifo_chunk_t *cur, *prev;
544 /* Initialize rbtree if needed and add default chunk to it. Expectation is
545 * that this is called with the heap where the rbtree's pool is pushed. */
546 if (!(f->flags & SVM_FIFO_F_MULTI_CHUNK))
548 rb_tree_init (&f->chunk_lookup);
549 rb_tree_add2 (&f->chunk_lookup, 0, pointer_to_uword (f->start_chunk));
550 f->flags |= SVM_FIFO_F_MULTI_CHUNK;
553 /* Initialize chunks and add to lookup rbtree */
557 prev = f->new_chunks;
567 cur->start_byte = prev->start_byte + prev->length;
568 rb_tree_add2 (&f->chunk_lookup, cur->start_byte,
569 pointer_to_uword (cur));
574 /* If fifo is not wrapped, update the size now */
575 if (!svm_fifo_is_wrapped (f))
577 ASSERT (!f->new_chunks);
578 svm_fifo_grow (f, c);
582 /* Postpone size update */
586 f->flags |= SVM_FIFO_F_GROW;
591 * Removes chunks that are after fifo end byte
594 svm_fifo_collect_chunks (svm_fifo_t * f)
596 svm_fifo_chunk_t *list, *cur;
598 f->flags &= ~SVM_FIFO_F_COLLECT_CHUNKS;
600 list = f->new_chunks;
605 rb_tree_del (&f->chunk_lookup, cur->start_byte);
613 svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail)
615 u32 len_to_shrink = 0, tail_pos, len;
616 svm_fifo_chunk_t *cur, *prev, *next, *start;
619 if (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX)
621 ooo_segment_t *last = ooo_segment_last (f);
622 tail_pos = ooo_segment_end_pos (f, last);
625 if (f->size_decrement)
627 /* Figure out available free space considering that there may be
629 len = clib_min (f->size_decrement, f_free_count (f, head, tail_pos));
631 f->size_decrement -= len;
634 /* Remove tail chunks if the following hold:
636 * - last used byte less than start of last chunk
638 if (tail_pos >= head && tail_pos <= f->end_chunk->start_byte)
640 /* Lookup the last position not to be removed. Since size still needs
641 * to be nitems + 1, nitems must fall within the usable space */
642 tail_pos = tail_pos > 0 ? tail_pos - 1 : tail_pos;
643 prev = svm_fifo_find_chunk (f, clib_max (f->nitems, tail_pos));
645 while (next != f->start_chunk)
649 len_to_shrink += cur->length;
653 f->size -= len_to_shrink;
655 prev->next = f->start_chunk;
657 cur->next = f->new_chunks;
658 f->new_chunks = start;
662 if (!f->size_decrement && f->size == f->nitems + 1)
664 f->flags &= ~SVM_FIFO_F_SHRINK;
665 f->flags |= SVM_FIFO_F_COLLECT_CHUNKS;
666 if (f->start_chunk == f->start_chunk->next)
667 f->flags &= ~SVM_FIFO_F_MULTI_CHUNK;
672 * Request to reduce fifo size by amount of bytes
675 svm_fifo_reduce_size (svm_fifo_t * f, u32 len, u8 try_shrink)
677 svm_fifo_chunk_t *cur;
680 /* Abort if trying to reduce by more than fifo size or if
681 * fifo is undergoing resizing already */
682 if (len >= f->size || f->size > f->nitems + 1
683 || (f->flags & SVM_FIFO_F_SHRINK) || (f->flags & SVM_FIFO_F_GROW))
686 /* last chunk that will not be removed */
687 cur = svm_fifo_find_chunk (f, f->nitems - len);
689 /* sum length of chunks that will be removed */
691 while (cur != f->start_chunk)
693 actual_len += cur->length;
697 ASSERT (actual_len <= len);
701 f->size_decrement = actual_len;
702 f->flags |= SVM_FIFO_F_SHRINK;
707 f_load_head_tail_prod (f, &head, &tail);
708 svm_fifo_try_shrink (f, head, tail);
715 svm_fifo_free_chunk_lookup (svm_fifo_t * f)
717 rb_tree_free_nodes (&f->chunk_lookup);
721 svm_fifo_free (svm_fifo_t * f)
723 ASSERT (f->refcnt > 0);
725 if (--f->refcnt == 0)
727 /* ooo data is not allocated on segment heap */
728 svm_fifo_free_chunk_lookup (f);
734 svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len)
737 u32 head, tail, head_idx;
740 ASSERT (len <= f->nitems);
742 f_load_head_tail_cons (f, &head, &tail);
744 head_idx = head - c->start_byte;
745 n_chunk = c->length - head_idx;
747 clib_memcpy_fast (&c->data[head_idx], src, len);
750 clib_memcpy_fast (&c->data[head_idx], src, n_chunk);
751 clib_memcpy_fast (&c->next->data[0], src + n_chunk, len - n_chunk);
756 svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src)
758 u32 tail, head, free_count;
760 f_load_head_tail_prod (f, &head, &tail);
762 /* free space in fifo can only increase during enqueue: SPSC */
763 free_count = f_free_count (f, head, tail);
765 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
767 if (PREDICT_FALSE (free_count == 0))
768 return SVM_FIFO_EFULL;
770 /* number of bytes we're going to copy */
771 len = clib_min (free_count, len);
772 svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, src, len, &f->tail_chunk);
773 tail = (tail + len) % f->size;
775 svm_fifo_trace_add (f, head, len, 2);
777 /* collect out-of-order segments */
778 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
779 len += ooo_segment_try_collect (f, len, &tail);
781 /* store-rel: producer owned index (paired with load-acq in consumer) */
782 clib_atomic_store_rel_n (&f->tail, tail);
788 * Enqueue a future segment.
790 * Two choices: either copies the entire segment, or copies nothing
791 * Returns 0 of the entire segment was copied
792 * Returns -1 if none of the segment was copied due to lack of space
795 svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
797 u32 tail, head, free_count, tail_idx;
799 f_load_head_tail_prod (f, &head, &tail);
801 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
802 svm_fifo_try_shrink (f, head, tail);
804 /* free space in fifo can only increase during enqueue: SPSC */
805 free_count = f_free_count (f, head, tail);
807 /* will this request fit? */
808 if ((len + offset) > free_count)
809 return SVM_FIFO_EFULL;
811 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
812 svm_fifo_trace_add (f, offset, len, 1);
813 ooo_segment_add (f, offset, head, tail, len);
814 tail_idx = (tail + offset) % f->size;
816 if (!svm_fifo_chunk_includes_pos (f->ooo_enq, tail_idx))
817 f->ooo_enq = svm_fifo_find_chunk (f, tail_idx);
819 svm_fifo_copy_to_chunk (f, f->ooo_enq, tail_idx, src, len, &f->ooo_enq);
828 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
832 ASSERT (len <= svm_fifo_max_enqueue_prod (f));
833 /* load-relaxed: producer owned index */
835 tail = (tail + len) % f->size;
836 /* store-rel: producer owned index (paired with load-acq in consumer) */
837 clib_atomic_store_rel_n (&f->tail, tail);
841 svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
843 u32 tail, head, cursize;
845 f_load_head_tail_cons (f, &head, &tail);
847 /* current size of fifo can only increase during dequeue: SPSC */
848 cursize = f_cursize (f, head, tail);
850 if (PREDICT_FALSE (cursize == 0))
851 return SVM_FIFO_EEMPTY;
853 len = clib_min (cursize, len);
854 svm_fifo_copy_from_chunk (f, f->head_chunk, head, dst, len, &f->head_chunk);
855 head = (head + len) % f->size;
857 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_GROW))
858 svm_fifo_try_grow (f, head);
860 /* store-rel: consumer owned index (paired with load-acq in producer) */
861 clib_atomic_store_rel_n (&f->head, head);
867 svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
869 u32 tail, head, cursize, head_idx;
871 f_load_head_tail_cons (f, &head, &tail);
873 /* current size of fifo can only increase during peek: SPSC */
874 cursize = f_cursize (f, head, tail);
876 if (PREDICT_FALSE (cursize < offset))
877 return SVM_FIFO_EEMPTY;
879 len = clib_min (cursize - offset, len);
880 head_idx = (head + offset) % f->size;
881 if (!svm_fifo_chunk_includes_pos (f->ooo_deq, head_idx))
882 f->ooo_deq = svm_fifo_find_chunk (f, head_idx);
884 svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
889 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
891 u32 total_drop_bytes, tail, head, cursize;
893 f_load_head_tail_cons (f, &head, &tail);
895 /* number of bytes available */
896 cursize = f_cursize (f, head, tail);
897 if (PREDICT_FALSE (cursize == 0))
898 return SVM_FIFO_EEMPTY;
900 svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
902 /* number of bytes we're going to drop */
903 total_drop_bytes = clib_min (cursize, len);
906 head = (head + total_drop_bytes) % f->size;
908 /* store-rel: consumer owned index (paired with load-acq in producer) */
909 clib_atomic_store_rel_n (&f->head, head);
911 return total_drop_bytes;
915 svm_fifo_dequeue_drop_all (svm_fifo_t * f)
917 /* consumer foreign index */
918 u32 tail = clib_atomic_load_acq_n (&f->tail);
919 /* store-rel: consumer owned index (paired with load-acq in producer) */
920 clib_atomic_store_rel_n (&f->head, tail);
924 svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs)
926 u32 cursize, head, tail, head_idx;
928 f_load_head_tail_cons (f, &head, &tail);
930 /* consumer function, cursize can only increase while we're working */
931 cursize = f_cursize (f, head, tail);
933 if (PREDICT_FALSE (cursize == 0))
934 return SVM_FIFO_EEMPTY;
940 fs[0].len = f->size - head_idx;
941 fs[0].data = f->head_chunk->data + head_idx;
942 fs[1].len = cursize - fs[0].len;
943 fs[1].data = f->head_chunk->data;
948 fs[0].data = f->head_chunk->data + head_idx;
956 svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs)
960 /* consumer owned index */
963 ASSERT (fs[0].data == f->head_chunk->data + head);
964 head = (head + fs[0].len + fs[1].len) % f->size;
965 /* store-rel: consumer owned index (paired with load-acq in producer) */
966 clib_atomic_store_rel_n (&f->head, head);
973 * - no prod and cons are accessing either dest or src fifo
974 * - fifo is not multi chunk
977 svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
980 clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size);
982 f_load_head_tail_all_acq (sf, &head, &tail);
983 clib_atomic_store_rel_n (&df->head, head);
984 clib_atomic_store_rel_n (&df->tail, tail);
988 svm_fifo_n_ooo_segments (svm_fifo_t * f)
990 return pool_elts (f->ooo_segments);
994 svm_fifo_first_ooo_segment (svm_fifo_t * f)
996 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
1000 * Set fifo pointers to requested offset
1003 svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
1005 head = head % f->size;
1006 tail = tail % f->size;
1007 clib_atomic_store_rel_n (&f->head, head);
1008 clib_atomic_store_rel_n (&f->tail, tail);
1009 if (f->flags & SVM_FIFO_F_MULTI_CHUNK)
1011 svm_fifo_chunk_t *c;
1012 c = svm_fifo_find_chunk (f, head);
1014 f->head_chunk = f->ooo_deq = c;
1015 c = svm_fifo_find_chunk (f, tail);
1017 f->tail_chunk = f->ooo_enq = c;
1022 svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
1024 if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
1026 f->subscribers[f->n_subscribers++] = subscriber;
1030 svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
1034 for (i = 0; i < f->n_subscribers; i++)
1036 if (f->subscribers[i] != subscriber)
1038 f->subscribers[i] = f->subscribers[f->n_subscribers - 1];
1045 format_ooo_segment (u8 * s, va_list * args)
1047 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1048 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
1049 u32 normalized_start = (seg->start + f->nitems - f->tail) % f->size;
1050 s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
1051 (normalized_start + seg->length) % f->size, seg->length,
1052 seg->next, seg->prev);
1057 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
1060 svm_fifo_trace_elem_t *seg = 0;
1065 vec_foreach (seg, f->trace)
1067 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
1070 s = format (s, "\n");
1072 s = format (s, "\n");
1081 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
1085 svm_fifo_trace_elem_t *trace;
1087 svm_fifo_t *dummy_fifo;
1094 trace_len = vec_len (trace);
1100 dummy_fifo = svm_fifo_create (f->size);
1101 clib_memset (f->head_chunk->data, 0xFF, f->nitems);
1102 vec_validate (data, f->nitems);
1103 for (i = 0; i < vec_len (data); i++)
1106 for (i = 0; i < trace_len; i++)
1108 offset = trace[i].offset;
1109 if (trace[i].action == 1)
1112 s = format (s, "adding [%u, %u]:", trace[i].offset,
1113 (trace[i].offset + trace[i].len) % dummy_fifo->size);
1114 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
1115 trace[i].len, &data[offset]);
1117 else if (trace[i].action == 2)
1120 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
1121 svm_fifo_enqueue (dummy_fifo, trace[i].len, &data[offset]);
1126 s = format (s, "read: %u", trace[i].len);
1127 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
1130 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
1133 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
1139 format_ooo_list (u8 * s, va_list * args)
1141 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1142 u32 indent = va_arg (*args, u32);
1143 u32 ooo_segment_index = f->ooos_list_head;
1146 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
1148 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
1149 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
1151 ooo_segment_index = seg->next;
1158 format_svm_fifo (u8 * s, va_list * args)
1160 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1161 int verbose = va_arg (*args, int);
1167 indent = format_get_indent (s);
1168 s = format (s, "cursize %u nitems %u has_event %d\n",
1169 svm_fifo_max_dequeue (f), f->nitems, f->has_event);
1170 s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
1171 indent, (f->head % f->size), (f->tail % f->size),
1172 f->segment_manager);
1175 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
1176 format_white_space, indent, f->master_session_index,
1177 f->master_thread_index, f->client_session_index,
1178 f->client_thread_index);
1182 s = format (s, "%Uooo pool %d active elts newest %u\n",
1183 format_white_space, indent, pool_elts (f->ooo_segments),
1185 if (svm_fifo_has_ooo_data (f))
1186 s = format (s, " %U", format_ooo_list, f, indent, verbose);
1193 * fd.io coding-style-patch-verification: ON
1196 * eval: (c-set-style "gnu")