Discussion:
[Linuxptp-devel] [PATCH RFC] Add new servo for NTP SHM reference clock.
Miroslav Lichvar
2014-06-11 16:07:08 UTC
Permalink
This is a simple servo that provides the NTP SHM reference clock. It
doesn't make any clock adjustments and it always returns with the
unlocked state. It writes all samples to the SHM segment and another
process (e.g. chronyd or ntpd) is needed to read the samples and
actually synchronize the clock. The SHM segment number is set to the PTP
domain number to allow multiple SHM reference clocks running at the same
time.

This is mainly useful when other time sources are available on the
system (e.g. NTP, hardware reference clocks, or other PTP domains)
and a fallback to/from PTP is needed.

Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
config.c | 3 ++
config.h | 1 +
makefile | 7 ++--
ntpshm.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ntpshm.h | 31 ++++++++++++++
phc2sys.8 | 4 +-
phc2sys.c | 4 ++
ptp4l.8 | 4 +-
ptp4l.c | 2 +
servo.c | 4 ++
servo.h | 1 +
11 files changed, 198 insertions(+), 5 deletions(-)
create mode 100644 ntpshm.c
create mode 100644 ntpshm.h

diff --git a/config.c b/config.c
index e0b786b..0bc85c1 100644
--- a/config.c
+++ b/config.c
@@ -284,6 +284,7 @@ static enum parser_result parse_global_setting(const char *option,
if (r != PARSED_OK)
return r;
dds->domainNumber = uval;
+ *cfg->ntpshm_segment = uval;

} else if (!strcmp(option, "clockClass")) {
r = get_ranged_uint(value, &uval, 0, UINT8_MAX);
@@ -496,6 +497,8 @@ static enum parser_result parse_global_setting(const char *option,
cfg->clock_servo = CLOCK_SERVO_PI;
else if (!strcasecmp("linreg", value))
cfg->clock_servo = CLOCK_SERVO_LINREG;
+ else if (!strcasecmp("ntpshm", value))
+ cfg->clock_servo = CLOCK_SERVO_NTPSHM;
else
return BAD_VALUE;

diff --git a/config.h b/config.h
index a0b562a..be5a651 100644
--- a/config.h
+++ b/config.h
@@ -89,6 +89,7 @@ struct config {
double *pi_integral_scale;
double *pi_integral_exponent;
double *pi_integral_norm_max;
+ int *ntpshm_segment;

unsigned char *ptp_dst_mac;
unsigned char *p2p_dst_mac;
diff --git a/makefile b/makefile
index eff5109..e36835b 100644
--- a/makefile
+++ b/makefile
@@ -24,8 +24,9 @@ CFLAGS = -Wall $(VER) $(incdefs) $(DEBUG) $(EXTRA_CFLAGS)
LDLIBS = -lm -lrt $(EXTRA_LDFLAGS)
PRG = ptp4l pmc phc2sys hwstamp_ctl
OBJ = bmc.o clock.o clockadj.o clockcheck.o config.o fault.o \
- filter.o fsm.o linreg.o mave.o mmedian.o msg.o phc.o pi.o port.o print.o ptp4l.o raw.o \
- servo.o sk.o stats.o tlv.o transport.o udp.o udp6.o uds.o util.o version.o
+ filter.o fsm.o linreg.o mave.o mmedian.o msg.o ntpshm.o phc.o \
+ pi.o port.o print.o ptp4l.o raw.o servo.o sk.o stats.o tlv.o \
+ transport.o udp.o udp6.o uds.o util.o version.o

OBJECTS = $(OBJ) hwstamp_ctl.o phc2sys.o pmc.o pmc_common.o sysoff.o
SRC = $(OBJECTS:.o=.c)
@@ -47,7 +48,7 @@ ptp4l: $(OBJ)
pmc: msg.o pmc.o pmc_common.o print.o raw.o sk.o tlv.o transport.o udp.o \
udp6.o uds.o util.o version.o

-phc2sys: clockadj.o clockcheck.o linreg.o msg.o phc.o phc2sys.o pi.o \
+phc2sys: clockadj.o clockcheck.o linreg.o msg.o ntpshm.o phc.o phc2sys.o pi.o \
pmc_common.o print.o raw.o servo.o sk.o stats.o sysoff.o tlv.o \
transport.o udp.o udp6.o uds.o util.o version.o

diff --git a/ntpshm.c b/ntpshm.c
new file mode 100644
index 0000000..b6280a5
--- /dev/null
+++ b/ntpshm.c
@@ -0,0 +1,142 @@
+/**
+ * @file ntpshm.c
+ * @brief Implements a servo providing the NTP SHM reference clock to
+ * send the samples to another process.
+ * @note Copyright (C) 2014 Miroslav Lichvar <***@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/shm.h>
+
+#include "print.h"
+#include "ntpshm.h"
+#include "servo_private.h"
+
+#define NS_PER_SEC 1000000000
+
+/* Key of the first SHM segment */
+#define SHMKEY 0x4e545030
+
+/* Number of the SHM segment to be used */
+int ntpshm_segment = 0;
+
+/* Declaration of the SHM segment from ntp (ntpd/refclock_shm.c) */
+struct shmTime {
+ int mode; /* 0 - if valid set
+ * use values,
+ * clear valid
+ * 1 - if valid set
+ * if count before and after read of values is equal,
+ * use values
+ * clear valid
+ */
+ volatile int count;
+ time_t clockTimeStampSec;
+ int clockTimeStampUSec;
+ time_t receiveTimeStampSec;
+ int receiveTimeStampUSec;
+ int leap;
+ int precision;
+ int nsamples;
+ volatile int valid;
+ int clockTimeStampNSec;
+ int receiveTimeStampNSec;
+ int dummy[8];
+};
+
+struct ntpshm_servo {
+ struct servo servo;
+ struct shmTime *shm;
+};
+
+static void ntpshm_destroy(struct servo *servo)
+{
+ struct ntpshm_servo *s = container_of(servo, struct ntpshm_servo, servo);
+
+ shmdt(s->shm);
+ free(s);
+}
+
+static double ntpshm_sample(struct servo *servo,
+ int64_t offset,
+ uint64_t local_ts,
+ enum servo_state *state)
+{
+ struct ntpshm_servo *s = container_of(servo, struct ntpshm_servo, servo);
+ uint64_t clock_ts = local_ts - offset;
+
+ s->shm->mode = 1;
+ s->shm->count++;
+ s->shm->valid = 0;
+ /* TODO: write memory barrier */
+
+ s->shm->clockTimeStampSec = clock_ts / NS_PER_SEC;
+ s->shm->clockTimeStampNSec = clock_ts % NS_PER_SEC;
+ s->shm->clockTimeStampUSec = s->shm->clockTimeStampNSec / 1000;
+ s->shm->receiveTimeStampSec = local_ts / NS_PER_SEC;
+ s->shm->receiveTimeStampNSec = local_ts % NS_PER_SEC;
+ s->shm->receiveTimeStampUSec = s->shm->receiveTimeStampNSec / 1000;
+ s->shm->precision = -30; /* 1 nanosecond */
+ s->shm->leap = 0;
+ /* TODO: write memory barrier */
+
+ s->shm->count++;
+ s->shm->valid = 1;
+
+ *state = SERVO_UNLOCKED;
+ return 0.0;
+}
+
+static void ntpshm_sync_interval(struct servo *servo, double interval)
+{
+}
+
+static void ntpshm_reset(struct servo *servo)
+{
+}
+
+struct servo *ntpshm_servo_create(void)
+{
+ struct ntpshm_servo *s;
+ int shmid;
+
+ s = calloc(1, sizeof(*s));
+ if (!s)
+ return NULL;
+
+ s->servo.destroy = ntpshm_destroy;
+ s->servo.sample = ntpshm_sample;
+ s->servo.sync_interval = ntpshm_sync_interval;
+ s->servo.reset = ntpshm_reset;
+
+ shmid = shmget(SHMKEY + ntpshm_segment, sizeof (struct shmTime),
+ IPC_CREAT | 0600);
+ if (shmid == -1) {
+ pr_err("ntpshm: shmget failed: %m");
+ free(s);
+ return NULL;
+ }
+
+ s->shm = (struct shmTime *)shmat(shmid, 0, 0);
+ if (s->shm == (void *)-1) {
+ pr_err("ntpshm: shmat failed: %m");
+ free(s);
+ return NULL;
+ }
+
+ return &s->servo;
+}
diff --git a/ntpshm.h b/ntpshm.h
new file mode 100644
index 0000000..ea54a54
--- /dev/null
+++ b/ntpshm.h
@@ -0,0 +1,31 @@
+/**
+ * @file ntpshm.h
+ * @note Copyright (C) 2014 Miroslav Lichvar <***@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef HAVE_NTPSHM_H
+#define HAVE_NTPSHM_H
+
+#include "servo.h"
+
+/**
+ * The number of the SHM segment that will be used by newly created servo
+ */
+extern int ntpshm_segment;
+
+struct servo *ntpshm_servo_create(void);
+
+#endif
diff --git a/phc2sys.8 b/phc2sys.8
index 37a39d0..a906fb3 100644
--- a/phc2sys.8
+++ b/phc2sys.8
@@ -114,7 +114,9 @@ option.
.TP
.BI \-E " servo"
Specify which clock servo should be used. Valid values are pi for a PI
-controller and linreg for an adaptive controller using linear regression.
+controller, linreg for an adaptive controller using linear regression, and
+ntpshm for the NTP SHM reference clock to allow another process to synchronize
+the local clock (the SHM segment number is set to the domain number).
The default is pi.
.TP
.BI \-P " kp"
diff --git a/phc2sys.c b/phc2sys.c
index 3610f2a..9fd4136 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -43,6 +43,7 @@
#include "fsm.h"
#include "missing.h"
#include "notification.h"
+#include "ntpshm.h"
#include "phc.h"
#include "pi.h"
#include "pmc_common.h"
@@ -1213,6 +1214,8 @@ int main(int argc, char *argv[])
node.servo_type = CLOCK_SERVO_PI;
} else if (!strcasecmp(optarg, "linreg")) {
node.servo_type = CLOCK_SERVO_LINREG;
+ } else if (!strcasecmp(optarg, "ntpshm")) {
+ node.servo_type = CLOCK_SERVO_NTPSHM;
} else {
fprintf(stderr,
"invalid servo name %s\n", optarg);
@@ -1269,6 +1272,7 @@ int main(int argc, char *argv[])
case 'n':
if (get_arg_val_i(c, optarg, &domain_number, 0, 255))
return -1;
+ ntpshm_segment = domain_number;
break;
case 'x':
node.kernel_leap = 0;
diff --git a/ptp4l.8 b/ptp4l.8
index 0f06ee1..1bae78c 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -289,7 +289,9 @@ The default is 0 (disabled).
.TP
.B clock_servo
The servo which is used to synchronize the local clock. Valid values are pi for
-a PI controller and linreg for an adaptive controller using linear regression.
+a PI controller, linreg for an adaptive controller using linear regression, and
+ntpshm for the NTP SHM reference clock to allow another process to synchronize
+the local clock (the SHM segment number is set to the domain number).
The default is pi.
.TP
.B pi_proportional_const
diff --git a/ptp4l.c b/ptp4l.c
index bbf558d..a33900f 100644
--- a/ptp4l.c
+++ b/ptp4l.c
@@ -27,6 +27,7 @@

#include "clock.h"
#include "config.h"
+#include "ntpshm.h"
#include "pi.h"
#include "print.h"
#include "raw.h"
@@ -116,6 +117,7 @@ static struct config cfg_settings = {
.pi_integral_scale = &configured_pi_ki_scale,
.pi_integral_exponent = &configured_pi_ki_exponent,
.pi_integral_norm_max = &configured_pi_ki_norm_max,
+ .ntpshm_segment = &ntpshm_segment,

.ptp_dst_mac = ptp_dst_mac,
.p2p_dst_mac = p2p_dst_mac,
diff --git a/servo.c b/servo.c
index 6986e41..58bd4eb 100644
--- a/servo.c
+++ b/servo.c
@@ -19,6 +19,7 @@
#include <string.h>

#include "linreg.h"
+#include "ntpshm.h"
#include "pi.h"
#include "servo_private.h"

@@ -39,6 +40,9 @@ struct servo *servo_create(enum servo_type type, int fadj, int max_ppb, int sw_t
case CLOCK_SERVO_LINREG:
servo = linreg_servo_create(fadj);
break;
+ case CLOCK_SERVO_NTPSHM:
+ servo = ntpshm_servo_create();
+ break;
default:
return NULL;
}
diff --git a/servo.h b/servo.h
index 1536c9e..9cec6f4 100644
--- a/servo.h
+++ b/servo.h
@@ -55,6 +55,7 @@ struct servo;
enum servo_type {
CLOCK_SERVO_PI,
CLOCK_SERVO_LINREG,
+ CLOCK_SERVO_NTPSHM,
};

/**
--
1.9.3
Richard Cochran
2014-06-12 09:44:42 UTC
Permalink
Post by Miroslav Lichvar
This is mainly useful when other time sources are available on the
system (e.g. NTP, hardware reference clocks, or other PTP domains)
and a fallback to/from PTP is needed.
So, this would let ntpd mix PTP and NTP sources together?

What about adjusting the PHC?

Rebase? I get:
Applying: Add new servo for NTP SHM reference clock.
error: patch failed: phc2sys.c:43
error: phc2sys.c: patch does not apply
Post by Miroslav Lichvar
+static double ntpshm_sample(struct servo *servo,
+ int64_t offset,
+ uint64_t local_ts,
+ enum servo_state *state)
+{
+ struct ntpshm_servo *s = container_of(servo, struct ntpshm_servo, servo);
+ uint64_t clock_ts = local_ts - offset;
+
+ s->shm->mode = 1;
+ s->shm->count++;
+ s->shm->valid = 0;
+ /* TODO: write memory barrier */
How can this be done in user space?

Thanks,
Richard
Miroslav Lichvar
2014-06-12 10:34:49 UTC
Permalink
Post by Richard Cochran
Post by Miroslav Lichvar
This is mainly useful when other time sources are available on the
system (e.g. NTP, hardware reference clocks, or other PTP domains)
and a fallback to/from PTP is needed.
So, this would let ntpd mix PTP and NTP sources together?
Yes, ntpd (or preferably chronyd to get better accuracy) can compare
the sources, find falsetickers, select the one with best statistics or
combine a number of them together, and use that to synchronize the
clock.
Post by Richard Cochran
What about adjusting the PHC?
It's intended to be used with the system clock, ptp4l with software
time stamping or phc2sys. With PHC it should be possible too, but I'm
not aware of any program that can read SHM and synchronize PHC.
Post by Richard Cochran
Applying: Add new servo for NTP SHM reference clock.
error: patch failed: phc2sys.c:43
error: phc2sys.c: patch does not apply
I was working on branch with the patches from Jiri adding automatic
phc2sys configuration.
Post by Richard Cochran
Post by Miroslav Lichvar
+ s->shm->mode = 1;
+ s->shm->count++;
+ s->shm->valid = 0;
+ /* TODO: write memory barrier */
How can this be done in user space?
In gpsd they use these gcc builtins:

#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
__atomic_thread_fence(__ATOMIC_SEQ_CST);
#elif (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
__sync_synchronize();
#endif

I'm not sure if there is something better. Any suggestions?

I think it will work well even if no barriers are used here. On the
rare event that a sample is read as a mix of the old and new sample,
it will have a large offset and the filters implemented in
ntpd/chronyd should discard it.
--
Miroslav Lichvar
Richard Cochran
2014-06-14 09:26:07 UTC
Permalink
Post by Miroslav Lichvar
I'm not sure if there is something better. Any suggestions?
AFAICT, that is all gcc provides.
Post by Miroslav Lichvar
I think it will work well even if no barriers are used here. On the
rare event that a sample is read as a mix of the old and new sample,
it will have a large offset and the filters implemented in
ntpd/chronyd should discard it.
Okay, then I'll merge this patch if you say that it is ready. It looks
fine to me.

Thanks,
Richard
Miroslav Lichvar
2014-06-17 09:03:54 UTC
Permalink
Post by Richard Cochran
Post by Miroslav Lichvar
I think it will work well even if no barriers are used here. On the
rare event that a sample is read as a mix of the old and new sample,
it will have a large offset and the filters implemented in
ntpd/chronyd should discard it.
Okay, then I'll merge this patch if you say that it is ready. It looks
fine to me.
I think it's usable as it is and we can improve it later if that's ok
with you.

One thing that would be nice to have is forwarding leap seconds. We'll
probably need to add a new call to the servo interface and use it when
the kernel_leap option is disabled and a leap second is pending. This
could be useful also with the linreg servo to slew or step the leap
second gracefully instead of handling it as a sudden jump in the
offset and disturbing the estimated frequency and the statistics.

Thanks,
--
Miroslav Lichvar
Richard Cochran
2014-06-17 11:40:46 UTC
Permalink
Post by Miroslav Lichvar
I think it's usable as it is and we can improve it later if that's ok
with you.
Okay, applied.

Thanks,
Richard
Keller, Jacob E
2014-06-17 20:02:44 UTC
Permalink
Post by Miroslav Lichvar
Post by Richard Cochran
Post by Miroslav Lichvar
I think it will work well even if no barriers are used here. On the
rare event that a sample is read as a mix of the old and new sample,
it will have a large offset and the filters implemented in
ntpd/chronyd should discard it.
Okay, then I'll merge this patch if you say that it is ready. It looks
fine to me.
I think it's usable as it is and we can improve it later if that's ok
with you.
One thing that would be nice to have is forwarding leap seconds. We'll
probably need to add a new call to the servo interface and use it when
the kernel_leap option is disabled and a leap second is pending. This
could be useful also with the linreg servo to slew or step the leap
second gracefully instead of handling it as a sudden jump in the
offset and disturbing the estimated frequency and the statistics.
I like the sound of this second part. :)
Post by Miroslav Lichvar
Thanks,
Thanks,
J

Loading...