From 3dc9f1ff9271bdc7d82d67362de5dfd42dd79212 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 8 Aug 2023 15:44:25 +0200 Subject: [PATCH] ntp: don't require previous HW TX timestamp to wait for another Clients sockets are closed immediately after receiving valid response. Don't wait for the first early HW TX timestamp to enable waiting for late timestamps. It may take a long time or never come if the HW/driver is consistently slow. It's a chicken and egg problem. Instead, simply check if HW timestamping is enabled on at least one interface. Responses from NTP sources on other interfaces will always be saved (for 1 millisecond by default). --- ntp_core.c | 9 +-------- ntp_io.c | 12 ++++++++++++ ntp_io.h | 3 +++ ntp_io_linux.c | 8 ++++++++ ntp_io_linux.h | 2 ++ test/unit/ntp_core.c | 7 ++----- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/ntp_core.c b/ntp_core.c index 73519a31..22cab439 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -215,9 +215,6 @@ struct NCR_Instance_Record { SPF_Instance filter; int filter_count; - /* Flag indicating HW transmit timestamps are expected */ - int had_hw_tx_timestamp; - /* Response waiting for a HW transmit timestamp of the request */ struct SavedResponse *saved_response; @@ -794,8 +791,6 @@ NCR_ResetInstance(NCR_Instance instance) if (instance->filter) SPF_DropSamples(instance->filter); instance->filter_count = 0; - - instance->had_hw_tx_timestamp = 0; } /* ================================================== */ @@ -1966,7 +1961,7 @@ process_response(NCR_Instance inst, int saved, NTP_Local_Address *local_addr, response to the request, when at least one good response has already been accepted to avoid incorrectly confirming a tentative source. */ if (valid_packet && synced_packet && !saved && !inst->valid_rx && - inst->had_hw_tx_timestamp && inst->local_tx.source != NTP_TS_HARDWARE && + NIO_IsHwTsEnabled() && inst->local_tx.source != NTP_TS_HARDWARE && inst->report.total_good_count > 0) { if (save_response(inst, local_addr, rx_ts, message, info)) return 1; @@ -2692,8 +2687,6 @@ NCR_ProcessTxKnown(NCR_Instance inst, NTP_Local_Address *local_addr, message); if (tx_ts->source == NTP_TS_HARDWARE) { - inst->had_hw_tx_timestamp = 1; - if (has_saved_response(inst)) process_saved_response(inst); } diff --git a/ntp_io.c b/ntp_io.c index deb8e25a..fce7b177 100644 --- a/ntp_io.c +++ b/ntp_io.c @@ -278,6 +278,18 @@ NIO_Finalise(void) /* ================================================== */ +int +NIO_IsHwTsEnabled(void) +{ +#ifdef HAVE_LINUX_TIMESTAMPING + return NIO_Linux_IsHwTsEnabled(); +#else + return 0; +#endif +} + +/* ================================================== */ + int NIO_OpenClientSocket(NTP_Remote_Address *remote_addr) { diff --git a/ntp_io.h b/ntp_io.h index 19ffeed6..427bd966 100644 --- a/ntp_io.h +++ b/ntp_io.h @@ -39,6 +39,9 @@ extern void NIO_Initialise(void); /* Function to finalise the module */ extern void NIO_Finalise(void); +/* Function to check if HW timestamping is enabled on any interface */ +extern int NIO_IsHwTsEnabled(void); + /* Function to obtain a socket for sending client packets */ extern int NIO_OpenClientSocket(NTP_Remote_Address *remote_addr); diff --git a/ntp_io_linux.c b/ntp_io_linux.c index 6988a286..8f93f599 100644 --- a/ntp_io_linux.c +++ b/ntp_io_linux.c @@ -439,6 +439,14 @@ NIO_Linux_Finalise(void) /* ================================================== */ +int +NIO_Linux_IsHwTsEnabled(void) +{ + return ARR_GetSize(interfaces) > 0; +} + +/* ================================================== */ + int NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events) { diff --git a/ntp_io_linux.h b/ntp_io_linux.h index 089d4a12..79788895 100644 --- a/ntp_io_linux.h +++ b/ntp_io_linux.h @@ -33,6 +33,8 @@ extern void NIO_Linux_Initialise(void); extern void NIO_Linux_Finalise(void); +extern int NIO_Linux_IsHwTsEnabled(void); + extern int NIO_Linux_SetTimestampSocketOptions(int sock_fd, int client_only, int *events); extern int NIO_Linux_ProcessMessage(SCK_Message *message, NTP_Local_Address *local_addr, diff --git a/test/unit/ntp_core.c b/test/unit/ntp_core.c index 7d649867..08a15326 100644 --- a/test/unit/ntp_core.c +++ b/test/unit/ntp_core.c @@ -35,6 +35,7 @@ static struct timespec current_time; static NTP_Packet req_buffer, res_buffer; static int req_length, res_length; +#define NIO_IsHwTsEnabled() 1 #define NIO_OpenServerSocket(addr) ((addr)->ip_addr.family != IPADDR_UNSPEC ? 100 : 0) #define NIO_CloseServerSocket(fd) assert(fd == 100) #define NIO_OpenClientSocket(addr) ((addr)->ip_addr.family != IPADDR_UNSPEC ? 101 : 0) @@ -106,13 +107,9 @@ send_request(NCR_Instance inst, int late_hwts) } if (late_hwts) { - inst->had_hw_tx_timestamp = 1; inst->report.total_good_count++; } else { - if (random() % 2) - inst->had_hw_tx_timestamp = 0; - else - inst->report.total_good_count = 0; + inst->report.total_good_count = 0; } } -- 2.47.2