← Documents Documentation/networking/timestamping.rst GitHub 원문 ↗

Linux 6.18.37 · Networking

Network timestamping

Linux network software·hardware TX/RX timestamping의 socket API, data 반환과 driver·stacked PHC 구현 규칙입니다.

Source pathDocumentation/networking/timestamping.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

timestamping.rst:1-851

Linux timestamping은 generation 지점, reporting 형식과 반환 queue를 분리합니다. `SO_TIMESTAMPING` bitmap으로 software·hardware RX/TX와 scheduler·ACK·completion 시점을 선택하고, TX는 error queue, RX는 일반 `recvmsg()` ancillary data로 읽습니다.

Hardware timestamp는 netlink tsconfig와 driver `hwtstamp_config`가 device 전체 동작을 정하고 packet별 request는 skb flag가 전달합니다. DSA·PHY·MII snooper처럼 PHC가 겹치면 wire에 가장 가까운 outermost clock만 값을 제공하도록 driver가 협력해야 합니다.

Timestamp 전체 경로
setsockopt/cmsg generation requestskb timestamp pointSoftware 또는 hardware clockSCM_TIMESTAMPINGTX error queue / RX normal recvmsg
ETHTOOL_MSG_TSCONFIG_SEThwtstamp_configOutermost PHCDriver skb annotation

