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