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