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