rtimed 0.14.0

High-performance NTP/PTP time synchronization daemon (NTPv4, NTS, PTP). Installs as the `rtime` binary.
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! PTP client node that participates in IEEE 1588 as a slave-only device.
//!
//! Joins the PTP multicast group, listens for Announce/Sync/FollowUp messages,
//! sends DelayReq messages, and feeds SourceMeasurement into the shared selection
//! pipeline alongside NTP sources.

use std::collections::VecDeque;
use std::net::{Ipv4Addr, SocketAddr};
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::{Context, Result};
use rand::Rng;
use tokio::net::UdpSocket;
use tokio::sync::{mpsc, watch};
use tracing::{debug, error, info, warn};

use rtime_core::clock::LeapIndicator;
use rtime_core::config::PtpConfig;
use rtime_core::source::{SourceId, SourceMeasurement};
use rtime_core::timestamp::{NtpDuration, NtpTimestamp, PtpTimestamp};
use rtime_net::multicast::{
    PTP_EVENT_PORT, PTP_GENERAL_PORT, PTP_PRIMARY_MULTICAST_V4, join_multicast,
    set_multicast_interface, set_multicast_loopback, set_multicast_ttl,
};
use rtime_ptp::announce::ForeignMasterTable;
use rtime_ptp::delay::E2eDelayState;
use rtime_ptp::message::{MessageType, PortIdentity, PtpFlags, PtpHeader, PtpMessage};

/// Maximum PTP packet size we expect to receive.
const PTP_MAX_PACKET_SIZE: usize = 1500;

/// Number of jitter samples to keep for RMS calculation.
const JITTER_WINDOW: usize = 8;

/// Maximum PTP packets processed per second (rate limit).
const PTP_MAX_PACKETS_PER_SEC: u64 = 200;

/// Default announce interval for the foreign master table (2^1 = 2 seconds).
const DEFAULT_ANNOUNCE_INTERVAL_SECS: f64 = 2.0;

/// Maximum number of foreign masters to track.
const MAX_FOREIGN_MASTERS: usize = 5;

/// Interval between DelayReq messages (seconds).
const DELAY_REQ_INTERVAL_SECS: u64 = 2;

/// Generate a random clock identity using cryptographic RNG.
fn generate_clock_identity() -> [u8; 8] {
    let mut id = [0u8; 8];
    rand::rng().fill_bytes(&mut id);
    // Mark as locally-administered by setting bit 1 of the first octet
    id[0] |= 0x02;
    id
}

/// Resolve the interface name to an IPv4 address for multicast binding.
/// Falls back to UNSPECIFIED (0.0.0.0) if the interface cannot be resolved.
fn resolve_interface_addr(interface: &str) -> Ipv4Addr {
    // Try to find the interface address via /sys/class/net
    // For simplicity, we'll try to parse it as an IP address first,
    // then fall back to listing interfaces.
    if let Ok(addr) = interface.parse::<Ipv4Addr>() {
        return addr;
    }

    // Try to find the interface by name using getifaddrs
    // For now, fall back to UNSPECIFIED and let the kernel pick.
    warn!(
        "Could not resolve PTP interface '{}' to IPv4 address, using 0.0.0.0",
        interface
    );
    Ipv4Addr::UNSPECIFIED
}

