1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2010-2014 Intel Corporation. 3 4Link Status Interrupt Sample Application 5======================================== 6 7The Link Status Interrupt sample application is a simple example of packet processing using 8the Data Plane Development Kit (DPDK) that 9demonstrates how network link status changes for a network port can be captured and 10used by a DPDK application. 11 12Overview 13-------- 14 15The Link Status Interrupt sample application registers a user space callback for the link status interrupt of each port 16and performs L2 forwarding for each packet that is received on an RX_PORT. 17The 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 25This application can be used to demonstrate the usage of link status interrupt and its user space callbacks 26and the behavior of L2 forwarding each time the link status changes. 27 28Compiling the Application 29------------------------- 30 31To compile the sample application see :doc:`compiling`. 32 33The application is located in the ``link_status_interrupt`` sub-directory. 34 35Running the Application 36----------------------- 37 38The 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 44where, 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 52To run the application in a linux environment with 4 lcores, 4 memory channels, 16 ports and 8 RX queues per lcore, 53issue 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 59Refer to the *DPDK Getting Started Guide* for general information on running applications 60and the Environment Abstraction Layer (EAL) options. 61 62Explanation 63----------- 64 65The following sections provide some explanation of the code. 66 67Command Line Arguments 68~~~~~~~~~~~~~~~~~~~~~~ 69 70The Link Status Interrupt sample application takes specific parameters, 71in addition to Environment Abstraction Layer (EAL) arguments (see Section `Running the Application`_). 72 73Command line parsing is done in the same way as it is done in the L2 Forwarding Sample Application. 74See :ref:`l2_fwd_app_cmd_arguments` for more information. 75 76Mbuf Pool Initialization 77~~~~~~~~~~~~~~~~~~~~~~~~ 78 79Mbuf pool initialization is done in the same way as it is done in the L2 Forwarding Sample Application. 80See :ref:`l2_fwd_app_mbuf_init` for more information. 81 82Driver Initialization 83~~~~~~~~~~~~~~~~~~~~~ 84 85The main part of the code in the main() function relates to the initialization of the driver. 86To 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.. literalinclude:: ../../../examples/link_status_interrupt/main.c 90 :language: c 91 :start-after: Each logical core is assigned a dedicated TX queue on each port. 8< 92 :end-before: >8 End of assigning logical core. 93 :dedent: 1 94 95The next step is to configure the RX and TX queues. 96For each port, there is only one RX queue (only one lcore is able to poll a given port). 97The number of TX queues depends on the number of available lcores. 98The rte_eth_dev_configure() function is used to configure the number of queues for a port: 99 100.. literalinclude:: ../../../examples/link_status_interrupt/main.c 101 :language: c 102 :start-after: Configure RX and TX queues. 8< 103 :end-before: >8 End of configure RX and TX queues. 104 :dedent: 2 105 106The global configuration is stored in a static structure: 107 108.. literalinclude:: ../../../examples/link_status_interrupt/main.c 109 :language: c 110 :start-after: Global configuration stored in a static structure. 8< 111 :end-before: >8 End of global configuration stored in a static structure. 112 113Configuring lsc to 0 (the default) disables the generation of any link status change interrupts in kernel space 114and no user space interrupt event is received. 115The public interface rte_eth_link_get() accesses the NIC registers directly to update the link status. 116Configuring lsc to non-zero enables the generation of link status change interrupts in kernel space 117when a link status change is present and calls the user space callbacks registered by the application. 118The public interface rte_eth_link_get() just reads the link status in a global structure 119that would be updated in the interrupt host thread only. 120 121Interrupt Callback Registration 122~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 123 124The application can register one or more callbacks to a specific port and interrupt event. 125An example callback function that has been written as indicated below. 126 127.. literalinclude:: ../../../examples/link_status_interrupt/main.c 128 :language: c 129 :start-after: lsi_event_callback 8< 130 :end-before: >8 End of registering one or more callbacks. 131 132This function is called when a link status interrupt is present for the right port. 133The port_id indicates which port the interrupt applies to. 134The type parameter identifies the interrupt event type, 135which currently can be RTE_ETH_EVENT_INTR_LSC only, but other types can be added in the future. 136The param parameter is the address of the parameter for the callback. 137This function should be implemented with care since it will be called in the interrupt host thread, 138which is different from the main thread of its caller. 139 140The application registers the lsi_event_callback and a NULL parameter to the link status interrupt event on each port: 141 142.. literalinclude:: ../../../examples/link_status_interrupt/main.c 143 :language: c 144 :start-after: RTE callback register. 8< 145 :end-before: >8 End of registering lsi interrupt callback. 146 :dedent: 2 147 148This registration can be done only after calling the rte_eth_dev_configure() function and before calling any other function. 149If lsc is initialized with 0, the callback is never called since no interrupt event would ever be present. 150 151RX Queue Initialization 152~~~~~~~~~~~~~~~~~~~~~~~ 153 154The application uses one lcore to poll one or several ports, depending on the -q option, 155which specifies the number of queues per lcore. 156 157For example, if the user specifies -q 4, the application is able to poll four ports with one lcore. 158If there are 16 ports on the target (and if the portmask argument is -p ffff), 159the application will need four lcores to poll all the ports. 160 161.. literalinclude:: ../../../examples/link_status_interrupt/main.c 162 :language: c 163 :start-after: RX queue initialization. 8< 164 :end-before: >8 End of RX queue initialization. 165 :dedent: 2 166 167The list of queues that must be polled for a given lcore is stored in a private structure called struct lcore_queue_conf. 168 169.. literalinclude:: ../../../examples/link_status_interrupt/main.c 170 :language: c 171 :start-after: List of queues must be polled for a give lcore. 8< 172 :end-before: >8 End of list of queues to be polled. 173 174The n_rx_port and rx_port_list[] fields are used in the main packet processing loop 175(see `Receive, Process and Transmit Packets`_). 176 177The global configuration for the RX queues is stored in a static structure: 178 179.. literalinclude:: ../../../examples/link_status_interrupt/main.c 180 :language: c 181 :start-after: List of queues must be polled for a give lcore. 8< 182 :end-before: >8 End of list of queues to be polled. 183 184TX Queue Initialization 185~~~~~~~~~~~~~~~~~~~~~~~ 186 187Each lcore should be able to transmit on any port. 188For every port, a single TX queue is initialized. 189 190.. literalinclude:: ../../../examples/link_status_interrupt/main.c 191 :language: c 192 :start-after: init one TX queue logical core on each port. 8< 193 :end-before: >8 End of init one TX queue. 194 :dedent: 2 195 196The global configuration for TX queues is stored in a static structure: 197 198.. code-block:: c 199 200 static const struct rte_eth_txconf tx_conf = { 201 .tx_thresh = { 202 .pthresh = TX_PTHRESH, 203 .hthresh = TX_HTHRESH, 204 .wthresh = TX_WTHRESH, 205 }, 206 .tx_free_thresh = RTE_TEST_TX_DESC_DEFAULT + 1, /* disable feature */ 207 }; 208 209Receive, Process and Transmit Packets 210~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 211 212In the lsi_main_loop() function, the main task is to read ingress packets from the RX queues. 213This is done using the following code: 214 215.. literalinclude:: ../../../examples/link_status_interrupt/main.c 216 :language: c 217 :start-after: Read packet from RX queues. 8< 218 :end-before: >8 End of reading packet from RX queues. 219 :dedent: 2 220 221Packets are read in a burst of size MAX_PKT_BURST. 222The rte_eth_rx_burst() function writes the mbuf pointers in a local table and returns the number of available mbufs in the table. 223 224Then, each mbuf in the table is processed by the lsi_simple_forward() function. 225The processing is very simple: processes the TX port from the RX port and then replaces the source and destination MAC addresses. 226 227.. note:: 228 229 In the following code, the two lines for calculating the output port require some explanation. 230 If portId is even, the first line does nothing (as portid & 1 will be 0), and the second line adds 1. 231 If portId is odd, the first line subtracts one and the second line does nothing. 232 Therefore, 0 goes to 1, and 1 to 0, 2 goes to 3 and 3 to 2, and so on. 233 234.. literalinclude:: ../../../examples/link_status_interrupt/main.c 235 :language: c 236 :start-after: Replacing the source and destination MAC addresses. 8< 237 :end-before: >8 End of replacing the source and destination MAC addresses. 238 239Then, the packet is sent using the lsi_send_packet(m, dst_port) function. 240For this test application, the processing is exactly the same for all packets arriving on the same RX port. 241Therefore, it would have been possible to call the lsi_send_burst() function directly from the main loop 242to send all the received packets on the same TX port using 243the burst-oriented send function, which is more efficient. 244 245However, in real-life applications (such as, L3 routing), 246packet N is not necessarily forwarded on the same port as packet N-1. 247The application is implemented to illustrate that so the same approach can be reused in a more complex application. 248 249The lsi_send_packet() function stores the packet in a per-lcore and per-txport table. 250If the table is full, the whole packets table is transmitted using the lsi_send_burst() function: 251 252.. literalinclude:: ../../../examples/l2fwd-crypto/main.c 253 :language: c 254 :start-after: Enqueue packets for TX and prepare them to be sent. 8< 255 :end-before: >8 End of Enqueuing packets for TX. 256 257To ensure that no packets remain in the tables, each lcore does a draining of the TX queue in its main loop. 258This technique introduces some latency when there are not many packets to send. 259However, it improves performance: 260 261.. literalinclude:: ../../../examples/link_status_interrupt/main.c 262 :language: c 263 :start-after: Draining TX queue in its main loop. 8< 264 :end-before: >8 End of draining TX queue in its main loop. 265 :dedent: 2 266