4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Derived from FreeBSD's bufring.h
37 **************************************************************************
39 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions are met:
45 * 1. Redistributions of source code must retain the above copyright notice,
46 * this list of conditions and the following disclaimer.
48 * 2. The name of Kip Macy nor the names of other
49 * contributors may be used to endorse or promote products derived from
50 * this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
53 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
56 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62 * POSSIBILITY OF SUCH DAMAGE.
64 ***************************************************************************/
73 * The Ring Manager is a fixed-size queue, implemented as a table of
74 * pointers. Head and tail pointers are modified atomically, allowing
75 * concurrent access to it. It has the following features:
77 * - FIFO (First In First Out)
78 * - Maximum size is fixed; the pointers are stored in a table.
79 * - Lockless implementation.
80 * - Multi- or single-consumer dequeue.
81 * - Multi- or single-producer enqueue.
85 * Note: the ring implementation is not preemptable. A lcore must not
86 * be interrupted by another task that uses the same ring.
95 #include <sys/queue.h>
97 #include <rte_common.h>
98 #include <rte_memory.h>
99 #include <rte_lcore.h>
100 #include <rte_atomic.h>
101 #include <rte_branch_prediction.h>
103 enum rte_ring_queue_behavior {
104 RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
105 RTE_RING_QUEUE_VARIABLE /* Enq/Deq as many items a possible from ring */
108 #ifdef RTE_LIBRTE_RING_DEBUG
110 * A structure that stores the ring statistics (per-lcore).
112 struct rte_ring_debug_stats {
113 uint64_t enq_success_bulk; /**< Successful enqueues number. */
114 uint64_t enq_success_objs; /**< Objects successfully enqueued. */
115 uint64_t enq_quota_bulk; /**< Successful enqueues above watermark. */
116 uint64_t enq_quota_objs; /**< Objects enqueued above watermark. */
117 uint64_t enq_fail_bulk; /**< Failed enqueues number. */
118 uint64_t enq_fail_objs; /**< Objects that failed to be enqueued. */
119 uint64_t deq_success_bulk; /**< Successful dequeues number. */
120 uint64_t deq_success_objs; /**< Objects successfully dequeued. */
121 uint64_t deq_fail_bulk; /**< Failed dequeues number. */
122 uint64_t deq_fail_objs; /**< Objects that failed to be dequeued. */
123 } __rte_cache_aligned;
126 #define RTE_RING_NAMESIZE 32 /**< The maximum length of a ring name. */
127 #define RTE_RING_MZ_PREFIX "RG_"
130 * An RTE ring structure.
132 * The producer and the consumer have a head and a tail index. The particularity
133 * of these index is that they are not between 0 and size(ring). These indexes
134 * are between 0 and 2^32, and we mask their value when we access the ring[]
135 * field. Thanks to this assumption, we can do subtractions between 2 index
136 * values in a modulo-32bit base: that's why the overflow of the indexes is not
140 TAILQ_ENTRY(rte_ring) next; /**< Next in list. */
142 char name[RTE_RING_NAMESIZE]; /**< Name of the ring. */
143 int flags; /**< Flags supplied at creation. */
145 /** Ring producer status. */
147 uint32_t watermark; /**< Maximum items before EDQUOT. */
148 uint32_t sp_enqueue; /**< True, if single producer. */
149 uint32_t size; /**< Size of ring. */
150 uint32_t mask; /**< Mask (size-1) of ring. */
151 volatile uint32_t head; /**< Producer head. */
152 volatile uint32_t tail; /**< Producer tail. */
153 } prod __rte_cache_aligned;
155 /** Ring consumer status. */
157 uint32_t sc_dequeue; /**< True, if single consumer. */
158 uint32_t size; /**< Size of the ring. */
159 uint32_t mask; /**< Mask (size-1) of ring. */
160 volatile uint32_t head; /**< Consumer head. */
161 volatile uint32_t tail; /**< Consumer tail. */
162 #ifdef RTE_RING_SPLIT_PROD_CONS
163 } cons __rte_cache_aligned;
168 #ifdef RTE_LIBRTE_RING_DEBUG
169 struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
172 void * ring[0] __rte_cache_aligned; /**< Memory space of ring starts here.
173 * not volatile so need to be careful
174 * about compiler re-ordering */
177 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
178 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
179 #define RTE_RING_QUOT_EXCEED (1 << 31) /**< Quota exceed for burst ops */
180 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
183 * @internal When debug is enabled, store ring statistics.
185 * A pointer to the ring.
187 * The name of the statistics field to increment in the ring.
189 * The number to add to the object-oriented statistics.
191 #ifdef RTE_LIBRTE_RING_DEBUG
192 #define __RING_STAT_ADD(r, name, n) do { \
193 unsigned __lcore_id = rte_lcore_id(); \
194 r->stats[__lcore_id].name##_objs += n; \
195 r->stats[__lcore_id].name##_bulk += 1; \
198 #define __RING_STAT_ADD(r, name, n) do {} while(0)
202 * Calculate the memory size needed for a ring
204 * This function returns the number of bytes needed for a ring, given
205 * the number of elements in it. This value is the sum of the size of
206 * the structure rte_ring and the size of the memory needed by the
207 * objects pointers. The value is aligned to a cache line size.
210 * The number of elements in the ring (must be a power of 2).
212 * - The memory size needed for the ring on success.
213 * - -EINVAL if count is not a power of 2.
215 ssize_t rte_ring_get_memsize(unsigned count);
218 * Create a new ring named *name* in memory.
220 * This function uses ``memzone_reserve()`` to allocate memory. Its size is
221 * set to *count*, which must be a power of two. Water marking is
222 * disabled by default.
223 * Note that the real usable ring size is *count-1* instead of
227 * The name of the ring.
229 * The size of the ring (must be a power of 2).
231 * The *socket_id* argument is the socket identifier in case of
232 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
233 * constraint for the reserved zone.
235 * An OR of the following:
236 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
237 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
238 * is "single-producer". Otherwise, it is "multi-producers".
239 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
240 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
241 * is "single-consumer". Otherwise, it is "multi-consumers".
243 * On success, the pointer to the new allocated ring. NULL on error with
244 * rte_errno set appropriately. Possible errno values include:
245 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
246 * - E_RTE_SECONDARY - function was called from a secondary process instance
247 * - E_RTE_NO_TAILQ - no tailq list could be got for the ring list
248 * - EINVAL - count provided is not a power of 2
249 * - ENOSPC - the maximum number of memzones has already been allocated
250 * - EEXIST - a memzone with the same name already exists
251 * - ENOMEM - no appropriate memory area found in which to create memzone
253 struct rte_ring *rte_ring_create(const char *name, unsigned count,
254 int socket_id, unsigned flags);
257 * Change the high water mark.
259 * If *count* is 0, water marking is disabled. Otherwise, it is set to the
260 * *count* value. The *count* value must be greater than 0 and less
261 * than the ring size.
263 * This function can be called at any time (not necessarily at
267 * A pointer to the ring structure.
269 * The new water mark value.
271 * - 0: Success; water mark changed.
272 * - -EINVAL: Invalid water mark value.
274 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
277 * Dump the status of the ring to the console.
280 * A pointer to the ring structure.
282 void rte_ring_dump(const struct rte_ring *r);
284 /* the actual enqueue of pointers on the ring.
285 * Placed here since identical code needed in both
286 * single and multi producer enqueue functions */
287 #define ENQUEUE_PTRS() do { \
288 const uint32_t size = r->prod.size; \
289 uint32_t idx = prod_head & mask; \
290 if (likely(idx + n < size)) { \
291 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
292 r->ring[idx] = obj_table[i]; \
293 r->ring[idx+1] = obj_table[i+1]; \
294 r->ring[idx+2] = obj_table[i+2]; \
295 r->ring[idx+3] = obj_table[i+3]; \
298 case 3: r->ring[idx++] = obj_table[i++]; \
299 case 2: r->ring[idx++] = obj_table[i++]; \
300 case 1: r->ring[idx++] = obj_table[i++]; \
303 for (i = 0; idx < size; i++, idx++)\
304 r->ring[idx] = obj_table[i]; \
305 for (idx = 0; i < n; i++, idx++) \
306 r->ring[idx] = obj_table[i]; \
310 /* the actual copy of pointers on the ring to obj_table.
311 * Placed here since identical code needed in both
312 * single and multi consumer dequeue functions */
313 #define DEQUEUE_PTRS() do { \
314 uint32_t idx = cons_head & mask; \
315 const uint32_t size = r->cons.size; \
316 if (likely(idx + n < size)) { \
317 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
318 obj_table[i] = r->ring[idx]; \
319 obj_table[i+1] = r->ring[idx+1]; \
320 obj_table[i+2] = r->ring[idx+2]; \
321 obj_table[i+3] = r->ring[idx+3]; \
324 case 3: obj_table[i++] = r->ring[idx++]; \
325 case 2: obj_table[i++] = r->ring[idx++]; \
326 case 1: obj_table[i++] = r->ring[idx++]; \
329 for (i = 0; idx < size; i++, idx++) \
330 obj_table[i] = r->ring[idx]; \
331 for (idx = 0; i < n; i++, idx++) \
332 obj_table[i] = r->ring[idx]; \
337 * @internal Enqueue several objects on the ring (multi-producers safe).
339 * This function uses a "compare and set" instruction to move the
340 * producer index atomically.
343 * A pointer to the ring structure.
345 * A pointer to a table of void * pointers (objects).
347 * The number of objects to add in the ring from the obj_table.
349 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
350 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
352 * Depend on the behavior value
353 * if behavior = RTE_RING_QUEUE_FIXED
354 * - 0: Success; objects enqueue.
355 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
356 * high water mark is exceeded.
357 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
358 * if behavior = RTE_RING_QUEUE_VARIABLE
359 * - n: Actual number of objects enqueued.
361 static inline int __attribute__((always_inline))
362 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
363 unsigned n, enum rte_ring_queue_behavior behavior)
365 uint32_t prod_head, prod_next;
366 uint32_t cons_tail, free_entries;
367 const unsigned max = n;
370 uint32_t mask = r->prod.mask;
373 /* move prod.head atomically */
375 /* Reset n to the initial burst count */
378 prod_head = r->prod.head;
379 cons_tail = r->cons.tail;
380 /* The subtraction is done between two unsigned 32bits value
381 * (the result is always modulo 32 bits even if we have
382 * prod_head > cons_tail). So 'free_entries' is always between 0
383 * and size(ring)-1. */
384 free_entries = (mask + cons_tail - prod_head);
386 /* check that we have enough room in ring */
387 if (unlikely(n > free_entries)) {
388 if (behavior == RTE_RING_QUEUE_FIXED) {
389 __RING_STAT_ADD(r, enq_fail, n);
393 /* No free entry available */
394 if (unlikely(free_entries == 0)) {
395 __RING_STAT_ADD(r, enq_fail, n);
403 prod_next = prod_head + n;
404 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
406 } while (unlikely(success == 0));
408 /* write entries in ring */
410 rte_compiler_barrier();
412 /* if we exceed the watermark */
413 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
414 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
415 (int)(n | RTE_RING_QUOT_EXCEED);
416 __RING_STAT_ADD(r, enq_quota, n);
419 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
420 __RING_STAT_ADD(r, enq_success, n);
424 * If there are other enqueues in progress that preceeded us,
425 * we need to wait for them to complete
427 while (unlikely(r->prod.tail != prod_head))
430 r->prod.tail = prod_next;
435 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
438 * A pointer to the ring structure.
440 * A pointer to a table of void * pointers (objects).
442 * The number of objects to add in the ring from the obj_table.
444 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
445 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
447 * Depend on the behavior value
448 * if behavior = RTE_RING_QUEUE_FIXED
449 * - 0: Success; objects enqueue.
450 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
451 * high water mark is exceeded.
452 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
453 * if behavior = RTE_RING_QUEUE_VARIABLE
454 * - n: Actual number of objects enqueued.
456 static inline int __attribute__((always_inline))
457 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
458 unsigned n, enum rte_ring_queue_behavior behavior)
460 uint32_t prod_head, cons_tail;
461 uint32_t prod_next, free_entries;
463 uint32_t mask = r->prod.mask;
466 prod_head = r->prod.head;
467 cons_tail = r->cons.tail;
468 /* The subtraction is done between two unsigned 32bits value
469 * (the result is always modulo 32 bits even if we have
470 * prod_head > cons_tail). So 'free_entries' is always between 0
471 * and size(ring)-1. */
472 free_entries = mask + cons_tail - prod_head;
474 /* check that we have enough room in ring */
475 if (unlikely(n > free_entries)) {
476 if (behavior == RTE_RING_QUEUE_FIXED) {
477 __RING_STAT_ADD(r, enq_fail, n);
481 /* No free entry available */
482 if (unlikely(free_entries == 0)) {
483 __RING_STAT_ADD(r, enq_fail, n);
491 prod_next = prod_head + n;
492 r->prod.head = prod_next;
494 /* write entries in ring */
496 rte_compiler_barrier();
498 /* if we exceed the watermark */
499 if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
500 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
501 (int)(n | RTE_RING_QUOT_EXCEED);
502 __RING_STAT_ADD(r, enq_quota, n);
505 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
506 __RING_STAT_ADD(r, enq_success, n);
509 r->prod.tail = prod_next;
514 * @internal Dequeue several objects from a ring (multi-consumers safe). When
515 * the request objects are more than the available objects, only dequeue the
516 * actual number of objects
518 * This function uses a "compare and set" instruction to move the
519 * consumer index atomically.
522 * A pointer to the ring structure.
524 * A pointer to a table of void * pointers (objects) that will be filled.
526 * The number of objects to dequeue from the ring to the obj_table.
528 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
529 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
531 * Depend on the behavior value
532 * if behavior = RTE_RING_QUEUE_FIXED
533 * - 0: Success; objects dequeued.
534 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
536 * if behavior = RTE_RING_QUEUE_VARIABLE
537 * - n: Actual number of objects dequeued.
540 static inline int __attribute__((always_inline))
541 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
542 unsigned n, enum rte_ring_queue_behavior behavior)
544 uint32_t cons_head, prod_tail;
545 uint32_t cons_next, entries;
546 const unsigned max = n;
549 uint32_t mask = r->prod.mask;
551 /* move cons.head atomically */
553 /* Restore n as it may change every loop */
556 cons_head = r->cons.head;
557 prod_tail = r->prod.tail;
558 /* The subtraction is done between two unsigned 32bits value
559 * (the result is always modulo 32 bits even if we have
560 * cons_head > prod_tail). So 'entries' is always between 0
561 * and size(ring)-1. */
562 entries = (prod_tail - cons_head);
564 /* Set the actual entries for dequeue */
566 if (behavior == RTE_RING_QUEUE_FIXED) {
567 __RING_STAT_ADD(r, deq_fail, n);
571 if (unlikely(entries == 0)){
572 __RING_STAT_ADD(r, deq_fail, n);
580 cons_next = cons_head + n;
581 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
583 } while (unlikely(success == 0));
587 rte_compiler_barrier();
590 * If there are other dequeues in progress that preceded us,
591 * we need to wait for them to complete
593 while (unlikely(r->cons.tail != cons_head))
596 __RING_STAT_ADD(r, deq_success, n);
597 r->cons.tail = cons_next;
599 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
603 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
604 * When the request objects are more than the available objects, only dequeue
605 * the actual number of objects
608 * A pointer to the ring structure.
610 * A pointer to a table of void * pointers (objects) that will be filled.
612 * The number of objects to dequeue from the ring to the obj_table.
614 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
615 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
617 * Depend on the behavior value
618 * if behavior = RTE_RING_QUEUE_FIXED
619 * - 0: Success; objects dequeued.
620 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
622 * if behavior = RTE_RING_QUEUE_VARIABLE
623 * - n: Actual number of objects dequeued.
625 static inline int __attribute__((always_inline))
626 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
627 unsigned n, enum rte_ring_queue_behavior behavior)
629 uint32_t cons_head, prod_tail;
630 uint32_t cons_next, entries;
632 uint32_t mask = r->prod.mask;
634 cons_head = r->cons.head;
635 prod_tail = r->prod.tail;
636 /* The subtraction is done between two unsigned 32bits value
637 * (the result is always modulo 32 bits even if we have
638 * cons_head > prod_tail). So 'entries' is always between 0
639 * and size(ring)-1. */
640 entries = prod_tail - cons_head;
643 if (behavior == RTE_RING_QUEUE_FIXED) {
644 __RING_STAT_ADD(r, deq_fail, n);
648 if (unlikely(entries == 0)){
649 __RING_STAT_ADD(r, deq_fail, n);
657 cons_next = cons_head + n;
658 r->cons.head = cons_next;
662 rte_compiler_barrier();
664 __RING_STAT_ADD(r, deq_success, n);
665 r->cons.tail = cons_next;
666 return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
670 * Enqueue several objects on the ring (multi-producers safe).
672 * This function uses a "compare and set" instruction to move the
673 * producer index atomically.
676 * A pointer to the ring structure.
678 * A pointer to a table of void * pointers (objects).
680 * The number of objects to add in the ring from the obj_table.
682 * - 0: Success; objects enqueue.
683 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
684 * high water mark is exceeded.
685 * - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
687 static inline int __attribute__((always_inline))
688 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
691 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
695 * Enqueue several objects on a ring (NOT multi-producers safe).
698 * A pointer to the ring structure.
700 * A pointer to a table of void * pointers (objects).
702 * The number of objects to add in the ring from the obj_table.
704 * - 0: Success; objects enqueued.
705 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
706 * high water mark is exceeded.
707 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
709 static inline int __attribute__((always_inline))
710 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
713 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
717 * Enqueue several objects on a ring.
719 * This function calls the multi-producer or the single-producer
720 * version depending on the default behavior that was specified at
721 * ring creation time (see flags).
724 * A pointer to the ring structure.
726 * A pointer to a table of void * pointers (objects).
728 * The number of objects to add in the ring from the obj_table.
730 * - 0: Success; objects enqueued.
731 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
732 * high water mark is exceeded.
733 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
735 static inline int __attribute__((always_inline))
736 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
739 if (r->prod.sp_enqueue)
740 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
742 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
746 * Enqueue one object on a ring (multi-producers safe).
748 * This function uses a "compare and set" instruction to move the
749 * producer index atomically.
752 * A pointer to the ring structure.
754 * A pointer to the object to be added.
756 * - 0: Success; objects enqueued.
757 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
758 * high water mark is exceeded.
759 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
761 static inline int __attribute__((always_inline))
762 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
764 return rte_ring_mp_enqueue_bulk(r, &obj, 1);
768 * Enqueue one object on a ring (NOT multi-producers safe).
771 * A pointer to the ring structure.
773 * A pointer to the object to be added.
775 * - 0: Success; objects enqueued.
776 * - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
777 * high water mark is exceeded.
778 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
780 static inline int __attribute__((always_inline))
781 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
783 return rte_ring_sp_enqueue_bulk(r, &obj, 1);
787 * Enqueue one object on a ring.
789 * This function calls the multi-producer or the single-producer
790 * version, depending on the default behaviour that was specified at
791 * ring creation time (see flags).
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_enqueue(struct rte_ring *r, void *obj)
806 if (r->prod.sp_enqueue)
807 return rte_ring_sp_enqueue(r, obj);
809 return rte_ring_mp_enqueue(r, obj);
813 * Dequeue several objects from a ring (multi-consumers safe).
815 * This function uses a "compare and set" instruction to move the
816 * consumer index atomically.
819 * A pointer to the ring structure.
821 * A pointer to a table of void * pointers (objects) that will be filled.
823 * The number of objects to dequeue from the ring to the obj_table.
825 * - 0: Success; objects dequeued.
826 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
829 static inline int __attribute__((always_inline))
830 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
832 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
836 * Dequeue several objects from a ring (NOT multi-consumers safe).
839 * A pointer to the ring structure.
841 * A pointer to a table of void * pointers (objects) that will be filled.
843 * The number of objects to dequeue from the ring to the obj_table,
844 * must be strictly positive.
846 * - 0: Success; objects dequeued.
847 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
850 static inline int __attribute__((always_inline))
851 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
853 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
857 * Dequeue several objects from a ring.
859 * This function calls the multi-consumers or the single-consumer
860 * version, depending on the default behaviour that was specified at
861 * ring creation time (see flags).
864 * A pointer to the ring structure.
866 * A pointer to a table of void * pointers (objects) that will be filled.
868 * The number of objects to dequeue from the ring to the obj_table.
870 * - 0: Success; objects dequeued.
871 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
874 static inline int __attribute__((always_inline))
875 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
877 if (r->cons.sc_dequeue)
878 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
880 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
884 * Dequeue one object from a ring (multi-consumers safe).
886 * This function uses a "compare and set" instruction to move the
887 * consumer index atomically.
890 * A pointer to the ring structure.
892 * A pointer to a void * pointer (object) that will be filled.
894 * - 0: Success; objects dequeued.
895 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
898 static inline int __attribute__((always_inline))
899 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
901 return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
905 * Dequeue one object from a ring (NOT multi-consumers safe).
908 * A pointer to the ring structure.
910 * A pointer to a void * pointer (object) that will be filled.
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_sc_dequeue(struct rte_ring *r, void **obj_p)
919 return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
923 * Dequeue one object from a ring.
925 * This function calls the multi-consumers or the single-consumer
926 * version depending on the default behaviour that was specified at
927 * ring creation time (see flags).
930 * A pointer to the ring structure.
932 * A pointer to a void * pointer (object) that will be filled.
934 * - 0: Success, objects dequeued.
935 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
938 static inline int __attribute__((always_inline))
939 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
941 if (r->cons.sc_dequeue)
942 return rte_ring_sc_dequeue(r, obj_p);
944 return rte_ring_mc_dequeue(r, obj_p);
948 * Test if a ring is full.
951 * A pointer to the ring structure.
953 * - 1: The ring is full.
954 * - 0: The ring is not full.
957 rte_ring_full(const struct rte_ring *r)
959 uint32_t prod_tail = r->prod.tail;
960 uint32_t cons_tail = r->cons.tail;
961 return (((cons_tail - prod_tail - 1) & r->prod.mask) == 0);
965 * Test if a ring is empty.
968 * A pointer to the ring structure.
970 * - 1: The ring is empty.
971 * - 0: The ring is not empty.
974 rte_ring_empty(const struct rte_ring *r)
976 uint32_t prod_tail = r->prod.tail;
977 uint32_t cons_tail = r->cons.tail;
978 return !!(cons_tail == prod_tail);
982 * Return the number of entries in a ring.
985 * A pointer to the ring structure.
987 * The number of entries in the ring.
989 static inline unsigned
990 rte_ring_count(const struct rte_ring *r)
992 uint32_t prod_tail = r->prod.tail;
993 uint32_t cons_tail = r->cons.tail;
994 return ((prod_tail - cons_tail) & r->prod.mask);
998 * Return the number of free entries in a ring.
1001 * A pointer to the ring structure.
1003 * The number of free entries in the ring.
1005 static inline unsigned
1006 rte_ring_free_count(const struct rte_ring *r)
1008 uint32_t prod_tail = r->prod.tail;
1009 uint32_t cons_tail = r->cons.tail;
1010 return ((cons_tail - prod_tail - 1) & r->prod.mask);
1014 * Dump the status of all rings on the console
1016 void rte_ring_list_dump(void);
1019 * Search a ring from its name
1022 * The name of the ring.
1024 * The pointer to the ring matching the name, or NULL if not found,
1025 * with rte_errno set appropriately. Possible rte_errno values include:
1026 * - ENOENT - required entry not available to return.
1028 struct rte_ring *rte_ring_lookup(const char *name);
1031 * Enqueue several objects on the ring (multi-producers safe).
1033 * This function uses a "compare and set" instruction to move the
1034 * producer index atomically.
1037 * A pointer to the ring structure.
1039 * A pointer to a table of void * pointers (objects).
1041 * The number of objects to add in the ring from the obj_table.
1043 * - n: Actual number of objects enqueued.
1045 static inline int __attribute__((always_inline))
1046 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1049 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1053 * Enqueue several objects on a ring (NOT multi-producers safe).
1056 * A pointer to the ring structure.
1058 * A pointer to a table of void * pointers (objects).
1060 * The number of objects to add in the ring from the obj_table.
1062 * - n: Actual number of objects enqueued.
1064 static inline int __attribute__((always_inline))
1065 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1068 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1072 * Enqueue several objects on a ring.
1074 * This function calls the multi-producer or the single-producer
1075 * version depending on the default behavior that was specified at
1076 * ring creation time (see flags).
1079 * A pointer to the ring structure.
1081 * A pointer to a table of void * pointers (objects).
1083 * The number of objects to add in the ring from the obj_table.
1085 * - n: Actual number of objects enqueued.
1087 static inline int __attribute__((always_inline))
1088 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1091 if (r->prod.sp_enqueue)
1092 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1094 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1098 * Dequeue several objects from a ring (multi-consumers safe). When the request
1099 * objects are more than the available objects, only dequeue the actual number
1102 * This function uses a "compare and set" instruction to move the
1103 * consumer index atomically.
1106 * A pointer to the ring structure.
1108 * A pointer to a table of void * pointers (objects) that will be filled.
1110 * The number of objects to dequeue from the ring to the obj_table.
1112 * - n: Actual number of objects dequeued, 0 if ring is empty
1114 static inline int __attribute__((always_inline))
1115 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1117 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1121 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1122 * request objects are more than the available objects, only dequeue the
1123 * actual number of objects
1126 * A pointer to the ring structure.
1128 * A pointer to a table of void * pointers (objects) that will be filled.
1130 * The number of objects to dequeue from the ring to the obj_table.
1132 * - n: Actual number of objects dequeued, 0 if ring is empty
1134 static inline int __attribute__((always_inline))
1135 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1137 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1141 * Dequeue multiple objects from a ring up to a maximum number.
1143 * This function calls the multi-consumers or the single-consumer
1144 * version, depending on the default behaviour that was specified at
1145 * ring creation time (see flags).
1148 * A pointer to the ring structure.
1150 * A pointer to a table of void * pointers (objects) that will be filled.
1152 * The number of objects to dequeue from the ring to the obj_table.
1154 * - Number of objects dequeued, or a negative error code on error
1156 static inline int __attribute__((always_inline))
1157 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1159 if (r->cons.sc_dequeue)
1160 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1162 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1169 #endif /* _RTE_RING_H_ */