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