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);
162 } __rte_cache_aligned;
164 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
165 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
166 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
168 /* @internal defines for passing to the enqueue dequeue worker functions */
175 * Calculate the memory size needed for a ring
177 * This function returns the number of bytes needed for a ring, given
178 * the number of elements in it. This value is the sum of the size of
179 * the structure rte_ring and the size of the memory needed by the
180 * objects pointers. The value is aligned to a cache line size.
183 * The number of elements in the ring (must be a power of 2).
185 * - The memory size needed for the ring on success.
186 * - -EINVAL if count is not a power of 2.
188 ssize_t rte_ring_get_memsize(unsigned count);
191 * Initialize a ring structure.
193 * Initialize a ring structure in memory pointed by "r". The size of the
194 * memory area must be large enough to store the ring structure and the
195 * object table. It is advised to use rte_ring_get_memsize() to get the
198 * The ring size is set to *count*, which must be a power of two. Water
199 * marking is disabled by default. The real usable ring size is
200 * *count-1* instead of *count* to differentiate a free ring from an
203 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
204 * memory given by the caller may not be shareable among dpdk
208 * The pointer to the ring structure followed by the objects table.
210 * The name of the ring.
212 * The number of elements in the ring (must be a power of 2).
214 * An OR of the following:
215 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
216 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
217 * is "single-producer". Otherwise, it is "multi-producers".
218 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
219 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
220 * is "single-consumer". Otherwise, it is "multi-consumers".
222 * 0 on success, or a negative value on error.
224 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
228 * Create a new ring named *name* in memory.
230 * This function uses ``memzone_reserve()`` to allocate memory. Then it
231 * calls rte_ring_init() to initialize an empty ring.
233 * The new ring size is set to *count*, which must be a power of
234 * two. Water marking is disabled by default. The real usable ring size
235 * is *count-1* instead of *count* to differentiate a free ring from an
238 * The ring is added in RTE_TAILQ_RING list.
241 * The name of the ring.
243 * The size of the ring (must be a power of 2).
245 * The *socket_id* argument is the socket identifier in case of
246 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
247 * constraint for the reserved zone.
249 * An OR of the following:
250 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
251 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
252 * is "single-producer". Otherwise, it is "multi-producers".
253 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
254 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
255 * is "single-consumer". Otherwise, it is "multi-consumers".
257 * On success, the pointer to the new allocated ring. NULL on error with
258 * rte_errno set appropriately. Possible errno values include:
259 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
260 * - E_RTE_SECONDARY - function was called from a secondary process instance
261 * - EINVAL - count provided is not a power of 2
262 * - ENOSPC - the maximum number of memzones has already been allocated
263 * - EEXIST - a memzone with the same name already exists
264 * - ENOMEM - no appropriate memory area found in which to create memzone
266 struct rte_ring *rte_ring_create(const char *name, unsigned count,
267 int socket_id, unsigned flags);
269 * De-allocate all memory used by the ring.
274 void rte_ring_free(struct rte_ring *r);
277 * Dump the status of the ring to a file.
280 * A pointer to a file for output
282 * A pointer to the ring structure.
284 void rte_ring_dump(FILE *f, const struct rte_ring *r);
286 /* the actual enqueue of pointers on the ring.
287 * Placed here since identical code needed in both
288 * single and multi producer enqueue functions */
289 #define ENQUEUE_PTRS(r, ring_start, prod_head, obj_table, n, obj_type) do { \
291 const uint32_t size = (r)->size; \
292 uint32_t idx = prod_head & (r)->mask; \
293 obj_type *ring = (void *)ring_start; \
294 if (likely(idx + n < size)) { \
295 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
296 ring[idx] = obj_table[i]; \
297 ring[idx+1] = obj_table[i+1]; \
298 ring[idx+2] = obj_table[i+2]; \
299 ring[idx+3] = obj_table[i+3]; \
303 ring[idx++] = obj_table[i++]; /* fallthrough */ \
305 ring[idx++] = obj_table[i++]; /* fallthrough */ \
307 ring[idx++] = obj_table[i++]; \
310 for (i = 0; idx < size; i++, idx++)\
311 ring[idx] = obj_table[i]; \
312 for (idx = 0; i < n; i++, idx++) \
313 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(r, ring_start, cons_head, obj_table, n, obj_type) do { \
322 uint32_t idx = cons_head & (r)->mask; \
323 const uint32_t size = (r)->size; \
324 obj_type *ring = (void *)ring_start; \
325 if (likely(idx + n < size)) { \
326 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
327 obj_table[i] = ring[idx]; \
328 obj_table[i+1] = ring[idx+1]; \
329 obj_table[i+2] = ring[idx+2]; \
330 obj_table[i+3] = ring[idx+3]; \
334 obj_table[i++] = ring[idx++]; /* fallthrough */ \
336 obj_table[i++] = ring[idx++]; /* fallthrough */ \
338 obj_table[i++] = ring[idx++]; \
341 for (i = 0; idx < size; i++, idx++) \
342 obj_table[i] = ring[idx]; \
343 for (idx = 0; i < n; i++, idx++) \
344 obj_table[i] = ring[idx]; \
348 static inline __attribute__((always_inline)) void
349 update_tail(struct rte_ring_headtail *ht, uint32_t old_val, uint32_t new_val,
353 * If there are other enqueues/dequeues in progress that preceded us,
354 * we need to wait for them to complete
357 while (unlikely(ht->tail != old_val))
364 * @internal This function updates the producer head for enqueue
367 * A pointer to the ring structure
369 * Indicates whether multi-producer path is needed or not
371 * The number of elements we will want to enqueue, i.e. how far should the
374 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
375 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
377 * Returns head value as it was before the move, i.e. where enqueue starts
379 * Returns the current/new head value i.e. where enqueue finishes
380 * @param free_entries
381 * Returns the amount of free space in the ring BEFORE head was moved
383 * Actual number of objects enqueued.
384 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
386 static inline __attribute__((always_inline)) unsigned int
387 __rte_ring_move_prod_head(struct rte_ring *r, int is_sp,
388 unsigned int n, enum rte_ring_queue_behavior behavior,
389 uint32_t *old_head, uint32_t *new_head,
390 uint32_t *free_entries)
392 const uint32_t mask = r->mask;
393 unsigned int max = n;
397 /* Reset n to the initial burst count */
400 *old_head = r->prod.head;
401 const uint32_t cons_tail = r->cons.tail;
402 /* The subtraction is done between two unsigned 32bits value
403 * (the result is always modulo 32 bits even if we have
404 * *old_head > cons_tail). So 'free_entries' is always between 0
405 * and size(ring)-1. */
406 *free_entries = (mask + cons_tail - *old_head);
408 /* check that we have enough room in ring */
409 if (unlikely(n > *free_entries))
410 n = (behavior == RTE_RING_QUEUE_FIXED) ?
416 *new_head = *old_head + n;
418 r->prod.head = *new_head, success = 1;
420 success = rte_atomic32_cmpset(&r->prod.head,
421 *old_head, *new_head);
422 } while (unlikely(success == 0));
427 * @internal Enqueue several objects on the ring
430 * A pointer to the ring structure.
432 * A pointer to a table of void * pointers (objects).
434 * The number of objects to add in the ring from the obj_table.
436 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
437 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
439 * Indicates whether to use single producer or multi-producer head update
441 * returns the amount of space after the enqueue operation has finished
443 * Actual number of objects enqueued.
444 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
446 static inline __attribute__((always_inline)) unsigned int
447 __rte_ring_do_enqueue(struct rte_ring *r, void * const *obj_table,
448 unsigned int n, enum rte_ring_queue_behavior behavior,
449 int is_sp, unsigned int *free_space)
451 uint32_t prod_head, prod_next;
452 uint32_t free_entries;
454 n = __rte_ring_move_prod_head(r, is_sp, n, behavior,
455 &prod_head, &prod_next, &free_entries);
459 ENQUEUE_PTRS(r, &r[1], prod_head, obj_table, n, void *);
462 update_tail(&r->prod, prod_head, prod_next, is_sp);
464 if (free_space != NULL)
465 *free_space = free_entries - n;
470 * @internal This function updates the consumer head for dequeue
473 * A pointer to the ring structure
475 * Indicates whether multi-consumer path is needed or not
477 * The number of elements we will want to enqueue, i.e. how far should the
480 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
481 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
483 * Returns head value as it was before the move, i.e. where dequeue starts
485 * Returns the current/new head value i.e. where dequeue finishes
487 * Returns the number of entries in the ring BEFORE head was moved
489 * - Actual number of objects dequeued.
490 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
492 static inline __attribute__((always_inline)) unsigned int
493 __rte_ring_move_cons_head(struct rte_ring *r, int is_sc,
494 unsigned int n, enum rte_ring_queue_behavior behavior,
495 uint32_t *old_head, uint32_t *new_head,
498 unsigned int max = n;
501 /* move cons.head atomically */
503 /* Restore n as it may change every loop */
506 *old_head = r->cons.head;
507 const uint32_t prod_tail = r->prod.tail;
508 /* The subtraction is done between two unsigned 32bits value
509 * (the result is always modulo 32 bits even if we have
510 * cons_head > prod_tail). So 'entries' is always between 0
511 * and size(ring)-1. */
512 *entries = (prod_tail - *old_head);
514 /* Set the actual entries for dequeue */
516 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
518 if (unlikely(n == 0))
521 *new_head = *old_head + n;
523 r->cons.head = *new_head, success = 1;
525 success = rte_atomic32_cmpset(&r->cons.head, *old_head,
527 } while (unlikely(success == 0));
532 * @internal Dequeue several objects from the ring
535 * A pointer to the ring structure.
537 * A pointer to a table of void * pointers (objects).
539 * The number of objects to pull from the ring.
541 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
542 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
544 * Indicates whether to use single consumer or multi-consumer head update
546 * returns the number of remaining ring entries after the dequeue has finished
548 * - Actual number of objects dequeued.
549 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
551 static inline __attribute__((always_inline)) unsigned int
552 __rte_ring_do_dequeue(struct rte_ring *r, void **obj_table,
553 unsigned int n, enum rte_ring_queue_behavior behavior,
554 int is_sc, unsigned int *available)
556 uint32_t cons_head, cons_next;
559 n = __rte_ring_move_cons_head(r, is_sc, n, behavior,
560 &cons_head, &cons_next, &entries);
564 DEQUEUE_PTRS(r, &r[1], cons_head, obj_table, n, void *);
567 update_tail(&r->cons, cons_head, cons_next, is_sc);
570 if (available != NULL)
571 *available = entries - n;
576 * Enqueue several objects on the ring (multi-producers safe).
578 * This function uses a "compare and set" instruction to move the
579 * producer index atomically.
582 * A pointer to the ring structure.
584 * A pointer to a table of void * pointers (objects).
586 * The number of objects to add in the ring from the obj_table.
588 * if non-NULL, returns the amount of space in the ring after the
589 * enqueue operation has finished.
591 * The number of objects enqueued, either 0 or n
593 static inline unsigned int __attribute__((always_inline))
594 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
595 unsigned int n, unsigned int *free_space)
597 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
598 __IS_MP, free_space);
602 * Enqueue several objects on a ring (NOT multi-producers safe).
605 * A pointer to the ring structure.
607 * A pointer to a table of void * pointers (objects).
609 * The number of objects to add in the ring from the obj_table.
611 * if non-NULL, returns the amount of space in the ring after the
612 * enqueue operation has finished.
614 * The number of objects enqueued, either 0 or n
616 static inline unsigned int __attribute__((always_inline))
617 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
618 unsigned int n, unsigned int *free_space)
620 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
621 __IS_SP, free_space);
625 * Enqueue several objects on a ring.
627 * This function calls the multi-producer or the single-producer
628 * version depending on the default behavior that was specified at
629 * ring creation time (see flags).
632 * A pointer to the ring structure.
634 * A pointer to a table of void * pointers (objects).
636 * The number of objects to add in the ring from the obj_table.
638 * if non-NULL, returns the amount of space in the ring after the
639 * enqueue operation has finished.
641 * The number of objects enqueued, either 0 or n
643 static inline unsigned int __attribute__((always_inline))
644 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
645 unsigned int n, unsigned int *free_space)
647 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
648 r->prod.single, free_space);
652 * Enqueue one object on a ring (multi-producers safe).
654 * This function uses a "compare and set" instruction to move the
655 * producer index atomically.
658 * A pointer to the ring structure.
660 * A pointer to the object to be added.
662 * - 0: Success; objects enqueued.
663 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
665 static inline int __attribute__((always_inline))
666 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
668 return rte_ring_mp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
672 * Enqueue one object on a ring (NOT multi-producers safe).
675 * A pointer to the ring structure.
677 * A pointer to the object to be added.
679 * - 0: Success; objects enqueued.
680 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
682 static inline int __attribute__((always_inline))
683 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
685 return rte_ring_sp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
689 * Enqueue one object on a ring.
691 * This function calls the multi-producer or the single-producer
692 * version, depending on the default behaviour that was specified at
693 * ring creation time (see flags).
696 * A pointer to the ring structure.
698 * A pointer to the object to be added.
700 * - 0: Success; objects enqueued.
701 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
703 static inline int __attribute__((always_inline))
704 rte_ring_enqueue(struct rte_ring *r, void *obj)
706 return rte_ring_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
710 * Dequeue several objects from a ring (multi-consumers safe).
712 * This function uses a "compare and set" instruction to move the
713 * consumer index atomically.
716 * A pointer to the ring structure.
718 * A pointer to a table of void * pointers (objects) that will be filled.
720 * The number of objects to dequeue from the ring to the obj_table.
722 * If non-NULL, returns the number of remaining ring entries after the
723 * dequeue has finished.
725 * The number of objects dequeued, either 0 or n
727 static inline unsigned int __attribute__((always_inline))
728 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table,
729 unsigned int n, unsigned int *available)
731 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
736 * Dequeue several objects from a ring (NOT multi-consumers safe).
739 * A pointer to the ring structure.
741 * A pointer to a table of void * pointers (objects) that will be filled.
743 * The number of objects to dequeue from the ring to the obj_table,
744 * must be strictly positive.
746 * If non-NULL, returns the number of remaining ring entries after the
747 * dequeue has finished.
749 * The number of objects dequeued, either 0 or n
751 static inline unsigned int __attribute__((always_inline))
752 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
753 unsigned int n, unsigned int *available)
755 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
760 * Dequeue several objects from a ring.
762 * This function calls the multi-consumers or the single-consumer
763 * version, depending on the default behaviour that was specified at
764 * ring creation time (see flags).
767 * A pointer to the ring structure.
769 * A pointer to a table of void * pointers (objects) that will be filled.
771 * The number of objects to dequeue from the ring to the obj_table.
773 * If non-NULL, returns the number of remaining ring entries after the
774 * dequeue has finished.
776 * The number of objects dequeued, either 0 or n
778 static inline unsigned int __attribute__((always_inline))
779 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
780 unsigned int *available)
782 return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
783 r->cons.single, available);
787 * Dequeue one object from a ring (multi-consumers safe).
789 * This function uses a "compare and set" instruction to move the
790 * consumer index atomically.
793 * A pointer to the ring structure.
795 * A pointer to a void * pointer (object) that will be filled.
797 * - 0: Success; objects dequeued.
798 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
801 static inline int __attribute__((always_inline))
802 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
804 return rte_ring_mc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
808 * Dequeue one object from a ring (NOT multi-consumers safe).
811 * A pointer to the ring structure.
813 * A pointer to a void * pointer (object) that will be filled.
815 * - 0: Success; objects dequeued.
816 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
819 static inline int __attribute__((always_inline))
820 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
822 return rte_ring_sc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
826 * Dequeue one object from a ring.
828 * This function calls the multi-consumers or the single-consumer
829 * version depending on the default behaviour that was specified at
830 * ring creation time (see flags).
833 * A pointer to the ring structure.
835 * A pointer to a void * pointer (object) that will be filled.
837 * - 0: Success, objects dequeued.
838 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
841 static inline int __attribute__((always_inline))
842 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
844 return rte_ring_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
848 * Test if a ring is full.
851 * A pointer to the ring structure.
853 * - 1: The ring is full.
854 * - 0: The ring is not full.
857 rte_ring_full(const struct rte_ring *r)
859 uint32_t prod_tail = r->prod.tail;
860 uint32_t cons_tail = r->cons.tail;
861 return ((cons_tail - prod_tail - 1) & r->mask) == 0;
865 * Test if a ring is empty.
868 * A pointer to the ring structure.
870 * - 1: The ring is empty.
871 * - 0: The ring is not empty.
874 rte_ring_empty(const struct rte_ring *r)
876 uint32_t prod_tail = r->prod.tail;
877 uint32_t cons_tail = r->cons.tail;
878 return !!(cons_tail == prod_tail);
882 * Return the number of entries in a ring.
885 * A pointer to the ring structure.
887 * The number of entries in the ring.
889 static inline unsigned
890 rte_ring_count(const struct rte_ring *r)
892 uint32_t prod_tail = r->prod.tail;
893 uint32_t cons_tail = r->cons.tail;
894 return (prod_tail - cons_tail) & r->mask;
898 * Return the number of free entries in a ring.
901 * A pointer to the ring structure.
903 * The number of free entries in the ring.
905 static inline unsigned
906 rte_ring_free_count(const struct rte_ring *r)
908 uint32_t prod_tail = r->prod.tail;
909 uint32_t cons_tail = r->cons.tail;
910 return (cons_tail - prod_tail - 1) & r->mask;
914 * Return the size of the ring.
917 * A pointer to the ring structure.
919 * The number of elements which can be stored in the ring.
921 static inline unsigned int
922 rte_ring_get_size(const struct rte_ring *r)
928 * Dump the status of all rings on the console
931 * A pointer to a file for output
933 void rte_ring_list_dump(FILE *f);
936 * Search a ring from its name
939 * The name of the ring.
941 * The pointer to the ring matching the name, or NULL if not found,
942 * with rte_errno set appropriately. Possible rte_errno values include:
943 * - ENOENT - required entry not available to return.
945 struct rte_ring *rte_ring_lookup(const char *name);
948 * Enqueue several objects on the ring (multi-producers safe).
950 * This function uses a "compare and set" instruction to move the
951 * producer index atomically.
954 * A pointer to the ring structure.
956 * A pointer to a table of void * pointers (objects).
958 * The number of objects to add in the ring from the obj_table.
960 * if non-NULL, returns the amount of space in the ring after the
961 * enqueue operation has finished.
963 * - n: Actual number of objects enqueued.
965 static inline unsigned __attribute__((always_inline))
966 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
967 unsigned int n, unsigned int *free_space)
969 return __rte_ring_do_enqueue(r, obj_table, n,
970 RTE_RING_QUEUE_VARIABLE, __IS_MP, free_space);
974 * Enqueue several objects on a ring (NOT multi-producers safe).
977 * A pointer to the ring structure.
979 * A pointer to a table of void * pointers (objects).
981 * The number of objects to add in the ring from the obj_table.
983 * if non-NULL, returns the amount of space in the ring after the
984 * enqueue operation has finished.
986 * - n: Actual number of objects enqueued.
988 static inline unsigned __attribute__((always_inline))
989 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
990 unsigned int n, unsigned int *free_space)
992 return __rte_ring_do_enqueue(r, obj_table, n,
993 RTE_RING_QUEUE_VARIABLE, __IS_SP, free_space);
997 * Enqueue several objects on a ring.
999 * This function calls the multi-producer or the single-producer
1000 * version depending on the default behavior that was specified at
1001 * ring creation time (see flags).
1004 * A pointer to the ring structure.
1006 * A pointer to a table of void * pointers (objects).
1008 * The number of objects to add in the ring from the obj_table.
1010 * if non-NULL, returns the amount of space in the ring after the
1011 * enqueue operation has finished.
1013 * - n: Actual number of objects enqueued.
1015 static inline unsigned __attribute__((always_inline))
1016 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1017 unsigned int n, unsigned int *free_space)
1019 return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE,
1020 r->prod.single, free_space);
1024 * Dequeue several objects from a ring (multi-consumers safe). When the request
1025 * objects are more than the available objects, only dequeue the actual number
1028 * This function uses a "compare and set" instruction to move the
1029 * consumer index atomically.
1032 * A pointer to the ring structure.
1034 * A pointer to a table of void * pointers (objects) that will be filled.
1036 * The number of objects to dequeue from the ring to the obj_table.
1038 * If non-NULL, returns the number of remaining ring entries after the
1039 * dequeue has finished.
1041 * - n: Actual number of objects dequeued, 0 if ring is empty
1043 static inline unsigned __attribute__((always_inline))
1044 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
1045 unsigned int n, unsigned int *available)
1047 return __rte_ring_do_dequeue(r, obj_table, n,
1048 RTE_RING_QUEUE_VARIABLE, __IS_MC, available);
1052 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1053 * request objects are more than the available objects, only dequeue the
1054 * actual number of objects
1057 * A pointer to the ring structure.
1059 * A pointer to a table of void * pointers (objects) that will be filled.
1061 * The number of objects to dequeue from the ring to the obj_table.
1063 * If non-NULL, returns the number of remaining ring entries after the
1064 * dequeue has finished.
1066 * - n: Actual number of objects dequeued, 0 if ring is empty
1068 static inline unsigned __attribute__((always_inline))
1069 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
1070 unsigned int n, unsigned int *available)
1072 return __rte_ring_do_dequeue(r, obj_table, n,
1073 RTE_RING_QUEUE_VARIABLE, __IS_SC, available);
1077 * Dequeue multiple objects from a ring up to a maximum number.
1079 * This function calls the multi-consumers or the single-consumer
1080 * version, depending on the default behaviour that was specified at
1081 * ring creation time (see flags).
1084 * A pointer to the ring structure.
1086 * A pointer to a table of void * pointers (objects) that will be filled.
1088 * The number of objects to dequeue from the ring to the obj_table.
1090 * If non-NULL, returns the number of remaining ring entries after the
1091 * dequeue has finished.
1093 * - Number of objects dequeued
1095 static inline unsigned __attribute__((always_inline))
1096 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
1097 unsigned int n, unsigned int *available)
1099 return __rte_ring_do_dequeue(r, obj_table, n,
1100 RTE_RING_QUEUE_VARIABLE,
1101 r->cons.single, available);
1108 #endif /* _RTE_RING_H_ */