From 192b8e315c4585489d7aa7f59683035998805e40 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Mon, 12 Nov 2018 17:28:00 +0100 Subject: [PATCH] sysoff: Add support for PTP_SYS_OFFSET_PRECISE ioctl. This ioctl uses cross timestamping for a more accurate measurement of the offset. It is supported on some onboard Intel NICs using the e1000e driver and a virtual PHC with the ptp_kvm driver. Signed-off-by: Miroslav Lichvar --- sysoff.c | 21 +++++++++++++++++++++ sysoff.h | 1 + 2 files changed, 22 insertions(+) diff --git a/sysoff.c b/sysoff.c index f709a9b..9f65d95 100644 --- a/sysoff.c +++ b/sysoff.c @@ -22,6 +22,7 @@ #include #include +#include "print.h" #include "sysoff.h" #define NS_PER_SEC 1000000000LL @@ -39,6 +40,23 @@ static struct { uint64_t timestamp; } samples[PTP_MAX_SAMPLES]; +static int sysoff_precise(int fd, int64_t *result, uint64_t *ts) +{ +#ifdef PTP_SYS_OFFSET_PRECISE + struct ptp_sys_offset_precise pso; + memset(&pso, 0, sizeof(pso)); + if (ioctl(fd, PTP_SYS_OFFSET_PRECISE, &pso)) { + pr_debug("ioctl PTP_SYS_OFFSET_PRECISE: %m"); + return SYSOFF_RUN_TIME_MISSING; + } + *result = pctns(&pso.sys_realtime) - pctns(&pso.device); + *ts = pctns(&pso.sys_realtime); + return SYSOFF_PRECISE; +#else + return SYSOFF_COMPILE_TIME_MISSING; +#endif +} + static void insertion_sort(int length, int64_t interval, int64_t offset, uint64_t ts) { int i = length - 1; @@ -91,6 +109,9 @@ int sysoff_measure(int fd, int method, int n_samples, int64_t *result, uint64_t *ts, int64_t *delay) { switch (method) { + case SYSOFF_PRECISE: + *delay = 0; + return sysoff_precise(fd, result, ts); case SYSOFF_BASIC: return sysoff_basic(fd, n_samples, result, ts, delay); } diff --git a/sysoff.h b/sysoff.h index 02ecdfa..37f7353 100644 --- a/sysoff.h +++ b/sysoff.h @@ -23,6 +23,7 @@ enum { SYSOFF_COMPILE_TIME_MISSING = -2, SYSOFF_RUN_TIME_MISSING = -1, + SYSOFF_PRECISE, SYSOFF_BASIC, SYSOFF_LAST, };