xref: /dpdk/doc/guides/sample_app_ug/l2_forward_event.rst (revision 7917b0d38e92e8b9ec5a870415b791420e10f11a)
1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright(c) 2010-2014 Intel Corporation.
3
4.. _l2_fwd_event_app:
5
6L2 Forwarding Eventdev Sample Application
7=========================================
8
9The L2 Forwarding eventdev sample application is a simple example of packet
10processing using the Data Plane Development Kit (DPDK) to demonstrate usage of
11poll and event mode packet I/O mechanism.
12
13Overview
14--------
15
16The L2 Forwarding eventdev sample application, performs L2 forwarding for each
17packet that is received on an RX_PORT. The destination port is the adjacent port
18from the enabled portmask, that is, if the first four ports are enabled (portmask=0x0f),
19ports 1 and 2 forward into each other, and ports 3 and 4 forward into each other.
20Also, if MAC addresses updating is enabled, the MAC addresses are affected as follows:
21
22*   The source MAC address is replaced by the TX_PORT MAC address
23
24*   The destination MAC address is replaced by  02:00:00:00:00:TX_PORT_ID
25
26Application receives packets from RX_PORT using below mentioned methods:
27
28*   Poll mode
29
30*   Eventdev mode (default)
31
32This application can be used to benchmark performance using a traffic-generator,
33as shown in the :numref:`figure_l2fwd_event_benchmark_setup`.
34
35.. _figure_l2fwd_event_benchmark_setup:
36
37.. figure:: img/l2_fwd_benchmark_setup.*
38
39   Performance Benchmark Setup (Basic Environment)
40
41Compiling the Application
42-------------------------
43
44To compile the sample application see :doc:`compiling`.
45
46The application is located in the ``l2fwd-event`` sub-directory.
47
48Running the Application
49-----------------------
50
51The application requires a number of command line options:
52
53.. code-block:: console
54
55    ./<build_dir>/examples/dpdk-l2fwd-event [EAL options] -- -p PORTMASK
56                                                        [-q NQ]
57                                                        [--[no-]mac-updating]
58                                                        [--mode=MODE]
59                                                        [--eventq-sched=SCHED_MODE]
60                                                        [--event-vector [--event-vector-size SIZE] [--event-vector-tmo NS]]
61
62where,
63
64*   p PORTMASK: A hexadecimal bitmask of the ports to configure
65
66*   q NQ: Maximum number of queues per lcore (default is 1)
67
68*   --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default).
69
70*   --mode=MODE: Packet transfer mode for I/O, poll or eventdev. Eventdev by default.
71
72*   --eventq-sched=SCHED_MODE: Event queue schedule mode, Ordered, Atomic or Parallel. Atomic by default.
73
74*   --config: Configure forwarding port pair mapping. Alternate port pairs by default.
75
76*   --event-vector: Enable event vectorization. Only valid if --mode=eventdev.
77
78*   --event-vector-size: Max vector size if event vectorization is enabled.
79
80*   --event-vector-tmo: Max timeout to form vector in nanoseconds if event vectorization is enabled.
81
82Sample usage commands are given below to run the application into different mode:
83
84Poll mode with 4 lcores, 16 ports and 8 RX queues per lcore and MAC address updating enabled,
85issue the command:
86
87.. code-block:: console
88
89    ./<build_dir>/examples/dpdk-l2fwd-event -l 0-3 -n 4 -- -q 8 -p ffff --mode=poll
90
91Eventdev mode with 4 lcores, 16 ports , sched method ordered and MAC address updating enabled,
92issue the command:
93
94.. code-block:: console
95
96    ./<build_dir>/examples/dpdk-l2fwd-event -l 0-3 -n 4 -- -p ffff --eventq-sched=ordered
97
98or
99
100.. code-block:: console
101
102    ./<build_dir>/examples/dpdk-l2fwd-event -l 0-3 -n 4 -- -q 8 -p ffff --mode=eventdev --eventq-sched=ordered
103
104Refer to the *DPDK Getting Started Guide* for general information on running
105applications and the Environment Abstraction Layer (EAL) options.
106
107To run application with S/W scheduler, it uses following DPDK services:
108
109*   Software scheduler
110*   Rx adapter service function
111*   Tx adapter service function
112
113Application needs service cores to run above mentioned services. Service cores
114must be provided as EAL parameters along with the --vdev=event_sw0 to enable S/W
115scheduler. Following is the sample command:
116
117.. code-block:: console
118
119    ./<build_dir>/examples/dpdk-l2fwd-event -l 0-7 -s 0-3 -n 4 --vdev event_sw0 -- -q 8 -p ffff --mode=eventdev --eventq-sched=ordered
120
121Explanation
122-----------
123
124The following sections provide some explanation of the code.
125
126.. _l2_fwd_event_app_cmd_arguments:
127
128Command Line Arguments
129~~~~~~~~~~~~~~~~~~~~~~
130
131The L2 Forwarding eventdev sample application takes specific parameters,
132in addition to Environment Abstraction Layer (EAL) arguments.
133The preferred way to parse parameters is to use the getopt() function,
134since it is part of a well-defined and portable library.
135
136The parsing of arguments is done in the **l2fwd_parse_args()** function for non
137eventdev parameters and in **parse_eventdev_args()** for eventdev parameters.
138The method of argument parsing is not described here. Refer to the
139*glibc getopt(3)* man page for details.
140
141EAL arguments are parsed first, then application-specific arguments.
142This is done at the beginning of the main() function and eventdev parameters
143are parsed in eventdev_resource_setup() function during eventdev setup:
144
145.. literalinclude:: ../../../examples/l2fwd-event/main.c
146        :language: c
147        :start-after: Init EAL. 8<
148        :end-before: >8 End of init EAL.
149        :dedent: 1
150
151Mbuf Pool Initialization
152~~~~~~~~~~~~~~~~~~~~~~~~
153
154Once the arguments are parsed, the mbuf pool is created.
155The mbuf pool contains a set of mbuf objects that will be used by the driver
156and the application to store network packet data:
157
158.. literalinclude:: ../../../examples/l2fwd-event/main.c
159        :language: c
160        :start-after: Create the mbuf pool. 8<
161        :end-before: >8 End of creation of mbuf pool.
162        :dedent: 1
163
164The rte_mempool is a generic structure used to handle pools of objects.
165In this case, it is necessary to create a pool that will be used by the driver.
166The number of allocated pkt mbufs is NB_MBUF, with a data room size of
167RTE_MBUF_DEFAULT_BUF_SIZE each.
168A per-lcore cache of 32 mbufs is kept.
169The memory is allocated in NUMA socket 0,
170but it is possible to extend this code to allocate one mbuf pool per socket.
171
172The rte_pktmbuf_pool_create() function uses the default mbuf pool and mbuf
173initializers, respectively rte_pktmbuf_pool_init() and rte_pktmbuf_init().
174An advanced application may want to use the mempool API to create the
175mbuf pool with more control.
176
177.. _l2_fwd_event_app_drv_init:
178
179Driver Initialization
180~~~~~~~~~~~~~~~~~~~~~
181
182The main part of the code in the main() function relates to the initialization
183of the driver. To fully understand this code, it is recommended to study the
184chapters that related to the Poll Mode and Event mode Driver in the
185*DPDK Programmer's Guide* - Rel 1.4 EAR and the *DPDK API Reference*.
186
187.. literalinclude:: ../../../examples/l2fwd-event/main.c
188        :language: c
189        :start-after: Reset l2fwd_dst_ports. 8<
190        :end-before: >8 End of reset l2fwd_dst_ports.
191        :dedent: 1
192
193The next step is to configure the RX and TX queues. For each port, there is only
194one RX queue (only one lcore is able to poll a given port). The number of TX
195queues depends on the number of available lcores. The rte_eth_dev_configure()
196function is used to configure the number of queues for a port:
197
198.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_common.c
199        :language: c
200        :start-after: Configure RX and TX queue. 8<
201        :end-before: >8 End of configuration RX and TX queue.
202        :dedent: 2
203
204RX Queue Initialization
205~~~~~~~~~~~~~~~~~~~~~~~
206
207The application uses one lcore to poll one or several ports, depending on the -q
208option, which specifies the number of queues per lcore.
209
210For example, if the user specifies -q 4, the application is able to poll four
211ports with one lcore. If there are 16 ports on the target (and if the portmask
212argument is -p ffff ), the application will need four lcores to poll all the
213ports.
214
215.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_common.c
216        :language: c
217        :start-after: Using lcore to poll one or several ports. 8<
218        :end-before: >8 End of using lcore to poll one or several ports.
219        :dedent: 2
220
221The list of queues that must be polled for a given lcore is stored in a private
222structure called struct lcore_queue_conf.
223
224.. literalinclude:: ../../../examples/l2fwd/main.c
225        :language: c
226        :start-after: List of queues to be polled for a given lcore. 8<
227        :end-before: >8 End of list of queues to be polled for a given lcore.
228
229The values n_rx_port and rx_port_list[] are used in the main packet processing
230loop (see :ref:`l2_fwd_event_app_rx_tx_packets`).
231
232.. _l2_fwd_event_app_tx_init:
233
234TX Queue Initialization
235~~~~~~~~~~~~~~~~~~~~~~~
236
237Each lcore should be able to transmit on any port. For every port, a single TX
238queue is initialized.
239
240.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_common.c
241        :language: c
242        :start-after: Init one TX queue on each port. 8<
243        :end-before: >8 End of init one TX queue on each port.
244        :dedent: 2
245
246To configure eventdev support, application setups following components:
247
248*   Event dev
249*   Event queue
250*   Event Port
251*   Rx/Tx adapters
252*   Ethernet ports
253
254.. _l2_fwd_event_app_event_dev_init:
255
256Event device Initialization
257~~~~~~~~~~~~~~~~~~~~~~~~~~~
258Application can use either H/W or S/W based event device scheduler
259implementation and supports single instance of event device. It configures event
260device as per below configuration
261
262.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event_generic.c
263        :language: c
264        :start-after: Configures event device as per below configuration. 8<
265        :end-before: >8 End of configuration event device as per below configuration.
266        :dedent: 1
267
268In case of S/W scheduler, application runs eventdev scheduler service on service
269core. Application retrieves service id and finds the best possible service core to
270run S/W scheduler.
271
272.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event.c
273        :language: c
274        :start-after: Running eventdev scheduler service on service core. 8<
275        :end-before: >8 End of running eventdev scheduler service on service core.
276        :dedent: 1
277
278Event queue Initialization
279~~~~~~~~~~~~~~~~~~~~~~~~~~
280Each Ethernet device is assigned a dedicated event queue which will be linked
281to all available event ports i.e. each lcore can dequeue packets from any of the
282Ethernet ports.
283
284.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event_generic.c
285        :language: c
286        :start-after: Event queue initialization. 8<
287        :end-before: >8 End of event queue initialization.
288        :dedent: 1
289
290In case of S/W scheduler, an extra event queue is created which will be used for
291Tx adapter service function for enqueue operation.
292
293.. _l2_fwd_app_event_port_init:
294
295Event port Initialization
296~~~~~~~~~~~~~~~~~~~~~~~~~
297Each worker thread is assigned a dedicated event port for enq/deq operations
298to/from an event device. All event ports are linked with all available event
299queues.
300
301.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event_generic.c
302        :language: c
303        :start-after: Event port initialization. 8<
304        :end-before: >8 End of event port initialization.
305        :dedent: 1
306
307In case of S/W scheduler, an extra event port is created by DPDK library which
308is retrieved  by the application and same will be used by Tx adapter service.
309
310.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event_generic.c
311        :language: c
312        :start-after: Extra port created. 8<
313        :end-before: >8 End of extra port created.
314        :dedent: 1
315
316Rx/Tx adapter Initialization
317~~~~~~~~~~~~~~~~~~~~~~~~~~~~
318Each Ethernet port is assigned a dedicated Rx/Tx adapter for H/W scheduler. Each
319Ethernet port's Rx queues are connected to its respective event queue at
320priority 0 via Rx adapter configuration and Ethernet port's tx queues are
321connected via Tx adapter.
322
323.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event_internal_port.c
324        :language: c
325        :start-after: Assigned ethernet port. 8<
326        :end-before: >8 End of assigned ethernet port.
327        :dedent: 1
328
329For S/W scheduler instead of dedicated adapters, common Rx/Tx adapters are
330configured which will be shared among all the Ethernet ports. Also DPDK library
331need service cores to run internal services for Rx/Tx adapters. Application gets
332service id for Rx/Tx adapters and after successful setup it runs the services
333on dedicated service cores.
334
335.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event.c
336        :language: c
337        :start-after: Gets service ID for RX/TX adapters. 8<
338        :end-before: >8 End of get service ID for RX/TX adapters.
339        :dedent: 1
340
341.. _l2_fwd_event_app_rx_tx_packets:
342
343Receive, Process and Transmit Packets
344~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
345
346In the **l2fwd_main_loop()** function, the main task is to read ingress packets from
347the RX queues. This is done using the following code:
348
349.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_poll.c
350        :language: c
351        :start-after: Reading ingress packets. 8<
352        :end-before: >8 End of reading ingress packets.
353        :dedent: 2
354
355Packets are read in a burst of size MAX_PKT_BURST. The rte_eth_rx_burst()
356function writes the mbuf pointers in a local table and returns the number of
357available mbufs in the table.
358
359Then, each mbuf in the table is processed by the l2fwd_simple_forward()
360function. The processing is very simple: process the TX port from the RX port,
361then replace the source and destination MAC addresses if MAC addresses updating
362is enabled.
363
364During the initialization process, a static array of destination ports
365(l2fwd_dst_ports[]) is filled such that for each source port, a destination port
366is assigned that is either the next or previous enabled port from the portmask.
367If number of ports are odd in portmask then packet from last port will be
368forwarded to first port i.e. if portmask=0x07, then forwarding will take place
369like p0--->p1, p1--->p2, p2--->p0.
370
371Also to optimize enqueue operation, l2fwd_simple_forward() stores incoming mbufs
372up to MAX_PKT_BURST. Once it reaches up to limit, all packets are transmitted to
373destination ports.
374
375.. literalinclude:: ../../../examples/l2fwd/main.c
376        :language: c
377        :start-after: Simple forward. 8<
378        :end-before: >8 End of simple forward.
379
380For this test application, the processing is exactly the same for all packets
381arriving on the same RX port. Therefore, it would have been possible to call
382the rte_eth_tx_buffer() function directly from the main loop to send all the
383received packets on the same TX port, using the burst-oriented send function,
384which is more efficient.
385
386However, in real-life applications (such as, L3 routing),
387packet N is not necessarily forwarded on the same port as packet N-1.
388The application is implemented to illustrate that, so the same approach can be
389reused in a more complex application.
390
391To ensure that no packets remain in the tables, each lcore does a draining of TX
392queue in its main loop. This technique introduces some latency when there are
393not many packets to send, however it improves performance:
394
395.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_poll.c
396        :language: c
397        :start-after: Draining TX queue in main loop. 8<
398        :end-before: >8 End of draining TX queue in main loop.
399        :dedent: 2
400
401In the **l2fwd_event_loop()** function, the main task is to read ingress
402packets from the event ports. This is done using the following code:
403
404.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event.c
405        :language: c
406        :start-after: Read packet from eventdev. 8<
407        :end-before: >8 End of reading packets from eventdev.
408        :dedent: 2
409
410
411Before reading packets, deq_len is fetched to ensure correct allowed deq length
412by the eventdev.
413The rte_event_dequeue_burst() function writes the mbuf pointers in a local table
414and returns the number of available mbufs in the table.
415
416Then, each mbuf in the table is processed by the l2fwd_eventdev_forward()
417function. The processing is very simple: process the TX port from the RX port,
418then replace the source and destination MAC addresses if MAC addresses updating
419is enabled.
420
421During the initialization process, a static array of destination ports
422(l2fwd_dst_ports[]) is filled such that for each source port, a destination port
423is assigned that is either the next or previous enabled port from the portmask.
424If number of ports are odd in portmask then packet from last port will be
425forwarded to first port i.e. if portmask=0x07, then forwarding will take place
426like p0--->p1, p1--->p2, p2--->p0.
427
428l2fwd_eventdev_forward() does not stores incoming mbufs. Packet will forwarded
429be to destination ports via Tx adapter or generic event dev enqueue API
430depending H/W or S/W scheduler is used.
431
432.. literalinclude:: ../../../examples/l2fwd-event/l2fwd_event.c
433        :language: c
434        :start-after: Read packet from eventdev. 8<
435        :end-before: >8 End of reading packets from eventdev.
436        :dedent: 2
437