Add clock wrapper class
This commit is contained in:
1 parent
f7bbb32788
commit
c1286b0ba9
7 files changed
+67
-45
No files matched your search
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ extern crate core;
|
|||||||
|
|
||||||
mod baseliner;
|
mod baseliner;
|
||||||
mod cake;
|
mod cake;
|
||||||
|
mod clock;
|
||||||
mod config;
|
mod config;
|
||||||
mod endian;
|
mod endian;
|
||||||
mod log;
|
mod log;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use crate::MeasurementType;
|
use crate::MeasurementType;
|
||||||
use etherparse::ReadError;
|
use etherparse::ReadError;
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
use nix::errno::Errno;
|
|
||||||
use socket2::{Domain, Protocol, SockAddr, Socket, Type};
|
use socket2::{Domain, Protocol, SockAddr, Socket, Type};
|
||||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@@ -23,8 +22,6 @@ pub enum PingError {
|
|||||||
InvalidType(String),
|
InvalidType(String),
|
||||||
#[error("No transport")]
|
#[error("No transport")]
|
||||||
NoTransport,
|
NoTransport,
|
||||||
#[error("Socket error")]
|
|
||||||
Socket(#[from] Errno),
|
|
||||||
#[error("Wrong ICMP identifier (expected {expected:?}, found {found:?})")]
|
#[error("Wrong ICMP identifier (expected {expected:?}, found {found:?})")]
|
||||||
WrongID { expected: u16, found: u16 },
|
WrongID { expected: u16, found: u16 },
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-11
@@ -1,11 +1,11 @@
|
|||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
|
||||||
|
use crate::clock::Clock;
|
||||||
use crate::pinger::{PingError, PingListener, PingReply, PingSender};
|
use crate::pinger::{PingError, PingListener, PingReply, PingSender};
|
||||||
use byteorder::*;
|
use byteorder::*;
|
||||||
use etherparse::TransportSlice::{Icmpv4, Icmpv6};
|
use etherparse::TransportSlice::{Icmpv4, Icmpv6};
|
||||||
use etherparse::{IcmpEchoHeader, Icmpv4Header, Icmpv4Type, SlicedPacket};
|
use etherparse::{IcmpEchoHeader, Icmpv4Header, Icmpv4Type, SlicedPacket};
|
||||||
use nix::sys::time::TimeValLike;
|
use rustix::thread::ClockId;
|
||||||
use nix::time::{clock_gettime, ClockId};
|
|
||||||
|
|
||||||
pub struct PingerICMPEchoListener {}
|
pub struct PingerICMPEchoListener {}
|
||||||
|
|
||||||
@@ -32,11 +32,10 @@ impl PingListener for PingerICMPEchoListener {
|
|||||||
.expect("Couldn't parse payload to time")
|
.expect("Couldn't parse payload to time")
|
||||||
as i64;
|
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: i64 = time_ms - time_sent;
|
||||||
|
|
||||||
let rtt = time_ms - time_sent;
|
|
||||||
return Ok(PingReply {
|
return Ok(PingReply {
|
||||||
reflector,
|
reflector,
|
||||||
seq: echo.seq,
|
seq: echo.seq,
|
||||||
@@ -47,8 +46,8 @@ impl PingListener for PingerICMPEchoListener {
|
|||||||
originate_timestamp: 0,
|
originate_timestamp: 0,
|
||||||
receive_timestamp: 0,
|
receive_timestamp: 0,
|
||||||
transmit_timestamp: 0,
|
transmit_timestamp: 0,
|
||||||
last_receive_time_s: time_now.tv_sec() as f64
|
last_receive_time_s: clock.get_seconds() as f64
|
||||||
+ (time_now.tv_nsec() as f64 / 1e9),
|
+ (clock.get_nanoseconds() as f64 / 1e9),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
type_ => {
|
type_ => {
|
||||||
@@ -71,9 +70,9 @@ impl PingListener for PingerICMPEchoListener {
|
|||||||
|
|
||||||
impl PingSender for PingerICMPEchoSender {
|
impl PingSender for PingerICMPEchoSender {
|
||||||
fn craft_packet(&self, id: u16, seq: u16) -> Vec<u8> {
|
fn craft_packet(&self, id: u16, seq: u16) -> Vec<u8> {
|
||||||
let time = clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap();
|
let clock = Clock::new(ClockId::Monotonic);
|
||||||
let time_u64: u64 = time.num_milliseconds() as u64;
|
let time_ms = clock.to_milliseconds();
|
||||||
let payload = time_u64.to_ne_bytes();
|
let payload = time_ms.to_ne_bytes();
|
||||||
|
|
||||||
// Construct a header with checksum based on the payload
|
// Construct a header with checksum based on the payload
|
||||||
let hdr = Icmpv4Header::with_checksum(
|
let hdr = Icmpv4Header::with_checksum(
|
||||||
|
|||||||
+9
-13
@@ -1,12 +1,11 @@
|
|||||||
use std::net::IpAddr;
|
use crate::clock::Clock;
|
||||||
|
|
||||||
use crate::endian::ToNativeEndian;
|
use crate::endian::ToNativeEndian;
|
||||||
use crate::pinger::{PingError, PingListener, PingReply, PingSender};
|
use crate::pinger::{PingError, PingListener, PingReply, PingSender};
|
||||||
use crate::utils::Utils;
|
|
||||||
use etherparse::icmpv4::TimestampMessage;
|
use etherparse::icmpv4::TimestampMessage;
|
||||||
use etherparse::TransportSlice::{Icmpv4, Icmpv6};
|
use etherparse::TransportSlice::{Icmpv4, Icmpv6};
|
||||||
use etherparse::{Icmpv4Header, Icmpv4Type, SlicedPacket};
|
use etherparse::{Icmpv4Header, Icmpv4Type, SlicedPacket};
|
||||||
use nix::time::{clock_gettime, ClockId};
|
use rustix::thread::ClockId;
|
||||||
|
use std::net::IpAddr;
|
||||||
|
|
||||||
pub struct PingerICMPTimestampListener {}
|
pub struct PingerICMPTimestampListener {}
|
||||||
|
|
||||||
@@ -27,9 +26,8 @@ impl PingListener for PingerICMPTimestampListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let time_now = clock_gettime(ClockId::CLOCK_REALTIME).unwrap();
|
let time_now = Clock::new(ClockId::Realtime);
|
||||||
let time_since_midnight: i64 = (time_now.tv_sec() as i64 % 86400 * 1000)
|
let time_since_midnight = time_now.get_time_since_midnight();
|
||||||
+ (time_now.tv_nsec() as i64 / 1000000);
|
|
||||||
|
|
||||||
let originate_timestamp = reply.originate_timestamp.to_ne();
|
let originate_timestamp = reply.originate_timestamp.to_ne();
|
||||||
let receive_timestamp = reply.receive_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,
|
originate_timestamp: originate_timestamp as i64,
|
||||||
receive_timestamp: receive_timestamp as i64,
|
receive_timestamp: receive_timestamp as i64,
|
||||||
transmit_timestamp: transmit_timestamp as i64,
|
transmit_timestamp: transmit_timestamp as i64,
|
||||||
last_receive_time_s: time_now.tv_sec() as f64
|
last_receive_time_s: time_now.get_seconds() as f64
|
||||||
+ (time_now.tv_nsec() as f64 / 1e9),
|
+ (time_now.get_nanoseconds() as f64 / 1e9),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
type_ => {
|
type_ => {
|
||||||
@@ -73,9 +71,7 @@ impl PingListener for PingerICMPTimestampListener {
|
|||||||
|
|
||||||
impl PingSender for PingerICMPTimestampSender {
|
impl PingSender for PingerICMPTimestampSender {
|
||||||
fn craft_packet(&self, id: u16, seq: u16) -> Vec<u8> {
|
fn craft_packet(&self, id: u16, seq: u16) -> Vec<u8> {
|
||||||
let time = clock_gettime(ClockId::CLOCK_REALTIME).unwrap();
|
let time_since_midnight = Clock::new(ClockId::Realtime).get_time_since_midnight();
|
||||||
let time_since_midnight: u32 =
|
|
||||||
((time.tv_sec() % 86400 * 1000) + (time.tv_nsec() / 1000000)) as u32;
|
|
||||||
|
|
||||||
let payload: [u8; 0] = [];
|
let payload: [u8; 0] = [];
|
||||||
|
|
||||||
@@ -84,7 +80,7 @@ impl PingSender for PingerICMPTimestampSender {
|
|||||||
Icmpv4Type::TimestampRequest(TimestampMessage {
|
Icmpv4Type::TimestampRequest(TimestampMessage {
|
||||||
id,
|
id,
|
||||||
seq,
|
seq,
|
||||||
originate_timestamp: time_since_midnight,
|
originate_timestamp: time_since_midnight as u32,
|
||||||
receive_timestamp: 0,
|
receive_timestamp: 0,
|
||||||
transmit_timestamp: 0,
|
transmit_timestamp: 0,
|
||||||
}),
|
}),
|
||||||
|
|||||||
+13
-4
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::clock::Clock;
|
||||||
use crate::netlink::{Netlink, NetlinkError, Qdisc};
|
use crate::netlink::{Netlink, NetlinkError, Qdisc};
|
||||||
use crate::utils::Utils;
|
use crate::utils::Utils;
|
||||||
use crate::{Config, ReflectorStats};
|
use crate::{Config, ReflectorStats};
|
||||||
@@ -5,6 +6,7 @@ use log::{debug, error, info, warn};
|
|||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
use rustix::thread::ClockId;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
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_ns = ((self.config.min_change_interval % 1.0) * 1e9) as u32;
|
||||||
let sleep_time = Duration::new(sleep_time_s, sleep_time_ns);
|
let sleep_time = Duration::new(sleep_time_s, sleep_time_ns);
|
||||||
|
|
||||||
let (start_s, _) = Utils::get_current_time()?;
|
let time = Clock::new(ClockId::Monotonic);
|
||||||
let (mut lastchg_s, mut lastchg_ns) = Utils::get_current_time()?;
|
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 lastchg_t = lastchg_s - start_s + lastchg_ns / 1e9;
|
||||||
let mut lastdump_t = lastchg_t - 310.0;
|
let mut lastdump_t = lastchg_t - 310.0;
|
||||||
|
|
||||||
@@ -331,7 +335,8 @@ impl Ratecontroller {
|
|||||||
loop {
|
loop {
|
||||||
sleep(sleep_time);
|
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;
|
let now_abstime = now_s + now_ns / 1e9;
|
||||||
now_s = now_s - start_s;
|
now_s = now_s - start_s;
|
||||||
let now_t = now_s + now_ns / 1e9;
|
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_dl.current_rate = self.state_dl.next_rate;
|
||||||
self.state_ul.current_rate = self.state_ul.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!(
|
debug!(
|
||||||
"{},{},{},{},{},{},{},{}",
|
"{},{},{},{},{},{},{},{}",
|
||||||
|
|||||||
@@ -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 {}
|
pub struct Utils {}
|
||||||
|
|
||||||
impl Utils {
|
impl Utils {
|
||||||
@@ -17,8 +7,4 @@ impl Utils {
|
|||||||
pub fn ewma_factor(tick: f64, dur: f64) -> f64 {
|
pub fn ewma_factor(tick: f64, dur: f64) -> f64 {
|
||||||
((0.5_f64).ln() / (dur / tick)).exp()
|
((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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user