/// Run the PTP client node.
///
/// This task:
/// 1. Joins the PTP multicast group on the configured interface
/// 2. Listens for Announce messages and maintains a foreign master table
/// 3. Listens for Sync/FollowUp messages and records T1/T2
/// 4. Sends DelayReq and processes DelayResp to record T3/T4
/// 5. Computes offset/delay via E2E delay mechanism
/// 6. Sends SourceMeasurement on the shared channel
pub async fn run_ptp_node(
    config: Arc<PtpConfig>,
    measurement_tx: mpsc::Sender<SourceMeasurement>,
    mut shutdown: watch::Receiver<bool>,
) -> Result<()> {
    let interface_addr = resolve_interface_addr(&config.interface);
    let our_identity = generate_clock_identity();
    let our_port = PortIdentity {
        clock_identity: our_identity,
        port_number: 1,
    };

    info!(
        "PTP node starting: domain={}, interface={} ({}), identity={:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
        config.domain,
        config.interface,
        interface_addr,
        our_identity[0],
        our_identity[1],
        our_identity[2],
        our_identity[3],
        our_identity[4],
        our_identity[5],
        our_identity[6],
        our_identity[7],
    );

    // Bind the event socket (port 319) for Sync/DelayReq/DelayResp.
    let event_socket = UdpSocket::bind(SocketAddr::new(
        Ipv4Addr::UNSPECIFIED.into(),
        PTP_EVENT_PORT,
    ))
    .await
    .context("failed to bind PTP event socket (port 319)")?;

    // Bind the general socket (port 320) for Announce/FollowUp.
    let general_socket = UdpSocket::bind(SocketAddr::new(
        Ipv4Addr::UNSPECIFIED.into(),
        PTP_GENERAL_PORT,
    ))
    .await
    .context("failed to bind PTP general socket (port 320)")?;

    // Join multicast on both sockets.
    join_multicast(&event_socket, PTP_PRIMARY_MULTICAST_V4, interface_addr)
        .context("failed to join PTP multicast on event socket")?;
    join_multicast(&general_socket, PTP_PRIMARY_MULTICAST_V4, interface_addr)
        .context("failed to join PTP multicast on general socket")?;

    // Configure multicast settings on event socket (for sending DelayReq).
    set_multicast_interface(&event_socket, interface_addr)
        .context("failed to set multicast interface on event socket")?;
    set_multicast_loopback(&event_socket, false)
        .context("failed to disable multicast loopback on event socket")?;
    set_multicast_ttl(&event_socket, 1).context("failed to set multicast TTL on event socket")?;

    info!(
        "PTP node: joined multicast group {}",
        PTP_PRIMARY_MULTICAST_V4
    );

    let event_socket = Arc::new(event_socket);
    let general_socket = Arc::new(general_socket);

    // Foreign master table for tracking announce messages.
    let mut foreign_masters =
        ForeignMasterTable::new(DEFAULT_ANNOUNCE_INTERVAL_SECS, MAX_FOREIGN_MASTERS);

    // E2E delay state for collecting timestamps.
    let mut delay_state = E2eDelayState::new();

    // Track the current master we are syncing to.
    let mut current_master: Option<PortIdentity> = None;

    // Sequence ID for DelayReq messages.
    let mut delay_req_seq: u16 = 0;

    // Jitter tracking.
    let mut jitter_samples: VecDeque<f64> = VecDeque::new();
    let mut last_offset_ms: Option<f64> = None;

    // Timer for sending DelayReq messages.
    let mut delay_req_interval =
        tokio::time::interval(Duration::from_secs(DELAY_REQ_INTERVAL_SECS));
    delay_req_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);

    // Timer for expiring stale foreign masters.
    let mut expire_interval = tokio::time::interval(Duration::from_secs(10));
    expire_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);

    // Track the last Sync sequence ID to correlate with FollowUp.
    let mut pending_sync_seq: Option<u16> = None;
    let mut pending_sync_t2: Option<PtpTimestamp> = None;

    let mut event_buf = [0u8; PTP_MAX_PACKET_SIZE];
    let mut general_buf = [0u8; PTP_MAX_PACKET_SIZE];

    // Rate limiting for incoming PTP packets.
    let mut ptp_packet_count: u64 = 0;
    let mut ptp_rate_limit_reset = Instant::now();

    loop {
        tokio::select! {
            // Receive on event socket (Sync, DelayResp).
            result = event_socket.recv_from(&mut event_buf) => {
                match result {
                    Ok((len, from)) => {
                        // Rate limit incoming packets.
                        let now_rl = Instant::now();
                        if now_rl.checked_duration_since(ptp_rate_limit_reset).unwrap_or_default() >= Duration::from_secs(1) {
                            ptp_packet_count = 0;
                            ptp_rate_limit_reset = now_rl;
                        }
                        ptp_packet_count += 1;
                        if ptp_packet_count > PTP_MAX_PACKETS_PER_SEC {
                            continue;
                        }

                        let recv_time = current_ptp_timestamp();
                        if let Err(e) = handle_event_message(
                            &event_buf[..len],
                            from,
                            recv_time,
                            config.domain,
                            &our_port,
                            &mut delay_state,
                            &mut pending_sync_seq,
                            &mut pending_sync_t2,
                            &mut current_master,
                            &mut jitter_samples,
                            &mut last_offset_ms,
                            &measurement_tx,
                        ).await {
                            debug!("PTP event message error: {}", e);
                        }
                    }
                    Err(e) => {
                        error!("PTP event socket recv error: {}", e);
                    }
                }
            }

            // Receive on general socket (Announce, FollowUp).
            result = general_socket.recv_from(&mut general_buf) => {
                match result {
                    Ok((len, from)) => {
                        // Share rate limit counter with event socket.
                        let now_rl = Instant::now();
                        if now_rl.checked_duration_since(ptp_rate_limit_reset).unwrap_or_default() >= Duration::from_secs(1) {
                            ptp_packet_count = 0;
                            ptp_rate_limit_reset = now_rl;
                        }
                        ptp_packet_count += 1;
                        if ptp_packet_count > PTP_MAX_PACKETS_PER_SEC {
                            continue;
                        }

                        if let Err(e) = handle_general_message(
                            &general_buf[..len],
                            from,
                            config.domain,
                            &our_port,
                            &mut foreign_masters,
                            &mut delay_state,
                            &mut current_master,
                            &mut pending_sync_seq,
                            &mut pending_sync_t2,
                        ) {
                            debug!("PTP general message error: {}", e);
                        }
                    }
                    Err(e) => {
                        error!("PTP general socket recv error: {}", e);
                    }
                }
            }

            // Periodically send DelayReq to current master.
            _ = delay_req_interval.tick() => {
                if let Some(ref _master) = current_master {
                    let t3 = current_ptp_timestamp();
                    if let Err(e) = send_delay_req(
                        &event_socket,
                        config.domain,
                        &our_port,
                        &mut delay_req_seq,
                        t3,
                    ).await {
                        debug!("Failed to send DelayReq: {}", e);
                    } else {
                        delay_state.set_delay_req_departure(t3);
                    }
                }
            }

            // Periodically expire stale foreign masters.
            _ = expire_interval.tick() => {
                foreign_masters.expire_stale(Instant::now());
            }

            // Shutdown signal.
            result = shutdown.changed() => {
                if result.is_ok() && *shutdown.borrow() {
                    info!("PTP node shutting down");
                    break;
                }
            }
        }
    }

    Ok(())
}

