]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Add support for SO_TIMESTAMP control messages
authorMiroslav Lichvar <mlichvar@redhat.com>
Fri, 7 Aug 2009 15:24:19 +0000 (17:24 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Sat, 8 Aug 2009 10:50:04 +0000 (12:50 +0200)
SO_TIMESTAMP messages contain kernel time stamps for received packets,
this should improve accuracy and avoid the impact of CPU scheduling
latencies.

ntp_io.c

index c3594b3ae8754f95c1caa319ddb68ddd1698a7f0..4e4b98966565bf4eb0ce6bfd86e7fde7c586b4fc 100644 (file)
--- a/ntp_io.c
+++ b/ntp_io.c
@@ -119,6 +119,12 @@ NIO_Initialise(void)
     /* Don't quit - we might survive anyway */
   }
 
+  /* Enable receiving of timestamp control messages */
+  if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMP, (char *)&on_off, sizeof(on_off)) < 0) {
+    LOG(LOGS_ERR, LOGF_NtpIO, "Could not set timestamp socket options");
+    /* Don't quit - we might survive anyway */
+  }
+
   /* We want the local IP info too */
   if (setsockopt(sock_fd, IPPROTO_IP, IP_PKTINFO, (char *)&on_off, sizeof(on_off)) < 0) {
     LOG(LOGS_ERR, LOGF_NtpIO, "Could not request packet info using socket option");
@@ -233,6 +239,20 @@ read_from_socket(void *anything)
         memcpy(&ipi, CMSG_DATA(cmsg), sizeof(ipi));
         remote_addr.local_ip_addr = ntohl(ipi.ipi_spec_dst.s_addr);
       }
+
+      if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMP) {
+        struct timeval tv;
+        double correction;
+
+        memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
+        correction = LCL_GetOffsetCorrection(&tv);
+        UTI_AddDoubleToTimeval(&tv, correction, &tv);
+#if 0
+        UTI_DiffTimevalsToDouble(&correction, &now, &tv);
+        LOG(LOGS_INFO, LOGF_NtpIO, "timestamp diff: %f", correction);
+#endif
+        now = tv;
+      }
     }
 
     if (status == NTP_NORMAL_PACKET_SIZE) {