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. */
127 #define RTE_RING_MZ_PREFIX "RG_"
130 * An RTE ring structure.
132 * The producer and the consumer have a head and a tail index. The particularity
133 * of these index is that they are not between 0 and size(ring). These indexes
134 * are between 0 and 2^32, and we mask their value when we access the ring[]
135 * field. Thanks to this assumption, we can do subtractions between 2 index
136 * values in a modulo-32bit base: that's why the overflow of the indexes is not
140 TAILQ_ENTRY(rte_ring) next; /**< Next in list. */
142 char name[RTE_RING_NAMESIZE]; /**< Name of the ring. */
143 int flags; /**< Flags supplied at creation. */
145 /** Ring producer status. */
147 uint32_t watermark; /**< Maximum items before EDQUOT. */
148 uint32_t sp_enqueue; /**< True, if single producer. */
149 uint32_t size; /**< Size of ring. */
150 uint32_t mask; /**< Mask (size-1) of ring. */
151 volatile uint32_t head; /**< Producer head. */
152 volatile uint32_t tail; /**< Producer tail. */
153 } prod __rte_cache_aligned;
155 /** Ring consumer status. */
157 uint32_t sc_dequeue; /**< True, if single consumer. */
158 uint32_t size; /**< Size of the ring. */
159 uint32_t mask; /**< Mask (size-1) of ring. */
160 volatile uint32_t head; /**< Consumer head. */
161 volatile uint32_t tail; /**< Consumer tail. */
162 #ifdef RTE_RING_SPLIT_PROD_CONS
163 } cons __rte_cache_aligned;
168 #ifdef RTE_LIBRTE_RING_DEBUG
169 struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
172 void * ring[0] __rte_cache_aligned; /**< Memory space of ring starts here.
173 * not volatile so need to be careful
174 * about compiler re-ordering */
177 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
178 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
179 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
180 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
183 * @internal When debug is enabled, store ring statistics.
185 * A pointer to the ring.
187 * The name of the statistics field to increment in the ring.
189 * The number to add to the object-oriented statistics.
191 #ifdef RTE_LIBRTE_RING_DEBUG
192 #define __RING_STAT_ADD(r, name, n) do { \
193 unsigned __lcore_id = rte_lcore_id(); \
194 r->stats[__lcore_id].name##_objs += n; \
195 r->stats[__lcore_id].name##_bulk += 1; \
198 #define __RING_STAT_ADD(r, name, n) do {} while(0)
202 * Calculate the memory size needed for a ring
204 * This function returns the number of bytes needed for a ring, given
205 * the number of elements in it. This value is the sum of the size of
206 * the structure rte_ring and the size of the memory needed by the
207 * objects pointers. The value is aligned to a cache line size.
210 * The number of elements in the ring (must be a power of 2).
212 * - The memory size needed for the ring on success.
213 * - -EINVAL if count is not a power of 2.
215 ssize_t rte_ring_get_memsize(unsigned count);
218 * Initialize a ring structure.
220 * Initialize a ring structure in memory pointed by "r". The size of the
221 * memory area must be large enough to store the ring structure and the
222 * object table. It is advised to use rte_ring_get_memsize() to get the
225 * The ring size is set to *count*, which must be a power of two. Water
226 * marking is disabled by default. The real usable ring size is
227 * *count-1* instead of *count* to differentiate a free ring from an
230 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
231 * memory given by the caller may not be shareable among dpdk
235 * The pointer to the ring structure followed by the objects table.
237 * The name of the ring.
239 * The number of elements in the ring (must be a power of 2).
241 * An OR of the following:
242 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
243 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
244 * is "single-producer". Otherwise, it is "multi-producers".
245 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
246 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
247 * is "single-consumer". Otherwise, it is "multi-consumers".
249 * 0 on success, or a negative value on error.
251 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
255 * Create a new ring named *name* in memory.
257 * This function uses ``memzone_reserve()`` to allocate memory. Then it
258 * calls rte_ring_init() to initialize an empty ring.
260 * The new ring size is set to *count*, which must be a power of
261 * two. Water marking is disabled by default. The real usable ring size
262 * is *count-1* instead of *count* to differentiate a free ring from an
265 * The ring is added in RTE_TAILQ_RING list.
268 * The name of the ring.
270 * The size of the ring (must be a power of 2).
272 * The *socket_id* argument is the socket identifier in case of
273 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
274 * constraint for the reserved zone.
276 * An OR of the following:
277 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
278 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
279 * is "single-producer". Otherwise, it is "multi-producers".
280 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
281 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
282 * is "single-consumer". Otherwise, it is "multi-consumers".
284 * On success, the pointer to the new allocated ring. NULL on error with
285 * rte_errno set appropriately. Possible errno values include:
286 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
287 * - E_RTE_SECONDARY - function was called from a secondary process instance
288 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring list
289 * - EINVAL - count provided is not a power of 2
290 * - ENOSPC - the maximum number of memzones has already been allocated
291 * - EEXIST - a memzone with the same name already exists
292 * - ENOMEM - no appropriate memory area found in which to create memzone
294 struct rte_ring *rte_ring_create(const char *name, unsigned count,
295 int socket_id, unsigned flags);
298 * Change the high water mark.
300 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
301 * *count* value. The *count* value must be greater than 0 and less
302 * than the ring size.
304 * This function can be called at any time (not necessarily at
308 * A pointer to the ring structure.
310 * The new water mark value.
312 * - 0: Success; water mark changed.
313 * - -EINVAL: Invalid water mark value.
315 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
318 * Dump the status of the ring to the console.
321 * A pointer to the ring structure.
323 void rte_ring_dump(const struct rte_ring *r);
325 /* the actual enqueue of pointers on the ring.
326 * Placed here since identical code needed in both
327 * single and multi producer enqueue functions */
328 #define ENQUEUE_PTRS() do { \
329 const uint32_t size = r->prod.size; \
330 uint32_t idx = prod_head & mask; \
331 if (likely(idx + n < size)) { \
332 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
333 r->ring[idx] = obj_table[i]; \
334 r->ring[idx+1] = obj_table[i+1]; \
335 r->ring[idx+2] = obj_table[i+2]; \
336 r->ring[idx+3] = obj_table[i+3]; \
339 case 3: r->ring[idx++] = obj_table[i++]; \
340 case 2: r->ring[idx++] = obj_table[i++]; \
341 case 1: r->ring[idx++] = obj_table[i++]; \
344 for (i = 0; idx < size; i++, idx++)\
345 r->ring[idx] = obj_table[i]; \
346 for (idx = 0; i < n; i++, idx++) \
347 r->ring[idx] = obj_table[i]; \
351 /* the actual copy of pointers on the ring to obj_table.
352 * Placed here since identical code needed in both
353 * single and multi consumer dequeue functions */
354 #define DEQUEUE_PTRS() do { \
355 uint32_t idx = cons_head & mask; \
356 const uint32_t size = r->cons.size; \
357 if (likely(idx + n < size)) { \
358 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
359 obj_table[i] = r->ring[idx]; \
360 obj_table[i+1] = r->ring[idx+1]; \
361 obj_table[i+2] = r->ring[idx+2]; \
362 obj_table[i+3] = r->ring[idx+3]; \
365 case 3: obj_table[i++] = r->ring[idx++]; \
366 case 2: obj_table[i++] = r->ring[idx++]; \
367 case 1: obj_table[i++] = r->ring[idx++]; \
370 for (i = 0; idx < size; i++, idx++) \
371 obj_table[i] = r->ring[idx]; \
372 for (idx = 0; i < n; i++, idx++) \
373 obj_table[i] = r->ring[idx]; \
378 * @internal Enqueue several objects on the ring (multi-producers safe).
380 * This function uses a "compare and set" instruction to move the
381 * producer index atomically.
384 * A pointer to the ring structure.
386 * A pointer to a table of void * pointers (objects).
388 * The number of objects to add in the ring from the obj_table.
390 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
391 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
393 * Depend on the behavior value
394 * if behavior = RTE_RING_QUEUE_FIXED
395 * - 0: Success; objects enqueue.
396 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
397 * high water mark is exceeded.
398 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
399 * if behavior = RTE_RING_QUEUE_VARIABLE
400 * - n: Actual number of objects enqueued.
402 static inline int __attribute__((always_inline))
403 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
404 unsigned n, enum rte_ring_queue_behavior behavior)
406 uint32_t prod_head, prod_next;
407 uint32_t cons_tail, free_entries;
408 const unsigned max = n;
411 uint32_t mask = r->prod.mask;
414 /* move prod.head atomically */
416 /* Reset n to the initial burst count */
419 prod_head = r->prod.head;
420 cons_tail = r->cons.tail;
421 /* The subtraction is done between two unsigned 32bits value
422 * (the result is always modulo 32 bits even if we have
423 * prod_head > cons_tail). So 'free_entries' is always between 0
424 * and size(ring)-1. */
425 free_entries = (mask + cons_tail - prod_head);
427 /* check that we have enough room in ring */
428 if (unlikely(n > free_entries)) {
429 if (behavior == RTE_RING_QUEUE_FIXED) {
430 __RING_STAT_ADD(r, enq_fail, n);
434 /* No free entry available */
435 if (unlikely(free_entries == 0)) {
436 __RING_STAT_ADD(r, enq_fail, n);
444 prod_next = prod_head + n;
445 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
447 } while (unlikely(success == 0));
449 /* write entries in ring */
451 rte_compiler_barrier();
453 /* if we exceed the watermark */
454 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
455 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
456 (int)(n | RTE_RING_QUOT_EXCEED);
457 __RING_STAT_ADD(r, enq_quota, n);
460 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
461 __RING_STAT_ADD(r, enq_success, n);
465 * If there are other enqueues in progress that preceeded us,
466 * we need to wait for them to complete
468 while (unlikely(r->prod.tail != prod_head))
471 r->prod.tail = prod_next;
476 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
479 * A pointer to the ring structure.
481 * A pointer to a table of void * pointers (objects).
483 * The number of objects to add in the ring from the obj_table.
485 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
486 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
488 * Depend on the behavior value
489 * if behavior = RTE_RING_QUEUE_FIXED
490 * - 0: Success; objects enqueue.
491 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
492 * high water mark is exceeded.
493 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
494 * if behavior = RTE_RING_QUEUE_VARIABLE
495 * - n: Actual number of objects enqueued.
497 static inline int __attribute__((always_inline))
498 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
499 unsigned n, enum rte_ring_queue_behavior behavior)
501 uint32_t prod_head, cons_tail;
502 uint32_t prod_next, free_entries;
504 uint32_t mask = r->prod.mask;
507 prod_head = r->prod.head;
508 cons_tail = r->cons.tail;
509 /* The subtraction is done between two unsigned 32bits value
510 * (the result is always modulo 32 bits even if we have
511 * prod_head > cons_tail). So 'free_entries' is always between 0
512 * and size(ring)-1. */
513 free_entries = mask + cons_tail - prod_head;
515 /* check that we have enough room in ring */
516 if (unlikely(n > free_entries)) {
517 if (behavior == RTE_RING_QUEUE_FIXED) {
518 __RING_STAT_ADD(r, enq_fail, n);
522 /* No free entry available */
523 if (unlikely(free_entries == 0)) {
524 __RING_STAT_ADD(r, enq_fail, n);
532 prod_next = prod_head + n;
533 r->prod.head = prod_next;
535 /* write entries in ring */
537 rte_compiler_barrier();
539 /* if we exceed the watermark */
540 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
541 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
542 (int)(n | RTE_RING_QUOT_EXCEED);
543 __RING_STAT_ADD(r, enq_quota, n);
546 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
547 __RING_STAT_ADD(r, enq_success, n);
550 r->prod.tail = prod_next;
555 * @internal Dequeue several objects from a ring (multi-consumers safe). When
556 * the request objects are more than the available objects, only dequeue the
557 * actual number of objects
559 * This function uses a "compare and set" instruction to move the
560 * consumer index atomically.
563 * A pointer to the ring structure.
565 * A pointer to a table of void * pointers (objects) that will be filled.
567 * The number of objects to dequeue from the ring to the obj_table.
569 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
570 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
572 * Depend on the behavior value
573 * if behavior = RTE_RING_QUEUE_FIXED
574 * - 0: Success; objects dequeued.
575 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
577 * if behavior = RTE_RING_QUEUE_VARIABLE
578 * - n: Actual number of objects dequeued.
581 static inline int __attribute__((always_inline))
582 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
583 unsigned n, enum rte_ring_queue_behavior behavior)
585 uint32_t cons_head, prod_tail;
586 uint32_t cons_next, entries;
587 const unsigned max = n;
590 uint32_t mask = r->prod.mask;
592 /* move cons.head atomically */
594 /* Restore n as it may change every loop */
597 cons_head = r->cons.head;
598 prod_tail = r->prod.tail;
599 /* The subtraction is done between two unsigned 32bits value
600 * (the result is always modulo 32 bits even if we have
601 * cons_head > prod_tail). So 'entries' is always between 0
602 * and size(ring)-1. */
603 entries = (prod_tail - cons_head);
605 /* Set the actual entries for dequeue */
607 if (behavior == RTE_RING_QUEUE_FIXED) {
608 __RING_STAT_ADD(r, deq_fail, n);
612 if (unlikely(entries == 0)){
613 __RING_STAT_ADD(r, deq_fail, n);
621 cons_next = cons_head + n;
622 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
624 } while (unlikely(success == 0));
628 rte_compiler_barrier();
631 * If there are other dequeues in progress that preceded us,
632 * we need to wait for them to complete
634 while (unlikely(r->cons.tail != cons_head))
637 __RING_STAT_ADD(r, deq_success, n);
638 r->cons.tail = cons_next;
640 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
644 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
645 * When the request objects are more than the available objects, only dequeue
646 * the actual number of objects
649 * A pointer to the ring structure.
651 * A pointer to a table of void * pointers (objects) that will be filled.
653 * The number of objects to dequeue from the ring to the obj_table.
655 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
656 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
658 * Depend on the behavior value
659 * if behavior = RTE_RING_QUEUE_FIXED
660 * - 0: Success; objects dequeued.
661 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
663 * if behavior = RTE_RING_QUEUE_VARIABLE
664 * - n: Actual number of objects dequeued.
666 static inline int __attribute__((always_inline))
667 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
668 unsigned n, enum rte_ring_queue_behavior behavior)
670 uint32_t cons_head, prod_tail;
671 uint32_t cons_next, entries;
673 uint32_t mask = r->prod.mask;
675 cons_head = r->cons.head;
676 prod_tail = r->prod.tail;
677 /* The subtraction is done between two unsigned 32bits value
678 * (the result is always modulo 32 bits even if we have
679 * cons_head > prod_tail). So 'entries' is always between 0
680 * and size(ring)-1. */
681 entries = prod_tail - cons_head;
684 if (behavior == RTE_RING_QUEUE_FIXED) {
685 __RING_STAT_ADD(r, deq_fail, n);
689 if (unlikely(entries == 0)){
690 __RING_STAT_ADD(r, deq_fail, n);
698 cons_next = cons_head + n;
699 r->cons.head = cons_next;
703 rte_compiler_barrier();
705 __RING_STAT_ADD(r, deq_success, n);
706 r->cons.tail = cons_next;
707 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
711 * Enqueue several objects on the ring (multi-producers safe).
713 * This function uses a "compare and set" instruction to move the
714 * producer index atomically.
717 * A pointer to the ring structure.
719 * A pointer to a table of void * pointers (objects).
721 * The number of objects to add in the ring from the obj_table.
723 * - 0: Success; objects enqueue.
724 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
725 * high water mark is exceeded.
726 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
728 static inline int __attribute__((always_inline))
729 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
732 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
736 * Enqueue several objects on a ring (NOT multi-producers safe).
739 * A pointer to the ring structure.
741 * A pointer to a table of void * pointers (objects).
743 * The number of objects to add in the ring from the obj_table.
745 * - 0: Success; objects enqueued.
746 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
747 * high water mark is exceeded.
748 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
750 static inline int __attribute__((always_inline))
751 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
754 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
758 * Enqueue several objects on a ring.
760 * This function calls the multi-producer or the single-producer
761 * version depending on the default behavior that was specified at
762 * ring creation time (see flags).
765 * A pointer to the ring structure.
767 * A pointer to a table of void * pointers (objects).
769 * The number of objects to add in the ring from the obj_table.
771 * - 0: Success; objects enqueued.
772 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
773 * high water mark is exceeded.
774 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
776 static inline int __attribute__((always_inline))
777 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
780 if (r->prod.sp_enqueue)
781 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
783 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
787 * Enqueue one object on a ring (multi-producers safe).
789 * This function uses a "compare and set" instruction to move the
790 * producer index atomically.
793 * A pointer to the ring structure.
795 * A pointer to the object to be added.
797 * - 0: Success; objects enqueued.
798 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
799 * high water mark is exceeded.
800 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
802 static inline int __attribute__((always_inline))
803 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
805 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
809 * Enqueue one object on a ring (NOT multi-producers safe).
812 * A pointer to the ring structure.
814 * A pointer to the object to be added.
816 * - 0: Success; objects enqueued.
817 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
818 * high water mark is exceeded.
819 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
821 static inline int __attribute__((always_inline))
822 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
824 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
828 * Enqueue one object on a ring.
830 * This function calls the multi-producer or the single-producer
831 * version, depending on the default behaviour that was specified at
832 * ring creation time (see flags).
835 * A pointer to the ring structure.
837 * A pointer to the object to be added.
839 * - 0: Success; objects enqueued.
840 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
841 * high water mark is exceeded.
842 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
844 static inline int __attribute__((always_inline))
845 rte_ring_enqueue(struct rte_ring *r, void *obj)
847 if (r->prod.sp_enqueue)
848 return rte_ring_sp_enqueue(r, obj);
850 return rte_ring_mp_enqueue(r, obj);
854 * Dequeue several objects from a ring (multi-consumers safe).
856 * This function uses a "compare and set" instruction to move the
857 * consumer index atomically.
860 * A pointer to the ring structure.
862 * A pointer to a table of void * pointers (objects) that will be filled.
864 * The number of objects to dequeue from the ring to the obj_table.
866 * - 0: Success; objects dequeued.
867 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
870 static inline int __attribute__((always_inline))
871 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
873 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
877 * Dequeue several objects from a ring (NOT multi-consumers safe).
880 * A pointer to the ring structure.
882 * A pointer to a table of void * pointers (objects) that will be filled.
884 * The number of objects to dequeue from the ring to the obj_table,
885 * must be strictly positive.
887 * - 0: Success; objects dequeued.
888 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
891 static inline int __attribute__((always_inline))
892 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
894 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
898 * Dequeue several objects from a ring.
900 * This function calls the multi-consumers or the single-consumer
901 * version, depending on the default behaviour that was specified at
902 * ring creation time (see flags).
905 * A pointer to the ring structure.
907 * A pointer to a table of void * pointers (objects) that will be filled.
909 * The number of objects to dequeue from the ring to the obj_table.
911 * - 0: Success; objects dequeued.
912 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
915 static inline int __attribute__((always_inline))
916 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
918 if (r->cons.sc_dequeue)
919 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
921 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
925 * Dequeue one object from a ring (multi-consumers safe).
927 * This function uses a "compare and set" instruction to move the
928 * consumer index atomically.
931 * A pointer to the ring structure.
933 * A pointer to a void * pointer (object) that will be filled.
935 * - 0: Success; objects dequeued.
936 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
939 static inline int __attribute__((always_inline))
940 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
942 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
946 * Dequeue one object from a ring (NOT multi-consumers safe).
949 * A pointer to the ring structure.
951 * A pointer to a void * pointer (object) that will be filled.
953 * - 0: Success; objects dequeued.
954 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
957 static inline int __attribute__((always_inline))
958 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
960 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
964 * Dequeue one object from a ring.
966 * This function calls the multi-consumers or the single-consumer
967 * version depending on the default behaviour that was specified at
968 * ring creation time (see flags).
971 * A pointer to the ring structure.
973 * A pointer to a void * pointer (object) that will be filled.
975 * - 0: Success, objects dequeued.
976 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
979 static inline int __attribute__((always_inline))
980 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
982 if (r->cons.sc_dequeue)
983 return rte_ring_sc_dequeue(r, obj_p);
985 return rte_ring_mc_dequeue(r, obj_p);
989 * Test if a ring is full.
992 * A pointer to the ring structure.
994 * - 1: The ring is full.
995 * - 0: The ring is not full.
998 rte_ring_full(const struct rte_ring *r)
1000 uint32_t prod_tail = r->prod.tail;
1001 uint32_t cons_tail = r->cons.tail;
1002 return (((cons_tail - prod_tail - 1) & r->prod.mask) == 0);
1006 * Test if a ring is empty.
1009 * A pointer to the ring structure.
1011 * - 1: The ring is empty.
1012 * - 0: The ring is not empty.
1015 rte_ring_empty(const struct rte_ring *r)
1017 uint32_t prod_tail = r->prod.tail;
1018 uint32_t cons_tail = r->cons.tail;
1019 return !!(cons_tail == prod_tail);
1023 * Return the number of entries in a ring.
1026 * A pointer to the ring structure.
1028 * The number of entries in the ring.
1030 static inline unsigned
1031 rte_ring_count(const struct rte_ring *r)
1033 uint32_t prod_tail = r->prod.tail;
1034 uint32_t cons_tail = r->cons.tail;
1035 return ((prod_tail - cons_tail) & r->prod.mask);
1039 * Return the number of free entries in a ring.
1042 * A pointer to the ring structure.
1044 * The number of free entries in the ring.
1046 static inline unsigned
1047 rte_ring_free_count(const struct rte_ring *r)
1049 uint32_t prod_tail = r->prod.tail;
1050 uint32_t cons_tail = r->cons.tail;
1051 return ((cons_tail - prod_tail - 1) & r->prod.mask);
1055 * Dump the status of all rings on the console
1057 void rte_ring_list_dump(void);
1060 * Search a ring from its name
1063 * The name of the ring.
1065 * The pointer to the ring matching the name, or NULL if not found,
1066 * with rte_errno set appropriately. Possible rte_errno values include:
1067 * - ENOENT - required entry not available to return.
1069 struct rte_ring *rte_ring_lookup(const char *name);
1072 * Enqueue several objects on the ring (multi-producers safe).
1074 * This function uses a "compare and set" instruction to move the
1075 * producer index atomically.
1078 * A pointer to the ring structure.
1080 * A pointer to a table of void * pointers (objects).
1082 * The number of objects to add in the ring from the obj_table.
1084 * - n: Actual number of objects enqueued.
1086 static inline int __attribute__((always_inline))
1087 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1090 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1094 * Enqueue several objects on a ring (NOT multi-producers safe).
1097 * A pointer to the ring structure.
1099 * A pointer to a table of void * pointers (objects).
1101 * The number of objects to add in the ring from the obj_table.
1103 * - n: Actual number of objects enqueued.
1105 static inline int __attribute__((always_inline))
1106 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1109 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1113 * Enqueue several objects on a ring.
1115 * This function calls the multi-producer or the single-producer
1116 * version depending on the default behavior that was specified at
1117 * ring creation time (see flags).
1120 * A pointer to the ring structure.
1122 * A pointer to a table of void * pointers (objects).
1124 * The number of objects to add in the ring from the obj_table.
1126 * - n: Actual number of objects enqueued.
1128 static inline int __attribute__((always_inline))
1129 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1132 if (r->prod.sp_enqueue)
1133 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1135 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1139 * Dequeue several objects from a ring (multi-consumers safe). When the request
1140 * objects are more than the available objects, only dequeue the actual number
1143 * This function uses a "compare and set" instruction to move the
1144 * consumer index atomically.
1147 * A pointer to the ring structure.
1149 * A pointer to a table of void * pointers (objects) that will be filled.
1151 * The number of objects to dequeue from the ring to the obj_table.
1153 * - n: Actual number of objects dequeued, 0 if ring is empty
1155 static inline int __attribute__((always_inline))
1156 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1158 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1162 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1163 * request objects are more than the available objects, only dequeue the
1164 * actual number of objects
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 * - n: Actual number of objects dequeued, 0 if ring is empty
1175 static inline int __attribute__((always_inline))
1176 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1178 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1182 * Dequeue multiple objects from a ring up to a maximum number.
1184 * This function calls the multi-consumers or the single-consumer
1185 * version, depending on the default behaviour that was specified at
1186 * ring creation time (see flags).
1189 * A pointer to the ring structure.
1191 * A pointer to a table of void * pointers (objects) that will be filled.
1193 * The number of objects to dequeue from the ring to the obj_table.
1195 * - Number of objects dequeued, or a negative error code on error
1197 static inline int __attribute__((always_inline))
1198 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1200 if (r->cons.sc_dequeue)
1201 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1203 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1210 #endif /* _RTE_RING_H_ */