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