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