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. */
157 uint32_t capacity; /**< Usable size of ring */
159 /** Ring producer status. */
160 struct rte_ring_headtail prod __rte_aligned(PROD_ALIGN);
162 /** Ring consumer status. */
163 struct rte_ring_headtail cons __rte_aligned(CONS_ALIGN);
166 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
167 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
169 * Ring is to hold exactly requested number of entries.
170 * Without this flag set, the ring size requested must be a power of 2, and the
171 * usable space will be that size - 1. With the flag, the requested size will
172 * be rounded up to the next power of two, but the usable space will be exactly
173 * that requested. Worst case, if a power-of-2 size is requested, half the
174 * ring space will be wasted.
176 #define RING_F_EXACT_SZ 0x0004
177 #define RTE_RING_SZ_MASK (0x7fffffffU) /**< Ring size mask */
179 /* @internal defines for passing to the enqueue dequeue worker functions */
186 * Calculate the memory size needed for a ring
188 * This function returns the number of bytes needed for a ring, given
189 * the number of elements in it. This value is the sum of the size of
190 * the structure rte_ring and the size of the memory needed by the
191 * objects pointers. The value is aligned to a cache line size.
194 * The number of elements in the ring (must be a power of 2).
196 * - The memory size needed for the ring on success.
197 * - -EINVAL if count is not a power of 2.
199 ssize_t rte_ring_get_memsize(unsigned count);
202 * Initialize a ring structure.
204 * Initialize a ring structure in memory pointed by "r". The size of the
205 * memory area must be large enough to store the ring structure and the
206 * object table. It is advised to use rte_ring_get_memsize() to get the
209 * The ring size is set to *count*, which must be a power of two. Water
210 * marking is disabled by default. The real usable ring size is
211 * *count-1* instead of *count* to differentiate a free ring from an
214 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
215 * memory given by the caller may not be shareable among dpdk
219 * The pointer to the ring structure followed by the objects table.
221 * The name of the ring.
223 * The number of elements in the ring (must be a power of 2).
225 * An OR of the following:
226 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
227 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
228 * is "single-producer". Otherwise, it is "multi-producers".
229 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
230 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
231 * is "single-consumer". Otherwise, it is "multi-consumers".
233 * 0 on success, or a negative value on error.
235 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
239 * Create a new ring named *name* in memory.
241 * This function uses ``memzone_reserve()`` to allocate memory. Then it
242 * calls rte_ring_init() to initialize an empty ring.
244 * The new ring size is set to *count*, which must be a power of
245 * two. Water marking is disabled by default. The real usable ring size
246 * is *count-1* instead of *count* to differentiate a free ring from an
249 * The ring is added in RTE_TAILQ_RING list.
252 * The name of the ring.
254 * The size of the ring (must be a power of 2).
256 * The *socket_id* argument is the socket identifier in case of
257 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
258 * constraint for the reserved zone.
260 * An OR of the following:
261 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
262 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
263 * is "single-producer". Otherwise, it is "multi-producers".
264 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
265 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
266 * is "single-consumer". Otherwise, it is "multi-consumers".
268 * On success, the pointer to the new allocated ring. NULL on error with
269 * rte_errno set appropriately. Possible errno values include:
270 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
271 * - E_RTE_SECONDARY - function was called from a secondary process instance
272 * - EINVAL - count provided is not a power of 2
273 * - ENOSPC - the maximum number of memzones has already been allocated
274 * - EEXIST - a memzone with the same name already exists
275 * - ENOMEM - no appropriate memory area found in which to create memzone
277 struct rte_ring *rte_ring_create(const char *name, unsigned count,
278 int socket_id, unsigned flags);
280 * De-allocate all memory used by the ring.
285 void rte_ring_free(struct rte_ring *r);
288 * Dump the status of the ring to a file.
291 * A pointer to a file for output
293 * A pointer to the ring structure.
295 void rte_ring_dump(FILE *f, const struct rte_ring *r);
297 /* the actual enqueue of pointers on the ring.
298 * Placed here since identical code needed in both
299 * single and multi producer enqueue functions */
300 #define ENQUEUE_PTRS(r, ring_start, prod_head, obj_table, n, obj_type) do { \
302 const uint32_t size = (r)->size; \
303 uint32_t idx = prod_head & (r)->mask; \
304 obj_type *ring = (obj_type *)ring_start; \
305 if (likely(idx + n < size)) { \
306 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
307 ring[idx] = obj_table[i]; \
308 ring[idx+1] = obj_table[i+1]; \
309 ring[idx+2] = obj_table[i+2]; \
310 ring[idx+3] = obj_table[i+3]; \
314 ring[idx++] = obj_table[i++]; /* fallthrough */ \
316 ring[idx++] = obj_table[i++]; /* fallthrough */ \
318 ring[idx++] = obj_table[i++]; \
321 for (i = 0; idx < size; i++, idx++)\
322 ring[idx] = obj_table[i]; \
323 for (idx = 0; i < n; i++, idx++) \
324 ring[idx] = obj_table[i]; \
328 /* the actual copy of pointers on the ring to obj_table.
329 * Placed here since identical code needed in both
330 * single and multi consumer dequeue functions */
331 #define DEQUEUE_PTRS(r, ring_start, cons_head, obj_table, n, obj_type) do { \
333 uint32_t idx = cons_head & (r)->mask; \
334 const uint32_t size = (r)->size; \
335 obj_type *ring = (obj_type *)ring_start; \
336 if (likely(idx + n < size)) { \
337 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
338 obj_table[i] = ring[idx]; \
339 obj_table[i+1] = ring[idx+1]; \
340 obj_table[i+2] = ring[idx+2]; \
341 obj_table[i+3] = ring[idx+3]; \
345 obj_table[i++] = ring[idx++]; /* fallthrough */ \
347 obj_table[i++] = ring[idx++]; /* fallthrough */ \
349 obj_table[i++] = ring[idx++]; \
352 for (i = 0; idx < size; i++, idx++) \
353 obj_table[i] = ring[idx]; \
354 for (idx = 0; i < n; i++, idx++) \
355 obj_table[i] = ring[idx]; \
359 static __rte_always_inline void
360 update_tail(struct rte_ring_headtail *ht, uint32_t old_val, uint32_t new_val,
364 * If there are other enqueues/dequeues in progress that preceded us,
365 * we need to wait for them to complete
368 while (unlikely(ht->tail != old_val))
375 * @internal This function updates the producer head for enqueue
378 * A pointer to the ring structure
380 * Indicates whether multi-producer path is needed or not
382 * The number of elements we will want to enqueue, i.e. how far should the
385 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
386 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
388 * Returns head value as it was before the move, i.e. where enqueue starts
390 * Returns the current/new head value i.e. where enqueue finishes
391 * @param free_entries
392 * Returns the amount of free space in the ring BEFORE head was moved
394 * Actual number of objects enqueued.
395 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
397 static __rte_always_inline unsigned int
398 __rte_ring_move_prod_head(struct rte_ring *r, int is_sp,
399 unsigned int n, enum rte_ring_queue_behavior behavior,
400 uint32_t *old_head, uint32_t *new_head,
401 uint32_t *free_entries)
403 const uint32_t capacity = r->capacity;
404 unsigned int max = n;
408 /* Reset n to the initial burst count */
411 *old_head = r->prod.head;
413 /* add rmb barrier to avoid load/load reorder in weak
414 * memory model. It is noop on x86
418 const uint32_t cons_tail = r->cons.tail;
420 * The subtraction is done between two unsigned 32bits value
421 * (the result is always modulo 32 bits even if we have
422 * *old_head > cons_tail). So 'free_entries' is always between 0
423 * and capacity (which is < size).
425 *free_entries = (capacity + cons_tail - *old_head);
427 /* check that we have enough room in ring */
428 if (unlikely(n > *free_entries))
429 n = (behavior == RTE_RING_QUEUE_FIXED) ?
435 *new_head = *old_head + n;
437 r->prod.head = *new_head, success = 1;
439 success = rte_atomic32_cmpset(&r->prod.head,
440 *old_head, *new_head);
441 } while (unlikely(success == 0));
446 * @internal Enqueue several objects on the ring
449 * A pointer to the ring structure.
451 * A pointer to a table of void * pointers (objects).
453 * The number of objects to add in the ring from the obj_table.
455 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
456 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
458 * Indicates whether to use single producer or multi-producer head update
460 * returns the amount of space after the enqueue operation has finished
462 * Actual number of objects enqueued.
463 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
465 static __rte_always_inline unsigned int
466 __rte_ring_do_enqueue(struct rte_ring *r, void * const *obj_table,
467 unsigned int n, enum rte_ring_queue_behavior behavior,
468 int is_sp, unsigned int *free_space)
470 uint32_t prod_head, prod_next;
471 uint32_t free_entries;
473 n = __rte_ring_move_prod_head(r, is_sp, n, behavior,
474 &prod_head, &prod_next, &free_entries);
478 ENQUEUE_PTRS(r, &r[1], prod_head, obj_table, n, void *);
481 update_tail(&r->prod, prod_head, prod_next, is_sp);
483 if (free_space != NULL)
484 *free_space = free_entries - n;
489 * @internal This function updates the consumer head for dequeue
492 * A pointer to the ring structure
494 * Indicates whether multi-consumer path is needed or not
496 * The number of elements we will want to enqueue, i.e. how far should the
499 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
500 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
502 * Returns head value as it was before the move, i.e. where dequeue starts
504 * Returns the current/new head value i.e. where dequeue finishes
506 * Returns the number of entries in the ring BEFORE head was moved
508 * - Actual number of objects dequeued.
509 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
511 static __rte_always_inline unsigned int
512 __rte_ring_move_cons_head(struct rte_ring *r, int is_sc,
513 unsigned int n, enum rte_ring_queue_behavior behavior,
514 uint32_t *old_head, uint32_t *new_head,
517 unsigned int max = n;
520 /* move cons.head atomically */
522 /* Restore n as it may change every loop */
525 *old_head = r->cons.head;
527 /* add rmb barrier to avoid load/load reorder in weak
528 * memory model. It is noop on x86
532 const uint32_t prod_tail = r->prod.tail;
533 /* The subtraction is done between two unsigned 32bits value
534 * (the result is always modulo 32 bits even if we have
535 * cons_head > prod_tail). So 'entries' is always between 0
536 * and size(ring)-1. */
537 *entries = (prod_tail - *old_head);
539 /* Set the actual entries for dequeue */
541 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
543 if (unlikely(n == 0))
546 *new_head = *old_head + n;
548 r->cons.head = *new_head, success = 1;
550 success = rte_atomic32_cmpset(&r->cons.head, *old_head,
552 } while (unlikely(success == 0));
557 * @internal Dequeue several objects from the ring
560 * A pointer to the ring structure.
562 * A pointer to a table of void * pointers (objects).
564 * The number of objects to pull from the ring.
566 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
567 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
569 * Indicates whether to use single consumer or multi-consumer head update
571 * returns the number of remaining ring entries after the dequeue has finished
573 * - Actual number of objects dequeued.
574 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
576 static __rte_always_inline unsigned int
577 __rte_ring_do_dequeue(struct rte_ring *r, void **obj_table,
578 unsigned int n, enum rte_ring_queue_behavior behavior,
579 int is_sc, unsigned int *available)
581 uint32_t cons_head, cons_next;
584 n = __rte_ring_move_cons_head(r, is_sc, n, behavior,
585 &cons_head, &cons_next, &entries);
589 DEQUEUE_PTRS(r, &r[1], cons_head, obj_table, n, void *);
592 update_tail(&r->cons, cons_head, cons_next, is_sc);
595 if (available != NULL)
596 *available = entries - n;
601 * Enqueue several objects on the ring (multi-producers safe).
603 * This function uses a "compare and set" instruction to move the
604 * producer index atomically.
607 * A pointer to the ring structure.
609 * A pointer to a table of void * pointers (objects).
611 * The number of objects to add in the ring from the obj_table.
613 * if non-NULL, returns the amount of space in the ring after the
614 * enqueue operation has finished.
616 * The number of objects enqueued, either 0 or n
618 static __rte_always_inline unsigned int
619 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
620 unsigned int n, unsigned int *free_space)
622 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
623 __IS_MP, free_space);
627 * Enqueue several objects on a ring (NOT multi-producers safe).
630 * A pointer to the ring structure.
632 * A pointer to a table of void * pointers (objects).
634 * The number of objects to add in the ring from the obj_table.
636 * if non-NULL, returns the amount of space in the ring after the
637 * enqueue operation has finished.
639 * The number of objects enqueued, either 0 or n
641 static __rte_always_inline unsigned int
642 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
643 unsigned int n, unsigned int *free_space)
645 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
646 __IS_SP, free_space);
650 * Enqueue several objects on a ring.
652 * This function calls the multi-producer or the single-producer
653 * version depending on the default behavior that was specified at
654 * ring creation time (see flags).
657 * A pointer to the ring structure.
659 * A pointer to a table of void * pointers (objects).
661 * The number of objects to add in the ring from the obj_table.
663 * if non-NULL, returns the amount of space in the ring after the
664 * enqueue operation has finished.
666 * The number of objects enqueued, either 0 or n
668 static __rte_always_inline unsigned int
669 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
670 unsigned int n, unsigned int *free_space)
672 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
673 r->prod.single, free_space);
677 * Enqueue one object on a ring (multi-producers safe).
679 * This function uses a "compare and set" instruction to move the
680 * producer index atomically.
683 * A pointer to the ring structure.
685 * A pointer to the object to be added.
687 * - 0: Success; objects enqueued.
688 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
690 static __rte_always_inline int
691 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
693 return rte_ring_mp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
697 * Enqueue one object on a ring (NOT multi-producers safe).
700 * A pointer to the ring structure.
702 * A pointer to the object to be added.
704 * - 0: Success; objects enqueued.
705 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
707 static __rte_always_inline int
708 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
710 return rte_ring_sp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
714 * Enqueue one object on a ring.
716 * This function calls the multi-producer or the single-producer
717 * version, depending on the default behaviour that was specified at
718 * ring creation time (see flags).
721 * A pointer to the ring structure.
723 * A pointer to the object to be added.
725 * - 0: Success; objects enqueued.
726 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
728 static __rte_always_inline int
729 rte_ring_enqueue(struct rte_ring *r, void *obj)
731 return rte_ring_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
735 * Dequeue several objects from a ring (multi-consumers safe).
737 * This function uses a "compare and set" instruction to move the
738 * consumer index atomically.
741 * A pointer to the ring structure.
743 * A pointer to a table of void * pointers (objects) that will be filled.
745 * The number of objects to dequeue from the ring to the obj_table.
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_mc_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 (NOT multi-consumers safe).
764 * A pointer to the ring structure.
766 * A pointer to a table of void * pointers (objects) that will be filled.
768 * The number of objects to dequeue from the ring to the obj_table,
769 * must be strictly positive.
771 * If non-NULL, returns the number of remaining ring entries after the
772 * dequeue has finished.
774 * The number of objects dequeued, either 0 or n
776 static __rte_always_inline unsigned int
777 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
778 unsigned int n, unsigned int *available)
780 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
785 * Dequeue several objects from a ring.
787 * This function calls the multi-consumers or the single-consumer
788 * version, depending on the default behaviour that was specified at
789 * ring creation time (see flags).
792 * A pointer to the ring structure.
794 * A pointer to a table of void * pointers (objects) that will be filled.
796 * The number of objects to dequeue from the ring to the obj_table.
798 * If non-NULL, returns the number of remaining ring entries after the
799 * dequeue has finished.
801 * The number of objects dequeued, either 0 or n
803 static __rte_always_inline unsigned int
804 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
805 unsigned int *available)
807 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
808 r->cons.single, available);
812 * Dequeue one object from a ring (multi-consumers safe).
814 * This function uses a "compare and set" instruction to move the
815 * consumer index atomically.
818 * A pointer to the ring structure.
820 * A pointer to a void * pointer (object) that will be filled.
822 * - 0: Success; objects dequeued.
823 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
826 static __rte_always_inline int
827 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
829 return rte_ring_mc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
833 * Dequeue one object from a ring (NOT multi-consumers safe).
836 * A pointer to the ring structure.
838 * A pointer to a void * pointer (object) that will be filled.
840 * - 0: Success; objects dequeued.
841 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
844 static __rte_always_inline int
845 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
847 return rte_ring_sc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
851 * Dequeue one object from a ring.
853 * This function calls the multi-consumers or the single-consumer
854 * version depending on the default behaviour that was specified at
855 * ring creation time (see flags).
858 * A pointer to the ring structure.
860 * A pointer to a void * pointer (object) that will be filled.
862 * - 0: Success, objects dequeued.
863 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
866 static __rte_always_inline int
867 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
869 return rte_ring_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
873 * Return the number of entries in a ring.
876 * A pointer to the ring structure.
878 * The number of entries in the ring.
880 static inline unsigned
881 rte_ring_count(const struct rte_ring *r)
883 uint32_t prod_tail = r->prod.tail;
884 uint32_t cons_tail = r->cons.tail;
885 uint32_t count = (prod_tail - cons_tail) & r->mask;
886 return (count > r->capacity) ? r->capacity : count;
890 * Return the number of free entries in a ring.
893 * A pointer to the ring structure.
895 * The number of free entries in the ring.
897 static inline unsigned
898 rte_ring_free_count(const struct rte_ring *r)
900 return r->capacity - rte_ring_count(r);
904 * Test if a ring is full.
907 * A pointer to the ring structure.
909 * - 1: The ring is full.
910 * - 0: The ring is not full.
913 rte_ring_full(const struct rte_ring *r)
915 return rte_ring_free_count(r) == 0;
919 * Test if a ring is empty.
922 * A pointer to the ring structure.
924 * - 1: The ring is empty.
925 * - 0: The ring is not empty.
928 rte_ring_empty(const struct rte_ring *r)
930 return rte_ring_count(r) == 0;
934 * Return the size of the ring.
937 * A pointer to the ring structure.
939 * The size of the data store used by the ring.
940 * NOTE: this is not the same as the usable space in the ring. To query that
941 * use ``rte_ring_get_capacity()``.
943 static inline unsigned int
944 rte_ring_get_size(const struct rte_ring *r)
950 * Return the number of elements which can be stored in the ring.
953 * A pointer to the ring structure.
955 * The usable size of the ring.
957 static inline unsigned int
958 rte_ring_get_capacity(const struct rte_ring *r)
964 * Dump the status of all rings on the console
967 * A pointer to a file for output
969 void rte_ring_list_dump(FILE *f);
972 * Search a ring from its name
975 * The name of the ring.
977 * The pointer to the ring matching the name, or NULL if not found,
978 * with rte_errno set appropriately. Possible rte_errno values include:
979 * - ENOENT - required entry not available to return.
981 struct rte_ring *rte_ring_lookup(const char *name);
984 * Enqueue several objects on the ring (multi-producers safe).
986 * This function uses a "compare and set" instruction to move the
987 * producer index atomically.
990 * A pointer to the ring structure.
992 * A pointer to a table of void * pointers (objects).
994 * The number of objects to add in the ring from the obj_table.
996 * if non-NULL, returns the amount of space in the ring after the
997 * enqueue operation has finished.
999 * - n: Actual number of objects enqueued.
1001 static __rte_always_inline unsigned
1002 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1003 unsigned int n, unsigned int *free_space)
1005 return __rte_ring_do_enqueue(r, obj_table, n,
1006 RTE_RING_QUEUE_VARIABLE, __IS_MP, free_space);
1010 * Enqueue several objects on a ring (NOT multi-producers safe).
1013 * A pointer to the ring structure.
1015 * A pointer to a table of void * pointers (objects).
1017 * The number of objects to add in the ring from the obj_table.
1019 * if non-NULL, returns the amount of space in the ring after the
1020 * enqueue operation has finished.
1022 * - n: Actual number of objects enqueued.
1024 static __rte_always_inline unsigned
1025 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1026 unsigned int n, unsigned int *free_space)
1028 return __rte_ring_do_enqueue(r, obj_table, n,
1029 RTE_RING_QUEUE_VARIABLE, __IS_SP, free_space);
1033 * Enqueue several objects on a ring.
1035 * This function calls the multi-producer or the single-producer
1036 * version depending on the default behavior that was specified at
1037 * ring creation time (see flags).
1040 * A pointer to the ring structure.
1042 * A pointer to a table of void * pointers (objects).
1044 * The number of objects to add in the ring from the obj_table.
1046 * if non-NULL, returns the amount of space in the ring after the
1047 * enqueue operation has finished.
1049 * - n: Actual number of objects enqueued.
1051 static __rte_always_inline unsigned
1052 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1053 unsigned int n, unsigned int *free_space)
1055 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE,
1056 r->prod.single, free_space);
1060 * Dequeue several objects from a ring (multi-consumers safe). When the request
1061 * objects are more than the available objects, only dequeue the actual number
1064 * This function uses a "compare and set" instruction to move the
1065 * consumer index atomically.
1068 * A pointer to the ring structure.
1070 * A pointer to a table of void * pointers (objects) that will be filled.
1072 * The number of objects to dequeue from the ring to the obj_table.
1074 * If non-NULL, returns the number of remaining ring entries after the
1075 * dequeue has finished.
1077 * - n: Actual number of objects dequeued, 0 if ring is empty
1079 static __rte_always_inline unsigned
1080 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
1081 unsigned int n, unsigned int *available)
1083 return __rte_ring_do_dequeue(r, obj_table, n,
1084 RTE_RING_QUEUE_VARIABLE, __IS_MC, available);
1088 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1089 * request objects are more than the available objects, only dequeue the
1090 * actual number of objects
1093 * A pointer to the ring structure.
1095 * A pointer to a table of void * pointers (objects) that will be filled.
1097 * The number of objects to dequeue from the ring to the obj_table.
1099 * If non-NULL, returns the number of remaining ring entries after the
1100 * dequeue has finished.
1102 * - n: Actual number of objects dequeued, 0 if ring is empty
1104 static __rte_always_inline unsigned
1105 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
1106 unsigned int n, unsigned int *available)
1108 return __rte_ring_do_dequeue(r, obj_table, n,
1109 RTE_RING_QUEUE_VARIABLE, __IS_SC, available);
1113 * Dequeue multiple objects from a ring up to a maximum number.
1115 * This function calls the multi-consumers or the single-consumer
1116 * version, depending on the default behaviour that was specified at
1117 * ring creation time (see flags).
1120 * A pointer to the ring structure.
1122 * A pointer to a table of void * pointers (objects) that will be filled.
1124 * The number of objects to dequeue from the ring to the obj_table.
1126 * If non-NULL, returns the number of remaining ring entries after the
1127 * dequeue has finished.
1129 * - Number of objects dequeued
1131 static __rte_always_inline unsigned
1132 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
1133 unsigned int n, unsigned int *available)
1135 return __rte_ring_do_dequeue(r, obj_table, n,
1136 RTE_RING_QUEUE_VARIABLE,
1137 r->cons.single, available);
1144 #endif /* _RTE_RING_H_ */