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 28 29Running the Application 30----------------------- 31 32To run the example in a ``linux`` environment: 33 34.. code-block:: console 35 36 ./<build_dir>/examples/dpdk-rxtx_callbacks -l 1 -n 4 -- [-t] 37 38Use -t to enable hardware timestamping. If not supported by the NIC, an error 39will be displayed. 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.. literalinclude:: ../../../examples/rxtx_callbacks/main.c 76 :language: c 77 :start-after: Port initialization. 8< 78 :end-before: >8 End of port initialization. 79 80 81The RX and TX callbacks are added to the ports/queues as function pointers: 82 83.. literalinclude:: ../../../examples/rxtx_callbacks/main.c 84 :language: c 85 :start-after: RX and TX callbacks are added to the ports. 8< 86 :end-before: >8 End of RX and TX callbacks. 87 :dedent: 1 88 89More than one callback can be added and additional information can be passed 90to callback function pointers as a ``void*``. In the examples above ``NULL`` 91is used. 92 93The ``add_timestamps()`` and ``calc_latency()`` functions are explained below. 94 95 96The add_timestamps() Callback 97~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 98 99The ``add_timestamps()`` callback is added to the RX port and is applied to 100all packets received: 101 102.. literalinclude:: ../../../examples/rxtx_callbacks/main.c 103 :language: c 104 :start-after: Callback added to the RX port and applied to packets. 8< 105 :end-before: >8 End of callback addition and application. 106 107The DPDK function ``rte_rdtsc()`` is used to add a cycle count timestamp to 108each packet (see the *cycles* section of the *DPDK API Documentation* for 109details). 110 111 112The calc_latency() Callback 113~~~~~~~~~~~~~~~~~~~~~~~~~~~ 114 115The ``calc_latency()`` callback is added to the TX port and is applied to all 116packets prior to transmission: 117 118.. literalinclude:: ../../../examples/rxtx_callbacks/main.c 119 :language: c 120 :start-after: Callback is added to the TX port. 8< 121 :end-before: >8 End of callback addition. 122 123The ``calc_latency()`` function accumulates the total number of packets and 124the total number of cycles used. Once more than 100 million packets have been 125transmitted the average cycle count per packet is printed out and the counters 126are reset. 127