timestamping.txt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. All three values correspond to the same event in time, but were
  51. generated in different ways. Each of these values may be empty (= all
  52. zero), in which case no such value was available. If the application
  53. is not interested in some of these values, they can be left blank to
  54. avoid the potential overhead of calculating them.
  55. systime is the value of the system time at that moment. This
  56. corresponds to the value also returned via SO_TIMESTAMP[NS]. If the
  57. time stamp was generated by hardware, then this field is
  58. empty. Otherwise it is filled in if SOF_TIMESTAMPING_SOFTWARE is
  59. set.
  60. hwtimeraw is the original hardware time stamp. Filled in if
  61. SOF_TIMESTAMPING_RAW_HARDWARE is set. No assumptions about its
  62. relation to system time should be made.
  63. hwtimetrans is the hardware time stamp transformed so that it
  64. corresponds as good as possible to system time. This correlation is
  65. not perfect; as a consequence, sorting packets received via different
  66. NICs by their hwtimetrans may differ from the order in which they were
  67. received. hwtimetrans may be non-monotonic even for the same NIC.
  68. Filled in if SOF_TIMESTAMPING_SYS_HARDWARE is set. Requires support
  69. by the network device and will be empty without that support.
  70. SIOCSHWTSTAMP:
  71. Hardware time stamping must also be initialized for each device driver
  72. that is expected to do hardware time stamping. The parameter is:
  73. struct hwtstamp_config {
  74. int flags; /* no flags defined right now, must be zero */
  75. int tx_type; /* HWTSTAMP_TX_* */
  76. int rx_filter; /* HWTSTAMP_FILTER_* */
  77. };
  78. Desired behavior is passed into the kernel and to a specific device by
  79. calling ioctl(SIOCSHWTSTAMP) with a pointer to a struct ifreq whose
  80. ifr_data points to a struct hwtstamp_config. The tx_type and
  81. rx_filter are hints to the driver what it is expected to do. If
  82. the requested fine-grained filtering for incoming packets is not
  83. supported, the driver may time stamp more than just the requested types
  84. of packets.
  85. A driver which supports hardware time stamping shall update the struct
  86. with the actual, possibly more permissive configuration. If the
  87. requested packets cannot be time stamped, then nothing should be
  88. changed and ERANGE shall be returned (in contrast to EINVAL, which
  89. indicates that SIOCSHWTSTAMP is not supported at all).
  90. Only a processes with admin rights may change the configuration. User
  91. space is responsible to ensure that multiple processes don't interfere
  92. with each other and that the settings are reset.
  93. /* possible values for hwtstamp_config->tx_type */
  94. enum {
  95. /*
  96. * no outgoing packet will need hardware time stamping;
  97. * should a packet arrive which asks for it, no hardware
  98. * time stamping will be done
  99. */
  100. HWTSTAMP_TX_OFF,
  101. /*
  102. * enables hardware time stamping for outgoing packets;
  103. * the sender of the packet decides which are to be
  104. * time stamped by setting SOF_TIMESTAMPING_TX_SOFTWARE
  105. * before sending the packet
  106. */
  107. HWTSTAMP_TX_ON,
  108. };
  109. /* possible values for hwtstamp_config->rx_filter */
  110. enum {
  111. /* time stamp no incoming packet at all */
  112. HWTSTAMP_FILTER_NONE,
  113. /* time stamp any incoming packet */
  114. HWTSTAMP_FILTER_ALL,
  115. /* return value: time stamp all packets requested plus some others */
  116. HWTSTAMP_FILTER_SOME,
  117. /* PTP v1, UDP, any kind of event packet */
  118. HWTSTAMP_FILTER_PTP_V1_L4_EVENT,
  119. ...
  120. };
  121. DEVICE IMPLEMENTATION
  122. A driver which supports hardware time stamping must support the
  123. SIOCSHWTSTAMP ioctl. Time stamps for received packets must be stored
  124. in the skb with skb_hwtstamp_set().
  125. Time stamps for outgoing packets are to be generated as follows:
  126. - In hard_start_xmit(), check if skb_hwtstamp_check_tx_hardware()
  127. returns non-zero. If yes, then the driver is expected
  128. to do hardware time stamping.
  129. - If this is possible for the skb and requested, then declare
  130. that the driver is doing the time stamping by calling
  131. skb_hwtstamp_tx_in_progress(). A driver not supporting
  132. hardware time stamping doesn't do that. A driver must never
  133. touch sk_buff::tstamp! It is used to store how time stamping
  134. for an outgoing packets is to be done.
  135. - As soon as the driver has sent the packet and/or obtained a
  136. hardware time stamp for it, it passes the time stamp back by
  137. calling skb_hwtstamp_tx() with the original skb, the raw
  138. hardware time stamp and a handle to the device (necessary
  139. to convert the hardware time stamp to system time). If obtaining
  140. the hardware time stamp somehow fails, then the driver should
  141. not fall back to software time stamping. The rationale is that
  142. this would occur at a later time in the processing pipeline
  143. than other software time stamping and therefore could lead
  144. to unexpected deltas between time stamps.
  145. - If the driver did not call skb_hwtstamp_tx_in_progress(), then
  146. dev_hard_start_xmit() checks whether software time stamping
  147. is wanted as fallback and potentially generates the time stamp.