ring: move code in a new header file
[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_config.h>
100 #include <rte_memory.h>
101 #include <rte_lcore.h>
102 #include <rte_atomic.h>
103 #include <rte_branch_prediction.h>
104 #include <rte_memzone.h>
105 #include <rte_pause.h>
106
107 #define RTE_TAILQ_RING_NAME "RTE_RING"
108
109 enum rte_ring_queue_behavior {
110         RTE_RING_QUEUE_FIXED = 0, /* Enq/Deq a fixed number of items from a ring */
111         RTE_RING_QUEUE_VARIABLE   /* Enq/Deq as many items as possible from ring */
112 };
113
114 #define RTE_RING_MZ_PREFIX "RG_"
115 /**< The maximum length of a ring name. */
116 #define RTE_RING_NAMESIZE (RTE_MEMZONE_NAMESIZE - \
117                            sizeof(RTE_RING_MZ_PREFIX) + 1)
118
119 struct rte_memzone; /* forward declaration, so as not to require memzone.h */
120
121 #if RTE_CACHE_LINE_SIZE < 128
122 #define PROD_ALIGN (RTE_CACHE_LINE_SIZE * 2)
123 #define CONS_ALIGN (RTE_CACHE_LINE_SIZE * 2)
124 #else
125 #define PROD_ALIGN RTE_CACHE_LINE_SIZE
126 #define CONS_ALIGN RTE_CACHE_LINE_SIZE
127 #endif
128
129 /* structure to hold a pair of head/tail values and other metadata */
130 struct rte_ring_headtail {
131         volatile uint32_t head;  /**< Prod/consumer head. */
132         volatile uint32_t tail;  /**< Prod/consumer tail. */
133         uint32_t single;         /**< True if single prod/cons */
134 };
135
136 /**
137  * An RTE ring structure.
138  *
139  * The producer and the consumer have a head and a tail index. The particularity
140  * of these index is that they are not between 0 and size(ring). These indexes
141  * are between 0 and 2^32, and we mask their value when we access the ring[]
142  * field. Thanks to this assumption, we can do subtractions between 2 index
143  * values in a modulo-32bit base: that's why the overflow of the indexes is not
144  * a problem.
145  */
146 struct rte_ring {
147         /*
148          * Note: this field kept the RTE_MEMZONE_NAMESIZE size due to ABI
149          * compatibility requirements, it could be changed to RTE_RING_NAMESIZE
150          * next time the ABI changes
151          */
152         char name[RTE_MEMZONE_NAMESIZE] __rte_cache_aligned; /**< Name of the ring. */
153         int flags;               /**< Flags supplied at creation. */
154         const struct rte_memzone *memzone;
155                         /**< Memzone, if any, containing the rte_ring */
156         uint32_t size;           /**< Size of ring. */
157         uint32_t mask;           /**< Mask (size-1) of ring. */
158         uint32_t capacity;       /**< Usable size of ring */
159
160         /** Ring producer status. */
161         struct rte_ring_headtail prod __rte_aligned(PROD_ALIGN);
162
163         /** Ring consumer status. */
164         struct rte_ring_headtail cons __rte_aligned(CONS_ALIGN);
165 };
166
167 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
168 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
169 /**
170  * Ring is to hold exactly requested number of entries.
171  * Without this flag set, the ring size requested must be a power of 2, and the
172  * usable space will be that size - 1. With the flag, the requested size will
173  * be rounded up to the next power of two, but the usable space will be exactly
174  * that requested. Worst case, if a power-of-2 size is requested, half the
175  * ring space will be wasted.
176  */
177 #define RING_F_EXACT_SZ 0x0004
178 #define RTE_RING_SZ_MASK  (0x7fffffffU) /**< Ring size mask */
179
180 /* @internal defines for passing to the enqueue dequeue worker functions */
181 #define __IS_SP 1
182 #define __IS_MP 0
183 #define __IS_SC 1
184 #define __IS_MC 0
185
186 /**
187  * Calculate the memory size needed for a ring
188  *
189  * This function returns the number of bytes needed for a ring, given
190  * the number of elements in it. This value is the sum of the size of
191  * the structure rte_ring and the size of the memory needed by the
192  * objects pointers. The value is aligned to a cache line size.
193  *
194  * @param count
195  *   The number of elements in the ring (must be a power of 2).
196  * @return
197  *   - The memory size needed for the ring on success.
198  *   - -EINVAL if count is not a power of 2.
199  */
200 ssize_t rte_ring_get_memsize(unsigned count);
201
202 /**
203  * Initialize a ring structure.
204  *
205  * Initialize a ring structure in memory pointed by "r". The size of the
206  * memory area must be large enough to store the ring structure and the
207  * object table. It is advised to use rte_ring_get_memsize() to get the
208  * appropriate size.
209  *
210  * The ring size is set to *count*, which must be a power of two. Water
211  * marking is disabled by default. The real usable ring size is
212  * *count-1* instead of *count* to differentiate a free ring from an
213  * empty ring.
214  *
215  * The ring is not added in RTE_TAILQ_RING global list. Indeed, the
216  * memory given by the caller may not be shareable among dpdk
217  * processes.
218  *
219  * @param r
220  *   The pointer to the ring structure followed by the objects table.
221  * @param name
222  *   The name of the ring.
223  * @param count
224  *   The number of elements in the ring (must be a power of 2).
225  * @param flags
226  *   An OR of the following:
227  *    - RING_F_SP_ENQ: If this flag is set, the default behavior when
228  *      using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
229  *      is "single-producer". Otherwise, it is "multi-producers".
230  *    - RING_F_SC_DEQ: If this flag is set, the default behavior when
231  *      using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
232  *      is "single-consumer". Otherwise, it is "multi-consumers".
233  * @return
234  *   0 on success, or a negative value on error.
235  */
236 int rte_ring_init(struct rte_ring *r, const char *name, unsigned count,
237         unsigned flags);
238
239 /**
240  * Create a new ring named *name* in memory.
241  *
242  * This function uses ``memzone_reserve()`` to allocate memory. Then it
243  * calls rte_ring_init() to initialize an empty ring.
244  *
245  * The new ring size is set to *count*, which must be a power of
246  * two. Water marking is disabled by default. The real usable ring size
247  * is *count-1* instead of *count* to differentiate a free ring from an
248  * empty ring.
249  *
250  * The ring is added in RTE_TAILQ_RING list.
251  *
252  * @param name
253  *   The name of the ring.
254  * @param count
255  *   The size of the ring (must be a power of 2).
256  * @param socket_id
257  *   The *socket_id* argument is the socket identifier in case of
258  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
259  *   constraint for the reserved zone.
260  * @param flags
261  *   An OR of the following:
262  *    - RING_F_SP_ENQ: If this flag is set, the default behavior when
263  *      using ``rte_ring_enqueue()`` or ``rte_ring_enqueue_bulk()``
264  *      is "single-producer". Otherwise, it is "multi-producers".
265  *    - RING_F_SC_DEQ: If this flag is set, the default behavior when
266  *      using ``rte_ring_dequeue()`` or ``rte_ring_dequeue_bulk()``
267  *      is "single-consumer". Otherwise, it is "multi-consumers".
268  * @return
269  *   On success, the pointer to the new allocated ring. NULL on error with
270  *    rte_errno set appropriately. Possible errno values include:
271  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
272  *    - E_RTE_SECONDARY - function was called from a secondary process instance
273  *    - EINVAL - count provided is not a power of 2
274  *    - ENOSPC - the maximum number of memzones has already been allocated
275  *    - EEXIST - a memzone with the same name already exists
276  *    - ENOMEM - no appropriate memory area found in which to create memzone
277  */
278 struct rte_ring *rte_ring_create(const char *name, unsigned count,
279                                  int socket_id, unsigned flags);
280 /**
281  * De-allocate all memory used by the ring.
282  *
283  * @param r
284  *   Ring to free
285  */
286 void rte_ring_free(struct rte_ring *r);
287
288 /**
289  * Dump the status of the ring to a file.
290  *
291  * @param f
292  *   A pointer to a file for output
293  * @param r
294  *   A pointer to the ring structure.
295  */
296 void rte_ring_dump(FILE *f, const struct rte_ring *r);
297
298 /* the actual enqueue of pointers on the ring.
299  * Placed here since identical code needed in both
300  * single and multi producer enqueue functions */
301 #define ENQUEUE_PTRS(r, ring_start, prod_head, obj_table, n, obj_type) do { \
302         unsigned int i; \
303         const uint32_t size = (r)->size; \
304         uint32_t idx = prod_head & (r)->mask; \
305         obj_type *ring = (obj_type *)ring_start; \
306         if (likely(idx + n < size)) { \
307                 for (i = 0; i < (n & ((~(unsigned)0x3))); i+=4, idx+=4) { \
308                         ring[idx] = obj_table[i]; \
309                         ring[idx+1] = obj_table[i+1]; \
310                         ring[idx+2] = obj_table[i+2]; \
311                         ring[idx+3] = obj_table[i+3]; \
312                 } \
313                 switch (n & 0x3) { \
314                 case 3: \
315                         ring[idx++] = obj_table[i++]; /* fallthrough */ \
316                 case 2: \
317                         ring[idx++] = obj_table[i++]; /* fallthrough */ \
318                 case 1: \
319                         ring[idx++] = obj_table[i++]; \
320                 } \
321         } else { \
322                 for (i = 0; idx < size; i++, idx++)\
323                         ring[idx] = obj_table[i]; \
324                 for (idx = 0; i < n; i++, idx++) \
325                         ring[idx] = obj_table[i]; \
326         } \
327 } while (0)
328
329 /* the actual copy of pointers on the ring to obj_table.
330  * Placed here since identical code needed in both
331  * single and multi consumer dequeue functions */
332 #define DEQUEUE_PTRS(r, ring_start, cons_head, obj_table, n, obj_type) do { \
333         unsigned int i; \
334         uint32_t idx = cons_head & (r)->mask; \
335         const uint32_t size = (r)->size; \
336         obj_type *ring = (obj_type *)ring_start; \
337         if (likely(idx + n < size)) { \
338                 for (i = 0; i < (n & (~(unsigned)0x3)); i+=4, idx+=4) {\
339                         obj_table[i] = ring[idx]; \
340                         obj_table[i+1] = ring[idx+1]; \
341                         obj_table[i+2] = ring[idx+2]; \
342                         obj_table[i+3] = ring[idx+3]; \
343                 } \
344                 switch (n & 0x3) { \
345                 case 3: \
346                         obj_table[i++] = ring[idx++]; /* fallthrough */ \
347                 case 2: \
348                         obj_table[i++] = ring[idx++]; /* fallthrough */ \
349                 case 1: \
350                         obj_table[i++] = ring[idx++]; \
351                 } \
352         } else { \
353                 for (i = 0; idx < size; i++, idx++) \
354                         obj_table[i] = ring[idx]; \
355                 for (idx = 0; i < n; i++, idx++) \
356                         obj_table[i] = ring[idx]; \
357         } \
358 } while (0)
359
360 /* Move common functions to generic file */
361 #include "rte_ring_generic.h"
362
363 /**
364  * @internal Enqueue several objects on the ring
365  *
366   * @param r
367  *   A pointer to the ring structure.
368  * @param obj_table
369  *   A pointer to a table of void * pointers (objects).
370  * @param n
371  *   The number of objects to add in the ring from the obj_table.
372  * @param behavior
373  *   RTE_RING_QUEUE_FIXED:    Enqueue a fixed number of items from a ring
374  *   RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
375  * @param is_sp
376  *   Indicates whether to use single producer or multi-producer head update
377  * @param free_space
378  *   returns the amount of space after the enqueue operation has finished
379  * @return
380  *   Actual number of objects enqueued.
381  *   If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
382  */
383 static __rte_always_inline unsigned int
384 __rte_ring_do_enqueue(struct rte_ring *r, void * const *obj_table,
385                  unsigned int n, enum rte_ring_queue_behavior behavior,
386                  int is_sp, unsigned int *free_space)
387 {
388         uint32_t prod_head, prod_next;
389         uint32_t free_entries;
390
391         n = __rte_ring_move_prod_head(r, is_sp, n, behavior,
392                         &prod_head, &prod_next, &free_entries);
393         if (n == 0)
394                 goto end;
395
396         ENQUEUE_PTRS(r, &r[1], prod_head, obj_table, n, void *);
397
398         update_tail(&r->prod, prod_head, prod_next, is_sp, 1);
399 end:
400         if (free_space != NULL)
401                 *free_space = free_entries - n;
402         return n;
403 }
404
405 /**
406  * @internal Dequeue several objects from the ring
407  *
408  * @param r
409  *   A pointer to the ring structure.
410  * @param obj_table
411  *   A pointer to a table of void * pointers (objects).
412  * @param n
413  *   The number of objects to pull from the ring.
414  * @param behavior
415  *   RTE_RING_QUEUE_FIXED:    Dequeue a fixed number of items from a ring
416  *   RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
417  * @param is_sc
418  *   Indicates whether to use single consumer or multi-consumer head update
419  * @param available
420  *   returns the number of remaining ring entries after the dequeue has finished
421  * @return
422  *   - Actual number of objects dequeued.
423  *     If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
424  */
425 static __rte_always_inline unsigned int
426 __rte_ring_do_dequeue(struct rte_ring *r, void **obj_table,
427                  unsigned int n, enum rte_ring_queue_behavior behavior,
428                  int is_sc, unsigned int *available)
429 {
430         uint32_t cons_head, cons_next;
431         uint32_t entries;
432
433         n = __rte_ring_move_cons_head(r, is_sc, n, behavior,
434                         &cons_head, &cons_next, &entries);
435         if (n == 0)
436                 goto end;
437
438         DEQUEUE_PTRS(r, &r[1], cons_head, obj_table, n, void *);
439
440         update_tail(&r->cons, cons_head, cons_next, is_sc, 0);
441
442 end:
443         if (available != NULL)
444                 *available = entries - n;
445         return n;
446 }
447
448 /**
449  * Enqueue several objects on the ring (multi-producers safe).
450  *
451  * This function uses a "compare and set" instruction to move the
452  * producer index atomically.
453  *
454  * @param r
455  *   A pointer to the ring structure.
456  * @param obj_table
457  *   A pointer to a table of void * pointers (objects).
458  * @param n
459  *   The number of objects to add in the ring from the obj_table.
460  * @param free_space
461  *   if non-NULL, returns the amount of space in the ring after the
462  *   enqueue operation has finished.
463  * @return
464  *   The number of objects enqueued, either 0 or n
465  */
466 static __rte_always_inline unsigned int
467 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
468                          unsigned int n, unsigned int *free_space)
469 {
470         return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
471                         __IS_MP, free_space);
472 }
473
474 /**
475  * Enqueue several objects on a ring (NOT multi-producers safe).
476  *
477  * @param r
478  *   A pointer to the ring structure.
479  * @param obj_table
480  *   A pointer to a table of void * pointers (objects).
481  * @param n
482  *   The number of objects to add in the ring from the obj_table.
483  * @param free_space
484  *   if non-NULL, returns the amount of space in the ring after the
485  *   enqueue operation has finished.
486  * @return
487  *   The number of objects enqueued, either 0 or n
488  */
489 static __rte_always_inline unsigned int
490 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
491                          unsigned int n, unsigned int *free_space)
492 {
493         return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
494                         __IS_SP, free_space);
495 }
496
497 /**
498  * Enqueue several objects on a ring.
499  *
500  * This function calls the multi-producer or the single-producer
501  * version depending on the default behavior that was specified at
502  * ring creation time (see flags).
503  *
504  * @param r
505  *   A pointer to the ring structure.
506  * @param obj_table
507  *   A pointer to a table of void * pointers (objects).
508  * @param n
509  *   The number of objects to add in the ring from the obj_table.
510  * @param free_space
511  *   if non-NULL, returns the amount of space in the ring after the
512  *   enqueue operation has finished.
513  * @return
514  *   The number of objects enqueued, either 0 or n
515  */
516 static __rte_always_inline unsigned int
517 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
518                       unsigned int n, unsigned int *free_space)
519 {
520         return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
521                         r->prod.single, free_space);
522 }
523
524 /**
525  * Enqueue one object on a ring (multi-producers safe).
526  *
527  * This function uses a "compare and set" instruction to move the
528  * producer index atomically.
529  *
530  * @param r
531  *   A pointer to the ring structure.
532  * @param obj
533  *   A pointer to the object to be added.
534  * @return
535  *   - 0: Success; objects enqueued.
536  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
537  */
538 static __rte_always_inline int
539 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
540 {
541         return rte_ring_mp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
542 }
543
544 /**
545  * Enqueue one object on a ring (NOT multi-producers safe).
546  *
547  * @param r
548  *   A pointer to the ring structure.
549  * @param obj
550  *   A pointer to the object to be added.
551  * @return
552  *   - 0: Success; objects enqueued.
553  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
554  */
555 static __rte_always_inline int
556 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
557 {
558         return rte_ring_sp_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
559 }
560
561 /**
562  * Enqueue one object on a ring.
563  *
564  * This function calls the multi-producer or the single-producer
565  * version, depending on the default behaviour that was specified at
566  * ring creation time (see flags).
567  *
568  * @param r
569  *   A pointer to the ring structure.
570  * @param obj
571  *   A pointer to the object to be added.
572  * @return
573  *   - 0: Success; objects enqueued.
574  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
575  */
576 static __rte_always_inline int
577 rte_ring_enqueue(struct rte_ring *r, void *obj)
578 {
579         return rte_ring_enqueue_bulk(r, &obj, 1, NULL) ? 0 : -ENOBUFS;
580 }
581
582 /**
583  * Dequeue several objects from a ring (multi-consumers safe).
584  *
585  * This function uses a "compare and set" instruction to move the
586  * consumer index atomically.
587  *
588  * @param r
589  *   A pointer to the ring structure.
590  * @param obj_table
591  *   A pointer to a table of void * pointers (objects) that will be filled.
592  * @param n
593  *   The number of objects to dequeue from the ring to the obj_table.
594  * @param available
595  *   If non-NULL, returns the number of remaining ring entries after the
596  *   dequeue has finished.
597  * @return
598  *   The number of objects dequeued, either 0 or n
599  */
600 static __rte_always_inline unsigned int
601 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table,
602                 unsigned int n, unsigned int *available)
603 {
604         return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
605                         __IS_MC, available);
606 }
607
608 /**
609  * Dequeue several objects from a ring (NOT multi-consumers safe).
610  *
611  * @param r
612  *   A pointer to the ring structure.
613  * @param obj_table
614  *   A pointer to a table of void * pointers (objects) that will be filled.
615  * @param n
616  *   The number of objects to dequeue from the ring to the obj_table,
617  *   must be strictly positive.
618  * @param available
619  *   If non-NULL, returns the number of remaining ring entries after the
620  *   dequeue has finished.
621  * @return
622  *   The number of objects dequeued, either 0 or n
623  */
624 static __rte_always_inline unsigned int
625 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table,
626                 unsigned int n, unsigned int *available)
627 {
628         return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
629                         __IS_SC, available);
630 }
631
632 /**
633  * Dequeue several objects from a ring.
634  *
635  * This function calls the multi-consumers or the single-consumer
636  * version, depending on the default behaviour that was specified at
637  * ring creation time (see flags).
638  *
639  * @param r
640  *   A pointer to the ring structure.
641  * @param obj_table
642  *   A pointer to a table of void * pointers (objects) that will be filled.
643  * @param n
644  *   The number of objects to dequeue from the ring to the obj_table.
645  * @param available
646  *   If non-NULL, returns the number of remaining ring entries after the
647  *   dequeue has finished.
648  * @return
649  *   The number of objects dequeued, either 0 or n
650  */
651 static __rte_always_inline unsigned int
652 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned int n,
653                 unsigned int *available)
654 {
655         return __rte_ring_do_dequeue(r, obj_table, n, RTE_RING_QUEUE_FIXED,
656                                 r->cons.single, available);
657 }
658
659 /**
660  * Dequeue one object from a ring (multi-consumers safe).
661  *
662  * This function uses a "compare and set" instruction to move the
663  * consumer index atomically.
664  *
665  * @param r
666  *   A pointer to the ring structure.
667  * @param obj_p
668  *   A pointer to a void * pointer (object) that will be filled.
669  * @return
670  *   - 0: Success; objects dequeued.
671  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
672  *     dequeued.
673  */
674 static __rte_always_inline int
675 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
676 {
677         return rte_ring_mc_dequeue_bulk(r, obj_p, 1, NULL)  ? 0 : -ENOENT;
678 }
679
680 /**
681  * Dequeue one object from a ring (NOT multi-consumers safe).
682  *
683  * @param r
684  *   A pointer to the ring structure.
685  * @param obj_p
686  *   A pointer to a void * pointer (object) that will be filled.
687  * @return
688  *   - 0: Success; objects dequeued.
689  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
690  *     dequeued.
691  */
692 static __rte_always_inline int
693 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
694 {
695         return rte_ring_sc_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
696 }
697
698 /**
699  * Dequeue one object from a ring.
700  *
701  * This function calls the multi-consumers or the single-consumer
702  * version depending on the default behaviour that was specified at
703  * ring creation time (see flags).
704  *
705  * @param r
706  *   A pointer to the ring structure.
707  * @param obj_p
708  *   A pointer to a void * pointer (object) that will be filled.
709  * @return
710  *   - 0: Success, objects dequeued.
711  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
712  *     dequeued.
713  */
714 static __rte_always_inline int
715 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
716 {
717         return rte_ring_dequeue_bulk(r, obj_p, 1, NULL) ? 0 : -ENOENT;
718 }
719
720 /**
721  * Return the number of entries in a ring.
722  *
723  * @param r
724  *   A pointer to the ring structure.
725  * @return
726  *   The number of entries in the ring.
727  */
728 static inline unsigned
729 rte_ring_count(const struct rte_ring *r)
730 {
731         uint32_t prod_tail = r->prod.tail;
732         uint32_t cons_tail = r->cons.tail;
733         uint32_t count = (prod_tail - cons_tail) & r->mask;
734         return (count > r->capacity) ? r->capacity : count;
735 }
736
737 /**
738  * Return the number of free entries in a ring.
739  *
740  * @param r
741  *   A pointer to the ring structure.
742  * @return
743  *   The number of free entries in the ring.
744  */
745 static inline unsigned
746 rte_ring_free_count(const struct rte_ring *r)
747 {
748         return r->capacity - rte_ring_count(r);
749 }
750
751 /**
752  * Test if a ring is full.
753  *
754  * @param r
755  *   A pointer to the ring structure.
756  * @return
757  *   - 1: The ring is full.
758  *   - 0: The ring is not full.
759  */
760 static inline int
761 rte_ring_full(const struct rte_ring *r)
762 {
763         return rte_ring_free_count(r) == 0;
764 }
765
766 /**
767  * Test if a ring is empty.
768  *
769  * @param r
770  *   A pointer to the ring structure.
771  * @return
772  *   - 1: The ring is empty.
773  *   - 0: The ring is not empty.
774  */
775 static inline int
776 rte_ring_empty(const struct rte_ring *r)
777 {
778         return rte_ring_count(r) == 0;
779 }
780
781 /**
782  * Return the size of the ring.
783  *
784  * @param r
785  *   A pointer to the ring structure.
786  * @return
787  *   The size of the data store used by the ring.
788  *   NOTE: this is not the same as the usable space in the ring. To query that
789  *   use ``rte_ring_get_capacity()``.
790  */
791 static inline unsigned int
792 rte_ring_get_size(const struct rte_ring *r)
793 {
794         return r->size;
795 }
796
797 /**
798  * Return the number of elements which can be stored in the ring.
799  *
800  * @param r
801  *   A pointer to the ring structure.
802  * @return
803  *   The usable size of the ring.
804  */
805 static inline unsigned int
806 rte_ring_get_capacity(const struct rte_ring *r)
807 {
808         return r->capacity;
809 }
810
811 /**
812  * Dump the status of all rings on the console
813  *
814  * @param f
815  *   A pointer to a file for output
816  */
817 void rte_ring_list_dump(FILE *f);
818
819 /**
820  * Search a ring from its name
821  *
822  * @param name
823  *   The name of the ring.
824  * @return
825  *   The pointer to the ring matching the name, or NULL if not found,
826  *   with rte_errno set appropriately. Possible rte_errno values include:
827  *    - ENOENT - required entry not available to return.
828  */
829 struct rte_ring *rte_ring_lookup(const char *name);
830
831 /**
832  * Enqueue several objects on the ring (multi-producers safe).
833  *
834  * This function uses a "compare and set" instruction to move the
835  * producer index atomically.
836  *
837  * @param r
838  *   A pointer to the ring structure.
839  * @param obj_table
840  *   A pointer to a table of void * pointers (objects).
841  * @param n
842  *   The number of objects to add in the ring from the obj_table.
843  * @param free_space
844  *   if non-NULL, returns the amount of space in the ring after the
845  *   enqueue operation has finished.
846  * @return
847  *   - n: Actual number of objects enqueued.
848  */
849 static __rte_always_inline unsigned
850 rte_ring_mp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
851                          unsigned int n, unsigned int *free_space)
852 {
853         return __rte_ring_do_enqueue(r, obj_table, n,
854                         RTE_RING_QUEUE_VARIABLE, __IS_MP, free_space);
855 }
856
857 /**
858  * Enqueue several objects on a ring (NOT multi-producers safe).
859  *
860  * @param r
861  *   A pointer to the ring structure.
862  * @param obj_table
863  *   A pointer to a table of void * pointers (objects).
864  * @param n
865  *   The number of objects to add in the ring from the obj_table.
866  * @param free_space
867  *   if non-NULL, returns the amount of space in the ring after the
868  *   enqueue operation has finished.
869  * @return
870  *   - n: Actual number of objects enqueued.
871  */
872 static __rte_always_inline unsigned
873 rte_ring_sp_enqueue_burst(struct rte_ring *r, void * const *obj_table,
874                          unsigned int n, unsigned int *free_space)
875 {
876         return __rte_ring_do_enqueue(r, obj_table, n,
877                         RTE_RING_QUEUE_VARIABLE, __IS_SP, free_space);
878 }
879
880 /**
881  * Enqueue several objects on a ring.
882  *
883  * This function calls the multi-producer or the single-producer
884  * version depending on the default behavior that was specified at
885  * ring creation time (see flags).
886  *
887  * @param r
888  *   A pointer to the ring structure.
889  * @param obj_table
890  *   A pointer to a table of void * pointers (objects).
891  * @param n
892  *   The number of objects to add in the ring from the obj_table.
893  * @param free_space
894  *   if non-NULL, returns the amount of space in the ring after the
895  *   enqueue operation has finished.
896  * @return
897  *   - n: Actual number of objects enqueued.
898  */
899 static __rte_always_inline unsigned
900 rte_ring_enqueue_burst(struct rte_ring *r, void * const *obj_table,
901                       unsigned int n, unsigned int *free_space)
902 {
903         return __rte_ring_do_enqueue(r, obj_table, n, RTE_RING_QUEUE_VARIABLE,
904                         r->prod.single, free_space);
905 }
906
907 /**
908  * Dequeue several objects from a ring (multi-consumers safe). When the request
909  * objects are more than the available objects, only dequeue the actual number
910  * of objects
911  *
912  * This function uses a "compare and set" instruction to move the
913  * consumer index atomically.
914  *
915  * @param r
916  *   A pointer to the ring structure.
917  * @param obj_table
918  *   A pointer to a table of void * pointers (objects) that will be filled.
919  * @param n
920  *   The number of objects to dequeue from the ring to the obj_table.
921  * @param available
922  *   If non-NULL, returns the number of remaining ring entries after the
923  *   dequeue has finished.
924  * @return
925  *   - n: Actual number of objects dequeued, 0 if ring is empty
926  */
927 static __rte_always_inline unsigned
928 rte_ring_mc_dequeue_burst(struct rte_ring *r, void **obj_table,
929                 unsigned int n, unsigned int *available)
930 {
931         return __rte_ring_do_dequeue(r, obj_table, n,
932                         RTE_RING_QUEUE_VARIABLE, __IS_MC, available);
933 }
934
935 /**
936  * Dequeue several objects from a ring (NOT multi-consumers safe).When the
937  * request objects are more than the available objects, only dequeue the
938  * actual number of objects
939  *
940  * @param r
941  *   A pointer to the ring structure.
942  * @param obj_table
943  *   A pointer to a table of void * pointers (objects) that will be filled.
944  * @param n
945  *   The number of objects to dequeue from the ring to the obj_table.
946  * @param available
947  *   If non-NULL, returns the number of remaining ring entries after the
948  *   dequeue has finished.
949  * @return
950  *   - n: Actual number of objects dequeued, 0 if ring is empty
951  */
952 static __rte_always_inline unsigned
953 rte_ring_sc_dequeue_burst(struct rte_ring *r, void **obj_table,
954                 unsigned int n, unsigned int *available)
955 {
956         return __rte_ring_do_dequeue(r, obj_table, n,
957                         RTE_RING_QUEUE_VARIABLE, __IS_SC, available);
958 }
959
960 /**
961  * Dequeue multiple objects from a ring up to a maximum number.
962  *
963  * This function calls the multi-consumers or the single-consumer
964  * version, depending on the default behaviour that was specified at
965  * ring creation time (see flags).
966  *
967  * @param r
968  *   A pointer to the ring structure.
969  * @param obj_table
970  *   A pointer to a table of void * pointers (objects) that will be filled.
971  * @param n
972  *   The number of objects to dequeue from the ring to the obj_table.
973  * @param available
974  *   If non-NULL, returns the number of remaining ring entries after the
975  *   dequeue has finished.
976  * @return
977  *   - Number of objects dequeued
978  */
979 static __rte_always_inline unsigned
980 rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table,
981                 unsigned int n, unsigned int *available)
982 {
983         return __rte_ring_do_dequeue(r, obj_table, n,
984                                 RTE_RING_QUEUE_VARIABLE,
985                                 r->cons.single, available);
986 }
987
988 #ifdef __cplusplus
989 }
990 #endif
991
992 #endif /* _RTE_RING_H_ */