Skip to content

Commit 636ea0a

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 636ea0a

18 files changed

Lines changed: 433 additions & 59 deletions

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ libc = "0.2.82"
1515
serde = "1.0.120"
1616
serde_derive = "1.0.120"
1717
text_io = "0.1.8"
18-
nix = {version = "0.27.1", features = ["socket", "fs"]}
18+
nix = {version = "0.27.1", features = ["socket", "fs"]}

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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
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

@@ -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

src/commands/create.rs

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

4+
use crate::config::{KrunvmConfig, NetworkMode, VmConfig};
5+
use crate::APP_NAME;
46
use clap::Args;
57
use std::fs;
68
use std::io::Write;
@@ -12,8 +14,6 @@ use crate::utils::{
1214
get_buildah_args, mount_container, path_pairs_to_hash_map, port_pairs_to_hash_map,
1315
umount_container, BuildahCommand, PathPair, PortPair,
1416
};
15-
use crate::{KrunvmConfig, VmConfig, APP_NAME};
16-
1717
#[cfg(target_os = "macos")]
1818
const KRUNVM_ROSETTA_FILE: &str = ".krunvm-rosetta";
1919

@@ -51,6 +51,10 @@ pub struct CreateCmd {
5151
#[arg(long = "port")]
5252
ports: Vec<PortPair>,
5353

54+
/// Network connection mode to use
55+
#[arg(long)]
56+
net: Option<NetworkMode>,
57+
5458
/// Create a x86_64 microVM even on an Aarch64 host
5559
#[arg(short, long)]
5660
#[cfg(target_os = "macos")]
@@ -68,6 +72,7 @@ impl CreateCmd {
6872
let mapped_ports = port_pairs_to_hash_map(self.ports);
6973
let image = self.image;
7074
let name = self.name;
75+
let network_mode = self.net.unwrap_or_else(|| cfg.default_network_mode.clone());
7176

7277
if let Some(ref name) = name {
7378
if cfg.vmconfig_map.contains_key(name) {
@@ -160,6 +165,7 @@ https://threedots.ovh/blog/2022/06/quick-look-at-rosetta-on-linux/
160165
workdir: workdir.to_string(),
161166
mapped_volumes,
162167
mapped_ports,
168+
network_mode,
163169
};
164170

165171
let rootfs = mount_container(cfg, &vmcfg).unwrap();

src/commands/delete.rs

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

4-
use crate::{KrunvmConfig, APP_NAME};
4+
use crate::config;
5+
use crate::config::KrunvmConfig;
56
use clap::Args;
67

78
use crate::utils::{remove_container, umount_container};
@@ -26,6 +27,6 @@ impl DeleteCmd {
2627
umount_container(cfg, &vmcfg).unwrap();
2728
remove_container(cfg, &vmcfg).unwrap();
2829

29-
confy::store(APP_NAME, &cfg).unwrap();
30+
config::save(cfg).unwrap()
3031
}
3132
}

0 commit comments

Comments
 (0)