]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1374] Support SO_TIMESTAMPNS
authorHarlan Stenn <stenn@ntp.org>
Tue, 9 Oct 2012 03:50:56 +0000 (23:50 -0400)
committerHarlan Stenn <stenn@ntp.org>
Tue, 9 Oct 2012 03:50:56 +0000 (23:50 -0400)
bk: 50739f20bjmMHhSxJ5vUqBJ-WMDsNg

ChangeLog
ntpd/ntp_io.c

index ffa6086b14e646fcf0838097700b14801e266fba..83e074849e6fefab80c1da41f07a573c81287cf3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* [Bug 1374] Support SO_TIMESTAMPNS.
 * [Bug 2266] Remove deprecated refclock_trak.c from Windows Makefile
   equivalents.
 * [Bug 2274] Bring libopts/enum.c back to (old) ANSI C compliance.
index 3d77864ecf1ce30a73f86469b998a5fff654d94b..14ae72dfeb817d0d26eaae2218d02615919398de 100644 (file)
@@ -87,17 +87,25 @@ struct nic_rule_tag {
 nic_rule *nic_rule_list;
 
 
-#if defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP)
-#if defined(CMSG_FIRSTHDR)
-#define HAVE_TIMESTAMP
-#define USE_TIMESTAMP_CMSG
-#ifndef TIMESTAMP_CTLMSGBUF_SIZE
-#define TIMESTAMP_CTLMSGBUF_SIZE 1536 /* moderate default */
-#endif
+#if defined(SO_TIMESTAMPNS) && defined(SCM_TIMESTAMPNS) && defined(CMSG_FIRSTHDR)
+#  define HAVE_PACKET_TIMESTAMP
+#  define HAVE_TIMESTAMPNS
+#  ifdef TIMESTAMPNS_CTLMSGBUF_SIZE
+#   define CMSG_BUFSIZE TIMESTAMPNS_CTLMSGBUF_SIZE
+#  else
+#   define CMSG_BUFSIZE  1536 /* moderate default */
+#  endif
+#elif defined(SO_TIMESTAMP) && defined(SCM_TIMESTAMP) && defined(CMSG_FIRSTHDR)
+#  define HAVE_PACKET_TIMESTAMP
+#  define HAVE_TIMESTAMP
+#  ifdef TIMESTAMP_CTLMSGBUF_SIZE
+#   define CMSG_BUFSIZE TIMESTAMP_CTLMSGBUF_SIZE
+#  else
+#   define CMSG_BUFSIZE  1536 /* moderate default */
+#  endif
 #else
 /* fill in for old/other timestamp interfaces */
 #endif
-#endif
 
 #if defined(SYS_WINNT)
 #include "win32_io.h"
@@ -2943,6 +2951,19 @@ open_socket(
                                    fd, stoa(addr)));
        }       
 #endif
+#ifdef HAVE_TIMESTAMPNS
+       {
+               if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS,
+                              (char*)&on, sizeof(on)))
+                       msyslog(LOG_DEBUG,
+                               "setsockopt SO_TIMESTAMPNS on fails on address %s: %m",
+                               stoa(addr));
+               else
+                       DPRINTF(4, ("setsockopt SO_TIMESTAMPNS enabled on fd %d address %s\n",
+                                   fd, stoa(addr)));
+       }       
+#endif
+
        DPRINTF(4, ("bind(%d) AF_INET%s, addr %s%%%d#%d, flags 0x%x\n",
                   fd, IS_IPV6(addr) ? "6" : "", stoa(addr),
                   SCOPE(addr), SRCPORT(addr), interf->flags));
