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 */
173 * Calculate the memory size needed for a ring
175 * This function returns the number of bytes needed for a ring, given
176 * the number of elements in it. This value is the sum of the size of
177 * the structure rte_ring and the size of the memory needed by the
178 * objects pointers. The value is aligned to a cache line size.
181 * The number of elements in the ring (must be a power of 2).
183 * - The memory size needed for the ring on success.
184 * - -EINVAL if count is not a power of 2.
186 ssize_t rte_ring_get_memsize(unsigned count);
189 * Initialize a ring structure.
191 * Initialize a ring structure in memory pointed by "r". The size of the
192 * memory area must be large enough to store the ring structure and the
193 * object table. It is advised to use rte_ring_get_memsize() to get the
196 * The ring size is set to *count*, which must be a power of two. Water
197 * marking is disabled by default. The real usable ring size is
198 * *count-1* instead of *count* to differentiate a free ring from an
201 * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
202 * memory given by the caller may not be shareable among dpdk
206 * The pointer to the ring structure followed by the objects table.
208 * The name of the ring.
210 * The number of elements in the ring (must be a power of 2).
212 * An OR of the following:
213 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
214 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
215 * is "single-producer". Otherwise, it is "multi-producers".
216 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
217 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
218 * is "single-consumer". Otherwise, it is "multi-consumers".
220 * 0 on success, or a negative value on error.
222 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
226 * Create a new ring named *name* in memory.
228 * This function uses ``memzone_reserve()`` to allocate memory. Then it
229 * calls rte_ring_init() to initialize an empty ring.
231 * The new ring size is set to *count*, which must be a power of
232 * two. Water marking is disabled by default. The real usable ring size
233 * is *count-1* instead of *count* to differentiate a free ring from an
236 * The ring is added in RTE_TAILQ_RING list.
239 * The name of the ring.
241 * The size of the ring (must be a power of 2).
243 * The *socket_id* argument is the socket identifier in case of
244 * NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
245 * constraint for the reserved zone.
247 * An OR of the following:
248 * - RING_F_SP_ENQ: If this flag is set, the default behavior when
249 * using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
250 * is "single-producer". Otherwise, it is "multi-producers".
251 * - RING_F_SC_DEQ: If this flag is set, the default behavior when
252 * using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
253 * is "single-consumer". Otherwise, it is "multi-consumers".
255 * On success, the pointer to the new allocated ring. NULL on error with
256 * rte_errno set appropriately. Possible errno values include:
257 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
258 * - E_RTE_SECONDARY - function was called from a secondary process instance
259 * - EINVAL - count provided is not a power of 2
260 * - ENOSPC - the maximum number of memzones has already been allocated
261 * - EEXIST - a memzone with the same name already exists
262 * - ENOMEM - no appropriate memory area found in which to create memzone
264 struct rte_ring *rte_ring_create(const char *name, unsigned count,
265 int socket_id, unsigned flags);
267 * De-allocate all memory used by the ring.
272 void rte_ring_free(struct rte_ring *r);
275 * Dump the status of the ring to a file.
278 * A pointer to a file for output
280 * A pointer to the ring structure.
282 void rte_ring_dump(FILE *f, 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->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->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 * Actual number of objects enqueued.
353 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
355 static inline unsigned int __attribute__((always_inline))
356 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
357 unsigned int n, enum rte_ring_queue_behavior behavior,
358 unsigned int *free_space)
360 uint32_t prod_head, prod_next;
361 uint32_t cons_tail, free_entries;
362 const unsigned int max = n;
365 uint32_t mask = r->mask;
367 /* move prod.head atomically */
369 /* Reset n to the initial burst count */
372 prod_head = r->prod.head;
373 cons_tail = r->cons.tail;
374 /* The subtraction is done between two unsigned 32bits value
375 * (the result is always modulo 32 bits even if we have
376 * prod_head > cons_tail). So 'free_entries' is always between 0
377 * and size(ring)-1. */
378 free_entries = (mask + cons_tail - prod_head);
380 /* check that we have enough room in ring */
381 if (unlikely(n > free_entries))
382 n = (behavior == RTE_RING_QUEUE_FIXED) ?
388 prod_next = prod_head + n;
389 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
391 } while (unlikely(success == 0));
393 /* write entries in ring */
398 * If there are other enqueues in progress that preceded us,
399 * we need to wait for them to complete
401 while (unlikely(r->prod.tail != prod_head))
404 r->prod.tail = prod_next;
406 if (free_space != NULL)
407 *free_space = free_entries - n;
412 * @internal Enqueue several objects on a ring (NOT multi-producers safe).
415 * A pointer to the ring structure.
417 * A pointer to a table of void * pointers (objects).
419 * The number of objects to add in the ring from the obj_table.
421 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
422 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
424 * Actual number of objects enqueued.
425 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
427 static inline unsigned int __attribute__((always_inline))
428 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
429 unsigned int n, enum rte_ring_queue_behavior behavior,
430 unsigned int *free_space)
432 uint32_t prod_head, cons_tail;
433 uint32_t prod_next, free_entries;
435 uint32_t mask = r->mask;
437 prod_head = r->prod.head;
438 cons_tail = r->cons.tail;
439 /* The subtraction is done between two unsigned 32bits value
440 * (the result is always modulo 32 bits even if we have
441 * prod_head > cons_tail). So 'free_entries' is always between 0
442 * and size(ring)-1. */
443 free_entries = mask + cons_tail - prod_head;
445 /* check that we have enough room in ring */
446 if (unlikely(n > free_entries))
447 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : free_entries;
453 prod_next = prod_head + n;
454 r->prod.head = prod_next;
456 /* write entries in ring */
460 r->prod.tail = prod_next;
462 if (free_space != NULL)
463 *free_space = free_entries - n;
468 * @internal Dequeue several objects from a ring (multi-consumers safe). When
469 * the request objects are more than the available objects, only dequeue the
470 * actual number of objects
472 * This function uses a "compare and set" instruction to move the
473 * consumer index atomically.
476 * A pointer to the ring structure.
478 * A pointer to a table of void * pointers (objects) that will be filled.
480 * The number of objects to dequeue from the ring to the obj_table.
482 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
483 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
485 * - Actual number of objects dequeued.
486 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
489 static inline unsigned int __attribute__((always_inline))
490 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
491 unsigned int n, enum rte_ring_queue_behavior behavior,
492 unsigned int *available)
494 uint32_t cons_head, prod_tail;
495 uint32_t cons_next, entries;
496 const unsigned max = n;
499 uint32_t mask = r->mask;
501 /* move cons.head atomically */
503 /* Restore n as it may change every loop */
506 cons_head = r->cons.head;
507 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 - cons_head);
514 /* Set the actual entries for dequeue */
516 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : entries;
518 if (unlikely(n == 0))
521 cons_next = cons_head + n;
522 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
524 } while (unlikely(success == 0));
531 * If there are other dequeues in progress that preceded us,
532 * we need to wait for them to complete
534 while (unlikely(r->cons.tail != cons_head))
537 r->cons.tail = cons_next;
539 if (available != NULL)
540 *available = entries - n;
545 * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
546 * When the request objects are more than the available objects, only dequeue
547 * the actual number of objects
550 * A pointer to the ring structure.
552 * A pointer to a table of void * pointers (objects) that will be filled.
554 * The number of objects to dequeue from the ring to the obj_table.
556 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
557 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
559 * - Actual number of objects dequeued.
560 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
562 static inline unsigned int __attribute__((always_inline))
563 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
564 unsigned int n, enum rte_ring_queue_behavior behavior,
565 unsigned int *available)
567 uint32_t cons_head, prod_tail;
568 uint32_t cons_next, entries;
570 uint32_t mask = r->mask;
572 cons_head = r->cons.head;
573 prod_tail = r->prod.tail;
574 /* The subtraction is done between two unsigned 32bits value
575 * (the result is always modulo 32 bits even if we have
576 * cons_head > prod_tail). So 'entries' is always between 0
577 * and size(ring)-1. */
578 entries = prod_tail - cons_head;
581 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : entries;
583 if (unlikely(entries == 0))
586 cons_next = cons_head + n;
587 r->cons.head = cons_next;
593 r->cons.tail = cons_next;
595 if (available != NULL)
596 *available = entries - n;
601 * Enqueue several objects on the ring (multi-producers safe).
603 * This function uses a "compare and set" instruction to move the
604 * producer index atomically.
607 * A pointer to the ring structure.
609 * A pointer to a table of void * pointers (objects).
611 * The number of objects to add in the ring from the obj_table.
613 * if non-NULL, returns the amount of space in the ring after the
614 * enqueue operation has finished.
616 * The number of objects enqueued, either 0 or n
618 static inline unsigned int __attribute__((always_inline))
619 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
620 unsigned int n, unsigned int *free_space)
622 return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
627 * Enqueue several objects on a ring (NOT multi-producers safe).
630 * A pointer to the ring structure.
632 * A pointer to a table of void * pointers (objects).
634 * The number of objects to add in the ring from the obj_table.
636 * if non-NULL, returns the amount of space in the ring after the
637 * enqueue operation has finished.
639 * The number of objects enqueued, either 0 or n
641 static inline unsigned int __attribute__((always_inline))
642 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
643 unsigned int n, unsigned int *free_space)
645 return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
650 * Enqueue several objects on a ring.
652 * This function calls the multi-producer or the single-producer
653 * version depending on the default behavior that was specified at
654 * ring creation time (see flags).
657 * A pointer to the ring structure.
659 * A pointer to a table of void * pointers (objects).
661 * The number of objects to add in the ring from the obj_table.
663 * if non-NULL, returns the amount of space in the ring after the
664 * enqueue operation has finished.
666 * The number of objects enqueued, either 0 or n
668 static inline unsigned int __attribute__((always_inline))
669 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
670 unsigned int n, unsigned int *free_space)
673 return rte_ring_sp_enqueue_bulk(r, obj_table, n, free_space);
675 return rte_ring_mp_enqueue_bulk(r, obj_table, n, free_space);
679 * Enqueue one object on a ring (multi-producers safe).
681 * This function uses a "compare and set" instruction to move the
682 * producer index atomically.
685 * A pointer to the ring structure.
687 * A pointer to the object to be added.
689 * - 0: Success; objects enqueued.
690 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
692 static inline int __attribute__((always_inline))
693 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
695 return rte_ring_mp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
699 * Enqueue one object on a ring (NOT multi-producers safe).
702 * A pointer to the ring structure.
704 * A pointer to the object to be added.
706 * - 0: Success; objects enqueued.
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(struct rte_ring *r, void *obj)
712 return rte_ring_sp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
716 * Enqueue one object on a ring.
718 * This function calls the multi-producer or the single-producer
719 * version, depending on the default behaviour that was specified at
720 * ring creation time (see flags).
723 * A pointer to the ring structure.
725 * A pointer to the object to be added.
727 * - 0: Success; objects enqueued.
728 * - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
730 static inline int __attribute__((always_inline))
731 rte_ring_enqueue(struct rte_ring *r, void *obj)
733 return rte_ring_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
737 * Dequeue several objects from a ring (multi-consumers safe).
739 * This function uses a "compare and set" instruction to move the
740 * consumer index atomically.
743 * A pointer to the ring structure.
745 * A pointer to a table of void * pointers (objects) that will be filled.
747 * The number of objects to dequeue from the ring to the obj_table.
749 * If non-NULL, returns the number of remaining ring entries after the
750 * dequeue has finished.
752 * The number of objects dequeued, either 0 or n
754 static inline unsigned int __attribute__((always_inline))
755 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table,
756 unsigned int n, unsigned int *available)
758 return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
763 * Dequeue several objects from a ring (NOT multi-consumers safe).
766 * A pointer to the ring structure.
768 * A pointer to a table of void * pointers (objects) that will be filled.
770 * The number of objects to dequeue from the ring to the obj_table,
771 * must be strictly positive.
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_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
780 unsigned int n, unsigned int *available)
782 return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
787 * Dequeue several objects from a ring.
789 * This function calls the multi-consumers or the single-consumer
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 a table of void * pointers (objects) that will be filled.
798 * The number of objects to dequeue from the ring to the obj_table.
800 * If non-NULL, returns the number of remaining ring entries after the
801 * dequeue has finished.
803 * The number of objects dequeued, either 0 or n
805 static inline unsigned int __attribute__((always_inline))
806 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
807 unsigned int *available)
810 return rte_ring_sc_dequeue_bulk(r, obj_table, n, available);
812 return rte_ring_mc_dequeue_bulk(r, obj_table, n, available);
816 * Dequeue one object from a ring (multi-consumers safe).
818 * This function uses a "compare and set" instruction to move the
819 * consumer index atomically.
822 * A pointer to the ring structure.
824 * A pointer to a void * pointer (object) that will be filled.
826 * - 0: Success; objects dequeued.
827 * - -ENOENT: Not enough entries in the ring to dequeue; no object is
830 static inline int __attribute__((always_inline))
831 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
833 return rte_ring_mc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
837 * Dequeue one object from a ring (NOT multi-consumers safe).
840 * A pointer to the ring structure.
842 * A pointer to a void * pointer (object) that will be filled.
844 * - 0: Success; objects dequeued.
845 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
848 static inline int __attribute__((always_inline))
849 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
851 return rte_ring_sc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
855 * Dequeue one object from a ring.
857 * This function calls the multi-consumers or the single-consumer
858 * version depending on the default behaviour that was specified at
859 * ring creation time (see flags).
862 * A pointer to the ring structure.
864 * A pointer to a void * pointer (object) that will be filled.
866 * - 0: Success, objects dequeued.
867 * - -ENOENT: Not enough entries in the ring to dequeue, no object is
870 static inline int __attribute__((always_inline))
871 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
873 return rte_ring_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
877 * Test if a ring is full.
880 * A pointer to the ring structure.
882 * - 1: The ring is full.
883 * - 0: The ring is not full.
886 rte_ring_full(const struct rte_ring *r)
888 uint32_t prod_tail = r->prod.tail;
889 uint32_t cons_tail = r->cons.tail;
890 return ((cons_tail - prod_tail - 1) & r->mask) == 0;
894 * Test if a ring is empty.
897 * A pointer to the ring structure.
899 * - 1: The ring is empty.
900 * - 0: The ring is not empty.
903 rte_ring_empty(const struct rte_ring *r)
905 uint32_t prod_tail = r->prod.tail;
906 uint32_t cons_tail = r->cons.tail;
907 return !!(cons_tail == prod_tail);
911 * Return the number of entries in a ring.
914 * A pointer to the ring structure.
916 * The number of entries in the ring.
918 static inline unsigned
919 rte_ring_count(const struct rte_ring *r)
921 uint32_t prod_tail = r->prod.tail;
922 uint32_t cons_tail = r->cons.tail;
923 return (prod_tail - cons_tail) & r->mask;
927 * Return the number of free entries in a ring.
930 * A pointer to the ring structure.
932 * The number of free entries in the ring.
934 static inline unsigned
935 rte_ring_free_count(const struct rte_ring *r)
937 uint32_t prod_tail = r->prod.tail;
938 uint32_t cons_tail = r->cons.tail;
939 return (cons_tail - prod_tail - 1) & r->mask;
943 * Return the size of the ring.
946 * A pointer to the ring structure.
948 * The number of elements which can be stored in the ring.
950 static inline unsigned int
951 rte_ring_get_size(const struct rte_ring *r)
957 * Dump the status of all rings on the console
960 * A pointer to a file for output
962 void rte_ring_list_dump(FILE *f);
965 * Search a ring from its name
968 * The name of the ring.
970 * The pointer to the ring matching the name, or NULL if not found,
971 * with rte_errno set appropriately. Possible rte_errno values include:
972 * - ENOENT - required entry not available to return.
974 struct rte_ring *rte_ring_lookup(const char *name);
977 * Enqueue several objects on the ring (multi-producers safe).
979 * This function uses a "compare and set" instruction to move the
980 * producer index atomically.
983 * A pointer to the ring structure.
985 * A pointer to a table of void * pointers (objects).
987 * The number of objects to add in the ring from the obj_table.
989 * if non-NULL, returns the amount of space in the ring after the
990 * enqueue operation has finished.
992 * - n: Actual number of objects enqueued.
994 static inline unsigned __attribute__((always_inline))
995 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
996 unsigned int n, unsigned int *free_space)
998 return __rte_ring_mp_do_enqueue(r, obj_table, n,
999 RTE_RING_QUEUE_VARIABLE, free_space);
1003 * Enqueue several objects on a ring (NOT multi-producers safe).
1006 * A pointer to the ring structure.
1008 * A pointer to a table of void * pointers (objects).
1010 * The number of objects to add in the ring from the obj_table.
1012 * if non-NULL, returns the amount of space in the ring after the
1013 * enqueue operation has finished.
1015 * - n: Actual number of objects enqueued.
1017 static inline unsigned __attribute__((always_inline))
1018 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1019 unsigned int n, unsigned int *free_space)
1021 return __rte_ring_sp_do_enqueue(r, obj_table, n,
1022 RTE_RING_QUEUE_VARIABLE, free_space);
1026 * Enqueue several objects on a ring.
1028 * This function calls the multi-producer or the single-producer
1029 * version depending on the default behavior that was specified at
1030 * ring creation time (see flags).
1033 * A pointer to the ring structure.
1035 * A pointer to a table of void * pointers (objects).
1037 * The number of objects to add in the ring from the obj_table.
1039 * if non-NULL, returns the amount of space in the ring after the
1040 * enqueue operation has finished.
1042 * - n: Actual number of objects enqueued.
1044 static inline unsigned __attribute__((always_inline))
1045 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1046 unsigned int n, unsigned int *free_space)
1049 return rte_ring_sp_enqueue_burst(r, obj_table, n, free_space);
1051 return rte_ring_mp_enqueue_burst(r, obj_table, n, free_space);
1055 * Dequeue several objects from a ring (multi-consumers safe). When the request
1056 * objects are more than the available objects, only dequeue the actual number
1059 * This function uses a "compare and set" instruction to move the
1060 * consumer index atomically.
1063 * A pointer to the ring structure.
1065 * A pointer to a table of void * pointers (objects) that will be filled.
1067 * The number of objects to dequeue from the ring to the obj_table.
1069 * If non-NULL, returns the number of remaining ring entries after the
1070 * dequeue has finished.
1072 * - n: Actual number of objects dequeued, 0 if ring is empty
1074 static inline unsigned __attribute__((always_inline))
1075 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
1076 unsigned int n, unsigned int *available)
1078 return __rte_ring_mc_do_dequeue(r, obj_table, n,
1079 RTE_RING_QUEUE_VARIABLE, available);
1083 * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1084 * request objects are more than the available objects, only dequeue the
1085 * actual number of objects
1088 * A pointer to the ring structure.
1090 * A pointer to a table of void * pointers (objects) that will be filled.
1092 * The number of objects to dequeue from the ring to the obj_table.
1094 * If non-NULL, returns the number of remaining ring entries after the
1095 * dequeue has finished.
1097 * - n: Actual number of objects dequeued, 0 if ring is empty
1099 static inline unsigned __attribute__((always_inline))
1100 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
1101 unsigned int n, unsigned int *available)
1103 return __rte_ring_sc_do_dequeue(r, obj_table, n,
1104 RTE_RING_QUEUE_VARIABLE, available);
1108 * Dequeue multiple objects from a ring up to a maximum number.
1110 * This function calls the multi-consumers or the single-consumer
1111 * version, depending on the default behaviour that was specified at
1112 * ring creation time (see flags).
1115 * A pointer to the ring structure.
1117 * A pointer to a table of void * pointers (objects) that will be filled.
1119 * The number of objects to dequeue from the ring to the obj_table.
1121 * If non-NULL, returns the number of remaining ring entries after the
1122 * dequeue has finished.
1124 * - Number of objects dequeued
1126 static inline unsigned __attribute__((always_inline))
1127 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
1128 unsigned int n, unsigned int *available)
1131 return rte_ring_sc_dequeue_burst(r, obj_table, n, available);
1133 return rte_ring_mc_dequeue_burst(r, obj_table, n, available);
1140 #endif /* _RTE_RING_H_ */