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