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,
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);
37 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);
48 clib_memcpy_fast (&c->data[tail_idx], src, len);
52 CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
53 svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len,
58 head_idx -= c->start_byte;
59 n_chunk = c->length - head_idx;
63 clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
64 while ((to_copy -= n_chunk))
67 n_chunk = clib_min (c->length, to_copy);
68 clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
75 clib_memcpy_fast (dst, &c->data[head_idx], len);
79 #ifndef CLIB_MARCH_VARIANT
82 svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
83 const u8 * src, u32 len, u8 update_tail)
85 CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
90 svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
91 u8 * dst, u32 len, u8 update_head)
93 CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
98 position_lt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
100 return (ooo_segment_distance_from_tail (f, a, tail)
101 < ooo_segment_distance_from_tail (f, b, tail));
105 position_leq (svm_fifo_t * f, u32 a, u32 b, u32 tail)
107 return (ooo_segment_distance_from_tail (f, a, tail)
108 <= ooo_segment_distance_from_tail (f, b, tail));
112 position_gt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
114 return (ooo_segment_distance_from_tail (f, a, tail)
115 > ooo_segment_distance_from_tail (f, b, tail));
119 position_diff (svm_fifo_t * f, u32 posa, u32 posb, u32 tail)
121 return ooo_segment_distance_from_tail (f, posa, tail)
122 - ooo_segment_distance_from_tail (f, posb, tail);
126 ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s)
128 return s->start + s->length;
132 format_ooo_segment (u8 * s, va_list * args)
134 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
135 ooo_segment_t *seg = va_arg (*args, ooo_segment_t *);
136 u32 normalized_start = (seg->start + f->nitems - f->tail) % f->size;
137 s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start,
138 (normalized_start + seg->length) % f->size, seg->length,
139 seg->next, seg->prev);
144 svm_fifo_dump_trace (u8 * s, svm_fifo_t * f)
147 svm_fifo_trace_elem_t *seg = 0;
152 vec_foreach (seg, f->trace)
154 s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action);
157 s = format (s, "\n");
159 s = format (s, "\n");
168 svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose)
172 svm_fifo_trace_elem_t *trace;
174 svm_fifo_t *dummy_fifo;
181 trace_len = vec_len (trace);
187 dummy_fifo = svm_fifo_create (f->size);
188 clib_memset (f->head_chunk->data, 0xFF, f->nitems);
189 vec_validate (data, f->nitems);
190 for (i = 0; i < vec_len (data); i++)
193 for (i = 0; i < trace_len; i++)
195 offset = trace[i].offset;
196 if (trace[i].action == 1)
199 s = format (s, "adding [%u, %u]:", trace[i].offset,
200 (trace[i].offset + trace[i].len) % dummy_fifo->size);
201 svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset,
202 trace[i].len, &data[offset]);
204 else if (trace[i].action == 2)
207 s = format (s, "adding [%u, %u]:", 0, trace[i].len);
208 svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]);
213 s = format (s, "read: %u", trace[i].len);
214 svm_fifo_dequeue_drop (dummy_fifo, trace[i].len);
217 s = format (s, "%U", format_svm_fifo, dummy_fifo, 1);
220 s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1);
226 format_ooo_list (u8 * s, va_list * args)
228 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
229 u32 indent = va_arg (*args, u32);
230 u32 ooo_segment_index = f->ooos_list_head;
233 while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX)
235 seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index);
236 s = format (s, "%U%U\n", format_white_space, indent, format_ooo_segment,
238 ooo_segment_index = seg->next;
245 format_svm_fifo (u8 * s, va_list * args)
247 svm_fifo_t *f = va_arg (*args, svm_fifo_t *);
248 int verbose = va_arg (*args, int);
254 indent = format_get_indent (s);
255 s = format (s, "cursize %u nitems %u has_event %d\n",
256 svm_fifo_max_dequeue (f), f->nitems, f->has_event);
257 s = format (s, "%Uhead %u tail %u segment manager %u\n", format_white_space,
258 indent, (f->head % f->size), (f->tail % f->size),
262 s = format (s, "%Uvpp session %d thread %d app session %d thread %d\n",
263 format_white_space, indent, f->master_session_index,
264 f->master_thread_index, f->client_session_index,
265 f->client_thread_index);
269 s = format (s, "%Uooo pool %d active elts newest %u\n",
270 format_white_space, indent, pool_elts (f->ooo_segments),
272 if (svm_fifo_has_ooo_data (f))
273 s = format (s, " %U", format_ooo_list, f, indent, verbose);
279 svm_fifo_init (svm_fifo_t * f, u32 size)
283 * usable size of the fifo set to rounded_data_size - 1
284 * to differentiate between free fifo and empty fifo.
286 f->nitems = f->size - 1;
287 f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX;
288 f->ct_session_index = SVM_FIFO_INVALID_SESSION_INDEX;
289 f->segment_index = SVM_FIFO_INVALID_INDEX;
291 f->default_chunk.start_byte = 0;
292 f->default_chunk.length = f->size;
293 f->default_chunk.next = f->start_chunk = &f->default_chunk;
294 f->end_chunk = f->head_chunk = f->tail_chunk = f->start_chunk;
297 /** create an svm fifo, in the current heap. Fails vs blow up the process */
299 svm_fifo_create (u32 data_size_in_bytes)
302 u32 rounded_data_size;
304 /* always round fifo data size to the next highest power-of-two */
305 rounded_data_size = (1 << (max_log2 (data_size_in_bytes)));
306 f = clib_mem_alloc_aligned_or_null (sizeof (*f) + rounded_data_size,
307 CLIB_CACHE_LINE_BYTES);
311 clib_memset (f, 0, sizeof (*f));
312 svm_fifo_init (f, data_size_in_bytes);
317 svm_fifo_size_update (svm_fifo_t * f, svm_fifo_chunk_t * c)
319 svm_fifo_chunk_t *prev;
325 c->start_byte = prev->start_byte + prev->length;
326 add_bytes += c->length;
332 prev->next = f->start_chunk;
333 f->size += add_bytes;
334 f->nitems = f->size - 1;
339 svm_fifo_try_size_update (svm_fifo_t * f, u32 new_head)
341 if (new_head % f->size > f->tail % f->size)
344 svm_fifo_size_update (f, f->new_chunks);
345 f->flags &= ~SVM_FIFO_F_SIZE_UPDATE;
349 svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c)
351 if (svm_fifo_is_wrapped (f))
355 svm_fifo_chunk_t *prev;
357 prev = f->new_chunks;
366 f->flags |= SVM_FIFO_F_SIZE_UPDATE;
370 svm_fifo_size_update (f, c);
374 svm_fifo_free (svm_fifo_t * f)
376 ASSERT (f->refcnt > 0);
378 if (--f->refcnt == 0)
380 pool_free (f->ooo_segments);
385 always_inline ooo_segment_t *
386 ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
390 pool_get (f->ooo_segments, s);
395 s->prev = s->next = OOO_SEGMENT_INVALID_INDEX;
401 ooo_segment_del (svm_fifo_t * f, u32 index)
403 ooo_segment_t *cur, *prev = 0, *next = 0;
404 cur = pool_elt_at_index (f->ooo_segments, index);
406 if (cur->next != OOO_SEGMENT_INVALID_INDEX)
408 next = pool_elt_at_index (f->ooo_segments, cur->next);
409 next->prev = cur->prev;
412 if (cur->prev != OOO_SEGMENT_INVALID_INDEX)
414 prev = pool_elt_at_index (f->ooo_segments, cur->prev);
415 prev->next = cur->next;
419 f->ooos_list_head = cur->next;
422 pool_put (f->ooo_segments, cur);
426 * Add segment to fifo's out-of-order segment list. Takes care of merging
427 * adjacent segments and removing overlapping ones.
430 ooo_segment_add (svm_fifo_t * f, u32 offset, u32 head, u32 tail, u32 length)
432 ooo_segment_t *s, *new_s, *prev, *next, *it;
433 u32 new_index, s_end_pos, s_index;
434 u32 offset_pos, offset_end_pos;
436 ASSERT (offset + length <= ooo_segment_distance_from_tail (f, head, tail)
439 offset_pos = tail + offset;
440 offset_end_pos = tail + offset + length;
442 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
444 if (f->ooos_list_head == OOO_SEGMENT_INVALID_INDEX)
446 s = ooo_segment_new (f, offset_pos, length);
447 f->ooos_list_head = s - f->ooo_segments;
448 f->ooos_newest = f->ooos_list_head;
452 /* Find first segment that starts after new segment */
453 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
454 while (s->next != OOO_SEGMENT_INVALID_INDEX
455 && position_lt (f, s->start, offset_pos, tail))
456 s = pool_elt_at_index (f->ooo_segments, s->next);
458 /* If we have a previous and we overlap it, use it as starting point */
459 prev = ooo_segment_get_prev (f, s);
461 && position_leq (f, offset_pos, ooo_segment_end_pos (f, prev), tail))
464 s_end_pos = ooo_segment_end_pos (f, s);
466 /* Since we have previous, offset start position cannot be smaller
467 * than prev->start. Check tail */
468 ASSERT (position_lt (f, s->start, offset_pos, tail));
472 s_index = s - f->ooo_segments;
473 s_end_pos = ooo_segment_end_pos (f, s);
475 /* No overlap, add before current segment */
476 if (position_lt (f, offset_end_pos, s->start, tail))
478 new_s = ooo_segment_new (f, offset_pos, length);
479 new_index = new_s - f->ooo_segments;
481 /* Pool might've moved, get segment again */
482 s = pool_elt_at_index (f->ooo_segments, s_index);
483 if (s->prev != OOO_SEGMENT_INVALID_INDEX)
485 new_s->prev = s->prev;
486 prev = pool_elt_at_index (f->ooo_segments, new_s->prev);
487 prev->next = new_index;
492 f->ooos_list_head = new_index;
495 new_s->next = s_index;
497 f->ooos_newest = new_index;
500 /* No overlap, add after current segment */
501 else if (position_gt (f, offset_pos, s_end_pos, tail))
503 new_s = ooo_segment_new (f, offset_pos, length);
504 new_index = new_s - f->ooo_segments;
506 /* Pool might've moved, get segment again */
507 s = pool_elt_at_index (f->ooo_segments, s_index);
509 /* Needs to be last */
510 ASSERT (s->next == OOO_SEGMENT_INVALID_INDEX);
512 new_s->prev = s_index;
514 f->ooos_newest = new_index;
524 if (position_lt (f, offset_pos, s->start, tail))
526 s->start = offset_pos;
527 s->length = position_diff (f, s_end_pos, s->start, tail);
528 f->ooos_newest = s - f->ooo_segments;
533 /* Overlapping tail */
534 if (position_gt (f, offset_end_pos, s_end_pos, tail))
536 s->length = position_diff (f, offset_end_pos, s->start, tail);
538 /* Remove the completely overlapped segments in the tail */
539 it = ooo_segment_next (f, s);
540 while (it && position_leq (f, ooo_segment_end_pos (f, it),
541 offset_end_pos, tail))
543 next = ooo_segment_next (f, it);
544 ooo_segment_del (f, it - f->ooo_segments);
548 /* If partial overlap with last, merge */
549 if (it && position_leq (f, it->start, offset_end_pos, tail))
551 s->length = position_diff (f, ooo_segment_end_pos (f, it),
553 ooo_segment_del (f, it - f->ooo_segments);
555 f->ooos_newest = s - f->ooo_segments;
560 * Removes segments that can now be enqueued because the fifo's tail has
561 * advanced. Returns the number of bytes added to tail.
564 ooo_segment_try_collect (svm_fifo_t * f, u32 n_bytes_enqueued, u32 * tail)
567 u32 index, bytes = 0;
570 s = pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
571 diff = ooo_segment_distance_to_tail (f, s->start, *tail);
573 ASSERT (diff != n_bytes_enqueued);
575 if (diff > n_bytes_enqueued)
578 /* If last tail update overlaps one/multiple ooo segments, remove them */
579 while (0 <= diff && diff < n_bytes_enqueued)
581 index = s - f->ooo_segments;
583 /* Segment end is beyond the tail. Advance tail and remove segment */
584 if (s->length > diff)
586 bytes = s->length - diff;
587 *tail = *tail + bytes;
588 ooo_segment_del (f, index);
592 /* If we have next go on */
593 if (s->next != OOO_SEGMENT_INVALID_INDEX)
595 s = pool_elt_at_index (f->ooo_segments, s->next);
596 diff = ooo_segment_distance_to_tail (f, s->start, *tail);
597 ooo_segment_del (f, index);
602 ooo_segment_del (f, index);
607 ASSERT (bytes <= f->nitems);
612 svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len)
615 u32 head, tail, head_idx;
618 ASSERT (len <= f->nitems);
620 f_load_head_tail_cons (f, &head, &tail);
622 head_idx = head % f->size;
623 head_idx -= c->start_byte;
624 n_chunk = c->length - head_idx;
626 clib_memcpy_fast (&c->data[head_idx], data, len);
629 clib_memcpy_fast (&c->data[head_idx], data, n_chunk);
630 clib_memcpy_fast (&c->next->data[0], data + n_chunk, len - n_chunk);
635 svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 len, const u8 * src)
637 u32 tail, head, free_count;
639 f_load_head_tail_prod (f, &head, &tail);
641 /* free space in fifo can only increase during enqueue: SPSC */
642 free_count = f_free_count (f, head, tail);
644 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
646 if (PREDICT_FALSE (free_count == 0))
647 return SVM_FIFO_FULL;
649 /* number of bytes we're going to copy */
650 len = clib_min (free_count, len);
652 svm_fifo_copy_to_chunk (f, f->tail_chunk, tail % f->size, src, len,
653 1 /* update tail */ );
656 svm_fifo_trace_add (f, head, n_total, 2);
658 /* collect out-of-order segments */
659 if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
660 len += ooo_segment_try_collect (f, len, &tail);
662 /* store-rel: producer owned index (paired with load-acq in consumer) */
663 clib_atomic_store_rel_n (&f->tail, tail);
669 * Enqueue a future segment.
671 * Two choices: either copies the entire segment, or copies nothing
672 * Returns 0 of the entire segment was copied
673 * Returns -1 if none of the segment was copied due to lack of space
676 svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
678 u32 tail, head, free_count;
680 f_load_head_tail_prod (f, &head, &tail);
682 /* free space in fifo can only increase during enqueue: SPSC */
683 free_count = f_free_count (f, head, tail);
685 /* will this request fit? */
686 if ((len + offset) > free_count)
689 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
691 svm_fifo_trace_add (f, offset, len, 1);
693 ooo_segment_add (f, offset, head, tail, len);
695 svm_fifo_copy_to_chunk (f, f->tail_chunk, (tail + offset) % f->size, src,
696 len, 0 /* update tail */ );
702 svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 len, u8 * dst)
704 u32 tail, head, cursize;
706 f_load_head_tail_cons (f, &head, &tail);
708 /* current size of fifo can only increase during dequeue: SPSC */
709 cursize = f_cursize (f, head, tail);
711 if (PREDICT_FALSE (cursize == 0))
712 return -2; /* nothing in the fifo */
714 len = clib_min (cursize, len);
715 ASSERT (cursize >= len);
717 svm_fifo_copy_from_chunk (f, f->head_chunk, head % f->size, dst, len,
718 1 /* update head */ );
721 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SIZE_UPDATE))
722 svm_fifo_try_size_update (f, head);
724 /* store-rel: consumer owned index (paired with load-acq in producer) */
725 clib_atomic_store_rel_n (&f->head, head);
731 svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 len, u8 * dst)
733 u32 tail, head, cursize;
735 f_load_head_tail_cons (f, &head, &tail);
737 /* current size of fifo can only increase during peek: SPSC */
738 cursize = f_cursize (f, head, tail);
740 if (PREDICT_FALSE (cursize < relative_offset))
741 return -2; /* nothing in the fifo */
743 len = clib_min (cursize - relative_offset, len);
745 svm_fifo_copy_from_chunk (f, f->head_chunk,
746 (head + relative_offset) % f->size, dst, len,
747 0 /* update head */ );
752 svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
754 u32 total_drop_bytes;
755 u32 tail, head, cursize;
757 f_load_head_tail_cons (f, &head, &tail);
759 /* number of bytes we're going to drop */
760 cursize = f_cursize (f, head, tail);
762 if (PREDICT_FALSE (cursize == 0))
763 return -2; /* nothing in the fifo */
765 svm_fifo_trace_add (f, tail, total_drop_bytes, 3);
767 /* number of bytes we're going to drop */
768 total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes;
771 head += total_drop_bytes;
773 ASSERT (cursize >= total_drop_bytes);
774 /* store-rel: consumer owned index (paired with load-acq in producer) */
775 clib_atomic_store_rel_n (&f->head, head);
777 return total_drop_bytes;
781 svm_fifo_dequeue_drop_all (svm_fifo_t * f)
783 /* consumer foreign index */
784 u32 tail = clib_atomic_load_acq_n (&f->tail);
785 /* store-rel: consumer owned index (paired with load-acq in producer) */
786 clib_atomic_store_rel_n (&f->head, tail);
790 svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs)
792 u32 cursize, head, tail, head_idx;
794 f_load_head_tail_cons (f, &head, &tail);
796 /* consumer function, cursize can only increase while we're working */
797 cursize = f_cursize (f, head, tail);
799 if (PREDICT_FALSE (cursize == 0))
800 return -2; /* nothing in the fifo */
802 head_idx = head % f->size;
806 fs[0].len = f->size - head_idx;
807 fs[0].data = f->head_chunk->data + head_idx;
808 fs[1].len = cursize - fs[0].len;
809 fs[1].data = f->head_chunk->data;
814 fs[0].data = f->head_chunk->data + head_idx;
822 svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs)
826 /* consumer owned index */
828 head_idx = head % f->size;
830 ASSERT (fs[0].data == f->head_chunk->data + head_idx);
831 head += fs[0].len + fs[1].len;
832 /* store-rel: consumer owned index (paired with load-acq in producer) */
833 clib_atomic_store_rel_n (&f->head, head);
836 /* Assumption: no prod and cons are accessing either dest or src fifo */
838 svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf)
841 clib_memcpy_fast (df->head_chunk->data, sf->head_chunk->data, sf->size);
843 f_load_head_tail_all_acq (sf, &head, &tail);
844 clib_atomic_store_rel_n (&df->head, head);
845 clib_atomic_store_rel_n (&df->tail, tail);
849 svm_fifo_number_ooo_segments (svm_fifo_t * f)
851 return pool_elts (f->ooo_segments);
855 svm_fifo_first_ooo_segment (svm_fifo_t * f)
857 return pool_elt_at_index (f->ooo_segments, f->ooos_list_head);
861 * Set fifo pointers to requested offset
864 svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
866 clib_atomic_store_rel_n (&f->head, pointer);
867 clib_atomic_store_rel_n (&f->tail, pointer);
871 svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber)
873 if (f->n_subscribers >= SVM_FIFO_MAX_EVT_SUBSCRIBERS)
875 f->subscribers[f->n_subscribers++] = subscriber;
879 svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber)
883 for (i = 0; i < f->n_subscribers; i++)
885 if (f->subscribers[i] != subscriber)
887 f->subscribers[i] = f->subscribers[f->n_subscribers - 1];
895 * fd.io coding-style-patch-verification: ON
898 * eval: (c-set-style "gnu")