Add new servo for NTP SHM reference clock.

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 <mlichvar@redhat.com>
master
Miroslav Lichvar 2014-06-11 18:07:08 +02:00 committed by Richard Cochran
parent f28a3b3664
commit 9710fb1a22
11 changed files with 198 additions and 5 deletions

View File

@ -284,6 +284,7 @@ static enum parser_result parse_global_setting(const char *option,
if (r != PARSED_OK) if (r != PARSED_OK)
return r; return r;
dds->domainNumber = uval; dds->domainNumber = uval;
*cfg->ntpshm_segment = uval;
} else if (!strcmp(option, "clockClass")) { } else if (!strcmp(option, "clockClass")) {
r = get_ranged_uint(value, &uval, 0, UINT8_MAX); 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; cfg->clock_servo = CLOCK_SERVO_PI;
else if (!strcasecmp("linreg", value)) else if (!strcasecmp("linreg", value))
cfg->clock_servo = CLOCK_SERVO_LINREG; cfg->clock_servo = CLOCK_SERVO_LINREG;
else if (!strcasecmp("ntpshm", value))
cfg->clock_servo = CLOCK_SERVO_NTPSHM;
else else
return BAD_VALUE; return BAD_VALUE;

View File

@ -89,6 +89,7 @@ struct config {
double *pi_integral_scale; double *pi_integral_scale;
double *pi_integral_exponent; double *pi_integral_exponent;
double *pi_integral_norm_max; double *pi_integral_norm_max;
int *ntpshm_segment;
unsigned char *ptp_dst_mac; unsigned char *ptp_dst_mac;
unsigned char *p2p_dst_mac; unsigned char *p2p_dst_mac;

View File

@ -24,8 +24,9 @@ CFLAGS = -Wall $(VER) $(incdefs) $(DEBUG) $(EXTRA_CFLAGS)
LDLIBS = -lm -lrt $(EXTRA_LDFLAGS) LDLIBS = -lm -lrt $(EXTRA_LDFLAGS)
PRG = ptp4l pmc phc2sys hwstamp_ctl PRG = ptp4l pmc phc2sys hwstamp_ctl
OBJ = bmc.o clock.o clockadj.o clockcheck.o config.o fault.o \ 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 \ filter.o fsm.o linreg.o mave.o mmedian.o msg.o ntpshm.o phc.o \
servo.o sk.o stats.o tlv.o transport.o udp.o udp6.o uds.o util.o version.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 OBJECTS = $(OBJ) hwstamp_ctl.o phc2sys.o pmc.o pmc_common.o sysoff.o
SRC = $(OBJECTS:.o=.c) 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 \ 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 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 \ 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 transport.o udp.o udp6.o uds.o util.o version.o

142
ntpshm.c 100644
View File

@ -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 <mlichvar@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;
}

31
ntpshm.h 100644
View File

@ -0,0 +1,31 @@
/**
* @file ntpshm.h
* @note Copyright (C) 2014 Miroslav Lichvar <mlichvar@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

View File

@ -114,7 +114,9 @@ option.
.TP .TP
.BI \-E " servo" .BI \-E " servo"
Specify which clock servo should be used. Valid values are pi for a PI 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. The default is pi.
.TP .TP
.BI \-P " kp" .BI \-P " kp"

View File

@ -43,6 +43,7 @@
#include "fsm.h" #include "fsm.h"
#include "missing.h" #include "missing.h"
#include "notification.h" #include "notification.h"
#include "ntpshm.h"
#include "phc.h" #include "phc.h"
#include "pi.h" #include "pi.h"
#include "pmc_common.h" #include "pmc_common.h"
@ -1216,6 +1217,8 @@ int main(int argc, char *argv[])
node.servo_type = CLOCK_SERVO_PI; node.servo_type = CLOCK_SERVO_PI;
} else if (!strcasecmp(optarg, "linreg")) { } else if (!strcasecmp(optarg, "linreg")) {
node.servo_type = CLOCK_SERVO_LINREG; node.servo_type = CLOCK_SERVO_LINREG;
} else if (!strcasecmp(optarg, "ntpshm")) {
node.servo_type = CLOCK_SERVO_NTPSHM;
} else { } else {
fprintf(stderr, fprintf(stderr,
"invalid servo name %s\n", optarg); "invalid servo name %s\n", optarg);
@ -1272,6 +1275,7 @@ int main(int argc, char *argv[])
case 'n': case 'n':
if (get_arg_val_i(c, optarg, &domain_number, 0, 255)) if (get_arg_val_i(c, optarg, &domain_number, 0, 255))
return -1; return -1;
ntpshm_segment = domain_number;
break; break;
case 'x': case 'x':
node.kernel_leap = 0; node.kernel_leap = 0;

View File

@ -289,7 +289,9 @@ The default is 0 (disabled).
.TP .TP
.B clock_servo .B clock_servo
The servo which is used to synchronize the local clock. Valid values are pi for 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. The default is pi.
.TP .TP
.B pi_proportional_const .B pi_proportional_const

View File

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

View File

@ -19,6 +19,7 @@
#include <string.h> #include <string.h>
#include "linreg.h" #include "linreg.h"
#include "ntpshm.h"
#include "pi.h" #include "pi.h"
#include "servo_private.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: case CLOCK_SERVO_LINREG:
servo = linreg_servo_create(fadj); servo = linreg_servo_create(fadj);
break; break;
case CLOCK_SERVO_NTPSHM:
servo = ntpshm_servo_create();
break;
default: default:
return NULL; return NULL;
} }

View File

@ -55,6 +55,7 @@ struct servo;
enum servo_type { enum servo_type {
CLOCK_SERVO_PI, CLOCK_SERVO_PI,
CLOCK_SERVO_LINREG, CLOCK_SERVO_LINREG,
CLOCK_SERVO_NTPSHM,
}; };
/** /**