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