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 enum rte_ring_queue_behavior {
105 RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
106 RTE_RING_QUEUE_VARIABLE /* Enq/Deq as many items a possible from ring */
109 #ifdef RTE_LIBRTE_RING_DEBUG
111 * A structure that stores the ring statistics (per-lcore).
113 struct rte_ring_debug_stats {
114 uint64_t enq_success_bulk; /**< Successful enqueues number. */
115 uint64_t enq_success_objs; /**< Objects successfully enqueued. */
116 uint64_t enq_quota_bulk; /**< Successful enqueues above watermark. */
117 uint64_t enq_quota_objs; /**< Objects enqueued above watermark. */
118 uint64_t enq_fail_bulk; /**< Failed enqueues number. */
119 uint64_t enq_fail_objs; /**< Objects that failed to be enqueued. */
120 uint64_t deq_success_bulk; /**< Successful dequeues number. */
121 uint64_t deq_success_objs; /**< Objects successfully dequeued. */
122 uint64_t deq_fail_bulk; /**< Failed dequeues number. */
123 uint64_t deq_fail_objs; /**< Objects that failed to be dequeued. */
124 } __rte_cache_aligned;
127 #define RTE_RING_NAMESIZE 32 /**< The maximum length of a ring name. */
128 #define RTE_RING_MZ_PREFIX "RG_"
131 * An RTE ring structure.
133 * The producer and the consumer have a head and a tail index. The particularity
134 * of these index is that they are not between 0 and size(ring). These indexes
135 * are between 0 and 2^32, and we mask their value when we access the ring[]
136 * field. Thanks to this assumption, we can do subtractions between 2 index
137 * values in a modulo-32bit base: that's why the overflow of the indexes is not
141 char name[RTE_RING_NAMESIZE]; /**< Name of the ring. */
142 int flags; /**< Flags supplied at creation. */
144 /** Ring producer status. */
146 uint32_t watermark; /**< Maximum items before EDQUOT. */
147 uint32_t sp_enqueue; /**< True, if single producer. */
148 uint32_t size; /**< Size of ring. */
149 uint32_t mask; /**< Mask (size-1) of ring. */
150 volatile uint32_t head; /**< Producer head. */
151 volatile uint32_t tail; /**< Producer tail. */
152 } prod __rte_cache_aligned;
154 /** Ring consumer status. */
156 uint32_t sc_dequeue; /**< True, if single consumer. */
157 uint32_t size; /**< Size of the ring. */
158 uint32_t mask; /**< Mask (size-1) of ring. */
159 volatile uint32_t head; /**< Consumer head. */
160 volatile uint32_t tail; /**< Consumer tail. */
161 #ifdef RTE_RING_SPLIT_PROD_CONS
162 } cons __rte_cache_aligned;
167 #ifdef RTE_LIBRTE_RING_DEBUG
168 struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
171 void * ring[0] __rte_cache_aligned; /**< Memory space of ring starts here.
172 * not volatile so need to be careful
173 * about compiler re-ordering */
176 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
177 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
178 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
179 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
182 * @internal When debug is enabled, store ring statistics.
184 * A pointer to the ring.
186 * The name of the statistics field to increment in the ring.
188 * The number to add to the object-oriented statistics.
190 #ifdef RTE_LIBRTE_RING_DEBUG
191 #define __RING_STAT_ADD(r, name, n) do { \
192 unsigned __lcore_id = rte_lcore_id(); \
193 r->stats[__lcore_id].name##_objs += n; \
194 r->stats[__lcore_id].name##_bulk += 1; \
197 #define __RING_STAT_ADD(r, name, n) do {} while(0)
201 * Calculate the memory size needed for a ring
203 * This function returns the number of bytes needed for a ring, given
204 * the number of elements in it. This value is the sum of the size of
205 * the structure rte_ring and the size of the memory needed by the
206 * objects pointers. The value is aligned to a cache line size.
209 * The number of elements in the ring (must be a power of 2).
211 * - The memory size needed for the ring on success.
212 * - -EINVAL if count is not a power of 2.
214 ssize_t rte_ring_get_memsize(unsigned count);
217 * Initialize a ring structure.
219 * Initialize a ring structure in memory pointed by "r". The size of the
220 * memory area must be large enough to store the ring structure and the
221 * object table. It is advised to use rte_ring_get_memsize() to get the
224 * The ring size is set to *count*, which must be a power of two. Water
225 * marking is disabled by default. The real usable ring size is
226 * *count-1* instead of *count* to differentiate a free ring from an
229 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
230 * memory given by the caller may not be shareable among dpdk
234 * The pointer to the ring structure followed by the objects table.
236 * The name of the ring.
238 * The number of elements in the ring (must be a power of 2).
240 * An OR of the following:
241 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
242 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
243 * is "single-producer". Otherwise, it is "multi-producers".
244 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
245 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
246 * is "single-consumer". Otherwise, it is "multi-consumers".
248 * 0 on success, or a negative value on error.
250 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
254 * Create a new ring named *name* in memory.
256 * This function uses ``memzone_reserve()`` to allocate memory. Then it
257 * calls rte_ring_init() to initialize an empty ring.
259 * The new ring size is set to *count*, which must be a power of
260 * two. Water marking is disabled by default. The real usable ring size
261 * is *count-1* instead of *count* to differentiate a free ring from an
264 * The ring is added in RTE_TAILQ_RING list.
267 * The name of the ring.
269 * The size of the ring (must be a power of 2).
271 * The *socket_id* argument is the socket identifier in case of
272 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
273 * constraint for the reserved zone.
275 * An OR of the following:
276 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
277 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
278 * is "single-producer". Otherwise, it is "multi-producers".
279 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
280 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
281 * is "single-consumer". Otherwise, it is "multi-consumers".
283 * On success, the pointer to the new allocated ring. NULL on error with
284 * rte_errno set appropriately. Possible errno values include:
285 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
286 * - E_RTE_SECONDARY - function was called from a secondary process instance
287 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring list
288 * - EINVAL - count provided is not a power of 2
289 * - ENOSPC - the maximum number of memzones has already been allocated
290 * - EEXIST - a memzone with the same name already exists
291 * - ENOMEM - no appropriate memory area found in which to create memzone
293 struct rte_ring *rte_ring_create(const char *name, unsigned count,
294 int socket_id, unsigned flags);
297 * Change the high water mark.
299 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
300 * *count* value. The *count* value must be greater than 0 and less
301 * than the ring size.
303 * This function can be called at any time (not necessarily at
307 * A pointer to the ring structure.
309 * The new water mark value.
311 * - 0: Success; water mark changed.
312 * - -EINVAL: Invalid water mark value.
314 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
317 * Dump the status of the ring to the console.
320 * A pointer to a file for output
322 * A pointer to the ring structure.
324 void rte_ring_dump(FILE *f, const struct rte_ring *r);
326 /* the actual enqueue of pointers on the ring.
327 * Placed here since identical code needed in both
328 * single and multi producer enqueue functions */
329 #define ENQUEUE_PTRS() do { \
330 const uint32_t size = r->prod.size; \
331 uint32_t idx = prod_head & mask; \
332 if (likely(idx + n < size)) { \
333 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
334 r->ring[idx] = obj_table[i]; \
335 r->ring[idx+1] = obj_table[i+1]; \
336 r->ring[idx+2] = obj_table[i+2]; \
337 r->ring[idx+3] = obj_table[i+3]; \
340 case 3: r->ring[idx++] = obj_table[i++]; \
341 case 2: r->ring[idx++] = obj_table[i++]; \
342 case 1: r->ring[idx++] = obj_table[i++]; \
345 for (i = 0; idx < size; i++, idx++)\
346 r->ring[idx] = obj_table[i]; \
347 for (idx = 0; i < n; i++, idx++) \
348 r->ring[idx] = obj_table[i]; \
352 /* the actual copy of pointers on the ring to obj_table.
353 * Placed here since identical code needed in both
354 * single and multi consumer dequeue functions */
355 #define DEQUEUE_PTRS() do { \
356 uint32_t idx = cons_head & mask; \
357 const uint32_t size = r->cons.size; \
358 if (likely(idx + n < size)) { \
359 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
360 obj_table[i] = r->ring[idx]; \
361 obj_table[i+1] = r->ring[idx+1]; \
362 obj_table[i+2] = r->ring[idx+2]; \
363 obj_table[i+3] = r->ring[idx+3]; \
366 case 3: obj_table[i++] = r->ring[idx++]; \
367 case 2: obj_table[i++] = r->ring[idx++]; \
368 case 1: obj_table[i++] = r->ring[idx++]; \
371 for (i = 0; idx < size; i++, idx++) \
372 obj_table[i] = r->ring[idx]; \
373 for (idx = 0; i < n; i++, idx++) \
374 obj_table[i] = r->ring[idx]; \
379 * @internal Enqueue several objects on the ring (multi-producers safe).
381 * This function uses a "compare and set" instruction to move the
382 * producer index atomically.
385 * A pointer to the ring structure.
387 * A pointer to a table of void * pointers (objects).
389 * The number of objects to add in the ring from the obj_table.
391 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
392 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
394 * Depend on the behavior value
395 * if behavior = RTE_RING_QUEUE_FIXED
396 * - 0: Success; objects enqueue.
397 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
398 * high water mark is exceeded.
399 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
400 * if behavior = RTE_RING_QUEUE_VARIABLE
401 * - n: Actual number of objects enqueued.
403 static inline int __attribute__((always_inline))
404 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
405 unsigned n, enum rte_ring_queue_behavior behavior)
407 uint32_t prod_head, prod_next;
408 uint32_t cons_tail, free_entries;
409 const unsigned max = n;
412 uint32_t mask = r->prod.mask;
415 /* move prod.head atomically */
417 /* Reset n to the initial burst count */
420 prod_head = r->prod.head;
421 cons_tail = r->cons.tail;
422 /* The subtraction is done between two unsigned 32bits value
423 * (the result is always modulo 32 bits even if we have
424 * prod_head > cons_tail). So 'free_entries' is always between 0
425 * and size(ring)-1. */
426 free_entries = (mask + cons_tail - prod_head);
428 /* check that we have enough room in ring */
429 if (unlikely(n > free_entries)) {
430 if (behavior == RTE_RING_QUEUE_FIXED) {
431 __RING_STAT_ADD(r, enq_fail, n);
435 /* No free entry available */
436 if (unlikely(free_entries == 0)) {
437 __RING_STAT_ADD(r, enq_fail, n);
445 prod_next = prod_head + n;
446 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
448 } while (unlikely(success == 0));
450 /* write entries in ring */
452 rte_compiler_barrier();
454 /* if we exceed the watermark */
455 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
456 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
457 (int)(n | RTE_RING_QUOT_EXCEED);
458 __RING_STAT_ADD(r, enq_quota, n);
461 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
462 __RING_STAT_ADD(r, enq_success, n);
466 * If there are other enqueues in progress that preceded us,
467 * we need to wait for them to complete
469 while (unlikely(r->prod.tail != prod_head))
472 r->prod.tail = prod_next;
477 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
480 * A pointer to the ring structure.
482 * A pointer to a table of void * pointers (objects).
484 * The number of objects to add in the ring from the obj_table.
486 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
487 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
489 * Depend on the behavior value
490 * if behavior = RTE_RING_QUEUE_FIXED
491 * - 0: Success; objects enqueue.
492 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
493 * high water mark is exceeded.
494 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
495 * if behavior = RTE_RING_QUEUE_VARIABLE
496 * - n: Actual number of objects enqueued.
498 static inline int __attribute__((always_inline))
499 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
500 unsigned n, enum rte_ring_queue_behavior behavior)
502 uint32_t prod_head, cons_tail;
503 uint32_t prod_next, free_entries;
505 uint32_t mask = r->prod.mask;
508 prod_head = r->prod.head;
509 cons_tail = r->cons.tail;
510 /* The subtraction is done between two unsigned 32bits value
511 * (the result is always modulo 32 bits even if we have
512 * prod_head > cons_tail). So 'free_entries' is always between 0
513 * and size(ring)-1. */
514 free_entries = mask + cons_tail - prod_head;
516 /* check that we have enough room in ring */
517 if (unlikely(n > free_entries)) {
518 if (behavior == RTE_RING_QUEUE_FIXED) {
519 __RING_STAT_ADD(r, enq_fail, n);
523 /* No free entry available */
524 if (unlikely(free_entries == 0)) {
525 __RING_STAT_ADD(r, enq_fail, n);
533 prod_next = prod_head + n;
534 r->prod.head = prod_next;
536 /* write entries in ring */
538 rte_compiler_barrier();
540 /* if we exceed the watermark */
541 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
542 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
543 (int)(n | RTE_RING_QUOT_EXCEED);
544 __RING_STAT_ADD(r, enq_quota, n);
547 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
548 __RING_STAT_ADD(r, enq_success, n);
551 r->prod.tail = prod_next;
556 * @internal Dequeue several objects from a ring (multi-consumers safe). When
557 * the request objects are more than the available objects, only dequeue the
558 * actual number of objects
560 * This function uses a "compare and set" instruction to move the
561 * consumer index atomically.
564 * A pointer to the ring structure.
566 * A pointer to a table of void * pointers (objects) that will be filled.
568 * The number of objects to dequeue from the ring to the obj_table.
570 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
571 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
573 * Depend on the behavior value
574 * if behavior = RTE_RING_QUEUE_FIXED
575 * - 0: Success; objects dequeued.
576 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
578 * if behavior = RTE_RING_QUEUE_VARIABLE
579 * - n: Actual number of objects dequeued.
582 static inline int __attribute__((always_inline))
583 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
584 unsigned n, enum rte_ring_queue_behavior behavior)
586 uint32_t cons_head, prod_tail;
587 uint32_t cons_next, entries;
588 const unsigned max = n;
591 uint32_t mask = r->prod.mask;
593 /* move cons.head atomically */
595 /* Restore n as it may change every loop */
598 cons_head = r->cons.head;
599 prod_tail = r->prod.tail;
600 /* The subtraction is done between two unsigned 32bits value
601 * (the result is always modulo 32 bits even if we have
602 * cons_head > prod_tail). So 'entries' is always between 0
603 * and size(ring)-1. */
604 entries = (prod_tail - cons_head);
606 /* Set the actual entries for dequeue */
608 if (behavior == RTE_RING_QUEUE_FIXED) {
609 __RING_STAT_ADD(r, deq_fail, n);
613 if (unlikely(entries == 0)){
614 __RING_STAT_ADD(r, deq_fail, n);
622 cons_next = cons_head + n;
623 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
625 } while (unlikely(success == 0));
629 rte_compiler_barrier();
632 * If there are other dequeues in progress that preceded us,
633 * we need to wait for them to complete
635 while (unlikely(r->cons.tail != cons_head))
638 __RING_STAT_ADD(r, deq_success, n);
639 r->cons.tail = cons_next;
641 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
645 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
646 * When the request objects are more than the available objects, only dequeue
647 * the actual number of objects
650 * A pointer to the ring structure.
652 * A pointer to a table of void * pointers (objects) that will be filled.
654 * The number of objects to dequeue from the ring to the obj_table.
656 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
657 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
659 * Depend on the behavior value
660 * if behavior = RTE_RING_QUEUE_FIXED
661 * - 0: Success; objects dequeued.
662 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
664 * if behavior = RTE_RING_QUEUE_VARIABLE
665 * - n: Actual number of objects dequeued.
667 static inline int __attribute__((always_inline))
668 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
669 unsigned n, enum rte_ring_queue_behavior behavior)
671 uint32_t cons_head, prod_tail;
672 uint32_t cons_next, entries;
674 uint32_t mask = r->prod.mask;
676 cons_head = r->cons.head;
677 prod_tail = r->prod.tail;
678 /* The subtraction is done between two unsigned 32bits value
679 * (the result is always modulo 32 bits even if we have
680 * cons_head > prod_tail). So 'entries' is always between 0
681 * and size(ring)-1. */
682 entries = prod_tail - cons_head;
685 if (behavior == RTE_RING_QUEUE_FIXED) {
686 __RING_STAT_ADD(r, deq_fail, n);
690 if (unlikely(entries == 0)){
691 __RING_STAT_ADD(r, deq_fail, n);
699 cons_next = cons_head + n;
700 r->cons.head = cons_next;
704 rte_compiler_barrier();
706 __RING_STAT_ADD(r, deq_success, n);
707 r->cons.tail = cons_next;
708 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
712 * Enqueue several objects on the ring (multi-producers safe).
714 * This function uses a "compare and set" instruction to move the
715 * producer index atomically.
718 * A pointer to the ring structure.
720 * A pointer to a table of void * pointers (objects).
722 * The number of objects to add in the ring from the obj_table.
724 * - 0: Success; objects enqueue.
725 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
726 * high water mark is exceeded.
727 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
729 static inline int __attribute__((always_inline))
730 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
733 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
737 * Enqueue several objects on a ring (NOT multi-producers safe).
740 * A pointer to the ring structure.
742 * A pointer to a table of void * pointers (objects).
744 * The number of objects to add in the ring from the obj_table.
746 * - 0: Success; objects enqueued.
747 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
748 * high water mark is exceeded.
749 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
751 static inline int __attribute__((always_inline))
752 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
755 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
759 * Enqueue several objects on a ring.
761 * This function calls the multi-producer or the single-producer
762 * version depending on the default behavior that was specified at
763 * ring creation time (see flags).
766 * A pointer to the ring structure.
768 * A pointer to a table of void * pointers (objects).
770 * The number of objects to add in the ring from the obj_table.
772 * - 0: Success; objects enqueued.
773 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
774 * high water mark is exceeded.
775 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
777 static inline int __attribute__((always_inline))
778 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
781 if (r->prod.sp_enqueue)
782 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
784 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
788 * Enqueue one object on a ring (multi-producers safe).
790 * This function uses a "compare and set" instruction to move the
791 * producer index atomically.
794 * A pointer to the ring structure.
796 * A pointer to the object to be added.
798 * - 0: Success; objects enqueued.
799 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
800 * high water mark is exceeded.
801 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
803 static inline int __attribute__((always_inline))
804 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
806 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
810 * Enqueue one object on a ring (NOT multi-producers safe).
813 * A pointer to the ring structure.
815 * A pointer to the object to be added.
817 * - 0: Success; objects enqueued.
818 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
819 * high water mark is exceeded.
820 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
822 static inline int __attribute__((always_inline))
823 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
825 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
829 * Enqueue one object on a ring.
831 * This function calls the multi-producer or the single-producer
832 * version, depending on the default behaviour that was specified at
833 * ring creation time (see flags).
836 * A pointer to the ring structure.
838 * A pointer to the object to be added.
840 * - 0: Success; objects enqueued.
841 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
842 * high water mark is exceeded.
843 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
845 static inline int __attribute__((always_inline))
846 rte_ring_enqueue(struct rte_ring *r, void *obj)
848 if (r->prod.sp_enqueue)
849 return rte_ring_sp_enqueue(r, obj);
851 return rte_ring_mp_enqueue(r, obj);
855 * Dequeue several objects from a ring (multi-consumers safe).
857 * This function uses a "compare and set" instruction to move the
858 * consumer index atomically.
861 * A pointer to the ring structure.
863 * A pointer to a table of void * pointers (objects) that will be filled.
865 * The number of objects to dequeue from the ring to the obj_table.
867 * - 0: Success; objects dequeued.
868 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
871 static inline int __attribute__((always_inline))
872 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
874 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
878 * Dequeue several objects from a ring (NOT multi-consumers safe).
881 * A pointer to the ring structure.
883 * A pointer to a table of void * pointers (objects) that will be filled.
885 * The number of objects to dequeue from the ring to the obj_table,
886 * must be strictly positive.
888 * - 0: Success; objects dequeued.
889 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
892 static inline int __attribute__((always_inline))
893 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
895 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
899 * Dequeue several objects from a ring.
901 * This function calls the multi-consumers or the single-consumer
902 * version, depending on the default behaviour that was specified at
903 * ring creation time (see flags).
906 * A pointer to the ring structure.
908 * A pointer to a table of void * pointers (objects) that will be filled.
910 * The number of objects to dequeue from the ring to the obj_table.
912 * - 0: Success; objects dequeued.
913 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
916 static inline int __attribute__((always_inline))
917 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
919 if (r->cons.sc_dequeue)
920 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
922 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
926 * Dequeue one object from a ring (multi-consumers safe).
928 * This function uses a "compare and set" instruction to move the
929 * consumer index atomically.
932 * A pointer to the ring structure.
934 * A pointer to a void * pointer (object) that will be filled.
936 * - 0: Success; objects dequeued.
937 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
940 static inline int __attribute__((always_inline))
941 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
943 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
947 * Dequeue one object from a ring (NOT multi-consumers safe).
950 * A pointer to the ring structure.
952 * A pointer to a void * pointer (object) that will be filled.
954 * - 0: Success; objects dequeued.
955 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
958 static inline int __attribute__((always_inline))
959 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
961 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
965 * Dequeue one object from a ring.
967 * This function calls the multi-consumers or the single-consumer
968 * version depending on the default behaviour that was specified at
969 * ring creation time (see flags).
972 * A pointer to the ring structure.
974 * A pointer to a void * pointer (object) that will be filled.
976 * - 0: Success, objects dequeued.
977 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
980 static inline int __attribute__((always_inline))
981 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
983 if (r->cons.sc_dequeue)
984 return rte_ring_sc_dequeue(r, obj_p);
986 return rte_ring_mc_dequeue(r, obj_p);
990 * Test if a ring is full.
993 * A pointer to the ring structure.
995 * - 1: The ring is full.
996 * - 0: The ring is not full.
999 rte_ring_full(const struct rte_ring *r)
1001 uint32_t prod_tail = r->prod.tail;
1002 uint32_t cons_tail = r->cons.tail;
1003 return (((cons_tail - prod_tail - 1) & r->prod.mask) == 0);
1007 * Test if a ring is empty.
1010 * A pointer to the ring structure.
1012 * - 1: The ring is empty.
1013 * - 0: The ring is not empty.
1016 rte_ring_empty(const struct rte_ring *r)
1018 uint32_t prod_tail = r->prod.tail;
1019 uint32_t cons_tail = r->cons.tail;
1020 return !!(cons_tail == prod_tail);
1024 * Return the number of entries in a ring.
1027 * A pointer to the ring structure.
1029 * The number of entries in the ring.
1031 static inline unsigned
1032 rte_ring_count(const struct rte_ring *r)
1034 uint32_t prod_tail = r->prod.tail;
1035 uint32_t cons_tail = r->cons.tail;
1036 return ((prod_tail - cons_tail) & r->prod.mask);
1040 * Return the number of free entries in a ring.
1043 * A pointer to the ring structure.
1045 * The number of free entries in the ring.
1047 static inline unsigned
1048 rte_ring_free_count(const struct rte_ring *r)
1050 uint32_t prod_tail = r->prod.tail;
1051 uint32_t cons_tail = r->cons.tail;
1052 return ((cons_tail - prod_tail - 1) & r->prod.mask);
1056 * Dump the status of all rings on the console
1059 * A pointer to a file for output
1061 void rte_ring_list_dump(FILE *f);
1064 * Search a ring from its name
1067 * The name of the ring.
1069 * The pointer to the ring matching the name, or NULL if not found,
1070 * with rte_errno set appropriately. Possible rte_errno values include:
1071 * - ENOENT - required entry not available to return.
1073 struct rte_ring *rte_ring_lookup(const char *name);
1076 * Enqueue several objects on the ring (multi-producers safe).
1078 * This function uses a "compare and set" instruction to move the
1079 * producer index atomically.
1082 * A pointer to the ring structure.
1084 * A pointer to a table of void * pointers (objects).
1086 * The number of objects to add in the ring from the obj_table.
1088 * - n: Actual number of objects enqueued.
1090 static inline unsigned __attribute__((always_inline))
1091 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1094 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1098 * Enqueue several objects on a ring (NOT multi-producers safe).
1101 * A pointer to the ring structure.
1103 * A pointer to a table of void * pointers (objects).
1105 * The number of objects to add in the ring from the obj_table.
1107 * - n: Actual number of objects enqueued.
1109 static inline unsigned __attribute__((always_inline))
1110 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1113 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1117 * Enqueue several objects on a ring.
1119 * This function calls the multi-producer or the single-producer
1120 * version depending on the default behavior that was specified at
1121 * ring creation time (see flags).
1124 * A pointer to the ring structure.
1126 * A pointer to a table of void * pointers (objects).
1128 * The number of objects to add in the ring from the obj_table.
1130 * - n: Actual number of objects enqueued.
1132 static inline unsigned __attribute__((always_inline))
1133 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1136 if (r->prod.sp_enqueue)
1137 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1139 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1143 * Dequeue several objects from a ring (multi-consumers safe). When the request
1144 * objects are more than the available objects, only dequeue the actual number
1147 * This function uses a "compare and set" instruction to move the
1148 * consumer index atomically.
1151 * A pointer to the ring structure.
1153 * A pointer to a table of void * pointers (objects) that will be filled.
1155 * The number of objects to dequeue from the ring to the obj_table.
1157 * - n: Actual number of objects dequeued, 0 if ring is empty
1159 static inline unsigned __attribute__((always_inline))
1160 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1162 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1166 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1167 * request objects are more than the available objects, only dequeue the
1168 * actual number of objects
1171 * A pointer to the ring structure.
1173 * A pointer to a table of void * pointers (objects) that will be filled.
1175 * The number of objects to dequeue from the ring to the obj_table.
1177 * - n: Actual number of objects dequeued, 0 if ring is empty
1179 static inline unsigned __attribute__((always_inline))
1180 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1182 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1186 * Dequeue multiple objects from a ring up to a maximum number.
1188 * This function calls the multi-consumers or the single-consumer
1189 * version, depending on the default behaviour that was specified at
1190 * ring creation time (see flags).
1193 * A pointer to the ring structure.
1195 * A pointer to a table of void * pointers (objects) that will be filled.
1197 * The number of objects to dequeue from the ring to the obj_table.
1199 * - Number of objects dequeued
1201 static inline unsigned __attribute__((always_inline))
1202 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1204 if (r->cons.sc_dequeue)
1205 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1207 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1214 #endif /* _RTE_RING_H_ */