Michal Schmidt d962ba
From e558311df376973727c9924c1416a2101e55673d Mon Sep 17 00:00:00 2001
Michal Schmidt d962ba
From: Miroslav Lichvar <mlichvar@redhat.com>
Michal Schmidt d962ba
Date: Tue, 2 Sep 2014 14:29:51 +0200
Michal Schmidt d962ba
Subject: [PATCH 09/12] timesyncd: allow two missed replies before reselecting
Michal Schmidt d962ba
 server
Michal Schmidt d962ba
Michal Schmidt d962ba
After receiving a reply from the server, allow two missed replies before
Michal Schmidt d962ba
switching to another server to avoid unnecessary clock hopping when
Michal Schmidt d962ba
packets are getting lost in the network.
Michal Schmidt d962ba
Michal Schmidt d962ba
(cherry picked from commit e8206972be6a7ebeb198cd0d400bc7a94a6a5fc5)
Michal Schmidt d962ba
---
Michal Schmidt d962ba
 src/timesync/timesyncd-manager.c | 27 ++++++++++++++++++---------
Michal Schmidt d962ba
 src/timesync/timesyncd-manager.h |  1 +
Michal Schmidt d962ba
 2 files changed, 19 insertions(+), 9 deletions(-)
Michal Schmidt d962ba
Michal Schmidt d962ba
diff --git a/src/timesync/timesyncd-manager.c b/src/timesync/timesyncd-manager.c
Michal Schmidt d962ba
index 19a28f3..a66852d 100644
Michal Schmidt d962ba
--- a/src/timesync/timesyncd-manager.c
Michal Schmidt d962ba
+++ b/src/timesync/timesyncd-manager.c
Michal Schmidt d962ba
@@ -92,6 +92,9 @@
Michal Schmidt d962ba
 /* Maximum acceptable root distance in seconds. */
Michal Schmidt d962ba
 #define NTP_MAX_ROOT_DISTANCE           5.0
Michal Schmidt d962ba
 
Michal Schmidt d962ba
+/* Maximum number of missed replies before selecting another source. */
Michal Schmidt d962ba
+#define NTP_MAX_MISSED_REPLIES          2
Michal Schmidt d962ba
+
Michal Schmidt d962ba
 /*
Michal Schmidt d962ba
  * "NTP timestamps are represented as a 64-bit unsigned fixed-point number,
Michal Schmidt d962ba
  * in seconds relative to 0h on 1 January 1900."
Michal Schmidt d962ba
@@ -206,15 +209,18 @@ static int manager_send_request(Manager *m) {
Michal Schmidt d962ba
                 return manager_connect(m);
Michal Schmidt d962ba
         }
Michal Schmidt d962ba
 
Michal Schmidt d962ba
-        r = sd_event_add_time(
Michal Schmidt d962ba
-                        m->event,
Michal Schmidt d962ba
-                        &m->event_timeout,
Michal Schmidt d962ba
-                        clock_boottime_or_monotonic(),
Michal Schmidt d962ba
-                        now(clock_boottime_or_monotonic()) + TIMEOUT_USEC, 0,
Michal Schmidt d962ba
-                        manager_timeout, m);
Michal Schmidt d962ba
-        if (r < 0) {
Michal Schmidt d962ba
-                log_error("Failed to arm timeout timer: %s", strerror(-r));
Michal Schmidt d962ba
-                return r;
Michal Schmidt d962ba
+        m->missed_replies++;
Michal Schmidt d962ba
+        if (m->missed_replies > NTP_MAX_MISSED_REPLIES) {
Michal Schmidt d962ba
+                r = sd_event_add_time(
Michal Schmidt d962ba
+                                m->event,
Michal Schmidt d962ba
+                                &m->event_timeout,
Michal Schmidt d962ba
+                                clock_boottime_or_monotonic(),
Michal Schmidt d962ba
+                                now(clock_boottime_or_monotonic()) + TIMEOUT_USEC, 0,
Michal Schmidt d962ba
+                                manager_timeout, m);
Michal Schmidt d962ba
+                if (r < 0) {
Michal Schmidt d962ba
+                        log_error("Failed to arm timeout timer: %s", strerror(-r));
Michal Schmidt d962ba
+                        return r;
Michal Schmidt d962ba
+                }
Michal Schmidt d962ba
         }
Michal Schmidt d962ba
 
Michal Schmidt d962ba
         return 0;
Michal Schmidt d962ba
@@ -549,6 +555,8 @@ static int manager_receive_response(sd_event_source *source, int fd, uint32_t re
Michal Schmidt d962ba
                 return 0;
Michal Schmidt d962ba
         }
Michal Schmidt d962ba
 
Michal Schmidt d962ba
+        m->missed_replies = 0;
Michal Schmidt d962ba
+
Michal Schmidt d962ba
         /* check our "time cookie" (we just stored nanoseconds in the fraction field) */
Michal Schmidt d962ba
         if (be32toh(ntpmsg.origin_time.sec) != m->trans_time.tv_sec + OFFSET_1900_1970 ||
Michal Schmidt d962ba
             be32toh(ntpmsg.origin_time.frac) != m->trans_time.tv_nsec) {
Michal Schmidt d962ba
@@ -712,6 +720,7 @@ static int manager_begin(Manager *m) {
Michal Schmidt d962ba
         assert_return(m->current_server_name, -EHOSTUNREACH);
Michal Schmidt d962ba
         assert_return(m->current_server_address, -EHOSTUNREACH);
Michal Schmidt d962ba
 
Michal Schmidt d962ba
+        m->missed_replies = NTP_MAX_MISSED_REPLIES;
Michal Schmidt d962ba
         m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
Michal Schmidt d962ba
 
Michal Schmidt d962ba
         server_address_pretty(m->current_server_address, &pretty);
Michal Schmidt d962ba
diff --git a/src/timesync/timesyncd-manager.h b/src/timesync/timesyncd-manager.h
Michal Schmidt d962ba
index 0ac0e17..8296d41 100644
Michal Schmidt d962ba
--- a/src/timesync/timesyncd-manager.h
Michal Schmidt d962ba
+++ b/src/timesync/timesyncd-manager.h
Michal Schmidt d962ba
@@ -53,6 +53,7 @@ struct Manager {
Michal Schmidt d962ba
         ServerName *current_server_name;
Michal Schmidt d962ba
         ServerAddress *current_server_address;
Michal Schmidt d962ba
         int server_socket;
Michal Schmidt d962ba
+        int missed_replies;
Michal Schmidt d962ba
         uint64_t packet_count;
Michal Schmidt d962ba
         sd_event_source *event_timeout;
Michal Schmidt d962ba
 
Michal Schmidt d962ba
-- 
Michal Schmidt d962ba
2.1.0
Michal Schmidt d962ba