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