/// Get the current time as a PtpTimestamp (approximate, from system clock).
fn current_ptp_timestamp() -> PtpTimestamp {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default();
    PtpTimestamp::new(now.as_secs(), now.subsec_nanos())
}

/// Handle a message received on the event socket (Sync, DelayResp).
#[allow(clippy::too_many_arguments)]
async fn handle_event_message(
    data: &[u8],
    _from: SocketAddr,
    recv_time: PtpTimestamp,
    domain: u8,
    our_port: &PortIdentity,
    delay_state: &mut E2eDelayState,
    pending_sync_seq: &mut Option<u16>,
    pending_sync_t2: &mut Option<PtpTimestamp>,
    current_master: &mut Option<PortIdentity>,
    jitter_samples: &mut VecDeque<f64>,
    last_offset_ms: &mut Option<f64>,
    measurement_tx: &mpsc::Sender<SourceMeasurement>,
) -> Result<()> {
    let msg = PtpMessage::parse(data).context("failed to parse PTP event message")?;
    let header = msg.header();

    // Filter by domain.
    if header.domain_number != domain {
        return Ok(());
    }

    // Ignore our own messages.
    if header.source_port_identity == *our_port {
        return Ok(());
    }

    match msg {
        PtpMessage::Sync {
            header,
            origin_timestamp,
        } => {
            // If this Sync is from our current master (or we don't have one yet).
            let master = header.source_port_identity;
            if current_master.is_none() || *current_master == Some(master) {
                // Record T2 (receive time of Sync).
                delay_state.set_sync_arrival(recv_time);

                if header.flags.has(PtpFlags::TWO_STEP) {
                    // Two-step: wait for FollowUp with T1.
                    *pending_sync_seq = Some(header.sequence_id);
                    *pending_sync_t2 = Some(recv_time);
                } else {
                    // One-step: origin_timestamp is T1.
                    delay_state.set_sync_departure(origin_timestamp);
                    *pending_sync_seq = None;
                    *pending_sync_t2 = None;
                }

                debug!(
                    "PTP Sync from {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}: seq={} two_step={}",
                    master.clock_identity[0],
                    master.clock_identity[1],
                    master.clock_identity[2],
                    master.clock_identity[3],
                    master.clock_identity[4],
                    master.clock_identity[5],
                    master.clock_identity[6],
                    master.clock_identity[7],
                    header.sequence_id,
                    header.flags.has(PtpFlags::TWO_STEP),
                );
            }
        }

        PtpMessage::DelayResp {
            header,
            receive_timestamp,
            requesting_port,
        } => {
            // Only process DelayResp addressed to us.
            if requesting_port != *our_port {
                return Ok(());
            }

            // Record T4 (master's receive time of our DelayReq).
            delay_state.set_delay_resp_arrival(receive_timestamp);

            debug!(
                "PTP DelayResp: seq={} T4={}",
                header.sequence_id, receive_timestamp,
            );

            // Try to compute offset/delay if all timestamps collected.
            if let Some((offset, delay)) = delay_state.compute() {
                let offset_ms = offset.to_millis_f64();
                let delay_ms = delay.to_millis_f64();

                info!(
                    "PTP E2E measurement: offset={:+.3}ms delay={:.3}ms",
                    offset_ms, delay_ms,
                );

                // Update jitter estimate.
                let jitter = if let Some(prev) = *last_offset_ms {
                    let diff = offset_ms - prev;
                    jitter_samples.push_back(diff * diff);
                    if jitter_samples.len() > JITTER_WINDOW {
                        jitter_samples.pop_front();
                    }
                    let sum: f64 = jitter_samples.iter().sum();
                    (sum / jitter_samples.len() as f64).sqrt()
                } else {
                    0.0
                };
                *last_offset_ms = Some(offset_ms);

                // Determine source identity from the master.
                let master_port = current_master.unwrap_or(header.source_port_identity);

                let measurement = SourceMeasurement {
                    id: SourceId::Ptp {
                        clock_identity: master_port.clock_identity,
                        port_number: master_port.port_number,
                    },
                    offset,
                    delay,
                    dispersion: NtpDuration::from_nanos(1_000), // 1us estimated dispersion
                    jitter,
                    stratum: 1, // PTP masters are typically stratum 1
                    leap_indicator: LeapIndicator::NoWarning,
                    root_delay: delay, // approximate
                    root_dispersion: NtpDuration::from_nanos(1_000),
                    time: NtpTimestamp::now(),
                };

                if let Err(e) = measurement_tx.send(measurement).await {
                    warn!("Failed to send PTP measurement: {}", e);
                }

                // Reset for next cycle.
                delay_state.reset();
            }
        }

        _ => {
            // Ignore other event messages (DelayReq, PDelayReq, etc.).
        }
    }

    Ok(())
}

