106bfa47eSMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0 206bfa47eSMauro Carvalho Chehab 306bfa47eSMauro Carvalho Chehab============ 406bfa47eSMauro Carvalho ChehabTimestamping 506bfa47eSMauro Carvalho Chehab============ 606bfa47eSMauro Carvalho Chehab 706bfa47eSMauro Carvalho Chehab 806bfa47eSMauro Carvalho Chehab1. Control Interfaces 906bfa47eSMauro Carvalho Chehab===================== 1006bfa47eSMauro Carvalho Chehab 1106bfa47eSMauro Carvalho ChehabThe interfaces for receiving network packages timestamps are: 1206bfa47eSMauro Carvalho Chehab 1306bfa47eSMauro Carvalho ChehabSO_TIMESTAMP 1406bfa47eSMauro Carvalho Chehab Generates a timestamp for each incoming packet in (not necessarily 1506bfa47eSMauro Carvalho Chehab monotonic) system time. Reports the timestamp via recvmsg() in a 1606bfa47eSMauro Carvalho Chehab control message in usec resolution. 1706bfa47eSMauro Carvalho Chehab SO_TIMESTAMP is defined as SO_TIMESTAMP_NEW or SO_TIMESTAMP_OLD 1806bfa47eSMauro Carvalho Chehab based on the architecture type and time_t representation of libc. 1906bfa47eSMauro Carvalho Chehab Control message format is in struct __kernel_old_timeval for 2006bfa47eSMauro Carvalho Chehab SO_TIMESTAMP_OLD and in struct __kernel_sock_timeval for 2106bfa47eSMauro Carvalho Chehab SO_TIMESTAMP_NEW options respectively. 2206bfa47eSMauro Carvalho Chehab 2306bfa47eSMauro Carvalho ChehabSO_TIMESTAMPNS 2406bfa47eSMauro Carvalho Chehab Same timestamping mechanism as SO_TIMESTAMP, but reports the 2506bfa47eSMauro Carvalho Chehab timestamp as struct timespec in nsec resolution. 2606bfa47eSMauro Carvalho Chehab SO_TIMESTAMPNS is defined as SO_TIMESTAMPNS_NEW or SO_TIMESTAMPNS_OLD 2706bfa47eSMauro Carvalho Chehab based on the architecture type and time_t representation of libc. 2806bfa47eSMauro Carvalho Chehab Control message format is in struct timespec for SO_TIMESTAMPNS_OLD 2906bfa47eSMauro Carvalho Chehab and in struct __kernel_timespec for SO_TIMESTAMPNS_NEW options 3006bfa47eSMauro Carvalho Chehab respectively. 3106bfa47eSMauro Carvalho Chehab 3206bfa47eSMauro Carvalho ChehabIP_MULTICAST_LOOP + SO_TIMESTAMP[NS] 3306bfa47eSMauro Carvalho Chehab Only for multicast:approximate transmit timestamp obtained by 3406bfa47eSMauro Carvalho Chehab reading the looped packet receive timestamp. 3506bfa47eSMauro Carvalho Chehab 3606bfa47eSMauro Carvalho ChehabSO_TIMESTAMPING 3706bfa47eSMauro Carvalho Chehab Generates timestamps on reception, transmission or both. Supports 3806bfa47eSMauro Carvalho Chehab multiple timestamp sources, including hardware. Supports generating 3906bfa47eSMauro Carvalho Chehab timestamps for stream sockets. 4006bfa47eSMauro Carvalho Chehab 4106bfa47eSMauro Carvalho Chehab 4206bfa47eSMauro Carvalho Chehab1.1 SO_TIMESTAMP (also SO_TIMESTAMP_OLD and SO_TIMESTAMP_NEW) 4306bfa47eSMauro Carvalho Chehab------------------------------------------------------------- 4406bfa47eSMauro Carvalho Chehab 4506bfa47eSMauro Carvalho ChehabThis socket option enables timestamping of datagrams on the reception 4606bfa47eSMauro Carvalho Chehabpath. Because the destination socket, if any, is not known early in 4706bfa47eSMauro Carvalho Chehabthe network stack, the feature has to be enabled for all packets. The 4806bfa47eSMauro Carvalho Chehabsame is true for all early receive timestamp options. 4906bfa47eSMauro Carvalho Chehab 5006bfa47eSMauro Carvalho ChehabFor interface details, see `man 7 socket`. 5106bfa47eSMauro Carvalho Chehab 5206bfa47eSMauro Carvalho ChehabAlways use SO_TIMESTAMP_NEW timestamp to always get timestamp in 5306bfa47eSMauro Carvalho Chehabstruct __kernel_sock_timeval format. 5406bfa47eSMauro Carvalho Chehab 5506bfa47eSMauro Carvalho ChehabSO_TIMESTAMP_OLD returns incorrect timestamps after the year 2038 5606bfa47eSMauro Carvalho Chehabon 32 bit machines. 5706bfa47eSMauro Carvalho Chehab 585daf8384SJan Luebbe1.2 SO_TIMESTAMPNS (also SO_TIMESTAMPNS_OLD and SO_TIMESTAMPNS_NEW) 595daf8384SJan Luebbe------------------------------------------------------------------- 6006bfa47eSMauro Carvalho Chehab 6106bfa47eSMauro Carvalho ChehabThis option is identical to SO_TIMESTAMP except for the returned data type. 6206bfa47eSMauro Carvalho ChehabIts struct timespec allows for higher resolution (ns) timestamps than the 6306bfa47eSMauro Carvalho Chehabtimeval of SO_TIMESTAMP (ms). 6406bfa47eSMauro Carvalho Chehab 6506bfa47eSMauro Carvalho ChehabAlways use SO_TIMESTAMPNS_NEW timestamp to always get timestamp in 6606bfa47eSMauro Carvalho Chehabstruct __kernel_timespec format. 6706bfa47eSMauro Carvalho Chehab 6806bfa47eSMauro Carvalho ChehabSO_TIMESTAMPNS_OLD returns incorrect timestamps after the year 2038 6906bfa47eSMauro Carvalho Chehabon 32 bit machines. 7006bfa47eSMauro Carvalho Chehab 7106bfa47eSMauro Carvalho Chehab1.3 SO_TIMESTAMPING (also SO_TIMESTAMPING_OLD and SO_TIMESTAMPING_NEW) 7206bfa47eSMauro Carvalho Chehab---------------------------------------------------------------------- 7306bfa47eSMauro Carvalho Chehab 7406bfa47eSMauro Carvalho ChehabSupports multiple types of timestamp requests. As a result, this 7506bfa47eSMauro Carvalho Chehabsocket option takes a bitmap of flags, not a boolean. In:: 7606bfa47eSMauro Carvalho Chehab 7706bfa47eSMauro Carvalho Chehab err = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val)); 7806bfa47eSMauro Carvalho Chehab 7906bfa47eSMauro Carvalho Chehabval is an integer with any of the following bits set. Setting other 8006bfa47eSMauro Carvalho Chehabbit returns EINVAL and does not change the current state. 8106bfa47eSMauro Carvalho Chehab 8206bfa47eSMauro Carvalho ChehabThe socket option configures timestamp generation for individual 8306bfa47eSMauro Carvalho Chehabsk_buffs (1.3.1), timestamp reporting to the socket's error 8406bfa47eSMauro Carvalho Chehabqueue (1.3.2) and options (1.3.3). Timestamp generation can also 8506bfa47eSMauro Carvalho Chehabbe enabled for individual sendmsg calls using cmsg (1.3.4). 8606bfa47eSMauro Carvalho Chehab 8706bfa47eSMauro Carvalho Chehab 8806bfa47eSMauro Carvalho Chehab1.3.1 Timestamp Generation 8906bfa47eSMauro Carvalho Chehab^^^^^^^^^^^^^^^^^^^^^^^^^^ 9006bfa47eSMauro Carvalho Chehab 9106bfa47eSMauro Carvalho ChehabSome bits are requests to the stack to try to generate timestamps. Any 9206bfa47eSMauro Carvalho Chehabcombination of them is valid. Changes to these bits apply to newly 9306bfa47eSMauro Carvalho Chehabcreated packets, not to packets already in the stack. As a result, it 9406bfa47eSMauro Carvalho Chehabis possible to selectively request timestamps for a subset of packets 9506bfa47eSMauro Carvalho Chehab(e.g., for sampling) by embedding an send() call within two setsockopt 9606bfa47eSMauro Carvalho Chehabcalls, one to enable timestamp generation and one to disable it. 9706bfa47eSMauro Carvalho ChehabTimestamps may also be generated for reasons other than being 9806bfa47eSMauro Carvalho Chehabrequested by a particular socket, such as when receive timestamping is 9906bfa47eSMauro Carvalho Chehabenabled system wide, as explained earlier. 10006bfa47eSMauro Carvalho Chehab 10106bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_RX_HARDWARE: 10206bfa47eSMauro Carvalho Chehab Request rx timestamps generated by the network adapter. 10306bfa47eSMauro Carvalho Chehab 10406bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_RX_SOFTWARE: 10506bfa47eSMauro Carvalho Chehab Request rx timestamps when data enters the kernel. These timestamps 10606bfa47eSMauro Carvalho Chehab are generated just after a device driver hands a packet to the 10706bfa47eSMauro Carvalho Chehab kernel receive stack. 10806bfa47eSMauro Carvalho Chehab 10906bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_TX_HARDWARE: 11006bfa47eSMauro Carvalho Chehab Request tx timestamps generated by the network adapter. This flag 11106bfa47eSMauro Carvalho Chehab can be enabled via both socket options and control messages. 11206bfa47eSMauro Carvalho Chehab 11306bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_TX_SOFTWARE: 11406bfa47eSMauro Carvalho Chehab Request tx timestamps when data leaves the kernel. These timestamps 11506bfa47eSMauro Carvalho Chehab are generated in the device driver as close as possible, but always 11606bfa47eSMauro Carvalho Chehab prior to, passing the packet to the network interface. Hence, they 11706bfa47eSMauro Carvalho Chehab require driver support and may not be available for all devices. 11806bfa47eSMauro Carvalho Chehab This flag can be enabled via both socket options and control messages. 11906bfa47eSMauro Carvalho Chehab 12006bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_TX_SCHED: 12106bfa47eSMauro Carvalho Chehab Request tx timestamps prior to entering the packet scheduler. Kernel 12206bfa47eSMauro Carvalho Chehab transmit latency is, if long, often dominated by queuing delay. The 12306bfa47eSMauro Carvalho Chehab difference between this timestamp and one taken at 12406bfa47eSMauro Carvalho Chehab SOF_TIMESTAMPING_TX_SOFTWARE will expose this latency independent 12506bfa47eSMauro Carvalho Chehab of protocol processing. The latency incurred in protocol 12606bfa47eSMauro Carvalho Chehab processing, if any, can be computed by subtracting a userspace 12706bfa47eSMauro Carvalho Chehab timestamp taken immediately before send() from this timestamp. On 12806bfa47eSMauro Carvalho Chehab machines with virtual devices where a transmitted packet travels 12906bfa47eSMauro Carvalho Chehab through multiple devices and, hence, multiple packet schedulers, 13006bfa47eSMauro Carvalho Chehab a timestamp is generated at each layer. This allows for fine 13106bfa47eSMauro Carvalho Chehab grained measurement of queuing delay. This flag can be enabled 13206bfa47eSMauro Carvalho Chehab via both socket options and control messages. 13306bfa47eSMauro Carvalho Chehab 13406bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_TX_ACK: 13506bfa47eSMauro Carvalho Chehab Request tx timestamps when all data in the send buffer has been 13606bfa47eSMauro Carvalho Chehab acknowledged. This only makes sense for reliable protocols. It is 13706bfa47eSMauro Carvalho Chehab currently only implemented for TCP. For that protocol, it may 13806bfa47eSMauro Carvalho Chehab over-report measurement, because the timestamp is generated when all 13906bfa47eSMauro Carvalho Chehab data up to and including the buffer at send() was acknowledged: the 14006bfa47eSMauro Carvalho Chehab cumulative acknowledgment. The mechanism ignores SACK and FACK. 14106bfa47eSMauro Carvalho Chehab This flag can be enabled via both socket options and control messages. 14206bfa47eSMauro Carvalho Chehab 14306bfa47eSMauro Carvalho Chehab 14406bfa47eSMauro Carvalho Chehab1.3.2 Timestamp Reporting 14506bfa47eSMauro Carvalho Chehab^^^^^^^^^^^^^^^^^^^^^^^^^ 14606bfa47eSMauro Carvalho Chehab 14706bfa47eSMauro Carvalho ChehabThe other three bits control which timestamps will be reported in a 14806bfa47eSMauro Carvalho Chehabgenerated control message. Changes to the bits take immediate 14906bfa47eSMauro Carvalho Chehabeffect at the timestamp reporting locations in the stack. Timestamps 15006bfa47eSMauro Carvalho Chehabare only reported for packets that also have the relevant timestamp 15106bfa47eSMauro Carvalho Chehabgeneration request set. 15206bfa47eSMauro Carvalho Chehab 15306bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_SOFTWARE: 15406bfa47eSMauro Carvalho Chehab Report any software timestamps when available. 15506bfa47eSMauro Carvalho Chehab 15606bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_SYS_HARDWARE: 15706bfa47eSMauro Carvalho Chehab This option is deprecated and ignored. 15806bfa47eSMauro Carvalho Chehab 15906bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_RAW_HARDWARE: 16006bfa47eSMauro Carvalho Chehab Report hardware timestamps as generated by 161e503f82eSJason Xing SOF_TIMESTAMPING_TX_HARDWARE or SOF_TIMESTAMPING_RX_HARDWARE 162e503f82eSJason Xing when available. 16306bfa47eSMauro Carvalho Chehab 16406bfa47eSMauro Carvalho Chehab 16506bfa47eSMauro Carvalho Chehab1.3.3 Timestamp Options 16606bfa47eSMauro Carvalho Chehab^^^^^^^^^^^^^^^^^^^^^^^ 16706bfa47eSMauro Carvalho Chehab 16806bfa47eSMauro Carvalho ChehabThe interface supports the options 16906bfa47eSMauro Carvalho Chehab 17006bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_OPT_ID: 17106bfa47eSMauro Carvalho Chehab Generate a unique identifier along with each packet. A process can 17206bfa47eSMauro Carvalho Chehab have multiple concurrent timestamping requests outstanding. Packets 17306bfa47eSMauro Carvalho Chehab can be reordered in the transmit path, for instance in the packet 17406bfa47eSMauro Carvalho Chehab scheduler. In that case timestamps will be queued onto the error 17506bfa47eSMauro Carvalho Chehab queue out of order from the original send() calls. It is not always 17606bfa47eSMauro Carvalho Chehab possible to uniquely match timestamps to the original send() calls 17706bfa47eSMauro Carvalho Chehab based on timestamp order or payload inspection alone, then. 17806bfa47eSMauro Carvalho Chehab 17906bfa47eSMauro Carvalho Chehab This option associates each packet at send() with a unique 18006bfa47eSMauro Carvalho Chehab identifier and returns that along with the timestamp. The identifier 18106bfa47eSMauro Carvalho Chehab is derived from a per-socket u32 counter (that wraps). For datagram 18206bfa47eSMauro Carvalho Chehab sockets, the counter increments with each sent packet. For stream 183b534dc46SWillem de Bruijn sockets, it increments with every byte. For stream sockets, also set 184b534dc46SWillem de Bruijn SOF_TIMESTAMPING_OPT_ID_TCP, see the section below. 18506bfa47eSMauro Carvalho Chehab 18606bfa47eSMauro Carvalho Chehab The counter starts at zero. It is initialized the first time that 18706bfa47eSMauro Carvalho Chehab the socket option is enabled. It is reset each time the option is 18806bfa47eSMauro Carvalho Chehab enabled after having been disabled. Resetting the counter does not 18906bfa47eSMauro Carvalho Chehab change the identifiers of existing packets in the system. 19006bfa47eSMauro Carvalho Chehab 19106bfa47eSMauro Carvalho Chehab This option is implemented only for transmit timestamps. There, the 19206bfa47eSMauro Carvalho Chehab timestamp is always looped along with a struct sock_extended_err. 19306bfa47eSMauro Carvalho Chehab The option modifies field ee_data to pass an id that is unique 19406bfa47eSMauro Carvalho Chehab among all possibly concurrently outstanding timestamp requests for 19506bfa47eSMauro Carvalho Chehab that socket. 19606bfa47eSMauro Carvalho Chehab 197b534dc46SWillem de BruijnSOF_TIMESTAMPING_OPT_ID_TCP: 198b534dc46SWillem de Bruijn Pass this modifier along with SOF_TIMESTAMPING_OPT_ID for new TCP 199b534dc46SWillem de Bruijn timestamping applications. SOF_TIMESTAMPING_OPT_ID defines how the 200b534dc46SWillem de Bruijn counter increments for stream sockets, but its starting point is 201b534dc46SWillem de Bruijn not entirely trivial. This option fixes that. 202b534dc46SWillem de Bruijn 203b534dc46SWillem de Bruijn For stream sockets, if SOF_TIMESTAMPING_OPT_ID is set, this should 204b534dc46SWillem de Bruijn always be set too. On datagram sockets the option has no effect. 205b534dc46SWillem de Bruijn 206b534dc46SWillem de Bruijn A reasonable expectation is that the counter is reset to zero with 207b534dc46SWillem de Bruijn the system call, so that a subsequent write() of N bytes generates 208b534dc46SWillem de Bruijn a timestamp with counter N-1. SOF_TIMESTAMPING_OPT_ID_TCP 209b534dc46SWillem de Bruijn implements this behavior under all conditions. 210b534dc46SWillem de Bruijn 211b534dc46SWillem de Bruijn SOF_TIMESTAMPING_OPT_ID without modifier often reports the same, 212b534dc46SWillem de Bruijn especially when the socket option is set when no data is in 213b534dc46SWillem de Bruijn transmission. If data is being transmitted, it may be off by the 214b534dc46SWillem de Bruijn length of the output queue (SIOCOUTQ). 215b534dc46SWillem de Bruijn 216b534dc46SWillem de Bruijn The difference is due to being based on snd_una versus write_seq. 217b534dc46SWillem de Bruijn snd_una is the offset in the stream acknowledged by the peer. This 218b534dc46SWillem de Bruijn depends on factors outside of process control, such as network RTT. 219b534dc46SWillem de Bruijn write_seq is the last byte written by the process. This offset is 220b534dc46SWillem de Bruijn not affected by external inputs. 221b534dc46SWillem de Bruijn 222b534dc46SWillem de Bruijn The difference is subtle and unlikely to be noticed when configured 223b534dc46SWillem de Bruijn at initial socket creation, when no data is queued or sent. But 224b534dc46SWillem de Bruijn SOF_TIMESTAMPING_OPT_ID_TCP behavior is more robust regardless of 225b534dc46SWillem de Bruijn when the socket option is set. 22606bfa47eSMauro Carvalho Chehab 22706bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_OPT_CMSG: 22806bfa47eSMauro Carvalho Chehab Support recv() cmsg for all timestamped packets. Control messages 22906bfa47eSMauro Carvalho Chehab are already supported unconditionally on all packets with receive 23006bfa47eSMauro Carvalho Chehab timestamps and on IPv6 packets with transmit timestamp. This option 23106bfa47eSMauro Carvalho Chehab extends them to IPv4 packets with transmit timestamp. One use case 23206bfa47eSMauro Carvalho Chehab is to correlate packets with their egress device, by enabling socket 23306bfa47eSMauro Carvalho Chehab option IP_PKTINFO simultaneously. 23406bfa47eSMauro Carvalho Chehab 23506bfa47eSMauro Carvalho Chehab 23606bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_OPT_TSONLY: 23706bfa47eSMauro Carvalho Chehab Applies to transmit timestamps only. Makes the kernel return the 23806bfa47eSMauro Carvalho Chehab timestamp as a cmsg alongside an empty packet, as opposed to 23906bfa47eSMauro Carvalho Chehab alongside the original packet. This reduces the amount of memory 24006bfa47eSMauro Carvalho Chehab charged to the socket's receive budget (SO_RCVBUF) and delivers 24106bfa47eSMauro Carvalho Chehab the timestamp even if sysctl net.core.tstamp_allow_data is 0. 24206bfa47eSMauro Carvalho Chehab This option disables SOF_TIMESTAMPING_OPT_CMSG. 24306bfa47eSMauro Carvalho Chehab 24406bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_OPT_STATS: 24506bfa47eSMauro Carvalho Chehab Optional stats that are obtained along with the transmit timestamps. 24606bfa47eSMauro Carvalho Chehab It must be used together with SOF_TIMESTAMPING_OPT_TSONLY. When the 24706bfa47eSMauro Carvalho Chehab transmit timestamp is available, the stats are available in a 24806bfa47eSMauro Carvalho Chehab separate control message of type SCM_TIMESTAMPING_OPT_STATS, as a 24906bfa47eSMauro Carvalho Chehab list of TLVs (struct nlattr) of types. These stats allow the 25006bfa47eSMauro Carvalho Chehab application to associate various transport layer stats with 25106bfa47eSMauro Carvalho Chehab the transmit timestamps, such as how long a certain block of 25206bfa47eSMauro Carvalho Chehab data was limited by peer's receiver window. 25306bfa47eSMauro Carvalho Chehab 25406bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_OPT_PKTINFO: 25506bfa47eSMauro Carvalho Chehab Enable the SCM_TIMESTAMPING_PKTINFO control message for incoming 25606bfa47eSMauro Carvalho Chehab packets with hardware timestamps. The message contains struct 25706bfa47eSMauro Carvalho Chehab scm_ts_pktinfo, which supplies the index of the real interface which 25806bfa47eSMauro Carvalho Chehab received the packet and its length at layer 2. A valid (non-zero) 25906bfa47eSMauro Carvalho Chehab interface index will be returned only if CONFIG_NET_RX_BUSY_POLL is 26006bfa47eSMauro Carvalho Chehab enabled and the driver is using NAPI. The struct contains also two 26106bfa47eSMauro Carvalho Chehab other fields, but they are reserved and undefined. 26206bfa47eSMauro Carvalho Chehab 26306bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_OPT_TX_SWHW: 26406bfa47eSMauro Carvalho Chehab Request both hardware and software timestamps for outgoing packets 26506bfa47eSMauro Carvalho Chehab when SOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_TX_SOFTWARE 26606bfa47eSMauro Carvalho Chehab are enabled at the same time. If both timestamps are generated, 26706bfa47eSMauro Carvalho Chehab two separate messages will be looped to the socket's error queue, 26806bfa47eSMauro Carvalho Chehab each containing just one timestamp. 26906bfa47eSMauro Carvalho Chehab 270*be8e9eb3SJason XingSOF_TIMESTAMPING_OPT_RX_FILTER: 271*be8e9eb3SJason Xing Filter out spurious receive timestamps: report a receive timestamp 272*be8e9eb3SJason Xing only if the matching timestamp generation flag is enabled. 273*be8e9eb3SJason Xing 274*be8e9eb3SJason Xing Receive timestamps are generated early in the ingress path, before a 275*be8e9eb3SJason Xing packet's destination socket is known. If any socket enables receive 276*be8e9eb3SJason Xing timestamps, packets for all socket will receive timestamped packets. 277*be8e9eb3SJason Xing Including those that request timestamp reporting with 278*be8e9eb3SJason Xing SOF_TIMESTAMPING_SOFTWARE and/or SOF_TIMESTAMPING_RAW_HARDWARE, but 279*be8e9eb3SJason Xing do not request receive timestamp generation. This can happen when 280*be8e9eb3SJason Xing requesting transmit timestamps only. 281*be8e9eb3SJason Xing 282*be8e9eb3SJason Xing Receiving spurious timestamps is generally benign. A process can 283*be8e9eb3SJason Xing ignore the unexpected non-zero value. But it makes behavior subtly 284*be8e9eb3SJason Xing dependent on other sockets. This flag isolates the socket for more 285*be8e9eb3SJason Xing deterministic behavior. 286*be8e9eb3SJason Xing 28706bfa47eSMauro Carvalho ChehabNew applications are encouraged to pass SOF_TIMESTAMPING_OPT_ID to 28806bfa47eSMauro Carvalho Chehabdisambiguate timestamps and SOF_TIMESTAMPING_OPT_TSONLY to operate 28906bfa47eSMauro Carvalho Chehabregardless of the setting of sysctl net.core.tstamp_allow_data. 29006bfa47eSMauro Carvalho Chehab 29106bfa47eSMauro Carvalho ChehabAn exception is when a process needs additional cmsg data, for 29206bfa47eSMauro Carvalho Chehabinstance SOL_IP/IP_PKTINFO to detect the egress network interface. 29306bfa47eSMauro Carvalho ChehabThen pass option SOF_TIMESTAMPING_OPT_CMSG. This option depends on 29406bfa47eSMauro Carvalho Chehabhaving access to the contents of the original packet, so cannot be 29506bfa47eSMauro Carvalho Chehabcombined with SOF_TIMESTAMPING_OPT_TSONLY. 29606bfa47eSMauro Carvalho Chehab 29706bfa47eSMauro Carvalho Chehab 29806bfa47eSMauro Carvalho Chehab1.3.4. Enabling timestamps via control messages 29906bfa47eSMauro Carvalho Chehab^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 30006bfa47eSMauro Carvalho Chehab 30106bfa47eSMauro Carvalho ChehabIn addition to socket options, timestamp generation can be requested 30206bfa47eSMauro Carvalho Chehabper write via cmsg, only for SOF_TIMESTAMPING_TX_* (see Section 1.3.1). 30306bfa47eSMauro Carvalho ChehabUsing this feature, applications can sample timestamps per sendmsg() 30406bfa47eSMauro Carvalho Chehabwithout paying the overhead of enabling and disabling timestamps via 30506bfa47eSMauro Carvalho Chehabsetsockopt:: 30606bfa47eSMauro Carvalho Chehab 30706bfa47eSMauro Carvalho Chehab struct msghdr *msg; 30806bfa47eSMauro Carvalho Chehab ... 30906bfa47eSMauro Carvalho Chehab cmsg = CMSG_FIRSTHDR(msg); 31006bfa47eSMauro Carvalho Chehab cmsg->cmsg_level = SOL_SOCKET; 31106bfa47eSMauro Carvalho Chehab cmsg->cmsg_type = SO_TIMESTAMPING; 31206bfa47eSMauro Carvalho Chehab cmsg->cmsg_len = CMSG_LEN(sizeof(__u32)); 31306bfa47eSMauro Carvalho Chehab *((__u32 *) CMSG_DATA(cmsg)) = SOF_TIMESTAMPING_TX_SCHED | 31406bfa47eSMauro Carvalho Chehab SOF_TIMESTAMPING_TX_SOFTWARE | 31506bfa47eSMauro Carvalho Chehab SOF_TIMESTAMPING_TX_ACK; 31606bfa47eSMauro Carvalho Chehab err = sendmsg(fd, msg, 0); 31706bfa47eSMauro Carvalho Chehab 31806bfa47eSMauro Carvalho ChehabThe SOF_TIMESTAMPING_TX_* flags set via cmsg will override 31906bfa47eSMauro Carvalho Chehabthe SOF_TIMESTAMPING_TX_* flags set via setsockopt. 32006bfa47eSMauro Carvalho Chehab 32106bfa47eSMauro Carvalho ChehabMoreover, applications must still enable timestamp reporting via 32206bfa47eSMauro Carvalho Chehabsetsockopt to receive timestamps:: 32306bfa47eSMauro Carvalho Chehab 32406bfa47eSMauro Carvalho Chehab __u32 val = SOF_TIMESTAMPING_SOFTWARE | 32506bfa47eSMauro Carvalho Chehab SOF_TIMESTAMPING_OPT_ID /* or any other flag */; 32606bfa47eSMauro Carvalho Chehab err = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val)); 32706bfa47eSMauro Carvalho Chehab 32806bfa47eSMauro Carvalho Chehab 32906bfa47eSMauro Carvalho Chehab1.4 Bytestream Timestamps 33006bfa47eSMauro Carvalho Chehab------------------------- 33106bfa47eSMauro Carvalho Chehab 33206bfa47eSMauro Carvalho ChehabThe SO_TIMESTAMPING interface supports timestamping of bytes in a 33306bfa47eSMauro Carvalho Chehabbytestream. Each request is interpreted as a request for when the 33406bfa47eSMauro Carvalho Chehabentire contents of the buffer has passed a timestamping point. That 33506bfa47eSMauro Carvalho Chehabis, for streams option SOF_TIMESTAMPING_TX_SOFTWARE will record 33606bfa47eSMauro Carvalho Chehabwhen all bytes have reached the device driver, regardless of how 33706bfa47eSMauro Carvalho Chehabmany packets the data has been converted into. 33806bfa47eSMauro Carvalho Chehab 33906bfa47eSMauro Carvalho ChehabIn general, bytestreams have no natural delimiters and therefore 34006bfa47eSMauro Carvalho Chehabcorrelating a timestamp with data is non-trivial. A range of bytes 34106bfa47eSMauro Carvalho Chehabmay be split across segments, any segments may be merged (possibly 34206bfa47eSMauro Carvalho Chehabcoalescing sections of previously segmented buffers associated with 34306bfa47eSMauro Carvalho Chehabindependent send() calls). Segments can be reordered and the same 34406bfa47eSMauro Carvalho Chehabbyte range can coexist in multiple segments for protocols that 34506bfa47eSMauro Carvalho Chehabimplement retransmissions. 34606bfa47eSMauro Carvalho Chehab 34706bfa47eSMauro Carvalho ChehabIt is essential that all timestamps implement the same semantics, 34806bfa47eSMauro Carvalho Chehabregardless of these possible transformations, as otherwise they are 34906bfa47eSMauro Carvalho Chehabincomparable. Handling "rare" corner cases differently from the 35006bfa47eSMauro Carvalho Chehabsimple case (a 1:1 mapping from buffer to skb) is insufficient 35106bfa47eSMauro Carvalho Chehabbecause performance debugging often needs to focus on such outliers. 35206bfa47eSMauro Carvalho Chehab 35306bfa47eSMauro Carvalho ChehabIn practice, timestamps can be correlated with segments of a 35406bfa47eSMauro Carvalho Chehabbytestream consistently, if both semantics of the timestamp and the 35506bfa47eSMauro Carvalho Chehabtiming of measurement are chosen correctly. This challenge is no 35606bfa47eSMauro Carvalho Chehabdifferent from deciding on a strategy for IP fragmentation. There, the 35706bfa47eSMauro Carvalho Chehabdefinition is that only the first fragment is timestamped. For 35806bfa47eSMauro Carvalho Chehabbytestreams, we chose that a timestamp is generated only when all 35906bfa47eSMauro Carvalho Chehabbytes have passed a point. SOF_TIMESTAMPING_TX_ACK as defined is easy to 36006bfa47eSMauro Carvalho Chehabimplement and reason about. An implementation that has to take into 36106bfa47eSMauro Carvalho Chehabaccount SACK would be more complex due to possible transmission holes 36206bfa47eSMauro Carvalho Chehaband out of order arrival. 36306bfa47eSMauro Carvalho Chehab 36406bfa47eSMauro Carvalho ChehabOn the host, TCP can also break the simple 1:1 mapping from buffer to 36506bfa47eSMauro Carvalho Chehabskbuff as a result of Nagle, cork, autocork, segmentation and GSO. The 36606bfa47eSMauro Carvalho Chehabimplementation ensures correctness in all cases by tracking the 36706bfa47eSMauro Carvalho Chehabindividual last byte passed to send(), even if it is no longer the 36806bfa47eSMauro Carvalho Chehablast byte after an skbuff extend or merge operation. It stores the 36906bfa47eSMauro Carvalho Chehabrelevant sequence number in skb_shinfo(skb)->tskey. Because an skbuff 37006bfa47eSMauro Carvalho Chehabhas only one such field, only one timestamp can be generated. 37106bfa47eSMauro Carvalho Chehab 37206bfa47eSMauro Carvalho ChehabIn rare cases, a timestamp request can be missed if two requests are 37306bfa47eSMauro Carvalho Chehabcollapsed onto the same skb. A process can detect this situation by 37406bfa47eSMauro Carvalho Chehabenabling SOF_TIMESTAMPING_OPT_ID and comparing the byte offset at 37506bfa47eSMauro Carvalho Chehabsend time with the value returned for each timestamp. It can prevent 37606bfa47eSMauro Carvalho Chehabthe situation by always flushing the TCP stack in between requests, 37706bfa47eSMauro Carvalho Chehabfor instance by enabling TCP_NODELAY and disabling TCP_CORK and 378173b6d1cSEric Dumazetautocork. After linux-4.7, a better way to prevent coalescing is 379173b6d1cSEric Dumazetto use MSG_EOR flag at sendmsg() time. 38006bfa47eSMauro Carvalho Chehab 38106bfa47eSMauro Carvalho ChehabThese precautions ensure that the timestamp is generated only when all 38206bfa47eSMauro Carvalho Chehabbytes have passed a timestamp point, assuming that the network stack 38306bfa47eSMauro Carvalho Chehabitself does not reorder the segments. The stack indeed tries to avoid 38406bfa47eSMauro Carvalho Chehabreordering. The one exception is under administrator control: it is 38506bfa47eSMauro Carvalho Chehabpossible to construct a packet scheduler configuration that delays 38606bfa47eSMauro Carvalho Chehabsegments from the same stream differently. Such a setup would be 38706bfa47eSMauro Carvalho Chehabunusual. 38806bfa47eSMauro Carvalho Chehab 38906bfa47eSMauro Carvalho Chehab 39006bfa47eSMauro Carvalho Chehab2 Data Interfaces 39106bfa47eSMauro Carvalho Chehab================== 39206bfa47eSMauro Carvalho Chehab 39306bfa47eSMauro Carvalho ChehabTimestamps are read using the ancillary data feature of recvmsg(). 39406bfa47eSMauro Carvalho ChehabSee `man 3 cmsg` for details of this interface. The socket manual 39506bfa47eSMauro Carvalho Chehabpage (`man 7 socket`) describes how timestamps generated with 39606bfa47eSMauro Carvalho ChehabSO_TIMESTAMP and SO_TIMESTAMPNS records can be retrieved. 39706bfa47eSMauro Carvalho Chehab 39806bfa47eSMauro Carvalho Chehab 39906bfa47eSMauro Carvalho Chehab2.1 SCM_TIMESTAMPING records 40006bfa47eSMauro Carvalho Chehab---------------------------- 40106bfa47eSMauro Carvalho Chehab 40206bfa47eSMauro Carvalho ChehabThese timestamps are returned in a control message with cmsg_level 40306bfa47eSMauro Carvalho ChehabSOL_SOCKET, cmsg_type SCM_TIMESTAMPING, and payload of type 40406bfa47eSMauro Carvalho Chehab 40506bfa47eSMauro Carvalho ChehabFor SO_TIMESTAMPING_OLD:: 40606bfa47eSMauro Carvalho Chehab 40706bfa47eSMauro Carvalho Chehab struct scm_timestamping { 40806bfa47eSMauro Carvalho Chehab struct timespec ts[3]; 40906bfa47eSMauro Carvalho Chehab }; 41006bfa47eSMauro Carvalho Chehab 41106bfa47eSMauro Carvalho ChehabFor SO_TIMESTAMPING_NEW:: 41206bfa47eSMauro Carvalho Chehab 41306bfa47eSMauro Carvalho Chehab struct scm_timestamping64 { 41406bfa47eSMauro Carvalho Chehab struct __kernel_timespec ts[3]; 41506bfa47eSMauro Carvalho Chehab 41606bfa47eSMauro Carvalho ChehabAlways use SO_TIMESTAMPING_NEW timestamp to always get timestamp in 41706bfa47eSMauro Carvalho Chehabstruct scm_timestamping64 format. 41806bfa47eSMauro Carvalho Chehab 41906bfa47eSMauro Carvalho ChehabSO_TIMESTAMPING_OLD returns incorrect timestamps after the year 2038 42006bfa47eSMauro Carvalho Chehabon 32 bit machines. 42106bfa47eSMauro Carvalho Chehab 42206bfa47eSMauro Carvalho ChehabThe structure can return up to three timestamps. This is a legacy 42306bfa47eSMauro Carvalho Chehabfeature. At least one field is non-zero at any time. Most timestamps 42406bfa47eSMauro Carvalho Chehabare passed in ts[0]. Hardware timestamps are passed in ts[2]. 42506bfa47eSMauro Carvalho Chehab 42606bfa47eSMauro Carvalho Chehabts[1] used to hold hardware timestamps converted to system time. 42706bfa47eSMauro Carvalho ChehabInstead, expose the hardware clock device on the NIC directly as 42806bfa47eSMauro Carvalho Chehaba HW PTP clock source, to allow time conversion in userspace and 42906bfa47eSMauro Carvalho Chehaboptionally synchronize system time with a userspace PTP stack such 43006bfa47eSMauro Carvalho Chehabas linuxptp. For the PTP clock API, see Documentation/driver-api/ptp.rst. 43106bfa47eSMauro Carvalho Chehab 43206bfa47eSMauro Carvalho ChehabNote that if the SO_TIMESTAMP or SO_TIMESTAMPNS option is enabled 43306bfa47eSMauro Carvalho Chehabtogether with SO_TIMESTAMPING using SOF_TIMESTAMPING_SOFTWARE, a false 43406bfa47eSMauro Carvalho Chehabsoftware timestamp will be generated in the recvmsg() call and passed 43506bfa47eSMauro Carvalho Chehabin ts[0] when a real software timestamp is missing. This happens also 43606bfa47eSMauro Carvalho Chehabon hardware transmit timestamps. 43706bfa47eSMauro Carvalho Chehab 43806bfa47eSMauro Carvalho Chehab2.1.1 Transmit timestamps with MSG_ERRQUEUE 43906bfa47eSMauro Carvalho Chehab^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 44006bfa47eSMauro Carvalho Chehab 44106bfa47eSMauro Carvalho ChehabFor transmit timestamps the outgoing packet is looped back to the 44206bfa47eSMauro Carvalho Chehabsocket's error queue with the send timestamp(s) attached. A process 44306bfa47eSMauro Carvalho Chehabreceives the timestamps by calling recvmsg() with flag MSG_ERRQUEUE 44406bfa47eSMauro Carvalho Chehabset and with a msg_control buffer sufficiently large to receive the 44506bfa47eSMauro Carvalho Chehabrelevant metadata structures. The recvmsg call returns the original 44606bfa47eSMauro Carvalho Chehaboutgoing data packet with two ancillary messages attached. 44706bfa47eSMauro Carvalho Chehab 44806bfa47eSMauro Carvalho ChehabA message of cm_level SOL_IP(V6) and cm_type IP(V6)_RECVERR 44906bfa47eSMauro Carvalho Chehabembeds a struct sock_extended_err. This defines the error type. For 45006bfa47eSMauro Carvalho Chehabtimestamps, the ee_errno field is ENOMSG. The other ancillary message 45106bfa47eSMauro Carvalho Chehabwill have cm_level SOL_SOCKET and cm_type SCM_TIMESTAMPING. This 45206bfa47eSMauro Carvalho Chehabembeds the struct scm_timestamping. 45306bfa47eSMauro Carvalho Chehab 45406bfa47eSMauro Carvalho Chehab 45506bfa47eSMauro Carvalho Chehab2.1.1.2 Timestamp types 45606bfa47eSMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~ 45706bfa47eSMauro Carvalho Chehab 45806bfa47eSMauro Carvalho ChehabThe semantics of the three struct timespec are defined by field 45906bfa47eSMauro Carvalho Chehabee_info in the extended error structure. It contains a value of 46006bfa47eSMauro Carvalho Chehabtype SCM_TSTAMP_* to define the actual timestamp passed in 46106bfa47eSMauro Carvalho Chehabscm_timestamping. 46206bfa47eSMauro Carvalho Chehab 46306bfa47eSMauro Carvalho ChehabThe SCM_TSTAMP_* types are 1:1 matches to the SOF_TIMESTAMPING_* 46406bfa47eSMauro Carvalho Chehabcontrol fields discussed previously, with one exception. For legacy 46506bfa47eSMauro Carvalho Chehabreasons, SCM_TSTAMP_SND is equal to zero and can be set for both 46606bfa47eSMauro Carvalho ChehabSOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_TX_SOFTWARE. It 46706bfa47eSMauro Carvalho Chehabis the first if ts[2] is non-zero, the second otherwise, in which 46806bfa47eSMauro Carvalho Chehabcase the timestamp is stored in ts[0]. 46906bfa47eSMauro Carvalho Chehab 47006bfa47eSMauro Carvalho Chehab 47106bfa47eSMauro Carvalho Chehab2.1.1.3 Fragmentation 47206bfa47eSMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~ 47306bfa47eSMauro Carvalho Chehab 47406bfa47eSMauro Carvalho ChehabFragmentation of outgoing datagrams is rare, but is possible, e.g., by 47506bfa47eSMauro Carvalho Chehabexplicitly disabling PMTU discovery. If an outgoing packet is fragmented, 47606bfa47eSMauro Carvalho Chehabthen only the first fragment is timestamped and returned to the sending 47706bfa47eSMauro Carvalho Chehabsocket. 47806bfa47eSMauro Carvalho Chehab 47906bfa47eSMauro Carvalho Chehab 48006bfa47eSMauro Carvalho Chehab2.1.1.4 Packet Payload 48106bfa47eSMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~ 48206bfa47eSMauro Carvalho Chehab 48306bfa47eSMauro Carvalho ChehabThe calling application is often not interested in receiving the whole 48406bfa47eSMauro Carvalho Chehabpacket payload that it passed to the stack originally: the socket 48506bfa47eSMauro Carvalho Chehaberror queue mechanism is just a method to piggyback the timestamp on. 48606bfa47eSMauro Carvalho ChehabIn this case, the application can choose to read datagrams with a 48706bfa47eSMauro Carvalho Chehabsmaller buffer, possibly even of length 0. The payload is truncated 48806bfa47eSMauro Carvalho Chehabaccordingly. Until the process calls recvmsg() on the error queue, 48906bfa47eSMauro Carvalho Chehabhowever, the full packet is queued, taking up budget from SO_RCVBUF. 49006bfa47eSMauro Carvalho Chehab 49106bfa47eSMauro Carvalho Chehab 49206bfa47eSMauro Carvalho Chehab2.1.1.5 Blocking Read 49306bfa47eSMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~ 49406bfa47eSMauro Carvalho Chehab 49506bfa47eSMauro Carvalho ChehabReading from the error queue is always a non-blocking operation. To 49606bfa47eSMauro Carvalho Chehabblock waiting on a timestamp, use poll or select. poll() will return 49706bfa47eSMauro Carvalho ChehabPOLLERR in pollfd.revents if any data is ready on the error queue. 49806bfa47eSMauro Carvalho ChehabThere is no need to pass this flag in pollfd.events. This flag is 49906bfa47eSMauro Carvalho Chehabignored on request. See also `man 2 poll`. 50006bfa47eSMauro Carvalho Chehab 50106bfa47eSMauro Carvalho Chehab 50206bfa47eSMauro Carvalho Chehab2.1.2 Receive timestamps 50306bfa47eSMauro Carvalho Chehab^^^^^^^^^^^^^^^^^^^^^^^^ 50406bfa47eSMauro Carvalho Chehab 50506bfa47eSMauro Carvalho ChehabOn reception, there is no reason to read from the socket error queue. 50606bfa47eSMauro Carvalho ChehabThe SCM_TIMESTAMPING ancillary data is sent along with the packet data 50706bfa47eSMauro Carvalho Chehabon a normal recvmsg(). Since this is not a socket error, it is not 50806bfa47eSMauro Carvalho Chehabaccompanied by a message SOL_IP(V6)/IP(V6)_RECVERROR. In this case, 50906bfa47eSMauro Carvalho Chehabthe meaning of the three fields in struct scm_timestamping is 51006bfa47eSMauro Carvalho Chehabimplicitly defined. ts[0] holds a software timestamp if set, ts[1] 51106bfa47eSMauro Carvalho Chehabis again deprecated and ts[2] holds a hardware timestamp if set. 51206bfa47eSMauro Carvalho Chehab 51306bfa47eSMauro Carvalho Chehab 51406bfa47eSMauro Carvalho Chehab3. Hardware Timestamping configuration: SIOCSHWTSTAMP and SIOCGHWTSTAMP 51506bfa47eSMauro Carvalho Chehab======================================================================= 51606bfa47eSMauro Carvalho Chehab 51706bfa47eSMauro Carvalho ChehabHardware time stamping must also be initialized for each device driver 51806bfa47eSMauro Carvalho Chehabthat is expected to do hardware time stamping. The parameter is defined in 51906bfa47eSMauro Carvalho Chehabinclude/uapi/linux/net_tstamp.h as:: 52006bfa47eSMauro Carvalho Chehab 52106bfa47eSMauro Carvalho Chehab struct hwtstamp_config { 52206bfa47eSMauro Carvalho Chehab int flags; /* no flags defined right now, must be zero */ 52306bfa47eSMauro Carvalho Chehab int tx_type; /* HWTSTAMP_TX_* */ 52406bfa47eSMauro Carvalho Chehab int rx_filter; /* HWTSTAMP_FILTER_* */ 52506bfa47eSMauro Carvalho Chehab }; 52606bfa47eSMauro Carvalho Chehab 52706bfa47eSMauro Carvalho ChehabDesired behavior is passed into the kernel and to a specific device by 52806bfa47eSMauro Carvalho Chehabcalling ioctl(SIOCSHWTSTAMP) with a pointer to a struct ifreq whose 52906bfa47eSMauro Carvalho Chehabifr_data points to a struct hwtstamp_config. The tx_type and 53006bfa47eSMauro Carvalho Chehabrx_filter are hints to the driver what it is expected to do. If 53106bfa47eSMauro Carvalho Chehabthe requested fine-grained filtering for incoming packets is not 53206bfa47eSMauro Carvalho Chehabsupported, the driver may time stamp more than just the requested types 53306bfa47eSMauro Carvalho Chehabof packets. 53406bfa47eSMauro Carvalho Chehab 53506bfa47eSMauro Carvalho ChehabDrivers are free to use a more permissive configuration than the requested 53606bfa47eSMauro Carvalho Chehabconfiguration. It is expected that drivers should only implement directly the 53706bfa47eSMauro Carvalho Chehabmost generic mode that can be supported. For example if the hardware can 538cbb91dcbSJakub Kicinskisupport HWTSTAMP_FILTER_PTP_V2_EVENT, then it should generally always upscale 539cbb91dcbSJakub KicinskiHWTSTAMP_FILTER_PTP_V2_L2_SYNC, and so forth, as HWTSTAMP_FILTER_PTP_V2_EVENT 54006bfa47eSMauro Carvalho Chehabis more generic (and more useful to applications). 54106bfa47eSMauro Carvalho Chehab 54206bfa47eSMauro Carvalho ChehabA driver which supports hardware time stamping shall update the struct 54306bfa47eSMauro Carvalho Chehabwith the actual, possibly more permissive configuration. If the 54406bfa47eSMauro Carvalho Chehabrequested packets cannot be time stamped, then nothing should be 54506bfa47eSMauro Carvalho Chehabchanged and ERANGE shall be returned (in contrast to EINVAL, which 54606bfa47eSMauro Carvalho Chehabindicates that SIOCSHWTSTAMP is not supported at all). 54706bfa47eSMauro Carvalho Chehab 54806bfa47eSMauro Carvalho ChehabOnly a processes with admin rights may change the configuration. User 54906bfa47eSMauro Carvalho Chehabspace is responsible to ensure that multiple processes don't interfere 55006bfa47eSMauro Carvalho Chehabwith each other and that the settings are reset. 55106bfa47eSMauro Carvalho Chehab 55206bfa47eSMauro Carvalho ChehabAny process can read the actual configuration by passing this 55306bfa47eSMauro Carvalho Chehabstructure to ioctl(SIOCGHWTSTAMP) in the same way. However, this has 55406bfa47eSMauro Carvalho Chehabnot been implemented in all drivers. 55506bfa47eSMauro Carvalho Chehab 55606bfa47eSMauro Carvalho Chehab:: 55706bfa47eSMauro Carvalho Chehab 55806bfa47eSMauro Carvalho Chehab /* possible values for hwtstamp_config->tx_type */ 55906bfa47eSMauro Carvalho Chehab enum { 56006bfa47eSMauro Carvalho Chehab /* 56106bfa47eSMauro Carvalho Chehab * no outgoing packet will need hardware time stamping; 56206bfa47eSMauro Carvalho Chehab * should a packet arrive which asks for it, no hardware 56306bfa47eSMauro Carvalho Chehab * time stamping will be done 56406bfa47eSMauro Carvalho Chehab */ 56506bfa47eSMauro Carvalho Chehab HWTSTAMP_TX_OFF, 56606bfa47eSMauro Carvalho Chehab 56706bfa47eSMauro Carvalho Chehab /* 56806bfa47eSMauro Carvalho Chehab * enables hardware time stamping for outgoing packets; 56906bfa47eSMauro Carvalho Chehab * the sender of the packet decides which are to be 57006bfa47eSMauro Carvalho Chehab * time stamped by setting SOF_TIMESTAMPING_TX_SOFTWARE 57106bfa47eSMauro Carvalho Chehab * before sending the packet 57206bfa47eSMauro Carvalho Chehab */ 57306bfa47eSMauro Carvalho Chehab HWTSTAMP_TX_ON, 57406bfa47eSMauro Carvalho Chehab }; 57506bfa47eSMauro Carvalho Chehab 57606bfa47eSMauro Carvalho Chehab /* possible values for hwtstamp_config->rx_filter */ 57706bfa47eSMauro Carvalho Chehab enum { 57806bfa47eSMauro Carvalho Chehab /* time stamp no incoming packet at all */ 57906bfa47eSMauro Carvalho Chehab HWTSTAMP_FILTER_NONE, 58006bfa47eSMauro Carvalho Chehab 58106bfa47eSMauro Carvalho Chehab /* time stamp any incoming packet */ 58206bfa47eSMauro Carvalho Chehab HWTSTAMP_FILTER_ALL, 58306bfa47eSMauro Carvalho Chehab 58406bfa47eSMauro Carvalho Chehab /* return value: time stamp all packets requested plus some others */ 58506bfa47eSMauro Carvalho Chehab HWTSTAMP_FILTER_SOME, 58606bfa47eSMauro Carvalho Chehab 58706bfa47eSMauro Carvalho Chehab /* PTP v1, UDP, any kind of event packet */ 58806bfa47eSMauro Carvalho Chehab HWTSTAMP_FILTER_PTP_V1_L4_EVENT, 58906bfa47eSMauro Carvalho Chehab 59006bfa47eSMauro Carvalho Chehab /* for the complete list of values, please check 59106bfa47eSMauro Carvalho Chehab * the include file include/uapi/linux/net_tstamp.h 59206bfa47eSMauro Carvalho Chehab */ 59306bfa47eSMauro Carvalho Chehab }; 59406bfa47eSMauro Carvalho Chehab 59506bfa47eSMauro Carvalho Chehab3.1 Hardware Timestamping Implementation: Device Drivers 59606bfa47eSMauro Carvalho Chehab-------------------------------------------------------- 59706bfa47eSMauro Carvalho Chehab 59806bfa47eSMauro Carvalho ChehabA driver which supports hardware time stamping must support the 59906bfa47eSMauro Carvalho ChehabSIOCSHWTSTAMP ioctl and update the supplied struct hwtstamp_config with 60006bfa47eSMauro Carvalho Chehabthe actual values as described in the section on SIOCSHWTSTAMP. It 60106bfa47eSMauro Carvalho Chehabshould also support SIOCGHWTSTAMP. 60206bfa47eSMauro Carvalho Chehab 60306bfa47eSMauro Carvalho ChehabTime stamps for received packets must be stored in the skb. To get a pointer 60406bfa47eSMauro Carvalho Chehabto the shared time stamp structure of the skb call skb_hwtstamps(). Then 60506bfa47eSMauro Carvalho Chehabset the time stamps in the structure:: 60606bfa47eSMauro Carvalho Chehab 60706bfa47eSMauro Carvalho Chehab struct skb_shared_hwtstamps { 60806bfa47eSMauro Carvalho Chehab /* hardware time stamp transformed into duration 60906bfa47eSMauro Carvalho Chehab * since arbitrary point in time 61006bfa47eSMauro Carvalho Chehab */ 61106bfa47eSMauro Carvalho Chehab ktime_t hwtstamp; 61206bfa47eSMauro Carvalho Chehab }; 61306bfa47eSMauro Carvalho Chehab 61406bfa47eSMauro Carvalho ChehabTime stamps for outgoing packets are to be generated as follows: 61506bfa47eSMauro Carvalho Chehab 61606bfa47eSMauro Carvalho Chehab- In hard_start_xmit(), check if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) 61706bfa47eSMauro Carvalho Chehab is set no-zero. If yes, then the driver is expected to do hardware time 61806bfa47eSMauro Carvalho Chehab stamping. 61906bfa47eSMauro Carvalho Chehab- If this is possible for the skb and requested, then declare 62006bfa47eSMauro Carvalho Chehab that the driver is doing the time stamping by setting the flag 62106bfa47eSMauro Carvalho Chehab SKBTX_IN_PROGRESS in skb_shinfo(skb)->tx_flags , e.g. with:: 62206bfa47eSMauro Carvalho Chehab 62306bfa47eSMauro Carvalho Chehab skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 62406bfa47eSMauro Carvalho Chehab 62506bfa47eSMauro Carvalho Chehab You might want to keep a pointer to the associated skb for the next step 62606bfa47eSMauro Carvalho Chehab and not free the skb. A driver not supporting hardware time stamping doesn't 62706bfa47eSMauro Carvalho Chehab do that. A driver must never touch sk_buff::tstamp! It is used to store 62806bfa47eSMauro Carvalho Chehab software generated time stamps by the network subsystem. 62906bfa47eSMauro Carvalho Chehab- Driver should call skb_tx_timestamp() as close to passing sk_buff to hardware 63006bfa47eSMauro Carvalho Chehab as possible. skb_tx_timestamp() provides a software time stamp if requested 63106bfa47eSMauro Carvalho Chehab and hardware timestamping is not possible (SKBTX_IN_PROGRESS not set). 63206bfa47eSMauro Carvalho Chehab- As soon as the driver has sent the packet and/or obtained a 63306bfa47eSMauro Carvalho Chehab hardware time stamp for it, it passes the time stamp back by 634a9725e1dSWillem de Bruijn calling skb_tstamp_tx() with the original skb, the raw 635a9725e1dSWillem de Bruijn hardware time stamp. skb_tstamp_tx() clones the original skb and 63606bfa47eSMauro Carvalho Chehab adds the timestamps, therefore the original skb has to be freed now. 63706bfa47eSMauro Carvalho Chehab If obtaining the hardware time stamp somehow fails, then the driver 63806bfa47eSMauro Carvalho Chehab should not fall back to software time stamping. The rationale is that 63906bfa47eSMauro Carvalho Chehab this would occur at a later time in the processing pipeline than other 64006bfa47eSMauro Carvalho Chehab software time stamping and therefore could lead to unexpected deltas 64106bfa47eSMauro Carvalho Chehab between time stamps. 64294d9f78fSVladimir Oltean 64394d9f78fSVladimir Oltean3.2 Special considerations for stacked PTP Hardware Clocks 64494d9f78fSVladimir Oltean---------------------------------------------------------- 64594d9f78fSVladimir Oltean 64694d9f78fSVladimir OlteanThere are situations when there may be more than one PHC (PTP Hardware Clock) 64794d9f78fSVladimir Olteanin the data path of a packet. The kernel has no explicit mechanism to allow the 64894d9f78fSVladimir Olteanuser to select which PHC to use for timestamping Ethernet frames. Instead, the 64994d9f78fSVladimir Olteanassumption is that the outermost PHC is always the most preferable, and that 65094d9f78fSVladimir Olteankernel drivers collaborate towards achieving that goal. Currently there are 3 65194d9f78fSVladimir Olteancases of stacked PHCs, detailed below: 65294d9f78fSVladimir Oltean 65394d9f78fSVladimir Oltean3.2.1 DSA (Distributed Switch Architecture) switches 65494d9f78fSVladimir Oltean^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 65594d9f78fSVladimir Oltean 65694d9f78fSVladimir OlteanThese are Ethernet switches which have one of their ports connected to an 65794d9f78fSVladimir Oltean(otherwise completely unaware) host Ethernet interface, and perform the role of 65894d9f78fSVladimir Olteana port multiplier with optional forwarding acceleration features. Each DSA 65994d9f78fSVladimir Olteanswitch port is visible to the user as a standalone (virtual) network interface, 66094d9f78fSVladimir Olteanand its network I/O is performed, under the hood, indirectly through the host 66194d9f78fSVladimir Olteaninterface (redirecting to the host port on TX, and intercepting frames on RX). 66294d9f78fSVladimir Oltean 66394d9f78fSVladimir OlteanWhen a DSA switch is attached to a host port, PTP synchronization has to 66494d9f78fSVladimir Olteansuffer, since the switch's variable queuing delay introduces a path delay 66594d9f78fSVladimir Olteanjitter between the host port and its PTP partner. For this reason, some DSA 66694d9f78fSVladimir Olteanswitches include a timestamping clock of their own, and have the ability to 66794d9f78fSVladimir Olteanperform network timestamping on their own MAC, such that path delays only 66894d9f78fSVladimir Olteanmeasure wire and PHY propagation latencies. Timestamping DSA switches are 66994d9f78fSVladimir Olteansupported in Linux and expose the same ABI as any other network interface (save 67094d9f78fSVladimir Olteanfor the fact that the DSA interfaces are in fact virtual in terms of network 67194d9f78fSVladimir OlteanI/O, they do have their own PHC). It is typical, but not mandatory, for all 67294d9f78fSVladimir Olteaninterfaces of a DSA switch to share the same PHC. 67394d9f78fSVladimir Oltean 67494d9f78fSVladimir OlteanBy design, PTP timestamping with a DSA switch does not need any special 67594d9f78fSVladimir Olteanhandling in the driver for the host port it is attached to. However, when the 67694d9f78fSVladimir Olteanhost port also supports PTP timestamping, DSA will take care of intercepting 677a7605370SArnd Bergmannthe ``.ndo_eth_ioctl`` calls towards the host port, and block attempts to enable 67894d9f78fSVladimir Olteanhardware timestamping on it. This is because the SO_TIMESTAMPING API does not 67994d9f78fSVladimir Olteanallow the delivery of multiple hardware timestamps for the same packet, so 68094d9f78fSVladimir Olteananybody else except for the DSA switch port must be prevented from doing so. 68194d9f78fSVladimir Oltean 682d150946eSYangbo LuIn the generic layer, DSA provides the following infrastructure for PTP 683d150946eSYangbo Lutimestamping: 68494d9f78fSVladimir Oltean 685d150946eSYangbo Lu- ``.port_txtstamp()``: a hook called prior to the transmission of 686d150946eSYangbo Lu packets with a hardware TX timestamping request from user space. 687d150946eSYangbo Lu This is required for two-step timestamping, since the hardware 688d150946eSYangbo Lu timestamp becomes available after the actual MAC transmission, so the 689d150946eSYangbo Lu driver must be prepared to correlate the timestamp with the original 690d150946eSYangbo Lu packet so that it can re-enqueue the packet back into the socket's 691d150946eSYangbo Lu error queue. To save the packet for when the timestamp becomes 692d150946eSYangbo Lu available, the driver can call ``skb_clone_sk`` , save the clone pointer 693d150946eSYangbo Lu in skb->cb and enqueue a tx skb queue. Typically, a switch will have a 694d150946eSYangbo Lu PTP TX timestamp register (or sometimes a FIFO) where the timestamp 695d150946eSYangbo Lu becomes available. In case of a FIFO, the hardware might store 696d150946eSYangbo Lu key-value pairs of PTP sequence ID/message type/domain number and the 697d150946eSYangbo Lu actual timestamp. To perform the correlation correctly between the 698d150946eSYangbo Lu packets in a queue waiting for timestamping and the actual timestamps, 699d150946eSYangbo Lu drivers can use a BPF classifier (``ptp_classify_raw``) to identify 700d150946eSYangbo Lu the PTP transport type, and ``ptp_parse_header`` to interpret the PTP 701d150946eSYangbo Lu header fields. There may be an IRQ that is raised upon this 702d150946eSYangbo Lu timestamp's availability, or the driver might have to poll after 703d150946eSYangbo Lu invoking ``dev_queue_xmit()`` towards the host interface. 704d150946eSYangbo Lu One-step TX timestamping do not require packet cloning, since there is 705d150946eSYangbo Lu no follow-up message required by the PTP protocol (because the 706d150946eSYangbo Lu TX timestamp is embedded into the packet by the MAC), and therefore 707d150946eSYangbo Lu user space does not expect the packet annotated with the TX timestamp 708d150946eSYangbo Lu to be re-enqueued into its socket's error queue. 70994d9f78fSVladimir Oltean 710d150946eSYangbo Lu- ``.port_rxtstamp()``: On RX, the BPF classifier is run by DSA to 711d150946eSYangbo Lu identify PTP event messages (any other packets, including PTP general 712d150946eSYangbo Lu messages, are not timestamped). The original (and only) timestampable 713d150946eSYangbo Lu skb is provided to the driver, for it to annotate it with a timestamp, 714d150946eSYangbo Lu if that is immediately available, or defer to later. On reception, 715d150946eSYangbo Lu timestamps might either be available in-band (through metadata in the 716d150946eSYangbo Lu DSA header, or attached in other ways to the packet), or out-of-band 717d150946eSYangbo Lu (through another RX timestamping FIFO). Deferral on RX is typically 718d150946eSYangbo Lu necessary when retrieving the timestamp needs a sleepable context. In 719d150946eSYangbo Lu that case, it is the responsibility of the DSA driver to call 72021f95a88SSebastian Andrzej Siewior ``netif_rx()`` on the freshly timestamped skb. 72194d9f78fSVladimir Oltean 72294d9f78fSVladimir Oltean3.2.2 Ethernet PHYs 72394d9f78fSVladimir Oltean^^^^^^^^^^^^^^^^^^^ 72494d9f78fSVladimir Oltean 72594d9f78fSVladimir OlteanThese are devices that typically fulfill a Layer 1 role in the network stack, 72694d9f78fSVladimir Olteanhence they do not have a representation in terms of a network interface as DSA 72794d9f78fSVladimir Olteanswitches do. However, PHYs may be able to detect and timestamp PTP packets, for 72894d9f78fSVladimir Olteanperformance reasons: timestamps taken as close as possible to the wire have the 72994d9f78fSVladimir Olteanpotential to yield a more stable and precise synchronization. 73094d9f78fSVladimir Oltean 73194d9f78fSVladimir OlteanA PHY driver that supports PTP timestamping must create a ``struct 73294d9f78fSVladimir Olteanmii_timestamper`` and add a pointer to it in ``phydev->mii_ts``. The presence 73394d9f78fSVladimir Olteanof this pointer will be checked by the networking stack. 73494d9f78fSVladimir Oltean 73594d9f78fSVladimir OlteanSince PHYs do not have network interface representations, the timestamping and 73694d9f78fSVladimir Olteanethtool ioctl operations for them need to be mediated by their respective MAC 73794d9f78fSVladimir Olteandriver. Therefore, as opposed to DSA switches, modifications need to be done 73894d9f78fSVladimir Olteanto each individual MAC driver for PHY timestamping support. This entails: 73994d9f78fSVladimir Oltean 740a7605370SArnd Bergmann- Checking, in ``.ndo_eth_ioctl``, whether ``phy_has_hwtstamp(netdev->phydev)`` 74194d9f78fSVladimir Oltean is true or not. If it is, then the MAC driver should not process this request 74294d9f78fSVladimir Oltean but instead pass it on to the PHY using ``phy_mii_ioctl()``. 74394d9f78fSVladimir Oltean 74494d9f78fSVladimir Oltean- On RX, special intervention may or may not be needed, depending on the 74594d9f78fSVladimir Oltean function used to deliver skb's up the network stack. In the case of plain 74694d9f78fSVladimir Oltean ``netif_rx()`` and similar, MAC drivers must check whether 74794d9f78fSVladimir Oltean ``skb_defer_rx_timestamp(skb)`` is necessary or not - and if it is, don't 74894d9f78fSVladimir Oltean call ``netif_rx()`` at all. If ``CONFIG_NETWORK_PHY_TIMESTAMPING`` is 74994d9f78fSVladimir Oltean enabled, and ``skb->dev->phydev->mii_ts`` exists, its ``.rxtstamp()`` hook 75094d9f78fSVladimir Oltean will be called now, to determine, using logic very similar to DSA, whether 75194d9f78fSVladimir Oltean deferral for RX timestamping is necessary. Again like DSA, it becomes the 75294d9f78fSVladimir Oltean responsibility of the PHY driver to send the packet up the stack when the 75394d9f78fSVladimir Oltean timestamp is available. 75494d9f78fSVladimir Oltean 75594d9f78fSVladimir Oltean For other skb receive functions, such as ``napi_gro_receive`` and 75694d9f78fSVladimir Oltean ``netif_receive_skb``, the stack automatically checks whether 75794d9f78fSVladimir Oltean ``skb_defer_rx_timestamp()`` is necessary, so this check is not needed inside 75894d9f78fSVladimir Oltean the driver. 75994d9f78fSVladimir Oltean 76094d9f78fSVladimir Oltean- On TX, again, special intervention might or might not be needed. The 76194d9f78fSVladimir Oltean function that calls the ``mii_ts->txtstamp()`` hook is named 76294d9f78fSVladimir Oltean ``skb_clone_tx_timestamp()``. This function can either be called directly 76394d9f78fSVladimir Oltean (case in which explicit MAC driver support is indeed needed), but the 76494d9f78fSVladimir Oltean function also piggybacks from the ``skb_tx_timestamp()`` call, which many MAC 76594d9f78fSVladimir Oltean drivers already perform for software timestamping purposes. Therefore, if a 76694d9f78fSVladimir Oltean MAC supports software timestamping, it does not need to do anything further 76794d9f78fSVladimir Oltean at this stage. 76894d9f78fSVladimir Oltean 76994d9f78fSVladimir Oltean3.2.3 MII bus snooping devices 77094d9f78fSVladimir Oltean^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 77194d9f78fSVladimir Oltean 77294d9f78fSVladimir OlteanThese perform the same role as timestamping Ethernet PHYs, save for the fact 77394d9f78fSVladimir Olteanthat they are discrete devices and can therefore be used in conjunction with 77494d9f78fSVladimir Olteanany PHY even if it doesn't support timestamping. In Linux, they are 77594d9f78fSVladimir Olteandiscoverable and attachable to a ``struct phy_device`` through Device Tree, and 77694d9f78fSVladimir Olteanfor the rest, they use the same mii_ts infrastructure as those. See 77794d9f78fSVladimir OlteanDocumentation/devicetree/bindings/ptp/timestamper.txt for more details. 77894d9f78fSVladimir Oltean 77994d9f78fSVladimir Oltean3.2.4 Other caveats for MAC drivers 78094d9f78fSVladimir Oltean^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 78194d9f78fSVladimir Oltean 78294d9f78fSVladimir OlteanStacked PHCs, especially DSA (but not only) - since that doesn't require any 78394d9f78fSVladimir Olteanmodification to MAC drivers, so it is more difficult to ensure correctness of 78494d9f78fSVladimir Olteanall possible code paths - is that they uncover bugs which were impossible to 78594d9f78fSVladimir Olteantrigger before the existence of stacked PTP clocks. One example has to do with 78694d9f78fSVladimir Olteanthis line of code, already presented earlier:: 78794d9f78fSVladimir Oltean 78894d9f78fSVladimir Oltean skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 78994d9f78fSVladimir Oltean 79094d9f78fSVladimir OlteanAny TX timestamping logic, be it a plain MAC driver, a DSA switch driver, a PHY 79194d9f78fSVladimir Olteandriver or a MII bus snooping device driver, should set this flag. 79294d9f78fSVladimir OlteanBut a MAC driver that is unaware of PHC stacking might get tripped up by 79394d9f78fSVladimir Olteansomebody other than itself setting this flag, and deliver a duplicate 79494d9f78fSVladimir Olteantimestamp. 79594d9f78fSVladimir OlteanFor example, a typical driver design for TX timestamping might be to split the 79694d9f78fSVladimir Olteantransmission part into 2 portions: 79794d9f78fSVladimir Oltean 79894d9f78fSVladimir Oltean1. "TX": checks whether PTP timestamping has been previously enabled through 799a7605370SArnd Bergmann the ``.ndo_eth_ioctl`` ("``priv->hwtstamp_tx_enabled == true``") and the 80094d9f78fSVladimir Oltean current skb requires a TX timestamp ("``skb_shinfo(skb)->tx_flags & 80194d9f78fSVladimir Oltean SKBTX_HW_TSTAMP``"). If this is true, it sets the 80294d9f78fSVladimir Oltean "``skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS``" flag. Note: as 80394d9f78fSVladimir Oltean described above, in the case of a stacked PHC system, this condition should 80494d9f78fSVladimir Oltean never trigger, as this MAC is certainly not the outermost PHC. But this is 80594d9f78fSVladimir Oltean not where the typical issue is. Transmission proceeds with this packet. 80694d9f78fSVladimir Oltean 80794d9f78fSVladimir Oltean2. "TX confirmation": Transmission has finished. The driver checks whether it 80894d9f78fSVladimir Oltean is necessary to collect any TX timestamp for it. Here is where the typical 80994d9f78fSVladimir Oltean issues are: the MAC driver takes a shortcut and only checks whether 81094d9f78fSVladimir Oltean "``skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS``" was set. With a stacked 81194d9f78fSVladimir Oltean PHC system, this is incorrect because this MAC driver is not the only entity 81294d9f78fSVladimir Oltean in the TX data path who could have enabled SKBTX_IN_PROGRESS in the first 81394d9f78fSVladimir Oltean place. 81494d9f78fSVladimir Oltean 81594d9f78fSVladimir OlteanThe correct solution for this problem is for MAC drivers to have a compound 81694d9f78fSVladimir Olteancheck in their "TX confirmation" portion, not only for 81794d9f78fSVladimir Oltean"``skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS``", but also for 81894d9f78fSVladimir Oltean"``priv->hwtstamp_tx_enabled == true``". Because the rest of the system ensures 81994d9f78fSVladimir Olteanthat PTP timestamping is not enabled for anything other than the outermost PHC, 82094d9f78fSVladimir Olteanthis enhanced check will avoid delivering a duplicated TX timestamp to user 82194d9f78fSVladimir Olteanspace. 822