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