From 4da088ec2f153337f2c710a447b232ce797f4b23 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 20 Jun 2024 14:31:04 +0200 Subject: [PATCH] ntp: make NTP-over-PTP domain configurable Add ptpdomain directive to set the domain number of transmitted and accepted NTP-over-PTP messages. It might need to be changed in networks using a PTP profile with the same domain number. The default domain number of 123 follows the current NTP-over-PTP specification. --- conf.c | 12 ++++++++++++ conf.h | 1 + doc/chrony.conf.adoc | 5 +++++ ntp_io.c | 4 ++-- ptp.h | 1 - test/simulation/142-ntpoverptp | 17 +++++++++++++++++ 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/conf.c b/conf.c index 01ace0d..774dc67 100644 --- a/conf.c +++ b/conf.c @@ -287,6 +287,8 @@ static double hwts_timeout = 0.001; /* PTP event port (disabled by default) */ static int ptp_port = 0; +/* PTP domain number of NTP-over-PTP messages */ +static int ptp_domain = 123; typedef struct { NTP_Source_Type type; @@ -708,6 +710,8 @@ CNF_ParseLine(const char *filename, int number, char *line) parse_source(p, command, 1); } else if (!strcasecmp(command, "port")) { parse_int(p, &ntp_port); + } else if (!strcasecmp(command, "ptpdomain")) { + parse_int(p, &ptp_domain); } else if (!strcasecmp(command, "ptpport")) { parse_int(p, &ptp_port); } else if (!strcasecmp(command, "ratelimit")) { @@ -2572,6 +2576,14 @@ CNF_GetPtpPort(void) /* ================================================== */ +int +CNF_GetPtpDomain(void) +{ + return ptp_domain; +} + +/* ================================================== */ + int CNF_GetRefresh(void) { diff --git a/conf.h b/conf.h index fb45fb7..cc0f17e 100644 --- a/conf.h +++ b/conf.h @@ -159,6 +159,7 @@ extern int CNF_GetHwTsInterface(unsigned int index, CNF_HwTsInterface **iface); extern double CNF_GetHwTsTimeout(void); extern int CNF_GetPtpPort(void); +extern int CNF_GetPtpDomain(void); extern int CNF_GetRefresh(void); diff --git a/doc/chrony.conf.adoc b/doc/chrony.conf.adoc index 56ed842..15684a8 100644 --- a/doc/chrony.conf.adoc +++ b/doc/chrony.conf.adoc @@ -2802,6 +2802,11 @@ hwtimestamp * rxfilter ptp ptpport 319 ---- +[[ptpdomain]]*ptpdomain* _domain_:: +The *ptpdomain* directive sets the PTP domain number of transmitted and +accepted NTP-over-PTP messages. Messages from other domains are ignored. +The default is 123, the minimum is 0, and the maximum is 255. + [[sched_priority]]*sched_priority* _priority_:: On Linux, FreeBSD, NetBSD, and illumos, the *sched_priority* directive will select the SCHED_FIFO real-time scheduler at the specified priority (which must diff --git a/ntp_io.c b/ntp_io.c index fc80aca..7a034f7 100644 --- a/ntp_io.c +++ b/ntp_io.c @@ -517,7 +517,7 @@ NIO_UnwrapMessage(SCK_Message *message, int sock_fd, double *net_correction) (msg->header.version != PTP_VERSION_2 && (msg->header.version != PTP_VERSION_2_1 || msg->header.min_sdoid != 0)) || ntohs(msg->header.length) != message->length || - msg->header.domain != PTP_DOMAIN_NTP || + msg->header.domain != CNF_GetPtpDomain() || ntohs(msg->header.flags) != PTP_FLAG_UNICAST || ntohs(msg->tlv_header.type) != PTP_TLV_NTP || ntohs(msg->tlv_header.length) != message->length - PTP_NTP_PREFIX_LENGTH) { @@ -565,7 +565,7 @@ wrap_message(SCK_Message *message, int sock_fd) ptp_message->header.type = PTP_TYPE_DELAY_REQ; ptp_message->header.version = PTP_VERSION_2; ptp_message->header.length = htons(PTP_NTP_PREFIX_LENGTH + message->length); - ptp_message->header.domain = PTP_DOMAIN_NTP; + ptp_message->header.domain = CNF_GetPtpDomain(); ptp_message->header.flags = htons(PTP_FLAG_UNICAST); ptp_message->header.sequence_id = htons(sequence_id++); ptp_message->tlv_header.type = htons(PTP_TLV_NTP); diff --git a/ptp.h b/ptp.h index 4330f4b..8bf639a 100644 --- a/ptp.h +++ b/ptp.h @@ -35,7 +35,6 @@ #define PTP_VERSION_2_1 (2 | 1 << 4) #define PTP_TYPE_SYNC 0 #define PTP_TYPE_DELAY_REQ 1 -#define PTP_DOMAIN_NTP 123 #define PTP_FLAG_UNICAST (1 << (2 + 8)) #define PTP_TLV_NTP 0x2023 diff --git a/test/simulation/142-ntpoverptp b/test/simulation/142-ntpoverptp index 2996dc0..8f966af 100755 --- a/test/simulation/142-ntpoverptp +++ b/test/simulation/142-ntpoverptp @@ -16,6 +16,7 @@ peers=2 max_sync_time=420 server_conf=" +ptpdomain 123 ptpport 319" client_conf=" ptpport 319 @@ -103,4 +104,20 @@ if check_config_h 'FEAT_DEBUG 1'; then check_log_messages "apply_net_correction.*Applied" 0 0 || test_fail fi +freq_offset=-1e-4 +delay_correction="" +server_conf="ptpport 319" +client_conf="ptpport 319 +ptpdomain 124 +authselectmode ignore +keyfile tmp/peer.keys" +time_max_limit=$default_time_max_limit +time_rms_limit=$default_time_rms_limit +freq_max_limit=$default_freq_max_limit +freq_rms_limit=$default_freq_rms_limit + +run_test || test_fail +check_chronyd_exit || test_fail +check_sync || test_fail + test_pass