statime/port/
master.rs

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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
use arrayvec::ArrayVec;

use super::{state::PortState, ForwardedTLVProvider, Port, PortActionIterator, Running};
use crate::{
    datastructures::{
        common::{PortIdentity, Tlv, TlvSetBuilder, TlvType},
        messages::{DelayReqMessage, Header, Message, MAX_DATA_LEN},
    },
    filters::Filter,
    port::{actions::TimestampContextInner, PortAction, TimestampContext},
    ptp_instance::PtpInstanceStateMutex,
    time::Time,
};

impl<A, C, F: Filter, R, S: PtpInstanceStateMutex> Port<'_, Running, A, R, C, F, S> {
    pub(super) fn send_sync(&mut self) -> PortActionIterator {
        if matches!(self.port_state, PortState::Master) {
            log::trace!("sending sync message");

            let seq_id = self.sync_seq_ids.generate();
            let packet_length = match self
                .instance_state
                .with_ref(|state| {
                    Message::sync(
                        &state.default_ds,
                        self.port_identity,
                        seq_id,
                        self.config.minor_ptp_version.into(),
                    )
                })
                .serialize(&mut self.packet_buffer)
            {
                Ok(message) => message,
                Err(error) => {
                    log::error!("Statime bug: Could not serialize sync: {:?}", error);
                    return actions![];
                }
            };

            actions![
                PortAction::ResetSyncTimer {
                    duration: self.config.sync_interval.as_core_duration(),
                },
                PortAction::SendEvent {
                    context: TimestampContext {
                        inner: TimestampContextInner::Sync { id: seq_id },
                    },
                    data: &self.packet_buffer[..packet_length],
                    link_local: false,
                }
            ]
        } else {
            actions![]
        }
    }

    pub(super) fn handle_sync_timestamp(&mut self, id: u16, timestamp: Time) -> PortActionIterator {
        if matches!(self.port_state, PortState::Master) {
            let packet_length = match self
                .instance_state
                .with_ref(|state| {
                    Message::follow_up(
                        &state.default_ds,
                        self.port_identity,
                        id,
                        timestamp,
                        self.config.minor_ptp_version.into(),
                    )
                })
                .serialize(&mut self.packet_buffer)
            {
                Ok(length) => length,
                Err(error) => {
                    log::error!(
                        "Statime bug: Could not serialize sync follow up {:?}",
                        error
                    );
                    return actions![];
                }
            };

            actions![PortAction::SendGeneral {
                data: &self.packet_buffer[..packet_length],
                link_local: false,
            }]
        } else {
            actions![]
        }
    }

    pub(super) fn send_announce(
        &mut self,
        tlv_provider: &mut impl ForwardedTLVProvider,
    ) -> PortActionIterator {
        if matches!(self.port_state, PortState::Master) {
            log::trace!("sending announce message");

            let mut tlv_buffer = [0; MAX_DATA_LEN];
            let mut tlv_builder = TlvSetBuilder::new(&mut tlv_buffer);

            let mut message = self.instance_state.with_ref(|state| {
                Message::announce(
                    state,
                    self.port_identity,
                    self.announce_seq_ids.generate(),
                    self.config.minor_ptp_version.into(),
                )
            });
            let mut tlv_margin = MAX_DATA_LEN - message.wire_size();

            let path_trace_enabled = self.instance_state.with_ref(|state| {
                let default_ds = &state.default_ds;
                let path_trace_ds = &state.path_trace_ds;
                if path_trace_ds.enable {
                    'path_trace: {
                        let mut path = path_trace_ds.list.clone();
                        if path.try_push(default_ds.clock_identity).is_err() {
                            break 'path_trace;
                        }

                        let value: ArrayVec<u8, MAX_DATA_LEN> =
                            path.into_iter().flat_map(|ci| ci.0).collect();
                        let tlv = Tlv {
                            tlv_type: TlvType::PathTrace,
                            value: value.as_slice().into(),
                        };

                        let tlv_size = tlv.wire_size();
                        if tlv_margin > tlv_size {
                            tlv_margin -= tlv_size;
                            // Will not fail as previous checks ensure sufficient space in buffer.
                            tlv_builder.add(tlv).unwrap();
                        }
                    }
                }

                path_trace_ds.enable
            });

            while let Some(tlv) = tlv_provider.next_if_smaller(tlv_margin) {
                assert!(tlv.size() < tlv_margin);
                let parent_port_identity = self
                    .instance_state
                    .with_ref(|s| s.parent_ds.parent_port_identity);
                if parent_port_identity != tlv.sender_identity {
                    // Ignore, shouldn't be forwarded
                    continue;
                }

                // Don't forward PATH_TRACE TLVs, we processed them and added our own
                if path_trace_enabled && tlv.tlv.tlv_type == TlvType::PathTrace {
                    continue;
                }

                tlv_margin -= tlv.size();
                // Will not fail as previous checks ensure sufficient space in buffer.
                tlv_builder.add(tlv.tlv).unwrap();
            }

            message.suffix = tlv_builder.build();

            let packet_length = match message.serialize(&mut self.packet_buffer) {
                Ok(length) => length,
                Err(error) => {
                    log::error!(
                        "Statime bug: Could not serialize announce message {:?}",
                        error
                    );
                    return actions![];
                }
            };

            actions![
                PortAction::ResetAnnounceTimer {
                    duration: self.config.announce_interval.as_core_duration(),
                },
                PortAction::SendGeneral {
                    data: &self.packet_buffer[..packet_length],
                    link_local: false,
                }
            ]
        } else {
            actions![]
        }
    }

    pub(super) fn handle_delay_req(
        &mut self,
        header: Header,
        message: DelayReqMessage,
        timestamp: Time,
    ) -> PortActionIterator {
        if matches!(self.port_state, PortState::Master) {
            log::debug!("Received DelayReq");
            let delay_resp_message = Message::delay_resp(
                header,
                message,
                self.port_identity,
                self.config.min_delay_req_interval(),
                timestamp,
            );

            let packet_length = match delay_resp_message.serialize(&mut self.packet_buffer) {
                Ok(length) => length,
                Err(error) => {
                    log::error!("Could not serialize delay response: {:?}", error);
                    return actions![];
                }
            };

            actions![PortAction::SendGeneral {
                data: &self.packet_buffer[..packet_length],
                link_local: false,
            }]
        } else {
            actions![]
        }
    }

    pub(super) fn handle_pdelay_req(
        &mut self,
        header: Header,
        timestamp: Time,
    ) -> PortActionIterator {
        log::debug!("Received PDelayReq");
        let pdelay_resp_message = self.instance_state.with_ref(|state| {
            Message::pdelay_resp(
                &state.default_ds,
                self.port_identity,
                header,
                timestamp,
                self.config.minor_ptp_version.into(),
            )
        });

        let packet_length = match pdelay_resp_message.serialize(&mut self.packet_buffer) {
            Ok(length) => length,
            Err(error) => {
                log::error!("Could not serialize pdelay response: {:?}", error);
                return actions![];
            }
        };

        actions![PortAction::SendEvent {
            data: &self.packet_buffer[..packet_length],
            context: TimestampContext {
                inner: TimestampContextInner::PDelayResp {
                    id: header.sequence_id,
                    requestor_identity: header.source_port_identity
                }
            },
            link_local: true,
        }]
    }

    pub(super) fn handle_pdelay_response_timestamp(
        &mut self,
        id: u16,
        requestor_identity: PortIdentity,
        timestamp: Time,
    ) -> PortActionIterator {
        let pdelay_resp_follow_up_messgae = self.instance_state.with_ref(|state| {
            Message::pdelay_resp_follow_up(
                &state.default_ds,
                self.port_identity,
                requestor_identity,
                id,
                timestamp,
                self.config.minor_ptp_version.into(),
            )
        });

        let packet_length = match pdelay_resp_follow_up_messgae.serialize(&mut self.packet_buffer) {
            Ok(length) => length,
            Err(error) => {
                log::error!("Could not serialize pdelay_response_followup: {:?}", error);
                return actions![];
            }
        };

        actions![PortAction::SendGeneral {
            data: &self.packet_buffer[..packet_length],
            link_local: true,
        }]
    }
}

