2011-11-13 01:44:55 +08:00
|
|
|
/**
|
|
|
|
* @file clock.c
|
|
|
|
* @note Copyright (C) 2011 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>
|
2015-10-21 16:48:43 +08:00
|
|
|
#include <linux/net_tstamp.h>
|
2011-11-13 01:44:55 +08:00
|
|
|
#include <poll.h>
|
2011-11-26 16:26:59 +08:00
|
|
|
#include <stdlib.h>
|
2011-11-13 01:44:55 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2014-08-14 21:56:03 +08:00
|
|
|
#include <sys/queue.h>
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2014-05-07 00:41:46 +08:00
|
|
|
#include "address.h"
|
2011-11-13 01:44:55 +08:00
|
|
|
#include "bmc.h"
|
|
|
|
#include "clock.h"
|
2013-03-08 00:27:29 +08:00
|
|
|
#include "clockadj.h"
|
2013-10-23 00:57:15 +08:00
|
|
|
#include "clockcheck.h"
|
2011-11-13 01:44:55 +08:00
|
|
|
#include "foreign.h"
|
2013-10-29 18:58:18 +08:00
|
|
|
#include "filter.h"
|
2011-11-13 01:44:55 +08:00
|
|
|
#include "missing.h"
|
|
|
|
#include "msg.h"
|
|
|
|
#include "phc.h"
|
|
|
|
#include "port.h"
|
|
|
|
#include "servo.h"
|
2013-02-08 02:56:53 +08:00
|
|
|
#include "stats.h"
|
2011-11-13 01:44:55 +08:00
|
|
|
#include "print.h"
|
2017-10-09 22:31:46 +08:00
|
|
|
#include "rtnl.h"
|
2012-07-29 20:31:30 +08:00
|
|
|
#include "tlv.h"
|
2015-03-26 23:32:13 +08:00
|
|
|
#include "tsproc.h"
|
2012-08-24 21:30:02 +08:00
|
|
|
#include "uds.h"
|
2011-11-13 01:44:55 +08:00
|
|
|
#include "util.h"
|
|
|
|
|
2012-03-24 19:46:24 +08:00
|
|
|
#define N_CLOCK_PFD (N_POLLFD + 1) /* one extra per port, for the fault timer */
|
2012-09-04 13:37:10 +08:00
|
|
|
#define POW2_41 ((double)(1ULL << 41))
|
2011-11-27 21:19:29 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
struct port {
|
|
|
|
LIST_ENTRY(port) list;
|
|
|
|
};
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2012-08-18 01:16:32 +08:00
|
|
|
struct freq_estimator {
|
|
|
|
tmv_t origin1;
|
|
|
|
tmv_t ingress1;
|
2013-02-09 15:51:19 +08:00
|
|
|
unsigned int max_count;
|
|
|
|
unsigned int count;
|
2012-08-18 01:16:32 +08:00
|
|
|
};
|
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
struct clock_stats {
|
|
|
|
struct stats *offset;
|
|
|
|
struct stats *freq;
|
|
|
|
struct stats *delay;
|
2013-02-12 20:23:05 +08:00
|
|
|
unsigned int max_count;
|
2013-02-08 02:56:53 +08:00
|
|
|
};
|
|
|
|
|
2014-05-07 00:41:46 +08:00
|
|
|
struct clock_subscriber {
|
|
|
|
LIST_ENTRY(clock_subscriber) list;
|
|
|
|
uint8_t events[EVENT_BITMASK_CNT];
|
|
|
|
struct PortIdentity targetPortIdentity;
|
|
|
|
struct address addr;
|
|
|
|
UInteger16 sequenceId;
|
2014-05-07 00:41:47 +08:00
|
|
|
time_t expiration;
|
2014-05-07 00:41:46 +08:00
|
|
|
};
|
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
struct clock {
|
2015-10-18 22:26:49 +08:00
|
|
|
enum clock_type type;
|
2015-08-11 02:05:37 +08:00
|
|
|
struct config *config;
|
2011-11-13 01:44:55 +08:00
|
|
|
clockid_t clkid;
|
|
|
|
struct servo *servo;
|
2014-12-17 22:24:02 +08:00
|
|
|
enum servo_type servo_type;
|
2011-11-13 01:44:55 +08:00
|
|
|
struct defaultDS dds;
|
|
|
|
struct dataset default_dataset;
|
|
|
|
struct currentDS cur;
|
2012-12-01 05:35:26 +08:00
|
|
|
struct parent_ds dad;
|
2011-11-13 01:44:55 +08:00
|
|
|
struct timePropertiesDS tds;
|
2012-08-07 15:20:55 +08:00
|
|
|
struct ClockIdentity ptl[PATH_TRACE_MAX];
|
2011-11-13 01:44:55 +08:00
|
|
|
struct foreign_clock *best;
|
2012-09-12 23:06:34 +08:00
|
|
|
struct ClockIdentity best_id;
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_HEAD(ports_head, port) ports;
|
2014-08-14 21:56:02 +08:00
|
|
|
struct port *uds_port;
|
2014-08-14 21:56:03 +08:00
|
|
|
struct pollfd *pollfd;
|
2014-08-14 21:56:04 +08:00
|
|
|
int pollfd_valid;
|
2012-08-24 21:30:02 +08:00
|
|
|
int nports; /* does not include the UDS port */
|
2014-08-14 21:56:05 +08:00
|
|
|
int last_port_number;
|
2017-02-06 01:31:27 +08:00
|
|
|
int sde;
|
2012-12-01 04:52:37 +08:00
|
|
|
int free_running;
|
|
|
|
int freq_est_interval;
|
2013-12-02 03:32:05 +08:00
|
|
|
int grand_master_capable; /* for 802.1AS only */
|
2012-08-28 00:57:43 +08:00
|
|
|
int utc_timescale;
|
2014-06-18 21:44:49 +08:00
|
|
|
int utc_offset_set;
|
2013-03-08 00:27:32 +08:00
|
|
|
int leap_set;
|
2013-03-08 00:27:33 +08:00
|
|
|
int kernel_leap;
|
2017-07-09 03:29:23 +08:00
|
|
|
int utc_offset;
|
2013-07-06 16:37:19 +08:00
|
|
|
int time_flags; /* grand master role */
|
2013-07-06 02:43:45 +08:00
|
|
|
int time_source; /* grand master role */
|
2013-03-08 00:27:32 +08:00
|
|
|
enum servo_state servo_state;
|
2017-10-09 22:31:45 +08:00
|
|
|
enum timestamp_type timestamping;
|
2011-11-13 01:44:55 +08:00
|
|
|
tmv_t master_offset;
|
|
|
|
tmv_t path_delay;
|
2015-03-26 23:32:13 +08:00
|
|
|
tmv_t ingress_ts;
|
2018-02-15 19:48:39 +08:00
|
|
|
tmv_t initial_delay;
|
2015-03-26 23:32:13 +08:00
|
|
|
struct tsproc *tsproc;
|
2012-08-18 01:16:32 +08:00
|
|
|
struct freq_estimator fest;
|
2012-08-31 22:04:12 +08:00
|
|
|
struct time_status_np status;
|
2012-08-31 21:49:19 +08:00
|
|
|
double nrr;
|
2013-02-04 06:01:51 +08:00
|
|
|
struct clock_description desc;
|
2013-02-08 02:56:53 +08:00
|
|
|
struct clock_stats stats;
|
|
|
|
int stats_interval;
|
2013-10-23 00:57:15 +08:00
|
|
|
struct clockcheck *sanity_check;
|
2014-01-09 01:42:39 +08:00
|
|
|
struct interface uds_interface;
|
2014-05-07 00:41:46 +08:00
|
|
|
LIST_HEAD(clock_subscribers_head, clock_subscriber) subscribers;
|
2011-11-13 01:44:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct clock the_clock;
|
|
|
|
|
|
|
|
static void handle_state_decision_event(struct clock *c);
|
2014-08-14 21:56:03 +08:00
|
|
|
static int clock_resize_pollfd(struct clock *c, int new_nports);
|
|
|
|
static void clock_remove_port(struct clock *c, struct port *p);
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2012-07-29 20:31:30 +08:00
|
|
|
static int cid_eq(struct ClockIdentity *a, struct ClockIdentity *b)
|
|
|
|
{
|
|
|
|
return 0 == memcmp(a, b, sizeof(*a));
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:41:46 +08:00
|
|
|
#ifndef LIST_FOREACH_SAFE
|
|
|
|
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
|
|
|
|
for ((var) = LIST_FIRST((head)); \
|
|
|
|
(var) && ((tvar) = LIST_NEXT((var), field), 1); \
|
|
|
|
(var) = (tvar))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void remove_subscriber(struct clock_subscriber *s)
|
|
|
|
{
|
|
|
|
LIST_REMOVE(s, list);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_update_subscription(struct clock *c, struct ptp_message *req,
|
2014-05-07 00:41:47 +08:00
|
|
|
uint8_t *bitmask, uint16_t duration)
|
2014-05-07 00:41:46 +08:00
|
|
|
{
|
|
|
|
struct clock_subscriber *s;
|
|
|
|
int i, remove = 1;
|
2014-05-07 00:41:47 +08:00
|
|
|
struct timespec now;
|
2014-05-07 00:41:46 +08:00
|
|
|
|
|
|
|
for (i = 0; i < EVENT_BITMASK_CNT; i++) {
|
|
|
|
if (bitmask[i]) {
|
|
|
|
remove = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_FOREACH(s, &c->subscribers, list) {
|
|
|
|
if (!memcmp(&s->targetPortIdentity, &req->header.sourcePortIdentity,
|
|
|
|
sizeof(struct PortIdentity))) {
|
|
|
|
/* Found, update the transport address and event
|
|
|
|
* mask. */
|
|
|
|
if (!remove) {
|
|
|
|
s->addr = req->address;
|
|
|
|
memcpy(s->events, bitmask, EVENT_BITMASK_CNT);
|
2014-05-07 00:41:47 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
s->expiration = now.tv_sec + duration;
|
2014-05-07 00:41:46 +08:00
|
|
|
} else {
|
|
|
|
remove_subscriber(s);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (remove)
|
|
|
|
return;
|
|
|
|
/* Not present yet, add the subscriber. */
|
|
|
|
s = malloc(sizeof(*s));
|
|
|
|
if (!s) {
|
|
|
|
pr_err("failed to allocate memory for a subscriber");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s->targetPortIdentity = req->header.sourcePortIdentity;
|
|
|
|
s->addr = req->address;
|
|
|
|
memcpy(s->events, bitmask, EVENT_BITMASK_CNT);
|
2014-05-07 00:41:47 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
s->expiration = now.tv_sec + duration;
|
2014-05-07 00:41:46 +08:00
|
|
|
s->sequenceId = 0;
|
|
|
|
LIST_INSERT_HEAD(&c->subscribers, s, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_get_subscription(struct clock *c, struct ptp_message *req,
|
2014-05-07 00:41:47 +08:00
|
|
|
uint8_t *bitmask, uint16_t *duration)
|
2014-05-07 00:41:46 +08:00
|
|
|
{
|
|
|
|
struct clock_subscriber *s;
|
2014-05-07 00:41:47 +08:00
|
|
|
struct timespec now;
|
2014-05-07 00:41:46 +08:00
|
|
|
|
|
|
|
LIST_FOREACH(s, &c->subscribers, list) {
|
|
|
|
if (!memcmp(&s->targetPortIdentity, &req->header.sourcePortIdentity,
|
|
|
|
sizeof(struct PortIdentity))) {
|
|
|
|
memcpy(bitmask, s->events, EVENT_BITMASK_CNT);
|
2014-05-07 00:41:47 +08:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
if (s->expiration < now.tv_sec)
|
|
|
|
*duration = 0;
|
|
|
|
else
|
|
|
|
*duration = s->expiration - now.tv_sec;
|
2014-05-07 00:41:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* A client without entry means the client has no subscriptions. */
|
|
|
|
memset(bitmask, 0, EVENT_BITMASK_CNT);
|
2014-05-07 00:41:47 +08:00
|
|
|
*duration = 0;
|
2014-05-07 00:41:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_flush_subscriptions(struct clock *c)
|
|
|
|
{
|
|
|
|
struct clock_subscriber *s, *tmp;
|
|
|
|
|
|
|
|
LIST_FOREACH_SAFE(s, &c->subscribers, list, tmp) {
|
|
|
|
remove_subscriber(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:41:47 +08:00
|
|
|
static void clock_prune_subscriptions(struct clock *c)
|
|
|
|
{
|
|
|
|
struct clock_subscriber *s, *tmp;
|
|
|
|
struct timespec now;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
LIST_FOREACH_SAFE(s, &c->subscribers, list, tmp) {
|
|
|
|
if (s->expiration <= now.tv_sec) {
|
|
|
|
pr_info("subscriber %s timed out",
|
|
|
|
pid2str(&s->targetPortIdentity));
|
|
|
|
remove_subscriber(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:41:46 +08:00
|
|
|
void clock_send_notification(struct clock *c, struct ptp_message *msg,
|
|
|
|
int msglen, enum notification event)
|
|
|
|
{
|
|
|
|
unsigned int event_pos = event / 8;
|
|
|
|
uint8_t mask = 1 << (event % 8);
|
2014-08-14 21:56:02 +08:00
|
|
|
struct port *uds = c->uds_port;
|
2014-05-07 00:41:46 +08:00
|
|
|
struct clock_subscriber *s;
|
|
|
|
|
|
|
|
LIST_FOREACH(s, &c->subscribers, list) {
|
|
|
|
if (!(s->events[event_pos] & mask))
|
|
|
|
continue;
|
|
|
|
/* send event */
|
|
|
|
msg->header.sequenceId = htons(s->sequenceId);
|
|
|
|
s->sequenceId++;
|
|
|
|
msg->management.targetPortIdentity.clockIdentity =
|
|
|
|
s->targetPortIdentity.clockIdentity;
|
|
|
|
msg->management.targetPortIdentity.portNumber =
|
|
|
|
htons(s->targetPortIdentity.portNumber);
|
|
|
|
msg->address = s->addr;
|
|
|
|
port_forward_to(uds, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 03:09:59 +08:00
|
|
|
void clock_destroy(struct clock *c)
|
2011-11-13 01:44:55 +08:00
|
|
|
{
|
2014-08-14 21:56:03 +08:00
|
|
|
struct port *p, *tmp;
|
2014-05-07 00:41:46 +08:00
|
|
|
|
|
|
|
clock_flush_subscriptions(c);
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_FOREACH_SAFE(p, &c->ports, list, tmp) {
|
|
|
|
clock_remove_port(c, p);
|
|
|
|
}
|
2014-08-14 21:56:02 +08:00
|
|
|
port_close(c->uds_port);
|
2014-08-14 21:56:03 +08:00
|
|
|
free(c->pollfd);
|
2011-11-13 01:44:55 +08:00
|
|
|
if (c->clkid != CLOCK_REALTIME) {
|
|
|
|
phc_close(c->clkid);
|
|
|
|
}
|
2012-08-20 15:05:42 +08:00
|
|
|
servo_destroy(c->servo);
|
2015-03-26 23:32:13 +08:00
|
|
|
tsproc_destroy(c->tsproc);
|
2013-02-08 02:56:53 +08:00
|
|
|
stats_destroy(c->stats.offset);
|
|
|
|
stats_destroy(c->stats.freq);
|
|
|
|
stats_destroy(c->stats.delay);
|
2013-12-26 17:48:25 +08:00
|
|
|
if (c->sanity_check)
|
|
|
|
clockcheck_destroy(c->sanity_check);
|
2011-11-13 01:44:55 +08:00
|
|
|
memset(c, 0, sizeof(*c));
|
2012-08-28 03:09:10 +08:00
|
|
|
msg_cleanup();
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
static int clock_fault_timeout(struct port *port, int set)
|
2012-03-24 19:46:24 +08:00
|
|
|
{
|
2013-03-19 00:08:21 +08:00
|
|
|
struct fault_interval i;
|
2013-01-30 23:28:54 +08:00
|
|
|
|
2013-03-19 00:08:21 +08:00
|
|
|
if (!set) {
|
2014-08-14 21:56:03 +08:00
|
|
|
pr_debug("clearing fault on port %d", port_number(port));
|
|
|
|
return port_set_fault_timer_lin(port, 0);
|
2012-03-24 19:46:24 +08:00
|
|
|
}
|
2013-03-19 00:08:21 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
fault_interval(port, last_fault_type(port), &i);
|
2013-03-19 00:08:21 +08:00
|
|
|
|
|
|
|
if (i.type == FTMO_LINEAR_SECONDS) {
|
|
|
|
pr_debug("waiting %d seconds to clear fault on port %d",
|
2014-08-14 21:56:03 +08:00
|
|
|
i.val, port_number(port));
|
|
|
|
return port_set_fault_timer_lin(port, i.val);
|
2013-03-19 00:08:21 +08:00
|
|
|
} else if (i.type == FTMO_LOG2_SECONDS) {
|
|
|
|
pr_debug("waiting 2^{%d} seconds to clear fault on port %d",
|
2014-08-14 21:56:03 +08:00
|
|
|
i.val, port_number(port));
|
|
|
|
return port_set_fault_timer_log(port, 1, i.val);
|
2013-03-19 00:08:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pr_err("Unsupported fault interval type %d", i.type);
|
|
|
|
return -1;
|
2012-03-24 19:46:24 +08:00
|
|
|
}
|
|
|
|
|
2012-09-02 20:30:50 +08:00
|
|
|
static void clock_freq_est_reset(struct clock *c)
|
|
|
|
{
|
|
|
|
c->fest.origin1 = tmv_zero();
|
|
|
|
c->fest.ingress1 = tmv_zero();
|
|
|
|
c->fest.count = 0;
|
2016-07-30 21:48:46 +08:00
|
|
|
}
|
2012-09-02 20:30:50 +08:00
|
|
|
|
2013-02-05 02:05:37 +08:00
|
|
|
static void clock_management_send_error(struct port *p,
|
|
|
|
struct ptp_message *msg, int error_id)
|
|
|
|
{
|
|
|
|
if (port_management_error(port_identity(p), p, msg, error_id))
|
|
|
|
pr_err("failed to send management error status");
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:41:49 +08:00
|
|
|
/* The 'p' and 'req' paremeters are needed for the GET actions that operate
|
|
|
|
* on per-client datasets. If such actions do not apply to the caller, it is
|
|
|
|
* allowed to pass both of them as NULL.
|
|
|
|
*/
|
|
|
|
static int clock_management_fill_response(struct clock *c, struct port *p,
|
|
|
|
struct ptp_message *req,
|
|
|
|
struct ptp_message *rsp, int id)
|
2012-08-22 13:31:21 +08:00
|
|
|
{
|
2013-07-06 16:37:19 +08:00
|
|
|
struct grandmaster_settings_np *gsn;
|
2018-02-22 07:14:30 +08:00
|
|
|
struct management_tlv_datum *mtd;
|
2014-05-07 00:41:46 +08:00
|
|
|
struct subscribe_events_np *sen;
|
2018-02-22 07:14:30 +08:00
|
|
|
struct management_tlv *tlv;
|
|
|
|
struct time_status_np *tsn;
|
2013-02-25 07:56:37 +08:00
|
|
|
struct PTPText *text;
|
2018-02-18 11:58:50 +08:00
|
|
|
int datalen = 0;
|
2012-08-22 13:31:21 +08:00
|
|
|
|
|
|
|
tlv = (struct management_tlv *) rsp->management.suffix;
|
|
|
|
tlv->type = TLV_MANAGEMENT;
|
|
|
|
tlv->id = id;
|
|
|
|
|
|
|
|
switch (id) {
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_USER_DESCRIPTION:
|
2013-02-25 07:56:37 +08:00
|
|
|
text = (struct PTPText *) tlv->data;
|
|
|
|
text->length = c->desc.userDescription.length;
|
|
|
|
memcpy(text->text, c->desc.userDescription.text, text->length);
|
|
|
|
datalen = 1 + text->length;
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_DEFAULT_DATA_SET:
|
2012-12-01 15:32:40 +08:00
|
|
|
memcpy(tlv->data, &c->dds, sizeof(c->dds));
|
|
|
|
datalen = sizeof(c->dds);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_CURRENT_DATA_SET:
|
2012-08-22 13:31:21 +08:00
|
|
|
memcpy(tlv->data, &c->cur, sizeof(c->cur));
|
|
|
|
datalen = sizeof(c->cur);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_PARENT_DATA_SET:
|
2012-12-02 16:13:55 +08:00
|
|
|
memcpy(tlv->data, &c->dad.pds, sizeof(c->dad.pds));
|
|
|
|
datalen = sizeof(c->dad.pds);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_TIME_PROPERTIES_DATA_SET:
|
2012-12-03 01:57:38 +08:00
|
|
|
memcpy(tlv->data, &c->tds, sizeof(c->tds));
|
|
|
|
datalen = sizeof(c->tds);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_PRIORITY1:
|
2013-03-04 02:41:06 +08:00
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
mtd->val = c->dds.priority1;
|
|
|
|
datalen = sizeof(*mtd);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_PRIORITY2:
|
2013-03-04 02:48:43 +08:00
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
mtd->val = c->dds.priority2;
|
|
|
|
datalen = sizeof(*mtd);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_DOMAIN:
|
2013-03-04 02:54:17 +08:00
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
mtd->val = c->dds.domainNumber;
|
|
|
|
datalen = sizeof(*mtd);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_SLAVE_ONLY:
|
2013-03-04 03:06:17 +08:00
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
mtd->val = c->dds.flags & DDS_SLAVE_ONLY;
|
|
|
|
datalen = sizeof(*mtd);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_CLOCK_ACCURACY:
|
2013-03-04 03:10:46 +08:00
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
mtd->val = c->dds.clockQuality.clockAccuracy;
|
|
|
|
datalen = sizeof(*mtd);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_TRACEABILITY_PROPERTIES:
|
2013-03-04 03:20:12 +08:00
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
mtd->val = c->tds.flags & (TIME_TRACEABLE|FREQ_TRACEABLE);
|
|
|
|
datalen = sizeof(*mtd);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_TIMESCALE_PROPERTIES:
|
2013-03-04 03:26:00 +08:00
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
mtd->val = c->tds.flags & PTP_TIMESCALE;
|
|
|
|
datalen = sizeof(*mtd);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_TIME_STATUS_NP:
|
2012-09-12 23:44:20 +08:00
|
|
|
tsn = (struct time_status_np *) tlv->data;
|
|
|
|
tsn->master_offset = c->master_offset;
|
2015-03-26 23:32:13 +08:00
|
|
|
tsn->ingress_time = tmv_to_nanoseconds(c->ingress_ts);
|
2012-09-12 23:44:20 +08:00
|
|
|
tsn->cumulativeScaledRateOffset =
|
2014-02-21 18:08:33 +08:00
|
|
|
(Integer32) (c->status.cumulativeScaledRateOffset +
|
2012-09-12 23:44:20 +08:00
|
|
|
c->nrr * POW2_41 - POW2_41);
|
|
|
|
tsn->scaledLastGmPhaseChange = c->status.scaledLastGmPhaseChange;
|
|
|
|
tsn->gmTimeBaseIndicator = c->status.gmTimeBaseIndicator;
|
|
|
|
tsn->lastGmPhaseChange = c->status.lastGmPhaseChange;
|
2012-12-01 05:35:26 +08:00
|
|
|
if (cid_eq(&c->dad.pds.grandmasterIdentity, &c->dds.clockIdentity))
|
2012-09-12 23:44:20 +08:00
|
|
|
tsn->gmPresent = 0;
|
|
|
|
else
|
|
|
|
tsn->gmPresent = 1;
|
2012-12-01 05:35:26 +08:00
|
|
|
tsn->gmIdentity = c->dad.pds.grandmasterIdentity;
|
2012-09-12 23:44:20 +08:00
|
|
|
datalen = sizeof(*tsn);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_GRANDMASTER_SETTINGS_NP:
|
2013-07-06 16:37:19 +08:00
|
|
|
gsn = (struct grandmaster_settings_np *) tlv->data;
|
|
|
|
gsn->clockQuality = c->dds.clockQuality;
|
|
|
|
gsn->utc_offset = c->utc_offset;
|
|
|
|
gsn->time_flags = c->time_flags;
|
|
|
|
gsn->time_source = c->time_source;
|
|
|
|
datalen = sizeof(*gsn);
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_SUBSCRIBE_EVENTS_NP:
|
2014-08-14 21:56:02 +08:00
|
|
|
if (p != c->uds_port) {
|
2014-05-07 00:41:46 +08:00
|
|
|
/* Only the UDS port allowed. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sen = (struct subscribe_events_np *)tlv->data;
|
2014-05-07 00:41:47 +08:00
|
|
|
clock_get_subscription(c, req, sen->bitmask, &sen->duration);
|
2014-05-07 00:41:46 +08:00
|
|
|
break;
|
2018-02-18 11:58:50 +08:00
|
|
|
default:
|
|
|
|
/* The caller should *not* respond to this message. */
|
|
|
|
return 0;
|
2012-08-22 13:31:21 +08:00
|
|
|
}
|
2018-02-18 11:58:50 +08:00
|
|
|
if (datalen % 2) {
|
|
|
|
tlv->data[datalen] = 0;
|
|
|
|
datalen++;
|
2012-08-22 13:31:21 +08:00
|
|
|
}
|
2018-02-18 11:58:50 +08:00
|
|
|
tlv->length = sizeof(tlv->id) + datalen;
|
|
|
|
rsp->header.messageLength += sizeof(*tlv) + datalen;
|
|
|
|
rsp->tlv_count = 1;
|
|
|
|
|
|
|
|
/* The caller can respond to this message. */
|
|
|
|
return 1;
|
2014-05-07 00:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int clock_management_get_response(struct clock *c, struct port *p,
|
|
|
|
int id, struct ptp_message *req)
|
|
|
|
{
|
|
|
|
struct PortIdentity pid = port_identity(p);
|
|
|
|
struct ptp_message *rsp;
|
|
|
|
int respond;
|
|
|
|
|
|
|
|
rsp = port_management_reply(pid, p, req);
|
|
|
|
if (!rsp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
respond = clock_management_fill_response(c, p, req, rsp, id);
|
|
|
|
if (respond)
|
|
|
|
port_prepare_and_send(p, rsp, 0);
|
2012-08-22 13:31:21 +08:00
|
|
|
msg_put(rsp);
|
2014-05-07 00:41:49 +08:00
|
|
|
return respond;
|
2012-08-22 13:31:21 +08:00
|
|
|
}
|
|
|
|
|
2013-02-07 00:23:03 +08:00
|
|
|
static int clock_management_set(struct clock *c, struct port *p,
|
2013-07-08 03:27:32 +08:00
|
|
|
int id, struct ptp_message *req, int *changed)
|
2013-02-07 00:23:03 +08:00
|
|
|
{
|
|
|
|
int respond = 0;
|
2013-07-06 19:04:29 +08:00
|
|
|
struct management_tlv *tlv;
|
2015-03-18 02:50:39 +08:00
|
|
|
struct management_tlv_datum *mtd;
|
2013-07-06 19:04:29 +08:00
|
|
|
struct grandmaster_settings_np *gsn;
|
2014-05-07 00:41:46 +08:00
|
|
|
struct subscribe_events_np *sen;
|
2013-07-06 19:04:29 +08:00
|
|
|
|
|
|
|
tlv = (struct management_tlv *) req->management.suffix;
|
|
|
|
|
2013-02-07 00:23:03 +08:00
|
|
|
switch (id) {
|
2015-03-18 02:50:39 +08:00
|
|
|
case TLV_PRIORITY1:
|
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
c->dds.priority1 = mtd->val;
|
|
|
|
*changed = 1;
|
|
|
|
respond = 1;
|
|
|
|
break;
|
|
|
|
case TLV_PRIORITY2:
|
|
|
|
mtd = (struct management_tlv_datum *) tlv->data;
|
|
|
|
c->dds.priority2 = mtd->val;
|
|
|
|
*changed = 1;
|
|
|
|
respond = 1;
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_GRANDMASTER_SETTINGS_NP:
|
2013-07-06 19:04:29 +08:00
|
|
|
gsn = (struct grandmaster_settings_np *) tlv->data;
|
|
|
|
c->dds.clockQuality = gsn->clockQuality;
|
|
|
|
c->utc_offset = gsn->utc_offset;
|
|
|
|
c->time_flags = gsn->time_flags;
|
|
|
|
c->time_source = gsn->time_source;
|
2013-07-08 03:27:32 +08:00
|
|
|
*changed = 1;
|
2013-07-06 19:04:29 +08:00
|
|
|
respond = 1;
|
|
|
|
break;
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_SUBSCRIBE_EVENTS_NP:
|
2014-05-07 00:41:46 +08:00
|
|
|
sen = (struct subscribe_events_np *)tlv->data;
|
2014-05-07 00:41:47 +08:00
|
|
|
clock_update_subscription(c, req, sen->bitmask,
|
|
|
|
sen->duration);
|
2014-05-07 00:41:46 +08:00
|
|
|
respond = 1;
|
|
|
|
break;
|
2013-02-07 00:23:03 +08:00
|
|
|
}
|
|
|
|
if (respond && !clock_management_get_response(c, p, id, req))
|
|
|
|
pr_err("failed to send management set response");
|
|
|
|
return respond ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
static void clock_stats_update(struct clock_stats *s,
|
2018-03-02 02:12:19 +08:00
|
|
|
double offset, double freq)
|
2013-02-08 02:56:53 +08:00
|
|
|
{
|
|
|
|
struct stats_result offset_stats, freq_stats, delay_stats;
|
|
|
|
|
|
|
|
stats_add_value(s->offset, offset);
|
|
|
|
stats_add_value(s->freq, freq);
|
|
|
|
|
|
|
|
if (stats_get_num_values(s->offset) < s->max_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stats_get_result(s->offset, &offset_stats);
|
|
|
|
stats_get_result(s->freq, &freq_stats);
|
|
|
|
|
|
|
|
/* Path delay stats are updated separately, they may be empty. */
|
|
|
|
if (!stats_get_result(s->delay, &delay_stats)) {
|
|
|
|
pr_info("rms %4.0f max %4.0f "
|
|
|
|
"freq %+6.0f +/- %3.0f "
|
|
|
|
"delay %5.0f +/- %3.0f",
|
|
|
|
offset_stats.rms, offset_stats.max_abs,
|
|
|
|
freq_stats.mean, freq_stats.stddev,
|
|
|
|
delay_stats.mean, delay_stats.stddev);
|
|
|
|
} else {
|
|
|
|
pr_info("rms %4.0f max %4.0f "
|
|
|
|
"freq %+6.0f +/- %3.0f",
|
|
|
|
offset_stats.rms, offset_stats.max_abs,
|
|
|
|
freq_stats.mean, freq_stats.stddev);
|
|
|
|
}
|
|
|
|
|
|
|
|
stats_reset(s->offset);
|
|
|
|
stats_reset(s->freq);
|
|
|
|
stats_reset(s->delay);
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
static enum servo_state clock_no_adjust(struct clock *c, tmv_t ingress,
|
|
|
|
tmv_t origin)
|
2012-08-18 01:16:32 +08:00
|
|
|
{
|
2012-08-31 22:08:14 +08:00
|
|
|
double fui;
|
2013-02-08 02:56:51 +08:00
|
|
|
double ratio, freq;
|
2012-08-18 01:16:32 +08:00
|
|
|
struct freq_estimator *f = &c->fest;
|
|
|
|
enum servo_state state = SERVO_UNLOCKED;
|
|
|
|
/*
|
|
|
|
* The ratio of the local clock freqency to the master clock
|
|
|
|
* is estimated by:
|
|
|
|
*
|
|
|
|
* (ingress_2 - ingress_1) / (origin_2 - origin_1)
|
|
|
|
*
|
|
|
|
* Both of the origin time estimates include the path delay,
|
|
|
|
* but we assume that the path delay is in fact constant.
|
|
|
|
* By leaving out the path delay altogther, we can avoid the
|
|
|
|
* error caused by our imperfect path delay measurement.
|
|
|
|
*/
|
2012-09-02 20:33:44 +08:00
|
|
|
if (!f->ingress1) {
|
2015-03-26 23:32:13 +08:00
|
|
|
f->ingress1 = ingress;
|
|
|
|
f->origin1 = origin;
|
2012-09-02 20:33:44 +08:00
|
|
|
return state;
|
|
|
|
}
|
2012-08-18 01:16:32 +08:00
|
|
|
f->count++;
|
|
|
|
if (f->count < f->max_count) {
|
|
|
|
return state;
|
|
|
|
}
|
2015-03-26 23:32:13 +08:00
|
|
|
if (tmv_eq(ingress, f->ingress1)) {
|
2012-09-04 13:51:59 +08:00
|
|
|
pr_warning("bad timestamps in rate ratio calculation");
|
|
|
|
return state;
|
|
|
|
}
|
2012-09-02 20:33:44 +08:00
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
ratio = tmv_dbl(tmv_sub(origin, f->origin1)) /
|
|
|
|
tmv_dbl(tmv_sub(ingress, f->ingress1));
|
2013-02-08 02:56:51 +08:00
|
|
|
freq = (1.0 - ratio) * 1e9;
|
2012-09-02 20:33:44 +08:00
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
if (c->stats.max_count > 1) {
|
2018-03-02 02:12:19 +08:00
|
|
|
clock_stats_update(&c->stats, tmv_dbl(c->master_offset), freq);
|
2013-02-08 02:56:53 +08:00
|
|
|
} else {
|
|
|
|
pr_info("master offset %10" PRId64 " s%d freq %+7.0f "
|
|
|
|
"path delay %9" PRId64,
|
2013-03-08 00:27:31 +08:00
|
|
|
tmv_to_nanoseconds(c->master_offset), state, freq,
|
|
|
|
tmv_to_nanoseconds(c->path_delay));
|
2013-02-08 02:56:53 +08:00
|
|
|
}
|
2012-09-02 20:33:44 +08:00
|
|
|
|
2012-09-04 13:37:10 +08:00
|
|
|
fui = 1.0 + (c->status.cumulativeScaledRateOffset + 0.0) / POW2_41;
|
2012-09-02 20:33:44 +08:00
|
|
|
|
|
|
|
pr_debug("peer/local %.9f", c->nrr);
|
|
|
|
pr_debug("fup_info %.9f", fui);
|
|
|
|
pr_debug("product %.9f", fui * c->nrr);
|
|
|
|
pr_debug("sum-1 %.9f", fui + c->nrr - 1.0);
|
|
|
|
pr_debug("master/local %.9f", ratio);
|
|
|
|
pr_debug("diff %+.9f", ratio - (fui + c->nrr - 1.0));
|
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
f->ingress1 = ingress;
|
|
|
|
f->origin1 = origin;
|
2012-08-18 01:16:32 +08:00
|
|
|
f->count = 0;
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
static void clock_update_grandmaster(struct clock *c)
|
|
|
|
{
|
2012-12-01 05:35:26 +08:00
|
|
|
struct parentDS *pds = &c->dad.pds;
|
2011-11-13 01:44:55 +08:00
|
|
|
memset(&c->cur, 0, sizeof(c->cur));
|
2012-08-07 15:20:55 +08:00
|
|
|
memset(c->ptl, 0, sizeof(c->ptl));
|
2012-12-01 05:35:26 +08:00
|
|
|
pds->parentPortIdentity.clockIdentity = c->dds.clockIdentity;
|
|
|
|
pds->parentPortIdentity.portNumber = 0;
|
|
|
|
pds->grandmasterIdentity = c->dds.clockIdentity;
|
|
|
|
pds->grandmasterClockQuality = c->dds.clockQuality;
|
|
|
|
pds->grandmasterPriority1 = c->dds.priority1;
|
|
|
|
pds->grandmasterPriority2 = c->dds.priority2;
|
2012-08-07 15:20:55 +08:00
|
|
|
c->dad.path_length = 0;
|
2013-07-06 16:37:19 +08:00
|
|
|
c->tds.currentUtcOffset = c->utc_offset;
|
|
|
|
c->tds.flags = c->time_flags;
|
2013-07-06 02:43:45 +08:00
|
|
|
c->tds.timeSource = c->time_source;
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_update_slave(struct clock *c)
|
|
|
|
{
|
2012-12-01 05:35:26 +08:00
|
|
|
struct parentDS *pds = &c->dad.pds;
|
2011-11-13 01:44:55 +08:00
|
|
|
struct ptp_message *msg = TAILQ_FIRST(&c->best->messages);
|
|
|
|
c->cur.stepsRemoved = 1 + c->best->dataset.stepsRemoved;
|
2012-12-01 05:35:26 +08:00
|
|
|
pds->parentPortIdentity = c->best->dataset.sender;
|
|
|
|
pds->grandmasterIdentity = msg->announce.grandmasterIdentity;
|
|
|
|
pds->grandmasterClockQuality = msg->announce.grandmasterClockQuality;
|
|
|
|
pds->grandmasterPriority1 = msg->announce.grandmasterPriority1;
|
|
|
|
pds->grandmasterPriority2 = msg->announce.grandmasterPriority2;
|
2011-11-13 01:44:55 +08:00
|
|
|
c->tds.currentUtcOffset = msg->announce.currentUtcOffset;
|
2012-12-02 18:24:31 +08:00
|
|
|
c->tds.flags = msg->header.flagField[1];
|
2011-11-13 01:44:55 +08:00
|
|
|
c->tds.timeSource = msg->announce.timeSource;
|
2012-12-02 18:24:31 +08:00
|
|
|
if (!(c->tds.flags & PTP_TIMESCALE)) {
|
2012-08-27 02:36:43 +08:00
|
|
|
pr_warning("foreign master not using PTP timescale");
|
|
|
|
}
|
2017-07-09 03:29:23 +08:00
|
|
|
if (c->tds.currentUtcOffset < c->utc_offset) {
|
2012-08-27 02:36:43 +08:00
|
|
|
pr_warning("running in a temporal vortex");
|
|
|
|
}
|
2017-07-23 04:30:10 +08:00
|
|
|
if ((c->tds.flags & UTC_OFF_VALID && c->tds.flags & TIME_TRACEABLE) ||
|
|
|
|
(c->tds.currentUtcOffset > c->utc_offset)) {
|
|
|
|
pr_info("updating UTC offset to %d", c->tds.currentUtcOffset);
|
|
|
|
c->utc_offset = c->tds.currentUtcOffset;
|
|
|
|
}
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
|
2013-03-08 00:27:32 +08:00
|
|
|
static int clock_utc_correct(struct clock *c, tmv_t ingress)
|
2012-08-28 00:57:43 +08:00
|
|
|
{
|
|
|
|
struct timespec offset;
|
2013-03-08 00:27:32 +08:00
|
|
|
int utc_offset, leap, clock_leap;
|
|
|
|
uint64_t ts;
|
|
|
|
|
2012-08-28 00:57:43 +08:00
|
|
|
if (!c->utc_timescale)
|
2013-03-08 00:27:32 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-07-24 14:21:34 +08:00
|
|
|
utc_offset = c->utc_offset;
|
2013-03-08 00:27:32 +08:00
|
|
|
|
|
|
|
if (c->tds.flags & LEAP_61) {
|
|
|
|
leap = 1;
|
|
|
|
} else if (c->tds.flags & LEAP_59) {
|
|
|
|
leap = -1;
|
2012-08-28 00:57:43 +08:00
|
|
|
} else {
|
2013-03-08 00:27:32 +08:00
|
|
|
leap = 0;
|
2012-08-28 00:57:43 +08:00
|
|
|
}
|
2013-03-08 00:27:32 +08:00
|
|
|
|
|
|
|
/* Handle leap seconds. */
|
|
|
|
if ((leap || c->leap_set) && c->clkid == CLOCK_REALTIME) {
|
|
|
|
/* If the clock will be stepped, the time stamp has to be the
|
|
|
|
target time. Ignore possible 1 second error in utc_offset. */
|
|
|
|
if (c->servo_state == SERVO_UNLOCKED) {
|
|
|
|
ts = tmv_to_nanoseconds(tmv_sub(ingress,
|
|
|
|
c->master_offset));
|
|
|
|
if (c->tds.flags & PTP_TIMESCALE)
|
|
|
|
ts -= utc_offset * NS_PER_SEC;
|
|
|
|
} else {
|
|
|
|
ts = tmv_to_nanoseconds(ingress);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_leap = leap_second_status(ts, c->leap_set,
|
|
|
|
&leap, &utc_offset);
|
|
|
|
if (c->leap_set != clock_leap) {
|
2013-03-08 00:27:33 +08:00
|
|
|
if (c->kernel_leap)
|
2013-04-08 21:44:09 +08:00
|
|
|
sysclk_set_leap(clock_leap);
|
2014-06-20 22:32:48 +08:00
|
|
|
else
|
|
|
|
servo_leap(c->servo, clock_leap);
|
2013-03-08 00:27:32 +08:00
|
|
|
c->leap_set = clock_leap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 21:44:49 +08:00
|
|
|
/* Update TAI-UTC offset of the system clock if valid and traceable. */
|
|
|
|
if (c->tds.flags & UTC_OFF_VALID && c->tds.flags & TIME_TRACEABLE &&
|
|
|
|
c->utc_offset_set != utc_offset && c->clkid == CLOCK_REALTIME) {
|
|
|
|
sysclk_set_tai_offset(utc_offset);
|
|
|
|
c->utc_offset_set = utc_offset;
|
|
|
|
}
|
|
|
|
|
2013-03-08 00:27:32 +08:00
|
|
|
if (!(c->tds.flags & PTP_TIMESCALE))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
offset.tv_sec = utc_offset;
|
2012-08-28 00:57:43 +08:00
|
|
|
offset.tv_nsec = 0;
|
|
|
|
/* Local clock is UTC, but master is TAI. */
|
|
|
|
c->master_offset = tmv_add(c->master_offset, timespec_to_tmv(offset));
|
2013-03-08 00:27:32 +08:00
|
|
|
return 0;
|
2012-08-28 00:57:43 +08:00
|
|
|
}
|
|
|
|
|
2012-08-24 21:30:02 +08:00
|
|
|
static int forwarding(struct clock *c, struct port *p)
|
2012-07-29 20:31:30 +08:00
|
|
|
{
|
|
|
|
enum port_state ps = port_state(p);
|
|
|
|
switch (ps) {
|
|
|
|
case PS_MASTER:
|
|
|
|
case PS_GRAND_MASTER:
|
|
|
|
case PS_SLAVE:
|
|
|
|
case PS_UNCALIBRATED:
|
|
|
|
case PS_PRE_MASTER:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-14 21:56:02 +08:00
|
|
|
if (p == c->uds_port && ps != PS_FAULTY) {
|
2012-08-24 21:30:02 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2012-07-29 20:31:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
/* public methods */
|
|
|
|
|
|
|
|
UInteger8 clock_class(struct clock *c)
|
|
|
|
{
|
|
|
|
return c->dds.clockQuality.clockClass;
|
|
|
|
}
|
|
|
|
|
2015-08-11 02:10:08 +08:00
|
|
|
struct config *clock_config(struct clock *c)
|
|
|
|
{
|
|
|
|
return c->config;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
static int clock_add_port(struct clock *c, int phc_index,
|
2014-08-14 21:56:05 +08:00
|
|
|
enum timestamp_type timestamping,
|
2014-08-14 21:56:03 +08:00
|
|
|
struct interface *iface)
|
|
|
|
{
|
2014-11-05 19:52:38 +08:00
|
|
|
struct port *p, *piter, *lastp = NULL;
|
2014-08-14 21:56:03 +08:00
|
|
|
|
2016-07-29 21:39:25 +08:00
|
|
|
if (clock_resize_pollfd(c, c->nports + 1)) {
|
2014-08-14 21:56:03 +08:00
|
|
|
return -1;
|
2016-07-29 21:39:25 +08:00
|
|
|
}
|
|
|
|
p = port_open(phc_index, timestamping, ++c->last_port_number, iface, c);
|
2014-08-14 21:56:03 +08:00
|
|
|
if (!p) {
|
|
|
|
/* No need to shrink pollfd */
|
|
|
|
return -1;
|
|
|
|
}
|
2016-07-29 21:39:25 +08:00
|
|
|
LIST_FOREACH(piter, &c->ports, list) {
|
2014-11-05 19:52:38 +08:00
|
|
|
lastp = piter;
|
2016-07-29 21:39:25 +08:00
|
|
|
}
|
|
|
|
if (lastp) {
|
2014-11-05 19:52:38 +08:00
|
|
|
LIST_INSERT_AFTER(lastp, p, list);
|
2016-07-29 21:39:25 +08:00
|
|
|
} else {
|
2014-11-05 19:52:38 +08:00
|
|
|
LIST_INSERT_HEAD(&c->ports, p, list);
|
2016-07-29 21:39:25 +08:00
|
|
|
}
|
2014-08-14 21:56:03 +08:00
|
|
|
c->nports++;
|
|
|
|
clock_fda_changed(c);
|
2016-07-30 04:33:04 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_remove_port(struct clock *c, struct port *p)
|
|
|
|
{
|
|
|
|
/* Do not call clock_resize_pollfd, it's pointless to shrink
|
|
|
|
* the allocated memory at this point, clock_destroy will free
|
|
|
|
* it all anyway. This function is usable from other parts of
|
|
|
|
* the code, but even then we don't mind if pollfd is larger
|
|
|
|
* than necessary. */
|
|
|
|
LIST_REMOVE(p, list);
|
|
|
|
c->nports--;
|
|
|
|
clock_fda_changed(c);
|
|
|
|
port_close(p);
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:31:45 +08:00
|
|
|
int clock_required_modes(struct clock *c)
|
|
|
|
{
|
|
|
|
int required_modes = 0;
|
|
|
|
|
|
|
|
switch (c->timestamping) {
|
|
|
|
case TS_SOFTWARE:
|
|
|
|
required_modes |= SOF_TIMESTAMPING_TX_SOFTWARE |
|
|
|
|
SOF_TIMESTAMPING_RX_SOFTWARE |
|
|
|
|
SOF_TIMESTAMPING_SOFTWARE;
|
|
|
|
break;
|
|
|
|
case TS_LEGACY_HW:
|
|
|
|
required_modes |= SOF_TIMESTAMPING_TX_HARDWARE |
|
|
|
|
SOF_TIMESTAMPING_RX_HARDWARE |
|
|
|
|
SOF_TIMESTAMPING_SYS_HARDWARE;
|
|
|
|
break;
|
|
|
|
case TS_HARDWARE:
|
|
|
|
case TS_ONESTEP:
|
|
|
|
required_modes |= SOF_TIMESTAMPING_TX_HARDWARE |
|
|
|
|
SOF_TIMESTAMPING_RX_HARDWARE |
|
|
|
|
SOF_TIMESTAMPING_RAW_HARDWARE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return required_modes;
|
|
|
|
}
|
|
|
|
|
2017-10-09 22:31:46 +08:00
|
|
|
/*
|
|
|
|
* If we do not have a slave or the rtnl query failed, then use our
|
|
|
|
* own interface name as the time stamping interface name.
|
|
|
|
*/
|
|
|
|
static void ensure_ts_label(struct interface *iface)
|
|
|
|
{
|
|
|
|
if (iface->ts_label[0] == '\0')
|
|
|
|
strncpy(iface->ts_label, iface->name, MAX_IFNAME_SIZE);
|
|
|
|
}
|
|
|
|
|
2015-10-18 22:26:49 +08:00
|
|
|
struct clock *clock_create(enum clock_type type, struct config *config,
|
2015-10-21 16:52:25 +08:00
|
|
|
const char *phc_device)
|
2011-11-13 01:44:55 +08:00
|
|
|
{
|
2015-08-16 03:45:19 +08:00
|
|
|
enum timestamp_type timestamping =
|
|
|
|
config_get_int(config, NULL, "time_stamping");
|
2014-11-06 06:20:51 +08:00
|
|
|
int fadj = 0, max_adj = 0, sw_ts = timestamping == TS_SOFTWARE ? 1 : 0;
|
2015-08-16 03:56:59 +08:00
|
|
|
enum servo_type servo = config_get_int(config, NULL, "clock_servo");
|
2015-10-21 16:52:25 +08:00
|
|
|
int phc_index, required_modes = 0;
|
2011-11-13 01:44:55 +08:00
|
|
|
struct clock *c = &the_clock;
|
2014-08-14 21:56:03 +08:00
|
|
|
struct port *p;
|
2015-10-18 23:19:43 +08:00
|
|
|
unsigned char oui[OUI_LEN];
|
|
|
|
char phc[32], *tmp;
|
2015-10-21 16:52:25 +08:00
|
|
|
struct interface *iface, *udsif = &c->uds_interface;
|
2013-10-25 20:45:35 +08:00
|
|
|
struct timespec ts;
|
2015-08-15 20:37:24 +08:00
|
|
|
int sfl;
|
2012-08-24 21:30:02 +08:00
|
|
|
|
2013-10-25 20:45:35 +08:00
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
srandom(ts.tv_sec ^ ts.tv_nsec);
|
2011-11-26 16:26:59 +08:00
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
if (c->nports)
|
|
|
|
clock_destroy(c);
|
|
|
|
|
2015-10-18 22:26:49 +08:00
|
|
|
switch (type) {
|
|
|
|
case CLOCK_TYPE_ORDINARY:
|
|
|
|
case CLOCK_TYPE_BOUNDARY:
|
|
|
|
c->type = type;
|
|
|
|
break;
|
|
|
|
case CLOCK_TYPE_P2P:
|
|
|
|
case CLOCK_TYPE_E2E:
|
|
|
|
case CLOCK_TYPE_MANAGEMENT:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-18 23:19:43 +08:00
|
|
|
/* Initialize the defaultDS. */
|
|
|
|
c->dds.clockQuality.clockClass =
|
|
|
|
config_get_int(config, NULL, "clockClass");
|
|
|
|
c->dds.clockQuality.clockAccuracy =
|
|
|
|
config_get_int(config, NULL, "clockAccuracy");
|
|
|
|
c->dds.clockQuality.offsetScaledLogVariance =
|
|
|
|
config_get_int(config, NULL, "offsetScaledLogVariance");
|
|
|
|
|
|
|
|
c->desc.productDescription.max_symbols = 64;
|
|
|
|
c->desc.revisionData.max_symbols = 32;
|
|
|
|
c->desc.userDescription.max_symbols = 128;
|
|
|
|
|
|
|
|
tmp = config_get_string(config, NULL, "productDescription");
|
|
|
|
if (count_char(tmp, ';') != 2 ||
|
|
|
|
static_ptp_text_set(&c->desc.productDescription, tmp)) {
|
|
|
|
pr_err("invalid productDescription '%s'", tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tmp = config_get_string(config, NULL, "revisionData");
|
|
|
|
if (count_char(tmp, ';') != 2 ||
|
|
|
|
static_ptp_text_set(&c->desc.revisionData, tmp)) {
|
|
|
|
pr_err("invalid revisionData '%s'", tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tmp = config_get_string(config, NULL, "userDescription");
|
|
|
|
if (static_ptp_text_set(&c->desc.userDescription, tmp)) {
|
|
|
|
pr_err("invalid userDescription '%s'", tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tmp = config_get_string(config, NULL, "manufacturerIdentity");
|
|
|
|
if (OUI_LEN != sscanf(tmp, "%hhx:%hhx:%hhx", &oui[0], &oui[1], &oui[2])) {
|
|
|
|
pr_err("invalid manufacturerIdentity '%s'", tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(c->desc.manufacturerIdentity, oui, OUI_LEN);
|
|
|
|
|
|
|
|
c->dds.domainNumber = config_get_int(config, NULL, "domainNumber");
|
|
|
|
|
|
|
|
if (config_get_int(config, NULL, "slaveOnly")) {
|
|
|
|
c->dds.flags |= DDS_SLAVE_ONLY;
|
|
|
|
}
|
|
|
|
if (config_get_int(config, NULL, "twoStepFlag")) {
|
|
|
|
c->dds.flags |= DDS_TWO_STEP_FLAG;
|
|
|
|
}
|
|
|
|
c->dds.priority1 = config_get_int(config, NULL, "priority1");
|
|
|
|
c->dds.priority2 = config_get_int(config, NULL, "priority2");
|
|
|
|
|
|
|
|
if (!config_get_int(config, NULL, "gmCapable") &&
|
|
|
|
c->dds.flags & DDS_SLAVE_ONLY) {
|
|
|
|
pr_err("Cannot mix 1588 slaveOnly with 802.1AS !gmCapable");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!config_get_int(config, NULL, "gmCapable") ||
|
|
|
|
c->dds.flags & DDS_SLAVE_ONLY) {
|
|
|
|
c->dds.clockQuality.clockClass = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(c->dds.flags & DDS_TWO_STEP_FLAG)) {
|
2016-10-16 18:14:08 +08:00
|
|
|
switch (timestamping) {
|
2015-10-18 23:19:43 +08:00
|
|
|
case TS_SOFTWARE:
|
|
|
|
case TS_LEGACY_HW:
|
|
|
|
pr_err("one step is only possible "
|
|
|
|
"with hardware time stamping");
|
|
|
|
return NULL;
|
|
|
|
case TS_HARDWARE:
|
2016-10-16 17:55:14 +08:00
|
|
|
timestamping = TS_ONESTEP;
|
2015-10-18 23:19:43 +08:00
|
|
|
if (config_set_int(config, "time_stamping", TS_ONESTEP))
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case TS_ONESTEP:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-21 16:48:43 +08:00
|
|
|
/* Check the time stamping mode on each interface. */
|
2017-10-09 22:31:45 +08:00
|
|
|
c->timestamping = timestamping;
|
|
|
|
required_modes = clock_required_modes(c);
|
2015-10-21 16:48:43 +08:00
|
|
|
STAILQ_FOREACH(iface, &config->interfaces, list) {
|
2017-10-09 22:31:46 +08:00
|
|
|
rtnl_get_ts_label(iface);
|
|
|
|
ensure_ts_label(iface);
|
|
|
|
sk_get_ts_info(iface->ts_label, &iface->ts_info);
|
2015-10-21 16:48:43 +08:00
|
|
|
if (iface->ts_info.valid &&
|
|
|
|
((iface->ts_info.so_timestamping & required_modes) != required_modes)) {
|
|
|
|
pr_err("interface '%s' does not support "
|
|
|
|
"requested timestamping mode", iface->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iface = STAILQ_FIRST(&config->interfaces);
|
2015-10-21 16:52:25 +08:00
|
|
|
|
|
|
|
/* determine PHC Clock index */
|
|
|
|
if (config_get_int(config, NULL, "free_running")) {
|
|
|
|
phc_index = -1;
|
2016-10-16 18:14:08 +08:00
|
|
|
} else if (timestamping == TS_SOFTWARE || timestamping == TS_LEGACY_HW) {
|
2015-10-21 16:52:25 +08:00
|
|
|
phc_index = -1;
|
|
|
|
} else if (phc_device) {
|
|
|
|
if (1 != sscanf(phc_device, "/dev/ptp%d", &phc_index)) {
|
|
|
|
pr_err("bad ptp device string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else if (iface->ts_info.valid) {
|
|
|
|
phc_index = iface->ts_info.phc_index;
|
|
|
|
} else {
|
|
|
|
pr_err("PTP device not specified and automatic determination"
|
|
|
|
" is not supported. Please specify PTP device.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (phc_index >= 0) {
|
|
|
|
pr_info("selected /dev/ptp%d as PTP clock", phc_index);
|
|
|
|
}
|
|
|
|
|
2015-10-18 23:19:43 +08:00
|
|
|
if (generate_clock_identity(&c->dds.clockIdentity, iface->name)) {
|
|
|
|
pr_err("failed to generate a clock identity");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the UDS. */
|
2015-08-22 05:25:15 +08:00
|
|
|
snprintf(udsif->name, sizeof(udsif->name), "%s",
|
|
|
|
config_get_string(config, NULL, "uds_address"));
|
2016-07-05 20:48:59 +08:00
|
|
|
if (config_set_section_int(config, udsif->name,
|
|
|
|
"announceReceiptTimeout", 0)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-09-06 03:27:04 +08:00
|
|
|
if (config_set_section_int(config, udsif->name,
|
|
|
|
"delay_mechanism", DM_AUTO)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-16 02:52:09 +08:00
|
|
|
if (config_set_section_int(config, udsif->name,
|
|
|
|
"network_transport", TRANS_UDS)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-16 18:59:57 +08:00
|
|
|
if (config_set_section_int(config, udsif->name, "delay_filter_length", 1)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-09 01:42:39 +08:00
|
|
|
|
2015-08-11 02:05:37 +08:00
|
|
|
c->config = config;
|
2015-08-15 20:11:17 +08:00
|
|
|
c->free_running = config_get_int(config, NULL, "free_running");
|
2015-08-15 19:08:39 +08:00
|
|
|
c->freq_est_interval = config_get_int(config, NULL, "freq_est_interval");
|
2015-08-15 20:19:11 +08:00
|
|
|
c->grand_master_capable = config_get_int(config, NULL, "gmCapable");
|
2015-08-15 20:29:04 +08:00
|
|
|
c->kernel_leap = config_get_int(config, NULL, "kernel_leap");
|
2017-07-09 03:29:23 +08:00
|
|
|
c->utc_offset = config_get_int(config, NULL, "utc_offset");
|
2015-08-15 20:41:01 +08:00
|
|
|
c->time_source = config_get_int(config, NULL, "timeSource");
|
2012-12-01 04:52:37 +08:00
|
|
|
|
|
|
|
if (c->free_running) {
|
2012-09-05 19:40:21 +08:00
|
|
|
c->clkid = CLOCK_INVALID;
|
2013-04-05 20:32:24 +08:00
|
|
|
if (timestamping == TS_SOFTWARE || timestamping == TS_LEGACY_HW) {
|
|
|
|
c->utc_timescale = 1;
|
|
|
|
}
|
2012-09-05 19:40:21 +08:00
|
|
|
} else if (phc_index >= 0) {
|
2017-05-22 03:17:03 +08:00
|
|
|
snprintf(phc, sizeof(phc), "/dev/ptp%d", phc_index);
|
2011-11-13 01:44:55 +08:00
|
|
|
c->clkid = phc_open(phc);
|
|
|
|
if (c->clkid == CLOCK_INVALID) {
|
|
|
|
pr_err("Failed to open %s: %m", phc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
max_adj = phc_max_adj(c->clkid);
|
|
|
|
if (!max_adj) {
|
|
|
|
pr_err("clock is not adjustable");
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-12-20 01:39:11 +08:00
|
|
|
clockadj_init(c->clkid);
|
2011-11-13 01:44:55 +08:00
|
|
|
} else {
|
|
|
|
c->clkid = CLOCK_REALTIME;
|
2012-08-28 00:57:43 +08:00
|
|
|
c->utc_timescale = 1;
|
2013-12-20 01:39:11 +08:00
|
|
|
clockadj_init(c->clkid);
|
2013-04-05 15:47:21 +08:00
|
|
|
max_adj = sysclk_max_freq();
|
2013-04-08 21:44:09 +08:00
|
|
|
sysclk_set_leap(0);
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
2014-06-18 21:44:49 +08:00
|
|
|
c->utc_offset_set = 0;
|
2013-03-08 00:27:32 +08:00
|
|
|
c->leap_set = 0;
|
2013-07-06 16:37:19 +08:00
|
|
|
c->time_flags = c->utc_timescale ? 0 : PTP_TIMESCALE;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2012-09-21 15:27:53 +08:00
|
|
|
if (c->clkid != CLOCK_INVALID) {
|
2013-03-08 00:27:29 +08:00
|
|
|
fadj = (int) clockadj_get_freq(c->clkid);
|
2013-04-04 23:28:21 +08:00
|
|
|
/* Due to a bug in older kernels, the reading may silently fail
|
|
|
|
and return 0. Set the frequency back to make sure fadj is
|
|
|
|
the actual frequency of the clock. */
|
|
|
|
clockadj_set_freq(c->clkid, fadj);
|
2012-09-21 15:27:53 +08:00
|
|
|
}
|
2015-08-12 04:18:20 +08:00
|
|
|
c->servo = servo_create(c->config, servo, -fadj, max_adj, sw_ts);
|
2011-11-13 01:44:55 +08:00
|
|
|
if (!c->servo) {
|
|
|
|
pr_err("Failed to create clock servo");
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-03-08 00:27:32 +08:00
|
|
|
c->servo_state = SERVO_UNLOCKED;
|
2014-12-17 22:24:02 +08:00
|
|
|
c->servo_type = servo;
|
2015-08-16 03:20:19 +08:00
|
|
|
c->tsproc = tsproc_create(config_get_int(config, NULL, "tsproc_mode"),
|
2015-08-16 03:25:47 +08:00
|
|
|
config_get_int(config, NULL, "delay_filter"),
|
2015-08-16 18:59:57 +08:00
|
|
|
config_get_int(config, NULL, "delay_filter_length"));
|
2015-03-26 23:32:13 +08:00
|
|
|
if (!c->tsproc) {
|
|
|
|
pr_err("Failed to create time stamp processor");
|
2011-11-27 21:19:29 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-15 19:48:39 +08:00
|
|
|
c->initial_delay = dbl_tmv(config_get_int(config, NULL, "initial_delay"));
|
2014-02-21 18:08:34 +08:00
|
|
|
c->nrr = 1.0;
|
2015-08-15 20:24:33 +08:00
|
|
|
c->stats_interval = config_get_int(config, NULL, "summary_interval");
|
2013-02-08 02:56:53 +08:00
|
|
|
c->stats.offset = stats_create();
|
|
|
|
c->stats.freq = stats_create();
|
|
|
|
c->stats.delay = stats_create();
|
|
|
|
if (!c->stats.offset || !c->stats.freq || !c->stats.delay) {
|
|
|
|
pr_err("failed to create stats");
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-15 20:37:24 +08:00
|
|
|
sfl = config_get_int(config, NULL, "sanity_freq_limit");
|
|
|
|
if (sfl) {
|
|
|
|
c->sanity_check = clockcheck_create(sfl);
|
2013-10-23 00:57:15 +08:00
|
|
|
if (!c->sanity_check) {
|
|
|
|
pr_err("Failed to create clock sanity check");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-11-13 01:44:55 +08:00
|
|
|
|
|
|
|
/* Initialize the parentDS. */
|
2012-02-27 01:03:16 +08:00
|
|
|
clock_update_grandmaster(c);
|
2012-12-01 05:35:26 +08:00
|
|
|
c->dad.pds.parentStats = 0;
|
|
|
|
c->dad.pds.observedParentOffsetScaledLogVariance = 0xffff;
|
|
|
|
c->dad.pds.observedParentClockPhaseChangeRate = 0x7fffffff;
|
2012-08-07 15:20:55 +08:00
|
|
|
c->dad.ptl = c->ptl;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2013-02-08 02:56:50 +08:00
|
|
|
clock_sync_interval(c, 0);
|
2012-03-24 19:46:24 +08:00
|
|
|
|
2014-05-07 00:41:46 +08:00
|
|
|
LIST_INIT(&c->subscribers);
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_INIT(&c->ports);
|
2014-08-14 21:56:05 +08:00
|
|
|
c->last_port_number = 0;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
if (clock_resize_pollfd(c, 0)) {
|
|
|
|
pr_err("failed to allocate pollfd");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-08-31 04:44:55 +08:00
|
|
|
|
|
|
|
/* Create the UDS interface. */
|
2014-08-14 21:56:02 +08:00
|
|
|
c->uds_port = port_open(phc_index, timestamping, 0, udsif, c);
|
|
|
|
if (!c->uds_port) {
|
2012-08-24 21:30:02 +08:00
|
|
|
pr_err("failed to open the UDS port");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-14 21:56:03 +08:00
|
|
|
clock_fda_changed(c);
|
2012-08-24 21:30:02 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
/* Create the ports. */
|
2015-10-19 04:10:05 +08:00
|
|
|
STAILQ_FOREACH(iface, &config->interfaces, list) {
|
2014-08-14 21:56:06 +08:00
|
|
|
if (clock_add_port(c, phc_index, timestamping, iface)) {
|
|
|
|
pr_err("failed to open port %s", iface->name);
|
2014-08-14 21:56:03 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
c->dds.numberPorts = c->nports;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_FOREACH(p, &c->ports, list) {
|
|
|
|
port_dispatch(p, EV_INITIALIZE, 0);
|
|
|
|
}
|
2014-08-14 21:56:02 +08:00
|
|
|
port_dispatch(c->uds_port, EV_INITIALIZE, 0);
|
2017-07-10 15:39:30 +08:00
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dataset *clock_best_foreign(struct clock *c)
|
|
|
|
{
|
|
|
|
return c->best ? &c->best->dataset : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct port *clock_best_port(struct clock *c)
|
|
|
|
{
|
|
|
|
return c->best ? c->best->port : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dataset *clock_default_ds(struct clock *c)
|
|
|
|
{
|
|
|
|
struct dataset *out = &c->default_dataset;
|
|
|
|
struct defaultDS *in = &c->dds;
|
|
|
|
|
|
|
|
out->priority1 = in->priority1;
|
|
|
|
out->identity = in->clockIdentity;
|
|
|
|
out->quality = in->clockQuality;
|
|
|
|
out->priority2 = in->priority2;
|
|
|
|
out->stepsRemoved = 0;
|
|
|
|
out->sender.clockIdentity = in->clockIdentity;
|
|
|
|
out->sender.portNumber = 0;
|
|
|
|
out->receiver.clockIdentity = in->clockIdentity;
|
|
|
|
out->receiver.portNumber = 0;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
UInteger8 clock_domain_number(struct clock *c)
|
|
|
|
{
|
|
|
|
return c->dds.domainNumber;
|
|
|
|
}
|
|
|
|
|
2015-11-01 03:44:15 +08:00
|
|
|
struct port *clock_first_port(struct clock *c)
|
|
|
|
{
|
|
|
|
return LIST_FIRST(&c->ports);
|
|
|
|
}
|
|
|
|
|
2012-08-31 22:04:12 +08:00
|
|
|
void clock_follow_up_info(struct clock *c, struct follow_up_info_tlv *f)
|
|
|
|
{
|
|
|
|
c->status.cumulativeScaledRateOffset = f->cumulativeScaledRateOffset;
|
|
|
|
c->status.scaledLastGmPhaseChange = f->scaledLastGmPhaseChange;
|
|
|
|
c->status.gmTimeBaseIndicator = f->gmTimeBaseIndicator;
|
|
|
|
memcpy(&c->status.lastGmPhaseChange, &f->lastGmPhaseChange,
|
|
|
|
sizeof(c->status.lastGmPhaseChange));
|
|
|
|
}
|
|
|
|
|
2013-12-02 03:32:05 +08:00
|
|
|
int clock_gm_capable(struct clock *c)
|
|
|
|
{
|
|
|
|
return c->grand_master_capable;
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
struct ClockIdentity clock_identity(struct clock *c)
|
|
|
|
{
|
|
|
|
return c->dds.clockIdentity;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
static int clock_resize_pollfd(struct clock *c, int new_nports)
|
|
|
|
{
|
|
|
|
struct pollfd *new_pollfd;
|
|
|
|
|
2017-07-10 15:39:30 +08:00
|
|
|
/* Need to allocate one whole extra block of fds for UDS. */
|
2014-08-14 21:56:03 +08:00
|
|
|
new_pollfd = realloc(c->pollfd,
|
2017-07-10 15:39:30 +08:00
|
|
|
(new_nports + 1) * N_CLOCK_PFD *
|
2014-08-14 21:56:03 +08:00
|
|
|
sizeof(struct pollfd));
|
|
|
|
if (!new_pollfd)
|
|
|
|
return -1;
|
|
|
|
c->pollfd = new_pollfd;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_fill_pollfd(struct pollfd *dest, struct port *p)
|
2011-11-13 01:44:55 +08:00
|
|
|
{
|
2014-08-14 21:56:03 +08:00
|
|
|
struct fdarray *fda;
|
|
|
|
int i;
|
2014-08-14 21:56:02 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
fda = port_fda(p);
|
|
|
|
for (i = 0; i < N_POLLFD; i++) {
|
|
|
|
dest[i].fd = fda->fd[i];
|
|
|
|
dest[i].events = POLLIN|POLLPRI;
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
2014-08-14 21:56:03 +08:00
|
|
|
dest[i].fd = port_fault_fd(p);
|
|
|
|
dest[i].events = POLLIN|POLLPRI;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:04 +08:00
|
|
|
static void clock_check_pollfd(struct clock *c)
|
2014-08-14 21:56:03 +08:00
|
|
|
{
|
|
|
|
struct port *p;
|
2017-07-10 15:39:30 +08:00
|
|
|
struct pollfd *dest = c->pollfd;
|
2014-08-14 21:56:03 +08:00
|
|
|
|
2014-08-14 21:56:04 +08:00
|
|
|
if (c->pollfd_valid)
|
|
|
|
return;
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_FOREACH(p, &c->ports, list) {
|
|
|
|
clock_fill_pollfd(dest, p);
|
|
|
|
dest += N_CLOCK_PFD;
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
2014-08-14 21:56:03 +08:00
|
|
|
clock_fill_pollfd(dest, c->uds_port);
|
2014-08-14 21:56:04 +08:00
|
|
|
c->pollfd_valid = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clock_fda_changed(struct clock *c)
|
|
|
|
{
|
|
|
|
c->pollfd_valid = 0;
|
2012-07-29 20:31:30 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:02 +08:00
|
|
|
static int clock_do_forward_mgmt(struct clock *c,
|
|
|
|
struct port *in, struct port *out,
|
|
|
|
struct ptp_message *msg, int *pre_sent)
|
|
|
|
{
|
|
|
|
if (in == out || !forwarding(c, out))
|
|
|
|
return 0;
|
2017-12-11 21:21:25 +08:00
|
|
|
|
|
|
|
/* Don't forward any requests to the UDS port. */
|
|
|
|
if (out == c->uds_port) {
|
|
|
|
switch (management_action(msg)) {
|
|
|
|
case GET:
|
|
|
|
case SET:
|
|
|
|
case COMMAND:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:02 +08:00
|
|
|
if (!*pre_sent) {
|
|
|
|
/* delay calling msg_pre_send until
|
|
|
|
* actually forwarding */
|
|
|
|
msg_pre_send(msg);
|
|
|
|
*pre_sent = 1;
|
|
|
|
}
|
|
|
|
return port_forward(out, msg);
|
|
|
|
}
|
|
|
|
|
2013-01-01 04:12:54 +08:00
|
|
|
static void clock_forward_mgmt_msg(struct clock *c, struct port *p, struct ptp_message *msg)
|
2012-07-29 20:31:30 +08:00
|
|
|
{
|
2014-08-14 21:56:03 +08:00
|
|
|
struct port *piter;
|
|
|
|
int pdulen = 0, msg_ready = 0;
|
2014-08-14 21:56:02 +08:00
|
|
|
|
2013-01-01 04:12:54 +08:00
|
|
|
if (forwarding(c, p) && msg->management.boundaryHops) {
|
2014-08-14 21:56:02 +08:00
|
|
|
pdulen = msg->header.messageLength;
|
|
|
|
msg->management.boundaryHops--;
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_FOREACH(piter, &c->ports, list) {
|
|
|
|
if (clock_do_forward_mgmt(c, p, piter, msg, &msg_ready))
|
|
|
|
pr_err("port %d: management forward failed",
|
|
|
|
port_number(piter));
|
|
|
|
}
|
2014-08-14 21:56:02 +08:00
|
|
|
if (clock_do_forward_mgmt(c, p, c->uds_port, msg, &msg_ready))
|
|
|
|
pr_err("uds port: management forward failed");
|
2013-01-01 04:12:54 +08:00
|
|
|
if (msg_ready) {
|
|
|
|
msg_post_recv(msg, pdulen);
|
|
|
|
msg->management.boundaryHops++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-08 03:27:32 +08:00
|
|
|
int clock_manage(struct clock *c, struct port *p, struct ptp_message *msg)
|
2013-01-01 04:12:54 +08:00
|
|
|
{
|
2014-08-14 21:56:03 +08:00
|
|
|
int changed = 0, res, answers;
|
|
|
|
struct port *piter;
|
2012-07-29 20:31:30 +08:00
|
|
|
struct management_tlv *mgt;
|
|
|
|
struct ClockIdentity *tcid, wildcard = {
|
|
|
|
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Forward this message out all eligible ports. */
|
2013-01-01 04:12:54 +08:00
|
|
|
clock_forward_mgmt_msg(c, p, msg);
|
2012-07-29 20:31:30 +08:00
|
|
|
|
|
|
|
/* Apply this message to the local clock and ports. */
|
|
|
|
tcid = &msg->management.targetPortIdentity.clockIdentity;
|
|
|
|
if (!cid_eq(tcid, &wildcard) && !cid_eq(tcid, &c->dds.clockIdentity)) {
|
2013-07-08 03:27:32 +08:00
|
|
|
return changed;
|
2012-07-29 20:31:30 +08:00
|
|
|
}
|
|
|
|
if (msg->tlv_count != 1) {
|
2013-07-08 03:27:32 +08:00
|
|
|
return changed;
|
2012-07-29 20:31:30 +08:00
|
|
|
}
|
|
|
|
mgt = (struct management_tlv *) msg->management.suffix;
|
2012-08-22 13:31:21 +08:00
|
|
|
|
2013-02-07 00:24:35 +08:00
|
|
|
/*
|
|
|
|
The correct length according to the management ID is checked
|
|
|
|
in tlv.c, but management TLVs with empty bodies are also
|
|
|
|
received successfully to support GETs and CMDs. At this
|
|
|
|
point the TLV either has the correct length or length 2.
|
|
|
|
*/
|
2012-11-30 18:39:21 +08:00
|
|
|
switch (management_action(msg)) {
|
|
|
|
case GET:
|
2012-12-04 20:25:50 +08:00
|
|
|
if (clock_management_get_response(c, p, mgt->id, msg))
|
2013-07-08 03:27:32 +08:00
|
|
|
return changed;
|
2012-12-04 20:25:50 +08:00
|
|
|
break;
|
2012-11-30 18:39:21 +08:00
|
|
|
case SET:
|
2014-06-25 18:45:23 +08:00
|
|
|
if (mgt->length == 2 && mgt->id != TLV_NULL_MANAGEMENT) {
|
|
|
|
clock_management_send_error(p, msg, TLV_WRONG_LENGTH);
|
2013-07-08 03:27:32 +08:00
|
|
|
return changed;
|
2013-02-07 00:24:35 +08:00
|
|
|
}
|
2014-08-14 21:56:02 +08:00
|
|
|
if (p != c->uds_port) {
|
2014-02-21 23:50:13 +08:00
|
|
|
/* Sorry, only allowed on the UDS port. */
|
2014-06-25 18:45:23 +08:00
|
|
|
clock_management_send_error(p, msg, TLV_NOT_SUPPORTED);
|
2014-02-21 23:50:13 +08:00
|
|
|
return changed;
|
|
|
|
}
|
2013-07-08 03:27:32 +08:00
|
|
|
if (clock_management_set(c, p, mgt->id, msg, &changed))
|
|
|
|
return changed;
|
2013-02-07 00:23:03 +08:00
|
|
|
break;
|
2012-11-30 18:39:21 +08:00
|
|
|
case COMMAND:
|
|
|
|
break;
|
2013-02-05 02:05:40 +08:00
|
|
|
default:
|
2013-07-08 03:27:32 +08:00
|
|
|
return changed;
|
2012-11-30 18:39:21 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 00:41:51 +08:00
|
|
|
switch (mgt->id) {
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_PORT_PROPERTIES_NP:
|
2014-08-14 21:56:02 +08:00
|
|
|
if (p != c->uds_port) {
|
2014-05-07 00:41:51 +08:00
|
|
|
/* Only the UDS port allowed. */
|
2014-06-25 18:45:23 +08:00
|
|
|
clock_management_send_error(p, msg, TLV_NOT_SUPPORTED);
|
2014-05-07 00:41:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-29 20:31:30 +08:00
|
|
|
switch (mgt->id) {
|
2014-06-25 18:45:23 +08:00
|
|
|
case TLV_USER_DESCRIPTION:
|
|
|
|
case TLV_SAVE_IN_NON_VOLATILE_STORAGE:
|
|
|
|
case TLV_RESET_NON_VOLATILE_STORAGE:
|
|
|
|
case TLV_INITIALIZE:
|
|
|
|
case TLV_FAULT_LOG:
|
|
|
|
case TLV_FAULT_LOG_RESET:
|
|
|
|
case TLV_DEFAULT_DATA_SET:
|
|
|
|
case TLV_CURRENT_DATA_SET:
|
|
|
|
case TLV_PARENT_DATA_SET:
|
|
|
|
case TLV_TIME_PROPERTIES_DATA_SET:
|
|
|
|
case TLV_PRIORITY1:
|
|
|
|
case TLV_PRIORITY2:
|
|
|
|
case TLV_DOMAIN:
|
|
|
|
case TLV_SLAVE_ONLY:
|
|
|
|
case TLV_TIME:
|
|
|
|
case TLV_CLOCK_ACCURACY:
|
|
|
|
case TLV_UTC_PROPERTIES:
|
|
|
|
case TLV_TRACEABILITY_PROPERTIES:
|
|
|
|
case TLV_TIMESCALE_PROPERTIES:
|
|
|
|
case TLV_PATH_TRACE_LIST:
|
|
|
|
case TLV_PATH_TRACE_ENABLE:
|
|
|
|
case TLV_GRANDMASTER_CLUSTER_TABLE:
|
|
|
|
case TLV_ACCEPTABLE_MASTER_TABLE:
|
|
|
|
case TLV_ACCEPTABLE_MASTER_MAX_TABLE_SIZE:
|
|
|
|
case TLV_ALTERNATE_TIME_OFFSET_ENABLE:
|
|
|
|
case TLV_ALTERNATE_TIME_OFFSET_NAME:
|
|
|
|
case TLV_ALTERNATE_TIME_OFFSET_MAX_KEY:
|
|
|
|
case TLV_ALTERNATE_TIME_OFFSET_PROPERTIES:
|
|
|
|
case TLV_TRANSPARENT_CLOCK_DEFAULT_DATA_SET:
|
|
|
|
case TLV_PRIMARY_DOMAIN:
|
|
|
|
case TLV_TIME_STATUS_NP:
|
|
|
|
case TLV_GRANDMASTER_SETTINGS_NP:
|
|
|
|
case TLV_SUBSCRIBE_EVENTS_NP:
|
|
|
|
clock_management_send_error(p, msg, TLV_NOT_SUPPORTED);
|
2012-07-29 20:31:30 +08:00
|
|
|
break;
|
|
|
|
default:
|
2014-05-02 18:37:45 +08:00
|
|
|
answers = 0;
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_FOREACH(piter, &c->ports, list) {
|
|
|
|
res = port_manage(piter, p, msg);
|
2014-05-02 18:37:45 +08:00
|
|
|
if (res < 0)
|
|
|
|
return changed;
|
|
|
|
if (res > 0)
|
|
|
|
answers++;
|
|
|
|
}
|
|
|
|
if (!answers) {
|
|
|
|
/* IEEE 1588 Interpretation #21 suggests to use
|
2014-06-25 18:45:23 +08:00
|
|
|
* TLV_WRONG_VALUE for ports that do not exist */
|
|
|
|
clock_management_send_error(p, msg, TLV_WRONG_VALUE);
|
2012-07-29 20:31:30 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-07-08 03:27:32 +08:00
|
|
|
return changed;
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 00:41:49 +08:00
|
|
|
void clock_notify_event(struct clock *c, enum notification event)
|
|
|
|
{
|
2014-08-14 21:56:02 +08:00
|
|
|
struct port *uds = c->uds_port;
|
2014-05-07 00:41:49 +08:00
|
|
|
struct PortIdentity pid = port_identity(uds);
|
|
|
|
struct ptp_message *msg;
|
|
|
|
UInteger16 msg_len;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
/* set id */
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* targetPortIdentity and sequenceId will be filled by
|
|
|
|
* clock_send_notification */
|
|
|
|
msg = port_management_notify(pid, uds);
|
|
|
|
if (!msg)
|
|
|
|
return;
|
|
|
|
if (!clock_management_fill_response(c, NULL, NULL, msg, id))
|
|
|
|
goto err;
|
|
|
|
msg_len = msg->header.messageLength;
|
|
|
|
if (msg_pre_send(msg))
|
|
|
|
goto err;
|
|
|
|
clock_send_notification(c, msg, msg_len, event);
|
|
|
|
err:
|
|
|
|
msg_put(msg);
|
|
|
|
}
|
|
|
|
|
2012-12-01 05:35:26 +08:00
|
|
|
struct parent_ds *clock_parent_ds(struct clock *c)
|
2011-12-27 20:36:17 +08:00
|
|
|
{
|
|
|
|
return &c->dad;
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
struct PortIdentity clock_parent_identity(struct clock *c)
|
|
|
|
{
|
2012-12-01 05:35:26 +08:00
|
|
|
return c->dad.pds.parentPortIdentity;
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
|
2017-07-10 15:39:29 +08:00
|
|
|
void clock_set_sde(struct clock *c, int sde)
|
|
|
|
{
|
|
|
|
c->sde = sde;
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
int clock_poll(struct clock *c)
|
|
|
|
{
|
2017-02-06 01:31:27 +08:00
|
|
|
int cnt, i;
|
2011-11-13 01:44:55 +08:00
|
|
|
enum fsm_event event;
|
2014-08-14 21:56:03 +08:00
|
|
|
struct pollfd *cur;
|
|
|
|
struct port *p;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2014-08-14 21:56:04 +08:00
|
|
|
clock_check_pollfd(c);
|
2017-07-10 15:39:30 +08:00
|
|
|
cnt = poll(c->pollfd, (c->nports + 1) * N_CLOCK_PFD, -1);
|
2011-11-13 01:44:55 +08:00
|
|
|
if (cnt < 0) {
|
|
|
|
if (EINTR == errno) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
pr_emerg("poll failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!cnt) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
cur = c->pollfd;
|
2016-08-31 04:44:55 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_FOREACH(p, &c->ports, list) {
|
2012-03-24 19:46:24 +08:00
|
|
|
/* Let the ports handle their events. */
|
2017-02-06 01:25:18 +08:00
|
|
|
for (i = 0; i < N_POLLFD; i++) {
|
2014-08-14 21:56:03 +08:00
|
|
|
if (cur[i].revents & (POLLIN|POLLPRI)) {
|
|
|
|
event = port_event(p, i);
|
2011-11-13 01:44:55 +08:00
|
|
|
if (EV_STATE_DECISION_EVENT == event)
|
2017-02-06 01:31:27 +08:00
|
|
|
c->sde = 1;
|
2012-07-08 18:14:28 +08:00
|
|
|
if (EV_ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES == event)
|
2017-02-06 01:31:27 +08:00
|
|
|
c->sde = 1;
|
2017-02-06 01:25:18 +08:00
|
|
|
port_dispatch(p, event, 0);
|
2013-01-30 22:13:17 +08:00
|
|
|
/* Clear any fault after a little while. */
|
2014-08-14 21:56:03 +08:00
|
|
|
if (PS_FAULTY == port_state(p)) {
|
|
|
|
clock_fault_timeout(p, 1);
|
2013-01-30 23:28:54 +08:00
|
|
|
break;
|
2013-01-30 22:13:17 +08:00
|
|
|
}
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-24 19:46:24 +08:00
|
|
|
|
2016-08-31 04:44:55 +08:00
|
|
|
/*
|
|
|
|
* When the fault timer expires we clear the fault,
|
|
|
|
* but only if the link is up.
|
|
|
|
*/
|
2014-08-14 21:56:03 +08:00
|
|
|
if (cur[N_POLLFD].revents & (POLLIN|POLLPRI)) {
|
|
|
|
clock_fault_timeout(p, 0);
|
2016-08-31 04:44:55 +08:00
|
|
|
if (port_link_status_get(p)) {
|
|
|
|
port_dispatch(p, EV_FAULT_CLEARED, 0);
|
|
|
|
}
|
2012-03-24 19:46:24 +08:00
|
|
|
}
|
2014-08-14 21:56:03 +08:00
|
|
|
|
|
|
|
cur += N_CLOCK_PFD;
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
|
2012-08-24 21:30:02 +08:00
|
|
|
/* Check the UDS port. */
|
2014-08-14 21:56:03 +08:00
|
|
|
for (i = 0; i < N_POLLFD; i++) {
|
|
|
|
if (cur[i].revents & (POLLIN|POLLPRI)) {
|
|
|
|
event = port_event(c->uds_port, i);
|
2013-07-08 03:27:32 +08:00
|
|
|
if (EV_STATE_DECISION_EVENT == event)
|
2017-02-06 01:31:27 +08:00
|
|
|
c->sde = 1;
|
2012-08-24 21:30:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 01:31:27 +08:00
|
|
|
if (c->sde) {
|
2011-11-13 01:44:55 +08:00
|
|
|
handle_state_decision_event(c);
|
2017-02-06 01:31:27 +08:00
|
|
|
c->sde = 0;
|
|
|
|
}
|
2014-05-07 00:41:47 +08:00
|
|
|
clock_prune_subscriptions(c);
|
2011-11-13 01:44:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:32:12 +08:00
|
|
|
void clock_path_delay(struct clock *c, tmv_t req, tmv_t rx)
|
2011-11-13 01:44:55 +08:00
|
|
|
{
|
2015-03-26 23:32:13 +08:00
|
|
|
tsproc_up_ts(c->tsproc, req, rx);
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
if (tsproc_update_delay(c->tsproc, &c->path_delay))
|
2012-03-11 17:52:01 +08:00
|
|
|
return;
|
|
|
|
|
2012-08-22 13:25:46 +08:00
|
|
|
c->cur.meanPathDelay = tmv_to_TimeInterval(c->path_delay);
|
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
if (c->stats.delay)
|
2018-03-02 02:12:19 +08:00
|
|
|
stats_add_value(c->stats.delay, tmv_dbl(c->path_delay));
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
void clock_peer_delay(struct clock *c, tmv_t ppd, tmv_t req, tmv_t rx,
|
|
|
|
double nrr)
|
2012-04-05 20:04:15 +08:00
|
|
|
{
|
|
|
|
c->path_delay = ppd;
|
2012-08-31 21:49:19 +08:00
|
|
|
c->nrr = nrr;
|
2013-02-08 02:56:53 +08:00
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
tsproc_set_delay(c->tsproc, ppd);
|
|
|
|
tsproc_up_ts(c->tsproc, req, rx);
|
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
if (c->stats.delay)
|
2018-03-02 02:12:19 +08:00
|
|
|
stats_add_value(c->stats.delay, tmv_dbl(ppd));
|
2012-04-05 20:04:15 +08:00
|
|
|
}
|
|
|
|
|
2011-11-13 19:39:49 +08:00
|
|
|
int clock_slave_only(struct clock *c)
|
|
|
|
{
|
2012-12-01 04:52:37 +08:00
|
|
|
return c->dds.flags & DDS_SLAVE_ONLY;
|
2011-11-13 19:39:49 +08:00
|
|
|
}
|
|
|
|
|
2011-12-27 18:28:09 +08:00
|
|
|
UInteger16 clock_steps_removed(struct clock *c)
|
|
|
|
{
|
|
|
|
return c->cur.stepsRemoved;
|
|
|
|
}
|
|
|
|
|
2014-12-17 22:24:02 +08:00
|
|
|
int clock_switch_phc(struct clock *c, int phc_index)
|
|
|
|
{
|
|
|
|
struct servo *servo;
|
|
|
|
int fadj, max_adj;
|
|
|
|
clockid_t clkid;
|
|
|
|
char phc[32];
|
|
|
|
|
2017-05-22 03:17:03 +08:00
|
|
|
snprintf(phc, sizeof(phc), "/dev/ptp%d", phc_index);
|
2014-12-17 22:24:02 +08:00
|
|
|
clkid = phc_open(phc);
|
|
|
|
if (clkid == CLOCK_INVALID) {
|
|
|
|
pr_err("Switching PHC, failed to open %s: %m", phc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
max_adj = phc_max_adj(clkid);
|
|
|
|
if (!max_adj) {
|
|
|
|
pr_err("Switching PHC, clock is not adjustable");
|
|
|
|
phc_close(clkid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fadj = (int) clockadj_get_freq(clkid);
|
|
|
|
clockadj_set_freq(clkid, fadj);
|
2015-08-12 04:18:20 +08:00
|
|
|
servo = servo_create(c->config, c->servo_type, -fadj, max_adj, 0);
|
2014-12-17 22:24:02 +08:00
|
|
|
if (!servo) {
|
|
|
|
pr_err("Switching PHC, failed to create clock servo");
|
|
|
|
phc_close(clkid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
phc_close(c->clkid);
|
|
|
|
servo_destroy(c->servo);
|
|
|
|
c->clkid = clkid;
|
|
|
|
c->servo = servo;
|
|
|
|
c->servo_state = SERVO_UNLOCKED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:32:12 +08:00
|
|
|
enum servo_state clock_synchronize(struct clock *c, tmv_t ingress, tmv_t origin)
|
2011-11-13 01:44:55 +08:00
|
|
|
{
|
2015-03-26 23:32:15 +08:00
|
|
|
double adj, weight;
|
2011-12-18 18:02:26 +08:00
|
|
|
enum servo_state state = SERVO_UNLOCKED;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
c->ingress_ts = ingress;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
tsproc_down_ts(c->tsproc, origin, ingress);
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2015-03-26 23:32:15 +08:00
|
|
|
if (tsproc_update_offset(c->tsproc, &c->master_offset, &weight))
|
2011-12-18 18:02:26 +08:00
|
|
|
return state;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2013-03-08 00:27:32 +08:00
|
|
|
if (clock_utc_correct(c, ingress))
|
|
|
|
return c->servo_state;
|
|
|
|
|
|
|
|
c->cur.offsetFromMaster = tmv_to_TimeInterval(c->master_offset);
|
|
|
|
|
2012-12-01 04:52:37 +08:00
|
|
|
if (c->free_running)
|
2015-03-26 23:32:13 +08:00
|
|
|
return clock_no_adjust(c, ingress, origin);
|
2012-08-18 01:16:32 +08:00
|
|
|
|
2013-03-08 00:27:31 +08:00
|
|
|
adj = servo_sample(c->servo, tmv_to_nanoseconds(c->master_offset),
|
2015-03-26 23:32:15 +08:00
|
|
|
tmv_to_nanoseconds(ingress), weight, &state);
|
2013-03-08 00:27:32 +08:00
|
|
|
c->servo_state = state;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
if (c->stats.max_count > 1) {
|
2018-03-02 02:12:19 +08:00
|
|
|
clock_stats_update(&c->stats, tmv_dbl(c->master_offset), adj);
|
2013-02-08 02:56:53 +08:00
|
|
|
} else {
|
|
|
|
pr_info("master offset %10" PRId64 " s%d freq %+7.0f "
|
|
|
|
"path delay %9" PRId64,
|
2013-03-08 00:27:31 +08:00
|
|
|
tmv_to_nanoseconds(c->master_offset), state, adj,
|
|
|
|
tmv_to_nanoseconds(c->path_delay));
|
2013-02-08 02:56:53 +08:00
|
|
|
}
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2015-03-26 23:32:13 +08:00
|
|
|
tsproc_set_clock_rate_ratio(c->tsproc, clock_rate_ratio(c));
|
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
switch (state) {
|
|
|
|
case SERVO_UNLOCKED:
|
|
|
|
break;
|
|
|
|
case SERVO_JUMP:
|
2013-03-08 00:27:29 +08:00
|
|
|
clockadj_set_freq(c->clkid, -adj);
|
2013-03-08 00:27:31 +08:00
|
|
|
clockadj_step(c->clkid, -tmv_to_nanoseconds(c->master_offset));
|
2015-03-26 23:32:13 +08:00
|
|
|
c->ingress_ts = tmv_zero();
|
2013-10-23 00:57:15 +08:00
|
|
|
if (c->sanity_check) {
|
|
|
|
clockcheck_set_freq(c->sanity_check, -adj);
|
|
|
|
clockcheck_step(c->sanity_check,
|
|
|
|
-tmv_to_nanoseconds(c->master_offset));
|
|
|
|
}
|
2015-03-26 23:32:13 +08:00
|
|
|
tsproc_reset(c->tsproc, 0);
|
2011-11-13 01:44:55 +08:00
|
|
|
break;
|
|
|
|
case SERVO_LOCKED:
|
2013-03-08 00:27:29 +08:00
|
|
|
clockadj_set_freq(c->clkid, -adj);
|
2013-04-08 21:44:10 +08:00
|
|
|
if (c->clkid == CLOCK_REALTIME)
|
|
|
|
sysclk_set_sync();
|
2013-10-23 00:57:15 +08:00
|
|
|
if (c->sanity_check)
|
|
|
|
clockcheck_set_freq(c->sanity_check, -adj);
|
2011-11-13 01:44:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-12-18 18:02:26 +08:00
|
|
|
return state;
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
|
2012-08-22 02:04:45 +08:00
|
|
|
void clock_sync_interval(struct clock *c, int n)
|
|
|
|
{
|
2013-02-08 02:56:53 +08:00
|
|
|
int shift;
|
2012-08-22 02:04:45 +08:00
|
|
|
|
2013-02-08 02:56:53 +08:00
|
|
|
shift = c->freq_est_interval - n;
|
2012-08-22 02:04:45 +08:00
|
|
|
if (shift < 0)
|
|
|
|
shift = 0;
|
2013-02-12 20:23:06 +08:00
|
|
|
else if (shift >= sizeof(int) * 8) {
|
|
|
|
shift = sizeof(int) * 8 - 1;
|
|
|
|
pr_warning("freq_est_interval is too long");
|
|
|
|
}
|
2012-08-22 02:04:45 +08:00
|
|
|
c->fest.max_count = (1 << shift);
|
2013-02-08 02:56:53 +08:00
|
|
|
|
|
|
|
shift = c->stats_interval - n;
|
|
|
|
if (shift < 0)
|
|
|
|
shift = 0;
|
2013-02-12 20:23:06 +08:00
|
|
|
else if (shift >= sizeof(int) * 8) {
|
|
|
|
shift = sizeof(int) * 8 - 1;
|
|
|
|
pr_warning("summary_interval is too long");
|
|
|
|
}
|
2013-02-08 02:56:53 +08:00
|
|
|
c->stats.max_count = (1 << shift);
|
2013-05-16 22:31:45 +08:00
|
|
|
|
|
|
|
servo_sync_interval(c->servo, n < 0 ? 1.0 / (1 << -n) : 1 << n);
|
2012-08-22 02:04:45 +08:00
|
|
|
}
|
|
|
|
|
2011-12-27 20:36:17 +08:00
|
|
|
struct timePropertiesDS *clock_time_properties(struct clock *c)
|
|
|
|
{
|
|
|
|
return &c->tds;
|
|
|
|
}
|
|
|
|
|
2013-04-03 01:25:29 +08:00
|
|
|
void clock_update_time_properties(struct clock *c, struct timePropertiesDS tds)
|
|
|
|
{
|
|
|
|
c->tds = tds;
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
static void handle_state_decision_event(struct clock *c)
|
|
|
|
{
|
|
|
|
struct foreign_clock *best = NULL, *fc;
|
2013-07-15 03:34:01 +08:00
|
|
|
struct ClockIdentity best_id;
|
2014-08-14 21:56:03 +08:00
|
|
|
struct port *piter;
|
|
|
|
int fresh_best = 0;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_FOREACH(piter, &c->ports, list) {
|
|
|
|
fc = port_compute_best(piter);
|
2011-11-13 01:44:55 +08:00
|
|
|
if (!fc)
|
|
|
|
continue;
|
|
|
|
if (!best || dscmp(&fc->dataset, &best->dataset) > 0)
|
|
|
|
best = fc;
|
|
|
|
}
|
|
|
|
|
2013-07-15 03:34:01 +08:00
|
|
|
if (best) {
|
|
|
|
best_id = best->dataset.identity;
|
|
|
|
} else {
|
|
|
|
best_id = c->dds.clockIdentity;
|
|
|
|
}
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2012-01-01 16:02:29 +08:00
|
|
|
pr_notice("selected best master clock %s",
|
2013-07-15 03:34:01 +08:00
|
|
|
cid2str(&best_id));
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2013-07-15 03:34:01 +08:00
|
|
|
if (!cid_eq(&best_id, &c->best_id)) {
|
2012-09-02 20:30:50 +08:00
|
|
|
clock_freq_est_reset(c);
|
2015-03-26 23:32:13 +08:00
|
|
|
tsproc_reset(c->tsproc, 1);
|
2018-02-15 19:48:39 +08:00
|
|
|
if (c->initial_delay)
|
|
|
|
tsproc_set_delay(c->tsproc, c->initial_delay);
|
2015-03-26 23:32:13 +08:00
|
|
|
c->ingress_ts = tmv_zero();
|
2018-02-15 19:48:39 +08:00
|
|
|
c->path_delay = c->initial_delay;
|
2014-02-21 18:08:34 +08:00
|
|
|
c->nrr = 1.0;
|
2012-01-07 04:12:50 +08:00
|
|
|
fresh_best = 1;
|
|
|
|
}
|
2011-11-27 21:19:29 +08:00
|
|
|
|
2011-11-13 01:44:55 +08:00
|
|
|
c->best = best;
|
2013-07-15 03:34:01 +08:00
|
|
|
c->best_id = best_id;
|
2011-11-13 01:44:55 +08:00
|
|
|
|
2014-08-14 21:56:03 +08:00
|
|
|
LIST_FOREACH(piter, &c->ports, list) {
|
2011-11-13 01:44:55 +08:00
|
|
|
enum port_state ps;
|
|
|
|
enum fsm_event event;
|
2014-08-14 21:56:03 +08:00
|
|
|
ps = bmc_state_decision(c, piter);
|
2011-11-13 01:44:55 +08:00
|
|
|
switch (ps) {
|
|
|
|
case PS_LISTENING:
|
|
|
|
event = EV_NONE;
|
|
|
|
break;
|
|
|
|
case PS_GRAND_MASTER:
|
2013-07-15 03:34:01 +08:00
|
|
|
pr_notice("assuming the grand master role");
|
2011-11-13 01:44:55 +08:00
|
|
|
clock_update_grandmaster(c);
|
2011-12-27 18:04:50 +08:00
|
|
|
event = EV_RS_GRAND_MASTER;
|
|
|
|
break;
|
2011-11-13 01:44:55 +08:00
|
|
|
case PS_MASTER:
|
|
|
|
event = EV_RS_MASTER;
|
|
|
|
break;
|
|
|
|
case PS_PASSIVE:
|
|
|
|
event = EV_RS_PASSIVE;
|
|
|
|
break;
|
|
|
|
case PS_SLAVE:
|
|
|
|
clock_update_slave(c);
|
|
|
|
event = EV_RS_SLAVE;
|
|
|
|
break;
|
|
|
|
default:
|
2012-03-21 19:20:45 +08:00
|
|
|
event = EV_FAULT_DETECTED;
|
2011-11-13 01:44:55 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-08-14 21:56:03 +08:00
|
|
|
port_dispatch(piter, event, fresh_best);
|
2011-11-13 01:44:55 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-04 06:01:51 +08:00
|
|
|
|
|
|
|
struct clock_description *clock_description(struct clock *c)
|
|
|
|
{
|
|
|
|
return &c->desc;
|
|
|
|
}
|
2013-02-25 07:56:37 +08:00
|
|
|
|
2015-10-03 17:27:02 +08:00
|
|
|
enum clock_type clock_type(struct clock *c)
|
2013-02-25 07:56:37 +08:00
|
|
|
{
|
2015-10-18 22:26:49 +08:00
|
|
|
return c->type;
|
2013-02-25 07:56:37 +08:00
|
|
|
}
|
2013-10-23 00:57:15 +08:00
|
|
|
|
|
|
|
void clock_check_ts(struct clock *c, struct timespec ts)
|
|
|
|
{
|
|
|
|
if (c->sanity_check &&
|
|
|
|
clockcheck_sample(c->sanity_check,
|
|
|
|
ts.tv_sec * NS_PER_SEC + ts.tv_nsec)) {
|
|
|
|
servo_reset(c->servo);
|
|
|
|
}
|
|
|
|
}
|
2014-03-14 01:34:19 +08:00
|
|
|
|
|
|
|
double clock_rate_ratio(struct clock *c)
|
|
|
|
{
|
|
|
|
return servo_rate_ratio(c->servo);
|
|
|
|
}
|