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