8cf965
commit c0e49c708814ec783726fe92202371847703c5ed
8cf965
Author: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
Date:   Mon Nov 12 17:27:58 2018 +0100
8cf965
8cf965
    sysoff: Initialize data for ioctl(PTP_SYS_OFFSET).
8cf965
    
8cf965
    This fixes valgrind errors.
8cf965
    
8cf965
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
8cf965
diff --git a/sysoff.c b/sysoff.c
8cf965
index f7b6240..407a01c 100644
8cf965
--- a/sysoff.c
8cf965
+++ b/sysoff.c
8cf965
@@ -18,6 +18,7 @@
8cf965
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
8cf965
  */
8cf965
 #include <stdio.h>
8cf965
+#include <string.h>
8cf965
 #include <sys/ioctl.h>
8cf965
 #include <linux/ptp_clock.h>
8cf965
 
8cf965
@@ -76,6 +77,7 @@ int sysoff_measure(int fd, int n_samples,
8cf965
 		   int64_t *result, uint64_t *ts, int64_t *delay)
8cf965
 {
8cf965
 	struct ptp_sys_offset pso;
8cf965
+	memset(&pso, 0, sizeof(pso));
8cf965
 	pso.n_samples = n_samples;
8cf965
 	if (ioctl(fd, PTP_SYS_OFFSET, &pso)) {
8cf965
 		perror("ioctl PTP_SYS_OFFSET");
8cf965
8cf965
commit 93baf34adb81046a5e1c3b9a3e685029f2046993
8cf965
Author: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
Date:   Mon Nov 12 17:27:59 2018 +0100
8cf965
8cf965
    sysoff: Extend API for different sysoff methods.
8cf965
    
8cf965
    The kernel supports different PTP_SYS_OFFSET* ioctls. Use the sysoff
8cf965
    enum to allow selecting between them in sysoff_measure().
8cf965
    
8cf965
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
8cf965
diff --git a/phc2sys.c b/phc2sys.c
8cf965
index 15f8d75..2cd477a 100644
8cf965
--- a/phc2sys.c
8cf965
+++ b/phc2sys.c
8cf965
@@ -74,7 +74,7 @@ struct clock {
8cf965
 	LIST_ENTRY(clock) dst_list;
8cf965
 	clockid_t clkid;
8cf965
 	int phc_index;
8cf965
-	int sysoff_supported;
8cf965
+	int sysoff_method;
8cf965
 	int is_utc;
8cf965
 	int dest_only;
8cf965
 	int state;
8cf965
@@ -255,9 +255,8 @@ static struct clock *clock_add(struct node *node, char *device)
8cf965
 		c->servo = servo_add(node, c);
8cf965
 
8cf965
 	if (clkid != CLOCK_INVALID && clkid != CLOCK_REALTIME)
8cf965
-		c->sysoff_supported = (SYSOFF_SUPPORTED ==
8cf965
-				       sysoff_probe(CLOCKID_TO_FD(clkid),
8cf965
-						    node->phc_readings));
8cf965
+		c->sysoff_method = sysoff_probe(CLOCKID_TO_FD(clkid),
8cf965
+						node->phc_readings);
8cf965
 
8cf965
 	LIST_INSERT_HEAD(&node->clocks, c, list);
8cf965
 	return c;
8cf965
@@ -784,11 +783,12 @@ static int do_loop(struct node *node, int subscriptions)
8cf965
 				continue;
8cf965
 
8cf965
 			if (clock->clkid == CLOCK_REALTIME &&
8cf965
-			    node->master->sysoff_supported) {
8cf965
+			    node->master->sysoff_method >= 0) {
8cf965
 				/* use sysoff */
8cf965
 				if (sysoff_measure(CLOCKID_TO_FD(node->master->clkid),
8cf965
+						   node->master->sysoff_method,
8cf965
 						   node->phc_readings,
8cf965
-						   &offset, &ts, &delay))
8cf965
+						   &offset, &ts, &delay) < 0)
8cf965
 					return -1;
8cf965
 			} else {
8cf965
 				/* use phc */
8cf965
diff --git a/phc_ctl.c b/phc_ctl.c
8cf965
index 4a78a19..b9a9cf4 100644
8cf965
--- a/phc_ctl.c
8cf965
+++ b/phc_ctl.c
8cf965
@@ -367,10 +367,12 @@ static int do_cmp(clockid_t clkid, int cmdc, char *cmdv[])
8cf965
 	struct timespec ts, rta, rtb;
8cf965
 	int64_t sys_offset, delay = 0, offset;
8cf965
 	uint64_t sys_ts;
8cf965
+	int method;
8cf965
 
8cf965
-	if (SYSOFF_SUPPORTED ==
8cf965
-	    sysoff_measure(CLOCKID_TO_FD(clkid),
8cf965
-			   9, &sys_offset, &sys_ts, &delay)) {
8cf965
+	method = sysoff_probe(CLOCKID_TO_FD(clkid), 9);
8cf965
+
8cf965
+	if (method >= 0 && sysoff_measure(CLOCKID_TO_FD(clkid), method, 9,
8cf965
+					  &sys_offset, &sys_ts, &delay) >= 0) {
8cf965
 		pr_notice( "offset from CLOCK_REALTIME is %"PRId64"ns\n",
8cf965
 			sys_offset);
8cf965
 		return 0;
8cf965
diff --git a/sysoff.c b/sysoff.c
8cf965
index 407a01c..f709a9b 100644
8cf965
--- a/sysoff.c
8cf965
+++ b/sysoff.c
8cf965
@@ -73,8 +73,8 @@ static int64_t sysoff_estimate(struct ptp_clock_time *pct, int n_samples,
8cf965
 	return samples[0].offset;
8cf965
 }
8cf965
 
8cf965
-int sysoff_measure(int fd, int n_samples,
8cf965
-		   int64_t *result, uint64_t *ts, int64_t *delay)
8cf965
+static int sysoff_basic(int fd, int n_samples,
8cf965
+			int64_t *result, uint64_t *ts, int64_t *delay)
8cf965
 {
8cf965
 	struct ptp_sys_offset pso;
8cf965
 	memset(&pso, 0, sizeof(pso));
8cf965
@@ -84,13 +84,24 @@ int sysoff_measure(int fd, int n_samples,
8cf965
 		return SYSOFF_RUN_TIME_MISSING;
8cf965
 	}
8cf965
 	*result = sysoff_estimate(pso.ts, n_samples, ts, delay);
8cf965
-	return SYSOFF_SUPPORTED;
8cf965
+	return SYSOFF_BASIC;
8cf965
+}
8cf965
+
8cf965
+int sysoff_measure(int fd, int method, int n_samples,
8cf965
+		   int64_t *result, uint64_t *ts, int64_t *delay)
8cf965
+{
8cf965
+	switch (method) {
8cf965
+	case SYSOFF_BASIC:
8cf965
+		return sysoff_basic(fd, n_samples, result, ts, delay);
8cf965
+	}
8cf965
+	return SYSOFF_COMPILE_TIME_MISSING;
8cf965
 }
8cf965
 
8cf965
 int sysoff_probe(int fd, int n_samples)
8cf965
 {
8cf965
 	int64_t junk, delay;
8cf965
 	uint64_t ts;
8cf965
+	int i;
8cf965
 
8cf965
 	if (n_samples > PTP_MAX_SAMPLES) {
8cf965
 		fprintf(stderr, "warning: %d exceeds kernel max readings %d\n",
8cf965
@@ -99,7 +110,13 @@ int sysoff_probe(int fd, int n_samples)
8cf965
 		return SYSOFF_RUN_TIME_MISSING;
8cf965
 	}
8cf965
 
8cf965
-	return sysoff_measure(fd, n_samples, &junk, &ts, &delay);
8cf965
+	for (i = 0; i < SYSOFF_LAST; i++) {
8cf965
+		if (sysoff_measure(fd, i, n_samples, &junk, &ts, &delay) < 0)
8cf965
+			continue;
8cf965
+		return i;
8cf965
+	}
8cf965
+
8cf965
+	return SYSOFF_RUN_TIME_MISSING;
8cf965
 }
8cf965
 
8cf965
 #else /* !PTP_SYS_OFFSET */
8cf965
diff --git a/sysoff.h b/sysoff.h
8cf965
index cb70265..02ecdfa 100644
8cf965
--- a/sysoff.h
8cf965
+++ b/sysoff.h
8cf965
@@ -21,13 +21,14 @@
8cf965
 #include <stdint.h>
8cf965
 
8cf965
 enum {
8cf965
-	SYSOFF_SUPPORTED,
8cf965
-	SYSOFF_COMPILE_TIME_MISSING,
8cf965
-	SYSOFF_RUN_TIME_MISSING,
8cf965
+	SYSOFF_COMPILE_TIME_MISSING = -2,
8cf965
+	SYSOFF_RUN_TIME_MISSING = -1,
8cf965
+	SYSOFF_BASIC,
8cf965
+	SYSOFF_LAST,
8cf965
 };
8cf965
 
8cf965
 /**
8cf965
- * Check to see if the PTP_SYS_OFFSET ioctl is supported.
8cf965
+ * Check to see if a PTP_SYS_OFFSET ioctl is supported.
8cf965
  * @param fd  An open file descriptor to a PHC device.
8cf965
  * @return  One of the SYSOFF_ enumeration values.
8cf965
  */
8cf965
@@ -36,11 +37,12 @@ int sysoff_probe(int fd, int n_samples);
8cf965
 /**
8cf965
  * Measure the offset between a PHC and the system time.
8cf965
  * @param fd         An open file descriptor to a PHC device.
8cf965
+ * @param method     A non-negative SYSOFF_ value returned by sysoff_probe().
8cf965
  * @param n_samples  The number of consecutive readings to make.
8cf965
  * @param result     The estimated offset in nanoseconds.
8cf965
  * @param ts         The system time corresponding to the 'result'.
8cf965
  * @param delay      The delay in reading of the clock in nanoseconds.
8cf965
  * @return  One of the SYSOFF_ enumeration values.
8cf965
  */
8cf965
-int sysoff_measure(int fd, int n_samples,
8cf965
+int sysoff_measure(int fd, int method, int n_samples,
8cf965
 		   int64_t *result, uint64_t *ts, int64_t *delay);
8cf965
8cf965
commit 192b8e315c4585489d7aa7f59683035998805e40
8cf965
Author: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
Date:   Mon Nov 12 17:28:00 2018 +0100
8cf965
8cf965
    sysoff: Add support for PTP_SYS_OFFSET_PRECISE ioctl.
8cf965
    
8cf965
    This ioctl uses cross timestamping for a more accurate measurement of
8cf965
    the offset. It is supported on some onboard Intel NICs using the e1000e
8cf965
    driver and a virtual PHC with the ptp_kvm driver.
8cf965
    
8cf965
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
8cf965
diff --git a/sysoff.c b/sysoff.c
8cf965
index f709a9b..9f65d95 100644
8cf965
--- a/sysoff.c
8cf965
+++ b/sysoff.c
8cf965
@@ -22,6 +22,7 @@
8cf965
 #include <sys/ioctl.h>
8cf965
 #include <linux/ptp_clock.h>
8cf965
 
8cf965
+#include "print.h"
8cf965
 #include "sysoff.h"
8cf965
 
8cf965
 #define NS_PER_SEC 1000000000LL
8cf965
@@ -39,6 +40,23 @@ static struct {
8cf965
 	uint64_t timestamp;
8cf965
 } samples[PTP_MAX_SAMPLES];
8cf965
 
8cf965
+static int sysoff_precise(int fd, int64_t *result, uint64_t *ts)
8cf965
+{
8cf965
+#ifdef PTP_SYS_OFFSET_PRECISE
8cf965
+	struct ptp_sys_offset_precise pso;
8cf965
+	memset(&pso, 0, sizeof(pso));
8cf965
+	if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, &pso)) {
8cf965
+		pr_debug("ioctl PTP_SYS_OFFSET_PRECISE: %m");
8cf965
+		return SYSOFF_RUN_TIME_MISSING;
8cf965
+	}
8cf965
+	*result = pctns(&pso.sys_realtime) - pctns(&pso.device);
8cf965
+	*ts = pctns(&pso.sys_realtime);
8cf965
+	return SYSOFF_PRECISE;
8cf965
+#else
8cf965
+	return SYSOFF_COMPILE_TIME_MISSING;
8cf965
+#endif
8cf965
+}
8cf965
+
8cf965
 static void insertion_sort(int length, int64_t interval, int64_t offset, uint64_t ts)
8cf965
 {
8cf965
 	int i = length - 1;
8cf965
@@ -91,6 +109,9 @@ int sysoff_measure(int fd, int method, int n_samples,
8cf965
 		   int64_t *result, uint64_t *ts, int64_t *delay)
8cf965
 {
8cf965
 	switch (method) {
8cf965
+	case SYSOFF_PRECISE:
8cf965
+		*delay = 0;
8cf965
+		return sysoff_precise(fd, result, ts);
8cf965
 	case SYSOFF_BASIC:
8cf965
 		return sysoff_basic(fd, n_samples, result, ts, delay);
8cf965
 	}
8cf965
diff --git a/sysoff.h b/sysoff.h
8cf965
index 02ecdfa..37f7353 100644
8cf965
--- a/sysoff.h
8cf965
+++ b/sysoff.h
8cf965
@@ -23,6 +23,7 @@
8cf965
 enum {
8cf965
 	SYSOFF_COMPILE_TIME_MISSING = -2,
8cf965
 	SYSOFF_RUN_TIME_MISSING = -1,
8cf965
+	SYSOFF_PRECISE,
8cf965
 	SYSOFF_BASIC,
8cf965
 	SYSOFF_LAST,
8cf965
 };
8cf965
8cf965
commit 68a9011c9d7d859920da339ba59c14dc1d617a45
8cf965
Author: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
Date:   Mon Nov 12 17:28:01 2018 +0100
8cf965
8cf965
    sysoff: Add support for PTP_SYS_OFFSET_EXTENDED ioctl.
8cf965
    
8cf965
    This is a more accurate variant of the the PTP_SYS_OFFSET ioctl, which
8cf965
    will probably be supported in future kernel versions.
8cf965
    
8cf965
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
8cf965
diff --git a/sysoff.c b/sysoff.c
8cf965
index 9f65d95..b993ee9 100644
8cf965
--- a/sysoff.c
8cf965
+++ b/sysoff.c
8cf965
@@ -71,17 +71,23 @@ static void insertion_sort(int length, int64_t interval, int64_t offset, uint64_
8cf965
 	samples[i+1].timestamp = ts;
8cf965
 }
8cf965
 
8cf965
-static int64_t sysoff_estimate(struct ptp_clock_time *pct, int n_samples,
8cf965
-			       uint64_t *ts, int64_t *delay)
8cf965
+static int64_t sysoff_estimate(struct ptp_clock_time *pct, int extended,
8cf965
+			       int n_samples, uint64_t *ts, int64_t *delay)
8cf965
 {
8cf965
 	int64_t t1, t2, tp;
8cf965
 	int64_t interval, offset;
8cf965
 	int i;
8cf965
 
8cf965
 	for (i = 0; i < n_samples; i++) {
8cf965
-		t1 = pctns(&pct[2*i]);
8cf965
-		tp = pctns(&pct[2*i+1]);
8cf965
-		t2 = pctns(&pct[2*i+2]);
8cf965
+		if (extended) {
8cf965
+			t1 = pctns(&pct[3*i]);
8cf965
+			tp = pctns(&pct[3*i+1]);
8cf965
+			t2 = pctns(&pct[3*i+2]);
8cf965
+		} else {
8cf965
+			t1 = pctns(&pct[2*i]);
8cf965
+			tp = pctns(&pct[2*i+1]);
8cf965
+			t2 = pctns(&pct[2*i+2]);
8cf965
+		}
8cf965
 		interval = t2 - t1;
8cf965
 		offset = (t2 + t1) / 2 - tp;
8cf965
 		insertion_sort(i, interval, offset, (t2 + t1) / 2);
8cf965
@@ -91,6 +97,24 @@ static int64_t sysoff_estimate(struct ptp_clock_time *pct, int n_samples,
8cf965
 	return samples[0].offset;
8cf965
 }
8cf965
 
8cf965
+static int sysoff_extended(int fd, int n_samples,
8cf965
+			   int64_t *result, uint64_t *ts, int64_t *delay)
8cf965
+{
8cf965
+#ifdef PTP_SYS_OFFSET_EXTENDED
8cf965
+	struct ptp_sys_offset_extended pso;
8cf965
+	memset(&pso, 0, sizeof(pso));
8cf965
+	pso.n_samples = n_samples;
8cf965
+	if (ioctl(fd, PTP_SYS_OFFSET_EXTENDED, &pso)) {
8cf965
+		pr_debug("ioctl PTP_SYS_OFFSET_EXTENDED: %m");
8cf965
+		return SYSOFF_RUN_TIME_MISSING;
8cf965
+	}
8cf965
+	*result = sysoff_estimate(&pso.ts[0][0], 1, n_samples, ts, delay);
8cf965
+	return SYSOFF_EXTENDED;
8cf965
+#else
8cf965
+	return SYSOFF_COMPILE_TIME_MISSING;
8cf965
+#endif
8cf965
+}
8cf965
+
8cf965
 static int sysoff_basic(int fd, int n_samples,
8cf965
 			int64_t *result, uint64_t *ts, int64_t *delay)
8cf965
 {
8cf965
@@ -101,7 +125,7 @@ static int sysoff_basic(int fd, int n_samples,
8cf965
 		perror("ioctl PTP_SYS_OFFSET");
8cf965
 		return SYSOFF_RUN_TIME_MISSING;
8cf965
 	}
8cf965
-	*result = sysoff_estimate(pso.ts, n_samples, ts, delay);
8cf965
+	*result = sysoff_estimate(pso.ts, 0, n_samples, ts, delay);
8cf965
 	return SYSOFF_BASIC;
8cf965
 }
8cf965
 
8cf965
@@ -112,6 +136,8 @@ int sysoff_measure(int fd, int method, int n_samples,
8cf965
 	case SYSOFF_PRECISE:
8cf965
 		*delay = 0;
8cf965
 		return sysoff_precise(fd, result, ts);
8cf965
+	case SYSOFF_EXTENDED:
8cf965
+		return sysoff_extended(fd, n_samples, result, ts, delay);
8cf965
 	case SYSOFF_BASIC:
8cf965
 		return sysoff_basic(fd, n_samples, result, ts, delay);
8cf965
 	}
8cf965
diff --git a/sysoff.h b/sysoff.h
8cf965
index 37f7353..79d2290 100644
8cf965
--- a/sysoff.h
8cf965
+++ b/sysoff.h
8cf965
@@ -24,6 +24,7 @@ enum {
8cf965
 	SYSOFF_COMPILE_TIME_MISSING = -2,
8cf965
 	SYSOFF_RUN_TIME_MISSING = -1,
8cf965
 	SYSOFF_PRECISE,
8cf965
+	SYSOFF_EXTENDED,
8cf965
 	SYSOFF_BASIC,
8cf965
 	SYSOFF_LAST,
8cf965
 };
8cf965
8cf965
commit 8142da41b61fb5b9ee4ad8f5ab56adb0447cd37b
8cf965
Author: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
Date:   Mon Nov 12 17:28:02 2018 +0100
8cf965
8cf965
    phc2sys: Use reversed sysoff when synchronizing to system clock.
8cf965
    
8cf965
    If synchronizing a PHC to the system clock, use one of the
8cf965
    PTP_SYS_OFFSET ioctls (if supported) to measure the offset between the
8cf965
    two clocks. Negate the offset and switch the timestamp before passing
8cf965
    them to the servo.
8cf965
    
8cf965
    This makes the synchronization between PHC and system clock symmetric.
8cf965
    
8cf965
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
8cf965
8cf965
diff --git a/phc2sys.c b/phc2sys.c
8cf965
index 2cd477a..b8f1ea0 100644
8cf965
--- a/phc2sys.c
8cf965
+++ b/phc2sys.c
8cf965
@@ -790,6 +790,16 @@ static int do_loop(struct node *node, int subscriptions)
8cf965
 						   node->phc_readings,
8cf965
 						   &offset, &ts, &delay) < 0)
8cf965
 					return -1;
8cf965
+			} else if (node->master->clkid == CLOCK_REALTIME &&
8cf965
+				   clock->sysoff_method >= 0) {
8cf965
+				/* use reversed sysoff */
8cf965
+				if (sysoff_measure(CLOCKID_TO_FD(clock->clkid),
8cf965
+						   clock->sysoff_method,
8cf965
+						   node->phc_readings,
8cf965
+						   &offset, &ts, &delay) < 0)
8cf965
+					return -1;
8cf965
+				ts += offset;
8cf965
+				offset = -offset;
8cf965
 			} else {
8cf965
 				/* use phc */
8cf965
 				if (!read_phc(node->master->clkid, clock->clkid,