ring: create common structure for prod and cons metadata
[dpdk.git] / lib / librte_ring / rte_ring.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 /*
35  * Derived from FreeBSD's bufring.h
36  *
37  **************************************************************************
38  *
39  * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions are met:
44  *
45  * 1. Redistributions of source code must retain the above copyright notice,
46  *    this list of conditions and the following disclaimer.
47  *
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.
51  *
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.
63  *
64  ***************************************************************************/
65
66 #ifndef _RTE_RING_H_
67 #define _RTE_RING_H_
68
69 /**
70  * @file
71  * RTE Ring
72  *
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:
76  *
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.
82  * - Bulk dequeue.
83  * - Bulk enqueue.
84  *
85  * Note: the ring implementation is not preemptable. A lcore must not
86  * be interrupted by another task that uses the same ring.
87  *
88  */
89
90 #ifdef __cplusplus
91 extern "C" {
92 #endif
93
94 #include <stdio.h>
95 #include <stdint.h>
96 #include <sys/queue.h>
97 #include <errno.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>
104
105 #define RTE_TAILQ_RING_NAME "RTE_RING"
106
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 */
110 };
111
112 #ifdef RTE_LIBRTE_RING_DEBUG
113 /**
114  * A structure that stores the ring statistics (per-lcore).
115  */
116 struct rte_ring_debug_stats {
117         uint64_t enq_success_bulk; /**< Successful enqueues number. */
118         uint64_t enq_success_objs; /**< Objects successfully enqueued. */
119         uint64_t enq_quota_bulk;   /**< Successful enqueues above watermark. */
120         uint64_t enq_quota_objs;   /**< Objects enqueued above watermark. */
121         uint64_t enq_fail_bulk;    /**< Failed enqueues number. */
122         uint64_t enq_fail_objs;    /**< Objects that failed to be enqueued. */
123         uint64_t deq_success_bulk; /**< Successful dequeues number. */
124         uint64_t deq_success_objs; /**< Objects successfully dequeued. */
125         uint64_t deq_fail_bulk;    /**< Failed dequeues number. */
126         uint64_t deq_fail_objs;    /**< Objects that failed to be dequeued. */
127 } __rte_cache_aligned;
128 #endif
129
130 #define RTE_RING_MZ_PREFIX "RG_"
131 /**< The maximum length of a ring name. */
132 #define RTE_RING_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
133                            sizeof(RTE_RING_MZ_PREFIX) + 1)
134
135 #ifndef RTE_RING_PAUSE_REP_COUNT
136 #define RTE_RING_PAUSE_REP_COUNT 0 /**< Yield after pause num of times, no yield
137                                     *   if RTE_RING_PAUSE_REP not defined. */
138 #endif
139
140 struct rte_memzone; /* forward declaration, so as not to require memzone.h */
141
142 #if RTE_CACHE_LINE_SIZE < 128
143 #define PROD_ALIGN (RTE_CACHE_LINE_SIZE * 2)
144 #define CONS_ALIGN (RTE_CACHE_LINE_SIZE * 2)
145 #else
146 #define PROD_ALIGN RTE_CACHE_LINE_SIZE
147 #define CONS_ALIGN RTE_CACHE_LINE_SIZE
148 #endif
149
150 /* structure to hold a pair of head/tail values and other metadata */
151 struct rte_ring_headtail {
152         volatile uint32_t head;  /**< Prod/consumer head. */
153         volatile uint32_t tail;  /**< Prod/consumer tail. */
154         uint32_t size;           /**< Size of ring. */
155         uint32_t mask;           /**< Mask (size-1) of ring. */
156         uint32_t single;         /**< True if single prod/cons */
157         uint32_t watermark;      /**< Max items before EDQUOT in producer. */
158 };
159
160 /**
161  * An RTE ring structure.
162  *
163  * The producer and the consumer have a head and a tail index. The particularity
164  * of these index is that they are not between 0 and size(ring). These indexes
165  * are between 0 and 2^32, and we mask their value when we access the ring[]
166  * field. Thanks to this assumption, we can do subtractions between 2 index
167  * values in a modulo-32bit base: that's why the overflow of the indexes is not
168  * a problem.
169  */
170 struct rte_ring {
171         /*
172          * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
173          * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
174          * next time the ABI changes
175          */
176         char name[RTE_MEMZONE_NAMESIZE];    /**< Name of the ring. */
177         int flags;                       /**< Flags supplied at creation. */
178         const struct rte_memzone *memzone;
179                         /**< Memzone, if any, containing the rte_ring */
180
181         /** Ring producer status. */
182         struct rte_ring_headtail prod __rte_aligned(PROD_ALIGN);
183
184         /** Ring consumer status. */
185         struct rte_ring_headtail cons __rte_aligned(CONS_ALIGN);
186
187 #ifdef RTE_LIBRTE_RING_DEBUG
188         struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
189 #endif
190
191         void *ring[] __rte_cache_aligned;   /**< Memory space of ring starts here.
192                                              * not volatile so need to be careful
193                                              * about compiler re-ordering */
194 };
195
196 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
197 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
198 #define RTE_RING_QUOT_EXCEED (1 << 31)  /**< Quota exceed for burst ops */
199 #define RTE_RING_SZ_MASK  (unsigned)(0x0fffffff) /**< Ring size mask */
200
201 /**
202  * @internal When debug is enabled, store ring statistics.
203  * @param r
204  *   A pointer to the ring.
205  * @param name
206  *   The name of the statistics field to increment in the ring.
207  * @param n
208  *   The number to add to the object-oriented statistics.
209  */
210 #ifdef RTE_LIBRTE_RING_DEBUG
211 #define __RING_STAT_ADD(r, name, n) do {                        \
212                 unsigned __lcore_id = rte_lcore_id();           \
213                 if (__lcore_id < RTE_MAX_LCORE) {               \
214                         r->stats[__lcore_id].name##_objs += n;  \
215                         r->stats[__lcore_id].name##_bulk += 1;  \
216                 }                                               \
217         } while(0)
218 #else
219 #define __RING_STAT_ADD(r, name, n) do {} while(0)
220 #endif
221
222 /**
223  * Calculate the memory size needed for a ring
224  *
225  * This function returns the number of bytes needed for a ring, given
226  * the number of elements in it. This value is the sum of the size of
227  * the structure rte_ring and the size of the memory needed by the
228  * objects pointers. The value is aligned to a cache line size.
229  *
230  * @param count
231  *   The number of elements in the ring (must be a power of 2).
232  * @return
233  *   - The memory size needed for the ring on success.
234  *   - -EINVAL if count is not a power of 2.
235  */
236 ssize_t rte_ring_get_memsize(unsigned count);
237
238 /**
239  * Initialize a ring structure.
240  *
241  * Initialize a ring structure in memory pointed by "r". The size of the
242  * memory area must be large enough to store the ring structure and the
243  * object table. It is advised to use rte_ring_get_memsize() to get the
244  * appropriate size.
245  *
246  * The ring size is set to *count*, which must be a power of two. Water
247  * marking is disabled by default. The real usable ring size is
248  * *count-1* instead of *count* to differentiate a free ring from an
249  * empty ring.
250  *
251  * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
252  * memory given by the caller may not be shareable among dpdk
253  * processes.
254  *
255  * @param r
256  *   The pointer to the ring structure followed by the objects table.
257  * @param name
258  *   The name of the ring.
259  * @param count
260  *   The number of elements in the ring (must be a power of 2).
261  * @param flags
262  *   An OR of the following:
263  *    - RING_F_SP_ENQ: If this flag is set, the default behavior when
264  *      using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
265  *      is "single-producer". Otherwise, it is "multi-producers".
266  *    - RING_F_SC_DEQ: If this flag is set, the default behavior when
267  *      using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
268  *      is "single-consumer". Otherwise, it is "multi-consumers".
269  * @return
270  *   0 on success, or a negative value on error.
271  */
272 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
273         unsigned flags);
274
275 /**
276  * Create a new ring named *name* in memory.
277  *
278  * This function uses ``memzone_reserve()`` to allocate memory. Then it
279  * calls rte_ring_init() to initialize an empty ring.
280  *
281  * The new ring size is set to *count*, which must be a power of
282  * two. Water marking is disabled by default. The real usable ring size
283  * is *count-1* instead of *count* to differentiate a free ring from an
284  * empty ring.
285  *
286  * The ring is added in RTE_TAILQ_RING list.
287  *
288  * @param name
289  *   The name of the ring.
290  * @param count
291  *   The size of the ring (must be a power of 2).
292  * @param socket_id
293  *   The *socket_id* argument is the socket identifier in case of
294  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
295  *   constraint for the reserved zone.
296  * @param flags
297  *   An OR of the following:
298  *    - RING_F_SP_ENQ: If this flag is set, the default behavior when
299  *      using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
300  *      is "single-producer". Otherwise, it is "multi-producers".
301  *    - RING_F_SC_DEQ: If this flag is set, the default behavior when
302  *      using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
303  *      is "single-consumer". Otherwise, it is "multi-consumers".
304  * @return
305  *   On success, the pointer to the new allocated ring. NULL on error with
306  *    rte_errno set appropriately. Possible errno values include:
307  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
308  *    - E_RTE_SECONDARY - function was called from a secondary process instance
309  *    - EINVAL - count provided is not a power of 2
310  *    - ENOSPC - the maximum number of memzones has already been allocated
311  *    - EEXIST - a memzone with the same name already exists
312  *    - ENOMEM - no appropriate memory area found in which to create memzone
313  */
314 struct rte_ring *rte_ring_create(const char *name, unsigned count,
315                                  int socket_id, unsigned flags);
316 /**
317  * De-allocate all memory used by the ring.
318  *
319  * @param r
320  *   Ring to free
321  */
322 void rte_ring_free(struct rte_ring *r);
323
324 /**
325  * Change the high water mark.
326  *
327  * If *count* is 0, water marking is disabled. Otherwise, it is set to the
328  * *count* value. The *count* value must be greater than 0 and less
329  * than the ring size.
330  *
331  * This function can be called at any time (not necessarily at
332  * initialization).
333  *
334  * @param r
335  *   A pointer to the ring structure.
336  * @param count
337  *   The new water mark value.
338  * @return
339  *   - 0: Success; water mark changed.
340  *   - -EINVAL: Invalid water mark value.
341  */
342 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
343
344 /**
345  * Dump the status of the ring to a file.
346  *
347  * @param f
348  *   A pointer to a file for output
349  * @param r
350  *   A pointer to the ring structure.
351  */
352 void rte_ring_dump(FILE *f, const struct rte_ring *r);
353
354 /* the actual enqueue of pointers on the ring.
355  * Placed here since identical code needed in both
356  * single and multi producer enqueue functions */
357 #define ENQUEUE_PTRS() do { \
358         const uint32_t size = r->prod.size; \
359         uint32_t idx = prod_head & mask; \
360         if (likely(idx + n < size)) { \
361                 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
362                         r->ring[idx] = obj_table[i]; \
363                         r->ring[idx+1] = obj_table[i+1]; \
364                         r->ring[idx+2] = obj_table[i+2]; \
365                         r->ring[idx+3] = obj_table[i+3]; \
366                 } \
367                 switch (n & 0x3) { \
368                         case 3: r->ring[idx++] = obj_table[i++]; \
369                         case 2: r->ring[idx++] = obj_table[i++]; \
370                         case 1: r->ring[idx++] = obj_table[i++]; \
371                 } \
372         } else { \
373                 for (i = 0; idx < size; i++, idx++)\
374                         r->ring[idx] = obj_table[i]; \
375                 for (idx = 0; i < n; i++, idx++) \
376                         r->ring[idx] = obj_table[i]; \
377         } \
378 } while(0)
379
380 /* the actual copy of pointers on the ring to obj_table.
381  * Placed here since identical code needed in both
382  * single and multi consumer dequeue functions */
383 #define DEQUEUE_PTRS() do { \
384         uint32_t idx = cons_head & mask; \
385         const uint32_t size = r->cons.size; \
386         if (likely(idx + n < size)) { \
387                 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
388                         obj_table[i] = r->ring[idx]; \
389                         obj_table[i+1] = r->ring[idx+1]; \
390                         obj_table[i+2] = r->ring[idx+2]; \
391                         obj_table[i+3] = r->ring[idx+3]; \
392                 } \
393                 switch (n & 0x3) { \
394                         case 3: obj_table[i++] = r->ring[idx++]; \
395                         case 2: obj_table[i++] = r->ring[idx++]; \
396                         case 1: obj_table[i++] = r->ring[idx++]; \
397                 } \
398         } else { \
399                 for (i = 0; idx < size; i++, idx++) \
400                         obj_table[i] = r->ring[idx]; \
401                 for (idx = 0; i < n; i++, idx++) \
402                         obj_table[i] = r->ring[idx]; \
403         } \
404 } while (0)
405
406 /**
407  * @internal Enqueue several objects on the ring (multi-producers safe).
408  *
409  * This function uses a "compare and set" instruction to move the
410  * producer index atomically.
411  *
412  * @param r
413  *   A pointer to the ring structure.
414  * @param obj_table
415  *   A pointer to a table of void * pointers (objects).
416  * @param n
417  *   The number of objects to add in the ring from the obj_table.
418  * @param behavior
419  *   RTE_RING_QUEUE_FIXED:    Enqueue a fixed number of items from a ring
420  *   RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
421  * @return
422  *   Depend on the behavior value
423  *   if behavior = RTE_RING_QUEUE_FIXED
424  *   - 0: Success; objects enqueue.
425  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
426  *     high water mark is exceeded.
427  *   - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
428  *   if behavior = RTE_RING_QUEUE_VARIABLE
429  *   - n: Actual number of objects enqueued.
430  */
431 static inline int __attribute__((always_inline))
432 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
433                          unsigned n, enum rte_ring_queue_behavior behavior)
434 {
435         uint32_t prod_head, prod_next;
436         uint32_t cons_tail, free_entries;
437         const unsigned max = n;
438         int success;
439         unsigned i, rep = 0;
440         uint32_t mask = r->prod.mask;
441         int ret;
442
443         /* Avoid the unnecessary cmpset operation below, which is also
444          * potentially harmful when n equals 0. */
445         if (n == 0)
446                 return 0;
447
448         /* move prod.head atomically */
449         do {
450                 /* Reset n to the initial burst count */
451                 n = max;
452
453                 prod_head = r->prod.head;
454                 cons_tail = r->cons.tail;
455                 /* The subtraction is done between two unsigned 32bits value
456                  * (the result is always modulo 32 bits even if we have
457                  * prod_head > cons_tail). So 'free_entries' is always between 0
458                  * and size(ring)-1. */
459                 free_entries = (mask + cons_tail - prod_head);
460
461                 /* check that we have enough room in ring */
462                 if (unlikely(n > free_entries)) {
463                         if (behavior == RTE_RING_QUEUE_FIXED) {
464                                 __RING_STAT_ADD(r, enq_fail, n);
465                                 return -ENOBUFS;
466                         }
467                         else {
468                                 /* No free entry available */
469                                 if (unlikely(free_entries == 0)) {
470                                         __RING_STAT_ADD(r, enq_fail, n);
471                                         return 0;
472                                 }
473
474                                 n = free_entries;
475                         }
476                 }
477
478                 prod_next = prod_head + n;
479                 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
480                                               prod_next);
481         } while (unlikely(success == 0));
482
483         /* write entries in ring */
484         ENQUEUE_PTRS();
485         rte_smp_wmb();
486
487         /* if we exceed the watermark */
488         if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
489                 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
490                                 (int)(n | RTE_RING_QUOT_EXCEED);
491                 __RING_STAT_ADD(r, enq_quota, n);
492         }
493         else {
494                 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
495                 __RING_STAT_ADD(r, enq_success, n);
496         }
497
498         /*
499          * If there are other enqueues in progress that preceded us,
500          * we need to wait for them to complete
501          */
502         while (unlikely(r->prod.tail != prod_head)) {
503                 rte_pause();
504
505                 /* Set RTE_RING_PAUSE_REP_COUNT to avoid spin too long waiting
506                  * for other thread finish. It gives pre-empted thread a chance
507                  * to proceed and finish with ring dequeue operation. */
508                 if (RTE_RING_PAUSE_REP_COUNT &&
509                     ++rep == RTE_RING_PAUSE_REP_COUNT) {
510                         rep = 0;
511                         sched_yield();
512                 }
513         }
514         r->prod.tail = prod_next;
515         return ret;
516 }
517
518 /**
519  * @internal Enqueue several objects on a ring (NOT multi-producers safe).
520  *
521  * @param r
522  *   A pointer to the ring structure.
523  * @param obj_table
524  *   A pointer to a table of void * pointers (objects).
525  * @param n
526  *   The number of objects to add in the ring from the obj_table.
527  * @param behavior
528  *   RTE_RING_QUEUE_FIXED:    Enqueue a fixed number of items from a ring
529  *   RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
530  * @return
531  *   Depend on the behavior value
532  *   if behavior = RTE_RING_QUEUE_FIXED
533  *   - 0: Success; objects enqueue.
534  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
535  *     high water mark is exceeded.
536  *   - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
537  *   if behavior = RTE_RING_QUEUE_VARIABLE
538  *   - n: Actual number of objects enqueued.
539  */
540 static inline int __attribute__((always_inline))
541 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
542                          unsigned n, enum rte_ring_queue_behavior behavior)
543 {
544         uint32_t prod_head, cons_tail;
545         uint32_t prod_next, free_entries;
546         unsigned i;
547         uint32_t mask = r->prod.mask;
548         int ret;
549
550         prod_head = r->prod.head;
551         cons_tail = r->cons.tail;
552         /* The subtraction is done between two unsigned 32bits value
553          * (the result is always modulo 32 bits even if we have
554          * prod_head > cons_tail). So 'free_entries' is always between 0
555          * and size(ring)-1. */
556         free_entries = mask + cons_tail - prod_head;
557
558         /* check that we have enough room in ring */
559         if (unlikely(n > free_entries)) {
560                 if (behavior == RTE_RING_QUEUE_FIXED) {
561                         __RING_STAT_ADD(r, enq_fail, n);
562                         return -ENOBUFS;
563                 }
564                 else {
565                         /* No free entry available */
566                         if (unlikely(free_entries == 0)) {
567                                 __RING_STAT_ADD(r, enq_fail, n);
568                                 return 0;
569                         }
570
571                         n = free_entries;
572                 }
573         }
574
575         prod_next = prod_head + n;
576         r->prod.head = prod_next;
577
578         /* write entries in ring */
579         ENQUEUE_PTRS();
580         rte_smp_wmb();
581
582         /* if we exceed the watermark */
583         if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
584                 ret = (behavior == RTE_RING_QUEUE_FIXED) ? -EDQUOT :
585                         (int)(n | RTE_RING_QUOT_EXCEED);
586                 __RING_STAT_ADD(r, enq_quota, n);
587         }
588         else {
589                 ret = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : n;
590                 __RING_STAT_ADD(r, enq_success, n);
591         }
592
593         r->prod.tail = prod_next;
594         return ret;
595 }
596
597 /**
598  * @internal Dequeue several objects from a ring (multi-consumers safe). When
599  * the request objects are more than the available objects, only dequeue the
600  * actual number of objects
601  *
602  * This function uses a "compare and set" instruction to move the
603  * consumer index atomically.
604  *
605  * @param r
606  *   A pointer to the ring structure.
607  * @param obj_table
608  *   A pointer to a table of void * pointers (objects) that will be filled.
609  * @param n
610  *   The number of objects to dequeue from the ring to the obj_table.
611  * @param behavior
612  *   RTE_RING_QUEUE_FIXED:    Dequeue a fixed number of items from a ring
613  *   RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
614  * @return
615  *   Depend on the behavior value
616  *   if behavior = RTE_RING_QUEUE_FIXED
617  *   - 0: Success; objects dequeued.
618  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
619  *     dequeued.
620  *   if behavior = RTE_RING_QUEUE_VARIABLE
621  *   - n: Actual number of objects dequeued.
622  */
623
624 static inline int __attribute__((always_inline))
625 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
626                  unsigned n, enum rte_ring_queue_behavior behavior)
627 {
628         uint32_t cons_head, prod_tail;
629         uint32_t cons_next, entries;
630         const unsigned max = n;
631         int success;
632         unsigned i, rep = 0;
633         uint32_t mask = r->prod.mask;
634
635         /* Avoid the unnecessary cmpset operation below, which is also
636          * potentially harmful when n equals 0. */
637         if (n == 0)
638                 return 0;
639
640         /* move cons.head atomically */
641         do {
642                 /* Restore n as it may change every loop */
643                 n = max;
644
645                 cons_head = r->cons.head;
646                 prod_tail = r->prod.tail;
647                 /* The subtraction is done between two unsigned 32bits value
648                  * (the result is always modulo 32 bits even if we have
649                  * cons_head > prod_tail). So 'entries' is always between 0
650                  * and size(ring)-1. */
651                 entries = (prod_tail - cons_head);
652
653                 /* Set the actual entries for dequeue */
654                 if (n > entries) {
655                         if (behavior == RTE_RING_QUEUE_FIXED) {
656                                 __RING_STAT_ADD(r, deq_fail, n);
657                                 return -ENOENT;
658                         }
659                         else {
660                                 if (unlikely(entries == 0)){
661                                         __RING_STAT_ADD(r, deq_fail, n);
662                                         return 0;
663                                 }
664
665                                 n = entries;
666                         }
667                 }
668
669                 cons_next = cons_head + n;
670                 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
671                                               cons_next);
672         } while (unlikely(success == 0));
673
674         /* copy in table */
675         DEQUEUE_PTRS();
676         rte_smp_rmb();
677
678         /*
679          * If there are other dequeues in progress that preceded us,
680          * we need to wait for them to complete
681          */
682         while (unlikely(r->cons.tail != cons_head)) {
683                 rte_pause();
684
685                 /* Set RTE_RING_PAUSE_REP_COUNT to avoid spin too long waiting
686                  * for other thread finish. It gives pre-empted thread a chance
687                  * to proceed and finish with ring dequeue operation. */
688                 if (RTE_RING_PAUSE_REP_COUNT &&
689                     ++rep == RTE_RING_PAUSE_REP_COUNT) {
690                         rep = 0;
691                         sched_yield();
692                 }
693         }
694         __RING_STAT_ADD(r, deq_success, n);
695         r->cons.tail = cons_next;
696
697         return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
698 }
699
700 /**
701  * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
702  * When the request objects are more than the available objects, only dequeue
703  * the actual number of objects
704  *
705  * @param r
706  *   A pointer to the ring structure.
707  * @param obj_table
708  *   A pointer to a table of void * pointers (objects) that will be filled.
709  * @param n
710  *   The number of objects to dequeue from the ring to the obj_table.
711  * @param behavior
712  *   RTE_RING_QUEUE_FIXED:    Dequeue a fixed number of items from a ring
713  *   RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
714  * @return
715  *   Depend on the behavior value
716  *   if behavior = RTE_RING_QUEUE_FIXED
717  *   - 0: Success; objects dequeued.
718  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
719  *     dequeued.
720  *   if behavior = RTE_RING_QUEUE_VARIABLE
721  *   - n: Actual number of objects dequeued.
722  */
723 static inline int __attribute__((always_inline))
724 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
725                  unsigned n, enum rte_ring_queue_behavior behavior)
726 {
727         uint32_t cons_head, prod_tail;
728         uint32_t cons_next, entries;
729         unsigned i;
730         uint32_t mask = r->prod.mask;
731
732         cons_head = r->cons.head;
733         prod_tail = r->prod.tail;
734         /* The subtraction is done between two unsigned 32bits value
735          * (the result is always modulo 32 bits even if we have
736          * cons_head > prod_tail). So 'entries' is always between 0
737          * and size(ring)-1. */
738         entries = prod_tail - cons_head;
739
740         if (n > entries) {
741                 if (behavior == RTE_RING_QUEUE_FIXED) {
742                         __RING_STAT_ADD(r, deq_fail, n);
743                         return -ENOENT;
744                 }
745                 else {
746                         if (unlikely(entries == 0)){
747                                 __RING_STAT_ADD(r, deq_fail, n);
748                                 return 0;
749                         }
750
751                         n = entries;
752                 }
753         }
754
755         cons_next = cons_head + n;
756         r->cons.head = cons_next;
757
758         /* copy in table */
759         DEQUEUE_PTRS();
760         rte_smp_rmb();
761
762         __RING_STAT_ADD(r, deq_success, n);
763         r->cons.tail = cons_next;
764         return behavior == RTE_RING_QUEUE_FIXED ? 0 : n;
765 }
766
767 /**
768  * Enqueue several objects on the ring (multi-producers safe).
769  *
770  * This function uses a "compare and set" instruction to move the
771  * producer index atomically.
772  *
773  * @param r
774  *   A pointer to the ring structure.
775  * @param obj_table
776  *   A pointer to a table of void * pointers (objects).
777  * @param n
778  *   The number of objects to add in the ring from the obj_table.
779  * @return
780  *   - 0: Success; objects enqueue.
781  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
782  *     high water mark is exceeded.
783  *   - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
784  */
785 static inline int __attribute__((always_inline))
786 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
787                          unsigned n)
788 {
789         return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
790 }
791
792 /**
793  * Enqueue several objects on a ring (NOT multi-producers safe).
794  *
795  * @param r
796  *   A pointer to the ring structure.
797  * @param obj_table
798  *   A pointer to a table of void * pointers (objects).
799  * @param n
800  *   The number of objects to add in the ring from the obj_table.
801  * @return
802  *   - 0: Success; objects enqueued.
803  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
804  *     high water mark is exceeded.
805  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
806  */
807 static inline int __attribute__((always_inline))
808 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
809                          unsigned n)
810 {
811         return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
812 }
813
814 /**
815  * Enqueue several objects on a ring.
816  *
817  * This function calls the multi-producer or the single-producer
818  * version depending on the default behavior that was specified at
819  * ring creation time (see flags).
820  *
821  * @param r
822  *   A pointer to the ring structure.
823  * @param obj_table
824  *   A pointer to a table of void * pointers (objects).
825  * @param n
826  *   The number of objects to add in the ring from the obj_table.
827  * @return
828  *   - 0: Success; objects enqueued.
829  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
830  *     high water mark is exceeded.
831  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
832  */
833 static inline int __attribute__((always_inline))
834 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
835                       unsigned n)
836 {
837         if (r->prod.single)
838                 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
839         else
840                 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
841 }
842
843 /**
844  * Enqueue one object on a ring (multi-producers safe).
845  *
846  * This function uses a "compare and set" instruction to move the
847  * producer index atomically.
848  *
849  * @param r
850  *   A pointer to the ring structure.
851  * @param obj
852  *   A pointer to the object to be added.
853  * @return
854  *   - 0: Success; objects enqueued.
855  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
856  *     high water mark is exceeded.
857  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
858  */
859 static inline int __attribute__((always_inline))
860 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
861 {
862         return rte_ring_mp_enqueue_bulk(r, &obj, 1);
863 }
864
865 /**
866  * Enqueue one object on a ring (NOT multi-producers safe).
867  *
868  * @param r
869  *   A pointer to the ring structure.
870  * @param obj
871  *   A pointer to the object to be added.
872  * @return
873  *   - 0: Success; objects enqueued.
874  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
875  *     high water mark is exceeded.
876  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
877  */
878 static inline int __attribute__((always_inline))
879 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
880 {
881         return rte_ring_sp_enqueue_bulk(r, &obj, 1);
882 }
883
884 /**
885  * Enqueue one object on a ring.
886  *
887  * This function calls the multi-producer or the single-producer
888  * version, depending on the default behaviour that was specified at
889  * ring creation time (see flags).
890  *
891  * @param r
892  *   A pointer to the ring structure.
893  * @param obj
894  *   A pointer to the object to be added.
895  * @return
896  *   - 0: Success; objects enqueued.
897  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
898  *     high water mark is exceeded.
899  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
900  */
901 static inline int __attribute__((always_inline))
902 rte_ring_enqueue(struct rte_ring *r, void *obj)
903 {
904         if (r->prod.single)
905                 return rte_ring_sp_enqueue(r, obj);
906         else
907                 return rte_ring_mp_enqueue(r, obj);
908 }
909
910 /**
911  * Dequeue several objects from a ring (multi-consumers safe).
912  *
913  * This function uses a "compare and set" instruction to move the
914  * consumer index atomically.
915  *
916  * @param r
917  *   A pointer to the ring structure.
918  * @param obj_table
919  *   A pointer to a table of void * pointers (objects) that will be filled.
920  * @param n
921  *   The number of objects to dequeue from the ring to the obj_table.
922  * @return
923  *   - 0: Success; objects dequeued.
924  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
925  *     dequeued.
926  */
927 static inline int __attribute__((always_inline))
928 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
929 {
930         return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
931 }
932
933 /**
934  * Dequeue several objects from a ring (NOT multi-consumers safe).
935  *
936  * @param r
937  *   A pointer to the ring structure.
938  * @param obj_table
939  *   A pointer to a table of void * pointers (objects) that will be filled.
940  * @param n
941  *   The number of objects to dequeue from the ring to the obj_table,
942  *   must be strictly positive.
943  * @return
944  *   - 0: Success; objects dequeued.
945  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
946  *     dequeued.
947  */
948 static inline int __attribute__((always_inline))
949 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
950 {
951         return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED);
952 }
953
954 /**
955  * Dequeue several objects from a ring.
956  *
957  * This function calls the multi-consumers or the single-consumer
958  * version, depending on the default behaviour that was specified at
959  * ring creation time (see flags).
960  *
961  * @param r
962  *   A pointer to the ring structure.
963  * @param obj_table
964  *   A pointer to a table of void * pointers (objects) that will be filled.
965  * @param n
966  *   The number of objects to dequeue from the ring to the obj_table.
967  * @return
968  *   - 0: Success; objects dequeued.
969  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
970  *     dequeued.
971  */
972 static inline int __attribute__((always_inline))
973 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
974 {
975         if (r->cons.single)
976                 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
977         else
978                 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
979 }
980
981 /**
982  * Dequeue one object from a ring (multi-consumers safe).
983  *
984  * This function uses a "compare and set" instruction to move the
985  * consumer index atomically.
986  *
987  * @param r
988  *   A pointer to the ring structure.
989  * @param obj_p
990  *   A pointer to a void * pointer (object) that will be filled.
991  * @return
992  *   - 0: Success; objects dequeued.
993  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
994  *     dequeued.
995  */
996 static inline int __attribute__((always_inline))
997 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
998 {
999         return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
1000 }
1001
1002 /**
1003  * Dequeue one object from a ring (NOT multi-consumers safe).
1004  *
1005  * @param r
1006  *   A pointer to the ring structure.
1007  * @param obj_p
1008  *   A pointer to a void * pointer (object) that will be filled.
1009  * @return
1010  *   - 0: Success; objects dequeued.
1011  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
1012  *     dequeued.
1013  */
1014 static inline int __attribute__((always_inline))
1015 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
1016 {
1017         return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
1018 }
1019
1020 /**
1021  * Dequeue one object from a ring.
1022  *
1023  * This function calls the multi-consumers or the single-consumer
1024  * version depending on the default behaviour that was specified at
1025  * ring creation time (see flags).
1026  *
1027  * @param r
1028  *   A pointer to the ring structure.
1029  * @param obj_p
1030  *   A pointer to a void * pointer (object) that will be filled.
1031  * @return
1032  *   - 0: Success, objects dequeued.
1033  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
1034  *     dequeued.
1035  */
1036 static inline int __attribute__((always_inline))
1037 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
1038 {
1039         if (r->cons.single)
1040                 return rte_ring_sc_dequeue(r, obj_p);
1041         else
1042                 return rte_ring_mc_dequeue(r, obj_p);
1043 }
1044
1045 /**
1046  * Test if a ring is full.
1047  *
1048  * @param r
1049  *   A pointer to the ring structure.
1050  * @return
1051  *   - 1: The ring is full.
1052  *   - 0: The ring is not full.
1053  */
1054 static inline int
1055 rte_ring_full(const struct rte_ring *r)
1056 {
1057         uint32_t prod_tail = r->prod.tail;
1058         uint32_t cons_tail = r->cons.tail;
1059         return ((cons_tail - prod_tail - 1) & r->prod.mask) == 0;
1060 }
1061
1062 /**
1063  * Test if a ring is empty.
1064  *
1065  * @param r
1066  *   A pointer to the ring structure.
1067  * @return
1068  *   - 1: The ring is empty.
1069  *   - 0: The ring is not empty.
1070  */
1071 static inline int
1072 rte_ring_empty(const struct rte_ring *r)
1073 {
1074         uint32_t prod_tail = r->prod.tail;
1075         uint32_t cons_tail = r->cons.tail;
1076         return !!(cons_tail == prod_tail);
1077 }
1078
1079 /**
1080  * Return the number of entries in a ring.
1081  *
1082  * @param r
1083  *   A pointer to the ring structure.
1084  * @return
1085  *   The number of entries in the ring.
1086  */
1087 static inline unsigned
1088 rte_ring_count(const struct rte_ring *r)
1089 {
1090         uint32_t prod_tail = r->prod.tail;
1091         uint32_t cons_tail = r->cons.tail;
1092         return (prod_tail - cons_tail) & r->prod.mask;
1093 }
1094
1095 /**
1096  * Return the number of free entries in a ring.
1097  *
1098  * @param r
1099  *   A pointer to the ring structure.
1100  * @return
1101  *   The number of free entries in the ring.
1102  */
1103 static inline unsigned
1104 rte_ring_free_count(const struct rte_ring *r)
1105 {
1106         uint32_t prod_tail = r->prod.tail;
1107         uint32_t cons_tail = r->cons.tail;
1108         return (cons_tail - prod_tail - 1) & r->prod.mask;
1109 }
1110
1111 /**
1112  * Return the size of the ring.
1113  *
1114  * @param r
1115  *   A pointer to the ring structure.
1116  * @return
1117  *   The number of elements which can be stored in the ring.
1118  */
1119 static inline unsigned int
1120 rte_ring_get_size(const struct rte_ring *r)
1121 {
1122         return r->prod.size;
1123 }
1124
1125 /**
1126  * Dump the status of all rings on the console
1127  *
1128  * @param f
1129  *   A pointer to a file for output
1130  */
1131 void rte_ring_list_dump(FILE *f);
1132
1133 /**
1134  * Search a ring from its name
1135  *
1136  * @param name
1137  *   The name of the ring.
1138  * @return
1139  *   The pointer to the ring matching the name, or NULL if not found,
1140  *   with rte_errno set appropriately. Possible rte_errno values include:
1141  *    - ENOENT - required entry not available to return.
1142  */
1143 struct rte_ring *rte_ring_lookup(const char *name);
1144
1145 /**
1146  * Enqueue several objects on the ring (multi-producers safe).
1147  *
1148  * This function uses a "compare and set" instruction to move the
1149  * producer index atomically.
1150  *
1151  * @param r
1152  *   A pointer to the ring structure.
1153  * @param obj_table
1154  *   A pointer to a table of void * pointers (objects).
1155  * @param n
1156  *   The number of objects to add in the ring from the obj_table.
1157  * @return
1158  *   - n: Actual number of objects enqueued.
1159  */
1160 static inline unsigned __attribute__((always_inline))
1161 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1162                          unsigned n)
1163 {
1164         return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1165 }
1166
1167 /**
1168  * Enqueue several objects on a ring (NOT multi-producers safe).
1169  *
1170  * @param r
1171  *   A pointer to the ring structure.
1172  * @param obj_table
1173  *   A pointer to a table of void * pointers (objects).
1174  * @param n
1175  *   The number of objects to add in the ring from the obj_table.
1176  * @return
1177  *   - n: Actual number of objects enqueued.
1178  */
1179 static inline unsigned __attribute__((always_inline))
1180 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1181                          unsigned n)
1182 {
1183         return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1184 }
1185
1186 /**
1187  * Enqueue several objects on a ring.
1188  *
1189  * This function calls the multi-producer or the single-producer
1190  * version depending on the default behavior that was specified at
1191  * ring creation time (see flags).
1192  *
1193  * @param r
1194  *   A pointer to the ring structure.
1195  * @param obj_table
1196  *   A pointer to a table of void * pointers (objects).
1197  * @param n
1198  *   The number of objects to add in the ring from the obj_table.
1199  * @return
1200  *   - n: Actual number of objects enqueued.
1201  */
1202 static inline unsigned __attribute__((always_inline))
1203 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1204                       unsigned n)
1205 {
1206         if (r->prod.single)
1207                 return rte_ring_sp_enqueue_burst(r, obj_table, n);
1208         else
1209                 return rte_ring_mp_enqueue_burst(r, obj_table, n);
1210 }
1211
1212 /**
1213  * Dequeue several objects from a ring (multi-consumers safe). When the request
1214  * objects are more than the available objects, only dequeue the actual number
1215  * of objects
1216  *
1217  * This function uses a "compare and set" instruction to move the
1218  * consumer index atomically.
1219  *
1220  * @param r
1221  *   A pointer to the ring structure.
1222  * @param obj_table
1223  *   A pointer to a table of void * pointers (objects) that will be filled.
1224  * @param n
1225  *   The number of objects to dequeue from the ring to the obj_table.
1226  * @return
1227  *   - n: Actual number of objects dequeued, 0 if ring is empty
1228  */
1229 static inline unsigned __attribute__((always_inline))
1230 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1231 {
1232         return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1233 }
1234
1235 /**
1236  * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1237  * request objects are more than the available objects, only dequeue the
1238  * actual number of objects
1239  *
1240  * @param r
1241  *   A pointer to the ring structure.
1242  * @param obj_table
1243  *   A pointer to a table of void * pointers (objects) that will be filled.
1244  * @param n
1245  *   The number of objects to dequeue from the ring to the obj_table.
1246  * @return
1247  *   - n: Actual number of objects dequeued, 0 if ring is empty
1248  */
1249 static inline unsigned __attribute__((always_inline))
1250 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1251 {
1252         return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE);
1253 }
1254
1255 /**
1256  * Dequeue multiple objects from a ring up to a maximum number.
1257  *
1258  * This function calls the multi-consumers or the single-consumer
1259  * version, depending on the default behaviour that was specified at
1260  * ring creation time (see flags).
1261  *
1262  * @param r
1263  *   A pointer to the ring structure.
1264  * @param obj_table
1265  *   A pointer to a table of void * pointers (objects) that will be filled.
1266  * @param n
1267  *   The number of objects to dequeue from the ring to the obj_table.
1268  * @return
1269  *   - Number of objects dequeued
1270  */
1271 static inline unsigned __attribute__((always_inline))
1272 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned n)
1273 {
1274         if (r->cons.single)
1275                 return rte_ring_sc_dequeue_burst(r, obj_table, n);
1276         else
1277                 return rte_ring_mc_dequeue_burst(r, obj_table, n);
1278 }
1279
1280 #ifdef __cplusplus
1281 }
1282 #endif
1283
1284 #endif /* _RTE_RING_H_ */