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>
105 #define RTE_TAILQ_RING_NAME "RTE_RING"
107 enum rte_ring_queue_behavior {
108 RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
109 RTE_RING_QUEUE_VARIABLE /* Enq/Deq as many items as possible from ring */
112 #define RTE_RING_MZ_PREFIX "RG_"
113 /**< The maximum length of a ring name. */
114 #define RTE_RING_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
115 sizeof(RTE_RING_MZ_PREFIX) + 1)
117 struct rte_memzone; /* forward declaration, so as not to require memzone.h */
119 #if RTE_CACHE_LINE_SIZE < 128
120 #define PROD_ALIGN (RTE_CACHE_LINE_SIZE * 2)
121 #define CONS_ALIGN (RTE_CACHE_LINE_SIZE * 2)
123 #define PROD_ALIGN RTE_CACHE_LINE_SIZE
124 #define CONS_ALIGN RTE_CACHE_LINE_SIZE
127 /* structure to hold a pair of head/tail values and other metadata */
128 struct rte_ring_headtail {
129 volatile uint32_t head; /**< Prod/consumer head. */
130 volatile uint32_t tail; /**< Prod/consumer tail. */
131 uint32_t single; /**< True if single prod/cons */
135 * An RTE ring structure.
137 * The producer and the consumer have a head and a tail index. The particularity
138 * of these index is that they are not between 0 and size(ring). These indexes
139 * are between 0 and 2^32, and we mask their value when we access the ring[]
140 * field. Thanks to this assumption, we can do subtractions between 2 index
141 * values in a modulo-32bit base: that's why the overflow of the indexes is not
146 * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
147 * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
148 * next time the ABI changes
150 char name[RTE_MEMZONE_NAMESIZE]; /**< Name of the ring. */
151 int flags; /**< Flags supplied at creation. */
152 const struct rte_memzone *memzone;
153 /**< Memzone, if any, containing the rte_ring */
154 uint32_t size; /**< Size of ring. */
155 uint32_t mask; /**< Mask (size-1) of ring. */
156 uint32_t watermark; /**< Max items before EDQUOT in producer. */
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);
164 void *ring[] __rte_cache_aligned; /**< Memory space of ring starts here.
165 * not volatile so need to be careful
166 * about compiler re-ordering */
169 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
170 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
171 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
172 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
175 * Calculate the memory size needed for a ring
177 * This function returns the number of bytes needed for a ring, given
178 * the number of elements in it. This value is the sum of the size of
179 * the structure rte_ring and the size of the memory needed by the
180 * objects pointers. The value is aligned to a cache line size.
183 * The number of elements in the ring (must be a power of 2).
185 * - The memory size needed for the ring on success.
186 * - -EINVAL if count is not a power of 2.
188 ssize_t rte_ring_get_memsize(unsigned count);
191 * Initialize a ring structure.
193 * Initialize a ring structure in memory pointed by "r". The size of the
194 * memory area must be large enough to store the ring structure and the
195 * object table. It is advised to use rte_ring_get_memsize() to get the
198 * The ring size is set to *count*, which must be a power of two. Water
199 * marking is disabled by default. The real usable ring size is
200 * *count-1* instead of *count* to differentiate a free ring from an
203 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
204 * memory given by the caller may not be shareable among dpdk
208 * The pointer to the ring structure followed by the objects table.
210 * The name of the ring.
212 * The number of elements in the ring (must be a power of 2).
214 * An OR of the following:
215 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
216 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
217 * is "single-producer". Otherwise, it is "multi-producers".
218 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
219 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
220 * is "single-consumer". Otherwise, it is "multi-consumers".
222 * 0 on success, or a negative value on error.
224 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
228 * Create a new ring named *name* in memory.
230 * This function uses ``memzone_reserve()`` to allocate memory. Then it
231 * calls rte_ring_init() to initialize an empty ring.
233 * The new ring size is set to *count*, which must be a power of
234 * two. Water marking is disabled by default. The real usable ring size
235 * is *count-1* instead of *count* to differentiate a free ring from an
238 * The ring is added in RTE_TAILQ_RING list.
241 * The name of the ring.
243 * The size of the ring (must be a power of 2).
245 * The *socket_id* argument is the socket identifier in case of
246 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
247 * constraint for the reserved zone.
249 * An OR of the following:
250 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
251 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
252 * is "single-producer". Otherwise, it is "multi-producers".
253 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
254 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
255 * is "single-consumer". Otherwise, it is "multi-consumers".
257 * On success, the pointer to the new allocated ring. NULL on error with
258 * rte_errno set appropriately. Possible errno values include:
259 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
260 * - E_RTE_SECONDARY - function was called from a secondary process instance
261 * - EINVAL - count provided is not a power of 2
262 * - ENOSPC - the maximum number of memzones has already been allocated
263 * - EEXIST - a memzone with the same name already exists
264 * - ENOMEM - no appropriate memory area found in which to create memzone
266 struct rte_ring *rte_ring_create(const char *name, unsigned count,
267 int socket_id, unsigned flags);
269 * De-allocate all memory used by the ring.
274 void rte_ring_free(struct rte_ring *r);
277 * Change the high water mark.
279 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
280 * *count* value. The *count* value must be greater than 0 and less
281 * than the ring size.
283 * This function can be called at any time (not necessarily at
287 * A pointer to the ring structure.
289 * The new water mark value.
291 * - 0: Success; water mark changed.
292 * - -EINVAL: Invalid water mark value.
294 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
297 * Dump the status of the ring to a file.
300 * A pointer to a file for output
302 * A pointer to the ring structure.
304 void rte_ring_dump(FILE *f, const struct rte_ring *r);
306 /* the actual enqueue of pointers on the ring.
307 * Placed here since identical code needed in both
308 * single and multi producer enqueue functions */
309 #define ENQUEUE_PTRS() do { \
310 const uint32_t size = r->size; \
311 uint32_t idx = prod_head & mask; \
312 if (likely(idx + n < size)) { \
313 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
314 r->ring[idx] = obj_table[i]; \
315 r->ring[idx+1] = obj_table[i+1]; \
316 r->ring[idx+2] = obj_table[i+2]; \
317 r->ring[idx+3] = obj_table[i+3]; \
320 case 3: r->ring[idx++] = obj_table[i++]; \
321 case 2: r->ring[idx++] = obj_table[i++]; \
322 case 1: r->ring[idx++] = obj_table[i++]; \
325 for (i = 0; idx < size; i++, idx++)\
326 r->ring[idx] = obj_table[i]; \
327 for (idx = 0; i < n; i++, idx++) \
328 r->ring[idx] = obj_table[i]; \
332 /* the actual copy of pointers on the ring to obj_table.
333 * Placed here since identical code needed in both
334 * single and multi consumer dequeue functions */
335 #define DEQUEUE_PTRS() do { \
336 uint32_t idx = cons_head & mask; \
337 const uint32_t size = r->size; \
338 if (likely(idx + n < size)) { \
339 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
340 obj_table[i] = r->ring[idx]; \
341 obj_table[i+1] = r->ring[idx+1]; \
342 obj_table[i+2] = r->ring[idx+2]; \
343 obj_table[i+3] = r->ring[idx+3]; \
346 case 3: obj_table[i++] = r->ring[idx++]; \
347 case 2: obj_table[i++] = r->ring[idx++]; \
348 case 1: obj_table[i++] = r->ring[idx++]; \
351 for (i = 0; idx < size; i++, idx++) \
352 obj_table[i] = r->ring[idx]; \
353 for (idx = 0; i < n; i++, idx++) \
354 obj_table[i] = r->ring[idx]; \
359 * @internal Enqueue several objects on the ring (multi-producers safe).
361 * This function uses a "compare and set" instruction to move the
362 * producer index atomically.
365 * A pointer to the ring structure.
367 * A pointer to a table of void * pointers (objects).
369 * The number of objects to add in the ring from the obj_table.
371 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
372 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
374 * Depend on the behavior value
375 * if behavior = RTE_RING_QUEUE_FIXED
376 * - 0: Success; objects enqueue.
377 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
378 * high water mark is exceeded.
379 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
380 * if behavior = RTE_RING_QUEUE_VARIABLE
381 * - n: Actual number of objects enqueued.
383 static inline int __attribute__((always_inline))
384 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
385 unsigned n, enum rte_ring_queue_behavior behavior)
387 uint32_t prod_head, prod_next;
388 uint32_t cons_tail, free_entries;
389 const unsigned max = n;
392 uint32_t mask = r->mask;
395 /* Avoid the unnecessary cmpset operation below, which is also
396 * potentially harmful when n equals 0. */
400 /* move prod.head atomically */
402 /* Reset n to the initial burst count */
405 prod_head = r->prod.head;
406 cons_tail = r->cons.tail;
407 /* The subtraction is done between two unsigned 32bits value
408 * (the result is always modulo 32 bits even if we have
409 * prod_head > cons_tail). So 'free_entries' is always between 0
410 * and size(ring)-1. */
411 free_entries = (mask + cons_tail - prod_head);
413 /* check that we have enough room in ring */
414 if (unlikely(n > free_entries)) {
415 if (behavior == RTE_RING_QUEUE_FIXED)
418 /* No free entry available */
419 if (unlikely(free_entries == 0))
425 prod_next = prod_head + n;
426 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
428 } while (unlikely(success == 0));
430 /* write entries in ring */
434 /* if we exceed the watermark */
435 if (unlikely(((mask + 1) - free_entries + n) > r->watermark))
436 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
437 (int)(n | RTE_RING_QUOT_EXCEED);
439 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
442 * If there are other enqueues in progress that preceded us,
443 * we need to wait for them to complete
445 while (unlikely(r->prod.tail != prod_head))
448 r->prod.tail = prod_next;
453 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
456 * A pointer to the ring structure.
458 * A pointer to a table of void * pointers (objects).
460 * The number of objects to add in the ring from the obj_table.
462 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
463 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
465 * Depend on the behavior value
466 * if behavior = RTE_RING_QUEUE_FIXED
467 * - 0: Success; objects enqueue.
468 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
469 * high water mark is exceeded.
470 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
471 * if behavior = RTE_RING_QUEUE_VARIABLE
472 * - n: Actual number of objects enqueued.
474 static inline int __attribute__((always_inline))
475 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
476 unsigned n, enum rte_ring_queue_behavior behavior)
478 uint32_t prod_head, cons_tail;
479 uint32_t prod_next, free_entries;
481 uint32_t mask = r->mask;
484 prod_head = r->prod.head;
485 cons_tail = r->cons.tail;
486 /* The subtraction is done between two unsigned 32bits value
487 * (the result is always modulo 32 bits even if we have
488 * prod_head > cons_tail). So 'free_entries' is always between 0
489 * and size(ring)-1. */
490 free_entries = mask + cons_tail - prod_head;
492 /* check that we have enough room in ring */
493 if (unlikely(n > free_entries)) {
494 if (behavior == RTE_RING_QUEUE_FIXED)
497 /* No free entry available */
498 if (unlikely(free_entries == 0))
504 prod_next = prod_head + n;
505 r->prod.head = prod_next;
507 /* write entries in ring */
511 /* if we exceed the watermark */
512 if (unlikely(((mask + 1) - free_entries + n) > r->watermark))
513 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
514 (int)(n | RTE_RING_QUOT_EXCEED);
516 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
518 r->prod.tail = prod_next;
523 * @internal Dequeue several objects from a ring (multi-consumers safe). When
524 * the request objects are more than the available objects, only dequeue the
525 * actual number of objects
527 * This function uses a "compare and set" instruction to move the
528 * consumer index atomically.
531 * A pointer to the ring structure.
533 * A pointer to a table of void * pointers (objects) that will be filled.
535 * The number of objects to dequeue from the ring to the obj_table.
537 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
538 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
540 * Depend on the behavior value
541 * if behavior = RTE_RING_QUEUE_FIXED
542 * - 0: Success; objects dequeued.
543 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
545 * if behavior = RTE_RING_QUEUE_VARIABLE
546 * - n: Actual number of objects dequeued.
549 static inline int __attribute__((always_inline))
550 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
551 unsigned n, enum rte_ring_queue_behavior behavior)
553 uint32_t cons_head, prod_tail;
554 uint32_t cons_next, entries;
555 const unsigned max = n;
558 uint32_t mask = r->mask;
560 /* Avoid the unnecessary cmpset operation below, which is also
561 * potentially harmful when n equals 0. */
565 /* move cons.head atomically */
567 /* Restore n as it may change every loop */
570 cons_head = r->cons.head;
571 prod_tail = r->prod.tail;
572 /* The subtraction is done between two unsigned 32bits value
573 * (the result is always modulo 32 bits even if we have
574 * cons_head > prod_tail). So 'entries' is always between 0
575 * and size(ring)-1. */
576 entries = (prod_tail - cons_head);
578 /* Set the actual entries for dequeue */
580 if (behavior == RTE_RING_QUEUE_FIXED)
583 if (unlikely(entries == 0))
589 cons_next = cons_head + n;
590 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
592 } while (unlikely(success == 0));
599 * If there are other dequeues in progress that preceded us,
600 * we need to wait for them to complete
602 while (unlikely(r->cons.tail != cons_head))
605 r->cons.tail = cons_next;
607 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
611 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
612 * When the request objects are more than the available objects, only dequeue
613 * the actual number of objects
616 * A pointer to the ring structure.
618 * A pointer to a table of void * pointers (objects) that will be filled.
620 * The number of objects to dequeue from the ring to the obj_table.
622 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
623 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
625 * Depend on the behavior value
626 * if behavior = RTE_RING_QUEUE_FIXED
627 * - 0: Success; objects dequeued.
628 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
630 * if behavior = RTE_RING_QUEUE_VARIABLE
631 * - n: Actual number of objects dequeued.
633 static inline int __attribute__((always_inline))
634 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
635 unsigned n, enum rte_ring_queue_behavior behavior)
637 uint32_t cons_head, prod_tail;
638 uint32_t cons_next, entries;
640 uint32_t mask = r->mask;
642 cons_head = r->cons.head;
643 prod_tail = r->prod.tail;
644 /* The subtraction is done between two unsigned 32bits value
645 * (the result is always modulo 32 bits even if we have
646 * cons_head > prod_tail). So 'entries' is always between 0
647 * and size(ring)-1. */
648 entries = prod_tail - cons_head;
651 if (behavior == RTE_RING_QUEUE_FIXED)
654 if (unlikely(entries == 0))
660 cons_next = cons_head + n;
661 r->cons.head = cons_next;
667 r->cons.tail = cons_next;
668 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
672 * Enqueue several objects on the ring (multi-producers safe).
674 * This function uses a "compare and set" instruction to move the
675 * producer index atomically.
678 * A pointer to the ring structure.
680 * A pointer to a table of void * pointers (objects).
682 * The number of objects to add in the ring from the obj_table.
684 * - 0: Success; objects enqueue.
685 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
686 * high water mark is exceeded.
687 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
689 static inline int __attribute__((always_inline))
690 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
693 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
697 * Enqueue several objects on a ring (NOT multi-producers safe).
700 * A pointer to the ring structure.
702 * A pointer to a table of void * pointers (objects).
704 * The number of objects to add in the ring from the obj_table.
706 * - 0: Success; objects enqueued.
707 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
708 * high water mark is exceeded.
709 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
711 static inline int __attribute__((always_inline))
712 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
715 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
719 * Enqueue several objects on a ring.
721 * This function calls the multi-producer or the single-producer
722 * version depending on the default behavior that was specified at
723 * ring creation time (see flags).
726 * A pointer to the ring structure.
728 * A pointer to a table of void * pointers (objects).
730 * The number of objects to add in the ring from the obj_table.
732 * - 0: Success; objects enqueued.
733 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
734 * high water mark is exceeded.
735 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
737 static inline int __attribute__((always_inline))
738 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
742 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
744 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
748 * Enqueue one object on a ring (multi-producers safe).
750 * This function uses a "compare and set" instruction to move the
751 * producer index atomically.
754 * A pointer to the ring structure.
756 * A pointer to the object to be added.
758 * - 0: Success; objects enqueued.
759 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
760 * high water mark is exceeded.
761 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
763 static inline int __attribute__((always_inline))
764 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
766 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
770 * Enqueue one object on a ring (NOT multi-producers safe).
773 * A pointer to the ring structure.
775 * A pointer to the object to be added.
777 * - 0: Success; objects enqueued.
778 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
779 * high water mark is exceeded.
780 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
782 static inline int __attribute__((always_inline))
783 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
785 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
789 * Enqueue one object on a ring.
791 * This function calls the multi-producer or the single-producer
792 * version, depending on the default behaviour that was specified at
793 * ring creation time (see flags).
796 * A pointer to the ring structure.
798 * A pointer to the object to be added.
800 * - 0: Success; objects enqueued.
801 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
802 * high water mark is exceeded.
803 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
805 static inline int __attribute__((always_inline))
806 rte_ring_enqueue(struct rte_ring *r, void *obj)
809 return rte_ring_sp_enqueue(r, obj);
811 return rte_ring_mp_enqueue(r, obj);
815 * Dequeue several objects from a ring (multi-consumers safe).
817 * This function uses a "compare and set" instruction to move the
818 * consumer index atomically.
821 * A pointer to the ring structure.
823 * A pointer to a table of void * pointers (objects) that will be filled.
825 * The number of objects to dequeue from the ring to the obj_table.
827 * - 0: Success; objects dequeued.
828 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
831 static inline int __attribute__((always_inline))
832 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
834 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
838 * Dequeue several objects from a ring (NOT multi-consumers safe).
841 * A pointer to the ring structure.
843 * A pointer to a table of void * pointers (objects) that will be filled.
845 * The number of objects to dequeue from the ring to the obj_table,
846 * must be strictly positive.
848 * - 0: Success; objects dequeued.
849 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
852 static inline int __attribute__((always_inline))
853 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
855 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
859 * Dequeue several objects from a ring.
861 * This function calls the multi-consumers or the single-consumer
862 * version, depending on the default behaviour that was specified at
863 * ring creation time (see flags).
866 * A pointer to the ring structure.
868 * A pointer to a table of void * pointers (objects) that will be filled.
870 * The number of objects to dequeue from the ring to the obj_table.
872 * - 0: Success; objects dequeued.
873 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
876 static inline int __attribute__((always_inline))
877 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
880 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
882 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
886 * Dequeue one object from a ring (multi-consumers safe).
888 * This function uses a "compare and set" instruction to move the
889 * consumer index atomically.
892 * A pointer to the ring structure.
894 * A pointer to a void * pointer (object) that will be filled.
896 * - 0: Success; objects dequeued.
897 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
900 static inline int __attribute__((always_inline))
901 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
903 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
907 * Dequeue one object from a ring (NOT multi-consumers safe).
910 * A pointer to the ring structure.
912 * A pointer to a void * pointer (object) that will be filled.
914 * - 0: Success; objects dequeued.
915 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
918 static inline int __attribute__((always_inline))
919 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
921 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
925 * Dequeue one object from a ring.
927 * This function calls the multi-consumers or the single-consumer
928 * version depending on the default behaviour that was specified at
929 * ring creation time (see flags).
932 * A pointer to the ring structure.
934 * A pointer to a void * pointer (object) that will be filled.
936 * - 0: Success, objects dequeued.
937 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
940 static inline int __attribute__((always_inline))
941 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
944 return rte_ring_sc_dequeue(r, obj_p);
946 return rte_ring_mc_dequeue(r, obj_p);
950 * Test if a ring is full.
953 * A pointer to the ring structure.
955 * - 1: The ring is full.
956 * - 0: The ring is not full.
959 rte_ring_full(const struct rte_ring *r)
961 uint32_t prod_tail = r->prod.tail;
962 uint32_t cons_tail = r->cons.tail;
963 return ((cons_tail - prod_tail - 1) & r->mask) == 0;
967 * Test if a ring is empty.
970 * A pointer to the ring structure.
972 * - 1: The ring is empty.
973 * - 0: The ring is not empty.
976 rte_ring_empty(const struct rte_ring *r)
978 uint32_t prod_tail = r->prod.tail;
979 uint32_t cons_tail = r->cons.tail;
980 return !!(cons_tail == prod_tail);
984 * Return the number of entries in a ring.
987 * A pointer to the ring structure.
989 * The number of entries in the ring.
991 static inline unsigned
992 rte_ring_count(const struct rte_ring *r)
994 uint32_t prod_tail = r->prod.tail;
995 uint32_t cons_tail = r->cons.tail;
996 return (prod_tail - cons_tail) & r->mask;
1000 * Return the number of free entries in a ring.
1003 * A pointer to the ring structure.
1005 * The number of free entries in the ring.
1007 static inline unsigned
1008 rte_ring_free_count(const struct rte_ring *r)
1010 uint32_t prod_tail = r->prod.tail;
1011 uint32_t cons_tail = r->cons.tail;
1012 return (cons_tail - prod_tail - 1) & r->mask;
1016 * Return the size of the ring.
1019 * A pointer to the ring structure.
1021 * The number of elements which can be stored in the ring.
1023 static inline unsigned int
1024 rte_ring_get_size(const struct rte_ring *r)
1030 * Dump the status of all rings on the console
1033 * A pointer to a file for output
1035 void rte_ring_list_dump(FILE *f);
1038 * Search a ring from its name
1041 * The name of the ring.
1043 * The pointer to the ring matching the name, or NULL if not found,
1044 * with rte_errno set appropriately. Possible rte_errno values include:
1045 * - ENOENT - required entry not available to return.
1047 struct rte_ring *rte_ring_lookup(const char *name);
1050 * Enqueue several objects on the ring (multi-producers safe).
1052 * This function uses a "compare and set" instruction to move the
1053 * producer index atomically.
1056 * A pointer to the ring structure.
1058 * A pointer to a table of void * pointers (objects).
1060 * The number of objects to add in the ring from the obj_table.
1062 * - n: Actual number of objects enqueued.
1064 static inline unsigned __attribute__((always_inline))
1065 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1068 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1072 * Enqueue several objects on a ring (NOT multi-producers safe).
1075 * A pointer to the ring structure.
1077 * A pointer to a table of void * pointers (objects).
1079 * The number of objects to add in the ring from the obj_table.
1081 * - n: Actual number of objects enqueued.
1083 static inline unsigned __attribute__((always_inline))
1084 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1087 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1091 * Enqueue several objects on a ring.
1093 * This function calls the multi-producer or the single-producer
1094 * version depending on the default behavior that was specified at
1095 * ring creation time (see flags).
1098 * A pointer to the ring structure.
1100 * A pointer to a table of void * pointers (objects).
1102 * The number of objects to add in the ring from the obj_table.
1104 * - n: Actual number of objects enqueued.
1106 static inline unsigned __attribute__((always_inline))
1107 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1111 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1113 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1117 * Dequeue several objects from a ring (multi-consumers safe). When the request
1118 * objects are more than the available objects, only dequeue the actual number
1121 * This function uses a "compare and set" instruction to move the
1122 * consumer index atomically.
1125 * A pointer to the ring structure.
1127 * A pointer to a table of void * pointers (objects) that will be filled.
1129 * The number of objects to dequeue from the ring to the obj_table.
1131 * - n: Actual number of objects dequeued, 0 if ring is empty
1133 static inline unsigned __attribute__((always_inline))
1134 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1136 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1140 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1141 * request objects are more than the available objects, only dequeue the
1142 * actual number of objects
1145 * A pointer to the ring structure.
1147 * A pointer to a table of void * pointers (objects) that will be filled.
1149 * The number of objects to dequeue from the ring to the obj_table.
1151 * - n: Actual number of objects dequeued, 0 if ring is empty
1153 static inline unsigned __attribute__((always_inline))
1154 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1156 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1160 * Dequeue multiple objects from a ring up to a maximum number.
1162 * This function calls the multi-consumers or the single-consumer
1163 * version, depending on the default behaviour that was specified at
1164 * ring creation time (see flags).
1167 * A pointer to the ring structure.
1169 * A pointer to a table of void * pointers (objects) that will be filled.
1171 * The number of objects to dequeue from the ring to the obj_table.
1173 * - Number of objects dequeued
1175 static inline unsigned __attribute__((always_inline))
1176 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1179 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1181 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1188 #endif /* _RTE_RING_H_ */