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>
104 #define RTE_TAILQ_RING_NAME "RTE_RING"
106 enum rte_ring_queue_behavior {
107 RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
108 RTE_RING_QUEUE_VARIABLE /* Enq/Deq as many items a possible from ring */
111 #ifdef RTE_LIBRTE_RING_DEBUG
113 * A structure that stores the ring statistics (per-lcore).
115 struct rte_ring_debug_stats {
116 uint64_t enq_success_bulk; /**< Successful enqueues number. */
117 uint64_t enq_success_objs; /**< Objects successfully enqueued. */
118 uint64_t enq_quota_bulk; /**< Successful enqueues above watermark. */
119 uint64_t enq_quota_objs; /**< Objects enqueued above watermark. */
120 uint64_t enq_fail_bulk; /**< Failed enqueues number. */
121 uint64_t enq_fail_objs; /**< Objects that failed to be enqueued. */
122 uint64_t deq_success_bulk; /**< Successful dequeues number. */
123 uint64_t deq_success_objs; /**< Objects successfully dequeued. */
124 uint64_t deq_fail_bulk; /**< Failed dequeues number. */
125 uint64_t deq_fail_objs; /**< Objects that failed to be dequeued. */
126 } __rte_cache_aligned;
129 #define RTE_RING_NAMESIZE 32 /**< The maximum length of a ring name. */
130 #define RTE_RING_MZ_PREFIX "RG_"
132 #ifndef RTE_RING_PAUSE_REP_COUNT
133 #define RTE_RING_PAUSE_REP_COUNT 0 /**< Yield after pause num of times, no yield
134 * if RTE_RING_PAUSE_REP not defined. */
137 struct rte_memzone; /* forward declaration, so as not to require memzone.h */
140 * An RTE ring structure.
142 * The producer and the consumer have a head and a tail index. The particularity
143 * of these index is that they are not between 0 and size(ring). These indexes
144 * are between 0 and 2^32, and we mask their value when we access the ring[]
145 * field. Thanks to this assumption, we can do subtractions between 2 index
146 * values in a modulo-32bit base: that's why the overflow of the indexes is not
150 char name[RTE_RING_NAMESIZE]; /**< Name of the ring. */
151 int flags; /**< Flags supplied at creation. */
152 const struct rte_memzone *memzone;
153 /**< Memzone, if any, containing the rte_ring */
155 /** Ring producer status. */
157 uint32_t watermark; /**< Maximum items before EDQUOT. */
158 uint32_t sp_enqueue; /**< True, if single producer. */
159 uint32_t size; /**< Size of ring. */
160 uint32_t mask; /**< Mask (size-1) of ring. */
161 volatile uint32_t head; /**< Producer head. */
162 volatile uint32_t tail; /**< Producer tail. */
163 } prod __rte_cache_aligned;
165 /** Ring consumer status. */
167 uint32_t sc_dequeue; /**< True, if single consumer. */
168 uint32_t size; /**< Size of the ring. */
169 uint32_t mask; /**< Mask (size-1) of ring. */
170 volatile uint32_t head; /**< Consumer head. */
171 volatile uint32_t tail; /**< Consumer tail. */
172 #ifdef RTE_RING_SPLIT_PROD_CONS
173 } cons __rte_cache_aligned;
178 #ifdef RTE_LIBRTE_RING_DEBUG
179 struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
182 void * ring[0] __rte_cache_aligned; /**< Memory space of ring starts here.
183 * not volatile so need to be careful
184 * about compiler re-ordering */
187 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
188 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
189 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
190 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
193 * @internal When debug is enabled, store ring statistics.
195 * A pointer to the ring.
197 * The name of the statistics field to increment in the ring.
199 * The number to add to the object-oriented statistics.
201 #ifdef RTE_LIBRTE_RING_DEBUG
202 #define __RING_STAT_ADD(r, name, n) do { \
203 unsigned __lcore_id = rte_lcore_id(); \
204 if (__lcore_id < RTE_MAX_LCORE) { \
205 r->stats[__lcore_id].name##_objs += n; \
206 r->stats[__lcore_id].name##_bulk += 1; \
210 #define __RING_STAT_ADD(r, name, n) do {} while(0)
214 * Calculate the memory size needed for a ring
216 * This function returns the number of bytes needed for a ring, given
217 * the number of elements in it. This value is the sum of the size of
218 * the structure rte_ring and the size of the memory needed by the
219 * objects pointers. The value is aligned to a cache line size.
222 * The number of elements in the ring (must be a power of 2).
224 * - The memory size needed for the ring on success.
225 * - -EINVAL if count is not a power of 2.
227 ssize_t rte_ring_get_memsize(unsigned count);
230 * Initialize a ring structure.
232 * Initialize a ring structure in memory pointed by "r". The size of the
233 * memory area must be large enough to store the ring structure and the
234 * object table. It is advised to use rte_ring_get_memsize() to get the
237 * The ring size is set to *count*, which must be a power of two. Water
238 * marking is disabled by default. The real usable ring size is
239 * *count-1* instead of *count* to differentiate a free ring from an
242 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
243 * memory given by the caller may not be shareable among dpdk
247 * The pointer to the ring structure followed by the objects table.
249 * The name of the ring.
251 * The number of elements in the ring (must be a power of 2).
253 * An OR of the following:
254 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
255 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
256 * is "single-producer". Otherwise, it is "multi-producers".
257 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
258 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
259 * is "single-consumer". Otherwise, it is "multi-consumers".
261 * 0 on success, or a negative value on error.
263 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
267 * Create a new ring named *name* in memory.
269 * This function uses ``memzone_reserve()`` to allocate memory. Then it
270 * calls rte_ring_init() to initialize an empty ring.
272 * The new ring size is set to *count*, which must be a power of
273 * two. Water marking is disabled by default. The real usable ring size
274 * is *count-1* instead of *count* to differentiate a free ring from an
277 * The ring is added in RTE_TAILQ_RING list.
280 * The name of the ring.
282 * The size of the ring (must be a power of 2).
284 * The *socket_id* argument is the socket identifier in case of
285 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
286 * constraint for the reserved zone.
288 * An OR of the following:
289 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
290 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
291 * is "single-producer". Otherwise, it is "multi-producers".
292 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
293 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
294 * is "single-consumer". Otherwise, it is "multi-consumers".
296 * On success, the pointer to the new allocated ring. NULL on error with
297 * rte_errno set appropriately. Possible errno values include:
298 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
299 * - E_RTE_SECONDARY - function was called from a secondary process instance
300 * - EINVAL - count provided is not a power of 2
301 * - ENOSPC - the maximum number of memzones has already been allocated
302 * - EEXIST - a memzone with the same name already exists
303 * - ENOMEM - no appropriate memory area found in which to create memzone
305 struct rte_ring *rte_ring_create(const char *name, unsigned count,
306 int socket_id, unsigned flags);
308 * De-allocate all memory used by the ring.
313 void rte_ring_free(struct rte_ring *r);
316 * Change the high water mark.
318 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
319 * *count* value. The *count* value must be greater than 0 and less
320 * than the ring size.
322 * This function can be called at any time (not necessarily at
326 * A pointer to the ring structure.
328 * The new water mark value.
330 * - 0: Success; water mark changed.
331 * - -EINVAL: Invalid water mark value.
333 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
336 * Dump the status of the ring to the console.
339 * A pointer to a file for output
341 * A pointer to the ring structure.
343 void rte_ring_dump(FILE *f, const struct rte_ring *r);
345 /* the actual enqueue of pointers on the ring.
346 * Placed here since identical code needed in both
347 * single and multi producer enqueue functions */
348 #define ENQUEUE_PTRS() do { \
349 const uint32_t size = r->prod.size; \
350 uint32_t idx = prod_head & mask; \
351 if (likely(idx + n < size)) { \
352 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
353 r->ring[idx] = obj_table[i]; \
354 r->ring[idx+1] = obj_table[i+1]; \
355 r->ring[idx+2] = obj_table[i+2]; \
356 r->ring[idx+3] = obj_table[i+3]; \
359 case 3: r->ring[idx++] = obj_table[i++]; \
360 case 2: r->ring[idx++] = obj_table[i++]; \
361 case 1: r->ring[idx++] = obj_table[i++]; \
364 for (i = 0; idx < size; i++, idx++)\
365 r->ring[idx] = obj_table[i]; \
366 for (idx = 0; i < n; i++, idx++) \
367 r->ring[idx] = obj_table[i]; \
371 /* the actual copy of pointers on the ring to obj_table.
372 * Placed here since identical code needed in both
373 * single and multi consumer dequeue functions */
374 #define DEQUEUE_PTRS() do { \
375 uint32_t idx = cons_head & mask; \
376 const uint32_t size = r->cons.size; \
377 if (likely(idx + n < size)) { \
378 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
379 obj_table[i] = r->ring[idx]; \
380 obj_table[i+1] = r->ring[idx+1]; \
381 obj_table[i+2] = r->ring[idx+2]; \
382 obj_table[i+3] = r->ring[idx+3]; \
385 case 3: obj_table[i++] = r->ring[idx++]; \
386 case 2: obj_table[i++] = r->ring[idx++]; \
387 case 1: obj_table[i++] = r->ring[idx++]; \
390 for (i = 0; idx < size; i++, idx++) \
391 obj_table[i] = r->ring[idx]; \
392 for (idx = 0; i < n; i++, idx++) \
393 obj_table[i] = r->ring[idx]; \
398 * @internal Enqueue several objects on the ring (multi-producers safe).
400 * This function uses a "compare and set" instruction to move the
401 * producer index atomically.
404 * A pointer to the ring structure.
406 * A pointer to a table of void * pointers (objects).
408 * The number of objects to add in the ring from the obj_table.
410 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
411 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
413 * Depend on the behavior value
414 * if behavior = RTE_RING_QUEUE_FIXED
415 * - 0: Success; objects enqueue.
416 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
417 * high water mark is exceeded.
418 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
419 * if behavior = RTE_RING_QUEUE_VARIABLE
420 * - n: Actual number of objects enqueued.
422 static inline int __attribute__((always_inline))
423 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
424 unsigned n, enum rte_ring_queue_behavior behavior)
426 uint32_t prod_head, prod_next;
427 uint32_t cons_tail, free_entries;
428 const unsigned max = n;
431 uint32_t mask = r->prod.mask;
434 /* move prod.head atomically */
436 /* Reset n to the initial burst count */
439 prod_head = r->prod.head;
440 cons_tail = r->cons.tail;
441 /* The subtraction is done between two unsigned 32bits value
442 * (the result is always modulo 32 bits even if we have
443 * prod_head > cons_tail). So 'free_entries' is always between 0
444 * and size(ring)-1. */
445 free_entries = (mask + cons_tail - prod_head);
447 /* check that we have enough room in ring */
448 if (unlikely(n > free_entries)) {
449 if (behavior == RTE_RING_QUEUE_FIXED) {
450 __RING_STAT_ADD(r, enq_fail, n);
454 /* No free entry available */
455 if (unlikely(free_entries == 0)) {
456 __RING_STAT_ADD(r, enq_fail, n);
464 prod_next = prod_head + n;
465 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
467 } while (unlikely(success == 0));
469 /* write entries in ring */
473 /* if we exceed the watermark */
474 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
475 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
476 (int)(n | RTE_RING_QUOT_EXCEED);
477 __RING_STAT_ADD(r, enq_quota, n);
480 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
481 __RING_STAT_ADD(r, enq_success, n);
485 * If there are other enqueues in progress that preceded us,
486 * we need to wait for them to complete
488 while (unlikely(r->prod.tail != prod_head)) {
491 /* Set RTE_RING_PAUSE_REP_COUNT to avoid spin too long waiting
492 * for other thread finish. It gives pre-empted thread a chance
493 * to proceed and finish with ring dequeue operation. */
494 if (RTE_RING_PAUSE_REP_COUNT &&
495 ++rep == RTE_RING_PAUSE_REP_COUNT) {
500 r->prod.tail = prod_next;
505 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
508 * A pointer to the ring structure.
510 * A pointer to a table of void * pointers (objects).
512 * The number of objects to add in the ring from the obj_table.
514 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
515 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
517 * Depend on the behavior value
518 * if behavior = RTE_RING_QUEUE_FIXED
519 * - 0: Success; objects enqueue.
520 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
521 * high water mark is exceeded.
522 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
523 * if behavior = RTE_RING_QUEUE_VARIABLE
524 * - n: Actual number of objects enqueued.
526 static inline int __attribute__((always_inline))
527 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
528 unsigned n, enum rte_ring_queue_behavior behavior)
530 uint32_t prod_head, cons_tail;
531 uint32_t prod_next, free_entries;
533 uint32_t mask = r->prod.mask;
536 prod_head = r->prod.head;
537 cons_tail = r->cons.tail;
538 /* The subtraction is done between two unsigned 32bits value
539 * (the result is always modulo 32 bits even if we have
540 * prod_head > cons_tail). So 'free_entries' is always between 0
541 * and size(ring)-1. */
542 free_entries = mask + cons_tail - prod_head;
544 /* check that we have enough room in ring */
545 if (unlikely(n > free_entries)) {
546 if (behavior == RTE_RING_QUEUE_FIXED) {
547 __RING_STAT_ADD(r, enq_fail, n);
551 /* No free entry available */
552 if (unlikely(free_entries == 0)) {
553 __RING_STAT_ADD(r, enq_fail, n);
561 prod_next = prod_head + n;
562 r->prod.head = prod_next;
564 /* write entries in ring */
568 /* if we exceed the watermark */
569 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
570 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
571 (int)(n | RTE_RING_QUOT_EXCEED);
572 __RING_STAT_ADD(r, enq_quota, n);
575 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
576 __RING_STAT_ADD(r, enq_success, n);
579 r->prod.tail = prod_next;
584 * @internal Dequeue several objects from a ring (multi-consumers safe). When
585 * the request objects are more than the available objects, only dequeue the
586 * actual number of objects
588 * This function uses a "compare and set" instruction to move the
589 * consumer index atomically.
592 * A pointer to the ring structure.
594 * A pointer to a table of void * pointers (objects) that will be filled.
596 * The number of objects to dequeue from the ring to the obj_table.
598 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
599 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
601 * Depend on the behavior value
602 * if behavior = RTE_RING_QUEUE_FIXED
603 * - 0: Success; objects dequeued.
604 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
606 * if behavior = RTE_RING_QUEUE_VARIABLE
607 * - n: Actual number of objects dequeued.
610 static inline int __attribute__((always_inline))
611 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
612 unsigned n, enum rte_ring_queue_behavior behavior)
614 uint32_t cons_head, prod_tail;
615 uint32_t cons_next, entries;
616 const unsigned max = n;
619 uint32_t mask = r->prod.mask;
621 /* move cons.head atomically */
623 /* Restore n as it may change every loop */
626 cons_head = r->cons.head;
627 prod_tail = r->prod.tail;
628 /* The subtraction is done between two unsigned 32bits value
629 * (the result is always modulo 32 bits even if we have
630 * cons_head > prod_tail). So 'entries' is always between 0
631 * and size(ring)-1. */
632 entries = (prod_tail - cons_head);
634 /* Set the actual entries for dequeue */
636 if (behavior == RTE_RING_QUEUE_FIXED) {
637 __RING_STAT_ADD(r, deq_fail, n);
641 if (unlikely(entries == 0)){
642 __RING_STAT_ADD(r, deq_fail, n);
650 cons_next = cons_head + n;
651 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
653 } while (unlikely(success == 0));
660 * If there are other dequeues in progress that preceded us,
661 * we need to wait for them to complete
663 while (unlikely(r->cons.tail != cons_head)) {
666 /* Set RTE_RING_PAUSE_REP_COUNT to avoid spin too long waiting
667 * for other thread finish. It gives pre-empted thread a chance
668 * to proceed and finish with ring dequeue operation. */
669 if (RTE_RING_PAUSE_REP_COUNT &&
670 ++rep == RTE_RING_PAUSE_REP_COUNT) {
675 __RING_STAT_ADD(r, deq_success, n);
676 r->cons.tail = cons_next;
678 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
682 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
683 * When the request objects are more than the available objects, only dequeue
684 * the actual number of objects
687 * A pointer to the ring structure.
689 * A pointer to a table of void * pointers (objects) that will be filled.
691 * The number of objects to dequeue from the ring to the obj_table.
693 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
694 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
696 * Depend on the behavior value
697 * if behavior = RTE_RING_QUEUE_FIXED
698 * - 0: Success; objects dequeued.
699 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
701 * if behavior = RTE_RING_QUEUE_VARIABLE
702 * - n: Actual number of objects dequeued.
704 static inline int __attribute__((always_inline))
705 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
706 unsigned n, enum rte_ring_queue_behavior behavior)
708 uint32_t cons_head, prod_tail;
709 uint32_t cons_next, entries;
711 uint32_t mask = r->prod.mask;
713 cons_head = r->cons.head;
714 prod_tail = r->prod.tail;
715 /* The subtraction is done between two unsigned 32bits value
716 * (the result is always modulo 32 bits even if we have
717 * cons_head > prod_tail). So 'entries' is always between 0
718 * and size(ring)-1. */
719 entries = prod_tail - cons_head;
722 if (behavior == RTE_RING_QUEUE_FIXED) {
723 __RING_STAT_ADD(r, deq_fail, n);
727 if (unlikely(entries == 0)){
728 __RING_STAT_ADD(r, deq_fail, n);
736 cons_next = cons_head + n;
737 r->cons.head = cons_next;
743 __RING_STAT_ADD(r, deq_success, n);
744 r->cons.tail = cons_next;
745 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
749 * Enqueue several objects on the ring (multi-producers safe).
751 * This function uses a "compare and set" instruction to move the
752 * producer index atomically.
755 * A pointer to the ring structure.
757 * A pointer to a table of void * pointers (objects).
759 * The number of objects to add in the ring from the obj_table.
761 * - 0: Success; objects enqueue.
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_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
770 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
774 * Enqueue several objects on a ring (NOT multi-producers safe).
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 enqueued.
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_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
792 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
796 * Enqueue several objects on a ring.
798 * This function calls the multi-producer or the single-producer
799 * version depending on the default behavior that was specified at
800 * ring creation time (see flags).
803 * A pointer to the ring structure.
805 * A pointer to a table of void * pointers (objects).
807 * The number of objects to add in the ring from the obj_table.
809 * - 0: Success; objects enqueued.
810 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
811 * high water mark is exceeded.
812 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
814 static inline int __attribute__((always_inline))
815 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
818 if (r->prod.sp_enqueue)
819 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
821 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
825 * Enqueue one object on a ring (multi-producers safe).
827 * This function uses a "compare and set" instruction to move the
828 * producer index atomically.
831 * A pointer to the ring structure.
833 * A pointer to the object to be added.
835 * - 0: Success; objects enqueued.
836 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
837 * high water mark is exceeded.
838 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
840 static inline int __attribute__((always_inline))
841 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
843 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
847 * Enqueue one object on a ring (NOT multi-producers safe).
850 * A pointer to the ring structure.
852 * A pointer to the object to be added.
854 * - 0: Success; objects enqueued.
855 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
856 * high water mark is exceeded.
857 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
859 static inline int __attribute__((always_inline))
860 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
862 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
866 * Enqueue one object on a ring.
868 * This function calls the multi-producer or the single-producer
869 * version, depending on the default behaviour that was specified at
870 * ring creation time (see flags).
873 * A pointer to the ring structure.
875 * A pointer to the object to be added.
877 * - 0: Success; objects enqueued.
878 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
879 * high water mark is exceeded.
880 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
882 static inline int __attribute__((always_inline))
883 rte_ring_enqueue(struct rte_ring *r, void *obj)
885 if (r->prod.sp_enqueue)
886 return rte_ring_sp_enqueue(r, obj);
888 return rte_ring_mp_enqueue(r, obj);
892 * Dequeue several objects from a ring (multi-consumers safe).
894 * This function uses a "compare and set" instruction to move the
895 * consumer index atomically.
898 * A pointer to the ring structure.
900 * A pointer to a table of void * pointers (objects) that will be filled.
902 * The number of objects to dequeue from the ring to the obj_table.
904 * - 0: Success; objects dequeued.
905 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
908 static inline int __attribute__((always_inline))
909 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
911 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
915 * Dequeue several objects from a ring (NOT multi-consumers safe).
918 * A pointer to the ring structure.
920 * A pointer to a table of void * pointers (objects) that will be filled.
922 * The number of objects to dequeue from the ring to the obj_table,
923 * must be strictly positive.
925 * - 0: Success; objects dequeued.
926 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
929 static inline int __attribute__((always_inline))
930 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
932 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
936 * Dequeue several objects from a ring.
938 * This function calls the multi-consumers or the single-consumer
939 * version, depending on the default behaviour that was specified at
940 * ring creation time (see flags).
943 * A pointer to the ring structure.
945 * A pointer to a table of void * pointers (objects) that will be filled.
947 * The number of objects to dequeue from the ring to the obj_table.
949 * - 0: Success; objects dequeued.
950 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
953 static inline int __attribute__((always_inline))
954 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
956 if (r->cons.sc_dequeue)
957 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
959 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
963 * Dequeue one object from a ring (multi-consumers safe).
965 * This function uses a "compare and set" instruction to move the
966 * consumer index atomically.
969 * A pointer to the ring structure.
971 * A pointer to a void * pointer (object) that will be filled.
973 * - 0: Success; objects dequeued.
974 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
977 static inline int __attribute__((always_inline))
978 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
980 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
984 * Dequeue one object from a ring (NOT multi-consumers safe).
987 * A pointer to the ring structure.
989 * A pointer to a void * pointer (object) that will be filled.
991 * - 0: Success; objects dequeued.
992 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
995 static inline int __attribute__((always_inline))
996 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
998 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
1002 * Dequeue one object from a ring.
1004 * This function calls the multi-consumers or the single-consumer
1005 * version depending on the default behaviour that was specified at
1006 * ring creation time (see flags).
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_dequeue(struct rte_ring *r, void **obj_p)
1020 if (r->cons.sc_dequeue)
1021 return rte_ring_sc_dequeue(r, obj_p);
1023 return rte_ring_mc_dequeue(r, obj_p);
1027 * Test if a ring is full.
1030 * A pointer to the ring structure.
1032 * - 1: The ring is full.
1033 * - 0: The ring is not full.
1036 rte_ring_full(const struct rte_ring *r)
1038 uint32_t prod_tail = r->prod.tail;
1039 uint32_t cons_tail = r->cons.tail;
1040 return ((cons_tail - prod_tail - 1) & r->prod.mask) == 0;
1044 * Test if a ring is empty.
1047 * A pointer to the ring structure.
1049 * - 1: The ring is empty.
1050 * - 0: The ring is not empty.
1053 rte_ring_empty(const struct rte_ring *r)
1055 uint32_t prod_tail = r->prod.tail;
1056 uint32_t cons_tail = r->cons.tail;
1057 return !!(cons_tail == prod_tail);
1061 * Return the number of entries in a ring.
1064 * A pointer to the ring structure.
1066 * The number of entries in the ring.
1068 static inline unsigned
1069 rte_ring_count(const struct rte_ring *r)
1071 uint32_t prod_tail = r->prod.tail;
1072 uint32_t cons_tail = r->cons.tail;
1073 return (prod_tail - cons_tail) & r->prod.mask;
1077 * Return the number of free entries in a ring.
1080 * A pointer to the ring structure.
1082 * The number of free entries in the ring.
1084 static inline unsigned
1085 rte_ring_free_count(const struct rte_ring *r)
1087 uint32_t prod_tail = r->prod.tail;
1088 uint32_t cons_tail = r->cons.tail;
1089 return (cons_tail - prod_tail - 1) & r->prod.mask;
1093 * Dump the status of all rings on the console
1096 * A pointer to a file for output
1098 void rte_ring_list_dump(FILE *f);
1101 * Search a ring from its name
1104 * The name of the ring.
1106 * The pointer to the ring matching the name, or NULL if not found,
1107 * with rte_errno set appropriately. Possible rte_errno values include:
1108 * - ENOENT - required entry not available to return.
1110 struct rte_ring *rte_ring_lookup(const char *name);
1113 * Enqueue several objects on the ring (multi-producers safe).
1115 * This function uses a "compare and set" instruction to move the
1116 * producer index atomically.
1119 * A pointer to the ring structure.
1121 * A pointer to a table of void * pointers (objects).
1123 * The number of objects to add in the ring from the obj_table.
1125 * - n: Actual number of objects enqueued.
1127 static inline unsigned __attribute__((always_inline))
1128 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1131 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1135 * Enqueue several objects on a ring (NOT multi-producers safe).
1138 * A pointer to the ring structure.
1140 * A pointer to a table of void * pointers (objects).
1142 * The number of objects to add in the ring from the obj_table.
1144 * - n: Actual number of objects enqueued.
1146 static inline unsigned __attribute__((always_inline))
1147 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1150 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1154 * Enqueue several objects on a ring.
1156 * This function calls the multi-producer or the single-producer
1157 * version depending on the default behavior that was specified at
1158 * ring creation time (see flags).
1161 * A pointer to the ring structure.
1163 * A pointer to a table of void * pointers (objects).
1165 * The number of objects to add in the ring from the obj_table.
1167 * - n: Actual number of objects enqueued.
1169 static inline unsigned __attribute__((always_inline))
1170 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1173 if (r->prod.sp_enqueue)
1174 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1176 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1180 * Dequeue several objects from a ring (multi-consumers safe). When the request
1181 * objects are more than the available objects, only dequeue the actual number
1184 * This function uses a "compare and set" instruction to move the
1185 * consumer index atomically.
1188 * A pointer to the ring structure.
1190 * A pointer to a table of void * pointers (objects) that will be filled.
1192 * The number of objects to dequeue from the ring to the obj_table.
1194 * - n: Actual number of objects dequeued, 0 if ring is empty
1196 static inline unsigned __attribute__((always_inline))
1197 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1199 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1203 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1204 * request objects are more than the available objects, only dequeue the
1205 * actual number of objects
1208 * A pointer to the ring structure.
1210 * A pointer to a table of void * pointers (objects) that will be filled.
1212 * The number of objects to dequeue from the ring to the obj_table.
1214 * - n: Actual number of objects dequeued, 0 if ring is empty
1216 static inline unsigned __attribute__((always_inline))
1217 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1219 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1223 * Dequeue multiple objects from a ring up to a maximum number.
1225 * This function calls the multi-consumers or the single-consumer
1226 * version, depending on the default behaviour that was specified at
1227 * ring creation time (see flags).
1230 * A pointer to the ring structure.
1232 * A pointer to a table of void * pointers (objects) that will be filled.
1234 * The number of objects to dequeue from the ring to the obj_table.
1236 * - Number of objects dequeued
1238 static inline unsigned __attribute__((always_inline))
1239 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1241 if (r->cons.sc_dequeue)
1242 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1244 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1251 #endif /* _RTE_RING_H_ */