a57d2f0bd217ffb19b0fad68e77dd68ca68b7938
[dpdk.git] / doc / guides / sample_app_ug / link_status_intr.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2010-2014 Intel Corporation.
3
4 Link Status Interrupt Sample Application
5 ========================================
6
7 The Link Status Interrupt sample application is a simple example of packet processing using
8 the Data Plane Development Kit (DPDK) that
9 demonstrates how network link status changes for a network port can be captured and
10 used by a DPDK application.
11
12 Overview
13 --------
14
15 The Link Status Interrupt sample application registers a user space callback for the link status interrupt of each port
16 and performs L2 forwarding for each packet that is received on an RX_PORT.
17 The following operations are performed:
18
19 *   RX_PORT and TX_PORT are paired with available ports one-by-one according to the core mask
20
21 *   The source MAC address is replaced by the TX_PORT MAC address
22
23 *   The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID
24
25 This application can be used to demonstrate the usage of link status interrupt and its user space callbacks
26 and the behavior of L2 forwarding each time the link status changes.
27
28 Compiling the Application
29 -------------------------
30
31 To compile the sample application see :doc:`compiling`.
32
33 The application is located in the ``link_status_interrupt`` sub-directory.
34
35 Running the Application
36 -----------------------
37
38 The application requires a number of command line options:
39
40 .. code-block:: console
41
42     ./<build_dir>/examples/dpdk-link_status_interrupt [EAL options] -- -p PORTMASK [-q NQ][-T PERIOD]
43
44 where,
45
46 *   -p PORTMASK: A hexadecimal bitmask of the ports to configure
47
48 *   -q NQ: A number of queues (=ports) per lcore (default is 1)
49
50 *   -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default)
51
52 To run the application in a linux environment with 4 lcores, 4 memory channels, 16 ports and 8 RX queues per lcore,
53 issue the command:
54
55 .. code-block:: console
56
57     $ ./<build_dir>/examples/dpdk-link_status_interrupt -l 0-3 -n 4-- -q 8 -p ffff
58
59 Refer to the *DPDK Getting Started Guide* for general information on running applications
60 and the Environment Abstraction Layer (EAL) options.
61
62 Explanation
63 -----------
64
65 The following sections provide some explanation of the code.
66
67 Command Line Arguments
68 ~~~~~~~~~~~~~~~~~~~~~~
69
70 The Link Status Interrupt sample application takes specific parameters,
71 in addition to Environment Abstraction Layer (EAL) arguments (see Section `Running the Application`_).
72
73 Command line parsing is done in the same way as it is done in the L2 Forwarding Sample Application.
74 See :ref:`l2_fwd_app_cmd_arguments` for more information.
75
76 Mbuf Pool Initialization
77 ~~~~~~~~~~~~~~~~~~~~~~~~
78
79 Mbuf pool initialization is done in the same way as it is done in the L2 Forwarding Sample Application.
80 See :ref:`l2_fwd_app_mbuf_init` for more information.
81
82 Driver Initialization
83 ~~~~~~~~~~~~~~~~~~~~~
84
85 The main part of the code in the main() function relates to the initialization of the driver.
86 To fully understand this code, it is recommended to study the chapters that related to the Poll Mode Driver in the
87 *DPDK Programmer's Guide and the DPDK API Reference*.
88
89 .. code-block:: c
90
91     /*
92      * Each logical core is assigned a dedicated TX queue on each port.
93      */
94
95     RTE_ETH_FOREACH_DEV(portid) {
96         /* skip ports that are not enabled */
97
98         if ((lsi_enabled_port_mask & (1 << portid)) == 0)
99             continue;
100
101         /* save the destination port id */
102
103         if (nb_ports_in_mask % 2) {
104             lsi_dst_ports[portid] = portid_last;
105             lsi_dst_ports[portid_last] = portid;
106         }
107         else
108             portid_last = portid;
109
110         nb_ports_in_mask++;
111
112         rte_eth_dev_info_get((uint8_t) portid, &dev_info);
113     }
114
115 The next step is to configure the RX and TX queues.
116 For each port, there is only one RX queue (only one lcore is able to poll a given port).
117 The number of TX queues depends on the number of available lcores.
118 The rte_eth_dev_configure() function is used to configure the number of queues for a port:
119
120 .. code-block:: c
121
122     ret = rte_eth_dev_configure((uint8_t) portid, 1, 1, &port_conf);
123     if (ret < 0)
124         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n", ret, portid);
125
126 The global configuration is stored in a static structure:
127
128 .. code-block:: c
129
130     static const struct rte_eth_conf port_conf = {
131         .rxmode = {
132             .split_hdr_size = 0,
133         },
134         .txmode = {},
135         .intr_conf = {
136             .lsc = 1, /**< link status interrupt feature enabled */
137         },
138     };
139
140 Configuring lsc to 0 (the default) disables the generation of any link status change interrupts in kernel space
141 and no user space interrupt event is received.
142 The public interface rte_eth_link_get() accesses the NIC registers directly to update the link status.
143 Configuring lsc to non-zero enables the generation of link status change interrupts in kernel space
144 when a link status change is present and calls the user space callbacks registered by the application.
145 The public interface rte_eth_link_get() just reads the link status in a global structure
146 that would be updated in the interrupt host thread only.
147
148 Interrupt Callback Registration
149 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150
151 The application can register one or more callbacks to a specific port and interrupt event.
152 An example callback function that has been written as indicated below.
153
154 .. code-block:: c
155
156     static void
157     lsi_event_callback(uint16_t port_id, enum rte_eth_event_type type, void *param)
158     {
159         struct rte_eth_link link;
160         int ret;
161         char link_status[RTE_ETH_LINK_MAX_STR_LEN];
162
163         RTE_SET_USED(param);
164
165         printf("\n\nIn registered callback...\n");
166
167         printf("Event type: %s\n", type == RTE_ETH_EVENT_INTR_LSC ? "LSC interrupt" : "unknown event");
168
169         ret = rte_eth_link_get_nowait(port_id, &link);
170         if (ret < 0) {
171             printf("Failed to get port %d link status: %s\n\n",
172                    port_id, rte_strerror(-ret));
173         } else {
174             rte_eth_link_to_str(link_status, sizeof(link_status), &link);
175             printf("Port %d %s\n", port_id, link_status);
176         }
177     }
178
179 This function is called when a link status interrupt is present for the right port.
180 The port_id indicates which port the interrupt applies to.
181 The type parameter identifies the interrupt event type,
182 which currently can be RTE_ETH_EVENT_INTR_LSC only, but other types can be added in the future.
183 The param parameter is the address of the parameter for the callback.
184 This function should be implemented with care since it will be called in the interrupt host thread,
185 which is different from the main thread of its caller.
186
187 The application registers the lsi_event_callback and a NULL parameter to the link status interrupt event on each port:
188
189 .. code-block:: c
190
191     rte_eth_dev_callback_register((uint8_t)portid, RTE_ETH_EVENT_INTR_LSC, lsi_event_callback, NULL);
192
193 This registration can be done only after calling the rte_eth_dev_configure() function and before calling any other function.
194 If lsc is initialized with 0, the callback is never called since no interrupt event would ever be present.
195
196 RX Queue Initialization
197 ~~~~~~~~~~~~~~~~~~~~~~~
198
199 The application uses one lcore to poll one or several ports, depending on the -q option,
200 which specifies the number of queues per lcore.
201
202 For example, if the user specifies -q 4, the application is able to poll four ports with one lcore.
203 If there are 16 ports on the target (and if the portmask argument is -p ffff),
204 the application will need four lcores to poll all the ports.
205
206 .. code-block:: c
207
208     ret = rte_eth_rx_queue_setup((uint8_t) portid, 0, nb_rxd, SOCKET0, &rx_conf, lsi_pktmbuf_pool);
209     if (ret < 0)
210         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, port=%u\n", ret, portid);
211
212 The list of queues that must be polled for a given lcore is stored in a private structure called struct lcore_queue_conf.
213
214 .. code-block:: c
215
216     struct lcore_queue_conf {
217         unsigned n_rx_port;
218         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE]; unsigned tx_queue_id;
219         struct mbuf_table tx_mbufs[LSI_MAX_PORTS];
220     } rte_cache_aligned;
221
222     struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
223
224 The n_rx_port and rx_port_list[] fields are used in the main packet processing loop
225 (see `Receive, Process and Transmit Packets`_).
226
227 The global configuration for the RX queues is stored in a static structure:
228
229 .. code-block:: c
230
231     static const struct rte_eth_rxconf rx_conf = {
232         .rx_thresh = {
233             .pthresh = RX_PTHRESH,
234             .hthresh = RX_HTHRESH,
235             .wthresh = RX_WTHRESH,
236         },
237     };
238
239 TX Queue Initialization
240 ~~~~~~~~~~~~~~~~~~~~~~~
241
242 Each lcore should be able to transmit on any port.
243 For every port, a single TX queue is initialized.
244
245 .. code-block:: c
246
247     /* init one TX queue logical core on each port */
248
249     fflush(stdout);
250
251     ret = rte_eth_tx_queue_setup(portid, 0, nb_txd, rte_eth_dev_socket_id(portid), &tx_conf);
252     if (ret < 0)
253         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d,port=%u\n", ret, (unsigned) portid);
254
255 The global configuration for TX queues is stored in a static structure:
256
257 .. code-block:: c
258
259     static const struct rte_eth_txconf tx_conf = {
260         .tx_thresh = {
261             .pthresh = TX_PTHRESH,
262             .hthresh = TX_HTHRESH,
263             .wthresh = TX_WTHRESH,
264         },
265         .tx_free_thresh = RTE_TEST_TX_DESC_DEFAULT + 1, /* disable feature */
266     };
267
268 Receive, Process and Transmit Packets
269 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
270
271 In the lsi_main_loop() function, the main task is to read ingress packets from the RX queues.
272 This is done using the following code:
273
274 .. code-block:: c
275
276     /*
277      *   Read packet from RX queues
278      */
279
280     for (i = 0; i < qconf->n_rx_port; i++) {
281         portid = qconf->rx_port_list[i];
282         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0, pkts_burst, MAX_PKT_BURST);
283         port_statistics[portid].rx += nb_rx;
284
285         for (j = 0; j < nb_rx; j++) {
286             m = pkts_burst[j];
287             rte_prefetch0(rte_pktmbuf_mtod(m, void *));
288             lsi_simple_forward(m, portid);
289         }
290     }
291
292 Packets are read in a burst of size MAX_PKT_BURST.
293 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
295 Then, each mbuf in the table is processed by the lsi_simple_forward() function.
296 The processing is very simple: processes the TX port from the RX port and then replaces the source and destination MAC addresses.
297
298 .. note::
299
300     In the following code, the two lines for calculating the output port require some explanation.
301     If portId is even, the first line does nothing (as portid & 1 will be 0), and the second line adds 1.
302     If portId is odd, the first line subtracts one and the second line does nothing.
303     Therefore, 0 goes to 1, and 1 to 0, 2 goes to 3 and 3 to 2, and so on.
304
305 .. code-block:: c
306
307     static void
308     lsi_simple_forward(struct rte_mbuf *m, unsigned portid)
309     {
310         struct rte_ether_hdr *eth;
311         void *tmp;
312         unsigned dst_port = lsi_dst_ports[portid];
313
314         eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
315
316         /* 02:00:00:00:00:xx */
317
318         tmp = &eth->d_addr.addr_bytes[0];
319
320         *((uint64_t *)tmp) = 0x000000000002 + (dst_port << 40);
321
322         /* src addr */
323         rte_ether_addr_copy(&lsi_ports_eth_addr[dst_port], &eth->s_addr);
324
325         lsi_send_packet(m, dst_port);
326     }
327
328 Then, the packet is sent using the lsi_send_packet(m, dst_port) function.
329 For this test application, the processing is exactly the same for all packets arriving on the same RX port.
330 Therefore, it would have been possible to call the lsi_send_burst() function directly from the main loop
331 to send all the received packets on the same TX port using
332 the burst-oriented send function, which is more efficient.
333
334 However, in real-life applications (such as, L3 routing),
335 packet N is not necessarily forwarded on the same port as packet N-1.
336 The application is implemented to illustrate that so the same approach can be reused in a more complex application.
337
338 The lsi_send_packet() function stores the packet in a per-lcore and per-txport table.
339 If the table is full, the whole packets table is transmitted using the lsi_send_burst() function:
340
341 .. code-block:: c
342
343     /* Send the packet on an output interface */
344
345     static int
346     lsi_send_packet(struct rte_mbuf *m, uint16_t port)
347     {
348         unsigned lcore_id, len;
349         struct lcore_queue_conf *qconf;
350
351         lcore_id = rte_lcore_id();
352         qconf = &lcore_queue_conf[lcore_id];
353         len = qconf->tx_mbufs[port].len;
354         qconf->tx_mbufs[port].m_table[len] = m;
355         len++;
356
357         /* enough pkts to be sent */
358
359         if (unlikely(len == MAX_PKT_BURST)) {
360             lsi_send_burst(qconf, MAX_PKT_BURST, port);
361             len = 0;
362         }
363         qconf->tx_mbufs[port].len = len;
364
365         return 0;
366     }
367
368 To ensure that no packets remain in the tables, each lcore does a draining of the TX queue in its main loop.
369 This technique introduces some latency when there are not many packets to send.
370 However, it improves performance:
371
372 .. code-block:: c
373
374     cur_tsc = rte_rdtsc();
375
376     /*
377      *    TX burst queue drain
378      */
379
380     diff_tsc = cur_tsc - prev_tsc;
381
382     if (unlikely(diff_tsc > drain_tsc)) {
383         /* this could be optimized (use queueid instead of * portid), but it is not called so often */
384
385         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
386             if (qconf->tx_mbufs[portid].len == 0)
387                 continue;
388
389             lsi_send_burst(&lcore_queue_conf[lcore_id],
390             qconf->tx_mbufs[portid].len, (uint8_t) portid);
391             qconf->tx_mbufs[portid].len = 0;
392         }
393
394         /* if timer is enabled */
395
396         if (timer_period > 0) {
397             /* advance the timer */
398
399             timer_tsc += diff_tsc;
400
401             /* if timer has reached its timeout */
402
403             if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
404                 /* do this only on main core */
405                 if (lcore_id == rte_get_main_lcore()) {
406                     print_stats();
407
408                     /* reset the timer */
409                     timer_tsc = 0;
410                 }
411             }
412         }
413         prev_tsc = cur_tsc;
414    }