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 CLIB_MEM_UNPOISON (c, sizeof (*c));
74 CLIB_MEM_UNPOISON (c->data, c->length);
75 n_chunk = clib_min (c->length, to_copy);
76 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
77 c = c->length <= to_copy ? c->next : c;
84 clib_memcpy_fast (dst, &c->data[head_idx], len);
88 #ifndef CLIB_MARCH_VARIANT
91 svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
92 const u8 * src, u32 len, svm_fifo_chunk_t ** last)
94 CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
99 svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
100 u8 * dst, u32 len, svm_fifo_chunk_t ** last)
102 CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
107 ooo_segment_end_pos (ooo_segment_t * s)
109 return (s->start + s->length);
113 svm_fifo_free_ooo_data (svm_fifo_t * f)
115 pool_free (f->ooo_segments);
118 static inline ooo_segment_t *
119 ooo_segment_prev (svm_fifo_t * f, ooo_segment_t * s)
121 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
123 return pool_elt_at_index (f->ooo_segments, s->prev);
126 static inline ooo_segment_t *
127 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
129 if (s->next == OOO_SEGMENT_INVALID_INDEX)
131 return pool_elt_at_index (f->ooo_segments, s->next);
134 static inline ooo_segment_t *
135 ooo_segment_alloc (svm_fifo_t * f, u32 start, u32 length)
139 pool_get (f->ooo_segments, s);
143 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
149 ooo_segment_free (svm_fifo_t * f, u32 index)
151 ooo_segment_t *cur, *prev = 0, *next = 0;
152 cur = pool_elt_at_index (f->ooo_segments, index);
154 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
156 next = pool_elt_at_index (f->ooo_segments, cur->next);
157 next->prev = cur->prev;
160 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
162 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
163 prev->next = cur->next;
167 f->ooos_list_head = cur->next;
170 pool_put (f->ooo_segments, cur);
174 * Add segment to fifo's out-of-order segment list. Takes care of merging
175 * adjacent segments and removing overlapping ones.
178 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
180 ooo_segment_t *s, *new_s, *prev, *next, *it;
181 u32 new_index, s_end_pos, s_index;
182 u32 offset_pos, offset_end_pos;
184 ASSERT (offset + length <= f_free_count (f, head, tail));
186 offset_pos = tail + offset;
187 offset_end_pos = tail + offset + length;
189 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
191 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
193 s = ooo_segment_alloc (f, offset_pos, length);
194 f->ooos_list_head = s - f->ooo_segments;
195 f->ooos_newest = f->ooos_list_head;
199 /* Find first segment that starts after new segment */
200 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
201 while (s->next != OOO_SEGMENT_INVALID_INDEX
202 && f_pos_lt (s->start, offset_pos))
203 s = pool_elt_at_index (f->ooo_segments, s->next);
205 /* If we have a previous and we overlap it, use it as starting point */
206 prev = ooo_segment_prev (f, s);
207 if (prev && f_pos_leq (offset_pos, ooo_segment_end_pos (prev)))
210 s_end_pos = ooo_segment_end_pos (s);
212 /* Since we have previous, offset start position cannot be smaller
213 * than prev->start. Check tail */
214 ASSERT (f_pos_lt (s->start, offset_pos));
218 s_index = s - f->ooo_segments;
219 s_end_pos = ooo_segment_end_pos (s);
221 /* No overlap, add before current segment */
222 if (f_pos_lt (offset_end_pos, s->start))
224 new_s = ooo_segment_alloc (f, offset_pos, length);
225 new_index = new_s - f->ooo_segments;
227 /* Pool might've moved, get segment again */
228 s = pool_elt_at_index (f->ooo_segments, s_index);
229 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
231 new_s->prev = s->prev;
232 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
233 prev->next = new_index;
238 f->ooos_list_head = new_index;
241 new_s->next = s_index;
243 f->ooos_newest = new_index;
246 /* No overlap, add after current segment */
247 else if (f_pos_gt (offset_pos, s_end_pos))
249 new_s = ooo_segment_alloc (f, offset_pos, length);
250 new_index = new_s - f->ooo_segments;
252 /* Pool might've moved, get segment again */
253 s = pool_elt_at_index (f->ooo_segments, s_index);
255 /* Needs to be last */
256 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
258 new_s->prev = s_index;
260 f->ooos_newest = new_index;
270 if (f_pos_lt (offset_pos, s->start))
272 s->start = offset_pos;
273 s->length = s_end_pos - s->start;
274 f->ooos_newest = s - f->ooo_segments;
279 /* Overlapping tail */
280 if (f_pos_gt (offset_end_pos, s_end_pos))
282 s->length = offset_end_pos - s->start;
284 /* Remove the completely overlapped segments in the tail */
285 it = ooo_segment_next (f, s);
286 while (it && f_pos_leq (ooo_segment_end_pos (it), offset_end_pos))
288 next = ooo_segment_next (f, it);
289 ooo_segment_free (f, it - f->ooo_segments);
293 /* If partial overlap with last, merge */
294 if (it && f_pos_leq (it->start, offset_end_pos))
296 s->length = ooo_segment_end_pos (it) - s->start;
297 ooo_segment_free (f, it - f->ooo_segments);
299 f->ooos_newest = s - f->ooo_segments;
304 * Removes segments that can now be enqueued because the fifo's tail has
305 * advanced. Returns the number of bytes added to tail.
308 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
310 u32 s_index, bytes = 0;
314 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
315 diff = *tail - s->start;
317 ASSERT (diff != n_bytes_enqueued);
319 if (diff > n_bytes_enqueued)
322 /* If last tail update overlaps one/multiple ooo segments, remove them */
323 while (0 <= diff && diff < n_bytes_enqueued)
325 s_index = s - f->ooo_segments;
327 /* Segment end is beyond the tail. Advance tail and remove segment */
328 if (s->length > diff)
330 bytes = s->length - diff;
331 *tail = *tail + bytes;
332 ooo_segment_free (f, s_index);
336 /* If we have next go on */
337 if (s->next != OOO_SEGMENT_INVALID_INDEX)
339 s = pool_elt_at_index (f->ooo_segments, s->next);
340 diff = *tail - s->start;
341 ooo_segment_free (f, s_index);
346 ooo_segment_free (f, s_index);
351 ASSERT (bytes <= f->shr->size);
355 __clib_unused static ooo_segment_t *
356 ooo_segment_last (svm_fifo_t * f)
360 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
363 s = svm_fifo_first_ooo_segment (f);
364 while (s->next != OOO_SEGMENT_INVALID_INDEX)
365 s = pool_elt_at_index (f->ooo_segments, s->next);
370 svm_fifo_init (svm_fifo_t * f, u32 size)
372 svm_fifo_chunk_t *c, *prev;
376 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
377 f->segment_index = SVM_FIFO_INVALID_INDEX;
379 f->shr->head = f->shr->tail = f->flags = 0;
380 f->shr->head_chunk = f->shr->tail_chunk = f->shr->start_chunk;
381 f->ooo_deq = f->ooo_enq = 0;
383 min_alloc = size > 32 << 10 ? size >> 3 : 4096;
384 min_alloc = clib_min (min_alloc, 64 << 10);
385 f->shr->min_alloc = min_alloc;
390 f->shr->start_chunk->start_byte = 0;
391 prev = f->shr->start_chunk;
392 prev->enq_rb_index = prev->deq_rb_index = RBTREE_TNIL_INDEX;
397 c->start_byte = prev->start_byte + prev->length;
398 c->enq_rb_index = c->deq_rb_index = RBTREE_TNIL_INDEX;
405 svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type)
409 ASSERT (!rb_tree_is_init (&f->ooo_enq_lookup));
410 rb_tree_init (&f->ooo_enq_lookup);
414 ASSERT (!rb_tree_is_init (&f->ooo_deq_lookup));
415 rb_tree_init (&f->ooo_deq_lookup);
420 * Creates a fifo in the current heap. Fails vs blow up the process
423 svm_fifo_alloc (u32 data_size_in_bytes)
425 u32 rounded_data_size;
429 f = clib_mem_alloc_aligned_or_null (sizeof (*f), CLIB_CACHE_LINE_BYTES);
433 clib_memset (f, 0, sizeof (*f));
435 /* always round fifo data size to the next highest power-of-two */
436 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
437 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_data_size,
438 CLIB_CACHE_LINE_BYTES);
445 clib_memset (c, 0, sizeof (*c));
447 c->length = data_size_in_bytes;
448 c->enq_rb_index = RBTREE_TNIL_INDEX;
449 c->deq_rb_index = RBTREE_TNIL_INDEX;
450 f->shr->start_chunk = f->shr->end_chunk = c;
456 * Creates a fifo chunk in the current heap
459 svm_fifo_chunk_alloc (u32 size)
464 /* round chunk size to the next highest power-of-two */
465 rounded_size = (1 << (max_log2 (size)));
466 c = clib_mem_alloc_aligned_or_null (sizeof (*c) + rounded_size,
467 CLIB_CACHE_LINE_BYTES);
471 clib_memset (c, 0, sizeof (*c));
472 c->length = rounded_size;
477 * Find chunk for given byte position
480 * @param pos normalized position in fifo
482 * @return chunk that includes given position or 0
484 static svm_fifo_chunk_t *
485 svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
489 c = f->shr->start_chunk;
490 while (c && !f_chunk_includes_pos (c, pos))
496 static svm_fifo_chunk_t *
497 svm_fifo_find_next_chunk (svm_fifo_t * f, svm_fifo_chunk_t * start, u32 pos)
504 while (c && !f_chunk_includes_pos (c, pos))
511 svm_fifo_max_read_chunk (svm_fifo_t * f)
513 u32 head, tail, end_chunk;
515 f_load_head_tail_cons (f, &head, &tail);
516 ASSERT (!f->shr->head_chunk ||
517 f_chunk_includes_pos (f->shr->head_chunk, head));
519 if (!f->shr->head_chunk)
521 f->shr->head_chunk = svm_fifo_find_chunk (f, head);
522 if (PREDICT_FALSE (!f->shr->head_chunk))
526 end_chunk = f_chunk_end (f->shr->head_chunk);
528 return f_pos_lt (end_chunk, tail) ? end_chunk - head : tail - head;
532 svm_fifo_max_write_chunk (svm_fifo_t * f)
536 f_load_head_tail_prod (f, &head, &tail);
537 ASSERT (!f->shr->tail_chunk ||
538 f_chunk_includes_pos (f->shr->tail_chunk, tail));
540 return f->shr->tail_chunk ? f_chunk_end (f->shr->tail_chunk) - tail : 0;
544 f_find_node_rbtree (rb_tree_t * rt, u32 pos)
546 rb_node_t *cur, *prev;
548 cur = rb_node (rt, rt->root);
549 if (PREDICT_FALSE (rb_node_is_tnil (rt, cur)))
552 while (pos != cur->key)
555 if (f_pos_lt (pos, cur->key))
557 cur = rb_node_left (rt, cur);
558 if (rb_node_is_tnil (rt, cur))
560 cur = rb_tree_predecessor (rt, prev);
566 cur = rb_node_right (rt, cur);
567 if (rb_node_is_tnil (rt, cur))
575 if (rb_node_is_tnil (rt, cur))
581 static svm_fifo_chunk_t *
582 f_find_chunk_rbtree (rb_tree_t * rt, u32 pos)
587 if (!rb_tree_is_init (rt))
590 n = f_find_node_rbtree (rt, pos);
593 c = uword_to_pointer (n->opaque, svm_fifo_chunk_t *);
594 if (f_chunk_includes_pos (c, pos))
601 f_update_ooo_enq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
603 rb_tree_t *rt = &f->ooo_enq_lookup;
607 /* Use linear search if rbtree is not initialized */
608 if (PREDICT_FALSE (!rb_tree_is_init (rt)))
610 f->ooo_enq = svm_fifo_find_next_chunk (f, f->shr->tail_chunk, start_pos);
614 if (rt->root == RBTREE_TNIL_INDEX)
616 c = f->shr->tail_chunk;
617 ASSERT (c->enq_rb_index == RBTREE_TNIL_INDEX);
618 c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
619 pointer_to_uword (c), f_pos_lt);
623 cur = f_find_node_rbtree (rt, start_pos);
624 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
625 ASSERT (f_pos_leq (c->start_byte, start_pos));
628 if (f_chunk_includes_pos (c, start_pos))
631 if (f_chunk_includes_pos (c, end_pos))
637 if (!c || c->enq_rb_index != RBTREE_TNIL_INDEX)
640 c->enq_rb_index = rb_tree_add_custom (rt, c->start_byte,
641 pointer_to_uword (c), f_pos_lt);
643 if (f_chunk_includes_pos (c, start_pos))
646 while (!f_chunk_includes_pos (c, end_pos));
650 f_update_ooo_deq (svm_fifo_t * f, u32 start_pos, u32 end_pos)
652 rb_tree_t *rt = &f->ooo_deq_lookup;
656 /* Use linear search if rbtree is not initialized */
657 if (PREDICT_FALSE (!rb_tree_is_init (rt)))
659 f->ooo_deq = svm_fifo_find_chunk (f, start_pos);
663 if (rt->root == RBTREE_TNIL_INDEX)
665 c = f->shr->start_chunk;
666 ASSERT (c->deq_rb_index == RBTREE_TNIL_INDEX);
667 c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
668 pointer_to_uword (c), f_pos_lt);
672 cur = f_find_node_rbtree (rt, start_pos);
673 c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
674 ASSERT (f_pos_leq (c->start_byte, start_pos));
677 if (f_chunk_includes_pos (c, start_pos))
680 if (f_chunk_includes_pos (c, end_pos))
686 if (!c || c->deq_rb_index != RBTREE_TNIL_INDEX)
689 c->deq_rb_index = rb_tree_add_custom (rt, c->start_byte,
690 pointer_to_uword (c), f_pos_lt);
692 if (f_chunk_includes_pos (c, start_pos))
695 while (!f_chunk_includes_pos (c, end_pos));
698 static svm_fifo_chunk_t *
699 f_lookup_clear_enq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
702 rb_tree_t *rt = &f->ooo_enq_lookup;
707 while (c && !f_chunk_includes_pos (c, end_pos))
709 if (c->enq_rb_index != RBTREE_TNIL_INDEX)
711 n = rb_node (rt, c->enq_rb_index);
712 rb_tree_del_node (rt, n);
713 c->enq_rb_index = RBTREE_TNIL_INDEX;
719 /* No ooo segments left, so make sure the current chunk
720 * is not tracked in the enq rbtree */
721 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX
722 && c && c->enq_rb_index != RBTREE_TNIL_INDEX)
724 n = rb_node (rt, c->enq_rb_index);
725 rb_tree_del_node (rt, n);
726 c->enq_rb_index = RBTREE_TNIL_INDEX;
732 static svm_fifo_chunk_t *
733 f_lookup_clear_deq_chunks (svm_fifo_t * f, svm_fifo_chunk_t * start,
736 rb_tree_t *rt = &f->ooo_deq_lookup;
741 while (c && !f_chunk_includes_pos (c, end_pos))
743 if (c->deq_rb_index != RBTREE_TNIL_INDEX)
745 n = rb_node (rt, c->deq_rb_index);
746 rb_tree_del_node (rt, n);
747 c->deq_rb_index = RBTREE_TNIL_INDEX;
757 svm_fifo_free_chunk_lookup (svm_fifo_t * f)
759 rb_tree_free_nodes (&f->ooo_enq_lookup);
760 rb_tree_free_nodes (&f->ooo_deq_lookup);
764 svm_fifo_free (svm_fifo_t * f)
766 ASSERT (f->refcnt > 0);
768 if (--f->refcnt == 0)
770 /* ooo data is not allocated on segment heap */
771 svm_fifo_free_chunk_lookup (f);
777 svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len)
780 u32 head, tail, head_idx;
783 ASSERT (len <= f->shr->size);
785 f_load_head_tail_cons (f, &head, &tail);
787 if (!f->shr->head_chunk)
788 f->shr->head_chunk = svm_fifo_find_chunk (f, head);
790 c = f->shr->head_chunk;
791 head_idx = head - c->start_byte;
792 n_chunk = c->length - head_idx;
794 clib_memcpy_fast (&c->data[head_idx], src, len);
797 ASSERT (len - n_chunk <= c->next->length);
798 clib_memcpy_fast (&c->data[head_idx], src, n_chunk);
799 clib_memcpy_fast (&c->next->data[0], src + n_chunk, len - n_chunk);
804 f_try_chunk_alloc (svm_fifo_t * f, u32 head, u32 tail, u32 len)
806 svm_fifo_chunk_t *c, *cur, *prev;
807 u32 alloc_size, free_alloced;
809 free_alloced = f_chunk_end (f->shr->end_chunk) - tail;
811 alloc_size = clib_min (f->shr->min_alloc, f->shr->size - (tail - head));
812 alloc_size = clib_max (alloc_size, len - free_alloced);
814 c = fsh_alloc_chunk (f->fs_hdr, f->shr->slice_index, alloc_size);
815 if (PREDICT_FALSE (!c))
819 prev = f->shr->end_chunk;
823 cur->start_byte = prev->start_byte + prev->length;
824 cur->enq_rb_index = RBTREE_TNIL_INDEX;
825 cur->deq_rb_index = RBTREE_TNIL_INDEX;
832 f->shr->end_chunk->next = c;
833 f->shr->end_chunk = prev;
835 if (!f->shr->tail_chunk)
836 f->shr->tail_chunk = c;
842 svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src)
844 u32 tail, head, free_count;
845 svm_fifo_chunk_t *old_tail_c;
847 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
849 f_load_head_tail_prod (f, &head, &tail);
851 /* free space in fifo can only increase during enqueue: SPSC */
852 free_count = f_free_count (f, head, tail);
854 if (PREDICT_FALSE (free_count == 0))
855 return SVM_FIFO_EFULL;
857 /* number of bytes we're going to copy */
858 len = clib_min (free_count, len);
860 if (f_pos_gt (tail + len, f_chunk_end (f->shr->end_chunk)))
862 if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
864 len = f_chunk_end (f->shr->end_chunk) - tail;
866 return SVM_FIFO_EGROW;
870 old_tail_c = f->shr->tail_chunk;
872 svm_fifo_copy_to_chunk (f, f->shr->tail_chunk, tail, src, len,
873 &f->shr->tail_chunk);
876 svm_fifo_trace_add (f, head, len, 2);
878 /* collect out-of-order segments */
879 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
881 len += ooo_segment_try_collect (f, len, &tail);
882 /* Tail chunk might've changed even if nothing was collected */
883 f->shr->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
887 /* store-rel: producer owned index (paired with load-acq in consumer) */
888 clib_atomic_store_rel_n (&f->shr->tail, tail);
894 * Enqueue a future segment.
896 * Two choices: either copies the entire segment, or copies nothing
897 * Returns 0 of the entire segment was copied
898 * Returns -1 if none of the segment was copied due to lack of space
901 svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
903 u32 tail, head, free_count, enq_pos;
905 f_load_head_tail_prod (f, &head, &tail);
907 /* free space in fifo can only increase during enqueue: SPSC */
908 free_count = f_free_count (f, head, tail);
909 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
911 /* will this request fit? */
912 if ((len + offset) > free_count)
913 return SVM_FIFO_EFULL;
915 enq_pos = tail + offset;
917 if (f_pos_gt (enq_pos + len, f_chunk_end (f->shr->end_chunk)))
919 if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, offset + len)))
920 return SVM_FIFO_EGROW;
923 svm_fifo_trace_add (f, offset, len, 1);
924 ooo_segment_add (f, offset, head, tail, len);
926 if (!f->ooo_enq || !f_chunk_includes_pos (f->ooo_enq, enq_pos))
927 f_update_ooo_enq (f, enq_pos, enq_pos + len);
929 svm_fifo_copy_to_chunk (f, f->ooo_enq, enq_pos, src, len, &f->ooo_enq);
938 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len)
942 ASSERT (len <= svm_fifo_max_enqueue_prod (f));
943 /* load-relaxed: producer owned index */
947 if (rb_tree_is_init (&f->ooo_enq_lookup))
950 f_lookup_clear_enq_chunks (f, f->shr->tail_chunk, tail);
956 svm_fifo_find_next_chunk (f, f->shr->tail_chunk, tail);
959 /* store-rel: producer owned index (paired with load-acq in consumer) */
960 clib_atomic_store_rel_n (&f->shr->tail, tail);
964 svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[],
965 u32 n_segs, u8 allow_partial)
967 u32 tail, head, free_count, len = 0, i;
968 svm_fifo_chunk_t *old_tail_c;
970 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
972 f_load_head_tail_prod (f, &head, &tail);
974 /* free space in fifo can only increase during enqueue: SPSC */
975 free_count = f_free_count (f, head, tail);
977 if (PREDICT_FALSE (free_count == 0))
978 return SVM_FIFO_EFULL;
980 for (i = 0; i < n_segs; i++)
983 old_tail_c = f->shr->tail_chunk;
987 if (PREDICT_FALSE (free_count < len))
988 return SVM_FIFO_EFULL;
990 if (f_pos_gt (tail + len, f_chunk_end (f->shr->end_chunk)))
992 if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
993 return SVM_FIFO_EGROW;
996 for (i = 0; i < n_segs; i++)
998 svm_fifo_copy_to_chunk (f, f->shr->tail_chunk, tail, segs[i].data,
999 segs[i].len, &f->shr->tail_chunk);
1000 tail += segs[i].len;
1005 len = clib_min (free_count, len);
1007 if (f_pos_gt (tail + len, f_chunk_end (f->shr->end_chunk)))
1009 if (PREDICT_FALSE (f_try_chunk_alloc (f, head, tail, len)))
1011 len = f_chunk_end (f->shr->end_chunk) - tail;
1013 return SVM_FIFO_EGROW;
1020 u32 to_copy = clib_min (segs[i].len, len);
1021 svm_fifo_copy_to_chunk (f, f->shr->tail_chunk, tail, segs[i].data,
1022 to_copy, &f->shr->tail_chunk);
1029 /* collect out-of-order segments */
1030 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
1032 len += ooo_segment_try_collect (f, len, &tail);
1033 /* Tail chunk might've changed even if nothing was collected */
1034 f->shr->tail_chunk = f_lookup_clear_enq_chunks (f, old_tail_c, tail);
1038 /* store-rel: producer owned index (paired with load-acq in consumer) */
1039 clib_atomic_store_rel_n (&f->shr->tail, tail);
1044 always_inline svm_fifo_chunk_t *
1045 f_unlink_chunks (svm_fifo_t * f, u32 end_pos, u8 maybe_ooo)
1047 svm_fifo_chunk_t *start, *prev = 0, *c;
1051 ASSERT (!f_chunk_includes_pos (f->shr->start_chunk, end_pos));
1054 rt = &f->ooo_deq_lookup;
1056 c = f->shr->start_chunk;
1060 if (maybe_ooo && c->deq_rb_index != RBTREE_TNIL_INDEX)
1062 n = rb_node (rt, c->deq_rb_index);
1063 ASSERT (n == f_find_node_rbtree (rt, c->start_byte));
1064 rb_tree_del_node (rt, n);
1065 c->deq_rb_index = RBTREE_TNIL_INDEX;
1072 while (!f_chunk_includes_pos (c, end_pos));
1076 if (f->ooo_deq && f_pos_lt (f->ooo_deq->start_byte, f_chunk_end (c)))
1081 if (PREDICT_FALSE (f->ooo_deq != 0))
1085 /* Avoid unlinking the last chunk */
1090 start = f->shr->start_chunk;
1091 f->shr->start_chunk = c;
1097 svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst)
1099 u32 tail, head, cursize;
1101 f_load_head_tail_cons (f, &head, &tail);
1103 /* current size of fifo can only increase during dequeue: SPSC */
1104 cursize = f_cursize (f, head, tail);
1106 if (PREDICT_FALSE (cursize == 0))
1107 return SVM_FIFO_EEMPTY;
1109 len = clib_min (cursize, len);
1111 if (!f->shr->head_chunk)
1112 f->shr->head_chunk = svm_fifo_find_chunk (f, head);
1114 svm_fifo_copy_from_chunk (f, f->shr->head_chunk, head, dst, len,
1115 &f->shr->head_chunk);
1118 /* In order dequeues are not supported in combination with ooo peeking.
1119 * Use svm_fifo_dequeue_drop instead. */
1120 ASSERT (rb_tree_n_nodes (&f->ooo_deq_lookup) <= 1);
1122 if (f_pos_geq (head, f_chunk_end (f->shr->start_chunk)))
1123 fsh_collect_chunks (f->fs_hdr, f->shr->slice_index,
1124 f_unlink_chunks (f, head, 0));
1126 /* store-rel: consumer owned index (paired with load-acq in producer) */
1127 clib_atomic_store_rel_n (&f->shr->head, head);
1133 svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
1135 u32 tail, head, cursize, head_idx;
1137 f_load_head_tail_cons (f, &head, &tail);
1139 /* current size of fifo can only increase during peek: SPSC */
1140 cursize = f_cursize (f, head, tail);
1142 if (PREDICT_FALSE (cursize < offset))
1143 return SVM_FIFO_EEMPTY;
1145 len = clib_min (cursize - offset, len);
1146 head_idx = head + offset;
1148 CLIB_MEM_UNPOISON (f->ooo_deq, sizeof (*f->ooo_deq));
1149 if (!f->ooo_deq || !f_chunk_includes_pos (f->ooo_deq, head_idx))
1150 f_update_ooo_deq (f, head_idx, head_idx + len);
1152 svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
1157 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len)
1159 u32 total_drop_bytes, tail, head, cursize;
1161 f_load_head_tail_cons (f, &head, &tail);
1163 /* number of bytes available */
1164 cursize = f_cursize (f, head, tail);
1165 if (PREDICT_FALSE (cursize == 0))
1166 return SVM_FIFO_EEMPTY;
1168 /* number of bytes we're going to drop */
1169 total_drop_bytes = clib_min (cursize, len);
1171 svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
1174 head = head + total_drop_bytes;
1176 if (f_pos_geq (head, f_chunk_end (f->shr->start_chunk)))
1178 fsh_collect_chunks (f->fs_hdr, f->shr->slice_index,
1179 f_unlink_chunks (f, head, 1));
1180 f->shr->head_chunk = f_chunk_includes_pos (f->shr->start_chunk, head) ?
1181 f->shr->start_chunk :
1185 /* store-rel: consumer owned index (paired with load-acq in producer) */
1186 clib_atomic_store_rel_n (&f->shr->head, head);
1188 return total_drop_bytes;
1192 * Drop all data from fifo
1196 svm_fifo_dequeue_drop_all (svm_fifo_t * f)
1200 f_load_head_tail_all_acq (f, &head, &tail);
1202 if (!f->shr->head_chunk || !f_chunk_includes_pos (f->shr->head_chunk, head))
1203 f->shr->head_chunk = svm_fifo_find_chunk (f, head);
1205 f->shr->head_chunk = f_lookup_clear_deq_chunks (f, f->shr->head_chunk, tail);
1207 if (f_pos_geq (tail, f_chunk_end (f->shr->start_chunk)))
1208 fsh_collect_chunks (f->fs_hdr, f->shr->slice_index,
1209 f_unlink_chunks (f, tail, 0));
1211 /* store-rel: consumer owned index (paired with load-acq in producer) */
1212 clib_atomic_store_rel_n (&f->shr->head, tail);
1216 svm_fifo_fill_chunk_list (svm_fifo_t * f)
1220 f_load_head_tail_prod (f, &head, &tail);
1222 if (f_chunk_end (f->shr->end_chunk) - head >= f->shr->size)
1225 if (f_try_chunk_alloc (f, head, tail, f->shr->size - (tail - head)))
1226 return SVM_FIFO_EGROW;
1232 svm_fifo_provision_chunks (svm_fifo_t *f, svm_fifo_seg_t *fs, u32 n_segs,
1235 u32 head, tail, n_avail, head_pos, n_bytes, fs_index = 1, clen;
1236 svm_fifo_chunk_t *c;
1238 f_load_head_tail_prod (f, &head, &tail);
1240 if (f_free_count (f, head, tail) < len)
1241 return SVM_FIFO_EFULL;
1243 n_avail = f_chunk_end (f->shr->end_chunk) - tail;
1245 if (n_avail < len && f_try_chunk_alloc (f, head, tail, len))
1246 return SVM_FIFO_EGROW;
1248 c = f->shr->tail_chunk;
1249 head_pos = (tail - c->start_byte);
1250 fs[0].data = c->data + head_pos;
1251 fs[0].len = clib_min (c->length - head_pos, len);
1252 n_bytes = fs[0].len;
1254 while (n_bytes < len && fs_index < n_segs)
1257 clen = clib_min (c->length, len - n_bytes);
1258 fs[fs_index].data = c->data;
1259 fs[fs_index].len = clen;
1268 svm_fifo_segments (svm_fifo_t * f, u32 offset, svm_fifo_seg_t * fs,
1269 u32 n_segs, u32 max_bytes)
1271 u32 cursize, to_read, head, tail, fs_index = 1;
1272 u32 n_bytes, head_pos, len, start;
1273 svm_fifo_chunk_t *c;
1275 f_load_head_tail_cons (f, &head, &tail);
1277 /* consumer function, cursize can only increase while we're working */
1278 cursize = f_cursize (f, head, tail);
1280 if (PREDICT_FALSE (cursize == 0))
1281 return SVM_FIFO_EEMPTY;
1283 if (offset >= cursize)
1284 return SVM_FIFO_EEMPTY;
1286 to_read = clib_min (cursize - offset, max_bytes);
1287 start = head + offset;
1289 if (!f->shr->head_chunk)
1290 f->shr->head_chunk = svm_fifo_find_chunk (f, head);
1292 c = f->shr->head_chunk;
1294 while (!f_chunk_includes_pos (c, start))
1297 head_pos = start - c->start_byte;
1298 fs[0].data = c->data + head_pos;
1299 fs[0].len = clib_min (c->length - head_pos, to_read);
1300 n_bytes = fs[0].len;
1302 while (n_bytes < to_read && fs_index < n_segs)
1305 len = clib_min (c->length, to_read - n_bytes);
1306 fs[fs_index].data = c->data;
1307 fs[fs_index].len = len;
1319 * - no prod and cons are accessing either dest or src fifo
1320 * - fifo is not multi chunk
1323 svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
1327 /* Support only single chunk clones for now */
1328 ASSERT (svm_fifo_n_chunks (sf) == 1);
1330 clib_memcpy_fast (df->shr->head_chunk->data, sf->shr->head_chunk->data,
1333 f_load_head_tail_all_acq (sf, &head, &tail);
1334 clib_atomic_store_rel_n (&df->shr->head, head);
1335 clib_atomic_store_rel_n (&df->shr->tail, tail);
1339 svm_fifo_n_ooo_segments (svm_fifo_t * f)
1341 return pool_elts (f->ooo_segments);
1345 svm_fifo_first_ooo_segment (svm_fifo_t * f)
1347 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
1351 * Set fifo pointers to requested offset
1354 svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
1356 svm_fifo_chunk_t *c;
1358 clib_atomic_store_rel_n (&f->shr->head, head);
1359 clib_atomic_store_rel_n (&f->shr->tail, tail);
1361 c = svm_fifo_find_chunk (f, head);
1363 f->shr->head_chunk = f->ooo_deq = c;
1364 c = svm_fifo_find_chunk (f, tail);
1366 f->shr->tail_chunk = f->ooo_enq = c;
1370 svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
1372 if (f->shr->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
1374 f->shr->subscribers[f->shr->n_subscribers++] = subscriber;
1378 svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
1382 for (i = 0; i < f->shr->n_subscribers; i++)
1384 if (f->shr->subscribers[i] != subscriber)
1386 f->shr->subscribers[i] = f->shr->subscribers[f->shr->n_subscribers - 1];
1387 f->shr->n_subscribers--;
1393 svm_fifo_is_sane (svm_fifo_t * f)
1395 svm_fifo_chunk_t *tmp;
1397 if (f->shr->head_chunk &&
1398 !f_chunk_includes_pos (f->shr->head_chunk, f->shr->head))
1400 if (f->shr->tail_chunk &&
1401 !f_chunk_includes_pos (f->shr->tail_chunk, f->shr->tail))
1405 if (rb_tree_is_init (&f->ooo_deq_lookup))
1407 if (f_pos_lt (f->ooo_deq->start_byte,
1408 f->shr->start_chunk->start_byte) ||
1409 f_pos_gt (f->ooo_deq->start_byte,
1410 f_chunk_end (f->shr->end_chunk)))
1413 tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup,
1414 f->ooo_deq->start_byte);
1417 tmp = svm_fifo_find_chunk (f, f->ooo_deq->start_byte);
1418 if (tmp != f->ooo_deq)
1423 if (rb_tree_is_init (&f->ooo_enq_lookup))
1425 if (f_pos_lt (f->ooo_enq->start_byte,
1426 f->shr->start_chunk->start_byte) ||
1427 f_pos_gt (f->ooo_enq->start_byte,
1428 f_chunk_end (f->shr->end_chunk)))
1431 tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup,
1432 f->ooo_enq->start_byte);
1436 tmp = svm_fifo_find_next_chunk (f, f->shr->tail_chunk,
1437 f->ooo_enq->start_byte);
1439 if (tmp != f->ooo_enq)
1443 if (f->shr->start_chunk->next)
1445 svm_fifo_chunk_t *c, *prev = 0, *tmp;
1446 u32 chunks_bytes = 0;
1448 c = f->shr->start_chunk;
1451 tmp = svm_fifo_find_chunk (f, c->start_byte);
1454 if (prev && (prev->start_byte + prev->length != c->start_byte))
1457 if (c->enq_rb_index != RBTREE_TNIL_INDEX)
1459 tmp = f_find_chunk_rbtree (&f->ooo_enq_lookup, c->start_byte);
1466 if (c->deq_rb_index != RBTREE_TNIL_INDEX)
1468 tmp = f_find_chunk_rbtree (&f->ooo_deq_lookup, c->start_byte);
1476 chunks_bytes += c->length;
1482 if (chunks_bytes < f->shr->tail - f->shr->head)
1490 svm_fifo_n_chunks (svm_fifo_t * f)
1492 svm_fifo_chunk_t *c;
1495 c = f->shr->start_chunk;
1506 format_ooo_segment (u8 * s, va_list * args)
1508 svm_fifo_t __clib_unused *f = va_arg (*args, svm_fifo_t *);
1509 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
1510 s = format (s, "[%u, %u], len %u, next %d, prev %d", seg->start,
1511 seg->start + seg->length, seg->length, seg->next, seg->prev);
1516 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
1519 svm_fifo_trace_elem_t *seg = 0;
1524 vec_foreach (seg, f->trace)
1526 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
1529 s = format (s, "\n");
1531 s = format (s, "\n");
1540 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
1544 svm_fifo_trace_elem_t *trace;
1546 svm_fifo_t *placeholder_fifo;
1553 trace_len = vec_len (trace);
1559 placeholder_fifo = svm_fifo_alloc (f->shr->size);
1560 svm_fifo_init (f, f->shr->size);
1561 clib_memset (f->shr->head_chunk->data, 0xFF, f->shr->size);
1562 vec_validate (data, f->shr->size);
1563 for (i = 0; i < vec_len (data); i++)
1566 for (i = 0; i < trace_len; i++)
1568 offset = trace[i].offset;
1569 if (trace[i].action == 1)
1572 s = format (s, "adding [%u, %u]:", trace[i].offset,
1573 (trace[i].offset + trace[i].len));
1574 svm_fifo_enqueue_with_offset (placeholder_fifo, trace[i].offset,
1575 trace[i].len, &data[offset]);
1577 else if (trace[i].action == 2)
1580 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
1581 svm_fifo_enqueue (placeholder_fifo, trace[i].len, &data[offset]);
1586 s = format (s, "read: %u", trace[i].len);
1587 svm_fifo_dequeue_drop (placeholder_fifo, trace[i].len);
1590 s = format (s, "%U", format_svm_fifo, placeholder_fifo, 1);
1593 s = format (s, "result: %U", format_svm_fifo, placeholder_fifo, 1);
1599 format_ooo_list (u8 * s, va_list * args)
1601 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1602 u32 indent = va_arg (*args, u32);
1603 u32 ooo_segment_index = f->ooos_list_head;
1606 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
1608 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
1609 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
1611 ooo_segment_index = seg->next;
1618 format_svm_fifo (u8 * s, va_list * args)
1620 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
1621 int verbose = va_arg (*args, int);
1627 indent = format_get_indent (s);
1628 s = format (s, "cursize %u nitems %u has_event %d min_alloc %u\n",
1629 svm_fifo_max_dequeue (f), f->shr->size, f->shr->has_event,
1631 s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
1632 indent, f->shr->head, f->shr->tail, f->segment_manager);
1635 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
1636 format_white_space, indent, f->shr->master_session_index,
1637 f->master_thread_index, f->shr->client_session_index,
1638 f->client_thread_index);
1642 s = format (s, "%Uooo pool %d active elts newest %u\n",
1643 format_white_space, indent, pool_elts (f->ooo_segments),
1645 if (svm_fifo_has_ooo_data (f))
1646 s = format (s, " %U", format_ooo_list, f, indent, verbose);
1653 * fd.io coding-style-patch-verification: ON
1656 * eval: (c-set-style "gnu")