Control plane 설정부터 userspace report까지 연결합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. SPDX-License-Identifier: GPL-2.0
2
3 ============
4 Timestamping
5 ============
6
7
8 1. Control Interfaces
9 =====================
10
11 The interfaces for receiving network packages timestamps are:
12
13 SO_TIMESTAMP
14 Generates a timestamp for each incoming packet in (not necessarily
15 monotonic) system time. Reports the timestamp via recvmsg() in a
16 control message in usec resolution.
17 SO_TIMESTAMP is defined as SO_TIMESTAMP_NEW or SO_TIMESTAMP_OLD
18 based on the architecture type and time_t representation of libc.
19 Control message format is in struct __kernel_old_timeval for
20 SO_TIMESTAMP_OLD and in struct __kernel_sock_timeval for
21 SO_TIMESTAMP_NEW options respectively.
22
23 SO_TIMESTAMPNS
24 Same timestamping mechanism as SO_TIMESTAMP, but reports the
25 timestamp as struct timespec in nsec resolution.
26 SO_TIMESTAMPNS is defined as SO_TIMESTAMPNS_NEW or SO_TIMESTAMPNS_OLD
27 based on the architecture type and time_t representation of libc.
28 Control message format is in struct timespec for SO_TIMESTAMPNS_OLD
29 and in struct __kernel_timespec for SO_TIMESTAMPNS_NEW options
30 respectively.
31
32 IP_MULTICAST_LOOP + SO_TIMESTAMP[NS]
33 Only for multicast:approximate transmit timestamp obtained by
34 reading the looped packet receive timestamp.
35
36 SO_TIMESTAMPING
37 Generates timestamps on reception, transmission or both. Supports
38 multiple timestamp sources, including hardware. Supports generating
39 timestamps for stream sockets.
40
41
42 1.1 SO_TIMESTAMP (also SO_TIMESTAMP_OLD and SO_TIMESTAMP_NEW)
43 -------------------------------------------------------------
44
45 This socket option enables timestamping of datagrams on the reception
46 path. Because the destination socket, if any, is not known early in
47 the network stack, the feature has to be enabled for all packets. The
48 same is true for all early receive timestamp options.
49
50 For interface details, see `man 7 socket`.
51
52 Always use SO_TIMESTAMP_NEW timestamp to always get timestamp in
53 struct __kernel_sock_timeval format.
54
55 SO_TIMESTAMP_OLD returns incorrect timestamps after the year 2038
56 on 32 bit machines.
57
58 1.2 SO_TIMESTAMPNS (also SO_TIMESTAMPNS_OLD and SO_TIMESTAMPNS_NEW)
59 -------------------------------------------------------------------
60
61 This option is identical to SO_TIMESTAMP except for the returned data type.
62 Its struct timespec allows for higher resolution (ns) timestamps than the
63 timeval of SO_TIMESTAMP (ms).
64
65 Always use SO_TIMESTAMPNS_NEW timestamp to always get timestamp in
66 struct __kernel_timespec format.
67
68 SO_TIMESTAMPNS_OLD returns incorrect timestamps after the year 2038
69 on 32 bit machines.
70
71 1.3 SO_TIMESTAMPING (also SO_TIMESTAMPING_OLD and SO_TIMESTAMPING_NEW)
72 ----------------------------------------------------------------------
73
74 Supports multiple types of timestamp requests. As a result, this
75 socket option takes a bitmap of flags, not a boolean. In::
76
77 err = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val));
78
79 val is an integer with any of the following bits set. Setting other
80 bit returns EINVAL and does not change the current state.
81
82 The socket option configures timestamp generation for individual
83 sk_buffs (1.3.1), timestamp reporting to the socket's error
84 queue (1.3.2) and options (1.3.3). Timestamp generation can also
85 be enabled for individual sendmsg calls using cmsg (1.3.4).
86
87
88 1.3.1 Timestamp Generation
89 ^^^^^^^^^^^^^^^^^^^^^^^^^^
90
91 Some bits are requests to the stack to try to generate timestamps. Any
92 combination of them is valid. Changes to these bits apply to newly
93 created packets, not to packets already in the stack. As a result, it
94 is possible to selectively request timestamps for a subset of packets
95 (e.g., for sampling) by embedding an send() call within two setsockopt
96 calls, one to enable timestamp generation and one to disable it.
97 Timestamps may also be generated for reasons other than being
98 requested by a particular socket, such as when receive timestamping is
99 enabled system wide, as explained earlier.
100
101 SOF_TIMESTAMPING_RX_HARDWARE:
102 Request rx timestamps generated by the network adapter.
103
104 SOF_TIMESTAMPING_RX_SOFTWARE:
105 Request rx timestamps when data enters the kernel. These timestamps
106 are generated just after a device driver hands a packet to the
107 kernel receive stack.
108
109 SOF_TIMESTAMPING_TX_HARDWARE:
110 Request tx timestamps generated by the network adapter. This flag
111 can be enabled via both socket options and control messages.
112
113 SOF_TIMESTAMPING_TX_SOFTWARE:
114 Request tx timestamps when data leaves the kernel. These timestamps
115 are generated in the device driver as close as possible, but always
116 prior to, passing the packet to the network interface. Hence, they
117 require driver support and may not be available for all devices.
118 This flag can be enabled via both socket options and control messages.
119
120 SOF_TIMESTAMPING_TX_SCHED:
121 Request tx timestamps prior to entering the packet scheduler. Kernel
122 transmit latency is, if long, often dominated by queuing delay. The
123 difference between this timestamp and one taken at
124 SOF_TIMESTAMPING_TX_SOFTWARE will expose this latency independent
125 of protocol processing. The latency incurred in protocol
126 processing, if any, can be computed by subtracting a userspace
127 timestamp taken immediately before send() from this timestamp. On
128 machines with virtual devices where a transmitted packet travels
129 through multiple devices and, hence, multiple packet schedulers,
130 a timestamp is generated at each layer. This allows for fine
131 grained measurement of queuing delay. This flag can be enabled
132 via both socket options and control messages.
133
134 SOF_TIMESTAMPING_TX_ACK:
135 Request tx timestamps when all data in the send buffer has been
136 acknowledged. This only makes sense for reliable protocols. It is
137 currently only implemented for TCP. For that protocol, it may
138 over-report measurement, because the timestamp is generated when all
139 data up to and including the buffer at send() was acknowledged: the
140 cumulative acknowledgment. The mechanism ignores SACK and FACK.
141 This flag can be enabled via both socket options and control messages.
142
143 SOF_TIMESTAMPING_TX_COMPLETION:
144 Request tx timestamps on packet tx completion. The completion
145 timestamp is generated by the kernel when it receives packet a
146 completion report from the hardware. Hardware may report multiple
147 packets at once, and completion timestamps reflect the timing of the
148 report and not actual tx time. This flag can be enabled via both
149 socket options and control messages.
150
151
152 1.3.2 Timestamp Reporting
153 ^^^^^^^^^^^^^^^^^^^^^^^^^
154
155 The other three bits control which timestamps will be reported in a
156 generated control message. Changes to the bits take immediate
157 effect at the timestamp reporting locations in the stack. Timestamps
158 are only reported for packets that also have the relevant timestamp
159 generation request set.
160
161 SOF_TIMESTAMPING_SOFTWARE:
162 Report any software timestamps when available.
163
164 SOF_TIMESTAMPING_SYS_HARDWARE:
165 This option is deprecated and ignored.
166
167 SOF_TIMESTAMPING_RAW_HARDWARE:
168 Report hardware timestamps as generated by
169 SOF_TIMESTAMPING_TX_HARDWARE or SOF_TIMESTAMPING_RX_HARDWARE
170 when available.
171
172
173 1.3.3 Timestamp Options
174 ^^^^^^^^^^^^^^^^^^^^^^^
175
176 The interface supports the options
177
178 SOF_TIMESTAMPING_OPT_ID:
179 Generate a unique identifier along with each packet. A process can
180 have multiple concurrent timestamping requests outstanding. Packets
181 can be reordered in the transmit path, for instance in the packet
182 scheduler. In that case timestamps will be queued onto the error
183 queue out of order from the original send() calls. It is not always
184 possible to uniquely match timestamps to the original send() calls
185 based on timestamp order or payload inspection alone, then.
186
187 This option associates each packet at send() with a unique
188 identifier and returns that along with the timestamp. The identifier
189 is derived from a per-socket u32 counter (that wraps). For datagram
190 sockets, the counter increments with each sent packet. For stream
191 sockets, it increments with every byte. For stream sockets, also set
192 SOF_TIMESTAMPING_OPT_ID_TCP, see the section below.
193
194 The counter starts at zero. It is initialized the first time that
195 the socket option is enabled. It is reset each time the option is
196 enabled after having been disabled. Resetting the counter does not
197 change the identifiers of existing packets in the system.
198
199 This option is implemented only for transmit timestamps. There, the
200 timestamp is always looped along with a struct sock_extended_err.
201 The option modifies field ee_data to pass an id that is unique
202 among all possibly concurrently outstanding timestamp requests for
203 that socket.
204
205 The process can optionally override the default generated ID, by
206 passing a specific ID with control message SCM_TS_OPT_ID (not
207 supported for TCP sockets)::
208
209 struct msghdr *msg;
210 ...
211 cmsg = CMSG_FIRSTHDR(msg);
212 cmsg->cmsg_level = SOL_SOCKET;
213 cmsg->cmsg_type = SCM_TS_OPT_ID;
214 cmsg->cmsg_len = CMSG_LEN(sizeof(__u32));
215 *((__u32 *) CMSG_DATA(cmsg)) = opt_id;
216 err = sendmsg(fd, msg, 0);
217
218
219 SOF_TIMESTAMPING_OPT_ID_TCP:
220 Pass this modifier along with SOF_TIMESTAMPING_OPT_ID for new TCP
221 timestamping applications. SOF_TIMESTAMPING_OPT_ID defines how the
222 counter increments for stream sockets, but its starting point is
223 not entirely trivial. This option fixes that.
224
225 For stream sockets, if SOF_TIMESTAMPING_OPT_ID is set, this should
226 always be set too. On datagram sockets the option has no effect.
227
228 A reasonable expectation is that the counter is reset to zero with
229 the system call, so that a subsequent write() of N bytes generates
230 a timestamp with counter N-1. SOF_TIMESTAMPING_OPT_ID_TCP
231 implements this behavior under all conditions.
232
233 SOF_TIMESTAMPING_OPT_ID without modifier often reports the same,
234 especially when the socket option is set when no data is in
235 transmission. If data is being transmitted, it may be off by the
236 length of the output queue (SIOCOUTQ).
237
238 The difference is due to being based on snd_una versus write_seq.
239 snd_una is the offset in the stream acknowledged by the peer. This
240 depends on factors outside of process control, such as network RTT.
241 write_seq is the last byte written by the process. This offset is
242 not affected by external inputs.
243
244 The difference is subtle and unlikely to be noticed when configured
245 at initial socket creation, when no data is queued or sent. But
246 SOF_TIMESTAMPING_OPT_ID_TCP behavior is more robust regardless of
247 when the socket option is set.
248
249 SOF_TIMESTAMPING_OPT_CMSG:
250 Support recv() cmsg for all timestamped packets. Control messages
251 are already supported unconditionally on all packets with receive
252 timestamps and on IPv6 packets with transmit timestamp. This option
253 extends them to IPv4 packets with transmit timestamp. One use case
254 is to correlate packets with their egress device, by enabling socket
255 option IP_PKTINFO simultaneously.
256
257
258 SOF_TIMESTAMPING_OPT_TSONLY:
259 Applies to transmit timestamps only. Makes the kernel return the
260 timestamp as a cmsg alongside an empty packet, as opposed to
261 alongside the original packet. This reduces the amount of memory
262 charged to the socket's receive budget (SO_RCVBUF) and delivers
263 the timestamp even if sysctl net.core.tstamp_allow_data is 0.
264 This option disables SOF_TIMESTAMPING_OPT_CMSG.
265
266 SOF_TIMESTAMPING_OPT_STATS:
267 Optional stats that are obtained along with the transmit timestamps.
268 It must be used together with SOF_TIMESTAMPING_OPT_TSONLY. When the
269 transmit timestamp is available, the stats are available in a
270 separate control message of type SCM_TIMESTAMPING_OPT_STATS, as a
271 list of TLVs (struct nlattr) of types. These stats allow the
272 application to associate various transport layer stats with
273 the transmit timestamps, such as how long a certain block of
274 data was limited by peer's receiver window.
275
276 SOF_TIMESTAMPING_OPT_PKTINFO:
277 Enable the SCM_TIMESTAMPING_PKTINFO control message for incoming
278 packets with hardware timestamps. The message contains struct
279 scm_ts_pktinfo, which supplies the index of the real interface which
280 received the packet and its length at layer 2. A valid (non-zero)
281 interface index will be returned only if CONFIG_NET_RX_BUSY_POLL is
282 enabled and the driver is using NAPI. The struct contains also two
283 other fields, but they are reserved and undefined.
284
285 SOF_TIMESTAMPING_OPT_TX_SWHW:
286 Request both hardware and software timestamps for outgoing packets
287 when SOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_TX_SOFTWARE
288 are enabled at the same time. If both timestamps are generated,
289 two separate messages will be looped to the socket's error queue,
290 each containing just one timestamp.
291
292 SOF_TIMESTAMPING_OPT_RX_FILTER:
293 Filter out spurious receive timestamps: report a receive timestamp
294 only if the matching timestamp generation flag is enabled.
295
296 Receive timestamps are generated early in the ingress path, before a
297 packet's destination socket is known. If any socket enables receive
298 timestamps, packets for all socket will receive timestamped packets.
299 Including those that request timestamp reporting with
300 SOF_TIMESTAMPING_SOFTWARE and/or SOF_TIMESTAMPING_RAW_HARDWARE, but
301 do not request receive timestamp generation. This can happen when
302 requesting transmit timestamps only.
303
304 Receiving spurious timestamps is generally benign. A process can
305 ignore the unexpected non-zero value. But it makes behavior subtly
306 dependent on other sockets. This flag isolates the socket for more
307 deterministic behavior.
308
309 New applications are encouraged to pass SOF_TIMESTAMPING_OPT_ID to
310 disambiguate timestamps and SOF_TIMESTAMPING_OPT_TSONLY to operate
311 regardless of the setting of sysctl net.core.tstamp_allow_data.
312
313 An exception is when a process needs additional cmsg data, for
314 instance SOL_IP/IP_PKTINFO to detect the egress network interface.
315 Then pass option SOF_TIMESTAMPING_OPT_CMSG. This option depends on
316 having access to the contents of the original packet, so cannot be
317 combined with SOF_TIMESTAMPING_OPT_TSONLY.
318
319
320 1.3.4. Enabling timestamps via control messages
321 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
322
323 In addition to socket options, timestamp generation can be requested
324 per write via cmsg, only for SOF_TIMESTAMPING_TX_* (see Section 1.3.1).
325 Using this feature, applications can sample timestamps per sendmsg()
326 without paying the overhead of enabling and disabling timestamps via
327 setsockopt::
328
329 struct msghdr *msg;
330 ...
331 cmsg = CMSG_FIRSTHDR(msg);
332 cmsg->cmsg_level = SOL_SOCKET;
333 cmsg->cmsg_type = SO_TIMESTAMPING;
334 cmsg->cmsg_len = CMSG_LEN(sizeof(__u32));
335 *((__u32 *) CMSG_DATA(cmsg)) = SOF_TIMESTAMPING_TX_SCHED |
336 SOF_TIMESTAMPING_TX_SOFTWARE |
337 SOF_TIMESTAMPING_TX_ACK;
338 err = sendmsg(fd, msg, 0);
339
340 The SOF_TIMESTAMPING_TX_* flags set via cmsg will override
341 the SOF_TIMESTAMPING_TX_* flags set via setsockopt.
342
343 Moreover, applications must still enable timestamp reporting via
344 setsockopt to receive timestamps::
345
346 __u32 val = SOF_TIMESTAMPING_SOFTWARE |
347 SOF_TIMESTAMPING_OPT_ID /* or any other flag */;
348 err = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val));
349
350
351 1.4 Bytestream Timestamps
352 -------------------------
353
354 The SO_TIMESTAMPING interface supports timestamping of bytes in a
355 bytestream. Each request is interpreted as a request for when the
356 entire contents of the buffer has passed a timestamping point. That
357 is, for streams option SOF_TIMESTAMPING_TX_SOFTWARE will record
358 when all bytes have reached the device driver, regardless of how
359 many packets the data has been converted into.
360
361 In general, bytestreams have no natural delimiters and therefore
362 correlating a timestamp with data is non-trivial. A range of bytes
363 may be split across segments, any segments may be merged (possibly
364 coalescing sections of previously segmented buffers associated with
365 independent send() calls). Segments can be reordered and the same
366 byte range can coexist in multiple segments for protocols that
367 implement retransmissions.
368
369 It is essential that all timestamps implement the same semantics,
370 regardless of these possible transformations, as otherwise they are
371 incomparable. Handling "rare" corner cases differently from the
372 simple case (a 1:1 mapping from buffer to skb) is insufficient
373 because performance debugging often needs to focus on such outliers.
374
375 In practice, timestamps can be correlated with segments of a
376 bytestream consistently, if both semantics of the timestamp and the
377 timing of measurement are chosen correctly. This challenge is no
378 different from deciding on a strategy for IP fragmentation. There, the
379 definition is that only the first fragment is timestamped. For
380 bytestreams, we chose that a timestamp is generated only when all
381 bytes have passed a point. SOF_TIMESTAMPING_TX_ACK as defined is easy to
382 implement and reason about. An implementation that has to take into
383 account SACK would be more complex due to possible transmission holes
384 and out of order arrival.
385
386 On the host, TCP can also break the simple 1:1 mapping from buffer to
387 skbuff as a result of Nagle, cork, autocork, segmentation and GSO. The
388 implementation ensures correctness in all cases by tracking the
389 individual last byte passed to send(), even if it is no longer the
390 last byte after an skbuff extend or merge operation. It stores the
391 relevant sequence number in skb_shinfo(skb)->tskey. Because an skbuff
392 has only one such field, only one timestamp can be generated.
393
394 In rare cases, a timestamp request can be missed if two requests are
395 collapsed onto the same skb. A process can detect this situation by
396 enabling SOF_TIMESTAMPING_OPT_ID and comparing the byte offset at
397 send time with the value returned for each timestamp. It can prevent
398 the situation by always flushing the TCP stack in between requests,
399 for instance by enabling TCP_NODELAY and disabling TCP_CORK and
400 autocork. After linux-4.7, a better way to prevent coalescing is
401 to use MSG_EOR flag at sendmsg() time.
402
403 These precautions ensure that the timestamp is generated only when all
404 bytes have passed a timestamp point, assuming that the network stack
405 itself does not reorder the segments. The stack indeed tries to avoid
406 reordering. The one exception is under administrator control: it is
407 possible to construct a packet scheduler configuration that delays
408 segments from the same stream differently. Such a setup would be
409 unusual.
410
411
412 2 Data Interfaces
413 ==================
414
415 Timestamps are read using the ancillary data feature of recvmsg().
416 See `man 3 cmsg` for details of this interface. The socket manual
417 page (`man 7 socket`) describes how timestamps generated with
418 SO_TIMESTAMP and SO_TIMESTAMPNS records can be retrieved.
419
420
421 2.1 SCM_TIMESTAMPING records
422 ----------------------------
423
424 These timestamps are returned in a control message with cmsg_level
425 SOL_SOCKET, cmsg_type SCM_TIMESTAMPING, and payload of type
426
427 For SO_TIMESTAMPING_OLD::
428
429 struct scm_timestamping {
430 struct timespec ts[3];
431 };
432
433 For SO_TIMESTAMPING_NEW::
434
435 struct scm_timestamping64 {
436 struct __kernel_timespec ts[3];
437
438 Always use SO_TIMESTAMPING_NEW timestamp to always get timestamp in
439 struct scm_timestamping64 format.
440
441 SO_TIMESTAMPING_OLD returns incorrect timestamps after the year 2038
442 on 32 bit machines.
443
444 The structure can return up to three timestamps. This is a legacy
445 feature. At least one field is non-zero at any time. Most timestamps
446 are passed in ts[0]. Hardware timestamps are passed in ts[2].
447
448 ts[1] used to hold hardware timestamps converted to system time.
449 Instead, expose the hardware clock device on the NIC directly as
450 a HW PTP clock source, to allow time conversion in userspace and
451 optionally synchronize system time with a userspace PTP stack such
452 as linuxptp. For the PTP clock API, see Documentation/driver-api/ptp.rst.
453
454 Note that if the SO_TIMESTAMP or SO_TIMESTAMPNS option is enabled
455 together with SO_TIMESTAMPING using SOF_TIMESTAMPING_SOFTWARE, a false
456 software timestamp will be generated in the recvmsg() call and passed
457 in ts[0] when a real software timestamp is missing. This happens also
458 on hardware transmit timestamps.
459
460 2.1.1 Transmit timestamps with MSG_ERRQUEUE
461 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
462
463 For transmit timestamps the outgoing packet is looped back to the
464 socket's error queue with the send timestamp(s) attached. A process
465 receives the timestamps by calling recvmsg() with flag MSG_ERRQUEUE
466 set and with a msg_control buffer sufficiently large to receive the
467 relevant metadata structures. The recvmsg call returns the original
468 outgoing data packet with two ancillary messages attached.
469
470 A message of cm_level SOL_IP(V6) and cm_type IP(V6)_RECVERR
471 embeds a struct sock_extended_err. This defines the error type. For
472 timestamps, the ee_errno field is ENOMSG. The other ancillary message
473 will have cm_level SOL_SOCKET and cm_type SCM_TIMESTAMPING. This
474 embeds the struct scm_timestamping.
475
476
477 2.1.1.2 Timestamp types
478 ~~~~~~~~~~~~~~~~~~~~~~~
479
480 The semantics of the three struct timespec are defined by field
481 ee_info in the extended error structure. It contains a value of
482 type SCM_TSTAMP_* to define the actual timestamp passed in
483 scm_timestamping.
484
485 The SCM_TSTAMP_* types are 1:1 matches to the SOF_TIMESTAMPING_*
486 control fields discussed previously, with one exception. For legacy
487 reasons, SCM_TSTAMP_SND is equal to zero and can be set for both
488 SOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_TX_SOFTWARE. It
489 is the first if ts[2] is non-zero, the second otherwise, in which
490 case the timestamp is stored in ts[0].
491
492
493 2.1.1.3 Fragmentation
494 ~~~~~~~~~~~~~~~~~~~~~
495
496 Fragmentation of outgoing datagrams is rare, but is possible, e.g., by
497 explicitly disabling PMTU discovery. If an outgoing packet is fragmented,
498 then only the first fragment is timestamped and returned to the sending
499 socket.
500
501
502 2.1.1.4 Packet Payload
503 ~~~~~~~~~~~~~~~~~~~~~~
504
505 The calling application is often not interested in receiving the whole
506 packet payload that it passed to the stack originally: the socket
507 error queue mechanism is just a method to piggyback the timestamp on.
508 In this case, the application can choose to read datagrams with a
509 smaller buffer, possibly even of length 0. The payload is truncated
510 accordingly. Until the process calls recvmsg() on the error queue,
511 however, the full packet is queued, taking up budget from SO_RCVBUF.
512
513
514 2.1.1.5 Blocking Read
515 ~~~~~~~~~~~~~~~~~~~~~
516
517 Reading from the error queue is always a non-blocking operation. To
518 block waiting on a timestamp, use poll or select. poll() will return
519 POLLERR in pollfd.revents if any data is ready on the error queue.
520 There is no need to pass this flag in pollfd.events. This flag is
521 ignored on request. See also `man 2 poll`.
522
523
524 2.1.2 Receive timestamps
525 ^^^^^^^^^^^^^^^^^^^^^^^^
526
527 On reception, there is no reason to read from the socket error queue.
528 The SCM_TIMESTAMPING ancillary data is sent along with the packet data
529 on a normal recvmsg(). Since this is not a socket error, it is not
530 accompanied by a message SOL_IP(V6)/IP(V6)_RECVERROR. In this case,
531 the meaning of the three fields in struct scm_timestamping is
532 implicitly defined. ts[0] holds a software timestamp if set, ts[1]
533 is again deprecated and ts[2] holds a hardware timestamp if set.
534
535
536 3. Hardware Timestamping configuration: ETHTOOL_MSG_TSCONFIG_SET/GET
537 ====================================================================
538
539 Hardware time stamping must also be initialized for each device driver
540 that is expected to do hardware time stamping. The parameter is defined in
541 include/uapi/linux/net_tstamp.h as::
542
543 struct hwtstamp_config {
544 int flags; /* no flags defined right now, must be zero */
545 int tx_type; /* HWTSTAMP_TX_* */
546 int rx_filter; /* HWTSTAMP_FILTER_* */
547 };
548
549 Desired behavior is passed into the kernel and to a specific device by
550 calling the tsconfig netlink socket ``ETHTOOL_MSG_TSCONFIG_SET``.
551 The ``ETHTOOL_A_TSCONFIG_TX_TYPES``, ``ETHTOOL_A_TSCONFIG_RX_FILTERS`` and
552 ``ETHTOOL_A_TSCONFIG_HWTSTAMP_FLAGS`` netlink attributes are then used to set
553 the struct hwtstamp_config accordingly.
554
555 The ``ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER`` netlink nested attribute is used
556 to select the source of the hardware time stamping. It is composed of an index
557 for the device source and a qualifier for the type of time stamping.
558
559 Drivers are free to use a more permissive configuration than the requested
560 configuration. It is expected that drivers should only implement directly the
561 most generic mode that can be supported. For example if the hardware can
562 support HWTSTAMP_FILTER_PTP_V2_EVENT, then it should generally always upscale
563 HWTSTAMP_FILTER_PTP_V2_L2_SYNC, and so forth, as HWTSTAMP_FILTER_PTP_V2_EVENT
564 is more generic (and more useful to applications).
565
566 A driver which supports hardware time stamping shall update the struct
567 with the actual, possibly more permissive configuration. If the
568 requested packets cannot be time stamped, then nothing should be
569 changed and ERANGE shall be returned (in contrast to EINVAL, which
570 indicates that SIOCSHWTSTAMP is not supported at all).
571
572 Only a processes with admin rights may change the configuration. User
573 space is responsible to ensure that multiple processes don't interfere
574 with each other and that the settings are reset.
575
576 Any process can read the actual configuration by requesting tsconfig netlink
577 socket ``ETHTOOL_MSG_TSCONFIG_GET``.
578
579 The legacy configuration is the use of the ioctl(SIOCSHWTSTAMP) with a pointer
580 to a struct ifreq whose ifr_data points to a struct hwtstamp_config.
581 The tx_type and rx_filter are hints to the driver what it is expected to do.
582 If the requested fine-grained filtering for incoming packets is not
583 supported, the driver may time stamp more than just the requested types
584 of packets. ioctl(SIOCGHWTSTAMP) is used in the same way as the
585 ioctl(SIOCSHWTSTAMP). However, this has not been implemented in all drivers.
586
587 ::
588
589 /* possible values for hwtstamp_config->tx_type */
590 enum {
591 /*
592 * no outgoing packet will need hardware time stamping;
593 * should a packet arrive which asks for it, no hardware
594 * time stamping will be done
595 */
596 HWTSTAMP_TX_OFF,
597
598 /*
599 * enables hardware time stamping for outgoing packets;
600 * the sender of the packet decides which are to be
601 * time stamped by setting SOF_TIMESTAMPING_TX_SOFTWARE
602 * before sending the packet
603 */
604 HWTSTAMP_TX_ON,
605 };
606
607 /* possible values for hwtstamp_config->rx_filter */
608 enum {
609 /* time stamp no incoming packet at all */
610 HWTSTAMP_FILTER_NONE,
611
612 /* time stamp any incoming packet */
613 HWTSTAMP_FILTER_ALL,
614
615 /* return value: time stamp all packets requested plus some others */
616 HWTSTAMP_FILTER_SOME,
617
618 /* PTP v1, UDP, any kind of event packet */
619 HWTSTAMP_FILTER_PTP_V1_L4_EVENT,
620
621 /* for the complete list of values, please check
622 * the include file include/uapi/linux/net_tstamp.h
623 */
624 };
625
626 3.1 Hardware Timestamping Implementation: Device Drivers
627 --------------------------------------------------------
628
629 A driver which supports hardware time stamping must support the
630 ndo_hwtstamp_set NDO or the legacy SIOCSHWTSTAMP ioctl and update the
631 supplied struct hwtstamp_config with the actual values as described in
632 the section on SIOCSHWTSTAMP. It should also support ndo_hwtstamp_get or
633 the legacy SIOCGHWTSTAMP.
634
635 Time stamps for received packets must be stored in the skb. To get a pointer
636 to the shared time stamp structure of the skb call skb_hwtstamps(). Then
637 set the time stamps in the structure::
638
639 struct skb_shared_hwtstamps {
640 /* hardware time stamp transformed into duration
641 * since arbitrary point in time
642 */
643 ktime_t hwtstamp;
644 };
645
646 Time stamps for outgoing packets are to be generated as follows:
647
648 - In hard_start_xmit(), check if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
649 is set no-zero. If yes, then the driver is expected to do hardware time
650 stamping.
651 - If this is possible for the skb and requested, then declare
652 that the driver is doing the time stamping by setting the flag
653 SKBTX_IN_PROGRESS in skb_shinfo(skb)->tx_flags , e.g. with::
654
655 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
656
657 You might want to keep a pointer to the associated skb for the next step
658 and not free the skb. A driver not supporting hardware time stamping doesn't
659 do that. A driver must never touch sk_buff::tstamp! It is used to store
660 software generated time stamps by the network subsystem.
661 - Driver should call skb_tx_timestamp() as close to passing sk_buff to hardware
662 as possible. skb_tx_timestamp() provides a software time stamp if requested
663 and hardware timestamping is not possible (SKBTX_IN_PROGRESS not set).
664 - As soon as the driver has sent the packet and/or obtained a
665 hardware time stamp for it, it passes the time stamp back by
666 calling skb_tstamp_tx() with the original skb, the raw
667 hardware time stamp. skb_tstamp_tx() clones the original skb and
668 adds the timestamps, therefore the original skb has to be freed now.
669 If obtaining the hardware time stamp somehow fails, then the driver
670 should not fall back to software time stamping. The rationale is that
671 this would occur at a later time in the processing pipeline than other
672 software time stamping and therefore could lead to unexpected deltas
673 between time stamps.
674
675 3.2 Special considerations for stacked PTP Hardware Clocks
676 ----------------------------------------------------------
677
678 There are situations when there may be more than one PHC (PTP Hardware Clock)
679 in the data path of a packet. The kernel has no explicit mechanism to allow the
680 user to select which PHC to use for timestamping Ethernet frames. Instead, the
681 assumption is that the outermost PHC is always the most preferable, and that
682 kernel drivers collaborate towards achieving that goal. Currently there are 3
683 cases of stacked PHCs, detailed below:
684
685 3.2.1 DSA (Distributed Switch Architecture) switches
686 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
687
688 These are Ethernet switches which have one of their ports connected to an
689 (otherwise completely unaware) host Ethernet interface, and perform the role of
690 a port multiplier with optional forwarding acceleration features. Each DSA
691 switch port is visible to the user as a standalone (virtual) network interface,
692 and its network I/O is performed, under the hood, indirectly through the host
693 interface (redirecting to the host port on TX, and intercepting frames on RX).
694
695 When a DSA switch is attached to a host port, PTP synchronization has to
696 suffer, since the switch's variable queuing delay introduces a path delay
697 jitter between the host port and its PTP partner. For this reason, some DSA
698 switches include a timestamping clock of their own, and have the ability to
699 perform network timestamping on their own MAC, such that path delays only
700 measure wire and PHY propagation latencies. Timestamping DSA switches are
701 supported in Linux and expose the same ABI as any other network interface (save
702 for the fact that the DSA interfaces are in fact virtual in terms of network
703 I/O, they do have their own PHC). It is typical, but not mandatory, for all
704 interfaces of a DSA switch to share the same PHC.
705
706 By design, PTP timestamping with a DSA switch does not need any special
707 handling in the driver for the host port it is attached to. However, when the
708 host port also supports PTP timestamping, DSA will take care of intercepting
709 the ``.ndo_eth_ioctl`` calls towards the host port, and block attempts to enable
710 hardware timestamping on it. This is because the SO_TIMESTAMPING API does not
711 allow the delivery of multiple hardware timestamps for the same packet, so
712 anybody else except for the DSA switch port must be prevented from doing so.
713
714 In the generic layer, DSA provides the following infrastructure for PTP
715 timestamping:
716
717 - ``.port_txtstamp()``: a hook called prior to the transmission of
718 packets with a hardware TX timestamping request from user space.
719 This is required for two-step timestamping, since the hardware
720 timestamp becomes available after the actual MAC transmission, so the
721 driver must be prepared to correlate the timestamp with the original
722 packet so that it can re-enqueue the packet back into the socket's
723 error queue. To save the packet for when the timestamp becomes
724 available, the driver can call ``skb_clone_sk`` , save the clone pointer
725 in skb->cb and enqueue a tx skb queue. Typically, a switch will have a
726 PTP TX timestamp register (or sometimes a FIFO) where the timestamp
727 becomes available. In case of a FIFO, the hardware might store
728 key-value pairs of PTP sequence ID/message type/domain number and the
729 actual timestamp. To perform the correlation correctly between the
730 packets in a queue waiting for timestamping and the actual timestamps,
731 drivers can use a BPF classifier (``ptp_classify_raw``) to identify
732 the PTP transport type, and ``ptp_parse_header`` to interpret the PTP
733 header fields. There may be an IRQ that is raised upon this
734 timestamp's availability, or the driver might have to poll after
735 invoking ``dev_queue_xmit()`` towards the host interface.
736 One-step TX timestamping do not require packet cloning, since there is
737 no follow-up message required by the PTP protocol (because the
738 TX timestamp is embedded into the packet by the MAC), and therefore
739 user space does not expect the packet annotated with the TX timestamp
740 to be re-enqueued into its socket's error queue.
741
742 - ``.port_rxtstamp()``: On RX, the BPF classifier is run by DSA to
743 identify PTP event messages (any other packets, including PTP general
744 messages, are not timestamped). The original (and only) timestampable
745 skb is provided to the driver, for it to annotate it with a timestamp,
746 if that is immediately available, or defer to later. On reception,
747 timestamps might either be available in-band (through metadata in the
748 DSA header, or attached in other ways to the packet), or out-of-band
749 (through another RX timestamping FIFO). Deferral on RX is typically
750 necessary when retrieving the timestamp needs a sleepable context. In
751 that case, it is the responsibility of the DSA driver to call
752 ``netif_rx()`` on the freshly timestamped skb.
753
754 3.2.2 Ethernet PHYs
755 ^^^^^^^^^^^^^^^^^^^
756
757 These are devices that typically fulfill a Layer 1 role in the network stack,
758 hence they do not have a representation in terms of a network interface as DSA
759 switches do. However, PHYs may be able to detect and timestamp PTP packets, for
760 performance reasons: timestamps taken as close as possible to the wire have the
761 potential to yield a more stable and precise synchronization.
762
763 A PHY driver that supports PTP timestamping must create a ``struct
764 mii_timestamper`` and add a pointer to it in ``phydev->mii_ts``. The presence
765 of this pointer will be checked by the networking stack.
766
767 Since PHYs do not have network interface representations, the timestamping and
768 ethtool ioctl operations for them need to be mediated by their respective MAC
769 driver. Therefore, as opposed to DSA switches, modifications need to be done
770 to each individual MAC driver for PHY timestamping support. This entails:
771
772 - Checking, in ``.ndo_eth_ioctl``, whether ``phy_has_hwtstamp(netdev->phydev)``
773 is true or not. If it is, then the MAC driver should not process this request
774 but instead pass it on to the PHY using ``phy_mii_ioctl()``.
775
776 - On RX, special intervention may or may not be needed, depending on the
777 function used to deliver skb's up the network stack. In the case of plain
778 ``netif_rx()`` and similar, MAC drivers must check whether
779 ``skb_defer_rx_timestamp(skb)`` is necessary or not - and if it is, don't
780 call ``netif_rx()`` at all. If ``CONFIG_NETWORK_PHY_TIMESTAMPING`` is
781 enabled, and ``skb->dev->phydev->mii_ts`` exists, its ``.rxtstamp()`` hook
782 will be called now, to determine, using logic very similar to DSA, whether
783 deferral for RX timestamping is necessary. Again like DSA, it becomes the
784 responsibility of the PHY driver to send the packet up the stack when the
785 timestamp is available.
786
787 For other skb receive functions, such as ``napi_gro_receive`` and
788 ``netif_receive_skb``, the stack automatically checks whether
789 ``skb_defer_rx_timestamp()`` is necessary, so this check is not needed inside
790 the driver.
791
792 - On TX, again, special intervention might or might not be needed. The
793 function that calls the ``mii_ts->txtstamp()`` hook is named
794 ``skb_clone_tx_timestamp()``. This function can either be called directly
795 (case in which explicit MAC driver support is indeed needed), but the
796 function also piggybacks from the ``skb_tx_timestamp()`` call, which many MAC
797 drivers already perform for software timestamping purposes. Therefore, if a
798 MAC supports software timestamping, it does not need to do anything further
799 at this stage.
800
801 3.2.3 MII bus snooping devices
802 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
803
804 These perform the same role as timestamping Ethernet PHYs, save for the fact
805 that they are discrete devices and can therefore be used in conjunction with
806 any PHY even if it doesn't support timestamping. In Linux, they are
807 discoverable and attachable to a ``struct phy_device`` through Device Tree, and
808 for the rest, they use the same mii_ts infrastructure as those. See
809 Documentation/devicetree/bindings/ptp/timestamper.txt for more details.
810
811 3.2.4 Other caveats for MAC drivers
812 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
813
814 The use of stacked PHCs may uncover MAC driver bugs which were impossible to
815 trigger without them. One example has to do with this line of code, already
816 presented earlier::
817
818 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
819
820 Any TX timestamping logic, be it a plain MAC driver, a DSA switch driver, a PHY
821 driver or a MII bus snooping device driver, should set this flag.
822 But a MAC driver that is unaware of PHC stacking might get tripped up by
823 somebody other than itself setting this flag, and deliver a duplicate
824 timestamp.
825 For example, a typical driver design for TX timestamping might be to split the
826 transmission part into 2 portions:
827
828 1. "TX": checks whether PTP timestamping has been previously enabled through
829 the ``.ndo_eth_ioctl`` ("``priv->hwtstamp_tx_enabled == true``") and the
830 current skb requires a TX timestamp ("``skb_shinfo(skb)->tx_flags &
831 SKBTX_HW_TSTAMP``"). If this is true, it sets the
832 "``skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS``" flag. Note: as
833 described above, in the case of a stacked PHC system, this condition should
834 never trigger, as this MAC is certainly not the outermost PHC. But this is
835 not where the typical issue is. Transmission proceeds with this packet.
836
837 2. "TX confirmation": Transmission has finished. The driver checks whether it
838 is necessary to collect any TX timestamp for it. Here is where the typical
839 issues are: the MAC driver takes a shortcut and only checks whether
840 "``skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS``" was set. With a stacked
841 PHC system, this is incorrect because this MAC driver is not the only entity
842 in the TX data path who could have enabled SKBTX_IN_PROGRESS in the first
843 place.
844
845 The correct solution for this problem is for MAC drivers to have a compound
846 check in their "TX confirmation" portion, not only for
847 "``skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS``", but also for
848 "``priv->hwtstamp_tx_enabled == true``". Because the rest of the system ensures
849 that PTP timestamping is not enabled for anything other than the outermost PHC,
850 this enhanced check will avoid delivering a duplicated TX timestamp to user
851 space.
852

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

Timestamp control interface 개요

1-41

이 문서는 GPL-2.0 라이선스를 따르며 network packet timestamp를 생성하고 읽는 Linux interface를 설명합니다.

`SO_TIMESTAMP`는 incoming packet마다 반드시 monotonic하지는 않은 system time timestamp를 만들고 `recvmsg()` control message로 microsecond 해상도에서 보고합니다. Architecture와 libc `time_t` 표현에 따라 `SO_TIMESTAMP_NEW` 또는 `SO_TIMESTAMP_OLD`로 정의됩니다. OLD는 `struct __kernel_old_timeval`, NEW는 `struct __kernel_sock_timeval` 형식을 사용합니다.

`SO_TIMESTAMPNS`는 같은 mechanism을 nanosecond 해상도의 `struct timespec`으로 보고합니다. OLD는 `struct timespec`, NEW는 `struct __kernel_timespec` 형식입니다.

Multicast에서는 `IP_MULTICAST_LOOP`와 `SO_TIMESTAMP[NS]`를 함께 사용해 loopback으로 돌아온 packet의 receive timestamp를 읽는 방식으로 근사 transmit timestamp를 얻을 수 있습니다.

`SO_TIMESTAMPING`은 receive, transmit 또는 양쪽 timestamp를 생성하며 hardware를 포함한 여러 source와 stream socket timestamping을 지원합니다.

Timestamp control interface
Interface방향형식·특성
SO_TIMESTAMPRXusec, timeval 계열
SO_TIMESTAMPNSRXnsec, timespec 계열
IP_MULTICAST_LOOP + SO_TIMESTAMP[NS]근사 TXlooped RX timestamp
SO_TIMESTAMPINGRX/TXsoftware·hardware·stream 지원

해상도와 적용 방향을 비교합니다.

.. SPDX-License-Identifier: GPL-2.0

============
Timestamping
============


1. Control Interfaces
=====================

The interfaces for receiving network packages timestamps are:

SO_TIMESTAMP
  Generates a timestamp for each incoming packet in (not necessarily
  monotonic) system time. Reports the timestamp via recvmsg() in a
  control message in usec resolution.
  SO_TIMESTAMP is defined as SO_TIMESTAMP_NEW or SO_TIMESTAMP_OLD
  based on the architecture type and time_t representation of libc.
  Control message format is in struct __kernel_old_timeval for
  SO_TIMESTAMP_OLD and in struct __kernel_sock_timeval for
  SO_TIMESTAMP_NEW options respectively.

SO_TIMESTAMPNS
  Same timestamping mechanism as SO_TIMESTAMP, but reports the
  timestamp as struct timespec in nsec resolution.
  SO_TIMESTAMPNS is defined as SO_TIMESTAMPNS_NEW or SO_TIMESTAMPNS_OLD
  based on the architecture type and time_t representation of libc.
  Control message format is in struct timespec for SO_TIMESTAMPNS_OLD
  and in struct __kernel_timespec for SO_TIMESTAMPNS_NEW options
  respectively.

IP_MULTICAST_LOOP + SO_TIMESTAMP[NS]
  Only for multicast:approximate transmit timestamp obtained by
  reading the looped packet receive timestamp.

SO_TIMESTAMPING
  Generates timestamps on reception, transmission or both. Supports
  multiple timestamp sources, including hardware. Supports generating
  timestamps for stream sockets.

SO_TIMESTAMP와 SO_TIMESTAMPNS

42-70

`SO_TIMESTAMP`는 receive path의 datagram timestamping을 활성화합니다. Network stack 초반에는 destination socket을 아직 알 수 없으므로 모든 packet에 대해 기능을 켜야 하며, 다른 early receive timestamp option도 같습니다. 자세한 interface는 `man 7 socket`을 참조합니다.

항상 `struct __kernel_sock_timeval`을 받으려면 `SO_TIMESTAMP_NEW`를 사용해야 합니다. 32-bit machine에서 `SO_TIMESTAMP_OLD`는 2038년 이후 잘못된 timestamp를 반환합니다.

`SO_TIMESTAMPNS`는 반환 data type을 제외하면 `SO_TIMESTAMP`와 동일하며, `timeval`보다 높은 nanosecond 해상도의 `struct timespec`을 사용합니다. 항상 `struct __kernel_timespec`을 받으려면 `SO_TIMESTAMPNS_NEW`를 사용해야 합니다. 32-bit machine의 OLD variant는 역시 2038년 이후 잘못됩니다.

1.1 SO_TIMESTAMP (also SO_TIMESTAMP_OLD and SO_TIMESTAMP_NEW)
-------------------------------------------------------------

This socket option enables timestamping of datagrams on the reception
path. Because the destination socket, if any, is not known early in
the network stack, the feature has to be enabled for all packets. The
same is true for all early receive timestamp options.

For interface details, see `man 7 socket`.

Always use SO_TIMESTAMP_NEW timestamp to always get timestamp in
struct __kernel_sock_timeval format.

SO_TIMESTAMP_OLD returns incorrect timestamps after the year 2038
on 32 bit machines.

1.2 SO_TIMESTAMPNS (also SO_TIMESTAMPNS_OLD and SO_TIMESTAMPNS_NEW)
-------------------------------------------------------------------

This option is identical to SO_TIMESTAMP except for the returned data type.
Its struct timespec allows for higher resolution (ns) timestamps than the
timeval of SO_TIMESTAMP (ms).

Always use SO_TIMESTAMPNS_NEW timestamp to always get timestamp in
struct __kernel_timespec format.

SO_TIMESTAMPNS_OLD returns incorrect timestamps after the year 2038
on 32 bit machines.

SO_TIMESTAMPING generation flag

71-151

`SO_TIMESTAMPING`은 여러 timestamp request type을 지원하므로 boolean이 아니라 flag bitmap을 받습니다. `setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val))`로 설정하며 정의되지 않은 bit가 있으면 `EINVAL`을 반환하고 현재 상태를 바꾸지 않습니다.

Socket option은 개별 `sk_buff`의 timestamp generation, socket error queue 보고, 추가 option을 함께 설정합니다. 개별 `sendmsg()` 호출은 cmsg로 generation만 따로 요청할 수도 있습니다.

Generation bit는 stack에 timestamp 생성을 시도하라고 요청합니다. 조합은 자유롭고 변경은 새 packet에만 적용됩니다. 따라서 enable과 disable `setsockopt()` 사이에 `send()`를 두어 일부 packet만 sampling할 수 있습니다. System-wide RX timestamping처럼 특정 socket request 이외의 이유로도 timestamp가 생성될 수 있습니다.

Timestamp generation flag
Flag측정 지점비고
SOF_TIMESTAMPING_RX_HARDWARENetwork adapter RXAdapter 생성
SOF_TIMESTAMPING_RX_SOFTWAREDriver가 kernel receive stack에 넘긴 직후Kernel ingress 초반
SOF_TIMESTAMPING_TX_HARDWARENetwork adapter TXSocket option/cmsg 가능
SOF_TIMESTAMPING_TX_SOFTWAREDriver가 interface에 넘기기 직전Driver 지원 필요, socket option/cmsg 가능
SOF_TIMESTAMPING_TX_SCHEDPacket scheduler 진입 전여러 virtual layer에서 각각 생성 가능
SOF_TIMESTAMPING_TX_ACKSend buffer의 해당 data까지 cumulative ACKReliable protocol, 현재 TCP만; SACK/FACK 무시
SOF_TIMESTAMPING_TX_COMPLETIONHardware completion report 수신실제 TX 시각이 아니라 report 시각

Timestamp를 채집하는 지점과 의미입니다.

`TX_SCHED`와 `TX_SOFTWARE`의 차이는 scheduler queuing delay를 보여 줍니다. `send()` 직전 userspace timestamp와 `TX_SCHED`의 차이는 protocol processing latency를 나타냅니다.

`TX_ACK`는 `send()` buffer까지 포함한 모든 data가 cumulative ACK됐을 때 생성되어 측정치를 크게 보고할 수 있습니다. `TX_COMPLETION`은 hardware가 여러 packet을 묶어 완료 보고할 수 있으므로 실제 wire transmit time이 아니라 completion report timing입니다.

1.3 SO_TIMESTAMPING (also SO_TIMESTAMPING_OLD and SO_TIMESTAMPING_NEW)
----------------------------------------------------------------------

Supports multiple types of timestamp requests. As a result, this
socket option takes a bitmap of flags, not a boolean. In::

  err = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val));

