Skip to content

Commit 46bc7c6

Browse files
committed
Add support for starting VMs with passt network
This required adding another field to the config. This is done by migrating the old config to a newer version. We are backwards compatible on configuration but not forwards compatible (older versions of krunvm will not be able to use the config from this version) If we want forward compatibility, I feel like we need to ditch the confy crate. Signed-off-by: Matej Hrica <mhrica@redhat.com>
1 parent 90a8299 commit 46bc7c6

20 files changed

Lines changed: 510 additions & 62 deletions

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ serde = "1.0.120"
1616
serde_derive = "1.0.120"
1717
text_io = "0.1.8"
1818
nix = {version = "0.27.1", features = ["socket", "fs"]}
19+
rand="0.8.5"

build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const COMMANDS: [&str; 7] = [
1212
];
1313

1414
fn main() {
15+
println!("cargo:warning=HELLLOOOOOOO?");
1516
let outdir = match env::var_os("OUT_DIR") {
1617
Some(outdir) => outdir,
1718
None => {
@@ -41,7 +42,7 @@ fn generate_man_page<P: AsRef<Path>>(outdir: P, command: &str) -> io::Result<()>
4142
let outfile = outdir.join(format!("{}.1", command));
4243
let cwd = env::current_dir()?;
4344
let txt_path = cwd.join("docs").join(format!("{}.1.txt", command));
44-
45+
println!("cargo:warning=Ascii doctor target: {} from {}", outfile.display(), txt_path.display());
4546
let result = process::Command::new("asciidoctor")
4647
.arg("--doctype")
4748
.arg("manpage")

docs/krunvm-changevm.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ host visible in the guest.
6161
An empty string ("") tells krunvm to not set a working directory
6262
explicitly, letting libkrun decide which one should be set.
6363

64+
*--net* _NETWORK_MODE_::
65+
Configures the network connection mode. Supported modes are either PASST or TSI.
6466

6567
SEE ALSO
6668
--------

docs/krunvm-config.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ OPTIONS
3434
Sets the default mount of RAM, in MiB, that will be configured for
3535
newly created microVMs.
3636

37+
*--net* _NETWORK_MODE_::
38+
Sets the default network connection mode, that will be configured for
39+
newly created microVMs. Supported modes are PASST or TSI.
3740

3841
SEE ALSO
3942
--------

docs/krunvm-create.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ host visible in the guest.
5353
An empty string ("") tells krunvm to not set a working directory
5454
explicitly, letting libkrun decide which one should be set.
5555

56+
*--net* _NETWORK_MODE_::
57+
Set the network connection mode. Supported modes are either PASST or TSI.
5658

5759
SEE ALSO
5860
--------

docs/krunvm.1.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ microVM and exposing ports from the guest to the host (and the
2929
networks connected to it).
3030

3131
Networking to the guest running in the microVM is provided by
32-
libkrun's TSI (Transparent Socket Impersonation), enabling a seamless
33-
experience that doesn't require network bridges nor other explicit
34-
network configuration.
32+
either libkrun's TSI (Transparent Socket Impersonation) or PASST.
3533

34+
TSI enables a seamless experience that doesn't require network bridges nor other explicit
35+
network configuration. It only supports impersonating AF_INET SOCK_DGRAM and SOCK_STREAM sockets.
36+
This implies it's not possible to communicate outside the VM with raw sockets.
37+
38+
PASST uses virtio-net guest device and sends all traffic to a passt subprocess.
39+
Support of network protocols is therefore dependent on what passt supports.
40+
Note that currently you need to run a DHCP client in the guest to get an IP address.
3641

3742
GLOBAL OPTIONS
3843
--------------

src/bindings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern "C" {
1313
pub fn krun_set_mapped_volumes(ctx: u32, mapped_volumes: *const *const c_char) -> i32;
1414
pub fn krun_set_port_map(ctx: u32, port_map: *const *const c_char) -> i32;
1515
pub fn krun_set_workdir(ctx: u32, workdir_path: *const c_char) -> i32;
16+
pub fn krun_set_passt_fd(ctx: u32, fd: c_int) -> i32;
1617
pub fn krun_set_exec(
1718
ctx: u32,
1819
exec_path: *const c_char,

src/commands/changevm.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
use clap::Args;
55
use std::collections::HashMap;
66

7+
use crate::config::{KrunvmConfig, NetworkMode};
78
use crate::utils::{path_pairs_to_hash_map, port_pairs_to_hash_map, PathPair, PortPair};
8-
use crate::{KrunvmConfig, APP_NAME};
99

1010
use super::list::printvm;
1111

12-
/// Change the configuration of a microVM
12+
/// Change the config of a microVM
1313
#[derive(Args, Debug)]
1414
pub struct ChangeVmCmd {
1515
/// Name of the VM to be modified
@@ -46,6 +46,10 @@ pub struct ChangeVmCmd {
4646
/// Port(s) in format "host_port:guest_port" to be exposed to the host
4747
#[arg(long = "port")]
4848
ports: Vec<PortPair>,
49+
50+
/// Set the network connection mode for the microVM
51+
#[arg(long)]
52+
net: Option<NetworkMode>,
4953
}
5054

5155
impl ChangeVmCmd {
@@ -130,12 +134,17 @@ impl ChangeVmCmd {
130134
cfg_changed = true;
131135
}
132136

137+
if let Some(network_mode) = self.net {
138+
vmcfg.network_mode = network_mode;
139+
cfg_changed = true;
140+
}
141+
133142
println!();
134143
printvm(vmcfg);
135144
println!();
136145

137146
if cfg_changed {
138-
confy::store(APP_NAME, &cfg).unwrap();
147+
crate::config::save(cfg).unwrap();
139148
}
140149
}
141150
}

src/commands/config.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2021 Red Hat, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::{KrunvmConfig, APP_NAME};
4+
use crate::config::{KrunvmConfig, NetworkMode};
55
use clap::Args;
66

77
/// Configure global values
@@ -18,6 +18,10 @@ pub struct ConfigCmd {
1818
/// DNS server to use in the microVM
1919
#[arg(long)]
2020
dns: Option<String>,
21+
22+
/// Default network connection mode to use
23+
#[arg(long)]
24+
net: Option<NetworkMode>,
2125
}
2226

2327
impl ConfigCmd {
@@ -47,11 +51,18 @@ impl ConfigCmd {
4751
cfg_changed = true;
4852
}
4953

54+
if let Some(network_mode) = self.net {
55+
if network_mode != cfg.default_network_mode {
56+
cfg.default_network_mode = network_mode;
57+
cfg_changed = true;
58+
}
59+
}
60+
5061
if cfg_changed {
51-
confy::store(APP_NAME, &cfg).unwrap();
62+
crate::config::save(cfg).unwrap();
5263
}
5364

54-
println!("Global configuration:");
65+
println!("Global config:");
5566
println!(
5667
"Default number of CPUs for newly created VMs: {}",
5768
cfg.default_cpus

0 commit comments

Comments
 (0)