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