1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
.. SPDX-License-Identifier: BSD-3-Clause
Copyright(c) 2015 Intel Corporation.
Basic Forwarding Sample Application
===================================
The Basic Forwarding sample application is a simple *skeleton* example of a
forwarding application.
Overview
--------
This application demonstrates the basic components of a DPDK forwarding application.
For more detailed implementations, see the L2 and L3 forwarding sample applications.
Compiling the Application
-------------------------
To compile the sample application, see :doc:`compiling`.
The application is located in the ``skeleton`` sub-directory.
Running the Application
-----------------------
To run the example in a ``linux`` environment:
.. code-block:: console
./<build_dir>/examples/dpdk-skeleton -l 1 -n 4
Refer to *DPDK Getting Started Guide* for general information on running
applications and Environment Abstraction Layer (EAL) options.
Explanation
-----------
The following sections provide an explanation of the main components of the
code.
All DPDK library functions used in the sample code are prefixed with ``rte_``
and are explained in detail in the *DPDK API Documentation*.
The Main Function
~~~~~~~~~~~~~~~~~
The ``main()`` function performs the initialization and calls the execution
threads for each lcore.
The first task is to initialize the Environment Abstraction Layer (EAL).
The ``argc`` and ``argv`` arguments are provided to the ``rte_eal_init()``
function. The value returned is the number of parsed arguments:
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Initializion the Environment Abstraction Layer (EAL). 8<
:end-before: >8 End of initialization the Environment Abstraction Layer (EAL).
:dedent: 1
The ``main()`` also allocates a mempool to hold the mbufs (Message Buffers)
used by the application:
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Allocates mempool to hold the mbufs. 8<
:end-before: >8 End of allocating mempool to hold mbuf.
:dedent: 1
Mbufs are the packet buffer structure used by DPDK. They are explained in
detail in the "Mbuf Library" section of the *DPDK Programmer's Guide*.
The ``main()`` function also initializes all the ports using the user defined
``port_init()`` function which is explained in the next section:
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Initializing all ports. 8<
:end-before: >8 End of initializing all ports.
:dedent: 1
Once the initialization is complete, the application is ready to launch a
function on an lcore.
In this example, ``lcore_main()`` is called on a single lcore.
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Called on single lcore. 8<
:end-before: >8 End of called on single lcore.
:dedent: 1
The ``lcore_main()`` function is explained below.
The Port Initialization Function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The main functional part of the port initialization used in the Basic
Forwarding application is shown below:
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Main functional part of port initialization. 8<
:end-before: >8 End of main functional part of port initialization.
The Ethernet ports are configured with default settings using the
``rte_eth_dev_configure()`` function.
In this example, the ports are set up with 1 Rx and 1 Tx queue using the
``rte_eth_rx_queue_setup()`` and ``rte_eth_tx_queue_setup()`` functions.
The Ethernet port is then started:
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Starting Ethernet port. 8<
:end-before: >8 End of starting of ethernet port.
:dedent: 1
Finally, the Rx port is set in promiscuous mode:
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Enable RX in promiscuous mode for the Ethernet device.
:end-before: End of setting RX port in promiscuous mode.
:dedent: 1
The Lcores Main
~~~~~~~~~~~~~~~
As we saw above, the ``main()`` function calls an application function
on the available lcores.
For the basic forwarding application, the lcore function
looks like the following:
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Basic forwarding application lcore. 8<
:end-before: >8 End Basic forwarding application lcore.
The main work of the application is done within the loop:
.. literalinclude:: ../../../examples/skeleton/basicfwd.c
:language: c
:start-after: Main work of application loop. 8<
:end-before: >8 End of loop.
:dedent: 1
Packets are received in bursts on the RX ports and transmitted in bursts on
the TX ports. The ports are grouped in pairs with a simple mapping scheme
using the an XOR on the port number::
0 -> 1
1 -> 0
2 -> 3
3 -> 2
etc.
The ``rte_eth_tx_burst()`` function frees the memory buffers of packets that
are transmitted. If packets fail to transmit, ``(nb_tx < nb_rx)``, then they
must be freed explicitly using ``rte_pktmbuf_free()``.
The forwarding loop can be interrupted and the application closed using
``Ctrl-C``.
|