4 * Copyright(c) 2010-2014 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.
95 #include <sys/queue.h>
97 #include <rte_common.h>
98 #include <rte_memory.h>
99 #include <rte_lcore.h>
100 #include <rte_atomic.h>
101 #include <rte_branch_prediction.h>
103 enum rte_ring_queue_behavior {
104 RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
105 RTE_RING_QUEUE_VARIABLE /* Enq/Deq as many items a possible from ring */
108 #ifdef RTE_LIBRTE_RING_DEBUG
110 * A structure that stores the ring statistics (per-lcore).
112 struct rte_ring_debug_stats {
113 uint64_t enq_success_bulk; /**< Successful enqueues number. */
114 uint64_t enq_success_objs; /**< Objects successfully enqueued. */
115 uint64_t enq_quota_bulk; /**< Successful enqueues above watermark. */
116 uint64_t enq_quota_objs; /**< Objects enqueued above watermark. */
117 uint64_t enq_fail_bulk; /**< Failed enqueues number. */
118 uint64_t enq_fail_objs; /**< Objects that failed to be enqueued. */
119 uint64_t deq_success_bulk; /**< Successful dequeues number. */
120 uint64_t deq_success_objs; /**< Objects successfully dequeued. */
121 uint64_t deq_fail_bulk; /**< Failed dequeues number. */
122 uint64_t deq_fail_objs; /**< Objects that failed to be dequeued. */
123 } __rte_cache_aligned;
126 #define RTE_RING_NAMESIZE 32 /**< The maximum length of a ring name. */
129 * An RTE ring structure.
131 * The producer and the consumer have a head and a tail index. The particularity
132 * of these index is that they are not between 0 and size(ring). These indexes
133 * are between 0 and 2^32, and we mask their value when we access the ring[]
134 * field. Thanks to this assumption, we can do subtractions between 2 index
135 * values in a modulo-32bit base: that's why the overflow of the indexes is not
139 TAILQ_ENTRY(rte_ring) next; /**< Next in list. */
141 char name[RTE_RING_NAMESIZE]; /**< Name of the ring. */
142 int flags; /**< Flags supplied at creation. */
144 /** Ring producer status. */
146 uint32_t watermark; /**< Maximum items before EDQUOT. */
147 uint32_t sp_enqueue; /**< True, if single producer. */
148 uint32_t size; /**< Size of ring. */
149 uint32_t mask; /**< Mask (size-1) of ring. */
150 volatile uint32_t head; /**< Producer head. */
151 volatile uint32_t tail; /**< Producer tail. */
152 } prod __rte_cache_aligned;
154 /** Ring consumer status. */
156 uint32_t sc_dequeue; /**< True, if single consumer. */
157 uint32_t size; /**< Size of the ring. */
158 uint32_t mask; /**< Mask (size-1) of ring. */
159 volatile uint32_t head; /**< Consumer head. */
160 volatile uint32_t tail; /**< Consumer tail. */
161 #ifdef RTE_RING_SPLIT_PROD_CONS
162 } cons __rte_cache_aligned;
167 #ifdef RTE_LIBRTE_RING_DEBUG
168 struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
171 void * ring[0] __rte_cache_aligned; /**< Memory space of ring starts here.
172 * not volatile so need to be careful
173 * about compiler re-ordering */
176 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
177 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
178 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
179 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
182 * @internal When debug is enabled, store ring statistics.
184 * A pointer to the ring.
186 * The name of the statistics field to increment in the ring.
188 * The number to add to the object-oriented statistics.
190 #ifdef RTE_LIBRTE_RING_DEBUG
191 #define __RING_STAT_ADD(r, name, n) do { \
192 unsigned __lcore_id = rte_lcore_id(); \
193 r->stats[__lcore_id].name##_objs += n; \
194 r->stats[__lcore_id].name##_bulk += 1; \
197 #define __RING_STAT_ADD(r, name, n) do {} while(0)
201 * Create a new ring named *name* in memory.
203 * This function uses ``memzone_reserve()`` to allocate memory. Its size is
204 * set to *count*, which must be a power of two. Water marking is
205 * disabled by default.
206 * Note that the real usable ring size is *count-1* instead of
210 * The name of the ring.
212 * The size of the ring (must be a power of 2).
214 * The *socket_id* argument is the socket identifier in case of
215 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
216 * constraint for the reserved zone.
218 * An OR of the following:
219 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
220 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
221 * is "single-producer". Otherwise, it is "multi-producers".
222 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
223 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
224 * is "single-consumer". Otherwise, it is "multi-consumers".
226 * On success, the pointer to the new allocated ring. NULL on error with
227 * rte_errno set appropriately. Possible errno values include:
228 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
229 * - E_RTE_SECONDARY - function was called from a secondary process instance
230 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring list
231 * - EINVAL - count provided is not a power of 2
232 * - ENOSPC - the maximum number of memzones has already been allocated
233 * - EEXIST - a memzone with the same name already exists
234 * - ENOMEM - no appropriate memory area found in which to create memzone
236 struct rte_ring *rte_ring_create(const char *name, unsigned count,
237 int socket_id, unsigned flags);
240 * Change the high water mark.
242 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
243 * *count* value. The *count* value must be greater than 0 and less
244 * than the ring size.
246 * This function can be called at any time (not necessarily at
250 * A pointer to the ring structure.
252 * The new water mark value.
254 * - 0: Success; water mark changed.
255 * - -EINVAL: Invalid water mark value.
257 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
260 * Dump the status of the ring to the console.
263 * A pointer to the ring structure.
265 void rte_ring_dump(const struct rte_ring *r);
267 /* the actual enqueue of pointers on the ring.
268 * Placed here since identical code needed in both
269 * single and multi producer enqueue functions */
270 #define ENQUEUE_PTRS() do { \
271 const uint32_t size = r->prod.size; \
272 uint32_t idx = prod_head & mask; \
273 if (likely(idx + n < size)) { \
274 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
275 r->ring[idx] = obj_table[i]; \
276 r->ring[idx+1] = obj_table[i+1]; \
277 r->ring[idx+2] = obj_table[i+2]; \
278 r->ring[idx+3] = obj_table[i+3]; \
281 case 3: r->ring[idx++] = obj_table[i++]; \
282 case 2: r->ring[idx++] = obj_table[i++]; \
283 case 1: r->ring[idx++] = obj_table[i++]; \
286 for (i = 0; idx < size; i++, idx++)\
287 r->ring[idx] = obj_table[i]; \
288 for (idx = 0; i < n; i++, idx++) \
289 r->ring[idx] = obj_table[i]; \
293 /* the actual copy of pointers on the ring to obj_table.
294 * Placed here since identical code needed in both
295 * single and multi consumer dequeue functions */
296 #define DEQUEUE_PTRS() do { \
297 uint32_t idx = cons_head & mask; \
298 const uint32_t size = r->cons.size; \
299 if (likely(idx + n < size)) { \
300 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
301 obj_table[i] = r->ring[idx]; \
302 obj_table[i+1] = r->ring[idx+1]; \
303 obj_table[i+2] = r->ring[idx+2]; \
304 obj_table[i+3] = r->ring[idx+3]; \
307 case 3: obj_table[i++] = r->ring[idx++]; \
308 case 2: obj_table[i++] = r->ring[idx++]; \
309 case 1: obj_table[i++] = r->ring[idx++]; \
312 for (i = 0; idx < size; i++, idx++) \
313 obj_table[i] = r->ring[idx]; \
314 for (idx = 0; i < n; i++, idx++) \
315 obj_table[i] = r->ring[idx]; \
320 * @internal Enqueue several objects on the ring (multi-producers safe).
322 * This function uses a "compare and set" instruction to move the
323 * producer index atomically.
326 * A pointer to the ring structure.
328 * A pointer to a table of void * pointers (objects).
330 * The number of objects to add in the ring from the obj_table.
332 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
333 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
335 * Depend on the behavior value
336 * if behavior = RTE_RING_QUEUE_FIXED
337 * - 0: Success; objects enqueue.
338 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
339 * high water mark is exceeded.
340 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
341 * if behavior = RTE_RING_QUEUE_VARIABLE
342 * - n: Actual number of objects enqueued.
344 static inline int __attribute__((always_inline))
345 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
346 unsigned n, enum rte_ring_queue_behavior behavior)
348 uint32_t prod_head, prod_next;
349 uint32_t cons_tail, free_entries;
350 const unsigned max = n;
353 uint32_t mask = r->prod.mask;
356 /* move prod.head atomically */
358 /* Reset n to the initial burst count */
361 prod_head = r->prod.head;
362 cons_tail = r->cons.tail;
363 /* The subtraction is done between two unsigned 32bits value
364 * (the result is always modulo 32 bits even if we have
365 * prod_head > cons_tail). So 'free_entries' is always between 0
366 * and size(ring)-1. */
367 free_entries = (mask + cons_tail - prod_head);
369 /* check that we have enough room in ring */
370 if (unlikely(n > free_entries)) {
371 if (behavior == RTE_RING_QUEUE_FIXED) {
372 __RING_STAT_ADD(r, enq_fail, n);
376 /* No free entry available */
377 if (unlikely(free_entries == 0)) {
378 __RING_STAT_ADD(r, enq_fail, n);
386 prod_next = prod_head + n;
387 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
389 } while (unlikely(success == 0));
391 /* write entries in ring */
393 rte_compiler_barrier();
395 /* if we exceed the watermark */
396 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
397 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
398 (int)(n | RTE_RING_QUOT_EXCEED);
399 __RING_STAT_ADD(r, enq_quota, n);
402 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
403 __RING_STAT_ADD(r, enq_success, n);
407 * If there are other enqueues in progress that preceeded us,
408 * we need to wait for them to complete
410 while (unlikely(r->prod.tail != prod_head))
413 r->prod.tail = prod_next;
418 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
421 * A pointer to the ring structure.
423 * A pointer to a table of void * pointers (objects).
425 * The number of objects to add in the ring from the obj_table.
427 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
428 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
430 * Depend on the behavior value
431 * if behavior = RTE_RING_QUEUE_FIXED
432 * - 0: Success; objects enqueue.
433 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
434 * high water mark is exceeded.
435 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
436 * if behavior = RTE_RING_QUEUE_VARIABLE
437 * - n: Actual number of objects enqueued.
439 static inline int __attribute__((always_inline))
440 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
441 unsigned n, enum rte_ring_queue_behavior behavior)
443 uint32_t prod_head, cons_tail;
444 uint32_t prod_next, free_entries;
446 uint32_t mask = r->prod.mask;
449 prod_head = r->prod.head;
450 cons_tail = r->cons.tail;
451 /* The subtraction is done between two unsigned 32bits value
452 * (the result is always modulo 32 bits even if we have
453 * prod_head > cons_tail). So 'free_entries' is always between 0
454 * and size(ring)-1. */
455 free_entries = mask + cons_tail - prod_head;
457 /* check that we have enough room in ring */
458 if (unlikely(n > free_entries)) {
459 if (behavior == RTE_RING_QUEUE_FIXED) {
460 __RING_STAT_ADD(r, enq_fail, n);
464 /* No free entry available */
465 if (unlikely(free_entries == 0)) {
466 __RING_STAT_ADD(r, enq_fail, n);
474 prod_next = prod_head + n;
475 r->prod.head = prod_next;
477 /* write entries in ring */
479 rte_compiler_barrier();
481 /* if we exceed the watermark */
482 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
483 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
484 (int)(n | RTE_RING_QUOT_EXCEED);
485 __RING_STAT_ADD(r, enq_quota, n);
488 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
489 __RING_STAT_ADD(r, enq_success, n);
492 r->prod.tail = prod_next;
497 * @internal Dequeue several objects from a ring (multi-consumers safe). When
498 * the request objects are more than the available objects, only dequeue the
499 * actual number of objects
501 * This function uses a "compare and set" instruction to move the
502 * consumer index atomically.
505 * A pointer to the ring structure.
507 * A pointer to a table of void * pointers (objects) that will be filled.
509 * The number of objects to dequeue from the ring to the obj_table.
511 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
512 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
514 * Depend on the behavior value
515 * if behavior = RTE_RING_QUEUE_FIXED
516 * - 0: Success; objects dequeued.
517 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
519 * if behavior = RTE_RING_QUEUE_VARIABLE
520 * - n: Actual number of objects dequeued.
523 static inline int __attribute__((always_inline))
524 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
525 unsigned n, enum rte_ring_queue_behavior behavior)
527 uint32_t cons_head, prod_tail;
528 uint32_t cons_next, entries;
529 const unsigned max = n;
532 uint32_t mask = r->prod.mask;
534 /* move cons.head atomically */
536 /* Restore n as it may change every loop */
539 cons_head = r->cons.head;
540 prod_tail = r->prod.tail;
541 /* The subtraction is done between two unsigned 32bits value
542 * (the result is always modulo 32 bits even if we have
543 * cons_head > prod_tail). So 'entries' is always between 0
544 * and size(ring)-1. */
545 entries = (prod_tail - cons_head);
547 /* Set the actual entries for dequeue */
549 if (behavior == RTE_RING_QUEUE_FIXED) {
550 __RING_STAT_ADD(r, deq_fail, n);
554 if (unlikely(entries == 0)){
555 __RING_STAT_ADD(r, deq_fail, n);
563 cons_next = cons_head + n;
564 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
566 } while (unlikely(success == 0));
570 rte_compiler_barrier();
573 * If there are other dequeues in progress that preceded us,
574 * we need to wait for them to complete
576 while (unlikely(r->cons.tail != cons_head))
579 __RING_STAT_ADD(r, deq_success, n);
580 r->cons.tail = cons_next;
582 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
586 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
587 * When the request objects are more than the available objects, only dequeue
588 * the actual number of objects
591 * A pointer to the ring structure.
593 * A pointer to a table of void * pointers (objects) that will be filled.
595 * The number of objects to dequeue from the ring to the obj_table.
597 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
598 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
600 * Depend on the behavior value
601 * if behavior = RTE_RING_QUEUE_FIXED
602 * - 0: Success; objects dequeued.
603 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
605 * if behavior = RTE_RING_QUEUE_VARIABLE
606 * - n: Actual number of objects dequeued.
608 static inline int __attribute__((always_inline))
609 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
610 unsigned n, enum rte_ring_queue_behavior behavior)
612 uint32_t cons_head, prod_tail;
613 uint32_t cons_next, entries;
615 uint32_t mask = r->prod.mask;
617 cons_head = r->cons.head;
618 prod_tail = r->prod.tail;
619 /* The subtraction is done between two unsigned 32bits value
620 * (the result is always modulo 32 bits even if we have
621 * cons_head > prod_tail). So 'entries' is always between 0
622 * and size(ring)-1. */
623 entries = prod_tail - cons_head;
626 if (behavior == RTE_RING_QUEUE_FIXED) {
627 __RING_STAT_ADD(r, deq_fail, n);
631 if (unlikely(entries == 0)){
632 __RING_STAT_ADD(r, deq_fail, n);
640 cons_next = cons_head + n;
641 r->cons.head = cons_next;
645 rte_compiler_barrier();
647 __RING_STAT_ADD(r, deq_success, n);
648 r->cons.tail = cons_next;
649 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
653 * Enqueue several objects on the 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 a table of void * pointers (objects).
663 * The number of objects to add in the ring from the obj_table.
665 * - 0: Success; objects enqueue.
666 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
667 * high water mark is exceeded.
668 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
670 static inline int __attribute__((always_inline))
671 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
674 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
678 * Enqueue several objects on a ring (NOT multi-producers safe).
681 * A pointer to the ring structure.
683 * A pointer to a table of void * pointers (objects).
685 * The number of objects to add in the ring from the obj_table.
687 * - 0: Success; objects enqueued.
688 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
689 * high water mark is exceeded.
690 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
692 static inline int __attribute__((always_inline))
693 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
696 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
700 * Enqueue several objects on a ring.
702 * This function calls the multi-producer or the single-producer
703 * version depending on the default behavior that was specified at
704 * ring creation time (see flags).
707 * A pointer to the ring structure.
709 * A pointer to a table of void * pointers (objects).
711 * The number of objects to add in the ring from the obj_table.
713 * - 0: Success; objects enqueued.
714 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
715 * high water mark is exceeded.
716 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
718 static inline int __attribute__((always_inline))
719 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
722 if (r->prod.sp_enqueue)
723 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
725 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
729 * Enqueue one object on a ring (multi-producers safe).
731 * This function uses a "compare and set" instruction to move the
732 * producer index atomically.
735 * A pointer to the ring structure.
737 * A pointer to the object to be added.
739 * - 0: Success; objects enqueued.
740 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
741 * high water mark is exceeded.
742 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
744 static inline int __attribute__((always_inline))
745 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
747 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
751 * Enqueue one object on a ring (NOT multi-producers safe).
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_sp_enqueue(struct rte_ring *r, void *obj)
766 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
770 * Enqueue one object on a ring.
772 * This function calls the multi-producer or the single-producer
773 * version, depending on the default behaviour that was specified at
774 * ring creation time (see flags).
777 * A pointer to the ring structure.
779 * A pointer to the object to be added.
781 * - 0: Success; objects enqueued.
782 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
783 * high water mark is exceeded.
784 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
786 static inline int __attribute__((always_inline))
787 rte_ring_enqueue(struct rte_ring *r, void *obj)
789 if (r->prod.sp_enqueue)
790 return rte_ring_sp_enqueue(r, obj);
792 return rte_ring_mp_enqueue(r, obj);
796 * Dequeue several objects from a ring (multi-consumers safe).
798 * This function uses a "compare and set" instruction to move the
799 * consumer index atomically.
802 * A pointer to the ring structure.
804 * A pointer to a table of void * pointers (objects) that will be filled.
806 * The number of objects to dequeue from the ring to the obj_table.
808 * - 0: Success; objects dequeued.
809 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
812 static inline int __attribute__((always_inline))
813 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
815 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
819 * Dequeue several objects from a ring (NOT multi-consumers safe).
822 * A pointer to the ring structure.
824 * A pointer to a table of void * pointers (objects) that will be filled.
826 * The number of objects to dequeue from the ring to the obj_table,
827 * must be strictly positive.
829 * - 0: Success; objects dequeued.
830 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
833 static inline int __attribute__((always_inline))
834 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
836 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
840 * Dequeue several objects from a ring.
842 * This function calls the multi-consumers or the single-consumer
843 * version, depending on the default behaviour that was specified at
844 * ring creation time (see flags).
847 * A pointer to the ring structure.
849 * A pointer to a table of void * pointers (objects) that will be filled.
851 * The number of objects to dequeue from the ring to the obj_table.
853 * - 0: Success; objects dequeued.
854 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
857 static inline int __attribute__((always_inline))
858 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
860 if (r->cons.sc_dequeue)
861 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
863 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
867 * Dequeue one object from a ring (multi-consumers safe).
869 * This function uses a "compare and set" instruction to move the
870 * consumer index atomically.
873 * A pointer to the ring structure.
875 * A pointer to a void * pointer (object) that will be filled.
877 * - 0: Success; objects dequeued.
878 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
881 static inline int __attribute__((always_inline))
882 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
884 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
888 * Dequeue one object from a ring (NOT multi-consumers safe).
891 * A pointer to the ring structure.
893 * A pointer to a void * pointer (object) that will be filled.
895 * - 0: Success; objects dequeued.
896 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
899 static inline int __attribute__((always_inline))
900 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
902 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
906 * Dequeue one object from a ring.
908 * This function calls the multi-consumers or the single-consumer
909 * version depending on the default behaviour that was specified at
910 * ring creation time (see flags).
913 * A pointer to the ring structure.
915 * A pointer to a void * pointer (object) that will be filled.
917 * - 0: Success, objects dequeued.
918 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
921 static inline int __attribute__((always_inline))
922 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
924 if (r->cons.sc_dequeue)
925 return rte_ring_sc_dequeue(r, obj_p);
927 return rte_ring_mc_dequeue(r, obj_p);
931 * Test if a ring is full.
934 * A pointer to the ring structure.
936 * - 1: The ring is full.
937 * - 0: The ring is not full.
940 rte_ring_full(const struct rte_ring *r)
942 uint32_t prod_tail = r->prod.tail;
943 uint32_t cons_tail = r->cons.tail;
944 return (((cons_tail - prod_tail - 1) & r->prod.mask) == 0);
948 * Test if a ring is empty.
951 * A pointer to the ring structure.
953 * - 1: The ring is empty.
954 * - 0: The ring is not empty.
957 rte_ring_empty(const struct rte_ring *r)
959 uint32_t prod_tail = r->prod.tail;
960 uint32_t cons_tail = r->cons.tail;
961 return !!(cons_tail == prod_tail);
965 * Return the number of entries in a ring.
968 * A pointer to the ring structure.
970 * The number of entries in the ring.
972 static inline unsigned
973 rte_ring_count(const struct rte_ring *r)
975 uint32_t prod_tail = r->prod.tail;
976 uint32_t cons_tail = r->cons.tail;
977 return ((prod_tail - cons_tail) & r->prod.mask);
981 * Return the number of free entries in a ring.
984 * A pointer to the ring structure.
986 * The number of free entries in the ring.
988 static inline unsigned
989 rte_ring_free_count(const struct rte_ring *r)
991 uint32_t prod_tail = r->prod.tail;
992 uint32_t cons_tail = r->cons.tail;
993 return ((cons_tail - prod_tail - 1) & r->prod.mask);
997 * Dump the status of all rings on the console
999 void rte_ring_list_dump(void);
1002 * Search a ring from its name
1005 * The name of the ring.
1007 * The pointer to the ring matching the name, or NULL if not found,
1008 * with rte_errno set appropriately. Possible rte_errno values include:
1009 * - ENOENT - required entry not available to return.
1011 struct rte_ring *rte_ring_lookup(const char *name);
1014 * Enqueue several objects on the ring (multi-producers safe).
1016 * This function uses a "compare and set" instruction to move the
1017 * producer index atomically.
1020 * A pointer to the ring structure.
1022 * A pointer to a table of void * pointers (objects).
1024 * The number of objects to add in the ring from the obj_table.
1026 * - n: Actual number of objects enqueued.
1028 static inline int __attribute__((always_inline))
1029 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1032 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1036 * Enqueue several objects on a ring (NOT multi-producers safe).
1039 * A pointer to the ring structure.
1041 * A pointer to a table of void * pointers (objects).
1043 * The number of objects to add in the ring from the obj_table.
1045 * - n: Actual number of objects enqueued.
1047 static inline int __attribute__((always_inline))
1048 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1051 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1055 * Enqueue several objects on a ring.
1057 * This function calls the multi-producer or the single-producer
1058 * version depending on the default behavior that was specified at
1059 * ring creation time (see flags).
1062 * A pointer to the ring structure.
1064 * A pointer to a table of void * pointers (objects).
1066 * The number of objects to add in the ring from the obj_table.
1068 * - n: Actual number of objects enqueued.
1070 static inline int __attribute__((always_inline))
1071 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1074 if (r->prod.sp_enqueue)
1075 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1077 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1081 * Dequeue several objects from a ring (multi-consumers safe). When the request
1082 * objects are more than the available objects, only dequeue the actual number
1085 * This function uses a "compare and set" instruction to move the
1086 * consumer index atomically.
1089 * A pointer to the ring structure.
1091 * A pointer to a table of void * pointers (objects) that will be filled.
1093 * The number of objects to dequeue from the ring to the obj_table.
1095 * - n: Actual number of objects dequeued, 0 if ring is empty
1097 static inline int __attribute__((always_inline))
1098 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1100 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1104 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1105 * request objects are more than the available objects, only dequeue the
1106 * actual number of objects
1109 * A pointer to the ring structure.
1111 * A pointer to a table of void * pointers (objects) that will be filled.
1113 * The number of objects to dequeue from the ring to the obj_table.
1115 * - n: Actual number of objects dequeued, 0 if ring is empty
1117 static inline int __attribute__((always_inline))
1118 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1120 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1124 * Dequeue multiple objects from a ring up to a maximum number.
1126 * This function calls the multi-consumers or the single-consumer
1127 * version, depending on the default behaviour that was specified at
1128 * ring creation time (see flags).
1131 * A pointer to the ring structure.
1133 * A pointer to a table of void * pointers (objects) that will be filled.
1135 * The number of objects to dequeue from the ring to the obj_table.
1137 * - Number of objects dequeued, or a negative error code on error
1139 static inline int __attribute__((always_inline))
1140 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1142 if (r->cons.sc_dequeue)
1143 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1145 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1152 #endif /* _RTE_RING_H_ */