rtime-core 0.14.0

Core types, clock algorithms (Marzullo source selection, PI servo, clock filter), and configuration for the rTime NTP/PTP time synchronization daemon
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use serde::Deserialize;

#[derive(Debug, Clone, Deserialize, Default)]
pub struct RtimeConfig {
    #[serde(default)]
    pub general: GeneralConfig,
    #[serde(default)]
    pub clock: ClockConfig,
    #[serde(default)]
    pub ntp: NtpConfig,
    #[serde(default)]
    pub ptp: PtpConfig,
    #[serde(default)]
    pub metrics: MetricsConfig,
    #[serde(default)]
    pub management: ManagementConfig,
}

#[derive(Debug, Clone, Deserialize)]
pub struct GeneralConfig {
    #[serde(default = "default_log_level")]
    pub log_level: String,
}

impl Default for GeneralConfig {
    fn default() -> Self {
        Self {
            log_level: default_log_level(),
        }
    }
}

fn default_log_level() -> String {
    "info".into()
}

#[derive(Debug, Clone, Deserialize)]
pub struct ClockConfig {
    #[serde(default = "default_true")]
    pub discipline: bool,
    #[serde(default = "default_step_threshold_ms")]
    pub step_threshold_ms: f64,
    #[serde(default = "default_panic_threshold_ms")]
    pub panic_threshold_ms: f64,
    #[serde(default = "default_true")]
    pub allow_initial_step: bool,
    #[serde(default = "default_clock_interface")]
    pub interface: String,
}

impl Default for ClockConfig {
    fn default() -> Self {
        Self {
            discipline: true,
            step_threshold_ms: default_step_threshold_ms(),
            panic_threshold_ms: default_panic_threshold_ms(),
            allow_initial_step: true,
            interface: default_clock_interface(),
        }
    }
}

fn default_true() -> bool {
    true
}

fn default_step_threshold_ms() -> f64 {
    128.0
}

fn default_panic_threshold_ms() -> f64 {
    // 1000 seconds — matches the classic `ntpd` panic default. The previous
    // value (1s) was a steady-state-tracking guard masquerading as the panic
    // threshold; in practice it stranded clocks whenever a VM was suspended
    // for longer than a second. The cold-boot bypass (`allow_initial_step`)
    // still handles offsets larger than this on first sample.
    1_000_000.0
}

fn default_clock_interface() -> String {
    "system".into()
}

#[derive(Debug, Clone, Deserialize)]
pub struct NtpConfig {
    #[serde(default = "default_true")]
    pub enabled: bool,
    #[serde(default = "default_ntp_listen")]
    pub listen: String,
    #[serde(default = "default_rate_limit")]
    pub rate_limit: f64,
    #[serde(default = "default_rate_burst")]
    pub rate_burst: u32,
    #[serde(default)]
    pub nts: NtsConfig,
    #[serde(default)]
    pub sources: Vec<NtpSourceConfig>,
}

impl Default for NtpConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            listen: default_ntp_listen(),
            rate_limit: default_rate_limit(),
            rate_burst: default_rate_burst(),
            nts: NtsConfig::default(),
            sources: Vec::new(),
        }
    }
}

fn default_ntp_listen() -> String {
    "127.0.0.1:123".into()
}

fn default_rate_limit() -> f64 {
    16.0
}

fn default_rate_burst() -> u32 {
    32
}

#[derive(Debug, Clone, Deserialize)]
pub struct NtsConfig {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default = "default_nts_ke_listen")]
    pub ke_listen: String,
    pub certificate: Option<String>,
    pub private_key: Option<String>,
}

impl Default for NtsConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            ke_listen: default_nts_ke_listen(),
            certificate: None,
            private_key: None,
        }
    }
}

fn default_nts_ke_listen() -> String {
    "127.0.0.1:4460".into()
}

#[derive(Debug, Clone, Deserialize)]
pub struct NtpSourceConfig {
    pub address: String,
    #[serde(default)]
    pub nts: bool,
    #[serde(default = "default_min_poll")]
    pub min_poll: i8,
    #[serde(default = "default_max_poll")]
    pub max_poll: i8,
}

