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