Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: virtual (overlay) system clock #504

Merged
merged 10 commits into from
Sep 20, 2024
3 changes: 3 additions & 0 deletions docs/man/statime.toml.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ will be indicated by each configuration setting shown.
`path-trace` = *bool*
: The instance uses the path trace option. This allows detecting clock loops when enabled on all instances in the network.

`virtual-system-clock` = *bool* (**false**)
: Use a virtual overlay clock instead of adjusting the system clock.

## `[[port]]`

`interface` = *interface name*
Expand Down
2 changes: 1 addition & 1 deletion docs/precompiled/man/statime.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" Automatically generated by Pandoc 3.2
.\" Automatically generated by Pandoc 3.4
.\"
.TH "STATIME" "8" "" "statime 0.2.1" "statime"
.SH NAME
Expand Down
5 changes: 4 additions & 1 deletion docs/precompiled/man/statime.toml.5
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" Automatically generated by Pandoc 3.2
.\" Automatically generated by Pandoc 3.4
.\"
.TH "STATIME.TOML" "5" "" "statime 0.2.1" "statime"
.SH NAME
Expand Down Expand Up @@ -42,6 +42,9 @@ A tie breaker for the best master clock algorithm in the range
A tie breaker for the best master clock algorithm in the range
\f[CR]0..256\f[R].
\f[CR]0\f[R] being highest priority an \f[CR]255\f[R] the lowest.
.TP
\f[CR]virtual\-system\-clock\f[R] = \f[I]bool\f[R] (\f[B]false\f[R])
Use a virtual overlay clock instead of adjusting the system clock.
.SS \f[CR][[port]]\f[R]
.TP
\f[CR]interface\f[R] = \f[I]interface name\f[R]
Expand Down
28 changes: 27 additions & 1 deletion statime-linux/src/clock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clock_steering::{unix::UnixClock, TimeOffset};
use statime::{
config::{LeapIndicator, TimePropertiesDS},
time::{Duration, Time},
Clock,
Clock, OverlayClock, SharedClock,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -172,3 +172,29 @@ impl Clock for LinuxClock {
pub fn libc_timespec_into_instant(spec: libc::timespec) -> Time {
Time::from_fixed_nanos(spec.tv_sec as i128 * 1_000_000_000i128 + spec.tv_nsec as i128)
}

pub trait PortTimestampToTime {
fn port_timestamp_to_time(&self, ts: timestamped_socket::socket::Timestamp) -> Time;
}

impl PortTimestampToTime for LinuxClock {
fn port_timestamp_to_time(&self, mut ts: timestamped_socket::socket::Timestamp) -> Time {
// get_tai gives zero if this is a hardware clock, and the needed
// correction when this port uses software timestamping
ts.seconds += self.get_tai_offset().expect("Unable to get tai offset") as libc::time_t;
Time::from_fixed_nanos(ts.seconds as i128 * 1_000_000_000i128 + ts.nanos as i128)
}
}

impl PortTimestampToTime for OverlayClock<LinuxClock> {
fn port_timestamp_to_time(&self, ts: timestamped_socket::socket::Timestamp) -> Time {
let roclock_time = self.underlying().port_timestamp_to_time(ts);
self.time_from_underlying(roclock_time)
}
}

impl PortTimestampToTime for SharedClock<OverlayClock<LinuxClock>> {
fn port_timestamp_to_time(&self, ts: timestamped_socket::socket::Timestamp) -> Time {
self.0.lock().unwrap().port_timestamp_to_time(ts)
}
}
3 changes: 3 additions & 0 deletions statime-linux/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct Config {
pub ports: Vec<PortConfig>,
#[serde(default)]
pub observability: ObservabilityConfig,
#[serde(default)]
pub virtual_system_clock: bool,
}

#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -285,6 +287,7 @@ interface = "enp0s31f6"
path_trace: false,
ports: vec![expected_port],
observability: ObservabilityConfig::default(),
virtual_system_clock: false,
};

let actual = toml::from_str(MINIMAL_CONFIG).unwrap();
Expand Down
Loading
Loading