timestamping.txt 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. The existing interfaces for getting network packages time stamped are:
  2. * SO_TIMESTAMP
  3. Generate time stamp for each incoming packet using the (not necessarily
  4. monotonous!) system time. Result is returned via recv_msg() in a
  5. control message as timeval (usec resolution).
  6. * SO_TIMESTAMPNS
  7. Same time stamping mechanism as SO_TIMESTAMP, but returns result as
  8. timespec (nsec resolution).
  9. * IP_MULTICAST_LOOP + SO_TIMESTAMP[NS]
  10. Only for multicasts: approximate send time stamp by receiving the looped
  11. packet and using its receive time stamp.
  12. The following interface complements the existing ones: receive time
  13. stamps can be generated and returned for arbitrary packets and much
  14. closer to the point where the packet is really sent. Time stamps can
  15. be generated in software (as before) or in hardware (if the hardware
  16. has such a feature).
  17. SO_TIMESTAMPING:
  18. Instructs the socket layer which kind of information is wanted. The
  19. parameter is an integer with some of the following bits set. Setting
  20. other bits is an error and doesn't change the current state.
  21. SOF_TIMESTAMPING_TX_HARDWARE: try to obtain send time stamp in hardware
  22. SOF_TIMESTAMPING_TX_SOFTWARE: if SOF_TIMESTAMPING_TX_HARDWARE is off or
  23. fails, then do it in software
  24. SOF_TIMESTAMPING_RX_HARDWARE: return the original, unmodified time stamp
  25. as generated by the hardware
  26. SOF_TIMESTAMPING_RX_SOFTWARE: if SOF_TIMESTAMPING_RX_HARDWARE is off or
  27. fails, then do it in software
  28. SOF_TIMESTAMPING_RAW_HARDWARE: return original raw hardware time stamp
  29. SOF_TIMESTAMPING_SYS_HARDWARE: return hardware time stamp transformed to
  30. the system time base
  31. SOF_TIMESTAMPING_SOFTWARE: return system time stamp generated in
  32. software
  33. SOF_TIMESTAMPING_TX/RX determine how time stamps are generated.
  34. SOF_TIMESTAMPING_RAW/SYS determine how they are reported in the
  35. following control message:
  36. struct scm_timestamping {
  37. struct timespec systime;
  38. struct timespec hwtimetrans;
  39. struct timespec hwtimeraw;
  40. };
  41. recvmsg() can be used to get this control message for regular incoming
  42. packets. For send time stamps the outgoing packet is looped back to
  43. the socket's error queue with the send time stamp(s) attached. It can
  44. be received with recvmsg(flags=MSG_ERRQUEUE). The call returns the
  45. original outgoing packet data including all headers preprended down to
  46. and including the link layer, the scm_timestamping control message and
  47. a sock_extended_err control message with ee_errno==ENOMSG and
  48. ee_origin==SO_EE_ORIGIN_TIMESTAMPING. A socket with such a pending
  49. bounced packet is ready for reading as far as select() is concerned.
  50. If the outgoing packet has to be fragmented, then only the first
  51. fragment is time stamped and returned to the sending socket.
  52. All three values correspond to the same event in time, but were
  53. generated in different ways. Each of these values may be empty (= all
  54. zero), in which case no such value was available. If the application
  55. is not interested in some of these values, they can be left blank to
  56. avoid the potential overhead of calculating them.
  57. systime is the value of the system time at that moment. This
  58. corresponds to the value also returned via SO_TIMESTAMP[NS]. If the
  59. time stamp was generated by hardware, then this field is
  60. empty. Otherwise it is filled in if SOF_TIMESTAMPING_SOFTWARE is
  61. set.
  62. hwtimeraw is the original hardware time stamp. Filled in if
  63. SOF_TIMESTAMPING_RAW_HARDWARE is set. No assumptions about its
  64. relation to system time should be made.
  65. hwtimetrans is the hardware time stamp transformed so that it
  66. corresponds as good as possible to system time. This correlation is
  67. not perfect; as a consequence, sorting packets received via different
  68. NICs by their hwtimetrans may differ from the order in which they were
  69. received. hwtimetrans may be non-monotonic even for the same NIC.
  70. Filled in if SOF_TIMESTAMPING_SYS_HARDWARE is set. Requires support
  71. by the network device and will be empty without that support.
  72. SIOCSHWTSTAMP:
  73. Hardware time stamping must also be initialized for each device driver
  74. that is expected to do hardware time stamping. The parameter is defined in
  75. /include/linux/net_tstamp.h as:
  76. struct hwtstamp_config {
  77. int flags; /* no flags defined right now, must be zero */
  78. int tx_type; /* HWTSTAMP_TX_* */
  79. int rx_filter; /* HWTSTAMP_FILTER_* */
  80. };
  81. Desired behavior is passed into the kernel and to a specific device by
  82. calling ioctl(SIOCSHWTSTAMP) with a pointer to a struct ifreq whose
  83. ifr_data points to a struct hwtstamp_config. The tx_type and
  84. rx_filter are hints to the driver what it is expected to do. If
  85. the requested fine-grained filtering for incoming packets is not
  86. supported, the driver may time stamp more than just the requested types
  87. of packets.
  88. A driver which supports hardware time stamping shall update the struct
  89. with the actual, possibly more permissive configuration. If the
  90. requested packets cannot be time stamped, then nothing should be
  91. changed and ERANGE shall be returned (in contrast to EINVAL, which
  92. indicates that SIOCSHWTSTAMP is not supported at all).
  93. Only a processes with admin rights may change the configuration. User
  94. space is responsible to ensure that multiple processes don't interfere
  95. with each other and that the settings are reset.
  96. /* possible values for hwtstamp_config->tx_type */
  97. enum {
  98. /*
  99. * no outgoing packet will need hardware time stamping;
  100. * should a packet arrive which asks for it, no hardware
  101. * time stamping will be done
  102. */
  103. HWTSTAMP_TX_OFF,
  104. /*
  105. * enables hardware time stamping for outgoing packets;
  106. * the sender of the packet decides which are to be
  107. * time stamped by setting SOF_TIMESTAMPING_TX_SOFTWARE
  108. * before sending the packet
  109. */
  110. HWTSTAMP_TX_ON,
  111. };
  112. /* possible values for hwtstamp_config->rx_filter */
  113. enum {
  114. /* time stamp no incoming packet at all */
  115. HWTSTAMP_FILTER_NONE,
  116. /* time stamp any incoming packet */
  117. HWTSTAMP_FILTER_ALL,
  118. /* return value: time stamp all packets requested plus some others */
  119. HWTSTAMP_FILTER_SOME,
  120. /* PTP v1, UDP, any kind of event packet */
  121. HWTSTAMP_FILTER_PTP_V1_L4_EVENT,
  122. /* for the complete list of values, please check
  123. * the include file /include/linux/net_tstamp.h
  124. */
  125. };
  126. DEVICE IMPLEMENTATION
  127. A driver which supports hardware time stamping must support the
  128. SIOCSHWTSTAMP ioctl and update the supplied struct hwtstamp_config with
  129. the actual values as described in the section on SIOCSHWTSTAMP.
  130. Time stamps for received packets must be stored in the skb. To get a pointer
  131. to the shared time stamp structure of the skb call skb_hwtstamps(). Then
  132. set the time stamps in the structure:
  133. struct skb_shared_hwtstamps {
  134. /* hardware time stamp transformed into duration
  135. * since arbitrary point in time
  136. */
  137. ktime_t hwtstamp;
  138. ktime_t syststamp; /* hwtstamp transformed to system time base */
  139. };
  140. Time stamps for outgoing packets are to be generated as follows:
  141. - In hard_start_xmit(), check if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
  142. is set no-zero. If yes, then the driver is expected to do hardware time
  143. stamping.
  144. - If this is possible for the skb and requested, then declare
  145. that the driver is doing the time stamping by setting the flag
  146. SKBTX_IN_PROGRESS in skb_shinfo(skb)->tx_flags , e.g. with
  147. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  148. You might want to keep a pointer to the associated skb for the next step
  149. and not free the skb. A driver not supporting hardware time stamping doesn't
  150. do that. A driver must never touch sk_buff::tstamp! It is used to store
  151. software generated time stamps by the network subsystem.
  152. - As soon as the driver has sent the packet and/or obtained a
  153. hardware time stamp for it, it passes the time stamp back by
  154. calling skb_hwtstamp_tx() with the original skb, the raw
  155. hardware time stamp. skb_hwtstamp_tx() clones the original skb and
  156. adds the timestamps, therefore the original skb has to be freed now.
  157. If obtaining the hardware time stamp somehow fails, then the driver
  158. should not fall back to software time stamping. The rationale is that
  159. this would occur at a later time in the processing pipeline than other
  160. software time stamping and therefore could lead to unexpected deltas
  161. between time stamps.
  162. - If the driver did not set the SKBTX_IN_PROGRESS flag (see above), then
  163. dev_hard_start_xmit() checks whether software time stamping
  164. is wanted as fallback and potentially generates the time stamp.