val is an integer with any of the following bits set. Setting other
bit returns EINVAL and does not change the current state.

The socket option configures timestamp generation for individual
sk_buffs (1.3.1), timestamp reporting to the socket's error
queue (1.3.2) and options (1.3.3). Timestamp generation can also
be enabled for individual sendmsg calls using cmsg (1.3.4).


1.3.1 Timestamp Generation
^^^^^^^^^^^^^^^^^^^^^^^^^^

Some bits are requests to the stack to try to generate timestamps. Any
combination of them is valid. Changes to these bits apply to newly
created packets, not to packets already in the stack. As a result, it
is possible to selectively request timestamps for a subset of packets
(e.g., for sampling) by embedding an send() call within two setsockopt
calls, one to enable timestamp generation and one to disable it.
Timestamps may also be generated for reasons other than being
requested by a particular socket, such as when receive timestamping is
enabled system wide, as explained earlier.

SOF_TIMESTAMPING_RX_HARDWARE:
  Request rx timestamps generated by the network adapter.

SOF_TIMESTAMPING_RX_SOFTWARE:
  Request rx timestamps when data enters the kernel. These timestamps
  are generated just after a device driver hands a packet to the
  kernel receive stack.

SOF_TIMESTAMPING_TX_HARDWARE:
  Request tx timestamps generated by the network adapter. This flag
  can be enabled via both socket options and control messages.

