]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: don't require previous HW TX timestamp to wait for another
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 8 Aug 2023 13:44:25 +0000 (15:44 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 8 Aug 2023 14:06:58 +0000 (16:06 +0200)
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
ntp_io.c
ntp_io.h
ntp_io_linux.c
ntp_io_linux.h
test/unit/ntp_core.c

index 73519a318de83ecc61aa37e10977ca5c455bc037..22cab439bf55ad66f73b48dc5b372151843542b0 100644 (file)
@@ -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);
   }
index deb8e25ad61840e072db401d98a99d4aa51be270..fce7b1772478c460a1968497966783b432b9b845 100644 (file)
--- 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)
 {
index 19ffeed661039d8aba5b10ef707b495da2ab977d..427bd966ea95f35593c874382cb3e2f48f58e2f1 100644 (file)
--- 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);
 
index 6988a2863bcb859d0dff0c9e9ac4162d8ccfea7b..8f93f599dd8ca8eeefff81a8056c631c9639af6d 100644 (file)
@@ -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)
 {
index 089d4a12ac2435bf3d9ac2de0aada2a18f0e6b9e..79788895c782e54d52f9652f76befea7bb6d5138 100644 (file)
@@ -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,
index 7d6498674a6add87fb151c43369399352fe866c2..08a153260751bfb2e2532bdfb6f769c1f7ee56d0 100644 (file)
@@ -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;
   }
 }