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/pool.h>
25 #include <vppinfra/format.h>
26 #include <vppinfra/rbtree.h>
28 /** Out-of-order segment */
31 u32 next; /**< Next linked-list element pool index */
32 u32 prev; /**< Previous linked-list element pool index */
34 u32 start; /**< Start of segment, normalized*/
35 u32 length; /**< Length of segment */
38 #define SVM_FIFO_TRACE (0)
39 #define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
40 #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0)
41 #define SVM_FIFO_INVALID_INDEX ((u32)~0)
42 #define SVM_FIFO_MAX_EVT_SUBSCRIBERS 7
46 SVM_FIFO_NO_TX_NOTIF = 0,
47 SVM_FIFO_WANT_TX_NOTIF = 1,
48 SVM_FIFO_WANT_TX_NOTIF_IF_FULL = 2,
56 } svm_fifo_trace_elem_t;
58 typedef struct svm_fifo_chunk_
62 struct svm_fifo_chunk_ *next;
66 typedef enum svm_fifo_flag_
68 SVM_FIFO_F_SIZE_UPDATE = 1 << 0,
69 SVM_FIFO_F_MULTI_CHUNK = 1 << 1,
70 SVM_FIFO_F_LL_TRACKED = 1 << 2,
73 typedef struct _svm_fifo
75 CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
76 u32 size; /**< size of the fifo */
77 u32 nitems; /**< usable size(size-1) */
78 u8 flags; /**< fifo flags */
79 svm_fifo_chunk_t *start_chunk;/**< first chunk in fifo chunk list */
80 svm_fifo_chunk_t *end_chunk; /**< end chunk in fifo chunk list */
81 svm_fifo_chunk_t *new_chunks; /**< chunks yet to be added to list */
82 rb_tree_t chunk_lookup;
84 CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
85 volatile u32 has_event; /**< non-zero if deq event exists */
87 u32 master_session_index;
88 u32 client_session_index;
89 u8 master_thread_index;
90 u8 client_thread_index;
93 u32 ct_session_index; /**< Local session index for vpp */
94 u32 freelist_index; /**< aka log2(allocated_size) - const. */
95 i8 refcnt; /**< reference count */
96 struct _svm_fifo *next; /**< next in freelist/active chain */
97 struct _svm_fifo *prev; /**< prev in active chain */
99 CLIB_CACHE_LINE_ALIGN_MARK (consumer);
100 u32 head; /**< fifo head position/byte */
101 svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
102 svm_fifo_chunk_t *ooo_deq; /**< last chunk used for ooo dequeue */
103 volatile u32 want_tx_ntf; /**< producer wants nudge */
104 volatile u32 has_tx_ntf;
106 CLIB_CACHE_LINE_ALIGN_MARK (producer);
107 u32 tail; /**< fifo tail position/byte */
108 u32 ooos_list_head; /**< Head of out-of-order linked-list */
109 svm_fifo_chunk_t *tail_chunk; /**< tracks chunk where tail lands */
110 svm_fifo_chunk_t *ooo_enq; /**< last chunk used for ooo enqueue */
111 ooo_segment_t *ooo_segments; /**< Pool of ooo segments */
112 u32 ooos_newest; /**< Last segment to have been updated */
113 volatile u8 n_subscribers;
114 u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
117 svm_fifo_trace_elem_t *trace;
120 svm_fifo_chunk_t default_chunk;
128 typedef struct svm_fifo_seg_
135 #define svm_fifo_trace_add(_f, _s, _l, _t) \
137 svm_fifo_trace_elem_t *trace_elt; \
138 vec_add2(_f->trace, trace_elt, 1); \
139 trace_elt->offset = _s; \
140 trace_elt->len = _l; \
141 trace_elt->action = _t; \
144 #define svm_fifo_trace_add(_f, _s, _l, _t)
147 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
148 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
150 /* internal function */
152 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
154 /* load-relaxed: consumer owned index */
156 /* load-acq: consumer foreign index (paired with store-rel in producer) */
157 *tail = clib_atomic_load_acq_n (&f->tail);
160 /* internal function */
162 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
164 /* load relaxed: producer owned index */
166 /* load-acq: producer foreign index (paired with store-rel in consumer) */
167 *head = clib_atomic_load_acq_n (&f->head);
170 /* producer consumer role independent */
171 /* internal function */
173 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
175 /* load-acq : consumer foreign index (paired with store-rel) */
176 *tail = clib_atomic_load_acq_n (&f->tail);
177 /* load-acq : producer foriegn index (paired with store-rel) */
178 *head = clib_atomic_load_acq_n (&f->head);
181 /* internal function */
183 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
185 return (f->nitems + head - tail);
188 /* internal function */
190 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
192 return (f->nitems - f_free_count (f, head, tail));
195 /* used by consumer */
197 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
200 f_load_head_tail_cons (f, &head, &tail);
201 return f_cursize (f, head, tail);
204 /* used by producer*/
206 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
209 f_load_head_tail_prod (f, &head, &tail);
210 return f_cursize (f, head, tail);
213 /* use producer or consumer specific functions for perfomance.
214 * svm_fifo_max_dequeue_cons (svm_fifo_t *f)
215 * svm_fifo_max_dequeue_prod (svm_fifo_t *f)
218 svm_fifo_max_dequeue (svm_fifo_t * f)
221 f_load_head_tail_all_acq (f, &head, &tail);
222 return f_cursize (f, head, tail);
225 /* used by producer */
227 svm_fifo_is_full_prod (svm_fifo_t * f)
229 return (svm_fifo_max_dequeue_prod (f) == f->nitems);
232 /* use producer or consumer specific functions for perfomance.
233 * svm_fifo_is_full_prod (svm_fifo_t * f)
234 * add cons version if needed
237 svm_fifo_is_full (svm_fifo_t * f)
239 return (svm_fifo_max_dequeue (f) == f->nitems);
242 /* used by consumer */
244 svm_fifo_is_empty_cons (svm_fifo_t * f)
246 return (svm_fifo_max_dequeue_cons (f) == 0);
249 /* used by producer */
251 svm_fifo_is_empty_prod (svm_fifo_t * f)
253 return (svm_fifo_max_dequeue_prod (f) == 0);
256 /* use producer or consumer specific functions for perfomance.
257 * svm_fifo_is_empty_cons (svm_fifo_t * f)
258 * svm_fifo_is_empty_prod (svm_fifo_t * f)
261 svm_fifo_is_empty (svm_fifo_t * f)
263 return (svm_fifo_max_dequeue (f) == 0);
267 svm_fifo_is_wrapped (svm_fifo_t * f)
270 f_load_head_tail_all_acq (f, &head, &tail);
271 return head % f->size > tail % f->size;
274 /* used by producer*/
276 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
279 f_load_head_tail_prod (f, &head, &tail);
280 return f_free_count (f, head, tail);
283 /* use producer or consumer specfic functions for perfomance.
284 * svm_fifo_max_enqueue_prod (svm_fifo_t *f)
285 * add consumer specific version if needed.
288 svm_fifo_max_enqueue (svm_fifo_t * f)
291 f_load_head_tail_all_acq (f, &head, &tail);
292 return f_free_count (f, head, tail);
296 svm_fifo_has_event (svm_fifo_t * f)
302 svm_fifo_has_ooo_data (svm_fifo_t * f)
304 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
308 * Sets fifo event flag.
310 * Also acts as a release ordering.
312 * @return 1 if flag was not set.
315 svm_fifo_set_event (svm_fifo_t * f)
317 /* return __sync_lock_test_and_set (&f->has_event, 1) == 0;
318 return __sync_bool_compare_and_swap (&f->has_event, 0, 1); */
319 return !clib_atomic_swap_rel_n (&f->has_event, 1);
323 * Unsets fifo event flag.
325 * Also acts as an acquire barrier.
328 svm_fifo_unset_event (svm_fifo_t * f)
330 clib_atomic_swap_acq_n (&f->has_event, 0);
333 svm_fifo_t *svm_fifo_create (u32 data_size_in_bytes);
334 void svm_fifo_init (svm_fifo_t * f, u32 size);
336 * Grow fifo size by adding chunk to chunk list
338 * If fifos are allocated on a segment, this should be called with
339 * the segment's heap pushed.
341 * @param f fifo to be extended
342 * @param c chunk or linked list of chunks to be added
344 void svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c);
345 void svm_fifo_free (svm_fifo_t * f);
347 int svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
348 const u8 * copy_from_here);
349 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset,
350 u32 required_bytes, u8 * copy_from_here);
351 int svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here);
353 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 max_bytes, u8 * copy_here);
354 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes);
355 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
356 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs);
357 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs);
358 void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
359 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
360 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len);
361 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 subscriber);
362 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
363 format_function_t format_svm_fifo;
366 * Max contiguous chunk of data that can be read
369 svm_fifo_max_read_chunk (svm_fifo_t * f)
372 u32 head_idx, tail_idx;
373 f_load_head_tail_cons (f, &head, &tail);
374 head_idx = head % f->size;
375 tail_idx = tail % f->size;
376 return tail_idx > head_idx ? (tail_idx - head_idx) : (f->size - head_idx);
380 * Max contiguous chunk of data that can be written
383 svm_fifo_max_write_chunk (svm_fifo_t * f)
386 u32 head_idx, tail_idx;
387 f_load_head_tail_prod (f, &head, &tail);
388 head_idx = head % f->size;
389 tail_idx = tail % f->size;
390 return tail_idx >= head_idx ? (f->size - tail_idx) : (head_idx - tail_idx);
394 * Advance tail pointer
396 * Useful for moving tail pointer after external enqueue.
399 svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 bytes)
401 ASSERT (bytes <= svm_fifo_max_enqueue_prod (f));
402 /* load-relaxed: producer owned index */
405 /* store-rel: producer owned index (paired with load-acq in consumer) */
406 clib_atomic_store_rel_n (&f->tail, tail);
410 svm_fifo_head (svm_fifo_t * f)
412 /* load-relaxed: consumer owned index */
413 return (f->head_chunk->data
414 + ((f->head % f->size) - f->head_chunk->start_byte));
418 svm_fifo_tail (svm_fifo_t * f)
420 /* load-relaxed: producer owned index */
421 return (f->tail_chunk->data
422 + ((f->tail % f->size) - f->tail_chunk->start_byte));
426 svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
428 f->want_tx_ntf |= ntf_type;
432 svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
434 f->want_tx_ntf &= ~ntf_type;
438 svm_fifo_clear_tx_ntf (svm_fifo_t * f)
440 /* Set the flag if want_tx_notif_if_full was the only ntf requested */
441 f->has_tx_ntf = f->want_tx_ntf == SVM_FIFO_WANT_TX_NOTIF_IF_FULL;
442 svm_fifo_del_want_tx_ntf (f, SVM_FIFO_WANT_TX_NOTIF);
446 svm_fifo_reset_tx_ntf (svm_fifo_t * f)
452 svm_fifo_needs_tx_ntf (svm_fifo_t * f, u32 n_last_deq)
454 u8 want_ntf = f->want_tx_ntf;
456 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_TX_NOTIF))
458 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF)
460 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF_IF_FULL)
462 u32 max_deq = svm_fifo_max_dequeue_cons (f);
463 u32 nitems = f->nitems;
464 if (!f->has_tx_ntf && max_deq < nitems
465 && max_deq + n_last_deq >= nitems)
474 svm_fifo_n_subscribers (svm_fifo_t * f)
476 return f->n_subscribers;
479 u32 svm_fifo_number_ooo_segments (svm_fifo_t * f);
480 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
482 always_inline ooo_segment_t *
483 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
485 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
487 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
491 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
493 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
497 ooo_segment_distance_from_tail (svm_fifo_t * f, u32 pos, u32 tail)
499 return ((pos - tail) % f->size);
503 ooo_segment_distance_to_tail (svm_fifo_t * f, u32 pos, u32 tail)
505 return ((tail - pos) % f->size);
509 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
512 /* load-relaxed: producer owned index */
515 return ooo_segment_distance_from_tail (f, s->start, tail);
519 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
524 always_inline ooo_segment_t *
525 ooo_segment_get_prev (svm_fifo_t * f, ooo_segment_t * s)
527 if (s->prev == OOO_SEGMENT_INVALID_INDEX)
529 return pool_elt_at_index (f->ooo_segments, s->prev);
532 always_inline ooo_segment_t *
533 ooo_segment_next (svm_fifo_t * f, ooo_segment_t * s)
535 if (s->next == OOO_SEGMENT_INVALID_INDEX)
537 return pool_elt_at_index (f->ooo_segments, s->next);
540 #endif /* __included_ssvm_fifo_h__ */
543 * fd.io coding-style-patch-verification: ON
546 * eval: (c-set-style "gnu")