Skip to content

Commit 32d7633

Browse files
mtjhrcslp
authored andcommitted
[2/4] Change network config representation in ContextConfig
This enables implementing krun_set_passt_fd, but note that the actual virtio-net device using passt will be implemented in the next commits. Signed-off-by: Matej Hrica <[email protected]>
1 parent 640e377 commit 32d7633

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

Cargo.lock

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

src/libkrun/src/lib.rs

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::env;
88
use std::ffi::CStr;
99
#[cfg(target_os = "linux")]
1010
use std::ffi::CString;
11+
use std::os::fd::RawFd;
1112
#[cfg(not(feature = "tee"))]
1213
use std::path::Path;
1314
#[cfg(feature = "tee")]
@@ -44,6 +45,27 @@ const MAX_ARGS: usize = 4096;
4445
// Path to the init binary to be executed inside the VM.
4546
const INIT_PATH: &str = "/init.krun";
4647

48+
#[derive(Default)]
49+
struct TsiConfig {
50+
port_map: Option<HashMap<u16, u16>>,
51+
}
52+
53+
struct PasstConfig {
54+
fd: RawFd,
55+
}
56+
57+
enum NetworkConfig {
58+
Tsi(TsiConfig),
59+
Passt(PasstConfig),
60+
}
61+
62+
impl Default for NetworkConfig {
63+
/// Default network mode is TSI, for backwards compatibility
64+
fn default() -> Self {
65+
NetworkConfig::Tsi(Default::default())
66+
}
67+
}
68+
4769
#[derive(Default)]
4870
struct ContextConfig {
4971
vmr: VmResources,
@@ -52,13 +74,13 @@ struct ContextConfig {
5274
env: Option<String>,
5375
args: Option<String>,
5476
rlimits: Option<String>,
77+
net_cfg: NetworkConfig,
5578
#[cfg(not(feature = "tee"))]
5679
fs_cfg: Option<FsDeviceConfig>,
5780
#[cfg(feature = "tee")]
5881
root_block_cfg: Option<BlockDeviceConfig>,
5982
#[cfg(feature = "tee")]
6083
data_block_cfg: Option<BlockDeviceConfig>,
61-
port_map: Option<HashMap<u16, u16>>,
6284
#[cfg(feature = "tee")]
6385
tee_config_file: Option<PathBuf>,
6486
}
@@ -149,12 +171,18 @@ impl ContextConfig {
149171
self.data_block_cfg.clone()
150172
}
151173

152-
fn set_port_map(&mut self, port_map: HashMap<u16, u16>) {
153-
self.port_map = Some(port_map);
174+
fn set_net_cfg(&mut self, net_cfg: NetworkConfig) {
175+
self.net_cfg = net_cfg;
154176
}
155177

156-
fn get_port_map(&self) -> Option<HashMap<u16, u16>> {
157-
self.port_map.clone()
178+
fn set_port_map(&mut self, new_port_map: HashMap<u16, u16>) -> Result<(), ()> {
179+
match &mut self.net_cfg {
180+
NetworkConfig::Tsi(tsi_config) => {
181+
tsi_config.port_map.replace(new_port_map);
182+
Ok(())
183+
}
184+
NetworkConfig::Passt(_) => Err(()),
185+
}
158186
}
159187

160188
#[cfg(feature = "tee")]
@@ -461,7 +489,19 @@ pub unsafe extern "C" fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_c
461489
#[allow(clippy::missing_safety_doc)]
462490
#[no_mangle]
463491
pub unsafe extern "C" fn krun_set_passt_fd(ctx_id: u32, fd: c_int) -> i32 {
464-
todo!("krun_set_passt_fd({},{})", ctx_id, fd);
492+
if fd < 0 {
493+
return -libc::EINVAL;
494+
}
495+
496+
match CTX_MAP.lock().unwrap().entry(ctx_id) {
497+
Entry::Occupied(mut ctx_cfg) => {
498+
let cfg = ctx_cfg.get_mut();
499+
cfg.set_net_cfg(NetworkConfig::Passt(PasstConfig { fd }));
500+
}
501+
Entry::Vacant(_) => return -libc::ENOENT,
502+
}
503+
504+
KRUN_SUCCESS
465505
}
466506

467507
#[allow(clippy::missing_safety_doc)]
@@ -505,7 +545,9 @@ pub unsafe extern "C" fn krun_set_port_map(ctx_id: u32, c_port_map: *const *cons
505545
match CTX_MAP.lock().unwrap().entry(ctx_id) {
506546
Entry::Occupied(mut ctx_cfg) => {
507547
let cfg = ctx_cfg.get_mut();
508-
cfg.set_port_map(port_map);
548+
if cfg.set_port_map(port_map).is_err() {
549+
return -libc::ENOTSUP;
550+
}
509551
}
510552
Entry::Vacant(_) => return -libc::ENOENT,
511553
}
@@ -768,12 +810,19 @@ pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {
768810
return -libc::EINVAL;
769811
}
770812

771-
let vsock_device_config = VsockDeviceConfig {
772-
vsock_id: "vsock0".to_string(),
773-
guest_cid: 3,
774-
host_port_map: ctx_cfg.get_port_map(),
775-
};
776-
ctx_cfg.vmr.set_vsock_device(vsock_device_config).unwrap();
813+
match ctx_cfg.net_cfg {
814+
NetworkConfig::Tsi(tsi_cfg) => {
815+
let vsock_device_config = VsockDeviceConfig {
816+
vsock_id: "vsock0".to_string(),
817+
guest_cid: 3,
818+
host_port_map: tsi_cfg.port_map,
819+
};
820+
ctx_cfg.vmr.set_vsock_device(vsock_device_config).unwrap();
821+
}
822+
NetworkConfig::Passt(passt_cfg) => {
823+
todo!("Connect to fd {} and implement networking", passt_cfg.fd)
824+
}
825+
}
777826

778827
let _vmm = match vmm::builder::build_microvm(&ctx_cfg.vmr, &mut event_manager) {
779828
Ok(vmm) => vmm,

0 commit comments

Comments
 (0)