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