4 * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Derived from FreeBSD's bufring.h
37 **************************************************************************
39 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions are met:
45 * 1. Redistributions of source code must retain the above copyright notice,
46 * this list of conditions and the following disclaimer.
48 * 2. The name of Kip Macy nor the names of other
49 * contributors may be used to endorse or promote products derived from
50 * this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
53 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
56 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
64 ***************************************************************************/
73 * The Ring Manager is a fixed-size queue, implemented as a table of
74 * pointers. Head and tail pointers are modified atomically, allowing
75 * concurrent access to it. It has the following features:
77 * - FIFO (First In First Out)
78 * - Maximum size is fixed; the pointers are stored in a table.
79 * - Lockless implementation.
80 * - Multi- or single-consumer dequeue.
81 * - Multi- or single-producer enqueue.
85 * Note: the ring implementation is not preemptable. A lcore must not
86 * be interrupted by another task that uses the same ring.
96 #include <sys/queue.h>
98 #include <rte_common.h>
99 #include <rte_memory.h>
100 #include <rte_lcore.h>
101 #include <rte_atomic.h>
102 #include <rte_branch_prediction.h>
103 #include <rte_memzone.h>
104 #include <rte_pause.h>
106 #define RTE_TAILQ_RING_NAME "RTE_RING"
108 enum rte_ring_queue_behavior {
109 RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
110 RTE_RING_QUEUE_VARIABLE /* Enq/Deq as many items as possible from ring */
113 #define RTE_RING_MZ_PREFIX "RG_"
114 /**< The maximum length of a ring name. */
115 #define RTE_RING_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
116 sizeof(RTE_RING_MZ_PREFIX) + 1)
118 struct rte_memzone; /* forward declaration, so as not to require memzone.h */
120 #if RTE_CACHE_LINE_SIZE < 128
121 #define PROD_ALIGN (RTE_CACHE_LINE_SIZE * 2)
122 #define CONS_ALIGN (RTE_CACHE_LINE_SIZE * 2)
124 #define PROD_ALIGN RTE_CACHE_LINE_SIZE
125 #define CONS_ALIGN RTE_CACHE_LINE_SIZE
128 /* structure to hold a pair of head/tail values and other metadata */
129 struct rte_ring_headtail {
130 volatile uint32_t head; /**< Prod/consumer head. */
131 volatile uint32_t tail; /**< Prod/consumer tail. */
132 uint32_t single; /**< True if single prod/cons */
136 * An RTE ring structure.
138 * The producer and the consumer have a head and a tail index. The particularity
139 * of these index is that they are not between 0 and size(ring). These indexes
140 * are between 0 and 2^32, and we mask their value when we access the ring[]
141 * field. Thanks to this assumption, we can do subtractions between 2 index
142 * values in a modulo-32bit base: that's why the overflow of the indexes is not
147 * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
148 * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
149 * next time the ABI changes
151 char name[RTE_MEMZONE_NAMESIZE] __rte_cache_aligned; /**< Name of the ring. */
152 int flags; /**< Flags supplied at creation. */
153 const struct rte_memzone *memzone;
154 /**< Memzone, if any, containing the rte_ring */
155 uint32_t size; /**< Size of ring. */
156 uint32_t mask; /**< Mask (size-1) of ring. */
158 /** Ring producer status. */
159 struct rte_ring_headtail prod __rte_aligned(PROD_ALIGN);
161 /** Ring consumer status. */
162 struct rte_ring_headtail cons __rte_aligned(CONS_ALIGN);
165 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
166 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
167 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
169 /* @internal defines for passing to the enqueue dequeue worker functions */
176 * Calculate the memory size needed for a ring
178 * This function returns the number of bytes needed for a ring, given
179 * the number of elements in it. This value is the sum of the size of
180 * the structure rte_ring and the size of the memory needed by the
181 * objects pointers. The value is aligned to a cache line size.
184 * The number of elements in the ring (must be a power of 2).
186 * - The memory size needed for the ring on success.
187 * - -EINVAL if count is not a power of 2.
189 ssize_t rte_ring_get_memsize(unsigned count);
192 * Initialize a ring structure.
194 * Initialize a ring structure in memory pointed by "r". The size of the
195 * memory area must be large enough to store the ring structure and the
196 * object table. It is advised to use rte_ring_get_memsize() to get the
199 * The ring size is set to *count*, which must be a power of two. Water
200 * marking is disabled by default. The real usable ring size is
201 * *count-1* instead of *count* to differentiate a free ring from an
204 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
205 * memory given by the caller may not be shareable among dpdk
209 * The pointer to the ring structure followed by the objects table.
211 * The name of the ring.
213 * The number of elements in the ring (must be a power of 2).
215 * An OR of the following:
216 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
217 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
218 * is "single-producer". Otherwise, it is "multi-producers".
219 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
220 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
221 * is "single-consumer". Otherwise, it is "multi-consumers".
223 * 0 on success, or a negative value on error.
225 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
229 * Create a new ring named *name* in memory.
231 * This function uses ``memzone_reserve()`` to allocate memory. Then it
232 * calls rte_ring_init() to initialize an empty ring.
234 * The new ring size is set to *count*, which must be a power of
235 * two. Water marking is disabled by default. The real usable ring size
236 * is *count-1* instead of *count* to differentiate a free ring from an
239 * The ring is added in RTE_TAILQ_RING list.
242 * The name of the ring.
244 * The size of the ring (must be a power of 2).
246 * The *socket_id* argument is the socket identifier in case of
247 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
248 * constraint for the reserved zone.
250 * An OR of the following:
251 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
252 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
253 * is "single-producer". Otherwise, it is "multi-producers".
254 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
255 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
256 * is "single-consumer". Otherwise, it is "multi-consumers".
258 * On success, the pointer to the new allocated ring. NULL on error with
259 * rte_errno set appropriately. Possible errno values include:
260 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
261 * - E_RTE_SECONDARY - function was called from a secondary process instance
262 * - EINVAL - count provided is not a power of 2
263 * - ENOSPC - the maximum number of memzones has already been allocated
264 * - EEXIST - a memzone with the same name already exists
265 * - ENOMEM - no appropriate memory area found in which to create memzone
267 struct rte_ring *rte_ring_create(const char *name, unsigned count,
268 int socket_id, unsigned flags);
270 * De-allocate all memory used by the ring.
275 void rte_ring_free(struct rte_ring *r);
278 * Dump the status of the ring to a file.
281 * A pointer to a file for output
283 * A pointer to the ring structure.
285 void rte_ring_dump(FILE *f, const struct rte_ring *r);
287 /* the actual enqueue of pointers on the ring.
288 * Placed here since identical code needed in both
289 * single and multi producer enqueue functions */
290 #define ENQUEUE_PTRS(r, ring_start, prod_head, obj_table, n, obj_type) do { \
292 const uint32_t size = (r)->size; \
293 uint32_t idx = prod_head & (r)->mask; \
294 obj_type *ring = (obj_type *)ring_start; \
295 if (likely(idx + n < size)) { \
296 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
297 ring[idx] = obj_table[i]; \
298 ring[idx+1] = obj_table[i+1]; \
299 ring[idx+2] = obj_table[i+2]; \
300 ring[idx+3] = obj_table[i+3]; \
304 ring[idx++] = obj_table[i++]; /* fallthrough */ \
306 ring[idx++] = obj_table[i++]; /* fallthrough */ \
308 ring[idx++] = obj_table[i++]; \
311 for (i = 0; idx < size; i++, idx++)\
312 ring[idx] = obj_table[i]; \
313 for (idx = 0; i < n; i++, idx++) \
314 ring[idx] = obj_table[i]; \
318 /* the actual copy of pointers on the ring to obj_table.
319 * Placed here since identical code needed in both
320 * single and multi consumer dequeue functions */
321 #define DEQUEUE_PTRS(r, ring_start, cons_head, obj_table, n, obj_type) do { \
323 uint32_t idx = cons_head & (r)->mask; \
324 const uint32_t size = (r)->size; \
325 obj_type *ring = (obj_type *)ring_start; \
326 if (likely(idx + n < size)) { \
327 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
328 obj_table[i] = ring[idx]; \
329 obj_table[i+1] = ring[idx+1]; \
330 obj_table[i+2] = ring[idx+2]; \
331 obj_table[i+3] = ring[idx+3]; \
335 obj_table[i++] = ring[idx++]; /* fallthrough */ \
337 obj_table[i++] = ring[idx++]; /* fallthrough */ \
339 obj_table[i++] = ring[idx++]; \
342 for (i = 0; idx < size; i++, idx++) \
343 obj_table[i] = ring[idx]; \
344 for (idx = 0; i < n; i++, idx++) \
345 obj_table[i] = ring[idx]; \
349 static __rte_always_inline void
350 update_tail(struct rte_ring_headtail *ht, uint32_t old_val, uint32_t new_val,
354 * If there are other enqueues/dequeues in progress that preceded us,
355 * we need to wait for them to complete
358 while (unlikely(ht->tail != old_val))
365 * @internal This function updates the producer head for enqueue
368 * A pointer to the ring structure
370 * Indicates whether multi-producer path is needed or not
372 * The number of elements we will want to enqueue, i.e. how far should the
375 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
376 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
378 * Returns head value as it was before the move, i.e. where enqueue starts
380 * Returns the current/new head value i.e. where enqueue finishes
381 * @param free_entries
382 * Returns the amount of free space in the ring BEFORE head was moved
384 * Actual number of objects enqueued.
385 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
387 static __rte_always_inline unsigned int
388 __rte_ring_move_prod_head(struct rte_ring *r, int is_sp,
389 unsigned int n, enum rte_ring_queue_behavior behavior,
390 uint32_t *old_head, uint32_t *new_head,
391 uint32_t *free_entries)
393 const uint32_t mask = r->mask;
394 unsigned int max = n;
398 /* Reset n to the initial burst count */
401 *old_head = r->prod.head;
402 const uint32_t cons_tail = r->cons.tail;
403 /* The subtraction is done between two unsigned 32bits value
404 * (the result is always modulo 32 bits even if we have
405 * *old_head > cons_tail). So 'free_entries' is always between 0
406 * and size(ring)-1. */
407 *free_entries = (mask + cons_tail - *old_head);
409 /* check that we have enough room in ring */
410 if (unlikely(n > *free_entries))
411 n = (behavior == RTE_RING_QUEUE_FIXED) ?
417 *new_head = *old_head + n;
419 r->prod.head = *new_head, success = 1;
421 success = rte_atomic32_cmpset(&r->prod.head,
422 *old_head, *new_head);
423 } while (unlikely(success == 0));
428 * @internal Enqueue several objects on the ring
431 * A pointer to the ring structure.
433 * A pointer to a table of void * pointers (objects).
435 * The number of objects to add in the ring from the obj_table.
437 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
438 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
440 * Indicates whether to use single producer or multi-producer head update
442 * returns the amount of space after the enqueue operation has finished
444 * Actual number of objects enqueued.
445 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
447 static __rte_always_inline unsigned int
448 __rte_ring_do_enqueue(struct rte_ring *r, void * const *obj_table,
449 unsigned int n, enum rte_ring_queue_behavior behavior,
450 int is_sp, unsigned int *free_space)
452 uint32_t prod_head, prod_next;
453 uint32_t free_entries;
455 n = __rte_ring_move_prod_head(r, is_sp, n, behavior,
456 &prod_head, &prod_next, &free_entries);
460 ENQUEUE_PTRS(r, &r[1], prod_head, obj_table, n, void *);
463 update_tail(&r->prod, prod_head, prod_next, is_sp);
465 if (free_space != NULL)
466 *free_space = free_entries - n;
471 * @internal This function updates the consumer head for dequeue
474 * A pointer to the ring structure
476 * Indicates whether multi-consumer path is needed or not
478 * The number of elements we will want to enqueue, i.e. how far should the
481 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
482 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
484 * Returns head value as it was before the move, i.e. where dequeue starts
486 * Returns the current/new head value i.e. where dequeue finishes
488 * Returns the number of entries in the ring BEFORE head was moved
490 * - Actual number of objects dequeued.
491 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
493 static __rte_always_inline unsigned int
494 __rte_ring_move_cons_head(struct rte_ring *r, int is_sc,
495 unsigned int n, enum rte_ring_queue_behavior behavior,
496 uint32_t *old_head, uint32_t *new_head,
499 unsigned int max = n;
502 /* move cons.head atomically */
504 /* Restore n as it may change every loop */
507 *old_head = r->cons.head;
508 const uint32_t prod_tail = r->prod.tail;
509 /* The subtraction is done between two unsigned 32bits value
510 * (the result is always modulo 32 bits even if we have
511 * cons_head > prod_tail). So 'entries' is always between 0
512 * and size(ring)-1. */
513 *entries = (prod_tail - *old_head);
515 /* Set the actual entries for dequeue */
517 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
519 if (unlikely(n == 0))
522 *new_head = *old_head + n;
524 r->cons.head = *new_head, success = 1;
526 success = rte_atomic32_cmpset(&r->cons.head, *old_head,
528 } while (unlikely(success == 0));
533 * @internal Dequeue several objects from the ring
536 * A pointer to the ring structure.
538 * A pointer to a table of void * pointers (objects).
540 * The number of objects to pull from the ring.
542 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
543 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
545 * Indicates whether to use single consumer or multi-consumer head update
547 * returns the number of remaining ring entries after the dequeue has finished
549 * - Actual number of objects dequeued.
550 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
552 static __rte_always_inline unsigned int
553 __rte_ring_do_dequeue(struct rte_ring *r, void **obj_table,
554 unsigned int n, enum rte_ring_queue_behavior behavior,
555 int is_sc, unsigned int *available)
557 uint32_t cons_head, cons_next;
560 n = __rte_ring_move_cons_head(r, is_sc, n, behavior,
561 &cons_head, &cons_next, &entries);
565 DEQUEUE_PTRS(r, &r[1], cons_head, obj_table, n, void *);
568 update_tail(&r->cons, cons_head, cons_next, is_sc);
571 if (available != NULL)
572 *available = entries - n;
577 * Enqueue several objects on the ring (multi-producers safe).
579 * This function uses a "compare and set" instruction to move the
580 * producer index atomically.
583 * A pointer to the ring structure.
585 * A pointer to a table of void * pointers (objects).
587 * The number of objects to add in the ring from the obj_table.
589 * if non-NULL, returns the amount of space in the ring after the
590 * enqueue operation has finished.
592 * The number of objects enqueued, either 0 or n
594 static __rte_always_inline unsigned int
595 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
596 unsigned int n, unsigned int *free_space)
598 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
599 __IS_MP, free_space);
603 * Enqueue several objects on a ring (NOT multi-producers safe).
606 * A pointer to the ring structure.
608 * A pointer to a table of void * pointers (objects).
610 * The number of objects to add in the ring from the obj_table.
612 * if non-NULL, returns the amount of space in the ring after the
613 * enqueue operation has finished.
615 * The number of objects enqueued, either 0 or n
617 static __rte_always_inline unsigned int
618 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
619 unsigned int n, unsigned int *free_space)
621 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
622 __IS_SP, free_space);
626 * Enqueue several objects on a ring.
628 * This function calls the multi-producer or the single-producer
629 * version depending on the default behavior that was specified at
630 * ring creation time (see flags).
633 * A pointer to the ring structure.
635 * A pointer to a table of void * pointers (objects).
637 * The number of objects to add in the ring from the obj_table.
639 * if non-NULL, returns the amount of space in the ring after the
640 * enqueue operation has finished.
642 * The number of objects enqueued, either 0 or n
644 static __rte_always_inline unsigned int
645 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
646 unsigned int n, unsigned int *free_space)
648 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
649 r->prod.single, free_space);
653 * Enqueue one object on a ring (multi-producers safe).
655 * This function uses a "compare and set" instruction to move the
656 * producer index atomically.
659 * A pointer to the ring structure.
661 * A pointer to the object to be added.
663 * - 0: Success; objects enqueued.
664 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
666 static __rte_always_inline int
667 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
669 return rte_ring_mp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
673 * Enqueue one object on a ring (NOT multi-producers safe).
676 * A pointer to the ring structure.
678 * A pointer to the object to be added.
680 * - 0: Success; objects enqueued.
681 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
683 static __rte_always_inline int
684 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
686 return rte_ring_sp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
690 * Enqueue one object on a ring.
692 * This function calls the multi-producer or the single-producer
693 * version, depending on the default behaviour that was specified at
694 * ring creation time (see flags).
697 * A pointer to the ring structure.
699 * A pointer to the object to be added.
701 * - 0: Success; objects enqueued.
702 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
704 static __rte_always_inline int
705 rte_ring_enqueue(struct rte_ring *r, void *obj)
707 return rte_ring_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
711 * Dequeue several objects from a ring (multi-consumers safe).
713 * This function uses a "compare and set" instruction to move the
714 * consumer index atomically.
717 * A pointer to the ring structure.
719 * A pointer to a table of void * pointers (objects) that will be filled.
721 * The number of objects to dequeue from the ring to the obj_table.
723 * If non-NULL, returns the number of remaining ring entries after the
724 * dequeue has finished.
726 * The number of objects dequeued, either 0 or n
728 static __rte_always_inline unsigned int
729 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table,
730 unsigned int n, unsigned int *available)
732 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
737 * Dequeue several objects from a ring (NOT multi-consumers safe).
740 * A pointer to the ring structure.
742 * A pointer to a table of void * pointers (objects) that will be filled.
744 * The number of objects to dequeue from the ring to the obj_table,
745 * must be strictly positive.
747 * If non-NULL, returns the number of remaining ring entries after the
748 * dequeue has finished.
750 * The number of objects dequeued, either 0 or n
752 static __rte_always_inline unsigned int
753 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
754 unsigned int n, unsigned int *available)
756 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
761 * Dequeue several objects from a ring.
763 * This function calls the multi-consumers or the single-consumer
764 * version, depending on the default behaviour that was specified at
765 * ring creation time (see flags).
768 * A pointer to the ring structure.
770 * A pointer to a table of void * pointers (objects) that will be filled.
772 * The number of objects to dequeue from the ring to the obj_table.
774 * If non-NULL, returns the number of remaining ring entries after the
775 * dequeue has finished.
777 * The number of objects dequeued, either 0 or n
779 static __rte_always_inline unsigned int
780 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
781 unsigned int *available)
783 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
784 r->cons.single, available);
788 * Dequeue one object from a ring (multi-consumers safe).
790 * This function uses a "compare and set" instruction to move the
791 * consumer index atomically.
794 * A pointer to the ring structure.
796 * A pointer to a void * pointer (object) that will be filled.
798 * - 0: Success; objects dequeued.
799 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
802 static __rte_always_inline int
803 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
805 return rte_ring_mc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
809 * Dequeue one object from a ring (NOT multi-consumers safe).
812 * A pointer to the ring structure.
814 * A pointer to a void * pointer (object) that will be filled.
816 * - 0: Success; objects dequeued.
817 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
820 static __rte_always_inline int
821 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
823 return rte_ring_sc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
827 * Dequeue one object from a ring.
829 * This function calls the multi-consumers or the single-consumer
830 * version depending on the default behaviour that was specified at
831 * ring creation time (see flags).
834 * A pointer to the ring structure.
836 * A pointer to a void * pointer (object) that will be filled.
838 * - 0: Success, objects dequeued.
839 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
842 static __rte_always_inline int
843 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
845 return rte_ring_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
849 * Test if a ring is full.
852 * A pointer to the ring structure.
854 * - 1: The ring is full.
855 * - 0: The ring is not full.
858 rte_ring_full(const struct rte_ring *r)
860 uint32_t prod_tail = r->prod.tail;
861 uint32_t cons_tail = r->cons.tail;
862 return ((cons_tail - prod_tail - 1) & r->mask) == 0;
866 * Test if a ring is empty.
869 * A pointer to the ring structure.
871 * - 1: The ring is empty.
872 * - 0: The ring is not empty.
875 rte_ring_empty(const struct rte_ring *r)
877 uint32_t prod_tail = r->prod.tail;
878 uint32_t cons_tail = r->cons.tail;
879 return !!(cons_tail == prod_tail);
883 * Return the number of entries in a ring.
886 * A pointer to the ring structure.
888 * The number of entries in the ring.
890 static inline unsigned
891 rte_ring_count(const struct rte_ring *r)
893 uint32_t prod_tail = r->prod.tail;
894 uint32_t cons_tail = r->cons.tail;
895 return (prod_tail - cons_tail) & r->mask;
899 * Return the number of free entries in a ring.
902 * A pointer to the ring structure.
904 * The number of free entries in the ring.
906 static inline unsigned
907 rte_ring_free_count(const struct rte_ring *r)
909 uint32_t prod_tail = r->prod.tail;
910 uint32_t cons_tail = r->cons.tail;
911 return (cons_tail - prod_tail - 1) & r->mask;
915 * Return the size of the ring.
918 * A pointer to the ring structure.
920 * The number of elements which can be stored in the ring.
922 static inline unsigned int
923 rte_ring_get_size(const struct rte_ring *r)
929 * Dump the status of all rings on the console
932 * A pointer to a file for output
934 void rte_ring_list_dump(FILE *f);
937 * Search a ring from its name
940 * The name of the ring.
942 * The pointer to the ring matching the name, or NULL if not found,
943 * with rte_errno set appropriately. Possible rte_errno values include:
944 * - ENOENT - required entry not available to return.
946 struct rte_ring *rte_ring_lookup(const char *name);
949 * Enqueue several objects on the ring (multi-producers safe).
951 * This function uses a "compare and set" instruction to move the
952 * producer index atomically.
955 * A pointer to the ring structure.
957 * A pointer to a table of void * pointers (objects).
959 * The number of objects to add in the ring from the obj_table.
961 * if non-NULL, returns the amount of space in the ring after the
962 * enqueue operation has finished.
964 * - n: Actual number of objects enqueued.
966 static __rte_always_inline unsigned
967 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
968 unsigned int n, unsigned int *free_space)
970 return __rte_ring_do_enqueue(r, obj_table, n,
971 RTE_RING_QUEUE_VARIABLE, __IS_MP, free_space);
975 * Enqueue several objects on a ring (NOT multi-producers safe).
978 * A pointer to the ring structure.
980 * A pointer to a table of void * pointers (objects).
982 * The number of objects to add in the ring from the obj_table.
984 * if non-NULL, returns the amount of space in the ring after the
985 * enqueue operation has finished.
987 * - n: Actual number of objects enqueued.
989 static __rte_always_inline unsigned
990 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
991 unsigned int n, unsigned int *free_space)
993 return __rte_ring_do_enqueue(r, obj_table, n,
994 RTE_RING_QUEUE_VARIABLE, __IS_SP, free_space);
998 * Enqueue several objects on a ring.
1000 * This function calls the multi-producer or the single-producer
1001 * version depending on the default behavior that was specified at
1002 * ring creation time (see flags).
1005 * A pointer to the ring structure.
1007 * A pointer to a table of void * pointers (objects).
1009 * The number of objects to add in the ring from the obj_table.
1011 * if non-NULL, returns the amount of space in the ring after the
1012 * enqueue operation has finished.
1014 * - n: Actual number of objects enqueued.
1016 static __rte_always_inline unsigned
1017 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1018 unsigned int n, unsigned int *free_space)
1020 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE,
1021 r->prod.single, free_space);
1025 * Dequeue several objects from a ring (multi-consumers safe). When the request
1026 * objects are more than the available objects, only dequeue the actual number
1029 * This function uses a "compare and set" instruction to move the
1030 * consumer index atomically.
1033 * A pointer to the ring structure.
1035 * A pointer to a table of void * pointers (objects) that will be filled.
1037 * The number of objects to dequeue from the ring to the obj_table.
1039 * If non-NULL, returns the number of remaining ring entries after the
1040 * dequeue has finished.
1042 * - n: Actual number of objects dequeued, 0 if ring is empty
1044 static __rte_always_inline unsigned
1045 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
1046 unsigned int n, unsigned int *available)
1048 return __rte_ring_do_dequeue(r, obj_table, n,
1049 RTE_RING_QUEUE_VARIABLE, __IS_MC, available);
1053 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1054 * request objects are more than the available objects, only dequeue the
1055 * actual number of objects
1058 * A pointer to the ring structure.
1060 * A pointer to a table of void * pointers (objects) that will be filled.
1062 * The number of objects to dequeue from the ring to the obj_table.
1064 * If non-NULL, returns the number of remaining ring entries after the
1065 * dequeue has finished.
1067 * - n: Actual number of objects dequeued, 0 if ring is empty
1069 static __rte_always_inline unsigned
1070 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
1071 unsigned int n, unsigned int *available)
1073 return __rte_ring_do_dequeue(r, obj_table, n,
1074 RTE_RING_QUEUE_VARIABLE, __IS_SC, available);
1078 * Dequeue multiple objects from a ring up to a maximum number.
1080 * This function calls the multi-consumers or the single-consumer
1081 * version, depending on the default behaviour that was specified at
1082 * ring creation time (see flags).
1085 * A pointer to the ring structure.
1087 * A pointer to a table of void * pointers (objects) that will be filled.
1089 * The number of objects to dequeue from the ring to the obj_table.
1091 * If non-NULL, returns the number of remaining ring entries after the
1092 * dequeue has finished.
1094 * - Number of objects dequeued
1096 static __rte_always_inline unsigned
1097 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
1098 unsigned int n, unsigned int *available)
1100 return __rte_ring_do_dequeue(r, obj_table, n,
1101 RTE_RING_QUEUE_VARIABLE,
1102 r->cons.single, available);
1109 #endif /* _RTE_RING_H_ */