first public release
[dpdk.git] / lib / librte_ring / rte_ring.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 /*
37  * Derived from FreeBSD's bufring.h
38  *
39  **************************************************************************
40  *
41  * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
42  * All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions are met:
46  *
47  * 1. Redistributions of source code must retain the above copyright notice,
48  *    this list of conditions and the following disclaimer.
49  *
50  * 2. The name of Kip Macy nor the names of other
51  *    contributors may be used to endorse or promote products derived from
52  *    this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
55  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
58  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64  * POSSIBILITY OF SUCH DAMAGE.
65  *
66  ***************************************************************************/
67
68 #ifndef _RTE_RING_H_
69 #define _RTE_RING_H_
70
71 /**
72  * @file
73  * RTE Ring
74  *
75  * The Ring Manager is a fixed-size queue, implemented as a table of
76  * pointers. Head and tail pointers are modified atomically, allowing
77  * concurrent access to it. It has the following features:
78  *
79  * - FIFO (First In First Out)
80  * - Maximum size is fixed; the pointers are stored in a table.
81  * - Lockless implementation.
82  * - Multi- or single-consumer dequeue.
83  * - Multi- or single-producer enqueue.
84  * - Bulk dequeue.
85  * - Bulk enqueue.
86  *
87  * Note: the ring implementation is not preemptable. A lcore must not
88  * be interrupted by another task that uses the same ring.
89  *
90  */
91
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95
96 #include <stdint.h>
97 #include <sys/queue.h>
98 #include <errno.h>
99 #include <rte_common.h>
100 #include <rte_memory.h>
101 #include <rte_lcore.h>
102 #include <rte_atomic.h>
103 #include <rte_branch_prediction.h>
104
105
106 #ifdef RTE_LIBRTE_RING_DEBUG
107 /**
108  * A structure that stores the ring statistics (per-lcore).
109  */
110 struct rte_ring_debug_stats {
111         uint64_t enq_success_bulk; /**< Successful enqueues number. */
112         uint64_t enq_success_objs; /**< Objects successfully enqueued. */
113         uint64_t enq_quota_bulk;   /**< Successful enqueues above watermark. */
114         uint64_t enq_quota_objs;   /**< Objects enqueued above watermark. */
115         uint64_t enq_fail_bulk;    /**< Failed enqueues number. */
116         uint64_t enq_fail_objs;    /**< Objects that failed to be enqueued. */
117         uint64_t deq_success_bulk; /**< Successful dequeues number. */
118         uint64_t deq_success_objs; /**< Objects successfully dequeued. */
119         uint64_t deq_fail_bulk;    /**< Failed dequeues number. */
120         uint64_t deq_fail_objs;    /**< Objects that failed to be dequeued. */
121 } __rte_cache_aligned;
122 #endif
123
124 #define RTE_RING_NAMESIZE 32 /**< The maximum length of a ring name. */
125
126 /**
127  * An RTE ring structure.
128  *
129  * The producer and the consumer have a head and a tail index. The particularity
130  * of these index is that they are not between 0 and size(ring). These indexes
131  * are between 0 and 2^32, and we mask their value when we access the ring[]
132  * field. Thanks to this assumption, we can do subtractions between 2 index
133  * values in a modulo-32bit base: that's why the overflow of the indexes is not
134  * a problem.
135  */
136 struct rte_ring {
137         TAILQ_ENTRY(rte_ring) next;      /**< Next in list. */
138
139         char name[RTE_RING_NAMESIZE];    /**< Name of the ring. */
140         int flags;                       /**< Flags supplied at creation. */
141
142         /** Ring producer status. */
143         struct prod {
144                 volatile uint32_t bulk_default; /**< Default bulk count. */
145                 uint32_t watermark;      /**< Maximum items before EDQUOT. */
146                 uint32_t sp_enqueue;     /**< True, if single producer. */
147                 uint32_t size;           /**< Size of ring. */
148                 uint32_t mask;           /**< Mask (size-1) of ring. */
149                 volatile uint32_t head;  /**< Producer head. */
150                 volatile uint32_t tail;  /**< Producer tail. */
151         } prod __rte_cache_aligned;
152
153         /** Ring consumer status. */
154         struct cons {
155                 volatile uint32_t bulk_default; /**< Default bulk count. */
156                 uint32_t sc_dequeue;     /**< True, if single consumer. */
157                 uint32_t size;           /**< Size of the ring. */
158                 uint32_t mask;           /**< Mask (size-1) of ring. */
159                 volatile uint32_t head;  /**< Consumer head. */
160                 volatile uint32_t tail;  /**< Consumer tail. */
161         } cons __rte_cache_aligned;
162
163
164 #ifdef RTE_LIBRTE_RING_DEBUG
165         struct rte_ring_debug_stats stats[RTE_MAX_LCORE];
166 #endif
167
168         void * volatile ring[0] \
169                         __rte_cache_aligned; /**< Memory space of ring starts here. */
170 };
171
172 #define RING_F_SP_ENQ 0x0001 /**< The default enqueue is "single-producer". */
173 #define RING_F_SC_DEQ 0x0002 /**< The default dequeue is "single-consumer". */
174
175 /**
176  * When debug is enabled, store ring statistics.
177  * @param r
178  *   A pointer to the ring.
179  * @param name
180  *   The name of the statistics field to increment in the ring.
181  * @param n
182  *   The number to add to the object-oriented statistics.
183  */
184 #ifdef RTE_LIBRTE_RING_DEBUG
185 #define __RING_STAT_ADD(r, name, n) do {                \
186                 unsigned __lcore_id = rte_lcore_id();   \
187                 r->stats[__lcore_id].name##_objs += n;  \
188                 r->stats[__lcore_id].name##_bulk += 1;  \
189         } while(0)
190 #else
191 #define __RING_STAT_ADD(r, name, n) do {} while(0)
192 #endif
193
194 /**
195  * Create a new ring named *name* in memory.
196  *
197  * This function uses ``memzone_reserve()`` to allocate memory. Its size is
198  * set to *count*, which must be a power of two. Water marking is
199  * disabled by default. The default bulk count is initialized to 1.
200  * Note that the real usable ring size is *count-1* instead of
201  * *count*.
202  *
203  * @param name
204  *   The name of the ring.
205  * @param count
206  *   The size of the ring (must be a power of 2).
207  * @param socket_id
208  *   The *socket_id* argument is the socket identifier in case of
209  *   NUMA. The value can be *SOCKET_ID_ANY* if there is no NUMA
210  *   constraint for the reserved zone.
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  *   On success, the pointer to the new allocated ring. NULL on error with
221  *    rte_errno set appropriately. Possible errno values include:
222  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
223  *    - E_RTE_SECONDARY - function was called from a secondary process instance
224  *    - E_RTE_NO_TAILQ - no tailq list could be got for the ring list
225  *    - EINVAL - count provided is not a power of 2
226  *    - ENOSPC - the maximum number of memzones has already been allocated
227  *    - EEXIST - a memzone with the same name already exists
228  *    - ENOMEM - no appropriate memory area found in which to create memzone
229  */
230 struct rte_ring *rte_ring_create(const char *name, unsigned count,
231                                  int socket_id, unsigned flags);
232
233 /**
234  * Set the default bulk count for enqueue/dequeue.
235  *
236  * The parameter *count* is the default number of bulk elements to
237  * get/put when using ``rte_ring_*_{en,de}queue_bulk()``. It must be
238  * greater than 0 and less than half of the ring size.
239  *
240  * @param r
241  *   A pointer to the ring structure.
242  * @param count
243  *   A new water mark value.
244  * @return
245  *   - 0: Success; default_bulk_count changed.
246  *   - -EINVAL: Invalid count value.
247  */
248 static inline int
249 rte_ring_set_bulk_count(struct rte_ring *r, unsigned count)
250 {
251         if (unlikely(count == 0 || count >= r->prod.size))
252                 return -EINVAL;
253
254         r->prod.bulk_default = r->cons.bulk_default = count;
255         return 0;
256 }
257
258 /**
259  * Get the default bulk count for enqueue/dequeue.
260  *
261  * @param r
262  *   A pointer to the ring structure.
263  * @return
264  *   The default bulk count for enqueue/dequeue.
265  */
266 static inline unsigned
267 rte_ring_get_bulk_count(struct rte_ring *r)
268 {
269         return r->prod.bulk_default;
270 }
271
272 /**
273  * Change the high water mark.
274  *
275  * If *count* is 0, water marking is disabled. Otherwise, it is set to the
276  * *count* value. The *count* value must be greater than 0 and less
277  * than the ring size.
278  *
279  * This function can be called at any time (not necessarilly at
280  * initialization).
281  *
282  * @param r
283  *   A pointer to the ring structure.
284  * @param count
285  *   The new water mark value.
286  * @return
287  *   - 0: Success; water mark changed.
288  *   - -EINVAL: Invalid water mark value.
289  */
290 int rte_ring_set_water_mark(struct rte_ring *r, unsigned count);
291
292 /**
293  * Dump the status of the ring to the console.
294  *
295  * @param r
296  *   A pointer to the ring structure.
297  */
298 void rte_ring_dump(const struct rte_ring *r);
299
300 /**
301  * Enqueue several objects on the ring (multi-producers safe).
302  *
303  * This function uses a "compare and set" instruction to move the
304  * producer index atomically.
305  *
306  * @param r
307  *   A pointer to the ring structure.
308  * @param obj_table
309  *   A pointer to a table of void * pointers (objects).
310  * @param n
311  *   The number of objects to add in the ring from the obj_table. The
312  *   value must be strictly positive.
313  * @return
314  *   - 0: Success; objects enqueue.
315  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
316  *     high water mark is exceeded.
317  *   - -ENOBUFS: Not enough room in the ring to enqueue, no object is enqueued.
318  */
319 static inline int
320 rte_ring_mp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
321                          unsigned n)
322 {
323         uint32_t prod_head, prod_next;
324         uint32_t cons_tail, free_entries;
325         int success;
326         unsigned i;
327         uint32_t mask = r->prod.mask;
328         int ret;
329
330         /* move prod.head atomically */
331         do {
332                 prod_head = r->prod.head;
333                 cons_tail = r->cons.tail;
334                 /* The subtraction is done between two unsigned 32bits value
335                  * (the result is always modulo 32 bits even if we have
336                  * prod_head > cons_tail). So 'free_entries' is always between 0
337                  * and size(ring)-1. */
338                 free_entries = (mask + cons_tail - prod_head);
339
340                 /* check that we have enough room in ring */
341                 if (unlikely(n > free_entries)) {
342                         __RING_STAT_ADD(r, enq_fail, n);
343                         return -ENOBUFS;
344                 }
345
346                 prod_next = prod_head + n;
347                 success = rte_atomic32_cmpset(&r->prod.head, prod_head,
348                                               prod_next);
349         } while (unlikely(success == 0));
350
351         /* write entries in ring */
352         for (i = 0; likely(i < n); i++)
353                 r->ring[(prod_head + i) & mask] = obj_table[i];
354         rte_wmb();
355
356         /* return -EDQUOT if we exceed the watermark */
357         if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
358                 ret = -EDQUOT;
359                 __RING_STAT_ADD(r, enq_quota, n);
360         }
361         else {
362                 ret = 0;
363                 __RING_STAT_ADD(r, enq_success, n);
364         }
365
366         /*
367          * If there are other enqueues in progress that preceeded us,
368          * we need to wait for them to complete
369          */
370         while (unlikely(r->prod.tail != prod_head))
371                 rte_pause();
372
373         r->prod.tail = prod_next;
374         return ret;
375 }
376
377 /**
378  * Enqueue several objects on a ring (NOT multi-producers safe).
379  *
380  * @param r
381  *   A pointer to the ring structure.
382  * @param obj_table
383  *   A pointer to a table of void * pointers (objects).
384  * @param n
385  *   The number of objects to add in the ring from the obj_table. The
386  *   value must be strictly positive.
387  * @return
388  *   - 0: Success; objects enqueued.
389  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
390  *     high water mark is exceeded.
391  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
392  */
393 static inline int
394 rte_ring_sp_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
395                          unsigned n)
396 {
397         uint32_t prod_head, cons_tail;
398         uint32_t prod_next, free_entries;
399         unsigned i;
400         uint32_t mask = r->prod.mask;
401         int ret;
402
403         prod_head = r->prod.head;
404         cons_tail = r->cons.tail;
405         /* The subtraction is done between two unsigned 32bits value
406          * (the result is always modulo 32 bits even if we have
407          * prod_head > cons_tail). So 'free_entries' is always between 0
408          * and size(ring)-1. */
409         free_entries = mask + cons_tail - prod_head;
410
411         /* check that we have enough room in ring */
412         if (unlikely(n > free_entries)) {
413                 __RING_STAT_ADD(r, enq_fail, n);
414                 return -ENOBUFS;
415         }
416
417         prod_next = prod_head + n;
418         r->prod.head = prod_next;
419
420         /* write entries in ring */
421         for (i = 0; likely(i < n); i++)
422                 r->ring[(prod_head + i) & mask] = obj_table[i];
423         rte_wmb();
424
425         /* return -EDQUOT if we exceed the watermark */
426         if (unlikely(((mask + 1) - free_entries + n) > r->prod.watermark)) {
427                 ret = -EDQUOT;
428                 __RING_STAT_ADD(r, enq_quota, n);
429         }
430         else {
431                 ret = 0;
432                 __RING_STAT_ADD(r, enq_success, n);
433         }
434
435         r->prod.tail = prod_next;
436         return ret;
437 }
438
439 /**
440  * Enqueue several objects on a ring.
441  *
442  * This function calls the multi-producer or the single-producer
443  * version depending on the default behavior that was specified at
444  * ring creation time (see flags).
445  *
446  * @param r
447  *   A pointer to the ring structure.
448  * @param obj_table
449  *   A pointer to a table of void * pointers (objects).
450  * @param n
451  *   The number of objects to add in the ring from the obj_table.
452  * @return
453  *   - 0: Success; objects enqueued.
454  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
455  *     high water mark is exceeded.
456  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
457  */
458 static inline int
459 rte_ring_enqueue_bulk(struct rte_ring *r, void * const *obj_table,
460                       unsigned n)
461 {
462         if (r->prod.sp_enqueue)
463                 return rte_ring_sp_enqueue_bulk(r, obj_table, n);
464         else
465                 return rte_ring_mp_enqueue_bulk(r, obj_table, n);
466 }
467
468 /**
469  * Enqueue one object on a ring (multi-producers safe).
470  *
471  * This function uses a "compare and set" instruction to move the
472  * producer index atomically.
473  *
474  * @param r
475  *   A pointer to the ring structure.
476  * @param obj
477  *   A pointer to the object to be added.
478  * @return
479  *   - 0: Success; objects enqueued.
480  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
481  *     high water mark is exceeded.
482  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
483  */
484 static inline int
485 rte_ring_mp_enqueue(struct rte_ring *r, void *obj)
486 {
487         return rte_ring_mp_enqueue_bulk(r, &obj, 1);
488 }
489
490 /**
491  * Enqueue one object on a ring (NOT multi-producers safe).
492  *
493  * @param r
494  *   A pointer to the ring structure.
495  * @param obj
496  *   A pointer to the object to be added.
497  * @return
498  *   - 0: Success; objects enqueued.
499  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
500  *     high water mark is exceeded.
501  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
502  */
503 static inline int
504 rte_ring_sp_enqueue(struct rte_ring *r, void *obj)
505 {
506         return rte_ring_sp_enqueue_bulk(r, &obj, 1);
507 }
508
509 /**
510  * Enqueue one object on a ring.
511  *
512  * This function calls the multi-producer or the single-producer
513  * version, depending on the default behaviour that was specified at
514  * ring creation time (see flags).
515  *
516  * @param r
517  *   A pointer to the ring structure.
518  * @param obj
519  *   A pointer to the object to be added.
520  * @return
521  *   - 0: Success; objects enqueued.
522  *   - -EDQUOT: Quota exceeded. The objects have been enqueued, but the
523  *     high water mark is exceeded.
524  *   - -ENOBUFS: Not enough room in the ring to enqueue; no object is enqueued.
525  */
526 static inline int
527 rte_ring_enqueue(struct rte_ring *r, void *obj)
528 {
529         if (r->prod.sp_enqueue)
530                 return rte_ring_sp_enqueue(r, obj);
531         else
532                 return rte_ring_mp_enqueue(r, obj);
533 }
534
535 /**
536  * Dequeue several objects from a ring (multi-consumers safe).
537  *
538  * This function uses a "compare and set" instruction to move the
539  * consumer index atomically.
540  *
541  * @param r
542  *   A pointer to the ring structure.
543  * @param obj_table
544  *   A pointer to a table of void * pointers (objects) that will be filled.
545  * @param n
546  *   The number of objects to dequeue from the ring to the obj_table,
547  *   must be strictly positive
548  * @return
549  *   - 0: Success; objects dequeued.
550  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
551  *     dequeued.
552  */
553 static inline int
554 rte_ring_mc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
555 {
556         uint32_t cons_head, prod_tail;
557         uint32_t cons_next, entries;
558         int success;
559         unsigned i;
560         uint32_t mask = r->prod.mask;
561
562         /* move cons.head atomically */
563         do {
564                 cons_head = r->cons.head;
565                 prod_tail = r->prod.tail;
566                 /* The subtraction is done between two unsigned 32bits value
567                  * (the result is always modulo 32 bits even if we have
568                  * cons_head > prod_tail). So 'entries' is always between 0
569                  * and size(ring)-1. */
570                 entries = (prod_tail - cons_head);
571
572                 /* check that we have enough entries in ring */
573                 if (unlikely(n > entries)) {
574                         __RING_STAT_ADD(r, deq_fail, n);
575                         return -ENOENT;
576                 }
577
578                 cons_next = cons_head + n;
579                 success = rte_atomic32_cmpset(&r->cons.head, cons_head,
580                                               cons_next);
581         } while (unlikely(success == 0));
582
583         /* copy in table */
584         rte_rmb();
585         for (i = 0; likely(i < n); i++) {
586                 obj_table[i] = r->ring[(cons_head + i) & mask];
587         }
588
589         /*
590          * If there are other dequeues in progress that preceeded us,
591          * we need to wait for them to complete
592          */
593         while (unlikely(r->cons.tail != cons_head))
594                 rte_pause();
595
596         __RING_STAT_ADD(r, deq_success, n);
597         r->cons.tail = cons_next;
598         return 0;
599 }
600
601 /**
602  * Dequeue several objects from a ring (NOT multi-consumers safe).
603  *
604  * @param r
605  *   A pointer to the ring structure.
606  * @param obj_table
607  *   A pointer to a table of void * pointers (objects) that will be filled.
608  * @param n
609  *   The number of objects to dequeue from the ring to the obj_table,
610  *   must be strictly positive.
611  * @return
612  *   - 0: Success; objects dequeued.
613  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
614  *     dequeued.
615  */
616 static inline int
617 rte_ring_sc_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
618 {
619         uint32_t cons_head, prod_tail;
620         uint32_t cons_next, entries;
621         unsigned i;
622         uint32_t mask = r->prod.mask;
623
624         cons_head = r->cons.head;
625         prod_tail = r->prod.tail;
626         /* The subtraction is done between two unsigned 32bits value
627          * (the result is always modulo 32 bits even if we have
628          * cons_head > prod_tail). So 'entries' is always between 0
629          * and size(ring)-1. */
630         entries = prod_tail - cons_head;
631
632         /* check that we have enough entries in ring */
633         if (unlikely(n > entries)) {
634                 __RING_STAT_ADD(r, deq_fail, n);
635                 return -ENOENT;
636         }
637
638         cons_next = cons_head + n;
639         r->cons.head = cons_next;
640
641         /* copy in table */
642         rte_rmb();
643         for (i = 0; likely(i < n); i++) {
644                 obj_table[i] = r->ring[(cons_head + i) & mask];
645         }
646
647         __RING_STAT_ADD(r, deq_success, n);
648         r->cons.tail = cons_next;
649         return 0;
650 }
651
652 /**
653  * Dequeue several objects from a ring.
654  *
655  * This function calls the multi-consumers or the single-consumer
656  * version, depending on the default behaviour that was specified at
657  * ring creation time (see flags).
658  *
659  * @param r
660  *   A pointer to the ring structure.
661  * @param obj_table
662  *   A pointer to a table of void * pointers (objects) that will be filled.
663  * @param n
664  *   The number of objects to dequeue from the ring to the obj_table.
665  * @return
666  *   - 0: Success; objects dequeued.
667  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
668  *     dequeued.
669  */
670 static inline int
671 rte_ring_dequeue_bulk(struct rte_ring *r, void **obj_table, unsigned n)
672 {
673         if (r->cons.sc_dequeue)
674                 return rte_ring_sc_dequeue_bulk(r, obj_table, n);
675         else
676                 return rte_ring_mc_dequeue_bulk(r, obj_table, n);
677 }
678
679 /**
680  * Dequeue one object from a ring (multi-consumers safe).
681  *
682  * This function uses a "compare and set" instruction to move the
683  * consumer index atomically.
684  *
685  * @param r
686  *   A pointer to the ring structure.
687  * @param obj_p
688  *   A pointer to a void * pointer (object) that will be filled.
689  * @return
690  *   - 0: Success; objects dequeued.
691  *   - -ENOENT: Not enough entries in the ring to dequeue; no object is
692  *     dequeued.
693  */
694 static inline int
695 rte_ring_mc_dequeue(struct rte_ring *r, void **obj_p)
696 {
697         return rte_ring_mc_dequeue_bulk(r, obj_p, 1);
698 }
699
700 /**
701  * Dequeue one object from a ring (NOT multi-consumers safe).
702  *
703  * @param r
704  *   A pointer to the ring structure.
705  * @param obj_p
706  *   A pointer to a void * pointer (object) that will be filled.
707  * @return
708  *   - 0: Success; objects dequeued.
709  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
710  *     dequeued.
711  */
712 static inline int
713 rte_ring_sc_dequeue(struct rte_ring *r, void **obj_p)
714 {
715         return rte_ring_sc_dequeue_bulk(r, obj_p, 1);
716 }
717
718 /**
719  * Dequeue one object from a ring.
720  *
721  * This function calls the multi-consumers or the single-consumer
722  * version depending on the default behaviour that was specified at
723  * ring creation time (see flags).
724  *
725  * @param r
726  *   A pointer to the ring structure.
727  * @param obj_p
728  *   A pointer to a void * pointer (object) that will be filled.
729  * @return
730  *   - 0: Success, objects dequeued.
731  *   - -ENOENT: Not enough entries in the ring to dequeue, no object is
732  *     dequeued.
733  */
734 static inline int
735 rte_ring_dequeue(struct rte_ring *r, void **obj_p)
736 {
737         if (r->cons.sc_dequeue)
738                 return rte_ring_sc_dequeue(r, obj_p);
739         else
740                 return rte_ring_mc_dequeue(r, obj_p);
741 }
742
743 /**
744  * Test if a ring is full.
745  *
746  * @param r
747  *   A pointer to the ring structure.
748  * @return
749  *   - 1: The ring is full.
750  *   - 0: The ring is not full.
751  */
752 static inline int
753 rte_ring_full(const struct rte_ring *r)
754 {
755         uint32_t prod_tail = r->prod.tail;
756         uint32_t cons_tail = r->cons.tail;
757         return (((cons_tail - prod_tail - 1) & r->prod.mask) == 0);
758 }
759
760 /**
761  * Test if a ring is empty.
762  *
763  * @param r
764  *   A pointer to the ring structure.
765  * @return
766  *   - 1: The ring is empty.
767  *   - 0: The ring is not empty.
768  */
769 static inline int
770 rte_ring_empty(const struct rte_ring *r)
771 {
772         uint32_t prod_tail = r->prod.tail;
773         uint32_t cons_tail = r->cons.tail;
774         return !!(cons_tail == prod_tail);
775 }
776
777 /**
778  * Return the number of entries in a ring.
779  *
780  * @param r
781  *   A pointer to the ring structure.
782  * @return
783  *   The number of entries in the ring.
784  */
785 static inline unsigned
786 rte_ring_count(const struct rte_ring *r)
787 {
788         uint32_t prod_tail = r->prod.tail;
789         uint32_t cons_tail = r->cons.tail;
790         return ((prod_tail - cons_tail) & r->prod.mask);
791 }
792
793 /**
794  * Return the number of free entries in a ring.
795  *
796  * @param r
797  *   A pointer to the ring structure.
798  * @return
799  *   The number of free entries in the ring.
800  */
801 static inline unsigned
802 rte_ring_free_count(const struct rte_ring *r)
803 {
804         uint32_t prod_tail = r->prod.tail;
805         uint32_t cons_tail = r->cons.tail;
806         return ((cons_tail - prod_tail - 1) & r->prod.mask);
807 }
808
809 /**
810  * Dump the status of all rings on the console
811  */
812 void rte_ring_list_dump(void);
813
814 /**
815  * Search a ring from its name
816  *
817  * @param name
818  *   The name of the ring.
819  * @return
820  *   The pointer to the ring matching the name, or NULL if not found,
821  *   with rte_errno set appropriately. Possible rte_errno values include:
822  *    - ENOENT - required entry not available to return.
823  */
824 struct rte_ring *rte_ring_lookup(const char *name);
825
826 #ifdef __cplusplus
827 }
828 #endif
829
830 #endif /* _RTE_RING_H_ */