timestamping.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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:
  75. struct hwtstamp_config {
  76. int flags; /* no flags defined right now, must be zero */
  77. int tx_type; /* HWTSTAMP_TX_* */
  78. int rx_filter; /* HWTSTAMP_FILTER_* */
  79. };
  80. Desired behavior is passed into the kernel and to a specific device by
  81. calling ioctl(SIOCSHWTSTAMP) with a pointer to a struct ifreq whose
  82. ifr_data points to a struct hwtstamp_config. The tx_type and
  83. rx_filter are hints to the driver what it is expected to do. If
  84. the requested fine-grained filtering for incoming packets is not
  85. supported, the driver may time stamp more than just the requested types
  86. of packets.
  87. A driver which supports hardware time stamping shall update the struct
  88. with the actual, possibly more permissive configuration. If the
  89. requested packets cannot be time stamped, then nothing should be
  90. changed and ERANGE shall be returned (in contrast to EINVAL, which
  91. indicates that SIOCSHWTSTAMP is not supported at all).
  92. Only a processes with admin rights may change the configuration. User
  93. space is responsible to ensure that multiple processes don't interfere
  94. with each other and that the settings are reset.
  95. /* possible values for hwtstamp_config->tx_type */
  96. enum {
  97. /*
  98. * no outgoing packet will need hardware time stamping;
  99. * should a packet arrive which asks for it, no hardware
  100. * time stamping will be done
  101. */
  102. HWTSTAMP_TX_OFF,
  103. /*
  104. * enables hardware time stamping for outgoing packets;
  105. * the sender of the packet decides which are to be
  106. * time stamped by setting SOF_TIMESTAMPING_TX_SOFTWARE
  107. * before sending the packet
  108. */
  109. HWTSTAMP_TX_ON,
  110. };
  111. /* possible values for hwtstamp_config->rx_filter */
  112. enum {
  113. /* time stamp no incoming packet at all */
  114. HWTSTAMP_FILTER_NONE,
  115. /* time stamp any incoming packet */
  116. HWTSTAMP_FILTER_ALL,
  117. /* return value: time stamp all packets requested plus some others */
  118. HWTSTAMP_FILTER_SOME,
  119. /* PTP v1, UDP, any kind of event packet */
  120. HWTSTAMP_FILTER_PTP_V1_L4_EVENT,
  121. ...
  122. };
  123. DEVICE IMPLEMENTATION
  124. A driver which supports hardware time stamping must support the
  125. SIOCSHWTSTAMP ioctl. Time stamps for received packets must be stored
  126. in the skb with skb_hwtstamp_set().
  127. Time stamps for outgoing packets are to be generated as follows:
  128. - In hard_start_xmit(), check if skb_hwtstamp_check_tx_hardware()
  129. returns non-zero. If yes, then the driver is expected
  130. to do hardware time stamping.
  131. - If this is possible for the skb and requested, then declare
  132. that the driver is doing the time stamping by calling
  133. skb_hwtstamp_tx_in_progress(). A driver not supporting
  134. hardware time stamping doesn't do that. A driver must never
  135. touch sk_buff::tstamp! It is used to store how time stamping
  136. for an outgoing packets is to be done.
  137. - As soon as the driver has sent the packet and/or obtained a
  138. hardware time stamp for it, it passes the time stamp back by
  139. calling skb_hwtstamp_tx() with the original skb, the raw
  140. hardware time stamp and a handle to the device (necessary
  141. to convert the hardware time stamp to system time). If obtaining
  142. the hardware time stamp somehow fails, then the driver should
  143. not fall back to software time stamping. The rationale is that
  144. this would occur at a later time in the processing pipeline
  145. than other software time stamping and therefore could lead
  146. to unexpected deltas between time stamps.
  147. - If the driver did not call skb_hwtstamp_tx_in_progress(), then
  148. dev_hard_start_xmit() checks whether software time stamping
  149. is wanted as fallback and potentially generates the time stamp.