xref: /dpdk/doc/guides/sample_app_ug/skeleton.rst (revision 8750576fb2a9a067ffbcce4bab6481f3bfa47097)
15630257fSFerruh Yigit..  SPDX-License-Identifier: BSD-3-Clause
25630257fSFerruh Yigit    Copyright(c) 2015 Intel Corporation.
31443da3bSJohn McNamara
41443da3bSJohn McNamaraBasic Forwarding Sample Application
51443da3bSJohn McNamara===================================
61443da3bSJohn McNamara
71443da3bSJohn McNamaraThe Basic Forwarding sample application is a simple *skeleton* example of a
81443da3bSJohn McNamaraforwarding application.
91443da3bSJohn McNamara
10*8750576fSNandini PersadOverview
11*8750576fSNandini Persad--------
12*8750576fSNandini Persad
13*8750576fSNandini PersadThis application demonstrates the basic components of a DPDK forwarding application.
14*8750576fSNandini PersadFor more detailed implementations, see the L2 and L3 forwarding sample applications.
151443da3bSJohn McNamara
161443da3bSJohn McNamaraCompiling the Application
171443da3bSJohn McNamara-------------------------
181443da3bSJohn McNamara
19*8750576fSNandini PersadTo compile the sample application, see :doc:`compiling`.
201443da3bSJohn McNamara
217cacb056SHerakliusz LipiecThe application is located in the ``skeleton`` sub-directory.
221443da3bSJohn McNamara
231443da3bSJohn McNamaraRunning the Application
241443da3bSJohn McNamara-----------------------
251443da3bSJohn McNamara
26218c4e68SBruce RichardsonTo run the example in a ``linux`` environment:
271443da3bSJohn McNamara
281443da3bSJohn McNamara.. code-block:: console
291443da3bSJohn McNamara
30e2a94f9aSCiara Power    ./<build_dir>/examples/dpdk-skeleton -l 1 -n 4
311443da3bSJohn McNamara
321443da3bSJohn McNamaraRefer to *DPDK Getting Started Guide* for general information on running
33*8750576fSNandini Persadapplications and Environment Abstraction Layer (EAL) options.
341443da3bSJohn McNamara
351443da3bSJohn McNamara
361443da3bSJohn McNamaraExplanation
371443da3bSJohn McNamara-----------
381443da3bSJohn McNamara
391443da3bSJohn McNamaraThe following sections provide an explanation of the main components of the
401443da3bSJohn McNamaracode.
411443da3bSJohn McNamara
421443da3bSJohn McNamaraAll DPDK library functions used in the sample code are prefixed with ``rte_``
431443da3bSJohn McNamaraand are explained in detail in the *DPDK API Documentation*.
441443da3bSJohn McNamara
451443da3bSJohn McNamara
461443da3bSJohn McNamaraThe Main Function
471443da3bSJohn McNamara~~~~~~~~~~~~~~~~~
481443da3bSJohn McNamara
491443da3bSJohn McNamaraThe ``main()`` function performs the initialization and calls the execution
501443da3bSJohn McNamarathreads for each lcore.
511443da3bSJohn McNamara
52*8750576fSNandini PersadThe first task is to initialize the Environment Abstraction Layer (EAL).
53*8750576fSNandini PersadThe ``argc`` and ``argv`` arguments are provided to the ``rte_eal_init()``
541443da3bSJohn McNamarafunction. The value returned is the number of parsed arguments:
551443da3bSJohn McNamara
569a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
579a212dc0SConor Fogarty    :language: c
589a212dc0SConor Fogarty    :start-after: Initializion the Environment Abstraction Layer (EAL). 8<
597be78d02SJosh Soref    :end-before: >8 End of initialization the Environment Abstraction Layer (EAL).
609a212dc0SConor Fogarty    :dedent: 1
611443da3bSJohn McNamara
621443da3bSJohn McNamara
631443da3bSJohn McNamaraThe ``main()`` also allocates a mempool to hold the mbufs (Message Buffers)
641443da3bSJohn McNamaraused by the application:
651443da3bSJohn McNamara
669a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
679a212dc0SConor Fogarty    :language: c
689a212dc0SConor Fogarty    :start-after: Allocates mempool to hold the mbufs. 8<
699a212dc0SConor Fogarty    :end-before: >8 End of allocating mempool to hold mbuf.
709a212dc0SConor Fogarty    :dedent: 1
711443da3bSJohn McNamara
721443da3bSJohn McNamaraMbufs are the packet buffer structure used by DPDK. They are explained in
731443da3bSJohn McNamaradetail in the "Mbuf Library" section of the *DPDK Programmer's Guide*.
741443da3bSJohn McNamara
751443da3bSJohn McNamaraThe ``main()`` function also initializes all the ports using the user defined
761443da3bSJohn McNamara``port_init()`` function which is explained in the next section:
771443da3bSJohn McNamara
789a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
799a212dc0SConor Fogarty    :language: c
809a212dc0SConor Fogarty    :start-after: Initializing all ports. 8<
819a212dc0SConor Fogarty    :end-before: >8 End of initializing all ports.
829a212dc0SConor Fogarty    :dedent: 1
831443da3bSJohn McNamara
841443da3bSJohn McNamaraOnce the initialization is complete, the application is ready to launch a
85*8750576fSNandini Persadfunction on an lcore.
86*8750576fSNandini PersadIn this example, ``lcore_main()`` is called on a single lcore.
871443da3bSJohn McNamara
881443da3bSJohn McNamara
899a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
909a212dc0SConor Fogarty    :language: c
919a212dc0SConor Fogarty    :start-after: Called on single lcore. 8<
929a212dc0SConor Fogarty    :end-before: >8 End of called on single lcore.
939a212dc0SConor Fogarty    :dedent: 1
941443da3bSJohn McNamara
951443da3bSJohn McNamaraThe ``lcore_main()`` function is explained below.
961443da3bSJohn McNamara
971443da3bSJohn McNamara
981443da3bSJohn McNamaraThe Port Initialization Function
99*8750576fSNandini Persad~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1001443da3bSJohn McNamara
1011443da3bSJohn McNamaraThe main functional part of the port initialization used in the Basic
1021443da3bSJohn McNamaraForwarding application is shown below:
1031443da3bSJohn McNamara
1049a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
1059a212dc0SConor Fogarty    :language: c
1069a212dc0SConor Fogarty    :start-after: Main functional part of port initialization. 8<
1079a212dc0SConor Fogarty    :end-before: >8 End of main functional part of port initialization.
1081443da3bSJohn McNamara
1091443da3bSJohn McNamaraThe Ethernet ports are configured with default settings using the
1101bb4a528SFerruh Yigit``rte_eth_dev_configure()`` function.
1111443da3bSJohn McNamara
112*8750576fSNandini PersadIn this example, the ports are set up with 1 Rx and 1 Tx queue using the
1131443da3bSJohn McNamara``rte_eth_rx_queue_setup()`` and ``rte_eth_tx_queue_setup()`` functions.
1141443da3bSJohn McNamara
1151443da3bSJohn McNamaraThe Ethernet port is then started:
1161443da3bSJohn McNamara
1179a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
1189a212dc0SConor Fogarty        :language: c
1199a212dc0SConor Fogarty        :start-after: Starting Ethernet port. 8<
1209a212dc0SConor Fogarty        :end-before: >8 End of starting of ethernet port.
1219a212dc0SConor Fogarty        :dedent: 1
1221443da3bSJohn McNamara
123*8750576fSNandini PersadFinally, the Rx port is set in promiscuous mode:
1241443da3bSJohn McNamara
1259a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
1269a212dc0SConor Fogarty        :language: c
1279a212dc0SConor Fogarty        :start-after: Enable RX in promiscuous mode for the Ethernet device.
1289a212dc0SConor Fogarty        :end-before: End of setting RX port in promiscuous mode.
1299a212dc0SConor Fogarty        :dedent: 1
1301443da3bSJohn McNamara
1311443da3bSJohn McNamara
1321443da3bSJohn McNamaraThe Lcores Main
1331443da3bSJohn McNamara~~~~~~~~~~~~~~~
1341443da3bSJohn McNamara
135*8750576fSNandini PersadAs we saw above, the ``main()`` function calls an application function
136*8750576fSNandini Persadon the available lcores.
137*8750576fSNandini PersadFor the basic forwarding application, the lcore function
1381443da3bSJohn McNamaralooks like the following:
1391443da3bSJohn McNamara
1409a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
1419a212dc0SConor Fogarty        :language: c
1429a212dc0SConor Fogarty        :start-after: Basic forwarding application lcore. 8<
1439a212dc0SConor Fogarty        :end-before: >8 End Basic forwarding application lcore.
1441443da3bSJohn McNamara
1451443da3bSJohn McNamaraThe main work of the application is done within the loop:
1461443da3bSJohn McNamara
1479a212dc0SConor Fogarty.. literalinclude:: ../../../examples/skeleton/basicfwd.c
1489a212dc0SConor Fogarty        :language: c
1499a212dc0SConor Fogarty        :start-after: Main work of application loop. 8<
1509a212dc0SConor Fogarty        :end-before: >8 End of loop.
1519a212dc0SConor Fogarty        :dedent: 1
1521443da3bSJohn McNamara
1531443da3bSJohn McNamaraPackets are received in bursts on the RX ports and transmitted in bursts on
1541443da3bSJohn McNamarathe TX ports. The ports are grouped in pairs with a simple mapping scheme
1551443da3bSJohn McNamarausing the an XOR on the port number::
1561443da3bSJohn McNamara
1571443da3bSJohn McNamara    0 -> 1
1581443da3bSJohn McNamara    1 -> 0
1591443da3bSJohn McNamara
1601443da3bSJohn McNamara    2 -> 3
1611443da3bSJohn McNamara    3 -> 2
1621443da3bSJohn McNamara
1631443da3bSJohn McNamara    etc.
1641443da3bSJohn McNamara
1651443da3bSJohn McNamaraThe ``rte_eth_tx_burst()`` function frees the memory buffers of packets that
1661443da3bSJohn McNamaraare transmitted. If packets fail to transmit, ``(nb_tx < nb_rx)``, then they
1671443da3bSJohn McNamaramust be freed explicitly using ``rte_pktmbuf_free()``.
1681443da3bSJohn McNamara
1691443da3bSJohn McNamaraThe forwarding loop can be interrupted and the application closed using
1701443da3bSJohn McNamara``Ctrl-C``.
171