xref: /dpdk/doc/guides/prog_guide/graph_lib.rst (revision 8b78671d254e5aa1786ff0e011b9a4211779c1b3)
14dc6d8e6SJerin Jacob..  SPDX-License-Identifier: BSD-3-Clause
24dc6d8e6SJerin Jacob    Copyright(C) 2020 Marvell International Ltd.
34dc6d8e6SJerin Jacob
44dc6d8e6SJerin JacobGraph Library and Inbuilt Nodes
54dc6d8e6SJerin Jacob===============================
64dc6d8e6SJerin Jacob
74dc6d8e6SJerin JacobGraph architecture abstracts the data processing functions as a ``node`` and
84dc6d8e6SJerin Jacob``links`` them together to create a complex ``graph`` to enable reusable/modular
94dc6d8e6SJerin Jacobdata processing functions.
104dc6d8e6SJerin Jacob
114dc6d8e6SJerin JacobThe graph library provides API to enable graph framework operations such as
124dc6d8e6SJerin Jacobcreate, lookup, dump and destroy on graph and node operations such as clone,
134dc6d8e6SJerin Jacobedge update, and edge shrink, etc. The API also allows to create the stats
144dc6d8e6SJerin Jacobcluster to monitor per graph and per node stats.
154dc6d8e6SJerin Jacob
164dc6d8e6SJerin JacobFeatures
174dc6d8e6SJerin Jacob--------
184dc6d8e6SJerin Jacob
194dc6d8e6SJerin JacobFeatures of the Graph library are:
204dc6d8e6SJerin Jacob
214dc6d8e6SJerin Jacob- Nodes as plugins.
224dc6d8e6SJerin Jacob- Support for out of tree nodes.
234dc6d8e6SJerin Jacob- Inbuilt nodes for packet processing.
244dc6d8e6SJerin Jacob- Multi-process support.
254dc6d8e6SJerin Jacob- Low overhead graph walk and node enqueue.
264dc6d8e6SJerin Jacob- Low overhead statistics collection infrastructure.
274dc6d8e6SJerin Jacob- Support to export the graph as a Graphviz dot file. See ``rte_graph_export()``.
284dc6d8e6SJerin Jacob- Allow having another graph walk implementation in the future by segregating
29a2bc0584SZhirun Yan  the fast path(``rte_graph_worker.h``) and slow path code.
304dc6d8e6SJerin Jacob
314dc6d8e6SJerin JacobAdvantages of Graph architecture
324dc6d8e6SJerin Jacob--------------------------------
334dc6d8e6SJerin Jacob
344dc6d8e6SJerin Jacob- Memory latency is the enemy for high-speed packet processing, moving the
354dc6d8e6SJerin Jacob  similar packet processing code to a node will reduce the I cache and D
364dc6d8e6SJerin Jacob  caches misses.
374dc6d8e6SJerin Jacob- Exploits the probability that most packets will follow the same nodes in the
384dc6d8e6SJerin Jacob  graph.
394dc6d8e6SJerin Jacob- Allow SIMD instructions for packet processing of the node.-
404dc6d8e6SJerin Jacob- The modular scheme allows having reusable nodes for the consumers.
414dc6d8e6SJerin Jacob- The modular scheme allows us to abstract the vendor HW specific
424dc6d8e6SJerin Jacob  optimizations as a node.
434dc6d8e6SJerin Jacob
444dc6d8e6SJerin JacobPerformance tuning parameters
454dc6d8e6SJerin Jacob-----------------------------
464dc6d8e6SJerin Jacob
474dc6d8e6SJerin Jacob- Test with various burst size values (256, 128, 64, 32) using
4889c67ae2SCiara Power  RTE_GRAPH_BURST_SIZE config option.
494dc6d8e6SJerin Jacob  The testing shows, on x86 and arm64 servers, The sweet spot is 256 burst
504dc6d8e6SJerin Jacob  size. While on arm64 embedded SoCs, it is either 64 or 128.
5189c67ae2SCiara Power- Disable node statistics (using ``RTE_LIBRTE_GRAPH_STATS`` config option)
524dc6d8e6SJerin Jacob  if not needed.
534dc6d8e6SJerin Jacob
544dc6d8e6SJerin JacobProgramming model
554dc6d8e6SJerin Jacob-----------------
564dc6d8e6SJerin Jacob
574dc6d8e6SJerin JacobAnatomy of Node:
584dc6d8e6SJerin Jacob~~~~~~~~~~~~~~~~
594dc6d8e6SJerin Jacob
604dc6d8e6SJerin Jacob.. _figure_anatomy_of_a_node:
614dc6d8e6SJerin Jacob
624dc6d8e6SJerin Jacob.. figure:: img/anatomy_of_a_node.*
634dc6d8e6SJerin Jacob
64924e7d8fSThomas Monjalon   Anatomy of a node
654dc6d8e6SJerin Jacob
664dc6d8e6SJerin JacobThe node is the basic building block of the graph framework.
674dc6d8e6SJerin Jacob
684dc6d8e6SJerin JacobA node consists of:
694dc6d8e6SJerin Jacob
704dc6d8e6SJerin Jacobprocess():
714dc6d8e6SJerin Jacob^^^^^^^^^^
724dc6d8e6SJerin Jacob
734dc6d8e6SJerin JacobThe callback function will be invoked by worker thread using
744dc6d8e6SJerin Jacob``rte_graph_walk()`` function when there is data to be processed by the node.
754dc6d8e6SJerin JacobA graph node process the function using ``process()`` and enqueue to next
764dc6d8e6SJerin Jacobdownstream node using ``rte_node_enqueue*()`` function.
774dc6d8e6SJerin Jacob
784dc6d8e6SJerin JacobContext memory:
794dc6d8e6SJerin Jacob^^^^^^^^^^^^^^^
804dc6d8e6SJerin Jacob
814dc6d8e6SJerin JacobIt is memory allocated by the library to store the node-specific context
824dc6d8e6SJerin Jacobinformation. This memory will be used by process(), init(), fini() callbacks.
834dc6d8e6SJerin Jacob
844dc6d8e6SJerin Jacobinit():
854dc6d8e6SJerin Jacob^^^^^^^
864dc6d8e6SJerin Jacob
874dc6d8e6SJerin JacobThe callback function will be invoked by ``rte_graph_create()`` on when
884dc6d8e6SJerin Jacoba node gets attached to a graph.
894dc6d8e6SJerin Jacob
904dc6d8e6SJerin Jacobfini():
914dc6d8e6SJerin Jacob^^^^^^^
924dc6d8e6SJerin Jacob
934dc6d8e6SJerin JacobThe callback function will be invoked by ``rte_graph_destroy()`` on when a
944dc6d8e6SJerin Jacobnode gets detached to a graph.
954dc6d8e6SJerin Jacob
964dc6d8e6SJerin JacobNode name:
974dc6d8e6SJerin Jacob^^^^^^^^^^
984dc6d8e6SJerin Jacob
994dc6d8e6SJerin JacobIt is the name of the node. When a node registers to graph library, the library
1004dc6d8e6SJerin Jacobgives the ID as ``rte_node_t`` type. Both ID or Name shall be used lookup the
1014dc6d8e6SJerin Jacobnode. ``rte_node_from_name()``, ``rte_node_id_to_name()`` are the node
1024dc6d8e6SJerin Jacoblookup functions.
1034dc6d8e6SJerin Jacob
1044dc6d8e6SJerin Jacobnb_edges:
1054dc6d8e6SJerin Jacob^^^^^^^^^
1064dc6d8e6SJerin Jacob
1074dc6d8e6SJerin JacobThe number of downstream nodes connected to this node. The ``next_nodes[]``
1084dc6d8e6SJerin Jacobstores the downstream nodes objects. ``rte_node_edge_update()`` and
1094dc6d8e6SJerin Jacob``rte_node_edge_shrink()`` functions shall be used to update the ``next_node[]``
1104dc6d8e6SJerin Jacobobjects. Consumers of the node APIs are free to update the ``next_node[]``
1114dc6d8e6SJerin Jacobobjects till ``rte_graph_create()`` invoked.
1124dc6d8e6SJerin Jacob
1134dc6d8e6SJerin Jacobnext_node[]:
1144dc6d8e6SJerin Jacob^^^^^^^^^^^^
1154dc6d8e6SJerin Jacob
1164dc6d8e6SJerin JacobThe dynamic array to store the downstream nodes connected to this node. Downstream
1174dc6d8e6SJerin Jacobnode should not be current node itself or a source node.
1184dc6d8e6SJerin Jacob
1194dc6d8e6SJerin JacobSource node:
1204dc6d8e6SJerin Jacob^^^^^^^^^^^^
1214dc6d8e6SJerin Jacob
1224dc6d8e6SJerin JacobSource nodes are static nodes created using ``RTE_NODE_REGISTER`` by passing
1234dc6d8e6SJerin Jacob``flags`` as ``RTE_NODE_SOURCE_F``.
1244dc6d8e6SJerin JacobWhile performing the graph walk, the ``process()`` function of all the source
1254dc6d8e6SJerin Jacobnodes will be called first. So that these nodes can be used as input nodes for a graph.
1264dc6d8e6SJerin Jacob
1274dc6d8e6SJerin JacobNode creation and registration
1284dc6d8e6SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1294dc6d8e6SJerin Jacob* Node implementer creates the node by implementing ops and attributes of
1304dc6d8e6SJerin Jacob  ``struct rte_node_register``.
1314dc6d8e6SJerin Jacob
1324dc6d8e6SJerin Jacob* The library registers the node by invoking RTE_NODE_REGISTER on library load
1334dc6d8e6SJerin Jacob  using the constructor scheme. The constructor scheme used here to support multi-process.
1344dc6d8e6SJerin Jacob
1354dc6d8e6SJerin JacobLink the Nodes to create the graph topology
1364dc6d8e6SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1374dc6d8e6SJerin Jacob.. _figure_link_the_nodes:
1384dc6d8e6SJerin Jacob
1394dc6d8e6SJerin Jacob.. figure:: img/link_the_nodes.*
1404dc6d8e6SJerin Jacob
141924e7d8fSThomas Monjalon   Topology after linking the nodes
1424dc6d8e6SJerin Jacob
1434dc6d8e6SJerin JacobOnce nodes are available to the program, Application or node public API
1444dc6d8e6SJerin Jacobfunctions can links them together to create a complex packet processing graph.
1454dc6d8e6SJerin Jacob
1464dc6d8e6SJerin JacobThere are multiple different types of strategies to link the nodes.
1474dc6d8e6SJerin Jacob
1484dc6d8e6SJerin JacobMethod (a):
1494dc6d8e6SJerin Jacob^^^^^^^^^^^
1504dc6d8e6SJerin JacobProvide the ``next_nodes[]`` at the node registration time. See  ``struct rte_node_register::nb_edges``.
1514dc6d8e6SJerin JacobThis is a use case to address the static node scheme where one knows upfront the
1524dc6d8e6SJerin Jacob``next_nodes[]`` of the node.
1534dc6d8e6SJerin Jacob
1544dc6d8e6SJerin JacobMethod (b):
1554dc6d8e6SJerin Jacob^^^^^^^^^^^
1564dc6d8e6SJerin JacobUse ``rte_node_edge_get()``, ``rte_node_edge_update()``, ``rte_node_edge_shrink()``
1574dc6d8e6SJerin Jacobto update the ``next_nodes[]`` links for the node runtime but before graph create.
1584dc6d8e6SJerin Jacob
1594dc6d8e6SJerin JacobMethod (c):
1604dc6d8e6SJerin Jacob^^^^^^^^^^^
1614dc6d8e6SJerin JacobUse ``rte_node_clone()`` to clone a already existing node, created using RTE_NODE_REGISTER.
1624dc6d8e6SJerin JacobWhen ``rte_node_clone()`` invoked, The library, would clone all the attributes
1634dc6d8e6SJerin Jacobof the node and creates a new one. The name for cloned node shall be
1644dc6d8e6SJerin Jacob``"parent_node_name-user_provided_name"``.
1654dc6d8e6SJerin Jacob
1664dc6d8e6SJerin JacobThis method enables the use case of Rx and Tx nodes where multiple of those nodes
1674dc6d8e6SJerin Jacobneed to be cloned based on the number of CPU available in the system.
1684dc6d8e6SJerin JacobThe cloned nodes will be identical, except the ``"context memory"``.
1694dc6d8e6SJerin JacobContext memory will have information of port, queue pair in case of Rx and Tx
1704dc6d8e6SJerin Jacobethdev nodes.
1714dc6d8e6SJerin Jacob
1724dc6d8e6SJerin JacobCreate the graph object
1734dc6d8e6SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~
1744dc6d8e6SJerin JacobNow that the nodes are linked, Its time to create a graph by including
1754dc6d8e6SJerin Jacobthe required nodes. The application can provide a set of node patterns to
176b457a9b3SAshwin Sekhar T Kform a graph object. The ``fnmatch()`` API used underneath for the pattern
1774dc6d8e6SJerin Jacobmatching to include the required nodes. After the graph create any changes to
1784dc6d8e6SJerin Jacobnodes or graph is not allowed.
1794dc6d8e6SJerin Jacob
1804dc6d8e6SJerin JacobThe ``rte_graph_create()`` API shall be used to create the graph.
1814dc6d8e6SJerin Jacob
1824dc6d8e6SJerin JacobExample of a graph object creation:
1834dc6d8e6SJerin Jacob
1844dc6d8e6SJerin Jacob.. code-block:: console
1854dc6d8e6SJerin Jacob
1864dc6d8e6SJerin Jacob   {"ethdev_rx-0-0", ip4*, ethdev_tx-*"}
1874dc6d8e6SJerin Jacob
1884dc6d8e6SJerin JacobIn the above example, A graph object will be created with ethdev Rx
1894dc6d8e6SJerin Jacobnode of port 0 and queue 0, all ipv4* nodes in the system,
1904dc6d8e6SJerin Jacoband ethdev tx node of all ports.
1914dc6d8e6SJerin Jacob
192*8b78671dSZhirun YanGraph models
193*8b78671dSZhirun Yan~~~~~~~~~~~~
194*8b78671dSZhirun YanThere are two different kinds of graph walking models. User can select the model using
195*8b78671dSZhirun Yan``rte_graph_worker_model_set()`` API. If the application decides to use only one model,
196*8b78671dSZhirun Yanthe fast path check can be avoided by defining the model with RTE_GRAPH_MODEL_SELECT.
197*8b78671dSZhirun YanFor example:
198*8b78671dSZhirun Yan
199*8b78671dSZhirun Yan.. code-block:: c
200*8b78671dSZhirun Yan
201*8b78671dSZhirun Yan  #define RTE_GRAPH_MODEL_SELECT RTE_GRAPH_MODEL_RTC
202*8b78671dSZhirun Yan  #include "rte_graph_worker.h"
203*8b78671dSZhirun Yan
204*8b78671dSZhirun YanRTC (Run-To-Completion)
205*8b78671dSZhirun Yan^^^^^^^^^^^^^^^^^^^^^^^
206*8b78671dSZhirun YanThis is the default graph walking model. Specifically, ``rte_graph_walk_rtc()`` and
207*8b78671dSZhirun Yan``rte_node_enqueue*`` fast path API functions are designed to work on single-core to
208*8b78671dSZhirun Yanhave better performance. The fast path API works on graph object, So the multi-core
209*8b78671dSZhirun Yangraph processing strategy would be to create graph object PER WORKER.
210*8b78671dSZhirun Yan
211*8b78671dSZhirun YanExample:
212*8b78671dSZhirun Yan
213*8b78671dSZhirun YanGraph: node-0 -> node-1 -> node-2 @Core0.
214*8b78671dSZhirun Yan
215*8b78671dSZhirun Yan.. code-block:: diff
216*8b78671dSZhirun Yan
217*8b78671dSZhirun Yan    + - - - - - - - - - - - - - - - - - - - - - +
218*8b78671dSZhirun Yan    '                  Core #0                  '
219*8b78671dSZhirun Yan    '                                           '
220*8b78671dSZhirun Yan    ' +--------+     +---------+     +--------+ '
221*8b78671dSZhirun Yan    ' | Node-0 | --> | Node-1  | --> | Node-2 | '
222*8b78671dSZhirun Yan    ' +--------+     +---------+     +--------+ '
223*8b78671dSZhirun Yan    '                                           '
224*8b78671dSZhirun Yan    + - - - - - - - - - - - - - - - - - - - - - +
225*8b78671dSZhirun Yan
226*8b78671dSZhirun YanDispatch model
227*8b78671dSZhirun Yan^^^^^^^^^^^^^^
228*8b78671dSZhirun YanThe dispatch model enables a cross-core dispatching mechanism which employs
229*8b78671dSZhirun Yana scheduling work-queue to dispatch streams to other worker cores which
230*8b78671dSZhirun Yanbeing associated with the destination node.
231*8b78671dSZhirun Yan
232*8b78671dSZhirun YanUse ``rte_graph_model_mcore_dispatch_lcore_affinity_set()`` to set lcore affinity
233*8b78671dSZhirun Yanwith the node.
234*8b78671dSZhirun YanEach worker core will have a graph repetition. Use ``rte_graph_clone()`` to clone
235*8b78671dSZhirun Yangraph for each worker and use``rte_graph_model_mcore_dispatch_core_bind()`` to
236*8b78671dSZhirun Yanbind graph with the worker core.
237*8b78671dSZhirun Yan
238*8b78671dSZhirun YanExample:
239*8b78671dSZhirun Yan
240*8b78671dSZhirun YanGraph topo: node-0 -> Core1; node-1 -> node-2; node-2 -> node-3.
241*8b78671dSZhirun YanConfig graph: node-0 @Core0; node-1/3 @Core1; node-2 @Core2.
242*8b78671dSZhirun Yan
243*8b78671dSZhirun Yan.. code-block:: diff
244*8b78671dSZhirun Yan
245*8b78671dSZhirun Yan    + - - - - - -+     +- - - - - - - - - - - - - +     + - - - - - -+
246*8b78671dSZhirun Yan    '  Core #0   '     '          Core #1         '     '  Core #2   '
247*8b78671dSZhirun Yan    '            '     '                          '     '            '
248*8b78671dSZhirun Yan    ' +--------+ '     ' +--------+    +--------+ '     ' +--------+ '
249*8b78671dSZhirun Yan    ' | Node-0 | - - - ->| Node-1 |    | Node-3 |<- - - - | Node-2 | '
250*8b78671dSZhirun Yan    ' +--------+ '     ' +--------+    +--------+ '     ' +--------+ '
251*8b78671dSZhirun Yan    '            '     '     |                    '     '      ^     '
252*8b78671dSZhirun Yan    + - - - - - -+     +- - -|- - - - - - - - - - +     + - - -|- - -+
253*8b78671dSZhirun Yan                             |                                 |
254*8b78671dSZhirun Yan                             + - - - - - - - - - - - - - - - - +
255*8b78671dSZhirun Yan
2564dc6d8e6SJerin Jacob
2574dc6d8e6SJerin JacobIn fast path
2584dc6d8e6SJerin Jacob~~~~~~~~~~~~
2594dc6d8e6SJerin JacobTypical fast-path code looks like below, where the application
2604dc6d8e6SJerin Jacobgets the fast-path graph object using ``rte_graph_lookup()``
2614dc6d8e6SJerin Jacobon the worker thread and run the ``rte_graph_walk()`` in a tight loop.
2624dc6d8e6SJerin Jacob
2634dc6d8e6SJerin Jacob.. code-block:: c
2644dc6d8e6SJerin Jacob
2654dc6d8e6SJerin Jacob    struct rte_graph *graph = rte_graph_lookup("worker0");
2664dc6d8e6SJerin Jacob
2674dc6d8e6SJerin Jacob    while (!done) {
2684dc6d8e6SJerin Jacob        rte_graph_walk(graph);
2694dc6d8e6SJerin Jacob    }
2704dc6d8e6SJerin Jacob
2714dc6d8e6SJerin JacobContext update when graph walk in action
2724dc6d8e6SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2734dc6d8e6SJerin JacobThe fast-path object for the node is ``struct rte_node``.
2744dc6d8e6SJerin Jacob
2754dc6d8e6SJerin JacobIt may be possible that in slow-path or after the graph walk-in action,
2764dc6d8e6SJerin Jacobthe user needs to update the context of the node hence access to
2774dc6d8e6SJerin Jacob``struct rte_node *`` memory.
2784dc6d8e6SJerin Jacob
2794dc6d8e6SJerin Jacob``rte_graph_foreach_node()``, ``rte_graph_node_get()``,
2804f823975SThomas Monjalon``rte_graph_node_get_by_name()`` APIs can be used to get the
2814dc6d8e6SJerin Jacob``struct rte_node*``. ``rte_graph_foreach_node()`` iterator function works on
2824dc6d8e6SJerin Jacob``struct rte_graph *`` fast-path graph object while others works on graph ID or name.
2834dc6d8e6SJerin Jacob
2844dc6d8e6SJerin JacobGet the node statistics using graph cluster
2854dc6d8e6SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2864dc6d8e6SJerin JacobThe user may need to know the aggregate stats of the node across
2874dc6d8e6SJerin Jacobmultiple graph objects. Especially the situation where each graph object bound
2884dc6d8e6SJerin Jacobto a worker thread.
2894dc6d8e6SJerin Jacob
2904dc6d8e6SJerin JacobIntroduced a graph cluster object for statistics.
2914dc6d8e6SJerin Jacob``rte_graph_cluster_stats_create()`` API shall be used for creating a
2924dc6d8e6SJerin Jacobgraph cluster with multiple graph objects and ``rte_graph_cluster_stats_get()``
2934dc6d8e6SJerin Jacobto get the aggregate node statistics.
2944dc6d8e6SJerin Jacob
2954dc6d8e6SJerin JacobAn example statistics output from ``rte_graph_cluster_stats_get()``
2964dc6d8e6SJerin Jacob
2974dc6d8e6SJerin Jacob.. code-block:: diff
2984dc6d8e6SJerin Jacob
2994dc6d8e6SJerin Jacob    +---------+-----------+-------------+---------------+-----------+---------------+-----------+
3004dc6d8e6SJerin Jacob    |Node     |calls      |objs         |realloc_count  |objs/call  |objs/sec(10E6) |cycles/call|
3014dc6d8e6SJerin Jacob    +---------------------+-------------+---------------+-----------+---------------+-----------+
3024dc6d8e6SJerin Jacob    |node0    |12977424   |3322220544   |5              |256.000    |3047.151872    |20.0000    |
3034dc6d8e6SJerin Jacob    |node1    |12977653   |3322279168   |0              |256.000    |3047.210496    |17.0000    |
3044dc6d8e6SJerin Jacob    |node2    |12977696   |3322290176   |0              |256.000    |3047.221504    |17.0000    |
3054dc6d8e6SJerin Jacob    |node3    |12977734   |3322299904   |0              |256.000    |3047.231232    |17.0000    |
3064dc6d8e6SJerin Jacob    |node4    |12977784   |3322312704   |1              |256.000    |3047.243776    |17.0000    |
3074dc6d8e6SJerin Jacob    |node5    |12977825   |3322323200   |0              |256.000    |3047.254528    |17.0000    |
3084dc6d8e6SJerin Jacob    +---------+-----------+-------------+---------------+-----------+---------------+-----------+
3094dc6d8e6SJerin Jacob
3104dc6d8e6SJerin JacobNode writing guidelines
3114dc6d8e6SJerin Jacob~~~~~~~~~~~~~~~~~~~~~~~
3124dc6d8e6SJerin Jacob
3134dc6d8e6SJerin JacobThe ``process()`` function of a node is the fast-path function and that needs
3144dc6d8e6SJerin Jacobto be written carefully to achieve max performance.
3154dc6d8e6SJerin Jacob
3164dc6d8e6SJerin JacobBroadly speaking, there are two different types of nodes.
3174dc6d8e6SJerin Jacob
3184dc6d8e6SJerin JacobStatic nodes
3194dc6d8e6SJerin Jacob~~~~~~~~~~~~
3204dc6d8e6SJerin JacobThe first kind of nodes are those that have a fixed ``next_nodes[]`` for the
3214dc6d8e6SJerin Jacobcomplete burst (like ethdev_rx, ethdev_tx) and it is simple to write.
3224dc6d8e6SJerin Jacob``process()`` function can move the obj burst to the next node either using
3234dc6d8e6SJerin Jacob``rte_node_next_stream_move()`` or using ``rte_node_next_stream_get()`` and
3244dc6d8e6SJerin Jacob``rte_node_next_stream_put()``.
3254dc6d8e6SJerin Jacob
3264dc6d8e6SJerin JacobIntermediate nodes
3274dc6d8e6SJerin Jacob~~~~~~~~~~~~~~~~~~
3284dc6d8e6SJerin JacobThe second kind of such node is ``intermediate nodes`` that decide what is the
3294dc6d8e6SJerin Jacob``next_node[]`` to send to on a per-packet basis. In these nodes,
3304dc6d8e6SJerin Jacob
3314dc6d8e6SJerin Jacob* Firstly, there has to be the best possible packet processing logic.
3324dc6d8e6SJerin Jacob
3334dc6d8e6SJerin Jacob* Secondly, each packet needs to be queued to its next node.
3344dc6d8e6SJerin Jacob
3354dc6d8e6SJerin JacobThis can be done using ``rte_node_enqueue_[x1|x2|x4]()`` APIs if
3364dc6d8e6SJerin Jacobthey are to single next or ``rte_node_enqueue_next()`` that takes array of nexts.
3374dc6d8e6SJerin Jacob
3384dc6d8e6SJerin JacobIn scenario where multiple intermediate nodes are present but most of the time
3394dc6d8e6SJerin Jacobeach node using the same next node for all its packets, the cost of moving every
3404dc6d8e6SJerin Jacobpointer from current node's stream to next node's stream could be avoided.
3414dc6d8e6SJerin JacobThis is called home run and ``rte_node_next_stream_move()`` could be used to
3424dc6d8e6SJerin Jacobjust move stream from the current node to the next node with least number of cycles.
3434dc6d8e6SJerin JacobSince this can be avoided only in the case where all the packets are destined
3444dc6d8e6SJerin Jacobto the same next node, node implementation should be also having worst-case
3454dc6d8e6SJerin Jacobhandling where every packet could be going to different next node.
3464dc6d8e6SJerin Jacob
3474dc6d8e6SJerin JacobExample of intermediate node implementation with home run:
3484dc6d8e6SJerin Jacob^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3494dc6d8e6SJerin Jacob1. Start with speculation that next_node = node->ctx.
3504dc6d8e6SJerin JacobThis could be the next_node application used in the previous function call of this node.
3514dc6d8e6SJerin Jacob
3524dc6d8e6SJerin Jacob2. Get the next_node stream array with required space using
3534dc6d8e6SJerin Jacob``rte_node_next_stream_get(next_node, space)``.
3544dc6d8e6SJerin Jacob
3554dc6d8e6SJerin Jacob3. while n_left_from > 0 (i.e packets left to be sent) prefetch next pkt_set
3564dc6d8e6SJerin Jacoband process current pkt_set to find their next node
3574dc6d8e6SJerin Jacob
3584dc6d8e6SJerin Jacob4. if all the next nodes of the current pkt_set match speculated next node,
3594dc6d8e6SJerin Jacobjust count them as successfully speculated(``last_spec``) till now and
3604dc6d8e6SJerin Jacobcontinue the loop without actually moving them to the next node. else if there is
3614dc6d8e6SJerin Jacoba mismatch, copy all the pkt_set pointers that were ``last_spec`` and move the
3624dc6d8e6SJerin Jacobcurrent pkt_set to their respective next's nodes using ``rte_enqueue_next_x1()``.
3634dc6d8e6SJerin JacobAlso, one of the next_node can be updated as speculated next_node if it is more
3644dc6d8e6SJerin Jacobprobable. Finally, reset ``last_spec`` to zero.
3654dc6d8e6SJerin Jacob
3664dc6d8e6SJerin Jacob5. if n_left_from != 0 then goto 3) to process remaining packets.
3674dc6d8e6SJerin Jacob
3684dc6d8e6SJerin Jacob6. if last_spec == nb_objs, All the objects passed were successfully speculated
3694dc6d8e6SJerin Jacobto single next node. So, the current stream can be moved to next node using
3704dc6d8e6SJerin Jacob``rte_node_next_stream_move(node, next_node)``.
3714dc6d8e6SJerin JacobThis is the ``home run`` where memcpy of buffer pointers to next node is avoided.
3724dc6d8e6SJerin Jacob
3734dc6d8e6SJerin Jacob7. Update the ``node->ctx`` with more probable next node.
3744dc6d8e6SJerin Jacob
3754dc6d8e6SJerin JacobGraph object memory layout
3764dc6d8e6SJerin Jacob--------------------------
3774dc6d8e6SJerin Jacob.. _figure_graph_mem_layout:
3784dc6d8e6SJerin Jacob
3794dc6d8e6SJerin Jacob.. figure:: img/graph_mem_layout.*
3804dc6d8e6SJerin Jacob
381924e7d8fSThomas Monjalon   Memory layout
382924e7d8fSThomas Monjalon
383924e7d8fSThomas MonjalonUnderstanding the memory layout helps to debug the graph library and
3844dc6d8e6SJerin Jacobimprove the performance if needed.
3854dc6d8e6SJerin Jacob
3864dc6d8e6SJerin JacobGraph object consists of a header, circular buffer to store the pending
3874dc6d8e6SJerin Jacobstream when walking over the graph, and variable-length memory to store
3884dc6d8e6SJerin Jacobthe ``rte_node`` objects.
3894dc6d8e6SJerin Jacob
3904dc6d8e6SJerin JacobThe graph_nodes_mem_create() creates and populate this memory. The functions
3914dc6d8e6SJerin Jacobsuch as ``rte_graph_walk()`` and ``rte_node_enqueue_*`` use this memory
3924dc6d8e6SJerin Jacobto enable fastpath services.
3934dc6d8e6SJerin Jacob
3944dc6d8e6SJerin JacobInbuilt Nodes
3954dc6d8e6SJerin Jacob-------------
3964dc6d8e6SJerin Jacob
3974dc6d8e6SJerin JacobDPDK provides a set of nodes for data processing. The following section
3984dc6d8e6SJerin Jacobdetails the documentation for the same.
3994dc6d8e6SJerin Jacob
4004dc6d8e6SJerin Jacobethdev_rx
4014dc6d8e6SJerin Jacob~~~~~~~~~
4024dc6d8e6SJerin JacobThis node does ``rte_eth_rx_burst()`` into stream buffer passed to it
4034dc6d8e6SJerin Jacob(src node stream) and does ``rte_node_next_stream_move()`` only when
4044dc6d8e6SJerin Jacobthere are packets received. Each ``rte_node`` works only on one Rx port and
4054dc6d8e6SJerin Jacobqueue that it gets from node->ctx. For each (port X, rx_queue Y),
4064dc6d8e6SJerin Jacoba rte_node is cloned from  ethdev_rx_base_node as ``ethdev_rx-X-Y`` in
4074dc6d8e6SJerin Jacob``rte_node_eth_config()`` along with updating ``node->ctx``.
4084dc6d8e6SJerin JacobEach graph needs to be associated  with a unique rte_node for a (port, rx_queue).
4094dc6d8e6SJerin Jacob
4104dc6d8e6SJerin Jacobethdev_tx
4114dc6d8e6SJerin Jacob~~~~~~~~~
4124dc6d8e6SJerin JacobThis node does ``rte_eth_tx_burst()`` for a burst of objs received by it.
4134dc6d8e6SJerin JacobIt sends the burst to a fixed Tx Port and Queue information from
4144dc6d8e6SJerin Jacobnode->ctx. For each (port X), this ``rte_node`` is cloned from
4154dc6d8e6SJerin Jacobethdev_tx_node_base as "ethdev_tx-X" in ``rte_node_eth_config()``
4164dc6d8e6SJerin Jacobalong with updating node->context.
4174dc6d8e6SJerin Jacob
4184dc6d8e6SJerin JacobSince each graph doesn't need more than one Txq, per port, a Txq is assigned
4194dc6d8e6SJerin Jacobbased on graph id to each rte_node instance. Each graph needs to be associated
4204dc6d8e6SJerin Jacobwith a rte_node for each (port).
4214dc6d8e6SJerin Jacob
4224dc6d8e6SJerin Jacobpkt_drop
4234dc6d8e6SJerin Jacob~~~~~~~~
4244dc6d8e6SJerin JacobThis node frees all the objects passed to it considering them as
4254dc6d8e6SJerin Jacob``rte_mbufs`` that need to be freed.
4264dc6d8e6SJerin Jacob
4274dc6d8e6SJerin Jacobip4_lookup
4284dc6d8e6SJerin Jacob~~~~~~~~~~
4294dc6d8e6SJerin JacobThis node is an intermediate node that does LPM lookup for the received
4304dc6d8e6SJerin Jacobipv4 packets and the result determines each packets next node.
4314dc6d8e6SJerin Jacob
4324dc6d8e6SJerin JacobOn successful LPM lookup, the result contains the ``next_node`` id and
4334dc6d8e6SJerin Jacob``next-hop`` id with which the packet needs to be further processed.
4344dc6d8e6SJerin Jacob
4354dc6d8e6SJerin JacobOn LPM lookup failure, objects are redirected to pkt_drop node.
4364dc6d8e6SJerin Jacob``rte_node_ip4_route_add()`` is control path API to add ipv4 routes.
4374dc6d8e6SJerin JacobTo achieve home run, node use ``rte_node_stream_move()`` as mentioned in above
4384dc6d8e6SJerin Jacobsections.
4394dc6d8e6SJerin Jacob
4404dc6d8e6SJerin Jacobip4_rewrite
4414dc6d8e6SJerin Jacob~~~~~~~~~~~
4424dc6d8e6SJerin JacobThis node gets packets from ``ip4_lookup`` node with next-hop id for each
4434dc6d8e6SJerin Jacobpacket is embedded in ``node_mbuf_priv1(mbuf)->nh``. This id is used
4444dc6d8e6SJerin Jacobto determine the L2 header to be written to the packet before sending
4454dc6d8e6SJerin Jacobthe packet out to a particular ethdev_tx node.
4464dc6d8e6SJerin Jacob``rte_node_ip4_rewrite_add()`` is control path API to add next-hop info.
4474dc6d8e6SJerin Jacob
44820365d79SSunil Kumar Koriip6_lookup
44920365d79SSunil Kumar Kori~~~~~~~~~~
45020365d79SSunil Kumar KoriThis node is an intermediate node that does LPM lookup for the received
45120365d79SSunil Kumar KoriIPv6 packets and the result determines each packets next node.
45220365d79SSunil Kumar Kori
45320365d79SSunil Kumar KoriOn successful LPM lookup, the result contains the ``next_node`` ID
45420365d79SSunil Kumar Koriand `next-hop`` ID with which the packet needs to be further processed.
45520365d79SSunil Kumar Kori
45620365d79SSunil Kumar KoriOn LPM lookup failure, objects are redirected to ``pkt_drop`` node.
45720365d79SSunil Kumar Kori``rte_node_ip6_route_add()`` is control path API to add IPv6 routes.
45820365d79SSunil Kumar KoriTo achieve home run, node use ``rte_node_stream_move()``
45920365d79SSunil Kumar Korias mentioned in above sections.
46020365d79SSunil Kumar Kori
46116ac29cbSAmit Prakash Shuklaip6_rewrite
46216ac29cbSAmit Prakash Shukla~~~~~~~~~~~
46316ac29cbSAmit Prakash ShuklaThis node gets packets from ``ip6_lookup`` node with next-hop ID
46416ac29cbSAmit Prakash Shuklafor each packet is embedded in ``node_mbuf_priv1(mbuf)->nh``.
46516ac29cbSAmit Prakash ShuklaThis ID is used to determine the L2 header to be written to the packet
46616ac29cbSAmit Prakash Shuklabefore sending the packet out to a particular ``ethdev_tx`` node.
46716ac29cbSAmit Prakash Shukla``rte_node_ip6_rewrite_add()`` is control path API to add next-hop info.
46816ac29cbSAmit Prakash Shukla
4694dc6d8e6SJerin Jacobnull
4704dc6d8e6SJerin Jacob~~~~
4714dc6d8e6SJerin JacobThis node ignores the set of objects passed to it and reports that all are
4724dc6d8e6SJerin Jacobprocessed.
4732a0ae651SVamsi Attunuru
4742a0ae651SVamsi Attunurukernel_tx
4752a0ae651SVamsi Attunuru~~~~~~~~~
4762a0ae651SVamsi AttunuruThis node is an exit node that forwards the packets to kernel.
4772a0ae651SVamsi AttunuruIt will be used to forward any control plane traffic to kernel stack from DPDK.
4782a0ae651SVamsi AttunuruIt uses a raw socket interface to transmit the packets,
4792a0ae651SVamsi Attunuruit uses the packet's destination IP address in sockaddr_in address structure
4802a0ae651SVamsi Attunuruand ``sendto`` function to send data on the raw socket.
4812a0ae651SVamsi AttunuruAfter sending the burst of packets to kernel,
4822a0ae651SVamsi Attunuruthis node frees up the packet buffers.
4832d0cf6a7SVamsi Attunuru
4842d0cf6a7SVamsi Attunurukernel_rx
4852d0cf6a7SVamsi Attunuru~~~~~~~~~
4862d0cf6a7SVamsi AttunuruThis node is a source node which receives packets from kernel
4872d0cf6a7SVamsi Attunuruand forwards to any of the intermediate nodes.
4882d0cf6a7SVamsi AttunuruIt uses the raw socket interface to receive packets from kernel.
4892d0cf6a7SVamsi AttunuruUses ``poll`` function to poll on the socket fd
4902d0cf6a7SVamsi Attunurufor ``POLLIN`` events to read the packets from raw socket
4912d0cf6a7SVamsi Attunuruto stream buffer and does ``rte_node_next_stream_move()``
4922d0cf6a7SVamsi Attunuruwhen there are received packets.
493