SOF_TIMESTAMPING_TX_SOFTWARE:
  Request tx timestamps when data leaves the kernel. These timestamps
  are generated in the device driver as close as possible, but always
  prior to, passing the packet to the network interface. Hence, they
  require driver support and may not be available for all devices.
  This flag can be enabled via both socket options and control messages.

SOF_TIMESTAMPING_TX_SCHED:
  Request tx timestamps prior to entering the packet scheduler. Kernel
  transmit latency is, if long, often dominated by queuing delay. The
  difference between this timestamp and one taken at
  SOF_TIMESTAMPING_TX_SOFTWARE will expose this latency independent
  of protocol processing. The latency incurred in protocol
  processing, if any, can be computed by subtracting a userspace
  timestamp taken immediately before send() from this timestamp. On
  machines with virtual devices where a transmitted packet travels
  through multiple devices and, hence, multiple packet schedulers,
  a timestamp is generated at each layer. This allows for fine
  grained measurement of queuing delay. This flag can be enabled
  via both socket options and control messages.

SOF_TIMESTAMPING_TX_ACK:
  Request tx timestamps when all data in the send buffer has been
  acknowledged. This only makes sense for reliable protocols. It is
  currently only implemented for TCP. For that protocol, it may
  over-report measurement, because the timestamp is generated when all
  data up to and including the buffer at send() was acknowledged: the
  cumulative acknowledgment. The mechanism ignores SACK and FACK.
  This flag can be enabled via both socket options and control messages.

SOF_TIMESTAMPING_TX_COMPLETION:
  Request tx timestamps on packet tx completion.  The completion
  timestamp is generated by the kernel when it receives packet a
  completion report from the hardware. Hardware may report multiple
  packets at once, and completion timestamps reflect the timing of the
  report and not actual tx time. This flag can be enabled via both
  socket options and control messages.

Reporting flag와 timestamp option

152-319

Reporting bit는 생성된 control message에 어떤 timestamp를 실을지 결정하며 stack의 보고 지점에 즉시 적용됩니다. 해당 generation request가 설정된 packet의 timestamp만 보고됩니다.

`SOF_TIMESTAMPING_SOFTWARE`는 가능한 software timestamp를 보고합니다. `SOF_TIMESTAMPING_SYS_HARDWARE`는 deprecated되어 무시됩니다. `SOF_TIMESTAMPING_RAW_HARDWARE`는 TX/RX hardware generation flag가 만든 raw hardware timestamp를 보고합니다.

`SOF_TIMESTAMPING_OPT_ID`는 concurrent request와 TX path reordering에도 원래 `send()`를 식별할 수 있도록 packet마다 unique ID를 붙입니다. Socket별 wrapping `u32` counter에서 만들며 datagram은 packet마다, stream은 byte마다 증가합니다. Stream socket에서는 `SOF_TIMESTAMPING_OPT_ID_TCP`도 함께 설정해야 합니다.

Counter는 option을 처음 enable할 때 zero로 시작하고 disable 후 다시 enable할 때 reset됩니다. 이미 system 안에 있는 packet의 ID는 바뀌지 않습니다. TX timestamp에만 구현되며 `struct sock_extended_err.ee_data`로 ID를 돌려줍니다. Non-TCP socket은 `SCM_TS_OPT_ID` cmsg로 application이 ID를 덮어쓸 수도 있습니다. 원문의 `msghdr`/`cmsghdr` 예제를 그대로 보존합니다.

`SOF_TIMESTAMPING_OPT_ID_TCP`는 stream ID 시작점을 안정적으로 정의합니다. 이후 N byte `write()`가 counter `N-1` timestamp를 만들도록 항상 syscall 시점에 zero로 맞춥니다. Modifier 없는 방식은 output queue가 있으면 `SIOCOUTQ` 길이만큼 어긋날 수 있습니다. 이는 peer ACK와 RTT에 좌우되는 `snd_una` 대신 process가 마지막으로 쓴 byte인 `write_seq`를 기준으로 삼는 차이입니다.

