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.
19 #ifndef __included_ssvm_fifo_h__
20 #define __included_ssvm_fifo_h__
22 #include <vppinfra/clib.h>
23 #include <vppinfra/vec.h>
24 #include <vppinfra/mheap.h>
25 #include <vppinfra/heap.h>
26 #include <vppinfra/pool.h>
27 #include <vppinfra/format.h>
30 /** Out-of-order segment */
33 u32 next; /**< Next linked-list element pool index */
34 u32 prev; /**< Previous linked-list element pool index */
36 u32 start; /**< Start of segment, normalized*/
37 u32 length; /**< Length of segment */
40 format_function_t format_ooo_segment;
41 format_function_t format_ooo_list;
43 #define SVM_FIFO_TRACE (0)
44 #define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
45 #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0)
46 #define SVM_FIFO_INVALID_INDEX ((u32)~0)
47 #define SVM_FIFO_MAX_EVT_SUBSCRIBERS 8
51 SVM_FIFO_NO_TX_NOTIF = 0,
52 SVM_FIFO_WANT_TX_NOTIF = 1,
53 SVM_FIFO_WANT_TX_NOTIF_IF_FULL = 2,
61 } svm_fifo_trace_elem_t;
63 typedef struct svm_fifo_chunk_
67 struct svm_fifo_chunk_ *next;
71 typedef struct _svm_fifo
73 CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
74 u32 size; /**< size of the fifo */
75 u32 nitems; /**< usable size(size-1) */
76 struct _svm_fifo *next; /**< next in freelist/active chain */
77 struct _svm_fifo *prev; /**< prev in active chain */
79 CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
80 volatile u32 has_event; /**< non-zero if deq event exists */
82 u32 master_session_index;
83 u32 client_session_index;
84 u8 master_thread_index;
85 u8 client_thread_index;
88 u32 ct_session_index; /**< Local session index for vpp */
89 u32 freelist_index; /**< aka log2(allocated_size) - const. */
90 i8 refcnt; /**< reference count */
92 CLIB_CACHE_LINE_ALIGN_MARK (consumer);
94 svm_fifo_chunk_t *head_chunk;
95 volatile u32 want_tx_ntf; /**< producer wants nudge */
96 volatile u32 has_tx_ntf;
98 CLIB_CACHE_LINE_ALIGN_MARK (producer);
100 svm_fifo_chunk_t *tail_chunk;
102 ooo_segment_t *ooo_segments; /**< Pool of ooo segments */
103 u32 ooos_list_head; /**< Head of out-of-order linked-list */
104 u32 ooos_newest; /**< Last segment to have been updated */
105 volatile u8 n_subscribers;
106 u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
109 svm_fifo_trace_elem_t *trace;
112 svm_fifo_chunk_t default_chunk;
120 typedef struct svm_fifo_segment_
124 } svm_fifo_segment_t;
127 #define svm_fifo_trace_add(_f, _s, _l, _t) \
129 svm_fifo_trace_elem_t *trace_elt; \
130 vec_add2(_f->trace, trace_elt, 1); \
131 trace_elt->offset = _s; \
132 trace_elt->len = _l; \
133 trace_elt->action = _t; \
136 #define svm_fifo_trace_add(_f, _s, _l, _t)
139 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
140 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
142 /* internal function */
144 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
146 /* load-relaxed: consumer owned index */
148 /* load-acq: consumer foreign index (paired with store-rel in producer) */
149 *tail = clib_atomic_load_acq_n (&f->tail);
152 /* internal function */
154 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
156 /* load relaxed: producer owned index */
158 /* load-acq: producer foreign index (paired with store-rel in consumer) */
159 *head = clib_atomic_load_acq_n (&f->head);
162 /* producer consumer role independent */
163 /* internal function */
165 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
167 /* load-acq : consumer foreign index (paired with store-rel) */
168 *tail = clib_atomic_load_acq_n (&f->tail);
169 /* load-acq : producer foriegn index (paired with store-rel) */
170 *head = clib_atomic_load_acq_n (&f->head);
173 /* internal function */
175 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
177 return (f->nitems + head - tail);
180 /* internal function */
182 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
184 return (f->nitems - f_free_count (f, head, tail));
187 /* used by consumer */
189 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
192 f_load_head_tail_cons (f, &head, &tail);
193 return f_cursize (f, head, tail);
196 /* used by producer*/
198 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
201 f_load_head_tail_prod (f, &head, &tail);
202 return f_cursize (f, head, tail);
205 /* use producer or consumer specific functions for perfomance.
206 * svm_fifo_max_dequeue_cons (svm_fifo_t *f)
207 * svm_fifo_max_dequeue_prod (svm_fifo_t *f)
210 svm_fifo_max_dequeue (svm_fifo_t * f)
213 f_load_head_tail_all_acq (f, &head, &tail);
214 return f_cursize (f, head, tail);
217 /* used by producer */
219 svm_fifo_is_full_prod (svm_fifo_t * f)
221 return (svm_fifo_max_dequeue_prod (f) == f->nitems);
224 /* use producer or consumer specific functions for perfomance.
225 * svm_fifo_is_full_prod (svm_fifo_t * f)
226 * add cons version if needed
229 svm_fifo_is_full (svm_fifo_t * f)
231 return (svm_fifo_max_dequeue (f) == f->nitems);
234 /* used by consumer */
236 svm_fifo_is_empty_cons (svm_fifo_t * f)
238 return (svm_fifo_max_dequeue_cons (f) == 0);
241 /* used by producer */
243 svm_fifo_is_empty_prod (svm_fifo_t * f)
245 return (svm_fifo_max_dequeue_prod (f) == 0);
248 /* use producer or consumer specific functions for perfomance.
249 * svm_fifo_is_empty_cons (svm_fifo_t * f)
250 * svm_fifo_is_empty_prod (svm_fifo_t * f)
253 svm_fifo_is_empty (svm_fifo_t * f)
255 return (svm_fifo_max_dequeue (f) == 0);
258 /* used by producer*/
260 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
263 f_load_head_tail_prod (f, &head, &tail);
264 return f_free_count (f, head, tail);
267 /* use producer or consumer specfic functions for perfomance.
268 * svm_fifo_max_enqueue_prod (svm_fifo_t *f)
269 * add consumer specific version if needed.
272 svm_fifo_max_enqueue (svm_fifo_t * f)
275 f_load_head_tail_all_acq (f, &head, &tail);
276 return f_free_count (f, head, tail);
280 svm_fifo_has_event (svm_fifo_t * f)
286 svm_fifo_has_ooo_data (svm_fifo_t * f)
288 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
292 * Sets fifo event flag.
294 * Also acts as a release ordering.
296 * @return 1 if flag was not set.
299 svm_fifo_set_event (svm_fifo_t * f)
301 /* return __sync_lock_test_and_set (&f->has_event, 1) == 0;
302 return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */
303 return !clib_atomic_swap_rel_n (&f->has_event, 1);
307 * Unsets fifo event flag.
309 * Also acts as an acquire barrier.
312 svm_fifo_unset_event (svm_fifo_t * f)
314 clib_atomic_swap_acq_n (&f->has_event, 0);
317 svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
318 void svm_fifo_init (svm_fifo_t * f, u32 size);
319 void svm_fifo_free (svm_fifo_t * f);
321 int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
322 const u8 * copy_from_here);
323 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
324 u32 required_bytes, u8 * copy_from_here);
325 int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
327 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
328 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
329 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
330 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs);
331 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs);
332 void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer);
333 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
334 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
335 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber);
336 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
337 format_function_t format_svm_fifo;
340 * Max contiguous chunk of data that can be read
343 svm_fifo_max_read_chunk (svm_fifo_t * f)
346 u32 head_idx, tail_idx;
347 f_load_head_tail_cons (f, &head, &tail);
348 head_idx = head % f->size;
349 tail_idx = tail % f->size;
350 return tail_idx > head_idx ? (tail_idx - head_idx) : (f->size - head_idx);
354 * Max contiguous chunk of data that can be written
357 svm_fifo_max_write_chunk (svm_fifo_t * f)
360 u32 head_idx, tail_idx;
361 f_load_head_tail_prod (f, &head, &tail);
362 head_idx = head % f->size;
363 tail_idx = tail % f->size;
364 return tail_idx >= head_idx ? (f->size - tail_idx) : (head_idx - tail_idx);
368 * Advance tail pointer
370 * Useful for moving tail pointer after external enqueue.
373 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes)
375 ASSERT (bytes <= svm_fifo_max_enqueue_prod (f));
376 /* load-relaxed: producer owned index */
379 /* store-rel: producer owned index (paired with load-acq in consumer) */
380 clib_atomic_store_rel_n (&f->tail, tail);
384 svm_fifo_head (svm_fifo_t * f)
386 /* load-relaxed: consumer owned index */
387 return (f->head_chunk->data
388 + ((f->head % f->size) - f->head_chunk->start_byte));
392 svm_fifo_tail (svm_fifo_t * f)
394 /* load-relaxed: producer owned index */
395 return (f->tail_chunk->data
396 + ((f->tail % f->size) - f->tail_chunk->start_byte));
400 svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
402 f->want_tx_ntf |= ntf_type;
406 svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
408 f->want_tx_ntf &= ~ntf_type;
412 svm_fifo_clear_tx_ntf (svm_fifo_t * f)
414 /* Set the flag if want_tx_notif_if_full was the only ntf requested */
415 f->has_tx_ntf = f->want_tx_ntf == SVM_FIFO_WANT_TX_NOTIF_IF_FULL;
416 svm_fifo_del_want_tx_ntf (f, SVM_FIFO_WANT_TX_NOTIF);
420 svm_fifo_reset_tx_ntf (svm_fifo_t * f)
426 svm_fifo_needs_tx_ntf (svm_fifo_t * f, u32 n_last_deq)
428 u8 want_ntf = f->want_tx_ntf;
430 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_TX_NOTIF))
432 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF)
434 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF_IF_FULL)
436 u32 max_deq = svm_fifo_max_dequeue_cons (f);
437 u32 nitems = f->nitems;
438 if (!f->has_tx_ntf && max_deq < nitems
439 && max_deq + n_last_deq >= nitems)
448 svm_fifo_n_subscribers (svm_fifo_t * f)
450 return f->n_subscribers;
453 u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
454 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
456 always_inline ooo_segment_t *
457 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
459 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
461 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
465 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
467 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
471 ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos, u32 tail)
473 return ((pos - tail) % f->size);
477 ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos, u32 tail)
479 return ((tail - pos) % f->size);
483 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
486 /* load-relaxed: producer owned index */
489 return ooo_segment_distance_from_tail (f, s->start, tail);
493 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
498 always_inline ooo_segment_t *
499 ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s)
501 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
503 return pool_elt_at_index (f->ooo_segments, s->prev);
506 always_inline ooo_segment_t *
507 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
509 if (s->next == OOO_SEGMENT_INVALID_INDEX)
511 return pool_elt_at_index (f->ooo_segments, s->next);
514 #endif /* __included_ssvm_fifo_h__ */
517 * fd.io coding-style-patch-verification: ON
520 * eval: (c-set-style "gnu")