hyperv_ic_protocol/
timesync.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Protocol definitions for the timesync IC.

use crate::Version;
use bitfield_struct::bitfield;
use zerocopy::FromBytes;
use zerocopy::Immutable;
use zerocopy::IntoBytes;
use zerocopy::KnownLayout;
use zerocopy::little_endian::U64 as U64LE;

/// Version 1.0.
pub const TIMESYNC_VERSION_1: Version = Version::new(1, 0);
/// Version 3.0.
pub const TIMESYNC_VERSION_3: Version = Version::new(3, 0);
/// Version 4.0. Introduced a new message format.
pub const TIMESYNC_VERSION_4: Version = Version::new(4, 0);

/// Timesync messages used before version 4.0.
#[repr(C)]
#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
pub struct TimesyncMessage {
    /// The time of day measured in the parent, in UTC (or TAI? unclear).
    pub parent_time: U64LE,
    /// Unused.
    pub child_time: U64LE,
    /// The measured round trip time by the parent.
    pub round_trip_time: U64LE,
    /// Flags indicating the message's purpose.
    pub flags: TimesyncFlags,
    /// Reserved.
    pub reserved: [u8; 3],
}

/// Timesync messages used in version 4.0 and later.
#[repr(C)]
#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
pub struct TimesyncMessageV4 {
    /// The wall clock time measured in the parent, in UTC (or TAI? unclear).
    pub parent_time: U64LE,
    /// The VM reference time of the child, at the time the parent measured the
    /// wall clock time.
    pub vm_reference_time: u64,
    /// Flags indicating the message's purpose.
    pub flags: TimesyncFlags,
    /// The NTP leap indicator.
    pub leap_indicator: u8,
    /// The NTP stratum.
    pub stratum: u8,
    /// Reserved.
    pub reserved: [u8; 5],
}

/// Flags for timesync messages.
#[bitfield(u8)]
#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
pub struct TimesyncFlags {
    /// This is a sync message.
    pub sync: bool,
    /// This is a sample message.
    pub sample: bool,
    #[bits(6)]
    _rsvd: u8,
}