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