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 u32 freelist_index; /**< aka log2(allocated_size) - const. */
95 struct _svm_fifo *next; /**< next in freelist/active chain */
96 struct _svm_fifo *prev; /**< prev in active chain */
97 u32 size_decrement; /**< bytes to remove from fifo */
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; /**< Number of subscribers for io events */
114 u8 subscribers[SVM_FIFO_MAX_EVT_SUBSCRIBERS];
117 svm_fifo_trace_elem_t *trace;
120 svm_fifo_chunk_t default_chunk;
126 SVM_FIFO_EEMPTY = -3,
129 typedef struct svm_fifo_seg_
136 #define svm_fifo_trace_add(_f, _s, _l, _t) \
138 svm_fifo_trace_elem_t *trace_elt; \
139 vec_add2(_f->trace, trace_elt, 1); \
140 trace_elt->offset = _s; \
141 trace_elt->len = _l; \
142 trace_elt->action = _t; \
145 #define svm_fifo_trace_add(_f, _s, _l, _t)
148 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
149 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
152 * Load head and tail optimized for consumer
157 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
159 /* load-relaxed: consumer owned index */
161 /* load-acq: consumer foreign index (paired with store-rel in producer) */
162 *tail = clib_atomic_load_acq_n (&f->tail);
165 /** Load head and tail optimized for producer
170 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
172 /* load relaxed: producer owned index */
174 /* load-acq: producer foreign index (paired with store-rel in consumer) */
175 *head = clib_atomic_load_acq_n (&f->head);
179 * Load head and tail independent of producer/consumer role
184 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
186 /* load-acq : consumer foreign index (paired with store-rel) */
187 *tail = clib_atomic_load_acq_n (&f->tail);
188 /* load-acq : producer foriegn index (paired with store-rel) */
189 *head = clib_atomic_load_acq_n (&f->head);
193 * Distance to a from b, i.e., a - b in the fifo
198 f_distance_to (svm_fifo_t * f, u32 a, u32 b)
200 return ((f->size + a - b) % f->size);
204 * Distance from a to b, i.e., b - a in the fifo
209 f_distance_from (svm_fifo_t * f, u32 a, u32 b)
211 return ((f->size + b - a) % f->size);
215 * Fifo current size, i.e., number of bytes enqueued
220 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
222 return (head <= tail ? tail - head : f->size + tail - head);
226 * Fifo free bytes, i.e., number of free bytes
231 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
233 return (f->nitems - f_cursize (f, head, tail));
237 * Try to shrink fifo size.
241 void svm_fifo_try_shrink (svm_fifo_t * f, u32 head, u32 tail);
244 * Create fifo of requested size
246 * Allocates fifo on current heap.
248 * @param size data size in bytes for fifo to be allocated. Will be
249 * rounded to the next highest power-of-two value.
250 * @return pointer to new fifo
252 svm_fifo_t *svm_fifo_create (u32 size);
256 * @param size size for fifo
258 void svm_fifo_init (svm_fifo_t * f, u32 size);
260 * Allocate a fifo chunk on heap
262 * If the chunk is allocated on a fifo segment, this should be called
263 * with the segment's heap pushed.
265 * @param size chunk size in bytes. Will be rounded to the next highest
267 * @return new chunk or 0 if alloc failed
269 svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
271 * Grow fifo size by adding chunk to chunk list
273 * If fifos are allocated on a segment, this should be called with
274 * the segment's heap pushed.
276 * @param f fifo to be extended
277 * @param c chunk or linked list of chunks to be added
279 void svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c);
281 * Request to reduce fifo size by amount of bytes
283 * Because the producer might be enqueuing data when this is called, the
284 * actual size update is only applied when producer tries to enqueue new
285 * data, unless @param try_shrink is set.
288 * @param len number of bytes to remove from fifo. The actual number
289 * of bytes to be removed will be less or equal to this
291 * @param try_shrink flg to indicate if it's safe to try to shrink fifo
292 * size. It should be set only if this is called by the
293 * producer of if the producer is not using the fifo
294 * @return actual length fifo size will be reduced by
296 int svm_fifo_reduce_size (svm_fifo_t * f, u32 len, u8 try_shrink);
298 * Removes chunks that are after fifo end byte
300 * Needs to be called with segment heap pushed.
304 svm_fifo_chunk_t *svm_fifo_collect_chunks (svm_fifo_t * f);
306 * Free fifo and associated state
310 void svm_fifo_free (svm_fifo_t * f);
312 * Cleanup fifo chunk lookup rb tree
314 * The rb tree is allocated in segment heap so this should be called
317 * @param f fifo to cleanup
319 void svm_fifo_free_chunk_lookup (svm_fifo_t * f);
321 * Cleanup fifo ooo data
323 * The ooo data is allocated in producer process memory. The fifo
324 * segment heap should not be pushed.
326 * @param f fifo to cleanup
328 void svm_fifo_free_ooo_data (svm_fifo_t * f);
330 * Init fifo head and tail
333 * @param head head value that will be matched to a chunk
334 * @param tail tail value that will be matched to a chunk
336 void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
340 * Clones single/default chunk fifo. It does not work for fifos with
343 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
345 * Enqueue data to fifo
347 * Data is enqueued and tail pointer is updated atomically. If the new data
348 * enqueued partly overlaps or "touches" an out-of-order segment, said segment
349 * is "consumed" and the number of bytes returned is appropriately updated.
352 * @param len length of data to copy
353 * @param src buffer from where to copy the data
354 * @return number of contiguous bytes that can be consumed or error
356 int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src);
358 * Enqueue data to fifo with offset
360 * Data is enqueued without updating tail pointer. Instead, an out-of-order
361 * list of segments is generated and maintained. Fifo takes care of coalescing
362 * contiguous or overlapping segments.
365 * @param offset offset at which to copy the data
366 * @param len len of data to copy
367 * @param src buffer from where to copy the data
368 * @return 0 if enqueue was successful, error otherwise
370 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
374 * Advance tail pointer
376 * Useful for moving tail pointer after external enqueue.
379 * @param len number of bytes to add to tail
381 void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
383 * Overwrite fifo head with new data
385 * This should be typically used by dgram transport protocols that need
386 * to update the dgram header after dequeueing a chunk of data. It assumes
387 * that the dgram header is at most spread over two chunks.
390 * @param src src of new data
391 * @param len length of new data
393 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
395 * Dequeue data from fifo
397 * Data is dequeued to consumer provided buffer and head is atomically
401 * @param len length of data to dequeue
402 * @param dst buffer to where to dequeue the data
403 * @return number of bytes dequeued or error
405 int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
407 * Peek data from fifo
409 * Data is copied from requested offset into provided dst buffer. Head is
413 * @param offset offset from which to copy the data
414 * @param len length of data to copy
415 * @param dst buffer to where to dequeue the data
416 * @return number of bytes peeked
418 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
420 * Dequeue and drop bytes from fifo
422 * Advances fifo head by requested amount of bytes.
425 * @param len number of bytes to drop
426 * @return number of bytes dropped
428 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
430 * Dequeue and drop all bytes from fifo
432 * Advances head to tail position.
436 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
437 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs);
438 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs);
440 * Add io events subscriber to list
443 * @param sub subscriber opaque index (typically app worker index)
445 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
447 * Remove io events subscriber form list
450 * @param sub subscriber index to be removed
452 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
454 * Number of out-of-order segments for fifo
457 * @return number of out of order segments
459 u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
461 * First out-of-order segment for fifo
464 * @return first out-of-order segment for fifo
466 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
467 format_function_t format_svm_fifo;
470 * Fifo max bytes to dequeue optimized for consumer
473 * @return max number of bytes that can be dequeued
476 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
479 f_load_head_tail_cons (f, &head, &tail);
480 return f_cursize (f, head, tail);
484 * Fifo max bytes to dequeue optimized for producer
487 * @return max number of bytes that can be dequeued
490 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
493 f_load_head_tail_prod (f, &head, &tail);
494 return f_cursize (f, head, tail);
498 * Fifo max bytes to dequeue
500 * Note: use producer or consumer specific functions for performance:
501 * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f)
502 * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f)
505 svm_fifo_max_dequeue (svm_fifo_t * f)
508 f_load_head_tail_all_acq (f, &head, &tail);
509 return f_cursize (f, head, tail);
513 * Check if fifo is full optimized for producer
516 * @return 1 if fifo is full 0 otherwise
519 svm_fifo_is_full_prod (svm_fifo_t * f)
521 return (svm_fifo_max_dequeue_prod (f) == f->nitems);
524 /* Check if fifo is full.
526 * Note: use producer or consumer specific functions for performance.
527 * @ref svm_fifo_is_full_prod (svm_fifo_t * f)
528 * add cons version if needed
531 svm_fifo_is_full (svm_fifo_t * f)
533 return (svm_fifo_max_dequeue (f) == f->nitems);
537 * Check if fifo is empty optimized for consumer
540 * @return 1 if fifo is empty 0 otherwise
543 svm_fifo_is_empty_cons (svm_fifo_t * f)
545 return (svm_fifo_max_dequeue_cons (f) == 0);
549 * Check if fifo is empty optimized for producer
552 * @return 1 if fifo is empty 0 otherwise
555 svm_fifo_is_empty_prod (svm_fifo_t * f)
557 return (svm_fifo_max_dequeue_prod (f) == 0);
561 * Check if fifo is empty
563 * Note: use producer or consumer specific functions for perfomance.
564 * @ref svm_fifo_is_empty_cons (svm_fifo_t * f)
565 * @ref svm_fifo_is_empty_prod (svm_fifo_t * f)
568 svm_fifo_is_empty (svm_fifo_t * f)
570 return (svm_fifo_max_dequeue (f) == 0);
574 * Check if fifo is wrapped
577 * @return 1 if 'normalized' head is ahead of tail
580 svm_fifo_is_wrapped (svm_fifo_t * f)
583 f_load_head_tail_all_acq (f, &head, &tail);
588 * Maximum number of bytes that can be enqueued into fifo
590 * Optimized for producer
593 * @return max number of bytes that can be enqueued into fifo
596 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
599 f_load_head_tail_prod (f, &head, &tail);
600 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
601 svm_fifo_try_shrink (f, head, tail);
602 return f_free_count (f, head, tail);
605 /* Maximum number of bytes that can be enqueued into fifo
607 * Note: use producer or consumer specific functions for performance.
608 * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f)
609 * add consumer specific version if needed.
612 svm_fifo_max_enqueue (svm_fifo_t * f)
615 f_load_head_tail_all_acq (f, &head, &tail);
616 if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SHRINK))
617 svm_fifo_try_shrink (f, head, tail);
618 return f_free_count (f, head, tail);
622 * Max contiguous chunk of data that can be read
625 svm_fifo_max_read_chunk (svm_fifo_t * f)
628 f_load_head_tail_cons (f, &head, &tail);
629 return tail >= head ? (tail - head) : (f->size - head);
633 * Max contiguous chunk of data that can be written
636 svm_fifo_max_write_chunk (svm_fifo_t * f)
639 f_load_head_tail_prod (f, &head, &tail);
640 return tail > head ? f->size - tail : f_free_count (f, head, tail);
644 svm_fifo_head (svm_fifo_t * f)
646 /* load-relaxed: consumer owned index */
647 return (f->head_chunk->data + (f->head - f->head_chunk->start_byte));
651 svm_fifo_tail (svm_fifo_t * f)
653 /* load-relaxed: producer owned index */
654 return (f->tail_chunk->data + (f->tail - f->tail_chunk->start_byte));
658 svm_fifo_n_subscribers (svm_fifo_t * f)
660 return f->n_subscribers;
664 * Check if fifo has out-of-order data
667 * @return 1 if fifo has ooo data, 0 otherwise
670 svm_fifo_has_ooo_data (svm_fifo_t * f)
672 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
675 static inline ooo_segment_t *
676 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
678 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
680 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
684 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
686 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
690 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
693 /* load-relaxed: producer owned index */
696 return f_distance_to (f, s->start, tail);
700 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
706 * Check if fifo has io event
709 * @return 1 if fifo has event, 0 otherwise
712 svm_fifo_has_event (svm_fifo_t * f)
718 * Set fifo event flag.
720 * Forces release semantics.
723 * @return 1 if flag was not set, 0 otherwise
726 svm_fifo_set_event (svm_fifo_t * f)
728 return !clib_atomic_swap_rel_n (&f->has_event, 1);
732 * Unset fifo event flag.
734 * Forces acquire semantics
739 svm_fifo_unset_event (svm_fifo_t * f)
741 clib_atomic_swap_acq_n (&f->has_event, 0);
745 * Set specific want tx notification flag
747 * For list of flags see @ref svm_fifo_tx_ntf_t
750 * @param ntf_type type of notification requested
753 svm_fifo_add_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
755 f->want_tx_ntf |= ntf_type;
759 * Clear specific want tx notification flag
761 * For list of flags see @ref svm_fifo_tx_ntf_t
764 * @param ntf_type type of notification to be cleared
767 svm_fifo_del_want_tx_ntf (svm_fifo_t * f, u8 ntf_type)
769 f->want_tx_ntf &= ~ntf_type;
773 * Clear the want tx notification flag and set has tx notification
775 * Should be used after enqueuing a tx event. This clears the
776 * SVM_FIFO_WANT_TX_NOTIF flag but it does not clear
777 * SVM_FIFO_WANT_TX_NOTIF_IF_FULL. If the latter was set, has_tx_ntf is
778 * set to avoid enqueueing tx events for for all dequeue operations until
779 * it is manually cleared.
784 svm_fifo_clear_tx_ntf (svm_fifo_t * f)
786 /* Set the flag if want_tx_notif_if_full was the only ntf requested */
787 f->has_tx_ntf = f->want_tx_ntf == SVM_FIFO_WANT_TX_NOTIF_IF_FULL;
788 svm_fifo_del_want_tx_ntf (f, SVM_FIFO_WANT_TX_NOTIF);
792 * Clear has tx notification flag
794 * The fifo generates only one event per SVM_FIFO_WANT_TX_NOTIF_IF_FULL
795 * request and sets has_tx_ntf. To received new events the flag must be
796 * cleared using this function.
801 svm_fifo_reset_tx_ntf (svm_fifo_t * f)
807 * Check if fifo needs tx notification
809 * Determines based on tx notification request flags and state of the fifo if
810 * a tx io event should be generated.
813 * @param n_last_deq number of bytes last dequeued
814 * @return 1 if tx io event should be generated, 0 otherwise
817 svm_fifo_needs_tx_ntf (svm_fifo_t * f, u32 n_last_deq)
819 u8 want_ntf = f->want_tx_ntf;
821 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_TX_NOTIF))
823 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF)
825 else if (want_ntf & SVM_FIFO_WANT_TX_NOTIF_IF_FULL)
827 u32 max_deq = svm_fifo_max_dequeue_cons (f);
828 u32 nitems = f->nitems;
829 if (!f->has_tx_ntf && max_deq < nitems
830 && max_deq + n_last_deq >= nitems)
838 #endif /* __included_ssvm_fifo_h__ */
841 * fd.io coding-style-patch-verification: ON
844 * eval: (c-set-style "gnu")