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