]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/dpdk/doc/guides/prog_guide/ring_lib.rst
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / dpdk / doc / guides / prog_guide / ring_lib.rst
CommitLineData
9f95a23c
TL
1.. SPDX-License-Identifier: BSD-3-Clause
2 Copyright(c) 2010-2014 Intel Corporation.
7c673cae
FG
3
4.. _Ring_Library:
5
6Ring Library
7============
8
9The ring allows the management of queues.
10Instead of having a linked list of infinite size, the rte_ring has the following properties:
11
12* FIFO
13
14* Maximum size is fixed, the pointers are stored in a table
15
16* Lockless implementation
17
18* Multi-consumer or single-consumer dequeue
19
20* Multi-producer or single-producer enqueue
21
22* Bulk dequeue - Dequeues the specified count of objects if successful; otherwise fails
23
24* Bulk enqueue - Enqueues the specified count of objects if successful; otherwise fails
25
26* Burst dequeue - Dequeue the maximum available objects if the specified count cannot be fulfilled
27
28* Burst enqueue - Enqueue the maximum available objects if the specified count cannot be fulfilled
29
30The advantages of this data structure over a linked list queue are as follows:
31
32* Faster; only requires a single Compare-And-Swap instruction of sizeof(void \*) instead of several double-Compare-And-Swap instructions.
33
34* Simpler than a full lockless queue.
35
36* Adapted to bulk enqueue/dequeue operations.
37 As pointers are stored in a table, a dequeue of several objects will not produce as many cache misses as in a linked queue.
38 Also, a bulk dequeue of many objects does not cost more than a dequeue of a simple object.
39
40The disadvantages:
41
42* Size is fixed
43
44* Having many rings costs more in terms of memory than a linked list queue. An empty ring contains at least N pointers.
45
46A simplified representation of a Ring is shown in with consumer and producer head and tail pointers to objects stored in the data structure.
47
48.. _figure_ring1:
49
50.. figure:: img/ring1.*
51
52 Ring Structure
53
54
55References for Ring Implementation in FreeBSD*
56----------------------------------------------
57
58The following code was added in FreeBSD 8.0, and is used in some network device drivers (at least in Intel drivers):
59
60 * `bufring.h in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/sys/buf_ring.h?revision=199625&amp;view=markup>`_
61
62 * `bufring.c in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/kern/subr_bufring.c?revision=199625&amp;view=markup>`_
63
64Lockless Ring Buffer in Linux*
65------------------------------
66
67The following is a link describing the `Linux Lockless Ring Buffer Design <http://lwn.net/Articles/340400/>`_.
68
69Additional Features
70-------------------
71
72Name
73~~~~
74
75A ring is identified by a unique name.
76It is not possible to create two rings with the same name (rte_ring_create() returns NULL if this is attempted).
77
7c673cae
FG
78Use Cases
79---------
80
81Use cases for the Ring library include:
82
83 * Communication between applications in the DPDK
84
85 * Used by memory pool allocator
86
87Anatomy of a Ring Buffer
88------------------------
89
90This section explains how a ring buffer operates.
91The ring structure is composed of two head and tail couples; one is used by producers and one is used by the consumers.
92The figures of the following sections refer to them as prod_head, prod_tail, cons_head and cons_tail.
93
94Each figure represents a simplified state of the ring, which is a circular buffer.
95The content of the function local variables is represented on the top of the figure,
96and the content of ring structure is represented on the bottom of the figure.
97
98Single Producer Enqueue
99~~~~~~~~~~~~~~~~~~~~~~~
100
101This section explains what occurs when a producer adds an object to the ring.
102In this example, only the producer head and tail (prod_head and prod_tail) are modified,
103and there is only one producer.
104
105The initial state is to have a prod_head and prod_tail pointing at the same location.
106
107Enqueue First Step
108^^^^^^^^^^^^^^^^^^
109
110First, *ring->prod_head* and ring->cons_tail are copied in local variables.
111The prod_next local variable points to the next element of the table, or several elements after in case of bulk enqueue.
112
113If there is not enough room in the ring (this is detected by checking cons_tail), it returns an error.
114
115
116.. _figure_ring-enqueue1:
117
118.. figure:: img/ring-enqueue1.*
119
120 Enqueue first step
121
122
123Enqueue Second Step
124^^^^^^^^^^^^^^^^^^^
125
126The second step is to modify *ring->prod_head* in ring structure to point to the same location as prod_next.
127
128A pointer to the added object is copied in the ring (obj4).
129
130
131.. _figure_ring-enqueue2:
132
133.. figure:: img/ring-enqueue2.*
134
135 Enqueue second step
136
137
138Enqueue Last Step
139^^^^^^^^^^^^^^^^^
140
141Once 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*.
142The enqueue operation is finished.
143
144
145.. _figure_ring-enqueue3:
146
147.. figure:: img/ring-enqueue3.*
148
149 Enqueue last step
150
151
152Single Consumer Dequeue
153~~~~~~~~~~~~~~~~~~~~~~~
154
155This section explains what occurs when a consumer dequeues an object from the ring.
156In this example, only the consumer head and tail (cons_head and cons_tail) are modified and there is only one consumer.
157
158The initial state is to have a cons_head and cons_tail pointing at the same location.
159
160Dequeue First Step
161^^^^^^^^^^^^^^^^^^
162
163First, ring->cons_head and ring->prod_tail are copied in local variables.
164The cons_next local variable points to the next element of the table, or several elements after in the case of bulk dequeue.
165
166If there are not enough objects in the ring (this is detected by checking prod_tail), it returns an error.
167
168
169.. _figure_ring-dequeue1:
170
171.. figure:: img/ring-dequeue1.*
172
173 Dequeue last step
174
175
176Dequeue Second Step
177^^^^^^^^^^^^^^^^^^^
178
179The second step is to modify ring->cons_head in the ring structure to point to the same location as cons_next.
180
181The pointer to the dequeued object (obj1) is copied in the pointer given by the user.
182
183
184.. _figure_ring-dequeue2:
185
186.. figure:: img/ring-dequeue2.*
187
188 Dequeue second step
189
190
191Dequeue Last Step
192^^^^^^^^^^^^^^^^^
193
194Finally, ring->cons_tail in the ring structure is modified to point to the same location as ring->cons_head.
195The dequeue operation is finished.
196
197
198.. _figure_ring-dequeue3:
199
200.. figure:: img/ring-dequeue3.*
201
202 Dequeue last step
203
204
205Multiple Producers Enqueue
206~~~~~~~~~~~~~~~~~~~~~~~~~~
207
208This section explains what occurs when two producers concurrently add an object to the ring.
209In this example, only the producer head and tail (prod_head and prod_tail) are modified.
210
211The initial state is to have a prod_head and prod_tail pointing at the same location.
212
213Multiple Producers Enqueue First Step
214^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
215
216On both cores, *ring->prod_head* and ring->cons_tail are copied in local variables.
217The prod_next local variable points to the next element of the table,
218or several elements after in the case of bulk enqueue.
219
220If there is not enough room in the ring (this is detected by checking cons_tail), it returns an error.
221
222
223.. _figure_ring-mp-enqueue1:
224
225.. figure:: img/ring-mp-enqueue1.*
226
227 Multiple producer enqueue first step
228
229
230Multiple Producers Enqueue Second Step
231^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
232
233The second step is to modify ring->prod_head in the ring structure to point to the same location as prod_next.
234This operation is done using a Compare And Swap (CAS) instruction, which does the following operations atomically:
235
236* If ring->prod_head is different to local variable prod_head,
237 the CAS operation fails, and the code restarts at first step.
238
239* Otherwise, ring->prod_head is set to local prod_next,
240 the CAS operation is successful, and processing continues.
241
242In the figure, the operation succeeded on core 1, and step one restarted on core 2.
243
244
245.. _figure_ring-mp-enqueue2:
246
247.. figure:: img/ring-mp-enqueue2.*
248
249 Multiple producer enqueue second step
250
251
252Multiple Producers Enqueue Third Step
253^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
254
255The CAS operation is retried on core 2 with success.
256
257The core 1 updates one element of the ring(obj4), and the core 2 updates another one (obj5).
258
259
260.. _figure_ring-mp-enqueue3:
261
262.. figure:: img/ring-mp-enqueue3.*
263
264 Multiple producer enqueue third step
265
266
267Multiple Producers Enqueue Fourth Step
268^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
269
270Each core now wants to update ring->prod_tail.
271A core can only update it if ring->prod_tail is equal to the prod_head local variable.
272This is only true on core 1. The operation is finished on core 1.
273
274
275.. _figure_ring-mp-enqueue4:
276
277.. figure:: img/ring-mp-enqueue4.*
278
279 Multiple producer enqueue fourth step
280
281
282Multiple Producers Enqueue Last Step
283^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
284
285Once ring->prod_tail is updated by core 1, core 2 is allowed to update it too.
286The operation is also finished on core 2.
287
288
289.. _figure_ring-mp-enqueue5:
290
291.. figure:: img/ring-mp-enqueue5.*
292
293 Multiple producer enqueue last step
294
295
296Modulo 32-bit Indexes
297~~~~~~~~~~~~~~~~~~~~~
298
299In the preceding figures, the prod_head, prod_tail, cons_head and cons_tail indexes are represented by arrows.
300In the actual implementation, these values are not between 0 and size(ring)-1 as would be assumed.
301The indexes are between 0 and 2^32 -1, and we mask their value when we access the pointer table (the ring itself).
30232-bit modulo also implies that operations on indexes (such as, add/subtract) will automatically do 2^32 modulo
303if the result overflows the 32-bit number range.
304
305The following are two examples that help to explain how indexes are used in a ring.
306
307.. note::
308
309 To simplify the explanation, operations with modulo 16-bit are used instead of modulo 32-bit.
310 In addition, the four indexes are defined as unsigned 16-bit integers,
311 as opposed to unsigned 32-bit integers in the more realistic case.
312
313
314.. _figure_ring-modulo1:
315
316.. figure:: img/ring-modulo1.*
317
318 Modulo 32-bit indexes - Example 1
319
320
321This ring contains 11000 entries.
322
323
324.. _figure_ring-modulo2:
325
326.. figure:: img/ring-modulo2.*
327
328 Modulo 32-bit indexes - Example 2
329
330
331This ring contains 12536 entries.
332
333.. note::
334
335 For ease of understanding, we use modulo 65536 operations in the above examples.
336 In real execution cases, this is redundant for low efficiency, but is done automatically when the result overflows.
337
338The code always maintains a distance between producer and consumer between 0 and size(ring)-1.
339Thanks to this property, we can do subtractions between 2 index values in a modulo-32bit base:
340that's why the overflow of the indexes is not a problem.
341
342At any time, entries and free_entries are between 0 and size(ring)-1,
343even if only the first term of subtraction has overflowed:
344
345.. code-block:: c
346
347 uint32_t entries = (prod_tail - cons_head);
348 uint32_t free_entries = (mask + cons_tail -prod_head);
349
350References
351----------
352
353 * `bufring.h in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/sys/buf_ring.h?revision=199625&amp;view=markup>`_ (version 8)
354
355 * `bufring.c in FreeBSD <http://svn.freebsd.org/viewvc/base/release/8.0.0/sys/kern/subr_bufring.c?revision=199625&amp;view=markup>`_ (version 8)
356
357 * `Linux Lockless Ring Buffer Design <http://lwn.net/Articles/340400/>`_