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 <svm/fifo_types.h>
28 #define OOO_SEGMENT_INVALID_INDEX ((u32)~0)
29 #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0)
30 #define SVM_FIFO_INVALID_INDEX ((u32)~0)
32 typedef enum svm_fifo_deq_ntf_
34 SVM_FIFO_NO_DEQ_NOTIF = 0, /**< No notification requested */
35 SVM_FIFO_WANT_DEQ_NOTIF = 1, /**< Notify on dequeue */
36 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL = 2, /**< Notify on transition from full */
37 SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY = 4, /**< Notify on transition to empty */
40 typedef enum svm_fifo_flag_
42 SVM_FIFO_F_LL_TRACKED = 1 << 0,
52 typedef struct svm_fifo_seg_
59 #define svm_fifo_trace_add(_f, _s, _l, _t) \
61 svm_fifo_trace_elem_t *trace_elt; \
62 vec_add2(_f->trace, trace_elt, 1); \
63 trace_elt->offset = _s; \
64 trace_elt->len = _l; \
65 trace_elt->action = _t; \
68 #define svm_fifo_trace_add(_f, _s, _l, _t)
71 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
72 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
75 * Load head and tail optimized for consumer
80 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
82 /* load-relaxed: consumer owned index */
84 /* load-acq: consumer foreign index (paired with store-rel in producer) */
85 *tail = clib_atomic_load_acq_n (&f->tail);
88 /** Load head and tail optimized for producer
93 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
95 /* load relaxed: producer owned index */
97 /* load-acq: producer foreign index (paired with store-rel in consumer) */
98 *head = clib_atomic_load_acq_n (&f->head);
102 * Load head and tail independent of producer/consumer role
107 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
109 /* load-acq : consumer foreign index (paired with store-rel) */
110 *tail = clib_atomic_load_acq_n (&f->tail);
111 /* load-acq : producer foriegn index (paired with store-rel) */
112 *head = clib_atomic_load_acq_n (&f->head);
116 * Fifo current size, i.e., number of bytes enqueued
121 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
127 * Fifo free bytes, i.e., number of free bytes
132 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
134 return (f->size - f_cursize (f, head, tail));
138 f_chunk_end (svm_fifo_chunk_t * c)
140 return c->start_byte + c->length;
144 f_pos_lt (u32 a, u32 b)
146 return ((i32) (a - b) < 0);
150 f_pos_leq (u32 a, u32 b)
152 return ((i32) (a - b) <= 0);
156 f_pos_gt (u32 a, u32 b)
158 return ((i32) (a - b) > 0);
162 f_pos_geq (u32 a, u32 b)
164 return ((i32) (a - b) >= 0);
168 f_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos)
170 return (f_pos_geq (pos, c->start_byte)
171 && f_pos_lt (pos, c->start_byte + c->length));
175 * Create fifo of requested size
177 * Allocates fifo on current heap.
179 * @param size data size in bytes for fifo to be allocated. Will be
180 * rounded to the next highest power-of-two value.
181 * @return pointer to new fifo
183 svm_fifo_t *svm_fifo_alloc (u32 size);
188 * @param size size for fifo
190 void svm_fifo_init (svm_fifo_t * f, u32 size);
192 * Allocate a fifo chunk on heap
194 * If the chunk is allocated on a fifo segment, this should be called
195 * with the segment's heap pushed.
197 * @param size chunk size in bytes. Will be rounded to the next highest
199 * @return new chunk or 0 if alloc failed
201 svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
203 * Ensure the whole fifo size is writeable
205 * Allocates enough chunks to cover the whole fifo size.
209 int svm_fifo_fill_chunk_list (svm_fifo_t * f);
211 * Initialize rbtrees used for ooo lookups
214 * @param ooo_type type of ooo operation (0 enqueue, 1 dequeue)
216 void svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type);
218 * Free fifo and associated state
222 void svm_fifo_free (svm_fifo_t * f);
224 * Cleanup fifo chunk lookup rb tree
226 * The rb tree is allocated in segment heap so this should be called
229 * @param f fifo to cleanup
231 void svm_fifo_free_chunk_lookup (svm_fifo_t * f);
233 * Cleanup fifo ooo data
235 * The ooo data is allocated in producer process memory. The fifo
236 * segment heap should not be pushed.
238 * @param f fifo to cleanup
240 void svm_fifo_free_ooo_data (svm_fifo_t * f);
242 * Init fifo head and tail
245 * @param head head value that will be matched to a chunk
246 * @param tail tail value that will be matched to a chunk
248 void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
252 * Clones single/default chunk fifo. It does not work for fifos with
255 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
257 * Enqueue data to fifo
259 * Data is enqueued and tail pointer is updated atomically. If the new data
260 * enqueued partly overlaps or "touches" an out-of-order segment, said segment
261 * is "consumed" and the number of bytes returned is appropriately updated.
264 * @param len length of data to copy
265 * @param src buffer from where to copy the data
266 * @return number of contiguous bytes that can be consumed or error
268 int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src);
270 * Enqueue data to fifo with offset
272 * Data is enqueued without updating tail pointer. Instead, an out-of-order
273 * list of segments is generated and maintained. Fifo takes care of coalescing
274 * contiguous or overlapping segments.
277 * @param offset offset at which to copy the data
278 * @param len len of data to copy
279 * @param src buffer from where to copy the data
280 * @return 0 if enqueue was successful, error otherwise
282 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
286 * Advance tail pointer
288 * Useful for moving tail pointer after external enqueue.
291 * @param len number of bytes to add to tail
293 void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
295 * Overwrite fifo head with new data
297 * This should be typically used by dgram transport protocols that need
298 * to update the dgram header after dequeuing a chunk of data. It assumes
299 * that the dgram header is at most spread over two chunks.
302 * @param src src of new data
303 * @param len length of new data
305 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
307 * Dequeue data from fifo
309 * Data is dequeued to consumer provided buffer and head is atomically
310 * updated. This should not be used in combination with ooo lookups. If
311 * ooo peeking of data is needed in combination with dequeuing use @ref
312 * svm_fifo_dequeue_drop.
315 * @param len length of data to dequeue
316 * @param dst buffer to where to dequeue the data
317 * @return number of bytes dequeued or error
319 int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
321 * Peek data from fifo
323 * Data is copied from requested offset into provided dst buffer. Head is
327 * @param offset offset from which to copy the data
328 * @param len length of data to copy
329 * @param dst buffer to where to dequeue the data
330 * @return number of bytes peeked
332 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
334 * Dequeue and drop bytes from fifo
336 * Advances fifo head by requested amount of bytes.
339 * @param len number of bytes to drop
340 * @return number of bytes dropped
342 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
344 * Dequeue and drop all bytes from fifo
346 * Advances head to tail position.
350 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
351 int svm_fifo_segments (svm_fifo_t * f, svm_fifo_seg_t * fs);
352 void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_seg_t * fs);
354 * Add io events subscriber to list
357 * @param sub subscriber opaque index (typically app worker index)
359 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
361 * Remove io events subscriber form list
364 * @param sub subscriber index to be removed
366 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
368 * Number of out-of-order segments for fifo
371 * @return number of out of order segments
373 u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
375 * First out-of-order segment for fifo
378 * @return first out-of-order segment for fifo
380 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
382 * Check if fifo is sane. Debug only.
385 * @return 1 if sane, 0 otherwise
387 u8 svm_fifo_is_sane (svm_fifo_t * f);
389 * Number of chunks linked into the fifo
392 * @return number of chunks in fifo linked list
394 u32 svm_fifo_n_chunks (svm_fifo_t * f);
395 format_function_t format_svm_fifo;
398 * Fifo max bytes to dequeue optimized for consumer
401 * @return max number of bytes that can be dequeued
404 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
407 f_load_head_tail_cons (f, &head, &tail);
408 return f_cursize (f, head, tail);
412 * Fifo max bytes to dequeue optimized for producer
415 * @return max number of bytes that can be dequeued
418 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
421 f_load_head_tail_prod (f, &head, &tail);
422 return f_cursize (f, head, tail);
426 * Fifo max bytes to dequeue
428 * Note: use producer or consumer specific functions for performance:
429 * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f)
430 * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f)
433 svm_fifo_max_dequeue (svm_fifo_t * f)
436 f_load_head_tail_all_acq (f, &head, &tail);
437 return f_cursize (f, head, tail);
441 * Check if fifo is full optimized for producer
444 * @return 1 if fifo is full 0 otherwise
447 svm_fifo_is_full_prod (svm_fifo_t * f)
449 return (svm_fifo_max_dequeue_prod (f) == f->size);
452 /* Check if fifo is full.
454 * Note: use producer or consumer specific functions for performance.
455 * @ref svm_fifo_is_full_prod (svm_fifo_t * f)
456 * add cons version if needed
459 svm_fifo_is_full (svm_fifo_t * f)
461 return (svm_fifo_max_dequeue (f) == f->size);
465 * Check if fifo is empty optimized for consumer
468 * @return 1 if fifo is empty 0 otherwise
471 svm_fifo_is_empty_cons (svm_fifo_t * f)
473 return (svm_fifo_max_dequeue_cons (f) == 0);
477 * Check if fifo is empty optimized for producer
480 * @return 1 if fifo is empty 0 otherwise
483 svm_fifo_is_empty_prod (svm_fifo_t * f)
485 return (svm_fifo_max_dequeue_prod (f) == 0);
489 * Check if fifo is empty
491 * Note: use producer or consumer specific functions for perfomance.
492 * @ref svm_fifo_is_empty_cons (svm_fifo_t * f)
493 * @ref svm_fifo_is_empty_prod (svm_fifo_t * f)
496 svm_fifo_is_empty (svm_fifo_t * f)
498 return (svm_fifo_max_dequeue (f) == 0);
502 * Check if fifo is wrapped
505 * @return 1 if 'normalized' head is ahead of tail
508 svm_fifo_is_wrapped (svm_fifo_t * f)
511 f_load_head_tail_all_acq (f, &head, &tail);
516 * Maximum number of bytes that can be enqueued into fifo
518 * Optimized for producer
521 * @return max number of bytes that can be enqueued into fifo
524 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
527 f_load_head_tail_prod (f, &head, &tail);
528 return f_free_count (f, head, tail);
531 /* Maximum number of bytes that can be enqueued into fifo
533 * Note: use producer or consumer specific functions for performance.
534 * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f)
535 * add consumer specific version if needed.
538 svm_fifo_max_enqueue (svm_fifo_t * f)
541 f_load_head_tail_all_acq (f, &head, &tail);
542 return f_free_count (f, head, tail);
546 * Max contiguous chunk of data that can be read.
548 * Should only be called by consumers.
550 u32 svm_fifo_max_read_chunk (svm_fifo_t * f);
553 * Max contiguous chunk of data that can be written
555 * Should only be called by producers
557 u32 svm_fifo_max_write_chunk (svm_fifo_t * f);
560 * Fifo head chunk getter
563 * @return head chunk pointer
565 static inline svm_fifo_chunk_t *
566 svm_fifo_head_chunk (svm_fifo_t * f)
568 return f->head_chunk;
572 * Fifo head pointer getter
575 * @return head pointer
578 svm_fifo_head (svm_fifo_t * f)
582 /* load-relaxed: consumer owned index */
583 return (f->head_chunk->data + (f->head - f->head_chunk->start_byte));
587 * Fifo tail chunk getter
590 * @return tail chunk pointer
592 static inline svm_fifo_chunk_t *
593 svm_fifo_tail_chunk (svm_fifo_t * f)
595 return f->tail_chunk;
599 * Fifo tail pointer getter
602 * @return tail pointer
605 svm_fifo_tail (svm_fifo_t * f)
607 /* load-relaxed: producer owned index */
608 return (f->tail_chunk->data + (f->tail - f->tail_chunk->start_byte));
612 * Fifo number of subscribers getter
615 * @return number of subscribers
618 svm_fifo_n_subscribers (svm_fifo_t * f)
620 return f->n_subscribers;
624 * Check if fifo has out-of-order data
627 * @return 1 if fifo has ooo data, 0 otherwise
630 svm_fifo_has_ooo_data (svm_fifo_t * f)
632 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
635 static inline ooo_segment_t *
636 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
638 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
640 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
644 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
646 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
650 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
653 /* load-relaxed: producer owned index */
656 return (s->start - tail);
660 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
666 svm_fifo_size (svm_fifo_t * f)
672 svm_fifo_set_size (svm_fifo_t * f, u32 size)
674 if (size > (1 << f->fs_hdr->max_log2_chunk_size))
676 fsh_virtual_mem_update (f->fs_hdr, f->slice_index, (int) f->size - size);
681 * Check if fifo has io event
684 * @return 1 if fifo has event, 0 otherwise
687 svm_fifo_has_event (svm_fifo_t * f)
693 * Set fifo event flag.
695 * Forces release semantics.
698 * @return 1 if flag was not set, 0 otherwise
701 svm_fifo_set_event (svm_fifo_t * f)
703 return !clib_atomic_swap_rel_n (&f->has_event, 1);
707 * Unset fifo event flag.
709 * Forces acquire semantics
714 svm_fifo_unset_event (svm_fifo_t * f)
716 clib_atomic_swap_acq_n (&f->has_event, 0);
720 * Set specific want notification flag
722 * For list of flags see @ref svm_fifo_deq_ntf_t
725 * @param ntf_type type of notification requested
728 svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
730 f->want_deq_ntf |= ntf_type;
734 * Clear specific want notification flag
736 * For list of flags see @ref svm_fifo_ntf_t
739 * @param ntf_type type of notification to be cleared
742 svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
744 f->want_deq_ntf &= ~ntf_type;
748 * Clear the want notification flag and set has notification
750 * Should be used after enqueuing an event. This clears the
751 * SVM_FIFO_WANT_NOTIF flag but it does not clear
752 * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is
753 * set to avoid enqueueing events for for all dequeue operations until
754 * it is manually cleared.
759 svm_fifo_clear_deq_ntf (svm_fifo_t * f)
761 /* Set the flag if want_notif_if_full was the only ntf requested */
762 f->has_deq_ntf = f->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL;
763 svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF);
767 * Clear has notification flag
769 * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL
770 * request and sets has_ntf. To received new events the flag must be
771 * cleared using this function.
776 svm_fifo_reset_has_deq_ntf (svm_fifo_t * f)
782 * Check if fifo needs dequeue notification
784 * Determines based on notification request flags and state of the fifo if
785 * an event should be generated.
788 * @param n_last_deq number of bytes last dequeued
789 * @return 1 if event should be generated, 0 otherwise
792 svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq)
794 u8 want_ntf = f->want_deq_ntf;
796 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF))
798 else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF)
800 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL)
802 u32 max_deq = svm_fifo_max_dequeue_cons (f);
804 if (!f->has_deq_ntf && max_deq < size && max_deq + n_last_deq >= size)
807 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY)
809 if (!f->has_deq_ntf && svm_fifo_is_empty (f))
815 #endif /* __included_ssvm_fifo_h__ */
818 * fd.io coding-style-patch-verification: ON
821 * eval: (c-set-style "gnu")