Skip to content

Commit e79250b

Browse files
committed
Autoconfigure network without DHCP
Passt includes a DHCP server to allow guests to easily configure the network without being aware passt is on the other side. But we _are_ aware of that, so we can take advantage of it. Instead of using DHCP, read the configuration output from passt, marshall it into some environment variables, pass it to the guest, and have krun-guest read it and apply it using rtnetlink. By doing that, we reduce the startup time in half, from... $ time krun /bin/false (...) real 0m4,301s ... to ... $ time krun /bin/false (...) real 0m1,966s Signed-off-by: Sergio Lopez <[email protected]>
1 parent f8cebac commit e79250b

6 files changed

Lines changed: 304 additions & 80 deletions

File tree

Cargo.lock

Lines changed: 224 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/krun/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ anyhow = { version = "1.0.82", default-features = false, features = ["std"] }
1313
bpaf = { version = "0.9.11", default-features = false, features = [] }
1414
byteorder = { version = "1.5.0", default-features = false, features = ["std"] }
1515
env_logger = { version = "0.11.3", default-features = false, features = ["auto-color", "humantime", "unstable-kv"] }
16+
futures-util = "0.3.30"
1617
krun-sys = { path = "../krun-sys", version = "1.9.1", default-features = false, features = [] }
1718
log = { version = "0.4.21", default-features = false, features = ["kv"] }
1819
nix = { version = "0.28.0", default-features = false, features = ["user"] }
20+
regex = { version = "1.10.6" }
1921
rustix = { version = "0.38.34", default-features = false, features = ["fs", "mount", "process", "std", "system", "use-libc-auxv"] }
22+
rtnetlink = { version = "0.14.1" }
2023
serde = { version = "1.0.203", default-features = false, features = ["derive"] }
2124
serde_json = { version = "1.0.117", default-features = false, features = ["std"] }
2225
tempfile = { version = "3.10.1", default-features = false, features = [] }

crates/krun/src/bin/krun.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,19 @@ fn main() -> Result<()> {
187187
}
188188
}
189189

190+
let mut env = prepare_env_vars(env).context("Failed to prepare environment variables")?;
191+
env.insert(
192+
"KRUN_SERVER_PORT".to_owned(),
193+
options.server_port.to_string(),
194+
);
195+
190196
{
191197
let passt_fd: OwnedFd = if let Some(passt_socket) = options.passt_socket {
192198
connect_to_passt(passt_socket)
193199
.context("Failed to connect to `passt`")?
194200
.into()
195201
} else {
196-
start_passt(options.server_port)
202+
start_passt(options.server_port, &mut env)
197203
.context("Failed to start `passt`")?
198204
.into()
199205
};
@@ -330,11 +336,6 @@ fn main() -> Result<()> {
330336
vec
331337
};
332338

333-
let mut env = prepare_env_vars(env).context("Failed to prepare environment variables")?;
334-
env.insert(
335-
"KRUN_SERVER_PORT".to_owned(),
336-
options.server_port.to_string(),
337-
);
338339
let env: Vec<CString> = {
339340
let mut vec = Vec::with_capacity(env.len());
340341
for (key, value) in env {

crates/krun/src/guest/bin/krun-guest.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ fn main() -> Result<()> {
4242

4343
setup_fex()?;
4444

45-
configure_network()?;
45+
let rt = tokio::runtime::Runtime::new().unwrap();
46+
rt.block_on(async {
47+
if let Err(err) = configure_network().await {
48+
eprintln!("Failed to configure network, continuing without it: {err}");
49+
}
50+
});
4651

4752
if let Some(hidpipe_client_path) = find_in_path("hidpipe-client")? {
4853
Command::new(hidpipe_client_path)

0 commit comments

Comments
 (0)