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 */
33 u32 start; /**< Start of segment, normalized*/
34 u32 length; /**< Length of segment */
37 #define SVM_FIFO_TRACE (0)
38 #define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
39 #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0)
40 #define SVM_FIFO_INVALID_INDEX ((u32)~0)
41 #define SVM_FIFO_MAX_EVT_SUBSCRIBERS 7
43 typedef enum svm_fifo_tx_ntf_
45 SVM_FIFO_NO_TX_NOTIF = 0,
46 SVM_FIFO_WANT_TX_NOTIF = 1,
47 SVM_FIFO_WANT_TX_NOTIF_IF_FULL = 2,
55 } svm_fifo_trace_elem_t;
57 typedef struct svm_fifo_chunk_
59 u32 start_byte; /**< chunk start byte */
60 u32 length; /**< length of chunk in bytes */
61 struct svm_fifo_chunk_ *next; /**< pointer to next chunk in linked-lists */
62 u8 data[0]; /**< start of chunk data */
65 typedef enum svm_fifo_flag_
67 SVM_FIFO_F_MULTI_CHUNK = 1 << 0,
68 SVM_FIFO_F_GROW = 1 << 1,
69 SVM_FIFO_F_SHRINK = 1 << 2,
70 SVM_FIFO_F_COLLECT_CHUNKS = 1 << 3,
71 SVM_FIFO_F_LL_TRACKED = 1 << 4,
74 typedef struct _svm_fifo
76 CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
77 u32 size; /**< size of the fifo in bytes */
78 u32 nitems; /**< usable size (size-1) */
79 u8 flags; /**< fifo flags */
80 svm_fifo_chunk_t *start_chunk;/**< first chunk in fifo chunk list */
81 svm_fifo_chunk_t *end_chunk; /**< end chunk in fifo chunk list */
82 svm_fifo_chunk_t *new_chunks; /**< chunks yet to be added to list */
83 rb_tree_t chunk_lookup;
85 CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
86 volatile u32 has_event; /**< non-zero if deq event exists */
87 u32 master_session_index; /**< session layer session index */
88 u32 client_session_index; /**< app session index */
89 u8 master_thread_index; /**< session layer thread index */
90 u8 client_thread_index; /**< app worker index */
91 i8 refcnt; /**< reference count */
92 u32 segment_manager; /**< session layer segment manager index */
93 u32 segment_index; /**< segment index in segment manager */
94 struct _svm_fifo *next; /**< next in freelist/active chain */
95 struct _svm_fifo *prev; /**< prev in active chain */
96 u32 size_decrement; /**< bytes to remove from fifo */
98 CLIB_CACHE_LINE_ALIGN_MARK (consumer);
99 u32 head; /**< fifo head position/byte */
100 svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
101 svm_fifo_chunk_t *ooo_deq; /**< last chunk used for ooo dequeue */
102 volatile u32 want_tx_ntf; /**< producer wants nudge */
103 volatile u32 has_tx_ntf;
105 CLIB_CACHE_LINE_ALIGN_MARK (producer);
106 u32 tail; /**< fifo tail position/byte */
107 u32 ooos_list_head; /**< Head of out-of-order linked-list */
108 svm_fifo_chunk_t *tail_chunk; /**< tracks chunk where tail lands */
109 svm_fifo_chunk_t *ooo_enq; /**< last chunk used for ooo enqueue */
110 ooo_segment_t *ooo_segments; /**< Pool of ooo segments */
111 u32 ooos_newest; /**< Last segment to have been updated */
112 volatile u8 n_subscribers; /**< Number of subscribers for io events */
113 u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
116 svm_fifo_trace_elem_t *trace;
124 SVM_FIFO_EEMPTY = -3,
127 typedef struct svm_fifo_seg_
134 #define svm_fifo_trace_add(_f, _s, _l, _t) \
136 svm_fifo_trace_elem_t *trace_elt; \
137 vec_add2(_f->trace, trace_elt, 1); \
138 trace_elt->offset = _s; \
139 trace_elt->len = _l; \
140 trace_elt->action = _t; \
143 #define svm_fifo_trace_add(_f, _s, _l, _t)
146 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
147 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
150 * Load head and tail optimized for consumer
155 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
157 /* load-relaxed: consumer owned index */
159 /* load-acq: consumer foreign index (paired with store-rel in producer) */
160 *tail = clib_atomic_load_acq_n (&f->tail);
163 /** Load head and tail optimized for producer
168 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
170 /* load relaxed: producer owned index */
172 /* load-acq: producer foreign index (paired with store-rel in consumer) */
173 *head = clib_atomic_load_acq_n (&f->head);
177 * Load head and tail independent of producer/consumer role
182 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
184 /* load-acq : consumer foreign index (paired with store-rel) */
185 *tail = clib_atomic_load_acq_n (&f->tail);
186 /* load-acq : producer foriegn index (paired with store-rel) */
187 *head = clib_atomic_load_acq_n (&f->head);
191 * Distance to a from b, i.e., a - b in the fifo
196 f_distance_to (svm_fifo_t * f, u32 a, u32 b)
198 return ((f->size + a - b) % f->size);
202 * Distance from a to b, i.e., b - a in the fifo
207 f_distance_from (svm_fifo_t * f, u32 a, u32 b)
209 return ((f->size + b - a) % f->size);
213 * Fifo current size, i.e., number of bytes enqueued
218 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
220 return (head <= tail ? tail - head : f->size + tail - head);
224 * Fifo free bytes, i.e., number of free bytes
229 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
231 return (f->nitems - f_cursize (f, head, tail));
235 * Try to shrink fifo size.
239 void svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail);
242 * Create fifo of requested size
244 * Allocates fifo on current heap.
246 * @param size data size in bytes for fifo to be allocated. Will be
247 * rounded to the next highest power-of-two value.
248 * @return pointer to new fifo
250 svm_fifo_t *svm_fifo_create (u32 size);
254 * @param size size for fifo
256 void svm_fifo_init (svm_fifo_t * f, u32 size);
258 * Allocate a fifo chunk on heap
260 * If the chunk is allocated on a fifo segment, this should be called
261 * with the segment's heap pushed.
263 * @param size chunk size in bytes. Will be rounded to the next highest
265 * @return new chunk or 0 if alloc failed
267 svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
269 * Grow fifo size by adding chunk to chunk list
271 * If fifos are allocated on a segment, this should be called with
272 * the segment's heap pushed.
274 * @param f fifo to be extended
275 * @param c chunk or linked list of chunks to be added
277 void svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c);
279 * Request to reduce fifo size by amount of bytes
281 * Because the producer might be enqueuing data when this is called, the
282 * actual size update is only applied when producer tries to enqueue new
283 * data, unless @param try_shrink is set.
286 * @param len number of bytes to remove from fifo. The actual number
287 * of bytes to be removed will be less or equal to this
289 * @param try_shrink flg to indicate if it's safe to try to shrink fifo
290 * size. It should be set only if this is called by the
291 * producer of if the producer is not using the fifo
292 * @return actual length fifo size will be reduced by
294 int svm_fifo_reduce_size (svm_fifo_t * f, u32 len, u8 try_shrink);
296 * Removes chunks that are after fifo end byte
298 * Needs to be called with segment heap pushed.
302 svm_fifo_chunk_t *svm_fifo_collect_chunks (svm_fifo_t * f);
304 * Free fifo and associated state
308 void svm_fifo_free (svm_fifo_t * f);
310 * Cleanup fifo chunk lookup rb tree
312 * The rb tree is allocated in segment heap so this should be called
315 * @param f fifo to cleanup
317 void svm_fifo_free_chunk_lookup (svm_fifo_t * f);
319 * Cleanup fifo ooo data
321 * The ooo data is allocated in producer process memory. The fifo
322 * segment heap should not be pushed.
324 * @param f fifo to cleanup
326 void svm_fifo_free_ooo_data (svm_fifo_t * f);
328 * Init fifo head and tail
331 * @param head head value that will be matched to a chunk
332 * @param tail tail value that will be matched to a chunk
334 void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
338 * Clones single/default chunk fifo. It does not work for fifos with
341 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
343 * Enqueue data to fifo
345 * Data is enqueued and tail pointer is updated atomically. If the new data
346 * enqueued partly overlaps or "touches" an out-of-order segment, said segment
347 * is "consumed" and the number of bytes returned is appropriately updated.
350 * @param len length of data to copy
351 * @param src buffer from where to copy the data
352 * @return number of contiguous bytes that can be consumed or error
354 int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src);
356 * Enqueue data to fifo with offset
358 * Data is enqueued without updating tail pointer. Instead, an out-of-order
359 * list of segments is generated and maintained. Fifo takes care of coalescing
360 * contiguous or overlapping segments.
363 * @param offset offset at which to copy the data
364 * @param len len of data to copy
365 * @param src buffer from where to copy the data
366 * @return 0 if enqueue was successful, error otherwise
368 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
372 * Advance tail pointer
374 * Useful for moving tail pointer after external enqueue.
377 * @param len number of bytes to add to tail
379 void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
381 * Overwrite fifo head with new data
383 * This should be typically used by dgram transport protocols that need
384 * to update the dgram header after dequeueing a chunk of data. It assumes
385 * that the dgram header is at most spread over two chunks.
388 * @param src src of new data
389 * @param len length of new data
391 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
393 * Dequeue data from fifo
395 * Data is dequeued to consumer provided buffer and head is atomically
399 * @param len length of data to dequeue
400 * @param dst buffer to where to dequeue the data
401 * @return number of bytes dequeued or error
403 int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
405 * Peek data from fifo
407 * Data is copied from requested offset into provided dst buffer. Head is
411 * @param offset offset from which to copy the data
412 * @param len length of data to copy
413 * @param dst buffer to where to dequeue the data
414 * @return number of bytes peeked
416 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
418 * Dequeue and drop bytes from fifo
420 * Advances fifo head by requested amount of bytes.
423 * @param len number of bytes to drop
424 * @return number of bytes dropped
426 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
428 * Dequeue and drop all bytes from fifo
430 * Advances head to tail position.
434 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
435 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs);
436 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs);
438 * Add io events subscriber to list
441 * @param sub subscriber opaque index (typically app worker index)
443 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
445 * Remove io events subscriber form list
448 * @param sub subscriber index to be removed
450 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
452 * Number of out-of-order segments for fifo
455 * @return number of out of order segments
457 u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
459 * First out-of-order segment for fifo
462 * @return first out-of-order segment for fifo
464 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
465 format_function_t format_svm_fifo;
468 * Fifo max bytes to dequeue optimized for consumer
471 * @return max number of bytes that can be dequeued
474 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
477 f_load_head_tail_cons (f, &head, &tail);
478 return f_cursize (f, head, tail);
482 * Fifo max bytes to dequeue optimized for producer
485 * @return max number of bytes that can be dequeued
488 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
491 f_load_head_tail_prod (f, &head, &tail);
492 return f_cursize (f, head, tail);
496 * Fifo max bytes to dequeue
498 * Note: use producer or consumer specific functions for performance:
499 * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f)
500 * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f)
503 svm_fifo_max_dequeue (svm_fifo_t * f)
506 f_load_head_tail_all_acq (f, &head, &tail);
507 return f_cursize (f, head, tail);
511 * Check if fifo is full optimized for producer
514 * @return 1 if fifo is full 0 otherwise
517 svm_fifo_is_full_prod (svm_fifo_t * f)
519 return (svm_fifo_max_dequeue_prod (f) == f->nitems);
522 /* Check if fifo is full.
524 * Note: use producer or consumer specific functions for performance.
525 * @ref svm_fifo_is_full_prod (svm_fifo_t * f)
526 * add cons version if needed
529 svm_fifo_is_full (svm_fifo_t * f)
531 return (svm_fifo_max_dequeue (f) == f->nitems);
535 * Check if fifo is empty optimized for consumer
538 * @return 1 if fifo is empty 0 otherwise
541 svm_fifo_is_empty_cons (svm_fifo_t * f)
543 return (svm_fifo_max_dequeue_cons (f) == 0);
547 * Check if fifo is empty optimized for producer
550 * @return 1 if fifo is empty 0 otherwise
553 svm_fifo_is_empty_prod (svm_fifo_t * f)
555 return (svm_fifo_max_dequeue_prod (f) == 0);
559 * Check if fifo is empty
561 * Note: use producer or consumer specific functions for perfomance.
562 * @ref svm_fifo_is_empty_cons (svm_fifo_t * f)
563 * @ref svm_fifo_is_empty_prod (svm_fifo_t * f)
566 svm_fifo_is_empty (svm_fifo_t * f)
568 return (svm_fifo_max_dequeue (f) == 0);
572 * Check if fifo is wrapped
575 * @return 1 if 'normalized' head is ahead of tail
578 svm_fifo_is_wrapped (svm_fifo_t * f)
581 f_load_head_tail_all_acq (f, &head, &tail);
586 * Maximum number of bytes that can be enqueued into fifo
588 * Optimized for producer
591 * @return max number of bytes that can be enqueued into fifo
594 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
597 f_load_head_tail_prod (f, &head, &tail);
598 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
599 svm_fifo_try_shrink (f, head, tail);
600 return f_free_count (f, head, tail);
603 /* Maximum number of bytes that can be enqueued into fifo
605 * Note: use producer or consumer specific functions for performance.
606 * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f)
607 * add consumer specific version if needed.
610 svm_fifo_max_enqueue (svm_fifo_t * f)
613 f_load_head_tail_all_acq (f, &head, &tail);
614 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
615 svm_fifo_try_shrink (f, head, tail);
616 return f_free_count (f, head, tail);
620 * Max contiguous chunk of data that can be read
623 svm_fifo_max_read_chunk (svm_fifo_t * f)
626 f_load_head_tail_cons (f, &head, &tail);
627 return tail >= head ? (tail - head) : (f->size - head);
631 * Max contiguous chunk of data that can be written
634 svm_fifo_max_write_chunk (svm_fifo_t * f)
637 f_load_head_tail_prod (f, &head, &tail);
638 return tail > head ? f->size - tail : f_free_count (f, head, tail);
642 svm_fifo_head (svm_fifo_t * f)
644 /* load-relaxed: consumer owned index */
645 return (f->head_chunk->data + (f->head - f->head_chunk->start_byte));
649 svm_fifo_tail (svm_fifo_t * f)
651 /* load-relaxed: producer owned index */
652 return (f->tail_chunk->data + (f->tail - f->tail_chunk->start_byte));
656 svm_fifo_n_subscribers (svm_fifo_t * f)
658 return f->n_subscribers;
662 * Check if fifo has out-of-order data
665 * @return 1 if fifo has ooo data, 0 otherwise
668 svm_fifo_has_ooo_data (svm_fifo_t * f)
670 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
673 static inline ooo_segment_t *
674 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
676 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
678 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
682 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
684 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
688 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
691 /* load-relaxed: producer owned index */
694 return f_distance_to (f, s->start, tail);
698 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
704 * Check if fifo has io event
707 * @return 1 if fifo has event, 0 otherwise
710 svm_fifo_has_event (svm_fifo_t * f)
716 * Set fifo event flag.
718 * Forces release semantics.
721 * @return 1 if flag was not set, 0 otherwise
724 svm_fifo_set_event (svm_fifo_t * f)
726 return !clib_atomic_swap_rel_n (&f->has_event, 1);
730 * Unset fifo event flag.
732 * Forces acquire semantics
737 svm_fifo_unset_event (svm_fifo_t * f)
739 clib_atomic_swap_acq_n (&f->has_event, 0);
743 * Set specific want tx notification flag
745 * For list of flags see @ref svm_fifo_tx_ntf_t
748 * @param ntf_type type of notification requested
751 svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
753 f->want_tx_ntf |= ntf_type;
757 * Clear specific want tx notification flag
759 * For list of flags see @ref svm_fifo_tx_ntf_t
762 * @param ntf_type type of notification to be cleared
765 svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
767 f->want_tx_ntf &= ~ntf_type;
771 * Clear the want tx notification flag and set has tx notification
773 * Should be used after enqueuing a tx event. This clears the
774 * SVM_FIFO_WANT_TX_NOTIF flag but it does not clear
775 * SVM_FIFO_WANT_TX_NOTIF_IF_FULL. If the latter was set, has_tx_ntf is
776 * set to avoid enqueueing tx events for for all dequeue operations until
777 * it is manually cleared.
782 svm_fifo_clear_tx_ntf (svm_fifo_t * f)
784 /* Set the flag if want_tx_notif_if_full was the only ntf requested */
785 f->has_tx_ntf = f->want_tx_ntf == SVM_FIFO_WANT_TX_NOTIF_IF_FULL;
786 svm_fifo_del_want_tx_ntf (f, SVM_FIFO_WANT_TX_NOTIF);
790 * Clear has tx notification flag
792 * The fifo generates only one event per SVM_FIFO_WANT_TX_NOTIF_IF_FULL
793 * request and sets has_tx_ntf. To received new events the flag must be
794 * cleared using this function.
799 svm_fifo_reset_tx_ntf (svm_fifo_t * f)
805 * Check if fifo needs tx notification
807 * Determines based on tx notification request flags and state of the fifo if
808 * a tx io event should be generated.
811 * @param n_last_deq number of bytes last dequeued
812 * @return 1 if tx io event should be generated, 0 otherwise
815 svm_fifo_needs_tx_ntf (svm_fifo_t * f, u32 n_last_deq)
817 u8 want_ntf = f->want_tx_ntf;
819 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_TX_NOTIF))
821 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF)
823 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF_IF_FULL)
825 u32 max_deq = svm_fifo_max_dequeue_cons (f);
826 u32 nitems = f->nitems;
827 if (!f->has_tx_ntf && max_deq < nitems
828 && max_deq + n_last_deq >= nitems)
836 #endif /* __included_ssvm_fifo_h__ */
839 * fd.io coding-style-patch-verification: ON
842 * eval: (c-set-style "gnu")