xref: /dpdk/doc/guides/prog_guide/graph_lib.rst (revision f78c100bc87119c6a94130a6689d773afdaa9d98)
1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright(C) 2020 Marvell International Ltd.
3
4Graph Library and Inbuilt Nodes
5===============================
6
7Graph architecture abstracts the data processing functions as a ``node`` and
8``links`` them together to create a complex ``graph`` to enable reusable/modular
9data processing functions.
10
11The graph library provides API to enable graph framework operations such as
12create, lookup, dump and destroy on graph and node operations such as clone,
13edge update, and edge shrink, etc. The API also allows to create the stats
14cluster to monitor per graph and per node stats.
15
16Features
17--------
18
19Features of the Graph library are:
20
21- Nodes as plugins.
22- Support for out of tree nodes.
23- Inbuilt nodes for packet processing.
24- Multi-process support.
25- Low overhead graph walk and node enqueue.
26- Low overhead statistics collection infrastructure.
27- Support to export the graph as a Graphviz dot file. See ``rte_graph_export()``.
28- Allow having another graph walk implementation in the future by segregating
29  the fast path(``rte_graph_worker.h``) and slow path code.
30
31Advantages of Graph architecture
32--------------------------------
33
34- Memory latency is the enemy for high-speed packet processing, moving the
35  similar packet processing code to a node will reduce the I cache and D
36  caches misses.
37- Exploits the probability that most packets will follow the same nodes in the
38  graph.
39- Allow SIMD instructions for packet processing of the node.-
40- The modular scheme allows having reusable nodes for the consumers.
41- The modular scheme allows us to abstract the vendor HW specific
42  optimizations as a node.
43
44Performance tuning parameters
45-----------------------------
46
47- Test with various burst size values (256, 128, 64, 32) using
48  RTE_GRAPH_BURST_SIZE config option.
49  The testing shows, on x86 and arm64 servers, The sweet spot is 256 burst
50  size. While on arm64 embedded SoCs, it is either 64 or 128.
51- Disable node statistics (using ``RTE_LIBRTE_GRAPH_STATS`` config option)
52  if not needed.
53
54Programming model
55-----------------
56
57Anatomy of Node:
58~~~~~~~~~~~~~~~~
59
60.. _figure_anatomy_of_a_node:
61
62.. figure:: img/anatomy_of_a_node.*
63
64   Anatomy of a node
65
66The node is the basic building block of the graph framework.
67
68A node consists of:
69
70process():
71^^^^^^^^^^
72
73The callback function will be invoked by worker thread using
74``rte_graph_walk()`` function when there is data to be processed by the node.
75A graph node process the function using ``process()`` and enqueue to next
76downstream node using ``rte_node_enqueue*()`` function.
77
78Context memory:
79^^^^^^^^^^^^^^^
80
81It is memory allocated by the library to store the node-specific context
82information. This memory will be used by process(), init(), fini() callbacks.
83
84init():
85^^^^^^^
86
87The callback function will be invoked by ``rte_graph_create()`` on when
88a node gets attached to a graph.
89
90fini():
91^^^^^^^
92
93The callback function will be invoked by ``rte_graph_destroy()`` on when a
94node gets detached to a graph.
95
96Node name:
97^^^^^^^^^^
98
99It is the name of the node. When a node registers to graph library, the library
100gives the ID as ``rte_node_t`` type. Both ID or Name shall be used lookup the
101node. ``rte_node_from_name()``, ``rte_node_id_to_name()`` are the node
102lookup functions.
103
104nb_edges:
105^^^^^^^^^
106
107The number of downstream nodes connected to this node. The ``next_nodes[]``
108stores the downstream nodes objects. ``rte_node_edge_update()`` and
109``rte_node_edge_shrink()`` functions shall be used to update the ``next_node[]``
110objects. Consumers of the node APIs are free to update the ``next_node[]``
111objects till ``rte_graph_create()`` invoked.
112
113next_node[]:
114^^^^^^^^^^^^
115
116The dynamic array to store the downstream nodes connected to this node. Downstream
117node should not be current node itself or a source node.
118
119Source node:
120^^^^^^^^^^^^
121
122Source nodes are static nodes created using ``RTE_NODE_REGISTER`` by passing
123``flags`` as ``RTE_NODE_SOURCE_F``.
124While performing the graph walk, the ``process()`` function of all the source
125nodes will be called first. So that these nodes can be used as input nodes for a graph.
126
127Node creation and registration
128~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129* Node implementer creates the node by implementing ops and attributes of
130  ``struct rte_node_register``.
131
132* The library registers the node by invoking RTE_NODE_REGISTER on library load
133  using the constructor scheme. The constructor scheme used here to support multi-process.
134
135Link the Nodes to create the graph topology
136~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137.. _figure_link_the_nodes:
138
139.. figure:: img/link_the_nodes.*
140
141   Topology after linking the nodes
142
143Once nodes are available to the program, Application or node public API
144functions can links them together to create a complex packet processing graph.
145
146There are multiple different types of strategies to link the nodes.
147
148Method (a):
149^^^^^^^^^^^
150Provide the ``next_nodes[]`` at the node registration time. See  ``struct rte_node_register::nb_edges``.
151This is a use case to address the static node scheme where one knows upfront the
152``next_nodes[]`` of the node.
153
154Method (b):
155^^^^^^^^^^^
156Use ``rte_node_edge_get()``, ``rte_node_edge_update()``, ``rte_node_edge_shrink()``
157to update the ``next_nodes[]`` links for the node runtime but before graph create.
158
159Method (c):
160^^^^^^^^^^^
161Use ``rte_node_clone()`` to clone a already existing node, created using RTE_NODE_REGISTER.
162When ``rte_node_clone()`` invoked, The library, would clone all the attributes
163of the node and creates a new one. The name for cloned node shall be
164``"parent_node_name-user_provided_name"``.
165
166This method enables the use case of Rx and Tx nodes where multiple of those nodes
167need to be cloned based on the number of CPU available in the system.
168The cloned nodes will be identical, except the ``"context memory"``.
169Context memory will have information of port, queue pair in case of Rx and Tx
170ethdev nodes.
171
172Create the graph object
173~~~~~~~~~~~~~~~~~~~~~~~
174Now that the nodes are linked, Its time to create a graph by including
175the required nodes. The application can provide a set of node patterns to
176form a graph object. The ``fnmatch()`` API used underneath for the pattern
177matching to include the required nodes. After the graph create any changes to
178nodes or graph is not allowed.
179
180The ``rte_graph_create()`` API shall be used to create the graph.
181
182Example of a graph object creation:
183
184.. code-block:: console
185
186   {"ethdev_rx-0-0", ip4*, ethdev_tx-*"}
187
188In the above example, A graph object will be created with ethdev Rx
189node of port 0 and queue 0, all ipv4* nodes in the system,
190and ethdev tx node of all ports.
191
192Graph models
193~~~~~~~~~~~~
194There are two different kinds of graph walking models. User can select the model using
195``rte_graph_worker_model_set()`` API. If the application decides to use only one model,
196the fast path check can be avoided by defining the model with RTE_GRAPH_MODEL_SELECT.
197For example:
198
199.. code-block:: c
200
201  #define RTE_GRAPH_MODEL_SELECT RTE_GRAPH_MODEL_RTC
202  #include "rte_graph_worker.h"
203
204RTC (Run-To-Completion)
205^^^^^^^^^^^^^^^^^^^^^^^
206This is the default graph walking model. Specifically, ``rte_graph_walk_rtc()`` and
207``rte_node_enqueue*`` fast path API functions are designed to work on single-core to
208have better performance. The fast path API works on graph object, So the multi-core
209graph processing strategy would be to create graph object PER WORKER.
210
211Example:
212
213Graph: node-0 -> node-1 -> node-2 @Core0.
214
215.. code-block:: diff
216
217    + - - - - - - - - - - - - - - - - - - - - - +
218    '                  Core #0                  '
219    '                                           '
220    ' +--------+     +---------+     +--------+ '
221    ' | Node-0 | --> | Node-1  | --> | Node-2 | '
222    ' +--------+     +---------+     +--------+ '
223    '                                           '
224    + - - - - - - - - - - - - - - - - - - - - - +
225
226Dispatch model
227^^^^^^^^^^^^^^
228The dispatch model enables a cross-core dispatching mechanism which employs
229a scheduling work-queue to dispatch streams to other worker cores which
230being associated with the destination node.
231
232Use ``rte_graph_model_mcore_dispatch_lcore_affinity_set()`` to set lcore affinity
233with the node.
234Each worker core will have a graph repetition. Use ``rte_graph_clone()`` to clone
235graph for each worker and use``rte_graph_model_mcore_dispatch_core_bind()`` to
236bind graph with the worker core.
237
238Example:
239
240Graph topo: node-0 -> Core1; node-1 -> node-2; node-2 -> node-3.
241Config graph: node-0 @Core0; node-1/3 @Core1; node-2 @Core2.
242
243.. code-block:: diff
244
245    + - - - - - -+     +- - - - - - - - - - - - - +     + - - - - - -+
246    '  Core #0   '     '          Core #1         '     '  Core #2   '
247    '            '     '                          '     '            '
248    ' +--------+ '     ' +--------+    +--------+ '     ' +--------+ '
249    ' | Node-0 | - - - ->| Node-1 |    | Node-3 |<- - - - | Node-2 | '
250    ' +--------+ '     ' +--------+    +--------+ '     ' +--------+ '
251    '            '     '     |                    '     '      ^     '
252    + - - - - - -+     +- - -|- - - - - - - - - - +     + - - -|- - -+
253                             |                                 |
254                             + - - - - - - - - - - - - - - - - +
255
256
257In fast path
258~~~~~~~~~~~~
259Typical fast-path code looks like below, where the application
260gets the fast-path graph object using ``rte_graph_lookup()``
261on the worker thread and run the ``rte_graph_walk()`` in a tight loop.
262
263.. code-block:: c
264
265    struct rte_graph *graph = rte_graph_lookup("worker0");
266
267    while (!done) {
268        rte_graph_walk(graph);
269    }
270
271Context update when graph walk in action
272~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
273The fast-path object for the node is ``struct rte_node``.
274
275It may be possible that in slow-path or after the graph walk-in action,
276the user needs to update the context of the node hence access to
277``struct rte_node *`` memory.
278
279``rte_graph_foreach_node()``, ``rte_graph_node_get()``,
280``rte_graph_node_get_by_name()`` APIs can be used to get the
281``struct rte_node*``. ``rte_graph_foreach_node()`` iterator function works on
282``struct rte_graph *`` fast-path graph object while others works on graph ID or name.
283
284Get the node statistics using graph cluster
285~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286The user may need to know the aggregate stats of the node across
287multiple graph objects. Especially the situation where each graph object bound
288to a worker thread.
289
290Introduced a graph cluster object for statistics.
291``rte_graph_cluster_stats_create()`` API shall be used for creating a
292graph cluster with multiple graph objects and ``rte_graph_cluster_stats_get()``
293to get the aggregate node statistics.
294
295An example statistics output from ``rte_graph_cluster_stats_get()``
296
297.. code-block:: diff
298
299    +---------+-----------+-------------+---------------+-----------+---------------+-----------+
300    |Node     |calls      |objs         |realloc_count  |objs/call  |objs/sec(10E6) |cycles/call|
301    +---------------------+-------------+---------------+-----------+---------------+-----------+
302    |node0    |12977424   |3322220544   |5              |256.000    |3047.151872    |20.0000    |
303    |node1    |12977653   |3322279168   |0              |256.000    |3047.210496    |17.0000    |
304    |node2    |12977696   |3322290176   |0              |256.000    |3047.221504    |17.0000    |
305    |node3    |12977734   |3322299904   |0              |256.000    |3047.231232    |17.0000    |
306    |node4    |12977784   |3322312704   |1              |256.000    |3047.243776    |17.0000    |
307    |node5    |12977825   |3322323200   |0              |256.000    |3047.254528    |17.0000    |
308    +---------+-----------+-------------+---------------+-----------+---------------+-----------+
309
310Node writing guidelines
311~~~~~~~~~~~~~~~~~~~~~~~
312
313The ``process()`` function of a node is the fast-path function and that needs
314to be written carefully to achieve max performance.
315
316Broadly speaking, there are two different types of nodes.
317
318Static nodes
319~~~~~~~~~~~~
320The first kind of nodes are those that have a fixed ``next_nodes[]`` for the
321complete burst (like ethdev_rx, ethdev_tx) and it is simple to write.
322``process()`` function can move the obj burst to the next node either using
323``rte_node_next_stream_move()`` or using ``rte_node_next_stream_get()`` and
324``rte_node_next_stream_put()``.
325
326Intermediate nodes
327~~~~~~~~~~~~~~~~~~
328The second kind of such node is ``intermediate nodes`` that decide what is the
329``next_node[]`` to send to on a per-packet basis. In these nodes,
330
331* Firstly, there has to be the best possible packet processing logic.
332
333* Secondly, each packet needs to be queued to its next node.
334
335This can be done using ``rte_node_enqueue_[x1|x2|x4]()`` APIs if
336they are to single next or ``rte_node_enqueue_next()`` that takes array of nexts.
337
338In scenario where multiple intermediate nodes are present but most of the time
339each node using the same next node for all its packets, the cost of moving every
340pointer from current node's stream to next node's stream could be avoided.
341This is called home run and ``rte_node_next_stream_move()`` could be used to
342just move stream from the current node to the next node with least number of cycles.
343Since this can be avoided only in the case where all the packets are destined
344to the same next node, node implementation should be also having worst-case
345handling where every packet could be going to different next node.
346
347Example of intermediate node implementation with home run:
348^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3491. Start with speculation that next_node = node->ctx.
350This could be the next_node application used in the previous function call of this node.
351
3522. Get the next_node stream array with required space using
353``rte_node_next_stream_get(next_node, space)``.
354
3553. while n_left_from > 0 (i.e packets left to be sent) prefetch next pkt_set
356and process current pkt_set to find their next node
357
3584. if all the next nodes of the current pkt_set match speculated next node,
359just count them as successfully speculated(``last_spec``) till now and
360continue the loop without actually moving them to the next node. else if there is
361a mismatch, copy all the pkt_set pointers that were ``last_spec`` and move the
362current pkt_set to their respective next's nodes using ``rte_enqueue_next_x1()``.
363Also, one of the next_node can be updated as speculated next_node if it is more
364probable. Finally, reset ``last_spec`` to zero.
365
3665. if n_left_from != 0 then goto 3) to process remaining packets.
367
3686. if last_spec == nb_objs, All the objects passed were successfully speculated
369to single next node. So, the current stream can be moved to next node using
370``rte_node_next_stream_move(node, next_node)``.
371This is the ``home run`` where memcpy of buffer pointers to next node is avoided.
372
3737. Update the ``node->ctx`` with more probable next node.
374
375Graph object memory layout
376--------------------------
377.. _figure_graph_mem_layout:
378
379.. figure:: img/graph_mem_layout.*
380
381   Memory layout
382
383Understanding the memory layout helps to debug the graph library and
384improve the performance if needed.
385
386Graph object consists of a header, circular buffer to store the pending
387stream when walking over the graph, and variable-length memory to store
388the ``rte_node`` objects.
389
390The graph_nodes_mem_create() creates and populate this memory. The functions
391such as ``rte_graph_walk()`` and ``rte_node_enqueue_*`` use this memory
392to enable fastpath services.
393
394Inbuilt Nodes
395-------------
396
397DPDK provides a set of nodes for data processing.
398The following diagram depicts inbuilt nodes data flow.
399
400.. _figure_graph_inbuit_node_flow:
401
402.. figure:: img/graph_inbuilt_node_flow.*
403
404   Inbuilt nodes data flow
405
406Following section details the documentation for individual inbuilt node.
407
408ethdev_rx
409~~~~~~~~~
410This node does ``rte_eth_rx_burst()`` into stream buffer passed to it
411(src node stream) and does ``rte_node_next_stream_move()`` only when
412there are packets received. Each ``rte_node`` works only on one Rx port and
413queue that it gets from node->ctx. For each (port X, rx_queue Y),
414a rte_node is cloned from  ethdev_rx_base_node as ``ethdev_rx-X-Y`` in
415``rte_node_eth_config()`` along with updating ``node->ctx``.
416Each graph needs to be associated  with a unique rte_node for a (port, rx_queue).
417
418ethdev_tx
419~~~~~~~~~
420This node does ``rte_eth_tx_burst()`` for a burst of objs received by it.
421It sends the burst to a fixed Tx Port and Queue information from
422node->ctx. For each (port X), this ``rte_node`` is cloned from
423ethdev_tx_node_base as "ethdev_tx-X" in ``rte_node_eth_config()``
424along with updating node->context.
425
426Since each graph doesn't need more than one Txq, per port, a Txq is assigned
427based on graph id to each rte_node instance. Each graph needs to be associated
428with a rte_node for each (port).
429
430pkt_drop
431~~~~~~~~
432This node frees all the objects passed to it considering them as
433``rte_mbufs`` that need to be freed.
434
435ip4_lookup
436~~~~~~~~~~
437This node is an intermediate node that does LPM lookup for the received
438ipv4 packets and the result determines each packets next node.
439
440On successful LPM lookup, the result contains the ``next_node`` id and
441``next-hop`` id with which the packet needs to be further processed.
442
443On LPM lookup failure, objects are redirected to pkt_drop node.
444``rte_node_ip4_route_add()`` is control path API to add ipv4 routes.
445To achieve home run, node use ``rte_node_stream_move()`` as mentioned in above
446sections.
447
448ip4_rewrite
449~~~~~~~~~~~
450This node gets packets from ``ip4_lookup`` node with next-hop id for each
451packet is embedded in ``node_mbuf_priv1(mbuf)->nh``. This id is used
452to determine the L2 header to be written to the packet before sending
453the packet out to a particular ethdev_tx node.
454``rte_node_ip4_rewrite_add()`` is control path API to add next-hop info.
455
456ip6_lookup
457~~~~~~~~~~
458This node is an intermediate node that does LPM lookup for the received
459IPv6 packets and the result determines each packets next node.
460
461On successful LPM lookup, the result contains the ``next_node`` ID
462and `next-hop`` ID with which the packet needs to be further processed.
463
464On LPM lookup failure, objects are redirected to ``pkt_drop`` node.
465``rte_node_ip6_route_add()`` is control path API to add IPv6 routes.
466To achieve home run, node use ``rte_node_stream_move()``
467as mentioned in above sections.
468
469ip6_rewrite
470~~~~~~~~~~~
471This node gets packets from ``ip6_lookup`` node with next-hop ID
472for each packet is embedded in ``node_mbuf_priv1(mbuf)->nh``.
473This ID is used to determine the L2 header to be written to the packet
474before sending the packet out to a particular ``ethdev_tx`` node.
475``rte_node_ip6_rewrite_add()`` is control path API to add next-hop info.
476
477null
478~~~~
479This node ignores the set of objects passed to it and reports that all are
480processed.
481
482kernel_tx
483~~~~~~~~~
484This node is an exit node that forwards the packets to kernel.
485It will be used to forward any control plane traffic to kernel stack from DPDK.
486It uses a raw socket interface to transmit the packets,
487it uses the packet's destination IP address in sockaddr_in address structure
488and ``sendto`` function to send data on the raw socket.
489After sending the burst of packets to kernel,
490this node frees up the packet buffers.
491
492kernel_rx
493~~~~~~~~~
494This node is a source node which receives packets from kernel
495and forwards to any of the intermediate nodes.
496It uses the raw socket interface to receive packets from kernel.
497Uses ``poll`` function to poll on the socket fd
498for ``POLLIN`` events to read the packets from raw socket
499to stream buffer and does ``rte_node_next_stream_move()``
500when there are received packets.
501