From 4f7558872216b1ceb76ae89ae1dd56796835fbdb Mon Sep 17 00:00:00 2001 From: ckuethe <> Date: Wed, 12 Sep 2007 21:08:46 +0000 Subject: [PATCH] Add a knob to compensate for a refclock that is early or late. Based on a diff from Maurice Janssen. Manpage help from jmc and Maurice, other nits from deraadt and otto. ok deraadt, otto --- src/usr.sbin/ntpd/ntpd.conf.5 | 14 ++++++++++++-- src/usr.sbin/ntpd/ntpd.h | 4 +++- src/usr.sbin/ntpd/parse.y | 21 ++++++++++++++++++--- src/usr.sbin/ntpd/sensors.c | 9 ++++++--- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/usr.sbin/ntpd/ntpd.conf.5 b/src/usr.sbin/ntpd/ntpd.conf.5 index c33c5e90..72c87ce6 100644 --- a/src/usr.sbin/ntpd/ntpd.conf.5 +++ b/src/usr.sbin/ntpd/ntpd.conf.5 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ntpd.conf.5,v 1.16 2007/05/31 19:20:26 jmc Exp $ +.\" $OpenBSD: ntpd.conf.5,v 1.17 2007/09/12 21:08:45 ckuethe Exp $ .\" .\" Copyright (c) 2003, 2004 Henning Brauer .\" @@ -14,7 +14,7 @@ .\" AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT .\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: May 31 2007 $ +.Dd $Mdocdate: September 12 2007 $ .Dt NTPD.CONF 5 .Os .Sh NAME @@ -71,6 +71,7 @@ listen on 127.0.0.1 listen on ::1 .Ed .It Xo Ic sensor Ar device +.Op Ic correction Ar microseconds .Op Ic weight Ar weight-value .Xc Specify a timedelta sensor device @@ -92,6 +93,15 @@ For example: sensor * sensor udcf0 .Ed +.Pp +An optional correction in microseconds can be given to compensate +for the sensor's offset. +The maximum correction is 127 seconds. +For example, if your DCF77 receiver is lagging 15 ms behind +actual time: +.Bd -literal -offset indent +sensor udcf0 correction 15000 +.Ed .It Xo Ic server Ar address .Op Ic weight Ar weight-value .Xc diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index daf17772..9b841d79 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.85 2007/08/04 02:58:02 ckuethe Exp $ */ +/* $OpenBSD: ntpd.h,v 1.86 2007/09/12 21:08:46 ckuethe Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -141,6 +141,7 @@ struct ntp_sensor { time_t last; char *device; int sensordevid; + int correction; u_int8_t weight; u_int8_t shift; }; @@ -148,6 +149,7 @@ struct ntp_sensor { struct ntp_conf_sensor { TAILQ_ENTRY(ntp_conf_sensor) entry; char *device; + int correction; u_int8_t weight; }; diff --git a/src/usr.sbin/ntpd/parse.y b/src/usr.sbin/ntpd/parse.y index 2deaa62d..310440cd 100644 --- a/src/usr.sbin/ntpd/parse.y +++ b/src/usr.sbin/ntpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.32 2007/09/12 18:32:54 deraadt Exp $ */ +/* $OpenBSD: parse.y,v 1.33 2007/09/12 21:08:46 ckuethe Exp $ */ /* * Copyright (c) 2002, 2003, 2004 Henning Brauer @@ -53,6 +53,7 @@ int yylex(void); struct opts { int weight; + int correction; } opts; typedef struct { @@ -68,13 +69,14 @@ typedef struct { %} %token LISTEN ON -%token SERVER SERVERS SENSOR WEIGHT +%token SERVER SERVERS SENSOR CORRECTION WEIGHT %token ERROR %token STRING %token NUMBER %type address %type server_opts server_opts_l server_opt %type sensor_opts sensor_opts_l sensor_opt +%type correction %type weight %% @@ -193,6 +195,7 @@ conf_main : LISTEN ON address { s = new_sensor($2); s->weight = $3.weight; + s->correction = $3.correction; free($2); TAILQ_INSERT_TAIL(&conf->ntp_conf_sensors, s, entry); } @@ -232,7 +235,18 @@ sensor_opts : { bzero(&opts, sizeof opts); opts.weight = 1; } sensor_opts_l : sensor_opts_l sensor_opt | sensor_opt ; -sensor_opt : weight +sensor_opt : correction + | weight + ; + +correction : CORRECTION NUMBER { + if ($2 < -127000000 || $2 > 127000000) { + yyerror("correction must be between " + "-127000000 and 127000000 microseconds"); + YYERROR; + } + opts.correction = $2; + } ; weight : WEIGHT NUMBER { @@ -278,6 +292,7 @@ lookup(char *s) { /* this has to be sorted always */ static const struct keywords keywords[] = { + { "correction", CORRECTION}, { "listen", LISTEN}, { "on", ON}, { "sensor", SENSOR}, diff --git a/src/usr.sbin/ntpd/sensors.c b/src/usr.sbin/ntpd/sensors.c index dde110f8..0e0c69f6 100644 --- a/src/usr.sbin/ntpd/sensors.c +++ b/src/usr.sbin/ntpd/sensors.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sensors.c,v 1.33 2007/08/04 02:58:02 ckuethe Exp $ */ +/* $OpenBSD: sensors.c,v 1.34 2007/09/12 21:08:46 ckuethe Exp $ */ /* * Copyright (c) 2006 Henning Brauer @@ -121,13 +121,15 @@ sensor_add(int sensordev, char *dxname) s->next = getmonotime(); s->weight = cs->weight; + s->correction = cs->correction; if ((s->device = strdup(dxname)) == NULL) fatal("sensor_add strdup"); s->sensordevid = sensordev; TAILQ_INSERT_TAIL(&conf->ntp_sensors, s, entry); - log_debug("sensor %s added", s->device); + log_debug("sensor %s added (weight %d, correction %.6f)", + s->device, s->weight, s->correction / 1e6); } void @@ -176,7 +178,8 @@ sensor_query(struct ntp_sensor *s) * sensor.value = TS - TD in ns * if value is positive, system time is ahead */ - s->offsets[s->shift].offset = (sensor.value / -1e9) - getoffset(); + s->offsets[s->shift].offset = (sensor.value / -1e9) - getoffset() + + (s->correction / 1e6); s->offsets[s->shift].rcvd = sensor.tv.tv_sec; s->offsets[s->shift].good = 1;