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