xref: /dpdk/doc/guides/prog_guide/packet_distrib_lib.rst (revision 5630257fcc30397e7217139ec55da4f301f59fb7)
1*5630257fSFerruh Yigit..  SPDX-License-Identifier: BSD-3-Clause
2*5630257fSFerruh Yigit    Copyright(c) 2010-2014 Intel Corporation.
3fc1f2750SBernard Iremonger
4fc1f2750SBernard IremongerPacket Distributor Library
5fc1f2750SBernard Iremonger==========================
6fc1f2750SBernard Iremonger
748624fd9SSiobhan ButlerThe DPDK Packet Distributor library is a library designed to be used for dynamic load balancing of traffic
8fc1f2750SBernard Iremongerwhile supporting single packet at a time operation.
9fc1f2750SBernard IremongerWhen using this library, the logical cores in use are to be considered in two roles: firstly a distributor lcore,
10fc1f2750SBernard Iremongerwhich is responsible for load balancing or distributing packets,
11fc1f2750SBernard Iremongerand a set of worker lcores which are responsible for receiving the packets from the distributor and operating on them.
12fc1f2750SBernard IremongerThe model of operation is shown in the diagram below.
13fc1f2750SBernard Iremonger
144a22e6eeSJohn McNamara.. figure:: img/packet_distributor1.*
154a22e6eeSJohn McNamara
164a22e6eeSJohn McNamara   Packet Distributor mode of operation
174a22e6eeSJohn McNamara
187e0bb299SDavid HuntThere are two modes of operation of the API in the distributor library,
197e0bb299SDavid Huntone which sends one packet at a time to workers using 32-bits for flow_id,
207e0bb299SDavid Huntand an optimized mode which sends bursts of up to 8 packets at a time to workers, using 15 bits of flow_id.
217e0bb299SDavid HuntThe mode is selected by the type field in the ``rte_distributor_create()`` function.
22fc1f2750SBernard Iremonger
23fc1f2750SBernard IremongerDistributor Core Operation
24fc1f2750SBernard Iremonger--------------------------
25fc1f2750SBernard Iremonger
26fc1f2750SBernard IremongerThe distributor core does the majority of the processing for ensuring that packets are fairly shared among workers.
27fc1f2750SBernard IremongerThe operation of the distributor is as follows:
28fc1f2750SBernard Iremonger
29fc1f2750SBernard Iremonger#.  Packets are passed to the distributor component by having the distributor lcore thread call the "rte_distributor_process()" API
30fc1f2750SBernard Iremonger
31fc1f2750SBernard Iremonger#.  The worker lcores all share a single cache line with the distributor core in order to pass messages and packets to and from the worker.
32fc1f2750SBernard Iremonger    The process API call will poll all the worker cache lines to see what workers are requesting packets.
33fc1f2750SBernard Iremonger
34fc1f2750SBernard Iremonger#.  As workers request packets, the distributor takes packets from the set of packets passed in and distributes them to the workers.
35fc1f2750SBernard Iremonger    As it does so, it examines the "tag" -- stored in the RSS hash field in the mbuf -- for each packet
36fc1f2750SBernard Iremonger    and records what tags are being processed by each  worker.
37fc1f2750SBernard Iremonger
38fc1f2750SBernard Iremonger#.  If the next packet in the input set has a tag which is already being processed by a worker,
39fc1f2750SBernard Iremonger    then that packet will be queued up for processing by that worker
40fc1f2750SBernard Iremonger    and given to it in preference to other packets when that work next makes a request for work.
41fc1f2750SBernard Iremonger    This ensures that no two packets with the same tag are processed in parallel,
42fc1f2750SBernard Iremonger    and that all packets with the same tag are processed in input order.
43fc1f2750SBernard Iremonger
44fc1f2750SBernard Iremonger#.  Once all input packets passed to the process API have either been distributed to workers
45fc1f2750SBernard Iremonger    or been queued up for a worker which is processing a given tag,
46fc1f2750SBernard Iremonger    then the process API returns to the caller.
47fc1f2750SBernard Iremonger
48fc1f2750SBernard IremongerOther functions which are available to the distributor lcore are:
49fc1f2750SBernard Iremonger
50fc1f2750SBernard Iremonger*   rte_distributor_returned_pkts()
51fc1f2750SBernard Iremonger
52fc1f2750SBernard Iremonger*   rte_distributor_flush()
53fc1f2750SBernard Iremonger
54fc1f2750SBernard Iremonger*   rte_distributor_clear_returns()
55fc1f2750SBernard Iremonger
56fc1f2750SBernard IremongerOf these the most important API call is "rte_distributor_returned_pkts()"
57fc1f2750SBernard Iremongerwhich should only be called on the lcore which also calls the process API.
58fc1f2750SBernard IremongerIt returns to the caller all packets which have finished processing by all worker cores.
59fc1f2750SBernard IremongerWithin this set of returned packets, all packets sharing the same tag will be returned in their original order.
60fc1f2750SBernard Iremonger
61fc1f2750SBernard Iremonger**NOTE:**
62fc1f2750SBernard IremongerIf worker lcores buffer up packets internally for transmission in bulk afterwards,
63fc1f2750SBernard Iremongerthe packets sharing a tag will likely get out of order.
64fc1f2750SBernard IremongerOnce a worker lcore requests a new packet, the distributor assumes that it has completely finished with the previous packet and
65fc1f2750SBernard Iremongertherefore that additional packets with the same tag can safely be distributed to other workers --
66fc1f2750SBernard Iremongerwho may then flush their buffered packets sooner and cause packets to get out of order.
67fc1f2750SBernard Iremonger
68fc1f2750SBernard Iremonger**NOTE:**
69fc1f2750SBernard IremongerNo packet ordering guarantees are made about packets which do not share a common packet tag.
70fc1f2750SBernard Iremonger
71fc1f2750SBernard IremongerUsing the process and returned_pkts API, the following application workflow can be used,
72fc1f2750SBernard Iremongerwhile allowing packet order within a packet flow -- identified by a tag -- to be maintained.
73fc1f2750SBernard Iremonger
74fc1f2750SBernard Iremonger
754a22e6eeSJohn McNamara.. figure:: img/packet_distributor2.*
764a22e6eeSJohn McNamara
774a22e6eeSJohn McNamara   Application workflow
784a22e6eeSJohn McNamara
79fc1f2750SBernard Iremonger
80fc1f2750SBernard IremongerThe flush and clear_returns API calls, mentioned previously,
81fc1f2750SBernard Iremongerare likely of less use that the process and returned_pkts APIS, and are principally provided to aid in unit testing of the library.
8248624fd9SSiobhan ButlerDescriptions of these functions and their use can be found in the DPDK API Reference document.
83fc1f2750SBernard Iremonger
84fc1f2750SBernard IremongerWorker Operation
85fc1f2750SBernard Iremonger----------------
86fc1f2750SBernard Iremonger
87fc1f2750SBernard IremongerWorker cores are the cores which do the actual manipulation of the packets distributed by the packet distributor.
88fc1f2750SBernard IremongerEach worker calls "rte_distributor_get_pkt()" API to request a new packet when it has finished processing the previous one.
89fc1f2750SBernard Iremonger[The previous packet should be returned to the distributor component by passing it as the final parameter to this API call.]
90fc1f2750SBernard Iremonger
91fc1f2750SBernard IremongerSince it may be desirable to vary the number of worker cores, depending on the traffic load
92fc1f2750SBernard Iremongeri.e. to save power at times of lighter load,
93fc1f2750SBernard Iremongerit is possible to have a worker stop processing packets by calling "rte_distributor_return_pkt()" to indicate that
94fc1f2750SBernard Iremongerit has finished the current packet and does not want a new one.
95