Michal Schmidt 5a725f
From 8269ab5ca37ca93f52ec1766d673e85887aa0ebe Mon Sep 17 00:00:00 2001
Michal Schmidt 5a725f
From: Michal Schmidt <mschmidt@redhat.com>
Michal Schmidt 5a725f
Date: Wed, 10 Sep 2014 19:55:59 +0200
Michal Schmidt 5a725f
Subject: [PATCH] timesyncd: allow two missed replies before reselecting server
Michal Schmidt 5a725f
Michal Schmidt 5a725f
Upstream commit:
Michal Schmidt 5a725f
commit e8206972be6a7ebeb198cd0d400bc7a94a6a5fc5
Michal Schmidt 5a725f
Author: Miroslav Lichvar <mlichvar@redhat.com>
Michal Schmidt 5a725f
Date:   Tue Sep 2 14:29:51 2014 +0200
Michal Schmidt 5a725f
Michal Schmidt 5a725f
    timesyncd: allow two missed replies before reselecting server
Michal Schmidt 5a725f
Michal Schmidt 5a725f
    After receiving a reply from the server, allow two missed replies before
Michal Schmidt 5a725f
    switching to another server to avoid unnecessary clock hopping when
Michal Schmidt 5a725f
    packets are getting lost in the network.
Michal Schmidt 5a725f
Michal Schmidt 5a725f
Conflicts:
Michal Schmidt 5a725f
	src/timesync/timesyncd-manager.c
Michal Schmidt 5a725f
---
Michal Schmidt 5a725f
 src/timesync/timesyncd.c | 27 ++++++++++++++++++---------
Michal Schmidt 5a725f
 src/timesync/timesyncd.h |  1 +
Michal Schmidt 5a725f
 2 files changed, 19 insertions(+), 9 deletions(-)
Michal Schmidt 5a725f
Michal Schmidt 5a725f
diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c
Michal Schmidt 5a725f
index 589c5ec521..7470f4db97 100644
Michal Schmidt 5a725f
--- a/src/timesync/timesyncd.c
Michal Schmidt 5a725f
+++ b/src/timesync/timesyncd.c
Michal Schmidt 5a725f
@@ -94,6 +94,9 @@
Michal Schmidt 5a725f
 /* Maximum acceptable root distance in seconds. */
Michal Schmidt 5a725f
 #define NTP_MAX_ROOT_DISTANCE           5.0
Michal Schmidt 5a725f
 
Michal Schmidt 5a725f
+/* Maximum number of missed replies before selecting another source. */
Michal Schmidt 5a725f
+#define NTP_MAX_MISSED_REPLIES          2
Michal Schmidt 5a725f
+
Michal Schmidt 5a725f
 /*
Michal Schmidt 5a725f
  * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
Michal Schmidt 5a725f
  * in seconds relative to 0h on 1 January 1900."
Michal Schmidt 5a725f
@@ -277,15 +280,18 @@ static int manager_send_request(Manager *m) {
Michal Schmidt 5a725f
                 return r;
Michal Schmidt 5a725f
         }
Michal Schmidt 5a725f
 
Michal Schmidt 5a725f
-        r = sd_event_add_time(
Michal Schmidt 5a725f
-                        m->event,
Michal Schmidt 5a725f
-                        &m->event_timeout,
Michal Schmidt 5a725f
-                        clock_boottime_or_monotonic(),
Michal Schmidt 5a725f
-                        now(clock_boottime_or_monotonic()) + TIMEOUT_USEC, 0,
Michal Schmidt 5a725f
-                        manager_timeout, m);
Michal Schmidt 5a725f
-        if (r < 0) {
Michal Schmidt 5a725f
-                log_error("Failed to arm timeout timer: %s", strerror(-r));
Michal Schmidt 5a725f
-                return r;
Michal Schmidt 5a725f
+        m->missed_replies++;
Michal Schmidt 5a725f
+        if (m->missed_replies > NTP_MAX_MISSED_REPLIES) {
Michal Schmidt 5a725f
+                r = sd_event_add_time(
Michal Schmidt 5a725f
+                                m->event,
Michal Schmidt 5a725f
+                                &m->event_timeout,
Michal Schmidt 5a725f
+                                clock_boottime_or_monotonic(),
Michal Schmidt 5a725f
+                                now(clock_boottime_or_monotonic()) + TIMEOUT_USEC, 0,
Michal Schmidt 5a725f
+                                manager_timeout, m);
Michal Schmidt 5a725f
+                if (r < 0) {
Michal Schmidt 5a725f
+                        log_error("Failed to arm timeout timer: %s", strerror(-r));
Michal Schmidt 5a725f
+                        return r;
Michal Schmidt 5a725f
+                }
Michal Schmidt 5a725f
         }
Michal Schmidt 5a725f
 
Michal Schmidt 5a725f
         return 0;
Michal Schmidt 5a725f
@@ -627,6 +633,8 @@ static int manager_receive_response(sd_event_source *source, int fd, uint32_t re
Michal Schmidt 5a725f
                 return 0;
Michal Schmidt 5a725f
         }
Michal Schmidt 5a725f
 
Michal Schmidt 5a725f
+        m->missed_replies = 0;
Michal Schmidt 5a725f
+
Michal Schmidt 5a725f
         /* check our "time cookie" (we just stored nanoseconds in the fraction field) */
Michal Schmidt 5a725f
         if (be32toh(ntpmsg.origin_time.sec) != m->trans_time.tv_sec + OFFSET_1900_1970 ||
Michal Schmidt 5a725f
             be32toh(ntpmsg.origin_time.frac) != m->trans_time.tv_nsec) {
Michal Schmidt 5a725f
@@ -791,6 +799,7 @@ static int manager_begin(Manager *m) {
Michal Schmidt 5a725f
         assert_return(m->current_server_name, -EHOSTUNREACH);
Michal Schmidt 5a725f
         assert_return(m->current_server_address, -EHOSTUNREACH);
Michal Schmidt 5a725f
 
Michal Schmidt 5a725f
+        m->missed_replies = NTP_MAX_MISSED_REPLIES;
Michal Schmidt 5a725f
         m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
Michal Schmidt 5a725f
 
Michal Schmidt 5a725f
         sockaddr_pretty(&m->current_server_address->sockaddr.sa, m->current_server_address->socklen, true, &pretty);
Michal Schmidt 5a725f
diff --git a/src/timesync/timesyncd.h b/src/timesync/timesyncd.h
Michal Schmidt 5a725f
index 04d83f0cc9..bcd14f71f6 100644
Michal Schmidt 5a725f
--- a/src/timesync/timesyncd.h
Michal Schmidt 5a725f
+++ b/src/timesync/timesyncd.h
Michal Schmidt 5a725f
@@ -61,6 +61,7 @@ struct Manager {
Michal Schmidt 5a725f
         ServerName *current_server_name;
Michal Schmidt 5a725f
         ServerAddress *current_server_address;
Michal Schmidt 5a725f
         int server_socket;
Michal Schmidt 5a725f
+        int missed_replies;
Michal Schmidt 5a725f
         uint64_t packet_count;
Michal Schmidt 5a725f
         sd_event_source *event_timeout;
Michal Schmidt 5a725f