#[cfg(test)]
mod tests {
    use fixed::types::{I48F16, U96F32};

    use super::*;
    use crate::{
        config::DelayMechanism,
        datastructures::{
            common::{PortIdentity, TimeInterval},
            datasets::PathTraceDS,
            messages::{Header, MessageBody},
        },
        port::{
            tests::{setup_test_port, setup_test_state},
            NoForwardedTLVs,
        },
        time::Interval,
    };

    #[test]
    fn test_delay_response() {
        let state = setup_test_state();

        let mut port = setup_test_port(&state);

        port.set_forced_port_state(PortState::Master);

        port.config.delay_mechanism = DelayMechanism::E2E {
            interval: Interval::from_log_2(2),
        };

        let mut action = port.handle_delay_req(
            Header {
                sequence_id: 5123,
                source_port_identity: PortIdentity {
                    port_number: 83,
                    ..Default::default()
                },
                correction_field: TimeInterval(I48F16::from_bits(400)),
                ..Header::new(1)
            },
            DelayReqMessage {
                origin_timestamp: Time::from_micros(0).into(),
            },
            Time::from_fixed_nanos(U96F32::from_bits((200000 << 32) + (500 << 16))),
        );

        let Some(PortAction::SendGeneral {
            data,
            link_local: false,
        }) = action.next()
        else {
            panic!("Unexpected resulting action");
        };
        assert!(action.next().is_none());
        drop(action);

        let msg = Message::deserialize(data).unwrap();
        let msg_header = msg.header;

        let msg = match msg.body {
            MessageBody::DelayResp(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        assert_eq!(
            msg.requesting_port_identity,
            PortIdentity {
                port_number: 83,
                ..Default::default()
            }
        );
        assert_eq!(msg_header.sequence_id, 5123);
        assert_eq!(msg.receive_timestamp, Time::from_micros(200).into());
        assert_eq!(msg_header.log_message_interval, 2);
        assert_eq!(
            msg_header.correction_field,
            TimeInterval(I48F16::from_bits(900))
        );

        port.config.delay_mechanism = DelayMechanism::E2E {
            interval: Interval::from_log_2(5),
        };

        let mut action = port.handle_delay_req(
            Header {
                sequence_id: 879,
                source_port_identity: PortIdentity {
                    port_number: 12,
                    ..Default::default()
                },
                correction_field: TimeInterval(I48F16::from_bits(200)),
                ..Header::new(1)
            },
            DelayReqMessage {
                origin_timestamp: Time::from_micros(0).into(),
            },
            Time::from_fixed_nanos(U96F32::from_bits((220000 << 32) + (300 << 16))),
        );

        let Some(PortAction::SendGeneral {
            data,
            link_local: false,
        }) = action.next()
        else {
            panic!("Unexpected resulting action");
        };
        assert!(action.next().is_none());

        let msg = Message::deserialize(data).unwrap();
        let msg_header = msg.header;

        let msg = match msg.body {
            MessageBody::DelayResp(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        assert_eq!(
            msg.requesting_port_identity,
            PortIdentity {
                port_number: 12,
                ..Default::default()
            }
        );
        assert_eq!(msg_header.sequence_id, 879);
        assert_eq!(msg.receive_timestamp, Time::from_micros(220).into());
        assert_eq!(msg_header.log_message_interval, 5);
        assert_eq!(
            msg_header.correction_field,
            TimeInterval(I48F16::from_bits(500))
        );
    }

    #[test]
    fn test_announce() {
        let state = setup_test_state();

        let mut state_ref = state.borrow_mut();
        state_ref.default_ds.priority_1 = 15;
        state_ref.default_ds.priority_2 = 128;
        state_ref.parent_ds.grandmaster_priority_1 = 15;
        state_ref.parent_ds.grandmaster_priority_2 = 128;

        drop(state_ref);

        let mut port = setup_test_port(&state);

        port.set_forced_port_state(PortState::Master);

        let mut actions = port.send_announce(&mut NoForwardedTLVs);

        assert!(matches!(
            actions.next(),
            Some(PortAction::ResetAnnounceTimer { .. })
        ));
        let Some(PortAction::SendGeneral {
            data,
            link_local: false,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };
        assert!(actions.next().is_none());
        drop(actions);

        let msg = Message::deserialize(data).unwrap();
        let msg_header = msg.header;

        let msg_body = match msg.body {
            MessageBody::Announce(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        assert_eq!(msg_body.grandmaster_priority_1, 15);
        assert_eq!(msg.suffix, Default::default());

        let mut actions = port.send_announce(&mut NoForwardedTLVs);

        assert!(matches!(
            actions.next(),
            Some(PortAction::ResetAnnounceTimer { .. })
        ));
        let Some(PortAction::SendGeneral {
            data,
            link_local: false,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };
        assert!(actions.next().is_none());

        let msg2 = Message::deserialize(data).unwrap();
        let msg2_header = msg2.header;

        let msg2_body = match msg2.body {
            MessageBody::Announce(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        assert_eq!(msg2_body.grandmaster_priority_1, 15);
        assert_eq!(msg2.suffix, Default::default());
        assert_ne!(msg2_header.sequence_id, msg_header.sequence_id);
    }

    #[test]
    fn test_announce_path_trace() {
        let state = setup_test_state();

        let mut state_ref = state.borrow_mut();
        state_ref.default_ds.priority_1 = 15;
        state_ref.default_ds.priority_2 = 128;
        state_ref.parent_ds.grandmaster_priority_1 = 15;
        state_ref.parent_ds.grandmaster_priority_2 = 128;
        state_ref.path_trace_ds = PathTraceDS::new(true);

        drop(state_ref);

        let mut port = setup_test_port(&state);

        port.set_forced_port_state(PortState::Master);

        let mut actions = port.send_announce(&mut NoForwardedTLVs);

        assert!(matches!(
            actions.next(),
            Some(PortAction::ResetAnnounceTimer { .. })
        ));
        let Some(PortAction::SendGeneral {
            data,
            link_local: false,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };
        assert!(actions.next().is_none());
        drop(actions);

        let msg = Message::deserialize(data).unwrap();

        let msg_body = match msg.body {
            MessageBody::Announce(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        assert_eq!(msg_body.grandmaster_priority_1, 15);

        let mut tlvs = msg.suffix.tlv();
        let Some(Tlv {
            tlv_type: TlvType::PathTrace,
            value,
        }) = tlvs.next()
        else {
            panic!("Unexpected or missing TLV")
        };
        assert_eq!(value.as_ref(), [0; 8].as_ref());
        assert!(tlvs.next().is_none());
    }

    #[test]
    fn test_sync() {
        let state = setup_test_state();

        let mut state_ref = state.borrow_mut();
        state_ref.default_ds.priority_1 = 15;
        state_ref.default_ds.priority_2 = 128;
        state_ref.parent_ds.grandmaster_priority_1 = 15;
        state_ref.parent_ds.grandmaster_priority_2 = 128;

        drop(state_ref);

        let mut port = setup_test_port(&state);

        port.set_forced_port_state(PortState::Master);
        let mut actions = port.send_sync();

        assert!(matches!(
            actions.next(),
            Some(PortAction::ResetSyncTimer { .. })
        ));
        let Some(PortAction::SendEvent {
            context,
            data,
            link_local: false,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };
        assert!(actions.next().is_none());
        drop(actions);

        let sync = Message::deserialize(data).unwrap();
        let sync_header = sync.header;

        let _sync = match sync.body {
            MessageBody::Sync(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        let id = match context.inner {
            TimestampContextInner::Sync { id } => id,
            _ => panic!("Wrong type of context"),
        };

        let mut actions = port.handle_sync_timestamp(
            id,
            Time::from_fixed_nanos(U96F32::from_bits((601300 << 32) + (230 << 16))),
        );

        let Some(PortAction::SendGeneral {
            data,
            link_local: false,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };
        assert!(actions.next().is_none());
        drop(actions);

        let follow = Message::deserialize(data).unwrap();
        let follow_header = follow.header;

        let follow = match follow.body {
            MessageBody::FollowUp(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        assert_eq!(sync_header.sequence_id, follow_header.sequence_id);
        assert_eq!(
            sync_header.correction_field,
            TimeInterval(I48F16::from_bits(0))
        );
        assert_eq!(
            follow.precise_origin_timestamp,
            Time::from_fixed_nanos(601300).into()
        );
        assert_eq!(
            follow_header.correction_field,
            TimeInterval(I48F16::from_bits(230))
        );

        let mut actions = port.send_sync();

        assert!(matches!(
            actions.next(),
            Some(PortAction::ResetSyncTimer { .. })
        ));
        let Some(PortAction::SendEvent {
            context,
            data,
            link_local: false,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };
        assert!(actions.next().is_none());
        drop(actions);

        let sync2 = Message::deserialize(data).unwrap();
        let sync2_header = sync2.header;

        let _sync2 = match sync2.body {
            MessageBody::Sync(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        let id = match context.inner {
            TimestampContextInner::Sync { id } => id,
            _ => panic!("wrong type of context"),
        };

        let mut actions = port.handle_sync_timestamp(
            id,
            Time::from_fixed_nanos(U96F32::from_bits((1000601300 << 32) + (543 << 16))),
        );

        let Some(PortAction::SendGeneral {
            data,
            link_local: false,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };
        assert!(actions.next().is_none());

        let follow2 = Message::deserialize(data).unwrap();
        let follow2_header = follow2.header;

        let follow2 = match follow2.body {
            MessageBody::FollowUp(msg) => msg,
            _ => panic!("Unexpected message type"),
        };

        assert_ne!(sync_header.sequence_id, sync2_header.sequence_id);
        assert_eq!(sync2_header.sequence_id, follow2_header.sequence_id);
        assert_eq!(
            sync2_header.correction_field,
            TimeInterval(I48F16::from_bits(0))
        );
        assert_eq!(
            follow2.precise_origin_timestamp,
            Time::from_fixed_nanos(1000601300).into()
        );
        assert_eq!(
            follow2_header.correction_field,
            TimeInterval(I48F16::from_bits(543))
        );
    }

    #[test]
    fn test_peer_delay() {
        let state = setup_test_state();

        let mut port = setup_test_port(&state);

        let mut actions = port.handle_pdelay_req(Header::new(1), Time::from_micros(500));

        let Some(PortAction::SendEvent {
            context,
            data,
            link_local: true,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };

        let response = Message::deserialize(data).unwrap();
        let MessageBody::PDelayResp(response_body) = response.body else {
            panic!("Unexpected message sent by port");
        };
        assert_eq!(
            response_body.request_receive_timestamp,
            Time::from_micros(500).into()
        );
        drop(response);
        assert!(actions.next().is_none());
        drop(actions);

        let mut actions = port.handle_send_timestamp(context, Time::from_micros(550));

        let Some(PortAction::SendGeneral {
            data,
            link_local: true,
        }) = actions.next()
        else {
            panic!("Unexpected action");
        };

        let response = Message::deserialize(data).unwrap();
        let MessageBody::PDelayRespFollowUp(response_body) = response.body else {
            panic!("Unexpected message sent by port");
        };
        assert_eq!(
            response_body.response_origin_timestamp,
            Time::from_micros(550).into()
        );
        drop(response);
        assert!(actions.next().is_none());
        drop(actions);
    }
}