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_deq_ntf_
45 SVM_FIFO_NO_DEQ_NOTIF = 0, /**< No notification requested */
46 SVM_FIFO_WANT_DEQ_NOTIF = 1, /**< Notify on dequeue */
47 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL = 2, /**< Notify on transition from full */
48 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY = 4, /**< Notify on transition to empty */
56 } svm_fifo_trace_elem_t;
58 typedef struct svm_fifo_chunk_
60 u32 start_byte; /**< chunk start byte */
61 u32 length; /**< length of chunk in bytes */
62 struct svm_fifo_chunk_ *next; /**< pointer to next chunk in linked-lists */
63 rb_node_index_t rb_index; /**< node index if chunk in rbtree */
64 u8 data[0]; /**< start of chunk data */
67 typedef enum svm_fifo_flag_
69 SVM_FIFO_F_MULTI_CHUNK = 1 << 0,
70 SVM_FIFO_F_GROW = 1 << 1,
71 SVM_FIFO_F_SHRINK = 1 << 2,
72 SVM_FIFO_F_COLLECT_CHUNKS = 1 << 3,
73 SVM_FIFO_F_LL_TRACKED = 1 << 4,
74 SVM_FIFO_F_SINGLE_THREAD_OWNED = 1 << 5,
77 typedef struct _svm_fifo
79 CLIB_CACHE_LINE_ALIGN_MARK (shared_first);
80 u32 size; /**< size of the fifo in bytes */
81 u32 nitems; /**< usable size (size-1) */
82 svm_fifo_chunk_t *start_chunk;/**< first chunk in fifo chunk list */
83 svm_fifo_chunk_t *end_chunk; /**< end chunk in fifo chunk list */
84 rb_tree_t ooo_enq_lookup; /**< rbtree for ooo enq chunk lookup */
85 rb_tree_t ooo_deq_lookup; /**< rbtree for ooo deq chunk lookup */
86 u8 flags; /**< fifo flags */
87 u8 slice_index; /**< segment slice for fifo */
89 CLIB_CACHE_LINE_ALIGN_MARK (shared_second);
90 volatile u32 has_event; /**< non-zero if deq event exists */
91 u32 master_session_index; /**< session layer session index */
92 u32 client_session_index; /**< app session index */
93 u8 master_thread_index; /**< session layer thread index */
94 u8 client_thread_index; /**< app worker index */
95 i8 refcnt; /**< reference count */
96 u32 segment_manager; /**< session layer segment manager index */
97 u32 segment_index; /**< segment index in segment manager */
98 struct _svm_fifo *next; /**< next in freelist/active chain */
99 struct _svm_fifo *prev; /**< prev in active chain */
100 svm_fifo_chunk_t *new_chunks; /**< chunks yet to be added to list */
101 u32 size_decrement; /**< bytes to remove from fifo */
103 CLIB_CACHE_LINE_ALIGN_MARK (consumer);
104 u32 head; /**< fifo head position/byte */
105 svm_fifo_chunk_t *head_chunk; /**< tracks chunk where head lands */
106 svm_fifo_chunk_t *ooo_deq; /**< last chunk used for ooo dequeue */
107 volatile u32 want_deq_ntf; /**< producer wants nudge */
108 volatile u32 has_deq_ntf;
110 CLIB_CACHE_LINE_ALIGN_MARK (producer);
111 u32 tail; /**< fifo tail position/byte */
112 u32 ooos_list_head; /**< Head of out-of-order linked-list */
113 svm_fifo_chunk_t *tail_chunk; /**< tracks chunk where tail lands */
114 svm_fifo_chunk_t *ooo_enq; /**< last chunk used for ooo enqueue */
115 ooo_segment_t *ooo_segments; /**< Pool of ooo segments */
116 u32 ooos_newest; /**< Last segment to have been updated */
117 volatile u8 n_subscribers; /**< Number of subscribers for io events */
118 u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
121 svm_fifo_trace_elem_t *trace;
129 SVM_FIFO_EEMPTY = -3,
132 typedef struct svm_fifo_seg_
139 #define svm_fifo_trace_add(_f, _s, _l, _t) \
141 svm_fifo_trace_elem_t *trace_elt; \
142 vec_add2(_f->trace, trace_elt, 1); \
143 trace_elt->offset = _s; \
144 trace_elt->len = _l; \
145 trace_elt->action = _t; \
148 #define svm_fifo_trace_add(_f, _s, _l, _t)
151 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
152 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
155 * Load head and tail optimized for consumer
160 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
162 /* load-relaxed: consumer owned index */
164 /* load-acq: consumer foreign index (paired with store-rel in producer) */
165 *tail = clib_atomic_load_acq_n (&f->tail);
168 /** Load head and tail optimized for producer
173 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
175 /* load relaxed: producer owned index */
177 /* load-acq: producer foreign index (paired with store-rel in consumer) */
178 *head = clib_atomic_load_acq_n (&f->head);
182 * Load head and tail independent of producer/consumer role
187 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
189 /* load-acq : consumer foreign index (paired with store-rel) */
190 *tail = clib_atomic_load_acq_n (&f->tail);
191 /* load-acq : producer foriegn index (paired with store-rel) */
192 *head = clib_atomic_load_acq_n (&f->head);
196 * Distance to a from b, i.e., a - b in the fifo
201 f_distance_to (svm_fifo_t * f, u32 a, u32 b)
203 return ((f->size + a - b) % f->size);
207 * Distance from a to b, i.e., b - a in the fifo
212 f_distance_from (svm_fifo_t * f, u32 a, u32 b)
214 return ((f->size + b - a) % f->size);
218 * Fifo current size, i.e., number of bytes enqueued
223 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
225 return (head <= tail ? tail - head : f->size + tail - head);
229 * Fifo free bytes, i.e., number of free bytes
234 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
236 return (f->nitems - f_cursize (f, head, tail));
240 * Try to shrink fifo size.
244 void svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail);
247 * Create fifo of requested size
249 * Allocates fifo on current heap.
251 * @param size data size in bytes for fifo to be allocated. Will be
252 * rounded to the next highest power-of-two value.
253 * @return pointer to new fifo
255 svm_fifo_t *svm_fifo_create (u32 size);
260 * @param size size for fifo
262 void svm_fifo_init (svm_fifo_t * f, u32 size);
264 * Initialize fifo chunks and rbtree
268 void svm_fifo_init_chunks (svm_fifo_t * f);
270 * Allocate a fifo chunk on heap
272 * If the chunk is allocated on a fifo segment, this should be called
273 * with the segment's heap pushed.
275 * @param size chunk size in bytes. Will be rounded to the next highest
277 * @return new chunk or 0 if alloc failed
279 svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
281 * Grow fifo size by adding chunk to chunk list
283 * If fifos are allocated on a segment, this should be called with
284 * the segment's heap pushed.
286 * @param f fifo to be extended
287 * @param c chunk or linked list of chunks to be added
289 void svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c);
291 * Request to reduce fifo size by amount of bytes
293 * Because the producer might be enqueuing data when this is called, the
294 * actual size update is only applied when producer tries to enqueue new
295 * data, unless @param try_shrink is set.
298 * @param len number of bytes to remove from fifo. The actual number
299 * of bytes to be removed will be less or equal to this
301 * @param try_shrink flg to indicate if it's safe to try to shrink fifo
302 * size. It should be set only if this is called by the
303 * producer of if the producer is not using the fifo
304 * @return actual length fifo size will be reduced by
306 int svm_fifo_reduce_size (svm_fifo_t * f, u32 len, u8 try_shrink);
308 * Removes chunks that are after fifo end byte
310 * Needs to be called with segment heap pushed.
314 svm_fifo_chunk_t *svm_fifo_collect_chunks (svm_fifo_t * f);
316 * Free fifo and associated state
320 void svm_fifo_free (svm_fifo_t * f);
322 * Cleanup fifo chunk lookup rb tree
324 * The rb tree is allocated in segment heap so this should be called
327 * @param f fifo to cleanup
329 void svm_fifo_free_chunk_lookup (svm_fifo_t * f);
331 * Cleanup fifo ooo data
333 * The ooo data is allocated in producer process memory. The fifo
334 * segment heap should not be pushed.
336 * @param f fifo to cleanup
338 void svm_fifo_free_ooo_data (svm_fifo_t * f);
340 * Init fifo head and tail
343 * @param head head value that will be matched to a chunk
344 * @param tail tail value that will be matched to a chunk
346 void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
350 * Clones single/default chunk fifo. It does not work for fifos with
353 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
355 * Enqueue data to fifo
357 * Data is enqueued and tail pointer is updated atomically. If the new data
358 * enqueued partly overlaps or "touches" an out-of-order segment, said segment
359 * is "consumed" and the number of bytes returned is appropriately updated.
362 * @param len length of data to copy
363 * @param src buffer from where to copy the data
364 * @return number of contiguous bytes that can be consumed or error
366 int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src);
368 * Enqueue data to fifo with offset
370 * Data is enqueued without updating tail pointer. Instead, an out-of-order
371 * list of segments is generated and maintained. Fifo takes care of coalescing
372 * contiguous or overlapping segments.
375 * @param offset offset at which to copy the data
376 * @param len len of data to copy
377 * @param src buffer from where to copy the data
378 * @return 0 if enqueue was successful, error otherwise
380 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
384 * Advance tail pointer
386 * Useful for moving tail pointer after external enqueue.
389 * @param len number of bytes to add to tail
391 void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
393 * Overwrite fifo head with new data
395 * This should be typically used by dgram transport protocols that need
396 * to update the dgram header after dequeueing a chunk of data. It assumes
397 * that the dgram header is at most spread over two chunks.
400 * @param src src of new data
401 * @param len length of new data
403 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
405 * Dequeue data from fifo
407 * Data is dequeued to consumer provided buffer and head is atomically
411 * @param len length of data to dequeue
412 * @param dst buffer to where to dequeue the data
413 * @return number of bytes dequeued or error
415 int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
417 * Peek data from fifo
419 * Data is copied from requested offset into provided dst buffer. Head is
423 * @param offset offset from which to copy the data
424 * @param len length of data to copy
425 * @param dst buffer to where to dequeue the data
426 * @return number of bytes peeked
428 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
430 * Dequeue and drop bytes from fifo
432 * Advances fifo head by requested amount of bytes.
435 * @param len number of bytes to drop
436 * @return number of bytes dropped
438 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
440 * Dequeue and drop all bytes from fifo
442 * Advances head to tail position.
446 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
447 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs);
448 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs);
450 * Add io events subscriber to list
453 * @param sub subscriber opaque index (typically app worker index)
455 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
457 * Remove io events subscriber form list
460 * @param sub subscriber index to be removed
462 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
464 * Number of out-of-order segments for fifo
467 * @return number of out of order segments
469 u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
471 * First out-of-order segment for fifo
474 * @return first out-of-order segment for fifo
476 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
478 * Check if fifo is sane. Debug only.
481 * @return 1 if sane, 0 otherwise
483 u8 svm_fifo_is_sane (svm_fifo_t * f);
485 * Declare this fifo is used by only a single thread.
486 * In this special case, fifo-growth can be done in an efficient way without delay.
489 * @return 1 if the fifo is already owned by another thread, 0 otherwise
491 u8 svm_fifo_set_single_thread_owned (svm_fifo_t * f);
492 format_function_t format_svm_fifo;
495 * Fifo max bytes to dequeue optimized for consumer
498 * @return max number of bytes that can be dequeued
501 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
504 f_load_head_tail_cons (f, &head, &tail);
505 return f_cursize (f, head, tail);
509 * Fifo max bytes to dequeue optimized for producer
512 * @return max number of bytes that can be dequeued
515 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
518 f_load_head_tail_prod (f, &head, &tail);
519 return f_cursize (f, head, tail);
523 * Fifo max bytes to dequeue
525 * Note: use producer or consumer specific functions for performance:
526 * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f)
527 * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f)
530 svm_fifo_max_dequeue (svm_fifo_t * f)
533 f_load_head_tail_all_acq (f, &head, &tail);
534 return f_cursize (f, head, tail);
538 * Check if fifo is full optimized for producer
541 * @return 1 if fifo is full 0 otherwise
544 svm_fifo_is_full_prod (svm_fifo_t * f)
546 return (svm_fifo_max_dequeue_prod (f) == f->nitems);
549 /* Check if fifo is full.
551 * Note: use producer or consumer specific functions for performance.
552 * @ref svm_fifo_is_full_prod (svm_fifo_t * f)
553 * add cons version if needed
556 svm_fifo_is_full (svm_fifo_t * f)
558 return (svm_fifo_max_dequeue (f) == f->nitems);
562 * Check if fifo is empty optimized for consumer
565 * @return 1 if fifo is empty 0 otherwise
568 svm_fifo_is_empty_cons (svm_fifo_t * f)
570 return (svm_fifo_max_dequeue_cons (f) == 0);
574 * Check if fifo is empty optimized for producer
577 * @return 1 if fifo is empty 0 otherwise
580 svm_fifo_is_empty_prod (svm_fifo_t * f)
582 return (svm_fifo_max_dequeue_prod (f) == 0);
586 * Check if fifo is empty
588 * Note: use producer or consumer specific functions for perfomance.
589 * @ref svm_fifo_is_empty_cons (svm_fifo_t * f)
590 * @ref svm_fifo_is_empty_prod (svm_fifo_t * f)
593 svm_fifo_is_empty (svm_fifo_t * f)
595 return (svm_fifo_max_dequeue (f) == 0);
599 * Check if fifo is wrapped
602 * @return 1 if 'normalized' head is ahead of tail
605 svm_fifo_is_wrapped (svm_fifo_t * f)
608 f_load_head_tail_all_acq (f, &head, &tail);
613 * Maximum number of bytes that can be enqueued into fifo
615 * Optimized for producer
618 * @return max number of bytes that can be enqueued into fifo
621 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
624 f_load_head_tail_prod (f, &head, &tail);
625 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
626 svm_fifo_try_shrink (f, head, tail);
627 return f_free_count (f, head, tail);
630 /* Maximum number of bytes that can be enqueued into fifo
632 * Note: use producer or consumer specific functions for performance.
633 * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f)
634 * add consumer specific version if needed.
637 svm_fifo_max_enqueue (svm_fifo_t * f)
640 f_load_head_tail_all_acq (f, &head, &tail);
641 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
642 svm_fifo_try_shrink (f, head, tail);
643 return f_free_count (f, head, tail);
647 * Max contiguous chunk of data that can be read
650 svm_fifo_max_read_chunk (svm_fifo_t * f)
653 f_load_head_tail_cons (f, &head, &tail);
654 return tail >= head ? (tail - head) : (f->size - head);
658 * Max contiguous chunk of data that can be written
661 svm_fifo_max_write_chunk (svm_fifo_t * f)
664 f_load_head_tail_prod (f, &head, &tail);
665 return tail >= head ? f->size - tail : f_free_count (f, head, tail);
669 svm_fifo_head (svm_fifo_t * f)
671 /* load-relaxed: consumer owned index */
672 return (f->head_chunk->data + (f->head - f->head_chunk->start_byte));
676 svm_fifo_tail (svm_fifo_t * f)
678 /* load-relaxed: producer owned index */
679 return (f->tail_chunk->data + (f->tail - f->tail_chunk->start_byte));
683 svm_fifo_n_subscribers (svm_fifo_t * f)
685 return f->n_subscribers;
689 * Check if fifo has out-of-order data
692 * @return 1 if fifo has ooo data, 0 otherwise
695 svm_fifo_has_ooo_data (svm_fifo_t * f)
697 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
700 static inline ooo_segment_t *
701 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
703 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
705 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
709 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
711 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
715 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
718 /* load-relaxed: producer owned index */
721 return f_distance_to (f, s->start, tail);
725 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
731 * Check if fifo has io event
734 * @return 1 if fifo has event, 0 otherwise
737 svm_fifo_has_event (svm_fifo_t * f)
743 * Set fifo event flag.
745 * Forces release semantics.
748 * @return 1 if flag was not set, 0 otherwise
751 svm_fifo_set_event (svm_fifo_t * f)
753 return !clib_atomic_swap_rel_n (&f->has_event, 1);
757 * Unset fifo event flag.
759 * Forces acquire semantics
764 svm_fifo_unset_event (svm_fifo_t * f)
766 clib_atomic_swap_acq_n (&f->has_event, 0);
770 * Set specific want notification flag
772 * For list of flags see @ref svm_fifo_deq_ntf_t
775 * @param ntf_type type of notification requested
778 svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
780 f->want_deq_ntf |= ntf_type;
784 * Clear specific want notification flag
786 * For list of flags see @ref svm_fifo_ntf_t
789 * @param ntf_type type of notification to be cleared
792 svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
794 f->want_deq_ntf &= ~ntf_type;
798 * Clear the want notification flag and set has notification
800 * Should be used after enqueuing an event. This clears the
801 * SVM_FIFO_WANT_NOTIF flag but it does not clear
802 * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is
803 * set to avoid enqueueing events for for all dequeue operations until
804 * it is manually cleared.
809 svm_fifo_clear_deq_ntf (svm_fifo_t * f)
811 /* Set the flag if want_notif_if_full was the only ntf requested */
812 f->has_deq_ntf = f->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL;
813 svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF);
817 * Clear has notification flag
819 * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL
820 * request and sets has_ntf. To received new events the flag must be
821 * cleared using this function.
826 svm_fifo_reset_has_deq_ntf (svm_fifo_t * f)
832 * Check if fifo needs dequeue notification
834 * Determines based on notification request flags and state of the fifo if
835 * an event should be generated.
838 * @param n_last_deq number of bytes last dequeued
839 * @return 1 if event should be generated, 0 otherwise
842 svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq)
844 u8 want_ntf = f->want_deq_ntf;
846 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF))
848 else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF)
850 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL)
852 u32 max_deq = svm_fifo_max_dequeue_cons (f);
853 u32 nitems = f->nitems;
854 if (!f->has_deq_ntf && max_deq < nitems
855 && max_deq + n_last_deq >= nitems)
858 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY)
860 if (!f->has_deq_ntf && svm_fifo_is_empty (f))
866 #endif /* __included_ssvm_fifo_h__ */
869 * fd.io coding-style-patch-verification: ON
872 * eval: (c-set-style "gnu")