Blame phc2sys.c

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