From c1286b0ba9d3ae5ec876b17072e9ba49f21d1542 Mon Sep 17 00:00:00 2001 From: Nils Andreas Svee Date: Sat, 15 Oct 2022 13:44:25 +0200 Subject: [PATCH] Add clock wrapper class --- src/clock.rs | 34 ++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/pinger.rs | 3 --- src/pinger_icmp.rs | 21 ++++++++++----------- src/pinger_icmp_ts.rs | 22 +++++++++------------- src/ratecontroller.rs | 17 +++++++++++++---- src/utils.rs | 14 -------------- 7 files changed, 67 insertions(+), 45 deletions(-) create mode 100644 src/clock.rs diff --git a/src/clock.rs b/src/clock.rs new file mode 100644 index 0000000..c04f600 --- /dev/null +++ b/src/clock.rs @@ -0,0 +1,34 @@ +use rustix::fs::Timespec; +use rustix::thread::ClockId; +use rustix::time::clock_gettime; + +pub struct Clock { + time_s: u64, + time_ns: u64, +} + +impl Clock { + pub fn new(id: ClockId) -> Self { + let time: Timespec = clock_gettime(id); + Self { + time_s: time.tv_sec as u64, + time_ns: time.tv_nsec as u64, + } + } + + pub fn get_seconds(&self) -> u64 { + self.time_s + } + + pub fn get_nanoseconds(&self) -> u64 { + self.time_ns + } + + pub fn get_time_since_midnight(&self) -> i64 { + (self.time_s as i64 % 86400 * 1000) + (self.time_ns as i64 / 1000000) + } + + pub fn to_milliseconds(&self) -> u64 { + (self.time_s * 1000) + (self.time_ns / 1000000) + } +} diff --git a/src/main.rs b/src/main.rs index 92ca615..9381611 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate core; mod baseliner; mod cake; +mod clock; mod config; mod endian; mod log; diff --git a/src/pinger.rs b/src/pinger.rs index 12e312e..3f18949 100644 --- a/src/pinger.rs +++ b/src/pinger.rs @@ -1,7 +1,6 @@ use crate::MeasurementType; use etherparse::ReadError; use log::{debug, error}; -use nix::errno::Errno; use socket2::{Domain, Protocol, SockAddr, Socket, Type}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; use std::str::FromStr; @@ -23,8 +22,6 @@ pub enum PingError { InvalidType(String), #[error("No transport")] NoTransport, - #[error("Socket error")] - Socket(#[from] Errno), #[error("Wrong ICMP identifier (expected {expected:?}, found {found:?})")] WrongID { expected: u16, found: u16 }, } diff --git a/src/pinger_icmp.rs b/src/pinger_icmp.rs index 412ea94..fd9329a 100644 --- a/src/pinger_icmp.rs +++ b/src/pinger_icmp.rs @@ -1,11 +1,11 @@ use std::net::IpAddr; +use crate::clock::Clock; use crate::pinger::{PingError, PingListener, PingReply, PingSender}; use byteorder::*; use etherparse::TransportSlice::{Icmpv4, Icmpv6}; use etherparse::{IcmpEchoHeader, Icmpv4Header, Icmpv4Type, SlicedPacket}; -use nix::sys::time::TimeValLike; -use nix::time::{clock_gettime, ClockId}; +use rustix::thread::ClockId; pub struct PingerICMPEchoListener {} @@ -32,11 +32,10 @@ impl PingListener for PingerICMPEchoListener { .expect("Couldn't parse payload to time") as i64; - let time_now = clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap(); + let clock = Clock::new(ClockId::Monotonic); + let time_ms = clock.to_milliseconds() as i64; - let time_ms = time_now.num_milliseconds(); - - let rtt = time_ms - time_sent; + let rtt: i64 = time_ms - time_sent; return Ok(PingReply { reflector, seq: echo.seq, @@ -47,8 +46,8 @@ impl PingListener for PingerICMPEchoListener { originate_timestamp: 0, receive_timestamp: 0, transmit_timestamp: 0, - last_receive_time_s: time_now.tv_sec() as f64 - + (time_now.tv_nsec() as f64 / 1e9), + last_receive_time_s: clock.get_seconds() as f64 + + (clock.get_nanoseconds() as f64 / 1e9), }); } type_ => { @@ -71,9 +70,9 @@ impl PingListener for PingerICMPEchoListener { impl PingSender for PingerICMPEchoSender { fn craft_packet(&self, id: u16, seq: u16) -> Vec { - let time = clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap(); - let time_u64: u64 = time.num_milliseconds() as u64; - let payload = time_u64.to_ne_bytes(); + let clock = Clock::new(ClockId::Monotonic); + let time_ms = clock.to_milliseconds(); + let payload = time_ms.to_ne_bytes(); // Construct a header with checksum based on the payload let hdr = Icmpv4Header::with_checksum( diff --git a/src/pinger_icmp_ts.rs b/src/pinger_icmp_ts.rs index fe24385..b4ac07d 100644 --- a/src/pinger_icmp_ts.rs +++ b/src/pinger_icmp_ts.rs @@ -1,12 +1,11 @@ -use std::net::IpAddr; - +use crate::clock::Clock; use crate::endian::ToNativeEndian; use crate::pinger::{PingError, PingListener, PingReply, PingSender}; -use crate::utils::Utils; use etherparse::icmpv4::TimestampMessage; use etherparse::TransportSlice::{Icmpv4, Icmpv6}; use etherparse::{Icmpv4Header, Icmpv4Type, SlicedPacket}; -use nix::time::{clock_gettime, ClockId}; +use rustix::thread::ClockId; +use std::net::IpAddr; pub struct PingerICMPTimestampListener {} @@ -27,9 +26,8 @@ impl PingListener for PingerICMPTimestampListener { }); } - let time_now = clock_gettime(ClockId::CLOCK_REALTIME).unwrap(); - let time_since_midnight: i64 = (time_now.tv_sec() as i64 % 86400 * 1000) - + (time_now.tv_nsec() as i64 / 1000000); + let time_now = Clock::new(ClockId::Realtime); + let time_since_midnight = time_now.get_time_since_midnight(); let originate_timestamp = reply.originate_timestamp.to_ne(); let receive_timestamp = reply.receive_timestamp.to_ne(); @@ -49,8 +47,8 @@ impl PingListener for PingerICMPTimestampListener { originate_timestamp: originate_timestamp as i64, receive_timestamp: receive_timestamp as i64, transmit_timestamp: transmit_timestamp as i64, - last_receive_time_s: time_now.tv_sec() as f64 - + (time_now.tv_nsec() as f64 / 1e9), + last_receive_time_s: time_now.get_seconds() as f64 + + (time_now.get_nanoseconds() as f64 / 1e9), }); } type_ => { @@ -73,9 +71,7 @@ impl PingListener for PingerICMPTimestampListener { impl PingSender for PingerICMPTimestampSender { fn craft_packet(&self, id: u16, seq: u16) -> Vec { - let time = clock_gettime(ClockId::CLOCK_REALTIME).unwrap(); - let time_since_midnight: u32 = - ((time.tv_sec() % 86400 * 1000) + (time.tv_nsec() / 1000000)) as u32; + let time_since_midnight = Clock::new(ClockId::Realtime).get_time_since_midnight(); let payload: [u8; 0] = []; @@ -84,7 +80,7 @@ impl PingSender for PingerICMPTimestampSender { Icmpv4Type::TimestampRequest(TimestampMessage { id, seq, - originate_timestamp: time_since_midnight, + originate_timestamp: time_since_midnight as u32, receive_timestamp: 0, transmit_timestamp: 0, }), diff --git a/src/ratecontroller.rs b/src/ratecontroller.rs index 58a14e9..bd57d7f 100644 --- a/src/ratecontroller.rs +++ b/src/ratecontroller.rs @@ -1,3 +1,4 @@ +use crate::clock::Clock; use crate::netlink::{Netlink, NetlinkError, Qdisc}; use crate::utils::Utils; use crate::{Config, ReflectorStats}; @@ -5,6 +6,7 @@ use log::{debug, error, info, warn}; use rand::seq::SliceRandom; use rand::thread_rng; use rand::RngCore; +use rustix::thread::ClockId; use std::collections::HashMap; use std::fs::File; use std::io::Write; @@ -281,8 +283,10 @@ impl Ratecontroller { let sleep_time_ns = ((self.config.min_change_interval % 1.0) * 1e9) as u32; let sleep_time = Duration::new(sleep_time_s, sleep_time_ns); - let (start_s, _) = Utils::get_current_time()?; - let (mut lastchg_s, mut lastchg_ns) = Utils::get_current_time()?; + let time = Clock::new(ClockId::Monotonic); + let start_s = time.get_seconds() as f64; + let (mut lastchg_s, mut lastchg_ns) = + (time.get_seconds() as f64, time.get_nanoseconds() as f64); let mut lastchg_t = lastchg_s - start_s + lastchg_ns / 1e9; let mut lastdump_t = lastchg_t - 310.0; @@ -331,7 +335,8 @@ impl Ratecontroller { loop { sleep(sleep_time); - let (mut now_s, now_ns) = Utils::get_current_time()?; + let now = Clock::new(ClockId::Monotonic); + let (mut now_s, now_ns) = (now.get_seconds() as f64, now.get_nanoseconds() as f64); let now_abstime = now_s + now_ns / 1e9; now_s = now_s - start_s; let now_t = now_s + now_ns / 1e9; @@ -374,7 +379,11 @@ impl Ratecontroller { self.state_dl.current_rate = self.state_dl.next_rate; self.state_ul.current_rate = self.state_ul.next_rate; - (lastchg_s, lastchg_ns) = Utils::get_current_time()?; + let lastchg = Clock::new(ClockId::Monotonic); + (lastchg_s, lastchg_ns) = ( + lastchg.get_seconds() as f64, + lastchg.get_nanoseconds() as f64, + ); debug!( "{},{},{},{},{},{},{},{}", diff --git a/src/utils.rs b/src/utils.rs index 7bd013b..aa35045 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,13 +1,3 @@ -use nix::errno::Errno; -use nix::time::{clock_gettime, ClockId}; -use thiserror::Error; - -#[derive(Debug, Error)] -pub enum UtilError { - #[error("libc error")] - Libc(#[from] Errno), -} - pub struct Utils {} impl Utils { @@ -17,8 +7,4 @@ impl Utils { pub fn ewma_factor(tick: f64, dur: f64) -> f64 { ((0.5_f64).ln() / (dur / tick)).exp() } - pub fn get_current_time() -> Result<(f64, f64), UtilError> { - let time = clock_gettime(ClockId::CLOCK_MONOTONIC)?; - Ok((time.tv_sec() as f64, time.tv_nsec() as f64)) - } }