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 */
38 SVM_FIFO_WANT_DEQ_NOTIF_IF_LEQ_THRESH = 5, /**< Notify on transition to less
39 than or equal threshold */
42 typedef enum svm_fifo_flag_
44 SVM_FIFO_F_LL_TRACKED = 1 << 0,
54 typedef struct svm_fifo_seg_
61 #define svm_fifo_trace_add(_f, _s, _l, _t) \
63 svm_fifo_trace_elem_t *trace_elt; \
64 vec_add2(_f->trace, trace_elt, 1); \
65 trace_elt->offset = _s; \
66 trace_elt->len = _l; \
67 trace_elt->action = _t; \
70 #define svm_fifo_trace_add(_f, _s, _l, _t)
73 u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f);
74 u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose);
77 * Load head and tail optimized for consumer
82 f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail)
84 /* load-relaxed: consumer owned index */
86 /* load-acq: consumer foreign index (paired with store-rel in producer) */
87 *tail = clib_atomic_load_acq_n (&f->shr->tail);
90 /** Load head and tail optimized for producer
95 f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail)
97 /* load relaxed: producer owned index */
99 /* load-acq: producer foreign index (paired with store-rel in consumer) */
100 *head = clib_atomic_load_acq_n (&f->shr->head);
104 * Load head and tail independent of producer/consumer role
109 f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail)
111 /* load-acq : consumer foreign index (paired with store-rel) */
112 *tail = clib_atomic_load_acq_n (&f->shr->tail);
113 /* load-acq : producer foriegn index (paired with store-rel) */
114 *head = clib_atomic_load_acq_n (&f->shr->head);
118 * Fifo current size, i.e., number of bytes enqueued
123 f_cursize (svm_fifo_t * f, u32 head, u32 tail)
129 * Fifo free bytes, i.e., number of free bytes
134 f_free_count (svm_fifo_t * f, u32 head, u32 tail)
136 return (f->shr->size - f_cursize (f, head, tail));
140 f_chunk_end (svm_fifo_chunk_t * c)
142 return c->start_byte + c->length;
146 f_pos_lt (u32 a, u32 b)
148 return ((i32) (a - b) < 0);
152 f_pos_leq (u32 a, u32 b)
154 return ((i32) (a - b) <= 0);
158 f_pos_gt (u32 a, u32 b)
160 return ((i32) (a - b) > 0);
164 f_pos_geq (u32 a, u32 b)
166 return ((i32) (a - b) >= 0);
170 f_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos)
172 return (f_pos_geq (pos, c->start_byte)
173 && f_pos_lt (pos, c->start_byte + c->length));
176 always_inline svm_fifo_chunk_t *
177 f_start_cptr (svm_fifo_t *f)
179 return fs_chunk_ptr (f->fs_hdr, f->shr->start_chunk);
182 always_inline svm_fifo_chunk_t *
183 f_end_cptr (svm_fifo_t *f)
185 return fs_chunk_ptr (f->fs_hdr, f->shr->end_chunk);
188 always_inline svm_fifo_chunk_t *
189 f_head_cptr (svm_fifo_t *f)
191 return fs_chunk_ptr (f->fs_hdr, f->shr->head_chunk);
194 always_inline svm_fifo_chunk_t *
195 f_tail_cptr (svm_fifo_t *f)
197 return fs_chunk_ptr (f->fs_hdr, f->shr->tail_chunk);
200 always_inline svm_fifo_chunk_t *
201 f_cptr (svm_fifo_t *f, fs_sptr_t cp)
203 return fs_chunk_ptr (f->fs_hdr, cp);
206 always_inline fs_sptr_t
207 f_csptr (svm_fifo_t *f, svm_fifo_chunk_t *c)
209 return fs_chunk_sptr (f->fs_hdr, c);
213 f_csptr_link (svm_fifo_t *f, fs_sptr_t cp, svm_fifo_chunk_t *c)
215 fs_chunk_ptr (f->fs_hdr, cp)->next = fs_chunk_sptr (f->fs_hdr, c);
219 * Create fifo of requested size
221 * Allocates fifo on current heap.
223 * @param size data size in bytes for fifo to be allocated. Will be
224 * rounded to the next highest power-of-two value.
225 * @return pointer to new fifo
227 svm_fifo_t *svm_fifo_alloc (u32 size);
232 * @param size size for fifo
234 void svm_fifo_init (svm_fifo_t * f, u32 size);
236 * Allocate a fifo chunk on heap
238 * If the chunk is allocated on a fifo segment, this should be called
239 * with the segment's heap pushed.
241 * @param size chunk size in bytes. Will be rounded to the next highest
243 * @return new chunk or 0 if alloc failed
245 svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
247 * Ensure the whole fifo size is writeable
249 * Allocates enough chunks to cover the whole fifo size.
253 int svm_fifo_fill_chunk_list (svm_fifo_t * f);
255 * Provision and return chunks for number of bytes requested
257 * Allocates enough chunks to cover the bytes requested and returns them
258 * in the fifo segment array. The number of bytes provisioned may be less
259 * than requested if not enough segments were provided.
262 * @param fs array of fifo segments
263 * @param n_segs length of fifo segments array
264 * @param len number of bytes to preallocate
265 * @return number of fifo segments provisioned or error
267 int svm_fifo_provision_chunks (svm_fifo_t *f, svm_fifo_seg_t *fs, u32 n_segs,
270 * Initialize rbtrees used for ooo lookups
273 * @param ooo_type type of ooo operation (0 enqueue, 1 dequeue)
275 void svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type);
277 * Free fifo and associated state
281 void svm_fifo_free (svm_fifo_t * f);
283 * Cleanup fifo chunk lookup rb tree
285 * The rb tree is allocated in segment heap so this should be called
288 * @param f fifo to cleanup
290 void svm_fifo_free_chunk_lookup (svm_fifo_t * f);
292 * Cleanup fifo ooo data
294 * The ooo data is allocated in producer process memory. The fifo
295 * segment heap should not be pushed.
297 * @param f fifo to cleanup
299 void svm_fifo_free_ooo_data (svm_fifo_t * f);
301 * Init fifo head and tail
304 * @param head head value that will be matched to a chunk
305 * @param tail tail value that will be matched to a chunk
307 void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
311 * Clones single/default chunk fifo. It does not work for fifos with
314 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
316 * Enqueue data to fifo
318 * Data is enqueued and tail pointer is updated atomically. If the new data
319 * enqueued partly overlaps or "touches" an out-of-order segment, said segment
320 * is "consumed" and the number of bytes returned is appropriately updated.
323 * @param len length of data to copy
324 * @param src buffer from where to copy the data
325 * @return number of contiguous bytes that can be consumed or error
327 int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src);
329 * Enqueue data to fifo with offset
331 * Data is enqueued without updating tail pointer. Instead, an out-of-order
332 * list of segments is generated and maintained. Fifo takes care of coalescing
333 * contiguous or overlapping segments.
336 * @param offset offset at which to copy the data
337 * @param len len of data to copy
338 * @param src buffer from where to copy the data
339 * @return 0 if enqueue was successful, error otherwise
341 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
345 * Advance tail pointer
347 * Useful for moving tail pointer after external enqueue.
350 * @param len number of bytes to add to tail
352 void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
354 * Enqueue array of @ref svm_fifo_seg_t in order
357 * @param segs array of segments to enqueue
358 * @param n_segs number of segments
359 * @param allow_partial if set partial enqueues are allowed
360 * @return len if enqueue was successful, error otherwise
362 int svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[],
363 u32 n_segs, u8 allow_partial);
365 * Overwrite fifo head with new data
367 * This should be typically used by dgram transport protocols that need
368 * to update the dgram header after dequeuing a chunk of data. It assumes
369 * that the dgram header is at most spread over two chunks.
372 * @param src src of new data
373 * @param len length of new data
375 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
377 * Dequeue data from fifo
379 * Data is dequeued to consumer provided buffer and head is atomically
380 * updated. This should not be used in combination with ooo lookups. If
381 * ooo peeking of data is needed in combination with dequeuing use @ref
382 * svm_fifo_dequeue_drop.
385 * @param len length of data to dequeue
386 * @param dst buffer to where to dequeue the data
387 * @return number of bytes dequeued or error
389 int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
391 * Peek data from fifo
393 * Data is copied from requested offset into provided dst buffer. Head is
397 * @param offset offset from which to copy the data
398 * @param len length of data to copy
399 * @param dst buffer to where to dequeue the data
400 * @return number of bytes peeked
402 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
404 * Dequeue and drop bytes from fifo
406 * Advances fifo head by requested amount of bytes.
409 * @param len number of bytes to drop
410 * @return number of bytes dropped
412 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
414 * Dequeue and drop all bytes from fifo
416 * Advances head to tail position.
420 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
422 * Get pointers to fifo chunks data in @ref svm_fifo_seg_t array
424 * Populates fifo segment array with pointers to fifo chunk data and lengths.
425 * Because this returns pointers to data, it must be paired with
426 * @ref svm_fifo_dequeue_drop to actually release the fifo chunks after the
430 * @param offset offset from where to retrieve segments
431 * @param fs array of fifo segments allocated by caller
432 * @param n_segs number of fifo segments in array
433 * @param max_bytes max bytes to be mapped to fifo segments
434 * @return number of bytes in fifo segments or SVM_FIFO_EEMPTY
436 int svm_fifo_segments (svm_fifo_t * f, u32 offset, svm_fifo_seg_t * fs,
437 u32 n_segs, u32 max_bytes);
439 * Add io events subscriber to list
442 * @param sub subscriber opaque index (typically app worker index)
444 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
446 * Remove io events subscriber form list
449 * @param sub subscriber index to be removed
451 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
453 * Number of out-of-order segments for fifo
456 * @return number of out of order segments
458 u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
460 * First out-of-order segment for fifo
463 * @return first out-of-order segment for fifo
465 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
467 * Check if fifo is sane. Debug only.
470 * @return 1 if sane, 0 otherwise
472 u8 svm_fifo_is_sane (svm_fifo_t * f);
474 * Number of chunks linked into the fifo
477 * @return number of chunks in fifo linked list
479 u32 svm_fifo_n_chunks (svm_fifo_t * f);
480 format_function_t format_svm_fifo;
483 * Fifo max bytes to dequeue optimized for consumer
486 * @return max number of bytes that can be dequeued
489 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
492 f_load_head_tail_cons (f, &head, &tail);
493 return f_cursize (f, head, tail);
497 * Fifo max bytes to dequeue optimized for producer
500 * @return max number of bytes that can be dequeued
503 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
506 f_load_head_tail_prod (f, &head, &tail);
507 return f_cursize (f, head, tail);
511 * Fifo max bytes to dequeue
513 * Note: use producer or consumer specific functions for performance:
514 * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f)
515 * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f)
518 svm_fifo_max_dequeue (svm_fifo_t * f)
521 f_load_head_tail_all_acq (f, &head, &tail);
522 return f_cursize (f, head, tail);
526 * Check if fifo is full optimized for producer
529 * @return 1 if fifo is full 0 otherwise
532 svm_fifo_is_full_prod (svm_fifo_t * f)
534 return (svm_fifo_max_dequeue_prod (f) == f->shr->size);
537 /* Check if fifo is full.
539 * Note: use producer or consumer specific functions for performance.
540 * @ref svm_fifo_is_full_prod (svm_fifo_t * f)
541 * add cons version if needed
544 svm_fifo_is_full (svm_fifo_t * f)
546 return (svm_fifo_max_dequeue (f) == f->shr->size);
550 * Check if fifo is empty optimized for consumer
553 * @return 1 if fifo is empty 0 otherwise
556 svm_fifo_is_empty_cons (svm_fifo_t * f)
558 return (svm_fifo_max_dequeue_cons (f) == 0);
562 * Check if fifo is empty optimized for producer
565 * @return 1 if fifo is empty 0 otherwise
568 svm_fifo_is_empty_prod (svm_fifo_t * f)
570 return (svm_fifo_max_dequeue_prod (f) == 0);
574 * Check if fifo is empty
576 * Note: use producer or consumer specific functions for perfomance.
577 * @ref svm_fifo_is_empty_cons (svm_fifo_t * f)
578 * @ref svm_fifo_is_empty_prod (svm_fifo_t * f)
581 svm_fifo_is_empty (svm_fifo_t * f)
583 return (svm_fifo_max_dequeue (f) == 0);
587 * Check if fifo is wrapped
590 * @return 1 if 'normalized' head is ahead of tail
593 svm_fifo_is_wrapped (svm_fifo_t * f)
596 f_load_head_tail_all_acq (f, &head, &tail);
601 * Maximum number of bytes that can be enqueued into fifo
603 * Optimized for producer
606 * @return max number of bytes that can be enqueued into fifo
609 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
612 f_load_head_tail_prod (f, &head, &tail);
613 return f_free_count (f, head, tail);
616 /* Maximum number of bytes that can be enqueued into fifo
618 * Note: use producer or consumer specific functions for performance.
619 * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f)
620 * add consumer specific version if needed.
623 svm_fifo_max_enqueue (svm_fifo_t * f)
626 f_load_head_tail_all_acq (f, &head, &tail);
627 return f_free_count (f, head, tail);
631 * Max contiguous chunk of data that can be read.
633 * Should only be called by consumers.
635 u32 svm_fifo_max_read_chunk (svm_fifo_t * f);
638 * Max contiguous chunk of data that can be written
640 * Should only be called by producers
642 u32 svm_fifo_max_write_chunk (svm_fifo_t * f);
645 * Fifo number of subscribers getter
648 * @return number of subscribers
651 svm_fifo_n_subscribers (svm_fifo_t * f)
653 return f->shr->n_subscribers;
657 * Check if fifo has out-of-order data
660 * @return 1 if fifo has ooo data, 0 otherwise
663 svm_fifo_has_ooo_data (svm_fifo_t * f)
665 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
668 static inline ooo_segment_t *
669 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
671 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
673 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
677 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
679 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
683 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
686 /* load-relaxed: producer owned index */
689 return (s->start - tail);
693 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
699 svm_fifo_size (svm_fifo_t * f)
705 svm_fifo_set_size (svm_fifo_t * f, u32 size)
707 if (size > (1 << f->fs_hdr->max_log2_fifo_size))
709 fsh_virtual_mem_update (f->fs_hdr, f->shr->slice_index,
710 (int) f->shr->size - size);
715 * Check if fifo has io event
718 * @return 1 if fifo has event, 0 otherwise
721 svm_fifo_has_event (svm_fifo_t * f)
723 return f->shr->has_event;
727 * Set fifo event flag.
729 * Forces release semantics.
732 * @return 1 if flag was not set, 0 otherwise
735 svm_fifo_set_event (svm_fifo_t * f)
737 return !clib_atomic_swap_rel_n (&f->shr->has_event, 1);
741 * Unset fifo event flag.
743 * Forces acquire semantics
748 svm_fifo_unset_event (svm_fifo_t * f)
750 clib_atomic_swap_acq_n (&f->shr->has_event, 0);
754 * Set specific want notification flag
756 * For list of flags see @ref svm_fifo_deq_ntf_t
759 * @param ntf_type type of notification requested
762 svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
764 f->shr->want_deq_ntf |= ntf_type;
768 * Clear specific want notification flag
770 * For list of flags see @ref svm_fifo_ntf_t
773 * @param ntf_type type of notification to be cleared
776 svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
778 f->shr->want_deq_ntf &= ~ntf_type;
782 * Clear the want notification flag and set has notification
784 * Should be used after enqueuing an event. This clears the
785 * SVM_FIFO_WANT_NOTIF flag but it does not clear
786 * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is
787 * set to avoid enqueueing events for for all dequeue operations until
788 * it is manually cleared.
793 svm_fifo_clear_deq_ntf (svm_fifo_t * f)
795 /* Set the flag if want_notif_if_full was the only ntf requested */
796 f->shr->has_deq_ntf =
797 f->shr->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL;
798 svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF |
799 SVM_FIFO_WANT_DEQ_NOTIF_IF_LEQ_THRESH);
803 * Clear has notification flag
805 * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL
806 * request and sets has_ntf. To received new events the flag must be
807 * cleared using this function.
812 svm_fifo_reset_has_deq_ntf (svm_fifo_t * f)
814 f->shr->has_deq_ntf = 0;
818 * Check if fifo needs dequeue notification
820 * Determines based on notification request flags and state of the fifo if
821 * an event should be generated.
824 * @param n_last_deq number of bytes last dequeued
825 * @return 1 if event should be generated, 0 otherwise
828 svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq)
830 u8 want_ntf = f->shr->want_deq_ntf;
832 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF))
834 else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF)
836 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL)
838 u32 max_deq = svm_fifo_max_dequeue_cons (f);
839 u32 size = f->shr->size;
840 if (!f->shr->has_deq_ntf && max_deq < size &&
841 max_deq + n_last_deq >= size)
844 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY)
846 if (!f->shr->has_deq_ntf && svm_fifo_is_empty (f))
849 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_LEQ_THRESH)
851 if (!f->shr->has_deq_ntf &&
852 (svm_fifo_max_dequeue (f) <= f->shr->deq_thresh))
859 * Set the fifo dequeue threshold which will be used for notifications.
861 * Note: If not set, by default threshold is zero, equivalent to
865 svm_fifo_set_deq_thresh (svm_fifo_t *f, u32 thresh)
867 f->shr->deq_thresh = thresh;
870 #endif /* __included_ssvm_fifo_h__ */
873 * fd.io coding-style-patch-verification: ON
876 * eval: (c-set-style "gnu")