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->shr->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->shr->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->shr->tail);
111 /* load-acq : producer foriegn index (paired with store-rel) */
112 *head = clib_atomic_load_acq_n (&f->shr->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->shr->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));
174 always_inline svm_fifo_chunk_t *
175 f_start_cptr (svm_fifo_t *f)
177 return fs_chunk_ptr (f->fs_hdr, f->shr->start_chunk);
180 always_inline svm_fifo_chunk_t *
181 f_end_cptr (svm_fifo_t *f)
183 return fs_chunk_ptr (f->fs_hdr, f->shr->end_chunk);
186 always_inline svm_fifo_chunk_t *
187 f_head_cptr (svm_fifo_t *f)
189 return fs_chunk_ptr (f->fs_hdr, f->shr->head_chunk);
192 always_inline svm_fifo_chunk_t *
193 f_tail_cptr (svm_fifo_t *f)
195 return fs_chunk_ptr (f->fs_hdr, f->shr->tail_chunk);
198 always_inline svm_fifo_chunk_t *
199 f_cptr (svm_fifo_t *f, fs_sptr_t cp)
201 return fs_chunk_ptr (f->fs_hdr, cp);
204 always_inline fs_sptr_t
205 f_csptr (svm_fifo_t *f, svm_fifo_chunk_t *c)
207 return fs_chunk_sptr (f->fs_hdr, c);
211 f_csptr_link (svm_fifo_t *f, fs_sptr_t cp, svm_fifo_chunk_t *c)
213 fs_chunk_ptr (f->fs_hdr, cp)->next = fs_chunk_sptr (f->fs_hdr, c);
217 * Create fifo of requested size
219 * Allocates fifo on current heap.
221 * @param size data size in bytes for fifo to be allocated. Will be
222 * rounded to the next highest power-of-two value.
223 * @return pointer to new fifo
225 svm_fifo_t *svm_fifo_alloc (u32 size);
230 * @param size size for fifo
232 void svm_fifo_init (svm_fifo_t * f, u32 size);
234 * Allocate a fifo chunk on heap
236 * If the chunk is allocated on a fifo segment, this should be called
237 * with the segment's heap pushed.
239 * @param size chunk size in bytes. Will be rounded to the next highest
241 * @return new chunk or 0 if alloc failed
243 svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size);
245 * Ensure the whole fifo size is writeable
247 * Allocates enough chunks to cover the whole fifo size.
251 int svm_fifo_fill_chunk_list (svm_fifo_t * f);
253 * Provision and return chunks for number of bytes requested
255 * Allocates enough chunks to cover the bytes requested and returns them
256 * in the fifo segment array. The number of bytes provisioned may be less
257 * than requested if not enough segments were provided.
260 * @param fs array of fifo segments
261 * @param n_segs length of fifo segments array
262 * @param len number of bytes to preallocate
263 * @return number of fifo segments provisioned or error
265 int svm_fifo_provision_chunks (svm_fifo_t *f, svm_fifo_seg_t *fs, u32 n_segs,
268 * Initialize rbtrees used for ooo lookups
271 * @param ooo_type type of ooo operation (0 enqueue, 1 dequeue)
273 void svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type);
275 * Free fifo and associated state
279 void svm_fifo_free (svm_fifo_t * f);
281 * Cleanup fifo chunk lookup rb tree
283 * The rb tree is allocated in segment heap so this should be called
286 * @param f fifo to cleanup
288 void svm_fifo_free_chunk_lookup (svm_fifo_t * f);
290 * Cleanup fifo ooo data
292 * The ooo data is allocated in producer process memory. The fifo
293 * segment heap should not be pushed.
295 * @param f fifo to cleanup
297 void svm_fifo_free_ooo_data (svm_fifo_t * f);
299 * Init fifo head and tail
302 * @param head head value that will be matched to a chunk
303 * @param tail tail value that will be matched to a chunk
305 void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail);
309 * Clones single/default chunk fifo. It does not work for fifos with
312 void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf);
314 * Enqueue data to fifo
316 * Data is enqueued and tail pointer is updated atomically. If the new data
317 * enqueued partly overlaps or "touches" an out-of-order segment, said segment
318 * is "consumed" and the number of bytes returned is appropriately updated.
321 * @param len length of data to copy
322 * @param src buffer from where to copy the data
323 * @return number of contiguous bytes that can be consumed or error
325 int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src);
327 * Enqueue data to fifo with offset
329 * Data is enqueued without updating tail pointer. Instead, an out-of-order
330 * list of segments is generated and maintained. Fifo takes care of coalescing
331 * contiguous or overlapping segments.
334 * @param offset offset at which to copy the data
335 * @param len len of data to copy
336 * @param src buffer from where to copy the data
337 * @return 0 if enqueue was successful, error otherwise
339 int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len,
343 * Advance tail pointer
345 * Useful for moving tail pointer after external enqueue.
348 * @param len number of bytes to add to tail
350 void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len);
352 * Enqueue array of @ref svm_fifo_seg_t in order
355 * @param segs array of segments to enqueue
356 * @param n_segs number of segments
357 * @param allow_partial if set partial enqueues are allowed
358 * @return len if enqueue was successful, error otherwise
360 int svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[],
361 u32 n_segs, u8 allow_partial);
363 * Overwrite fifo head with new data
365 * This should be typically used by dgram transport protocols that need
366 * to update the dgram header after dequeuing a chunk of data. It assumes
367 * that the dgram header is at most spread over two chunks.
370 * @param src src of new data
371 * @param len length of new data
373 void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len);
375 * Dequeue data from fifo
377 * Data is dequeued to consumer provided buffer and head is atomically
378 * updated. This should not be used in combination with ooo lookups. If
379 * ooo peeking of data is needed in combination with dequeuing use @ref
380 * svm_fifo_dequeue_drop.
383 * @param len length of data to dequeue
384 * @param dst buffer to where to dequeue the data
385 * @return number of bytes dequeued or error
387 int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst);
389 * Peek data from fifo
391 * Data is copied from requested offset into provided dst buffer. Head is
395 * @param offset offset from which to copy the data
396 * @param len length of data to copy
397 * @param dst buffer to where to dequeue the data
398 * @return number of bytes peeked
400 int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst);
402 * Dequeue and drop bytes from fifo
404 * Advances fifo head by requested amount of bytes.
407 * @param len number of bytes to drop
408 * @return number of bytes dropped
410 int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len);
412 * Dequeue and drop all bytes from fifo
414 * Advances head to tail position.
418 void svm_fifo_dequeue_drop_all (svm_fifo_t * f);
420 * Get pointers to fifo chunks data in @ref svm_fifo_seg_t array
422 * Populates fifo segment array with pointers to fifo chunk data and lengths.
423 * Because this returns pointers to data, it must be paired with
424 * @ref svm_fifo_dequeue_drop to actually release the fifo chunks after the
428 * @param offset offset from where to retrieve segments
429 * @param fs array of fifo segments allocated by caller
430 * @param n_segs number of fifo segments in array
431 * @param max_bytes max bytes to be mapped to fifo segments
432 * @return number of bytes in fifo segments or SVM_FIFO_EEMPTY
434 int svm_fifo_segments (svm_fifo_t *f, u32 offset, svm_fifo_seg_t *fs,
435 u32 *n_segs, u32 max_bytes);
437 * Add io events subscriber to list
440 * @param sub subscriber opaque index (typically app worker index)
442 void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub);
444 * Remove io events subscriber form list
447 * @param sub subscriber index to be removed
449 void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber);
451 * Number of out-of-order segments for fifo
454 * @return number of out of order segments
456 u32 svm_fifo_n_ooo_segments (svm_fifo_t * f);
458 * First out-of-order segment for fifo
461 * @return first out-of-order segment for fifo
463 ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f);
465 * Check if fifo is sane. Debug only.
468 * @return 1 if sane, 0 otherwise
470 u8 svm_fifo_is_sane (svm_fifo_t * f);
472 * Number of chunks linked into the fifo
475 * @return number of chunks in fifo linked list
477 u32 svm_fifo_n_chunks (svm_fifo_t * f);
478 format_function_t format_svm_fifo;
481 * Fifo max bytes to dequeue optimized for consumer
484 * @return max number of bytes that can be dequeued
487 svm_fifo_max_dequeue_cons (svm_fifo_t * f)
490 f_load_head_tail_cons (f, &head, &tail);
491 return f_cursize (f, head, tail);
495 * Fifo max bytes to dequeue optimized for producer
498 * @return max number of bytes that can be dequeued
501 svm_fifo_max_dequeue_prod (svm_fifo_t * f)
504 f_load_head_tail_prod (f, &head, &tail);
505 return f_cursize (f, head, tail);
509 * Fifo max bytes to dequeue
511 * Note: use producer or consumer specific functions for performance:
512 * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f)
513 * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f)
516 svm_fifo_max_dequeue (svm_fifo_t * f)
519 f_load_head_tail_all_acq (f, &head, &tail);
520 return f_cursize (f, head, tail);
524 * Check if fifo is full optimized for producer
527 * @return 1 if fifo is full 0 otherwise
530 svm_fifo_is_full_prod (svm_fifo_t * f)
532 return (svm_fifo_max_dequeue_prod (f) == f->shr->size);
535 /* Check if fifo is full.
537 * Note: use producer or consumer specific functions for performance.
538 * @ref svm_fifo_is_full_prod (svm_fifo_t * f)
539 * add cons version if needed
542 svm_fifo_is_full (svm_fifo_t * f)
544 return (svm_fifo_max_dequeue (f) == f->shr->size);
548 * Check if fifo is empty optimized for consumer
551 * @return 1 if fifo is empty 0 otherwise
554 svm_fifo_is_empty_cons (svm_fifo_t * f)
556 return (svm_fifo_max_dequeue_cons (f) == 0);
560 * Check if fifo is empty optimized for producer
563 * @return 1 if fifo is empty 0 otherwise
566 svm_fifo_is_empty_prod (svm_fifo_t * f)
568 return (svm_fifo_max_dequeue_prod (f) == 0);
572 * Check if fifo is empty
574 * Note: use producer or consumer specific functions for perfomance.
575 * @ref svm_fifo_is_empty_cons (svm_fifo_t * f)
576 * @ref svm_fifo_is_empty_prod (svm_fifo_t * f)
579 svm_fifo_is_empty (svm_fifo_t * f)
581 return (svm_fifo_max_dequeue (f) == 0);
585 * Check if fifo is wrapped
588 * @return 1 if 'normalized' head is ahead of tail
591 svm_fifo_is_wrapped (svm_fifo_t * f)
594 f_load_head_tail_all_acq (f, &head, &tail);
599 * Maximum number of bytes that can be enqueued into fifo
601 * Optimized for producer
604 * @return max number of bytes that can be enqueued into fifo
607 svm_fifo_max_enqueue_prod (svm_fifo_t * f)
610 f_load_head_tail_prod (f, &head, &tail);
611 return f_free_count (f, head, tail);
614 /* Maximum number of bytes that can be enqueued into fifo
616 * Note: use producer or consumer specific functions for performance.
617 * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f)
618 * add consumer specific version if needed.
621 svm_fifo_max_enqueue (svm_fifo_t * f)
624 f_load_head_tail_all_acq (f, &head, &tail);
625 return f_free_count (f, head, tail);
629 * Max contiguous chunk of data that can be read.
631 * Should only be called by consumers.
633 u32 svm_fifo_max_read_chunk (svm_fifo_t * f);
636 * Max contiguous chunk of data that can be written
638 * Should only be called by producers
640 u32 svm_fifo_max_write_chunk (svm_fifo_t * f);
643 * Fifo number of subscribers getter
646 * @return number of subscribers
649 svm_fifo_n_subscribers (svm_fifo_t * f)
651 return f->shr->n_subscribers;
655 * Check if fifo has out-of-order data
658 * @return 1 if fifo has ooo data, 0 otherwise
661 svm_fifo_has_ooo_data (svm_fifo_t * f)
663 return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX;
666 static inline ooo_segment_t *
667 svm_fifo_newest_ooo_segment (svm_fifo_t * f)
669 if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX)
671 return pool_elt_at_index (f->ooo_segments, f->ooos_newest);
675 svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f)
677 f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
681 ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s)
684 /* load-relaxed: producer owned index */
687 return (s->start - tail);
691 ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s)
697 svm_fifo_size (svm_fifo_t * f)
703 svm_fifo_set_size (svm_fifo_t * f, u32 size)
705 if (size > (1 << f->fs_hdr->max_log2_fifo_size))
707 fsh_virtual_mem_update (f->fs_hdr, f->shr->slice_index,
708 (int) f->shr->size - size);
713 * Check if fifo has io event
716 * @return 1 if fifo has event, 0 otherwise
719 svm_fifo_has_event (svm_fifo_t * f)
721 return f->shr->has_event;
725 * Set fifo event flag.
727 * Forces release semantics.
730 * @return 1 if flag was not set, 0 otherwise
733 svm_fifo_set_event (svm_fifo_t * f)
735 return !clib_atomic_swap_rel_n (&f->shr->has_event, 1);
739 * Unset fifo event flag.
741 * Forces acquire semantics
746 svm_fifo_unset_event (svm_fifo_t * f)
748 clib_atomic_swap_acq_n (&f->shr->has_event, 0);
752 * Set specific want notification flag
754 * For list of flags see @ref svm_fifo_deq_ntf_t
757 * @param ntf_type type of notification requested
760 svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
762 f->shr->want_deq_ntf |= ntf_type;
766 * Clear specific want notification flag
768 * For list of flags see @ref svm_fifo_ntf_t
771 * @param ntf_type type of notification to be cleared
774 svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type)
776 f->shr->want_deq_ntf &= ~ntf_type;
780 * Clear the want notification flag and set has notification
782 * Should be used after enqueuing an event. This clears the
783 * SVM_FIFO_WANT_NOTIF flag but it does not clear
784 * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is
785 * set to avoid enqueueing events for for all dequeue operations until
786 * it is manually cleared.
791 svm_fifo_clear_deq_ntf (svm_fifo_t * f)
793 /* Set the flag if want_notif_if_full was the only ntf requested */
794 f->shr->has_deq_ntf =
795 f->shr->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL;
796 svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF);
800 * Clear has notification flag
802 * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL
803 * request and sets has_ntf. To received new events the flag must be
804 * cleared using this function.
809 svm_fifo_reset_has_deq_ntf (svm_fifo_t * f)
811 f->shr->has_deq_ntf = 0;
815 * Check if fifo needs dequeue notification
817 * Determines based on notification request flags and state of the fifo if
818 * an event should be generated.
821 * @param n_last_deq number of bytes last dequeued
822 * @return 1 if event should be generated, 0 otherwise
825 svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq)
827 u8 want_ntf = f->shr->want_deq_ntf;
829 if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF))
831 else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF)
832 return (svm_fifo_max_enqueue (f) >= f->shr->deq_thresh);
833 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL)
835 u32 max_deq = svm_fifo_max_dequeue_cons (f);
836 u32 size = f->shr->size;
837 if (!f->shr->has_deq_ntf && max_deq < size &&
838 max_deq + n_last_deq >= size)
841 if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY)
843 if (!f->shr->has_deq_ntf && svm_fifo_is_empty (f))
850 * Set the fifo dequeue threshold which will be used for notifications.
852 * Note: If not set, by default threshold is zero, equivalent to
853 * generating notification on each dequeue event.
856 svm_fifo_set_deq_thresh (svm_fifo_t *f, u32 thresh)
858 f->shr->deq_thresh = thresh;
861 #endif /* __included_ssvm_fifo_h__ */
864 * fd.io coding-style-patch-verification: ON
867 * eval: (c-set-style "gnu")