SO_TIMESTAMPING option
Option효과제약
OPT_IDTX request마다 ee_data IDDatagram=packet, stream=byte counter
OPT_ID_TCPTCP counter를 write_seq 기준으로 안정화Stream에서 OPT_ID와 함께 권장
OPT_CMSGIPv4 TX에도 recv cmsg 지원IP_PKTINFO와 egress device 연계
OPT_TSONLY원 packet 대신 empty packet+cmsgSO_RCVBUF 절약, OPT_CMSG 비활성
OPT_STATSSCM_TIMESTAMPING_OPT_STATS TLVOPT_TSONLY 필수
OPT_PKTINFOHW RX의 real ifindex와 L2 length유효 ifindex는 busy poll+NAPI 필요
OPT_TX_SWHWTX software와 hardware message 각각 반환두 generation flag 동시 enable
OPT_RX_FILTER요청 generation과 맞지 않는 RX timestamp 제거다른 socket 영향 격리

식별·payload·부가 정보와 filtering 기능입니다.

`SOF_TIMESTAMPING_OPT_CMSG`는 RX와 IPv6 TX에 이미 제공되는 cmsg를 IPv4 TX까지 확장하며 `IP_PKTINFO`와 함께 egress interface를 연계할 수 있습니다.

`SOF_TIMESTAMPING_OPT_TSONLY`는 TX timestamp를 original packet 대신 empty packet 옆 cmsg로 반환해 `SO_RCVBUF` 부담을 줄이고 `net.core.tstamp_allow_data=0`에서도 전달합니다. 이 option은 `OPT_CMSG`를 끕니다. `OPT_STATS`는 `OPT_TSONLY`와 함께 사용하며 transport statistic을 `SCM_TIMESTAMPING_OPT_STATS`의 `nlattr` TLV list로 전달합니다.

`OPT_PKTINFO`는 hardware timestamp가 있는 incoming packet에 `SCM_TIMESTAMPING_PKTINFO`를 추가합니다. `struct scm_ts_pktinfo`에는 실제 receive interface index와 L2 length가 있고, non-zero index는 `CONFIG_NET_RX_BUSY_POLL`과 NAPI driver가 모두 필요합니다.

`OPT_TX_SWHW`는 TX hardware와 software timestamp가 모두 생성되면 각각 하나의 timestamp만 담은 두 message를 error queue에 넣습니다. `OPT_RX_FILTER`는 ingress 초반의 system-wide generation 때문에 TX-only socket까지 받게 되는 spurious RX timestamp를 제거해 다른 socket과 독립적인 동작을 만듭니다.

새 application은 timestamp 식별을 위해 `OPT_ID`, data 허용 sysctl과 무관한 동작을 위해 `OPT_TSONLY`를 권장합니다. Egress interface용 `IP_PKTINFO` 같은 추가 cmsg가 필요하면 original packet content에 의존하는 `OPT_CMSG`를 사용해야 하므로 `OPT_TSONLY`와 함께 쓸 수 없습니다.

1.3.2 Timestamp Reporting
^^^^^^^^^^^^^^^^^^^^^^^^^

The other three bits control which timestamps will be reported in a
generated control message. Changes to the bits take immediate
effect at the timestamp reporting locations in the stack. Timestamps
are only reported for packets that also have the relevant timestamp
generation request set.

SOF_TIMESTAMPING_SOFTWARE:
  Report any software timestamps when available.

SOF_TIMESTAMPING_SYS_HARDWARE:
  This option is deprecated and ignored.

SOF_TIMESTAMPING_RAW_HARDWARE:
  Report hardware timestamps as generated by
  SOF_TIMESTAMPING_TX_HARDWARE or SOF_TIMESTAMPING_RX_HARDWARE
  when available.


1.3.3 Timestamp Options
^^^^^^^^^^^^^^^^^^^^^^^

The interface supports the options

SOF_TIMESTAMPING_OPT_ID:
  Generate a unique identifier along with each packet. A process can
  have multiple concurrent timestamping requests outstanding. Packets
  can be reordered in the transmit path, for instance in the packet
  scheduler. In that case timestamps will be queued onto the error
  queue out of order from the original send() calls. It is not always
  possible to uniquely match timestamps to the original send() calls
  based on timestamp order or payload inspection alone, then.

  This option associates each packet at send() with a unique
  identifier and returns that along with the timestamp. The identifier
  is derived from a per-socket u32 counter (that wraps). For datagram
  sockets, the counter increments with each sent packet. For stream
  sockets, it increments with every byte. For stream sockets, also set
  SOF_TIMESTAMPING_OPT_ID_TCP, see the section below.

  The counter starts at zero. It is initialized the first time that
  the socket option is enabled. It is reset each time the option is
  enabled after having been disabled. Resetting the counter does not
  change the identifiers of existing packets in the system.

  This option is implemented only for transmit timestamps. There, the
  timestamp is always looped along with a struct sock_extended_err.
  The option modifies field ee_data to pass an id that is unique
  among all possibly concurrently outstanding timestamp requests for
  that socket.

  The process can optionally override the default generated ID, by
  passing a specific ID with control message SCM_TS_OPT_ID (not
  supported for TCP sockets)::

    struct msghdr *msg;
    ...
    cmsg                         = CMSG_FIRSTHDR(msg);
    cmsg->cmsg_level                 = SOL_SOCKET;
    cmsg->cmsg_type                 = SCM_TS_OPT_ID;
    cmsg->cmsg_len                 = CMSG_LEN(sizeof(__u32));
    *((__u32 *) CMSG_DATA(cmsg)) = opt_id;
    err = sendmsg(fd, msg, 0);


SOF_TIMESTAMPING_OPT_ID_TCP:
  Pass this modifier along with SOF_TIMESTAMPING_OPT_ID for new TCP
  timestamping applications. SOF_TIMESTAMPING_OPT_ID defines how the
  counter increments for stream sockets, but its starting point is
  not entirely trivial. This option fixes that.

  For stream sockets, if SOF_TIMESTAMPING_OPT_ID is set, this should
  always be set too. On datagram sockets the option has no effect.

  A reasonable expectation is that the counter is reset to zero with
  the system call, so that a subsequent write() of N bytes generates
  a timestamp with counter N-1. SOF_TIMESTAMPING_OPT_ID_TCP
  implements this behavior under all conditions.

  SOF_TIMESTAMPING_OPT_ID without modifier often reports the same,
  especially when the socket option is set when no data is in
  transmission. If data is being transmitted, it may be off by the
  length of the output queue (SIOCOUTQ).

  The difference is due to being based on snd_una versus write_seq.
  snd_una is the offset in the stream acknowledged by the peer. This
  depends on factors outside of process control, such as network RTT.
  write_seq is the last byte written by the process. This offset is
  not affected by external inputs.

  The difference is subtle and unlikely to be noticed when configured
  at initial socket creation, when no data is queued or sent. But
  SOF_TIMESTAMPING_OPT_ID_TCP behavior is more robust regardless of
  when the socket option is set.

SOF_TIMESTAMPING_OPT_CMSG:
  Support recv() cmsg for all timestamped packets. Control messages
  are already supported unconditionally on all packets with receive
  timestamps and on IPv6 packets with transmit timestamp. This option
  extends them to IPv4 packets with transmit timestamp. One use case
  is to correlate packets with their egress device, by enabling socket
  option IP_PKTINFO simultaneously.


SOF_TIMESTAMPING_OPT_TSONLY:
  Applies to transmit timestamps only. Makes the kernel return the
  timestamp as a cmsg alongside an empty packet, as opposed to
  alongside the original packet. This reduces the amount of memory
  charged to the socket's receive budget (SO_RCVBUF) and delivers
  the timestamp even if sysctl net.core.tstamp_allow_data is 0.
  This option disables SOF_TIMESTAMPING_OPT_CMSG.

SOF_TIMESTAMPING_OPT_STATS:
  Optional stats that are obtained along with the transmit timestamps.
  It must be used together with SOF_TIMESTAMPING_OPT_TSONLY. When the
  transmit timestamp is available, the stats are available in a
  separate control message of type SCM_TIMESTAMPING_OPT_STATS, as a
  list of TLVs (struct nlattr) of types. These stats allow the
  application to associate various transport layer stats with
  the transmit timestamps, such as how long a certain block of
  data was limited by peer's receiver window.

SOF_TIMESTAMPING_OPT_PKTINFO:
  Enable the SCM_TIMESTAMPING_PKTINFO control message for incoming
  packets with hardware timestamps. The message contains struct
  scm_ts_pktinfo, which supplies the index of the real interface which
  received the packet and its length at layer 2. A valid (non-zero)
  interface index will be returned only if CONFIG_NET_RX_BUSY_POLL is
  enabled and the driver is using NAPI. The struct contains also two
  other fields, but they are reserved and undefined.

SOF_TIMESTAMPING_OPT_TX_SWHW:
  Request both hardware and software timestamps for outgoing packets
  when SOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_TX_SOFTWARE
  are enabled at the same time. If both timestamps are generated,
  two separate messages will be looped to the socket's error queue,
  each containing just one timestamp.

SOF_TIMESTAMPING_OPT_RX_FILTER:
  Filter out spurious receive timestamps: report a receive timestamp
  only if the matching timestamp generation flag is enabled.

  Receive timestamps are generated early in the ingress path, before a
  packet's destination socket is known. If any socket enables receive
  timestamps, packets for all socket will receive timestamped packets.
  Including those that request timestamp reporting with
  SOF_TIMESTAMPING_SOFTWARE and/or SOF_TIMESTAMPING_RAW_HARDWARE, but
  do not request receive timestamp generation. This can happen when
  requesting transmit timestamps only.

  Receiving spurious timestamps is generally benign. A process can
  ignore the unexpected non-zero value. But it makes behavior subtly
  dependent on other sockets. This flag isolates the socket for more
  deterministic behavior.

New applications are encouraged to pass SOF_TIMESTAMPING_OPT_ID to
disambiguate timestamps and SOF_TIMESTAMPING_OPT_TSONLY to operate
regardless of the setting of sysctl net.core.tstamp_allow_data.

An exception is when a process needs additional cmsg data, for
instance SOL_IP/IP_PKTINFO to detect the egress network interface.
Then pass option SOF_TIMESTAMPING_OPT_CMSG. This option depends on
having access to the contents of the original packet, so cannot be
combined with SOF_TIMESTAMPING_OPT_TSONLY.

Send별 cmsg generation 요청

320-350

Socket option 외에도 `SOF_TIMESTAMPING_TX_*` generation flag는 write별 cmsg로 요청할 수 있습니다. `setsockopt()`를 반복해 enable/disable하는 비용 없이 `sendmsg()`별 timestamp sampling이 가능합니다. 원문의 `msghdr`와 flag 예제를 그대로 보존합니다.

Cmsg로 설정한 `SOF_TIMESTAMPING_TX_*` flag는 `setsockopt()`의 TX generation flag를 덮어씁니다. 다만 timestamp를 실제로 받으려면 `SOF_TIMESTAMPING_SOFTWARE`, `SOF_TIMESTAMPING_OPT_ID` 같은 reporting/option flag를 `setsockopt()`로 계속 enable해야 합니다.

Per-send timestamp 요청
setsockopt: reporting/option enablesendmsg cmsg: TX generation flagPacket별 timestamp 생성Socket error queue 보고

Generation과 reporting 설정 경계를 보여 줍니다.

1.3.4. Enabling timestamps via control messages
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In addition to socket options, timestamp generation can be requested
per write via cmsg, only for SOF_TIMESTAMPING_TX_* (see Section 1.3.1).
Using this feature, applications can sample timestamps per sendmsg()
without paying the overhead of enabling and disabling timestamps via
setsockopt::

  struct msghdr *msg;
  ...
  cmsg                               = CMSG_FIRSTHDR(msg);
  cmsg->cmsg_level               = SOL_SOCKET;
  cmsg->cmsg_type               = SO_TIMESTAMPING;
  cmsg->cmsg_len               = CMSG_LEN(sizeof(__u32));
  *((__u32 *) CMSG_DATA(cmsg)) = SOF_TIMESTAMPING_TX_SCHED |
                                 SOF_TIMESTAMPING_TX_SOFTWARE |
                                 SOF_TIMESTAMPING_TX_ACK;
  err = sendmsg(fd, msg, 0);

The SOF_TIMESTAMPING_TX_* flags set via cmsg will override
the SOF_TIMESTAMPING_TX_* flags set via setsockopt.

Moreover, applications must still enable timestamp reporting via
setsockopt to receive timestamps::

  __u32 val = SOF_TIMESTAMPING_SOFTWARE |
              SOF_TIMESTAMPING_OPT_ID /* or any other flag */;
  err = setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &val, sizeof(val));

Bytestream timestamp 의미

351-411

`SO_TIMESTAMPING`은 bytestream의 byte timestamping을 지원합니다. Request는 buffer 전체 내용이 timestamp point를 지난 시각을 요구하는 것으로 해석합니다. 예를 들어 stream의 `TX_SOFTWARE`는 data가 몇 packet으로 바뀌었는지와 무관하게 모든 byte가 device driver에 도달했을 때 기록합니다.

Bytestream에는 자연스러운 delimiter가 없어 timestamp와 data 연계가 어렵습니다. Byte range는 segment로 분할되거나 합쳐질 수 있고 서로 다른 `send()` buffer 일부가 coalesce될 수 있습니다. Segment는 reorder될 수 있고 retransmission protocol에서는 같은 byte range가 여러 segment에 공존합니다.

이 변환과 관계없이 모든 timestamp가 같은 의미를 구현해야 비교할 수 있습니다. 성능 debugging은 드문 outlier를 다루므로 buffer와 skb가 1:1인 단순 경우와 corner case를 다르게 처리하면 안 됩니다.

