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.
96 #include <sys/queue.h>
98 #include <rte_common.h>
99 #include <rte_memory.h>
100 #include <rte_lcore.h>
101 #include <rte_atomic.h>
102 #include <rte_branch_prediction.h>
103 #include <rte_memzone.h>
105 #define RTE_TAILQ_RING_NAME "RTE_RING"
107 enum rte_ring_queue_behavior {
108 RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
109 RTE_RING_QUEUE_VARIABLE /* Enq/Deq as many items as possible from ring */
112 #ifdef RTE_LIBRTE_RING_DEBUG
114 * A structure that stores the ring statistics (per-lcore).
116 struct rte_ring_debug_stats {
117 uint64_t enq_success_bulk; /**< Successful enqueues number. */
118 uint64_t enq_success_objs; /**< Objects successfully enqueued. */
119 uint64_t enq_quota_bulk; /**< Successful enqueues above watermark. */
120 uint64_t enq_quota_objs; /**< Objects enqueued above watermark. */
121 uint64_t enq_fail_bulk; /**< Failed enqueues number. */
122 uint64_t enq_fail_objs; /**< Objects that failed to be enqueued. */
123 uint64_t deq_success_bulk; /**< Successful dequeues number. */
124 uint64_t deq_success_objs; /**< Objects successfully dequeued. */
125 uint64_t deq_fail_bulk; /**< Failed dequeues number. */
126 uint64_t deq_fail_objs; /**< Objects that failed to be dequeued. */
127 } __rte_cache_aligned;
130 #define RTE_RING_MZ_PREFIX "RG_"
131 /**< The maximum length of a ring name. */
132 #define RTE_RING_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
133 sizeof(RTE_RING_MZ_PREFIX) + 1)
135 #ifndef RTE_RING_PAUSE_REP_COUNT
136 #define RTE_RING_PAUSE_REP_COUNT 0 /**< Yield after pause num of times, no yield
137 * if RTE_RING_PAUSE_REP not defined. */
140 struct rte_memzone; /* forward declaration, so as not to require memzone.h */
142 #if RTE_CACHE_LINE_SIZE < 128
143 #define PROD_ALIGN (RTE_CACHE_LINE_SIZE * 2)
144 #define CONS_ALIGN (RTE_CACHE_LINE_SIZE * 2)
146 #define PROD_ALIGN RTE_CACHE_LINE_SIZE
147 #define CONS_ALIGN RTE_CACHE_LINE_SIZE
151 * An RTE ring structure.
153 * The producer and the consumer have a head and a tail index. The particularity
154 * of these index is that they are not between 0 and size(ring). These indexes
155 * are between 0 and 2^32, and we mask their value when we access the ring[]
156 * field. Thanks to this assumption, we can do subtractions between 2 index
157 * values in a modulo-32bit base: that's why the overflow of the indexes is not
162 * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
163 * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
164 * next time the ABI changes
166 char name[RTE_MEMZONE_NAMESIZE]; /**< Name of the ring. */
167 int flags; /**< Flags supplied at creation. */
168 const struct rte_memzone *memzone;
169 /**< Memzone, if any, containing the rte_ring */
171 /** Ring producer status. */
173 uint32_t watermark; /**< Maximum items before EDQUOT. */
174 uint32_t sp_enqueue; /**< True, if single producer. */
175 uint32_t size; /**< Size of ring. */
176 uint32_t mask; /**< Mask (size-1) of ring. */
177 volatile uint32_t head; /**< Producer head. */
178 volatile uint32_t tail; /**< Producer tail. */
179 } prod __rte_aligned(PROD_ALIGN);
181 /** Ring consumer status. */
183 uint32_t sc_dequeue; /**< True, if single consumer. */
184 uint32_t size; /**< Size of the ring. */
185 uint32_t mask; /**< Mask (size-1) of ring. */
186 volatile uint32_t head; /**< Consumer head. */
187 volatile uint32_t tail; /**< Consumer tail. */
188 } cons __rte_aligned(CONS_ALIGN);
190 #ifdef RTE_LIBRTE_RING_DEBUG
191 struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
194 void *ring[] __rte_cache_aligned; /**< Memory space of ring starts here.
195 * not volatile so need to be careful
196 * about compiler re-ordering */
199 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
200 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
201 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
202 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
205 * @internal When debug is enabled, store ring statistics.
207 * A pointer to the ring.
209 * The name of the statistics field to increment in the ring.
211 * The number to add to the object-oriented statistics.
213 #ifdef RTE_LIBRTE_RING_DEBUG
214 #define __RING_STAT_ADD(r, name, n) do { \
215 unsigned __lcore_id = rte_lcore_id(); \
216 if (__lcore_id < RTE_MAX_LCORE) { \
217 r->stats[__lcore_id].name##_objs += n; \
218 r->stats[__lcore_id].name##_bulk += 1; \
222 #define __RING_STAT_ADD(r, name, n) do {} while(0)
226 * Calculate the memory size needed for a ring
228 * This function returns the number of bytes needed for a ring, given
229 * the number of elements in it. This value is the sum of the size of
230 * the structure rte_ring and the size of the memory needed by the
231 * objects pointers. The value is aligned to a cache line size.
234 * The number of elements in the ring (must be a power of 2).
236 * - The memory size needed for the ring on success.
237 * - -EINVAL if count is not a power of 2.
239 ssize_t rte_ring_get_memsize(unsigned count);
242 * Initialize a ring structure.
244 * Initialize a ring structure in memory pointed by "r". The size of the
245 * memory area must be large enough to store the ring structure and the
246 * object table. It is advised to use rte_ring_get_memsize() to get the
249 * The ring size is set to *count*, which must be a power of two. Water
250 * marking is disabled by default. The real usable ring size is
251 * *count-1* instead of *count* to differentiate a free ring from an
254 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
255 * memory given by the caller may not be shareable among dpdk
259 * The pointer to the ring structure followed by the objects table.
261 * The name of the ring.
263 * The number of elements in the ring (must be a power of 2).
265 * An OR of the following:
266 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
267 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
268 * is "single-producer". Otherwise, it is "multi-producers".
269 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
270 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
271 * is "single-consumer". Otherwise, it is "multi-consumers".
273 * 0 on success, or a negative value on error.
275 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
279 * Create a new ring named *name* in memory.
281 * This function uses ``memzone_reserve()`` to allocate memory. Then it
282 * calls rte_ring_init() to initialize an empty ring.
284 * The new ring size is set to *count*, which must be a power of
285 * two. Water marking is disabled by default. The real usable ring size
286 * is *count-1* instead of *count* to differentiate a free ring from an
289 * The ring is added in RTE_TAILQ_RING list.
292 * The name of the ring.
294 * The size of the ring (must be a power of 2).
296 * The *socket_id* argument is the socket identifier in case of
297 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
298 * constraint for the reserved zone.
300 * An OR of the following:
301 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
302 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
303 * is "single-producer". Otherwise, it is "multi-producers".
304 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
305 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
306 * is "single-consumer". Otherwise, it is "multi-consumers".
308 * On success, the pointer to the new allocated ring. NULL on error with
309 * rte_errno set appropriately. Possible errno values include:
310 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
311 * - E_RTE_SECONDARY - function was called from a secondary process instance
312 * - EINVAL - count provided is not a power of 2
313 * - ENOSPC - the maximum number of memzones has already been allocated
314 * - EEXIST - a memzone with the same name already exists
315 * - ENOMEM - no appropriate memory area found in which to create memzone
317 struct rte_ring *rte_ring_create(const char *name, unsigned count,
318 int socket_id, unsigned flags);
320 * De-allocate all memory used by the ring.
325 void rte_ring_free(struct rte_ring *r);
328 * Change the high water mark.
330 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
331 * *count* value. The *count* value must be greater than 0 and less
332 * than the ring size.
334 * This function can be called at any time (not necessarily at
338 * A pointer to the ring structure.
340 * The new water mark value.
342 * - 0: Success; water mark changed.
343 * - -EINVAL: Invalid water mark value.
345 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
348 * Dump the status of the ring to a file.
351 * A pointer to a file for output
353 * A pointer to the ring structure.
355 void rte_ring_dump(FILE *f, const struct rte_ring *r);
357 /* the actual enqueue of pointers on the ring.
358 * Placed here since identical code needed in both
359 * single and multi producer enqueue functions */
360 #define ENQUEUE_PTRS() do { \
361 const uint32_t size = r->prod.size; \
362 uint32_t idx = prod_head & mask; \
363 if (likely(idx + n < size)) { \
364 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
365 r->ring[idx] = obj_table[i]; \
366 r->ring[idx+1] = obj_table[i+1]; \
367 r->ring[idx+2] = obj_table[i+2]; \
368 r->ring[idx+3] = obj_table[i+3]; \
371 case 3: r->ring[idx++] = obj_table[i++]; \
372 case 2: r->ring[idx++] = obj_table[i++]; \
373 case 1: r->ring[idx++] = obj_table[i++]; \
376 for (i = 0; idx < size; i++, idx++)\
377 r->ring[idx] = obj_table[i]; \
378 for (idx = 0; i < n; i++, idx++) \
379 r->ring[idx] = obj_table[i]; \
383 /* the actual copy of pointers on the ring to obj_table.
384 * Placed here since identical code needed in both
385 * single and multi consumer dequeue functions */
386 #define DEQUEUE_PTRS() do { \
387 uint32_t idx = cons_head & mask; \
388 const uint32_t size = r->cons.size; \
389 if (likely(idx + n < size)) { \
390 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
391 obj_table[i] = r->ring[idx]; \
392 obj_table[i+1] = r->ring[idx+1]; \
393 obj_table[i+2] = r->ring[idx+2]; \
394 obj_table[i+3] = r->ring[idx+3]; \
397 case 3: obj_table[i++] = r->ring[idx++]; \
398 case 2: obj_table[i++] = r->ring[idx++]; \
399 case 1: obj_table[i++] = r->ring[idx++]; \
402 for (i = 0; idx < size; i++, idx++) \
403 obj_table[i] = r->ring[idx]; \
404 for (idx = 0; i < n; i++, idx++) \
405 obj_table[i] = r->ring[idx]; \
410 * @internal Enqueue several objects on the ring (multi-producers safe).
412 * This function uses a "compare and set" instruction to move the
413 * producer index atomically.
416 * A pointer to the ring structure.
418 * A pointer to a table of void * pointers (objects).
420 * The number of objects to add in the ring from the obj_table.
422 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
423 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
425 * Depend on the behavior value
426 * if behavior = RTE_RING_QUEUE_FIXED
427 * - 0: Success; objects enqueue.
428 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
429 * high water mark is exceeded.
430 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
431 * if behavior = RTE_RING_QUEUE_VARIABLE
432 * - n: Actual number of objects enqueued.
434 static inline int __attribute__((always_inline))
435 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
436 unsigned n, enum rte_ring_queue_behavior behavior)
438 uint32_t prod_head, prod_next;
439 uint32_t cons_tail, free_entries;
440 const unsigned max = n;
443 uint32_t mask = r->prod.mask;
446 /* Avoid the unnecessary cmpset operation below, which is also
447 * potentially harmful when n equals 0. */
451 /* move prod.head atomically */
453 /* Reset n to the initial burst count */
456 prod_head = r->prod.head;
457 cons_tail = r->cons.tail;
458 /* The subtraction is done between two unsigned 32bits value
459 * (the result is always modulo 32 bits even if we have
460 * prod_head > cons_tail). So 'free_entries' is always between 0
461 * and size(ring)-1. */
462 free_entries = (mask + cons_tail - prod_head);
464 /* check that we have enough room in ring */
465 if (unlikely(n > free_entries)) {
466 if (behavior == RTE_RING_QUEUE_FIXED) {
467 __RING_STAT_ADD(r, enq_fail, n);
471 /* No free entry available */
472 if (unlikely(free_entries == 0)) {
473 __RING_STAT_ADD(r, enq_fail, n);
481 prod_next = prod_head + n;
482 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
484 } while (unlikely(success == 0));
486 /* write entries in ring */
490 /* if we exceed the watermark */
491 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
492 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
493 (int)(n | RTE_RING_QUOT_EXCEED);
494 __RING_STAT_ADD(r, enq_quota, n);
497 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
498 __RING_STAT_ADD(r, enq_success, n);
502 * If there are other enqueues in progress that preceded us,
503 * we need to wait for them to complete
505 while (unlikely(r->prod.tail != prod_head)) {
508 /* Set RTE_RING_PAUSE_REP_COUNT to avoid spin too long waiting
509 * for other thread finish. It gives pre-empted thread a chance
510 * to proceed and finish with ring dequeue operation. */
511 if (RTE_RING_PAUSE_REP_COUNT &&
512 ++rep == RTE_RING_PAUSE_REP_COUNT) {
517 r->prod.tail = prod_next;
522 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
525 * A pointer to the ring structure.
527 * A pointer to a table of void * pointers (objects).
529 * The number of objects to add in the ring from the obj_table.
531 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
532 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
534 * Depend on the behavior value
535 * if behavior = RTE_RING_QUEUE_FIXED
536 * - 0: Success; objects enqueue.
537 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
538 * high water mark is exceeded.
539 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
540 * if behavior = RTE_RING_QUEUE_VARIABLE
541 * - n: Actual number of objects enqueued.
543 static inline int __attribute__((always_inline))
544 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
545 unsigned n, enum rte_ring_queue_behavior behavior)
547 uint32_t prod_head, cons_tail;
548 uint32_t prod_next, free_entries;
550 uint32_t mask = r->prod.mask;
553 prod_head = r->prod.head;
554 cons_tail = r->cons.tail;
555 /* The subtraction is done between two unsigned 32bits value
556 * (the result is always modulo 32 bits even if we have
557 * prod_head > cons_tail). So 'free_entries' is always between 0
558 * and size(ring)-1. */
559 free_entries = mask + cons_tail - prod_head;
561 /* check that we have enough room in ring */
562 if (unlikely(n > free_entries)) {
563 if (behavior == RTE_RING_QUEUE_FIXED) {
564 __RING_STAT_ADD(r, enq_fail, n);
568 /* No free entry available */
569 if (unlikely(free_entries == 0)) {
570 __RING_STAT_ADD(r, enq_fail, n);
578 prod_next = prod_head + n;
579 r->prod.head = prod_next;
581 /* write entries in ring */
585 /* if we exceed the watermark */
586 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
587 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
588 (int)(n | RTE_RING_QUOT_EXCEED);
589 __RING_STAT_ADD(r, enq_quota, n);
592 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
593 __RING_STAT_ADD(r, enq_success, n);
596 r->prod.tail = prod_next;
601 * @internal Dequeue several objects from a ring (multi-consumers safe). When
602 * the request objects are more than the available objects, only dequeue the
603 * actual number of objects
605 * This function uses a "compare and set" instruction to move the
606 * consumer index atomically.
609 * A pointer to the ring structure.
611 * A pointer to a table of void * pointers (objects) that will be filled.
613 * The number of objects to dequeue from the ring to the obj_table.
615 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
616 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
618 * Depend on the behavior value
619 * if behavior = RTE_RING_QUEUE_FIXED
620 * - 0: Success; objects dequeued.
621 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
623 * if behavior = RTE_RING_QUEUE_VARIABLE
624 * - n: Actual number of objects dequeued.
627 static inline int __attribute__((always_inline))
628 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
629 unsigned n, enum rte_ring_queue_behavior behavior)
631 uint32_t cons_head, prod_tail;
632 uint32_t cons_next, entries;
633 const unsigned max = n;
636 uint32_t mask = r->prod.mask;
638 /* Avoid the unnecessary cmpset operation below, which is also
639 * potentially harmful when n equals 0. */
643 /* move cons.head atomically */
645 /* Restore n as it may change every loop */
648 cons_head = r->cons.head;
649 prod_tail = r->prod.tail;
650 /* The subtraction is done between two unsigned 32bits value
651 * (the result is always modulo 32 bits even if we have
652 * cons_head > prod_tail). So 'entries' is always between 0
653 * and size(ring)-1. */
654 entries = (prod_tail - cons_head);
656 /* Set the actual entries for dequeue */
658 if (behavior == RTE_RING_QUEUE_FIXED) {
659 __RING_STAT_ADD(r, deq_fail, n);
663 if (unlikely(entries == 0)){
664 __RING_STAT_ADD(r, deq_fail, n);
672 cons_next = cons_head + n;
673 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
675 } while (unlikely(success == 0));
682 * If there are other dequeues in progress that preceded us,
683 * we need to wait for them to complete
685 while (unlikely(r->cons.tail != cons_head)) {
688 /* Set RTE_RING_PAUSE_REP_COUNT to avoid spin too long waiting
689 * for other thread finish. It gives pre-empted thread a chance
690 * to proceed and finish with ring dequeue operation. */
691 if (RTE_RING_PAUSE_REP_COUNT &&
692 ++rep == RTE_RING_PAUSE_REP_COUNT) {
697 __RING_STAT_ADD(r, deq_success, n);
698 r->cons.tail = cons_next;
700 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
704 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
705 * When the request objects are more than the available objects, only dequeue
706 * the actual number of objects
709 * A pointer to the ring structure.
711 * A pointer to a table of void * pointers (objects) that will be filled.
713 * The number of objects to dequeue from the ring to the obj_table.
715 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
716 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
718 * Depend on the behavior value
719 * if behavior = RTE_RING_QUEUE_FIXED
720 * - 0: Success; objects dequeued.
721 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
723 * if behavior = RTE_RING_QUEUE_VARIABLE
724 * - n: Actual number of objects dequeued.
726 static inline int __attribute__((always_inline))
727 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
728 unsigned n, enum rte_ring_queue_behavior behavior)
730 uint32_t cons_head, prod_tail;
731 uint32_t cons_next, entries;
733 uint32_t mask = r->prod.mask;
735 cons_head = r->cons.head;
736 prod_tail = r->prod.tail;
737 /* The subtraction is done between two unsigned 32bits value
738 * (the result is always modulo 32 bits even if we have
739 * cons_head > prod_tail). So 'entries' is always between 0
740 * and size(ring)-1. */
741 entries = prod_tail - cons_head;
744 if (behavior == RTE_RING_QUEUE_FIXED) {
745 __RING_STAT_ADD(r, deq_fail, n);
749 if (unlikely(entries == 0)){
750 __RING_STAT_ADD(r, deq_fail, n);
758 cons_next = cons_head + n;
759 r->cons.head = cons_next;
765 __RING_STAT_ADD(r, deq_success, n);
766 r->cons.tail = cons_next;
767 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
771 * Enqueue several objects on the ring (multi-producers safe).
773 * This function uses a "compare and set" instruction to move the
774 * producer index atomically.
777 * A pointer to the ring structure.
779 * A pointer to a table of void * pointers (objects).
781 * The number of objects to add in the ring from the obj_table.
783 * - 0: Success; objects enqueue.
784 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
785 * high water mark is exceeded.
786 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
788 static inline int __attribute__((always_inline))
789 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
792 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
796 * Enqueue several objects on a ring (NOT multi-producers safe).
799 * A pointer to the ring structure.
801 * A pointer to a table of void * pointers (objects).
803 * The number of objects to add in the ring from the obj_table.
805 * - 0: Success; objects enqueued.
806 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
807 * high water mark is exceeded.
808 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
810 static inline int __attribute__((always_inline))
811 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
814 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
818 * Enqueue several objects on a ring.
820 * This function calls the multi-producer or the single-producer
821 * version depending on the default behavior that was specified at
822 * ring creation time (see flags).
825 * A pointer to the ring structure.
827 * A pointer to a table of void * pointers (objects).
829 * The number of objects to add in the ring from the obj_table.
831 * - 0: Success; objects enqueued.
832 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
833 * high water mark is exceeded.
834 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
836 static inline int __attribute__((always_inline))
837 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
840 if (r->prod.sp_enqueue)
841 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
843 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
847 * Enqueue one object on a ring (multi-producers safe).
849 * This function uses a "compare and set" instruction to move the
850 * producer index atomically.
853 * A pointer to the ring structure.
855 * A pointer to the object to be added.
857 * - 0: Success; objects enqueued.
858 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
859 * high water mark is exceeded.
860 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
862 static inline int __attribute__((always_inline))
863 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
865 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
869 * Enqueue one object on a ring (NOT multi-producers safe).
872 * A pointer to the ring structure.
874 * A pointer to the object to be added.
876 * - 0: Success; objects enqueued.
877 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
878 * high water mark is exceeded.
879 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
881 static inline int __attribute__((always_inline))
882 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
884 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
888 * Enqueue one object on a ring.
890 * This function calls the multi-producer or the single-producer
891 * version, depending on the default behaviour that was specified at
892 * ring creation time (see flags).
895 * A pointer to the ring structure.
897 * A pointer to the object to be added.
899 * - 0: Success; objects enqueued.
900 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
901 * high water mark is exceeded.
902 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
904 static inline int __attribute__((always_inline))
905 rte_ring_enqueue(struct rte_ring *r, void *obj)
907 if (r->prod.sp_enqueue)
908 return rte_ring_sp_enqueue(r, obj);
910 return rte_ring_mp_enqueue(r, obj);
914 * Dequeue several objects from a ring (multi-consumers safe).
916 * This function uses a "compare and set" instruction to move the
917 * consumer index atomically.
920 * A pointer to the ring structure.
922 * A pointer to a table of void * pointers (objects) that will be filled.
924 * The number of objects to dequeue from the ring to the obj_table.
926 * - 0: Success; objects dequeued.
927 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
930 static inline int __attribute__((always_inline))
931 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
933 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
937 * Dequeue several objects from a ring (NOT multi-consumers safe).
940 * A pointer to the ring structure.
942 * A pointer to a table of void * pointers (objects) that will be filled.
944 * The number of objects to dequeue from the ring to the obj_table,
945 * must be strictly positive.
947 * - 0: Success; objects dequeued.
948 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
951 static inline int __attribute__((always_inline))
952 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
954 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
958 * Dequeue several objects from a ring.
960 * This function calls the multi-consumers or the single-consumer
961 * version, depending on the default behaviour that was specified at
962 * ring creation time (see flags).
965 * A pointer to the ring structure.
967 * A pointer to a table of void * pointers (objects) that will be filled.
969 * The number of objects to dequeue from the ring to the obj_table.
971 * - 0: Success; objects dequeued.
972 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
975 static inline int __attribute__((always_inline))
976 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
978 if (r->cons.sc_dequeue)
979 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
981 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
985 * Dequeue one object from a ring (multi-consumers safe).
987 * This function uses a "compare and set" instruction to move the
988 * consumer index atomically.
991 * A pointer to the ring structure.
993 * A pointer to a void * pointer (object) that will be filled.
995 * - 0: Success; objects dequeued.
996 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
999 static inline int __attribute__((always_inline))
1000 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
1002 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
1006 * Dequeue one object from a ring (NOT multi-consumers safe).
1009 * A pointer to the ring structure.
1011 * A pointer to a void * pointer (object) that will be filled.
1013 * - 0: Success; objects dequeued.
1014 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
1017 static inline int __attribute__((always_inline))
1018 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
1020 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
1024 * Dequeue one object from a ring.
1026 * This function calls the multi-consumers or the single-consumer
1027 * version depending on the default behaviour that was specified at
1028 * ring creation time (see flags).
1031 * A pointer to the ring structure.
1033 * A pointer to a void * pointer (object) that will be filled.
1035 * - 0: Success, objects dequeued.
1036 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
1039 static inline int __attribute__((always_inline))
1040 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
1042 if (r->cons.sc_dequeue)
1043 return rte_ring_sc_dequeue(r, obj_p);
1045 return rte_ring_mc_dequeue(r, obj_p);
1049 * Test if a ring is full.
1052 * A pointer to the ring structure.
1054 * - 1: The ring is full.
1055 * - 0: The ring is not full.
1058 rte_ring_full(const struct rte_ring *r)
1060 uint32_t prod_tail = r->prod.tail;
1061 uint32_t cons_tail = r->cons.tail;
1062 return ((cons_tail - prod_tail - 1) & r->prod.mask) == 0;
1066 * Test if a ring is empty.
1069 * A pointer to the ring structure.
1071 * - 1: The ring is empty.
1072 * - 0: The ring is not empty.
1075 rte_ring_empty(const struct rte_ring *r)
1077 uint32_t prod_tail = r->prod.tail;
1078 uint32_t cons_tail = r->cons.tail;
1079 return !!(cons_tail == prod_tail);
1083 * Return the number of entries in a ring.
1086 * A pointer to the ring structure.
1088 * The number of entries in the ring.
1090 static inline unsigned
1091 rte_ring_count(const struct rte_ring *r)
1093 uint32_t prod_tail = r->prod.tail;
1094 uint32_t cons_tail = r->cons.tail;
1095 return (prod_tail - cons_tail) & r->prod.mask;
1099 * Return the number of free entries in a ring.
1102 * A pointer to the ring structure.
1104 * The number of free entries in the ring.
1106 static inline unsigned
1107 rte_ring_free_count(const struct rte_ring *r)
1109 uint32_t prod_tail = r->prod.tail;
1110 uint32_t cons_tail = r->cons.tail;
1111 return (cons_tail - prod_tail - 1) & r->prod.mask;
1115 * Return the size of the ring.
1118 * A pointer to the ring structure.
1120 * The number of elements which can be stored in the ring.
1122 static inline unsigned int
1123 rte_ring_get_size(const struct rte_ring *r)
1125 return r->prod.size;
1129 * Dump the status of all rings on the console
1132 * A pointer to a file for output
1134 void rte_ring_list_dump(FILE *f);
1137 * Search a ring from its name
1140 * The name of the ring.
1142 * The pointer to the ring matching the name, or NULL if not found,
1143 * with rte_errno set appropriately. Possible rte_errno values include:
1144 * - ENOENT - required entry not available to return.
1146 struct rte_ring *rte_ring_lookup(const char *name);
1149 * Enqueue several objects on the ring (multi-producers safe).
1151 * This function uses a "compare and set" instruction to move the
1152 * producer index atomically.
1155 * A pointer to the ring structure.
1157 * A pointer to a table of void * pointers (objects).
1159 * The number of objects to add in the ring from the obj_table.
1161 * - n: Actual number of objects enqueued.
1163 static inline unsigned __attribute__((always_inline))
1164 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1167 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1171 * Enqueue several objects on a ring (NOT multi-producers safe).
1174 * A pointer to the ring structure.
1176 * A pointer to a table of void * pointers (objects).
1178 * The number of objects to add in the ring from the obj_table.
1180 * - n: Actual number of objects enqueued.
1182 static inline unsigned __attribute__((always_inline))
1183 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1186 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1190 * Enqueue several objects on a ring.
1192 * This function calls the multi-producer or the single-producer
1193 * version depending on the default behavior that was specified at
1194 * ring creation time (see flags).
1197 * A pointer to the ring structure.
1199 * A pointer to a table of void * pointers (objects).
1201 * The number of objects to add in the ring from the obj_table.
1203 * - n: Actual number of objects enqueued.
1205 static inline unsigned __attribute__((always_inline))
1206 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1209 if (r->prod.sp_enqueue)
1210 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1212 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1216 * Dequeue several objects from a ring (multi-consumers safe). When the request
1217 * objects are more than the available objects, only dequeue the actual number
1220 * This function uses a "compare and set" instruction to move the
1221 * consumer index atomically.
1224 * A pointer to the ring structure.
1226 * A pointer to a table of void * pointers (objects) that will be filled.
1228 * The number of objects to dequeue from the ring to the obj_table.
1230 * - n: Actual number of objects dequeued, 0 if ring is empty
1232 static inline unsigned __attribute__((always_inline))
1233 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1235 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1239 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1240 * request objects are more than the available objects, only dequeue the
1241 * actual number of objects
1244 * A pointer to the ring structure.
1246 * A pointer to a table of void * pointers (objects) that will be filled.
1248 * The number of objects to dequeue from the ring to the obj_table.
1250 * - n: Actual number of objects dequeued, 0 if ring is empty
1252 static inline unsigned __attribute__((always_inline))
1253 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1255 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1259 * Dequeue multiple objects from a ring up to a maximum number.
1261 * This function calls the multi-consumers or the single-consumer
1262 * version, depending on the default behaviour that was specified at
1263 * ring creation time (see flags).
1266 * A pointer to the ring structure.
1268 * A pointer to a table of void * pointers (objects) that will be filled.
1270 * The number of objects to dequeue from the ring to the obj_table.
1272 * - Number of objects dequeued
1274 static inline unsigned __attribute__((always_inline))
1275 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1277 if (r->cons.sc_dequeue)
1278 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1280 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1287 #endif /* _RTE_RING_H_ */