doc: use code snippets in sample app guides
[dpdk.git] / doc / guides / sample_app_ug / ptpclient.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2015 Intel Corporation.
3
4 PTP Client Sample Application
5 =============================
6
7 The PTP (Precision Time Protocol) client sample application is a simple
8 example of using the DPDK IEEE1588 API to communicate with a PTP master clock
9 to synchronize the time on the NIC and, optionally, on the Linux system.
10
11 Note, PTP is a time syncing protocol and cannot be used within DPDK as a
12 time-stamping mechanism. See the following for an explanation of the protocol:
13 `Precision Time Protocol
14 <https://en.wikipedia.org/wiki/Precision_Time_Protocol>`_.
15
16
17 Limitations
18 -----------
19
20 The PTP sample application is intended as a simple reference implementation of
21 a PTP client using the DPDK IEEE1588 API.
22 In order to keep the application simple the following assumptions are made:
23
24 * The first discovered master is the main for the session.
25 * Only L2 PTP packets are supported.
26 * Only the PTP v2 protocol is supported.
27 * Only the slave clock is implemented.
28
29
30 How the Application Works
31 -------------------------
32
33 .. _figure_ptpclient_highlevel:
34
35 .. figure:: img/ptpclient.*
36
37    PTP Synchronization Protocol
38
39 The PTP synchronization in the sample application works as follows:
40
41 * Master sends *Sync* message - the slave saves it as T2.
42 * Master sends *Follow Up* message and sends time of T1.
43 * Slave sends *Delay Request* frame to PTP Master and stores T3.
44 * Master sends *Delay Response* T4 time which is time of received T3.
45
46 The adjustment for slave can be represented as:
47
48    adj = -[(T2-T1)-(T4 - T3)]/2
49
50 If the command line parameter ``-T 1`` is used the application also
51 synchronizes the PTP PHC clock with the Linux kernel clock.
52
53 Compiling the Application
54 -------------------------
55
56 To compile the sample application see :doc:`compiling`.
57
58 The application is located in the ``ptpclient`` sub-directory.
59
60
61 Running the Application
62 -----------------------
63
64 To run the example in a ``linux`` environment:
65
66 .. code-block:: console
67
68     ./<build_dir>/examples/dpdk-ptpclient -l 1 -n 4 -- -p 0x1 -T 0
69
70 Refer to *DPDK Getting Started Guide* for general information on running
71 applications and the Environment Abstraction Layer (EAL) options.
72
73 * ``-p portmask``: Hexadecimal portmask.
74 * ``-T 0``: Update only the PTP slave clock.
75 * ``-T 1``: Update the PTP slave clock and synchronize the Linux Kernel to the PTP clock.
76
77
78 Code Explanation
79 ----------------
80
81 The following sections provide an explanation of the main components of the
82 code.
83
84 All DPDK library functions used in the sample code are prefixed with ``rte_``
85 and are explained in detail in the *DPDK API Documentation*.
86
87
88 The Main Function
89 ~~~~~~~~~~~~~~~~~
90
91 The ``main()`` function performs the initialization and calls the execution
92 threads for each lcore.
93
94 The first task is to initialize the Environment Abstraction Layer (EAL).  The
95 ``argc`` and ``argv`` arguments are provided to the ``rte_eal_init()``
96 function. The value returned is the number of parsed arguments:
97
98 .. literalinclude:: ../../../examples/ptpclient/ptpclient.c
99     :language: c
100     :start-after: Initialize the Environment Abstraction Layer (EAL). 8<
101     :end-before: >8 End of initialization of EAL.
102     :dedent: 1
103
104 And than we parse application specific arguments
105
106 .. literalinclude:: ../../../examples/ptpclient/ptpclient.c
107     :language: c
108     :start-after: Parse specific arguments. 8<
109     :end-before: >8 End of parsing specific arguments.
110     :dedent: 1
111
112 The ``main()`` also allocates a mempool to hold the mbufs (Message Buffers)
113 used by the application:
114
115 .. literalinclude:: ../../../examples/ptpclient/ptpclient.c
116     :language: c
117     :start-after: Creates a new mempool in memory to hold the mbufs. 8<
118     :end-before:  >8 End of a new mempool in memory to hold the mbufs.
119     :dedent: 1
120
121 Mbufs are the packet buffer structure used by DPDK. They are explained in
122 detail in the "Mbuf Library" section of the *DPDK Programmer's Guide*.
123
124 The ``main()`` function also initializes all the ports using the user defined
125 ``port_init()`` function with portmask provided by user:
126
127 .. literalinclude:: ../../../examples/ptpclient/ptpclient.c
128     :language: c
129     :start-after: Initialize all ports. 8<
130     :end-before: >8 End of initialization of all ports.
131     :dedent: 1
132
133
134 Once the initialization is complete, the application is ready to launch a
135 function on an lcore. In this example ``lcore_main()`` is called on a single
136 lcore.
137
138 .. code-block:: c
139
140         lcore_main();
141
142 The ``lcore_main()`` function is explained below.
143
144
145 The Lcores Main
146 ~~~~~~~~~~~~~~~
147
148 As we saw above the ``main()`` function calls an application function on the
149 available lcores.
150
151 The main work of the application is done within the loop:
152
153 .. literalinclude:: ../../../examples/ptpclient/ptpclient.c
154     :language: c
155     :start-after: Read packet from RX queues. 8<
156     :end-before: >8 End of read packets from RX queues.
157     :dedent: 2
158
159 Packets are received one by one on the RX ports and, if required, PTP response
160 packets are transmitted on the TX ports.
161
162 If the offload flags in the mbuf indicate that the packet is a PTP packet then
163 the packet is parsed to determine which type:
164
165 .. literalinclude:: ../../../examples/ptpclient/ptpclient.c
166     :language: c
167     :start-after: Packet is parsed to determine which type. 8<
168     :end-before: >8 End of packet is parsed to determine which type.
169     :dedent: 3
170
171
172 All packets are freed explicitly using ``rte_pktmbuf_free()``.
173
174 The forwarding loop can be interrupted and the application closed using
175 ``Ctrl-C``.
176
177
178 PTP parsing
179 ~~~~~~~~~~~
180
181 The ``parse_ptp_frames()`` function processes PTP packets, implementing slave
182 PTP IEEE1588 L2 functionality.
183
184 .. literalinclude:: ../../../examples/ptpclient/ptpclient.c
185     :language: c
186     :start-after: Parse ptp frames. 8<
187     :end-before:  >8 End of function processes PTP packets.
188
189 There are 3 types of packets on the RX path which we must parse to create a minimal
190 implementation of the PTP slave client:
191
192 * SYNC packet.
193 * FOLLOW UP packet
194 * DELAY RESPONSE packet.
195
196 When we parse the *FOLLOW UP* packet we also create and send a *DELAY_REQUEST* packet.
197 Also when we parse the *DELAY RESPONSE* packet, and all conditions are met we adjust the PTP slave clock.