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