@@ -3176,7 +3197,7 @@ read_refclock_packet(
 #endif /* REFCLOCK */
 
 
-#ifdef HAVE_TIMESTAMP
+#ifdef HAVE_PACKET_TIMESTAMP
 /*
  * extract timestamps from control message buffer
  */
@@ -3187,7 +3208,6 @@ fetch_timestamp(
        l_fp                    ts
        )
 {
-#ifdef USE_TIMESTAMP_CMSG
        struct cmsghdr *        cmsghdr;
        struct timeval *        tvp;
        long                    ticks;
@@ -3202,6 +3222,7 @@ fetch_timestamp(
        while (cmsghdr != NULL) {
                switch (cmsghdr->cmsg_type)
                {
+#ifdef HAVE_TIMESTAMP
                case SCM_TIMESTAMP:
                        tvp = (struct timeval *)CMSG_DATA(cmsghdr);
                        if (sys_tick > measured_tick &&
@@ -3211,7 +3232,7 @@ fetch_timestamp(
                                tvp->tv_usec = (long)(ticks * 1e6 *
                                                      sys_tick);
                        }
-                       DPRINTF(4, ("fetch_timestamp: system network time stamp: %ld.%06ld\n",
+                       DPRINTF(4, ("fetch_timestamp: system usec network time stamp: %ld.%06ld\n",
                                    tvp->tv_sec, tvp->tv_usec));
                        nts = tval_stamp_to_lfp(*tvp);
                        fuzz = ntp_random() * 2. / FRAC * sys_fuzz;
@@ -3234,7 +3255,38 @@ fetch_timestamp(
                }
                cmsghdr = CMSG_NXTHDR(msghdr, cmsghdr);
        }
-#endif /* USE_TIMESTAMP_CMSG */
+#endif /* HAVE_TIMESTAMP */
+#ifdef HAVE_TIMESTAMPNS
+               case SCM_TIMESTAMPNS:
+               {
+                       struct timespec *tsp;
+                       double dtemp;
+                       l_fp nts;
+
+                       tsp = (struct timespec *)CMSG_DATA(cmsghdr);
+                       DPRINTF(4, ("fetch_timestamp: system nsec network time stamp: %ld.%09ld\n",
+                                   tsp->tv_sec, tsp->tv_nsec));
+                       nts.l_i = tsp->tv_sec + JAN_1970;
+                       dtemp = (tsp->tv_nsec 
+                                + (ntp_random() * 2. / FRAC)) / 1e9;
+                       nts.l_uf = (u_int32)(dtemp * FRAC);
+#ifdef DEBUG_TIMING
+                       {
+                               l_fp dts;
+
+                               dts = ts;
+                               L_SUB(&dts, &nts);
+                               collect_timing(rb, 
+                                              "input processing delay",
+                                              1, &dts);
+                               DPRINTF(4, ("fetch_timestamp: timestamp delta: %s (incl. prec fuzz)\n",
+                                           lfptoa(&dts, 9)));
+                       }
+#endif
+                       ts = nts;  /* network time stamp */
+                       break;
+               }
+#endif /* HAVE_TIMESTAMPNS */
        return ts;
 }
 #endif /* HAVE_TIMESTAMP */
@@ -3255,10 +3307,10 @@ read_network_packet(
        GETSOCKNAME_SOCKLEN_TYPE fromlen;
        int buflen;
        register struct recvbuf *rb;
-#ifdef HAVE_TIMESTAMP
+#ifdef HAVE_PACKET_TIMESTAMP
        struct msghdr msghdr;
        struct iovec iovec;
-       char control[TIMESTAMP_CTLMSGBUF_SIZE];
+       char control[CMSG_BUFSIZE];
 #endif
 
        /*
@@ -3293,7 +3345,7 @@ read_network_packet(
 
        fromlen = sizeof(rb->recv_srcadr);
 
-#ifndef HAVE_TIMESTAMP
+#ifndef HAVE_PACKET_TIMESTAMP
        rb->recv_length = recvfrom(fd, (char *)&rb->recv_space,
                                   sizeof(rb->recv_space), 0,
                                   &rb->recv_srcadr.sa, &fromlen);
@@ -3338,7 +3390,7 @@ read_network_packet(
         */
        rb->dstadr = itf;
        rb->fd = fd;
-#ifdef HAVE_TIMESTAMP
+#ifdef HAVE_PACKET_TIMESTAMP
        /* pick up a network time stamp if possible */
        ts = fetch_timestamp(rb, &msghdr, ts);
 #endif