4 * Copyright(c) 2010-2017 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
150 /* structure to hold a pair of head/tail values and other metadata */
151 struct rte_ring_headtail {
152 volatile uint32_t head; /**< Prod/consumer head. */
153 volatile uint32_t tail; /**< Prod/consumer tail. */
154 uint32_t single; /**< True if single prod/cons */
158 * An RTE ring structure.
160 * The producer and the consumer have a head and a tail index. The particularity
161 * of these index is that they are not between 0 and size(ring). These indexes
162 * are between 0 and 2^32, and we mask their value when we access the ring[]
163 * field. Thanks to this assumption, we can do subtractions between 2 index
164 * values in a modulo-32bit base: that's why the overflow of the indexes is not
169 * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
170 * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
171 * next time the ABI changes
173 char name[RTE_MEMZONE_NAMESIZE]; /**< Name of the ring. */
174 int flags; /**< Flags supplied at creation. */
175 const struct rte_memzone *memzone;
176 /**< Memzone, if any, containing the rte_ring */
177 uint32_t size; /**< Size of ring. */
178 uint32_t mask; /**< Mask (size-1) of ring. */
179 uint32_t watermark; /**< Max items before EDQUOT in producer. */
181 /** Ring producer status. */
182 struct rte_ring_headtail prod __rte_aligned(PROD_ALIGN);
184 /** Ring consumer status. */
185 struct rte_ring_headtail cons __rte_aligned(CONS_ALIGN);
187 #ifdef RTE_LIBRTE_RING_DEBUG
188 struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
191 void *ring[] __rte_cache_aligned; /**< Memory space of ring starts here.
192 * not volatile so need to be careful
193 * about compiler re-ordering */
196 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
197 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
198 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
199 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
202 * @internal When debug is enabled, store ring statistics.
204 * A pointer to the ring.
206 * The name of the statistics field to increment in the ring.
208 * The number to add to the object-oriented statistics.
210 #ifdef RTE_LIBRTE_RING_DEBUG
211 #define __RING_STAT_ADD(r, name, n) do { \
212 unsigned __lcore_id = rte_lcore_id(); \
213 if (__lcore_id < RTE_MAX_LCORE) { \
214 r->stats[__lcore_id].name##_objs += n; \
215 r->stats[__lcore_id].name##_bulk += 1; \
219 #define __RING_STAT_ADD(r, name, n) do {} while(0)
223 * Calculate the memory size needed for a ring
225 * This function returns the number of bytes needed for a ring, given
226 * the number of elements in it. This value is the sum of the size of
227 * the structure rte_ring and the size of the memory needed by the
228 * objects pointers. The value is aligned to a cache line size.
231 * The number of elements in the ring (must be a power of 2).
233 * - The memory size needed for the ring on success.
234 * - -EINVAL if count is not a power of 2.
236 ssize_t rte_ring_get_memsize(unsigned count);
239 * Initialize a ring structure.
241 * Initialize a ring structure in memory pointed by "r". The size of the
242 * memory area must be large enough to store the ring structure and the
243 * object table. It is advised to use rte_ring_get_memsize() to get the
246 * The ring size is set to *count*, which must be a power of two. Water
247 * marking is disabled by default. The real usable ring size is
248 * *count-1* instead of *count* to differentiate a free ring from an
251 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
252 * memory given by the caller may not be shareable among dpdk
256 * The pointer to the ring structure followed by the objects table.
258 * The name of the ring.
260 * The number of elements in the ring (must be a power of 2).
262 * An OR of the following:
263 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
264 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
265 * is "single-producer". Otherwise, it is "multi-producers".
266 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
267 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
268 * is "single-consumer". Otherwise, it is "multi-consumers".
270 * 0 on success, or a negative value on error.
272 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
276 * Create a new ring named *name* in memory.
278 * This function uses ``memzone_reserve()`` to allocate memory. Then it
279 * calls rte_ring_init() to initialize an empty ring.
281 * The new ring size is set to *count*, which must be a power of
282 * two. Water marking is disabled by default. The real usable ring size
283 * is *count-1* instead of *count* to differentiate a free ring from an
286 * The ring is added in RTE_TAILQ_RING list.
289 * The name of the ring.
291 * The size of the ring (must be a power of 2).
293 * The *socket_id* argument is the socket identifier in case of
294 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
295 * constraint for the reserved zone.
297 * An OR of the following:
298 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
299 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
300 * is "single-producer". Otherwise, it is "multi-producers".
301 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
302 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
303 * is "single-consumer". Otherwise, it is "multi-consumers".
305 * On success, the pointer to the new allocated ring. NULL on error with
306 * rte_errno set appropriately. Possible errno values include:
307 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
308 * - E_RTE_SECONDARY - function was called from a secondary process instance
309 * - EINVAL - count provided is not a power of 2
310 * - ENOSPC - the maximum number of memzones has already been allocated
311 * - EEXIST - a memzone with the same name already exists
312 * - ENOMEM - no appropriate memory area found in which to create memzone
314 struct rte_ring *rte_ring_create(const char *name, unsigned count,
315 int socket_id, unsigned flags);
317 * De-allocate all memory used by the ring.
322 void rte_ring_free(struct rte_ring *r);
325 * Change the high water mark.
327 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
328 * *count* value. The *count* value must be greater than 0 and less
329 * than the ring size.
331 * This function can be called at any time (not necessarily at
335 * A pointer to the ring structure.
337 * The new water mark value.
339 * - 0: Success; water mark changed.
340 * - -EINVAL: Invalid water mark value.
342 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
345 * Dump the status of the ring to a file.
348 * A pointer to a file for output
350 * A pointer to the ring structure.
352 void rte_ring_dump(FILE *f, const struct rte_ring *r);
354 /* the actual enqueue of pointers on the ring.
355 * Placed here since identical code needed in both
356 * single and multi producer enqueue functions */
357 #define ENQUEUE_PTRS() do { \
358 const uint32_t size = r->size; \
359 uint32_t idx = prod_head & mask; \
360 if (likely(idx + n < size)) { \
361 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
362 r->ring[idx] = obj_table[i]; \
363 r->ring[idx+1] = obj_table[i+1]; \
364 r->ring[idx+2] = obj_table[i+2]; \
365 r->ring[idx+3] = obj_table[i+3]; \
368 case 3: r->ring[idx++] = obj_table[i++]; \
369 case 2: r->ring[idx++] = obj_table[i++]; \
370 case 1: r->ring[idx++] = obj_table[i++]; \
373 for (i = 0; idx < size; i++, idx++)\
374 r->ring[idx] = obj_table[i]; \
375 for (idx = 0; i < n; i++, idx++) \
376 r->ring[idx] = obj_table[i]; \
380 /* the actual copy of pointers on the ring to obj_table.
381 * Placed here since identical code needed in both
382 * single and multi consumer dequeue functions */
383 #define DEQUEUE_PTRS() do { \
384 uint32_t idx = cons_head & mask; \
385 const uint32_t size = r->size; \
386 if (likely(idx + n < size)) { \
387 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
388 obj_table[i] = r->ring[idx]; \
389 obj_table[i+1] = r->ring[idx+1]; \
390 obj_table[i+2] = r->ring[idx+2]; \
391 obj_table[i+3] = r->ring[idx+3]; \
394 case 3: obj_table[i++] = r->ring[idx++]; \
395 case 2: obj_table[i++] = r->ring[idx++]; \
396 case 1: obj_table[i++] = r->ring[idx++]; \
399 for (i = 0; idx < size; i++, idx++) \
400 obj_table[i] = r->ring[idx]; \
401 for (idx = 0; i < n; i++, idx++) \
402 obj_table[i] = r->ring[idx]; \
407 * @internal Enqueue several objects on the ring (multi-producers safe).
409 * This function uses a "compare and set" instruction to move the
410 * producer index atomically.
413 * A pointer to the ring structure.
415 * A pointer to a table of void * pointers (objects).
417 * The number of objects to add in the ring from the obj_table.
419 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
420 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
422 * Depend on the behavior value
423 * if behavior = RTE_RING_QUEUE_FIXED
424 * - 0: Success; objects enqueue.
425 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
426 * high water mark is exceeded.
427 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
428 * if behavior = RTE_RING_QUEUE_VARIABLE
429 * - n: Actual number of objects enqueued.
431 static inline int __attribute__((always_inline))
432 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
433 unsigned n, enum rte_ring_queue_behavior behavior)
435 uint32_t prod_head, prod_next;
436 uint32_t cons_tail, free_entries;
437 const unsigned max = n;
440 uint32_t mask = r->mask;
443 /* Avoid the unnecessary cmpset operation below, which is also
444 * potentially harmful when n equals 0. */
448 /* move prod.head atomically */
450 /* Reset n to the initial burst count */
453 prod_head = r->prod.head;
454 cons_tail = r->cons.tail;
455 /* The subtraction is done between two unsigned 32bits value
456 * (the result is always modulo 32 bits even if we have
457 * prod_head > cons_tail). So 'free_entries' is always between 0
458 * and size(ring)-1. */
459 free_entries = (mask + cons_tail - prod_head);
461 /* check that we have enough room in ring */
462 if (unlikely(n > free_entries)) {
463 if (behavior == RTE_RING_QUEUE_FIXED) {
464 __RING_STAT_ADD(r, enq_fail, n);
468 /* No free entry available */
469 if (unlikely(free_entries == 0)) {
470 __RING_STAT_ADD(r, enq_fail, n);
478 prod_next = prod_head + n;
479 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
481 } while (unlikely(success == 0));
483 /* write entries in ring */
487 /* if we exceed the watermark */
488 if (unlikely(((mask + 1) - free_entries + n) > r->watermark)) {
489 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
490 (int)(n | RTE_RING_QUOT_EXCEED);
491 __RING_STAT_ADD(r, enq_quota, n);
494 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
495 __RING_STAT_ADD(r, enq_success, n);
499 * If there are other enqueues in progress that preceded us,
500 * we need to wait for them to complete
502 while (unlikely(r->prod.tail != prod_head)) {
505 /* Set RTE_RING_PAUSE_REP_COUNT to avoid spin too long waiting
506 * for other thread finish. It gives pre-empted thread a chance
507 * to proceed and finish with ring dequeue operation. */
508 if (RTE_RING_PAUSE_REP_COUNT &&
509 ++rep == RTE_RING_PAUSE_REP_COUNT) {
514 r->prod.tail = prod_next;
519 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
522 * A pointer to the ring structure.
524 * A pointer to a table of void * pointers (objects).
526 * The number of objects to add in the ring from the obj_table.
528 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
529 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
531 * Depend on the behavior value
532 * if behavior = RTE_RING_QUEUE_FIXED
533 * - 0: Success; objects enqueue.
534 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
535 * high water mark is exceeded.
536 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
537 * if behavior = RTE_RING_QUEUE_VARIABLE
538 * - n: Actual number of objects enqueued.
540 static inline int __attribute__((always_inline))
541 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
542 unsigned n, enum rte_ring_queue_behavior behavior)
544 uint32_t prod_head, cons_tail;
545 uint32_t prod_next, free_entries;
547 uint32_t mask = r->mask;
550 prod_head = r->prod.head;
551 cons_tail = r->cons.tail;
552 /* The subtraction is done between two unsigned 32bits value
553 * (the result is always modulo 32 bits even if we have
554 * prod_head > cons_tail). So 'free_entries' is always between 0
555 * and size(ring)-1. */
556 free_entries = mask + cons_tail - prod_head;
558 /* check that we have enough room in ring */
559 if (unlikely(n > free_entries)) {
560 if (behavior == RTE_RING_QUEUE_FIXED) {
561 __RING_STAT_ADD(r, enq_fail, n);
565 /* No free entry available */
566 if (unlikely(free_entries == 0)) {
567 __RING_STAT_ADD(r, enq_fail, n);
575 prod_next = prod_head + n;
576 r->prod.head = prod_next;
578 /* write entries in ring */
582 /* if we exceed the watermark */
583 if (unlikely(((mask + 1) - free_entries + n) > r->watermark)) {
584 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
585 (int)(n | RTE_RING_QUOT_EXCEED);
586 __RING_STAT_ADD(r, enq_quota, n);
589 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
590 __RING_STAT_ADD(r, enq_success, n);
593 r->prod.tail = prod_next;
598 * @internal Dequeue several objects from a ring (multi-consumers safe). When
599 * the request objects are more than the available objects, only dequeue the
600 * actual number of objects
602 * This function uses a "compare and set" instruction to move the
603 * consumer index atomically.
606 * A pointer to the ring structure.
608 * A pointer to a table of void * pointers (objects) that will be filled.
610 * The number of objects to dequeue from the ring to the obj_table.
612 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
613 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
615 * Depend on the behavior value
616 * if behavior = RTE_RING_QUEUE_FIXED
617 * - 0: Success; objects dequeued.
618 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
620 * if behavior = RTE_RING_QUEUE_VARIABLE
621 * - n: Actual number of objects dequeued.
624 static inline int __attribute__((always_inline))
625 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
626 unsigned n, enum rte_ring_queue_behavior behavior)
628 uint32_t cons_head, prod_tail;
629 uint32_t cons_next, entries;
630 const unsigned max = n;
633 uint32_t mask = r->mask;
635 /* Avoid the unnecessary cmpset operation below, which is also
636 * potentially harmful when n equals 0. */
640 /* move cons.head atomically */
642 /* Restore n as it may change every loop */
645 cons_head = r->cons.head;
646 prod_tail = r->prod.tail;
647 /* The subtraction is done between two unsigned 32bits value
648 * (the result is always modulo 32 bits even if we have
649 * cons_head > prod_tail). So 'entries' is always between 0
650 * and size(ring)-1. */
651 entries = (prod_tail - cons_head);
653 /* Set the actual entries for dequeue */
655 if (behavior == RTE_RING_QUEUE_FIXED) {
656 __RING_STAT_ADD(r, deq_fail, n);
660 if (unlikely(entries == 0)){
661 __RING_STAT_ADD(r, deq_fail, n);
669 cons_next = cons_head + n;
670 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
672 } while (unlikely(success == 0));
679 * If there are other dequeues in progress that preceded us,
680 * we need to wait for them to complete
682 while (unlikely(r->cons.tail != cons_head)) {
685 /* Set RTE_RING_PAUSE_REP_COUNT to avoid spin too long waiting
686 * for other thread finish. It gives pre-empted thread a chance
687 * to proceed and finish with ring dequeue operation. */
688 if (RTE_RING_PAUSE_REP_COUNT &&
689 ++rep == RTE_RING_PAUSE_REP_COUNT) {
694 __RING_STAT_ADD(r, deq_success, n);
695 r->cons.tail = cons_next;
697 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
701 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
702 * When the request objects are more than the available objects, only dequeue
703 * the actual number of objects
706 * A pointer to the ring structure.
708 * A pointer to a table of void * pointers (objects) that will be filled.
710 * The number of objects to dequeue from the ring to the obj_table.
712 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
713 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
715 * Depend on the behavior value
716 * if behavior = RTE_RING_QUEUE_FIXED
717 * - 0: Success; objects dequeued.
718 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
720 * if behavior = RTE_RING_QUEUE_VARIABLE
721 * - n: Actual number of objects dequeued.
723 static inline int __attribute__((always_inline))
724 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
725 unsigned n, enum rte_ring_queue_behavior behavior)
727 uint32_t cons_head, prod_tail;
728 uint32_t cons_next, entries;
730 uint32_t mask = r->mask;
732 cons_head = r->cons.head;
733 prod_tail = r->prod.tail;
734 /* The subtraction is done between two unsigned 32bits value
735 * (the result is always modulo 32 bits even if we have
736 * cons_head > prod_tail). So 'entries' is always between 0
737 * and size(ring)-1. */
738 entries = prod_tail - cons_head;
741 if (behavior == RTE_RING_QUEUE_FIXED) {
742 __RING_STAT_ADD(r, deq_fail, n);
746 if (unlikely(entries == 0)){
747 __RING_STAT_ADD(r, deq_fail, n);
755 cons_next = cons_head + n;
756 r->cons.head = cons_next;
762 __RING_STAT_ADD(r, deq_success, n);
763 r->cons.tail = cons_next;
764 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
768 * Enqueue several objects on the ring (multi-producers safe).
770 * This function uses a "compare and set" instruction to move the
771 * producer index atomically.
774 * A pointer to the ring structure.
776 * A pointer to a table of void * pointers (objects).
778 * The number of objects to add in the ring from the obj_table.
780 * - 0: Success; objects enqueue.
781 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
782 * high water mark is exceeded.
783 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
785 static inline int __attribute__((always_inline))
786 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
789 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
793 * Enqueue several objects on a ring (NOT multi-producers safe).
796 * A pointer to the ring structure.
798 * A pointer to a table of void * pointers (objects).
800 * The number of objects to add in the ring from the obj_table.
802 * - 0: Success; objects enqueued.
803 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
804 * high water mark is exceeded.
805 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
807 static inline int __attribute__((always_inline))
808 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
811 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
815 * Enqueue several objects on a ring.
817 * This function calls the multi-producer or the single-producer
818 * version depending on the default behavior that was specified at
819 * ring creation time (see flags).
822 * A pointer to the ring structure.
824 * A pointer to a table of void * pointers (objects).
826 * The number of objects to add in the ring from the obj_table.
828 * - 0: Success; objects enqueued.
829 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
830 * high water mark is exceeded.
831 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
833 static inline int __attribute__((always_inline))
834 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
838 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
840 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
844 * Enqueue one object on a ring (multi-producers safe).
846 * This function uses a "compare and set" instruction to move the
847 * producer index atomically.
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_mp_enqueue(struct rte_ring *r, void *obj)
862 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
866 * Enqueue one object on a ring (NOT multi-producers safe).
869 * A pointer to the ring structure.
871 * A pointer to the object to be added.
873 * - 0: Success; objects enqueued.
874 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
875 * high water mark is exceeded.
876 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
878 static inline int __attribute__((always_inline))
879 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
881 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
885 * Enqueue one object on a ring.
887 * This function calls the multi-producer or the single-producer
888 * version, depending on the default behaviour that was specified at
889 * ring creation time (see flags).
892 * A pointer to the ring structure.
894 * A pointer to the object to be added.
896 * - 0: Success; objects enqueued.
897 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
898 * high water mark is exceeded.
899 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
901 static inline int __attribute__((always_inline))
902 rte_ring_enqueue(struct rte_ring *r, void *obj)
905 return rte_ring_sp_enqueue(r, obj);
907 return rte_ring_mp_enqueue(r, obj);
911 * Dequeue several objects from a ring (multi-consumers safe).
913 * This function uses a "compare and set" instruction to move the
914 * consumer index atomically.
917 * A pointer to the ring structure.
919 * A pointer to a table of void * pointers (objects) that will be filled.
921 * The number of objects to dequeue from the ring to the obj_table.
923 * - 0: Success; objects dequeued.
924 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
927 static inline int __attribute__((always_inline))
928 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
930 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
934 * Dequeue several objects from a ring (NOT multi-consumers safe).
937 * A pointer to the ring structure.
939 * A pointer to a table of void * pointers (objects) that will be filled.
941 * The number of objects to dequeue from the ring to the obj_table,
942 * must be strictly positive.
944 * - 0: Success; objects dequeued.
945 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
948 static inline int __attribute__((always_inline))
949 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
951 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
955 * Dequeue several objects from a ring.
957 * This function calls the multi-consumers or the single-consumer
958 * version, depending on the default behaviour that was specified at
959 * ring creation time (see flags).
962 * A pointer to the ring structure.
964 * A pointer to a table of void * pointers (objects) that will be filled.
966 * The number of objects to dequeue from the ring to the obj_table.
968 * - 0: Success; objects dequeued.
969 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
972 static inline int __attribute__((always_inline))
973 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
976 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
978 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
982 * Dequeue one object from a ring (multi-consumers safe).
984 * This function uses a "compare and set" instruction to move the
985 * consumer index atomically.
988 * A pointer to the ring structure.
990 * A pointer to a void * pointer (object) that will be filled.
992 * - 0: Success; objects dequeued.
993 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
996 static inline int __attribute__((always_inline))
997 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
999 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
1003 * Dequeue one object from a ring (NOT multi-consumers safe).
1006 * A pointer to the ring structure.
1008 * A pointer to a void * pointer (object) that will be filled.
1010 * - 0: Success; objects dequeued.
1011 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
1014 static inline int __attribute__((always_inline))
1015 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
1017 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
1021 * Dequeue one object from a ring.
1023 * This function calls the multi-consumers or the single-consumer
1024 * version depending on the default behaviour that was specified at
1025 * ring creation time (see flags).
1028 * A pointer to the ring structure.
1030 * A pointer to a void * pointer (object) that will be filled.
1032 * - 0: Success, objects dequeued.
1033 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
1036 static inline int __attribute__((always_inline))
1037 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
1040 return rte_ring_sc_dequeue(r, obj_p);
1042 return rte_ring_mc_dequeue(r, obj_p);
1046 * Test if a ring is full.
1049 * A pointer to the ring structure.
1051 * - 1: The ring is full.
1052 * - 0: The ring is not full.
1055 rte_ring_full(const struct rte_ring *r)
1057 uint32_t prod_tail = r->prod.tail;
1058 uint32_t cons_tail = r->cons.tail;
1059 return ((cons_tail - prod_tail - 1) & r->mask) == 0;
1063 * Test if a ring is empty.
1066 * A pointer to the ring structure.
1068 * - 1: The ring is empty.
1069 * - 0: The ring is not empty.
1072 rte_ring_empty(const struct rte_ring *r)
1074 uint32_t prod_tail = r->prod.tail;
1075 uint32_t cons_tail = r->cons.tail;
1076 return !!(cons_tail == prod_tail);
1080 * Return the number of entries in a ring.
1083 * A pointer to the ring structure.
1085 * The number of entries in the ring.
1087 static inline unsigned
1088 rte_ring_count(const struct rte_ring *r)
1090 uint32_t prod_tail = r->prod.tail;
1091 uint32_t cons_tail = r->cons.tail;
1092 return (prod_tail - cons_tail) & r->mask;
1096 * Return the number of free entries in a ring.
1099 * A pointer to the ring structure.
1101 * The number of free entries in the ring.
1103 static inline unsigned
1104 rte_ring_free_count(const struct rte_ring *r)
1106 uint32_t prod_tail = r->prod.tail;
1107 uint32_t cons_tail = r->cons.tail;
1108 return (cons_tail - prod_tail - 1) & r->mask;
1112 * Return the size of the ring.
1115 * A pointer to the ring structure.
1117 * The number of elements which can be stored in the ring.
1119 static inline unsigned int
1120 rte_ring_get_size(const struct rte_ring *r)
1126 * Dump the status of all rings on the console
1129 * A pointer to a file for output
1131 void rte_ring_list_dump(FILE *f);
1134 * Search a ring from its name
1137 * The name of the ring.
1139 * The pointer to the ring matching the name, or NULL if not found,
1140 * with rte_errno set appropriately. Possible rte_errno values include:
1141 * - ENOENT - required entry not available to return.
1143 struct rte_ring *rte_ring_lookup(const char *name);
1146 * Enqueue several objects on the ring (multi-producers safe).
1148 * This function uses a "compare and set" instruction to move the
1149 * producer index atomically.
1152 * A pointer to the ring structure.
1154 * A pointer to a table of void * pointers (objects).
1156 * The number of objects to add in the ring from the obj_table.
1158 * - n: Actual number of objects enqueued.
1160 static inline unsigned __attribute__((always_inline))
1161 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1164 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1168 * Enqueue several objects on a ring (NOT multi-producers safe).
1171 * A pointer to the ring structure.
1173 * A pointer to a table of void * pointers (objects).
1175 * The number of objects to add in the ring from the obj_table.
1177 * - n: Actual number of objects enqueued.
1179 static inline unsigned __attribute__((always_inline))
1180 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1183 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1187 * Enqueue several objects on a ring.
1189 * This function calls the multi-producer or the single-producer
1190 * version depending on the default behavior that was specified at
1191 * ring creation time (see flags).
1194 * A pointer to the ring structure.
1196 * A pointer to a table of void * pointers (objects).
1198 * The number of objects to add in the ring from the obj_table.
1200 * - n: Actual number of objects enqueued.
1202 static inline unsigned __attribute__((always_inline))
1203 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1207 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1209 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1213 * Dequeue several objects from a ring (multi-consumers safe). When the request
1214 * objects are more than the available objects, only dequeue the actual number
1217 * This function uses a "compare and set" instruction to move the
1218 * consumer index atomically.
1221 * A pointer to the ring structure.
1223 * A pointer to a table of void * pointers (objects) that will be filled.
1225 * The number of objects to dequeue from the ring to the obj_table.
1227 * - n: Actual number of objects dequeued, 0 if ring is empty
1229 static inline unsigned __attribute__((always_inline))
1230 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1232 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1236 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1237 * request objects are more than the available objects, only dequeue the
1238 * actual number of objects
1241 * A pointer to the ring structure.
1243 * A pointer to a table of void * pointers (objects) that will be filled.
1245 * The number of objects to dequeue from the ring to the obj_table.
1247 * - n: Actual number of objects dequeued, 0 if ring is empty
1249 static inline unsigned __attribute__((always_inline))
1250 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1252 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1256 * Dequeue multiple objects from a ring up to a maximum number.
1258 * This function calls the multi-consumers or the single-consumer
1259 * version, depending on the default behaviour that was specified at
1260 * ring creation time (see flags).
1263 * A pointer to the ring structure.
1265 * A pointer to a table of void * pointers (objects) that will be filled.
1267 * The number of objects to dequeue from the ring to the obj_table.
1269 * - Number of objects dequeued
1271 static inline unsigned __attribute__((always_inline))
1272 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1275 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1277 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1284 #endif /* _RTE_RING_H_ */