From a712fd5907eb4d1258e667b64914cc2d4d34115f Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 29 Jan 2026 20:19:20 +0000 Subject: [PATCH] confd: new NTP setting stratum-weight This patch adds support for chronyd's stratumpweight both in the NTP client (ietf-system) and client+server (ietf-ntp) should fix the flaky NTP stratum test. Also, make sure to *not* 'rm -rf /etc/chrony/conf.d/*' in system.c to prevent clobbering settings potentially made in ntp.c Signed-off-by: Joachim Wiberg --- src/confd/src/ntp.c | 7 ++++- src/confd/src/system.c | 21 ++++++++++++- src/confd/yang/confd.inc | 4 +-- src/confd/yang/confd/infix-ntp.yang | 28 +++++++++++++++++ ...6-02-08.yang => infix-ntp@2026-03-09.yang} | 0 src/confd/yang/confd/infix-system.yang | 30 +++++++++++++++++++ ...2-02.yang => infix-system@2026-03-09.yang} | 0 .../case/ntp/client_stratum_selection/test.py | 1 + 8 files changed, 87 insertions(+), 4 deletions(-) rename src/confd/yang/confd/{infix-ntp@2026-02-08.yang => infix-ntp@2026-03-09.yang} (100%) rename src/confd/yang/confd/{infix-system@2025-12-02.yang => infix-system@2026-03-09.yang} (100%) diff --git a/src/confd/src/ntp.c b/src/confd/src/ntp.c index 2cf2465af..b50b57fb2 100644 --- a/src/confd/src/ntp.c +++ b/src/confd/src/ntp.c @@ -18,7 +18,7 @@ static int change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd sr_event_t event, struct confd *confd) { struct lyd_node *ntp, *entry, *makestep, *refclock; - const char *port; + const char *port, *sw; FILE *fp; if (diff && !lydx_get_xpathf(diff, XPATH_NTP_)) @@ -68,6 +68,11 @@ static int change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd fprintf(fp, "# Generated by confd\n"); fprintf(fp, "# This file configures chronyd as an NTP server\n\n"); + /* Global NTP source selection options */ + sw = lydx_get_cattr(ntp, "stratum-weight"); + if (sw) + fprintf(fp, "stratumweight %s\n\n", sw); + /* Port configuration (optional) */ port = lydx_get_cattr(ntp, "port"); if (port) { diff --git a/src/confd/src/system.c b/src/confd/src/system.c index 16ca790f2..7d9a7e8c9 100644 --- a/src/confd/src/system.c +++ b/src/confd/src/system.c @@ -25,6 +25,7 @@ #define XPATH_BASE_ "/ietf-system:system" #define XPATH_AUTH_ XPATH_BASE_"/authentication" #define XPATH_NTP_ XPATH_BASE_"/ntp" +#define NTP_CLIENT_CONF "/etc/chrony/conf.d/ntp-client.conf" #define XPATH_DNS_ XPATH_BASE_"/dns-resolver" #define XPATH_HOSTNAME_ XPATH_BASE_"/hostname" #define XPATH_MOTD_BANNER_ XPATH_BASE_"/infix-system:motd-banner" @@ -304,7 +305,8 @@ static int change_ntp_client(sr_session_ctx_t *session, struct lyd_node *config, case SR_EV_DONE: if (!srx_enabled(session, XPATH_NTP_"/enabled")) { - systemf("rm -rf /etc/chrony/conf.d/* /etc/chrony/sources.d/*"); + (void)remove(NTP_CLIENT_CONF); + systemf("rm -f /etc/chrony/sources.d/*"); /* Note: chronyd enable/disable is managed centrally in core.c */ systemf("initctl -nbq touch chronyd"); return SR_ERR_OK; @@ -405,6 +407,23 @@ static int change_ntp_client(sr_session_ctx_t *session, struct lyd_node *config, } sr_free_values(val, cnt); + /* Write global NTP client options (stratumweight, etc.) to conf.d */ + if (srx_enabled(session, XPATH_NTP_"/enabled")) { + char *sw = srx_get_str(session, XPATH_NTP_"/infix-system:stratum-weight"); + FILE *fp = fopen(NTP_CLIENT_CONF, "w"); + + if (fp) { + fprintf(fp, "# Generated by confd\n"); + if (sw) + fprintf(fp, "stratumweight %s\n", sw); + fclose(fp); + } else { + ERRNO("Failed creating %s", NTP_CLIENT_CONF); + } + if (sw) + free(sw); + } + if (changes) { if (touch("/run/chrony/.changes")) ERRNO("Failed recording changes to NTP client"); diff --git a/src/confd/yang/confd.inc b/src/confd/yang/confd.inc index 8efe9e3e1..74d4e8a6a 100644 --- a/src/confd/yang/confd.inc +++ b/src/confd/yang/confd.inc @@ -42,7 +42,7 @@ MODULES=( "infix-firewall-services@2025-04-26.yang" "infix-firewall-icmp-types@2025-04-26.yang" "infix-meta@2025-12-10.yang" - "infix-system@2025-12-02.yang" + "infix-system@2026-03-09.yang" "infix-services@2025-12-10.yang" "ieee802-ethernet-interface@2019-06-21.yang" "infix-ethernet-interface@2024-02-27.yang" @@ -51,6 +51,6 @@ MODULES=( "ietf-crypto-types -e cleartext-symmetric-keys" "infix-crypto-types@2025-11-09.yang" "ietf-keystore -e symmetric-keys" - "infix-ntp@2026-02-08.yang" + "infix-ntp@2026-03-09.yang" "infix-keystore@2025-12-17.yang" ) diff --git a/src/confd/yang/confd/infix-ntp.yang b/src/confd/yang/confd/infix-ntp.yang index 089f4e615..19addf890 100644 --- a/src/confd/yang/confd/infix-ntp.yang +++ b/src/confd/yang/confd/infix-ntp.yang @@ -29,6 +29,15 @@ module infix-ntp { contact "kernelkit@googlegroups.com"; description "Infix deviations and augments to ietf-ntp."; + revision 2026-03-09 { + description "Add stratumweight to NTP server configuration. + + Allows tuning the weight of stratum in NTP source selection + relative to measured distance. Setting to 0.0 ensures lower + stratum sources are always preferred regardless of distance."; + reference "internal"; + } + revision 2026-02-08 { description "Add GPS/GNSS reference clock support. @@ -56,6 +65,25 @@ module infix-ntp { */ augment "/ntp:ntp" { + leaf stratum-weight { + type decimal64 { + fraction-digits 3; + range "0.0..max"; + } + default "0.001"; + units "seconds"; + description + "Weight of stratum in NTP source selection relative to measured distance. + + A one-stratum difference counts as this many seconds of additional + distance when comparing candidates. + + The default (0.001 = 1ms) means stratum only wins when distance + differences are less than 1ms. Setting it to 0.0 ensures that a + lower-stratum source is always preferred over a higher-stratum source, + regardless of their measured distances."; + } + container makestep { presence "Enable clock stepping for large offsets"; description diff --git a/src/confd/yang/confd/infix-ntp@2026-02-08.yang b/src/confd/yang/confd/infix-ntp@2026-03-09.yang similarity index 100% rename from src/confd/yang/confd/infix-ntp@2026-02-08.yang rename to src/confd/yang/confd/infix-ntp@2026-03-09.yang diff --git a/src/confd/yang/confd/infix-system.yang b/src/confd/yang/confd/infix-system.yang index f9ecaa906..16c8de18e 100644 --- a/src/confd/yang/confd/infix-system.yang +++ b/src/confd/yang/confd/infix-system.yang @@ -28,6 +28,15 @@ module infix-system { contact "kernelkit@googlegroups.com"; description "Infix augments and deviations to ietf-system."; + revision 2026-03-09 { + description "Add stratumweight to NTP client configuration. + + Allows tuning the weight of stratum in NTP source selection + relative to measured distance. Setting to 0.0 ensures lower + stratum sources are always preferred regardless of distance."; + reference "internal"; + } + revision 2025-12-02 { description "Extend services with runtime statistics: - Add statistics container with memory-usage, uptime, restart-count"; @@ -307,6 +316,27 @@ module infix-system { } } + augment "/sys:system/sys:ntp" { + leaf stratum-weight { + type decimal64 { + fraction-digits 3; + range "0.0..max"; + } + default "0.001"; + units "seconds"; + description + "Weight of stratum in NTP source selection relative to measured distance. + + A one-stratum difference counts as this many seconds of additional + distance when comparing candidates. + + The default (0.001 = 1ms) means stratum only wins when distance + differences are less than 1ms. Setting it to 0.0 ensures that a + lower-stratum source is always preferred over a higher-stratum source, + regardless of their measured distances."; + } + } + augment "/sys:system/sys:authentication/sys:user" { description "Augment of ietf-system to support setting login shell for users."; leaf shell { diff --git a/src/confd/yang/confd/infix-system@2025-12-02.yang b/src/confd/yang/confd/infix-system@2026-03-09.yang similarity index 100% rename from src/confd/yang/confd/infix-system@2025-12-02.yang rename to src/confd/yang/confd/infix-system@2026-03-09.yang diff --git a/test/case/ntp/client_stratum_selection/test.py b/test/case/ntp/client_stratum_selection/test.py index 14f589e80..073d4a7e8 100755 --- a/test/case/ntp/client_stratum_selection/test.py +++ b/test/case/ntp/client_stratum_selection/test.py @@ -107,6 +107,7 @@ "system": { "ntp": { "enabled": True, + "infix-system:stratum-weight": 0.0, "server": [{ "name": "srv1", "udp": {