| 1 | //! Logic for steering OS clocks, aimed at NTP and PTP. |
| 2 | //! |
| 3 | //! This code is used in our implementations of NTP [ntpd-rs](https://github.com/pendulum-project/ntpd-rs) and PTP [statime](https://github.com/pendulum-project/statime). |
| 4 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::timerust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#; |
| 5 | |
| 6 | #[cfg(unix)] |
| 7 | pub mod unixrust-analyzer cargo clock-steering 0.2.1 unix/; |
| 8 | |
| 9 | /// A moment in time. |
| 10 | /// |
| 11 | /// The format makes it easy to convert into libc data structures, and supports subnanoseconds that |
| 12 | /// certain hardware can provide for additional precision. The value is an offset from the [unix epoch](https://en.wikipedia.org/wiki/Unix_time). |
| 13 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 14 | pub struct Timestamprust-analyzer cargo clock-steering 0.2.1 Timestamp# { |
| 15 | pub secondsrust-analyzer cargo clock-steering 0.2.1 Timestamp#seconds.: libcrust-analyzer cargo libc 0.2.189 crate/::time_trust-analyzer cargo libc 0.2.189 unix/linux_like/linux/gnu/b64/time_t#, |
| 16 | /// Nanos must be between 0 and 999999999 inclusive |
| 17 | pub nanosrust-analyzer cargo clock-steering 0.2.1 Timestamp#nanos.: u32, |
| 18 | } |
| 19 | |
| 20 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 21 | pub struct TimeOffsetrust-analyzer cargo clock-steering 0.2.1 TimeOffset# { |
| 22 | pub secondsrust-analyzer cargo clock-steering 0.2.1 TimeOffset#seconds.: libcrust-analyzer cargo libc 0.2.189 crate/::time_trust-analyzer cargo libc 0.2.189 unix/linux_like/linux/gnu/b64/time_t#, |
| 23 | /// Nanos must be between 0 and 999999999 inclusive |
| 24 | pub nanosrust-analyzer cargo clock-steering 0.2.1 TimeOffset#nanos.: u32, |
| 25 | } |
| 26 | |
| 27 | /// Indicate whether a leap second must be applied |
| 28 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 29 | pub enum LeapIndicatorrust-analyzer cargo clock-steering 0.2.1 LeapIndicator# { |
| 30 | /// No leap second warning |
| 31 | #[defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/default:] |
| 32 | NoWarningrust-analyzer cargo clock-steering 0.2.1 LeapIndicator#NoWarning#, |
| 33 | /// Last minute of the day has 61 seconds |
| 34 | Leap61rust-analyzer cargo clock-steering 0.2.1 LeapIndicator#Leap61#, |
| 35 | /// Last minute of the day has 59 seconds |
| 36 | Leap59rust-analyzer cargo clock-steering 0.2.1 LeapIndicator#Leap59#, |
| 37 | /// Unknown leap second status (the clock is unsynchronized) |
| 38 | Unknownrust-analyzer cargo clock-steering 0.2.1 LeapIndicator#Unknown#, |
| 39 | } |
| 40 | |
| 41 | /// Trait for reading information from and modifying an OS clock |
| 42 | pub trait Clockrust-analyzer cargo clock-steering 0.2.1 Clock# { |
| 43 | type Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#; |
| 44 | |
| 45 | // feature(error_in_core) https://github.com/rust-lang/rust/issues/103765 |
| 46 | // type Error: core::error::Error; |
| 47 | |
| 48 | /// Get the current time. |
| 49 | fn nowrust-analyzer cargo clock-steering 0.2.1 Clock#now().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Timestamprust-analyzer cargo clock-steering 0.2.1 Timestamp#, Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 50 | |
| 51 | /// Get the clock's resolution. |
| 52 | /// |
| 53 | /// The output [`Timestamp`] will be all zeros when the resolution is |
| 54 | /// unavailable. |
| 55 | fn resolutionrust-analyzer cargo clock-steering 0.2.1 Clock#resolution().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Timestamprust-analyzer cargo clock-steering 0.2.1 Timestamp#, Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 56 | |
| 57 | /// Change the frequency of the clock. |
| 58 | /// Returns the time at which the change was applied. |
| 59 | /// |
| 60 | /// The unit of the input is milliseconds (of drift) per second, |
| 61 | /// compared to the "natural" frequency of the clock. |
| 62 | fn set_frequencyrust-analyzer cargo clock-steering 0.2.1 Clock#set_frequency().(&self, frequency: f64) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Timestamprust-analyzer cargo clock-steering 0.2.1 Timestamp#, Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 63 | |
| 64 | /// Get the frequency of the clock |
| 65 | /// The unit of the output is milliseconds (of drift) per second, |
| 66 | /// compared to the "natural" frequency of the clock. |
| 67 | fn get_frequencyrust-analyzer cargo clock-steering 0.2.1 Clock#get_frequency().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<f64, Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 68 | |
| 69 | /// Change the current time of the clock by an offset. |
| 70 | /// Returns the time at which the change was applied. |
| 71 | fn step_clockrust-analyzer cargo clock-steering 0.2.1 Clock#step_clock().(&self, offset: TimeOffsetrust-analyzer cargo clock-steering 0.2.1 TimeOffset#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Timestamprust-analyzer cargo clock-steering 0.2.1 Timestamp#, Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 72 | |
| 73 | /// Change the indicators for upcoming leap seconds. |
| 74 | fn set_leap_secondsrust-analyzer cargo clock-steering 0.2.1 Clock#set_leap_seconds().(&self, leap_status: LeapIndicatorrust-analyzer cargo clock-steering 0.2.1 LeapIndicator#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 75 | |
| 76 | /// Disable all standard NTP kernel clock discipline. It is all your responsibility now. |
| 77 | /// |
| 78 | /// The disabled settings are: |
| 79 | /// |
| 80 | /// - [`libc::STA_PLL`]: kernel phase-locked loop |
| 81 | /// - [`libc::STA_FLL`]: kernel frequency-locked loop |
| 82 | /// - [`libc::STA_PPSTIME`]: pulse-per-second time |
| 83 | /// - [`libc::STA_PPSFREQ`]: pulse-per-second frequency discipline |
| 84 | fn disable_kernel_ntp_algorithmrust-analyzer cargo clock-steering 0.2.1 Clock#disable_kernel_ntp_algorithm().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 85 | |
| 86 | /// Set the offset between TAI and UTC. |
| 87 | fn set_tairust-analyzer cargo clock-steering 0.2.1 Clock#set_tai().(&self, tai_offset: i32) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 88 | |
| 89 | /// Get the offset between TAI and UTC. |
| 90 | fn get_tairust-analyzer cargo clock-steering 0.2.1 Clock#get_tai().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<i32, Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 91 | |
| 92 | /// Provide the system with the current best estimates for the statistical |
| 93 | /// error of the clock, and the maximum deviation due to frequency error and |
| 94 | /// distance to the root clock. |
| 95 | fn error_estimate_updaterust-analyzer cargo clock-steering 0.2.1 Clock#error_estimate_update().( |
| 96 | &self, |
| 97 | estimated_error: Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, |
| 98 | maximum_error: Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, |
| 99 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Self::Errorrust-analyzer cargo clock-steering 0.2.1 Clock#Error#>; |
| 100 | } |