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