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