fn default_min_poll() -> i8 {
    4
}

fn default_max_poll() -> i8 {
    10
}

#[derive(Debug, Clone, Deserialize)]
pub struct PtpConfig {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default)]
    pub domain: u8,
    #[serde(default = "default_ptp_interface")]
    pub interface: String,
    #[serde(default = "default_ptp_transport")]
    pub transport: String,
    #[serde(default = "default_priority")]
    pub priority1: u8,
    #[serde(default = "default_priority")]
    pub priority2: u8,
    #[serde(default = "default_delay_mechanism")]
    pub delay_mechanism: String,
}

impl Default for PtpConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            domain: 0,
            interface: default_ptp_interface(),
            transport: default_ptp_transport(),
            priority1: default_priority(),
            priority2: default_priority(),
            delay_mechanism: default_delay_mechanism(),
        }
    }
}

fn default_ptp_interface() -> String {
    "eth0".into()
}

fn default_ptp_transport() -> String {
    "udp-ipv4".into()
}

fn default_priority() -> u8 {
    128
}

fn default_delay_mechanism() -> String {
    "e2e".into()
}

#[derive(Debug, Clone, Deserialize)]
pub struct MetricsConfig {
    #[serde(default = "default_true")]
    pub enabled: bool,
    #[serde(default = "default_metrics_listen")]
    pub listen: String,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            listen: default_metrics_listen(),
        }
    }
}

fn default_metrics_listen() -> String {
    "127.0.0.1:9100".into()
}

#[derive(Debug, Clone, Deserialize)]
pub struct ManagementConfig {
    #[serde(default = "default_true")]
    pub enabled: bool,
    #[serde(default = "default_management_listen")]
    pub listen: String,
    /// Optional bearer token for API authentication.
    /// If set, all management API requests must include
    /// `Authorization: Bearer <token>` header.
    pub api_key: Option<String>,
}

impl Default for ManagementConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            listen: default_management_listen(),
            api_key: None,
        }
    }
}

fn default_management_listen() -> String {
    "127.0.0.1:9200".into()
}

