From deaf660a529c6a9c3a8e05d73067602b24ed7430 Mon Sep 17 00:00:00 2001 From: Muminul Islam Date: Tue, 7 Oct 2025 16:46:18 -0700 Subject: [PATCH] vmm: simplify VM creation API Create HypervisorVmConfig early and pass the struct to VM creation API in the vmm crate. Getting rid of multiple conditional parameter. Signed-off-by: Muminul Islam --- vmm/src/lib.rs | 21 +++++++++++++++------ vmm/src/vm.rs | 26 ++------------------------ 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index 7b4fbfe4d..afb4d5215 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -210,6 +210,20 @@ pub enum Error { #[error("Error applying landlock")] ApplyLandlock(#[source] LandlockError), } + +impl From<&VmConfig> for hypervisor::HypervisorVmConfig { + fn from(_value: &VmConfig) -> Self { + hypervisor::HypervisorVmConfig { + #[cfg(feature = "tdx")] + tdx_enabled: _value.platform.as_ref().map(|p| p.tdx).unwrap_or(false), + #[cfg(feature = "sev_snp")] + sev_snp_enabled: _value.is_sev_snp_enabled(), + #[cfg(feature = "sev_snp")] + mem_size: _value.memory.total_size(), + } + } +} + pub type Result = result::Result; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -1006,12 +1020,7 @@ impl Vmm { let vm = Vm::create_hypervisor_vm( self.hypervisor.as_ref(), - #[cfg(feature = "tdx")] - false, - #[cfg(feature = "sev_snp")] - false, - #[cfg(feature = "sev_snp")] - config.lock().unwrap().memory.total_size(), + (&*self.vm_config.as_ref().unwrap().lock().unwrap()).into(), ) .map_err(|e| { MigratableError::MigrateReceive(anyhow!( diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index ada1b3a14..79dca313c 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -1009,21 +1009,9 @@ impl Vm { vm_config.lock().unwrap().is_tdx_enabled() }; - #[cfg(feature = "sev_snp")] - let sev_snp_enabled = if snapshot.is_some() { - false - } else { - vm_config.lock().unwrap().is_sev_snp_enabled() - }; - let vm = Self::create_hypervisor_vm( hypervisor.as_ref(), - #[cfg(feature = "tdx")] - tdx_enabled, - #[cfg(feature = "sev_snp")] - sev_snp_enabled, - #[cfg(feature = "sev_snp")] - vm_config.lock().unwrap().memory.total_size(), + vm_config.as_ref().lock().unwrap().deref().into(), )?; #[cfg(all(feature = "kvm", target_arch = "x86_64"))] @@ -1083,19 +1071,9 @@ impl Vm { pub fn create_hypervisor_vm( hypervisor: &dyn hypervisor::Hypervisor, - #[cfg(feature = "tdx")] tdx_enabled: bool, - #[cfg(feature = "sev_snp")] sev_snp_enabled: bool, - #[cfg(feature = "sev_snp")] mem_size: u64, + config: HypervisorVmConfig, ) -> Result> { hypervisor.check_required_extensions().unwrap(); - let config = HypervisorVmConfig { - #[cfg(feature = "tdx")] - tdx_enabled, - #[cfg(feature = "sev_snp")] - sev_snp_enabled, - #[cfg(feature = "sev_snp")] - mem_size, - }; let vm = hypervisor.create_vm(config).unwrap();