Linux는 모든 byte가 point를 지날 때만 timestamp를 생성합니다. IP fragmentation이 첫 fragment만 timestamp하는 정책과 같은 명시적 의미 선택입니다. `TX_ACK`는 구현과 추론이 쉽지만 SACK까지 고려하면 hole과 out-of-order 때문에 복잡해집니다.

TCP의 Nagle, cork, autocork, segmentation과 GSO도 buffer와 skb의 1:1 관계를 깨뜨립니다. 구현은 skb extend/merge 뒤에도 각 `send()`가 넘긴 마지막 byte의 sequence를 `skb_shinfo(skb)->tskey`에 저장합니다. Skb에는 field가 하나뿐이라 timestamp도 하나만 생성할 수 있습니다.

두 request가 같은 skb로 합쳐지면 드물게 timestamp 하나를 놓칠 수 있습니다. `OPT_ID`로 send byte offset과 반환 ID를 비교해 감지합니다. `TCP_NODELAY`를 켜고 `TCP_CORK`와 autocork를 끄거나 Linux 4.7 이후 `sendmsg(MSG_EOR)`로 coalescing을 막을 수 있습니다.

이 조치는 network stack이 stream segment를 reorder하지 않는다는 전제에서 모든 byte가 point를 지난 뒤 timestamp가 생기도록 합니다. Stack은 reordering을 피하지만 관리자가 같은 stream segment에 서로 다른 지연을 주는 packet scheduler를 구성한 예외는 가능합니다.

Bytestream timestamp correlation
send() byte rangeNagle/cork/GSO/mergeskb tskey=요청의 마지막 byte모든 byte가 point 통과OPT_ID로 timestamp 연계

Buffer 변형 뒤에도 마지막 byte sequence를 기준으로 의미를 유지합니다.

1.4 Bytestream Timestamps
-------------------------

The SO_TIMESTAMPING interface supports timestamping of bytes in a
bytestream. Each request is interpreted as a request for when the
entire contents of the buffer has passed a timestamping point. That
is, for streams option SOF_TIMESTAMPING_TX_SOFTWARE will record
when all bytes have reached the device driver, regardless of how
many packets the data has been converted into.

In general, bytestreams have no natural delimiters and therefore
correlating a timestamp with data is non-trivial. A range of bytes
may be split across segments, any segments may be merged (possibly
coalescing sections of previously segmented buffers associated with
independent send() calls). Segments can be reordered and the same
byte range can coexist in multiple segments for protocols that
implement retransmissions.

It is essential that all timestamps implement the same semantics,
regardless of these possible transformations, as otherwise they are
incomparable. Handling "rare" corner cases differently from the
simple case (a 1:1 mapping from buffer to skb) is insufficient
because performance debugging often needs to focus on such outliers.

In practice, timestamps can be correlated with segments of a
bytestream consistently, if both semantics of the timestamp and the
timing of measurement are chosen correctly. This challenge is no
different from deciding on a strategy for IP fragmentation. There, the
definition is that only the first fragment is timestamped. For
bytestreams, we chose that a timestamp is generated only when all
bytes have passed a point. SOF_TIMESTAMPING_TX_ACK as defined is easy to
implement and reason about. An implementation that has to take into
account SACK would be more complex due to possible transmission holes
and out of order arrival.

On the host, TCP can also break the simple 1:1 mapping from buffer to
skbuff as a result of Nagle, cork, autocork, segmentation and GSO. The
implementation ensures correctness in all cases by tracking the
individual last byte passed to send(), even if it is no longer the
last byte after an skbuff extend or merge operation. It stores the
relevant sequence number in skb_shinfo(skb)->tskey. Because an skbuff
has only one such field, only one timestamp can be generated.

In rare cases, a timestamp request can be missed if two requests are
collapsed onto the same skb. A process can detect this situation by
enabling SOF_TIMESTAMPING_OPT_ID and comparing the byte offset at
send time with the value returned for each timestamp. It can prevent
the situation by always flushing the TCP stack in between requests,
for instance by enabling TCP_NODELAY and disabling TCP_CORK and
autocork. After linux-4.7, a better way to prevent coalescing is
to use MSG_EOR flag at sendmsg() time.

These precautions ensure that the timestamp is generated only when all
bytes have passed a timestamp point, assuming that the network stack
itself does not reorder the segments. The stack indeed tries to avoid
reordering. The one exception is under administrator control: it is
possible to construct a packet scheduler configuration that delays
segments from the same stream differently. Such a setup would be
unusual.

Ancillary data와 SCM_TIMESTAMPING record

412-459

Timestamp는 `recvmsg()` ancillary data로 읽습니다. Interface는 `man 3 cmsg`, `SO_TIMESTAMP`와 `SO_TIMESTAMPNS` record 회수는 `man 7 socket`을 참조합니다.

`SCM_TIMESTAMPING` control message는 `cmsg_level=SOL_SOCKET`, `cmsg_type=SCM_TIMESTAMPING`입니다. OLD payload는 세 `timespec`을 가진 `struct scm_timestamping`, NEW payload는 세 `__kernel_timespec`을 가진 `struct scm_timestamping64`입니다. 항상 NEW를 사용해야 하며 32-bit machine에서 OLD는 2038년 이후 잘못됩니다.

구조체는 legacy 이유로 최대 세 timestamp를 반환하지만 항상 하나 이상만 non-zero입니다. 대부분 `ts[0]`, hardware timestamp는 `ts[2]`에 있습니다. `ts[1]`의 system-time 변환 hardware timestamp는 deprecated됐습니다. 대신 NIC의 hardware clock을 PTP clock source로 직접 공개해 userspace가 변환하거나 linuxptp로 system time을 동기화합니다.

`SO_TIMESTAMP` 또는 `SO_TIMESTAMPNS`를 `SOF_TIMESTAMPING_SOFTWARE`와 함께 켜면 실제 software timestamp가 없을 때 `recvmsg()`가 거짓 software timestamp를 만들어 `ts[0]`에 넣을 수 있으며 hardware TX timestamp에서도 발생합니다.

scm_timestamping field
Field의미
ts[0]Software timestamp, 대부분의 timestamp
ts[1]Deprecated system-time-converted hardware timestamp
ts[2]Raw hardware timestamp

Legacy 3-field 구조의 현재 의미입니다.

2 Data Interfaces
==================

Timestamps are read using the ancillary data feature of recvmsg().
See `man 3 cmsg` for details of this interface. The socket manual
page (`man 7 socket`) describes how timestamps generated with
SO_TIMESTAMP and SO_TIMESTAMPNS records can be retrieved.


2.1 SCM_TIMESTAMPING records
----------------------------

These timestamps are returned in a control message with cmsg_level
SOL_SOCKET, cmsg_type SCM_TIMESTAMPING, and payload of type

For SO_TIMESTAMPING_OLD::

        struct scm_timestamping {
                struct timespec ts[3];
        };

