use std::collections::VecDeque;
use std::ffi::c_int;
use std::time::Duration;
use crate::readiness::Deadline;
use crate::scope::SimShared;
use crate::sockets::SockRec;
#[cfg(target_os = "linux")]
#[derive(Default)]
pub(crate) struct RxStartup {
delay: std::sync::OnceLock<Duration>,
active_at: std::sync::OnceLock<Duration>,
}
#[cfg(target_os = "linux")]
impl RxStartup {
pub(crate) fn set_delay(&self, delay: Duration) {
self.delay
.set(delay)
.expect("timestamp startup configured once");
}
fn enable(&self, shared: &SimShared) {
let delay = self.delay.get().copied().unwrap_or_default();
if !delay.is_zero() {
self.active_at
.get_or_init(|| shared.tstamp_now().real.saturating_add(delay));
}
}
fn stamps(&self, at: Duration) -> bool {
self.delay.get().is_none_or(Duration::is_zero)
|| self.active_at.get().is_some_and(|active| at >= *active)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Stamp {
pub(crate) real: Duration,
#[cfg_attr(target_os = "linux", allow(dead_code))]
pub(crate) mono: Duration,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub(crate) hw: Option<Duration>,
}
impl Stamp {
#[cfg(target_os = "linux")]
pub(crate) fn from_real(real: Duration) -> Self {
Stamp {
real,
mono: Duration::ZERO,
hw: None,
}
}
#[cfg(target_os = "linux")]
pub(crate) fn hardware(hw: Duration) -> Self {
Stamp {
real: Duration::ZERO,
mono: Duration::ZERO,
hw: Some(hw),
}
}
pub(crate) fn later(self, delay: Duration) -> Self {
Stamp {
real: self.real.saturating_add(delay),
mono: self.mono.saturating_add(delay),
hw: None,
}
}
}
impl SimShared {
pub(crate) fn tstamp_now(&self) -> Stamp {
match &self.clock {
Some(clock) => {
let mono = Duration::from_nanos(clock.monotonic());
Stamp {
real: clock.base_realtime() + mono,
mono,
hw: None,
}
}
None => snare_interpose::real(real_clocks),
}
}
pub(crate) fn monotonic_now(&self) -> Duration {
match &self.clock {
Some(clock) => Duration::from_nanos(clock.monotonic()),
None => snare_interpose::real(|| {
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts) };
Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32)
}),
}
}
pub(crate) fn reach_stamp(&self, stamp: Stamp) {
if let Some(clock) = &self.clock {
clock.reach_realtime(stamp.real);
}
}
}
fn real_clocks() -> Stamp {
let real = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default();
#[cfg(target_os = "macos")]
let mono = {
let ticks = unsafe { mac::mach_absolute_time() };
mac::ticks_to_nanos(ticks)
};
#[cfg(not(target_os = "macos"))]
let mono = Duration::ZERO;
Stamp {
real,
mono,
hw: None,
}
}
pub(crate) struct TxReport {
#[cfg(target_os = "linux")]
pub(crate) charge: usize,
#[cfg(target_os = "linux")]
charged: bool,
#[cfg(target_os = "linux")]
order: crate::sockets::ErrorOrder,
#[cfg(target_os = "linux")]
wake_counted: bool,
#[cfg_attr(target_os = "macos", allow(dead_code))]
key: u32,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
stage: u32,
#[cfg_attr(target_os = "macos", allow(dead_code))]
at: Stamp,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
visible: Option<Deadline>,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
packet: Vec<u8>,
}
#[derive(Default)]
pub(crate) struct TsState {
#[cfg(target_os = "linux")]
report_wakes: u64,
rcv: bool,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
rcv_ns: bool,
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
mono: bool,
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
cont: bool,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
flags: u32,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
bind_phc: c_int,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
tskey: u32,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
written: u32,
#[cfg_attr(target_os = "macos", allow(dead_code))]
txq: VecDeque<TxReport>,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
txtime: Option<(c_int, u32)>,
}
impl TsState {
pub(crate) fn has_tx_reports(&self) -> bool {
!self.txq.is_empty()
}
pub(crate) fn pending_time(&self) -> bool {
self.txq.iter().any(|report| report.visible.is_some())
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Rx {
Datagram,
Stream,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
ErrQueue,
}
pub(crate) type Cmsg = (c_int, c_int, Vec<u8>);
#[cfg(target_os = "linux")]
fn timeval(t: Duration) -> Vec<u8> {
let mut tv: libc::timeval = unsafe { std::mem::zeroed() };
tv.tv_sec = t.as_secs() as libc::time_t;
tv.tv_usec = t.subsec_micros() as libc::suseconds_t;
unsafe {
std::slice::from_raw_parts(
(&raw const tv).cast::<u8>(),
std::mem::size_of::<libc::timeval>(),
)
}
.to_vec()
}
#[cfg(target_os = "macos")]
fn timeval(t: Duration) -> Vec<u8> {
let seconds = (t.as_secs() as libc::time_t).to_ne_bytes();
let micros = (t.subsec_micros() as libc::suseconds_t).to_ne_bytes();
let mut bytes = vec![0; size_of::<libc::timeval>()];
let seconds_at = std::mem::offset_of!(libc::timeval, tv_sec);
let micros_at = std::mem::offset_of!(libc::timeval, tv_usec);
bytes[seconds_at..seconds_at + seconds.len()].copy_from_slice(&seconds);
bytes[micros_at..micros_at + micros.len()].copy_from_slice(µs);
bytes
}
#[cfg(all(test, target_os = "macos"))]
#[test]
fn timeval_bytes_have_zero_tail_padding() {
for (seconds, micros) in [(0i64, 0i32), (1_700_000_000, 250), (i64::MAX, 999_999)] {
let time = Duration::new(seconds as u64, micros as u32 * 1000);
let mut expected = Vec::from(seconds.to_ne_bytes());
expected.extend(micros.to_ne_bytes());
expected.extend([0; 4]);
assert_eq!(timeval(time), expected);
}
}
#[cfg(target_os = "linux")]
fn timespec(t: Duration) -> libc::timespec {
let mut ts: libc::timespec = unsafe { std::mem::zeroed() };
ts.tv_sec = t.as_secs() as libc::time_t;
ts.tv_nsec = t.subsec_nanos() as _;
ts
}
#[cfg(target_os = "linux")]
fn bytes_of<T: Copy>(value: &T) -> Vec<u8> {
unsafe { std::slice::from_raw_parts((value as *const T).cast::<u8>(), size_of::<T>()) }.to_vec()
}
unsafe fn read_int(val: *const u8, len: u32) -> Result<c_int, c_int> {
if val.is_null() || (len as usize) < size_of::<c_int>() {
return Err(libc::EINVAL);
}
Ok(unsafe { val.cast::<c_int>().read_unaligned() })
}
#[derive(Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub(crate) enum Kind {
Datagram,
Established,
Unconnected,
}
#[cfg(target_os = "linux")]
pub(crate) use linux::*;
#[cfg(target_os = "linux")]
mod linux {
use super::*;
pub(crate) const SO_TIMESTAMP: c_int = 29;
pub(crate) const SO_TIMESTAMPNS: c_int = 35;
pub(crate) const SO_TIMESTAMPING: c_int = 37;
pub(crate) const TX_HARDWARE: u32 = 1 << 0;
pub(crate) const TX_SOFTWARE: u32 = 1 << 1;
const RX_HARDWARE: u32 = 1 << 2;
pub(crate) const RX_SOFTWARE: u32 = 1 << 3;
pub(crate) const SOFTWARE: u32 = 1 << 4;
const RAW_HARDWARE: u32 = 1 << 6;
pub(crate) const OPT_ID: u32 = 1 << 7;
pub(crate) const TX_SCHED: u32 = 1 << 8;
pub(crate) const TX_ACK: u32 = 1 << 9;
pub(crate) const OPT_TSONLY: u32 = 1 << 11;
const OPT_STATS: u32 = 1 << 12;
const OPT_TX_SWHW: u32 = 1 << 14;
const BIND_PHC: u32 = 1 << 15;
const OPT_ID_TCP: u32 = 1 << 16;
const OPT_RX_FILTER: u32 = 1 << 17;
const MASK: u32 = (1 << 19) - 1;
pub(crate) const SCM_TSTAMP_SND: u32 = 0;
pub(crate) const SCM_TSTAMP_SCHED: u32 = 1;
pub(crate) const SCM_TSTAMP_ACK: u32 = 2;
const SO_EE_ORIGIN_TIMESTAMPING: u8 = 4;
pub(crate) const SO_TXTIME: c_int = 61;
const SOF_TXTIME_FLAGS_MASK: u32 = 0b11;
const SOCK_TXTIME_LEN: u32 = 8;
pub(crate) unsafe fn set_txtime(
rec: &SockRec,
net_admin: bool,
level: c_int,
name: c_int,
val: *const u8,
len: u32,
) -> Option<Result<(), c_int>> {
if level != libc::SOL_SOCKET || name != SO_TXTIME {
return None;
}
if len != SOCK_TXTIME_LEN {
return Some(Err(libc::EINVAL));
}
if val.is_null() {
return Some(Err(libc::EFAULT));
}
let (clockid, flags) = unsafe {
(
val.cast::<c_int>().read_unaligned(),
val.cast::<u32>().add(1).read_unaligned(),
)
};
if flags & !SOF_TXTIME_FLAGS_MASK != 0 {
return Some(Err(libc::EINVAL));
}
if clockid != libc::CLOCK_MONOTONIC && !net_admin {
return Some(Err(libc::EPERM));
}
if !matches!(
clockid,
libc::CLOCK_REALTIME | libc::CLOCK_MONOTONIC | libc::CLOCK_TAI
) {
return Some(Err(libc::EINVAL));
}
rec.state().ts.txtime = Some((clockid, flags));
Some(Ok(()))
}
pub(crate) unsafe fn txtime_cmsg(
rec: &SockRec,
hdr: *const libc::msghdr,
) -> Result<Option<u64>, c_int> {
let (control, controllen) = unsafe { ((*hdr).msg_control, (*hdr).msg_controllen) };
if control.is_null() || controllen == 0 {
return Ok(None);
}
let enabled = rec.state().ts.txtime.is_some();
let mut at = None;
let mut cmsg = unsafe { libc::CMSG_FIRSTHDR(hdr) };
while !cmsg.is_null() {
let (level, ty, len) = unsafe {
(
(*cmsg).cmsg_level,
(*cmsg).cmsg_type,
(*cmsg).cmsg_len as usize,
)
};
if level == libc::SOL_SOCKET && ty == SO_TXTIME {
if !enabled || len != unsafe { libc::CMSG_LEN(8) } as usize {
return Err(libc::EINVAL);
}
at = Some(unsafe { libc::CMSG_DATA(cmsg).cast::<u64>().read_unaligned() });
}
cmsg = unsafe { libc::CMSG_NXTHDR(hdr, cmsg) };
}
Ok(at)
}
pub(crate) unsafe fn set(
shared: Option<&SimShared>,
rec: &SockRec,
kind: Kind,
level: c_int,
name: c_int,
val: *const u8,
len: u32,
) -> Option<Result<(), c_int>> {
if level != libc::SOL_SOCKET
|| !matches!(name, SO_TIMESTAMP | SO_TIMESTAMPNS | SO_TIMESTAMPING)
{
return None;
}
let v = match unsafe { read_int(val, len) } {
Ok(v) => v,
Err(errno) => return Some(Err(errno)),
};
let mut state = rec.state();
let device = state.device.is_some();
let ts = &mut state.ts;
if name != SO_TIMESTAMPING {
ts.rcv = v != 0;
ts.rcv_ns = v != 0 && name == SO_TIMESTAMPNS;
if ts.rcv
&& let Some(shared) = shared
{
shared.rx_timestamp_startup.enable(shared);
}
return Some(Ok(()));
}
let flags = v as u32;
let bind_phc = if len as usize >= 2 * size_of::<c_int>() {
unsafe { val.cast::<c_int>().add(1).read_unaligned() }
} else {
0
};
if flags & !MASK != 0 || (flags & OPT_ID_TCP != 0 && flags & OPT_ID == 0) {
return Some(Err(libc::EINVAL));
}
let restart = flags & OPT_ID != 0 && ts.flags & OPT_ID == 0;
if restart && kind == Kind::Unconnected {
return Some(Err(libc::EINVAL));
}
if flags & OPT_STATS != 0 && flags & OPT_TSONLY == 0 {
return Some(Err(libc::EINVAL));
}
if flags & BIND_PHC != 0 {
return Some(Err(if device {
libc::EINVAL
} else {
libc::EOPNOTSUPP
}));
}
if restart {
ts.tskey = if kind == Kind::Established {
ts.written
} else {
0
};
}
ts.flags = flags;
ts.bind_phc = bind_phc;
if flags & RX_SOFTWARE != 0
&& let Some(shared) = shared
{
shared.rx_timestamp_startup.enable(shared);
}
Some(Ok(()))
}
pub(crate) unsafe fn get(
rec: &SockRec,
level: c_int,
name: c_int,
val: *mut u8,
len: *mut u32,
) -> Option<()> {
if level != libc::SOL_SOCKET {
return None;
}
let state = rec.state();
let ts = &state.ts;
let bytes = match name {
SO_TIMESTAMP => ((ts.rcv && !ts.rcv_ns) as c_int).to_ne_bytes().to_vec(),
SO_TIMESTAMPNS => (ts.rcv_ns as c_int).to_ne_bytes().to_vec(),
SO_TIMESTAMPING => [ts.flags.to_ne_bytes(), ts.bind_phc.to_ne_bytes()].concat(),
SO_TXTIME => {
let (clockid, flags) = ts.txtime.unwrap_or_default();
[clockid.to_ne_bytes(), flags.to_ne_bytes()].concat()
}
_ => return None,
};
drop(state);
if !val.is_null() && !len.is_null() {
unsafe {
let n = bytes.len().min(*len as usize);
std::ptr::copy_nonoverlapping(bytes.as_ptr(), val, n);
*len = n as u32;
}
}
Some(())
}
pub(crate) fn rx_cmsgs(
shared: Option<&SimShared>,
rec: &SockRec,
mut at: Stamp,
rx: Rx,
fallback: Option<&std::sync::OnceLock<Duration>>,
) -> Vec<Cmsg> {
let state = rec.state();
let ts = &state.ts;
if rx != Rx::ErrQueue
&& shared.is_some_and(|shared| !shared.rx_timestamp_startup.stamps(at.real))
{
at.real = fallback
.and_then(|fallback| fallback.get())
.copied()
.unwrap_or_default();
if at.real.is_zero() && ts.rcv && rx == Rx::Datagram {
let now = || shared.expect("timestamp startup sim").tstamp_now().real;
at.real = fallback.map_or_else(now, |fallback| *fallback.get_or_init(now));
}
}
let mut out = Vec::new();
if ts.rcv && !at.real.is_zero() {
out.push(if ts.rcv_ns {
(
libc::SOL_SOCKET,
SO_TIMESTAMPNS,
bytes_of(×pec(at.real)),
)
} else {
(libc::SOL_SOCKET, SO_TIMESTAMP, timeval(at.real))
});
}
let reports = |want: u32, generate: u32| {
ts.flags & want != 0
&& (ts.flags & generate != 0 || rx == Rx::ErrQueue || ts.flags & OPT_RX_FILTER == 0)
};
let sw = (reports(SOFTWARE, RX_SOFTWARE) && !at.real.is_zero()).then_some(at.real);
let hw = at.hw.filter(|_| reports(RAW_HARDWARE, RX_HARDWARE));
if sw.is_some() || hw.is_some() {
out.push((libc::SOL_SOCKET, SO_TIMESTAMPING, scm_timestamping(sw, hw)));
}
out
}
pub(crate) fn note_datagram_read(
shared: &SimShared,
rec: &SockRec,
at: Stamp,
fallback: &std::sync::OnceLock<Duration>,
) {
if !shared.rx_timestamp_startup.stamps(at.real) && rec.state().ts.rcv {
fallback.get_or_init(|| shared.tstamp_now().real);
}
}
pub(crate) fn scm_timestamping(sw: Option<Duration>, hw: Option<Duration>) -> Vec<u8> {
let slot = |t: Option<Duration>| timespec(t.unwrap_or_default());
bytes_of(&[slot(sw), slot(None), slot(hw)])
}
pub(crate) fn udp_sent(
rec: &SockRec,
at: Stamp,
hw: Option<Duration>,
packet: impl FnOnce() -> Vec<u8>,
) {
let mut state = rec.state();
let v6 = state.local.is_some_and(|a| a.is_ipv6());
let (buf, ts) = {
let state = &mut *state;
(&mut state.buf, &mut state.ts)
};
if ts.flags & (TX_SCHED | TX_SOFTWARE | TX_HARDWARE) == 0 {
return;
}
let hw = hw.filter(|_| ts.flags & TX_HARDWARE != 0);
let software = ts.flags & TX_SOFTWARE != 0 && (hw.is_none() || ts.flags & OPT_TX_SWHW != 0);
let mut reports = Vec::new();
if ts.flags & TX_SCHED != 0 {
reports.push((SCM_TSTAMP_SCHED, at));
}
if software {
reports.push((SCM_TSTAMP_SND, at));
}
if let Some(hw) = hw {
reports.push((SCM_TSTAMP_SND, Stamp::hardware(hw)));
}
let key = if ts.flags & OPT_ID != 0 {
let key = ts.tskey;
ts.tskey = ts.tskey.wrapping_add(1);
key
} else {
0
};
let packet = if ts.flags & OPT_TSONLY != 0 || reports.is_empty() {
Vec::new()
} else {
packet()
};
let charge = buf.error_charge(packet.len().saturating_sub(if v6 { 62 } else { 42 }), v6);
for (stage, at) in reports {
ts.txq.push_back(TxReport {
charge,
charged: false,
order: crate::sockets::ErrorOrder::new(None),
wake_counted: false,
key,
stage,
at,
visible: None,
packet: packet.clone(),
});
}
}
pub(crate) struct TcpWrite {
key: u32,
flags: u32,
}
pub(crate) fn tcp_requested(rec: &SockRec) -> bool {
rec.state().ts.flags & (TX_SCHED | TX_SOFTWARE | TX_ACK | TX_HARDWARE) != 0
}
pub(crate) fn tcp_written(rec: &SockRec, n: usize) -> Option<TcpWrite> {
let mut state = rec.state();
let ts = &mut state.ts;
ts.written = ts.written.wrapping_add(n as u32);
if n == 0 || ts.flags & (TX_SCHED | TX_SOFTWARE | TX_ACK | TX_HARDWARE) == 0 {
return None;
}
let key = if ts.flags & OPT_ID != 0 {
ts.written.wrapping_sub(1).wrapping_sub(ts.tskey)
} else {
0
};
Some(TcpWrite {
key,
flags: ts.flags,
})
}
pub(crate) fn tcp_transmitted(
rec: &SockRec,
request: TcpWrite,
at: Stamp,
len: usize,
rtt: Duration,
packet: impl FnOnce() -> Vec<u8>,
) {
let mut state = rec.state();
let v6 = state.local.is_some_and(|a| a.is_ipv6());
let (buf, ts) = {
let state = &mut *state;
(&mut state.buf, &mut state.ts)
};
let packet = if request.flags & OPT_TSONLY != 0 {
Vec::new()
} else {
packet()
};
let charge = buf.error_charge(0, v6) + if packet.is_empty() { 0 } else { len };
let ack = (!rtt.is_zero()).then(|| Deadline::after(rtt));
for (flag, stage, visible) in [
(TX_SCHED, SCM_TSTAMP_SCHED, None),
(TX_SOFTWARE, SCM_TSTAMP_SND, None),
(TX_ACK, SCM_TSTAMP_ACK, ack),
] {
if request.flags & flag == 0 {
continue;
}
let at = if stage == SCM_TSTAMP_ACK {
at.later(rtt)
} else {
at
};
ts.txq.push_back(TxReport {
charge,
charged: false,
order: crate::sockets::ErrorOrder::new(visible),
wake_counted: false,
key: request.key,
stage,
at,
visible,
packet: packet.clone(),
});
}
drop(state);
if let Some(ack) = ack {
ack.wake_waiters_then();
}
}
pub(crate) fn tx_report_ready(rec: &SockRec) -> bool {
next_tx_report(&rec.state().ts).is_some()
}
pub(crate) fn next_tx_report(ts: &TsState) -> Option<(usize, crate::sockets::ErrorOrder)> {
ts.txq
.iter()
.enumerate()
.filter(|(_, report)| report.charged)
.min_by_key(|(_, report)| report.order)
.map(|(i, report)| (i, report.order))
}
impl TsState {
pub(crate) fn admit_tx_report(&mut self, i: usize) {
self.txq[i].charged = true;
}
pub(crate) fn pop_tx_report(&mut self, i: usize) -> Option<TxReport> {
self.txq.remove(i)
}
}
pub(crate) fn pending_tx_report(
ts: &TsState,
until: Option<Duration>,
) -> Option<(usize, crate::sockets::ErrorOrder, usize)> {
ts.txq
.iter()
.enumerate()
.filter(|(_, report)| {
!report.charged
&& report.visible.is_none_or(|d| d.passed())
&& until.is_none_or(|until| report.order.at() <= until)
})
.min_by_key(|(_, report)| report.order)
.map(|(i, report)| (i, report.order, report.charge))
}
pub(crate) fn tx_report_wakes(ts: &mut TsState) -> u64 {
for report in &mut ts.txq {
if !report.wake_counted && report.charged {
report.wake_counted = true;
ts.report_wakes = ts.report_wakes.saturating_add(1);
}
}
ts.report_wakes
}
pub(crate) unsafe fn write_tx_report(
rec: &SockRec,
report: TxReport,
hdr: *mut libc::msghdr,
v6: bool,
now: Duration,
) -> Option<snare_interpose::NetResult> {
let copied = unsafe { crate::fabric::scatter(hdr, &report.packet) };
let mut at = report.at;
if at.real.is_zero() && rec.state().ts.rcv {
at.real = now;
}
let mut cmsgs = rx_cmsgs(None, rec, at, Rx::ErrQueue, None);
let mut ee = Vec::with_capacity(16 + 28);
ee.extend_from_slice(&(libc::ENOMSG as u32).to_ne_bytes());
ee.extend_from_slice(&[SO_EE_ORIGIN_TIMESTAMPING, 0, 0, 0]);
ee.extend_from_slice(&report.stage.to_ne_bytes());
ee.extend_from_slice(&report.key.to_ne_bytes());
let (level, ty, offender) = if v6 {
(
libc::SOL_IPV6,
libc::IPV6_RECVERR,
size_of::<libc::sockaddr_in6>(),
)
} else {
(
libc::SOL_IP,
libc::IP_RECVERR,
size_of::<libc::sockaddr_in>(),
)
};
ee.resize(ee.len() + offender, 0);
cmsgs.push((level, ty, ee));
let mut flags = libc::MSG_ERRQUEUE;
if copied < report.packet.len() {
flags |= libc::MSG_TRUNC;
}
unsafe {
if !(*hdr).msg_name.is_null() {
(*hdr).msg_namelen = 0;
}
if crate::fabric::write_cmsg_list(hdr, &cmsgs) {
flags |= libc::MSG_CTRUNC;
}
(*hdr).msg_flags = flags;
}
Some(snare_interpose::NetResult::Ok(copied as i64))
}
}
#[cfg(target_os = "macos")]
pub(crate) use mac::{get, rx_cmsgs, set};
#[cfg(target_os = "macos")]
mod mac {
use super::*;
const SO_TIMESTAMP: c_int = 0x0400;
const SO_TIMESTAMP_MONOTONIC: c_int = 0x0800;
const SO_TIMESTAMP_CONTINUOUS: c_int = 0x40000;
const SCM_TIMESTAMP: c_int = 0x02;
const SCM_TIMESTAMP_MONOTONIC: c_int = 0x04;
const SCM_TIMESTAMP_CONTINUOUS: c_int = 0x07;
unsafe extern "C" {
pub(super) fn mach_absolute_time() -> u64;
fn mach_timebase_info(info: *mut [u32; 2]) -> c_int;
}
fn timebase() -> (u64, u64) {
static TIMEBASE: snare_interpose::RaceCell<(u64, u64)> = snare_interpose::RaceCell::new();
*TIMEBASE
.get_or_init(|| {
let mut info = [1u32, 1];
unsafe { mach_timebase_info(&mut info) };
(u64::from(info[0]).max(1), u64::from(info[1]).max(1))
})
.0
}
fn nanos_to_ticks(d: Duration) -> u64 {
let (numer, denom) = timebase();
(d.as_nanos() * u128::from(denom) / u128::from(numer)) as u64
}
pub(super) fn ticks_to_nanos(ticks: u64) -> Duration {
let (numer, denom) = timebase();
Duration::from_nanos((u128::from(ticks) * u128::from(numer) / u128::from(denom)) as u64)
}
fn bit(name: c_int) -> Option<c_int> {
matches!(
name,
SO_TIMESTAMP | SO_TIMESTAMP_MONOTONIC | SO_TIMESTAMP_CONTINUOUS
)
.then_some(name)
}
pub(crate) unsafe fn set(
_shared: Option<&SimShared>,
rec: &SockRec,
_kind: Kind,
level: c_int,
name: c_int,
val: *const u8,
len: u32,
) -> Option<Result<(), c_int>> {
if level != libc::SOL_SOCKET {
return None;
}
bit(name)?;
let on = match unsafe { read_int(val, len) } {
Ok(v) => v != 0,
Err(errno) => return Some(Err(errno)),
};
let mut state = rec.state();
let ts = &mut state.ts;
match name {
SO_TIMESTAMP => ts.rcv = on,
SO_TIMESTAMP_MONOTONIC => ts.mono = on,
_ => ts.cont = on,
}
Some(Ok(()))
}
pub(crate) unsafe fn get(
rec: &SockRec,
level: c_int,
name: c_int,
val: *mut u8,
len: *mut u32,
) -> Option<()> {
if level != libc::SOL_SOCKET {
return None;
}
let bit = bit(name)?;
let ts = &rec.state().ts;
let on = match name {
SO_TIMESTAMP => ts.rcv,
SO_TIMESTAMP_MONOTONIC => ts.mono,
_ => ts.cont,
};
unsafe { crate::fabric::write_opt(if on { bit } else { 0 }, val, len) };
Some(())
}
pub(crate) fn rx_cmsgs(
_shared: Option<&SimShared>,
rec: &SockRec,
at: Stamp,
rx: Rx,
_fallback: Option<&std::sync::OnceLock<Duration>>,
) -> Vec<Cmsg> {
if rx != Rx::Datagram {
return Vec::new();
}
let ts = &rec.state().ts;
let mut out = Vec::new();
if ts.rcv {
out.push((libc::SOL_SOCKET, SCM_TIMESTAMP, timeval(at.real)));
}
if ts.mono || ts.cont {
let ticks = nanos_to_ticks(at.mono).to_ne_bytes().to_vec();
if ts.mono && ts.cont {
out.push((libc::SOL_SOCKET, SCM_TIMESTAMP_MONOTONIC, ticks.clone()));
}
let kind = if ts.cont {
SCM_TIMESTAMP_CONTINUOUS
} else {
SCM_TIMESTAMP_MONOTONIC
};
out.push((libc::SOL_SOCKET, kind, ticks));
}
out
}
}
#[cfg(target_os = "linux")]
pub(crate) fn looped_frame(
src: std::net::SocketAddr,
dst: std::net::SocketAddr,
tcp: bool,
payload: &[u8],
) -> Vec<u8> {
let mut l4 = Vec::new();
l4.extend_from_slice(&src.port().to_be_bytes());
l4.extend_from_slice(&dst.port().to_be_bytes());
if tcp {
l4.extend_from_slice(&[0; 8]);
l4.extend_from_slice(&[8 << 4, 0x18, 0xff, 0xff, 0, 0, 0, 0]);
l4.extend_from_slice(&[1, 1, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0]);
} else {
l4.extend_from_slice(&((8 + payload.len()) as u16).to_be_bytes());
l4.extend_from_slice(&[0, 0]);
}
l4.extend_from_slice(payload);
let proto = if tcp { 6u8 } else { 17 };
let mut frame = vec![0u8; 12];
match (src.ip(), dst.ip()) {
(std::net::IpAddr::V4(s), std::net::IpAddr::V4(d)) => {
frame.extend_from_slice(&0x0800u16.to_be_bytes());
let mut ip = vec![0x45, 0];
ip.extend_from_slice(&((20 + l4.len()) as u16).to_be_bytes());
ip.extend_from_slice(&[0, 0, 0x40, 0, 64, proto, 0, 0]);
ip.extend_from_slice(&s.octets());
ip.extend_from_slice(&d.octets());
let sum = ip
.chunks(2)
.map(|w| u32::from(u16::from_be_bytes([w[0], w[1]])))
.sum::<u32>();
let folded = (sum & 0xffff) + (sum >> 16);
let check = !(((folded & 0xffff) + (folded >> 16)) as u16);
ip[10..12].copy_from_slice(&check.to_be_bytes());
frame.extend_from_slice(&ip);
}
(s, d) => {
let v6 = |ip: std::net::IpAddr| match ip {
std::net::IpAddr::V6(v6) => v6,
std::net::IpAddr::V4(v4) => v4.to_ipv6_mapped(),
};
frame.extend_from_slice(&0x86ddu16.to_be_bytes());
frame.extend_from_slice(&[0x60, 0, 0, 0]);
frame.extend_from_slice(&(l4.len() as u16).to_be_bytes());
frame.extend_from_slice(&[proto, 64]);
frame.extend_from_slice(&v6(s).octets());
frame.extend_from_slice(&v6(d).octets());
}
}
frame.extend_from_slice(&l4);
frame
}