4 * Copyright(c) 2010-2013 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 /* dummy assembly operation to prevent compiler re-ordering of instructions */
177 #define COMPILER_BARRIER() do { asm volatile("" ::: "memory"); } while(0)
179 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
180 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
181 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
182 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
185 * @internal When debug is enabled, store ring statistics.
187 * A pointer to the ring.
189 * The name of the statistics field to increment in the ring.
191 * The number to add to the object-oriented statistics.
193 #ifdef RTE_LIBRTE_RING_DEBUG
194 #define __RING_STAT_ADD(r, name, n) do { \
195 unsigned __lcore_id = rte_lcore_id(); \
196 r->stats[__lcore_id].name##_objs += n; \
197 r->stats[__lcore_id].name##_bulk += 1; \
200 #define __RING_STAT_ADD(r, name, n) do {} while(0)
204 * Create a new ring named *name* in memory.
206 * This function uses ``memzone_reserve()`` to allocate memory. Its size is
207 * set to *count*, which must be a power of two. Water marking is
208 * disabled by default.
209 * Note that the real usable ring size is *count-1* instead of
213 * The name of the ring.
215 * The size of the ring (must be a power of 2).
217 * The *socket_id* argument is the socket identifier in case of
218 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
219 * constraint for the reserved zone.
221 * An OR of the following:
222 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
223 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
224 * is "single-producer". Otherwise, it is "multi-producers".
225 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
226 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
227 * is "single-consumer". Otherwise, it is "multi-consumers".
229 * On success, the pointer to the new allocated ring. NULL on error with
230 * rte_errno set appropriately. Possible errno values include:
231 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
232 * - E_RTE_SECONDARY - function was called from a secondary process instance
233 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring list
234 * - EINVAL - count provided is not a power of 2
235 * - ENOSPC - the maximum number of memzones has already been allocated
236 * - EEXIST - a memzone with the same name already exists
237 * - ENOMEM - no appropriate memory area found in which to create memzone
239 struct rte_ring *rte_ring_create(const char *name, unsigned count,
240 int socket_id, unsigned flags);
243 * Change the high water mark.
245 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
246 * *count* value. The *count* value must be greater than 0 and less
247 * than the ring size.
249 * This function can be called at any time (not necessarily at
253 * A pointer to the ring structure.
255 * The new water mark value.
257 * - 0: Success; water mark changed.
258 * - -EINVAL: Invalid water mark value.
260 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
263 * Dump the status of the ring to the console.
266 * A pointer to the ring structure.
268 void rte_ring_dump(const struct rte_ring *r);
270 /* the actual enqueue of pointers on the ring.
271 * Placed here since identical code needed in both
272 * single and multi producer enqueue functions */
273 #define ENQUEUE_PTRS() do { \
274 const uint32_t size = r->prod.size; \
275 uint32_t idx = prod_head & mask; \
276 if (likely(idx + n < size)) { \
277 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
278 r->ring[idx] = obj_table[i]; \
279 r->ring[idx+1] = obj_table[i+1]; \
280 r->ring[idx+2] = obj_table[i+2]; \
281 r->ring[idx+3] = obj_table[i+3]; \
284 case 3: r->ring[idx++] = obj_table[i++]; \
285 case 2: r->ring[idx++] = obj_table[i++]; \
286 case 1: r->ring[idx++] = obj_table[i++]; \
289 for (i = 0; idx < size; i++, idx++)\
290 r->ring[idx] = obj_table[i]; \
291 for (idx = 0; i < n; i++, idx++) \
292 r->ring[idx] = obj_table[i]; \
296 /* the actual copy of pointers on the ring to obj_table.
297 * Placed here since identical code needed in both
298 * single and multi consumer dequeue functions */
299 #define DEQUEUE_PTRS() do { \
300 uint32_t idx = cons_head & mask; \
301 const uint32_t size = r->cons.size; \
302 if (likely(idx + n < size)) { \
303 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
304 obj_table[i] = r->ring[idx]; \
305 obj_table[i+1] = r->ring[idx+1]; \
306 obj_table[i+2] = r->ring[idx+2]; \
307 obj_table[i+3] = r->ring[idx+3]; \
310 case 3: obj_table[i++] = r->ring[idx++]; \
311 case 2: obj_table[i++] = r->ring[idx++]; \
312 case 1: obj_table[i++] = r->ring[idx++]; \
315 for (i = 0; idx < size; i++, idx++) \
316 obj_table[i] = r->ring[idx]; \
317 for (idx = 0; i < n; i++, idx++) \
318 obj_table[i] = r->ring[idx]; \
323 * @internal Enqueue several objects on the ring (multi-producers safe).
325 * This function uses a "compare and set" instruction to move the
326 * producer index atomically.
329 * A pointer to the ring structure.
331 * A pointer to a table of void * pointers (objects).
333 * The number of objects to add in the ring from the obj_table.
335 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
336 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
338 * Depend on the behavior value
339 * if behavior = RTE_RING_QUEUE_FIXED
340 * - 0: Success; objects enqueue.
341 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
342 * high water mark is exceeded.
343 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
344 * if behavior = RTE_RING_QUEUE_VARIABLE
345 * - n: Actual number of objects enqueued.
347 static inline int __attribute__((always_inline))
348 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
349 unsigned n, enum rte_ring_queue_behavior behavior)
351 uint32_t prod_head, prod_next;
352 uint32_t cons_tail, free_entries;
353 const unsigned max = n;
356 uint32_t mask = r->prod.mask;
359 /* move prod.head atomically */
361 /* Reset n to the initial burst count */
364 prod_head = r->prod.head;
365 cons_tail = r->cons.tail;
366 /* The subtraction is done between two unsigned 32bits value
367 * (the result is always modulo 32 bits even if we have
368 * prod_head > cons_tail). So 'free_entries' is always between 0
369 * and size(ring)-1. */
370 free_entries = (mask + cons_tail - prod_head);
372 /* check that we have enough room in ring */
373 if (unlikely(n > free_entries)) {
374 if (behavior == RTE_RING_QUEUE_FIXED) {
375 __RING_STAT_ADD(r, enq_fail, n);
379 /* No free entry available */
380 if (unlikely(free_entries == 0)) {
381 __RING_STAT_ADD(r, enq_fail, n);
389 prod_next = prod_head + n;
390 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
392 } while (unlikely(success == 0));
394 /* write entries in ring */
398 /* if we exceed the watermark */
399 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
400 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
401 (int)(n | RTE_RING_QUOT_EXCEED);
402 __RING_STAT_ADD(r, enq_quota, n);
405 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
406 __RING_STAT_ADD(r, enq_success, n);
410 * If there are other enqueues in progress that preceeded us,
411 * we need to wait for them to complete
413 while (unlikely(r->prod.tail != prod_head))
416 r->prod.tail = prod_next;
421 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
424 * A pointer to the ring structure.
426 * A pointer to a table of void * pointers (objects).
428 * The number of objects to add in the ring from the obj_table.
430 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
431 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
433 * Depend on the behavior value
434 * if behavior = RTE_RING_QUEUE_FIXED
435 * - 0: Success; objects enqueue.
436 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
437 * high water mark is exceeded.
438 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
439 * if behavior = RTE_RING_QUEUE_VARIABLE
440 * - n: Actual number of objects enqueued.
442 static inline int __attribute__((always_inline))
443 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
444 unsigned n, enum rte_ring_queue_behavior behavior)
446 uint32_t prod_head, cons_tail;
447 uint32_t prod_next, free_entries;
449 uint32_t mask = r->prod.mask;
452 prod_head = r->prod.head;
453 cons_tail = r->cons.tail;
454 /* The subtraction is done between two unsigned 32bits value
455 * (the result is always modulo 32 bits even if we have
456 * prod_head > cons_tail). So 'free_entries' is always between 0
457 * and size(ring)-1. */
458 free_entries = mask + cons_tail - prod_head;
460 /* check that we have enough room in ring */
461 if (unlikely(n > free_entries)) {
462 if (behavior == RTE_RING_QUEUE_FIXED) {
463 __RING_STAT_ADD(r, enq_fail, n);
467 /* No free entry available */
468 if (unlikely(free_entries == 0)) {
469 __RING_STAT_ADD(r, enq_fail, n);
477 prod_next = prod_head + n;
478 r->prod.head = prod_next;
480 /* write entries in ring */
484 /* if we exceed the watermark */
485 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
486 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
487 (int)(n | RTE_RING_QUOT_EXCEED);
488 __RING_STAT_ADD(r, enq_quota, n);
491 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
492 __RING_STAT_ADD(r, enq_success, n);
495 r->prod.tail = prod_next;
500 * @internal Dequeue several objects from a ring (multi-consumers safe). When
501 * the request objects are more than the available objects, only dequeue the
502 * actual number of objects
504 * This function uses a "compare and set" instruction to move the
505 * consumer index atomically.
508 * A pointer to the ring structure.
510 * A pointer to a table of void * pointers (objects) that will be filled.
512 * The number of objects to dequeue from the ring to the obj_table.
514 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
515 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
517 * Depend on the behavior value
518 * if behavior = RTE_RING_QUEUE_FIXED
519 * - 0: Success; objects dequeued.
520 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
522 * if behavior = RTE_RING_QUEUE_VARIABLE
523 * - n: Actual number of objects dequeued.
526 static inline int __attribute__((always_inline))
527 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
528 unsigned n, enum rte_ring_queue_behavior behavior)
530 uint32_t cons_head, prod_tail;
531 uint32_t cons_next, entries;
532 const unsigned max = n;
535 uint32_t mask = r->prod.mask;
537 /* move cons.head atomically */
539 /* Restore n as it may change every loop */
542 cons_head = r->cons.head;
543 prod_tail = r->prod.tail;
544 /* The subtraction is done between two unsigned 32bits value
545 * (the result is always modulo 32 bits even if we have
546 * cons_head > prod_tail). So 'entries' is always between 0
547 * and size(ring)-1. */
548 entries = (prod_tail - cons_head);
550 /* Set the actual entries for dequeue */
552 if (behavior == RTE_RING_QUEUE_FIXED) {
553 __RING_STAT_ADD(r, deq_fail, n);
557 if (unlikely(entries == 0)){
558 __RING_STAT_ADD(r, deq_fail, n);
566 cons_next = cons_head + n;
567 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
569 } while (unlikely(success == 0));
576 * If there are other dequeues in progress that preceded us,
577 * we need to wait for them to complete
579 while (unlikely(r->cons.tail != cons_head))
582 __RING_STAT_ADD(r, deq_success, n);
583 r->cons.tail = cons_next;
585 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
589 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
590 * When the request objects are more than the available objects, only dequeue
591 * the actual number of objects
594 * A pointer to the ring structure.
596 * A pointer to a table of void * pointers (objects) that will be filled.
598 * The number of objects to dequeue from the ring to the obj_table.
600 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
601 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
603 * Depend on the behavior value
604 * if behavior = RTE_RING_QUEUE_FIXED
605 * - 0: Success; objects dequeued.
606 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
608 * if behavior = RTE_RING_QUEUE_VARIABLE
609 * - n: Actual number of objects dequeued.
611 static inline int __attribute__((always_inline))
612 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
613 unsigned n, enum rte_ring_queue_behavior behavior)
615 uint32_t cons_head, prod_tail;
616 uint32_t cons_next, entries;
618 uint32_t mask = r->prod.mask;
620 cons_head = r->cons.head;
621 prod_tail = r->prod.tail;
622 /* The subtraction is done between two unsigned 32bits value
623 * (the result is always modulo 32 bits even if we have
624 * cons_head > prod_tail). So 'entries' is always between 0
625 * and size(ring)-1. */
626 entries = prod_tail - cons_head;
629 if (behavior == RTE_RING_QUEUE_FIXED) {
630 __RING_STAT_ADD(r, deq_fail, n);
634 if (unlikely(entries == 0)){
635 __RING_STAT_ADD(r, deq_fail, n);
643 cons_next = cons_head + n;
644 r->cons.head = cons_next;
650 __RING_STAT_ADD(r, deq_success, n);
651 r->cons.tail = cons_next;
652 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
656 * Enqueue several objects on the ring (multi-producers safe).
658 * This function uses a "compare and set" instruction to move the
659 * producer index atomically.
662 * A pointer to the ring structure.
664 * A pointer to a table of void * pointers (objects).
666 * The number of objects to add in the ring from the obj_table.
668 * - 0: Success; objects enqueue.
669 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
670 * high water mark is exceeded.
671 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
673 static inline int __attribute__((always_inline))
674 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
677 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
681 * Enqueue several objects on a ring (NOT multi-producers safe).
684 * A pointer to the ring structure.
686 * A pointer to a table of void * pointers (objects).
688 * The number of objects to add in the ring from the obj_table.
690 * - 0: Success; objects enqueued.
691 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
692 * high water mark is exceeded.
693 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
695 static inline int __attribute__((always_inline))
696 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
699 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
703 * Enqueue several objects on a ring.
705 * This function calls the multi-producer or the single-producer
706 * version depending on the default behavior that was specified at
707 * ring creation time (see flags).
710 * A pointer to the ring structure.
712 * A pointer to a table of void * pointers (objects).
714 * The number of objects to add in the ring from the obj_table.
716 * - 0: Success; objects enqueued.
717 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
718 * high water mark is exceeded.
719 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
721 static inline int __attribute__((always_inline))
722 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
725 if (r->prod.sp_enqueue)
726 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
728 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
732 * Enqueue one object on a ring (multi-producers safe).
734 * This function uses a "compare and set" instruction to move the
735 * producer index atomically.
738 * A pointer to the ring structure.
740 * A pointer to the object to be added.
742 * - 0: Success; objects enqueued.
743 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
744 * high water mark is exceeded.
745 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
747 static inline int __attribute__((always_inline))
748 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
750 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
754 * Enqueue one object on a ring (NOT multi-producers safe).
757 * A pointer to the ring structure.
759 * A pointer to the object to be added.
761 * - 0: Success; objects enqueued.
762 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
763 * high water mark is exceeded.
764 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
766 static inline int __attribute__((always_inline))
767 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
769 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
773 * Enqueue one object on a ring.
775 * This function calls the multi-producer or the single-producer
776 * version, depending on the default behaviour that was specified at
777 * ring creation time (see flags).
780 * A pointer to the ring structure.
782 * A pointer to the object to be added.
784 * - 0: Success; objects enqueued.
785 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
786 * high water mark is exceeded.
787 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
789 static inline int __attribute__((always_inline))
790 rte_ring_enqueue(struct rte_ring *r, void *obj)
792 if (r->prod.sp_enqueue)
793 return rte_ring_sp_enqueue(r, obj);
795 return rte_ring_mp_enqueue(r, obj);
799 * Dequeue several objects from a ring (multi-consumers safe).
801 * This function uses a "compare and set" instruction to move the
802 * consumer index atomically.
805 * A pointer to the ring structure.
807 * A pointer to a table of void * pointers (objects) that will be filled.
809 * The number of objects to dequeue from the ring to the obj_table.
811 * - 0: Success; objects dequeued.
812 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
815 static inline int __attribute__((always_inline))
816 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
818 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
822 * Dequeue several objects from a ring (NOT multi-consumers safe).
825 * A pointer to the ring structure.
827 * A pointer to a table of void * pointers (objects) that will be filled.
829 * The number of objects to dequeue from the ring to the obj_table,
830 * must be strictly positive.
832 * - 0: Success; objects dequeued.
833 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
836 static inline int __attribute__((always_inline))
837 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
839 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
843 * Dequeue several objects from a ring.
845 * This function calls the multi-consumers or the single-consumer
846 * version, depending on the default behaviour that was specified at
847 * ring creation time (see flags).
850 * A pointer to the ring structure.
852 * A pointer to a table of void * pointers (objects) that will be filled.
854 * The number of objects to dequeue from the ring to the obj_table.
856 * - 0: Success; objects dequeued.
857 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
860 static inline int __attribute__((always_inline))
861 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
863 if (r->cons.sc_dequeue)
864 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
866 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
870 * Dequeue one object from a ring (multi-consumers safe).
872 * This function uses a "compare and set" instruction to move the
873 * consumer index atomically.
876 * A pointer to the ring structure.
878 * A pointer to a void * pointer (object) that will be filled.
880 * - 0: Success; objects dequeued.
881 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
884 static inline int __attribute__((always_inline))
885 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
887 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
891 * Dequeue one object from a ring (NOT multi-consumers safe).
894 * A pointer to the ring structure.
896 * A pointer to a void * pointer (object) that will be filled.
898 * - 0: Success; objects dequeued.
899 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
902 static inline int __attribute__((always_inline))
903 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
905 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
909 * Dequeue one object from a ring.
911 * This function calls the multi-consumers or the single-consumer
912 * version depending on the default behaviour that was specified at
913 * ring creation time (see flags).
916 * A pointer to the ring structure.
918 * A pointer to a void * pointer (object) that will be filled.
920 * - 0: Success, objects dequeued.
921 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
924 static inline int __attribute__((always_inline))
925 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
927 if (r->cons.sc_dequeue)
928 return rte_ring_sc_dequeue(r, obj_p);
930 return rte_ring_mc_dequeue(r, obj_p);
934 * Test if a ring is full.
937 * A pointer to the ring structure.
939 * - 1: The ring is full.
940 * - 0: The ring is not full.
943 rte_ring_full(const struct rte_ring *r)
945 uint32_t prod_tail = r->prod.tail;
946 uint32_t cons_tail = r->cons.tail;
947 return (((cons_tail - prod_tail - 1) & r->prod.mask) == 0);
951 * Test if a ring is empty.
954 * A pointer to the ring structure.
956 * - 1: The ring is empty.
957 * - 0: The ring is not empty.
960 rte_ring_empty(const struct rte_ring *r)
962 uint32_t prod_tail = r->prod.tail;
963 uint32_t cons_tail = r->cons.tail;
964 return !!(cons_tail == prod_tail);
968 * Return the number of entries in a ring.
971 * A pointer to the ring structure.
973 * The number of entries in the ring.
975 static inline unsigned
976 rte_ring_count(const struct rte_ring *r)
978 uint32_t prod_tail = r->prod.tail;
979 uint32_t cons_tail = r->cons.tail;
980 return ((prod_tail - cons_tail) & r->prod.mask);
984 * Return the number of free entries in a ring.
987 * A pointer to the ring structure.
989 * The number of free entries in the ring.
991 static inline unsigned
992 rte_ring_free_count(const struct rte_ring *r)
994 uint32_t prod_tail = r->prod.tail;
995 uint32_t cons_tail = r->cons.tail;
996 return ((cons_tail - prod_tail - 1) & r->prod.mask);
1000 * Dump the status of all rings on the console
1002 void rte_ring_list_dump(void);
1005 * Search a ring from its name
1008 * The name of the ring.
1010 * The pointer to the ring matching the name, or NULL if not found,
1011 * with rte_errno set appropriately. Possible rte_errno values include:
1012 * - ENOENT - required entry not available to return.
1014 struct rte_ring *rte_ring_lookup(const char *name);
1017 * Enqueue several objects on the ring (multi-producers safe).
1019 * This function uses a "compare and set" instruction to move the
1020 * producer index atomically.
1023 * A pointer to the ring structure.
1025 * A pointer to a table of void * pointers (objects).
1027 * The number of objects to add in the ring from the obj_table.
1029 * - n: Actual number of objects enqueued.
1031 static inline int __attribute__((always_inline))
1032 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1035 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1039 * Enqueue several objects on a ring (NOT multi-producers safe).
1042 * A pointer to the ring structure.
1044 * A pointer to a table of void * pointers (objects).
1046 * The number of objects to add in the ring from the obj_table.
1048 * - n: Actual number of objects enqueued.
1050 static inline int __attribute__((always_inline))
1051 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1054 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1058 * Enqueue several objects on a ring.
1060 * This function calls the multi-producer or the single-producer
1061 * version depending on the default behavior that was specified at
1062 * ring creation time (see flags).
1065 * A pointer to the ring structure.
1067 * A pointer to a table of void * pointers (objects).
1069 * The number of objects to add in the ring from the obj_table.
1071 * - n: Actual number of objects enqueued.
1073 static inline int __attribute__((always_inline))
1074 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1077 if (r->prod.sp_enqueue)
1078 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1080 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1084 * Dequeue several objects from a ring (multi-consumers safe). When the request
1085 * objects are more than the available objects, only dequeue the actual number
1088 * This function uses a "compare and set" instruction to move the
1089 * consumer index atomically.
1092 * A pointer to the ring structure.
1094 * A pointer to a table of void * pointers (objects) that will be filled.
1096 * The number of objects to dequeue from the ring to the obj_table.
1098 * - n: Actual number of objects dequeued, 0 if ring is empty
1100 static inline int __attribute__((always_inline))
1101 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1103 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1107 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1108 * request objects are more than the available objects, only dequeue the
1109 * actual number of objects
1112 * A pointer to the ring structure.
1114 * A pointer to a table of void * pointers (objects) that will be filled.
1116 * The number of objects to dequeue from the ring to the obj_table.
1118 * - n: Actual number of objects dequeued, 0 if ring is empty
1120 static inline int __attribute__((always_inline))
1121 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1123 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1127 * Dequeue multiple objects from a ring up to a maximum number.
1129 * This function calls the multi-consumers or the single-consumer
1130 * version, depending on the default behaviour that was specified at
1131 * ring creation time (see flags).
1134 * A pointer to the ring structure.
1136 * A pointer to a table of void * pointers (objects) that will be filled.
1138 * The number of objects to dequeue from the ring to the obj_table.
1140 * - Number of objects dequeued, or a negative error code on error
1142 static inline int __attribute__((always_inline))
1143 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1145 if (r->cons.sc_dequeue)
1146 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1148 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1155 #endif /* _RTE_RING_H_ */