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