Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions crates/netscenario-agent/tests/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,12 @@ fn a_hold_on_the_input_stops_the_stream_between_two_packets() {
let socket = UdpSocket::bind("127.0.0.1:0").unwrap();
socket.set_nonblocking(true).unwrap();
let target = socket.local_addr().unwrap().to_string();
// 800 kbit with 100 byte packets: 1,000 packets a second, for 400 ms
// 800 kbit with 100 byte packets: 1,000 packets a second, for 1.2 s. The run is long so that a machine under load, which
// answers the first hold late (the sleeps below are minimums, not exact), still has the second hold before the end: a run
// of 400 ms failed once in a full run that went out of the bounds of the packet below.
let mut process = Command::new(agent)
.args(["send", "--to", &target, "--rate", "800000", "--size", "100"])
.args(["--duration-ns", "400000000"])
.args(["--duration-ns", "1200000000"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
Expand All @@ -473,7 +475,9 @@ fn a_hold_on_the_input_stops_the_stream_between_two_packets() {
panic!("a barrier was expected, the sender said {line}");
};
assert_eq!(id, 5);
assert!((90..=130).contains(&packet), "held before packet {packet}");
// The hold came while the stream ran: after its start (a packet is due every millisecond, the sender needs a few to start)
// and before its end. Where exactly depends on the load, so the packets that arrive are compared with `packet`, not with a time.
assert!((1..1200).contains(&packet), "held before packet {packet}");
// Held: everything before the packet is there and nothing else arrives while the answer is held back
std::thread::sleep(Duration::from_millis(100));
let seen = drain(&socket);
Expand Down Expand Up @@ -505,7 +509,7 @@ fn a_hold_on_the_input_stops_the_stream_between_two_packets() {
assert!(process.wait().unwrap().success());
assert_eq!(
sent.packets + sent.errors,
400,
1200,
"no slot is lost or repeated"
);
assert_eq!(sent.barrier_timeouts, 0);
Expand Down
123 changes: 110 additions & 13 deletions crates/netscenario-backend-kernel/src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,10 @@ fn hold() -> ExitCode {
/// `sysctl` to rely on (D-078). Only names of the networking tables are accepted.
fn sysctl(settings: &[&str]) -> ExitCode {
for setting in settings {
let Some((name, value)) = setting.split_once('=') else {
return fail(&format!("'{setting}' is not name=value"));
let (name, value) = match split_setting(setting) {
Ok(parts) => parts,
Err(message) => return fail(&message),
};
if !name.starts_with("net.")
|| name.split('.').any(|part| {
part.is_empty()
|| part == ".."
|| !part
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
})
{
return fail(&format!("'{name}' is not the name of a network setting"));
}
let path = format!("/proc/sys/{}", name.replace('.', "/"));
if let Err(error) = std::fs::write(&path, value) {
return fail(&format!("cannot write {value} to {path}: {error}"));
Expand All @@ -98,6 +88,27 @@ fn sysctl(settings: &[&str]) -> ExitCode {
ExitCode::SUCCESS
}

/// The name and the value of `net.ipv4.ip_forward=1`, or why it is refused: it has to be `name=value`, and the name has to be in
/// the tables of the network with parts that are plain words (no empty part, no `..`, nothing that is not a letter, a digit, `_`
/// or `-`), so that the path in `/proc/sys` cannot leave them.
fn split_setting(setting: &str) -> Result<(&str, &str), String> {
let Some((name, value)) = setting.split_once('=') else {
return Err(format!("'{setting}' is not name=value"));
};
if !name.starts_with("net.")
|| name.split('.').any(|part| {
part.is_empty()
|| part == ".."
|| !part
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
})
{
return Err(format!("'{name}' is not the name of a network setting"));
}
Ok((name, value))
}

/// Puts the signals that a program may be sent (`TERM` by default, and the others of `shutdown`) back to what a program
/// expects. The supervisor is the first process of a pid namespace and may have been started with `SIGTERM` and `SIGINT`
/// ignored (the `unshare` of util-linux 2.37, on Ubuntu 22.04, does). An ignored signal stays ignored across `exec`, so
Expand Down Expand Up @@ -307,3 +318,89 @@ fn supervise(run_id: &str, max_nodes: usize) -> ExitCode {
let _ = provisioner.teardown();
ExitCode::SUCCESS
}

#[cfg(test)]
mod tests {
use super::*;

fn main_with(arguments: &[&str]) -> ExitCode {
main(arguments.iter().map(|argument| (*argument).to_owned()))
}

/// The command line of the helper: anything but the three forms, and a number of nodes that is no number, is refused
/// before anything is made (nothing is started, nothing is written).
#[test]
fn a_command_line_that_is_none_of_the_three_forms_is_refused() {
for arguments in [
&[][..],
&["supervise"],
&["supervise", "run"],
&["supervise", "run", "many"],
&["supervise", "run", "-1"],
&["supervise", "run", "4", "extra"],
&["hold"],
&["sysctl"],
&["restart", "run"],
] {
assert_eq!(main_with(arguments), ExitCode::FAILURE, "{arguments:?}");
}
}

/// `sysctl` writes only into the tables of the network (D-078): a name outside them, with a part that could leave the
/// directory or with a character that is no part of a name, is refused before a file is opened; a plain name is taken as it
/// is, which is the canary for the refusals (a check that refuses everything would pass the rest).
#[test]
fn a_setting_outside_the_network_tables_is_refused_before_a_file_is_opened() {
for (setting, why) in [
(
"kernel.hostname=x",
"'kernel.hostname' is not the name of a network setting",
),
(
"net..ipv4.ip_forward=1",
"'net..ipv4.ip_forward' is not the name of a network setting",
),
(
"net.ipv4.ip_forward.=1",
"'net.ipv4.ip_forward.' is not the name of a network setting",
),
(
"net.ipv4./.x=1",
"'net.ipv4./.x' is not the name of a network setting",
),
(
"net.ipv4.ip forward=1",
"'net.ipv4.ip forward' is not the name of a network setting",
),
("net.=1", "'net.' is not the name of a network setting"),
(
"net.ipv4.ip_forward",
"'net.ipv4.ip_forward' is not name=value",
),
] {
assert_eq!(split_setting(setting), Err(why.to_owned()), "{setting}");
}
for (setting, parts) in [
("net.ipv4.ip_forward=1", ("net.ipv4.ip_forward", "1")),
(
"net.ipv6.conf.all.disable_ipv6=0",
("net.ipv6.conf.all.disable_ipv6", "0"),
),
(
"net.ipv4.conf.veth-1.rp_filter=",
("net.ipv4.conf.veth-1.rp_filter", ""),
),
] {
assert_eq!(split_setting(setting), Ok(parts), "{setting}");
}
// A refusal stops the rest, and a good name that is not there fails at the write, with no file made
assert_eq!(
sysctl(&["kernel.x=1", "net.netscenario_no_such_setting=1"]),
ExitCode::FAILURE
);
assert_eq!(
sysctl(&["net.netscenario_no_such_setting.x=1"]),
ExitCode::FAILURE
);
}
}
6 changes: 6 additions & 0 deletions crates/netscenario-backend-kernel/tests/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ fn a_change_from_test_code_lands_between_two_packets_and_a_replay_loses_the_same
return;
}
let (result, dir) = live_run("live-a", Duration::from_secs(1), |handle| {
// The links the code may change, and how old the measured phase is when the wait is over (the wait is on the clock of
// the phase, so it is at least what was waited for)
assert_eq!(handle.links(), [access()]);
assert!(handle.elapsed().unwrap() >= Duration::from_secs(1));
let records = handle.change(LiveChange::set(access(), loss_30())).unwrap();
assert_eq!(records.len(), 1);
assert!(records[0].live && records[0].packet.is_some());
Expand Down Expand Up @@ -331,6 +335,8 @@ fn a_run_that_fails_does_not_leave_the_code_waiting() {
let error = run(&mut failing, &scenario(), &options).unwrap_err();
assert!(error.to_string().contains("agent"), "{error}");
let began = std::time::Instant::now();
// The workloads never began, so the phase has no age
assert_eq!(handle.elapsed(), None);
assert!(handle.change(LiveChange::down(access())).is_err());
assert!(handle.wait_until(Duration::from_secs(3_600)).is_err());
assert!(
Expand Down
84 changes: 54 additions & 30 deletions crates/netscenario-backend-kernel/tests/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

mod support;

use std::fmt::Write as _;
use std::time::Duration;

use netscenario_agent::records::{self, Record};
Expand Down Expand Up @@ -97,44 +98,61 @@ const DELAYS: &[(&str, f64, f64)] = &[
/// What a few milliseconds of two interfaces, a receiver that is not scheduled at once and a busy machine add to a delay.
const ROOM: f64 = 0.015;

fn measured_delays(id: &str) {
/// How many profiles one run holds: a run may use 8 network namespaces (SAF-02), the network and 7 terminals.
const PER_RUN: usize = 7;

/// The profiles of the library in as few runs as the limit of namespaces allows (`PER_RUN` each): the network is a star with
/// one link for each profile and a stream of 20 packets a second in each direction of each link. A run holds several profiles
/// because the links do not touch each other and what a profile adds to the kernel (its delay, jitter, loss and rate on its
/// own link) does not depend on the others; a run for each profile costs 14 times the set-up and the wait for nothing more.
/// A failure names the profile and the direction.
fn delays_of_the_profiles_in_run(run_index: usize) {
if !namespaces_available() || agent_exe().is_none() {
return;
}
let &(_, up, down) = DELAYS.iter().find(|(name, ..)| *name == id).unwrap();
let (_, records, dir) = go(&scenario(id, "", TWO_STREAMS), &format!("profile-{id}"));
// `up` is the first workload, from the terminal, which is the first node of the link: its delay is `forward`
delay_holds(&records[0], up, ROOM).unwrap_or_else(|why| panic!("{id}, up: {why}"));
delay_holds(&records[1], down, ROOM).unwrap_or_else(|why| panic!("{id}, down: {why}"));
let profiles = DELAYS.chunks(PER_RUN).nth(run_index).unwrap();
let mut nodes = String::from("network");
let mut links = String::new();
let mut streams = String::new();
for &(id, ..) in profiles {
write!(nodes, ", t-{id}").unwrap();
writeln!(
links,
" - {{ id: l-{id}, between: [t-{id}, network], profile: {id} }}"
)
.unwrap();
// `up` is from the terminal, which is the first node of the link: its delay is `forward`
write!(
streams,
" - {{ id: up-{id}, kind: udp-cbr, from: t-{id}, to: network, rate: 160kbit, packet-size: 1000b }}\n - {{ id: down-{id}, kind: udp-cbr, from: network, to: t-{id}, rate: 160kbit, packet-size: 1000b }}\n"
)
.unwrap();
}
let yaml = format!(
"version: 0\nname: profile-test\nduration: 3s\ntopology:\n nodes: [{nodes}]\n links:\n{links}workloads:\n{streams}"
);
let mut scenario = Scenario::from_yaml(&yaml, "profiles.yaml").unwrap();
scenario.measure.packets = true;
let (_, records, dir) = go(&scenario, &format!("profile-all-{run_index}"));
assert_eq!(records.len(), 2 * profiles.len());
for (index, &(id, up, down)) in profiles.iter().enumerate() {
delay_holds(&records[2 * index], up, ROOM).unwrap_or_else(|why| panic!("{id}, up: {why}"));
delay_holds(&records[2 * index + 1], down, ROOM)
.unwrap_or_else(|why| panic!("{id}, down: {why}"));
}
let _ = std::fs::remove_dir_all(dir);
}

macro_rules! delay_tests {
($($name:ident: $id:literal),* $(,)?) => {
$(
#[test]
fn $name() {
measured_delays($id);
}
)*
};
/// The first seven profiles of the table.
#[test]
fn the_delays_of_the_first_profiles_are_the_published_ones() {
delays_of_the_profiles_in_run(0);
}

delay_tests! {
the_delays_of_3g_good_are_the_published_medians: "3g-good",
the_delays_of_3g_typical_are_half_of_the_round_trip: "3g-typical",
the_delays_of_lte_good_differ_by_direction: "lte-good",
the_delays_of_lte_typical_are_half_of_the_round_trip: "lte-typical",
the_delays_of_5g_typical_are_half_of_the_round_trip: "5g-typical",
the_delays_of_5g_poor_are_the_published_average: "5g-poor",
the_delays_of_dsl_good_are_half_of_the_round_trip: "dsl-good",
the_delays_of_dsl_poor_are_half_of_the_round_trip: "dsl-poor",
the_delays_of_geo_good_are_half_of_the_round_trip: "geo-good",
the_delays_of_geo_typical_are_half_of_the_median: "geo-typical",
the_delays_of_geo_poor_are_half_of_the_median: "geo-poor",
the_delays_of_leo_typical_are_half_of_the_middle_median: "leo-typical",
the_delays_of_datacenter_rack_are_half_of_the_round_trip: "datacenter-rack",
the_delays_of_datacenter_fabric_are_half_of_the_round_trip: "datacenter-fabric",
/// The next seven.
#[test]
fn the_delays_of_the_next_profiles_are_the_published_ones() {
delays_of_the_profiles_in_run(1);
}

/// Canary for the list above: a profile in the library without a test here fails, and so does a test for a profile that is gone.
Expand All @@ -148,6 +166,12 @@ fn every_profile_of_the_library_is_tested_and_the_table_is_the_library() {
tested, library_ids,
"a profile without a test, or a test without a profile"
);
// ... and the runs above hold all of them (two tests, `PER_RUN` profiles each)
assert_eq!(
DELAYS.chunks(PER_RUN).count(),
2,
"a profile is in no run: add a test for the next run"
);
// The numbers of the table are those of the library: a changed number fails here until the table is changed on purpose
for &(id, up, down) in DELAYS {
let profile = library().get(id).unwrap();
Expand Down
31 changes: 30 additions & 1 deletion crates/netscenario-backend-kernel/tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,40 @@ pub(crate) fn assert_nothing_left(run_id: &str, context: &str) {
}
}

/// A directory for the files that programs of a test write.
/// A directory for the files that programs of a test write. It is removed when the thread of the test ends if the test passed;
/// the directory of a test that failed is kept for a look (`NETSCENARIO_KEEP_TEST_DIRS=1` keeps all of them).
pub(crate) fn work_dir(name: &str) -> PathBuf {
use std::cell::{Cell, RefCell};
use std::sync::Once;

struct Made(RefCell<Vec<PathBuf>>);
impl Drop for Made {
fn drop(&mut self) {
if std::env::var_os("NETSCENARIO_KEEP_TEST_DIRS").is_some() || FAILED.with(Cell::get) {
return;
}
for path in self.0.get_mut().drain(..) {
let _ = std::fs::remove_dir_all(path);
}
}
}
thread_local! {
static MADE: Made = const { Made(RefCell::new(Vec::new())) };
static FAILED: Cell<bool> = const { Cell::new(false) };
}
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let before = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
FAILED.with(|failed| failed.set(true));
before(info);
}));
});

let dir = std::env::temp_dir().join(format!("netscenario-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
MADE.with(|made| made.0.borrow_mut().push(dir.clone()));
dir
}

Expand Down
24 changes: 21 additions & 3 deletions crates/netscenario-backend-kernel/tests/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,30 @@ fn a_row_without_a_delivery_is_a_stall_that_the_scenario_must_accept() {
.iter()
.filter(|r| r.receive_ns >= from && r.receive_ns < to)
.count() as i64;
let late = 2.0 * 2000.0 * slack(&result);
let (upper, lower) = (3602.0 + 8.0 + late, 3602.0 - 8.0 - 400.0 - late);
// A step that starts a stall and is applied late lets the link run on at the full rate (a surplus); a step that ends one
// and is late keeps the link stalled (a deficit). Each direction has its own bound from the steps of its own kind, at the
// rate of 2,000 packets a second. (The steps that end a stall were once counted for the surplus too: one 128 ms late, on a
// loaded machine, made the bound useless and the test failed with 3,404 packets arrived, which is the right number.)
let late_at = |offset_ms: u64| -> f64 {
result
.events
.iter()
.filter(|e| e.scheduled_ns >= 1_000_000_000 && e.scheduled_ns < 3_000_000_000)
.filter(|e| e.scheduled_ns % 1_000_000_000 == offset_ms * 1_000_000)
.map(|e| e.applied_ns.saturating_sub(e.scheduled_ns) as f64 / 1e9 + 0.005)
.sum()
};
// The stall of a pass begins at 400 ms and ends at 500 ms
let (surplus, deficit) = (2000.0 * late_at(400), 2000.0 * late_at(500));
let (upper, lower) = (3602.0 + 8.0 + surplus, 3602.0 - 8.0 - 400.0 - deficit);
// Canary: the surplus allowed leaves out the 4,000 of a link that does not stall, so the bound can fail
assert!(
upper < 4000.0,
"the lateness of the steps ({late}) makes the bound useless"
"the lateness of the steps that start a stall ({surplus}) makes the bound useless"
);
assert!(
late_at(400) > 0.0 && late_at(500) > 0.0,
"the steps of the stalls were not found in the events"
);
assert!(
(lower..=upper).contains(&(arrived as f64)),
Expand Down
Loading