ntp: get TX timestamp after authentication

If the daemon transmit timestamp is saved for processing of a future
response or responding in the interleaved mode, get a more accurate
timestamp right before calling NIO_SendPacket(). Avoid unnecessary
reading of the clock for the transmit timestamp in the packet (i.e.
in interleaved modes and client basic mode).

This should improve accuracy and stability when authentication is
enabled in the client and symmetric basic modes and also interleaved
modes if kernel/hardware timestamps are not available.
This commit is contained in:
Miroslav Lichvar 2020-07-02 15:18:23 +02:00
parent 4e747da4b4
commit ff03b813b0
2 changed files with 29 additions and 21 deletions

View file

@ -200,10 +200,11 @@ authenticated source to be safely combined with unauthenticated sources in
order to improve the accuracy of the clock. They can be selected and used for order to improve the accuracy of the clock. They can be selected and used for
synchronisation only if they agree with the trusted and required source. synchronisation only if they agree with the trusted and required source.
*xleave*::: *xleave*:::
This option enables an interleaved mode which enables the server to This option enables the interleaved mode of NTP. It enables the server to
send transmit timestamps captured after the actual transmission (e.g. when the respond with more accurate transmit timestamps (e.g. kernel or hardware
server is running *chronyd* with software (kernel) or hardware timestamps), which cannot be contained in the transmitted packet itself and
timestamping). This can significantly improve the accuracy of the measurements. need to refer to a previous packet instead. This can significantly improve the
accuracy and stability of the measurements.
+ +
The interleaved mode is compatible with servers that support only the basic The interleaved mode is compatible with servers that support only the basic
mode. Note that even mode. Note that even

View file

@ -961,6 +961,10 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */
smooth_time = 0; smooth_time = 0;
smooth_offset = 0.0; smooth_offset = 0.0;
/* Get an initial transmit timestamp. A more accurate timestamp will be
taken later in this function. */
SCH_GetLastEventTime(&local_transmit, NULL, NULL);
if (my_mode == MODE_CLIENT) { if (my_mode == MODE_CLIENT) {
/* Don't reveal local time or state of the clock in client packets */ /* Don't reveal local time or state of the clock in client packets */
precision = 32; precision = 32;
@ -968,10 +972,6 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */
our_root_delay = our_root_dispersion = 0.0; our_root_delay = our_root_dispersion = 0.0;
UTI_ZeroTimespec(&our_ref_time); UTI_ZeroTimespec(&our_ref_time);
} else { } else {
/* This is accurate enough and cheaper than calling LCL_ReadCookedTime.
A more accurate timestamp will be taken later in this function. */
SCH_GetLastEventTime(&local_transmit, NULL, NULL);
REF_GetReferenceParams(&local_transmit, REF_GetReferenceParams(&local_transmit,
&are_we_synchronised, &leap_status, &are_we_synchronised, &leap_status,
&our_stratum, &our_stratum,
@ -1062,20 +1062,20 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */
/* Prepare random bits which will be added to the transmit timestamp */ /* Prepare random bits which will be added to the transmit timestamp */
UTI_GetNtp64Fuzz(&ts_fuzz, precision); UTI_GetNtp64Fuzz(&ts_fuzz, precision);
/* Transmit - this our local time right now! Also, we might need to /* Get a more accurate transmit timestamp if it needs to be saved in the
store this for our own use later, next time we receive a message packet (i.e. in the server, symmetric, and broadcast basic modes) */
from the source we're sending to now. */ if (!interleaved && precision < 32) {
LCL_ReadCookedTime(&local_transmit, &local_transmit_err); LCL_ReadCookedTime(&local_transmit, &local_transmit_err);
if (smooth_time)
UTI_AddDoubleToTimespec(&local_transmit, smooth_offset, &local_transmit);
if (smooth_time) /* Pre-compensate the transmit time by approximately how long it will
UTI_AddDoubleToTimespec(&local_transmit, smooth_offset, &local_transmit); take to generate the authentication data */
if (auth)
/* Pre-compensate the transmit time by approximately how long it will take NAU_AdjustRequestTimestamp(auth, &local_transmit);
to generate the authentication data */ else
if (auth) NAU_AdjustResponseTimestamp(request, request_info, &local_transmit);
NAU_AdjustRequestTimestamp(auth, &local_transmit); }
else
NAU_AdjustResponseTimestamp(request, request_info, &local_transmit);
UTI_TimespecToNtp64(interleaved ? &local_tx->ts : &local_transmit, UTI_TimespecToNtp64(interleaved ? &local_tx->ts : &local_transmit,
&message.transmit_ts, &ts_fuzz); &message.transmit_ts, &ts_fuzz);
@ -1111,9 +1111,16 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */
return 0; return 0;
} }
/* If the transmit timestamp will be saved, get an even more
accurate daemon timestamp closer to the transmission */
if (local_tx)
LCL_ReadCookedTime(&local_transmit, &local_transmit_err);
ret = NIO_SendPacket(&message, where_to, from, info.length, local_tx != NULL); ret = NIO_SendPacket(&message, where_to, from, info.length, local_tx != NULL);
if (local_tx) { if (local_tx) {
if (smooth_time)
UTI_AddDoubleToTimespec(&local_transmit, smooth_offset, &local_transmit);
local_tx->ts = local_transmit; local_tx->ts = local_transmit;
local_tx->err = local_transmit_err; local_tx->err = local_transmit_err;
local_tx->source = NTP_TS_DAEMON; local_tx->source = NTP_TS_DAEMON;