Skip to content

Commit 941f1d9

Browse files
mtjhrcslp
authored andcommitted
Conditionally compile virtio/net only when feature 'net' is enabled
The feature is disabled by default. You can set enviroment variable NET=1 when compiling using the Makefile to enable it. Signed-off-by: Matej Hrica <[email protected]>
1 parent 7ad949e commit 941f1d9

File tree

9 files changed

+42
-8
lines changed

9 files changed

+42
-8
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ ifeq ($(SEV),1)
2323
INIT_DEFS += $(SEV_LD_FLAGS)
2424
INIT_SRC += $(SNP_INIT_SRC)
2525
endif
26+
ifeq ($(NET),1)
27+
FEATURE_FLAGS += --features net
28+
endif
2629

2730
ifeq ($(ROSETTA),1)
2831
INIT_DEFS += -D__ROSETTA__

src/devices/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
[features]
88
tee = []
99
amd-sev = ["tee"]
10+
net = []
1011

1112
[dependencies]
1213
bitflags = "1.2.0"

src/devices/src/virtio/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod fs;
2121
#[cfg(target_os = "macos")]
2222
pub mod linux_errno;
2323
mod mmio;
24+
#[cfg(feature = "net")]
2425
pub mod net;
2526
mod queue;
2627
#[cfg(not(feature = "tee"))]
@@ -36,6 +37,7 @@ pub use self::device::*;
3637
#[cfg(not(feature = "tee"))]
3738
pub use self::fs::*;
3839
pub use self::mmio::*;
40+
#[cfg(feature = "net")]
3941
pub use self::net::*;
4042
pub use self::queue::*;
4143
#[cfg(not(feature = "tee"))]

src/libkrun/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ build = "build.rs"
88
[features]
99
tee = []
1010
amd-sev = [ "tee" ]
11+
net = []
1112

1213
[dependencies]
1314
env_logger = "0.9.0"

src/libkrun/src/lib.rs

Lines changed: 23 additions & 7 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+
#[cfg(feature = "net")]
1112
use std::os::fd::RawFd;
1213
#[cfg(not(feature = "tee"))]
1314
use std::path::Path;
@@ -33,6 +34,7 @@ use vmm::vmm_config::kernel_bundle::KernelBundle;
3334
#[cfg(feature = "tee")]
3435
use vmm::vmm_config::kernel_bundle::{InitrdBundle, QbootBundle};
3536
use vmm::vmm_config::machine_config::VmConfig;
37+
#[cfg(feature = "net")]
3638
use vmm::vmm_config::net::NetworkInterfaceConfig;
3739
use vmm::vmm_config::vsock::VsockDeviceConfig;
3840

@@ -51,12 +53,14 @@ struct TsiConfig {
5153
port_map: Option<HashMap<u16, u16>>,
5254
}
5355

56+
#[cfg(feature = "net")]
5457
struct PasstConfig {
5558
fd: RawFd,
5659
}
5760

5861
enum NetworkConfig {
5962
Tsi(TsiConfig),
63+
#[cfg(feature = "net")]
6064
Passt(PasstConfig),
6165
}
6266

@@ -172,6 +176,7 @@ impl ContextConfig {
172176
self.data_block_cfg.clone()
173177
}
174178

179+
#[cfg(feature = "net")]
175180
fn set_net_cfg(&mut self, net_cfg: NetworkConfig) {
176181
self.net_cfg = net_cfg;
177182
}
@@ -182,6 +187,7 @@ impl ContextConfig {
182187
tsi_config.port_map.replace(new_port_map);
183188
Ok(())
184189
}
190+
#[cfg(feature = "net")]
185191
NetworkConfig::Passt(_) => Err(()),
186192
}
187193
}
@@ -494,15 +500,24 @@ pub unsafe extern "C" fn krun_set_passt_fd(ctx_id: u32, fd: c_int) -> i32 {
494500
return -libc::EINVAL;
495501
}
496502

497-
match CTX_MAP.lock().unwrap().entry(ctx_id) {
498-
Entry::Occupied(mut ctx_cfg) => {
499-
let cfg = ctx_cfg.get_mut();
500-
cfg.set_net_cfg(NetworkConfig::Passt(PasstConfig { fd }));
501-
}
502-
Entry::Vacant(_) => return -libc::ENOENT,
503+
#[cfg(not(feature = "net"))]
504+
{
505+
let _ = ctx_id;
506+
let _ = fd;
507+
-libc::ENOTSUP
503508
}
504509

505-
KRUN_SUCCESS
510+
#[cfg(feature = "net")]
511+
{
512+
match CTX_MAP.lock().unwrap().entry(ctx_id) {
513+
Entry::Occupied(mut ctx_cfg) => {
514+
let cfg = ctx_cfg.get_mut();
515+
cfg.set_net_cfg(NetworkConfig::Passt(PasstConfig { fd }));
516+
}
517+
Entry::Vacant(_) => return -libc::ENOENT,
518+
}
519+
KRUN_SUCCESS
520+
}
506521
}
507522

