From ce4e0a3c2ff795df8d3cdd61b4d14a75a0f5d8e7 Mon Sep 17 00:00:00 2001 From: Bill Unruh Date: Tue, 26 Jun 2007 23:42:11 +0100 Subject: [PATCH 1/5] Fix problems with rtc_linux. 2) Changes to rtc_linux.c which a) do a double read of /dev/rtc when the PPM interupt is turned on after the wait time expires. The current read does not block to the second, as it should, thus two reads are needed. Also, changes so that at startup the system properly ignores the last system time from the initial burst mode for setting the system time since it can be way off. At present this last system time is included in the regression, which throws it off until finally that sample is dropped. --- rtc_linux.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/rtc_linux.c b/rtc_linux.c index 8765a12..1d19bbb 100644 --- a/rtc_linux.c +++ b/rtc_linux.c @@ -174,7 +174,7 @@ static double file_ref_offset, file_rate_ppm; /* ================================================== */ /* Flag to remember whether to assume the RTC is running on UTC */ -static int rtc_on_utc = 0; +static int rtc_on_utc = 1; /* ================================================== */ @@ -226,15 +226,18 @@ accumulate_sample(time_t rtc, struct timeval *sys) discard_samples(NEW_FIRST_WHEN_FULL); } - rtc_sec[n_samples] = rtc; /* Always use most recent sample as reference */ + /* use sample only if n_sample is not negative*/ + if(n_samples >=0) + { rtc_ref = rtc; - + rtc_sec[n_samples] = rtc; rtc_trim[n_samples] = 0.0; system_times[n_samples] = *sys; - ++n_samples; ++n_samples_since_regression; + } + ++n_samples; return; } @@ -742,8 +745,12 @@ handle_initial_trim(void) run_regression(1, &coefs_valid, &coef_ref_time, &coef_seconds_fast, &coef_gain_rate); n_samples_since_regression = 0; - n_samples = 0; + /* Set sample number to -1 so the next sample is not used, as it will not yet be corrected for System Trim*/ + + n_samples = -1; + + read_coefs_from_file(); if (valid_coefs_from_file) { @@ -866,6 +873,8 @@ read_from_device(void *any) int error = 0; status = read(fd, &data, sizeof(data)); + if (operating_mode == OM_NORMAL) + status = read(fd, &data, sizeof(data)); if (status < 0) { /* This looks like a bad error : the file descriptor was indicating it was * ready to read but we couldn't read anything. Give up. */ From ca1195a0e68325702d32319ef03b8ef1304fc016 Mon Sep 17 00:00:00 2001 From: "Richard P. Curnow" Date: Tue, 26 Jun 2007 23:43:28 +0100 Subject: [PATCH 2/5] Fix whitespace issue with last patch --- rtc_linux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtc_linux.c b/rtc_linux.c index 1d19bbb..dc5a15d 100644 --- a/rtc_linux.c +++ b/rtc_linux.c @@ -750,7 +750,7 @@ handle_initial_trim(void) n_samples = -1; - + read_coefs_from_file(); if (valid_coefs_from_file) { From 8022874a47fce4345e7bb273f3e8967d977d8766 Mon Sep 17 00:00:00 2001 From: Bill Unruh Date: Tue, 26 Jun 2007 23:45:04 +0100 Subject: [PATCH 3/5] Handle fluctuations in peer distance better. --- sourcestats.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sourcestats.c b/sourcestats.c index fecba31..163a2eb 100644 --- a/sourcestats.c +++ b/sourcestats.c @@ -373,9 +373,9 @@ find_best_sample_index(SST_Stats inst, double *times_back) /* This defines the assumed ratio between the standard deviation of the samples and the peer distance as measured from the round trip time. E.g. a value of 4 means that we think the standard deviation - is a quarter of the peer distance */ + is four times the fluctuation of the peer distance */ -#define SD_TO_DIST_RATIO 8.0 +#define SD_TO_DIST_RATIO 1.0 /* ================================================== */ /* This function runs the linear regression operation on the data. It From 75a7af9edc8235862c659fe433a993c4247ce38e Mon Sep 17 00:00:00 2001 From: Bill Unruh Date: Tue, 26 Jun 2007 23:46:33 +0100 Subject: [PATCH 4/5] Fix handling of stratum zero. Further to the discussion with John Hasler, here are new diffs which handles the incoming stratum 0 claim of a remote server by redefining the incoming stratum as one bigger than the Max if it is zero, as per the NTP version 4 documentation. If the incoming stratum is zero it sets it to NTP_MAX_STRATUM+1 . If our current stratum is larger than the NTP_MAX_STRATUM, the outgoing stratum is also set to zero as per the suggestions in the NTP docs. Introduces the new NTP_INVALID_STRATUM of 0 for doing these tests or setting the outgoing stratum. It is unclear whether chrony wants to follow NTP in setting the outgoing stratum to zero if it is unknown or invalid, rather than a number larger than the max stratum. Setting it to zero seems silly, since zero is already used to define the stratum of a hardware clock (GPS, atomic, etc). This seems ripe for confusion. But the fact that the ntp docs state to do this, and that ntp servers (eg ntp.ubc.ca) are already doing this (using 0 to mean invalid) means that chrony has to handle it on the incoming packets from the servers. --- ntp_core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ntp_core.c b/ntp_core.c index 6c18787..9523138 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -196,6 +196,9 @@ struct NCR_Instance_Record { /* Maximum allowed stratum */ #define NTP_MAX_STRATUM 15 +/* INVALID or Unkown stratum from external server as per the NTP 4 docs */ +#define NTP_INVALID_STRATUM 0 + /* ================================================== */ static ADF_AuthTable access_auth_table; @@ -539,7 +542,9 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ /* Generate transmit packet */ message.lvm = ((leap << 6) &0xc0) | ((version << 3) & 0x38) | (my_mode & 0x07); - message.stratum = our_stratum; + if(our_stratum<=NTP_MAX_STRATUM)message.stratum = our_stratum; + else message.stratum=NTP_INVALID_STRATUM; //(WGU) to handle NTP "Invalid" stratum as per the NTP V4 documents. + message.poll = my_poll; message.precision = LCL_GetSysPrecisionAsLog(); @@ -983,6 +988,9 @@ receive_packet(NTP_Packet *message, struct timeval *now, NCR_Instance inst, int test6 = 1; /* Succeeded */ } + /* (WGU) Set stratum to greater than any valid if incoming is 0 */ + /* as per the NPT v4 documentation*/ + if (message->stratum<=NTP_INVALID_STRATUM)message->stratum=NTP_MAX_STRATUM+1; /* Test 7 checks that the stratum in the packet is appropriate */ if ((message->stratum > REF_GetOurStratum()) || (message->stratum > NTP_MAX_STRATUM)) { From 4ba843f8f49253f692d7f31b079bd7c59ecd2e9b Mon Sep 17 00:00:00 2001 From: "Richard P. Curnow" Date: Tue, 26 Jun 2007 23:50:53 +0100 Subject: [PATCH 5/5] Fix formatting from last patch --- ntp_core.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ntp_core.c b/ntp_core.c index 9523138..b095b93 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -542,8 +542,12 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ /* Generate transmit packet */ message.lvm = ((leap << 6) &0xc0) | ((version << 3) & 0x38) | (my_mode & 0x07); - if(our_stratum<=NTP_MAX_STRATUM)message.stratum = our_stratum; - else message.stratum=NTP_INVALID_STRATUM; //(WGU) to handle NTP "Invalid" stratum as per the NTP V4 documents. + if (our_stratum <= NTP_MAX_STRATUM) { + message.stratum = our_stratum; + } else { + /* (WGU) to handle NTP "Invalid" stratum as per the NTP V4 documents. */ + message.stratum = NTP_INVALID_STRATUM; + } message.poll = my_poll; message.precision = LCL_GetSysPrecisionAsLog(); @@ -990,7 +994,10 @@ receive_packet(NTP_Packet *message, struct timeval *now, NCR_Instance inst, int /* (WGU) Set stratum to greater than any valid if incoming is 0 */ /* as per the NPT v4 documentation*/ - if (message->stratum<=NTP_INVALID_STRATUM)message->stratum=NTP_MAX_STRATUM+1; + if (message->stratum <= NTP_INVALID_STRATUM) { + message->stratum = NTP_MAX_STRATUM + 1; + } + /* Test 7 checks that the stratum in the packet is appropriate */ if ((message->stratum > REF_GetOurStratum()) || (message->stratum > NTP_MAX_STRATUM)) {