07dc89583f85a31301bfb3b3355343facde8b251
[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         unsigned int i; \
289         const uint32_t size = r->size; \
290         uint32_t idx = prod_head & mask; \
291         if (likely(idx + n < size)) { \
292                 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
293                         r->ring[idx] = obj_table[i]; \
294                         r->ring[idx+1] = obj_table[i+1]; \
295                         r->ring[idx+2] = obj_table[i+2]; \
296                         r->ring[idx+3] = obj_table[i+3]; \
297                 } \
298                 switch (n & 0x3) { \
299                         case 3: r->ring[idx++] = obj_table[i++]; \
300                         case 2: r->ring[idx++] = obj_table[i++]; \
301                         case 1: r->ring[idx++] = obj_table[i++]; \
302                 } \
303         } else { \
304                 for (i = 0; idx < size; i++, idx++)\
305                         r->ring[idx] = obj_table[i]; \
306                 for (idx = 0; i < n; i++, idx++) \
307                         r->ring[idx] = obj_table[i]; \
308         } \
309 } while(0)
310
311 /* the actual copy of pointers on the ring to obj_table.
312  * Placed here since identical code needed in both
313  * single and multi consumer dequeue functions */
314 #define DEQUEUE_PTRS() do { \
315         unsigned int i; \
316         uint32_t idx = cons_head & mask; \
317         const uint32_t size = r->size; \
318         if (likely(idx + n < size)) { \
319                 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
320                         obj_table[i] = r->ring[idx]; \
321                         obj_table[i+1] = r->ring[idx+1]; \
322                         obj_table[i+2] = r->ring[idx+2]; \
323                         obj_table[i+3] = r->ring[idx+3]; \
324                 } \
325                 switch (n & 0x3) { \
326                         case 3: obj_table[i++] = r->ring[idx++]; \
327                         case 2: obj_table[i++] = r->ring[idx++]; \
328                         case 1: obj_table[i++] = r->ring[idx++]; \
329                 } \
330         } else { \
331                 for (i = 0; idx < size; i++, idx++) \
332                         obj_table[i] = r->ring[idx]; \
333                 for (idx = 0; i < n; i++, idx++) \
334                         obj_table[i] = r->ring[idx]; \
335         } \
336 } while (0)
337
338 /**
339  * @internal Enqueue several objects on the ring (multi-producers safe).
340  *
341  * This function uses a "compare and set" instruction to move the
342  * producer index atomically.
343  *
344  * @param r
345  *   A pointer to the ring structure.
346  * @param obj_table
347  *   A pointer to a table of void * pointers (objects).
348  * @param n
349  *   The number of objects to add in the ring from the obj_table.
350  * @param behavior
351  *   RTE_RING_QUEUE_FIXED:    Enqueue a fixed number of items from a ring
352  *   RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
353  * @return
354  *   Actual number of objects enqueued.
355  *   If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
356  */
357 static inline unsigned int __attribute__((always_inline))
358 __rte_ring_mp_do_enqueue(struct rte_ring *r, void * const *obj_table,
359                          unsigned int n, enum rte_ring_queue_behavior behavior,
360                          unsigned int *free_space)
361 {
362         uint32_t prod_head, prod_next;
363         uint32_t cons_tail, free_entries;
364         const unsigned int max = n;
365         int success;
366         uint32_t mask = r->mask;
367
368         /* move prod.head atomically */
369         do {
370                 /* Reset n to the initial burst count */
371                 n = max;
372
373                 prod_head = r->prod.head;
374                 cons_tail = r->cons.tail;
375                 /* The subtraction is done between two unsigned 32bits value
376                  * (the result is always modulo 32 bits even if we have
377                  * prod_head > cons_tail). So 'free_entries' is always between 0
378                  * and size(ring)-1. */
379                 free_entries = (mask + cons_tail - prod_head);
380
381                 /* check that we have enough room in ring */
382                 if (unlikely(n > free_entries))
383                         n = (behavior == RTE_RING_QUEUE_FIXED) ?
384                                         0 : free_entries;
385
386                 if (n == 0)
387                         goto end;
388
389                 prod_next = prod_head + n;
390                 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
391                                               prod_next);
392         } while (unlikely(success == 0));
393
394         /* write entries in ring */
395         ENQUEUE_PTRS();
396         rte_smp_wmb();
397
398         /*
399          * If there are other enqueues in progress that preceded us,
400          * we need to wait for them to complete
401          */
402         while (unlikely(r->prod.tail != prod_head))
403                 rte_pause();
404
405         r->prod.tail = prod_next;
406 end:
407         if (free_space != NULL)
408                 *free_space = free_entries - n;
409         return n;
410 }
411
412 /**
413  * @internal Enqueue several objects on a ring (NOT multi-producers safe).
414  *
415  * @param r
416  *   A pointer to the ring structure.
417  * @param obj_table
418  *   A pointer to a table of void * pointers (objects).
419  * @param n
420  *   The number of objects to add in the ring from the obj_table.
421  * @param behavior
422  *   RTE_RING_QUEUE_FIXED:    Enqueue a fixed number of items from a ring
423  *   RTE_RING_QUEUE_VARIABLE: Enqueue as many items a possible from ring
424  * @return
425  *   Actual number of objects enqueued.
426  *   If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
427  */
428 static inline unsigned int __attribute__((always_inline))
429 __rte_ring_sp_do_enqueue(struct rte_ring *r, void * const *obj_table,
430                          unsigned int n, enum rte_ring_queue_behavior behavior,
431                          unsigned int *free_space)
432 {
433         uint32_t prod_head, cons_tail;
434         uint32_t prod_next, free_entries;
435         uint32_t mask = r->mask;
436
437         prod_head = r->prod.head;
438         cons_tail = r->cons.tail;
439         /* The subtraction is done between two unsigned 32bits value
440          * (the result is always modulo 32 bits even if we have
441          * prod_head > cons_tail). So 'free_entries' is always between 0
442          * and size(ring)-1. */
443         free_entries = mask + cons_tail - prod_head;
444
445         /* check that we have enough room in ring */
446         if (unlikely(n > free_entries))
447                 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : free_entries;
448
449         if (n == 0)
450                 goto end;
451
452
453         prod_next = prod_head + n;
454         r->prod.head = prod_next;
455
456         /* write entries in ring */
457         ENQUEUE_PTRS();
458         rte_smp_wmb();
459
460         r->prod.tail = prod_next;
461 end:
462         if (free_space != NULL)
463                 *free_space = free_entries - n;
464         return n;
465 }
466
467 /**
468  * @internal Dequeue several objects from a ring (multi-consumers safe). When
469  * the request objects are more than the available objects, only dequeue the
470  * actual number of objects
471  *
472  * This function uses a "compare and set" instruction to move the
473  * consumer index atomically.
474  *
475  * @param r
476  *   A pointer to the ring structure.
477  * @param obj_table
478  *   A pointer to a table of void * pointers (objects) that will be filled.
479  * @param n
480  *   The number of objects to dequeue from the ring to the obj_table.
481  * @param behavior
482  *   RTE_RING_QUEUE_FIXED:    Dequeue a fixed number of items from a ring
483  *   RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
484  * @return
485  *   - Actual number of objects dequeued.
486  *     If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
487  */
488
489 static inline unsigned int __attribute__((always_inline))
490 __rte_ring_mc_do_dequeue(struct rte_ring *r, void **obj_table,
491                  unsigned int n, enum rte_ring_queue_behavior behavior,
492                  unsigned int *available)
493 {
494         uint32_t cons_head, prod_tail;
495         uint32_t cons_next, entries;
496         const unsigned max = n;
497         int success;
498         uint32_t mask = r->mask;
499
500         /* move cons.head atomically */
501         do {
502                 /* Restore n as it may change every loop */
503                 n = max;
504
505                 cons_head = r->cons.head;
506                 prod_tail = r->prod.tail;
507                 /* The subtraction is done between two unsigned 32bits value
508                  * (the result is always modulo 32 bits even if we have
509                  * cons_head > prod_tail). So 'entries' is always between 0
510                  * and size(ring)-1. */
511                 entries = (prod_tail - cons_head);
512
513                 /* Set the actual entries for dequeue */
514                 if (n > entries)
515                         n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : entries;
516
517                 if (unlikely(n == 0))
518                         goto end;
519
520                 cons_next = cons_head + n;
521                 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
522                                               cons_next);
523         } while (unlikely(success == 0));
524
525         /* copy in table */
526         DEQUEUE_PTRS();
527         rte_smp_rmb();
528
529         /*
530          * If there are other dequeues in progress that preceded us,
531          * we need to wait for them to complete
532          */
533         while (unlikely(r->cons.tail != cons_head))
534                 rte_pause();
535
536         r->cons.tail = cons_next;
537 end:
538         if (available != NULL)
539                 *available = entries - n;
540         return n;
541 }
542
543 /**
544  * @internal Dequeue several objects from a ring (NOT multi-consumers safe).
545  * When the request objects are more than the available objects, only dequeue
546  * the actual number of objects
547  *
548  * @param r
549  *   A pointer to the ring structure.
550  * @param obj_table
551  *   A pointer to a table of void * pointers (objects) that will be filled.
552  * @param n
553  *   The number of objects to dequeue from the ring to the obj_table.
554  * @param behavior
555  *   RTE_RING_QUEUE_FIXED:    Dequeue a fixed number of items from a ring
556  *   RTE_RING_QUEUE_VARIABLE: Dequeue as many items a possible from ring
557  * @return
558  *   - Actual number of objects dequeued.
559  *     If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
560  */
561 static inline unsigned int __attribute__((always_inline))
562 __rte_ring_sc_do_dequeue(struct rte_ring *r, void **obj_table,
563                  unsigned int n, enum rte_ring_queue_behavior behavior,
564                  unsigned int *available)
565 {
566         uint32_t cons_head, prod_tail;
567         uint32_t cons_next, entries;
568         uint32_t mask = r->mask;
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         if (n > entries)
579                 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : entries;
580
581         if (unlikely(entries == 0))
582                 goto end;
583
584         cons_next = cons_head + n;
585         r->cons.head = cons_next;
586
587         /* copy in table */
588         DEQUEUE_PTRS();
589         rte_smp_rmb();
590
591         r->cons.tail = cons_next;
592 end:
593         if (available != NULL)
594                 *available = entries - n;
595         return n;
596 }
597
598 /**
599  * Enqueue several objects on the ring (multi-producers safe).
600  *
601  * This function uses a "compare and set" instruction to move the
602  * producer index atomically.
603  *
604  * @param r
605  *   A pointer to the ring structure.
606  * @param obj_table
607  *   A pointer to a table of void * pointers (objects).
608  * @param n
609  *   The number of objects to add in the ring from the obj_table.
610  * @param free_space
611  *   if non-NULL, returns the amount of space in the ring after the
612  *   enqueue operation has finished.
613  * @return
614  *   The number of objects enqueued, either 0 or n
615  */
616 static inline unsigned int __attribute__((always_inline))
617 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
618                          unsigned int n, unsigned int *free_space)
619 {
620         return __rte_ring_mp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
621                         free_space);
622 }
623
624 /**
625  * Enqueue several objects on a ring (NOT multi-producers safe).
626  *
627  * @param r
628  *   A pointer to the ring structure.
629  * @param obj_table
630  *   A pointer to a table of void * pointers (objects).
631  * @param n
632  *   The number of objects to add in the ring from the obj_table.
633  * @param free_space
634  *   if non-NULL, returns the amount of space in the ring after the
635  *   enqueue operation has finished.
636  * @return
637  *   The number of objects enqueued, either 0 or n
638  */
639 static inline unsigned int __attribute__((always_inline))
640 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
641                          unsigned int n, unsigned int *free_space)
642 {
643         return __rte_ring_sp_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
644                         free_space);
645 }
646
647 /**
648  * Enqueue several objects on a ring.
649  *
650  * This function calls the multi-producer or the single-producer
651  * version depending on the default behavior that was specified at
652  * ring creation time (see flags).
653  *
654  * @param r
655  *   A pointer to the ring structure.
656  * @param obj_table
657  *   A pointer to a table of void * pointers (objects).
658  * @param n
659  *   The number of objects to add in the ring from the obj_table.
660  * @param free_space
661  *   if non-NULL, returns the amount of space in the ring after the
662  *   enqueue operation has finished.
663  * @return
664  *   The number of objects enqueued, either 0 or n
665  */
666 static inline unsigned int __attribute__((always_inline))
667 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
668                       unsigned int n, unsigned int *free_space)
669 {
670         if (r->prod.single)
671                 return rte_ring_sp_enqueue_bulk(r, obj_table, n, free_space);
672         else
673                 return rte_ring_mp_enqueue_bulk(r, obj_table, n, free_space);
674 }
675
676 /**
677  * Enqueue one object on a ring (multi-producers safe).
678  *
679  * This function uses a "compare and set" instruction to move the
680  * producer index atomically.
681  *
682  * @param r
683  *   A pointer to the ring structure.
684  * @param obj
685  *   A pointer to the object to be added.
686  * @return
687  *   - 0: Success; objects enqueued.
688  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
689  */
690 static inline int __attribute__((always_inline))
691 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
692 {
693         return rte_ring_mp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
694 }
695
696 /**
697  * Enqueue one object on a ring (NOT multi-producers safe).
698  *
699  * @param r
700  *   A pointer to the ring structure.
701  * @param obj
702  *   A pointer to the object to be added.
703  * @return
704  *   - 0: Success; objects enqueued.
705  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
706  */
707 static inline int __attribute__((always_inline))
708 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
709 {
710         return rte_ring_sp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
711 }
712
713 /**
714  * Enqueue one object on a ring.
715  *
716  * This function calls the multi-producer or the single-producer
717  * version, depending on the default behaviour that was specified at
718  * ring creation time (see flags).
719  *
720  * @param r
721  *   A pointer to the ring structure.
722  * @param obj
723  *   A pointer to the object to be added.
724  * @return
725  *   - 0: Success; objects enqueued.
726  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
727  */
728 static inline int __attribute__((always_inline))
729 rte_ring_enqueue(struct rte_ring *r, void *obj)
730 {
731         return rte_ring_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
732 }
733
734 /**
735  * Dequeue several objects from a ring (multi-consumers safe).
736  *
737  * This function uses a "compare and set" instruction to move the
738  * consumer index atomically.
739  *
740  * @param r
741  *   A pointer to the ring structure.
742  * @param obj_table
743  *   A pointer to a table of void * pointers (objects) that will be filled.
744  * @param n
745  *   The number of objects to dequeue from the ring to the obj_table.
746  * @param available
747  *   If non-NULL, returns the number of remaining ring entries after the
748  *   dequeue has finished.
749  * @return
750  *   The number of objects dequeued, either 0 or n
751  */
752 static inline unsigned int __attribute__((always_inline))
753 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table,
754                 unsigned int n, unsigned int *available)
755 {
756         return __rte_ring_mc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
757                         available);
758 }
759
760 /**
761  * Dequeue several objects from a ring (NOT multi-consumers safe).
762  *
763  * @param r
764  *   A pointer to the ring structure.
765  * @param obj_table
766  *   A pointer to a table of void * pointers (objects) that will be filled.
767  * @param n
768  *   The number of objects to dequeue from the ring to the obj_table,
769  *   must be strictly positive.
770  * @param available
771  *   If non-NULL, returns the number of remaining ring entries after the
772  *   dequeue has finished.
773  * @return
774  *   The number of objects dequeued, either 0 or n
775  */
776 static inline unsigned int __attribute__((always_inline))
777 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
778                 unsigned int n, unsigned int *available)
779 {
780         return __rte_ring_sc_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
781                         available);
782 }
783
784 /**
785  * Dequeue several objects from a ring.
786  *
787  * This function calls the multi-consumers or the single-consumer
788  * version, depending on the default behaviour that was specified at
789  * ring creation time (see flags).
790  *
791  * @param r
792  *   A pointer to the ring structure.
793  * @param obj_table
794  *   A pointer to a table of void * pointers (objects) that will be filled.
795  * @param n
796  *   The number of objects to dequeue from the ring to the obj_table.
797  * @param available
798  *   If non-NULL, returns the number of remaining ring entries after the
799  *   dequeue has finished.
800  * @return
801  *   The number of objects dequeued, either 0 or n
802  */
803 static inline unsigned int __attribute__((always_inline))
804 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
805                 unsigned int *available)
806 {
807         if (r->cons.single)
808                 return rte_ring_sc_dequeue_bulk(r, obj_table, n, available);
809         else
810                 return rte_ring_mc_dequeue_bulk(r, obj_table, n, available);
811 }
812
813 /**
814  * Dequeue one object from a ring (multi-consumers safe).
815  *
816  * This function uses a "compare and set" instruction to move the
817  * consumer index atomically.
818  *
819  * @param r
820  *   A pointer to the ring structure.
821  * @param obj_p
822  *   A pointer to a void * pointer (object) that will be filled.
823  * @return
824  *   - 0: Success; objects dequeued.
825  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
826  *     dequeued.
827  */
828 static inline int __attribute__((always_inline))
829 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
830 {
831         return rte_ring_mc_dequeue_bulk(r, obj_p, 1, NULL)  ? 0 : -ENOBUFS;
832 }
833
834 /**
835  * Dequeue one object from a ring (NOT multi-consumers safe).
836  *
837  * @param r
838  *   A pointer to the ring structure.
839  * @param obj_p
840  *   A pointer to a void * pointer (object) that will be filled.
841  * @return
842  *   - 0: Success; objects dequeued.
843  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
844  *     dequeued.
845  */
846 static inline int __attribute__((always_inline))
847 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
848 {
849         return rte_ring_sc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
850 }
851
852 /**
853  * Dequeue one object from a ring.
854  *
855  * This function calls the multi-consumers or the single-consumer
856  * version depending on the default behaviour that was specified at
857  * ring creation time (see flags).
858  *
859  * @param r
860  *   A pointer to the ring structure.
861  * @param obj_p
862  *   A pointer to a void * pointer (object) that will be filled.
863  * @return
864  *   - 0: Success, objects dequeued.
865  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
866  *     dequeued.
867  */
868 static inline int __attribute__((always_inline))
869 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
870 {
871         return rte_ring_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOBUFS;
872 }
873
874 /**
875  * Test if a ring is full.
876  *
877  * @param r
878  *   A pointer to the ring structure.
879  * @return
880  *   - 1: The ring is full.
881  *   - 0: The ring is not full.
882  */
883 static inline int
884 rte_ring_full(const struct rte_ring *r)
885 {
886         uint32_t prod_tail = r->prod.tail;
887         uint32_t cons_tail = r->cons.tail;
888         return ((cons_tail - prod_tail - 1) & r->mask) == 0;
889 }
890
891 /**
892  * Test if a ring is empty.
893  *
894  * @param r
895  *   A pointer to the ring structure.
896  * @return
897  *   - 1: The ring is empty.
898  *   - 0: The ring is not empty.
899  */
900 static inline int
901 rte_ring_empty(const struct rte_ring *r)
902 {
903         uint32_t prod_tail = r->prod.tail;
904         uint32_t cons_tail = r->cons.tail;
905         return !!(cons_tail == prod_tail);
906 }
907
908 /**
909  * Return the number of entries in a ring.
910  *
911  * @param r
912  *   A pointer to the ring structure.
913  * @return
914  *   The number of entries in the ring.
915  */
916 static inline unsigned
917 rte_ring_count(const struct rte_ring *r)
918 {
919         uint32_t prod_tail = r->prod.tail;
920         uint32_t cons_tail = r->cons.tail;
921         return (prod_tail - cons_tail) & r->mask;
922 }
923
924 /**
925  * Return the number of free entries in a ring.
926  *
927  * @param r
928  *   A pointer to the ring structure.
929  * @return
930  *   The number of free entries in the ring.
931  */
932 static inline unsigned
933 rte_ring_free_count(const struct rte_ring *r)
934 {
935         uint32_t prod_tail = r->prod.tail;
936         uint32_t cons_tail = r->cons.tail;
937         return (cons_tail - prod_tail - 1) & r->mask;
938 }
939
940 /**
941  * Return the size of the ring.
942  *
943  * @param r
944  *   A pointer to the ring structure.
945  * @return
946  *   The number of elements which can be stored in the ring.
947  */
948 static inline unsigned int
949 rte_ring_get_size(const struct rte_ring *r)
950 {
951         return r->size;
952 }
953
954 /**
955  * Dump the status of all rings on the console
956  *
957  * @param f
958  *   A pointer to a file for output
959  */
960 void rte_ring_list_dump(FILE *f);
961
962 /**
963  * Search a ring from its name
964  *
965  * @param name
966  *   The name of the ring.
967  * @return
968  *   The pointer to the ring matching the name, or NULL if not found,
969  *   with rte_errno set appropriately. Possible rte_errno values include:
970  *    - ENOENT - required entry not available to return.
971  */
972 struct rte_ring *rte_ring_lookup(const char *name);
973
974 /**
975  * Enqueue several objects on the ring (multi-producers safe).
976  *
977  * This function uses a "compare and set" instruction to move the
978  * producer index atomically.
979  *
980  * @param r
981  *   A pointer to the ring structure.
982  * @param obj_table
983  *   A pointer to a table of void * pointers (objects).
984  * @param n
985  *   The number of objects to add in the ring from the obj_table.
986  * @param free_space
987  *   if non-NULL, returns the amount of space in the ring after the
988  *   enqueue operation has finished.
989  * @return
990  *   - n: Actual number of objects enqueued.
991  */
992 static inline unsigned __attribute__((always_inline))
993 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
994                          unsigned int n, unsigned int *free_space)
995 {
996         return __rte_ring_mp_do_enqueue(r, obj_table, n,
997                         RTE_RING_QUEUE_VARIABLE, free_space);
998 }
999
1000 /**
1001  * Enqueue several objects on a ring (NOT multi-producers safe).
1002  *
1003  * @param r
1004  *   A pointer to the ring structure.
1005  * @param obj_table
1006  *   A pointer to a table of void * pointers (objects).
1007  * @param n
1008  *   The number of objects to add in the ring from the obj_table.
1009  * @param free_space
1010  *   if non-NULL, returns the amount of space in the ring after the
1011  *   enqueue operation has finished.
1012  * @return
1013  *   - n: Actual number of objects enqueued.
1014  */
1015 static inline unsigned __attribute__((always_inline))
1016 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1017                          unsigned int n, unsigned int *free_space)
1018 {
1019         return __rte_ring_sp_do_enqueue(r, obj_table, n,
1020                         RTE_RING_QUEUE_VARIABLE, free_space);
1021 }
1022
1023 /**
1024  * Enqueue several objects on a ring.
1025  *
1026  * This function calls the multi-producer or the single-producer
1027  * version depending on the default behavior that was specified at
1028  * ring creation time (see flags).
1029  *
1030  * @param r
1031  *   A pointer to the ring structure.
1032  * @param obj_table
1033  *   A pointer to a table of void * pointers (objects).
1034  * @param n
1035  *   The number of objects to add in the ring from the obj_table.
1036  * @param free_space
1037  *   if non-NULL, returns the amount of space in the ring after the
1038  *   enqueue operation has finished.
1039  * @return
1040  *   - n: Actual number of objects enqueued.
1041  */
1042 static inline unsigned __attribute__((always_inline))
1043 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
1044                       unsigned int n, unsigned int *free_space)
1045 {
1046         if (r->prod.single)
1047                 return rte_ring_sp_enqueue_burst(r, obj_table, n, free_space);
1048         else
1049                 return rte_ring_mp_enqueue_burst(r, obj_table, n, free_space);
1050 }
1051
1052 /**
1053  * Dequeue several objects from a ring (multi-consumers safe). When the request
1054  * objects are more than the available objects, only dequeue the actual number
1055  * of objects
1056  *
1057  * This function uses a "compare and set" instruction to move the
1058  * consumer index atomically.
1059  *
1060  * @param r
1061  *   A pointer to the ring structure.
1062  * @param obj_table
1063  *   A pointer to a table of void * pointers (objects) that will be filled.
1064  * @param n
1065  *   The number of objects to dequeue from the ring to the obj_table.
1066  * @param available
1067  *   If non-NULL, returns the number of remaining ring entries after the
1068  *   dequeue has finished.
1069  * @return
1070  *   - n: Actual number of objects dequeued, 0 if ring is empty
1071  */
1072 static inline unsigned __attribute__((always_inline))
1073 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
1074                 unsigned int n, unsigned int *available)
1075 {
1076         return __rte_ring_mc_do_dequeue(r, obj_table, n,
1077                         RTE_RING_QUEUE_VARIABLE, available);
1078 }
1079
1080 /**
1081  * Dequeue several objects from a ring (NOT multi-consumers safe).When the
1082  * request objects are more than the available objects, only dequeue the
1083  * actual number of objects
1084  *
1085  * @param r
1086  *   A pointer to the ring structure.
1087  * @param obj_table
1088  *   A pointer to a table of void * pointers (objects) that will be filled.
1089  * @param n
1090  *   The number of objects to dequeue from the ring to the obj_table.
1091  * @param available
1092  *   If non-NULL, returns the number of remaining ring entries after the
1093  *   dequeue has finished.
1094  * @return
1095  *   - n: Actual number of objects dequeued, 0 if ring is empty
1096  */
1097 static inline unsigned __attribute__((always_inline))
1098 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
1099                 unsigned int n, unsigned int *available)
1100 {
1101         return __rte_ring_sc_do_dequeue(r, obj_table, n,
1102                         RTE_RING_QUEUE_VARIABLE, available);
1103 }
1104
1105 /**
1106  * Dequeue multiple objects from a ring up to a maximum number.
1107  *
1108  * This function calls the multi-consumers or the single-consumer
1109  * version, depending on the default behaviour that was specified at
1110  * ring creation time (see flags).
1111  *
1112  * @param r
1113  *   A pointer to the ring structure.
1114  * @param obj_table
1115  *   A pointer to a table of void * pointers (objects) that will be filled.
1116  * @param n
1117  *   The number of objects to dequeue from the ring to the obj_table.
1118  * @param available
1119  *   If non-NULL, returns the number of remaining ring entries after the
1120  *   dequeue has finished.
1121  * @return
1122  *   - Number of objects dequeued
1123  */
1124 static inline unsigned __attribute__((always_inline))
1125 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
1126                 unsigned int n, unsigned int *available)
1127 {
1128         if (r->cons.single)
1129                 return rte_ring_sc_dequeue_burst(r, obj_table, n, available);
1130         else
1131                 return rte_ring_mc_dequeue_burst(r, obj_table, n, available);
1132 }
1133
1134 #ifdef __cplusplus
1135 }
1136 #endif
1137
1138 #endif /* _RTE_RING_H_ */