Skip to content

Commit 4798eed

Browse files
committed
chore(sim,linux): Apply network emulation config also to host environment to be consistent with MacOS behavior
1 parent a9827d2 commit 4798eed

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

msg-sim/src/lib.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub mod assert;
1414
use assert::assert_status;
1515

1616
#[derive(Debug)]
17-
#[allow(unused)]
1817
pub struct SimulationConfig {
1918
/// The latency of the connection.
2019
pub latency: Option<Duration>,
@@ -291,9 +290,19 @@ impl Simulation {
291290

292291
assert_status(
293292
status,
294-
"Failed to set delay and loss network emulation parameters",
293+
"Failed to set delay and loss network emulation parameters to namespaced device",
295294
)?;
296295

296+
// Add delay to the host veth device to match MacOS symmetric behaviour
297+
let status = Command::new("sudo")
298+
.args([
299+
"tc", "qdisc", "add", "dev", &veth_host, "root", "handle", "1:", "netem", "delay",
300+
&delay,
301+
])
302+
.status()?;
303+
304+
assert_status(status, "Failed to set delay to the host veth device")?;
305+
297306
// Add bandwidth paramteres on namespaced veth device
298307
//
299308
// The behaviour is specified on top of the root queue discipline,
@@ -328,9 +337,24 @@ impl Simulation {
328337
])
329338
.status()?;
330339

331-
assert_status(status, "Failed to set bandwidth parameter")?;
332-
}
340+
assert_status(
341+
status,
342+
"Failed to set bandwidth parameter to the namespaced device",
343+
)?;
333344

345+
// Add bandwidth paramteres on host veth device
346+
let status = Command::new("sudo")
347+
.args([
348+
"tc", "qdisc", "add", "dev", &veth_host, "parent", "1:", "handle", "2:", "tbf",
349+
"rate", &bandwidth, "burst", &burst, "limit", &limit,
350+
])
351+
.status()?;
352+
353+
assert_status(
354+
status,
355+
"Failed to set bandwidth parameter to the host veth device",
356+
)?;
357+
}
334358
Ok(())
335359
}
336360

@@ -372,13 +396,14 @@ impl Drop for Simulation {
372396
#[cfg(target_os = "linux")]
373397
fn drop(&mut self) {
374398
let veth_host = self.veth_host_name();
375-
// Deleting the network namespace where the simulated endpoint lives
376-
// drops everything in there
377399
let network_namespace = self.namespace_name();
378400

401+
// The only thing we have to do in the host to delete the veth device
379402
let _ = Command::new("sudo")
380403
.args(["ip", "link", "del", &veth_host])
381404
.status();
405+
// Deleting the network namespace where the simulated endpoint lives
406+
// drops everything in there
382407
let _ = Command::new("sudo")
383408
.args(["ip", "netns", "del", &network_namespace])
384409
.status();
@@ -417,6 +442,9 @@ mod test {
417442
};
418443

419444
let res = simulator.start(addr, config);
445+
if let Err(e) = &res {
446+
eprintln!("{}", e);
447+
}
420448
assert!(res.is_ok());
421449
}
422450
}

0 commit comments

Comments
 (0)