xref: /dpdk/doc/guides/prog_guide/reorder_lib.rst (revision 41dd9a6bc2d9c6e20e139ad713cc9d172572dd43)
1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright(c) 2015 Intel Corporation.
3
4Reorder Library
5=================
6
7The Reorder Library provides a mechanism for reordering mbufs based on their
8sequence number.
9
10Operation
11----------
12
13The reorder library is essentially a buffer that reorders mbufs.
14The user inserts out of order mbufs into the reorder buffer and pulls in-order
15mbufs from it.
16
17At a given time, the reorder buffer contains mbufs whose sequence number are
18inside the sequence window. The sequence window is determined by the minimum
19sequence number and the number of entries that the buffer was configured to hold.
20For example, given a reorder buffer with 200 entries and a minimum sequence
21number of 350, the sequence window has low and high limits of 350 and 550
22respectively.
23
24When inserting mbufs, the reorder library differentiates between valid, early
25and late mbufs depending on the sequence number of the inserted mbuf:
26
27* valid: the sequence number is inside the window.
28* late: the sequence number is outside the window and less than the low limit.
29* early: the sequence number is outside the window and greater than the high
30  limit.
31
32The reorder buffer directly returns late mbufs and tries to accommodate early
33mbufs.
34
35
36Implementation Details
37-------------------------
38
39The reorder library is implemented as a pair of buffers, which referred to as
40the *Order* buffer and the *Ready* buffer.
41
42On an insert call, valid mbufs are inserted directly into the Order buffer and
43late mbufs are returned to the user with an error.
44
45In the case of early mbufs, the reorder buffer will try to move the window
46(incrementing the minimum sequence number) so that the mbuf becomes a valid one.
47To that end, mbufs in the Order buffer are moved into the Ready buffer.
48Any mbufs that have not arrived yet are ignored and therefore will become
49late mbufs.
50This means that as long as there is room in the Ready buffer, the window will
51be moved to accommodate early mbufs that would otherwise be outside the
52reordering window.
53
54For example, assuming that we have a buffer of 200 entries with a 350 minimum
55sequence number, and we need to insert an early mbuf with 565 sequence number.
56That means that we would need to move the windows at least 15 positions to
57accommodate the mbuf.
58The reorder buffer would try to move mbufs from at least the next 15 slots in
59the Order buffer to the Ready buffer, as long as there is room in the Ready buffer.
60Any gaps in the Order buffer at that point are skipped, and those packet will
61be reported as late packets when they arrive. The process of moving packets
62to the Ready buffer continues beyond the minimum required until a gap,
63i.e. missing mbuf, in the Order buffer is encountered.
64
65When draining mbufs, the reorder buffer would return  mbufs in the Ready
66buffer first and then from the Order buffer until a gap is found (mbufs that
67have not arrived yet).
68
69Use Case: Packet Distributor
70-------------------------------
71
72An application using the DPDK packet distributor could make use of the reorder
73library to transmit packets in the same order they were received.
74
75A basic packet distributor use case would consist of a distributor with
76multiple workers cores.
77The processing of packets by the workers is not guaranteed to be in order,
78hence a reorder buffer can be used to order as many packets as possible.
79
80In such a scenario, the distributor assigns a sequence number to mbufs before
81delivering them to the workers.
82As the workers finish processing the packets, the distributor inserts those
83mbufs into the reorder buffer and finally transmit drained mbufs.
84
85NOTE: Currently the reorder buffer is not thread safe so the same thread is
86responsible for inserting and draining mbufs.
87