1.. SPDX-License-Identifier: BSD-3-Clause 2 Copyright(c) 2015 Intel Corporation. 3 4RX/TX Callbacks Sample Application 5================================== 6 7The RX/TX Callbacks sample application is a packet forwarding application that 8demonstrates the use of user defined callbacks on received and transmitted 9packets. The application performs a simple latency check, using callbacks, to 10determine the time packets spend within the application. 11 12In the sample application a user defined callback is applied to all received 13packets to add a timestamp. A separate callback is applied to all packets 14prior to transmission to calculate the elapsed time, in CPU cycles. 15 16If hardware timestamping is supported by the NIC, the sample application will 17also display the average latency since the packet was timestamped in hardware, 18on top of the latency since the packet was received and processed by the RX 19callback. 20 21Compiling the Application 22------------------------- 23 24To compile the sample application see :doc:`compiling`. 25 26The application is located in the ``rxtx_callbacks`` sub-directory. 27 28The callbacks feature requires that the ``CONFIG_RTE_ETHDEV_RXTX_CALLBACKS`` 29setting is on in the ``config/common_`` config file that applies to the 30target. This is generally on by default: 31 32.. code-block:: console 33 34 CONFIG_RTE_ETHDEV_RXTX_CALLBACKS=y 35 36Running the Application 37----------------------- 38 39To run the example in a ``linux`` environment: 40 41.. code-block:: console 42 43 ./build/rxtx_callbacks -l 1 -n 4 -- [-t] 44 45Use -t to enable hardware timestamping. If not supported by the NIC, an error 46will be displayed. 47 48Refer to *DPDK Getting Started Guide* for general information on running 49applications and the Environment Abstraction Layer (EAL) options. 50 51 52 53Explanation 54----------- 55 56The ``rxtx_callbacks`` application is mainly a simple forwarding application 57based on the :doc:`skeleton`. See that section of the documentation for more 58details of the forwarding part of the application. 59 60The sections below explain the additional RX/TX callback code. 61 62 63The Main Function 64~~~~~~~~~~~~~~~~~ 65 66The ``main()`` function performs the application initialization and calls the 67execution threads for each lcore. This function is effectively identical to 68the ``main()`` function explained in :doc:`skeleton`. 69 70The ``lcore_main()`` function is also identical. 71 72The main difference is in the user defined ``port_init()`` function where the 73callbacks are added. This is explained in the next section: 74 75 76The Port Initialization Function 77~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 78 79The main functional part of the port initialization is shown below with 80comments: 81 82.. code-block:: c 83 84 static inline int 85 port_init(uint16_t port, struct rte_mempool *mbuf_pool) 86 { 87 struct rte_eth_conf port_conf = port_conf_default; 88 const uint16_t rx_rings = 1, tx_rings = 1; 89 struct rte_ether_addr addr; 90 int retval; 91 uint16_t q; 92 93 /* Configure the Ethernet device. */ 94 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); 95 if (retval != 0) 96 return retval; 97 98 /* Allocate and set up 1 RX queue per Ethernet port. */ 99 for (q = 0; q < rx_rings; q++) { 100 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE, 101 rte_eth_dev_socket_id(port), NULL, mbuf_pool); 102 if (retval < 0) 103 return retval; 104 } 105 106 /* Allocate and set up 1 TX queue per Ethernet port. */ 107 for (q = 0; q < tx_rings; q++) { 108 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE, 109 rte_eth_dev_socket_id(port), NULL); 110 if (retval < 0) 111 return retval; 112 } 113 114 /* Start the Ethernet port. */ 115 retval = rte_eth_dev_start(port); 116 if (retval < 0) 117 return retval; 118 119 /* Enable RX in promiscuous mode for the Ethernet device. */ 120 retval = rte_eth_promiscuous_enable(port); 121 if (retval != 0) 122 return retval; 123 124 /* Add the callbacks for RX and TX.*/ 125 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL); 126 rte_eth_add_tx_callback(port, 0, calc_latency, NULL); 127 128 return 0; 129 } 130 131 132The RX and TX callbacks are added to the ports/queues as function pointers: 133 134.. code-block:: c 135 136 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL); 137 rte_eth_add_tx_callback(port, 0, calc_latency, NULL); 138 139More than one callback can be added and additional information can be passed 140to callback function pointers as a ``void*``. In the examples above ``NULL`` 141is used. 142 143The ``add_timestamps()`` and ``calc_latency()`` functions are explained below. 144 145 146The add_timestamps() Callback 147~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 148 149The ``add_timestamps()`` callback is added to the RX port and is applied to 150all packets received: 151 152.. code-block:: c 153 154 static uint16_t 155 add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused, 156 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused) 157 { 158 unsigned i; 159 uint64_t now = rte_rdtsc(); 160 161 for (i = 0; i < nb_pkts; i++) 162 pkts[i]->udata64 = now; 163 164 return nb_pkts; 165 } 166 167The DPDK function ``rte_rdtsc()`` is used to add a cycle count timestamp to 168each packet (see the *cycles* section of the *DPDK API Documentation* for 169details). 170 171 172The calc_latency() Callback 173~~~~~~~~~~~~~~~~~~~~~~~~~~~ 174 175The ``calc_latency()`` callback is added to the TX port and is applied to all 176packets prior to transmission: 177 178.. code-block:: c 179 180 static uint16_t 181 calc_latency(uint16_t port __rte_unused, uint16_t qidx __rte_unused, 182 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused) 183 { 184 uint64_t cycles = 0; 185 uint64_t now = rte_rdtsc(); 186 unsigned i; 187 188 for (i = 0; i < nb_pkts; i++) 189 cycles += now - pkts[i]->udata64; 190 191 latency_numbers.total_cycles += cycles; 192 latency_numbers.total_pkts += nb_pkts; 193 194 if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) { 195 printf("Latency = %"PRIu64" cycles\n", 196 latency_numbers.total_cycles / latency_numbers.total_pkts); 197 198 latency_numbers.total_cycles = latency_numbers.total_pkts = 0; 199 } 200 201 return nb_pkts; 202 } 203 204The ``calc_latency()`` function accumulates the total number of packets and 205the total number of cycles used. Once more than 100 million packets have been 206transmitted the average cycle count per packet is printed out and the counters 207are reset. 208