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