1 // Contains the NTP time drift detection via the SNTP protocol:
2 // https://tools.ietf.org/html/rfc4330
13 log "github.com/sirupsen/logrus"
17 ntpPool = "pool.ntp.org" // ntpPool is the NTP server to query for the current time
18 ntpChecks = 3 // Number of measurements to do against the NTP server
21 // durationSlice attaches the methods of sort.Interface to []time.Duration,
22 // sorting in increasing order.
23 type durationSlice []time.Duration
25 func (s durationSlice) Len() int { return len(s) }
26 func (s durationSlice) Less(i, j int) bool { return s[i] < s[j] }
27 func (s durationSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
29 // checkClockDrift queries an NTP server for clock drifts and warns the user if
30 // one large enough is detected.
31 func checkClockDrift() {
32 drift, err := sntpDrift(ntpChecks)
36 if drift < -driftThreshold || drift > driftThreshold {
37 warning := fmt.Sprintf("System clock seems off by %v, which can prevent network connectivity", drift)
38 howtofix := fmt.Sprintf("Please enable network time synchronisation in system settings")
39 separator := strings.Repeat("-", len(warning))
41 log.WithFields(log.Fields{"module": logModule}).Warn(separator)
42 log.WithFields(log.Fields{"module": logModule}).Warn(warning)
43 log.WithFields(log.Fields{"module": logModule}).Warn(howtofix)
44 log.WithFields(log.Fields{"module": logModule}).Warn(separator)
46 log.WithFields(log.Fields{"module": logModule, "drift": drift}).Debug(fmt.Sprintf("Sanity NTP check reported all ok"))
50 // sntpDrift does a naive time resolution against an NTP server and returns the
51 // measured drift. This method uses the simple version of NTP. It's not precise
52 // but should be fine for these purposes.
54 // Note, it executes two extra measurements compared to the number of requested
55 // ones to be able to discard the two extremes as outliers.
56 func sntpDrift(measurements int) (time.Duration, error) {
57 // Resolve the address of the NTP server
58 addr, err := net.ResolveUDPAddr("udp", ntpPool+":123")
62 // Construct the time request (empty package with only 2 fields set):
63 // Bits 3-5: Protocol version, 3
64 // Bits 6-8: Mode of operation, client, 3
65 request := make([]byte, 48)
68 // Execute each of the measurements
69 drifts := []time.Duration{}
70 for i := 0; i < measurements+2; i++ {
71 // Dial the NTP server and send the time retrieval request
72 conn, err := net.DialUDP("udp", nil, addr)
79 if _, err = conn.Write(request); err != nil {
82 // Retrieve the reply and calculate the elapsed time
83 conn.SetDeadline(time.Now().Add(5 * time.Second))
85 reply := make([]byte, 48)
86 if _, err = conn.Read(reply); err != nil {
89 elapsed := time.Since(sent)
91 // Reconstruct the time from the reply data
92 sec := uint64(reply[43]) | uint64(reply[42])<<8 | uint64(reply[41])<<16 | uint64(reply[40])<<24
93 frac := uint64(reply[47]) | uint64(reply[46])<<8 | uint64(reply[45])<<16 | uint64(reply[44])<<24
95 nanosec := sec*1e9 + (frac*1e9)>>32
97 t := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(nanosec)).Local()
99 // Calculate the drift based on an assumed answer time of RRT/2
100 drifts = append(drifts, sent.Sub(t)+elapsed/2)
102 // Calculate average drif (drop two extremities to avoid outliers)
103 sort.Sort(durationSlice(drifts))
105 drift := time.Duration(0)
106 for i := 1; i < len(drifts)-1; i++ {
109 return drift / time.Duration(measurements), nil