impl RtimeConfig {
    /// Validate configuration values after deserialization.
    /// Returns an error describing the first invalid value found.
    pub fn validate(&self) -> Result<(), ConfigError> {
        // Clock thresholds must be positive and finite
        if !self.clock.step_threshold_ms.is_finite() || self.clock.step_threshold_ms <= 0.0 {
            return Err(ConfigError::InvalidValue(
                "clock.step_threshold_ms must be a positive finite number".into(),
            ));
        }
        if !self.clock.panic_threshold_ms.is_finite() || self.clock.panic_threshold_ms <= 0.0 {
            return Err(ConfigError::InvalidValue(
                "clock.panic_threshold_ms must be a positive finite number".into(),
            ));
        }
        if self.clock.step_threshold_ms >= self.clock.panic_threshold_ms {
            return Err(ConfigError::InvalidValue(format!(
                "clock.step_threshold_ms ({}) must be less than clock.panic_threshold_ms ({})",
                self.clock.step_threshold_ms, self.clock.panic_threshold_ms
            )));
        }

        // Rate limiting values
        if !self.ntp.rate_limit.is_finite() || self.ntp.rate_limit <= 0.0 {
            return Err(ConfigError::InvalidValue(
                "ntp.rate_limit must be a positive finite number".into(),
            ));
        }
        if self.ntp.rate_burst == 0 {
            return Err(ConfigError::InvalidValue(
                "ntp.rate_burst must be greater than 0".into(),
            ));
        }

        // Validate poll intervals for each source
        for (i, source) in self.ntp.sources.iter().enumerate() {
            if source.min_poll < 0 || source.min_poll > 17 {
                return Err(ConfigError::InvalidValue(format!(
                    "ntp.sources[{}].min_poll must be between 0 and 17",
                    i
                )));
            }
            if source.max_poll < 0 || source.max_poll > 17 {
                return Err(ConfigError::InvalidValue(format!(
                    "ntp.sources[{}].max_poll must be between 0 and 17",
                    i
                )));
            }
            if source.min_poll > source.max_poll {
                return Err(ConfigError::InvalidValue(format!(
                    "ntp.sources[{}].min_poll must be <= max_poll",
                    i
                )));
            }
        }

        // Validate listen addresses are parseable
        self.ntp
            .listen
            .parse::<std::net::SocketAddr>()
            .map_err(|_| {
                ConfigError::InvalidValue(format!(
                    "invalid ntp.listen address: {}",
                    self.ntp.listen
                ))
            })?;
        if self.metrics.enabled {
            self.metrics
                .listen
                .parse::<std::net::SocketAddr>()
                .map_err(|_| {
                    ConfigError::InvalidValue(format!(
                        "invalid metrics.listen address: {}",
                        self.metrics.listen
                    ))
                })?;
        }
        if self.management.enabled {
            self.management
                .listen
                .parse::<std::net::SocketAddr>()
                .map_err(|_| {
                    ConfigError::InvalidValue(format!(
                        "invalid management.listen address: {}",
                        self.management.listen
                    ))
                })?;
        }
        if self.ntp.nts.enabled {
            self.ntp
                .nts
                .ke_listen
                .parse::<std::net::SocketAddr>()
                .map_err(|_| {
                    ConfigError::InvalidValue(format!(
                        "invalid nts.ke_listen address: {}",
                        self.ntp.nts.ke_listen
                    ))
                })?;

            // Validate certificate and key files exist and are not world-readable
            let cert_path = self.ntp.nts.certificate.as_deref().ok_or_else(|| {
                ConfigError::InvalidValue("nts.certificate is required when NTS is enabled".into())
            })?;
            let key_path = self.ntp.nts.private_key.as_deref().ok_or_else(|| {
                ConfigError::InvalidValue("nts.private_key is required when NTS is enabled".into())
            })?;
            for (label, path) in [
                ("nts.certificate", cert_path),
                ("nts.private_key", key_path),
            ] {
                let meta = std::fs::metadata(path).map_err(|e| {
                    ConfigError::InvalidValue(format!("{} file '{}': {}", label, path, e))
                })?;
                if !meta.is_file() {
                    return Err(ConfigError::InvalidValue(format!(
                        "{} path '{}' is not a regular file",
                        label, path
                    )));
                }
                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    let mode = meta.permissions().mode();
                    if mode & 0o004 != 0 {
                        return Err(ConfigError::InvalidValue(format!(
                            "{} file '{}' is world-readable (mode {:o}); tighten permissions",
                            label, path, mode
                        )));
                    }
                }
            }
        }

        // Reject empty or whitespace-only API keys
        if self
            .management
            .api_key
            .as_ref()
            .is_some_and(|k| k.trim().is_empty())
        {
            return Err(ConfigError::InvalidValue(
                "management.api_key must not be empty or whitespace-only".into(),
            ));
        }

        // Non-loopback management/metrics checks
        let mgmt_addr: std::net::SocketAddr = self
            .management
            .listen
            .parse()
            .unwrap_or_else(|_| "127.0.0.1:9200".parse().unwrap());
        if self.management.enabled
            && !mgmt_addr.ip().is_loopback()
            && self.management.api_key.is_none()
        {
            return Err(ConfigError::InvalidValue(format!(
                "management API on non-loopback address {} requires api_key to be set",
                self.management.listen
            )));
        }

        let metrics_addr: std::net::SocketAddr = self
            .metrics
            .listen
            .parse()
            .unwrap_or_else(|_| "127.0.0.1:9100".parse().unwrap());
        if self.metrics.enabled && !metrics_addr.ip().is_loopback() {
            return Err(ConfigError::InvalidValue(format!(
                "metrics endpoint on non-loopback address {} is not allowed (bind to loopback or use a reverse proxy)",
                self.metrics.listen
            )));
        }

        Ok(())
    }
}

/// Configuration validation errors.
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    #[error("invalid config value: {0}")]
    InvalidValue(String),
}