From 8eef6310097a9c271060a681d033b96d21de9c81 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 26 Sep 2023 12:39:25 +0200 Subject: [PATCH] ntp: add server support for network correction Provide the network correction (PTP correction + RX duration) of the request in the new extension field if included in the request and NTP-over-PTP is enabled. --- ntp_core.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/ntp_core.c b/ntp_core.c index 7b0a399..6cd7879 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -1084,6 +1084,36 @@ add_ef_mono_root(NTP_Packet *message, NTP_PacketInfo *info, struct timespec *rx, /* ================================================== */ +static int +add_ef_net_correction(NTP_Packet *message, NTP_PacketInfo *info, + NTP_Local_Timestamp *local_rx) +{ + NTP_EFExpNetCorrection ef; + + if (CNF_GetPtpPort() == 0) { + DEBUG_LOG("ptpport disabled"); + return 1; + } + + 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))) { + DEBUG_LOG("Could not add EF"); + return 0; + } + + info->ext_field_flags |= NTP_EF_FLAG_EXP_NET_CORRECTION; + + return 1; +} + +/* ================================================== */ + static int transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ int interleaved, /* Flag enabling interleaved mode */ @@ -1234,6 +1264,10 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ 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 {