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