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 #define RTE_RING_MZ_PREFIX "RG_"
113 /**< The maximum length of a ring name. */
114 #define RTE_RING_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
115 sizeof(RTE_RING_MZ_PREFIX) + 1)
117 struct rte_memzone; /* forward declaration, so as not to require memzone.h */
119 #if RTE_CACHE_LINE_SIZE < 128
120 #define PROD_ALIGN (RTE_CACHE_LINE_SIZE * 2)
121 #define CONS_ALIGN (RTE_CACHE_LINE_SIZE * 2)
123 #define PROD_ALIGN RTE_CACHE_LINE_SIZE
124 #define CONS_ALIGN RTE_CACHE_LINE_SIZE
127 /* structure to hold a pair of head/tail values and other metadata */
128 struct rte_ring_headtail {
129 volatile uint32_t head; /**< Prod/consumer head. */
130 volatile uint32_t tail; /**< Prod/consumer tail. */
131 uint32_t single; /**< True if single prod/cons */
135 * An RTE ring structure.
137 * The producer and the consumer have a head and a tail index. The particularity
138 * of these index is that they are not between 0 and size(ring). These indexes
139 * are between 0 and 2^32, and we mask their value when we access the ring[]
140 * field. Thanks to this assumption, we can do subtractions between 2 index
141 * values in a modulo-32bit base: that's why the overflow of the indexes is not
146 * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
147 * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
148 * next time the ABI changes
150 char name[RTE_MEMZONE_NAMESIZE]; /**< Name of the ring. */
151 int flags; /**< Flags supplied at creation. */
152 const struct rte_memzone *memzone;
153 /**< Memzone, if any, containing the rte_ring */
154 uint32_t size; /**< Size of ring. */
155 uint32_t mask; /**< Mask (size-1) of ring. */
157 /** Ring producer status. */
158 struct rte_ring_headtail prod __rte_aligned(PROD_ALIGN);
160 /** Ring consumer status. */
161 struct rte_ring_headtail cons __rte_aligned(CONS_ALIGN);
163 void *ring[] __rte_cache_aligned; /**< Memory space of ring starts here.
164 * not volatile so need to be careful
165 * about compiler re-ordering */
168 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
169 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
170 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
172 /* @internal defines for passing to the enqueue dequeue worker functions */
179 * Calculate the memory size needed for a ring
181 * This function returns the number of bytes needed for a ring, given
182 * the number of elements in it. This value is the sum of the size of
183 * the structure rte_ring and the size of the memory needed by the
184 * objects pointers. The value is aligned to a cache line size.
187 * The number of elements in the ring (must be a power of 2).
189 * - The memory size needed for the ring on success.
190 * - -EINVAL if count is not a power of 2.
192 ssize_t rte_ring_get_memsize(unsigned count);
195 * Initialize a ring structure.
197 * Initialize a ring structure in memory pointed by "r". The size of the
198 * memory area must be large enough to store the ring structure and the
199 * object table. It is advised to use rte_ring_get_memsize() to get the
202 * The ring size is set to *count*, which must be a power of two. Water
203 * marking is disabled by default. The real usable ring size is
204 * *count-1* instead of *count* to differentiate a free ring from an
207 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
208 * memory given by the caller may not be shareable among dpdk
212 * The pointer to the ring structure followed by the objects table.
214 * The name of the ring.
216 * The number of elements in the ring (must be a power of 2).
218 * An OR of the following:
219 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
220 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
221 * is "single-producer". Otherwise, it is "multi-producers".
222 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
223 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
224 * is "single-consumer". Otherwise, it is "multi-consumers".
226 * 0 on success, or a negative value on error.
228 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
232 * Create a new ring named *name* in memory.
234 * This function uses ``memzone_reserve()`` to allocate memory. Then it
235 * calls rte_ring_init() to initialize an empty ring.
237 * The new ring size is set to *count*, which must be a power of
238 * two. Water marking is disabled by default. The real usable ring size
239 * is *count-1* instead of *count* to differentiate a free ring from an
242 * The ring is added in RTE_TAILQ_RING list.
245 * The name of the ring.
247 * The size of the ring (must be a power of 2).
249 * The *socket_id* argument is the socket identifier in case of
250 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
251 * constraint for the reserved zone.
253 * An OR of the following:
254 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
255 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
256 * is "single-producer". Otherwise, it is "multi-producers".
257 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
258 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
259 * is "single-consumer". Otherwise, it is "multi-consumers".
261 * On success, the pointer to the new allocated ring. NULL on error with
262 * rte_errno set appropriately. Possible errno values include:
263 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
264 * - E_RTE_SECONDARY - function was called from a secondary process instance
265 * - EINVAL - count provided is not a power of 2
266 * - ENOSPC - the maximum number of memzones has already been allocated
267 * - EEXIST - a memzone with the same name already exists
268 * - ENOMEM - no appropriate memory area found in which to create memzone
270 struct rte_ring *rte_ring_create(const char *name, unsigned count,
271 int socket_id, unsigned flags);
273 * De-allocate all memory used by the ring.
278 void rte_ring_free(struct rte_ring *r);
281 * Dump the status of the ring to a file.
284 * A pointer to a file for output
286 * A pointer to the ring structure.
288 void rte_ring_dump(FILE *f, const struct rte_ring *r);
290 /* the actual enqueue of pointers on the ring.
291 * Placed here since identical code needed in both
292 * single and multi producer enqueue functions */
293 #define ENQUEUE_PTRS() do { \
295 const uint32_t size = r->size; \
296 uint32_t idx = prod_head & r->mask; \
297 if (likely(idx + n < size)) { \
298 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
299 r->ring[idx] = obj_table[i]; \
300 r->ring[idx+1] = obj_table[i+1]; \
301 r->ring[idx+2] = obj_table[i+2]; \
302 r->ring[idx+3] = obj_table[i+3]; \
305 case 3: r->ring[idx++] = obj_table[i++]; \
306 case 2: r->ring[idx++] = obj_table[i++]; \
307 case 1: r->ring[idx++] = obj_table[i++]; \
310 for (i = 0; idx < size; i++, idx++)\
311 r->ring[idx] = obj_table[i]; \
312 for (idx = 0; i < n; i++, idx++) \
313 r->ring[idx] = obj_table[i]; \
317 /* the actual copy of pointers on the ring to obj_table.
318 * Placed here since identical code needed in both
319 * single and multi consumer dequeue functions */
320 #define DEQUEUE_PTRS() do { \
322 uint32_t idx = cons_head & r->mask; \
323 const uint32_t size = r->size; \
324 if (likely(idx + n < size)) { \
325 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
326 obj_table[i] = r->ring[idx]; \
327 obj_table[i+1] = r->ring[idx+1]; \
328 obj_table[i+2] = r->ring[idx+2]; \
329 obj_table[i+3] = r->ring[idx+3]; \
332 case 3: obj_table[i++] = r->ring[idx++]; \
333 case 2: obj_table[i++] = r->ring[idx++]; \
334 case 1: obj_table[i++] = r->ring[idx++]; \
337 for (i = 0; idx < size; i++, idx++) \
338 obj_table[i] = r->ring[idx]; \
339 for (idx = 0; i < n; i++, idx++) \
340 obj_table[i] = r->ring[idx]; \
344 static inline __attribute__((always_inline)) void
345 update_tail(struct rte_ring_headtail *ht, uint32_t old_val, uint32_t new_val,
349 * If there are other enqueues/dequeues in progress that preceded us,
350 * we need to wait for them to complete
353 while (unlikely(ht->tail != old_val))
360 * @internal This function updates the producer head for enqueue
363 * A pointer to the ring structure
365 * Indicates whether multi-producer path is needed or not
367 * The number of elements we will want to enqueue, i.e. how far should the
370 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
371 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
373 * Returns head value as it was before the move, i.e. where enqueue starts
375 * Returns the current/new head value i.e. where enqueue finishes
376 * @param free_entries
377 * Returns the amount of free space in the ring BEFORE head was moved
379 * Actual number of objects enqueued.
380 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
382 static inline __attribute__((always_inline)) unsigned int
383 __rte_ring_move_prod_head(struct rte_ring *r, int is_sp,
384 unsigned int n, enum rte_ring_queue_behavior behavior,
385 uint32_t *old_head, uint32_t *new_head,
386 uint32_t *free_entries)
388 const uint32_t mask = r->mask;
389 unsigned int max = n;
393 /* Reset n to the initial burst count */
396 *old_head = r->prod.head;
397 const uint32_t cons_tail = r->cons.tail;
398 /* The subtraction is done between two unsigned 32bits value
399 * (the result is always modulo 32 bits even if we have
400 * *old_head > cons_tail). So 'free_entries' is always between 0
401 * and size(ring)-1. */
402 *free_entries = (mask + cons_tail - *old_head);
404 /* check that we have enough room in ring */
405 if (unlikely(n > *free_entries))
406 n = (behavior == RTE_RING_QUEUE_FIXED) ?
412 *new_head = *old_head + n;
414 r->prod.head = *new_head, success = 1;
416 success = rte_atomic32_cmpset(&r->prod.head,
417 *old_head, *new_head);
418 } while (unlikely(success == 0));
423 * @internal Enqueue several objects on the ring
426 * A pointer to the ring structure.
428 * A pointer to a table of void * pointers (objects).
430 * The number of objects to add in the ring from the obj_table.
432 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
433 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
435 * Indicates whether to use single producer or multi-producer head update
437 * returns the amount of space after the enqueue operation has finished
439 * Actual number of objects enqueued.
440 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
442 static inline __attribute__((always_inline)) unsigned int
443 __rte_ring_do_enqueue(struct rte_ring *r, void * const *obj_table,
444 unsigned int n, enum rte_ring_queue_behavior behavior,
445 int is_sp, unsigned int *free_space)
447 uint32_t prod_head, prod_next;
448 uint32_t free_entries;
450 n = __rte_ring_move_prod_head(r, is_sp, n, behavior,
451 &prod_head, &prod_next, &free_entries);
458 update_tail(&r->prod, prod_head, prod_next, is_sp);
460 if (free_space != NULL)
461 *free_space = free_entries - n;
466 * @internal This function updates the consumer head for dequeue
469 * A pointer to the ring structure
471 * Indicates whether multi-consumer path is needed or not
473 * The number of elements we will want to enqueue, i.e. how far should the
476 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
477 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
479 * Returns head value as it was before the move, i.e. where dequeue starts
481 * Returns the current/new head value i.e. where dequeue finishes
483 * Returns the number of entries in the ring BEFORE head was moved
485 * - Actual number of objects dequeued.
486 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
488 static inline __attribute__((always_inline)) unsigned int
489 __rte_ring_move_cons_head(struct rte_ring *r, int is_sc,
490 unsigned int n, enum rte_ring_queue_behavior behavior,
491 uint32_t *old_head, uint32_t *new_head,
494 unsigned int max = n;
497 /* move cons.head atomically */
499 /* Restore n as it may change every loop */
502 *old_head = r->cons.head;
503 const uint32_t prod_tail = r->prod.tail;
504 /* The subtraction is done between two unsigned 32bits value
505 * (the result is always modulo 32 bits even if we have
506 * cons_head > prod_tail). So 'entries' is always between 0
507 * and size(ring)-1. */
508 *entries = (prod_tail - *old_head);
510 /* Set the actual entries for dequeue */
512 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
514 if (unlikely(n == 0))
517 *new_head = *old_head + n;
519 r->cons.head = *new_head, success = 1;
521 success = rte_atomic32_cmpset(&r->cons.head, *old_head,
523 } while (unlikely(success == 0));
528 * @internal Dequeue several objects from the ring
531 * A pointer to the ring structure.
533 * A pointer to a table of void * pointers (objects).
535 * The number of objects to pull from the ring.
537 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
538 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
540 * Indicates whether to use single consumer or multi-consumer head update
542 * returns the number of remaining ring entries after the dequeue has finished
544 * - Actual number of objects dequeued.
545 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
547 static inline __attribute__((always_inline)) unsigned int
548 __rte_ring_do_dequeue(struct rte_ring *r, void **obj_table,
549 unsigned int n, enum rte_ring_queue_behavior behavior,
550 int is_sc, unsigned int *available)
552 uint32_t cons_head, cons_next;
555 n = __rte_ring_move_cons_head(r, is_sc, n, behavior,
556 &cons_head, &cons_next, &entries);
563 update_tail(&r->cons, cons_head, cons_next, is_sc);
566 if (available != NULL)
567 *available = entries - n;
572 * Enqueue several objects on the ring (multi-producers safe).
574 * This function uses a "compare and set" instruction to move the
575 * producer index atomically.
578 * A pointer to the ring structure.
580 * A pointer to a table of void * pointers (objects).
582 * The number of objects to add in the ring from the obj_table.
584 * if non-NULL, returns the amount of space in the ring after the
585 * enqueue operation has finished.
587 * The number of objects enqueued, either 0 or n
589 static inline unsigned int __attribute__((always_inline))
590 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
591 unsigned int n, unsigned int *free_space)
593 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
594 __IS_MP, free_space);
598 * Enqueue several objects on a ring (NOT multi-producers safe).
601 * A pointer to the ring structure.
603 * A pointer to a table of void * pointers (objects).
605 * The number of objects to add in the ring from the obj_table.
607 * if non-NULL, returns the amount of space in the ring after the
608 * enqueue operation has finished.
610 * The number of objects enqueued, either 0 or n
612 static inline unsigned int __attribute__((always_inline))
613 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
614 unsigned int n, unsigned int *free_space)
616 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
617 __IS_SP, free_space);
621 * Enqueue several objects on a ring.
623 * This function calls the multi-producer or the single-producer
624 * version depending on the default behavior that was specified at
625 * ring creation time (see flags).
628 * A pointer to the ring structure.
630 * A pointer to a table of void * pointers (objects).
632 * The number of objects to add in the ring from the obj_table.
634 * if non-NULL, returns the amount of space in the ring after the
635 * enqueue operation has finished.
637 * The number of objects enqueued, either 0 or n
639 static inline unsigned int __attribute__((always_inline))
640 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
641 unsigned int n, unsigned int *free_space)
643 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
644 r->prod.single, free_space);
648 * Enqueue one object on a ring (multi-producers safe).
650 * This function uses a "compare and set" instruction to move the
651 * producer index atomically.
654 * A pointer to the ring structure.
656 * A pointer to the object to be added.
658 * - 0: Success; objects enqueued.
659 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
661 static inline int __attribute__((always_inline))
662 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
664 return rte_ring_mp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
668 * Enqueue one object on a ring (NOT multi-producers safe).
671 * A pointer to the ring structure.
673 * A pointer to the object to be added.
675 * - 0: Success; objects enqueued.
676 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
678 static inline int __attribute__((always_inline))
679 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
681 return rte_ring_sp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
685 * Enqueue one object on a ring.
687 * This function calls the multi-producer or the single-producer
688 * version, depending on the default behaviour that was specified at
689 * ring creation time (see flags).
692 * A pointer to the ring structure.
694 * A pointer to the object to be added.
696 * - 0: Success; objects enqueued.
697 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
699 static inline int __attribute__((always_inline))
700 rte_ring_enqueue(struct rte_ring *r, void *obj)
702 return rte_ring_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
706 * Dequeue several objects from a ring (multi-consumers safe).
708 * This function uses a "compare and set" instruction to move the
709 * consumer index atomically.
712 * A pointer to the ring structure.
714 * A pointer to a table of void * pointers (objects) that will be filled.
716 * The number of objects to dequeue from the ring to the obj_table.
718 * If non-NULL, returns the number of remaining ring entries after the
719 * dequeue has finished.
721 * The number of objects dequeued, either 0 or n
723 static inline unsigned int __attribute__((always_inline))
724 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table,
725 unsigned int n, unsigned int *available)
727 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
732 * Dequeue several objects from a ring (NOT multi-consumers safe).
735 * A pointer to the ring structure.
737 * A pointer to a table of void * pointers (objects) that will be filled.
739 * The number of objects to dequeue from the ring to the obj_table,
740 * must be strictly positive.
742 * If non-NULL, returns the number of remaining ring entries after the
743 * dequeue has finished.
745 * The number of objects dequeued, either 0 or n
747 static inline unsigned int __attribute__((always_inline))
748 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
749 unsigned int n, unsigned int *available)
751 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
756 * Dequeue several objects from a ring.
758 * This function calls the multi-consumers or the single-consumer
759 * version, depending on the default behaviour that was specified at
760 * ring creation time (see flags).
763 * A pointer to the ring structure.
765 * A pointer to a table of void * pointers (objects) that will be filled.
767 * The number of objects to dequeue from the ring to the obj_table.
769 * If non-NULL, returns the number of remaining ring entries after the
770 * dequeue has finished.
772 * The number of objects dequeued, either 0 or n
774 static inline unsigned int __attribute__((always_inline))
775 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
776 unsigned int *available)
778 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
779 r->cons.single, available);
783 * Dequeue one object from a ring (multi-consumers safe).
785 * This function uses a "compare and set" instruction to move the
786 * consumer index atomically.
789 * A pointer to the ring structure.
791 * A pointer to a void * pointer (object) that will be filled.
793 * - 0: Success; objects dequeued.
794 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
797 static inline int __attribute__((always_inline))
798 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
800 return rte_ring_mc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
804 * Dequeue one object from a ring (NOT multi-consumers safe).
807 * A pointer to the ring structure.
809 * A pointer to a void * pointer (object) that will be filled.
811 * - 0: Success; objects dequeued.
812 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
815 static inline int __attribute__((always_inline))
816 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
818 return rte_ring_sc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
822 * Dequeue one object from a ring.
824 * This function calls the multi-consumers or the single-consumer
825 * version depending on the default behaviour that was specified at
826 * ring creation time (see flags).
829 * A pointer to the ring structure.
831 * A pointer to a void * pointer (object) that will be filled.
833 * - 0: Success, objects dequeued.
834 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
837 static inline int __attribute__((always_inline))
838 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
840 return rte_ring_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
844 * Test if a ring is full.
847 * A pointer to the ring structure.
849 * - 1: The ring is full.
850 * - 0: The ring is not full.
853 rte_ring_full(const struct rte_ring *r)
855 uint32_t prod_tail = r->prod.tail;
856 uint32_t cons_tail = r->cons.tail;
857 return ((cons_tail - prod_tail - 1) & r->mask) == 0;
861 * Test if a ring is empty.
864 * A pointer to the ring structure.
866 * - 1: The ring is empty.
867 * - 0: The ring is not empty.
870 rte_ring_empty(const struct rte_ring *r)
872 uint32_t prod_tail = r->prod.tail;
873 uint32_t cons_tail = r->cons.tail;
874 return !!(cons_tail == prod_tail);
878 * Return the number of entries in a ring.
881 * A pointer to the ring structure.
883 * The number of entries in the ring.
885 static inline unsigned
886 rte_ring_count(const struct rte_ring *r)
888 uint32_t prod_tail = r->prod.tail;
889 uint32_t cons_tail = r->cons.tail;
890 return (prod_tail - cons_tail) & r->mask;
894 * Return the number of free entries in a ring.
897 * A pointer to the ring structure.
899 * The number of free entries in the ring.
901 static inline unsigned
902 rte_ring_free_count(const struct rte_ring *r)
904 uint32_t prod_tail = r->prod.tail;
905 uint32_t cons_tail = r->cons.tail;
906 return (cons_tail - prod_tail - 1) & r->mask;
910 * Return the size of the ring.
913 * A pointer to the ring structure.
915 * The number of elements which can be stored in the ring.
917 static inline unsigned int
918 rte_ring_get_size(const struct rte_ring *r)
924 * Dump the status of all rings on the console
927 * A pointer to a file for output
929 void rte_ring_list_dump(FILE *f);
932 * Search a ring from its name
935 * The name of the ring.
937 * The pointer to the ring matching the name, or NULL if not found,
938 * with rte_errno set appropriately. Possible rte_errno values include:
939 * - ENOENT - required entry not available to return.
941 struct rte_ring *rte_ring_lookup(const char *name);
944 * Enqueue several objects on the ring (multi-producers safe).
946 * This function uses a "compare and set" instruction to move the
947 * producer index atomically.
950 * A pointer to the ring structure.
952 * A pointer to a table of void * pointers (objects).
954 * The number of objects to add in the ring from the obj_table.
956 * if non-NULL, returns the amount of space in the ring after the
957 * enqueue operation has finished.
959 * - n: Actual number of objects enqueued.
961 static inline unsigned __attribute__((always_inline))
962 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
963 unsigned int n, unsigned int *free_space)
965 return __rte_ring_do_enqueue(r, obj_table, n,
966 RTE_RING_QUEUE_VARIABLE, __IS_MP, free_space);
970 * Enqueue several objects on a ring (NOT multi-producers safe).
973 * A pointer to the ring structure.
975 * A pointer to a table of void * pointers (objects).
977 * The number of objects to add in the ring from the obj_table.
979 * if non-NULL, returns the amount of space in the ring after the
980 * enqueue operation has finished.
982 * - n: Actual number of objects enqueued.
984 static inline unsigned __attribute__((always_inline))
985 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
986 unsigned int n, unsigned int *free_space)
988 return __rte_ring_do_enqueue(r, obj_table, n,
989 RTE_RING_QUEUE_VARIABLE, __IS_SP, free_space);
993 * Enqueue several objects on a ring.
995 * This function calls the multi-producer or the single-producer
996 * version depending on the default behavior that was specified at
997 * ring creation time (see flags).
1000 * A pointer to the ring structure.
1002 * A pointer to a table of void * pointers (objects).
1004 * The number of objects to add in the ring from the obj_table.
1006 * if non-NULL, returns the amount of space in the ring after the
1007 * enqueue operation has finished.
1009 * - n: Actual number of objects enqueued.
1011 static inline unsigned __attribute__((always_inline))
1012 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1013 unsigned int n, unsigned int *free_space)
1015 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE,
1016 r->prod.single, free_space);
1020 * Dequeue several objects from a ring (multi-consumers safe). When the request
1021 * objects are more than the available objects, only dequeue the actual number
1024 * This function uses a "compare and set" instruction to move the
1025 * consumer index atomically.
1028 * A pointer to the ring structure.
1030 * A pointer to a table of void * pointers (objects) that will be filled.
1032 * The number of objects to dequeue from the ring to the obj_table.
1034 * If non-NULL, returns the number of remaining ring entries after the
1035 * dequeue has finished.
1037 * - n: Actual number of objects dequeued, 0 if ring is empty
1039 static inline unsigned __attribute__((always_inline))
1040 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
1041 unsigned int n, unsigned int *available)
1043 return __rte_ring_do_dequeue(r, obj_table, n,
1044 RTE_RING_QUEUE_VARIABLE, __IS_MC, available);
1048 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1049 * request objects are more than the available objects, only dequeue the
1050 * actual number of objects
1053 * A pointer to the ring structure.
1055 * A pointer to a table of void * pointers (objects) that will be filled.
1057 * The number of objects to dequeue from the ring to the obj_table.
1059 * If non-NULL, returns the number of remaining ring entries after the
1060 * dequeue has finished.
1062 * - n: Actual number of objects dequeued, 0 if ring is empty
1064 static inline unsigned __attribute__((always_inline))
1065 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
1066 unsigned int n, unsigned int *available)
1068 return __rte_ring_do_dequeue(r, obj_table, n,
1069 RTE_RING_QUEUE_VARIABLE, __IS_SC, available);
1073 * Dequeue multiple objects from a ring up to a maximum number.
1075 * This function calls the multi-consumers or the single-consumer
1076 * version, depending on the default behaviour that was specified at
1077 * ring creation time (see flags).
1080 * A pointer to the ring structure.
1082 * A pointer to a table of void * pointers (objects) that will be filled.
1084 * The number of objects to dequeue from the ring to the obj_table.
1086 * If non-NULL, returns the number of remaining ring entries after the
1087 * dequeue has finished.
1089 * - Number of objects dequeued
1091 static inline unsigned __attribute__((always_inline))
1092 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
1093 unsigned int n, unsigned int *available)
1095 return __rte_ring_do_dequeue(r, obj_table, n,
1096 RTE_RING_QUEUE_VARIABLE,
1097 r->cons.single, available);
1104 #endif /* _RTE_RING_H_ */