1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2010-2014 Intel Corporation. 3 4Ring Library 5============ 6 7The ring allows the management of queues. 8Instead of having a linked list of infinite size, the rte_ring has the following properties: 9 10* FIFO 11 12* Maximum size is fixed, the objects are stored in a table 13 14* Objects can be pointers or elements of multiple of 4 byte size 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 32 bit Compare-And-Swap instruction instead of several pointer size Compare-And-Swap instructions. 33 34* Simpler than a full lockless queue. 35 36* Adapted to bulk enqueue/dequeue operations. 37 As objects 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 objects. 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&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&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 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 128The 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 first 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 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 object 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 350Producer/consumer synchronization modes 351--------------------------------------- 352 353rte_ring supports different synchronization modes for producers and consumers. 354These modes can be specified at ring creation/init time via ``flags`` 355parameter. 356That should help users to configure ring in the most suitable way for his 357specific usage scenarios. 358Currently supported modes: 359 360.. _Ring_Library_MPMC_Mode: 361 362MP/MC (default one) 363~~~~~~~~~~~~~~~~~~~ 364 365Multi-producer (/multi-consumer) mode. This is a default enqueue (/dequeue) 366mode for the ring. In this mode multiple threads can enqueue (/dequeue) 367objects to (/from) the ring. For 'classic' DPDK deployments (with one thread 368per core) this is usually the most suitable and fastest synchronization mode. 369As a well known limitation - it can perform quite pure on some overcommitted 370scenarios. 371 372.. _Ring_Library_SPSC_Mode: 373 374SP/SC 375~~~~~ 376Single-producer (/single-consumer) mode. In this mode only one thread at a time 377is allowed to enqueue (/dequeue) objects to (/from) the ring. 378 379.. _Ring_Library_MT_RTS_Mode: 380 381MP_RTS/MC_RTS 382~~~~~~~~~~~~~ 383 384Multi-producer (/multi-consumer) with Relaxed Tail Sync (RTS) mode. 385The main difference from the original MP/MC algorithm is that 386tail value is increased not by every thread that finished enqueue/dequeue, 387but only by the last one. 388That allows threads to avoid spinning on ring tail value, 389leaving actual tail value change to the last thread at a given instance. 390That technique helps to avoid the Lock-Waiter-Preemption (LWP) problem on tail 391update and improves average enqueue/dequeue times on overcommitted systems. 392To achieve that RTS requires 2 64-bit CAS for each enqueue(/dequeue) operation: 393one for head update, second for tail update. 394In comparison the original MP/MC algorithm requires one 32-bit CAS 395for head update and waiting/spinning on tail value. 396 397.. _Ring_Library_MT_HTS_Mode: 398 399MP_HTS/MC_HTS 400~~~~~~~~~~~~~ 401 402Multi-producer (/multi-consumer) with Head/Tail Sync (HTS) mode. 403In that mode enqueue/dequeue operation is fully serialized: 404at any given moment only one enqueue/dequeue operation can proceed. 405This is achieved by allowing a thread to proceed with changing ``head.value`` 406only when ``head.value == tail.value``. 407Both head and tail values are updated atomically (as one 64-bit value). 408To achieve that 64-bit CAS is used by head update routine. 409That technique also avoids the Lock-Waiter-Preemption (LWP) problem on tail 410update and helps to improve ring enqueue/dequeue behavior in overcommitted 411scenarios. Another advantage of fully serialized producer/consumer - 412it provides the ability to implement MT safe peek API for rte_ring. 413 414Ring Peek API 415------------- 416 417For ring with serialized producer/consumer (HTS sync mode) it is possible 418to split public enqueue/dequeue API into two phases: 419 420* enqueue/dequeue start 421 422* enqueue/dequeue finish 423 424That allows user to inspect objects in the ring without removing them 425from it (aka MT safe peek) and reserve space for the objects in the ring 426before actual enqueue. 427Note that this API is available only for two sync modes: 428 429* Single Producer/Single Consumer (SP/SC) 430 431* Multi-producer/Multi-consumer with Head/Tail Sync (HTS) 432 433It is a user responsibility to create/init ring with appropriate sync modes 434selected. As an example of usage: 435 436.. code-block:: c 437 438 /* read 1 elem from the ring: */ 439 uint32_t n = rte_ring_dequeue_bulk_start(ring, &obj, 1, NULL); 440 if (n != 0) { 441 /* examine object */ 442 if (object_examine(obj) == KEEP) 443 /* decided to keep it in the ring. */ 444 rte_ring_dequeue_finish(ring, 0); 445 else 446 /* decided to remove it from the ring. */ 447 rte_ring_dequeue_finish(ring, n); 448 } 449 450Note that between ``_start_`` and ``_finish_`` none other thread can proceed 451with enqueue(/dequeue) operation till ``_finish_`` completes. 452 453Ring Peek Zero Copy API 454----------------------- 455 456Along with the advantages of the peek APIs, zero copy APIs provide the ability 457to copy the data to the ring memory directly without the need for temporary 458storage (for ex: array of mbufs on the stack). 459 460These APIs make it possible to split public enqueue/dequeue API into 3 phases: 461 462* enqueue/dequeue start 463 464* copy data to/from the ring 465 466* enqueue/dequeue finish 467 468Note that this API is available only for two sync modes: 469 470* Single Producer/Single Consumer (SP/SC) 471 472* Multi-producer/Multi-consumer with Head/Tail Sync (HTS) 473 474It is a user responsibility to create/init ring with appropriate sync modes. 475Following is an example of usage: 476 477.. code-block:: c 478 479 /* Reserve space on the ring */ 480 n = rte_ring_enqueue_zc_burst_start(r, 32, &zcd, NULL); 481 /* Pkt I/O core polls packets from the NIC */ 482 if (n != 0) { 483 nb_rx = rte_eth_rx_burst(portid, queueid, zcd->ptr1, zcd->n1); 484 if (nb_rx == zcd->n1 && n != zcd->n1) 485 nb_rx += rte_eth_rx_burst(portid, queueid, zcd->ptr2, 486 n - zcd->n1); 487 /* Provide packets to the packet processing cores */ 488 rte_ring_enqueue_zc_finish(r, nb_rx); 489 } 490 491Note that between ``_start_`` and ``_finish_`` no other thread can proceed 492with enqueue(/dequeue) operation till ``_finish_`` completes. 493 494References 495---------- 496 497 * `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) 498 499 * `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) 500 501 * `Linux Lockless Ring Buffer Design <http://lwn.net/Articles/340400/>`_ 502