1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2010-2015 Intel Corporation. 3 4L2 Forwarding Sample Application (in Real and Virtualized Environments) with core load statistics 5================================================================================================= 6 7The L2 Forwarding sample application is a simple example of packet processing using 8the Data Plane Development Kit (DPDK) which 9also takes advantage of Single Root I/O Virtualization (SR-IOV) features in a virtualized environment. 10 11.. note:: 12 13 This application is a variation of L2 Forwarding sample application. It demonstrate possible 14 scheme of job stats library usage therefore some parts of this document is identical with original 15 L2 forwarding application. 16 17Overview 18-------- 19 20The L2 Forwarding sample application, which can operate in real and virtualized environments, 21performs L2 forwarding for each packet that is received. 22The destination port is the adjacent port from the enabled portmask 23if: the first four ports are enabled (portmask ``0xf``), 24ports 1 and 2 forward into each other, and ports 3 and 4 forward into each other. 25 26The MAC addresses are affected as follows: 27 28* The source MAC address is replaced by the TX port MAC address 29 30* The destination MAC address is replaced by ``02:00:00:00:00:TX_PORT_ID`` 31 32This application can be used to benchmark performance using a traffic-generator, as shown in the :numref:`figure_l2_fwd_benchmark_setup_jobstats`. 33 34The application can also be used in a virtualized environment as shown in :numref:`figure_l2_fwd_virtenv_benchmark_setup_jobstats`. 35 36The L2 Forwarding application can also be used as a starting point for developing a new application based on the DPDK. 37 38.. _figure_l2_fwd_benchmark_setup_jobstats: 39 40.. figure:: img/l2_fwd_benchmark_setup.* 41 42 Performance Benchmark Setup (Basic Environment) 43 44.. _figure_l2_fwd_virtenv_benchmark_setup_jobstats: 45 46.. figure:: img/l2_fwd_virtenv_benchmark_setup.* 47 48 Performance Benchmark Setup (Virtualized Environment) 49 50 51Virtual Function Setup Instructions 52~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 53 54This application can use the virtual function available in the system and 55can be used in a virtual machine without passing through 56the whole Network Device into a guest machine when in a virtualized scenario. 57The virtual functions can be enabled in the host machine or the hypervisor with the respective physical function driver. 58 59For example, in a Linux* host machine, it is possible to enable a virtual function using the following command: 60 61.. code-block:: console 62 63 modprobe ixgbe max_vfs=2,2 64 65This command enables two Virtual Functions on each of Physical Function of the NIC, 66with two physical ports in the PCI configuration space. 67It is important to note that enabled Virtual Function 0 and 2 would belong to Physical Function 0 68and Virtual Function 1 and 3 would belong to Physical Function 1, 69enabling a total of four Virtual Functions (in this case). 70 71 72Compiling the Application 73------------------------- 74 75To compile the sample application, see :doc:`compiling`. 76 77The application is located in the ``l2fwd-jobstats`` sub-directory. 78 79 80Running the Application 81----------------------- 82 83The application requires a number of command line options: 84 85.. code-block:: console 86 87 ./<build_dir>/examples/dpdk-l2fwd-jobstats [EAL options] -- -p PORTMASK [-q NQ] [-l] 88 89where, 90 91* p PORTMASK: A hexadecimal bitmask of the ports to configure 92 93* q NQ: Maximum number of queues per lcore (default is 1) 94 95* l: Use locale thousands separator when formatting big numbers. 96 97To run the application in a Linux environment with 4 lcores, 16 ports, 8 RX queues per lcore 98and thousands separator printing, issue the command: 99 100.. code-block:: console 101 102 $ ./<build_dir>/examples/dpdk-l2fwd-jobstats -l 0-3 -n 4 -- -q 8 -p ffff -l 103 104Refer to the *DPDK Getting Started Guide* for general information on running applications 105and the Environment Abstraction Layer (EAL) options. 106 107 108Explanation 109----------- 110 111The following sections provide explanation of the code. 112 113 114Command Line Arguments 115~~~~~~~~~~~~~~~~~~~~~~ 116 117The L2 Forwarding sample application takes specific parameters 118in addition to Environment Abstraction Layer (EAL) arguments 119(see `Running the Application`_). 120The preferred way to parse parameters is to use the ``getopt()`` function, 121since it is part of a well-defined and portable library. 122 123The parsing of arguments is done in the ``l2fwd_parse_args()`` function. 124This method of argument parsing is not described here. 125Refer to the *glibc getopt(3)* man page for details. 126 127EAL arguments are parsed first, then application-specific arguments. 128This is done at the beginning of the ``main()`` function: 129 130.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 131 :language: c 132 :start-after: Init EAL. 8< 133 :end-before: >8 End of init EAL. 134 :dedent: 1 135 136Mbuf Pool Initialization 137~~~~~~~~~~~~~~~~~~~~~~~~ 138 139Once the arguments are parsed, the mbuf pool is created. 140The mbuf pool contains a set of mbuf objects that will be used by the driver 141and the application to store network packet data: 142 143.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 144 :language: c 145 :start-after: Create the mbuf pool. 8< 146 :end-before: >8 End of creation of mbuf pool. 147 :dedent: 1 148 149The rte_mempool is a generic structure used to handle pools of objects. 150In this case, it is necessary to create a pool that will be used by the driver. 151The number of allocated pkt mbufs is ``NB_MBUF``, 152with a data room size of ``RTE_MBUF_DEFAULT_BUF_SIZE`` each. 153A per-lcore cache of ``MEMPOOL_CACHE_SIZE`` mbufs is kept. 154The memory is allocated in ``rte_socket_id()`` socket, 155but it is possible to extend this code to allocate one mbuf pool per socket. 156 157The ``rte_pktmbuf_pool_create()`` function uses the default mbuf pool and mbuf 158initializers, respectively ``rte_pktmbuf_pool_init()`` and ``rte_pktmbuf_init()``. 159An advanced application may want to use the mempool API to create the 160mbuf pool with more control. 161 162Driver Initialization 163~~~~~~~~~~~~~~~~~~~~~ 164 165The main part of the code in the ``main()`` function relates to the initialization of the driver. 166To fully understand this code, it is recommended to study the chapters that related to the Poll Mode Driver 167in the *DPDK Programmer's Guide* and the *DPDK API Reference*. 168 169.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 170 :language: c 171 :start-after: Reset l2fwd_dst_ports. 8< 172 :end-before: >8 End of reset l2fwd_dst_ports. 173 :dedent: 1 174 175The next step is to configure the RX and TX queues. 176For each port, there is only one RX queue (only one lcore is able to poll a given port). 177The number of TX queues depends on the number of available lcores. 178The ``rte_eth_dev_configure()`` function is used to configure the number of queues for a port: 179 180.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 181 :language: c 182 :start-after: Configure the RX and TX queues. 8< 183 :end-before: >8 End of configuring the RX and TX queues. 184 :dedent: 2 185 186RX Queue Initialization 187~~~~~~~~~~~~~~~~~~~~~~~ 188 189The application uses one lcore to poll one or several ports, depending on the -q option, 190which specifies the number of queues per lcore. 191 192For example, if the user specifies -q 4, the application is able to poll four ports with one lcore. 193If there are 16 ports on the target (and if the portmask argument is -p ffff ), 194the application will need four lcores to poll all the ports. 195 196.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 197 :language: c 198 :start-after: RX queue initialization. 8< 199 :end-before: >8 End of RX queue initialization. 200 :dedent: 2 201 202The list of queues that must be polled for a given lcore is stored in a private structure called struct lcore_queue_conf. 203 204.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 205 :language: c 206 :start-after: List of queues to be polled for given lcore. 8< 207 :end-before: >8 End of list of queues to be polled for given lcore. 208 209Values of struct lcore_queue_conf: 210 211* n_rx_port and rx_port_list[] are used in the main packet processing loop 212 (see Section `Receive, Process and Transmit Packets`_ later in this chapter). 213 214* rx_timers and flush_timer are used to ensure forced TX on low packet rate. 215 216* flush_job, idle_job and jobs_context are librte_jobstats objects used for managing l2fwd jobs. 217 218* stats_read_pending and lock are used during job stats read phase. 219 220TX Queue Initialization 221~~~~~~~~~~~~~~~~~~~~~~~ 222 223Each lcore should be able to transmit on any port. For every port, a single TX queue is initialized. 224 225.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 226 :language: c 227 :start-after: Init one TX queue on each port. 8< 228 :end-before: >8 End of init one TX queue on each port. 229 :dedent: 2 230 231Jobs statistics initialization 232~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 233There are several statistics objects available: 234 235* Flush job statistics 236 237.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 238 :language: c 239 :start-after: Add flush job. 8< 240 :end-before: >8 End of add flush job. 241 :dedent: 2 242 243* Statistics per RX port 244 245.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 246 :language: c 247 :start-after: Setup forward job. 8< 248 :end-before: >8 End of forward job. 249 :dedent: 3 250 251Following parameters are passed to ``rte_jobstats_init()``: 252 253* 0 as minimal poll period 254 255* drain_tsc as maximum poll period 256 257* MAX_PKT_BURST as desired target value (RX burst size) 258 259Main loop 260~~~~~~~~~ 261 262The forwarding path is reworked comparing to original L2 Forwarding application. 263In the ``l2fwd_main_loop()`` function, three loops are placed. 264 265.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 266 :language: c 267 :start-after: Minimize impact of stats reading. 8< 268 :end-before: >8 End of minimize impact of stats reading. 269 :dedent: 1 270 271The first infinite for loop is to minimize impact of stats reading. 272Lock is only locked/unlocked when asked. 273 274Second inner while loop do the whole jobs management. 275When any job is ready, the use ``rte_timer_manage()`` is used to call the job handler. 276 277In this place, functions ``l2fwd_fwd_job()`` and ``l2fwd_flush_job()`` are called when needed. 278Then, ``rte_jobstats_context_finish()`` is called to mark loop end - 279no other jobs are ready to execute. 280By this time, stats are ready to be read 281and if stats_read_pending is set, loop breaks allowing stats to be read. 282 283Third do-while loop is the idle job (idle stats counter). 284Its only purpose is monitoring if any job is ready 285or stats job read is pending for this lcore. 286Statistics from this part of the code is considered as 287the headroom available for additional processing. 288 289Receive, Process and Transmit Packets 290~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 291 292The main task of ``l2fwd_fwd_job()`` function is to read ingress packets 293from the Rx queue of particular port and forward it. 294This is done using the following code: 295 296.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 297 :language: c 298 :start-after: Call rx burst 2 times. 8< 299 :end-before: >8 End of call rx burst 2 times. 300 :dedent: 1 301 302Packets are read in a burst of size MAX_PKT_BURST. 303Then, each mbuf in the table is processed by the ``l2fwd_simple_forward()`` function. 304The processing is very simple: process the TX port from the RX port, then replace the source and destination MAC addresses. 305 306The ``rte_eth_rx_burst()`` function writes the mbuf pointers in a local table 307and returns the number of available mbufs in the table. 308 309After first read, a second try is issued. 310 311.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 312 :language: c 313 :start-after: Read second try. 8< 314 :end-before: >8 End of read second try. 315 :dedent: 1 316 317This second read is important to give the job stats library 318feedback of how many packets were processed. 319 320.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 321 :language: c 322 :start-after: Adjust period time in which we are running here. 8< 323 :end-before: >8 End of adjust period time in which we are running. 324 :dedent: 1 325 326To maximize performance, exactly ``MAX_PKT_BURST`` is expected (the target value) 327to be read for each ``l2fwd_fwd_job()`` call. 328If ``total_nb_rx`` is smaller than target value ``job->period`` will be increased. 329If it is greater, the period will be decreased. 330 331.. note:: 332 333 In the following code, one line for getting the output port requires some explanation. 334 335During the initialization process, 336a static array of destination ports (``l2fwd_dst_ports[]``) 337is filled such that for each source port, 338a destination port is assigned that is either the next or previous enabled port from the portmask. 339Naturally, the number of ports in the portmask must be even, otherwise, the application exits. 340 341.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 342 :language: c 343 :start-after: Start of l2fwd_simple_forward. 8< 344 :end-before: >8 End of l2fwd_simple_forward. 345 346Then, the packet is sent using the ``l2fwd_send_packet(m, dst_port)`` function. 347For this test application, the processing is exactly the same for all packets arriving on the same RX port. 348Therefore, it would have been possible to call the ``l2fwd_send_burst()`` function 349directly from the main loop 350to send all the received packets on the same TX port, 351using the burst-oriented send function, which is more efficient. 352 353However, in real-life applications (such as, L3 routing), 354packet N is not necessarily forwarded on the same port as packet N-1. 355The application is implemented to illustrate that, so the same approach can be reused in a more complex application. 356 357The ``l2fwd_send_packet()`` function stores the packet in a per-lcore and per-txport table. 358If the table is full, the whole packets table is transmitted 359using the ``l2fwd_send_burst()`` function: 360 361.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 362 :language: c 363 :start-after: Enqueue packets for TX and prepare them to be sent. 8< 364 :end-before: >8 End of Enqueuing packets for TX. 365 366To ensure that no packets remain in the tables, the flush job exists. 367The ``l2fwd_flush_job()`` 368is called periodically to for each lcore draining TX queue of each port. 369This technique introduces some latency when there are not many packets to send, 370however it improves performance: 371 372.. literalinclude:: ../../../examples/l2fwd-jobstats/main.c 373 :language: c 374 :start-after: Draining TX queue of each port. 8< 375 :end-before: >8 End of draining TX queue of each port. 376