diff options
author | Willem de Bruijn <willemb@google.com> | 2014-08-31 21:27:47 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2014-09-02 00:49:08 -0400 |
commit | 8fe2f761cae9da9f9031162f104164a812ce78ab (patch) | |
tree | 4a432cb062a036ec6bcbb74f09662f7cddcefe0a /Documentation/networking/timestamping.txt | |
parent | c5a65680b3c29ddf8f4a0bfb4ba75ea230735a38 (diff) |
net-timestamp: expand documentation
Expand Documentation/networking/timestamping.txt with new
interfaces and bytestream timestamping. Also minor
cleanup of the other text.
Import txtimestamp.c test of the new features.
Signed-off-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'Documentation/networking/timestamping.txt')
-rw-r--r-- | Documentation/networking/timestamping.txt | 368 |
1 files changed, 286 insertions, 82 deletions
diff --git a/Documentation/networking/timestamping.txt b/Documentation/networking/timestamping.txt index 897f942b976b..412f45ca2d73 100644 --- a/Documentation/networking/timestamping.txt +++ b/Documentation/networking/timestamping.txt | |||
@@ -1,102 +1,307 @@ | |||
1 | The existing interfaces for getting network packages time stamped are: | 1 | |
2 | 1. Control Interfaces | ||
3 | |||
4 | The interfaces for receiving network packages timestamps are: | ||
2 | 5 | ||
3 | * SO_TIMESTAMP | 6 | * SO_TIMESTAMP |
4 | Generate time stamp for each incoming packet using the (not necessarily | 7 | Generates a timestamp for each incoming packet in (not necessarily |
5 | monotonous!) system time. Result is returned via recv_msg() in a | 8 | monotonic) system time. Reports the timestamp via recvmsg() in a |
6 | control message as timeval (usec resolution). | 9 | control message as struct timeval (usec resolution). |
7 | 10 | ||
8 | * SO_TIMESTAMPNS | 11 | * SO_TIMESTAMPNS |
9 | Same time stamping mechanism as SO_TIMESTAMP, but returns result as | 12 | Same timestamping mechanism as SO_TIMESTAMP, but reports the |
10 | timespec (nsec resolution). | 13 | timestamp as struct timespec (nsec resolution). |
11 | 14 | ||
12 | * IP_MULTICAST_LOOP + SO_TIMESTAMP[NS] | 15 | * IP_MULTICAST_LOOP + SO_TIMESTAMP[NS] |
13 | Only for multicasts: approximate send time stamp by receiving the looped | 16 | Only for multicast:approximate transmit timestamp obtained by |
14 | packet and using its receive time stamp. | 17 | reading the looped packet receive timestamp. |
15 | 18 | ||
16 | The following interface complements the existing ones: receive time | 19 | * SO_TIMESTAMPING |
17 | stamps can be generated and returned for arbitrary packets and much | 20 | Generates timestamps on reception, transmission or both. Supports |
18 | closer to the point where the packet is really sent. Time stamps can | 21 | multiple timestamp sources, including hardware. Supports generating |
19 | be generated in software (as before) or in hardware (if the hardware | 22 | timestamps for stream sockets. |
20 | has such a feature). | ||
21 | 23 | ||
22 | SO_TIMESTAMPING: | ||
23 | 24 | ||
24 | Instructs the socket layer which kind of information should be collected | 25 | 1.1 SO_TIMESTAMP: |
25 | and/or reported. The parameter is an integer with some of the following | ||
26 | bits set. Setting other bits is an error and doesn't change the current | ||
27 | state. | ||
28 | 26 | ||
29 | Four of the bits are requests to the stack to try to generate | 27 | This socket option enables timestamping of datagrams on the reception |
30 | timestamps. Any combination of them is valid. | 28 | path. Because the destination socket, if any, is not known early in |
29 | the network stack, the feature has to be enabled for all packets. The | ||
30 | same is true for all early receive timestamp options. | ||
31 | 31 | ||
32 | SOF_TIMESTAMPING_TX_HARDWARE: try to obtain send time stamps in hardware | 32 | For interface details, see `man 7 socket`. |
33 | SOF_TIMESTAMPING_TX_SOFTWARE: try to obtain send time stamps in software | 33 | |
34 | SOF_TIMESTAMPING_RX_HARDWARE: try to obtain receive time stamps in hardware | 34 | |
35 | SOF_TIMESTAMPING_RX_SOFTWARE: try to obtain receive time stamps in software | 35 | 1.2 SO_TIMESTAMPNS: |
36 | |||
37 | This option is identical to SO_TIMESTAMP except for the returned data type. | ||
38 | Its struct timespec allows for higher resolution (ns) timestamps than the | ||
39 | timeval of SO_TIMESTAMP (ms). | ||
40 | |||
41 | |||
42 | 1.3 SO_TIMESTAMPING: | ||
43 | |||
44 | Supports multiple types of timestamp requests. As a result, this | ||
45 | socket option takes a bitmap of flags, not a boolean. In | ||
46 | |||
47 | err = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, (void *) val, &val); | ||
48 | |||
49 | val is an integer with any of the following bits set. Setting other | ||
50 | bit returns EINVAL and does not change the current state. | ||
36 | 51 | ||
37 | The other three bits control which timestamps will be reported in a | ||
38 | generated control message. If none of these bits are set or if none of | ||
39 | the set bits correspond to data that is available, then the control | ||
40 | message will not be generated: | ||
41 | 52 | ||
42 | SOF_TIMESTAMPING_SOFTWARE: report systime if available | 53 | 1.3.1 Timestamp Generation |
43 | SOF_TIMESTAMPING_SYS_HARDWARE: report hwtimetrans if available (deprecated) | ||
44 | SOF_TIMESTAMPING_RAW_HARDWARE: report hwtimeraw if available | ||
45 | 54 | ||
46 | It is worth noting that timestamps may be collected for reasons other | 55 | Some bits are requests to the stack to try to generate timestamps. Any |
47 | than being requested by a particular socket with | 56 | combination of them is valid. Changes to these bits apply to newly |
48 | SOF_TIMESTAMPING_[TR]X_(HARD|SOFT)WARE. For example, most drivers that | 57 | created packets, not to packets already in the stack. As a result, it |
49 | can generate hardware receive timestamps ignore | 58 | is possible to selectively request timestamps for a subset of packets |
50 | SOF_TIMESTAMPING_RX_HARDWARE. It is still a good idea to set that flag | 59 | (e.g., for sampling) by embedding an send() call within two setsockopt |
51 | in case future drivers pay attention. | 60 | calls, one to enable timestamp generation and one to disable it. |
61 | Timestamps may also be generated for reasons other than being | ||
62 | requested by a particular socket, such as when receive timestamping is | ||
63 | enabled system wide, as explained earlier. | ||
52 | 64 | ||
53 | If timestamps are reported, they will appear in a control message with | 65 | SOF_TIMESTAMPING_RX_HARDWARE: |
54 | cmsg_level==SOL_SOCKET, cmsg_type==SO_TIMESTAMPING, and a payload like | 66 | Request rx timestamps generated by the network adapter. |
55 | this: | 67 | |
68 | SOF_TIMESTAMPING_RX_SOFTWARE: | ||
69 | Request rx timestamps when data enters the kernel. These timestamps | ||
70 | are generated just after a device driver hands a packet to the | ||
71 | kernel receive stack. | ||
72 | |||
73 | SOF_TIMESTAMPING_TX_HARDWARE: | ||
74 | Request tx timestamps generated by the network adapter. | ||
75 | |||
76 | SOF_TIMESTAMPING_TX_SOFTWARE: | ||
77 | Request tx timestamps when data leaves the kernel. These timestamps | ||
78 | are generated in the device driver as close as possible, but always | ||
79 | prior to, passing the packet to the network interface. Hence, they | ||
80 | require driver support and may not be available for all devices. | ||
81 | |||
82 | SOF_TIMESTAMPING_TX_SCHED: | ||
83 | Request tx timestamps prior to entering the packet scheduler. Kernel | ||
84 | transmit latency is, if long, often dominated by queuing delay. The | ||
85 | difference between this timestamp and one taken at | ||
86 | SOF_TIMESTAMPING_TX_SOFTWARE will expose this latency independent | ||
87 | of protocol processing. The latency incurred in protocol | ||
88 | processing, if any, can be computed by subtracting a userspace | ||
89 | timestamp taken immediately before send() from this timestamp. On | ||
90 | machines with virtual devices where a transmitted packet travels | ||
91 | through multiple devices and, hence, multiple packet schedulers, | ||
92 | a timestamp is generated at each layer. This allows for fine | ||
93 | grained measurement of queuing delay. | ||
94 | |||
95 | SOF_TIMESTAMPING_TX_ACK: | ||
96 | Request tx timestamps when all data in the send buffer has been | ||
97 | acknowledged. This only makes sense for reliable protocols. It is | ||
98 | currently only implemented for TCP. For that protocol, it may | ||
99 | over-report measurement, because the timestamp is generated when all | ||
100 | data up to and including the buffer at send() was acknowledged: the | ||
101 | cumulative acknowledgment. The mechanism ignores SACK and FACK. | ||
102 | |||
103 | |||
104 | 1.3.2 Timestamp Reporting | ||
105 | |||
106 | The other three bits control which timestamps will be reported in a | ||
107 | generated control message. Changes to the bits take immediate | ||
108 | effect at the timestamp reporting locations in the stack. Timestamps | ||
109 | are only reported for packets that also have the relevant timestamp | ||
110 | generation request set. | ||
111 | |||
112 | SOF_TIMESTAMPING_SOFTWARE: | ||
113 | Report any software timestamps when available. | ||
114 | |||
115 | SOF_TIMESTAMPING_SYS_HARDWARE: | ||
116 | This option is deprecated and ignored. | ||
117 | |||
118 | SOF_TIMESTAMPING_RAW_HARDWARE: | ||
119 | Report hardware timestamps as generated by | ||
120 | SOF_TIMESTAMPING_TX_HARDWARE when available. | ||
121 | |||
122 | |||
123 | 1.3.3 Timestamp Options | ||
124 | |||
125 | The interface supports one option | ||
126 | |||
127 | SOF_TIMESTAMPING_OPT_ID: | ||
128 | |||
129 | Generate a unique identifier along with each packet. A process can | ||
130 | have multiple concurrent timestamping requests outstanding. Packets | ||
131 | can be reordered in the transmit path, for instance in the packet | ||
132 | scheduler. In that case timestamps will be queued onto the error | ||
133 | queue out of order from the original send() calls. This option | ||
134 | embeds a counter that is incremented at send() time, to order | ||
135 | timestamps within a flow. | ||
136 | |||
137 | This option is implemented only for transmit timestamps. There, the | ||
138 | timestamp is always looped along with a struct sock_extended_err. | ||
139 | The option modifies field ee_info to pass an id that is unique | ||
140 | among all possibly concurrently outstanding timestamp requests for | ||
141 | that socket. In practice, it is a monotonically increasing u32 | ||
142 | (that wraps). | ||
143 | |||
144 | In datagram sockets, the counter increments on each send call. In | ||
145 | stream sockets, it increments with every byte. | ||
146 | |||
147 | |||
148 | 1.4 Bytestream Timestamps | ||
149 | |||
150 | The SO_TIMESTAMPING interface supports timestamping of bytes in a | ||
151 | bytestream. Each request is interpreted as a request for when the | ||
152 | entire contents of the buffer has passed a timestamping point. That | ||
153 | is, for streams option SOF_TIMESTAMPING_TX_SOFTWARE will record | ||
154 | when all bytes have reached the device driver, regardless of how | ||
155 | many packets the data has been converted into. | ||
156 | |||
157 | In general, bytestreams have no natural delimiters and therefore | ||
158 | correlating a timestamp with data is non-trivial. A range of bytes | ||
159 | may be split across segments, any segments may be merged (possibly | ||
160 | coalescing sections of previously segmented buffers associated with | ||
161 | independent send() calls). Segments can be reordered and the same | ||
162 | byte range can coexist in multiple segments for protocols that | ||
163 | implement retransmissions. | ||
164 | |||
165 | It is essential that all timestamps implement the same semantics, | ||
166 | regardless of these possible transformations, as otherwise they are | ||
167 | incomparable. Handling "rare" corner cases differently from the | ||
168 | simple case (a 1:1 mapping from buffer to skb) is insufficient | ||
169 | because performance debugging often needs to focus on such outliers. | ||
170 | |||
171 | In practice, timestamps can be correlated with segments of a | ||
172 | bytestream consistently, if both semantics of the timestamp and the | ||
173 | timing of measurement are chosen correctly. This challenge is no | ||
174 | different from deciding on a strategy for IP fragmentation. There, the | ||
175 | definition is that only the first fragment is timestamped. For | ||
176 | bytestreams, we chose that a timestamp is generated only when all | ||
177 | bytes have passed a point. SOF_TIMESTAMPING_TX_ACK as defined is easy to | ||
178 | implement and reason about. An implementation that has to take into | ||
179 | account SACK would be more complex due to possible transmission holes | ||
180 | and out of order arrival. | ||
181 | |||
182 | On the host, TCP can also break the simple 1:1 mapping from buffer to | ||
183 | skbuff as a result of Nagle, cork, autocork, segmentation and GSO. The | ||
184 | implementation ensures correctness in all cases by tracking the | ||
185 | individual last byte passed to send(), even if it is no longer the | ||
186 | last byte after an skbuff extend or merge operation. It stores the | ||
187 | relevant sequence number in skb_shinfo(skb)->tskey. Because an skbuff | ||
188 | has only one such field, only one timestamp can be generated. | ||
189 | |||
190 | In rare cases, a timestamp request can be missed if two requests are | ||
191 | collapsed onto the same skb. A process can detect this situation by | ||
192 | enabling SOF_TIMESTAMPING_OPT_ID and comparing the byte offset at | ||
193 | send time with the value returned for each timestamp. It can prevent | ||
194 | the situation by always flushing the TCP stack in between requests, | ||
195 | for instance by enabling TCP_NODELAY and disabling TCP_CORK and | ||
196 | autocork. | ||
197 | |||
198 | These precautions ensure that the timestamp is generated only when all | ||
199 | bytes have passed a timestamp point, assuming that the network stack | ||
200 | itself does not reorder the segments. The stack indeed tries to avoid | ||
201 | reordering. The one exception is under administrator control: it is | ||
202 | possible to construct a packet scheduler configuration that delays | ||
203 | segments from the same stream differently. Such a setup would be | ||
204 | unusual. | ||
205 | |||
206 | |||
207 | 2 Data Interfaces | ||
208 | |||
209 | Timestamps are read using the ancillary data feature of recvmsg(). | ||
210 | See `man 3 cmsg` for details of this interface. The socket manual | ||
211 | page (`man 7 socket`) describes how timestamps generated with | ||
212 | SO_TIMESTAMP and SO_TIMESTAMPNS records can be retrieved. | ||
213 | |||
214 | |||
215 | 2.1 SCM_TIMESTAMPING records | ||
216 | |||
217 | These timestamps are returned in a control message with cmsg_level | ||
218 | SOL_SOCKET, cmsg_type SCM_TIMESTAMPING, and payload of type | ||
56 | 219 | ||
57 | struct scm_timestamping { | 220 | struct scm_timestamping { |
58 | struct timespec systime; | 221 | struct timespec ts[3]; |
59 | struct timespec hwtimetrans; | ||
60 | struct timespec hwtimeraw; | ||
61 | }; | 222 | }; |
62 | 223 | ||
63 | recvmsg() can be used to get this control message for regular incoming | 224 | The structure can return up to three timestamps. This is a legacy |
64 | packets. For send time stamps the outgoing packet is looped back to | 225 | feature. Only one field is non-zero at any time. Most timestamps |
65 | the socket's error queue with the send time stamp(s) attached. It can | 226 | are passed in ts[0]. Hardware timestamps are passed in ts[2]. |
66 | be received with recvmsg(flags=MSG_ERRQUEUE). The call returns the | 227 | |
67 | original outgoing packet data including all headers preprended down to | 228 | ts[1] used to hold hardware timestamps converted to system time. |
68 | and including the link layer, the scm_timestamping control message and | 229 | Instead, expose the hardware clock device on the NIC directly as |
69 | a sock_extended_err control message with ee_errno==ENOMSG and | 230 | a HW PTP clock source, to allow time conversion in userspace and |
70 | ee_origin==SO_EE_ORIGIN_TIMESTAMPING. A socket with such a pending | 231 | optionally synchronize system time with a userspace PTP stack such |
71 | bounced packet is ready for reading as far as select() is concerned. | 232 | as linuxptp. For the PTP clock API, see Documentation/ptp/ptp.txt. |
72 | If the outgoing packet has to be fragmented, then only the first | 233 | |
73 | fragment is time stamped and returned to the sending socket. | 234 | 2.1.1 Transmit timestamps with MSG_ERRQUEUE |
74 | 235 | ||
75 | All three values correspond to the same event in time, but were | 236 | For transmit timestamps the outgoing packet is looped back to the |
76 | generated in different ways. Each of these values may be empty (= all | 237 | socket's error queue with the send timestamp(s) attached. A process |
77 | zero), in which case no such value was available. If the application | 238 | receives the timestamps by calling recvmsg() with flag MSG_ERRQUEUE |
78 | is not interested in some of these values, they can be left blank to | 239 | set and with a msg_control buffer sufficiently large to receive the |
79 | avoid the potential overhead of calculating them. | 240 | relevant metadata structures. The recvmsg call returns the original |
80 | 241 | outgoing data packet with two ancillary messages attached. | |
81 | systime is the value of the system time at that moment. This | 242 | |
82 | corresponds to the value also returned via SO_TIMESTAMP[NS]. If the | 243 | A message of cm_level SOL_IP(V6) and cm_type IP(V6)_RECVERR |
83 | time stamp was generated by hardware, then this field is | 244 | embeds a struct sock_extended_err. This defines the error type. For |
84 | empty. Otherwise it is filled in if SOF_TIMESTAMPING_SOFTWARE is | 245 | timestamps, the ee_errno field is ENOMSG. The other ancillary message |
85 | set. | 246 | will have cm_level SOL_SOCKET and cm_type SCM_TIMESTAMPING. This |
86 | 247 | embeds the struct scm_timestamping. | |
87 | hwtimeraw is the original hardware time stamp. Filled in if | 248 | |
88 | SOF_TIMESTAMPING_RAW_HARDWARE is set. No assumptions about its | 249 | |
89 | relation to system time should be made. | 250 | 2.1.1.2 Timestamp types |
90 | 251 | ||
91 | hwtimetrans is always zero. This field is deprecated. It used to hold | 252 | The semantics of the three struct timespec are defined by field |
92 | hw timestamps converted to system time. Instead, expose the hardware | 253 | ee_info in the extended error structure. It contains a value of |
93 | clock device on the NIC directly as a HW PTP clock source, to allow | 254 | type SCM_TSTAMP_* to define the actual timestamp passed in |
94 | time conversion in userspace and optionally synchronize system time | 255 | scm_timestamping. |
95 | with a userspace PTP stack such as linuxptp. For the PTP clock API, | 256 | |
96 | see Documentation/ptp/ptp.txt. | 257 | The SCM_TSTAMP_* types are 1:1 matches to the SOF_TIMESTAMPING_* |
97 | 258 | control fields discussed previously, with one exception. For legacy | |
98 | 259 | reasons, SCM_TSTAMP_SND is equal to zero and can be set for both | |
99 | SIOCSHWTSTAMP, SIOCGHWTSTAMP: | 260 | SOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_TX_SOFTWARE. It |
261 | is the first if ts[2] is non-zero, the second otherwise, in which | ||
262 | case the timestamp is stored in ts[0]. | ||
263 | |||
264 | |||
265 | 2.1.1.3 Fragmentation | ||
266 | |||
267 | Fragmentation of outgoing datagrams is rare, but is possible, e.g., by | ||
268 | explicitly disabling PMTU discovery. If an outgoing packet is fragmented, | ||
269 | then only the first fragment is timestamped and returned to the sending | ||
270 | socket. | ||
271 | |||
272 | |||
273 | 2.1.1.4 Packet Payload | ||
274 | |||
275 | The calling application is often not interested in receiving the whole | ||
276 | packet payload that it passed to the stack originally: the socket | ||
277 | error queue mechanism is just a method to piggyback the timestamp on. | ||
278 | In this case, the application can choose to read datagrams with a | ||
279 | smaller buffer, possibly even of length 0. The payload is truncated | ||
280 | accordingly. Until the process calls recvmsg() on the error queue, | ||
281 | however, the full packet is queued, taking up budget from SO_RCVBUF. | ||
282 | |||
283 | |||
284 | 2.1.1.5 Blocking Read | ||
285 | |||
286 | Reading from the error queue is always a non-blocking operation. To | ||
287 | block waiting on a timestamp, use poll or select. poll() will return | ||
288 | POLLERR in pollfd.revents if any data is ready on the error queue. | ||
289 | There is no need to pass this flag in pollfd.events. This flag is | ||
290 | ignored on request. See also `man 2 poll`. | ||
291 | |||
292 | |||
293 | 2.1.2 Receive timestamps | ||
294 | |||
295 | On reception, there is no reason to read from the socket error queue. | ||
296 | The SCM_TIMESTAMPING ancillary data is sent along with the packet data | ||
297 | on a normal recvmsg(). Since this is not a socket error, it is not | ||
298 | accompanied by a message SOL_IP(V6)/IP(V6)_RECVERROR. In this case, | ||
299 | the meaning of the three fields in struct scm_timestamping is | ||
300 | implicitly defined. ts[0] holds a software timestamp if set, ts[1] | ||
301 | is again deprecated and ts[2] holds a hardware timestamp if set. | ||
302 | |||
303 | |||
304 | 3. Hardware Timestamping configuration: SIOCSHWTSTAMP and SIOCGHWTSTAMP | ||
100 | 305 | ||
101 | Hardware time stamping must also be initialized for each device driver | 306 | Hardware time stamping must also be initialized for each device driver |
102 | that is expected to do hardware time stamping. The parameter is defined in | 307 | that is expected to do hardware time stamping. The parameter is defined in |
@@ -167,8 +372,7 @@ enum { | |||
167 | */ | 372 | */ |
168 | }; | 373 | }; |
169 | 374 | ||
170 | 375 | 3.1 Hardware Timestamping Implementation: Device Drivers | |
171 | DEVICE IMPLEMENTATION | ||
172 | 376 | ||
173 | A driver which supports hardware time stamping must support the | 377 | A driver which supports hardware time stamping must support the |
174 | SIOCSHWTSTAMP ioctl and update the supplied struct hwtstamp_config with | 378 | SIOCSHWTSTAMP ioctl and update the supplied struct hwtstamp_config with |