Skip to content

Commit b078a57

Browse files
authored
Merge pull request #1378 from kernelkit/copilot/fix-client-stratum-selection
2 parents 30b12d1 + a712fd5 commit b078a57

8 files changed

Lines changed: 87 additions & 4 deletions

File tree

src/confd/src/ntp.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static int change(sr_session_ctx_t *session, struct lyd_node *config, struct lyd
1818
sr_event_t event, struct confd *confd)
1919
{
2020
struct lyd_node *ntp, *entry, *makestep, *refclock;
21-
const char *port;
21+
const char *port, *sw;
2222
FILE *fp;
2323

2424
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
6868
fprintf(fp, "# Generated by confd\n");
6969
fprintf(fp, "# This file configures chronyd as an NTP server\n\n");
7070

71+
/* Global NTP source selection options */
72+
sw = lydx_get_cattr(ntp, "stratum-weight");
73+
if (sw)
74+
fprintf(fp, "stratumweight %s\n\n", sw);
75+
7176
/* Port configuration (optional) */
7277
port = lydx_get_cattr(ntp, "port");
7378
if (port) {

src/confd/src/system.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define XPATH_BASE_ "/ietf-system:system"
2626
#define XPATH_AUTH_ XPATH_BASE_"/authentication"
2727
#define XPATH_NTP_ XPATH_BASE_"/ntp"
28+
#define NTP_CLIENT_CONF "/etc/chrony/conf.d/ntp-client.conf"
2829
#define XPATH_DNS_ XPATH_BASE_"/dns-resolver"
2930
#define XPATH_HOSTNAME_ XPATH_BASE_"/hostname"
3031
#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,
304305

305306
case SR_EV_DONE:
306307
if (!srx_enabled(session, XPATH_NTP_"/enabled")) {
307-
systemf("rm -rf /etc/chrony/conf.d/* /etc/chrony/sources.d/*");
308+
(void)remove(NTP_CLIENT_CONF);
309+
systemf("rm -f /etc/chrony/sources.d/*");
308310
/* Note: chronyd enable/disable is managed centrally in core.c */
309311
systemf("initctl -nbq touch chronyd");
310312
return SR_ERR_OK;
@@ -405,6 +407,23 @@ static int change_ntp_client(sr_session_ctx_t *session, struct lyd_node *config,
405407
}
406408
sr_free_values(val, cnt);
407409

410+
/* Write global NTP client options (stratumweight, etc.) to conf.d */
411+
if (srx_enabled(session, XPATH_NTP_"/enabled")) {
412+
char *sw = srx_get_str(session, XPATH_NTP_"/infix-system:stratum-weight");
413+
FILE *fp = fopen(NTP_CLIENT_CONF, "w");
414+
415+
if (fp) {
416+
fprintf(fp, "# Generated by confd\n");
417+
if (sw)
418+
fprintf(fp, "stratumweight %s\n", sw);
419+
fclose(fp);
420+
} else {
421+
ERRNO("Failed creating %s", NTP_CLIENT_CONF);
422+
}
423+
if (sw)
424+
free(sw);
425+
}
426+
408427
if (changes) {
409428
if (touch("/run/chrony/.changes"))
410429
ERRNO("Failed recording changes to NTP client");

src/confd/yang/confd.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ MODULES=(
4242
"infix-firewall-services@2025-04-26.yang"
4343
"infix-firewall-icmp-types@2025-04-26.yang"
4444
"infix-meta@2025-12-10.yang"
45-
"infix-system@2025-12-02.yang"
45+
"infix-system@2026-03-09.yang"
4646
"infix-services@2025-12-10.yang"
4747
"ieee802-ethernet-interface@2019-06-21.yang"
4848
"infix-ethernet-interface@2024-02-27.yang"
@@ -51,6 +51,6 @@ MODULES=(
5151
"ietf-crypto-types -e cleartext-symmetric-keys"
5252
"infix-crypto-types@2025-11-09.yang"
5353
"ietf-keystore -e symmetric-keys"
54-
"infix-ntp@2026-02-08.yang"
54+
"infix-ntp@2026-03-09.yang"
5555
"infix-keystore@2025-12-17.yang"
5656
)

src/confd/yang/confd/infix-ntp.yang

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ module infix-ntp {
2929
contact "kernelkit@googlegroups.com";
3030
description "Infix deviations and augments to ietf-ntp.";
3131

32+
revision 2026-03-09 {
33+
description "Add stratumweight to NTP server configuration.
34+
35+
Allows tuning the weight of stratum in NTP source selection
36+
relative to measured distance. Setting to 0.0 ensures lower
37+
stratum sources are always preferred regardless of distance.";
38+
reference "internal";
39+
}
40+
3241
revision 2026-02-08 {
3342
description
3443
"Add GPS/GNSS reference clock support.
@@ -56,6 +65,25 @@ module infix-ntp {
5665
*/
5766

5867
augment "/ntp:ntp" {
68+
leaf stratum-weight {
69+
type decimal64 {
70+
fraction-digits 3;
71+
range "0.0..max";
72+
}
73+
default "0.001";
74+
units "seconds";
75+
description
76+
"Weight of stratum in NTP source selection relative to measured distance.
77+
78+
A one-stratum difference counts as this many seconds of additional
79+
distance when comparing candidates.
80+
81+
The default (0.001 = 1ms) means stratum only wins when distance
82+
differences are less than 1ms. Setting it to 0.0 ensures that a
83+
lower-stratum source is always preferred over a higher-stratum source,
84+
regardless of their measured distances.";
85+
}
86+
5987
container makestep {
6088
presence "Enable clock stepping for large offsets";
6189
description
File renamed without changes.

src/confd/yang/confd/infix-system.yang

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ module infix-system {
2828
contact "kernelkit@googlegroups.com";
2929
description "Infix augments and deviations to ietf-system.";
3030

31+
revision 2026-03-09 {
32+
description "Add stratumweight to NTP client configuration.
33+
34+
Allows tuning the weight of stratum in NTP source selection
35+
relative to measured distance. Setting to 0.0 ensures lower
36+
stratum sources are always preferred regardless of distance.";
37+
reference "internal";
38+
}
39+
3140
revision 2025-12-02 {
3241
description "Extend services with runtime statistics:
3342
- Add statistics container with memory-usage, uptime, restart-count";
@@ -307,6 +316,27 @@ module infix-system {
307316
}
308317
}
309318

319+
augment "/sys:system/sys:ntp" {
320+
leaf stratum-weight {
321+
type decimal64 {
322+
fraction-digits 3;
323+
range "0.0..max";
324+
}
325+
default "0.001";
326+
units "seconds";
327+
description
328+
"Weight of stratum in NTP source selection relative to measured distance.
329+
330+
A one-stratum difference counts as this many seconds of additional
331+
distance when comparing candidates.
332+
333+
The default (0.001 = 1ms) means stratum only wins when distance
334+
differences are less than 1ms. Setting it to 0.0 ensures that a
335+
lower-stratum source is always preferred over a higher-stratum source,
336+
regardless of their measured distances.";
337+
}
338+
}
339+
310340
augment "/sys:system/sys:authentication/sys:user" {
311341
description "Augment of ietf-system to support setting login shell for users.";
312342
leaf shell {
File renamed without changes.

test/case/ntp/client_stratum_selection/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"system": {
108108
"ntp": {
109109
"enabled": True,
110+
"infix-system:stratum-weight": 0.0,
110111
"server": [{
111112
"name": "srv1",
112113
"udp": {

0 commit comments

Comments
 (0)