508523
#[allow(clippy::missing_safety_doc)]
@@ -820,6 +835,7 @@ pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {
820835
};
821836
ctx_cfg.vmr.set_vsock_device(vsock_device_config).unwrap();
822837
}
838+
#[cfg(feature = "net")]
823839
NetworkConfig::Passt(passt_cfg) => {
824840
let network_interface_config = NetworkInterfaceConfig {
825841
iface_id: "eth0".to_string(),

src/vmm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
[features]
88
tee = []
99
amd-sev = [ "tee", "codicon", "kbs-types", "procfs", "serde", "serde_json", "sev", "curl" ]
10+
net = []
1011

1112
[dependencies]
1213
crossbeam-channel = "0.5"

src/vmm/src/builder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ use crate::device_manager::legacy::PortIODeviceManager;
1717
use crate::device_manager::mmio::MMIODeviceManager;
1818
use devices::legacy::Gic;
1919
use devices::legacy::Serial;
20+
#[cfg(feature = "net")]
21+
use devices::virtio::Net;
2022
#[cfg(not(feature = "tee"))]
2123
use devices::virtio::VirtioShmRegion;
22-
use devices::virtio::{MmioTransport, Net, Vsock};
24+
use devices::virtio::{MmioTransport, Vsock};
2325

2426
#[cfg(feature = "tee")]
2527
use kbs_types::Tee;
@@ -586,6 +588,8 @@ pub fn build_microvm(
586588
attach_unixsock_vsock_device(&mut vmm, vsock, event_manager, intc)?;
587589
vmm.kernel_cmdline.insert_str("tsi_hijack")?;
588590
}
591+
592+
#[cfg(feature = "net")]
589593
attach_net_devices(&mut vmm, vm_resources.net_builder.iter(), event_manager)?;
590594

591595
if let Some(s) = &vm_resources.boot_config.kernel_cmdline_epilog {
@@ -1115,6 +1119,7 @@ fn attach_console_devices(
11151119
Ok(())
11161120
}
11171121

1122+
#[cfg(feature = "net")]
11181123
fn attach_net_devices<'a>(
11191124
vmm: &mut Vmm,
11201125
net_devices: impl Iterator<Item = &'a Arc<Mutex<Net>>>,

src/vmm/src/resources.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::vmm_config::fs::*;
2525
use crate::vmm_config::kernel_bundle::{InitrdBundle, QbootBundle, QbootBundleError};
2626
use crate::vmm_config::kernel_bundle::{KernelBundle, KernelBundleError};
2727
use crate::vmm_config::machine_config::{VmConfig, VmConfigError};
28+
#[cfg(feature = "net")]
2829
use crate::vmm_config::net::{NetBuilder, NetworkInterfaceConfig, NetworkInterfaceError};
2930
use crate::vmm_config::vsock::*;
3031
use crate::vstate::VcpuConfig;
@@ -103,6 +104,7 @@ pub struct VmResources {
103104
#[cfg(feature = "tee")]
104105
pub block: BlockBuilder,
105106
/// The network devices builder.
107+
#[cfg(feature = "net")]
106108
pub net_builder: NetBuilder,
107109
/// TEE configuration
108110
#[cfg(feature = "tee")]
@@ -241,6 +243,7 @@ impl VmResources {
241243
}
242244

243245
/// Sets a network device to be attached when the VM starts.
246+
#[cfg(feature = "net")]
244247
pub fn add_network_interface(
245248
&mut self,
246249
config: NetworkInterfaceConfig,
@@ -299,6 +302,7 @@ mod tests {
299302
kernel_bundle: Default::default(),
300303
fs: Default::default(),
301304
vsock: Default::default(),
305+
#[cfg(feature = "net")]
302306
net_builder: Default::default(),
303307
}
304308
}

src/vmm/src/vmm_config/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ pub mod machine_config;
2525
pub mod vsock;
2626

2727
/// Wrapper for configuring the network devices attached to the microVM.
28+
#[cfg(feature = "net")]
2829
pub mod net;

0 commit comments

Comments
 (0)