From 007a1ae4fef36cec10742e2bb05e37acd02f2b96 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 26 Nov 2025 12:14:08 +0100 Subject: [PATCH] ntp: update NTP-over-PTP support Update the support for NTP over PTP to the latest specification (currently in the RFC editor queue). Switch the NTP TLV to the organization-specific TLV using the IANA OUI and assigned TLV subtype 0x1. The Network Correction extension field has been assigned type 0x10A. The extfield option accepts F324 as an alias for 10A to not break existing configurations. Drop the experimental status. --- candm.h | 2 +- client.c | 4 ++-- cmdmon.c | 3 +-- cmdparse.c | 7 +++--- doc/chrony.conf.adoc | 38 +++++++++++++++++++-------------- doc/faq.adoc | 23 +++++++++++--------- ntp.h | 21 ++++++++---------- ntp_core.c | 39 ++++++++++++++++------------------ ntp_io.c | 15 ++++++++----- ptp.h | 10 ++++++--- test/simulation/110-chronyc | 2 +- test/simulation/142-ntpoverptp | 12 +++++------ 12 files changed, 94 insertions(+), 82 deletions(-) diff --git a/candm.h b/candm.h index 34024efd..16038c52 100644 --- a/candm.h +++ b/candm.h @@ -277,7 +277,7 @@ typedef struct { #define REQ_ADDSRC_NTS 0x200 #define REQ_ADDSRC_COPY 0x400 #define REQ_ADDSRC_EF_EXP_MONO_ROOT 0x800 -#define REQ_ADDSRC_EF_EXP_NET_CORRECTION 0x1000 +#define REQ_ADDSRC_EF_NET_CORRECTION 0x1000 #define REQ_ADDSRC_IPV4 0x2000 #define REQ_ADDSRC_IPV6 0x4000 diff --git a/client.c b/client.c index 3b924da8..1d6d3d53 100644 --- a/client.c +++ b/client.c @@ -1093,10 +1093,10 @@ process_cmd_add_source(CMD_Request *msg, char *line) (data.params.burst ? REQ_ADDSRC_BURST : 0) | (data.params.nts ? REQ_ADDSRC_NTS : 0) | (data.params.copy ? REQ_ADDSRC_COPY : 0) | + (data.params.ext_fields & NTP_EF_FLAG_NET_CORRECTION ? + REQ_ADDSRC_EF_NET_CORRECTION : 0) | (data.params.ext_fields & NTP_EF_FLAG_EXP_MONO_ROOT ? REQ_ADDSRC_EF_EXP_MONO_ROOT : 0) | - (data.params.ext_fields & NTP_EF_FLAG_EXP_NET_CORRECTION ? - REQ_ADDSRC_EF_EXP_NET_CORRECTION : 0) | (data.family == IPADDR_INET4 ? REQ_ADDSRC_IPV4 : 0) | (data.family == IPADDR_INET6 ? REQ_ADDSRC_IPV6 : 0) | convert_addsrc_sel_options(data.params.sel_options)); diff --git a/cmdmon.c b/cmdmon.c index c477b6df..08d1ceff 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -719,8 +719,7 @@ handle_add_source(CMD_Request *rx_message, CMD_Reply *tx_message) params.nts = !!(flags & REQ_ADDSRC_NTS); params.copy = !!(flags & REQ_ADDSRC_COPY); params.ext_fields = (flags & REQ_ADDSRC_EF_EXP_MONO_ROOT ? NTP_EF_FLAG_EXP_MONO_ROOT : 0) | - (flags & REQ_ADDSRC_EF_EXP_NET_CORRECTION ? - NTP_EF_FLAG_EXP_NET_CORRECTION : 0); + (flags & REQ_ADDSRC_EF_NET_CORRECTION ? NTP_EF_FLAG_NET_CORRECTION : 0); params.sel_options = convert_addsrc_select_options(ntohl(rx_message->data.ntp_source.flags)); status = NSR_AddSourceByName(name, family, port, pool, type, ¶ms, NULL); diff --git a/cmdparse.c b/cmdparse.c index 29c65b31..c55d8d4f 100644 --- a/cmdparse.c +++ b/cmdparse.c @@ -122,12 +122,13 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src) if (sscanf(line, "%"SCNx32"%n", &ef_type, &n) != 1) return CPS_InvalidValue; switch (ef_type) { + case NTP_EF_NET_CORRECTION: + case 0xF324: + src->params.ext_fields |= NTP_EF_FLAG_NET_CORRECTION; + break; case NTP_EF_EXP_MONO_ROOT: src->params.ext_fields |= NTP_EF_FLAG_EXP_MONO_ROOT; break; - case NTP_EF_EXP_NET_CORRECTION: - src->params.ext_fields |= NTP_EF_FLAG_EXP_NET_CORRECTION; - break; default: return CPS_InvalidValue; } diff --git a/doc/chrony.conf.adoc b/doc/chrony.conf.adoc index 2097632b..7ac63661 100644 --- a/doc/chrony.conf.adoc +++ b/doc/chrony.conf.adoc @@ -352,6 +352,21 @@ This option can be used multiple times to enable multiple extension fields. + The following extension fields are supported: + +_10A_:::: +The Network Correction extension field enabling the use of the Precision Time +Protocol (PTP) corrections in NTP-over-PTP messages updated by +one-step end-to-end transparent clocks (e.g. network switches). It can +significantly improve the accuracy and stability of the synchronisation. NTP +over PTP needs to be enabled by the <> directive and setting +the *port* option to the PTP port. The corrections are applied only to NTP +measurements with HW timestamps (enabled by the <> +directive). ++ +This field should be enabled only for servers known to be running *chronyd* +version 4.9 or later, or other implementations supporting the Network +Correction extension field. *chronyd* versions 4.5-4.8 supported an experimental +version of this extension field (type _F324_), which is no longer supported, +but type _F324_ is accepted as an alias for _10A_. _F323_:::: An experimental extension field to enable several improvements that were proposed for the next version of the NTP protocol (NTPv5). The field contains @@ -359,16 +374,6 @@ root delay and dispersion in higher resolution and a monotonic receive timestamp, which enables a frequency transfer between the server and client to significantly improve stability of the synchronisation. This field should be enabled only for servers known to be running *chronyd* version 4.2 or later. -_F324_:::: -An experimental extension field to enable the use of the Precision Time -Protocol (PTP) correction field in NTP-over-PTP messages updated by one-step -end-to-end transparent clocks in network switches and routers to significantly -improve accuracy and stability of the synchronisation. NTP-over-PTP can be -enabled by the <> directive and setting the *port* option to -the PTP port. The corrections are applied only to NTP measurements with HW -timestamps (enabled by the <> directive). This -field should be enabled only for servers known to be running *chronyd* version -4.5 or later. *ipv4*::: *ipv6*::: These options force *chronyd* to use only IPv4 or IPv6 addresses respectively @@ -3000,29 +3005,30 @@ Setting this directive to _/_ disables writing and checking of the PID file. [[ptpport]]*ptpport* _port_:: The *ptpport* directive enables *chronyd* to send and receive NTP messages -contained in PTP event messages (NTP-over-PTP) to enable hardware timestamping +contained in PTP event messages (NTP over PTP) to enable hardware timestamping on NICs that cannot timestamp NTP packets, but can timestamp unicast PTP packets, and also use corrections provided by PTP one-step end-to-end transparent clocks in network switches and routers. The port recognized by the NICs and PTP transparent clocks is 319 (PTP event port). The default value is 0 (disabled). + -The NTP-over-PTP support is experimental. The protocol and configuration can -change in future. It should be used only in local networks. +Support for NTP over PTP was added in *chronyd* version 4.9. +Versions 4.2-4.8 supported an experimental version of the transport, which is +not compatible with the final specification. + The PTP port will be open even if *chronyd* is not configured to operate as a server or client. The directive does not change the default protocol of -specified NTP sources. Each NTP source that should use NTP-over-PTP needs to +specified NTP sources. Each NTP source that should use NTP over PTP needs to be specified with the *port* option set to the PTP port. To actually enable hardware timestamping on NICs that can timestamp PTP packets only, the *rxfilter* option of the *hwtimestamp* directive needs to be set to _ptp_. The -extension field _F324_ needs to be enabled to use the corrections provided by +extension field _10A_ needs to be enabled to use the corrections provided by the PTP transparent clocks. + An example of client configuration is: + ---- -server ntp1.example.net minpoll 0 maxpoll 0 xleave port 319 extfield F324 +server ntp1.example.net minpoll 0 maxpoll 0 xleave port 319 extfield 10A hwtimestamp * rxfilter ptp ptpport 319 ---- diff --git a/doc/faq.adoc b/doc/faq.adoc index 66e58d92..2e92fed3 100644 --- a/doc/faq.adoc +++ b/doc/faq.adoc @@ -401,11 +401,12 @@ server ntp.local minpoll 0 maxpoll 0 xleave extfield F323 Since version 4.5, `chronyd` can apply corrections from PTP one-step end-to-end transparent clocks (e.g. network switches) to significantly improve accuracy of synchronisation in local networks. It requires the PTP transport to be enabled -by the `ptpport` directive, HW timestamping, and the `extfield F324` option. -For example: +by the `ptpport` directive, `port` option, HW timestamping, and the `extfield +F324` option (in versions 4.5-4.8) or `extfield 10A` option (version 4.9 and +later). For example: ---- -server ntp.local minpoll -4 maxpoll -4 xleave extfield F323 extfield F324 port 319 +server ntp.local minpoll -4 maxpoll -4 xleave extfield F323 extfield 10A port 319 ptpport 319 hwtimestamp eth0 minpoll -4 ---- @@ -668,13 +669,15 @@ packets (enabled by the `hwtimestamp` directive) if the NIC can timestamp other packets than PTP, which is usually the case at least for transmitted packets. The `ethtool -T` command can be used to verify the timestamping support. -As an experimental feature added in version 4.2, `chrony` can use PTP as a -transport for NTP messages (NTP over PTP) to enable hardware timestamping on -hardware which can timestamp PTP packets only. It can be enabled by the -`ptpport` directive. Since version 4.5, `chrony` can also apply corrections -provided by PTP one-step end-to-end transparent clocks to reach the accuracy of -ordinary PTP clocks. The application of PTP corrections can be enabled by the -`extfield F324` option. +As an experimental feature added in version 4.2, and fully supported since +version 4.9, `chrony` can use PTP as a transport for NTP messages (NTP over +PTP) to enable hardware timestamping on hardware which can timestamp only PTP +packets. NTP over PTP can be enabled by the `ptpport` directive and setting the +`port` option to the PTP port 319. Since version 4.5, `chrony` can also apply +corrections provided by PTP one-step end-to-end transparent clocks (e.g. +network switches) to reach the accuracy of ordinary PTP clocks. The application +of PTP corrections can be enabled by the `extfield F324` option in versions +before 4.9, or `extfield 10A` in later versions. === How can I avoid using wrong PHC refclock? diff --git a/ntp.h b/ntp.h index 165adbc2..3175c84e 100644 --- a/ntp.h +++ b/ntp.h @@ -115,11 +115,17 @@ typedef struct { /* Non-authentication extension fields and corresponding internal flags */ +#define NTP_EF_NET_CORRECTION 0x010A #define NTP_EF_EXP_MONO_ROOT 0xF323 -#define NTP_EF_EXP_NET_CORRECTION 0xF324 -#define NTP_EF_FLAG_EXP_MONO_ROOT 0x1 -#define NTP_EF_FLAG_EXP_NET_CORRECTION 0x2 +#define NTP_EF_FLAG_NET_CORRECTION 0x1 +#define NTP_EF_FLAG_EXP_MONO_ROOT 0x2 + +/* Network Correction extension field */ +typedef struct { + NTP_int64 correction; + uint32_t pad[4]; +} NTP_EFNetCorrection; /* Pre-NTPv5 experimental extension field */ typedef struct { @@ -132,15 +138,6 @@ typedef struct { #define NTP_EF_EXP_MONO_ROOT_MAGIC 0xF5BEDD9AU -/* Experimental extension field to provide PTP corrections */ -typedef struct { - uint32_t magic; - NTP_int64 correction; - uint32_t reserved[3]; -} NTP_EFExpNetCorrection; - -#define NTP_EF_EXP_NET_CORRECTION_MAGIC 0x07AC2CEBU - /* Authentication extension fields */ #define NTP_EF_NTS_UNIQUE_IDENTIFIER 0x0104 diff --git a/ntp_core.c b/ntp_core.c index 5df416d7..bb9920d0 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -378,8 +378,8 @@ do_size_checks(void) assert(offsetof(NTP_Packet, receive_ts) == 32); assert(offsetof(NTP_Packet, transmit_ts) == 40); + assert(sizeof (NTP_EFNetCorrection) == 24); assert(sizeof (NTP_EFExpMonoRoot) == 24); - assert(sizeof (NTP_EFExpNetCorrection) == 24); } /* ================================================== */ @@ -1094,7 +1094,7 @@ static int add_ef_net_correction(NTP_Packet *message, NTP_PacketInfo *info, NTP_Local_Timestamp *local_rx) { - NTP_EFExpNetCorrection ef; + NTP_EFNetCorrection ef; if (CNF_GetPtpPort() == 0) { DEBUG_LOG("ptpport disabled"); @@ -1102,18 +1102,17 @@ add_ef_net_correction(NTP_Packet *message, NTP_PacketInfo *info, } memset(&ef, 0, sizeof (ef)); - ef.magic = htonl(NTP_EF_EXP_NET_CORRECTION_MAGIC); if (info->mode != MODE_CLIENT && local_rx->net_correction > local_rx->rx_duration) { UTI_DoubleToNtp64(local_rx->net_correction, &ef.correction); } - if (!NEF_AddField(message, info, NTP_EF_EXP_NET_CORRECTION, &ef, sizeof (ef))) { + if (!NEF_AddField(message, info, NTP_EF_NET_CORRECTION, &ef, sizeof (ef))) { DEBUG_LOG("Could not add EF"); return 0; } - info->ext_field_flags |= NTP_EF_FLAG_EXP_NET_CORRECTION; + info->ext_field_flags |= NTP_EF_FLAG_NET_CORRECTION; return 1; } @@ -1265,15 +1264,15 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ return 0; if (ext_field_flags) { + if (ext_field_flags & NTP_EF_FLAG_NET_CORRECTION) { + if (!add_ef_net_correction(&message, &info, local_rx)) + return 0; + } if (ext_field_flags & NTP_EF_FLAG_EXP_MONO_ROOT) { if (!add_ef_mono_root(&message, &info, smooth_time ? NULL : &local_receive, our_root_delay, our_root_dispersion)) return 0; } - if (ext_field_flags & NTP_EF_FLAG_EXP_NET_CORRECTION) { - if (!add_ef_net_correction(&message, &info, local_rx)) - return 0; - } } do { @@ -1631,16 +1630,15 @@ parse_packet(NTP_Packet *packet, int length, NTP_PacketInfo *info) case NTP_EF_NTS_AUTH_AND_EEF: info->auth.mode = NTP_AUTH_NTS; break; + case NTP_EF_NET_CORRECTION: + if (ef_body_length == sizeof (NTP_EFNetCorrection)) + info->ext_field_flags |= NTP_EF_FLAG_NET_CORRECTION; + break; case NTP_EF_EXP_MONO_ROOT: if (is_exp_ef(ef_body, ef_body_length, sizeof (NTP_EFExpMonoRoot), NTP_EF_EXP_MONO_ROOT_MAGIC)) info->ext_field_flags |= NTP_EF_FLAG_EXP_MONO_ROOT; break; - case NTP_EF_EXP_NET_CORRECTION: - if (is_exp_ef(ef_body, ef_body_length, sizeof (NTP_EFExpNetCorrection), - NTP_EF_EXP_NET_CORRECTION_MAGIC)) - info->ext_field_flags |= NTP_EF_FLAG_EXP_NET_CORRECTION; - break; default: DEBUG_LOG("Unknown extension field type=%x", (unsigned int)ef_type); } @@ -1975,8 +1973,8 @@ process_response(NCR_Instance inst, int saved, NTP_Local_Address *local_addr, /* Extension fields */ int parsed, ef_length, ef_type, ef_body_length; void *ef_body; + NTP_EFNetCorrection *ef_net_correction; NTP_EFExpMonoRoot *ef_mono_root; - NTP_EFExpNetCorrection *ef_net_correction; NTP_Local_Timestamp local_receive, local_transmit; double remote_interval, local_interval, response_time; @@ -1998,18 +1996,17 @@ process_response(NCR_Instance inst, int saved, NTP_Local_Address *local_addr, break; switch (ef_type) { + case NTP_EF_NET_CORRECTION: + if (inst->ext_field_flags & NTP_EF_FLAG_NET_CORRECTION && + ef_body_length == sizeof (*ef_net_correction)) + ef_net_correction = ef_body; + break; case NTP_EF_EXP_MONO_ROOT: if (inst->ext_field_flags & NTP_EF_FLAG_EXP_MONO_ROOT && is_exp_ef(ef_body, ef_body_length, sizeof (*ef_mono_root), NTP_EF_EXP_MONO_ROOT_MAGIC)) ef_mono_root = ef_body; break; - case NTP_EF_EXP_NET_CORRECTION: - if (inst->ext_field_flags & NTP_EF_FLAG_EXP_NET_CORRECTION && - is_exp_ef(ef_body, ef_body_length, sizeof (*ef_net_correction), - NTP_EF_EXP_NET_CORRECTION_MAGIC)) - ef_net_correction = ef_body; - break; } } } diff --git a/ntp_io.c b/ntp_io.c index 71ccfab9..085b658c 100644 --- a/ntp_io.c +++ b/ntp_io.c @@ -519,8 +519,11 @@ NIO_UnwrapMessage(SCK_Message *message, int sock_fd, double *net_correction) ntohs(msg->header.length) != message->length || 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) { + (ntohs(msg->tlv_header.type) != PTP_TLV_ORGEXT2 && + ntohs(msg->tlv_header.type) != PTP_TLV_ORGEXT21) || + ntohs(msg->tlv_header.length) != 8 + message->length - PTP_NTP_PREFIX_LENGTH || + memcmp(msg->tlv_header.org_id_subtype, PTP_IANA_TLV_NTP_MSG, + sizeof (msg->tlv_header.org_id_subtype)) != 0) { DEBUG_LOG("Unexpected PTP message"); return 0; } @@ -547,7 +550,7 @@ wrap_message(SCK_Message *message, int sock_fd) { static uint16_t sequence_id = 0; - assert(PTP_NTP_PREFIX_LENGTH == 48); + assert(PTP_NTP_PREFIX_LENGTH == 56); if (!is_ptp_socket(sock_fd)) return 1; @@ -568,8 +571,10 @@ wrap_message(SCK_Message *message, int sock_fd) 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); - ptp_message->tlv_header.length = htons(message->length); + ptp_message->tlv_header.type = htons(PTP_TLV_ORGEXT2); + ptp_message->tlv_header.length = htons(8 + message->length); + memcpy(ptp_message->tlv_header.org_id_subtype, PTP_IANA_TLV_NTP_MSG, + sizeof (ptp_message->tlv_header.org_id_subtype)); memcpy((char *)ptp_message + PTP_NTP_PREFIX_LENGTH, message->data, message->length); message->data = ptp_message; diff --git a/ptp.h b/ptp.h index 8bf639a5..c8ffb2dc 100644 --- a/ptp.h +++ b/ptp.h @@ -36,7 +36,9 @@ #define PTP_TYPE_SYNC 0 #define PTP_TYPE_DELAY_REQ 1 #define PTP_FLAG_UNICAST (1 << (2 + 8)) -#define PTP_TLV_NTP 0x2023 +#define PTP_TLV_ORGEXT2 0x0003 +#define PTP_TLV_ORGEXT21 0x8000 +#define PTP_IANA_TLV_NTP_MSG "\x00\x00\x5E\x00\x00\x01" typedef struct { uint8_t type; @@ -56,12 +58,14 @@ typedef struct { typedef struct { uint16_t type; uint16_t length; -} PTP_TlvHeader; + uint8_t org_id_subtype[6]; + uint16_t pad; +} PTP_TlvNtpHeader; typedef struct { PTP_Header header; uint8_t origin_ts[10]; - PTP_TlvHeader tlv_header; + PTP_TlvNtpHeader tlv_header; NTP_Packet ntp_msg; } PTP_NtpMessage; diff --git a/test/simulation/110-chronyc b/test/simulation/110-chronyc index 448c89bc..6488d97c 100755 --- a/test/simulation/110-chronyc +++ b/test/simulation/110-chronyc @@ -114,7 +114,7 @@ limit=1 for chronyc_conf in \ "accheck 1.2.3.4" \ "add peer 10.0.0.0 minpoll 2 maxpoll 6" \ - "add server 10.0.0.0 minpoll 6 maxpoll 10 iburst burst key 1 certset 2 maxdelay 1e-3 maxdelayratio 10.0 maxdelaydevratio 10.0 maxdelayquant 0.5 mindelay 1e-4 asymmetry 0.5 offset 1e-5 minsamples 6 maxsamples 6 maxunreach 8 filter 3 offline auto_offline prefer noselect trust require xleave polltarget 20 port 123 presend 7 minstratum 3 version 4 nts ntsport 4460 copy extfield F323 extfield F324 ipv6 ipv4" \ + "add server 10.0.0.0 minpoll 6 maxpoll 10 iburst burst key 1 certset 2 maxdelay 1e-3 maxdelayratio 10.0 maxdelaydevratio 10.0 maxdelayquant 0.5 mindelay 1e-4 asymmetry 0.5 offset 1e-5 minsamples 6 maxsamples 6 maxunreach 8 filter 3 offline auto_offline prefer noselect trust require xleave polltarget 20 port 123 presend 7 minstratum 3 version 4 nts ntsport 4460 copy extfield 10A extfield F323 ipv6 ipv4" \ "add server node1.net1.clk" \ "allow 1.2.3.4" \ "allow 1.2" \ diff --git a/test/simulation/142-ntpoverptp b/test/simulation/142-ntpoverptp index 74cb8f7c..ac26cb8d 100755 --- a/test/simulation/142-ntpoverptp +++ b/test/simulation/142-ntpoverptp @@ -30,13 +30,13 @@ check_chronyd_exit || test_fail check_source_selection || test_fail check_sync || test_fail -check_file_messages " 2 1 .* 319 319 1 96 " 150 160 \ +check_file_messages " 2 1 .* 319 319 1 104 " 150 160 \ log.packets || test_fail -check_file_messages " 1 2 .* 319 319 1 96 " 150 160 \ +check_file_messages " 1 2 .* 319 319 1 104 " 150 160 \ log.packets || test_fail -check_file_messages " 2 3 .* 319 319 1 116 " 150 160 \ +check_file_messages " 2 3 .* 319 319 1 124 " 150 160 \ log.packets || test_fail -check_file_messages " 3 2 .* 319 319 1 116 " 150 160 \ +check_file_messages " 3 2 .* 319 319 1 124 " 150 160 \ log.packets || test_fail check_config_h 'HAVE_LINUX_TIMESTAMPING 1' || test_skip @@ -44,8 +44,8 @@ check_config_h 'HAVE_LINUX_TIMESTAMPING 1' || test_skip export CLKNETSIM_TIMESTAMPING=2 export CLKNETSIM_LINK_SPEED=100 -client_server_options+=" extfield F324 minpoll 0 maxpoll 0" -client_peer_options+=" extfield F324 minpoll 0 maxpoll 0 maxdelaydevratio 1e6" +client_server_options+=" extfield 10A minpoll 0 maxpoll 0" +client_peer_options+=" extfield 10A minpoll 0 maxpoll 0 maxdelaydevratio 1e6" server_conf+=" clockprecision 1e-9 hwtimestamp eth0" -- 2.47.3