2012-01-25 02:12:57 +08:00
|
|
|
/**
|
|
|
|
* @file phc2sys.c
|
|
|
|
* @brief Utility program to synchronize two clocks via a PPS.
|
|
|
|
* @note Copyright (C) 2012 Richard Cochran <richardcochran@gmail.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 <errno.h>
|
|
|
|
#include <fcntl.h>
|
2013-06-04 13:01:04 +08:00
|
|
|
#include <float.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
2014-06-12 03:35:21 +08:00
|
|
|
#include <net/if.h>
|
2013-01-30 01:06:18 +08:00
|
|
|
#include <poll.h>
|
2012-01-25 02:12:57 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2014-06-12 03:35:13 +08:00
|
|
|
#include <sys/queue.h>
|
2012-01-25 02:12:57 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <linux/pps.h>
|
|
|
|
#include <linux/ptp_clock.h>
|
|
|
|
|
2013-03-08 00:27:29 +08:00
|
|
|
#include "clockadj.h"
|
2013-10-23 00:57:15 +08:00
|
|
|
#include "clockcheck.h"
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "ds.h"
|
|
|
|
#include "fsm.h"
|
2012-01-25 02:12:57 +08:00
|
|
|
#include "missing.h"
|
2014-06-12 03:35:19 +08:00
|
|
|
#include "notification.h"
|
2014-06-12 00:07:08 +08:00
|
|
|
#include "ntpshm.h"
|
2013-04-08 21:44:05 +08:00
|
|
|
#include "phc.h"
|
2013-01-15 00:09:12 +08:00
|
|
|
#include "pi.h"
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "pmc_common.h"
|
|
|
|
#include "print.h"
|
2013-01-15 00:09:12 +08:00
|
|
|
#include "servo.h"
|
2012-09-28 11:42:37 +08:00
|
|
|
#include "sk.h"
|
2013-02-08 02:56:53 +08:00
|
|
|
#include "stats.h"
|
2012-11-26 20:25:35 +08:00
|
|
|
#include "sysoff.h"
|
2013-01-30 01:06:18 +08:00
|
|
|
#include "tlv.h"
|
2014-07-08 22:14:18 +08:00
|
|
|
#include "uds.h"
|
2013-03-08 00:27:30 +08:00
|
|
|
#include "util.h"
|
2012-12-10 17:28:28 +08:00
|
|
|
#include "version.h"
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
#define KP 0.7
|
|
|
|
#define KI 0.3
|
|
|
|
#define NS_PER_SEC 1000000000LL
|
|
|
|
|
2013-01-18 01:31:39 +08:00
|
|
|
#define PHC_PPS_OFFSET_LIMIT 10000000
|
2013-03-08 00:27:30 +08:00
|
|
|
#define PMC_UPDATE_INTERVAL (60 * NS_PER_SEC)
|
2014-06-12 03:35:19 +08:00
|
|
|
#define PMC_SUBSCRIBE_DURATION 180 /* 3 minutes */
|
|
|
|
/* Note that PMC_SUBSCRIBE_DURATION has to be longer than
|
|
|
|
* PMC_UPDATE_INTERVAL otherwise subscription will time out before it is
|
|
|
|
* renewed.
|
|
|
|
*/
|
2013-01-18 01:31:39 +08:00
|
|
|
|
2014-06-12 03:35:15 +08:00
|
|
|
struct clock {
|
|
|
|
LIST_ENTRY(clock) list;
|
|
|
|
clockid_t clkid;
|
2017-03-27 23:43:06 +08:00
|
|
|
int phc_index;
|
2014-06-12 03:35:15 +08:00
|
|
|
int sysoff_supported;
|
|
|
|
int is_utc;
|
2014-06-12 03:35:22 +08:00
|
|
|
int dest_only;
|
2014-06-12 03:35:19 +08:00
|
|
|
int state;
|
|
|
|
int new_state;
|
2014-06-18 21:44:48 +08:00
|
|
|
int sync_offset;
|
|
|
|
int leap_set;
|
2014-06-18 21:44:49 +08:00
|
|
|
int utc_offset_set;
|
2014-06-12 03:35:15 +08:00
|
|
|
struct servo *servo;
|
|
|
|
enum servo_state servo_state;
|
2014-06-12 03:35:16 +08:00
|
|
|
char *device;
|
2014-06-12 03:35:15 +08:00
|
|
|
const char *source_label;
|
|
|
|
struct stats *offset_stats;
|
|
|
|
struct stats *freq_stats;
|
|
|
|
struct stats *delay_stats;
|
|
|
|
struct clockcheck *sanity_check;
|
|
|
|
};
|
|
|
|
|
2014-06-12 03:35:17 +08:00
|
|
|
struct port {
|
|
|
|
LIST_ENTRY(port) list;
|
|
|
|
unsigned int number;
|
2014-06-12 03:35:19 +08:00
|
|
|
int state;
|
2014-06-12 03:35:17 +08:00
|
|
|
struct clock *clock;
|
|
|
|
};
|
|
|
|
|
2014-06-12 03:35:15 +08:00
|
|
|
struct node {
|
|
|
|
unsigned int stats_max_count;
|
|
|
|
int sanity_freq_limit;
|
|
|
|
enum servo_type servo_type;
|
|
|
|
int phc_readings;
|
|
|
|
double phc_interval;
|
|
|
|
int sync_offset;
|
|
|
|
int forced_sync_offset;
|
2014-06-18 21:44:49 +08:00
|
|
|
int utc_offset_traceable;
|
2014-06-12 03:35:15 +08:00
|
|
|
int leap;
|
|
|
|
int kernel_leap;
|
|
|
|
struct pmc *pmc;
|
|
|
|
int pmc_ds_requested;
|
|
|
|
uint64_t pmc_last_update;
|
2014-06-12 03:35:19 +08:00
|
|
|
int state_changed;
|
2014-06-12 03:35:23 +08:00
|
|
|
int clock_identity_set;
|
|
|
|
struct ClockIdentity clock_identity;
|
2014-06-12 03:35:17 +08:00
|
|
|
LIST_HEAD(port_head, port) ports;
|
2014-06-12 03:35:15 +08:00
|
|
|
LIST_HEAD(clock_head, clock) clocks;
|
|
|
|
struct clock *master;
|
|
|
|
};
|
|
|
|
|
2015-08-23 19:59:38 +08:00
|
|
|
static struct config *phc2sys_config;
|
2015-08-11 02:29:51 +08:00
|
|
|
|
2014-06-12 03:35:21 +08:00
|
|
|
static int update_pmc(struct node *node, int subscribe);
|
2014-06-12 03:35:15 +08:00
|
|
|
static int clock_handle_leap(struct node *node, struct clock *clock,
|
2014-06-18 21:44:48 +08:00
|
|
|
int64_t offset, uint64_t ts);
|
2014-06-12 03:35:21 +08:00
|
|
|
static int run_pmc_get_utc_offset(struct node *node, int timeout);
|
|
|
|
static void run_pmc_events(struct node *node);
|
2013-03-08 00:27:28 +08:00
|
|
|
|
2017-10-09 22:31:49 +08:00
|
|
|
static int normalize_state(int state);
|
|
|
|
static int run_pmc_port_properties(struct node *node, int timeout,
|
|
|
|
unsigned int port,
|
|
|
|
int *state, int *tstamping, char *iface);
|
|
|
|
|
2017-03-27 23:43:06 +08:00
|
|
|
static clockid_t clock_open(char *device, int *phc_index)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
2013-05-15 06:12:11 +08:00
|
|
|
struct sk_ts_info ts_info;
|
|
|
|
char phc_device[16];
|
2013-04-08 21:44:07 +08:00
|
|
|
int clkid;
|
2012-11-01 22:15:35 +08:00
|
|
|
|
2013-05-15 06:12:11 +08:00
|
|
|
/* check if device is CLOCK_REALTIME */
|
|
|
|
if (!strcasecmp(device, "CLOCK_REALTIME"))
|
|
|
|
return CLOCK_REALTIME;
|
2012-11-01 22:15:35 +08:00
|
|
|
|
2013-05-15 06:12:11 +08:00
|
|
|
/* check if device is valid phc device */
|
|
|
|
clkid = phc_open(device);
|
|
|
|
if (clkid != CLOCK_INVALID)
|
|
|
|
return clkid;
|
|
|
|
|
|
|
|
/* check if device is a valid ethernet device */
|
|
|
|
if (sk_get_ts_info(device, &ts_info) || !ts_info.valid) {
|
|
|
|
fprintf(stderr, "unknown clock %s: %m\n", device);
|
2012-11-01 22:15:35 +08:00
|
|
|
return CLOCK_INVALID;
|
|
|
|
}
|
|
|
|
|
2013-05-15 06:12:11 +08:00
|
|
|
if (ts_info.phc_index < 0) {
|
|
|
|
fprintf(stderr, "interface %s does not have a PHC\n", device);
|
|
|
|
return CLOCK_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(phc_device, "/dev/ptp%d", ts_info.phc_index);
|
|
|
|
clkid = phc_open(phc_device);
|
2013-04-08 21:44:07 +08:00
|
|
|
if (clkid == CLOCK_INVALID)
|
2012-01-25 02:12:57 +08:00
|
|
|
fprintf(stderr, "cannot open %s: %m\n", device);
|
2017-03-27 23:43:06 +08:00
|
|
|
*phc_index = ts_info.phc_index;
|
2013-04-08 21:44:07 +08:00
|
|
|
return clkid;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2017-10-09 22:31:48 +08:00
|
|
|
static struct servo *servo_add(struct node *node, struct clock *clock)
|
|
|
|
{
|
|
|
|
double ppb;
|
|
|
|
int max_ppb;
|
|
|
|
struct servo *servo;
|
|
|
|
|
|
|
|
clockadj_init(clock->clkid);
|
|
|
|
ppb = clockadj_get_freq(clock->clkid);
|
|
|
|
/* The reading may silently fail and return 0, reset the frequency to
|
|
|
|
make sure ppb is the actual frequency of the clock. */
|
|
|
|
clockadj_set_freq(clock->clkid, ppb);
|
|
|
|
if (clock->clkid == CLOCK_REALTIME) {
|
|
|
|
sysclk_set_leap(0);
|
|
|
|
max_ppb = sysclk_max_freq();
|
|
|
|
} else {
|
|
|
|
max_ppb = phc_max_adj(clock->clkid);
|
|
|
|
if (!max_ppb) {
|
|
|
|
pr_err("clock is not adjustable");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
servo = servo_create(phc2sys_config, node->servo_type,
|
|
|
|
-ppb, max_ppb, 0);
|
|
|
|
if (!servo) {
|
|
|
|
pr_err("Failed to create servo");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
servo_sync_interval(servo, node->phc_interval);
|
|
|
|
|
|
|
|
return servo;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:16 +08:00
|
|
|
static struct clock *clock_add(struct node *node, char *device)
|
2014-06-12 03:35:15 +08:00
|
|
|
{
|
|
|
|
struct clock *c;
|
2014-06-12 03:35:16 +08:00
|
|
|
clockid_t clkid = CLOCK_INVALID;
|
2017-10-09 22:31:48 +08:00
|
|
|
int phc_index = -1;
|
2014-06-12 03:35:15 +08:00
|
|
|
|
2014-06-12 03:35:16 +08:00
|
|
|
if (device) {
|
2017-03-27 23:43:06 +08:00
|
|
|
clkid = clock_open(device, &phc_index);
|
2014-06-12 03:35:16 +08:00
|
|
|
if (clkid == CLOCK_INVALID)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:15 +08:00
|
|
|
c = calloc(1, sizeof(*c));
|
|
|
|
if (!c) {
|
|
|
|
pr_err("failed to allocate memory for a clock");
|
2014-06-12 03:35:16 +08:00
|
|
|
return NULL;
|
2014-06-12 03:35:15 +08:00
|
|
|
}
|
|
|
|
c->clkid = clkid;
|
2017-03-27 23:43:06 +08:00
|
|
|
c->phc_index = phc_index;
|
2014-06-12 03:35:15 +08:00
|
|
|
c->servo_state = SERVO_UNLOCKED;
|
2014-06-12 03:35:16 +08:00
|
|
|
c->device = strdup(device);
|
2014-06-12 03:35:15 +08:00
|
|
|
|
|
|
|
if (c->clkid == CLOCK_REALTIME) {
|
|
|
|
c->source_label = "sys";
|
|
|
|
c->is_utc = 1;
|
|
|
|
} else {
|
|
|
|
c->source_label = "phc";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->stats_max_count > 0) {
|
|
|
|
c->offset_stats = stats_create();
|
|
|
|
c->freq_stats = stats_create();
|
|
|
|
c->delay_stats = stats_create();
|
|
|
|
if (!c->offset_stats ||
|
|
|
|
!c->freq_stats ||
|
|
|
|
!c->delay_stats) {
|
|
|
|
pr_err("failed to create stats");
|
2014-06-12 03:35:16 +08:00
|
|
|
return NULL;
|
2014-06-12 03:35:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node->sanity_freq_limit) {
|
|
|
|
c->sanity_check = clockcheck_create(node->sanity_freq_limit);
|
|
|
|
if (!c->sanity_check) {
|
|
|
|
pr_err("failed to create clock check");
|
2014-06-12 03:35:16 +08:00
|
|
|
return NULL;
|
2014-06-12 03:35:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:31:48 +08:00
|
|
|
c->servo = servo_add(node, c);
|
2014-06-12 03:35:15 +08:00
|
|
|
|
|
|
|
if (clkid != CLOCK_REALTIME)
|
|
|
|
c->sysoff_supported = (SYSOFF_SUPPORTED ==
|
|
|
|
sysoff_probe(CLOCKID_TO_FD(clkid),
|
|
|
|
node->phc_readings));
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&node->clocks, c, list);
|
2014-06-12 03:35:16 +08:00
|
|
|
return c;
|
2014-06-12 03:35:15 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 11:22:45 +08:00
|
|
|
static void clock_cleanup(struct node *node)
|
|
|
|
{
|
2018-04-01 02:32:55 +08:00
|
|
|
struct clock *c, *tmp;
|
2017-06-23 11:22:45 +08:00
|
|
|
|
2018-04-01 02:32:55 +08:00
|
|
|
LIST_FOREACH_SAFE(c, &node->clocks, list, tmp) {
|
|
|
|
if (c->servo) {
|
|
|
|
servo_destroy(c->servo);
|
|
|
|
}
|
|
|
|
if (c->sanity_check) {
|
|
|
|
clockcheck_destroy(c->sanity_check);
|
|
|
|
}
|
|
|
|
if (c->delay_stats) {
|
|
|
|
stats_destroy(c->delay_stats);
|
|
|
|
}
|
|
|
|
if (c->freq_stats) {
|
|
|
|
stats_destroy(c->freq_stats);
|
|
|
|
}
|
|
|
|
if (c->offset_stats) {
|
|
|
|
stats_destroy(c->offset_stats);
|
|
|
|
}
|
|
|
|
if (c->device) {
|
2017-06-23 11:22:45 +08:00
|
|
|
free(c->device);
|
2018-04-01 02:32:55 +08:00
|
|
|
}
|
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void port_cleanup(struct node *node)
|
|
|
|
{
|
|
|
|
struct port *p, *tmp;
|
|
|
|
|
|
|
|
LIST_FOREACH_SAFE(p, &node->ports, list, tmp) {
|
|
|
|
free(p);
|
2017-06-23 11:22:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:17 +08:00
|
|
|
static struct port *port_get(struct node *node, unsigned int number)
|
|
|
|
{
|
|
|
|
struct port *p;
|
|
|
|
|
|
|
|
LIST_FOREACH(p, &node->ports, list) {
|
|
|
|
if (p->number == number)
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct port *port_add(struct node *node, unsigned int number,
|
|
|
|
char *device)
|
|
|
|
{
|
|
|
|
struct port *p;
|
|
|
|
struct clock *c = NULL, *tmp;
|
|
|
|
|
|
|
|
p = port_get(node, number);
|
|
|
|
if (p)
|
|
|
|
return p;
|
|
|
|
/* port is a new one, look whether we have the device already on
|
|
|
|
* a different port */
|
|
|
|
LIST_FOREACH(tmp, &node->clocks, list) {
|
|
|
|
if (!strcmp(tmp->device, device)) {
|
|
|
|
c = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!c) {
|
|
|
|
c = clock_add(node, device);
|
|
|
|
if (!c)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = malloc(sizeof(*p));
|
|
|
|
if (!p) {
|
|
|
|
pr_err("failed to allocate memory for a port");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p->number = number;
|
|
|
|
p->clock = c;
|
|
|
|
LIST_INSERT_HEAD(&node->ports, p, list);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:31:49 +08:00
|
|
|
static void clock_reinit(struct node *node, struct clock *clock, int new_state)
|
2014-06-12 03:35:21 +08:00
|
|
|
{
|
2017-10-09 22:31:49 +08:00
|
|
|
int phc_index = -1, phc_switched = 0;
|
|
|
|
int state, timestamping, ret = -1;
|
|
|
|
struct port *p;
|
|
|
|
struct servo *servo;
|
|
|
|
struct sk_ts_info ts_info;
|
|
|
|
char iface[IFNAMSIZ];
|
|
|
|
clockid_t clkid = CLOCK_INVALID;
|
|
|
|
|
|
|
|
LIST_FOREACH(p, &node->ports, list) {
|
|
|
|
if (p->clock == clock) {
|
|
|
|
ret = run_pmc_port_properties(node, 1000, p->number,
|
|
|
|
&state, ×tamping,
|
|
|
|
iface);
|
|
|
|
if (ret > 0)
|
|
|
|
p->state = normalize_state(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret > 0 && timestamping != TS_SOFTWARE) {
|
|
|
|
/* Check if device changed */
|
|
|
|
if (strcmp(clock->device, iface)) {
|
|
|
|
free(clock->device);
|
|
|
|
clock->device = strdup(iface);
|
|
|
|
}
|
|
|
|
/* Check if phc index changed */
|
|
|
|
if (!sk_get_ts_info(clock->device, &ts_info) &&
|
|
|
|
clock->phc_index != ts_info.phc_index) {
|
|
|
|
clkid = clock_open(clock->device, &phc_index);
|
|
|
|
if (clkid == CLOCK_INVALID)
|
|
|
|
return;
|
|
|
|
|
|
|
|
phc_close(clock->clkid);
|
|
|
|
clock->clkid = clkid;
|
|
|
|
clock->phc_index = phc_index;
|
|
|
|
|
|
|
|
servo = servo_add(node, clock);
|
|
|
|
if (servo) {
|
|
|
|
servo_destroy(clock->servo);
|
|
|
|
clock->servo = servo;
|
|
|
|
}
|
|
|
|
|
|
|
|
phc_switched = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_state == PS_MASTER || phc_switched) {
|
|
|
|
servo_reset(clock->servo);
|
|
|
|
clock->servo_state = SERVO_UNLOCKED;
|
2014-06-12 03:35:21 +08:00
|
|
|
|
2017-10-09 22:31:49 +08:00
|
|
|
if (clock->offset_stats) {
|
|
|
|
stats_reset(clock->offset_stats);
|
|
|
|
stats_reset(clock->freq_stats);
|
|
|
|
stats_reset(clock->delay_stats);
|
|
|
|
}
|
2014-06-12 03:35:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reconfigure(struct node *node)
|
|
|
|
{
|
2014-11-21 05:18:48 +08:00
|
|
|
struct clock *c, *rt = NULL, *src = NULL, *last = NULL;
|
2014-06-12 03:35:21 +08:00
|
|
|
int src_cnt = 0, dst_cnt = 0;
|
|
|
|
|
|
|
|
pr_info("reconfiguring after port state change");
|
|
|
|
node->state_changed = 0;
|
|
|
|
|
|
|
|
LIST_FOREACH(c, &node->clocks, list) {
|
|
|
|
if (c->clkid == CLOCK_REALTIME) {
|
|
|
|
rt = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-14 01:18:12 +08:00
|
|
|
if (c->new_state) {
|
2017-10-09 22:31:49 +08:00
|
|
|
clock_reinit(node, c, c->new_state);
|
2014-11-14 01:18:12 +08:00
|
|
|
c->state = c->new_state;
|
|
|
|
c->new_state = 0;
|
|
|
|
}
|
2014-06-12 03:35:21 +08:00
|
|
|
|
2014-11-06 05:55:02 +08:00
|
|
|
switch (c->state) {
|
|
|
|
case PS_FAULTY:
|
|
|
|
case PS_DISABLED:
|
|
|
|
case PS_LISTENING:
|
|
|
|
case PS_PRE_MASTER:
|
|
|
|
case PS_MASTER:
|
|
|
|
case PS_PASSIVE:
|
2014-06-12 03:35:21 +08:00
|
|
|
pr_info("selecting %s for synchronization", c->device);
|
|
|
|
dst_cnt++;
|
2014-11-06 05:55:02 +08:00
|
|
|
break;
|
|
|
|
case PS_UNCALIBRATED:
|
|
|
|
src_cnt++;
|
|
|
|
break;
|
|
|
|
case PS_SLAVE:
|
|
|
|
src = c;
|
|
|
|
src_cnt++;
|
|
|
|
break;
|
2014-06-12 03:35:21 +08:00
|
|
|
}
|
2014-11-21 05:18:48 +08:00
|
|
|
last = c;
|
|
|
|
}
|
2015-02-10 23:54:43 +08:00
|
|
|
if (dst_cnt > 1 && !src) {
|
2014-11-21 05:18:48 +08:00
|
|
|
if (!rt || rt->dest_only) {
|
|
|
|
node->master = last;
|
2015-02-10 23:54:43 +08:00
|
|
|
/* Reset to original state in next reconfiguration. */
|
|
|
|
node->master->new_state = node->master->state;
|
|
|
|
node->master->state = PS_SLAVE;
|
|
|
|
if (rt)
|
|
|
|
rt->state = PS_SLAVE;
|
2014-11-21 05:18:48 +08:00
|
|
|
pr_info("no source, selecting %s as the default clock",
|
|
|
|
last->device);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-12 03:35:21 +08:00
|
|
|
}
|
|
|
|
if (src_cnt > 1) {
|
|
|
|
pr_info("multiple master clocks available, postponing sync...");
|
|
|
|
node->master = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (src_cnt > 0 && !src) {
|
|
|
|
pr_info("master clock not ready, waiting...");
|
|
|
|
node->master = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!src_cnt && !dst_cnt) {
|
|
|
|
pr_info("no PHC ready, waiting...");
|
|
|
|
node->master = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2014-06-12 03:35:22 +08:00
|
|
|
if ((!src_cnt && (!rt || rt->dest_only)) ||
|
|
|
|
(!dst_cnt && !rt)) {
|
|
|
|
pr_info("nothing to synchronize");
|
|
|
|
node->master = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2014-06-12 03:35:21 +08:00
|
|
|
if (!src_cnt) {
|
|
|
|
src = rt;
|
|
|
|
rt->state = PS_SLAVE;
|
2014-06-12 03:35:22 +08:00
|
|
|
} else if (rt) {
|
2014-06-12 03:35:21 +08:00
|
|
|
if (rt->state != PS_MASTER) {
|
|
|
|
rt->state = PS_MASTER;
|
2017-10-09 22:31:49 +08:00
|
|
|
clock_reinit(node, rt, rt->state);
|
2014-06-12 03:35:21 +08:00
|
|
|
}
|
|
|
|
pr_info("selecting %s for synchronization", rt->device);
|
|
|
|
}
|
|
|
|
node->master = src;
|
|
|
|
pr_info("selecting %s as the master clock", src->device);
|
|
|
|
}
|
|
|
|
|
2012-09-26 02:03:04 +08:00
|
|
|
static int read_phc(clockid_t clkid, clockid_t sysclk, int readings,
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t *offset, uint64_t *ts, int64_t *delay)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
2012-08-29 21:28:42 +08:00
|
|
|
struct timespec tdst1, tdst2, tsrc;
|
|
|
|
int i;
|
|
|
|
int64_t interval, best_interval = INT64_MAX;
|
2012-01-25 02:12:57 +08:00
|
|
|
|
2012-08-29 21:28:42 +08:00
|
|
|
/* Pick the quickest clkid reading. */
|
2012-08-31 01:44:38 +08:00
|
|
|
for (i = 0; i < readings; i++) {
|
2012-08-29 21:28:42 +08:00
|
|
|
if (clock_gettime(sysclk, &tdst1) ||
|
|
|
|
clock_gettime(clkid, &tsrc) ||
|
|
|
|
clock_gettime(sysclk, &tdst2)) {
|
2013-02-06 00:36:08 +08:00
|
|
|
pr_err("failed to read clock: %m");
|
2012-08-29 21:28:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
interval = (tdst2.tv_sec - tdst1.tv_sec) * NS_PER_SEC +
|
|
|
|
tdst2.tv_nsec - tdst1.tv_nsec;
|
|
|
|
|
|
|
|
if (best_interval > interval) {
|
|
|
|
best_interval = interval;
|
|
|
|
*offset = (tdst1.tv_sec - tsrc.tv_sec) * NS_PER_SEC +
|
2012-09-26 02:03:04 +08:00
|
|
|
tdst1.tv_nsec - tsrc.tv_nsec + interval / 2;
|
2012-08-29 21:28:42 +08:00
|
|
|
*ts = tdst2.tv_sec * NS_PER_SEC + tdst2.tv_nsec;
|
|
|
|
}
|
2012-08-30 20:45:50 +08:00
|
|
|
}
|
2013-02-08 02:56:52 +08:00
|
|
|
*delay = best_interval;
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
return 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:14 +08:00
|
|
|
static int64_t get_sync_offset(struct node *node, struct clock *dst)
|
|
|
|
{
|
|
|
|
int direction = node->forced_sync_offset;
|
|
|
|
|
|
|
|
if (!direction)
|
|
|
|
direction = dst->is_utc - node->master->is_utc;
|
2014-06-18 21:44:48 +08:00
|
|
|
return (int64_t)dst->sync_offset * NS_PER_SEC * direction;
|
2014-06-12 03:35:14 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
static void update_clock_stats(struct clock *clock, unsigned int max_count,
|
2013-02-08 02:56:53 +08:00
|
|
|
int64_t offset, double freq, int64_t delay)
|
|
|
|
{
|
|
|
|
struct stats_result offset_stats, freq_stats, delay_stats;
|
|
|
|
|
|
|
|
stats_add_value(clock->offset_stats, offset);
|
|
|
|
stats_add_value(clock->freq_stats, freq);
|
|
|
|
if (delay >= 0)
|
|
|
|
stats_add_value(clock->delay_stats, delay);
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
if (stats_get_num_values(clock->offset_stats) < max_count)
|
2013-02-08 02:56:53 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
stats_get_result(clock->offset_stats, &offset_stats);
|
|
|
|
stats_get_result(clock->freq_stats, &freq_stats);
|
|
|
|
|
|
|
|
if (!stats_get_result(clock->delay_stats, &delay_stats)) {
|
2017-06-17 06:07:19 +08:00
|
|
|
pr_info("%s "
|
|
|
|
"rms %4.0f max %4.0f "
|
2013-02-08 02:56:53 +08:00
|
|
|
"freq %+6.0f +/- %3.0f "
|
|
|
|
"delay %5.0f +/- %3.0f",
|
2017-06-17 06:07:19 +08:00
|
|
|
clock->device,
|
2013-02-08 02:56:53 +08:00
|
|
|
offset_stats.rms, offset_stats.max_abs,
|
|
|
|
freq_stats.mean, freq_stats.stddev,
|
|
|
|
delay_stats.mean, delay_stats.stddev);
|
|
|
|
} else {
|
2017-06-17 06:07:19 +08:00
|
|
|
pr_info("%s "
|
|
|
|
"rms %4.0f max %4.0f "
|
2013-02-08 02:56:53 +08:00
|
|
|
"freq %+6.0f +/- %3.0f",
|
2017-06-17 06:07:19 +08:00
|
|
|
clock->device,
|
2013-02-08 02:56:53 +08:00
|
|
|
offset_stats.rms, offset_stats.max_abs,
|
|
|
|
freq_stats.mean, freq_stats.stddev);
|
|
|
|
}
|
|
|
|
|
|
|
|
stats_reset(clock->offset_stats);
|
|
|
|
stats_reset(clock->freq_stats);
|
|
|
|
stats_reset(clock->delay_stats);
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
static void update_clock(struct node *node, struct clock *clock,
|
2014-06-18 21:44:48 +08:00
|
|
|
int64_t offset, uint64_t ts, int64_t delay)
|
2012-09-26 14:42:19 +08:00
|
|
|
{
|
2013-01-15 00:09:12 +08:00
|
|
|
enum servo_state state;
|
|
|
|
double ppb;
|
2012-09-26 14:42:19 +08:00
|
|
|
|
2014-06-18 21:44:48 +08:00
|
|
|
if (clock_handle_leap(node, clock, offset, ts))
|
2013-03-08 00:27:28 +08:00
|
|
|
return;
|
|
|
|
|
2014-06-12 03:35:14 +08:00
|
|
|
offset += get_sync_offset(node, clock);
|
2013-03-08 00:27:28 +08:00
|
|
|
|
2013-10-23 00:57:15 +08:00
|
|
|
if (clock->sanity_check && clockcheck_sample(clock->sanity_check, ts))
|
|
|
|
servo_reset(clock->servo);
|
|
|
|
|
2015-03-26 23:32:15 +08:00
|
|
|
ppb = servo_sample(clock->servo, offset, ts, 1.0, &state);
|
2013-03-08 00:27:30 +08:00
|
|
|
clock->servo_state = state;
|
2013-01-15 00:09:12 +08:00
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case SERVO_UNLOCKED:
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2013-01-15 00:09:12 +08:00
|
|
|
case SERVO_JUMP:
|
2013-03-08 00:27:29 +08:00
|
|
|
clockadj_step(clock->clkid, -offset);
|
2013-10-23 00:57:15 +08:00
|
|
|
if (clock->sanity_check)
|
|
|
|
clockcheck_step(clock->sanity_check, -offset);
|
2013-01-15 00:09:12 +08:00
|
|
|
/* Fall through. */
|
|
|
|
case SERVO_LOCKED:
|
2013-03-08 00:27:29 +08:00
|
|
|
clockadj_set_freq(clock->clkid, -ppb);
|
2013-04-08 21:44:10 +08:00
|
|
|
if (clock->clkid == CLOCK_REALTIME)
|
|
|
|
sysclk_set_sync();
|
2013-10-23 00:57:15 +08:00
|
|
|
if (clock->sanity_check)
|
|
|
|
clockcheck_set_freq(clock->sanity_check, -ppb);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
if (clock->offset_stats) {
|
2014-06-12 03:35:13 +08:00
|
|
|
update_clock_stats(clock, node->stats_max_count, offset, ppb, delay);
|
2013-02-08 02:56:52 +08:00
|
|
|
} else {
|
2013-02-08 02:56:53 +08:00
|
|
|
if (delay >= 0) {
|
2017-06-17 06:07:19 +08:00
|
|
|
pr_info("%s %s offset %9" PRId64 " s%d freq %+7.0f "
|
2013-02-08 02:56:53 +08:00
|
|
|
"delay %6" PRId64,
|
2017-06-17 06:07:19 +08:00
|
|
|
clock->device, node->master->source_label,
|
|
|
|
offset, state, ppb, delay);
|
2013-02-08 02:56:53 +08:00
|
|
|
} else {
|
2017-06-17 06:07:19 +08:00
|
|
|
pr_info("%s %s offset %9" PRId64 " s%d freq %+7.0f",
|
|
|
|
clock->device, node->master->source_label,
|
|
|
|
offset, state, ppb);
|
2013-02-08 02:56:53 +08:00
|
|
|
}
|
2013-02-08 02:56:52 +08:00
|
|
|
}
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2013-04-19 21:28:31 +08:00
|
|
|
static void enable_pps_output(clockid_t src)
|
|
|
|
{
|
|
|
|
int enable = 1;
|
|
|
|
|
|
|
|
if (!phc_has_pps(src))
|
|
|
|
return;
|
|
|
|
if (ioctl(CLOCKID_TO_FD(src), PTP_ENABLE_PPS, enable) < 0)
|
|
|
|
pr_warning("failed to enable PPS output");
|
|
|
|
}
|
|
|
|
|
2012-08-29 21:29:43 +08:00
|
|
|
static int read_pps(int fd, int64_t *offset, uint64_t *ts)
|
2012-01-25 02:12:57 +08:00
|
|
|
{
|
|
|
|
struct pps_fdata pfd;
|
2012-08-29 21:29:43 +08:00
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
pfd.timeout.sec = 10;
|
|
|
|
pfd.timeout.nsec = 0;
|
|
|
|
pfd.timeout.flags = ~PPS_TIME_INVALID;
|
|
|
|
if (ioctl(fd, PPS_FETCH, &pfd)) {
|
2013-02-06 00:36:08 +08:00
|
|
|
pr_err("failed to fetch PPS: %m");
|
2012-01-25 02:12:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-29 21:29:43 +08:00
|
|
|
|
|
|
|
*ts = pfd.info.assert_tu.sec * NS_PER_SEC;
|
|
|
|
*ts += pfd.info.assert_tu.nsec;
|
|
|
|
|
|
|
|
*offset = *ts % NS_PER_SEC;
|
|
|
|
if (*offset > NS_PER_SEC / 2)
|
|
|
|
*offset -= NS_PER_SEC;
|
|
|
|
|
|
|
|
return 1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
static int do_pps_loop(struct node *node, struct clock *clock, int fd)
|
2012-09-26 02:00:32 +08:00
|
|
|
{
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t pps_offset, phc_offset, phc_delay;
|
2013-01-18 01:31:39 +08:00
|
|
|
uint64_t pps_ts, phc_ts;
|
2014-06-12 03:35:13 +08:00
|
|
|
clockid_t src = node->master->clkid;
|
2012-09-26 02:00:32 +08:00
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
node->master->source_label = "pps";
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2013-04-19 21:28:31 +08:00
|
|
|
if (src == CLOCK_INVALID) {
|
|
|
|
/* The sync offset can't be applied with PPS alone. */
|
2014-06-12 03:35:14 +08:00
|
|
|
node->sync_offset = 0;
|
2013-04-19 21:28:31 +08:00
|
|
|
} else {
|
2014-06-12 03:35:13 +08:00
|
|
|
enable_pps_output(node->master->clkid);
|
2013-04-19 21:28:31 +08:00
|
|
|
}
|
2013-03-08 00:27:28 +08:00
|
|
|
|
2014-07-08 22:14:21 +08:00
|
|
|
while (is_running()) {
|
2012-09-26 02:00:32 +08:00
|
|
|
if (!read_pps(fd, &pps_offset, &pps_ts)) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-18 01:31:39 +08:00
|
|
|
|
|
|
|
/* If a PHC is available, use it to get the whole number
|
|
|
|
of seconds in the offset and PPS for the rest. */
|
|
|
|
if (src != CLOCK_INVALID) {
|
2014-06-12 03:35:13 +08:00
|
|
|
if (!read_phc(src, clock->clkid, node->phc_readings,
|
2013-02-08 02:56:52 +08:00
|
|
|
&phc_offset, &phc_ts, &phc_delay))
|
2013-01-18 01:31:39 +08:00
|
|
|
return -1;
|
2013-02-06 00:36:12 +08:00
|
|
|
|
2013-01-18 01:31:39 +08:00
|
|
|
/* Convert the time stamp to the PHC time. */
|
|
|
|
phc_ts -= phc_offset;
|
|
|
|
|
|
|
|
/* Check if it is close to the start of the second. */
|
|
|
|
if (phc_ts % NS_PER_SEC > PHC_PPS_OFFSET_LIMIT) {
|
2013-02-06 00:36:08 +08:00
|
|
|
pr_warning("PPS is not in sync with PHC"
|
|
|
|
" (0.%09lld)", phc_ts % NS_PER_SEC);
|
2013-01-18 01:31:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
phc_ts = phc_ts / NS_PER_SEC * NS_PER_SEC;
|
|
|
|
pps_offset = pps_ts - phc_ts;
|
|
|
|
}
|
|
|
|
|
2014-06-18 21:44:48 +08:00
|
|
|
if (update_pmc(node, 0) < 0)
|
2014-06-12 03:35:12 +08:00
|
|
|
continue;
|
2014-06-18 21:44:48 +08:00
|
|
|
update_clock(node, clock, pps_offset, pps_ts, -1);
|
2012-09-26 02:00:32 +08:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-06 05:55:02 +08:00
|
|
|
static int update_needed(struct clock *c)
|
|
|
|
{
|
|
|
|
switch (c->state) {
|
|
|
|
case PS_FAULTY:
|
|
|
|
case PS_DISABLED:
|
|
|
|
case PS_LISTENING:
|
|
|
|
case PS_PRE_MASTER:
|
|
|
|
case PS_MASTER:
|
|
|
|
case PS_PASSIVE:
|
|
|
|
return 1;
|
|
|
|
case PS_UNCALIBRATED:
|
|
|
|
case PS_SLAVE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:21 +08:00
|
|
|
static int do_loop(struct node *node, int subscriptions)
|
2012-11-26 20:25:35 +08:00
|
|
|
{
|
2014-06-12 03:35:13 +08:00
|
|
|
struct timespec interval;
|
|
|
|
struct clock *clock;
|
2012-11-26 20:25:35 +08:00
|
|
|
uint64_t ts;
|
2013-02-08 02:56:52 +08:00
|
|
|
int64_t offset, delay;
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
interval.tv_sec = node->phc_interval;
|
|
|
|
interval.tv_nsec = (node->phc_interval - interval.tv_sec) * 1e9;
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2014-07-08 22:14:21 +08:00
|
|
|
while (is_running()) {
|
2014-06-12 03:35:13 +08:00
|
|
|
clock_nanosleep(CLOCK_MONOTONIC, 0, &interval, NULL);
|
2014-06-18 21:44:48 +08:00
|
|
|
if (update_pmc(node, subscriptions) < 0)
|
2014-06-12 03:35:12 +08:00
|
|
|
continue;
|
2013-01-15 00:09:13 +08:00
|
|
|
|
2014-06-12 03:35:21 +08:00
|
|
|
if (subscriptions) {
|
|
|
|
run_pmc_events(node);
|
|
|
|
if (node->state_changed) {
|
|
|
|
/* force getting offset, as it may have
|
|
|
|
* changed after the port state change */
|
|
|
|
if (run_pmc_get_utc_offset(node, 1000) <= 0) {
|
|
|
|
pr_err("failed to get UTC offset");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
reconfigure(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!node->master)
|
|
|
|
continue;
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
LIST_FOREACH(clock, &node->clocks, list) {
|
2014-11-06 05:55:02 +08:00
|
|
|
if (!update_needed(clock))
|
2014-06-12 03:35:13 +08:00
|
|
|
continue;
|
2013-01-15 00:09:13 +08:00
|
|
|
|
2017-03-27 23:43:06 +08:00
|
|
|
/* don't try to synchronize the clock to itself */
|
|
|
|
if (clock->clkid == node->master->clkid ||
|
|
|
|
(clock->phc_index >= 0 &&
|
|
|
|
clock->phc_index == node->master->phc_index) ||
|
|
|
|
!strcmp(clock->device, node->master->device))
|
|
|
|
continue;
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
if (clock->clkid == CLOCK_REALTIME &&
|
|
|
|
node->master->sysoff_supported) {
|
|
|
|
/* use sysoff */
|
2014-06-12 03:35:21 +08:00
|
|
|
if (sysoff_measure(CLOCKID_TO_FD(node->master->clkid),
|
|
|
|
node->phc_readings,
|
2014-06-12 03:35:13 +08:00
|
|
|
&offset, &ts, &delay))
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
/* use phc */
|
|
|
|
if (!read_phc(node->master->clkid, clock->clkid,
|
|
|
|
node->phc_readings,
|
|
|
|
&offset, &ts, &delay))
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-18 21:44:48 +08:00
|
|
|
update_clock(node, clock, offset, ts, delay);
|
2013-01-15 00:09:13 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-08 22:14:21 +08:00
|
|
|
return 0;
|
2013-01-15 00:09:13 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:23 +08:00
|
|
|
static int check_clock_identity(struct node *node, struct ptp_message *msg)
|
|
|
|
{
|
|
|
|
if (!node->clock_identity_set)
|
|
|
|
return 1;
|
|
|
|
return !memcmp(&node->clock_identity,
|
|
|
|
&msg->header.sourcePortIdentity.clockIdentity,
|
|
|
|
sizeof(struct ClockIdentity));
|
|
|
|
}
|
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
static int is_msg_mgt(struct ptp_message *msg)
|
|
|
|
{
|
|
|
|
struct TLV *tlv;
|
|
|
|
|
|
|
|
if (msg_type(msg) != MANAGEMENT)
|
|
|
|
return 0;
|
|
|
|
if (management_action(msg) != RESPONSE)
|
|
|
|
return 0;
|
|
|
|
if (msg->tlv_count != 1)
|
|
|
|
return 0;
|
|
|
|
tlv = (struct TLV *) msg->management.suffix;
|
2014-06-12 03:35:20 +08:00
|
|
|
if (tlv->type == TLV_MANAGEMENT)
|
|
|
|
return 1;
|
|
|
|
if (tlv->type == TLV_MANAGEMENT_ERROR_STATUS)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_mgt_id(struct ptp_message *msg)
|
|
|
|
{
|
2013-04-04 23:28:30 +08:00
|
|
|
struct management_tlv *mgt = (struct management_tlv *) msg->management.suffix;
|
|
|
|
return mgt->id;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *get_mgt_data(struct ptp_message *msg)
|
|
|
|
{
|
2013-04-04 23:28:30 +08:00
|
|
|
struct management_tlv *mgt = (struct management_tlv *) msg->management.suffix;
|
|
|
|
return mgt->data;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:20 +08:00
|
|
|
static int get_mgt_err_id(struct ptp_message *msg)
|
|
|
|
{
|
|
|
|
struct management_error_status *mgt;
|
|
|
|
|
|
|
|
mgt = (struct management_error_status *)msg->management.suffix;
|
|
|
|
return mgt->id;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:19 +08:00
|
|
|
static int normalize_state(int state)
|
|
|
|
{
|
|
|
|
if (state != PS_MASTER && state != PS_SLAVE &&
|
|
|
|
state != PS_PRE_MASTER && state != PS_UNCALIBRATED) {
|
|
|
|
/* treat any other state as "not a master nor a slave" */
|
|
|
|
state = PS_DISABLED;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clock_compute_state(struct node *node, struct clock *clock)
|
|
|
|
{
|
|
|
|
struct port *p;
|
|
|
|
int state = PS_DISABLED;
|
|
|
|
|
|
|
|
LIST_FOREACH(p, &node->ports, list) {
|
|
|
|
if (p->clock != clock)
|
|
|
|
continue;
|
|
|
|
/* PS_SLAVE takes the highest precedence, PS_UNCALIBRATED
|
|
|
|
* after that, PS_MASTER is third, PS_PRE_MASTER fourth and
|
|
|
|
* all of that overrides PS_DISABLED, which corresponds
|
|
|
|
* nicely with the numerical values */
|
|
|
|
if (p->state > state)
|
|
|
|
state = p->state;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int recv_subscribed(struct node *node, struct ptp_message *msg,
|
|
|
|
int excluded)
|
|
|
|
{
|
|
|
|
int mgt_id, state;
|
|
|
|
struct portDS *pds;
|
|
|
|
struct port *port;
|
|
|
|
struct clock *clock;
|
|
|
|
|
|
|
|
mgt_id = get_mgt_id(msg);
|
|
|
|
if (mgt_id == excluded)
|
|
|
|
return 0;
|
|
|
|
switch (mgt_id) {
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_PORT_DATA_SET:
|
2014-06-12 03:35:19 +08:00
|
|
|
pds = get_mgt_data(msg);
|
|
|
|
port = port_get(node, pds->portIdentity.portNumber);
|
|
|
|
if (!port) {
|
|
|
|
pr_info("received data for unknown port %s",
|
|
|
|
pid2str(&pds->portIdentity));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
state = normalize_state(pds->portState);
|
|
|
|
if (port->state != state) {
|
|
|
|
pr_info("port %s changed state",
|
|
|
|
pid2str(&pds->portIdentity));
|
|
|
|
port->state = state;
|
|
|
|
clock = port->clock;
|
|
|
|
state = clock_compute_state(node, clock);
|
2017-09-01 19:41:41 +08:00
|
|
|
if (clock->state != state || clock->new_state) {
|
2014-06-12 03:35:19 +08:00
|
|
|
clock->new_state = state;
|
|
|
|
node->state_changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_subscription(struct node *node)
|
|
|
|
{
|
|
|
|
struct subscribe_events_np sen;
|
|
|
|
|
|
|
|
memset(&sen, 0, sizeof(sen));
|
|
|
|
sen.duration = PMC_SUBSCRIBE_DURATION;
|
|
|
|
sen.bitmask[0] = 1 << NOTIFY_PORT_STATE;
|
2014-06-25 18:45:23 +08:00
|
|
|
pmc_send_set_action(node->pmc, TLV_SUBSCRIBE_EVENTS_NP, &sen, sizeof(sen));
|
2014-06-12 03:35:19 +08:00
|
|
|
}
|
|
|
|
|
2018-04-16 03:13:47 +08:00
|
|
|
static int init_pmc(struct config *cfg, struct node *node)
|
2013-03-08 00:27:28 +08:00
|
|
|
{
|
2014-07-08 22:14:22 +08:00
|
|
|
char uds_local[MAX_IFNAME_SIZE + 1];
|
|
|
|
|
|
|
|
snprintf(uds_local, sizeof(uds_local), "/var/run/phc2sys.%d",
|
|
|
|
getpid());
|
2018-04-16 03:13:47 +08:00
|
|
|
node->pmc = pmc_create(cfg, TRANS_UDS, uds_local, 0,
|
|
|
|
config_get_int(cfg, NULL, "domainNumber"), 0, 1);
|
2014-06-12 03:35:13 +08:00
|
|
|
if (!node->pmc) {
|
2013-03-08 00:27:28 +08:00
|
|
|
pr_err("failed to create pmc");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:20 +08:00
|
|
|
/* Return values:
|
|
|
|
* 1: success
|
|
|
|
* 0: timeout
|
|
|
|
* -1: error reported by the other side
|
|
|
|
* -2: local error, fatal
|
|
|
|
*/
|
2014-06-12 03:35:13 +08:00
|
|
|
static int run_pmc(struct node *node, int timeout, int ds_id,
|
2014-06-12 03:35:11 +08:00
|
|
|
struct ptp_message **msg)
|
2013-01-30 01:06:18 +08:00
|
|
|
{
|
|
|
|
#define N_FD 1
|
|
|
|
struct pollfd pollfd[N_FD];
|
2014-06-12 03:35:20 +08:00
|
|
|
int cnt, res;
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-06-12 03:35:11 +08:00
|
|
|
while (1) {
|
2014-06-12 03:35:13 +08:00
|
|
|
pollfd[0].fd = pmc_get_transport_fd(node->pmc);
|
2013-01-30 01:06:18 +08:00
|
|
|
pollfd[0].events = POLLIN|POLLPRI;
|
2014-06-12 03:35:19 +08:00
|
|
|
if (!node->pmc_ds_requested && ds_id >= 0)
|
2013-01-30 01:06:18 +08:00
|
|
|
pollfd[0].events |= POLLOUT;
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
cnt = poll(pollfd, N_FD, timeout);
|
2013-01-30 01:06:18 +08:00
|
|
|
if (cnt < 0) {
|
2013-02-06 00:36:08 +08:00
|
|
|
pr_err("poll failed");
|
2014-06-12 03:35:20 +08:00
|
|
|
return -2;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
if (!cnt) {
|
2013-03-08 00:27:28 +08:00
|
|
|
/* Request the data set again in the next run. */
|
2014-06-12 03:35:13 +08:00
|
|
|
node->pmc_ds_requested = 0;
|
2013-03-08 00:27:28 +08:00
|
|
|
return 0;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
2013-03-08 00:27:28 +08:00
|
|
|
/* Send a new request if there are no pending messages. */
|
|
|
|
if ((pollfd[0].revents & POLLOUT) &&
|
|
|
|
!(pollfd[0].revents & (POLLIN|POLLPRI))) {
|
2014-06-12 03:35:19 +08:00
|
|
|
switch (ds_id) {
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_SUBSCRIBE_EVENTS_NP:
|
2014-06-12 03:35:19 +08:00
|
|
|
send_subscription(node);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pmc_send_get_action(node->pmc, ds_id);
|
|
|
|
break;
|
|
|
|
}
|
2014-06-12 03:35:13 +08:00
|
|
|
node->pmc_ds_requested = 1;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pollfd[0].revents & (POLLIN|POLLPRI)))
|
|
|
|
continue;
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
*msg = pmc_recv(node->pmc);
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-06-12 03:35:11 +08:00
|
|
|
if (!*msg)
|
2013-01-30 01:06:18 +08:00
|
|
|
continue;
|
|
|
|
|
2014-06-12 03:35:23 +08:00
|
|
|
if (!check_clock_identity(node, *msg)) {
|
|
|
|
msg_put(*msg);
|
|
|
|
*msg = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:20 +08:00
|
|
|
res = is_msg_mgt(*msg);
|
|
|
|
if (res < 0 && get_mgt_err_id(*msg) == ds_id) {
|
|
|
|
node->pmc_ds_requested = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (res <= 0 || recv_subscribed(node, *msg, ds_id) ||
|
2014-06-12 03:35:11 +08:00
|
|
|
get_mgt_id(*msg) != ds_id) {
|
|
|
|
msg_put(*msg);
|
|
|
|
*msg = NULL;
|
2013-01-30 01:06:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-12 03:35:13 +08:00
|
|
|
node->pmc_ds_requested = 0;
|
2014-06-12 03:35:11 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
static int run_pmc_wait_sync(struct node *node, int timeout)
|
2014-06-12 03:35:11 +08:00
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
int res;
|
|
|
|
void *data;
|
|
|
|
Enumeration8 portState;
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-06-12 03:35:11 +08:00
|
|
|
while (1) {
|
2014-06-25 18:45:23 +08:00
|
|
|
res = run_pmc(node, timeout, TLV_PORT_DATA_SET, &msg);
|
2014-06-12 03:35:11 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-06-12 03:35:11 +08:00
|
|
|
data = get_mgt_data(msg);
|
|
|
|
portState = ((struct portDS *)data)->portState;
|
2013-01-30 01:06:18 +08:00
|
|
|
msg_put(msg);
|
2014-06-12 03:35:11 +08:00
|
|
|
|
|
|
|
switch (portState) {
|
|
|
|
case PS_MASTER:
|
|
|
|
case PS_SLAVE:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* try to get more data sets (for other ports) */
|
2014-06-12 03:35:13 +08:00
|
|
|
node->pmc_ds_requested = 1;
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
2014-06-12 03:35:11 +08:00
|
|
|
}
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
static int run_pmc_get_utc_offset(struct node *node, int timeout)
|
2014-06-12 03:35:11 +08:00
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
int res;
|
|
|
|
struct timePropertiesDS *tds;
|
|
|
|
|
2014-06-25 18:45:23 +08:00
|
|
|
res = run_pmc(node, timeout, TLV_TIME_PROPERTIES_DATA_SET, &msg);
|
2014-06-12 03:35:11 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
tds = (struct timePropertiesDS *)get_mgt_data(msg);
|
|
|
|
if (tds->flags & PTP_TIMESCALE) {
|
2014-06-12 03:35:13 +08:00
|
|
|
node->sync_offset = tds->currentUtcOffset;
|
2014-06-12 03:35:11 +08:00
|
|
|
if (tds->flags & LEAP_61)
|
2014-06-12 03:35:13 +08:00
|
|
|
node->leap = 1;
|
2014-06-12 03:35:11 +08:00
|
|
|
else if (tds->flags & LEAP_59)
|
2014-06-12 03:35:13 +08:00
|
|
|
node->leap = -1;
|
2014-06-12 03:35:11 +08:00
|
|
|
else
|
2014-06-12 03:35:13 +08:00
|
|
|
node->leap = 0;
|
2014-06-18 21:44:49 +08:00
|
|
|
node->utc_offset_traceable = tds->flags & UTC_OFF_VALID &&
|
|
|
|
tds->flags & TIME_TRACEABLE;
|
2014-06-12 03:35:25 +08:00
|
|
|
} else {
|
|
|
|
node->sync_offset = 0;
|
|
|
|
node->leap = 0;
|
2014-06-18 21:44:49 +08:00
|
|
|
node->utc_offset_traceable = 0;
|
2014-06-12 03:35:11 +08:00
|
|
|
}
|
|
|
|
msg_put(msg);
|
2013-03-08 00:27:28 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:21 +08:00
|
|
|
static int run_pmc_get_number_ports(struct node *node, int timeout)
|
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
int res;
|
|
|
|
struct defaultDS *dds;
|
|
|
|
|
2014-06-25 18:45:23 +08:00
|
|
|
res = run_pmc(node, timeout, TLV_DEFAULT_DATA_SET, &msg);
|
2014-06-12 03:35:21 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
dds = (struct defaultDS *)get_mgt_data(msg);
|
|
|
|
res = dds->numberPorts;
|
|
|
|
msg_put(msg);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:19 +08:00
|
|
|
static int run_pmc_subscribe(struct node *node, int timeout)
|
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
int res;
|
|
|
|
|
2014-06-25 18:45:23 +08:00
|
|
|
res = run_pmc(node, timeout, TLV_SUBSCRIBE_EVENTS_NP, &msg);
|
2014-06-12 03:35:19 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
|
|
|
msg_put(msg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void run_pmc_events(struct node *node)
|
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
|
|
|
|
run_pmc(node, 0, -1, &msg);
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:21 +08:00
|
|
|
static int run_pmc_port_properties(struct node *node, int timeout,
|
|
|
|
unsigned int port,
|
|
|
|
int *state, int *tstamping, char *iface)
|
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
int res, len;
|
|
|
|
struct port_properties_np *ppn;
|
|
|
|
|
|
|
|
pmc_target_port(node->pmc, port);
|
|
|
|
while (1) {
|
2014-06-25 18:45:23 +08:00
|
|
|
res = run_pmc(node, timeout, TLV_PORT_PROPERTIES_NP, &msg);
|
2014-06-12 03:35:21 +08:00
|
|
|
if (res <= 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ppn = get_mgt_data(msg);
|
|
|
|
if (ppn->portIdentity.portNumber != port) {
|
|
|
|
msg_put(msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*state = ppn->port_state;
|
|
|
|
*tstamping = ppn->timestamping;
|
|
|
|
len = ppn->interface.length;
|
|
|
|
if (len > IFNAMSIZ - 1)
|
|
|
|
len = IFNAMSIZ - 1;
|
|
|
|
memcpy(iface, ppn->interface.text, len);
|
|
|
|
iface[len] = '\0';
|
|
|
|
|
|
|
|
msg_put(msg);
|
|
|
|
res = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
pmc_target_all(node->pmc);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:23 +08:00
|
|
|
static int run_pmc_clock_identity(struct node *node, int timeout)
|
|
|
|
{
|
|
|
|
struct ptp_message *msg;
|
|
|
|
struct defaultDS *dds;
|
|
|
|
int res;
|
|
|
|
|
2014-06-25 18:45:23 +08:00
|
|
|
res = run_pmc(node, timeout, TLV_DEFAULT_DATA_SET, &msg);
|
2014-06-12 03:35:23 +08:00
|
|
|
if (res <= 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
dds = (struct defaultDS *)get_mgt_data(msg);
|
|
|
|
memcpy(&node->clock_identity, &dds->clockIdentity,
|
|
|
|
sizeof(struct ClockIdentity));
|
|
|
|
node->clock_identity_set = 1;
|
|
|
|
msg_put(msg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
static void close_pmc(struct node *node)
|
2013-03-08 00:27:28 +08:00
|
|
|
{
|
2014-06-12 03:35:13 +08:00
|
|
|
pmc_destroy(node->pmc);
|
|
|
|
node->pmc = NULL;
|
2013-03-08 00:27:28 +08:00
|
|
|
}
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-06-12 03:35:22 +08:00
|
|
|
static int auto_init_ports(struct node *node, int add_rt)
|
2014-06-12 03:35:21 +08:00
|
|
|
{
|
|
|
|
struct port *port;
|
|
|
|
struct clock *clock;
|
|
|
|
int number_ports, res;
|
|
|
|
unsigned int i;
|
|
|
|
int state, timestamping;
|
|
|
|
char iface[IFNAMSIZ];
|
|
|
|
|
|
|
|
while (1) {
|
2014-06-12 03:35:23 +08:00
|
|
|
res = run_pmc_clock_identity(node, 1000);
|
2014-06-12 03:35:21 +08:00
|
|
|
if (res < 0)
|
|
|
|
return -1;
|
|
|
|
if (res > 0)
|
|
|
|
break;
|
|
|
|
/* res == 0, timeout */
|
|
|
|
pr_notice("Waiting for ptp4l...");
|
|
|
|
}
|
2014-06-12 03:35:23 +08:00
|
|
|
|
|
|
|
number_ports = run_pmc_get_number_ports(node, 1000);
|
|
|
|
if (number_ports <= 0) {
|
|
|
|
pr_err("failed to get number of ports");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-12 03:35:21 +08:00
|
|
|
|
|
|
|
res = run_pmc_subscribe(node, 1000);
|
|
|
|
if (res <= 0) {
|
|
|
|
pr_err("failed to subscribe");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i <= number_ports; i++) {
|
|
|
|
res = run_pmc_port_properties(node, 1000, i, &state,
|
|
|
|
×tamping, iface);
|
|
|
|
if (res == -1) {
|
|
|
|
/* port does not exist, ignore the port */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (res <= 0) {
|
|
|
|
pr_err("failed to get port properties");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (timestamping == TS_SOFTWARE) {
|
|
|
|
/* ignore ports with software time stamping */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
port = port_add(node, i, iface);
|
|
|
|
if (!port)
|
|
|
|
return -1;
|
|
|
|
port->state = normalize_state(state);
|
|
|
|
}
|
|
|
|
if (LIST_EMPTY(&node->clocks)) {
|
|
|
|
pr_err("no suitable ports available");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LIST_FOREACH(clock, &node->clocks, list) {
|
|
|
|
clock->new_state = clock_compute_state(node, clock);
|
|
|
|
}
|
|
|
|
node->state_changed = 1;
|
|
|
|
|
2014-06-12 03:35:22 +08:00
|
|
|
if (add_rt) {
|
|
|
|
clock = clock_add(node, "CLOCK_REALTIME");
|
|
|
|
if (!clock)
|
|
|
|
return -1;
|
|
|
|
if (add_rt == 1)
|
|
|
|
clock->dest_only = 1;
|
|
|
|
}
|
2014-06-12 03:35:21 +08:00
|
|
|
|
|
|
|
/* get initial offset */
|
|
|
|
if (run_pmc_get_utc_offset(node, 1000) <= 0) {
|
|
|
|
pr_err("failed to get UTC offset");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-18 21:44:48 +08:00
|
|
|
/* Returns: -1 in case of error, 0 otherwise */
|
2014-06-12 03:35:21 +08:00
|
|
|
static int update_pmc(struct node *node, int subscribe)
|
2013-03-08 00:27:28 +08:00
|
|
|
{
|
2014-06-12 03:35:12 +08:00
|
|
|
struct timespec tp;
|
|
|
|
uint64_t ts;
|
2013-03-08 00:27:30 +08:00
|
|
|
|
2014-06-20 22:25:01 +08:00
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
|
2014-06-12 03:35:12 +08:00
|
|
|
pr_err("failed to read clock: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ts = tp.tv_sec * NS_PER_SEC + tp.tv_nsec;
|
|
|
|
|
2014-06-12 03:35:13 +08:00
|
|
|
if (node->pmc &&
|
|
|
|
!(ts > node->pmc_last_update &&
|
|
|
|
ts - node->pmc_last_update < PMC_UPDATE_INTERVAL)) {
|
2014-06-12 03:35:21 +08:00
|
|
|
if (subscribe)
|
|
|
|
run_pmc_subscribe(node, 0);
|
2014-06-12 03:35:13 +08:00
|
|
|
if (run_pmc_get_utc_offset(node, 0) > 0)
|
|
|
|
node->pmc_last_update = ts;
|
2013-03-08 00:27:28 +08:00
|
|
|
}
|
2013-03-08 00:27:30 +08:00
|
|
|
|
2014-06-12 03:35:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns: non-zero to skip clock update */
|
2014-06-12 03:35:13 +08:00
|
|
|
static int clock_handle_leap(struct node *node, struct clock *clock,
|
2014-06-18 21:44:48 +08:00
|
|
|
int64_t offset, uint64_t ts)
|
2014-06-12 03:35:12 +08:00
|
|
|
{
|
2014-06-18 21:44:48 +08:00
|
|
|
int clock_leap, node_leap = node->leap;
|
|
|
|
|
|
|
|
clock->sync_offset = node->sync_offset;
|
|
|
|
|
2014-06-18 21:44:49 +08:00
|
|
|
if ((node_leap || clock->leap_set) &&
|
|
|
|
clock->is_utc != node->master->is_utc) {
|
|
|
|
/* If the master clock is in UTC, get a time stamp from it, as
|
|
|
|
it is the clock which will include the leap second. */
|
|
|
|
if (node->master->is_utc) {
|
|
|
|
struct timespec tp;
|
|
|
|
if (clock_gettime(node->master->clkid, &tp)) {
|
|
|
|
pr_err("failed to read clock: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ts = tp.tv_sec * NS_PER_SEC + tp.tv_nsec;
|
|
|
|
}
|
2014-06-12 03:35:12 +08:00
|
|
|
|
2014-06-18 21:44:49 +08:00
|
|
|
/* If the clock will be stepped, the time stamp has to be the
|
|
|
|
new time. Ignore possible 1 second error in UTC offset. */
|
|
|
|
if (clock->is_utc && clock->servo_state == SERVO_UNLOCKED)
|
|
|
|
ts -= offset + get_sync_offset(node, clock);
|
2014-06-12 03:35:12 +08:00
|
|
|
|
2014-06-18 21:44:49 +08:00
|
|
|
/* Suspend clock updates in the last second before midnight. */
|
|
|
|
if (is_utc_ambiguous(ts)) {
|
|
|
|
pr_info("clock update suspended due to leap second");
|
|
|
|
return 1;
|
2013-03-08 00:27:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 21:44:49 +08:00
|
|
|
clock_leap = leap_second_status(ts, clock->leap_set,
|
|
|
|
&node_leap,
|
|
|
|
&clock->sync_offset);
|
2013-03-08 00:27:30 +08:00
|
|
|
|
2014-06-18 21:44:49 +08:00
|
|
|
if (clock->leap_set != clock_leap) {
|
|
|
|
/* Only the system clock can leap. */
|
|
|
|
if (clock->clkid == CLOCK_REALTIME &&
|
|
|
|
node->kernel_leap)
|
|
|
|
sysclk_set_leap(clock_leap);
|
2014-06-20 22:32:48 +08:00
|
|
|
else
|
|
|
|
servo_leap(clock->servo, clock_leap);
|
2014-06-18 21:44:49 +08:00
|
|
|
clock->leap_set = clock_leap;
|
|
|
|
}
|
2013-03-08 00:27:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-18 21:44:49 +08:00
|
|
|
if (node->utc_offset_traceable &&
|
|
|
|
clock->utc_offset_set != clock->sync_offset) {
|
|
|
|
if (clock->clkid == CLOCK_REALTIME)
|
|
|
|
sysclk_set_tai_offset(clock->sync_offset);
|
|
|
|
clock->utc_offset_set = clock->sync_offset;
|
2013-03-08 00:27:30 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-25 02:12:57 +08:00
|
|
|
static void usage(char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n"
|
|
|
|
"usage: %s [options]\n\n"
|
2014-06-12 03:35:21 +08:00
|
|
|
"\n"
|
|
|
|
" automatic configuration:\n"
|
|
|
|
" -a turn on autoconfiguration\n"
|
2014-06-12 03:35:22 +08:00
|
|
|
" -r synchronize system (realtime) clock\n"
|
|
|
|
" repeat -r to consider it also as a time source\n"
|
2014-06-12 03:35:21 +08:00
|
|
|
" manual configuration:\n"
|
2012-11-01 22:15:36 +08:00
|
|
|
" -c [dev|name] slave clock (CLOCK_REALTIME)\n"
|
|
|
|
" -d [dev] master PPS device\n"
|
|
|
|
" -s [dev|name] master clock\n"
|
2014-06-12 03:35:21 +08:00
|
|
|
" -O [offset] slave-master time offset (0)\n"
|
|
|
|
" -w wait for ptp4l\n"
|
|
|
|
" common options:\n"
|
2014-03-14 01:34:18 +08:00
|
|
|
" -E [pi|linreg] clock servo (pi)\n"
|
2012-11-01 22:15:36 +08:00
|
|
|
" -P [kp] proportional constant (0.7)\n"
|
|
|
|
" -I [ki] integration constant (0.3)\n"
|
2013-02-06 00:36:04 +08:00
|
|
|
" -S [step] step threshold (disabled)\n"
|
2014-03-19 01:25:13 +08:00
|
|
|
" -F [step] step threshold only on start (0.00002)\n"
|
2013-04-24 21:49:11 +08:00
|
|
|
" -R [rate] slave clock update rate in HZ (1.0)\n"
|
2012-11-01 22:15:36 +08:00
|
|
|
" -N [num] number of master clock readings per update (5)\n"
|
2013-10-23 00:57:15 +08:00
|
|
|
" -L [limit] sanity frequency limit in ppb (200000000)\n"
|
2014-07-09 22:25:08 +08:00
|
|
|
" -M [num] NTP SHM segment number (0)\n"
|
2013-02-08 02:56:53 +08:00
|
|
|
" -u [num] number of clock updates in summary stats (0)\n"
|
2013-04-08 21:44:08 +08:00
|
|
|
" -n [num] domain number (0)\n"
|
2013-03-08 00:27:33 +08:00
|
|
|
" -x apply leap seconds by servo instead of kernel\n"
|
2014-07-08 22:14:18 +08:00
|
|
|
" -z [path] server address for UDS (/var/run/ptp4l)\n"
|
2013-03-28 09:46:00 +08:00
|
|
|
" -l [num] set the logging level to 'num' (6)\n"
|
2017-01-17 21:17:39 +08:00
|
|
|
" -t [tag] add tag to log messages\n"
|
2013-03-28 09:46:00 +08:00
|
|
|
" -m print messages to stdout\n"
|
|
|
|
" -q do not print messages to the syslog\n"
|
2012-12-10 17:28:28 +08:00
|
|
|
" -v prints the software version and exits\n"
|
2013-03-28 09:46:00 +08:00
|
|
|
" -h prints this message and exits\n"
|
2012-01-25 02:12:57 +08:00
|
|
|
"\n",
|
|
|
|
progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-04-16 03:13:49 +08:00
|
|
|
char *progname;
|
2014-06-12 03:35:16 +08:00
|
|
|
char *src_name = NULL, *dst_name = NULL;
|
|
|
|
struct clock *src, *dst;
|
2015-08-12 18:14:45 +08:00
|
|
|
struct config *cfg;
|
2014-06-12 03:35:22 +08:00
|
|
|
int autocfg = 0, rt = 0;
|
2014-06-12 03:35:13 +08:00
|
|
|
int c, domain_number = 0, pps_fd = -1;
|
2015-08-23 19:59:38 +08:00
|
|
|
int r = -1, wait_sync = 0;
|
2013-02-06 00:36:08 +08:00
|
|
|
int print_level = LOG_INFO, use_syslog = 1, verbose = 0;
|
2015-08-15 03:58:49 +08:00
|
|
|
int ntpshm_segment;
|
2015-08-12 18:14:45 +08:00
|
|
|
double phc_rate, tmp;
|
2014-06-12 03:35:13 +08:00
|
|
|
struct node node = {
|
|
|
|
.sanity_freq_limit = 200000000,
|
|
|
|
.servo_type = CLOCK_SERVO_PI,
|
|
|
|
.phc_readings = 5,
|
|
|
|
.phc_interval = 1.0,
|
2013-03-08 00:27:33 +08:00
|
|
|
.kernel_leap = 1,
|
2013-03-08 00:27:30 +08:00
|
|
|
};
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2014-07-08 22:14:21 +08:00
|
|
|
handle_term_signals();
|
|
|
|
|
2015-08-23 19:59:38 +08:00
|
|
|
cfg = phc2sys_config = config_create();
|
|
|
|
if (!cfg) {
|
2015-08-13 00:34:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:48:25 +08:00
|
|
|
config_set_double(cfg, "pi_proportional_const", KP);
|
2015-08-14 04:54:22 +08:00
|
|
|
config_set_double(cfg, "pi_integral_const", KI);
|
2012-01-25 02:12:57 +08:00
|
|
|
|
|
|
|
/* Process the command line arguments. */
|
|
|
|
progname = strrchr(argv[0], '/');
|
|
|
|
progname = progname ? 1+progname : argv[0];
|
2013-02-08 02:56:53 +08:00
|
|
|
while (EOF != (c = getopt(argc, argv,
|
2017-01-17 21:17:39 +08:00
|
|
|
"arc:d:s:E:P:I:S:F:R:N:O:L:M:i:u:wn:xz:l:t:mqvh"))) {
|
2012-01-25 02:12:57 +08:00
|
|
|
switch (c) {
|
2014-06-12 03:35:21 +08:00
|
|
|
case 'a':
|
|
|
|
autocfg = 1;
|
|
|
|
break;
|
2014-06-12 03:35:22 +08:00
|
|
|
case 'r':
|
|
|
|
rt++;
|
|
|
|
break;
|
2012-01-25 02:12:57 +08:00
|
|
|
case 'c':
|
2014-06-12 03:35:16 +08:00
|
|
|
dst_name = strdup(optarg);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
case 'd':
|
2013-02-06 00:36:07 +08:00
|
|
|
pps_fd = open(optarg, O_RDONLY);
|
|
|
|
if (pps_fd < 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"cannot open '%s': %m\n", optarg);
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2013-02-06 00:36:07 +08:00
|
|
|
}
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2013-05-15 06:12:11 +08:00
|
|
|
case 'i':
|
|
|
|
fprintf(stderr,
|
|
|
|
"'-i' has been deprecated. please use '-s' instead.\n");
|
2012-01-25 02:12:57 +08:00
|
|
|
case 's':
|
2014-06-12 03:35:16 +08:00
|
|
|
src_name = strdup(optarg);
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2014-03-14 01:34:18 +08:00
|
|
|
case 'E':
|
|
|
|
if (!strcasecmp(optarg, "pi")) {
|
2014-06-12 03:35:13 +08:00
|
|
|
node.servo_type = CLOCK_SERVO_PI;
|
2014-03-14 01:34:18 +08:00
|
|
|
} else if (!strcasecmp(optarg, "linreg")) {
|
2014-06-12 03:35:13 +08:00
|
|
|
node.servo_type = CLOCK_SERVO_LINREG;
|
2014-06-12 00:07:08 +08:00
|
|
|
} else if (!strcasecmp(optarg, "ntpshm")) {
|
|
|
|
node.servo_type = CLOCK_SERVO_NTPSHM;
|
2014-03-14 01:34:18 +08:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"invalid servo name %s\n", optarg);
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2014-03-14 01:34:18 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-01-25 02:12:57 +08:00
|
|
|
case 'P':
|
2015-08-23 19:59:38 +08:00
|
|
|
if (get_arg_val_d(c, optarg, &tmp, 0.0, DBL_MAX) ||
|
|
|
|
config_set_double(cfg, "pi_proportional_const", tmp))
|
|
|
|
goto end;
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
|
|
|
case 'I':
|
2015-08-23 19:59:38 +08:00
|
|
|
if (get_arg_val_d(c, optarg, &tmp, 0.0, DBL_MAX) ||
|
|
|
|
config_set_double(cfg, "pi_integral_const", tmp))
|
|
|
|
goto end;
|
2012-01-25 02:12:57 +08:00
|
|
|
break;
|
2013-02-06 00:36:04 +08:00
|
|
|
case 'S':
|
2015-08-23 19:59:38 +08:00
|
|
|
if (get_arg_val_d(c, optarg, &tmp, 0.0, DBL_MAX) ||
|
|
|
|
config_set_double(cfg, "step_threshold", tmp))
|
|
|
|
goto end;
|
2013-02-06 00:36:04 +08:00
|
|
|
break;
|
2013-06-20 12:30:15 +08:00
|
|
|
case 'F':
|
2015-08-23 19:59:38 +08:00
|
|
|
if (get_arg_val_d(c, optarg, &tmp, 0.0, DBL_MAX) ||
|
|
|
|
config_set_double(cfg, "first_step_threshold", tmp))
|
|
|
|
goto end;
|
2013-06-20 12:30:15 +08:00
|
|
|
break;
|
2012-08-31 01:44:38 +08:00
|
|
|
case 'R':
|
2013-09-04 11:04:37 +08:00
|
|
|
if (get_arg_val_d(c, optarg, &phc_rate, 1e-9, DBL_MAX))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2014-06-12 03:35:13 +08:00
|
|
|
node.phc_interval = 1.0 / phc_rate;
|
2012-08-31 01:44:38 +08:00
|
|
|
break;
|
|
|
|
case 'N':
|
2014-06-12 03:35:13 +08:00
|
|
|
if (get_arg_val_i(c, optarg, &node.phc_readings, 1, INT_MAX))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2012-08-31 01:44:38 +08:00
|
|
|
break;
|
2012-11-06 00:13:30 +08:00
|
|
|
case 'O':
|
2014-06-12 03:35:13 +08:00
|
|
|
if (get_arg_val_i(c, optarg, &node.sync_offset,
|
2013-06-04 13:01:04 +08:00
|
|
|
INT_MIN, INT_MAX))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2014-06-12 03:35:14 +08:00
|
|
|
node.forced_sync_offset = -1;
|
2012-11-06 00:13:30 +08:00
|
|
|
break;
|
2013-10-23 00:57:15 +08:00
|
|
|
case 'L':
|
2014-06-12 03:35:13 +08:00
|
|
|
if (get_arg_val_i(c, optarg, &node.sanity_freq_limit, 0, INT_MAX))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2013-10-23 00:57:15 +08:00
|
|
|
break;
|
2014-07-09 22:25:08 +08:00
|
|
|
case 'M':
|
2015-08-23 19:59:38 +08:00
|
|
|
if (get_arg_val_i(c, optarg, &ntpshm_segment, INT_MIN, INT_MAX) ||
|
|
|
|
config_set_int(cfg, "ntpshm_segment", ntpshm_segment))
|
|
|
|
goto end;
|
2014-07-09 22:25:08 +08:00
|
|
|
break;
|
2013-02-08 02:56:53 +08:00
|
|
|
case 'u':
|
2014-06-12 03:35:13 +08:00
|
|
|
if (get_arg_val_ui(c, optarg, &node.stats_max_count,
|
2013-06-04 13:01:04 +08:00
|
|
|
0, UINT_MAX))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2013-02-08 02:56:53 +08:00
|
|
|
break;
|
2013-01-30 01:06:18 +08:00
|
|
|
case 'w':
|
|
|
|
wait_sync = 1;
|
|
|
|
break;
|
2013-04-08 21:44:08 +08:00
|
|
|
case 'n':
|
2018-04-16 03:13:47 +08:00
|
|
|
if (get_arg_val_i(c, optarg, &domain_number, 0, 255) ||
|
|
|
|
config_set_int(cfg, "domainNumber", domain_number)) {
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2018-04-16 03:13:47 +08:00
|
|
|
}
|
2013-04-08 21:44:08 +08:00
|
|
|
break;
|
2013-03-08 00:27:33 +08:00
|
|
|
case 'x':
|
2014-06-12 03:35:13 +08:00
|
|
|
node.kernel_leap = 0;
|
2013-03-08 00:27:33 +08:00
|
|
|
break;
|
2014-07-08 22:14:18 +08:00
|
|
|
case 'z':
|
|
|
|
if (strlen(optarg) > MAX_IFNAME_SIZE) {
|
|
|
|
fprintf(stderr, "path %s too long, max is %d\n",
|
|
|
|
optarg, MAX_IFNAME_SIZE);
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2014-07-08 22:14:18 +08:00
|
|
|
}
|
2015-08-23 19:59:38 +08:00
|
|
|
if (config_set_string(cfg, "uds_address", optarg)) {
|
|
|
|
goto end;
|
2015-08-22 05:25:15 +08:00
|
|
|
}
|
2014-07-08 22:14:18 +08:00
|
|
|
break;
|
2013-02-06 00:36:08 +08:00
|
|
|
case 'l':
|
2013-06-04 13:01:04 +08:00
|
|
|
if (get_arg_val_i(c, optarg, &print_level,
|
2018-04-16 03:13:48 +08:00
|
|
|
PRINT_LEVEL_MIN, PRINT_LEVEL_MAX) ||
|
|
|
|
config_set_int(cfg, "logging_level", print_level)) {
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2018-04-16 03:13:48 +08:00
|
|
|
}
|
2013-02-06 00:36:08 +08:00
|
|
|
break;
|
2017-01-17 21:17:39 +08:00
|
|
|
case 't':
|
2018-04-16 03:13:49 +08:00
|
|
|
if (config_set_string(cfg, "message_tag", optarg)) {
|
|
|
|
goto end;
|
|
|
|
}
|
2017-01-17 21:17:39 +08:00
|
|
|
break;
|
2013-02-06 00:36:08 +08:00
|
|
|
case 'm':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
use_syslog = 0;
|
|
|
|
break;
|
2012-12-10 17:28:28 +08:00
|
|
|
case 'v':
|
|
|
|
version_show(stdout);
|
2015-08-23 19:59:38 +08:00
|
|
|
config_destroy(cfg);
|
2012-12-10 17:28:28 +08:00
|
|
|
return 0;
|
2012-01-25 02:12:57 +08:00
|
|
|
case 'h':
|
|
|
|
usage(progname);
|
2015-08-23 19:59:38 +08:00
|
|
|
config_destroy(cfg);
|
2012-01-25 02:12:57 +08:00
|
|
|
return 0;
|
|
|
|
default:
|
2013-06-17 22:54:02 +08:00
|
|
|
goto bad_usage;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:21 +08:00
|
|
|
if (autocfg && (src_name || dst_name || pps_fd >= 0 || wait_sync || node.forced_sync_offset)) {
|
2013-05-15 06:12:16 +08:00
|
|
|
fprintf(stderr,
|
2014-06-12 03:35:21 +08:00
|
|
|
"autoconfiguration cannot be mixed with manual config options.\n");
|
|
|
|
goto bad_usage;
|
|
|
|
}
|
|
|
|
if (!autocfg && pps_fd < 0 && !src_name) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"autoconfiguration or valid source clock must be selected.\n");
|
2013-06-17 22:54:02 +08:00
|
|
|
goto bad_usage;
|
2013-05-15 06:12:16 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:21 +08:00
|
|
|
if (!autocfg && !wait_sync && !node.forced_sync_offset) {
|
2013-05-15 06:12:16 +08:00
|
|
|
fprintf(stderr,
|
2014-06-12 03:35:16 +08:00
|
|
|
"time offset must be specified using -w or -O\n");
|
2013-06-17 22:54:02 +08:00
|
|
|
goto bad_usage;
|
2013-05-15 06:12:16 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 22:32:51 +08:00
|
|
|
if (node.servo_type == CLOCK_SERVO_NTPSHM) {
|
|
|
|
node.kernel_leap = 0;
|
|
|
|
node.sanity_freq_limit = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:16 +08:00
|
|
|
print_set_progname(progname);
|
2018-04-16 03:13:49 +08:00
|
|
|
print_set_tag(config_get_string(cfg, NULL, "message_tag"));
|
2014-06-12 03:35:16 +08:00
|
|
|
print_set_verbose(verbose);
|
|
|
|
print_set_syslog(use_syslog);
|
2018-04-16 03:13:48 +08:00
|
|
|
print_set_level(config_get_int(cfg, NULL, "logging_level"));
|
2014-06-12 03:35:16 +08:00
|
|
|
|
2014-06-12 03:35:21 +08:00
|
|
|
if (autocfg) {
|
2018-04-16 03:13:47 +08:00
|
|
|
if (init_pmc(cfg, &node))
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2014-06-12 03:35:22 +08:00
|
|
|
if (auto_init_ports(&node, rt) < 0)
|
2015-08-23 19:59:38 +08:00
|
|
|
goto end;
|
2014-07-08 22:14:21 +08:00
|
|
|
r = do_loop(&node, 1);
|
|
|
|
goto end;
|
2014-06-12 03:35:21 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 03:35:16 +08:00
|
|
|
src = clock_add(&node, src_name);
|
|
|
|
free(src_name);
|
2014-06-12 03:35:21 +08:00
|
|
|
if (!src) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"valid source clock must be selected.\n");
|
|
|
|
goto bad_usage;
|
|
|
|
}
|
|
|
|
src->state = PS_SLAVE;
|
2014-06-12 03:35:16 +08:00
|
|
|
node.master = src;
|
2014-06-12 03:35:21 +08:00
|
|
|
|
2014-06-12 03:35:16 +08:00
|
|
|
dst = clock_add(&node, dst_name ? dst_name : "CLOCK_REALTIME");
|
|
|
|
free(dst_name);
|
|
|
|
if (!dst) {
|
2013-05-15 06:12:16 +08:00
|
|
|
fprintf(stderr,
|
2014-06-12 03:35:16 +08:00
|
|
|
"valid destination clock must be selected.\n");
|
2013-06-17 22:54:02 +08:00
|
|
|
goto bad_usage;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|
2014-06-12 03:35:21 +08:00
|
|
|
dst->state = PS_MASTER;
|
2013-05-17 16:55:49 +08:00
|
|
|
|
2014-06-12 03:35:16 +08:00
|
|
|
if (pps_fd >= 0 && dst->clkid != CLOCK_REALTIME) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"cannot use a pps device unless destination is CLOCK_REALTIME\n");
|
|
|
|
goto bad_usage;
|
|
|
|
}
|
2014-06-12 03:35:13 +08:00
|
|
|
|
2014-07-08 22:14:21 +08:00
|
|
|
r = -1;
|
|
|
|
|
2013-01-30 01:06:18 +08:00
|
|
|
if (wait_sync) {
|
2018-04-16 03:13:47 +08:00
|
|
|
if (init_pmc(cfg, &node))
|
2014-07-08 22:14:21 +08:00
|
|
|
goto end;
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-07-08 22:14:21 +08:00
|
|
|
while (is_running()) {
|
2014-06-12 03:35:13 +08:00
|
|
|
r = run_pmc_wait_sync(&node, 1000);
|
2013-03-08 00:27:28 +08:00
|
|
|
if (r < 0)
|
2014-07-08 22:14:21 +08:00
|
|
|
goto end;
|
2014-06-12 03:35:11 +08:00
|
|
|
if (r > 0)
|
2013-03-08 00:27:28 +08:00
|
|
|
break;
|
|
|
|
else
|
|
|
|
pr_notice("Waiting for ptp4l...");
|
|
|
|
}
|
2013-01-30 01:06:18 +08:00
|
|
|
|
2014-06-12 03:35:14 +08:00
|
|
|
if (!node.forced_sync_offset) {
|
2014-06-12 03:35:13 +08:00
|
|
|
r = run_pmc_get_utc_offset(&node, 1000);
|
2014-06-12 03:35:11 +08:00
|
|
|
if (r <= 0) {
|
|
|
|
pr_err("failed to get UTC offset");
|
2014-07-08 22:14:21 +08:00
|
|
|
goto end;
|
2014-06-12 03:35:11 +08:00
|
|
|
}
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
2013-03-08 00:27:28 +08:00
|
|
|
|
2014-06-12 03:35:14 +08:00
|
|
|
if (node.forced_sync_offset ||
|
2014-06-12 03:35:16 +08:00
|
|
|
(src->clkid != CLOCK_REALTIME && dst->clkid != CLOCK_REALTIME) ||
|
|
|
|
src->clkid == CLOCK_INVALID)
|
2014-06-12 03:35:13 +08:00
|
|
|
close_pmc(&node);
|
2013-01-30 01:06:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-07 20:14:25 +08:00
|
|
|
if (pps_fd >= 0) {
|
2014-06-12 03:35:13 +08:00
|
|
|
/* only one destination clock allowed with PPS until we
|
|
|
|
* implement a mean to specify PTP port to PPS mapping */
|
2014-06-12 03:35:16 +08:00
|
|
|
servo_sync_interval(dst->servo, 1.0);
|
2014-07-08 22:14:21 +08:00
|
|
|
r = do_pps_loop(&node, dst, pps_fd);
|
|
|
|
} else {
|
|
|
|
r = do_loop(&node, 0);
|
2014-06-12 03:35:13 +08:00
|
|
|
}
|
2013-01-15 00:09:12 +08:00
|
|
|
|
2014-07-08 22:14:21 +08:00
|
|
|
end:
|
|
|
|
if (node.pmc)
|
|
|
|
close_pmc(&node);
|
2017-06-23 11:22:45 +08:00
|
|
|
clock_cleanup(&node);
|
2018-04-01 02:32:55 +08:00
|
|
|
port_cleanup(&node);
|
2015-08-23 19:59:38 +08:00
|
|
|
config_destroy(cfg);
|
2018-04-01 02:32:55 +08:00
|
|
|
msg_cleanup();
|
2014-07-08 22:14:21 +08:00
|
|
|
return r;
|
2013-06-17 22:54:02 +08:00
|
|
|
bad_usage:
|
|
|
|
usage(progname);
|
2015-08-23 19:59:38 +08:00
|
|
|
config_destroy(cfg);
|
2013-06-17 22:54:02 +08:00
|
|
|
return -1;
|
2012-01-25 02:12:57 +08:00
|
|
|
}
|