1 .. SPDX-License-Identifier: BSD-3-Clause
2 Copyright(c) 2015 Intel Corporation.
4 RX/TX Callbacks Sample Application
5 ==================================
7 The RX/TX Callbacks sample application is a packet forwarding application that
8 demonstrates the use of user defined callbacks on received and transmitted
9 packets. The application performs a simple latency check, using callbacks, to
10 determine the time packets spend within the application.
12 In the sample application a user defined callback is applied to all received
13 packets to add a timestamp. A separate callback is applied to all packets
14 prior to transmission to calculate the elapsed time, in CPU cycles.
16 If hardware timestamping is supported by the NIC, the sample application will
17 also display the average latency since the packet was timestamped in hardware,
18 on top of the latency since the packet was received and processed by the RX
21 Compiling the Application
22 -------------------------
24 To compile the sample application see :doc:`compiling`.
26 The application is located in the ``rxtx_callbacks`` sub-directory.
29 Running the Application
30 -----------------------
32 To run the example in a ``linux`` environment:
34 .. code-block:: console
36 ./<build_dir>/examples/dpdk-rxtx_callbacks -l 1 -n 4 -- [-t]
38 Use -t to enable hardware timestamping. If not supported by the NIC, an error
41 Refer to *DPDK Getting Started Guide* for general information on running
42 applications and the Environment Abstraction Layer (EAL) options.
49 The ``rxtx_callbacks`` application is mainly a simple forwarding application
50 based on the :doc:`skeleton`. See that section of the documentation for more
51 details of the forwarding part of the application.
53 The sections below explain the additional RX/TX callback code.
59 The ``main()`` function performs the application initialization and calls the
60 execution threads for each lcore. This function is effectively identical to
61 the ``main()`` function explained in :doc:`skeleton`.
63 The ``lcore_main()`` function is also identical.
65 The main difference is in the user defined ``port_init()`` function where the
66 callbacks are added. This is explained in the next section:
69 The Port Initialization Function
70 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72 The main functional part of the port initialization is shown below with
78 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
80 struct rte_eth_conf port_conf = port_conf_default;
81 const uint16_t rx_rings = 1, tx_rings = 1;
82 struct rte_ether_addr addr;
86 /* Configure the Ethernet device. */
87 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
91 /* Allocate and set up 1 RX queue per Ethernet port. */
92 for (q = 0; q < rx_rings; q++) {
93 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
94 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
99 /* Allocate and set up 1 TX queue per Ethernet port. */
100 for (q = 0; q < tx_rings; q++) {
101 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
102 rte_eth_dev_socket_id(port), NULL);
107 /* Start the Ethernet port. */
108 retval = rte_eth_dev_start(port);
112 /* Enable RX in promiscuous mode for the Ethernet device. */
113 retval = rte_eth_promiscuous_enable(port);
117 /* Add the callbacks for RX and TX.*/
118 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
119 rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
125 The RX and TX callbacks are added to the ports/queues as function pointers:
129 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
130 rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
132 More than one callback can be added and additional information can be passed
133 to callback function pointers as a ``void*``. In the examples above ``NULL``
136 The ``add_timestamps()`` and ``calc_latency()`` functions are explained below.
139 The add_timestamps() Callback
140 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142 The ``add_timestamps()`` callback is added to the RX port and is applied to
143 all packets received:
148 add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
149 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
152 uint64_t now = rte_rdtsc();
154 for (i = 0; i < nb_pkts; i++)
155 *tsc_field(pkts[i]) = now;
160 The DPDK function ``rte_rdtsc()`` is used to add a cycle count timestamp to
161 each packet (see the *cycles* section of the *DPDK API Documentation* for
165 The calc_latency() Callback
166 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
168 The ``calc_latency()`` callback is added to the TX port and is applied to all
169 packets prior to transmission:
174 calc_latency(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
175 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
178 uint64_t now = rte_rdtsc();
181 for (i = 0; i < nb_pkts; i++)
182 cycles += now - *tsc_field(pkts[i]);
184 latency_numbers.total_cycles += cycles;
185 latency_numbers.total_pkts += nb_pkts;
187 if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) {
188 printf("Latency = %"PRIu64" cycles\n",
189 latency_numbers.total_cycles / latency_numbers.total_pkts);
191 latency_numbers.total_cycles = latency_numbers.total_pkts = 0;
197 The ``calc_latency()`` function accumulates the total number of packets and
198 the total number of cycles used. Once more than 100 million packets have been
199 transmitted the average cycle count per packet is printed out and the counters