2 Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
15 * Neither the name of Intel Corporation nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 The ring allows the management of queues.
37 Instead of having a linked list of infinite size, the rte_ring has the following properties:
41 * Maximum size is fixed, the pointers are stored in a table
43 * Lockless implementation
45 * Multi-consumer or single-consumer dequeue
47 * Multi-producer or single-producer enqueue
49 * Bulk dequeue - Dequeues the specified count of objects if successful; otherwise fails
51 * Bulk enqueue - Enqueues the specified count of objects if successful; otherwise fails
53 * Burst dequeue - Dequeue the maximum available objects if the specified count cannot be fulfilled
55 * Burst enqueue - Enqueue the maximum available objects if the specified count cannot be fulfilled
57 The advantages of this data structure over a linked list queue are as follows:
59 * Faster; only requires a single Compare-And-Swap instruction of sizeof(void \*) instead of several double-Compare-And-Swap instructions.
61 * Simpler than a full lockless queue.
63 * Adapted to bulk enqueue/dequeue operations.
64 As pointers are stored in a table, a dequeue of several objects will not produce as many cache misses as in a linked queue.
65 Also, a bulk dequeue of many objects does not cost more than a dequeue of a simple object.
71 * Having many rings costs more in terms of memory than a linked list queue. An empty ring contains at least N pointers.
73 A simplified representation of a Ring is shown in with consumer and producer head and tail pointers to objects stored in the data structure.
77 .. figure:: img/ring1.*
82 References for Ring Implementation in FreeBSD*
83 ----------------------------------------------
85 The following code was added in FreeBSD 8.0, and is used in some network device drivers (at least in Intel drivers):
87 * `bufring.h in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/sys/buf_ring.h?revision=199625&view=markup>`_
89 * `bufring.c in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/kern/subr_bufring.c?revision=199625&view=markup>`_
91 Lockless Ring Buffer in Linux*
92 ------------------------------
94 The following is a link describing the `Linux Lockless Ring Buffer Design <http://lwn.net/Articles/340400/>`_.
102 A ring is identified by a unique name.
103 It is not possible to create two rings with the same name (rte_ring_create() returns NULL if this is attempted).
108 The ring can have a high water mark (threshold).
109 Once an enqueue operation reaches the high water mark, the producer is notified, if the water mark is configured.
111 This mechanism can be used, for example, to exert a back pressure on I/O to inform the LAN to PAUSE.
116 Use cases for the Ring library include:
118 * Communication between applications in the DPDK
120 * Used by memory pool allocator
122 Anatomy of a Ring Buffer
123 ------------------------
125 This section explains how a ring buffer operates.
126 The ring structure is composed of two head and tail couples; one is used by producers and one is used by the consumers.
127 The figures of the following sections refer to them as prod_head, prod_tail, cons_head and cons_tail.
129 Each figure represents a simplified state of the ring, which is a circular buffer.
130 The content of the function local variables is represented on the top of the figure,
131 and the content of ring structure is represented on the bottom of the figure.
133 Single Producer Enqueue
134 ~~~~~~~~~~~~~~~~~~~~~~~
136 This section explains what occurs when a producer adds an object to the ring.
137 In this example, only the producer head and tail (prod_head and prod_tail) are modified,
138 and there is only one producer.
140 The initial state is to have a prod_head and prod_tail pointing at the same location.
145 First, *ring->prod_head* and ring->cons_tail are copied in local variables.
146 The prod_next local variable points to the next element of the table, or several elements after in case of bulk enqueue.
148 If there is not enough room in the ring (this is detected by checking cons_tail), it returns an error.
151 .. _figure_ring-enqueue1:
153 .. figure:: img/ring-enqueue1.*
161 The second step is to modify *ring->prod_head* in ring structure to point to the same location as prod_next.
163 A pointer to the added object is copied in the ring (obj4).
166 .. _figure_ring-enqueue2:
168 .. figure:: img/ring-enqueue2.*
176 Once the object is added in the ring, ring->prod_tail in the ring structure is modified to point to the same location as *ring->prod_head*.
177 The enqueue operation is finished.
180 .. _figure_ring-enqueue3:
182 .. figure:: img/ring-enqueue3.*
187 Single Consumer Dequeue
188 ~~~~~~~~~~~~~~~~~~~~~~~
190 This section explains what occurs when a consumer dequeues an object from the ring.
191 In this example, only the consumer head and tail (cons_head and cons_tail) are modified and there is only one consumer.
193 The initial state is to have a cons_head and cons_tail pointing at the same location.
198 First, ring->cons_head and ring->prod_tail are copied in local variables.
199 The cons_next local variable points to the next element of the table, or several elements after in the case of bulk dequeue.
201 If there are not enough objects in the ring (this is detected by checking prod_tail), it returns an error.
204 .. _figure_ring-dequeue1:
206 .. figure:: img/ring-dequeue1.*
214 The second step is to modify ring->cons_head in the ring structure to point to the same location as cons_next.
216 The pointer to the dequeued object (obj1) is copied in the pointer given by the user.
219 .. _figure_ring-dequeue2:
221 .. figure:: img/ring-dequeue2.*
229 Finally, ring->cons_tail in the ring structure is modified to point to the same location as ring->cons_head.
230 The dequeue operation is finished.
233 .. _figure_ring-dequeue3:
235 .. figure:: img/ring-dequeue3.*
240 Multiple Producers Enqueue
241 ~~~~~~~~~~~~~~~~~~~~~~~~~~
243 This section explains what occurs when two producers concurrently add an object to the ring.
244 In this example, only the producer head and tail (prod_head and prod_tail) are modified.
246 The initial state is to have a prod_head and prod_tail pointing at the same location.
248 Multiple Producers Enqueue First Step
249 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
251 On both cores, *ring->prod_head* and ring->cons_tail are copied in local variables.
252 The prod_next local variable points to the next element of the table,
253 or several elements after in the case of bulk enqueue.
255 If there is not enough room in the ring (this is detected by checking cons_tail), it returns an error.
258 .. _figure_ring-mp-enqueue1:
260 .. figure:: img/ring-mp-enqueue1.*
262 Multiple producer enqueue first step
265 Multiple Producers Enqueue Second Step
266 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
268 The second step is to modify ring->prod_head in the ring structure to point to the same location as prod_next.
269 This operation is done using a Compare And Swap (CAS) instruction, which does the following operations atomically:
271 * If ring->prod_head is different to local variable prod_head,
272 the CAS operation fails, and the code restarts at first step.
274 * Otherwise, ring->prod_head is set to local prod_next,
275 the CAS operation is successful, and processing continues.
277 In the figure, the operation succeeded on core 1, and step one restarted on core 2.
280 .. _figure_ring-mp-enqueue2:
282 .. figure:: img/ring-mp-enqueue2.*
284 Multiple producer enqueue second step
287 Multiple Producers Enqueue Third Step
288 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
290 The CAS operation is retried on core 2 with success.
292 The core 1 updates one element of the ring(obj4), and the core 2 updates another one (obj5).
295 .. _figure_ring-mp-enqueue3:
297 .. figure:: img/ring-mp-enqueue3.*
299 Multiple producer enqueue third step
302 Multiple Producers Enqueue Fourth Step
303 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
305 Each core now wants to update ring->prod_tail.
306 A core can only update it if ring->prod_tail is equal to the prod_head local variable.
307 This is only true on core 1. The operation is finished on core 1.
310 .. _figure_ring-mp-enqueue4:
312 .. figure:: img/ring-mp-enqueue4.*
314 Multiple producer enqueue fourth step
317 Multiple Producers Enqueue Last Step
318 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
320 Once ring->prod_tail is updated by core 1, core 2 is allowed to update it too.
321 The operation is also finished on core 2.
324 .. _figure_ring-mp-enqueue5:
326 .. figure:: img/ring-mp-enqueue5.*
328 Multiple producer enqueue last step
331 Modulo 32-bit Indexes
332 ~~~~~~~~~~~~~~~~~~~~~
334 In the preceding figures, the prod_head, prod_tail, cons_head and cons_tail indexes are represented by arrows.
335 In the actual implementation, these values are not between 0 and size(ring)-1 as would be assumed.
336 The indexes are between 0 and 2^32 -1, and we mask their value when we access the pointer table (the ring itself).
337 32-bit modulo also implies that operations on indexes (such as, add/subtract) will automatically do 2^32 modulo
338 if the result overflows the 32-bit number range.
340 The following are two examples that help to explain how indexes are used in a ring.
344 To simplify the explanation, operations with modulo 16-bit are used instead of modulo 32-bit.
345 In addition, the four indexes are defined as unsigned 16-bit integers,
346 as opposed to unsigned 32-bit integers in the more realistic case.
349 .. _figure_ring-modulo1:
351 .. figure:: img/ring-modulo1.*
353 Modulo 32-bit indexes - Example 1
356 This ring contains 11000 entries.
359 .. _figure_ring-modulo2:
361 .. figure:: img/ring-modulo2.*
363 Modulo 32-bit indexes - Example 2
366 This ring contains 12536 entries.
370 For ease of understanding, we use modulo 65536 operations in the above examples.
371 In real execution cases, this is redundant for low efficiency, but is done automatically when the result overflows.
373 The code always maintains a distance between producer and consumer between 0 and size(ring)-1.
374 Thanks to this property, we can do subtractions between 2 index values in a modulo-32bit base:
375 that's why the overflow of the indexes is not a problem.
377 At any time, entries and free_entries are between 0 and size(ring)-1,
378 even if only the first term of subtraction has overflowed:
382 uint32_t entries = (prod_tail - cons_head);
383 uint32_t free_entries = (mask + cons_tail -prod_head);
388 * `bufring.h in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/sys/buf_ring.h?revision=199625&view=markup>`_ (version 8)
390 * `bufring.c in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/kern/subr_bufring.c?revision=199625&view=markup>`_ (version 8)
392 * `Linux Lockless Ring Buffer Design <http://lwn.net/Articles/340400/>`_