use std::ffi::{c_char, c_int};
use std::path::Path;
use std::sync::{Arc, Mutex};
use snare_interpose::NetResult as HostResult;
#[cfg(target_os = "linux")]
use snare_interpose::NetResult;
use snare_interpose::{Env, Fs, Host, Layer, Net, NetResult as FsResult};
use crate::HashMap;
use crate::fs_locks::{Locks, Target};
use crate::fs_perm::{self, Credentials, Kind, Owner};
use crate::fs_sim::{
Attributes, OpenFiles, Stamp, Times, descriptor_fcntl, err, fill_stat, ino_of, ok, path_of,
set_cloexec, status_flags, with_times,
};
#[cfg(target_os = "linux")]
use crate::fs_sim::{DirEntry, DirStream, fill_statx};
use crate::clock::{Clock, ClockLayer};
use crate::netif::{IpNet, NicCounters, NicPolicy, NicSnapshot, NicSpec, Route};
#[cfg(target_os = "linux")]
use crate::sockets::{Membership, RxProbe, SockRec, SocketKind};
pub const CAP_NET_BIND_SERVICE: c_int = crate::limits::CAP_NET_BIND_SERVICE;
pub const CAP_NET_ADMIN: c_int = crate::limits::CAP_NET_ADMIN;
pub const CAP_NET_RAW: c_int = crate::limits::CAP_NET_RAW;
pub const CAP_IPC_LOCK: c_int = crate::limits::CAP_IPC_LOCK;
pub const CAP_SYS_NICE: c_int = crate::limits::CAP_SYS_NICE;
#[derive(Clone, Copy, Debug, Default)]
pub struct LinkStats {
pub rx_packets: u64,
pub tx_packets: u64,
pub rx_bytes: u64,
pub tx_bytes: u64,
pub rx_errors: u64,
pub tx_errors: u64,
pub rx_dropped: u64,
pub tx_dropped: u64,
}
pub struct Nic {
name: String,
ifindex: u32,
mtu: i32,
driver: String,
driver_version: String,
bus_info: String,
hwtstamp_supported: bool,
hwtstamp: [i32; 3],
hwtstamp_rx_mapping: [Option<Result<i32, i32>>; 16],
tx_queues_allocated: Option<usize>,
no_queue: bool,
operstate: String,
carrier: Option<bool>,
subsystem: String,
address: Option<std::net::IpAddr>,
rx_queues: usize,
tx_queues: usize,
msi_irqs: Vec<u32>,
link_stats: LinkStats,
ptp_index: Option<u32>,
networks: Vec<IpNet>,
stations: Vec<std::net::IpAddr>,
policy: NicPolicy,
ethtool: crate::ethtool::NicEthtool,
}
impl Nic {
pub fn new(name: impl Into<String>, ifindex: u32) -> Self {
Nic {
name: name.into(),
ifindex,
mtu: 1500,
driver: "sim".to_string(),
driver_version: "0".to_string(),
bus_info: String::new(),
hwtstamp_supported: false,
hwtstamp: [0; 3],
hwtstamp_rx_mapping: [None; 16],
tx_queues_allocated: None,
no_queue: false,
operstate: "up".to_string(),
carrier: None,
subsystem: "pci".to_string(),
address: None,
rx_queues: 1,
tx_queues: 1,
msi_irqs: Vec::new(),
link_stats: LinkStats::default(),
ptp_index: None,
networks: Vec::new(),
stations: Vec::new(),
policy: NicPolicy::default(),
ethtool: crate::ethtool::NicEthtool::new(),
}
}
pub fn network(mut self, net: impl Into<IpNet>) -> Self {
self.networks.push(net.into());
self
}
pub fn station(mut self, ip: impl Into<std::net::IpAddr>) -> Self {
self.stations.push(ip.into());
self
}
pub fn policy(mut self, policy: NicPolicy) -> Self {
self.policy = policy;
self
}
fn topology_fact(&self) -> (NicSpec, NicCounters) {
let mut spec = NicSpec::new(self.name.clone())
.index(self.ifindex)
.mtu(self.mtu.max(0) as u32)
.link(self.carrier.unwrap_or(self.operstate == "up"))
.policy(self.policy.clone());
for net in self
.address
.map(IpNet::host)
.into_iter()
.chain(self.networks.iter().copied())
{
spec.addresses.retain(|n| n.addr != net.addr);
spec.addresses.push(net);
}
spec.stations = self.stations.clone();
let stats = self.link_stats;
let counters = NicCounters {
rx_packets: stats.rx_packets,
tx_packets: stats.tx_packets,
rx_bytes: stats.rx_bytes,
tx_bytes: stats.tx_bytes,
rx_errors: stats.rx_errors,
tx_errors: stats.tx_errors,
rx_dropped: stats.rx_dropped,
tx_dropped: stats.tx_dropped,
..NicCounters::default()
};
(spec, counters)
}
pub fn ptp_index(mut self, index: u32) -> Self {
self.ptp_index = Some(index);
self
}
pub fn link_stats(mut self, stats: LinkStats) -> Self {
self.link_stats = stats;
self
}
pub fn queues(mut self, rx: usize, tx: usize) -> Self {
self.rx_queues = rx;
self.tx_queues = tx;
self
}
pub fn msi_irqs<I: IntoIterator<Item = u32>>(mut self, irqs: I) -> Self {
self.msi_irqs = irqs.into_iter().collect();
self
}
pub fn address(mut self, ip: impl Into<std::net::IpAddr>) -> Self {
self.address = Some(ip.into());
self
}
pub fn mtu(mut self, mtu: i32) -> Self {
self.mtu = mtu;
self
}
pub fn operstate(mut self, state: impl Into<String>) -> Self {
self.operstate = state.into();
self
}
pub fn carrier(mut self, present: bool) -> Self {
self.carrier = Some(present);
self
}
pub fn subsystem(mut self, bus: impl Into<String>) -> Self {
self.subsystem = bus.into();
self
}
pub fn driver(mut self, name: impl Into<String>, version: impl Into<String>) -> Self {
self.driver = name.into();
self.driver_version = version.into();
self
}
pub fn bus_info(mut self, bus: impl Into<String>) -> Self {
self.bus_info = bus.into();
self
}
pub fn hardware_timestamping(mut self, on: bool) -> Self {
self.hwtstamp_supported = on;
self
}
pub fn hwtstamp_config(mut self, flags: i32, tx_type: i32, rx_filter: i32) -> Self {
self.hwtstamp = [flags, tx_type, rx_filter];
self
}
pub fn hwtstamp_rx_mapping(
mut self,
requested: i32,
applied: Result<i32, i32>,
) -> std::io::Result<Self> {
if !(0..HWTSTAMP_FILTER_CNT).contains(&requested)
|| match applied {
Ok(filter) => !(0..HWTSTAMP_FILTER_CNT).contains(&filter),
Err(error) => error <= 0,
}
{
return Err(std::io::Error::from_raw_os_error(libc::EINVAL));
}
self.hwtstamp_rx_mapping[requested as usize] = Some(applied);
Ok(self)
}
pub fn tx_queues_allocated(mut self, n: usize) -> Self {
self.tx_queues_allocated = Some(n);
self
}
pub fn no_queue(mut self) -> Self {
self.no_queue = true;
self
}
pub fn firmware(mut self, version: impl Into<String>) -> Self {
self.ethtool.firmware = version.into();
self
}
pub fn expansion_rom(mut self, version: impl Into<String>) -> Self {
self.ethtool.expansion_rom = version.into();
self
}
pub fn register_dump_len(mut self, bytes: u32) -> Self {
self.ethtool.register_dump_len = bytes;
self
}
pub fn eeprom_len(mut self, bytes: u32) -> Self {
self.ethtool.eeprom_len = bytes;
self
}
pub fn private_flags_count(mut self, count: u32) -> Self {
self.ethtool.private_flags_count = count;
self
}
pub fn self_test_count(mut self, count: u32) -> Self {
self.ethtool.self_test_count = count;
self
}
pub fn rings(mut self, rings: crate::ethtool::Rings) -> Self {
self.ethtool.rings = Some(rings);
self
}
pub fn coalesce(
mut self,
supported: crate::ethtool::CoalesceParams,
values: crate::ethtool::Coalesce,
) -> Self {
self.ethtool.coalesce = Some((supported, values));
self
}
pub fn coalesce_limits(mut self, usecs_max: u32, frames_max: u32) -> Self {
self.ethtool.coalesce_usecs_max = usecs_max;
self.ethtool.coalesce_frames_max = frames_max;
self
}
pub fn channels(mut self, channels: crate::ethtool::Channels) -> Self {
self.ethtool.channels = Some(channels);
self
}
pub fn pause(mut self, pause: crate::ethtool::Pause) -> Self {
self.ethtool.pause = Some(pause);
self
}
pub fn eee(mut self, eee: crate::ethtool::Eee) -> Self {
self.ethtool.eee = Some(eee);
self
}
pub fn ntuple(mut self, slots: u32) -> Self {
self.ethtool.flow_slots = Some(slots);
self.ethtool.hw_features |= crate::ethtool::ETH_FLAG_NTUPLE;
self
}
pub fn driver_stats<I, S>(mut self, stats: I) -> Self
where
I: IntoIterator<Item = (S, u64)>,
S: Into<String>,
{
self.ethtool.stats = stats.into_iter().map(|(n, v)| (n.into(), v)).collect();
self
}
pub fn etf_offload<I: IntoIterator<Item = u16>>(mut self, queues: I) -> Self {
self.ethtool.etf_offload = Some(queues.into_iter().collect());
self
}
pub fn threaded_napi(mut self, on: bool) -> Self {
self.ethtool.threaded_napi = on;
self
}
pub fn timestamping_caps(
mut self,
so_timestamping: u32,
tx_types: u32,
rx_filters: u32,
) -> Self {
self.ethtool.ts_info = Some((so_timestamping, tx_types, rx_filters));
self
}
pub fn flags(mut self, on: u32, settable: u32) -> Self {
self.ethtool.features = on;
self.ethtool.hw_features = settable;
self
}
}
struct NicState {
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
ifindex: u32,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
driver: String,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
driver_version: String,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
bus_info: String,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
hwtstamp_supported: bool,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
hwtstamp: [i32; 3],
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
hwtstamp_rx_mapping: [Option<Result<i32, i32>>; 16],
operstate: String,
carrier: Option<bool>,
subsystem: String,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
rx_queues: usize,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
tx_queues: usize,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
num_tx_queues: usize,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
no_queue: bool,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
msi_irqs: Vec<u32>,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
ethtool: crate::ethtool::NicEthtool,
#[cfg(target_os = "linux")]
qdiscs: crate::qdisc::NicQdiscs,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PtpCaps {
pub max_adj: i32,
pub n_alarm: i32,
pub n_ext_ts: i32,
pub n_per_out: i32,
pub pps: bool,
pub n_pins: i32,
pub cross_timestamping: bool,
pub adjust_phase: bool,
pub max_phase_adj: i32,
pub extended: bool,
}
impl Default for PtpCaps {
fn default() -> Self {
PtpCaps {
max_adj: 0,
n_alarm: 0,
n_ext_ts: 0,
n_per_out: 0,
pps: false,
n_pins: 0,
cross_timestamping: false,
adjust_phase: false,
max_phase_adj: 0,
extended: true,
}
}
}
impl PtpCaps {
pub fn from_words(w: [i32; 9]) -> Self {
PtpCaps {
max_adj: w[0],
n_alarm: w[1],
n_ext_ts: w[2],
n_per_out: w[3],
pps: w[4] != 0,
n_pins: w[5],
cross_timestamping: w[6] != 0,
adjust_phase: w[7] != 0,
max_phase_adj: w[8],
extended: true,
}
}
}
pub struct HostProfile {
cpu_count: usize,
online: Option<Vec<usize>>,
isolated: Vec<usize>,
nohz_full: Vec<usize>,
governor: String,
preempt_rt: bool,
caps: u64,
root: bool,
tracefs: Option<crate::tracefs::Tracefs>,
nics: Vec<Nic>,
default_qdisc: String,
tai_offset_secs: u64,
ptp_clocks: std::collections::BTreeMap<u32, i64>,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
ptp_caps: std::collections::BTreeMap<u32, PtpCaps>,
etf: Option<(i32, i32, u32)>,
isolate_env: bool,
env: Vec<(String, String)>,
routes: Vec<Route>,
sysname: String,
nodename: String,
kernel_release: String,
kernel_version: Option<String>,
machine: String,
}
#[cfg(target_os = "linux")]
const DEFAULT_SYSNAME: &str = "Linux";
#[cfg(target_os = "macos")]
const DEFAULT_SYSNAME: &str = "Darwin";
#[cfg(target_os = "linux")]
const DEFAULT_KERNEL_RELEASE: &str = "6.12.0";
#[cfg(target_os = "macos")]
const DEFAULT_KERNEL_RELEASE: &str = "25.0.0";
#[cfg(target_os = "linux")]
const DEFAULT_KERNEL_VERSION: &str = "#1 SMP PREEMPT_DYNAMIC";
#[cfg(target_os = "macos")]
const DEFAULT_KERNEL_VERSION: &str = "Darwin Kernel Version 25.0.0";
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
const DEFAULT_MACHINE: &str = "arm64";
#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))]
const DEFAULT_MACHINE: &str = std::env::consts::ARCH;
#[derive(Clone, Debug)]
struct Uname {
sysname: String,
release: String,
version: String,
machine: String,
}
impl Default for HostProfile {
fn default() -> Self {
Self::new()
}
}
impl HostProfile {
pub fn new() -> Self {
HostProfile {
cpu_count: 1,
online: None,
isolated: Vec::new(),
nohz_full: Vec::new(),
governor: "powersave".to_string(),
preempt_rt: false,
caps: 0,
root: false,
tracefs: None,
nics: Vec::new(),
default_qdisc: "fq_codel".to_string(),
tai_offset_secs: 37,
ptp_clocks: std::collections::BTreeMap::new(),
ptp_caps: std::collections::BTreeMap::new(),
etf: None,
isolate_env: false,
env: Vec::new(),
routes: Vec::new(),
sysname: DEFAULT_SYSNAME.to_string(),
nodename: "snare".to_string(),
kernel_release: DEFAULT_KERNEL_RELEASE.to_string(),
kernel_version: None,
machine: DEFAULT_MACHINE.to_string(),
}
}
pub fn sysname(mut self, name: impl Into<String>) -> Self {
self.sysname = name.into();
self
}
pub fn nodename(mut self, name: impl Into<String>) -> Self {
self.nodename = name.into();
self
}
pub fn kernel_release(mut self, release: impl Into<String>) -> Self {
self.kernel_release = release.into();
self
}
pub fn kernel_version(mut self, version: impl Into<String>) -> Self {
self.kernel_version = Some(version.into());
self
}
pub fn machine(mut self, machine: impl Into<String>) -> Self {
self.machine = machine.into();
self
}
pub fn real_uname(mut self) -> std::io::Result<Self> {
let mut uts: libc::utsname = unsafe { std::mem::zeroed() };
if snare_interpose::real(|| unsafe { libc::uname(&mut uts) }) != 0 {
return Err(std::io::Error::last_os_error());
}
let field = |f: &[c_char]| {
unsafe { std::ffi::CStr::from_ptr(f.as_ptr()) }
.to_string_lossy()
.into_owned()
};
self.sysname = field(&uts.sysname);
self.nodename = field(&uts.nodename);
self.kernel_release = field(&uts.release);
self.kernel_version = Some(field(&uts.version));
self.machine = field(&uts.machine);
Ok(self)
}
fn uname(&self) -> Uname {
let version = self.kernel_version.clone().unwrap_or_else(|| {
if cfg!(target_os = "linux") && self.preempt_rt {
"#1 SMP PREEMPT_RT".to_string()
} else {
DEFAULT_KERNEL_VERSION.to_string()
}
});
Uname {
sysname: self.sysname.clone(),
release: self.kernel_release.clone(),
version,
machine: self.machine.clone(),
}
}
pub fn route(mut self, route: Route) -> Self {
self.routes.push(route);
self
}
pub fn etf_qdisc(mut self, delta: i32, clockid: i32, flags: u32) -> Self {
self.default_qdisc = "etf".to_string();
self.etf = Some((delta, clockid, flags));
self
}
pub fn tai_offset(mut self, secs: u64) -> Self {
self.tai_offset_secs = secs;
self
}
pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.isolate_env = true;
self.env.push((key.into(), value.into()));
self
}
pub fn isolate_env(mut self) -> Self {
self.isolate_env = true;
self
}
pub fn ptp_clock(mut self, index: u32) -> Self {
self.ptp_clocks.entry(index).or_insert(0);
self
}
pub fn ptp_clock_offset(mut self, index: u32, offset_nanos: i64) -> Self {
self.ptp_clocks.insert(index, offset_nanos);
self
}
pub fn ptp_clock_caps(mut self, index: u32, caps: PtpCaps) -> Self {
self.ptp_clocks.entry(index).or_insert(0);
self.ptp_caps.insert(index, caps);
self
}
pub fn nic(mut self, nic: Nic) -> Self {
self.nics.push(nic);
self
}
pub fn default_qdisc(mut self, qdisc: impl Into<String>) -> Self {
self.default_qdisc = qdisc.into();
self
}
pub fn cpus(mut self, count: usize) -> Self {
self.cpu_count = count.max(1);
self
}
pub fn online<I: IntoIterator<Item = usize>>(mut self, cpus: I) -> Self {
self.online = Some(cpus.into_iter().collect());
self
}
pub fn isolated<I: IntoIterator<Item = usize>>(mut self, cpus: I) -> Self {
self.isolated = cpus.into_iter().collect();
self
}
pub fn nohz_full<I: IntoIterator<Item = usize>>(mut self, cpus: I) -> Self {
self.nohz_full = cpus.into_iter().collect();
self
}
pub fn governor(mut self, governor: impl Into<String>) -> Self {
self.governor = governor.into();
self
}
pub fn tracefs(mut self, tracefs: Option<crate::tracefs::Tracefs>) -> Self {
self.tracefs = tracefs;
self
}
pub fn preempt_rt(mut self, on: bool) -> Self {
self.preempt_rt = on;
self
}
pub fn cap(mut self, cap: c_int) -> Self {
if (0..64).contains(&cap) {
self.caps |= 1u64 << cap;
}
self
}
pub fn root(mut self, on: bool) -> Self {
self.root = on;
self
}
pub fn build(self) -> Arc<SimHost> {
Arc::new(SimHost::new(self))
}
}
fn rich_nic(name: &str, ifindex: u32, ip: std::net::IpAddr) -> Nic {
use crate::ethtool::{Channels, Coalesce, CoalesceParams, Eee, Pause, Rings};
Nic::new(name, ifindex)
.mtu(1500)
.driver("igb", "5.6.0")
.bus_info(format!("0000:{ifindex:02x}:00.0"))
.firmware("3.25, 0x800005d0")
.hardware_timestamping(true)
.operstate("up")
.address(ip)
.rings(Rings {
rx_max: 4096,
tx_max: 4096,
rx: 256,
tx: 256,
..Rings::default()
})
.coalesce(
CoalesceParams::USECS,
Coalesce {
rx_usecs: 3,
..Coalesce::default()
},
)
.coalesce_limits(10_000, u32::MAX)
.channels(Channels {
combined_max: 4,
other_max: 1,
combined: 1,
other: 1,
..Channels::default()
})
.pause(Pause {
autoneg: true,
rx: true,
tx: true,
})
.eee(Eee {
supported: 0x28,
advertised: 0x28,
lp_advertised: 0x28,
enabled: true,
tx_lpi_enabled: true,
..Eee::default()
})
.ntuple(16)
.etf_offload([0, 1])
}
pub struct EasyBuilder {
profile: HostProfile,
}
impl Default for EasyBuilder {
fn default() -> Self {
Self::realtime()
}
}
impl EasyBuilder {
pub fn realtime() -> Self {
let profile = HostProfile::new()
.cpus(8)
.isolated(2..8)
.nohz_full(2..8)
.governor("performance")
.preempt_rt(true)
.default_qdisc("fq")
.tracefs(Some(crate::tracefs::Tracefs::full()))
.cap(CAP_SYS_NICE)
.cap(CAP_IPC_LOCK)
.cap(CAP_NET_ADMIN)
.cap(CAP_NET_RAW)
.cap(CAP_NET_BIND_SERVICE)
.nic(rich_nic(
"eth0",
2,
std::net::Ipv4Addr::new(10, 0, 0, 10).into(),
));
EasyBuilder { profile }
}
pub fn server() -> Self {
let profile = HostProfile::new()
.cpus(16)
.isolated(4..16)
.governor("performance")
.default_qdisc("fq")
.cap(CAP_SYS_NICE)
.cap(CAP_IPC_LOCK)
.cap(CAP_NET_ADMIN)
.cap(CAP_NET_RAW)
.cap(CAP_NET_BIND_SERVICE)
.tracefs(Some(crate::tracefs::Tracefs::full()))
.nic(rich_nic("eth0", 2, std::net::Ipv4Addr::new(10, 0, 0, 20).into()).mtu(9000));
EasyBuilder { profile }
}
pub fn laptop() -> Self {
let profile = HostProfile::new()
.cpus(4)
.governor("powersave")
.default_qdisc("fq_codel")
.nic(
Nic::new("wlan0", 2)
.driver("iwlwifi", "1")
.operstate("up")
.address(std::net::Ipv4Addr::new(192, 168, 1, 50)),
);
EasyBuilder { profile }
}
pub fn unprivileged() -> Self {
let mut easy = Self::realtime();
easy.profile.caps = 0;
easy
}
pub fn minimal() -> Self {
EasyBuilder {
profile: HostProfile::new(),
}
}
pub fn cpus(mut self, n: usize) -> Self {
self.profile = self.profile.cpus(n);
self
}
pub fn isolate<I: IntoIterator<Item = usize>>(mut self, cpus: I) -> Self {
self.profile = self.profile.isolated(cpus);
self
}
pub fn governor(mut self, governor: impl Into<String>) -> Self {
self.profile = self.profile.governor(governor);
self
}
pub fn privileged(mut self, on: bool) -> Self {
self.profile.caps = 0;
if on {
self.profile = self
.profile
.cap(CAP_SYS_NICE)
.cap(CAP_IPC_LOCK)
.cap(CAP_NET_ADMIN)
.cap(CAP_NET_RAW)
.cap(CAP_NET_BIND_SERVICE);
}
self
}
pub fn realtime_kernel(mut self, on: bool) -> Self {
self.profile = self.profile.preempt_rt(on);
self
}
pub fn add_nic(mut self, nic: Nic) -> Self {
self.profile = self.profile.nic(nic);
self
}
pub fn nic(self, name: &str, ifindex: u32, ip: impl Into<std::net::IpAddr>) -> Self {
self.add_nic(rich_nic(name, ifindex, ip.into()))
}
pub fn without_nics(mut self) -> Self {
self.profile.nics.clear();
self
}
pub fn profile(self) -> HostProfile {
self.profile
}
pub fn build(self) -> Arc<SimHost> {
self.profile.build()
}
}
struct OpenFile {
data: Vec<u8>,
read_error: Option<c_int>,
cursor: usize,
writable: bool,
flags: c_int,
path: std::path::PathBuf,
description: u64,
}
#[derive(Clone, Default)]
struct ThreadState {
policy: c_int,
rt_priority: c_int,
nice: c_int,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
affinity: Vec<usize>,
}
const TID_BASE: i32 = 4000;
use crate::limits::{UNPRIVILEGED_GID, UNPRIVILEGED_UID};
#[cfg(target_os = "linux")]
#[derive(Clone)]
struct Datagram {
data: Vec<u8>,
src: std::net::SocketAddr,
timestamp: std::time::Duration,
rx_fallback: Arc<std::sync::OnceLock<std::time::Duration>>,
arrives: Option<crate::readiness::Deadline>,
via: Option<(Arc<crate::netif::LinkState>, u64)>,
hw: Option<std::time::Duration>,
mtu: Option<u32>,
ip: crate::ancillary::Header,
}
#[cfg(target_os = "linux")]
impl Datagram {
fn lost(&self) -> bool {
match (&self.via, self.arrives) {
(Some((link, epoch)), Some(at)) => link.lost_in_flight(*epoch, at.instant()),
_ => false,
}
}
}
#[cfg(target_os = "linux")]
impl crate::limits::Arrival for Datagram {
fn src(&self) -> std::net::SocketAddr {
self.src
}
fn payload(&self) -> usize {
self.data.len()
}
fn arrives(&self) -> Option<crate::readiness::Deadline> {
self.arrives
}
fn lost(&self) -> bool {
Datagram::lost(self)
}
fn path_mtu(&self) -> Option<u32> {
self.mtu
}
}
#[cfg(target_os = "linux")]
fn in_flight(
data: Vec<u8>,
src: std::net::SocketAddr,
sent: std::time::Duration,
delay: std::time::Duration,
via: Option<(Arc<crate::netif::LinkState>, u64)>,
ip: crate::ancillary::Header,
) -> Datagram {
let arrives = (!delay.is_zero()).then(|| crate::readiness::Deadline::after(delay));
if let Some(arrives) = arrives {
arrives.wake_waiters_then();
}
Datagram {
data,
src,
timestamp: sent + delay,
rx_fallback: Arc::default(),
arrives,
via,
hw: None,
mtu: None,
ip,
}
}
#[cfg(target_os = "linux")]
struct UdpSocket {
fd: c_int,
descriptors: std::collections::BTreeSet<c_int>,
domain: c_int,
local: Option<std::net::SocketAddr>,
requested: Option<std::net::SocketAddr>,
peer: Option<std::net::SocketAddr>,
rx: crate::limits::RxQueue<Datagram>,
nonblocking: bool,
fcntl: crate::fabric::SocketFcntl,
broadcast: bool,
sockopts: HashMap<(c_int, c_int), Vec<u8>>,
tx_deadline: Option<u64>,
rec: Arc<SockRec>,
_probe: Arc<HostRx>,
}
#[cfg(target_os = "linux")]
impl UdpSocket {
fn land(&mut self) {
self.rx.land(Some(&self.rec));
}
}
#[cfg(target_os = "linux")]
#[derive(Default)]
struct UdpSockets {
sockets: HashMap<u64, UdpSocket>,
descriptors: HashMap<c_int, u64>,
}
#[cfg(target_os = "linux")]
impl UdpSockets {
fn get(&self, fd: &c_int) -> Option<&UdpSocket> {
self.sockets.get(self.descriptors.get(fd)?)
}
fn get_mut(&mut self, fd: &c_int) -> Option<&mut UdpSocket> {
let id = *self.descriptors.get(fd)?;
self.sockets.get_mut(&id)
}
fn contains_key(&self, fd: &c_int) -> bool {
self.descriptors.contains_key(fd)
}
fn insert(&mut self, fd: c_int, socket: UdpSocket) {
let id = socket.rec.id.get();
self.descriptors.insert(fd, id);
self.sockets.insert(id, socket);
}
fn keys(&self) -> impl Iterator<Item = &c_int> {
self.descriptors.keys()
}
fn iter(&self) -> impl Iterator<Item = (c_int, &UdpSocket)> {
self.sockets.values().map(|socket| (socket.fd, socket))
}
fn alias(&mut self, oldfd: c_int, newfd: c_int) {
let id = self.descriptors[&oldfd];
self.descriptors.insert(newfd, id);
self.sockets.get_mut(&id).unwrap().descriptors.insert(newfd);
}
fn close(&mut self, fd: c_int) -> Option<Option<UdpSocket>> {
let id = self.descriptors.remove(&fd)?;
let socket = self.sockets.get_mut(&id)?;
socket.descriptors.remove(&fd);
if let Some(&remaining) = socket.descriptors.first() {
socket.fd = remaining;
Some(None)
} else {
Some(self.sockets.remove(&id))
}
}
}
#[cfg(target_os = "linux")]
struct NetlinkSocket {
replies: std::collections::VecDeque<Vec<u8>>,
responses: u64,
rec: Arc<SockRec>,
_probe: Arc<NetlinkRx>,
nonblocking: bool,
fcntl: crate::fabric::SocketFcntl,
descriptors: std::collections::BTreeSet<c_int>,
sockopts: HashMap<(c_int, c_int), Vec<u8>>,
}
#[cfg(target_os = "linux")]
struct NetlinkRx {
host: std::sync::Weak<SimHost>,
id: u64,
}
#[cfg(target_os = "linux")]
impl RxProbe for NetlinkRx {
fn land(&self) {}
fn queued(&self) -> (usize, usize) {
let Some(host) = self.host.upgrade() else {
return (0, 0);
};
let state = host.state.lock().unwrap();
state
.netlinks
.sockets
.get(&self.id)
.map_or((0, 0), |socket| {
(
socket.replies.len(),
socket.replies.iter().map(Vec::len).sum(),
)
})
}
fn next_len(&self) -> Option<usize> {
let host = self.host.upgrade()?;
host.state
.lock()
.unwrap()
.netlinks
.sockets
.get(&self.id)?
.replies
.front()
.map(Vec::len)
}
fn landed(&self) -> u64 {
let Some(host) = self.host.upgrade() else {
return 0;
};
host.state
.lock()
.unwrap()
.netlinks
.sockets
.get(&self.id)
.map_or(0, |socket| socket.responses)
}
}
#[cfg(target_os = "linux")]
#[derive(Default)]
struct NetlinkSockets {
sockets: HashMap<u64, NetlinkSocket>,
descriptors: HashMap<c_int, u64>,
}
#[cfg(target_os = "linux")]
impl NetlinkSockets {
fn get(&self, fd: &c_int) -> Option<&NetlinkSocket> {
self.sockets.get(self.descriptors.get(fd)?)
}
fn get_mut(&mut self, fd: &c_int) -> Option<&mut NetlinkSocket> {
self.sockets.get_mut(self.descriptors.get(fd)?)
}
fn contains_key(&self, fd: &c_int) -> bool {
self.descriptors.contains_key(fd)
}
fn keys(&self) -> impl Iterator<Item = &c_int> {
self.descriptors.keys()
}
fn insert(&mut self, fd: c_int, socket: NetlinkSocket) {
let id = socket.rec.id.get();
self.descriptors.insert(fd, id);
self.sockets.insert(id, socket);
}
fn alias(&mut self, oldfd: c_int, newfd: c_int) {
let id = self.descriptors[&oldfd];
self.descriptors.insert(newfd, id);
self.sockets.get_mut(&id).unwrap().descriptors.insert(newfd);
}
fn remove(&mut self, fd: &c_int) -> Option<Option<NetlinkSocket>> {
let id = self.descriptors.remove(fd)?;
let socket = self.sockets.get_mut(&id)?;
socket.descriptors.remove(fd);
Some(if socket.descriptors.is_empty() {
self.sockets.remove(&id)
} else {
None
})
}
}
#[cfg(target_os = "linux")]
struct HostRx {
host: std::sync::Weak<SimHost>,
id: u64,
}
#[cfg(target_os = "linux")]
impl RxProbe for HostRx {
fn pending_time(&self) -> bool {
self.host.upgrade().is_some_and(|host| {
host.state
.lock()
.unwrap()
.udp
.sockets
.get(&self.id)
.is_some_and(|sock| sock.rx.pending_time())
})
}
fn land(&self) {
if let Some(host) = self.host.upgrade()
&& let Some(sock) = host.state.lock().unwrap().udp.sockets.get_mut(&self.id)
{
sock.land();
}
}
fn queued(&self) -> (usize, usize) {
let Some(host) = self.host.upgrade() else {
return (0, 0);
};
let state = host.state.lock().unwrap();
state
.udp
.sockets
.get(&self.id)
.map_or((0, 0), |sock| sock.rx.queued())
}
fn next_len(&self) -> Option<usize> {
let host = self.host.upgrade()?;
let mut state = host.state.lock().unwrap();
let sock = state.udp.sockets.get_mut(&self.id)?;
sock.rx.next_len(Some(&sock.rec))
}
fn landed(&self) -> u64 {
let Some(host) = self.host.upgrade() else {
return 0;
};
let mut state = host.state.lock().unwrap();
state
.udp
.sockets
.get_mut(&self.id)
.map_or(0, |sock| sock.rx.landed(Some(&sock.rec)))
}
}
#[allow(clippy::unnecessary_cast)]
const PRIO_PROCESS: c_int = libc::PRIO_PROCESS as c_int;
pub(crate) struct HostState {
cpu_count: usize,
online: Vec<usize>,
isolated: Vec<usize>,
nohz_full: Vec<usize>,
governor: String,
preempt_rt: bool,
caps: u64,
root: bool,
cpu_dma_latency: Option<i32>,
open: OpenFiles<OpenFile>,
#[cfg(target_os = "linux")]
dirs: OpenFiles<DirStream>,
locks: HashMap<std::path::PathBuf, Arc<Locks>>,
threads: std::collections::BTreeMap<i32, ThreadState>,
thread_ids: HashMap<std::thread::ThreadId, i32>,
next_tid: i32,
#[cfg(target_os = "linux")]
pthreads: HashMap<u64, i32>,
nics: HashMap<String, NicState>,
default_qdisc: String,
#[cfg(target_os = "linux")]
ptp_clocks: std::collections::BTreeMap<u32, i64>,
#[cfg(target_os = "linux")]
ptp_caps: std::collections::BTreeMap<u32, PtpCaps>,
#[cfg(target_os = "linux")]
qdisc_auto: u32,
#[cfg(target_os = "macos")]
mac_threads: HashMap<u64, (c_int, c_int)>,
#[cfg(target_os = "linux")]
udp: UdpSockets,
#[cfg(target_os = "linux")]
netlinks: NetlinkSockets,
#[cfg(target_os = "linux")]
bound: HashMap<std::net::SocketAddr, Vec<u64>>,
#[cfg(target_os = "linux")]
next_ephemeral: u16,
env: HashMap<std::ffi::CString, std::ffi::CString>,
#[cfg(target_os = "linux")]
inherited_threads: HashMap<u64, ThreadState>,
shared: std::sync::Weak<crate::scope::SimShared>,
uname: Uname,
nodename: Vec<u8>,
clock: Arc<Clock>,
}
#[cfg(target_os = "linux")]
const NODENAME_MAX: usize = 64;
#[cfg(target_os = "macos")]
const NODENAME_MAX: usize = 255;
impl HostState {
fn is_dir(&self, fd: c_int) -> bool {
#[cfg(target_os = "linux")]
return self.dirs.contains_key(&fd);
#[cfg(not(target_os = "linux"))]
{
let _ = fd;
false
}
}
fn forget(&mut self, fd: c_int) -> Option<Dropped> {
#[cfg(target_os = "linux")]
if let Some(stream) = self.dirs.get(&fd) {
let path = stream.path.clone();
let last = self
.dirs
.remove(&fd)
.flatten()
.map(|stream| stream.description);
return Some(Dropped { path, last });
}
let path = self.open.get(&fd)?.path.clone();
let last = self.open.remove(&fd).flatten();
if last.is_some() && path == Path::new("/dev/cpu_dma_latency") {
self.cpu_dma_latency = None;
}
Some(Dropped {
path: Some(path),
last: last.map(|file| file.description),
})
}
fn hostname(&self) -> &[u8] {
let name = &self.nodename[..self.nodename.len().min(NODENAME_MAX)];
name.split(|&b| b == 0).next().unwrap_or_default()
}
fn online_count(&self) -> usize {
self.online
.iter()
.filter(|&&c| c < self.cpu_count)
.count()
.max(1)
}
#[cfg(target_os = "linux")]
fn socket_rec(&self, fd: c_int) -> Option<Arc<SockRec>> {
self.udp
.get(&fd)
.map(|socket| socket.rec.clone())
.or_else(|| self.netlinks.get(&fd).map(|socket| socket.rec.clone()))
}
#[cfg(target_os = "linux")]
fn remove_socket(&mut self, fd: c_int) {
if self.netlinks.remove(&fd).is_none()
&& let Some(socket) = self.udp.close(fd).flatten()
&& let Some(local) = socket.local
{
self.unbind_udp(local, socket.rec.id.get());
}
}
#[cfg(target_os = "linux")]
fn bind_udp(&mut self, addr: std::net::SocketAddr, id: u64) {
self.bound.entry(addr).or_default().push(id);
}
#[cfg(target_os = "linux")]
fn unbind_udp(&mut self, addr: std::net::SocketAddr, id: u64) {
if let Some(held) = self.bound.get_mut(&addr) {
held.retain(|&held| held != id);
if held.is_empty() {
self.bound.remove(&addr);
}
}
}
#[cfg(target_os = "linux")]
fn rehash(&mut self, fd: c_int, from: std::net::SocketAddr, to: std::net::SocketAddr) {
let Some(id) = self.udp.get(&fd).map(|sock| sock.rec.id.get()) else {
return;
};
if from == to {
return;
}
self.unbind_udp(from, id);
self.bind_udp(to, id);
if let Some(sock) = self.udp.get_mut(&fd) {
sock.local = Some(to);
sock.rec.set_local(to);
}
}
#[cfg(target_os = "linux")]
fn autobind(
&mut self,
fd: c_int,
ip: std::net::IpAddr,
shared: &crate::scope::SimShared,
) -> Option<std::net::SocketAddr> {
let sock = self.udp.get(&fd)?;
let (id, rec) = (sock.rec.id.get(), sock.rec.clone());
let dual = rec.state().dual_at(std::net::SocketAddr::new(ip, 0));
let sa = std::net::SocketAddr::new(ip, self.alloc_ephemeral(ip, dual)?);
rec.state().bound_order = shared.bind_order();
self.bind_udp(sa, id);
self.udp.get_mut(&fd)?.local = Some(sa);
rec.set_local(sa);
Some(sa)
}
#[cfg(target_os = "linux")]
fn udp_claim(sock: &UdpSocket) -> Option<crate::fabric::Claim> {
let local = sock.local?;
let addr = sock
.requested
.map_or(local, |r| std::net::SocketAddr::new(r.ip(), local.port()));
Some(crate::fabric::Claim::of(&sock.rec, addr))
}
#[cfg(target_os = "linux")]
fn udp_disconnect(&mut self, fd: c_int) {
let Some(sock) = self.udp.get_mut(&fd) else {
return;
};
sock.peer = None;
sock.rec.set_peer(None);
let Some(bound) = sock.local else {
return;
};
let requested = sock.requested;
let ip = requested
.map(|r| r.ip())
.filter(|ip| !ip.is_unspecified())
.unwrap_or(if sock.domain == libc::AF_INET6 {
std::net::Ipv6Addr::UNSPECIFIED.into()
} else {
std::net::Ipv4Addr::UNSPECIFIED.into()
});
if requested.is_some_and(|r| r.port() != 0) {
self.rehash(fd, bound, std::net::SocketAddr::new(ip, bound.port()));
return;
}
let id = sock.rec.id.get();
sock.local = None;
sock.rec.state().local =
Some(std::net::SocketAddr::new(ip, 0)).filter(|a| !a.ip().is_unspecified());
self.unbind_udp(bound, id);
}
#[cfg(target_os = "linux")]
fn alias_socket(&mut self, oldfd: c_int, newfd: c_int) {
if self.udp.contains_key(&oldfd) {
self.udp.alias(oldfd, newfd);
} else {
self.netlinks.alias(oldfd, newfd);
}
}
fn topo_nic(&self, name: &str) -> Option<NicSnapshot> {
self.shared.upgrade()?.nic(name)
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
fn topo_nics(&self) -> Vec<NicSnapshot> {
let mut nics = self.shared.upgrade().map(|s| s.nics()).unwrap_or_default();
nics.sort_by_key(|n| n.index);
nics
}
fn operstate(&self, nic: &NicSnapshot) -> String {
if !nic.spec.admin_up {
return "down".to_string();
}
if nic.spec.carrier {
let unknown = nic.loopback
|| self
.nics
.get(&nic.spec.name)
.is_some_and(|n| n.carrier == Some(true) && n.operstate == "unknown");
return if unknown { "unknown" } else { "up" }.to_string();
}
self.nics
.get(&nic.spec.name)
.filter(|n| {
n.operstate != "up" && !(n.carrier == Some(true) && n.operstate == "unknown")
})
.map(|n| n.operstate.clone())
.unwrap_or_else(|| "down".to_string())
}
fn read_error(&self, path: &Path) -> Option<c_int> {
let name = path
.to_str()?
.strip_prefix("/sys/class/net/")?
.strip_suffix("/carrier")?;
let nic = self.topo_nic(name)?;
(!nic.spec.admin_up).then_some(libc::EINVAL)
}
fn render(&self, path: &Path) -> Option<Vec<u8>> {
let p = path.to_str()?;
let text = match p {
"/sys/devices/system/cpu/online" => format_cpu_list(&self.online),
"/sys/devices/system/cpu/isolated" => format_cpu_list(&self.isolated),
"/sys/devices/system/cpu/nohz_full" => {
if self.nohz_full.is_empty() {
String::new()
} else {
format_cpu_list(&self.nohz_full)
}
}
"/sys/kernel/realtime" => {
if self.preempt_rt {
"1".to_string()
} else {
return None;
}
}
_ => return self.render_dynamic(p),
};
Some(with_newline(text))
}
fn render_dynamic(&self, p: &str) -> Option<Vec<u8>> {
if p == "/proc/sys/net/core/default_qdisc" {
return Some(with_newline(self.default_qdisc.clone()));
}
if p == "/proc/sys/net/ipv4/tcp_syn_retries" {
let retries = self.shared.upgrade()?.sys.limits().tcp_syn_retries;
return Some(with_newline(retries.to_string()));
}
if p == "/proc/sys/net/ipv4/tcp_syn_linear_timeouts" {
let linear = self.shared.upgrade()?.sys.limits().tcp_syn_linear_timeouts;
return Some(with_newline(linear.to_string()));
}
if cfg!(target_os = "linux")
&& (p == "/proc/self/status" || p == "/proc/thread-self/status")
{
return Some(self.status().into_bytes());
}
if cfg!(target_os = "linux") && p == "/proc/stat" {
return Some(self.proc_stat().into_bytes());
}
#[cfg(target_os = "linux")]
if p == "/proc/cpuinfo" {
return self.cpuinfo().map(String::into_bytes);
}
if cfg!(target_os = "linux")
&& let Some(name) = p.strip_prefix("/proc/sys/kernel/")
{
let u = &self.uname;
let text = match name {
"hostname" => self.hostname(),
"domainname" => b"(none)".as_slice(),
"ostype" => u.sysname.as_bytes(),
"osrelease" => u.release.as_bytes(),
"version" => u.version.as_bytes(),
_ => return None,
};
return Some([text, b"\n"].concat());
}
if let Some(rest) = p.strip_prefix("/sys/class/net/")
&& let Some((name, attr)) = rest.split_once('/')
{
let nic = self.topo_nic(name)?;
let c = &nic.counters;
let text = match attr {
"mtu" => nic.spec.mtu.to_string(),
"ifindex" => nic.index.to_string(),
"operstate" => self.operstate(&nic),
"carrier" => u8::from(nic.spec.carrier).to_string(),
"flags" => {
let operational = (libc::IFF_RUNNING | 0x10000) as u32;
format!("{:#x}", crate::ifaddrs::flags(&nic) & !operational)
}
"address" => {
let m = nic.hw_addr();
format!(
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
m[0], m[1], m[2], m[3], m[4], m[5]
)
}
"statistics/rx_packets" => c.rx_packets.to_string(),
"statistics/tx_packets" => c.tx_packets.to_string(),
"statistics/rx_bytes" => c.rx_bytes.to_string(),
"statistics/tx_bytes" => c.tx_bytes.to_string(),
"statistics/rx_errors" => c.rx_errors.to_string(),
"statistics/tx_errors" => c.tx_errors.to_string(),
"statistics/rx_dropped" => c.rx_dropped.to_string(),
"statistics/tx_dropped" => c.tx_dropped.to_string(),
"statistics/multicast" => c.multicast.to_string(),
"statistics/tx_carrier_errors" => c.tx_carrier_errors.to_string(),
"statistics/rx_nohandler" => c.rx_nohandler.to_string(),
"threaded" => {
u8::from(self.nics.get(name).is_some_and(|n| n.ethtool.threaded_napi))
.to_string()
}
_ => return None,
};
return Some(with_newline(text));
}
if let Some(rest) = p.strip_prefix("/sys/devices/system/cpu/cpu")
&& let Some((n, tail)) = rest.split_once('/')
{
let cpu: usize = n.parse().ok()?;
if cpu >= self.cpu_count {
return None;
}
if tail == "cpufreq/scaling_governor" {
return Some(with_newline(self.governor.clone()));
}
if tail == "online" {
let v = if self.online.contains(&cpu) { "1" } else { "0" };
return Some(with_newline(v.to_string()));
}
}
None
}
fn threaded_attr(&self, path: &Path) -> Option<String> {
let name = path
.to_str()?
.strip_prefix("/sys/class/net/")?
.strip_suffix("/threaded")?;
self.nics.contains_key(name).then(|| name.to_string())
}
fn store_threaded(&mut self, nic: &str, text: &[u8]) -> Result<(), c_int> {
if !self.has_cap(CAP_NET_ADMIN) {
return Err(libc::EPERM);
}
let text = std::str::from_utf8(text).map_err(|_| libc::EINVAL)?;
let text = text.strip_suffix('\n').unwrap_or(text);
let value =
if let Some(hex) = text.strip_prefix("0x").or_else(|| text.strip_prefix("0X")) {
u64::from_str_radix(hex, 16)
} else if text.len() > 1 && text.starts_with('0') {
u64::from_str_radix(&text[1..], 8)
} else {
text.strip_prefix('+').unwrap_or(text).parse()
}
.map_err(|_| libc::EINVAL)?;
if value > 1 {
return Err(libc::EOPNOTSUPP);
}
self.nics.get_mut(nic).unwrap().ethtool.threaded_napi = value == 1;
Ok(())
}
fn online_cpus(&self) -> Vec<usize> {
let mut cpus: Vec<usize> = self
.online
.iter()
.copied()
.filter(|&c| c < self.cpu_count)
.collect();
cpus.sort_unstable();
cpus.dedup();
if cpus.is_empty() {
cpus.push(0);
}
cpus
}
fn idle_ticks(&self) -> u64 {
self.clock.monotonic() / 10_000_000
}
fn proc_stat(&self) -> String {
use std::fmt::Write;
let cpus = self.online_cpus();
let idle = self.idle_ticks();
let total = idle * cpus.len() as u64;
let mut text = format!("cpu 0 0 0 {total} 0 0 0 0 0 0\n");
for cpu in cpus {
let _ = writeln!(text, "cpu{cpu} 0 0 0 {idle} 0 0 0 0 0 0");
}
let btime = self.clock.base_realtime().as_secs();
let _ = write!(
text,
"intr 0\nctxt 0\nbtime {btime}\nprocesses 1\nprocs_running 1\nprocs_blocked 0\n\
softirq 0 0 0 0 0 0 0 0 0 0 0\n"
);
text
}
#[cfg(target_os = "linux")]
fn cpuinfo(&self) -> Option<String> {
use std::fmt::Write;
let entry = machine_cpuinfo_entry()?;
let cpus = self.online_cpus();
let count = cpus.len();
let mut text = String::new();
for cpu in cpus {
for line in entry.lines() {
let renumbered = line.split_once(':').and_then(|(key, _)| {
let value = match key.trim() {
"processor" | "core id" | "apicid" | "initial apicid" => cpu,
"physical id" => 0,
"siblings" | "cpu cores" => count,
_ => return None,
};
Some(format!("{key}: {value}"))
});
let _ = writeln!(text, "{}", renumbered.as_deref().unwrap_or(line));
}
text.push('\n');
}
Some(text)
}
fn status(&self) -> String {
let privileges = self.privileges();
let (uid, gid) = if privileges.root {
(0, 0)
} else {
(UNPRIVILEGED_UID, UNPRIVILEGED_GID)
};
let umask = self
.shared
.upgrade()
.map_or(0o022, |shared| shared.sys.umask(None));
let caps = privileges.cap_mask();
format!(
"Name:\tsnare\nUmask:\t{umask:04o}\nState:\tR (running)\n\
Uid:\t{uid}\t{uid}\t{uid}\t{uid}\nGid:\t{gid}\t{gid}\t{gid}\t{gid}\n\
CapInh:\t{:016x}\nCapPrm:\t{caps:016x}\nCapEff:\t{caps:016x}\nCapBnd:\t{caps:016x}\n\
CapAmb:\t{:016x}\n",
0, 0
)
}
fn known_file(&self, path: &Path) -> bool {
self.render(path).is_some() || path == Path::new("/dev/cpu_dma_latency")
}
#[cfg(target_os = "linux")]
fn dir_entries(&self, path: &Path) -> Option<Vec<DirEntry>> {
let entry = |name: &str| {
let node = match name {
"." => path.to_owned(),
".." => path.parent().unwrap_or(path).to_owned(),
_ => path.join(name),
};
DirEntry {
name: name.as_bytes().to_vec(),
ino: ino_of(&node),
dtype: libc::DT_DIR,
}
};
let mut entries = vec![entry("."), entry("..")];
if path == Path::new("/sys/class/net") {
let mut names: Vec<_> = self.nics.keys().collect();
names.sort();
entries.extend(names.into_iter().map(|name| DirEntry {
name: name.as_bytes().to_vec(),
ino: ino_of(&path.join(name)),
dtype: libc::DT_LNK,
}));
return Some(entries);
}
let rest = path.to_str()?.strip_prefix("/sys/class/net/")?.to_string();
if let Some(name) = rest.strip_suffix("/queues") {
let nic = self.nics.get(name)?;
for i in 0..nic.rx_queues {
entries.push(entry(&format!("rx-{i}")));
}
for i in 0..nic.tx_queues {
entries.push(entry(&format!("tx-{i}")));
}
Some(entries)
} else if let Some(name) = rest.strip_suffix("/device/msi_irqs") {
let nic = self.nics.get(name)?;
for irq in &nic.msi_irqs {
entries.push(DirEntry {
name: irq.to_string().into_bytes(),
ino: ino_of(&path.join(irq.to_string())),
dtype: libc::DT_REG,
});
}
Some(entries)
} else {
None
}
}
fn owns_path(&self, path: &Path) -> bool {
const PREFIXES: &[&str] = &[
"/sys/devices/system/cpu",
"/sys/kernel",
"/sys/class/net",
"/proc/sys/net",
"/proc/self/status",
"/proc/thread-self/status",
];
let Some(p) = path.to_str() else {
return false;
};
PREFIXES
.iter()
.any(|pre| p == *pre || p.starts_with(&format!("{pre}/")))
}
fn current_tid(&mut self) -> i32 {
let id = std::thread::current().id();
if let Some(&tid) = self.thread_ids.get(&id) {
return tid;
}
#[cfg(target_os = "linux")]
let tid = {
let me = unsafe { libc::pthread_self() } as u64;
match self.pthreads.get(&me) {
Some(&tid) => tid,
None => {
let tid = self.mint_tid();
self.pthreads.insert(me, tid);
tid
}
}
};
#[cfg(not(target_os = "linux"))]
let tid = self.mint_tid();
self.thread_ids.insert(id, tid);
self.threads.entry(tid).or_default();
tid
}
fn mint_tid(&mut self) -> i32 {
let tid = self.next_tid;
self.next_tid += 1;
tid
}
#[cfg(target_os = "linux")]
fn pthread_tid(&mut self, thread: u64) -> i32 {
if thread == unsafe { libc::pthread_self() } as u64 {
return self.current_tid();
}
if let Some(&tid) = self.pthreads.get(&thread) {
return tid;
}
let tid = self.mint_tid();
self.pthreads.insert(thread, tid);
self.threads.entry(tid).or_default();
tid
}
#[cfg(target_os = "linux")]
fn sched_state(&self, tid: i32) -> crate::proclimits::SchedState {
let t = &self.threads[&tid];
crate::proclimits::SchedState {
policy: t.policy & !libc::SCHED_RESET_ON_FORK,
rt_priority: t.rt_priority,
nice: t.nice,
reset_on_fork: t.policy & libc::SCHED_RESET_ON_FORK != 0,
}
}
#[cfg(target_os = "linux")]
fn set_sched(&mut self, tid: i32, policy: c_int, priority: c_int) -> Option<HostResult> {
let cur = self.sched_state(tid);
match crate::proclimits::sched_setscheduler(&self.privileges(), cur, policy, priority) {
Ok(new) => {
let t = self.threads.get_mut(&tid).unwrap();
t.policy = new.policy
| if new.reset_on_fork {
libc::SCHED_RESET_ON_FORK
} else {
0
};
t.rt_priority = new.rt_priority;
ok(0)
}
Err(e) => err(e),
}
}
fn resolve(&mut self, pid: i32) -> i32 {
let tid = if pid == 0 { self.current_tid() } else { pid };
self.threads.entry(tid).or_default();
tid
}
fn has_cap(&self, cap: c_int) -> bool {
match self.shared.upgrade() {
Some(shared) => shared.sys.has_cap(cap),
None => (0..64).contains(&cap) && self.caps & (1u64 << cap) != 0,
}
}
fn privileges(&self) -> crate::Privileges {
match self.shared.upgrade() {
Some(shared) => shared.sys.privileges(),
None => crate::Privileges::from_caps(self.caps, self.root),
}
}
#[cfg(target_os = "linux")]
fn netlink_reply(&mut self, req: &[u8]) -> Vec<u8> {
const RTM_GETLINK: u16 = 18;
const RTM_GETADDR: u16 = 22;
const RTM_GETROUTE: u16 = 26;
const RTM_NEWQDISC: u16 = 36;
const RTM_DELQDISC: u16 = 37;
const RTM_GETQDISC: u16 = 38;
const GENL_ID_CTRL: u16 = 16;
const NLMSG_ERROR: u16 = 2;
const NLM_F_ACK: u16 = 4;
const NLM_F_DUMP: u16 = 0x300;
let (ty, seq) = if req.len() >= 12 {
(
u16::from_ne_bytes([req[4], req[5]]),
u32::from_ne_bytes([req[8], req[9], req[10], req[11]]),
)
} else {
(0, 0)
};
let family = req.get(16).copied().unwrap_or(0);
match ty {
RTM_GETLINK if u16::from_ne_bytes([req[6], req[7]]) & NLM_F_DUMP == 0 => {
match self.getlink_one(req) {
Ok(reply) => reply,
Err(errno) => {
let mut body = Vec::new();
body.extend_from_slice(&(-errno).to_ne_bytes());
body.extend_from_slice(&req[..req.len().min(16)]);
nl_message(NLMSG_ERROR, 0, seq, &body)
}
}
}
RTM_GETLINK => {
let nics = self.topo_nics();
let links: Vec<(&NicSnapshot, String)> =
nics.iter().map(|n| (n, self.operstate(n))).collect();
build_getlink_dump(&links, seq)
}
RTM_GETADDR => build_getaddr_dump(&self.topo_nics(), family, seq),
RTM_GETROUTE => {
let routes = self
.shared
.upgrade()
.map(|s| s.live_routes())
.unwrap_or_default();
build_getroute_dump(&routes, family, seq)
}
RTM_GETQDISC => {
let mut nics: Vec<&NicState> = self.nics.values().collect();
nics.sort_by_key(|n| n.ifindex);
build_getqdisc_dump(&nics, seq)
}
RTM_NEWQDISC | RTM_DELQDISC => {
let flags = u16::from_ne_bytes([req[6], req[7]]);
let result = self.change_qdisc(ty == RTM_NEWQDISC, flags, req);
if result.is_ok() && flags & NLM_F_ACK == 0 {
return Vec::new();
}
let mut body = Vec::new();
body.extend_from_slice(&(-result.err().unwrap_or(0)).to_ne_bytes());
body.extend_from_slice(&req[..req.len().min(16)]);
nl_message(NLMSG_ERROR, 0, seq, &body)
}
GENL_ID_CTRL => build_genl_ctrl_reply(req, seq),
_ => build_getlink_dump(&[], seq),
}
}
#[cfg(target_os = "linux")]
fn getlink_one(&self, req: &[u8]) -> Result<Vec<u8>, c_int> {
const IFLA_IFNAME: u16 = 3;
if req.len() < 32 {
return Err(libc::EINVAL);
}
let seq = u32::from_ne_bytes(req[8..12].try_into().unwrap());
let index = i32::from_ne_bytes(req[20..24].try_into().unwrap());
let len = (u32::from_ne_bytes(req[0..4].try_into().unwrap()) as usize).clamp(32, req.len());
let name = find_nlattr(&req[32..len], IFLA_IFNAME)
.map(|data| data.split(|&b| b == 0).next().unwrap_or_default());
let nics = self.topo_nics();
let nic = if index > 0 {
nics.iter().find(|n| n.index as i32 == index)
} else if let Some(name) = name {
nics.iter().find(|n| n.spec.name.as_bytes() == name)
} else {
return Err(libc::EINVAL);
}
.ok_or(libc::ENODEV)?;
Ok(getlink_message(nic, &self.operstate(nic), 0, seq))
}
#[cfg(target_os = "linux")]
fn queue_netlink_reply(&mut self, fd: c_int, req: &[u8]) -> Option<Arc<SockRec>> {
let reply = self.netlink_reply(req);
if reply.is_empty() {
return None;
}
let socket = self.netlinks.get_mut(&fd).unwrap();
socket.replies.push_back(reply);
socket.responses += 1;
Some(socket.rec.clone())
}
#[cfg(target_os = "linux")]
fn change_qdisc(&mut self, new: bool, flags: u16, req: &[u8]) -> Result<(), c_int> {
if !self.has_cap(CAP_NET_ADMIN) {
return Err(libc::EPERM);
}
if req.len() < 36 {
return Err(libc::EINVAL);
}
let word = |at: usize| u32::from_ne_bytes(req[at..at + 4].try_into().unwrap());
let (ifindex, handle, parent) = (word(20), word(24), word(28));
let len = (word(0) as usize).clamp(36, req.len());
let attrs = &req[36..len];
let default_qdisc = self.default_qdisc.clone();
let mut auto = self.qdisc_auto;
let nic = self
.nics
.values_mut()
.find(|n| n.ifindex == ifindex)
.ok_or(libc::ENODEV)?;
let dev = crate::qdisc::Device {
num_tx_queues: nic.num_tx_queues,
real_tx_queues: nic.tx_queues,
no_queue: nic.no_queue,
default_qdisc: &default_qdisc,
etf_offload: nic.ethtool.etf_offload.as_deref(),
};
let result = if new {
nic.qdiscs
.modify(&dev, &mut auto, flags, handle, parent, attrs)
} else {
nic.qdiscs.delete(&dev, handle, parent)
};
self.qdisc_auto = auto;
result
}
#[cfg(target_os = "linux")]
fn alloc_ephemeral(&mut self, ip: std::net::IpAddr, dual: bool) -> Option<u16> {
for _ in 49152..=65535u32 {
let p = self.next_ephemeral;
self.next_ephemeral = if p == u16::MAX { 49152 } else { p + 1 };
let taken = self.bound.iter().any(|(a, held)| {
let held_dual = || {
held.iter().any(|id| {
self.udp
.sockets
.get(id)
.is_some_and(|sock| sock.rec.state().dual_at(*a))
})
};
a.port() == p
&& (dual
|| (a.is_ipv4() == ip.is_ipv4()
&& (a.ip() == ip || ip.is_unspecified() || a.ip().is_unspecified()))
|| (ip.is_ipv4() && held_dual()))
});
if !taken {
return Some(p);
}
}
None
}
#[cfg(target_os = "linux")]
fn udp_cands(
&self,
dest: std::net::SocketAddr,
joined: bool,
) -> Vec<crate::netif::Cand<c_int>> {
let mut out: Vec<crate::netif::Cand<c_int>> = self
.udp
.iter()
.filter_map(|(fd, s)| {
let local = s.local.filter(|l| l.port() == dest.port())?;
if dest.ip().is_multicast() && !s.rec.takes_group(local, dest.ip(), joined) {
return None;
}
let state = s.rec.state();
Some(crate::netif::Cand {
addr: local,
endpoint: false,
device: state.device.as_ref().map(|d| d.0),
dual: state.dual_at(local),
share: crate::netif::Share::of_state(&state),
q: fd,
})
})
.collect();
out.sort_by_key(|c| c.q);
out
}
#[cfg(target_os = "linux")]
fn udp_land(
&mut self,
copies: Vec<crate::netif::Copy<c_int>>,
data: &[u8],
src: std::net::SocketAddr,
now: std::time::Duration,
ip: crate::ancillary::Header,
) {
for copy in copies {
if let Some(s) = self.udp.get_mut(©.q) {
if copy.delays.is_empty() {
s.rec.count_wire_lost();
} else {
s.rec.note_rx_nic(copy.nic.as_deref());
}
let port = s.local.map_or(0, |l| l.port());
let ip = crate::ancillary::Header {
ifindex: copy.ingress.unwrap_or(0),
..ip
};
let (packets, ip): (Vec<&[u8]>, _) = if ip.gso == 0 || crate::ancillary::gro(&s.rec)
{
(vec![data], ip)
} else {
(
data.chunks(usize::from(ip.gso)).collect(),
crate::ancillary::Header { gso: 0, ..ip },
)
};
for delay in copy.delays {
for &packet in &packets {
let mut dg =
in_flight(packet.to_vec(), src, now, delay, copy.via.clone(), ip);
dg.mtu = copy.mtu;
dg.hw = copy.nic.as_deref().and_then(|nic| {
nic_hw_stamp(
&self.nics,
&self.ptp_clocks,
nic,
dg.timestamp,
Some((port, packet)),
)
});
s.rx.push(dg, Some(&s.rec));
}
}
}
}
}
}
#[cfg(target_os = "linux")]
const STAMP_ALL_FILTERS: [c_int; 2] = [1, 2];
#[cfg(target_os = "linux")]
const PTP_EVENT_PORT: u16 = 319;
#[cfg(target_os = "linux")]
fn udp_hw_filter(filter: c_int, port: u16, data: &[u8]) -> bool {
if STAMP_ALL_FILTERS.contains(&filter) {
return true;
}
if filter == 15 {
return port == 123;
}
if port != PTP_EVENT_PORT || data.len() < 34 {
return false;
}
let version = data[1] & 0x0f;
let message = if version == 1 {
data[32]
} else {
data[0] & 0x0f
};
match (filter, version) {
(3, 1) => message <= 1,
(4, 1) => message == 0,
(5, 1) => message == 1,
(6 | 12, 2) => message <= 3,
(7 | 13, 2) => message == 0,
(8 | 14, 2) => message == 1,
_ => false,
}
}
#[cfg(target_os = "linux")]
fn nic_hw_stamp(
nics: &HashMap<String, NicState>,
ptp_clocks: &std::collections::BTreeMap<u32, i64>,
name: &str,
real: std::time::Duration,
rx_port: Option<(u16, &[u8])>,
) -> Option<std::time::Duration> {
let nic = nics.get(name).filter(|n| n.hwtstamp_supported)?;
let [_, tx_type, rx_filter] = nic.hwtstamp;
let stamps = match rx_port {
None => tx_type != 0,
Some((port, data)) => udp_hw_filter(rx_filter, port, data),
};
if !stamps {
return None;
}
let offset = nic
.ethtool
.phc_index
.and_then(|i| ptp_clocks.get(&i))
.copied()
.unwrap_or(0);
let nanos = (real.as_nanos() as i128 + i128::from(offset)).max(0);
Some(std::time::Duration::from_nanos(nanos as u64))
}
#[cfg(target_os = "linux")]
fn udp_onestep_stamp(tx_type: c_int, port: u16, data: &[u8]) -> bool {
if port != 319 || data.len() < 44 || data[1] & 0xf != 2 || data[6] & 2 != 0 {
return false;
}
let message = data[0] & 0xf;
message == 0 && matches!(tx_type, 2 | 3) || message == 3 && tx_type == 3 && data.len() >= 54
}
pub struct SimHost {
state: Mutex<HostState>,
devnull: c_int,
clock: Arc<Clock>,
isolate_env: bool,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
registries: Mutex<Option<Arc<crate::fabric::Registries>>>,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
me: std::sync::OnceLock<std::sync::Weak<SimHost>>,
topology: (Vec<(NicSpec, NicCounters)>, Vec<Route>),
tracefs: Option<Arc<crate::tracefs::TracefsPlane>>,
}
impl SimHost {
pub(crate) fn attach_fabric(self: &Arc<Self>, regs: Arc<crate::fabric::Registries>) {
let backend: std::sync::Weak<dyn crate::fabric::ForeignUdp> = Arc::downgrade(self) as _;
crate::fabric::attach_foreign_udp(®s, backend);
let mut state = self.state.lock().unwrap();
let privileges = crate::Privileges::from_caps(state.caps, state.root);
regs.shared.sys.set_privileges(|p| *p = privileges);
state.shared = Arc::downgrade(®s.shared);
drop(state);
if let Some(t) = &self.tracefs {
t.attach(®s.shared, self);
}
*self.registries.lock().unwrap() = Some(regs);
let _ = self.me.set(Arc::downgrade(self));
}
pub(crate) fn tracefs_plane(&self) -> Option<Arc<dyn snare_interpose::Fs>> {
self.tracefs
.clone()
.map(|t| t as Arc<dyn snare_interpose::Fs>)
}
pub fn tracefs(&self) -> Option<crate::tracefs::TracefsSim> {
self.tracefs.clone().map(crate::tracefs::TracefsSim::new)
}
pub(crate) fn current_affinity(&self) -> Option<Vec<usize>> {
#[cfg(target_os = "linux")]
{
let mut state = self.state.lock().unwrap();
let tid = state.resolve(0);
let affinity = state.threads[&tid].affinity.clone();
if affinity.is_empty() {
Some(state.online.to_vec())
} else {
Some(affinity)
}
}
#[cfg(not(target_os = "linux"))]
{
None
}
}
#[cfg(target_os = "linux")]
fn shared(&self) -> Option<Arc<crate::scope::SimShared>> {
let regs = self.registries.lock().unwrap();
regs.as_ref().map(|regs| regs.shared.clone())
}
#[cfg(target_os = "linux")]
fn sockopt_rec(&self, fd: c_int) -> Option<(Arc<SockRec>, bool)> {
let state = self.state.lock().unwrap();
if let Some(netlink) = state.netlinks.get(&fd) {
return Some((netlink.rec.clone(), true));
}
Some((state.udp.get(&fd)?.rec.clone(), false))
}
#[cfg(target_os = "linux")]
fn open_socket(&self, kind: SocketKind) -> Result<(c_int, Arc<SockRec>), c_int> {
let shared = self.shared().ok_or(libc::EAFNOSUPPORT)?;
let fd = self
.reserve_fd()
.map_err(|e| e.raw_os_error().unwrap_or(libc::EMFILE))?;
Ok((fd, shared.new_socket(kind, fd)))
}
#[cfg(target_os = "linux")]
fn detach_socket(&self, fd: c_int, replaced: bool) -> Option<()> {
let rec = {
let mut state = self.state.lock().unwrap();
let rec = state.socket_rec(fd)?;
state.remove_socket(fd);
rec
};
if replaced {
if let Some(shared) = self.shared() {
shared.sockets.fd_replaced(fd, &rec, shared.stamp());
}
} else {
self.socket_closed(fd);
}
if let Some(shared) = self.shared() {
shared.bump_keys(&[rec.wake_key()]);
}
Some(())
}
#[cfg(target_os = "linux")]
fn socket_closed(&self, fd: c_int) {
if let Some(shared) = self.shared() {
shared.socket_closed(fd);
}
}
#[cfg(target_os = "linux")]
fn udp_send(
&self,
fd: c_int,
data: Vec<u8>,
dest: std::net::SocketAddr,
now: std::time::Duration,
ctl: &crate::ancillary::SendCtl,
) -> Option<NetResult> {
if data.len() > if dest.is_ipv4() { 65507 } else { 65527 } {
return err(libc::EMSGSIZE);
}
let regs = self.registries.lock().unwrap().clone()?;
let shared = regs.shared.clone();
let station = regs.station_at(dest.ip());
let (local, requested, domain, broadcast, rec) = {
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
(
sock.local,
sock.requested,
sock.domain,
sock.broadcast,
sock.rec.clone(),
)
};
rec.land();
if let Some(errno) = rec.take_error_as(&shared, crate::sockets::Taker::Send) {
return err(errno);
}
let unhashed = crate::fabric::unhashed(requested);
if domain == libc::AF_INET6
&& let Err(errno) =
crate::fabric::dgram_crossing(&rec, local.or(unhashed), dest, false, false)
{
if local.is_none() && requested.is_none() {
let mut state = self.state.lock().unwrap();
let _ = state.autobind(fd, std::net::Ipv6Addr::UNSPECIFIED.into(), &shared);
}
return err(errno);
}
let mut view = rec.view(
local
.or(unhashed)
.map(|local| crate::netif::toward(local, dest)),
);
if ctl.oif.is_some() {
view.device = ctl.oif;
}
if let Some(src) = ctl.src.filter(|src| src.is_ipv4() == dest.is_ipv4()) {
view.local = Some(std::net::SocketAddr::new(
src,
view.local.map_or(0, |l| l.port()),
));
}
let sender =
match shared.route_send(&view, dest, crate::netif::Op::Send, station, broadcast) {
Ok(sender) => sender,
Err(errno) => return err(errno),
};
let gso = crate::ancillary::gso_size(&rec, ctl);
let checked = if gso != 0 {
crate::netif::frag::check_gso(&shared, &sender, dest, data.len(), gso)
} else {
crate::netif::frag::check_send(
&shared,
&rec,
domain == libc::AF_INET6,
&sender,
dest,
data.len(),
ctl.dontfrag,
)
};
if let Err(errno) = checked {
return err(errno);
}
let local_path = matches!(&sender, crate::netif::Sender::Host(path) if path.local);
if data.len() > usize::from(gso) && gso != 0 && !local_path {
let segment = crate::ancillary::SendCtl {
gso: Some(0),
..*ctl
};
for chunk in data.chunks(usize::from(gso)) {
if let Some(NetResult::Err(errno)) =
self.udp_send(fd, chunk.to_vec(), dest, now, &segment)
{
return err(errno);
}
}
return ok(data.len() as i64);
}
if let Some(local) = local
&& shared.policies.send_stalled(local)
{
return err(libc::EAGAIN);
}
let joined = dest.ip().is_multicast() && shared.host_joined(dest.ip());
let mut state = self.state.lock().unwrap();
let local = match local {
Some(local) => local,
None => {
let ip = unhashed.map_or(
if domain == libc::AF_INET6 {
std::net::IpAddr::V6(std::net::Ipv6Addr::UNSPECIFIED)
} else {
std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)
},
|a| a.ip(),
);
let Some(sa) = state.autobind(fd, ip, &shared) else {
return err(libc::EAGAIN);
};
sa
}
};
let src = match ctl.src {
Some(ip) => std::net::SocketAddr::new(ip, local.port()),
None => sender.source(crate::netif::toward(local, dest)),
};
let ip = crate::ancillary::Header {
gso: if data.len() > usize::from(gso) {
gso
} else {
0
},
..crate::ancillary::header(&rec, dest, ctl)
};
let len = data.len();
let cands = state.udp_cands(dest, joined);
let copies = shared.fan_out(
&sender,
crate::netif::Wire { src, dest, len },
station,
cands,
true,
);
let mut keys = crate::readiness::WakeKeys::default();
keys.push(rec.wake_key());
for copy in &copies {
if let Some(socket) = state.udp.get(©.q) {
keys.push(socket.rec.wake_key());
}
}
let mut reached = copies.len();
state.udp_land(copies, &data, src, now, ip);
let hw = sender
.egress_name()
.filter(|name| {
state
.nics
.get(*name)
.is_none_or(|nic| !udp_onestep_stamp(nic.hwtstamp[1], dest.port(), &data))
})
.and_then(|nic| nic_hw_stamp(&state.nics, &state.ptp_clocks, nic, now, None));
crate::tstamp::udp_sent(
&rec,
crate::tstamp::Stamp::from_real(now),
hw,
ctl.tx_flags,
|| crate::tstamp::looped_frame(src, dest, false, &data),
);
rec.note_tx_nic(sender.egress_name());
rec.count_udp_sent(dest, &sender);
drop(state);
shared.capture_udp(
&sender,
src,
dest,
crate::pcapng::Dir::Out,
&data,
Some(now),
);
reached += crate::fabric::deliver_to_endpoints(®s, &sender, src, dest, &data, ip);
if reached == 0 {
shared.unreachable_port(&rec, &sender, src, dest, &data, station);
}
shared.bump_keys(keys.as_slice());
ok(len as i64)
}
fn new(p: HostProfile) -> Self {
let uname = p.uname();
let online = p.online.unwrap_or_else(|| (0..p.cpu_count).collect());
let topology = (
p.nics.iter().map(Nic::topology_fact).collect(),
p.routes.clone(),
);
#[cfg(target_os = "linux")]
let ptp_clocks = {
let mut clocks = p.ptp_clocks.clone();
for nic in &p.nics {
if let Some(index) = nic.ptp_index {
clocks.entry(index).or_insert(0);
}
}
clocks
};
#[cfg(target_os = "linux")]
let default_qdisc = p.default_qdisc.clone();
let nics = p
.nics
.into_iter()
.map(|n| {
let mut ethtool = n.ethtool;
ethtool.phc_index = n.ptp_index;
let (rx_queues, tx_queues) = match ethtool.channels {
Some(c) => ((c.combined + c.rx) as usize, (c.combined + c.tx) as usize),
None => (n.rx_queues, n.tx_queues),
};
let num_tx_queues = n
.tx_queues_allocated
.or(ethtool
.channels
.map(|c| (c.combined_max + c.tx_max) as usize))
.unwrap_or(tx_queues)
.max(tx_queues);
#[cfg(target_os = "linux")]
let qdiscs = crate::qdisc::NicQdiscs::initial(
&crate::qdisc::Device {
num_tx_queues,
real_tx_queues: tx_queues,
no_queue: n.no_queue,
default_qdisc: &default_qdisc,
etf_offload: ethtool.etf_offload.as_deref(),
},
p.etf,
);
(
n.name,
NicState {
ifindex: n.ifindex,
driver: n.driver,
driver_version: n.driver_version,
bus_info: n.bus_info,
hwtstamp_supported: n.hwtstamp_supported,
hwtstamp: n.hwtstamp,
hwtstamp_rx_mapping: n.hwtstamp_rx_mapping,
operstate: n.operstate,
carrier: n.carrier,
subsystem: n.subsystem,
rx_queues,
tx_queues,
num_tx_queues,
no_queue: n.no_queue,
msi_irqs: n.msi_irqs,
ethtool,
#[cfg(target_os = "linux")]
qdiscs,
},
)
})
.collect();
let devnull =
snare_interpose::real(|| unsafe { libc::open(c"/dev/null".as_ptr(), libc::O_RDWR) });
let clock = Arc::new(Clock::new(p.tai_offset_secs));
let tracefs = Some(Arc::new(crate::tracefs::TracefsPlane::new(
p.tracefs.clone(),
p.root,
p.cpu_count,
clock.clone(),
)));
SimHost {
tracefs,
state: Mutex::new(HostState {
cpu_count: p.cpu_count,
online,
isolated: p.isolated,
nohz_full: p.nohz_full,
governor: p.governor,
preempt_rt: p.preempt_rt,
caps: p.caps,
root: p.root,
cpu_dma_latency: None,
open: OpenFiles::default(),
#[cfg(target_os = "linux")]
dirs: OpenFiles::default(),
locks: HashMap::default(),
threads: std::collections::BTreeMap::new(),
thread_ids: HashMap::default(),
next_tid: TID_BASE,
#[cfg(target_os = "linux")]
pthreads: HashMap::default(),
nics,
default_qdisc: p.default_qdisc,
#[cfg(target_os = "linux")]
ptp_clocks,
#[cfg(target_os = "linux")]
ptp_caps: p.ptp_caps,
#[cfg(target_os = "linux")]
qdisc_auto: 0x8000_0000,
#[cfg(target_os = "macos")]
mac_threads: HashMap::default(),
#[cfg(target_os = "linux")]
udp: UdpSockets::default(),
#[cfg(target_os = "linux")]
netlinks: NetlinkSockets::default(),
#[cfg(target_os = "linux")]
bound: HashMap::default(),
#[cfg(target_os = "linux")]
next_ephemeral: 49152,
#[cfg(target_os = "linux")]
inherited_threads: HashMap::default(),
env: p
.env
.into_iter()
.filter_map(|(k, v)| {
Some((
std::ffi::CString::new(k).ok()?,
std::ffi::CString::new(v).ok()?,
))
})
.collect(),
shared: std::sync::Weak::new(),
uname,
nodename: p.nodename.into_bytes(),
clock: clock.clone(),
}),
devnull,
clock,
isolate_env: p.isolate_env,
registries: Mutex::new(None),
me: std::sync::OnceLock::new(),
topology,
}
}
#[cfg(target_os = "macos")]
unsafe fn kern_node(
&self,
name: &str,
oldp: *mut u8,
oldlenp: *mut usize,
newp: *const u8,
newlen: usize,
) -> Option<HostResult> {
let mut state = self.state.lock().unwrap();
let u = &state.uname;
let value = match name {
"kern.ostype" => &u.sysname,
"kern.osrelease" => &u.release,
"kern.version" => &u.version,
"kern.hostname" => {
if !newp.is_null() && !state.privileges().root {
return err(libc::EPERM);
}
let old = [state.hostname(), b"\0"].concat();
let read = unsafe { read_node(&old, true, oldp, oldlenp) };
if !matches!(read, Some(HostResult::Ok(_))) || newp.is_null() {
return read;
}
if newlen > NODENAME_MAX {
return err(libc::EINVAL);
}
state.nodename = unsafe { std::slice::from_raw_parts(newp, newlen) }.to_vec();
return ok(0);
}
_ => return None,
};
if !newp.is_null() {
return err(libc::EPERM);
}
unsafe { read_node(&[value.as_bytes(), b"\0"].concat(), false, oldp, oldlenp) }
}
pub(crate) fn topology_facts(&self) -> (Vec<(NicSpec, NicCounters)>, Vec<Route>) {
self.topology.clone()
}
pub(crate) fn isolate_env(&self) -> bool {
self.isolate_env
}
pub(crate) fn clock_layer(self: &Arc<Self>) -> Arc<dyn Layer> {
Arc::new(ClockLayer(self.clock.clone()))
}
pub(crate) fn clock(&self) -> Arc<Clock> {
self.clock.clone()
}
fn boot(&self) -> Stamp {
self.clock.base_realtime().as_nanos() as Stamp
}
pub fn cpu_dma_latency(&self) -> Option<i32> {
self.state.lock().unwrap().cpu_dma_latency
}
pub fn ethtool(&self, nic: &str) -> Option<crate::ethtool::NicEthtool> {
self.state
.lock()
.unwrap()
.nics
.get(nic)
.map(|n| n.ethtool.clone())
}
pub fn set_driver_stat(&self, nic: &str, name: &str, value: u64) -> bool {
let mut state = self.state.lock().unwrap();
let Some(n) = state.nics.get_mut(nic) else {
return false;
};
match n.ethtool.stats.iter_mut().find(|(s, _)| s == name) {
Some((_, v)) => *v = value,
None => n.ethtool.stats.push((name.to_string(), value)),
}
true
}
#[cfg(target_os = "linux")]
pub fn last_tx_deadline(&self, fd: c_int) -> Option<u64> {
self.state
.lock()
.unwrap()
.udp
.get(&fd)
.and_then(|s| s.tx_deadline)
}
fn reserve_fd(&self) -> std::io::Result<c_int> {
if self.devnull < 0 {
return Err(std::io::Error::from_raw_os_error(libc::EMFILE));
}
let fd = unsafe { libc::dup(self.devnull) };
if fd < 0 {
return Err(std::io::Error::last_os_error());
}
Ok(fd)
}
fn lock_target(&self, fd: c_int) -> Option<(Arc<Locks>, Target)> {
let mut state = self.state.lock().unwrap();
let (path, target) = match state.open.get(&fd) {
Some(file) => {
let mode = file.flags & libc::O_ACCMODE;
let target = Target {
readable: mode != libc::O_WRONLY,
writable: mode != libc::O_RDONLY,
cursor: file.cursor as u64,
len: file.data.len() as u64,
description: file.description,
};
(file.path.clone(), target)
}
#[cfg(target_os = "linux")]
None => {
let stream = state.dirs.get(&fd)?;
let target = Target {
readable: true,
writable: false,
cursor: 0,
len: 0,
description: stream.description,
};
(stream.path.clone()?, target)
}
#[cfg(not(target_os = "linux"))]
None => return None,
};
Some((state.locks.entry(path).or_default().clone(), target))
}
unsafe fn status_target(&self, dirfd: c_int, path: *const c_char) -> Option<Result<(), c_int>> {
let state = self.state.lock().unwrap();
if path.is_null() {
return (state.open.contains_key(&dirfd) || state.is_dir(dirfd)).then_some(Ok(()));
}
let path = unsafe { path_of(path) }?;
#[cfg(target_os = "linux")]
if state.dir_entries(&path).is_some() {
return Some(Ok(()));
}
if state.known_file(&path) {
Some(Ok(()))
} else if state.owns_path(&path) {
Some(Err(libc::ENOENT))
} else {
None
}
}
fn release_locks(&self, dropped: Dropped) {
let Some(path) = dropped.path else {
return;
};
let locks = self.state.lock().unwrap().locks.get(&path).cloned();
if let Some(locks) = locks {
locks.release_process();
if let Some(description) = dropped.last {
locks.release_description(description);
}
}
}
}
struct Dropped {
path: Option<std::path::PathBuf>,
last: Option<u64>,
}
impl Drop for SimHost {
fn drop(&mut self) {
#[cfg(target_os = "linux")]
if let Ok(state) = self.state.lock() {
for &fd in state.udp.keys() {
unsafe { libc::close(fd) };
}
for &fd in state.netlinks.keys() {
unsafe { libc::close(fd) };
}
}
if self.devnull >= 0 {
unsafe { libc::close(self.devnull) };
}
}
}
#[cfg(target_os = "linux")]
fn directory_links(entries: &[DirEntry]) -> u64 {
2 + entries
.iter()
.skip(2)
.filter(|entry| entry.dtype == libc::DT_DIR)
.count() as u64
}
fn host_file_metadata(path: &Path, length: usize) -> (u32, u64) {
#[cfg(target_os = "linux")]
{
if path.starts_with("/proc") {
return (
if path.starts_with("/proc/sys") {
0o100644
} else {
0o100444
},
0,
);
}
if path.starts_with("/sys") {
let writable = matches!(
path.file_name().and_then(|name| name.to_str()),
Some(
"mtu"
| "flags"
| "threaded"
| "tx_queue_len"
| "gro_flush_timeout"
| "napi_defer_hard_irqs"
)
);
return (if writable { 0o100644 } else { 0o100444 }, unsafe {
libc::sysconf(libc::_SC_PAGESIZE)
}
as u64);
}
}
let _ = path;
(0o100444, length as u64)
}
pub(crate) fn host_stat(
buf: *mut u8,
path: &Path,
mode: u32,
size: u64,
links: u64,
boot: Stamp,
) -> Option<FsResult> {
let result = fill_stat(buf, 0, &host_attributes(path, mode, size, links));
let result = with_times(buf, result, &Times::at(boot), false);
#[cfg(target_os = "linux")]
if matches!(result, Some(FsResult::Ok(_))) {
unsafe {
(*buf.cast::<libc::stat>()).st_blocks = 0;
if path.starts_with("/proc") {
(*buf.cast::<libc::stat>()).st_blksize = 1024;
}
}
}
result
}
fn root_owned(path: &Path) -> bool {
path.starts_with("/sys") || path.starts_with("/proc/sys") || path.starts_with("/proc/net")
}
fn host_attributes(path: &Path, mode: u32, size: u64, links: u64) -> Attributes {
let (uid, gid) = if root_owned(path) {
(0, 0)
} else {
let caller = Credentials::current();
(caller.uid, caller.gid)
};
Attributes {
mode,
size,
ino: ino_of(path),
nlink: links,
uid,
gid,
}
}
#[cfg(target_os = "linux")]
pub(crate) fn host_statx(
buf: *mut u8,
path: &Path,
mode: u32,
size: u64,
links: u64,
boot: Stamp,
) -> Option<FsResult> {
let result = fill_statx(buf, 0, &host_attributes(path, mode, size, links));
let result = with_times(buf, result, &Times::at(boot), true);
if matches!(result, Some(FsResult::Ok(_))) {
unsafe {
if path.starts_with("/proc") || path.starts_with("/sys") {
let mask = buf.cast::<u32>().read_unaligned();
buf.cast::<u32>().write_unaligned(mask & !libc::STATX_BTIME);
buf.add(80).write_bytes(0, 16);
}
buf.add(48).cast::<u64>().write_unaligned(0);
if path.starts_with("/proc") {
buf.add(4).cast::<u32>().write_unaligned(1024);
}
}
}
result
}
impl Fs for SimHost {
fn owns(&self, fd: c_int) -> bool {
let state = self.state.lock().unwrap();
if state.open.contains_key(&fd) {
return true;
}
#[cfg(target_os = "linux")]
if state.dirs.contains_key(&fd) {
return true;
}
false
}
unsafe fn open(&self, path: *const c_char, flags: c_int, _mode: u32) -> Option<FsResult> {
let path = unsafe { path_of(path) }?;
let mut state = self.state.lock().unwrap();
if path == Path::new("/dev/cpu_dma_latency") {
let fd = match self.reserve_fd() {
Ok(fd) => fd,
Err(e) => return err(e.raw_os_error().unwrap_or(libc::EMFILE)),
};
state.cpu_dma_latency = Some(2_000_000_000);
state.open.insert(
fd,
OpenFile {
data: Vec::new(),
read_error: None,
cursor: 0,
writable: true,
flags: status_flags(flags),
path,
description: crate::fs_locks::description(),
},
);
set_cloexec(fd, flags);
return ok(fd as i64);
}
#[cfg(target_os = "linux")]
if let Some(index) = parse_ptp_index(&path) {
if !state.ptp_clocks.contains_key(&index) {
return err(libc::ENOENT);
}
let fd = match self.reserve_fd() {
Ok(fd) => fd,
Err(e) => return err(e.raw_os_error().unwrap_or(libc::EMFILE)),
};
state.open.insert(
fd,
OpenFile {
data: Vec::new(),
read_error: None,
cursor: 0,
writable: flags & (libc::O_WRONLY | libc::O_RDWR) != 0,
flags: status_flags(flags),
path,
description: crate::fs_locks::description(),
},
);
set_cloexec(fd, flags);
return ok(fd as i64);
}
#[cfg(target_os = "linux")]
if state.dir_entries(&path).is_some() {
if flags & (libc::O_WRONLY | libc::O_RDWR | libc::O_CREAT | libc::O_TRUNC) != 0 {
return err(libc::EISDIR);
}
drop(state);
let path = std::ffi::CString::new(path.as_os_str().as_encoded_bytes()).unwrap();
let result = unsafe { self.opendir(path.as_ptr()) };
if let Some(FsResult::Ok(fd)) = result
&& flags & libc::O_CLOEXEC == 0
{
descriptor_fcntl(fd as c_int, libc::F_SETFD, 0);
}
return result;
}
let Some(data) = state.render(&path) else {
return if state.owns_path(&path) {
err(libc::ENOENT)
} else {
None
};
};
let write = flags & libc::O_WRONLY != 0 || flags & libc::O_RDWR != 0;
if write && !(state.threaded_attr(&path).is_some() && state.privileges().root) {
return err(libc::EACCES);
}
let fd = match self.reserve_fd() {
Ok(fd) => fd,
Err(e) => return err(e.raw_os_error().unwrap_or(libc::EMFILE)),
};
let read_error = state.read_error(&path);
state.open.insert(
fd,
OpenFile {
data,
read_error,
cursor: 0,
writable: write,
flags: status_flags(flags),
path,
description: crate::fs_locks::description(),
},
);
set_cloexec(fd, flags);
ok(fd as i64)
}
unsafe fn openat(
&self,
dirfd: c_int,
path: *const c_char,
flags: c_int,
mode: u32,
) -> Option<FsResult> {
if dirfd != libc::AT_FDCWD {
let absolute = unsafe { path.as_ref() }.is_some_and(|p| *p == b'/' as c_char);
if !absolute {
return None;
}
}
unsafe { self.open(path, flags, mode) }
}
unsafe fn read(&self, fd: c_int, buf: *mut u8, len: usize) -> Option<FsResult> {
let mut state = self.state.lock().unwrap();
#[cfg(target_os = "linux")]
if state.dirs.contains_key(&fd) {
return err(libc::EISDIR);
}
let file = state.open.get_mut(&fd)?;
if file.flags & libc::O_ACCMODE == libc::O_WRONLY {
return err(libc::EBADF);
}
if let Some(errno) = file.read_error {
return err(errno);
}
let start = file.cursor.min(file.data.len());
let n = len.min(file.data.len() - start);
if n != 0 {
unsafe { std::ptr::copy_nonoverlapping(file.data[start..].as_ptr(), buf, n) };
}
file.cursor += n;
ok(n as i64)
}
unsafe fn pread(&self, fd: c_int, buf: *mut u8, len: usize, offset: i64) -> Option<FsResult> {
let state = self.state.lock().unwrap();
#[cfg(target_os = "linux")]
if state.dirs.contains_key(&fd) {
return err(if offset < 0 {
libc::EINVAL
} else {
libc::EISDIR
});
}
let file = state.open.get(&fd)?;
if offset < 0 {
return err(libc::EINVAL);
}
if file.flags & libc::O_ACCMODE == libc::O_WRONLY {
return err(libc::EBADF);
}
if let Some(errno) = file.read_error {
return err(errno);
}
let start = (offset as usize).min(file.data.len());
let n = len.min(file.data.len() - start);
if n != 0 {
unsafe { std::ptr::copy_nonoverlapping(file.data[start..].as_ptr(), buf, n) };
}
ok(n as i64)
}
unsafe fn write(&self, fd: c_int, buf: *const u8, len: usize) -> Option<FsResult> {
let mut state = self.state.lock().unwrap();
#[cfg(target_os = "linux")]
if state.dirs.contains_key(&fd) {
return err(libc::EBADF);
}
let is_latency = state
.open
.get(&fd)
.is_some_and(|f| f.writable && f.path == Path::new("/dev/cpu_dma_latency"));
if is_latency {
if len >= 4 {
let mut micros = [0u8; 4];
unsafe { std::ptr::copy_nonoverlapping(buf, micros.as_mut_ptr(), 4) };
state.cpu_dma_latency = Some(i32::from_ne_bytes(micros));
}
return ok(len as i64);
}
let threaded = state
.open
.get(&fd)
.filter(|f| f.writable)
.and_then(|f| state.threaded_attr(&f.path));
if let Some(nic) = threaded {
let text = unsafe { std::slice::from_raw_parts(buf, len) };
return match state.store_threaded(&nic, text) {
Ok(()) => ok(len as i64),
Err(e) => err(e),
};
}
if let Some(file) = state.open.get(&fd) {
return err(if file.writable {
libc::EACCES
} else {
libc::EBADF
});
}
None
}
unsafe fn pwrite(
&self,
fd: c_int,
_buf: *const u8,
_len: usize,
_offset: i64,
) -> Option<FsResult> {
let state = self.state.lock().unwrap();
let file = state.open.get(&fd)?;
err(if file.path.starts_with("/dev") {
libc::EOPNOTSUPP
} else {
libc::ESPIPE
})
}
unsafe fn ftruncate(&self, fd: c_int, _len: i64) -> Option<FsResult> {
let state = self.state.lock().unwrap();
state.open.get(&fd)?;
err(libc::EINVAL)
}
unsafe fn fsync(&self, fd: c_int) -> Option<FsResult> {
let state = self.state.lock().unwrap();
let file = state.open.get(&fd)?;
if file.path.starts_with("/sys") {
ok(0)
} else {
err(libc::EINVAL)
}
}
unsafe fn lseek(&self, fd: c_int, offset: i64, whence: c_int) -> Option<FsResult> {
let mut state = self.state.lock().unwrap();
#[cfg(target_os = "linux")]
if let Some(stream) = state.dirs.get_mut(&fd) {
let base = match whence {
libc::SEEK_SET => 0,
libc::SEEK_CUR => stream.cursor as i64,
_ => return err(libc::EINVAL),
};
let Some(target) = base.checked_add(offset).filter(|target| *target >= 0) else {
return err(libc::EINVAL);
};
stream.cursor = target as usize;
return ok(target);
}
let file = state.open.get_mut(&fd)?;
let base = match whence {
libc::SEEK_SET => 0i64,
libc::SEEK_CUR => file.cursor as i64,
libc::SEEK_END => file.data.len() as i64,
_ => return err(libc::EINVAL),
};
let target = base.saturating_add(offset);
if target < 0 {
return err(libc::EINVAL);
}
file.cursor = target as usize;
ok(target)
}
unsafe fn fstat(&self, fd: c_int, buf: *mut u8) -> Option<FsResult> {
let state = self.state.lock().unwrap();
#[cfg(target_os = "linux")]
if let Some(stream) = state.dirs.get(&fd) {
let path = stream.path.as_ref()?;
let links = directory_links(&stream.entries);
return host_stat(buf, path, 0o040755, 0, links, self.boot());
}
let file = state.open.get(&fd)?;
let (mode, size) = host_file_metadata(&file.path, file.data.len());
host_stat(buf, &file.path, mode, size, 1, self.boot())
}
unsafe fn stat(&self, path: *const c_char, buf: *mut u8) -> Option<FsResult> {
let path = unsafe { path_of(path) }?;
let state = self.state.lock().unwrap();
#[cfg(target_os = "linux")]
if let Some(entries) = state.dir_entries(&path) {
let links = directory_links(&entries);
return host_stat(buf, &path, 0o040755, 0, links, self.boot());
}
let Some(data) = state.render(&path) else {
return if state.owns_path(&path) {
err(libc::ENOENT)
} else {
None
};
};
let (mode, size) = host_file_metadata(&path, data.len());
host_stat(buf, &path, mode, size, 1, self.boot())
}
unsafe fn lstat(&self, path: *const c_char, buf: *mut u8) -> Option<FsResult> {
unsafe { self.stat(path, buf) }
}
unsafe fn readlink(&self, path: *const c_char, buf: *mut u8, len: usize) -> Option<FsResult> {
let path = unsafe { path_of(path) }?;
let p = path.to_str()?;
let state = self.state.lock().unwrap();
let subsystem = p
.strip_prefix("/sys/class/net/")
.and_then(|r| r.strip_suffix("/device/subsystem"))
.and_then(|name| state.nics.get(name));
let Some(nic) = subsystem else {
return if state.owns_path(&path) {
err(libc::ENOENT)
} else {
None
};
};
let target = format!("../../../../bus/{}", nic.subsystem);
let bytes = target.as_bytes();
let n = bytes.len().min(len);
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), buf, n) };
ok(n as i64)
}
unsafe fn readlinkat(
&self,
dirfd: c_int,
path: *const c_char,
buf: *mut u8,
len: usize,
) -> Option<FsResult> {
if dirfd != libc::AT_FDCWD {
let absolute = unsafe { path.as_ref() }.is_some_and(|p| *p == b'/' as c_char);
if !absolute {
return None;
}
}
unsafe { self.readlink(path, buf, len) }
}
#[cfg(target_os = "linux")]
unsafe fn statx(
&self,
dirfd: c_int,
path: *const c_char,
_flags: c_int,
_mask: u32,
buf: *mut u8,
) -> Option<FsResult> {
if let Some(p) = unsafe { path.as_ref() }
&& *p == 0
{
let state = self.state.lock().unwrap();
if let Some(stream) = state.dirs.get(&dirfd) {
let path = stream.path.as_ref()?;
let links = directory_links(&stream.entries);
return host_statx(buf, path, 0o040755, 0, links, self.boot());
}
let file = state.open.get(&dirfd)?;
let (mode, size) = host_file_metadata(&file.path, file.data.len());
return host_statx(buf, &file.path, mode, size, 1, self.boot());
}
let path = unsafe { path_of(path) }?;
let state = self.state.lock().unwrap();
if let Some(entries) = state.dir_entries(&path) {
let links = directory_links(&entries);
return host_statx(buf, &path, 0o040755, 0, links, self.boot());
}
let Some(data) = state.render(&path) else {
return if state.owns_path(&path) {
err(libc::ENOENT)
} else {
None
};
};
let (mode, size) = host_file_metadata(&path, data.len());
host_statx(buf, &path, mode, size, 1, self.boot())
}
unsafe fn access(&self, path: *const c_char, mode: c_int) -> Option<FsResult> {
let path = unsafe { path_of(path) }?;
let state = self.state.lock().unwrap();
if let Some(data) = state.render(&path) {
let attributes = host_attributes(&path, host_file_metadata(&path, data.len()).0, 0, 1);
let owner = Owner {
mode: attributes.mode & 0o7777,
uid: attributes.uid,
gid: attributes.gid,
};
let want = mode as u32 & (fs_perm::READ | fs_perm::WRITE | fs_perm::EXEC);
return match Credentials::current().check(&owner, Kind::File, want) {
Ok(()) => ok(0),
Err(error) => err(error),
};
}
if state.known_file(&path) {
ok(0)
} else if state.owns_path(&path) {
err(libc::ENOENT)
} else {
None
}
}
unsafe fn chmod(
&self,
dirfd: c_int,
path: *const c_char,
_mode: u32,
_flags: c_int,
) -> Option<FsResult> {
match unsafe { self.status_target(dirfd, path) }? {
Ok(()) => err(libc::EPERM),
Err(error) => err(error),
}
}
unsafe fn chown(
&self,
dirfd: c_int,
path: *const c_char,
uid: u32,
gid: u32,
_flags: c_int,
) -> Option<FsResult> {
match unsafe { self.status_target(dirfd, path) }? {
Ok(()) if uid == u32::MAX && gid == u32::MAX => ok(0),
Ok(()) => err(libc::EPERM),
Err(error) => err(error),
}
}
unsafe fn faccessat(
&self,
_dirfd: c_int,
path: *const c_char,
mode: c_int,
_flags: c_int,
) -> Option<FsResult> {
unsafe { self.access(path, mode) }
}
unsafe fn close(&self, fd: c_int) -> Option<FsResult> {
let dropped = self.state.lock().unwrap().forget(fd)?;
unsafe { libc::close(fd) };
self.release_locks(dropped);
ok(0)
}
unsafe fn fd_replaced(&self, fd: c_int) -> Option<FsResult> {
let dropped = self.state.lock().unwrap().forget(fd)?;
self.release_locks(dropped);
ok(0)
}
unsafe fn dup(&self, oldfd: c_int, newfd: c_int) -> Option<FsResult> {
let mut state = self.state.lock().unwrap();
let is_file = state.open.contains_key(&oldfd);
if !is_file && !state.is_dir(oldfd) {
return None;
}
let dropped = (oldfd != newfd).then(|| state.forget(newfd)).flatten();
if is_file {
state.open.alias(oldfd, newfd)?;
} else {
#[cfg(target_os = "linux")]
state.dirs.alias(oldfd, newfd)?;
}
drop(state);
if let Some(dropped) = dropped {
self.release_locks(dropped);
}
ok(newfd as i64)
}
unsafe fn dup_to(&self, oldfd: c_int, newfd: c_int, flags: Option<c_int>) -> Option<FsResult> {
let mut state = self.state.lock().unwrap();
let is_file = state.open.contains_key(&oldfd);
if !is_file && !state.is_dir(oldfd) {
return None;
}
let result = crate::fabric::duplicate_to(oldfd, newfd, flags);
if result < 0 {
return err(std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EBADF));
}
let dropped = (oldfd != newfd).then(|| state.forget(newfd)).flatten();
if is_file {
state.open.alias(oldfd, newfd);
} else {
#[cfg(target_os = "linux")]
state.dirs.alias(oldfd, newfd);
}
drop(state);
if let Some(dropped) = dropped {
self.release_locks(dropped);
}
ok(result as i64)
}
unsafe fn flock(&self, fd: c_int, operation: c_int) -> Option<FsResult> {
let (locks, target) = self.lock_target(fd)?;
locks.flock(target.description, operation)
}
unsafe fn fcntl(&self, fd: c_int, cmd: c_int, arg: i64) -> Option<FsResult> {
if crate::fs_locks::is_record_lock(cmd) {
let (locks, target) = self.lock_target(fd)?;
return unsafe { locks.fcntl(cmd, arg, &target) };
}
let mut state = self.state.lock().unwrap();
let is_file = state.open.contains_key(&fd);
let is_dir = state.is_dir(fd);
if !is_file && !is_dir {
return None;
}
match cmd {
libc::F_DUPFD | libc::F_DUPFD_CLOEXEC => {
let result = descriptor_fcntl(fd, cmd, arg)?;
if let FsResult::Ok(newfd) = result {
if is_file {
state.open.alias(fd, newfd as c_int);
}
#[cfg(target_os = "linux")]
if is_dir {
state.dirs.alias(fd, newfd as c_int);
}
}
Some(result)
}
libc::F_GETFD | libc::F_SETFD => descriptor_fcntl(fd, cmd, arg),
libc::F_GETFL => ok(state
.open
.get(&fd)
.map_or(libc::O_RDONLY, |file| file.flags) as i64),
libc::F_SETFL => {
if let Some(file) = state.open.get_mut(&fd) {
let mask = libc::O_APPEND | libc::O_NONBLOCK;
file.flags = (file.flags & !mask) | (arg as c_int & mask);
}
ok(0)
}
_ => err(libc::EINVAL),
}
}
#[cfg(target_os = "linux")]
unsafe fn ioctl(&self, fd: c_int, request: u64, arg: i64) -> Option<FsResult> {
if (request >> 8) & 0xff != PTP_IOCTL_MAGIC {
return None;
}
let (offset, caps, writable, root) = {
let state = self.state.lock().unwrap();
let file = state.open.get(&fd)?;
let index = parse_ptp_index(&file.path)?;
let offset = *state.ptp_clocks.get(&index)?;
let caps = state.ptp_caps.get(&index).copied().unwrap_or_default();
(offset, caps, file.writable, state.privileges().root)
};
snare_interpose::charge_latency();
let clock = PtpFile {
caps,
writable,
root,
};
unsafe { ptp_ioctl(&clock, request, arg, || self.clock.ptp_sample(offset)) }
}
#[cfg(target_os = "linux")]
unsafe fn opendir(&self, path: *const c_char) -> Option<FsResult> {
let path = unsafe { path_of(path) }?;
let mut state = self.state.lock().unwrap();
let Some(entries) = state.dir_entries(&path) else {
return if state.owns_path(&path) {
err(libc::ENOENT)
} else {
None
};
};
let fd = match self.reserve_fd() {
Ok(fd) => fd,
Err(e) => return err(e.raw_os_error().unwrap_or(libc::EMFILE)),
};
let mut stream = DirStream::new(entries);
stream.path = Some(path);
state.dirs.insert(fd, stream);
set_cloexec(fd, libc::O_CLOEXEC);
ok(fd as i64)
}
#[cfg(target_os = "linux")]
unsafe fn readdir(&self, fd: c_int) -> Option<FsResult> {
let mut state = self.state.lock().unwrap();
let d = state.dirs.get_mut(&fd)?;
let Some(e) = d.entries.get(d.cursor) else {
return ok(0); };
let (name, ino, dtype) = (e.name.clone(), e.ino, e.dtype);
d.cursor += 1;
crate::fs_sim::fill_dirent(&mut d.scratch, &name, ino, dtype);
ok((&*d.scratch as *const _) as i64)
}
#[cfg(target_os = "linux")]
unsafe fn fdopendir(&self, fd: c_int) -> Option<FsResult> {
if self.state.lock().unwrap().dirs.contains_key(&fd) {
set_cloexec(fd, libc::O_CLOEXEC);
ok(fd as i64)
} else {
None
}
}
#[cfg(target_os = "linux")]
unsafe fn statfs(&self, raw: *const c_char, buf: *mut u8) -> Option<FsResult> {
let path = unsafe { path_of(raw) }?;
let state = self.state.lock().unwrap();
if !state.owns_path(&path) {
return None;
}
if state.render(&path).is_none() && state.dir_entries(&path).is_none() {
return err(libc::ENOENT);
}
let root = if path.starts_with("/sys") {
c"/sys"
} else if path.starts_with("/proc") {
c"/proc"
} else {
c"/dev"
};
let result = unsafe { libc::statfs(root.as_ptr(), buf.cast()) };
if result == 0 {
ok(0)
} else {
err(std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO))
}
}
#[cfg(target_os = "linux")]
unsafe fn fstatfs(&self, fd: c_int, buf: *mut u8) -> Option<FsResult> {
let state = self.state.lock().unwrap();
let path = if let Some(file) = state.open.get(&fd) {
file.path.clone()
} else {
state.dirs.get(&fd)?.path.as_ref()?.clone()
};
let root = if path.starts_with("/sys") {
c"/sys"
} else if path.starts_with("/proc") {
c"/proc"
} else {
c"/dev"
};
let result = unsafe { libc::statfs(root.as_ptr(), buf.cast()) };
if result == 0 {
ok(0)
} else {
err(std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EIO))
}
}
#[cfg(target_os = "linux")]
unsafe fn getdents64(&self, fd: c_int, buf: *mut u8, len: usize) -> Option<FsResult> {
let mut state = self.state.lock().unwrap();
if let Some(stream) = state.dirs.get_mut(&fd) {
crate::fs_sim::pack_directory(stream, buf, len)
} else if state.open.contains_key(&fd) {
err(libc::ENOTDIR)
} else {
None
}
}
#[cfg(target_os = "linux")]
unsafe fn closedir(&self, fd: c_int) -> Option<FsResult> {
self.state.lock().unwrap().dirs.remove(&fd)?;
let ret = unsafe { libc::close(fd) };
ok(ret as i64)
}
}
#[cfg(target_os = "linux")]
unsafe fn errno_ptr() -> *mut c_int {
unsafe { libc::__errno_location() }
}
#[cfg(target_os = "macos")]
unsafe fn errno_ptr() -> *mut c_int {
unsafe { libc::__error() }
}
#[cfg(test)]
#[test]
fn cpu_latency_request_survives_closing_one_descriptor_alias() {
let host = HostProfile::new().build();
let Some(FsResult::Ok(fd)) =
(unsafe { Fs::open(&*host, c"/dev/cpu_dma_latency".as_ptr(), libc::O_RDWR, 0) })
else {
panic!("opening cpu latency request failed");
};
let fd = fd as c_int;
let alias = snare_interpose::real(|| unsafe { libc::dup(fd) });
assert!(alias >= 0);
assert!(matches!(
unsafe { Fs::dup(&*host, fd, alias) },
Some(FsResult::Ok(_))
));
let latency = 37i32.to_ne_bytes();
assert!(matches!(
unsafe { Fs::write(&*host, fd, latency.as_ptr(), latency.len()) },
Some(FsResult::Ok(4))
));
assert!(matches!(
unsafe { Fs::close(&*host, fd) },
Some(FsResult::Ok(0))
));
assert_eq!(host.state.lock().unwrap().cpu_dma_latency, Some(37));
assert!(matches!(
unsafe { Fs::close(&*host, alias) },
Some(FsResult::Ok(0))
));
assert_eq!(host.state.lock().unwrap().cpu_dma_latency, None);
}
impl Env for SimHost {
fn snapshot(&self) -> Option<Vec<std::ffi::CString>> {
let state = self.state.lock().unwrap();
let mut vars: Vec<_> = state
.env
.iter()
.map(|(key, value)| {
let mut bytes = key.as_bytes().to_vec();
bytes.push(b'=');
bytes.extend_from_slice(value.as_bytes());
std::ffi::CString::new(bytes).unwrap()
})
.collect();
vars.sort();
Some(vars)
}
unsafe fn getenv(&self, name: *const c_char) -> *mut c_char {
if name.is_null() {
return std::ptr::null_mut();
}
let name = unsafe { std::ffi::CStr::from_ptr(name) };
let state = self.state.lock().unwrap();
match state.env.get(name) {
Some(value) => value.as_ptr() as *mut c_char,
None => std::ptr::null_mut(),
}
}
unsafe fn setenv(&self, name: *const c_char, value: *const c_char, overwrite: c_int) -> c_int {
if name.is_null() || value.is_null() {
unsafe { *errno_ptr() = libc::EINVAL };
return -1;
}
let key = unsafe { std::ffi::CStr::from_ptr(name) }.to_owned();
if key.to_bytes().is_empty() || key.to_bytes().contains(&b'=') {
unsafe { *errno_ptr() = libc::EINVAL };
return -1;
}
let val = unsafe { std::ffi::CStr::from_ptr(value) }.to_owned();
let mut state = self.state.lock().unwrap();
if overwrite == 0 && state.env.contains_key(&key) {
return 0;
}
state.env.insert(key, val);
0
}
unsafe fn unsetenv(&self, name: *const c_char) -> c_int {
if name.is_null() {
unsafe { *errno_ptr() = libc::EINVAL };
return -1;
}
let key = unsafe { std::ffi::CStr::from_ptr(name) }.to_owned();
if key.to_bytes().is_empty() || key.to_bytes().contains(&b'=') {
unsafe { *errno_ptr() = libc::EINVAL };
return -1;
}
self.state.lock().unwrap().env.remove(&key);
0
}
}
impl Host for SimHost {
#[cfg(target_os = "linux")]
fn thread_inherit(&self, lineage: u64) {
let mut state = self.state.lock().unwrap();
let tid = state.current_tid();
let sched = state.threads[&tid].clone();
state.inherited_threads.insert(lineage, sched);
}
#[cfg(target_os = "linux")]
fn thread_adopt(&self, lineage: u64) {
let mut state = self.state.lock().unwrap();
if let Some(sched) = state.inherited_threads.remove(&lineage) {
let tid = state.mint_tid();
state
.pthreads
.insert(unsafe { libc::pthread_self() } as u64, tid);
state.threads.insert(tid, sched);
}
}
#[cfg(target_os = "linux")]
fn thread_cancel(&self, lineage: u64) {
self.state
.lock()
.unwrap()
.inherited_threads
.remove(&lineage);
}
#[cfg(target_os = "linux")]
unsafe fn clock_gettime(&self, id: i32, buf: *mut u8) -> Option<HostResult> {
if id & 7 != 3 {
return None;
}
let fd = !(id >> 3);
let offset = {
let state = self.state.lock().unwrap();
let file = state.open.get(&fd)?;
let index = parse_ptp_index(&file.path)?;
*state.ptp_clocks.get(&index)?
};
if buf.is_null() {
return err(libc::EFAULT);
}
let (tv_sec, tv_nsec) = self.clock.ptp_sample(offset)[0];
unsafe {
buf.cast::<libc::timespec>().write(libc::timespec {
tv_sec,
tv_nsec: tv_nsec as _,
})
};
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn clock_getres(&self, id: i32, buf: *mut u8) -> Option<HostResult> {
if id & 7 != 3 {
return None;
}
let state = self.state.lock().unwrap();
let file = state.open.get(&!(id >> 3))?;
parse_ptp_index(&file.path)?;
if !buf.is_null() {
unsafe {
buf.cast::<libc::timespec>().write(libc::timespec {
tv_sec: 0,
tv_nsec: 1,
})
};
}
ok(0)
}
fn has_cap(&self, cap: c_int) -> bool {
self.state.lock().unwrap().has_cap(cap)
}
fn geteuid(&self) -> Option<u32> {
let root = self.state.lock().unwrap().privileges().root;
Some(if root { 0 } else { UNPRIVILEGED_UID })
}
fn getuid(&self) -> Option<u32> {
self.geteuid()
}
fn getegid(&self) -> Option<u32> {
let root = self.state.lock().unwrap().privileges().root;
Some(if root { 0 } else { UNPRIVILEGED_GID })
}
unsafe fn getgroups(&self, size: c_int, list: *mut u32) -> Option<HostResult> {
let gid = self.getegid()?;
unsafe { crate::proclimits::fill_groups(&[gid], size, list) }
}
fn umask(&self, mask: Option<u32>) -> Option<HostResult> {
let shared = self.state.lock().unwrap().shared.upgrade()?;
ok(i64::from(shared.sys.umask(mask)))
}
fn sysconf(&self, name: c_int) -> Option<HostResult> {
let state = self.state.lock().unwrap();
match name {
libc::_SC_NPROCESSORS_CONF => ok(state.cpu_count as i64),
libc::_SC_NPROCESSORS_ONLN => ok(state.online_count() as i64),
_ => None,
}
}
unsafe fn uname(&self, buf: *mut u8) -> Option<HostResult> {
if buf.is_null() {
return err(libc::EFAULT);
}
let state = self.state.lock().unwrap();
let uts = unsafe { &mut *(buf as *mut libc::utsname) };
let u = &state.uname;
put_c_field(&mut uts.sysname, &u.sysname);
put_c_field(&mut uts.nodename, state.hostname());
put_c_field(&mut uts.release, &u.release);
put_c_field(&mut uts.version, &u.version);
put_c_field(&mut uts.machine, &u.machine);
#[cfg(target_os = "linux")]
put_c_field(&mut uts.domainname, "(none)");
ok(0)
}
unsafe fn sethostname(&self, name: *const u8, len: i64) -> Option<HostResult> {
let mut state = self.state.lock().unwrap();
let allowed = if cfg!(target_os = "linux") {
state.has_cap(crate::limits::CAP_SYS_ADMIN)
} else {
state.privileges().root
};
if !allowed {
return err(libc::EPERM);
}
if !(0..=NODENAME_MAX as i64).contains(&len) {
return err(libc::EINVAL);
}
if name.is_null() && len > 0 {
return err(libc::EFAULT);
}
state.nodename = if len == 0 {
Vec::new()
} else {
unsafe { std::slice::from_raw_parts(name, len as usize) }.to_vec()
};
ok(0)
}
fn gettid(&self) -> Option<HostResult> {
let mut state = self.state.lock().unwrap();
ok(state.current_tid() as i64)
}
#[cfg(target_os = "linux")]
unsafe fn sched_setscheduler(
&self,
pid: i32,
policy: c_int,
param: *const u8,
) -> Option<HostResult> {
if param.is_null() || pid < 0 || policy < 0 {
return err(libc::EINVAL);
}
let priority = unsafe { (param as *const c_int).read_unaligned() };
let mut state = self.state.lock().unwrap();
let tid = state.resolve(pid);
state.set_sched(tid, policy, priority)
}
unsafe fn sched_getscheduler(&self, pid: i32) -> Option<HostResult> {
if pid < 0 {
return err(libc::EINVAL);
}
let mut state = self.state.lock().unwrap();
let tid = state.resolve(pid);
ok(state.threads[&tid].policy as i64)
}
#[cfg(target_os = "linux")]
unsafe fn sched_setparam(&self, pid: i32, param: *const u8) -> Option<HostResult> {
if param.is_null() || pid < 0 {
return err(libc::EINVAL);
}
let priority = unsafe { (param as *const c_int).read_unaligned() };
let mut state = self.state.lock().unwrap();
let tid = state.resolve(pid);
state.set_sched(tid, crate::proclimits::SETPARAM_POLICY, priority)
}
unsafe fn sched_getparam(&self, pid: i32, param: *mut u8) -> Option<HostResult> {
if pid < 0 || param.is_null() {
return err(libc::EINVAL);
}
let mut state = self.state.lock().unwrap();
let tid = state.resolve(pid);
let priority = state.threads[&tid].rt_priority;
if !param.is_null() {
unsafe { (param as *mut c_int).write_unaligned(priority) };
}
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn sched_setaffinity(&self, pid: i32, len: usize, set: *const u8) -> Option<HostResult> {
if set.is_null() || len < std::mem::size_of::<libc::cpu_set_t>() {
return err(libc::EINVAL);
}
let set = unsafe { &*(set as *const libc::cpu_set_t) };
let mut state = self.state.lock().unwrap();
let cap = std::mem::size_of::<libc::cpu_set_t>() * 8;
let count = state.cpu_count.min(cap);
let wanted: Vec<usize> = (0..count)
.filter(|&c| unsafe { libc::CPU_ISSET(c, set) })
.collect();
if wanted.is_empty() {
return err(libc::EINVAL);
}
let tid = state.resolve(pid);
state.threads.get_mut(&tid).unwrap().affinity = wanted;
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn sched_getaffinity(&self, pid: i32, len: usize, set: *mut u8) -> Option<HostResult> {
if set.is_null() || len < std::mem::size_of::<libc::cpu_set_t>() {
return err(libc::EINVAL);
}
let mut state = self.state.lock().unwrap();
let cap = std::mem::size_of::<libc::cpu_set_t>() * 8;
let count = state.cpu_count.min(cap);
let tid = state.resolve(pid);
let affinity = state.threads[&tid].affinity.clone();
let set = set as *mut libc::cpu_set_t;
unsafe {
libc::CPU_ZERO(&mut *set);
if affinity.is_empty() {
for cpu in state.online.iter().copied().filter(|&c| c < count) {
libc::CPU_SET(cpu, &mut *set);
}
} else {
for cpu in affinity.into_iter().filter(|&c| c < cap) {
libc::CPU_SET(cpu, &mut *set);
}
}
}
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn setpriority(&self, which: c_int, who: u32, prio: c_int) -> Option<HostResult> {
if which != PRIO_PROCESS {
return None;
}
let mut state = self.state.lock().unwrap();
if who != 0 && !state.threads.contains_key(&(who as i32)) {
return err(libc::ESRCH);
}
let tid = state.resolve(who as i32);
let current = state.threads[&tid].nice;
match crate::proclimits::setpriority(&state.privileges(), current, prio) {
Ok(nice) => {
state.threads.get_mut(&tid).unwrap().nice = nice;
ok(0)
}
Err(e) => err(e),
}
}
unsafe fn getpriority(&self, which: c_int, who: u32) -> Option<HostResult> {
if which != PRIO_PROCESS {
return None;
}
let mut state = self.state.lock().unwrap();
let tid = state.resolve(who as i32);
ok(state.threads[&tid].nice as i64)
}
#[cfg(target_os = "linux")]
fn mlockall(&self, flags: c_int) -> Option<HostResult> {
let shared = self.state.lock().unwrap().shared.upgrade()?;
crate::proclimits::mlockall(&shared.sys, flags)
}
#[cfg(target_os = "linux")]
fn munlockall(&self) -> Option<HostResult> {
let shared = self.state.lock().unwrap().shared.upgrade()?;
crate::proclimits::munlockall(&shared.sys)
}
fn mlock(&self, addr: u64, len: u64) -> Option<HostResult> {
let shared = self.state.lock().unwrap().shared.upgrade()?;
crate::proclimits::mlock(&shared.sys, addr, len)
}
fn munlock(&self, addr: u64, len: u64) -> Option<HostResult> {
let shared = self.state.lock().unwrap().shared.upgrade()?;
crate::proclimits::munlock(&shared.sys, addr, len)
}
unsafe fn getrlimit(&self, resource: c_int, rlim: *mut u8) -> Option<HostResult> {
let shared = self.state.lock().unwrap().shared.upgrade()?;
unsafe { crate::proclimits::getrlimit(&shared.sys, resource, rlim) }
}
unsafe fn setrlimit(&self, resource: c_int, rlim: *const u8) -> Option<HostResult> {
let shared = self.state.lock().unwrap().shared.upgrade()?;
unsafe { crate::proclimits::setrlimit(&shared.sys, resource, rlim) }
}
#[cfg(target_os = "linux")]
unsafe fn prlimit(
&self,
pid: i32,
resource: c_int,
new: *const u8,
old: *mut u8,
) -> Option<HostResult> {
let shared = self.state.lock().unwrap().shared.upgrade()?;
unsafe { crate::proclimits::prlimit(&shared.sys, pid, resource, new, old) }
}
#[cfg(target_os = "linux")]
unsafe fn pthread_setschedparam(
&self,
thread: u64,
policy: c_int,
param: *const u8,
) -> Option<HostResult> {
if param.is_null() {
return err(libc::EINVAL);
}
let priority = unsafe { (param as *const c_int).read_unaligned() };
let mut state = self.state.lock().unwrap();
let tid = state.pthread_tid(thread);
if policy < 0 {
return err(libc::EINVAL);
}
state.set_sched(tid, policy, priority)
}
#[cfg(target_os = "linux")]
unsafe fn pthread_getschedparam(
&self,
thread: u64,
policy: *mut c_int,
param: *mut u8,
) -> Option<HostResult> {
let mut state = self.state.lock().unwrap();
let tid = state.pthread_tid(thread);
let t = state.threads[&tid].clone();
if !policy.is_null() {
unsafe { policy.write_unaligned(t.policy) };
}
if !param.is_null() {
unsafe { (param as *mut c_int).write_unaligned(t.rt_priority) };
}
ok(0)
}
#[cfg(target_os = "macos")]
unsafe fn pthread_setschedparam(
&self,
thread: u64,
policy: c_int,
param: *const u8,
) -> Option<HostResult> {
if ![libc::SCHED_OTHER, libc::SCHED_FIFO, libc::SCHED_RR].contains(&policy) {
return err(libc::EINVAL);
}
let priority = if param.is_null() {
0
} else {
unsafe { (param as *const c_int).read_unaligned() }
};
self.state
.lock()
.unwrap()
.mac_threads
.insert(mac_thread_id(thread), (policy, priority));
ok(0)
}
#[cfg(target_os = "macos")]
unsafe fn pthread_getschedparam(
&self,
thread: u64,
policy: *mut c_int,
param: *mut u8,
) -> Option<HostResult> {
let (pol, prio) = self
.state
.lock()
.unwrap()
.mac_threads
.get(&mac_thread_id(thread))
.copied()
.unwrap_or((0, 0)); if !policy.is_null() {
unsafe { policy.write_unaligned(pol) };
}
if !param.is_null() {
unsafe { (param as *mut c_int).write_unaligned(prio) };
}
ok(0)
}
#[cfg(target_os = "macos")]
unsafe fn host_processor_info(
&self,
flavor: c_int,
count: *mut u32,
info: *mut *mut c_int,
info_count: *mut u32,
) -> Option<HostResult> {
const CPU_TYPE: c_int = if cfg!(target_arch = "aarch64") {
0x0100_000c
} else {
0x0100_0007
};
const CPU_SUBTYPE: c_int = if cfg!(target_arch = "aarch64") { 2 } else { 8 };
if flavor != libc::PROCESSOR_BASIC_INFO && flavor != libc::PROCESSOR_CPU_LOAD_INFO {
return None;
}
let (cpus, words) = {
let state = self.state.lock().unwrap();
let online = state.online_cpus();
let idle = state.idle_ticks() as u32 as c_int;
let words: Vec<c_int> = (0..state.cpu_count)
.flat_map(|cpu| {
let running = online.contains(&cpu);
if flavor == libc::PROCESSOR_BASIC_INFO {
let master = cpu == 0;
vec![
CPU_TYPE,
CPU_SUBTYPE,
running.into(),
cpu as c_int,
master.into(),
]
} else {
vec![0, 0, if running { idle } else { 0 }, 0]
}
})
.collect();
(state.cpu_count, words)
};
let mut address: libc::vm_address_t = 0;
#[allow(deprecated)]
let kr = unsafe {
libc::vm_allocate(
libc::mach_task_self(),
&mut address,
std::mem::size_of_val(words.as_slice()),
libc::VM_FLAGS_ANYWHERE,
)
};
if kr != libc::KERN_SUCCESS {
return ok(kr.into());
}
unsafe {
std::ptr::copy_nonoverlapping(words.as_ptr(), address as *mut c_int, words.len());
*count = cpus as u32;
*info = address as *mut c_int;
*info_count = words.len() as u32;
}
ok(libc::KERN_SUCCESS.into())
}
#[cfg(target_os = "macos")]
unsafe fn thread_policy_set(
&self,
_thread: u32,
_flavor: c_int,
_info: *const u8,
_count: u32,
) -> Option<HostResult> {
ok(0)
}
#[cfg(target_os = "macos")]
fn set_qos(&self, qos_class: c_int, relative_priority: c_int) -> Option<HostResult> {
const QOS_CLASSES: [c_int; 5] = [0x21, 0x19, 0x15, 0x11, 0x09];
const QOS_MIN_RELATIVE_PRIORITY: c_int = -15;
if !QOS_CLASSES.contains(&qos_class)
|| !(QOS_MIN_RELATIVE_PRIORITY..=0).contains(&relative_priority)
{
return err(libc::EINVAL);
}
let me = mac_thread_id(unsafe { libc::pthread_self() } as u64);
if self.state.lock().unwrap().mac_threads.contains_key(&me) {
return err(libc::EPERM);
}
ok(0)
}
#[cfg(target_os = "macos")]
unsafe fn sysctl(
&self,
name: *const c_int,
namelen: u32,
oldp: *mut u8,
oldlenp: *mut usize,
newp: *const u8,
newlen: usize,
) -> Option<HostResult> {
if name.is_null() {
return None;
}
let mib = unsafe { std::slice::from_raw_parts(name, namelen as usize) };
use crate::netstats::macos;
let shared = self.state.lock().unwrap().shared.upgrade();
if let Some(shared) = shared
&& let Some(r) = unsafe { macos::read(&shared, mib, oldp, oldlenp, newp) }
{
return Some(r);
}
if let Some(name) = hw_name_of(mib) {
return match hw_node(&self.state.lock().unwrap(), name)? {
Ok(value) => unsafe { read_hw(&value, oldp, oldlenp, newp) },
Err(errno) => err(errno),
};
}
if let Some(name) = kern_name_of(mib) {
return unsafe { self.kern_node(name, oldp, oldlenp, newp, newlen) };
}
if namelen < 5 {
return None;
}
if mib[0] != CTL_NET || mib[1] != PF_ROUTE || mib[4] != NET_RT_IFLIST2 {
return None;
}
if oldlenp.is_null() {
return err(libc::EINVAL);
}
let nics = self.state.lock().unwrap().topo_nics();
let blob = build_iflist2(&nics);
if oldp.is_null() {
unsafe { *oldlenp = blob.len() };
return ok(0);
}
if unsafe { *oldlenp } < blob.len() {
return err(libc::ENOMEM);
}
unsafe {
std::ptr::copy_nonoverlapping(blob.as_ptr(), oldp, blob.len());
*oldlenp = blob.len();
}
ok(0)
}
#[cfg(target_os = "macos")]
unsafe fn sysctlbyname(
&self,
name: *const c_char,
oldp: *mut u8,
oldlenp: *mut usize,
newp: *const u8,
newlen: usize,
) -> Option<HostResult> {
use crate::netstats::macos;
let name = unsafe { macos::name_str(name) }?;
if let Some(node) = hw_node(&self.state.lock().unwrap(), name) {
return match node {
Ok(value) => unsafe { read_hw(&value, oldp, oldlenp, newp) },
Err(errno) => err(errno),
};
}
if let Some(r) = unsafe { self.kern_node(name, oldp, oldlenp, newp, newlen) } {
return Some(r);
}
let mib = macos::mib_of(name)?;
let shared = self.state.lock().unwrap().shared.upgrade()?;
unsafe { macos::read(&shared, &mib, oldp, oldlenp, newp) }
}
#[cfg(target_os = "macos")]
unsafe fn sysctlnametomib(
&self,
name: *const c_char,
mib: *mut c_int,
sizep: *mut usize,
) -> Option<HostResult> {
let name = unsafe { crate::netstats::macos::name_str(name) }?;
if let Some(Err(errno)) = hw_node(&self.state.lock().unwrap(), name) {
return err(errno);
}
unsafe { crate::netstats::macos::name_to_mib(name, mib, sizep) }
}
#[cfg(target_os = "linux")]
unsafe fn syscall(&self, number: i64, args: [i64; 6]) -> Option<HostResult> {
match number {
n if n == libc::SYS_sched_setscheduler => unsafe {
self.sched_setscheduler(args[0] as i32, args[1] as c_int, args[2] as *const u8)
},
n if n == libc::SYS_uname => unsafe { self.uname(args[0] as *mut u8) },
n if n == libc::SYS_sethostname => unsafe {
self.sethostname(args[0] as *const u8, i64::from(args[1] as c_int))
},
n if n == libc::SYS_sched_getscheduler => unsafe {
self.sched_getscheduler(args[0] as i32)
},
n if n == libc::SYS_sched_setparam => unsafe {
self.sched_setparam(args[0] as i32, args[1] as *const u8)
},
n if n == libc::SYS_sched_getparam => unsafe {
self.sched_getparam(args[0] as i32, args[1] as *mut u8)
},
n if n == libc::SYS_sched_setaffinity => unsafe {
self.sched_setaffinity(args[0] as i32, args[1] as usize, args[2] as *const u8)
},
n if n == libc::SYS_sched_getaffinity => unsafe {
self.sched_getaffinity(args[0] as i32, args[1] as usize, args[2] as *mut u8)
},
n if n == libc::SYS_setpriority => unsafe {
self.setpriority(args[0] as c_int, args[1] as u32, args[2] as c_int)
},
n if n == libc::SYS_getpriority => {
if args[0] as c_int != PRIO_PROCESS {
return None;
}
let mut state = self.state.lock().unwrap();
let tid = state.resolve(args[1] as i32);
ok((20 - state.threads[&tid].nice) as i64)
}
_ => unsafe { crate::proclimits::route_syscall(self, number, args) },
}
}
}
#[cfg(target_os = "linux")]
const SIOCETHTOOL: u64 = 0x8946;
#[cfg(target_os = "linux")]
const SIOCSHWTSTAMP: u64 = 0x89b0;
#[cfg(target_os = "linux")]
const SIOCGHWTSTAMP: u64 = 0x89b1;
#[cfg(target_os = "linux")]
unsafe fn read_ifname(arg: i64) -> Option<String> {
let p = arg as *const u8;
if p.is_null() {
return None;
}
let mut name = Vec::new();
for i in 0..16 {
let b = unsafe { *p.add(i) };
if b == 0 {
break;
}
name.push(b);
}
String::from_utf8(name).ok()
}
fn put_c_field(field: &mut [c_char], text: impl AsRef<[u8]>) {
let text = text.as_ref();
field.fill(0);
let n = text.len().min(field.len() - 1);
for (dst, &b) in field.iter_mut().zip(&text[..n]) {
*dst = b as c_char;
}
}
#[cfg(target_os = "macos")]
fn mac_thread_id(thread: u64) -> u64 {
let mut id = 0;
if unsafe { libc::pthread_threadid_np(thread as libc::pthread_t, &mut id) } == 0 {
id
} else {
thread
}
}
#[cfg(target_os = "linux")]
fn host_modelled(level: c_int, name: c_int) -> bool {
matches!(
(level, name),
(libc::SOL_SOCKET, libc::SO_BROADCAST)
| (libc::SOL_SOCKET, libc::SO_RCVTIMEO)
| (libc::SOL_SOCKET, libc::SO_SNDTIMEO)
| (libc::SOL_SOCKET, libc::SO_ERROR)
| (libc::IPPROTO_IP, libc::IP_RECVERR)
| (libc::IPPROTO_IPV6, libc::IPV6_RECVERR)
| (libc::IPPROTO_IP, libc::IP_DROP_MEMBERSHIP)
| (libc::IPPROTO_IPV6, libc::IPV6_DROP_MEMBERSHIP)
)
}
#[cfg(target_os = "linux")]
fn udp_parse_group(level: c_int, name: c_int, bytes: &[u8]) -> Option<std::net::IpAddr> {
if level == libc::IPPROTO_IP && name == libc::IP_ADD_MEMBERSHIP && bytes.len() >= 4 {
return Some(std::net::IpAddr::V4(std::net::Ipv4Addr::new(
bytes[0], bytes[1], bytes[2], bytes[3],
)));
}
if level == libc::IPPROTO_IPV6 && name == libc::IPV6_ADD_MEMBERSHIP && bytes.len() >= 16 {
let mut octets = [0u8; 16];
octets.copy_from_slice(&bytes[..16]);
return Some(std::net::IpAddr::V6(std::net::Ipv6Addr::from(octets)));
}
None
}
#[cfg(target_os = "linux")]
fn udp_membership(group: std::net::IpAddr, bytes: &[u8]) -> Membership {
let word = |at: usize| {
bytes
.get(at..at + 4)
.map_or(0, |b| u32::from_ne_bytes([b[0], b[1], b[2], b[3]]))
};
match group {
std::net::IpAddr::V4(_) => {
let interface = bytes
.get(4..8)
.map(|b| std::net::Ipv4Addr::new(b[0], b[1], b[2], b[3]))
.filter(|a| !a.is_unspecified());
Membership {
group,
interface_addr: interface.map(std::net::IpAddr::V4),
ifindex: word(8),
}
}
std::net::IpAddr::V6(_) => Membership {
group,
interface_addr: None,
ifindex: word(16),
},
}
}
#[cfg(target_os = "linux")]
unsafe fn write_sockaddr(addr: std::net::SocketAddr, buf: *mut u8, addrlen: *mut u32) {
if buf.is_null() || addrlen.is_null() {
return;
}
let cap = unsafe { *addrlen } as usize;
let bytes = sockaddr_bytes(addr);
let n = bytes.len().min(cap);
unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), buf, n);
*addrlen = bytes.len() as u32;
}
}
#[cfg(target_os = "linux")]
fn sockaddr_bytes(addr: std::net::SocketAddr) -> Vec<u8> {
use std::net::SocketAddr;
let mut bytes = Vec::new();
match addr {
SocketAddr::V4(v4) => {
bytes.extend_from_slice(&(libc::AF_INET as u16).to_ne_bytes());
bytes.extend_from_slice(&v4.port().to_be_bytes());
bytes.extend_from_slice(&v4.ip().octets());
bytes.resize(16, 0);
}
SocketAddr::V6(v6) => {
bytes.extend_from_slice(&(libc::AF_INET6 as u16).to_ne_bytes());
bytes.extend_from_slice(&v6.port().to_be_bytes());
bytes.extend_from_slice(&0u32.to_ne_bytes());
bytes.extend_from_slice(&v6.ip().octets());
bytes.extend_from_slice(&0u32.to_ne_bytes());
}
}
bytes
}
#[cfg(target_os = "linux")]
unsafe fn addr_write_from_msg(hdr: *mut libc::msghdr, src: std::net::SocketAddr) -> bool {
let name = unsafe { (*hdr).msg_name } as *mut u8;
if name.is_null() {
return false;
}
let mut caplen = unsafe { (*hdr).msg_namelen };
unsafe {
write_sockaddr(src, name, &mut caplen);
(*hdr).msg_namelen = caplen;
}
true
}
#[cfg(target_os = "linux")]
const HWTSTAMP_FLAG_MASK: c_int = 1;
#[cfg(target_os = "linux")]
const HWTSTAMP_TX_CNT: c_int = 4;
const HWTSTAMP_FILTER_CNT: c_int = 16;
#[cfg(target_os = "linux")]
fn hwtstamp_widenings(filter: c_int) -> Vec<c_int> {
let wider: &[c_int] = match filter {
0 | 1 => &[],
3 => &[1],
4 | 5 => &[3, 1],
6 => &[12, 1],
7 | 8 => &[6, 12, 1],
9 => &[12, 1],
10 | 11 => &[9, 12, 1],
12 => &[1],
13 | 14 => &[12, 1],
_ => &[1],
};
std::iter::once(filter)
.chain(wider.iter().copied())
.collect()
}
#[cfg(target_os = "linux")]
fn apply_hwtstamp(
cfg: [c_int; 3],
hardware: bool,
tx_types: u32,
rx_filters: u32,
mappings: &[Option<Result<c_int, c_int>>; 16],
) -> Result<[c_int; 3], c_int> {
let [flags, tx, rx] = cfg;
if flags & !HWTSTAMP_FLAG_MASK != 0 {
return Err(libc::EINVAL);
}
if !(0..HWTSTAMP_TX_CNT).contains(&tx) || !(0..HWTSTAMP_FILTER_CNT).contains(&rx) {
return Err(libc::ERANGE);
}
if !hardware {
return Err(libc::EOPNOTSUPP);
}
if tx_types & (1 << tx) == 0 {
return Err(libc::ERANGE);
}
let applied = match mappings[rx as usize] {
Some(outcome) => outcome?,
None => hwtstamp_widenings(rx)
.into_iter()
.find(|&f| f == 0 || rx_filters & (1 << f) != 0)
.ok_or(libc::ERANGE)?,
};
Ok([flags, tx, applied])
}
#[cfg(target_os = "linux")]
fn parse_ptp_index(path: &Path) -> Option<u32> {
path.to_str()?.strip_prefix("/dev/ptp")?.parse().ok()
}
#[cfg(target_os = "linux")]
const PTP_IOCTL_MAGIC: u64 = b'=' as u64;
#[cfg(target_os = "linux")]
const PTP_MAX_SAMPLES: usize = 25;
#[cfg(target_os = "linux")]
const PTP_MAX_CHANNELS: u32 = 2048;
#[cfg(target_os = "linux")]
const fn ptp_ioc(dir: u64, nr: u64, size: u64) -> u64 {
(dir << 30) | (size << 16) | (PTP_IOCTL_MAGIC << 8) | nr
}
#[cfg(target_os = "linux")]
mod ptp_req {
use super::ptp_ioc;
pub(super) const GETCAPS: [u64; 2] = [ptp_ioc(2, 1, 80), ptp_ioc(2, 10, 80)];
pub(super) const EXTTS: [u64; 2] = [ptp_ioc(1, 2, 16), ptp_ioc(1, 11, 16)];
pub(super) const PEROUT: [u64; 2] = [ptp_ioc(1, 3, 56), ptp_ioc(1, 12, 56)];
pub(super) const ENABLE_PPS: [u64; 2] = [ptp_ioc(1, 4, 4), ptp_ioc(1, 13, 4)];
pub(super) const SYS_OFFSET: [u64; 2] = [ptp_ioc(1, 5, 832), ptp_ioc(1, 14, 832)];
pub(super) const PIN_GETFUNC: [u64; 2] = [ptp_ioc(3, 6, 96), ptp_ioc(3, 15, 96)];
pub(super) const PIN_SETFUNC: [u64; 2] = [ptp_ioc(1, 7, 96), ptp_ioc(1, 16, 96)];
pub(super) const PRECISE: [u64; 2] = [ptp_ioc(3, 8, 64), ptp_ioc(3, 17, 64)];
pub(super) const EXTENDED: [u64; 2] = [ptp_ioc(3, 9, 1216), ptp_ioc(3, 18, 1216)];
pub(super) const MASK_CLEAR_ALL: u64 = ptp_ioc(0, 19, 0);
pub(super) const MASK_EN_SINGLE: u64 = ptp_ioc(1, 20, 4);
pub(super) const PRECISE_CYCLES: u64 = ptp_ioc(3, 21, 64);
pub(super) const EXTENDED_CYCLES: u64 = ptp_ioc(3, 22, 1216);
}
#[cfg(target_os = "linux")]
struct PtpFile {
caps: PtpCaps,
writable: bool,
root: bool,
}
#[cfg(target_os = "linux")]
unsafe fn write_ptp_clock_time(base: *mut u8, offset: usize, sec: i64, nsec: u32) {
unsafe {
(base.add(offset) as *mut i64).write_unaligned(sec);
(base.add(offset + 8) as *mut u32).write_unaligned(nsec);
(base.add(offset + 12) as *mut u32).write_unaligned(0);
}
}
#[cfg(target_os = "linux")]
unsafe fn ptp_ioctl(
clock: &PtpFile,
request: u64,
arg: i64,
sample: impl FnOnce() -> [(i64, u32); 3],
) -> Option<FsResult> {
use ptp_req::*;
let base = arg as *mut u8;
let word = |at: usize| unsafe { (base.add(at) as *const u32).read_unaligned() };
let caps = clock.caps;
let changes = EXTTS.contains(&request)
|| PEROUT.contains(&request)
|| PIN_SETFUNC.contains(&request)
|| ENABLE_PPS.contains(&request);
if changes && !clock.writable {
return err(libc::EACCES);
}
if ENABLE_PPS.contains(&request) {
if !clock.root {
return err(libc::EPERM);
}
return if caps.pps {
ok(0)
} else {
err(libc::EOPNOTSUPP)
};
}
if request == MASK_CLEAR_ALL {
return ok(0);
}
if request == PRECISE_CYCLES || request == EXTENDED_CYCLES {
return err(libc::EOPNOTSUPP);
}
let known = GETCAPS.contains(&request)
|| EXTTS.contains(&request)
|| PEROUT.contains(&request)
|| SYS_OFFSET.contains(&request)
|| PIN_GETFUNC.contains(&request)
|| PIN_SETFUNC.contains(&request)
|| PRECISE.contains(&request)
|| EXTENDED.contains(&request)
|| request == MASK_EN_SINGLE;
if !known {
return err(libc::ENOTTY);
}
if PRECISE.contains(&request) && !caps.cross_timestamping
|| EXTENDED.contains(&request) && !caps.extended
{
return err(libc::EOPNOTSUPP);
}
if base.is_null() {
return err(libc::EFAULT);
}
let within = |index: u32, count: i32| i64::from(index) < i64::from(count);
if GETCAPS.contains(&request) {
let words = [
caps.max_adj,
caps.n_alarm,
caps.n_ext_ts,
caps.n_per_out,
i32::from(caps.pps),
caps.n_pins,
i32::from(caps.cross_timestamping),
i32::from(caps.adjust_phase),
if caps.adjust_phase {
caps.max_phase_adj
} else {
0
},
];
unsafe {
std::ptr::write_bytes(base, 0, 80);
for (i, w) in words.iter().enumerate() {
(base.add(4 * i) as *mut i32).write_unaligned(*w);
}
}
return ok(0);
}
if EXTTS.contains(&request) {
let (index, flags) = (word(0), word(4));
if request == EXTTS[1]
&& (flags & !0x1f != 0
|| word(8) != 0
|| word(12) != 0
|| flags & 1 != 0 && flags & 0b110 == 0)
{
return err(libc::EINVAL);
}
return if within(index, caps.n_ext_ts) {
ok(0)
} else {
err(libc::EINVAL)
};
}
if PEROUT.contains(&request) {
let (index, flags) = (word(32), word(36));
if request == PEROUT[1]
&& (flags & !0b111 != 0
|| flags & 0b10 == 0 && (40..56).step_by(4).any(|at| word(at) != 0))
{
return err(libc::EINVAL);
}
return if within(index, caps.n_per_out) {
ok(0)
} else {
err(libc::EINVAL)
};
}
if PIN_GETFUNC.contains(&request) || PIN_SETFUNC.contains(&request) {
let index = word(64);
let v2 = request == PIN_GETFUNC[1] || request == PIN_SETFUNC[1];
if v2 && (76..96).step_by(4).any(|at| word(at) != 0) || !within(index, caps.n_pins) {
return err(libc::EINVAL);
}
if PIN_GETFUNC.contains(&request) {
unsafe {
std::ptr::write_bytes(base, 0, 96);
(base.add(64) as *mut u32).write_unaligned(index);
}
}
return ok(0);
}
if request == MASK_EN_SINGLE {
return if word(0) > PTP_MAX_CHANNELS {
err(libc::EFAULT)
} else {
ok(0)
};
}
let [device, realtime, monoraw] = sample();
if PRECISE.contains(&request) {
unsafe {
std::ptr::write_bytes(base, 0, 64);
write_ptp_clock_time(base, 0, device.0, device.1);
write_ptp_clock_time(base, 16, realtime.0, realtime.1);
write_ptp_clock_time(base, 32, monoraw.0, monoraw.1);
}
return ok(0);
}
if SYS_OFFSET.contains(&request) {
let n = word(0) as usize;
if n > PTP_MAX_SAMPLES {
return err(libc::EINVAL);
}
for i in 0..n {
let sys = 16 + i * 32;
unsafe {
write_ptp_clock_time(base, sys, realtime.0, realtime.1);
write_ptp_clock_time(base, sys + 16, device.0, device.1);
}
}
unsafe { write_ptp_clock_time(base, 16 + n * 32, realtime.0, realtime.1) };
return ok(0);
}
let (n, clockid) = (word(0) as usize, word(4) as c_int);
if n > PTP_MAX_SAMPLES || word(8) != 0 || word(12) != 0 {
return err(libc::EINVAL);
}
let system = match clockid {
libc::CLOCK_REALTIME => realtime,
libc::CLOCK_MONOTONIC | libc::CLOCK_MONOTONIC_RAW => monoraw,
_ => return err(libc::EINVAL),
};
for i in 0..n {
let at = 16 + i * 48;
unsafe {
write_ptp_clock_time(base, at, system.0, system.1);
write_ptp_clock_time(base, at + 16, device.0, device.1);
write_ptp_clock_time(base, at + 32, system.0, system.1);
}
}
ok(0)
}
#[cfg(target_os = "linux")]
fn push_rtattr(out: &mut Vec<u8>, ty: u16, payload: &[u8], nul_terminate: bool) {
let mut data = payload.to_vec();
if nul_terminate {
data.push(0);
}
let len = (4 + data.len()) as u16;
out.extend_from_slice(&len.to_ne_bytes());
out.extend_from_slice(&ty.to_ne_bytes());
out.extend_from_slice(&data);
while !out.len().is_multiple_of(4) {
out.push(0);
}
}
#[cfg(target_os = "linux")]
const NLM_F_MULTI: u16 = 2;
#[cfg(target_os = "linux")]
fn nl_done(out: &mut Vec<u8>, seq: u32) {
const NLMSG_DONE: u16 = 3;
out.extend_from_slice(&nl_message(
NLMSG_DONE,
NLM_F_MULTI,
seq,
&0i32.to_ne_bytes(),
));
}
#[cfg(target_os = "linux")]
fn oper_code(state: &str) -> u8 {
match state {
"notpresent" => 1,
"down" => 2,
"lowerlayerdown" => 3,
"testing" => 4,
"dormant" => 5,
"up" => 6,
_ => 0,
}
}
#[cfg(target_os = "linux")]
fn build_getlink_dump(nics: &[(&NicSnapshot, String)], seq: u32) -> Vec<u8> {
let mut out = Vec::new();
for (nic, operstate) in nics {
out.extend_from_slice(&getlink_message(nic, operstate, NLM_F_MULTI, seq));
}
nl_done(&mut out, seq);
out
}
#[cfg(target_os = "linux")]
fn getlink_message(nic: &NicSnapshot, operstate: &str, flags: u16, seq: u32) -> Vec<u8> {
const RTM_NEWLINK: u16 = 16;
const IFLA_ADDRESS: u16 = 1;
const IFLA_BROADCAST: u16 = 2;
const IFLA_IFNAME: u16 = 3;
const IFLA_MTU: u16 = 4;
const IFLA_OPERSTATE: u16 = 16;
const IFLA_STATS64: u16 = 23;
const IFLA_CARRIER: u16 = 33;
const ARPHRD_ETHER: u16 = 1;
const ARPHRD_LOOPBACK: u16 = 772;
let mut body = Vec::new();
body.push(0); body.push(0);
let hatype = if nic.loopback {
ARPHRD_LOOPBACK
} else {
ARPHRD_ETHER
};
body.extend_from_slice(&hatype.to_ne_bytes());
body.extend_from_slice(&(nic.index as i32).to_ne_bytes());
body.extend_from_slice(&crate::ifaddrs::flags(nic).to_ne_bytes()); body.extend_from_slice(&0u32.to_ne_bytes()); push_rtattr(&mut body, IFLA_IFNAME, nic.spec.name.as_bytes(), true);
push_rtattr(&mut body, IFLA_MTU, &nic.spec.mtu.to_ne_bytes(), false);
push_rtattr(&mut body, IFLA_OPERSTATE, &[oper_code(operstate)], false);
push_rtattr(
&mut body,
IFLA_CARRIER,
&[u8::from(nic.spec.carrier)],
false,
);
push_rtattr(&mut body, IFLA_ADDRESS, &nic.hw_addr(), false);
let broadcast = if nic.loopback { [0; 6] } else { [0xff; 6] };
push_rtattr(&mut body, IFLA_BROADCAST, &broadcast, false);
let c = &nic.counters;
let mut stats = [0u64; 24];
for (i, v) in [
(0, c.rx_packets),
(1, c.tx_packets),
(2, c.rx_bytes),
(3, c.tx_bytes),
(4, c.rx_errors),
(5, c.tx_errors),
(6, c.rx_dropped),
(7, c.tx_dropped),
(8, c.multicast),
(17, c.tx_carrier_errors),
(23, c.rx_nohandler),
] {
stats[i] = v;
}
let stats: Vec<u8> = stats.iter().flat_map(|v| v.to_ne_bytes()).collect();
push_rtattr(&mut body, IFLA_STATS64, &stats, false);
nl_message(RTM_NEWLINK, flags, seq, &body)
}
#[cfg(target_os = "linux")]
fn addr_scope(ip: std::net::IpAddr) -> u8 {
const RT_SCOPE_UNIVERSE: u8 = 0;
const RT_SCOPE_LINK: u8 = 253;
const RT_SCOPE_HOST: u8 = 254;
match ip {
ip if ip.is_loopback() => RT_SCOPE_HOST,
std::net::IpAddr::V6(v6) if v6.segments()[0] & 0xffc0 == 0xfe80 => RT_SCOPE_LINK,
_ => RT_SCOPE_UNIVERSE,
}
}
#[cfg(target_os = "linux")]
fn ip_bytes(ip: std::net::IpAddr) -> Vec<u8> {
match ip {
std::net::IpAddr::V4(v4) => v4.octets().to_vec(),
std::net::IpAddr::V6(v6) => v6.octets().to_vec(),
}
}
#[cfg(target_os = "linux")]
fn family_of(ip: std::net::IpAddr) -> u8 {
if ip.is_ipv4() {
libc::AF_INET as u8
} else {
libc::AF_INET6 as u8
}
}
#[cfg(target_os = "linux")]
fn build_getaddr_dump(nics: &[NicSnapshot], family: u8, seq: u32) -> Vec<u8> {
const RTM_NEWADDR: u16 = 20;
const IFA_ADDRESS: u16 = 1;
const IFA_LOCAL: u16 = 2;
const IFA_LABEL: u16 = 3;
const IFA_BROADCAST: u16 = 4;
const IFA_F_PERMANENT: u8 = 0x80;
let mut out = Vec::new();
for v4 in [true, false] {
let fam = if v4 { libc::AF_INET } else { libc::AF_INET6 } as u8;
if family != 0 && family != fam {
continue;
}
for nic in nics {
for net in nic.spec.addresses.iter().filter(|n| n.addr.is_ipv4() == v4) {
let mut body = vec![fam, net.prefix, IFA_F_PERMANENT, addr_scope(net.addr)];
body.extend_from_slice(&nic.index.to_ne_bytes());
push_rtattr(&mut body, IFA_ADDRESS, &ip_bytes(net.addr), false);
if v4 {
push_rtattr(&mut body, IFA_LOCAL, &ip_bytes(net.addr), false);
if let Some(b) = net.broadcast().filter(|_| !nic.loopback) {
push_rtattr(&mut body, IFA_BROADCAST, &ip_bytes(b), false);
}
push_rtattr(&mut body, IFA_LABEL, nic.spec.name.as_bytes(), true);
}
out.extend_from_slice(&nl_message(RTM_NEWADDR, NLM_F_MULTI, seq, &body));
}
}
}
nl_done(&mut out, seq);
out
}
#[cfg(target_os = "linux")]
fn build_getroute_dump(routes: &[(Route, u32)], family: u8, seq: u32) -> Vec<u8> {
const RTM_NEWROUTE: u16 = 24;
const RT_TABLE_MAIN: u8 = 254;
const RTPROT_KERNEL: u8 = 2;
const RTPROT_BOOT: u8 = 3;
const RT_SCOPE_UNIVERSE: u8 = 0;
const RT_SCOPE_LINK: u8 = 253;
const RTN_UNICAST: u8 = 1;
const RTA_DST: u16 = 1;
const RTA_OIF: u16 = 4;
const RTA_GATEWAY: u16 = 5;
const RTA_PRIORITY: u16 = 6;
const RTA_PREFSRC: u16 = 7;
const RTA_TABLE: u16 = 15;
let mut out = Vec::new();
for (route, index) in routes {
let fam = family_of(route.dest.addr);
if family != 0 && family != fam {
continue;
}
let connected = route.gateway.is_none() && route.metric == 0 && route.dest.prefix > 0;
let (protocol, scope) = if connected {
(RTPROT_KERNEL, RT_SCOPE_LINK)
} else {
(RTPROT_BOOT, RT_SCOPE_UNIVERSE)
};
let mut body = vec![
fam,
route.dest.prefix,
0,
0,
RT_TABLE_MAIN,
protocol,
scope,
RTN_UNICAST,
];
body.extend_from_slice(&0u32.to_ne_bytes()); push_rtattr(
&mut body,
RTA_TABLE,
&(RT_TABLE_MAIN as u32).to_ne_bytes(),
false,
);
if route.dest.prefix > 0 {
push_rtattr(&mut body, RTA_DST, &ip_bytes(route.dest.network()), false);
}
if let Some(gw) = route.gateway {
push_rtattr(&mut body, RTA_GATEWAY, &ip_bytes(gw), false);
}
if let Some(src) = route.src {
push_rtattr(&mut body, RTA_PREFSRC, &ip_bytes(src), false);
}
if route.metric > 0 {
push_rtattr(&mut body, RTA_PRIORITY, &route.metric.to_ne_bytes(), false);
}
push_rtattr(&mut body, RTA_OIF, &index.to_ne_bytes(), false);
out.extend_from_slice(&nl_message(RTM_NEWROUTE, NLM_F_MULTI, seq, &body));
}
nl_done(&mut out, seq);
out
}
#[cfg(target_os = "linux")]
fn nl_message(ty: u16, flags: u16, seq: u32, body: &[u8]) -> Vec<u8> {
let len = (16 + body.len()) as u32;
let mut out = Vec::with_capacity(16 + body.len());
out.extend_from_slice(&len.to_ne_bytes());
out.extend_from_slice(&ty.to_ne_bytes());
out.extend_from_slice(&flags.to_ne_bytes());
out.extend_from_slice(&seq.to_ne_bytes());
out.extend_from_slice(&0u32.to_ne_bytes());
out.extend_from_slice(body);
while !out.len().is_multiple_of(4) {
out.push(0);
}
out
}
#[cfg(target_os = "linux")]
fn find_nlattr(attrs: &[u8], want: u16) -> Option<&[u8]> {
let mut pos = 0;
while pos + 4 <= attrs.len() {
let len = u16::from_ne_bytes([attrs[pos], attrs[pos + 1]]) as usize;
let ty = u16::from_ne_bytes([attrs[pos + 2], attrs[pos + 3]]);
if len < 4 || pos + len > attrs.len() {
break;
}
if ty == want {
return Some(&attrs[pos + 4..pos + len]);
}
pos += len.next_multiple_of(4);
}
None
}
#[cfg(target_os = "linux")]
fn build_genl_ctrl_reply(req: &[u8], seq: u32) -> Vec<u8> {
const CTRL_CMD_NEWFAMILY: u8 = 1;
const CTRL_CMD_GETFAMILY: u8 = 3;
const CTRL_ATTR_FAMILY_ID: u16 = 1;
const CTRL_ATTR_FAMILY_NAME: u16 = 2;
const GENL_ID_CTRL: u16 = 16;
const NLMSG_ERROR: u16 = 2;
const NETDEV_FAMILY_ID: u16 = 24;
let cmd = req.get(16).copied().unwrap_or(0);
let name = req
.get(20..)
.and_then(|attrs| find_nlattr(attrs, CTRL_ATTR_FAMILY_NAME))
.map(|p| {
let end = p.iter().position(|&b| b == 0).unwrap_or(p.len());
&p[..end]
});
if cmd != CTRL_CMD_GETFAMILY || name != Some(b"netdev".as_slice()) {
let mut body = Vec::new();
body.extend_from_slice(&(-libc::ENODEV).to_ne_bytes());
body.extend_from_slice(&req[..req.len().min(16)]);
return nl_message(NLMSG_ERROR, 0, seq, &body);
}
let mut body = vec![CTRL_CMD_NEWFAMILY, 2, 0, 0]; push_rtattr(
&mut body,
CTRL_ATTR_FAMILY_ID,
&NETDEV_FAMILY_ID.to_ne_bytes(),
false,
);
push_rtattr(&mut body, CTRL_ATTR_FAMILY_NAME, b"netdev", true);
nl_message(GENL_ID_CTRL, 0, seq, &body)
}
#[cfg(target_os = "linux")]
fn build_getqdisc_dump(nics: &[&NicState], seq: u32) -> Vec<u8> {
const RTM_NEWQDISC: u16 = 36;
const NLMSG_DONE: u16 = 3;
const NLM_F_MULTI: u16 = 2;
const TCA_KIND: u16 = 1;
const TCA_OPTIONS: u16 = 2;
const TCA_ETF_PARMS: u16 = 1;
let mut out = Vec::new();
for nic in nics {
for (handle, parent, q) in nic.qdiscs.listing(nic.tx_queues) {
let mut body = vec![0u8, 0, 0, 0];
body.extend_from_slice(&(nic.ifindex as i32).to_ne_bytes());
body.extend_from_slice(&handle.to_ne_bytes());
body.extend_from_slice(&parent.to_ne_bytes());
body.extend_from_slice(&0u32.to_ne_bytes());
push_rtattr(&mut body, TCA_KIND, q.kind.as_bytes(), true);
if let Some((delta, clockid, flags)) = q.etf {
let mut parms = Vec::new();
parms.extend_from_slice(&delta.to_ne_bytes());
parms.extend_from_slice(&clockid.to_ne_bytes());
parms.extend_from_slice(&flags.to_ne_bytes());
let mut opts = Vec::new();
push_rtattr(&mut opts, TCA_ETF_PARMS, &parms, false);
push_rtattr(&mut body, TCA_OPTIONS, &opts, false);
}
out.extend_from_slice(&nl_message(RTM_NEWQDISC, NLM_F_MULTI, seq, &body));
}
}
out.extend_from_slice(&nl_message(
NLMSG_DONE,
NLM_F_MULTI,
seq,
&0i32.to_ne_bytes(),
));
out
}
impl Net for SimHost {
#[cfg(target_os = "linux")]
unsafe fn ioctl(&self, fd: c_int, request: u64, arg: i64) -> Option<NetResult> {
if request == libc::FIONREAD || request == libc::TIOCOUTQ {
let rec = self.state.lock().unwrap().udp.get(&fd)?.rec.clone();
if arg == 0 {
return err(libc::EFAULT);
}
let value = if request == libc::FIONREAD {
rec.fionread()
} else {
0
};
unsafe { (arg as *mut c_int).write_unaligned(value) };
return ok(0);
}
if request == libc::FIONBIO {
let mut state = self.state.lock().unwrap();
let sock = state.udp.get_mut(&fd)?;
if arg == 0 {
return err(libc::EFAULT);
}
sock.nonblocking = unsafe { (arg as *const c_int).read_unaligned() } != 0;
return ok(0);
}
if !matches!(request, SIOCETHTOOL | SIOCSHWTSTAMP | SIOCGHWTSTAMP) {
return None;
}
let name = unsafe { read_ifname(arg) }?;
let base = arg as *mut u8;
let mut state = self.state.lock().unwrap();
let has_net_admin = state.has_cap(CAP_NET_ADMIN);
let link_up = state
.topo_nic(&name)
.is_some_and(|n| n.spec.admin_up && n.spec.carrier);
let nic = state.nics.get_mut(&name)?;
match request {
SIOCETHTOOL => {
let data = unsafe { (base.add(16) as *const *mut u8).read_unaligned() };
if data.is_null() {
return err(libc::EFAULT);
}
let ctx = crate::ethtool::Ctx {
net_admin: has_net_admin,
link_up,
hw_timestamping: nic.hwtstamp_supported,
driver: &nic.driver,
version: &nic.driver_version,
bus_info: &nic.bus_info,
queues: (&mut nic.rx_queues, &mut nic.tx_queues),
};
match unsafe { nic.ethtool.ioctl(ctx, data) } {
Ok(()) => ok(0),
Err(e) => err(e),
}
}
SIOCSHWTSTAMP => {
if !has_net_admin {
return err(libc::EPERM);
}
let data = unsafe { (base.add(16) as *const *mut c_int).read_unaligned() };
if data.is_null() {
return err(libc::EFAULT);
}
let cfg: [c_int; 3] =
std::array::from_fn(|i| unsafe { data.add(i).read_unaligned() });
let (_, tx_types, rx_filters) = nic.ethtool.timestamping(nic.hwtstamp_supported);
match apply_hwtstamp(
cfg,
nic.hwtstamp_supported,
tx_types,
rx_filters,
&nic.hwtstamp_rx_mapping,
) {
Ok(applied) => {
nic.hwtstamp = applied;
for (i, v) in applied.iter().enumerate() {
unsafe { data.add(i).write_unaligned(*v) };
}
ok(0)
}
Err(e) => err(e),
}
}
SIOCGHWTSTAMP => {
if !nic.hwtstamp_supported {
return err(libc::EOPNOTSUPP);
}
let data = unsafe { (base.add(16) as *const *mut c_int).read_unaligned() };
if data.is_null() {
return err(libc::EFAULT);
}
for (i, v) in nic.hwtstamp.iter().enumerate() {
unsafe { data.add(i).write_unaligned(*v) };
}
ok(0)
}
_ => None,
}
}
#[cfg(target_os = "linux")]
fn owns(&self, fd: c_int) -> bool {
let state = self.state.lock().unwrap();
state.udp.contains_key(&fd) || state.netlinks.contains_key(&fd)
}
#[cfg(target_os = "linux")]
unsafe fn read(&self, fd: c_int, buf: *mut u8, len: usize) -> Option<NetResult> {
if !Net::owns(self, fd) {
return None;
}
unsafe { Net::recv(self, fd, buf, len, 0) }
}
#[cfg(target_os = "linux")]
unsafe fn write(&self, fd: c_int, buf: *const u8, len: usize) -> Option<NetResult> {
if !Net::owns(self, fd) {
return None;
}
unsafe { Net::send(self, fd, buf, len, 0) }
}
#[cfg(target_os = "linux")]
unsafe fn socket(&self, domain: c_int, ty: c_int, protocol: c_int) -> Option<NetResult> {
if domain == libc::AF_NETLINK {
let base = ty & 0xf;
if base != libc::SOCK_RAW && base != libc::SOCK_DGRAM {
return err(libc::ESOCKTNOSUPPORT);
}
let (fd, rec) = match self.open_socket(SocketKind::Netlink) {
Ok(opened) => opened,
Err(errno) => return err(errno),
};
rec.set_family(domain, protocol);
rec.set_type(base);
let probe = Arc::new(NetlinkRx {
host: self.me.get().cloned().unwrap_or_default(),
id: rec.id.get(),
});
rec.set_probe(Arc::downgrade(&probe) as std::sync::Weak<dyn RxProbe>);
self.state.lock().unwrap().netlinks.insert(
fd,
NetlinkSocket {
replies: std::collections::VecDeque::new(),
responses: 0,
rec,
_probe: probe,
nonblocking: ty & libc::SOCK_NONBLOCK != 0,
fcntl: crate::fabric::SocketFcntl::default(),
descriptors: std::collections::BTreeSet::from([fd]),
sockopts: HashMap::default(),
},
);
if ty & libc::SOCK_CLOEXEC != 0 {
snare_interpose::real(|| unsafe {
libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC)
});
}
return ok(fd as i64);
}
if domain != libc::AF_INET && domain != libc::AF_INET6 {
return None;
}
if ty & 0xFF != libc::SOCK_DGRAM {
return None; }
let (fd, rec) = match self.open_socket(SocketKind::Udp) {
Ok(opened) => opened,
Err(errno) => return err(errno),
};
rec.set_family(
domain,
if protocol == 0 {
libc::IPPROTO_UDP
} else {
protocol
},
);
rec.set_type(libc::SOCK_DGRAM);
{
let inet6 = domain == libc::AF_INET6;
let mut state = rec.state();
state.inet6 = inet6;
state.v6only = inet6
&& self
.shared()
.is_some_and(|shared| shared.sys.limits().bindv6only);
}
let probe = Arc::new(HostRx {
host: self.me.get().cloned().unwrap_or_default(),
id: rec.id.get(),
});
rec.set_probe(Arc::downgrade(&probe) as std::sync::Weak<dyn RxProbe>);
let nonblocking = ty & libc::SOCK_NONBLOCK != 0;
self.state.lock().unwrap().udp.insert(
fd,
UdpSocket {
fd,
descriptors: std::collections::BTreeSet::from([fd]),
domain,
local: None,
requested: None,
peer: None,
rx: crate::limits::RxQueue::default(),
nonblocking,
fcntl: crate::fabric::SocketFcntl::default(),
broadcast: false,
sockopts: HashMap::default(),
tx_deadline: None,
rec,
_probe: probe,
},
);
if ty & libc::SOCK_CLOEXEC != 0 {
let result = snare_interpose::real(|| unsafe {
libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC)
});
if result < 0 {
let errno = std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EMFILE);
unsafe { Net::close(self, fd) };
return err(errno);
}
}
ok(fd as i64)
}
#[cfg(target_os = "linux")]
unsafe fn bind(&self, fd: c_int, addr: *const u8, len: u32) -> Option<NetResult> {
let (rec, domain, id) = {
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
if sock.local.is_some() {
return err(libc::EINVAL);
}
(sock.rec.clone(), sock.domain, sock.rec.id.get())
};
let mut sa = match unsafe { crate::fabric::bind_addr(&rec, domain, addr, len, false) } {
Ok(sa) => sa,
Err(errno) => return err(errno),
};
let shared = self.shared();
if let Some(shared) = &shared {
if let Err(errno) = shared.claim_address(sa.ip()) {
return err(errno);
}
if let Some(errno) = shared.sys.bind_denied(sa) {
return err(errno);
}
}
let station = shared
.as_ref()
.is_some_and(|shared| shared.topo().is_station_ip(sa.ip()));
let want = crate::fabric::Claim::want(&rec, sa, station);
let asked = sa;
let mut state = self.state.lock().unwrap();
if sa.port() == 0 {
let dual = rec.state().dual_at(sa);
let Some(p) = state.alloc_ephemeral(sa.ip(), dual) else {
return err(libc::EADDRINUSE);
};
sa.set_port(p);
} else if state
.udp
.iter()
.filter(|(_, sock)| sock.rec.id.get() != id)
.filter_map(|(_, sock)| HostState::udp_claim(sock))
.any(|held| crate::fabric::bind_conflict(held, want))
{
return err(libc::EADDRINUSE);
}
if let Some(shared) = &shared {
rec.state().bound_order = shared.bind_order();
}
state.bind_udp(sa, id);
let sock = state.udp.get_mut(&fd).unwrap();
sock.local = Some(sa);
sock.requested = Some(asked);
sock.rec.set_local(sa);
drop(state);
crate::fabric::bound_at(&rec, sa);
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn connect(&self, fd: c_int, addr: *const u8, len: u32) -> Option<NetResult> {
if !self.state.lock().unwrap().udp.contains_key(&fd) {
return None;
}
if !addr.is_null()
&& len as usize >= size_of::<libc::sa_family_t>()
&& unsafe { (*addr.cast::<libc::sockaddr>()).sa_family } as c_int == libc::AF_UNSPEC
{
self.state.lock().unwrap().udp_disconnect(fd);
return ok(0);
}
let (local, requested, broadcast, rec, domain) = {
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
(
sock.local,
sock.requested,
sock.broadcast,
sock.rec.clone(),
sock.domain,
)
};
let (dest, native) = match unsafe {
crate::fabric::sock_addr(domain, addr, len, crate::fabric::AddrUse::Datagram)
} {
Ok(dest) => dest,
Err(errno) => return err(errno),
};
let unhashed = crate::fabric::unhashed(requested);
if domain == libc::AF_INET6
&& let Err(errno) =
crate::fabric::dgram_crossing(&rec, local.or(unhashed), dest, native, true)
{
return err(errno);
}
let regs = self.registries.lock().unwrap().clone()?;
let station = regs.station_at(dest.ip());
let from = local
.or(unhashed)
.map(|local| crate::netif::toward(local, dest));
let sender = match regs.shared.route_send(
&rec.view(from),
dest,
crate::netif::Op::Connect,
station,
broadcast,
) {
Ok(sender) => sender,
Err(errno) => return err(errno),
};
let mut state = self.state.lock().unwrap();
match state.udp.get(&fd)?.local {
None => {
let unbound = from.unwrap_or(std::net::SocketAddr::new(
crate::fabric::unspecified_like(dest.ip()),
0,
));
let ip = sender.connected_local(unbound).ip();
if state.autobind(fd, ip, ®s.shared).is_none() {
return err(libc::EAGAIN);
}
}
Some(bound) => {
let to = sender.connected_local(crate::netif::toward(bound, dest));
state.rehash(fd, bound, to);
}
}
let sock = state.udp.get_mut(&fd).unwrap();
sock.peer = Some(dest);
if let Some(local) = sock.local {
sock.rec.set_local(local);
}
sock.rec.set_peer(Some(dest));
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn sendto(
&self,
fd: c_int,
buf: *const u8,
len: usize,
flags: c_int,
addr: *const u8,
addr_len: u32,
) -> Option<NetResult> {
{
let mut state = self.state.lock().unwrap();
if state.netlinks.contains_key(&fd) {
let req = if buf.is_null() || len == 0 {
Vec::new()
} else {
unsafe { std::slice::from_raw_parts(buf, len) }.to_vec()
};
let reply = state.queue_netlink_reply(fd, &req);
drop(state);
if let Some(reply) = reply
&& let Some(shared) = self.shared()
{
shared.bump_keys(&[reply.wake_key()]);
}
return ok(len as i64);
}
if !state.udp.contains_key(&fd) {
return None;
}
}
if addr.is_null() {
return unsafe { self.send(fd, buf, len, flags) };
}
let domain = self.state.lock().unwrap().udp.get(&fd)?.domain;
let dest = match unsafe {
crate::fabric::sock_addr(domain, addr, addr_len, crate::fabric::AddrUse::Datagram)
} {
Ok((dest, _)) => dest,
Err(errno) => return err(errno),
};
snare_interpose::charge_latency();
let now = self.clock.tick_realtime();
let data = if buf.is_null() || len == 0 {
Vec::new()
} else {
unsafe { std::slice::from_raw_parts(buf, len) }.to_vec()
};
self.udp_send(fd, data, dest, now, &Default::default())
}
#[cfg(target_os = "linux")]
unsafe fn sendmsg(&self, fd: c_int, msg: *const u8, _flags: c_int) -> Option<NetResult> {
if msg.is_null() {
return err(libc::EFAULT);
}
let hdr = msg as *const libc::msghdr;
let mut data = Vec::new();
let iov = unsafe { (*hdr).msg_iov };
let iovlen = unsafe { (*hdr).msg_iovlen };
if !iov.is_null() {
for i in 0..iovlen {
let v = unsafe { &*iov.add(i) };
if v.iov_base.is_null() || v.iov_len == 0 {
continue;
}
data.extend_from_slice(unsafe {
std::slice::from_raw_parts(v.iov_base as *const u8, v.iov_len)
});
}
}
{
let mut state = self.state.lock().unwrap();
if state.netlinks.contains_key(&fd) {
let reply = state.queue_netlink_reply(fd, &data);
drop(state);
if let Some(reply) = reply
&& let Some(shared) = self.shared()
{
shared.bump_keys(&[reply.wake_key()]);
}
return ok(data.len() as i64);
}
}
let (rec, domain, peer) = {
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
(sock.rec.clone(), sock.domain, sock.peer)
};
let v6 = domain == libc::AF_INET6;
let name = unsafe { (*hdr).msg_name } as *const u8;
let dest = if name.is_null() {
match peer {
Some(peer) => peer,
None => return err(libc::EDESTADDRREQ),
}
} else {
let len = unsafe { (*hdr).msg_namelen };
match unsafe {
crate::fabric::sock_addr(domain, name, len, crate::fabric::AddrUse::Datagram)
} {
Ok((dest, _)) => dest,
Err(errno) => return err(errno),
}
};
let shared = self.shared()?;
let ctl = match unsafe { crate::ancillary::control(hdr) } {
Ok(None) => crate::ancillary::SendCtl::default(),
Ok(Some(control)) => {
match crate::ancillary::datagram_send(&shared, &rec, &control, v6, dest) {
Ok(ctl) => ctl,
Err(errno) => return err(errno),
}
}
Err(errno) => return err(errno),
};
if let Some(deadline) = ctl.txtime
&& let Some(sock) = self.state.lock().unwrap().udp.get_mut(&fd)
{
sock.tx_deadline = Some(deadline);
}
snare_interpose::charge_latency();
let now = self.clock.tick_realtime();
self.udp_send(fd, data, dest, now, &ctl)
}
#[cfg(target_os = "linux")]
unsafe fn send(
&self,
fd: c_int,
buf: *const u8,
len: usize,
_flags: c_int,
) -> Option<NetResult> {
{
let mut state = self.state.lock().unwrap();
if state.netlinks.contains_key(&fd) {
let req = if buf.is_null() || len == 0 {
Vec::new()
} else {
unsafe { std::slice::from_raw_parts(buf, len) }.to_vec()
};
let reply = state.queue_netlink_reply(fd, &req);
drop(state);
if let Some(reply) = reply
&& let Some(shared) = self.shared()
{
shared.bump_keys(&[reply.wake_key()]);
}
return ok(len as i64);
}
match state.udp.get(&fd) {
Some(sock) if sock.peer.is_none() => return err(libc::EDESTADDRREQ),
Some(_) => {}
None => return None,
}
}
snare_interpose::charge_latency();
let now = self.clock.tick_realtime();
let data = if buf.is_null() || len == 0 {
Vec::new()
} else {
unsafe { std::slice::from_raw_parts(buf, len) }.to_vec()
};
let dest = self
.state
.lock()
.unwrap()
.udp
.get(&fd)
.and_then(|s| s.peer)?;
self.udp_send(fd, data, dest, now, &Default::default())
}
#[cfg(target_os = "linux")]
unsafe fn recvfrom(
&self,
fd: c_int,
buf: *mut u8,
len: usize,
flags: c_int,
addr: *mut u8,
addr_len: *mut u32,
) -> Option<NetResult> {
{
let mut state = self.state.lock().unwrap();
if let Some(q) = state.netlinks.get_mut(&fd).map(|n| &mut n.replies) {
let Some(blob) = q.pop_front() else {
return err(libc::EAGAIN);
};
let n = len.min(blob.len());
unsafe { std::ptr::copy_nonoverlapping(blob.as_ptr(), buf, n) };
if n < blob.len() {
q.push_front(blob[n..].to_vec());
}
return ok(n as i64);
}
if !state.udp.contains_key(&fd) {
return None;
}
}
let (_peer, nonblocking, rec) = {
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
(sock.peer, sock.nonblocking, sock.rec.clone())
};
let shared = self.shared()?;
rec.land();
let deadline = rec.opts().rcvtimeo.map(crate::readiness::Deadline::timeout);
let pop = || {
let mut state = self.state.lock().unwrap();
let sock = state.udp.get_mut(&fd)?;
let accept = |_| true;
if flags & libc::MSG_PEEK != 0 {
sock.rx.peek(accept, Some(&sock.rec)).map(|(dg, _)| dg)
} else {
sock.rx.pop(accept, Some(&sock.rec)).map(|(dg, _)| dg)
}
};
let taker = if flags & libc::MSG_PEEK != 0 {
crate::sockets::Taker::Peek
} else {
crate::sockets::Taker::Recv
};
let dg = loop {
rec.land();
if let Some(errno) = rec.take_error_as(&shared, taker) {
return err(errno);
}
if let Some(dg) = pop() {
break dg;
}
if nonblocking || flags & libc::MSG_DONTWAIT != 0 {
snare_interpose::charge_latency();
return err(libc::EAGAIN);
}
if !crate::fabric::readiness().wait_until_on(
"udp recv",
deadline,
&[rec.wake_key()],
|| rec.pending_time(),
|| {
let mut state = self.state.lock().unwrap();
rec.peek_error().is_some()
|| state
.udp
.get_mut(&fd)
.is_some_and(|s| s.rx.has(|_| true, Some(&s.rec)))
},
) {
return err(libc::EAGAIN); }
};
self.clock.reach_realtime(dg.timestamp);
let n = len.min(dg.data.len());
crate::tstamp::note_datagram_read(
&shared,
&rec,
crate::tstamp::Stamp::from_real(dg.timestamp),
&dg.rx_fallback,
);
unsafe { std::ptr::copy_nonoverlapping(dg.data.as_ptr(), buf, n) };
if !addr.is_null() && !addr_len.is_null() {
let src = crate::netif::reported(rec.state().inet6, dg.src);
unsafe { write_sockaddr(src, addr, addr_len) };
}
ok(if flags & libc::MSG_TRUNC != 0 {
dg.data.len()
} else {
n
} as i64)
}
#[cfg(target_os = "linux")]
fn keep_error(&self, fd: c_int, errno: c_int) {
let rec = self.state.lock().unwrap().socket_rec(fd);
if let Some(rec) = rec {
rec.raise_error(errno, crate::sockets::ErrorOrigin::Raised, None);
}
}
#[cfg(target_os = "linux")]
unsafe fn recvmsg(&self, fd: c_int, msg: *mut u8, flags: c_int) -> Option<NetResult> {
if msg.is_null() {
return err(libc::EFAULT);
}
let hdr = msg as *mut libc::msghdr;
let mut state = self.state.lock().unwrap();
if let Some(q) = state.netlinks.get_mut(&fd).map(|n| &mut n.replies) {
let Some(blob) = q.pop_front() else {
return err(libc::EAGAIN);
};
let iov = unsafe { (*hdr).msg_iov };
let iovlen = unsafe { (*hdr).msg_iovlen };
let mut remaining = &blob[..];
let mut copied = 0usize;
if !iov.is_null() {
for i in 0..iovlen {
if remaining.is_empty() {
break;
}
let v = unsafe { &*iov.add(i) };
if v.iov_base.is_null() || v.iov_len == 0 {
continue;
}
let take = remaining.len().min(v.iov_len);
unsafe {
std::ptr::copy_nonoverlapping(
remaining.as_ptr(),
v.iov_base as *mut u8,
take,
)
};
remaining = &remaining[take..];
copied += take;
}
}
if !remaining.is_empty() {
q.push_front(remaining.to_vec());
}
unsafe { (*hdr).msg_controllen = 0 };
return ok(copied as i64);
}
let sock = state.udp.get_mut(&fd)?;
let errqueue = flags & libc::MSG_ERRQUEUE != 0;
let rec = sock.rec.clone();
if errqueue {
let v6 = sock.domain == libc::AF_INET6;
drop(state);
if let Some(read) =
unsafe { read_error_report(&rec, hdr, v6, self.shared()?.tstamp_now().real) }
{
return Some(read);
}
snare_interpose::charge_latency();
return err(libc::EAGAIN);
}
let wait = !sock.nonblocking && flags & libc::MSG_DONTWAIT == 0;
let v6 = sock.domain == libc::AF_INET6;
let shared = self.shared()?;
drop(state);
let deadline = rec.opts().rcvtimeo.map(crate::readiness::Deadline::timeout);
let (dg, drops) = loop {
rec.land();
if let Some(errno) = rec.take_error_as(&shared, crate::sockets::Taker::Recv) {
return err(errno);
}
let got = {
let mut state = self.state.lock().unwrap();
let sock = state.udp.get_mut(&fd)?;
if flags & libc::MSG_PEEK != 0 {
sock.rx.peek(|_| true, Some(&sock.rec))
} else {
sock.rx.pop(|_| true, Some(&sock.rec))
}
};
if let Some(got) = got {
break got;
}
if !wait {
snare_interpose::charge_latency();
return err(libc::EAGAIN);
}
if !crate::fabric::readiness().wait_until_on(
"udp recvmsg",
deadline,
&[rec.wake_key()],
|| rec.pending_time(),
|| {
let mut state = self.state.lock().unwrap();
rec.peek_error().is_some()
|| state
.udp
.get_mut(&fd)
.is_some_and(|s| s.rx.has(|_| true, Some(&s.rec)))
},
) {
return err(libc::EAGAIN);
}
};
self.clock.reach_realtime(dg.timestamp);
let rxq_ovfl = rec.state().buf.rxq_ovfl;
let copied = unsafe { crate::fabric::scatter(hdr, &dg.data) };
unsafe { addr_write_from_msg(hdr, crate::netif::reported(v6, dg.src)) };
let stamp = crate::tstamp::Stamp {
hw: dg.hw,
..crate::tstamp::Stamp::from_real(dg.timestamp)
};
let mut cmsgs: Vec<_> = crate::ancillary::gro_cmsg(&rec, &dg.ip)
.into_iter()
.collect();
cmsgs.extend(crate::tstamp::rx_cmsgs(
self.shared().as_deref(),
&rec,
stamp,
crate::tstamp::Rx::Datagram,
Some(dg.rx_fallback.as_ref()),
));
if rxq_ovfl && drops > 0 {
cmsgs.push((
libc::SOL_SOCKET,
crate::limits::SO_RXQ_OVFL,
drops.to_ne_bytes().to_vec(),
));
}
cmsgs.extend(crate::ancillary::rx_cmsgs(
&shared,
&rec,
v6,
dg.src.ip(),
&dg.ip,
));
let mut msg_flags = 0;
if copied < dg.data.len() {
msg_flags |= libc::MSG_TRUNC;
}
if unsafe { crate::fabric::write_cmsg_list(hdr, &cmsgs) } {
msg_flags |= libc::MSG_CTRUNC;
}
unsafe { (*hdr).msg_flags = msg_flags };
ok(copied as i64)
}
#[cfg(target_os = "linux")]
unsafe fn setsockopt(
&self,
fd: c_int,
level: c_int,
name: c_int,
val: *const u8,
len: u32,
) -> Option<NetResult> {
let (rec, netlink) = self.sockopt_rec(fd)?;
if netlink {
let shared = self.shared()?;
if let Some(result) =
unsafe { crate::limits::sockopt::set(&shared, &rec, level, name, val, len) }
{
return match result {
Ok(()) => ok(0),
Err(errno) => err(errno),
};
}
if shared.unmodelled_option(&rec, crate::sockets::UnmodelledOption::Set { level, name })
{
return err(libc::ENOPROTOOPT);
}
let bytes = if val.is_null() {
Vec::new()
} else {
unsafe { std::slice::from_raw_parts(val, len as usize) }.to_vec()
};
let mut state = self.state.lock().unwrap();
state
.netlinks
.get_mut(&fd)?
.sockopts
.insert((level, name), bytes);
return ok(0);
}
if let Some(shared) = self.shared()
&& let Some(result) = unsafe {
crate::netif::sockopt::set(&shared, &rec, level, name, val, len)
.or_else(|| crate::limits::sockopt::set(&shared, &rec, level, name, val, len))
}
{
return match result {
Ok(()) => ok(0),
Err(errno) => err(errno),
};
}
let (v6, bound) = {
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
(sock.domain == libc::AF_INET6, sock.local.is_some())
};
if (level, name) == (libc::IPPROTO_IPV6, libc::IPV6_V6ONLY) {
return unsafe { crate::fabric::v6only_set(&rec, bound, val, len) };
}
if level == libc::SOL_SOCKET && (name == libc::SO_REUSEADDR || name == libc::SO_REUSEPORT) {
let on = unsafe { crate::fabric::read_int(val, len) } != 0;
let mut state = rec.state();
if name == libc::SO_REUSEADDR {
state.opts.reuseaddr = on;
} else {
state.opts.reuseport = on;
}
return ok(0);
}
if let Some(result) = unsafe { crate::netif::frag::set(&rec, v6, level, name, val, len) }
.or_else(|| unsafe { crate::ancillary::set(&rec, v6, level, name, val, len) })
{
return match result {
Ok(()) => ok(0),
Err(errno) => err(errno),
};
}
if let Some(result) = unsafe {
crate::tstamp::set(
self.shared().as_deref(),
&rec,
crate::tstamp::Kind::Datagram,
level,
name,
val,
len,
)
} {
return match result {
Ok(()) => ok(0),
Err(errno) => err(errno),
};
}
let net_admin = self
.shared()
.is_some_and(|shared| shared.sys.has_cap(crate::limits::CAP_NET_ADMIN));
if let Some(result) =
unsafe { crate::tstamp::set_txtime(&rec, net_admin, level, name, val, len) }
{
return match result {
Ok(()) => ok(0),
Err(errno) => err(errno),
};
}
if !host_modelled(level, name)
&& !crate::fabric::harmless(level, name)
&& udp_parse_group(level, name, &[0; 16]).is_none()
&& let Some(shared) = self.shared()
&& shared.unmodelled_option(&rec, crate::sockets::UnmodelledOption::Set { level, name })
{
return err(libc::ENOPROTOOPT);
}
let mut state = self.state.lock().unwrap();
let sock = state.udp.get_mut(&fd)?;
let bytes = if val.is_null() {
Vec::new()
} else {
unsafe { std::slice::from_raw_parts(val, len as usize) }.to_vec()
};
if level == libc::SOL_SOCKET && name == libc::SO_BROADCAST {
sock.broadcast = bytes.first().is_some_and(|&b| b != 0);
}
if (level, name) == (libc::IPPROTO_IP, libc::IP_RECVERR)
|| (level, name) == (libc::IPPROTO_IPV6, libc::IPV6_RECVERR)
{
sock.rec.state().dgram.recverr = bytes.iter().any(|&b| b != 0);
}
if level == libc::SOL_SOCKET && (name == libc::SO_RCVTIMEO || name == libc::SO_SNDTIMEO) {
let timeout =
unsafe { crate::fabric::parse_timeval(val, len) }.filter(|d| !d.is_zero());
let mut rec = sock.rec.state();
if name == libc::SO_RCVTIMEO {
rec.opts.rcvtimeo = timeout;
} else {
rec.opts.sndtimeo = timeout;
}
return ok(0);
}
let drop_group = (level == libc::IPPROTO_IP && name == libc::IP_DROP_MEMBERSHIP)
|| (level == libc::IPPROTO_IPV6 && name == libc::IPV6_DROP_MEMBERSHIP);
let membership_name = if drop_group {
if level == libc::IPPROTO_IP {
libc::IP_ADD_MEMBERSHIP
} else {
libc::IPV6_ADD_MEMBERSHIP
}
} else {
name
};
if let Some(group) = udp_parse_group(level, membership_name, &bytes)
&& let Err(errno) = sock
.rec
.change_membership(udp_membership(group, &bytes), !drop_group)
{
return err(errno);
}
if crate::fabric::harmless(level, name) {
rec.keep_ignored(level, name, &bytes);
}
sock.sockopts.insert((level, name), bytes);
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn getsockopt(
&self,
fd: c_int,
level: c_int,
name: c_int,
val: *mut u8,
len: *mut u32,
) -> Option<NetResult> {
let (rec, netlink) = self.sockopt_rec(fd)?;
if netlink {
if let Some(result) =
unsafe { crate::limits::sockopt::get(&rec, level, name, val, len) }
{
return match result {
Ok(()) => ok(0),
Err(errno) => err(errno),
};
}
if val.is_null() || len.is_null() {
return err(libc::EFAULT);
}
if level == libc::SOL_SOCKET && name == libc::SO_ERROR {
let errno = self
.shared()
.and_then(|shared| rec.take_error_as(&shared, crate::sockets::Taker::SoError))
.unwrap_or(0);
unsafe { crate::fabric::write_opt(errno, val, len) };
return ok(0);
}
let stored = self
.state
.lock()
.unwrap()
.netlinks
.get(&fd)?
.sockopts
.get(&(level, name))
.cloned()
.unwrap_or_default();
if stored.is_empty()
&& let Some(shared) = self.shared()
&& shared
.unmodelled_option(&rec, crate::sockets::UnmodelledOption::Get { level, name })
{
return err(libc::ENOPROTOOPT);
}
let cap = unsafe { *len } as usize;
let n = if stored.is_empty() {
cap.min(4)
} else {
stored.len().min(cap)
};
unsafe {
std::ptr::write_bytes(val, 0, n);
std::ptr::copy_nonoverlapping(stored.as_ptr(), val, n.min(stored.len()));
*len = n as u32;
}
return ok(0);
}
rec.land();
if val.is_null() || len.is_null() {
return err(libc::EFAULT);
}
if unsafe { crate::netif::sockopt::get(&rec, level, name, val, len) }.is_some() {
return ok(0);
}
let v6 = self.state.lock().unwrap().udp.get(&fd)?.domain == libc::AF_INET6;
if (level, name) == (libc::IPPROTO_IPV6, libc::IPV6_V6ONLY) {
return unsafe { crate::fabric::v6only_get(&rec, val, len) };
}
if level == libc::SOL_SOCKET && (name == libc::SO_REUSEADDR || name == libc::SO_REUSEPORT) {
let opts = rec.opts();
let on = if name == libc::SO_REUSEADDR {
opts.reuseaddr
} else {
opts.reuseport
};
unsafe { crate::fabric::write_opt(crate::fabric::socket_flag(on, name), val, len) };
return ok(0);
}
if let Some(result) = unsafe { crate::netif::frag::get(&rec, v6, level, name, val, len) }
.or_else(|| unsafe { crate::ancillary::get(&rec, v6, level, name, val, len) })
{
return match result {
Ok(()) => ok(0),
Err(errno) => err(errno),
};
}
if let Some(result) = unsafe { crate::limits::sockopt::get(&rec, level, name, val, len) } {
return match result {
Ok(()) => ok(0),
Err(errno) => err(errno),
};
}
if unsafe { crate::tstamp::get(&rec, level, name, val, len) }.is_some() {
return ok(0);
}
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
if level == libc::SOL_SOCKET && (name == libc::SO_RCVTIMEO || name == libc::SO_SNDTIMEO) {
let opts = sock.rec.opts();
let d = if name == libc::SO_RCVTIMEO {
opts.rcvtimeo
} else {
opts.sndtimeo
};
unsafe { crate::fabric::write_timeval(d.unwrap_or_default(), val, len) };
return ok(0);
}
if level == libc::SOL_SOCKET && name == libc::SO_ERROR {
let rec = sock.rec.clone();
drop(state);
let errno = self
.shared()
.and_then(|shared| rec.take_error_as(&shared, crate::sockets::Taker::SoError))
.unwrap_or(0);
unsafe { crate::fabric::write_opt(errno, val, len) };
return ok(0);
}
let cap = unsafe { *len } as usize;
let empty = Vec::new();
let stored = sock.sockopts.get(&(level, name)).unwrap_or(&empty);
if stored.is_empty()
&& !host_modelled(level, name)
&& !crate::fabric::harmless(level, name)
&& let Some(shared) = self.shared()
&& shared.unmodelled_option(&rec, crate::sockets::UnmodelledOption::Get { level, name })
{
return err(libc::ENOPROTOOPT);
}
let n = if stored.is_empty() {
cap.min(4)
} else {
stored.len().min(cap)
};
unsafe {
std::ptr::write_bytes(val, 0, n);
if !stored.is_empty() {
std::ptr::copy_nonoverlapping(stored.as_ptr(), val, n);
}
*len = n as u32;
}
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn recv(&self, fd: c_int, buf: *mut u8, len: usize, flags: c_int) -> Option<NetResult> {
unsafe {
self.recvfrom(
fd,
buf,
len,
flags,
std::ptr::null_mut(),
std::ptr::null_mut(),
)
}
}
#[cfg(target_os = "linux")]
unsafe fn getsockname(
&self,
fd: c_int,
addr: *mut u8,
addr_len: *mut u32,
) -> Option<NetResult> {
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
let unhashed = sock.requested.map(|r| std::net::SocketAddr::new(r.ip(), 0));
let inet6 = sock.domain == libc::AF_INET6;
let local = sock.local.or(unhashed).unwrap_or_else(|| {
std::net::SocketAddr::new(
if inet6 {
std::net::IpAddr::V6(std::net::Ipv6Addr::UNSPECIFIED)
} else {
std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)
},
0,
)
});
drop(state);
unsafe { write_sockaddr(crate::netif::reported(inet6, local), addr, addr_len) };
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn getpeername(
&self,
fd: c_int,
addr: *mut u8,
addr_len: *mut u32,
) -> Option<NetResult> {
let state = self.state.lock().unwrap();
let sock = state.udp.get(&fd)?;
let Some(peer) = sock.peer else {
return err(libc::ENOTCONN);
};
let inet6 = sock.domain == libc::AF_INET6;
drop(state);
unsafe { write_sockaddr(crate::netif::reported(inet6, peer), addr, addr_len) };
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn close(&self, fd: c_int) -> Option<NetResult> {
self.detach_socket(fd, false)?;
unsafe { libc::close(fd) };
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn fd_replaced(&self, fd: c_int) -> Option<NetResult> {
self.detach_socket(fd, true)?;
ok(0)
}
#[cfg(target_os = "linux")]
unsafe fn dup_to(&self, fd: c_int, newfd: c_int, flags: Option<c_int>) -> Option<NetResult> {
let shared = self.shared()?;
let mut state = self.state.lock().unwrap();
let rec = state.socket_rec(fd)?;
let result = crate::fabric::duplicate_to(fd, newfd, flags);
if result < 0 {
return err(std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EBADF));
}
if fd == newfd
|| state
.socket_rec(newfd)
.is_some_and(|previous| Arc::ptr_eq(&previous, &rec))
{
return ok(result as i64);
}
let previous = state.socket_rec(newfd);
state.remove_socket(newfd);
state.alias_socket(fd, newfd);
shared.sockets.alias(newfd, &rec);
drop(state);
if let Some(previous) = previous {
shared.sockets.fd_replaced(newfd, &previous, shared.stamp());
shared.bump_keys(&[previous.wake_key()]);
}
ok(result as i64)
}
#[cfg(target_os = "linux")]
unsafe fn fcntl(&self, fd: c_int, cmd: c_int, arg: i64) -> Option<NetResult> {
let shared = self.shared();
let mut state = self.state.lock().unwrap();
let rec = state.socket_rec(fd)?;
if matches!(cmd, libc::F_DUPFD | libc::F_DUPFD_CLOEXEC) {
let newfd = snare_interpose::real(|| unsafe { libc::fcntl(fd, cmd, arg as c_int) });
if newfd < 0 {
return err(std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EMFILE));
}
state.alias_socket(fd, newfd);
shared?.sockets.alias(newfd, &rec);
return ok(newfd as i64);
}
if matches!(cmd, libc::F_GETFD | libc::F_SETFD) {
let result = snare_interpose::real(|| unsafe { libc::fcntl(fd, cmd, arg as c_int) });
return if result < 0 {
err(std::io::Error::last_os_error()
.raw_os_error()
.unwrap_or(libc::EINVAL))
} else {
ok(result as i64)
};
}
let (nonblocking, fcntl) = if let Some(socket) = state.udp.get_mut(&fd) {
(&mut socket.nonblocking, &mut socket.fcntl)
} else {
let netlink = state.netlinks.get_mut(&fd)?;
(&mut netlink.nonblocking, &mut netlink.fcntl)
};
match cmd {
libc::F_SETFL => {
fcntl.status = match crate::fabric::SocketFcntl::kept_status(arg) {
Ok(kept) => kept,
Err(errno) => return err(errno),
};
*nonblocking = arg as c_int & libc::O_NONBLOCK != 0;
ok(0)
}
libc::F_GETFL => {
let nonblock = if *nonblocking { libc::O_NONBLOCK } else { 0 };
ok((libc::O_RDWR | nonblock | fcntl.status) as i64)
}
_ => unsafe { fcntl.command(cmd, arg) }.or_else(|| err(libc::EINVAL)),
}
}
#[cfg(target_os = "linux")]
unsafe fn dup(&self, fd: c_int) -> Option<NetResult> {
unsafe { Net::fcntl(self, fd, libc::F_DUPFD, 0) }
}
}
#[cfg(target_os = "macos")]
enum HwValue {
Int(i32),
Str(&'static str),
}
#[cfg(target_os = "macos")]
fn hw_node(state: &HostState, name: &str) -> Option<Result<HwValue, c_int>> {
let all = state.cpu_count as i32;
let online = state.online_count() as i32;
let count = |field: &str| match field {
"logicalcpu_max" | "physicalcpu_max" => Some(Ok(HwValue::Int(all))),
"logicalcpu" | "physicalcpu" => Some(Ok(HwValue::Int(online))),
_ => None,
};
match name.strip_prefix("hw.")? {
"ncpu" => Some(Ok(HwValue::Int(all))),
"activecpu" => Some(Ok(HwValue::Int(online))),
"nperflevels" if cfg!(target_arch = "aarch64") => Some(Ok(HwValue::Int(1))),
level if cfg!(target_arch = "aarch64") && level.starts_with("perflevel") => {
let (index, field) = level["perflevel".len()..].split_once('.')?;
match (index, field) {
("0", "name") => Some(Ok(HwValue::Str("Performance"))),
("0", field) => count(field),
_ if index.parse::<u32>().is_ok() => Some(Err(libc::ENOENT)),
_ => None,
}
}
field => count(field),
}
}
#[cfg(target_os = "macos")]
const HW_NAMES: [&std::ffi::CStr; 22] = [
c"hw.ncpu",
c"hw.activecpu",
c"hw.logicalcpu",
c"hw.logicalcpu_max",
c"hw.physicalcpu",
c"hw.physicalcpu_max",
c"hw.nperflevels",
c"hw.perflevel0.name",
c"hw.perflevel0.logicalcpu",
c"hw.perflevel0.logicalcpu_max",
c"hw.perflevel0.physicalcpu",
c"hw.perflevel0.physicalcpu_max",
c"hw.perflevel1.name",
c"hw.perflevel1.logicalcpu",
c"hw.perflevel1.logicalcpu_max",
c"hw.perflevel1.physicalcpu",
c"hw.perflevel1.physicalcpu_max",
c"hw.perflevel2.name",
c"hw.perflevel2.logicalcpu",
c"hw.perflevel2.logicalcpu_max",
c"hw.perflevel2.physicalcpu",
c"hw.perflevel2.physicalcpu_max",
];
#[cfg(target_os = "macos")]
fn hw_name_of(mib: &[c_int]) -> Option<&'static str> {
if mib.first() != Some(&libc::CTL_HW) {
return None;
}
HW_NAMES.iter().find_map(|name| {
let mut words = [0 as c_int; 4];
let mut len = words.len();
let r = unsafe { libc::sysctlnametomib(name.as_ptr(), words.as_mut_ptr(), &mut len) };
(r == 0 && words[..len] == *mib).then(|| name.to_str().unwrap())
})
}
#[cfg(target_os = "macos")]
unsafe fn read_hw(
value: &HwValue,
oldp: *mut u8,
oldlenp: *mut usize,
newp: *const u8,
) -> Option<HostResult> {
if !newp.is_null() {
return err(libc::EPERM);
}
let bytes: Vec<u8> = match value {
HwValue::Int(v) => v.to_ne_bytes().to_vec(),
HwValue::Str(s) => [s.as_bytes(), b"\0"].concat(),
};
unsafe { read_node(&bytes, false, oldp, oldlenp) }
}
#[cfg(target_os = "macos")]
unsafe fn read_node(
bytes: &[u8],
truncate: bool,
oldp: *mut u8,
oldlenp: *mut usize,
) -> Option<HostResult> {
if oldlenp.is_null() {
return if oldp.is_null() {
ok(0)
} else {
err(libc::ENOMEM)
};
}
unsafe {
if oldp.is_null() {
*oldlenp = bytes.len();
return ok(0);
}
let room = *oldlenp;
if truncate && room > 0 && room < bytes.len() {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), oldp, room - 1);
*oldp.add(room - 1) = 0;
return ok(0);
}
if room < bytes.len() {
*oldlenp = 0;
return err(libc::ENOMEM);
}
std::ptr::copy_nonoverlapping(bytes.as_ptr(), oldp, bytes.len());
*oldlenp = bytes.len();
}
ok(0)
}
#[cfg(target_os = "macos")]
fn kern_name_of(mib: &[c_int]) -> Option<&'static str> {
match *mib {
[libc::CTL_KERN, libc::KERN_OSTYPE] => Some("kern.ostype"),
[libc::CTL_KERN, libc::KERN_OSRELEASE] => Some("kern.osrelease"),
[libc::CTL_KERN, libc::KERN_VERSION] => Some("kern.version"),
[libc::CTL_KERN, libc::KERN_HOSTNAME] => Some("kern.hostname"),
_ => None,
}
}
#[cfg(target_os = "macos")]
const CTL_NET: c_int = 4;
#[cfg(target_os = "macos")]
const PF_ROUTE: c_int = 17;
#[cfg(target_os = "macos")]
const NET_RT_IFLIST2: c_int = 6;
#[cfg(target_os = "macos")]
const IF_MSGHDR2_LEN: usize = 168;
#[cfg(target_os = "macos")]
fn build_iflist2(nics: &[NicSnapshot]) -> Vec<u8> {
const RTM_VERSION: u8 = 5;
const RTM_IFINFO2: u8 = 0x12;
const IFT_ETHER: u8 = 6;
const IFT_LOOP: u8 = 24;
let mut out = vec![0u8; nics.len() * IF_MSGHDR2_LEN];
for (i, nic) in nics.iter().enumerate() {
let msg = &mut out[i * IF_MSGHDR2_LEN..(i + 1) * IF_MSGHDR2_LEN];
msg[0..2].copy_from_slice(&(IF_MSGHDR2_LEN as u16).to_ne_bytes()); msg[2] = RTM_VERSION; msg[3] = RTM_IFINFO2; let flags = crate::ifaddrs::flags(nic);
msg[8..12].copy_from_slice(&flags.to_ne_bytes()); msg[12..14].copy_from_slice(&(nic.index as u16).to_ne_bytes());
let d = 32; msg[d] = if nic.loopback { IFT_LOOP } else { IFT_ETHER }; msg[d + 8..d + 12].copy_from_slice(&nic.spec.mtu.to_ne_bytes()); let c = &nic.counters;
let put = |msg: &mut [u8], off: usize, v: u64| {
msg[d + off..d + off + 8].copy_from_slice(&v.to_ne_bytes());
};
put(msg, 24, c.rx_packets); put(msg, 32, c.rx_errors); put(msg, 40, c.tx_packets); put(msg, 48, c.tx_errors); put(msg, 64, c.rx_bytes); put(msg, 72, c.tx_bytes); put(msg, 80, c.multicast); put(msg, 96, c.rx_dropped); }
out
}
#[cfg(target_os = "linux")]
fn machine_cpuinfo_entry() -> Option<&'static str> {
static ENTRY: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
ENTRY
.get_or_init(|| {
let text = snare_interpose::real(|| std::fs::read_to_string("/proc/cpuinfo")).ok()?;
text.split("\n\n")
.find(|entry| {
entry
.split_once(':')
.is_some_and(|(key, _)| key.trim() == "processor")
})
.map(str::to_owned)
})
.as_deref()
}
fn format_cpu_list(cpus: &[usize]) -> String {
if cpus.is_empty() {
return String::new();
}
let mut sorted = cpus.to_vec();
sorted.sort_unstable();
sorted.dedup();
let mut parts = Vec::new();
let mut start = sorted[0];
let mut prev = sorted[0];
for &c in &sorted[1..] {
if c == prev + 1 {
prev = c;
continue;
}
parts.push(range(start, prev));
start = c;
prev = c;
}
parts.push(range(start, prev));
parts.join(",")
}
fn range(start: usize, end: usize) -> String {
if start == end {
start.to_string()
} else {
format!("{start}-{end}")
}
}
#[cfg(target_os = "linux")]
pub(crate) unsafe fn write_icmp_report(
hdr: *mut libc::msghdr,
report: &crate::sockets::IcmpReport,
) -> Option<NetResult> {
const SO_EE_ORIGIN_ICMP: u8 = 2;
const SO_EE_ORIGIN_ICMP6: u8 = 3;
let copied = unsafe { crate::fabric::scatter(hdr, &report.payload) };
let offender = report.offender;
let (level, ty, origin, icmp_type, icmp_code) = if offender.is_ipv4() {
(libc::SOL_IP, libc::IP_RECVERR, SO_EE_ORIGIN_ICMP, 3u8, 3u8)
} else {
(libc::SOL_IPV6, libc::IPV6_RECVERR, SO_EE_ORIGIN_ICMP6, 1, 4)
};
let mut data = Vec::with_capacity(16 + 28);
data.extend_from_slice(&(report.errno as u32).to_ne_bytes());
data.extend_from_slice(&[origin, icmp_type, icmp_code, 0]);
data.extend_from_slice(&0u32.to_ne_bytes());
data.extend_from_slice(&0u32.to_ne_bytes());
data.extend_from_slice(&sockaddr_bytes(std::net::SocketAddr::new(offender.ip(), 0)));
unsafe {
addr_write_from_msg(hdr, offender);
let mut msg_flags = libc::MSG_ERRQUEUE;
if copied < report.payload.len() {
msg_flags |= libc::MSG_TRUNC;
}
if !crate::fabric::write_cmsgs_at(hdr, &[(level, ty, &data)]) {
msg_flags |= libc::MSG_CTRUNC;
}
(*hdr).msg_flags = msg_flags;
}
ok(copied as i64)
}
#[cfg(target_os = "linux")]
pub(crate) unsafe fn read_error_report(
rec: &SockRec,
hdr: *mut libc::msghdr,
v6: bool,
now: std::time::Duration,
) -> Option<NetResult> {
rec.land();
match rec.pop_error_report()? {
crate::sockets::ErrorReport::Icmp(report) => unsafe { write_icmp_report(hdr, &report) },
crate::sockets::ErrorReport::Tx(report) => unsafe {
crate::tstamp::write_tx_report(rec, report, hdr, v6, now)
},
}
}
fn with_newline(mut s: String) -> Vec<u8> {
s.push('\n');
s.into_bytes()
}
#[cfg(target_os = "linux")]
impl crate::fabric::ForeignUdp for SimHost {
fn deliver_from_peer(
&self,
src: std::net::SocketAddr,
dest: std::net::SocketAddr,
data: &[u8],
) -> usize {
let now = self.clock.tick_realtime();
let Some(regs) = self.registries.lock().unwrap().clone() else {
return 0;
};
let station = regs.station_at(dest.ip());
let sender = crate::netif::Sender::Station(src.ip());
let joined = dest.ip().is_multicast() && regs.shared.host_joined(dest.ip());
let mut state = self.state.lock().unwrap();
let cands = state.udp_cands(dest, joined);
let copies = regs.shared.fan_out(
&sender,
crate::netif::Wire {
src,
dest,
len: data.len(),
},
station,
cands,
true,
);
let mut keys = crate::readiness::WakeKeys::default();
for copy in &copies {
if let Some(socket) = state.udp.get(©.q) {
keys.push(socket.rec.wake_key());
}
}
let reached = copies.len();
state.udp_land(
copies,
data,
src,
now,
crate::ancillary::Header::plain(dest.ip()),
);
drop(state);
regs.shared.bump_keys(keys.as_slice());
reached
}
}
#[cfg(not(target_os = "linux"))]
impl crate::fabric::ForeignUdp for SimHost {
fn deliver_from_peer(
&self,
_: std::net::SocketAddr,
_: std::net::SocketAddr,
_: &[u8],
) -> usize {
0
}
}