For SO_TIMESTAMPING_NEW::

        struct scm_timestamping64 {
                struct __kernel_timespec ts[3];

Always use SO_TIMESTAMPING_NEW timestamp to always get timestamp in
struct scm_timestamping64 format.

SO_TIMESTAMPING_OLD returns incorrect timestamps after the year 2038
on 32 bit machines.

The structure can return up to three timestamps. This is a legacy
feature. At least one field is non-zero at any time. Most timestamps
are passed in ts[0]. Hardware timestamps are passed in ts[2].

ts[1] used to hold hardware timestamps converted to system time.
Instead, expose the hardware clock device on the NIC directly as
a HW PTP clock source, to allow time conversion in userspace and
optionally synchronize system time with a userspace PTP stack such
as linuxptp. For the PTP clock API, see Documentation/driver-api/ptp.rst.

Note that if the SO_TIMESTAMP or SO_TIMESTAMPNS option is enabled
together with SO_TIMESTAMPING using SOF_TIMESTAMPING_SOFTWARE, a false
software timestamp will be generated in the recvmsg() call and passed
in ts[0] when a real software timestamp is missing. This happens also
on hardware transmit timestamps.

TX error queue와 RX recvmsg

460-535

TX timestamp는 outgoing packet을 send timestamp와 함께 socket error queue로 loopback합니다. Process는 충분한 `msg_control` buffer와 `MSG_ERRQUEUE` flag로 `recvmsg()`를 호출합니다. Original outgoing packet과 ancillary message 두 개가 반환됩니다.

첫 message는 `SOL_IP(V6)`/`IP(V6)_RECVERR`의 `struct sock_extended_err`이며 timestamp에서는 `ee_errno=ENOMSG`입니다. 다른 message는 `SOL_SOCKET`/`SCM_TIMESTAMPING`의 `struct scm_timestamping`입니다.

Extended error의 `ee_info`에 있는 `SCM_TSTAMP_*` 값이 실제 timestamp type을 정의합니다. 대부분 `SOF_TIMESTAMPING_*` control field와 1:1입니다. Legacy `SCM_TSTAMP_SND=0`은 TX hardware와 software 모두에 쓰이며 `ts[2]`가 non-zero면 hardware, 아니면 `ts[0]`의 software timestamp입니다.

Outgoing datagram이 fragmentation되면 첫 fragment만 timestamp되고 sending socket에 반환됩니다.

Application이 original payload를 원하지 않으면 error queue에서 더 작은 buffer나 길이 0으로 읽어 payload를 truncate할 수 있습니다. 그러나 `recvmsg()` 전까지 full packet이 queue되어 `SO_RCVBUF` budget을 사용합니다.

Error queue read는 항상 non-blocking입니다. Timestamp를 기다리며 block하려면 `poll()` 또는 `select()`를 사용합니다. Data가 준비되면 `pollfd.revents`에 `POLLERR`가 오며 `pollfd.events`에 요청할 필요가 없습니다.

RX timestamp는 error queue를 읽지 않습니다. 일반 `recvmsg()`의 packet data와 함께 `SCM_TIMESTAMPING` ancillary data가 오며 socket error가 아니므로 `IP(V6)_RECVERROR` message가 없습니다. `ts[0]`은 software, `ts[1]`은 deprecated, `ts[2]`는 hardware timestamp입니다.

Timestamp 반환 경로
TX packetTimestamp 생성Socket error queuerecvmsg(MSG_ERRQUEUE)sock_extended_err + SCM_TIMESTAMPING
RX packetTimestamp 생성Normal receive queuerecvmsg()Packet + SCM_TIMESTAMPING

TX와 RX의 recvmsg 경로가 다릅니다.

2.1.1 Transmit timestamps with MSG_ERRQUEUE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For transmit timestamps the outgoing packet is looped back to the
socket's error queue with the send timestamp(s) attached. A process
receives the timestamps by calling recvmsg() with flag MSG_ERRQUEUE
set and with a msg_control buffer sufficiently large to receive the
relevant metadata structures. The recvmsg call returns the original
outgoing data packet with two ancillary messages attached.

A message of cm_level SOL_IP(V6) and cm_type IP(V6)_RECVERR
embeds a struct sock_extended_err. This defines the error type. For
timestamps, the ee_errno field is ENOMSG. The other ancillary message
will have cm_level SOL_SOCKET and cm_type SCM_TIMESTAMPING. This
embeds the struct scm_timestamping.


2.1.1.2 Timestamp types
~~~~~~~~~~~~~~~~~~~~~~~

The semantics of the three struct timespec are defined by field
ee_info in the extended error structure. It contains a value of
type SCM_TSTAMP_* to define the actual timestamp passed in
scm_timestamping.

The SCM_TSTAMP_* types are 1:1 matches to the SOF_TIMESTAMPING_*
control fields discussed previously, with one exception. For legacy
reasons, SCM_TSTAMP_SND is equal to zero and can be set for both
SOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_TX_SOFTWARE. It
is the first if ts[2] is non-zero, the second otherwise, in which
case the timestamp is stored in ts[0].


2.1.1.3 Fragmentation
~~~~~~~~~~~~~~~~~~~~~

Fragmentation of outgoing datagrams is rare, but is possible, e.g., by
explicitly disabling PMTU discovery. If an outgoing packet is fragmented,
then only the first fragment is timestamped and returned to the sending
socket.


2.1.1.4 Packet Payload
~~~~~~~~~~~~~~~~~~~~~~

The calling application is often not interested in receiving the whole
packet payload that it passed to the stack originally: the socket
error queue mechanism is just a method to piggyback the timestamp on.
In this case, the application can choose to read datagrams with a
smaller buffer, possibly even of length 0. The payload is truncated
accordingly. Until the process calls recvmsg() on the error queue,
however, the full packet is queued, taking up budget from SO_RCVBUF.


2.1.1.5 Blocking Read
~~~~~~~~~~~~~~~~~~~~~

Reading from the error queue is always a non-blocking operation. To
block waiting on a timestamp, use poll or select. poll() will return
POLLERR in pollfd.revents if any data is ready on the error queue.
There is no need to pass this flag in pollfd.events. This flag is
ignored on request. See also `man 2 poll`.


2.1.2 Receive timestamps
^^^^^^^^^^^^^^^^^^^^^^^^

On reception, there is no reason to read from the socket error queue.
The SCM_TIMESTAMPING ancillary data is sent along with the packet data
on a normal recvmsg(). Since this is not a socket error, it is not
accompanied by a message SOL_IP(V6)/IP(V6)_RECVERROR. In this case,
the meaning of the three fields in struct scm_timestamping is
implicitly defined. ts[0] holds a software timestamp if set, ts[1]
is again deprecated and ts[2] holds a hardware timestamp if set.

ETHTOOL_MSG_TSCONFIG hardware 설정

536-625

Hardware timestamp를 수행할 device driver마다 별도 초기화가 필요합니다. `include/uapi/linux/net_tstamp.h`의 `struct hwtstamp_config`는 현재 반드시 zero인 `flags`, `HWTSTAMP_TX_*`의 `tx_type`, `HWTSTAMP_FILTER_*`의 `rx_filter`를 가집니다.

원하는 동작은 tsconfig netlink `ETHTOOL_MSG_TSCONFIG_SET`으로 특정 device에 전달합니다. `ETHTOOL_A_TSCONFIG_TX_TYPES`, `ETHTOOL_A_TSCONFIG_RX_FILTERS`, `ETHTOOL_A_TSCONFIG_HWTSTAMP_FLAGS` attribute가 구조체를 설정합니다. Nested `ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER`는 device source index와 timestamp type qualifier로 hardware source를 고릅니다.

Driver는 요청보다 더 permissive한 설정을 사용할 수 있고 hardware가 직접 지원하는 가장 generic mode를 구현하는 것이 좋습니다. 예를 들어 `HWTSTAMP_FILTER_PTP_V2_EVENT`를 지원하면 더 좁은 `HWTSTAMP_FILTER_PTP_V2_L2_SYNC` 요청을 generic event mode로 확장하는 편이 유용합니다.

Hardware timestamp driver는 실제 설정으로 구조체를 갱신해야 합니다. 요청 packet을 timestamp할 수 없으면 아무것도 바꾸지 않고 `ERANGE`를 반환합니다. `EINVAL`은 `SIOCSHWTSTAMP` 자체를 지원하지 않는다는 뜻입니다.

Admin 권한 process만 설정을 바꿀 수 있으며 여러 process 충돌 방지와 설정 reset은 userspace 책임입니다. 어떤 process든 `ETHTOOL_MSG_TSCONFIG_GET`으로 실제 설정을 읽을 수 있습니다.

Legacy 방식은 `ifreq.ifr_data`가 `hwtstamp_config`를 가리키는 `ioctl(SIOCSHWTSTAMP)`입니다. `tx_type`과 `rx_filter`는 hint이며 fine-grained RX filter를 지원하지 않으면 더 많은 packet을 timestamp할 수 있습니다. `SIOCGHWTSTAMP` read 방식은 모든 driver에 구현되지는 않았습니다.

hwtstamp_config 값
의미
HWTSTAMP_TX_OFFOutgoing hardware timestamp 비활성
HWTSTAMP_TX_ONSender request에 따라 outgoing hardware timestamp
HWTSTAMP_FILTER_NONEIncoming timestamp 없음
HWTSTAMP_FILTER_ALL모든 incoming packet
HWTSTAMP_FILTER_SOME요청한 packet과 일부 추가 packet
HWTSTAMP_FILTER_PTP_V1_L4_EVENTPTP v1 UDP event packet

대표 TX type과 RX filter 의미입니다.

3. Hardware Timestamping configuration: ETHTOOL_MSG_TSCONFIG_SET/GET
====================================================================

Hardware time stamping must also be initialized for each device driver
that is expected to do hardware time stamping. The parameter is defined in
include/uapi/linux/net_tstamp.h as::

        struct hwtstamp_config {
                int flags;        /* no flags defined right now, must be zero */
                int tx_type;        /* HWTSTAMP_TX_* */
                int rx_filter;        /* HWTSTAMP_FILTER_* */
        };

Desired behavior is passed into the kernel and to a specific device by
calling the tsconfig netlink socket ``ETHTOOL_MSG_TSCONFIG_SET``.
The ``ETHTOOL_A_TSCONFIG_TX_TYPES``, ``ETHTOOL_A_TSCONFIG_RX_FILTERS`` and
``ETHTOOL_A_TSCONFIG_HWTSTAMP_FLAGS`` netlink attributes are then used to set
the struct hwtstamp_config accordingly.

The ``ETHTOOL_A_TSCONFIG_HWTSTAMP_PROVIDER`` netlink nested attribute is used
to select the source of the hardware time stamping. It is composed of an index
for the device source and a qualifier for the type of time stamping.

Drivers are free to use a more permissive configuration than the requested
configuration. It is expected that drivers should only implement directly the
most generic mode that can be supported. For example if the hardware can
support HWTSTAMP_FILTER_PTP_V2_EVENT, then it should generally always upscale
HWTSTAMP_FILTER_PTP_V2_L2_SYNC, and so forth, as HWTSTAMP_FILTER_PTP_V2_EVENT
is more generic (and more useful to applications).

A driver which supports hardware time stamping shall update the struct
with the actual, possibly more permissive configuration. If the
requested packets cannot be time stamped, then nothing should be
changed and ERANGE shall be returned (in contrast to EINVAL, which
indicates that SIOCSHWTSTAMP is not supported at all).

Only a processes with admin rights may change the configuration. User
space is responsible to ensure that multiple processes don't interfere
with each other and that the settings are reset.

Any process can read the actual configuration by requesting tsconfig netlink
socket ``ETHTOOL_MSG_TSCONFIG_GET``.

The legacy configuration is the use of the ioctl(SIOCSHWTSTAMP) with a pointer
to a struct ifreq whose ifr_data points to a struct hwtstamp_config.
The tx_type and rx_filter are hints to the driver what it is expected to do.
If the requested fine-grained filtering for incoming packets is not
supported, the driver may time stamp more than just the requested types
of packets. ioctl(SIOCGHWTSTAMP) is used in the same way as the
ioctl(SIOCSHWTSTAMP). However, this has not been implemented in all drivers.

::

    /* possible values for hwtstamp_config->tx_type */
    enum {
            /*
            * no outgoing packet will need hardware time stamping;
            * should a packet arrive which asks for it, no hardware
            * time stamping will be done
            */
            HWTSTAMP_TX_OFF,

            /*
            * enables hardware time stamping for outgoing packets;
            * the sender of the packet decides which are to be
            * time stamped by setting SOF_TIMESTAMPING_TX_SOFTWARE
            * before sending the packet
            */
            HWTSTAMP_TX_ON,
    };

    /* possible values for hwtstamp_config->rx_filter */
    enum {
            /* time stamp no incoming packet at all */
            HWTSTAMP_FILTER_NONE,

            /* time stamp any incoming packet */
            HWTSTAMP_FILTER_ALL,

            /* return value: time stamp all packets requested plus some others */
            HWTSTAMP_FILTER_SOME,

            /* PTP v1, UDP, any kind of event packet */
            HWTSTAMP_FILTER_PTP_V1_L4_EVENT,

            /* for the complete list of values, please check
            * the include file include/uapi/linux/net_tstamp.h
            */
    };

Device driver 구현 책임

626-674

Hardware timestamp driver는 `ndo_hwtstamp_set` NDO 또는 legacy `SIOCSHWTSTAMP`를 지원하고 실제 값으로 `hwtstamp_config`를 갱신해야 합니다. `ndo_hwtstamp_get` 또는 legacy `SIOCGHWTSTAMP`도 지원해야 합니다.

RX packet timestamp는 skb에 저장합니다. `skb_hwtstamps()`로 shared `struct skb_shared_hwtstamps`를 얻어 arbitrary origin 이후 duration인 `ktime_t hwtstamp`를 설정합니다.

TX에서는 `hard_start_xmit()`에서 `skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP`를 검사합니다. 가능하고 요청됐다면 `SKBTX_IN_PROGRESS`를 설정해 driver가 hardware timestamping을 수행 중임을 선언하고, 필요하면 다음 단계를 위해 skb pointer를 보관해 아직 free하지 않습니다. Hardware timestamp를 지원하지 않는 driver는 이 flag를 설정하지 않습니다.

Driver는 network subsystem의 software timestamp 저장소인 `sk_buff::tstamp`를 절대 건드리면 안 됩니다.

Hardware에 skb를 넘기기 직전 가능한 가까운 곳에서 `skb_tx_timestamp()`를 호출합니다. 요청됐지만 `SKBTX_IN_PROGRESS`가 없어 hardware가 불가능하면 이 함수가 software timestamp를 제공합니다.

Packet 전송 또는 hardware timestamp 획득 뒤 original skb와 raw timestamp로 `skb_tstamp_tx()`를 호출합니다. 이 함수가 original skb를 clone하고 timestamp를 붙이므로 original은 이제 free해야 합니다. Hardware timestamp 획득 실패 뒤 software timestamp로 fallback하면 pipeline의 늦은 지점에서 찍혀 다른 software timestamp와 예상 밖 delta를 만들 수 있으므로 fallback하면 안 됩니다.

Driver TX timestamp lifecycle
SKBTX_HW_TSTAMP 검사가능하면 SKBTX_IN_PROGRESSskb_tx_timestamp()Hardware transmit/completeskb_tstamp_tx(original, raw)Original skb free

skb 소유권과 timestamp 반환 순서를 보존합니다.

3.1 Hardware Timestamping Implementation: Device Drivers
--------------------------------------------------------

A driver which supports hardware time stamping must support the
ndo_hwtstamp_set NDO or the legacy SIOCSHWTSTAMP ioctl and update the
supplied struct hwtstamp_config with the actual values as described in
the section on SIOCSHWTSTAMP. It should also support ndo_hwtstamp_get or
the legacy SIOCGHWTSTAMP.

Time stamps for received packets must be stored in the skb. To get a pointer
to the shared time stamp structure of the skb call skb_hwtstamps(). Then
set the time stamps in the structure::

    struct skb_shared_hwtstamps {
            /* hardware time stamp transformed into duration
            * since arbitrary point in time
            */
            ktime_t        hwtstamp;
    };

Time stamps for outgoing packets are to be generated as follows:

- In hard_start_xmit(), check if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
  is set no-zero. If yes, then the driver is expected to do hardware time
  stamping.
- If this is possible for the skb and requested, then declare
  that the driver is doing the time stamping by setting the flag
  SKBTX_IN_PROGRESS in skb_shinfo(skb)->tx_flags , e.g. with::

      skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;

  You might want to keep a pointer to the associated skb for the next step
  and not free the skb. A driver not supporting hardware time stamping doesn't
  do that. A driver must never touch sk_buff::tstamp! It is used to store
  software generated time stamps by the network subsystem.
- Driver should call skb_tx_timestamp() as close to passing sk_buff to hardware
  as possible. skb_tx_timestamp() provides a software time stamp if requested
  and hardware timestamping is not possible (SKBTX_IN_PROGRESS not set).
- As soon as the driver has sent the packet and/or obtained a
  hardware time stamp for it, it passes the time stamp back by
  calling skb_tstamp_tx() with the original skb, the raw
  hardware time stamp. skb_tstamp_tx() clones the original skb and
  adds the timestamps, therefore the original skb has to be freed now.
  If obtaining the hardware time stamp somehow fails, then the driver
  should not fall back to software time stamping. The rationale is that
  this would occur at a later time in the processing pipeline than other
  software time stamping and therefore could lead to unexpected deltas
  between time stamps.

Stacked PTP Hardware Clock 원칙

675-684

Packet data path에 PHC(PTP Hardware Clock)가 여러 개 있을 수 있습니다. Kernel에는 Ethernet frame timestamping에 사용할 PHC를 userspace가 직접 고르는 mechanism이 없습니다. 대신 wire에 가장 바깥쪽인 PHC가 항상 가장 바람직하다고 가정하고 driver들이 협력합니다. 현재 stacked PHC case는 DSA switch, Ethernet PHY, MII bus snooping device 세 가지입니다.

3.2 Special considerations for stacked PTP Hardware Clocks
----------------------------------------------------------

There are situations when there may be more than one PHC (PTP Hardware Clock)
in the data path of a packet. The kernel has no explicit mechanism to allow the
user to select which PHC to use for timestamping Ethernet frames. Instead, the
assumption is that the outermost PHC is always the most preferable, and that
kernel drivers collaborate towards achieving that goal. Currently there are 3
cases of stacked PHCs, detailed below:

DSA switch의 outermost PHC

685-753

DSA switch는 port 하나가 PTP를 모르는 host Ethernet interface에 연결되고 port multiplier와 optional forwarding acceleration 역할을 합니다. 각 switch port는 독립 virtual network interface로 보이지만 실제 I/O는 TX에서 host port로 redirect되고 RX frame을 intercept하는 방식으로 host interface를 통합니다.

Switch의 variable queue delay는 host port와 PTP partner 사이 path jitter를 만들어 synchronization을 해칩니다. 일부 DSA switch는 자체 clock과 MAC timestamping으로 wire와 PHY propagation latency만 측정합니다. Linux는 이를 일반 network interface ABI로 제공하며 virtual I/O port여도 자체 PHC를 가집니다. 보통 switch port들이 PHC를 공유하지만 필수는 아닙니다.

DSA switch를 붙인 host port driver에는 특별한 처리가 필요 없습니다. Host port도 PTP를 지원하면 DSA가 host의 `.ndo_eth_ioctl` 호출을 intercept해 hardware timestamp enable을 막습니다. `SO_TIMESTAMPING` API는 packet 하나에 hardware timestamp 여러 개를 전달할 수 없으므로 DSA port만 생성해야 합니다.

`.port_txtstamp()`는 userspace가 hardware TX timestamp를 요청한 packet 전송 전에 호출됩니다. Two-step timestamp는 MAC 전송 뒤에 값이 나오므로 original packet과 값을 연계해 error queue로 다시 넣어야 합니다. Driver는 `skb_clone_sk()`로 clone해 `skb->cb`와 TX skb queue에 보관할 수 있습니다.

Switch의 timestamp register/FIFO가 PTP sequence ID, message type, domain과 timestamp pair를 제공할 수 있습니다. Driver는 `ptp_classify_raw()`로 transport를 분류하고 `ptp_parse_header()`로 header를 읽어 대기 packet과 값을 연계합니다. Timestamp IRQ를 쓰거나 host interface로 `dev_queue_xmit()`한 뒤 poll할 수 있습니다.

One-step TX는 MAC이 packet에 timestamp를 넣고 PTP follow-up message가 필요 없으므로 clone할 필요가 없고 userspace도 annotated packet의 error queue 반환을 기대하지 않습니다.

`.port_rxtstamp()`에서 DSA는 BPF classifier로 PTP event message만 식별합니다. Driver는 original skb에 즉시 timestamp를 붙이거나 나중으로 미룹니다. Timestamp는 DSA header metadata 같은 in-band 방식이나 RX FIFO 같은 out-of-band 방식일 수 있습니다. Sleepable context가 필요해 defer했다면 DSA driver가 새 timestamp를 붙인 skb에 `netif_rx()`를 호출해야 합니다.

DSA timestamp hook
Hook입력Driver 책임
port_txtstamp()HW TX request packetClone/queue, PTP key와 timestamp correlation, error queue 반환
port_rxtstamp()PTP event skbIn-band/out-of-band timestamp, defer 뒤 netif_rx()

TX two-step과 RX defer 책임입니다.

3.2.1 DSA (Distributed Switch Architecture) switches
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

These are Ethernet switches which have one of their ports connected to an
(otherwise completely unaware) host Ethernet interface, and perform the role of
a port multiplier with optional forwarding acceleration features.  Each DSA
switch port is visible to the user as a standalone (virtual) network interface,
and its network I/O is performed, under the hood, indirectly through the host
interface (redirecting to the host port on TX, and intercepting frames on RX).

When a DSA switch is attached to a host port, PTP synchronization has to
suffer, since the switch's variable queuing delay introduces a path delay
jitter between the host port and its PTP partner. For this reason, some DSA
switches include a timestamping clock of their own, and have the ability to
perform network timestamping on their own MAC, such that path delays only
measure wire and PHY propagation latencies. Timestamping DSA switches are
supported in Linux and expose the same ABI as any other network interface (save
for the fact that the DSA interfaces are in fact virtual in terms of network
I/O, they do have their own PHC).  It is typical, but not mandatory, for all
interfaces of a DSA switch to share the same PHC.

By design, PTP timestamping with a DSA switch does not need any special
handling in the driver for the host port it is attached to.  However, when the
host port also supports PTP timestamping, DSA will take care of intercepting
the ``.ndo_eth_ioctl`` calls towards the host port, and block attempts to enable
hardware timestamping on it. This is because the SO_TIMESTAMPING API does not
allow the delivery of multiple hardware timestamps for the same packet, so
anybody else except for the DSA switch port must be prevented from doing so.

In the generic layer, DSA provides the following infrastructure for PTP
timestamping:

- ``.port_txtstamp()``: a hook called prior to the transmission of
  packets with a hardware TX timestamping request from user space.
  This is required for two-step timestamping, since the hardware
  timestamp becomes available after the actual MAC transmission, so the
  driver must be prepared to correlate the timestamp with the original
  packet so that it can re-enqueue the packet back into the socket's
  error queue. To save the packet for when the timestamp becomes
  available, the driver can call ``skb_clone_sk`` , save the clone pointer
  in skb->cb and enqueue a tx skb queue. Typically, a switch will have a
  PTP TX timestamp register (or sometimes a FIFO) where the timestamp
  becomes available. In case of a FIFO, the hardware might store
  key-value pairs of PTP sequence ID/message type/domain number and the
  actual timestamp. To perform the correlation correctly between the
  packets in a queue waiting for timestamping and the actual timestamps,
  drivers can use a BPF classifier (``ptp_classify_raw``) to identify
  the PTP transport type, and ``ptp_parse_header`` to interpret the PTP
  header fields. There may be an IRQ that is raised upon this
  timestamp's availability, or the driver might have to poll after
  invoking ``dev_queue_xmit()`` towards the host interface.
  One-step TX timestamping do not require packet cloning, since there is
  no follow-up message required by the PTP protocol (because the
  TX timestamp is embedded into the packet by the MAC), and therefore
  user space does not expect the packet annotated with the TX timestamp
  to be re-enqueued into its socket's error queue.

- ``.port_rxtstamp()``: On RX, the BPF classifier is run by DSA to
  identify PTP event messages (any other packets, including PTP general
  messages, are not timestamped). The original (and only) timestampable
  skb is provided to the driver, for it to annotate it with a timestamp,
  if that is immediately available, or defer to later. On reception,
  timestamps might either be available in-band (through metadata in the
  DSA header, or attached in other ways to the packet), or out-of-band
  (through another RX timestamping FIFO). Deferral on RX is typically
  necessary when retrieving the timestamp needs a sleepable context. In
  that case, it is the responsibility of the DSA driver to call
  ``netif_rx()`` on the freshly timestamped skb.

Ethernet PHY timestamping

754-800

Ethernet PHY는 보통 Layer 1 장치라 DSA switch처럼 network interface 표현이 없습니다. 그래도 wire에 가까운 timestamp가 더 안정적이고 정밀할 수 있어 PTP packet을 감지·timestamp할 수 있습니다.

PTP timestamping PHY driver는 `struct mii_timestamper`를 만들고 pointer를 `phydev->mii_ts`에 넣어 network stack이 발견하게 합니다.

PHY에는 network interface가 없으므로 timestamping과 ethtool ioctl은 MAC driver가 중계해야 합니다. 각 MAC driver는 `.ndo_eth_ioctl`에서 `phy_has_hwtstamp(netdev->phydev)`를 검사하고 true면 직접 처리하지 말고 `phy_mii_ioctl()`로 PHY에 넘깁니다.

Plain `netif_rx()` 계열 RX에서는 MAC이 `skb_defer_rx_timestamp(skb)`가 필요한지 검사하고 필요하면 `netif_rx()`를 호출하지 않습니다. `CONFIG_NETWORK_PHY_TIMESTAMPING`과 `skb->dev->phydev->mii_ts`가 있으면 `.rxtstamp()`가 defer 필요성을 판단하며, defer 시 timestamp 준비 뒤 stack에 packet을 올리는 것은 PHY driver 책임입니다.

`napi_gro_receive()`나 `netif_receive_skb()`는 stack이 자동으로 defer를 검사하므로 MAC driver 검사가 필요 없습니다.

TX hook `mii_ts->txtstamp()`는 `skb_clone_tx_timestamp()`가 호출합니다. MAC이 직접 호출할 수도 있지만 많은 MAC driver가 software timestamp를 위해 호출하는 `skb_tx_timestamp()`에서도 연계됩니다. 따라서 software timestamp를 지원하는 MAC은 보통 추가 동작이 필요 없습니다.

PHY timestamp 중계
Userspace ioctlMAC ndo_eth_ioctlphy_has_hwtstampphy_mii_ioctlPHY mii_timestamper
RX skbskb_defer_rx_timestampPHY rxtstampTimestamp 준비Stack 전달

Network interface가 없는 PHY를 MAC과 stack이 연결합니다.

3.2.2 Ethernet PHYs
^^^^^^^^^^^^^^^^^^^

These are devices that typically fulfill a Layer 1 role in the network stack,
hence they do not have a representation in terms of a network interface as DSA
switches do. However, PHYs may be able to detect and timestamp PTP packets, for
performance reasons: timestamps taken as close as possible to the wire have the
potential to yield a more stable and precise synchronization.

A PHY driver that supports PTP timestamping must create a ``struct
mii_timestamper`` and add a pointer to it in ``phydev->mii_ts``. The presence
of this pointer will be checked by the networking stack.

Since PHYs do not have network interface representations, the timestamping and
ethtool ioctl operations for them need to be mediated by their respective MAC
driver.  Therefore, as opposed to DSA switches, modifications need to be done
to each individual MAC driver for PHY timestamping support. This entails:

- Checking, in ``.ndo_eth_ioctl``, whether ``phy_has_hwtstamp(netdev->phydev)``
  is true or not. If it is, then the MAC driver should not process this request
  but instead pass it on to the PHY using ``phy_mii_ioctl()``.

- On RX, special intervention may or may not be needed, depending on the
  function used to deliver skb's up the network stack. In the case of plain
  ``netif_rx()`` and similar, MAC drivers must check whether
  ``skb_defer_rx_timestamp(skb)`` is necessary or not - and if it is, don't
  call ``netif_rx()`` at all.  If ``CONFIG_NETWORK_PHY_TIMESTAMPING`` is
  enabled, and ``skb->dev->phydev->mii_ts`` exists, its ``.rxtstamp()`` hook
  will be called now, to determine, using logic very similar to DSA, whether
  deferral for RX timestamping is necessary.  Again like DSA, it becomes the
  responsibility of the PHY driver to send the packet up the stack when the
  timestamp is available.

  For other skb receive functions, such as ``napi_gro_receive`` and
  ``netif_receive_skb``, the stack automatically checks whether
  ``skb_defer_rx_timestamp()`` is necessary, so this check is not needed inside
  the driver.

- On TX, again, special intervention might or might not be needed.  The
  function that calls the ``mii_ts->txtstamp()`` hook is named
  ``skb_clone_tx_timestamp()``. This function can either be called directly
  (case in which explicit MAC driver support is indeed needed), but the
  function also piggybacks from the ``skb_tx_timestamp()`` call, which many MAC
  drivers already perform for software timestamping purposes. Therefore, if a
  MAC supports software timestamping, it does not need to do anything further
  at this stage.

MII bus snooping timestamp device

801-810

MII bus snooping device는 timestamping Ethernet PHY와 같은 역할을 하지만 discrete device라 timestamp 기능이 없는 어떤 PHY와도 함께 쓸 수 있습니다. Device Tree로 `struct phy_device`에 발견·연결되고 나머지는 같은 `mii_ts` infrastructure를 사용합니다. 자세한 binding path `Documentation/devicetree/bindings/ptp/timestamper.txt`를 보존합니다.

3.2.3 MII bus snooping devices
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

These perform the same role as timestamping Ethernet PHYs, save for the fact
that they are discrete devices and can therefore be used in conjunction with
any PHY even if it doesn't support timestamping. In Linux, they are
discoverable and attachable to a ``struct phy_device`` through Device Tree, and
for the rest, they use the same mii_ts infrastructure as those. See
Documentation/devicetree/bindings/ptp/timestamper.txt for more details.

Stacked PHC에서 MAC duplicate 방지

811-851

Stacked PHC는 단독 MAC 환경에서는 드러나지 않던 driver bug를 노출할 수 있습니다. Plain MAC, DSA, PHY, MII snooper 등 TX timestamp logic은 모두 `skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS`를 설정할 수 있습니다. PHC stacking을 모르는 MAC이 자신이 아닌 entity가 설정한 flag를 자기 request로 오인하면 duplicate timestamp를 전달합니다.

일반적인 TX 단계는 `.ndo_eth_ioctl`로 PTP가 enable됐는지 `priv->hwtstamp_tx_enabled`를 보고 현재 skb에 `SKBTX_HW_TSTAMP` request가 있는지 검사해 `SKBTX_IN_PROGRESS`를 설정합니다. Stacked system에서는 MAC이 outermost PHC가 아니므로 이 조건은 발생하지 않아야 합니다.

문제는 TX confirmation에서 MAC이 timestamp 회수 필요성을 `SKBTX_IN_PROGRESS` 하나만 보고 판단하는 shortcut입니다. 이 flag는 data path의 다른 timestamp entity가 설정했을 수 있습니다.

올바른 해결은 confirmation에서 `SKBTX_IN_PROGRESS`뿐 아니라 `priv->hwtstamp_tx_enabled == true`도 함께 검사하는 것입니다. System은 outermost PHC 이외에는 PTP timestamp를 enable하지 않으므로 compound check가 duplicate TX timestamp의 userspace 전달을 막습니다.

MAC TX confirmation 조건
검사판정
SKBTX_IN_PROGRESS만잘못됨: 다른 PHC가 설정했을 수 있음
SKBTX_IN_PROGRESS && hwtstamp_tx_enabled올바름: MAC 자신의 활성 request만 회수

Stacked PHC에서 flag 소유자를 구분합니다.

3.2.4 Other caveats for MAC drivers
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The use of stacked PHCs may uncover MAC driver bugs which were impossible to
trigger without them. One example has to do with this line of code, already
presented earlier::

      skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;

Any TX timestamping logic, be it a plain MAC driver, a DSA switch driver, a PHY
driver or a MII bus snooping device driver, should set this flag.
But a MAC driver that is unaware of PHC stacking might get tripped up by
somebody other than itself setting this flag, and deliver a duplicate
timestamp.
For example, a typical driver design for TX timestamping might be to split the
transmission part into 2 portions:

1. "TX": checks whether PTP timestamping has been previously enabled through
   the ``.ndo_eth_ioctl`` ("``priv->hwtstamp_tx_enabled == true``") and the
   current skb requires a TX timestamp ("``skb_shinfo(skb)->tx_flags &
   SKBTX_HW_TSTAMP``"). If this is true, it sets the
   "``skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS``" flag. Note: as
   described above, in the case of a stacked PHC system, this condition should
   never trigger, as this MAC is certainly not the outermost PHC. But this is
   not where the typical issue is.  Transmission proceeds with this packet.

2. "TX confirmation": Transmission has finished. The driver checks whether it
   is necessary to collect any TX timestamp for it. Here is where the typical
   issues are: the MAC driver takes a shortcut and only checks whether
   "``skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS``" was set. With a stacked
   PHC system, this is incorrect because this MAC driver is not the only entity
   in the TX data path who could have enabled SKBTX_IN_PROGRESS in the first
   place.

The correct solution for this problem is for MAC drivers to have a compound
check in their "TX confirmation" portion, not only for
"``skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS``", but also for
"``priv->hwtstamp_tx_enabled == true``". Because the rest of the system ensures
that PTP timestamping is not enabled for anything other than the outermost PHC,
this enhanced check will avoid delivering a duplicated TX timestamp to user
space.