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 <svm/fifo_segment.h>
22 #include <vppinfra/cpu.h>
24 CLIB_MARCH_FN (svm_fifo_copy_to_chunk, void, svm_fifo_t * f,
25 svm_fifo_chunk_t * c, u32 tail_idx, const u8 * src, u32 len,
26 svm_fifo_chunk_t ** last)
30 ASSERT (f_pos_geq (tail_idx, c->start_byte)
31 && f_pos_lt (tail_idx, c->start_byte + c->length));
33 tail_idx -= c->start_byte;
34 n_chunk = c->length - tail_idx;
38 clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
40 while ((to_copy -= n_chunk))
42 n_chunk = clib_min (c->length, to_copy);
43 clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
44 c = c->length <= to_copy ? c->next : c;
51 clib_memcpy_fast (&c->data[tail_idx], src, len);
55 CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
56 svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len,
57 svm_fifo_chunk_t ** last)
61 ASSERT (f_pos_geq (head_idx, c->start_byte)
62 && f_pos_lt (head_idx, c->start_byte + c->length));
64 head_idx -= c->start_byte;
65 n_chunk = c->length - head_idx;
69 clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
71 while ((to_copy -= n_chunk))
73 n_chunk = clib_min (c->length, to_copy);
74 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
75 c = c->length <= to_copy ? c->next : c;
82 clib_memcpy_fast (dst, &c->data[head_idx], len);
86 #ifndef CLIB_MARCH_VARIANT
89 svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
90 const u8 * src, u32 len, svm_fifo_chunk_t ** last)
92 CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
97 svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
98 u8 * dst, u32 len, svm_fifo_chunk_t ** last)
100 CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
105 ooo_segment_end_pos (ooo_segment_t * s)
107 return (s->start + s->length);
111 svm_fifo_free_ooo_data (svm_fifo_t * f)
113 pool_free (f->ooo_segments);
116 static inline ooo_segment_t *
117 ooo_segment_prev (svm_fifo_t * f, ooo_segment_t * s)
119 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
121 return pool_elt_at_index (f->ooo_segments, s->prev);
124 static inline ooo_segment_t *
125 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
127 if (s->next == OOO_SEGMENT_INVALID_INDEX)
129 return pool_elt_at_index (f->ooo_segments, s->next);
132 static inline ooo_segment_t *
133 ooo_segment_alloc (svm_fifo_t * f, u32 start, u32 length)
137 pool_get (f->ooo_segments, s);
141 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
147 ooo_segment_free (svm_fifo_t * f, u32 index)
149 ooo_segment_t *cur, *prev = 0, *next = 0;
150 cur = pool_elt_at_index (f->ooo_segments, index);
152 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
154 next = pool_elt_at_index (f->ooo_segments, cur->next);
155 next->prev = cur->prev;
158 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
160 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
161 prev->next = cur->next;
165 f->ooos_list_head = cur->next;
168 pool_put (f->ooo_segments, cur);
172 * Add segment to fifo's out-of-order segment list. Takes care of merging
173 * adjacent segments and removing overlapping ones.
176 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
178 ooo_segment_t *s, *new_s, *prev, *next, *it;
179 u32 new_index, s_end_pos, s_index;
180 u32 offset_pos, offset_end_pos;
182 ASSERT (offset + length <= f_free_count (f, head, tail));
184 offset_pos = tail + offset;
185 offset_end_pos = tail + offset + length;
187 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
189 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
191 s = ooo_segment_alloc (f, offset_pos, length);
192 f->ooos_list_head = s - f->ooo_segments;
193 f->ooos_newest = f->ooos_list_head;
197 /* Find first segment that starts after new segment */
198 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
199 while (s->next != OOO_SEGMENT_INVALID_INDEX
200 && f_pos_lt (s->start, offset_pos))
201 s = pool_elt_at_index (f->ooo_segments, s->next);
203 /* If we have a previous and we overlap it, use it as starting point */
204 prev = ooo_segment_prev (f, s);
205 if (prev && f_pos_leq (offset_pos, ooo_segment_end_pos (prev)))
208 s_end_pos = ooo_segment_end_pos (s);
210 /* Since we have previous, offset start position cannot be smaller
211 * than prev->start. Check tail */
212 ASSERT (f_pos_lt (s->start, offset_pos));
216 s_index = s - f->ooo_segments;
217 s_end_pos = ooo_segment_end_pos (s);
219 /* No overlap, add before current segment */
220 if (f_pos_lt (offset_end_pos, s->start))
222 new_s = ooo_segment_alloc (f, offset_pos, length);
223 new_index = new_s - f->ooo_segments;
225 /* Pool might've moved, get segment again */
226 s = pool_elt_at_index (f->ooo_segments, s_index);
227 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
229 new_s->prev = s->prev;
230 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
231 prev->next = new_index;
236 f->ooos_list_head = new_index;
239 new_s->next = s_index;
241 f->ooos_newest = new_index;
244 /* No overlap, add after current segment */
245 else if (f_pos_gt (offset_pos, s_end_pos))
247 new_s = ooo_segment_alloc (f, offset_pos, length);
248 new_index = new_s - f->ooo_segments;
250 /* Pool might've moved, get segment again */
251 s = pool_elt_at_index (f->ooo_segments, s_index);
253 /* Needs to be last */
254 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
256 new_s->prev = s_index;
258 f->ooos_newest = new_index;
268 if (f_pos_lt (offset_pos, s->start))
270 s->start = offset_pos;
271 s->length = s_end_pos - s->start;
272 f->ooos_newest = s - f->ooo_segments;
277 /* Overlapping tail */
278 if (f_pos_gt (offset_end_pos, s_end_pos))
280 s->length = offset_end_pos - s->start;
282 /* Remove the completely overlapped segments in the tail */
283 it = ooo_segment_next (f, s);
284 while (it && f_pos_leq (ooo_segment_end_pos (it), offset_end_pos))
286 next = ooo_segment_next (f, it);
287 ooo_segment_free (f, it - f->ooo_segments);
291 /* If partial overlap with last, merge */
292 if (it && f_pos_leq (it->start, offset_end_pos))
294 s->length = ooo_segment_end_pos (it) - s->start;
295 ooo_segment_free (f, it - f->ooo_segments);
297 f->ooos_newest = s - f->ooo_segments;
302 * Removes segments that can now be enqueued because the fifo's tail has
303 * advanced. Returns the number of bytes added to tail.
306 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
308 u32 s_index, bytes = 0;
312 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
313 diff = *tail - s->start;
315 ASSERT (diff != n_bytes_enqueued);
317 if (diff > n_bytes_enqueued)
320 /* If last tail update overlaps one/multiple ooo segments, remove them */
321 while (0 <= diff && diff < n_bytes_enqueued)
323 s_index = s - f->ooo_segments;
325 /* Segment end is beyond the tail. Advance tail and remove segment */
326 if (s->length > diff)
328 bytes = s->length - diff;
329 *tail = *tail + bytes;
330 ooo_segment_free (f, s_index);
334 /* If we have next go on */
335 if (s->next != OOO_SEGMENT_INVALID_INDEX)
337 s = pool_elt_at_index (f->ooo_segments, s->next);
338 diff = *tail - s->start;
339 ooo_segment_free (f, s_index);
344 ooo_segment_free (f, s_index);
349 ASSERT (bytes <= f->size);
353 __clib_unused static ooo_segment_t *
354 ooo_segment_last (svm_fifo_t * f)
358 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
361 s = svm_fifo_first_ooo_segment (f);
362 while (s->next != OOO_SEGMENT_INVALID_INDEX)
363 s = pool_elt_at_index (f->ooo_segments, s->next);
368 svm_fifo_init (svm_fifo_t * f, u32 size)
370 svm_fifo_chunk_t *c, *prev;
374 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
375 f->segment_index = SVM_FIFO_INVALID_INDEX;
377 f->head = f->tail = f->flags = 0;
378 f->head_chunk = f->tail_chunk = f->start_chunk;
379 f->ooo_deq = f->ooo_enq = 0;
381 min_alloc = size > 32 << 10 ? size >> 3 : 4096;
382 min_alloc = clib_min (min_alloc, 64 << 10);
383 f->min_alloc = min_alloc;
388 f->start_chunk->start_byte = 0;
389 prev = f->start_chunk;
394 c->start_byte = prev->start_byte + prev->length;
401 svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type)
405 ASSERT (!rb_tree_is_init (&f->ooo_enq_lookup));
406 rb_tree_init (&f->ooo_enq_lookup);
410 ASSERT (!rb_tree_is_init (&f->ooo_deq_lookup));
411 rb_tree_init (&f->ooo_deq_lookup);
416 * Creates a fifo in the current heap. Fails vs blow up the process
419 svm_fifo_alloc (u32 data_size_in_bytes)
421 u32 rounded_data_size;
425 f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES);
429 clib_memset (f, 0, sizeof (*f));
431 /* always round fifo data size to the next highest power-of-two */
432 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
433 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_data_size,
434 CLIB_CACHE_LINE_BYTES);
441 clib_memset (c, 0, sizeof (*c));
443 c->length = data_size_in_bytes;
444 c->enq_rb_index = RBTREE_TNIL_INDEX;
445 c->deq_rb_index = RBTREE_TNIL_INDEX;
446 f->start_chunk = f->end_chunk = c;
452 * Creates a fifo chunk in the current heap
455 svm_fifo_chunk_alloc (u32 size)
460 /* round chunk size to the next highest power-of-two */
461 rounded_size = (1 << (max_log2 (size)));
462 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_size,
463 CLIB_CACHE_LINE_BYTES);
467 clib_memset (c, 0, sizeof (*c));
468 c->length = rounded_size;
473 * Find chunk for given byte position
476 * @param pos normalized position in fifo
478 * @return chunk that includes given position or 0
480 static svm_fifo_chunk_t *
481 svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
486 while (c && !f_chunk_includes_pos (c, pos))
492 static svm_fifo_chunk_t *
493 svm_fifo_find_next_chunk (svm_fifo_t * f, svm_fifo_chunk_t * start, u32 pos)
500 while (c && !f_chunk_includes_pos (c, pos))
507 svm_fifo_max_read_chunk (svm_fifo_t * f)
509 u32 head, tail, end_chunk;
511 f_load_head_tail_cons (f, &head, &tail);
512 ASSERT (!f->head_chunk || f_chunk_includes_pos (f->head_chunk, head));
516 f->head_chunk = svm_fifo_find_chunk (f, head);
517 if (PREDICT_FALSE (!f->head_chunk))
521 end_chunk = f_chunk_end (f->head_chunk);
523 return f_pos_lt (end_chunk, tail) ? end_chunk - head : tail - head;
527 svm_fifo_max_write_chunk (svm_fifo_t * f)
531 f_load_head_tail_prod (f, &head, &tail);
532 ASSERT (!f->tail_chunk || f_chunk_includes_pos (f->tail_chunk, tail));
534 return f->tail_chunk ? f_chunk_end (f->tail_chunk) - tail : 0;
538 f_find_node_rbtree (rb_tree_t * rt, u32 pos)
540 rb_node_t *cur, *prev;
542 cur = rb_node (rt, rt->root);
543 if (PREDICT_FALSE (rb_node_is_tnil (rt, cur)))
546 while (pos != cur->key)
549 if (f_pos_lt (pos, cur->key))
551 cur = rb_node_left (rt, cur);
552 if (rb_node_is_tnil (rt, cur))
554 cur = rb_tree_predecessor (rt, prev);
560 cur = rb_node_right (rt, cur);
561 if (rb_node_is_tnil (rt, cur))
569 if (rb_node_is_tnil (rt, cur))
575 static svm_fifo_chunk_t *
576 f_find_chunk_rbtree (rb_tree_t * rt, u32 pos)
581 if (!rb_tree_is_init (rt))
584 n = f_find_node_rbtree (rt, pos);
587 c = uword_to_pointer (n->opaque, svm_fifo_chunk_t *);
588 if (f_chunk_includes_pos (c, pos))
595 f_update_ooo_enq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
597 rb_tree_t *rt = &f->ooo_enq_lookup;
601 /* Use linear search if rbtree is not initialized */
602 if (PREDICT_FALSE (!rb_tree_is_init (rt)))
604 f->ooo_enq = svm_fifo_find_next_chunk (f, f->tail_chunk, start_pos);
608 if (rt->root == RBTREE_TNIL_INDEX)
611 ASSERT (c->enq_rb_index == RBTREE_TNIL_INDEX);
612 c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
613 pointer_to_uword (c), f_pos_lt);
617 cur = f_find_node_rbtree (rt, start_pos);
618 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
619 ASSERT (f_pos_leq (c->start_byte, start_pos));
622 if (f_chunk_includes_pos (c, start_pos))
625 if (f_chunk_includes_pos (c, end_pos))
631 if (!c || c->enq_rb_index != RBTREE_TNIL_INDEX)
634 c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
635 pointer_to_uword (c), f_pos_lt);
637 if (f_chunk_includes_pos (c, start_pos))
640 while (!f_chunk_includes_pos (c, end_pos));
644 f_update_ooo_deq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
646 rb_tree_t *rt = &f->ooo_deq_lookup;
650 /* Use linear search if rbtree is not initialized */
651 if (PREDICT_FALSE (!rb_tree_is_init (rt)))
653 f->ooo_deq = svm_fifo_find_chunk (f, start_pos);
657 if (rt->root == RBTREE_TNIL_INDEX)
660 ASSERT (c->deq_rb_index == RBTREE_TNIL_INDEX);
661 c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
662 pointer_to_uword (c), f_pos_lt);
666 cur = f_find_node_rbtree (rt, start_pos);
667 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
668 ASSERT (f_pos_leq (c->start_byte, start_pos));
671 if (f_chunk_includes_pos (c, start_pos))
674 if (f_chunk_includes_pos (c, end_pos))
680 if (!c || c->deq_rb_index != RBTREE_TNIL_INDEX)
683 c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
684 pointer_to_uword (c), f_pos_lt);
686 if (f_chunk_includes_pos (c, start_pos))
689 while (!f_chunk_includes_pos (c, end_pos));
692 static svm_fifo_chunk_t *
693 f_lookup_clear_enq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
696 rb_tree_t *rt = &f->ooo_enq_lookup;
701 while (c && !f_chunk_includes_pos (c, end_pos))
703 if (c->enq_rb_index != RBTREE_TNIL_INDEX)
705 n = rb_node (rt, c->enq_rb_index);
706 rb_tree_del_node (rt, n);
707 c->enq_rb_index = RBTREE_TNIL_INDEX;
713 /* No ooo segments left, so make sure the current chunk
714 * is not tracked in the enq rbtree */
715 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX
716 && c && c->enq_rb_index != RBTREE_TNIL_INDEX)
718 n = rb_node (rt, c->enq_rb_index);
719 rb_tree_del_node (rt, n);
720 c->enq_rb_index = RBTREE_TNIL_INDEX;
726 static svm_fifo_chunk_t *
727 f_lookup_clear_deq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
730 rb_tree_t *rt = &f->ooo_deq_lookup;
735 while (c && !f_chunk_includes_pos (c, end_pos))
737 if (c->deq_rb_index != RBTREE_TNIL_INDEX)
739 n = rb_node (rt, c->deq_rb_index);
740 rb_tree_del_node (rt, n);
741 c->deq_rb_index = RBTREE_TNIL_INDEX;
751 svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c)
753 svm_fifo_chunk_t *cur, *prev;
760 cur->start_byte = prev->start_byte + prev->length;
761 cur->enq_rb_index = RBTREE_TNIL_INDEX;
762 cur->deq_rb_index = RBTREE_TNIL_INDEX;
769 f->end_chunk->next = c;
779 svm_fifo_free_chunk_lookup (svm_fifo_t * f)
781 rb_tree_free_nodes (&f->ooo_enq_lookup);
782 rb_tree_free_nodes (&f->ooo_deq_lookup);
786 svm_fifo_free (svm_fifo_t * f)
788 ASSERT (f->refcnt > 0);
790 if (--f->refcnt == 0)
792 /* ooo data is not allocated on segment heap */
793 svm_fifo_free_chunk_lookup (f);
799 svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len)
802 u32 head, tail, head_idx;
805 ASSERT (len <= f->size);
807 f_load_head_tail_cons (f, &head, &tail);
810 f->head_chunk = svm_fifo_find_chunk (f, head);
813 head_idx = head - c->start_byte;
814 n_chunk = c->length - head_idx;
816 clib_memcpy_fast (&c->data[head_idx], src, len);
819 ASSERT (len - n_chunk <= c->next->length);
820 clib_memcpy_fast (&c->data[head_idx], src, n_chunk);
821 clib_memcpy_fast (&c->next->data[0], src + n_chunk, len - n_chunk);
826 f_try_grow (svm_fifo_t * f, u32 head, u32 tail, u32 len)
829 u32 alloc_size, free_alloced;
831 free_alloced = f_chunk_end (f->end_chunk) - tail;
832 ASSERT (free_alloced < len);
834 alloc_size = clib_min (f->min_alloc, f->size - (tail - head));
835 alloc_size = clib_max (alloc_size, len - free_alloced);
837 c = fsh_alloc_chunk (f->fs_hdr, f->slice_index, alloc_size);
838 if (PREDICT_FALSE (!c))
841 svm_fifo_add_chunk (f, c);
846 svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src)
848 u32 tail, head, free_count;
849 svm_fifo_chunk_t *old_tail_c;
851 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
853 f_load_head_tail_prod (f, &head, &tail);
855 /* free space in fifo can only increase during enqueue: SPSC */
856 free_count = f_free_count (f, head, tail);
858 if (PREDICT_FALSE (free_count == 0))
859 return SVM_FIFO_EFULL;
861 /* number of bytes we're going to copy */
862 len = clib_min (free_count, len);
864 if (f_pos_gt (tail + len, f_chunk_end (f->end_chunk)))
866 if (PREDICT_FALSE (f_try_grow (f, head, tail, len)))
868 len = f_chunk_end (f->end_chunk) - tail;
870 return SVM_FIFO_EGROW;
874 old_tail_c = f->tail_chunk;
876 svm_fifo_copy_to_chunk (f, f->tail_chunk, tail, src, len, &f->tail_chunk);
879 svm_fifo_trace_add (f, head, len, 2);
881 /* collect out-of-order segments */
882 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
884 len += ooo_segment_try_collect (f, len, &tail);
885 /* Tail chunk might've changed even if nothing was collected */
886 f->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
890 /* store-rel: producer owned index (paired with load-acq in consumer) */
891 clib_atomic_store_rel_n (&f->tail, tail);
897 * Enqueue a future segment.
899 * Two choices: either copies the entire segment, or copies nothing
900 * Returns 0 of the entire segment was copied
901 * Returns -1 if none of the segment was copied due to lack of space
904 svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
906 u32 tail, head, free_count, enq_pos;
908 f_load_head_tail_prod (f, &head, &tail);
910 /* free space in fifo can only increase during enqueue: SPSC */
911 free_count = f_free_count (f, head, tail);
912 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
914 /* will this request fit? */
915 if ((len + offset) > free_count)
916 return SVM_FIFO_EFULL;
918 enq_pos = tail + offset;
920 if (f_pos_gt (enq_pos + len, f_chunk_end (f->end_chunk)))
922 if (PREDICT_FALSE (f_try_grow (f, head, tail, offset + len)))
923 return SVM_FIFO_EGROW;
926 svm_fifo_trace_add (f, offset, len, 1);
927 ooo_segment_add (f, offset, head, tail, len);
929 if (!f->ooo_enq || !f_chunk_includes_pos (f->ooo_enq, enq_pos))
930 f_update_ooo_enq (f, enq_pos, enq_pos + len);
932 svm_fifo_copy_to_chunk (f, f->ooo_enq, enq_pos, src, len, &f->ooo_enq);
941 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
945 ASSERT (len <= svm_fifo_max_enqueue_prod (f));
946 /* load-relaxed: producer owned index */
950 if (rb_tree_is_init (&f->ooo_enq_lookup))
952 f->tail_chunk = f_lookup_clear_enq_chunks (f, f->tail_chunk, tail);
957 f->tail_chunk = svm_fifo_find_next_chunk (f, f->tail_chunk, tail);
960 /* store-rel: producer owned index (paired with load-acq in consumer) */
961 clib_atomic_store_rel_n (&f->tail, tail);
964 always_inline svm_fifo_chunk_t *
965 f_unlink_chunks (svm_fifo_t * f, u32 end_pos, u8 maybe_ooo)
967 svm_fifo_chunk_t *start, *prev = 0, *c;
971 ASSERT (!f_chunk_includes_pos (f->start_chunk, end_pos));
974 rt = &f->ooo_deq_lookup;
980 if (maybe_ooo && c->deq_rb_index != RBTREE_TNIL_INDEX)
982 n = rb_node (rt, c->deq_rb_index);
983 ASSERT (n == f_find_node_rbtree (rt, c->start_byte));
984 rb_tree_del_node (rt, n);
985 c->deq_rb_index = RBTREE_TNIL_INDEX;
992 while (!f_chunk_includes_pos (c, end_pos));
996 if (f->ooo_deq && f_pos_lt (f->ooo_deq->start_byte, f_chunk_end (c)))
1001 if (PREDICT_FALSE (f->ooo_deq != 0))
1005 /* Avoid unlinking the last chunk */
1010 start = f->start_chunk;
1017 svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
1019 u32 tail, head, cursize;
1021 f_load_head_tail_cons (f, &head, &tail);
1023 /* current size of fifo can only increase during dequeue: SPSC */
1024 cursize = f_cursize (f, head, tail);
1026 if (PREDICT_FALSE (cursize == 0))
1027 return SVM_FIFO_EEMPTY;
1029 len = clib_min (cursize, len);
1032 f->head_chunk = svm_fifo_find_chunk (f, head);
1034 svm_fifo_copy_from_chunk (f, f->head_chunk, head, dst, len, &f->head_chunk);
1037 if (f_pos_geq (head, f_chunk_end (f->start_chunk)))
1038 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1039 f_unlink_chunks (f, head, 0));
1041 /* store-rel: consumer owned index (paired with load-acq in producer) */
1042 clib_atomic_store_rel_n (&f->head, head);
1048 svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
1050 u32 tail, head, cursize, head_idx;
1052 f_load_head_tail_cons (f, &head, &tail);
1054 /* current size of fifo can only increase during peek: SPSC */
1055 cursize = f_cursize (f, head, tail);
1057 if (PREDICT_FALSE (cursize < offset))
1058 return SVM_FIFO_EEMPTY;
1060 len = clib_min (cursize - offset, len);
1061 head_idx = head + offset;
1063 if (!f->ooo_deq || !f_chunk_includes_pos (f->ooo_deq, head_idx))
1064 f_update_ooo_deq (f, head_idx, head_idx + len);
1066 svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
1071 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
1073 u32 total_drop_bytes, tail, head, cursize;
1075 f_load_head_tail_cons (f, &head, &tail);
1077 /* number of bytes available */
1078 cursize = f_cursize (f, head, tail);
1079 if (PREDICT_FALSE (cursize == 0))
1080 return SVM_FIFO_EEMPTY;
1082 /* number of bytes we're going to drop */
1083 total_drop_bytes = clib_min (cursize, len);
1085 svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
1088 head = head + total_drop_bytes;
1090 if (f_pos_geq (head, f_chunk_end (f->start_chunk)))
1092 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1093 f_unlink_chunks (f, head, 1));
1095 f_chunk_includes_pos (f->start_chunk, head) ? f->start_chunk : 0;
1098 /* store-rel: consumer owned index (paired with load-acq in producer) */
1099 clib_atomic_store_rel_n (&f->head, head);
1101 return total_drop_bytes;
1105 * Drop all data from fifo
1107 * Should be called only from vpp side because of lookup cleanup
1110 svm_fifo_dequeue_drop_all (svm_fifo_t * f)
1114 f_load_head_tail_all_acq (f, &head, &tail);
1116 if (!f->head_chunk || !f_chunk_includes_pos (f->head_chunk, head))
1117 f->head_chunk = svm_fifo_find_chunk (f, head);
1119 f->head_chunk = f_lookup_clear_deq_chunks (f, f->head_chunk, tail);
1121 if (f_pos_geq (tail, f_chunk_end (f->start_chunk)))
1122 fsh_collect_chunks (f->fs_hdr, f->slice_index,
1123 f_unlink_chunks (f, tail, 0));
1125 /* store-rel: consumer owned index (paired with load-acq in producer) */
1126 clib_atomic_store_rel_n (&f->head, tail);
1130 svm_fifo_fill_chunk_list (svm_fifo_t * f)
1134 f_load_head_tail_prod (f, &head, &tail);
1136 if (f_chunk_end (f->end_chunk) - head >= f->size)
1139 if (f_try_grow (f, head, tail, f->size - (tail - head)))
1140 return SVM_FIFO_EGROW;
1146 svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs)
1148 u32 cursize, head, tail, head_idx;
1150 f_load_head_tail_cons (f, &head, &tail);
1152 /* consumer function, cursize can only increase while we're working */
1153 cursize = f_cursize (f, head, tail);
1155 if (PREDICT_FALSE (cursize == 0))
1156 return SVM_FIFO_EEMPTY;
1162 fs[0].len = f->size - head_idx;
1163 fs[0].data = f->head_chunk->data + head_idx;
1164 fs[1].len = cursize - fs[0].len;
1165 fs[1].data = f->head_chunk->data;
1169 fs[0].len = cursize;
1170 fs[0].data = f->head_chunk->data + head_idx;
1178 svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs)
1182 /* consumer owned index */
1185 ASSERT (fs[0].data == f->head_chunk->data + head);
1186 head = (head + fs[0].len + fs[1].len);
1187 /* store-rel: consumer owned index (paired with load-acq in producer) */
1188 clib_atomic_store_rel_n (&f->head, head);
1195 * - no prod and cons are accessing either dest or src fifo
1196 * - fifo is not multi chunk
1199 svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
1203 /* Support only single chunk clones for now */
1204 ASSERT (svm_fifo_n_chunks (sf) == 1);
1206 clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size);
1208 f_load_head_tail_all_acq (sf, &head, &tail);
1209 clib_atomic_store_rel_n (&df->head, head);
1210 clib_atomic_store_rel_n (&df->tail, tail);
1214 svm_fifo_n_ooo_segments (svm_fifo_t * f)
1216 return pool_elts (f->ooo_segments);
1220 svm_fifo_first_ooo_segment (svm_fifo_t * f)
1222 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
1226 * Set fifo pointers to requested offset
1229 svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
1231 svm_fifo_chunk_t *c;
1233 clib_atomic_store_rel_n (&f->head, head);
1234 clib_atomic_store_rel_n (&f->tail, tail);
1236 c = svm_fifo_find_chunk (f, head);
1238 f->head_chunk = f->ooo_deq = c;
1239 c = svm_fifo_find_chunk (f, tail);
1241 f->tail_chunk = f->ooo_enq = c;
1245 svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
1247 if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
1249 f->subscribers[f->n_subscribers++] = subscriber;
1253 svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
1257 for (i = 0; i < f->n_subscribers; i++)
1259 if (f->subscribers[i] != subscriber)
1261 f->subscribers[i] = f->subscribers[f->n_subscribers - 1];
1268 svm_fifo_is_sane (svm_fifo_t * f)
1270 svm_fifo_chunk_t *tmp;
1272 if (f->head_chunk && !f_chunk_includes_pos (f->head_chunk, f->head))
1274 if (f->tail_chunk && !f_chunk_includes_pos (f->tail_chunk, f->tail))
1278 if (rb_tree_is_init (&f->ooo_deq_lookup))
1280 if (f_pos_lt (f->ooo_deq->start_byte, f->start_chunk->start_byte)
1281 || f_pos_gt (f->ooo_deq->start_byte,
1282 f_chunk_end (f->end_chunk)))
1285 tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup,
1286 f->ooo_deq->start_byte);
1289 tmp = svm_fifo_find_chunk (f, f->ooo_deq->start_byte);
1290 if (tmp != f->ooo_deq)
1295 if (rb_tree_is_init (&f->ooo_enq_lookup))
1297 if (f_pos_lt (f->ooo_enq->start_byte, f->start_chunk->start_byte)
1298 || f_pos_gt (f->ooo_enq->start_byte,
1299 f_chunk_end (f->end_chunk)))
1302 tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup,
1303 f->ooo_enq->start_byte);
1307 tmp = svm_fifo_find_next_chunk (f, f->tail_chunk,
1308 f->ooo_enq->start_byte);
1310 if (tmp != f->ooo_enq)
1314 if (f->start_chunk->next)
1316 svm_fifo_chunk_t *c, *prev = 0, *tmp;
1317 u32 chunks_bytes = 0;
1322 tmp = svm_fifo_find_chunk (f, c->start_byte);
1325 if (prev && (prev->start_byte + prev->length != c->start_byte))
1328 if (c->enq_rb_index != RBTREE_TNIL_INDEX)
1330 tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup, c->start_byte);
1337 if (c->deq_rb_index != RBTREE_TNIL_INDEX)
1339 tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup, c->start_byte);
1347 chunks_bytes += c->length;
1353 if (chunks_bytes < f->tail - f->head)
1361 svm_fifo_n_chunks (svm_fifo_t * f)
1363 svm_fifo_chunk_t *c;
1377 format_ooo_segment (u8 * s, va_list * args)
1379 svm_fifo_t __clib_unused *f = va_arg (*args, svm_fifo_t *);
1380 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
1381 s = format (s, "[%u, %u], len %u, next %d, prev %d", seg->start,
1382 seg->start + seg->length, seg->length, seg->next, seg->prev);
1387 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
1390 svm_fifo_trace_elem_t *seg = 0;
1395 vec_foreach (seg, f->trace)
1397 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
1400 s = format (s, "\n");
1402 s = format (s, "\n");
1411 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
1415 svm_fifo_trace_elem_t *trace;
1417 svm_fifo_t *dummy_fifo;
1424 trace_len = vec_len (trace);
1430 dummy_fifo = svm_fifo_alloc (f->size);
1431 svm_fifo_init (f, f->size);
1432 clib_memset (f->head_chunk->data, 0xFF, f->size);
1433 vec_validate (data, f->size);
1434 for (i = 0; i < vec_len (data); i++)
1437 for (i = 0; i < trace_len; i++)
1439 offset = trace[i].offset;
1440 if (trace[i].action == 1)
1443 s = format (s, "adding [%u, %u]:", trace[i].offset,
1444 (trace[i].offset + trace[i].len));
1445 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
1446 trace[i].len, &data[offset]);
1448 else if (trace[i].action == 2)
1451 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
1452 svm_fifo_enqueue (dummy_fifo, trace[i].len, &data[offset]);
1457 s = format (s, "read: %u", trace[i].len);
1458 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
1461 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
1464 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
1470 format_ooo_list (u8 * s, va_list * args)
1472 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1473 u32 indent = va_arg (*args, u32);
1474 u32 ooo_segment_index = f->ooos_list_head;
1477 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
1479 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
1480 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
1482 ooo_segment_index = seg->next;
1489 format_svm_fifo (u8 * s, va_list * args)
1491 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1492 int verbose = va_arg (*args, int);
1498 indent = format_get_indent (s);
1499 s = format (s, "cursize %u nitems %u has_event %d min_alloc %u\n",
1500 svm_fifo_max_dequeue (f), f->size, f->has_event, f->min_alloc);
1501 s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
1502 indent, f->head, f->tail, f->segment_manager);
1505 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
1506 format_white_space, indent, f->master_session_index,
1507 f->master_thread_index, f->client_session_index,
1508 f->client_thread_index);
1512 s = format (s, "%Uooo pool %d active elts newest %u\n",
1513 format_white_space, indent, pool_elts (f->ooo_segments),
1515 if (svm_fifo_has_ooo_data (f))
1516 s = format (s, " %U", format_ooo_list, f, indent, verbose);
1523 * fd.io coding-style-patch-verification: ON
1526 * eval: (c-set-style "gnu")