/// Handle a message received on the general socket (Announce, FollowUp).
#[allow(clippy::too_many_arguments)]
fn handle_general_message(
    data: &[u8],
    _from: SocketAddr,
    domain: u8,
    our_port: &PortIdentity,
    foreign_masters: &mut ForeignMasterTable,
    delay_state: &mut E2eDelayState,
    current_master: &mut Option<PortIdentity>,
    pending_sync_seq: &mut Option<u16>,
    _pending_sync_t2: &mut Option<PtpTimestamp>,
) -> Result<()> {
    let msg = PtpMessage::parse(data).context("failed to parse PTP general message")?;
    let header = msg.header();

    // Filter by domain.
    if header.domain_number != domain {
        return Ok(());
    }

    // Ignore our own messages.
    if header.source_port_identity == *our_port {
        return Ok(());
    }

    match msg {
        PtpMessage::Announce { header, announce } => {
            let master = header.source_port_identity;
            let now = Instant::now();

            let qualified = foreign_masters.record_announce(master, announce.clone(), now);

            if qualified {
                // If we don't have a current master, accept the first qualified one.
                // In a more complete implementation, we'd run the BMCA here.
                if current_master.is_none() {
                    *current_master = Some(master);
                    info!(
                        "PTP selected master: {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}-{} (GM priority1={} class={})",
                        master.clock_identity[0],
                        master.clock_identity[1],
                        master.clock_identity[2],
                        master.clock_identity[3],
                        master.clock_identity[4],
                        master.clock_identity[5],
                        master.clock_identity[6],
                        master.clock_identity[7],
                        master.port_number,
                        announce.grandmaster_priority1,
                        announce.grandmaster_clock_quality.clock_class,
                    );
                }
            }

            debug!(
                "PTP Announce from {:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}: qualified={} steps_removed={}",
                master.clock_identity[0],
                master.clock_identity[1],
                master.clock_identity[2],
                master.clock_identity[3],
                master.clock_identity[4],
                master.clock_identity[5],
                master.clock_identity[6],
                master.clock_identity[7],
                qualified,
                announce.steps_removed,
            );
        }

        PtpMessage::FollowUp {
            header,
            precise_origin_timestamp,
        } => {
            // Match FollowUp to the pending Sync.
            if let Some(sync_seq) = *pending_sync_seq
                && header.sequence_id == sync_seq
            {
                // T1 = precise_origin_timestamp from FollowUp.
                delay_state.set_sync_departure(precise_origin_timestamp);
                *pending_sync_seq = None;

                debug!(
                    "PTP FollowUp: seq={} T1={}",
                    header.sequence_id, precise_origin_timestamp,
                );
            }
        }

        _ => {
            // Ignore other general messages.
        }
    }

    Ok(())
}

/// Send a DelayReq message to the PTP multicast group.
async fn send_delay_req(
    event_socket: &UdpSocket,
    domain: u8,
    our_port: &PortIdentity,
    seq: &mut u16,
    t3: PtpTimestamp,
) -> Result<()> {
    let current_seq = *seq;
    *seq = seq.wrapping_add(1);

    let msg = PtpMessage::DelayReq {
        header: PtpHeader::new(MessageType::DelayReq, domain, *our_port, current_seq),
        origin_timestamp: t3,
    };

    let bytes = msg.serialize();
    let dest = SocketAddr::new(PTP_PRIMARY_MULTICAST_V4.into(), PTP_EVENT_PORT);

    event_socket
        .send_to(&bytes, dest)
        .await
        .context("failed to send PTP DelayReq")?;

    debug!("PTP DelayReq sent: seq={}", current_seq);
    Ok(())
}