f06c5c866a43c3a94eab4ff98feea02b17aed171
[dpdk.git] / doc / guides / sample_app_ug / rxtx_callbacks.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2015 Intel Corporation.
3
4 RX/TX Callbacks Sample Application
5 ==================================
6
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.
11
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.
15
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
19 callback.
20
21 Compiling the Application
22 -------------------------
23
24 To compile the sample application see :doc:`compiling`.
25
26 The application is located in the ``rxtx_callbacks`` sub-directory.
27
28
29 Running the Application
30 -----------------------
31
32 To 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
38 Use -t to enable hardware timestamping. If not supported by the NIC, an error
39 will be displayed.
40
41 Refer to *DPDK Getting Started Guide* for general information on running
42 applications and the Environment Abstraction Layer (EAL) options.
43
44
45
46 Explanation
47 -----------
48
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.
52
53 The sections below explain the additional RX/TX callback code.
54
55
56 The Main Function
57 ~~~~~~~~~~~~~~~~~
58
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`.
62
63 The ``lcore_main()`` function is also identical.
64
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:
67
68
69 The Port Initialization  Function
70 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71
72 The main functional part of the port initialization is shown below with
73 comments:
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
81 The 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
89 More than one callback can be added and additional information can be passed
90 to callback function pointers as a ``void*``. In the examples above ``NULL``
91 is used.
92
93 The ``add_timestamps()`` and ``calc_latency()`` functions are explained below.
94
95
96 The add_timestamps() Callback
97 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98
99 The ``add_timestamps()`` callback is added to the RX port and is applied to
100 all 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
107 The DPDK function ``rte_rdtsc()`` is used to add a cycle count timestamp to
108 each packet (see the *cycles* section of the *DPDK API Documentation* for
109 details).
110
111
112 The calc_latency() Callback
113 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
114
115 The ``calc_latency()`` callback is added to the TX port and is applied to all
116 packets 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
123 The ``calc_latency()`` function accumulates the total number of packets and
124 the total number of cycles used. Once more than 100 million packets have been
125 transmitted the average cycle count per packet is printed out and the counters
126 are reset.