From 16782e8c6d3f5235bcceada0e1d8132860c77a54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Date: Thu, 27 Jan 2022 11:53:43 +0100 Subject: [PATCH] vmm: lib: Do the config validation in the Vmm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of doing the validation of the configuration change as part of the vm, let's do this in the uper layer, in the Vmm. Signed-off-by: Fabiano FidĂȘncio --- vmm/src/lib.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++-- vmm/src/vm.rs | 53 ------------------------------------- 2 files changed, 70 insertions(+), 55 deletions(-) diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index c0daa74ec..385a7ec3c 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -17,8 +17,8 @@ use crate::api::{ VmSendMigrationData, VmmPingResponse, }; use crate::config::{ - DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, RestoreConfig, UserDeviceConfig, - VmConfig, VsockConfig, + add_to_config, DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, RestoreConfig, + UserDeviceConfig, VmConfig, VsockConfig, }; #[cfg(all(feature = "kvm", target_arch = "x86_64"))] use crate::migration::get_vm_snapshot; @@ -639,6 +639,15 @@ impl Vmm { } fn vm_add_device(&mut self, device_cfg: DeviceConfig) -> result::Result, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.devices, device_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + if let Some(ref mut vm) = self.vm { let info = vm.add_device(device_cfg).map_err(|e| { error!("Error when adding new device to the VM: {:?}", e); @@ -654,6 +663,15 @@ impl Vmm { &mut self, device_cfg: UserDeviceConfig, ) -> result::Result, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.user_devices, device_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + if let Some(ref mut vm) = self.vm { let info = vm.add_user_device(device_cfg).map_err(|e| { error!("Error when adding new user device to the VM: {:?}", e); @@ -679,6 +697,15 @@ impl Vmm { } fn vm_add_disk(&mut self, disk_cfg: DiskConfig) -> result::Result, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.disks, disk_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + if let Some(ref mut vm) = self.vm { let info = vm.add_disk(disk_cfg).map_err(|e| { error!("Error when adding new disk to the VM: {:?}", e); @@ -691,6 +718,15 @@ impl Vmm { } fn vm_add_fs(&mut self, fs_cfg: FsConfig) -> result::Result, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.fs, fs_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + if let Some(ref mut vm) = self.vm { let info = vm.add_fs(fs_cfg).map_err(|e| { error!("Error when adding new fs to the VM: {:?}", e); @@ -703,6 +739,15 @@ impl Vmm { } fn vm_add_pmem(&mut self, pmem_cfg: PmemConfig) -> result::Result, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.pmem, pmem_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + if let Some(ref mut vm) = self.vm { let info = vm.add_pmem(pmem_cfg).map_err(|e| { error!("Error when adding new pmem device to the VM: {:?}", e); @@ -715,6 +760,15 @@ impl Vmm { } fn vm_add_net(&mut self, net_cfg: NetConfig) -> result::Result, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + add_to_config(&mut config.net, net_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + if let Some(ref mut vm) = self.vm { let info = vm.add_net(net_cfg).map_err(|e| { error!("Error when adding new network device to the VM: {:?}", e); @@ -727,6 +781,20 @@ impl Vmm { } fn vm_add_vsock(&mut self, vsock_cfg: VsockConfig) -> result::Result, VmError> { + self.vm_config.as_ref().ok_or(VmError::VmNotCreated)?; + + { + // Validate the configuration change in a cloned configuration + let mut config = self.vm_config.as_ref().unwrap().lock().unwrap().clone(); + + if config.vsock.is_some() { + return Err(VmError::TooManyVsockDevices); + } + + config.vsock = Some(vsock_cfg.clone()); + config.validate().map_err(VmError::ConfigValidation)?; + } + if let Some(ref mut vm) = self.vm { let info = vm.add_vsock(vsock_cfg).map_err(|e| { error!("Error when adding new vsock device to the VM: {:?}", e); diff --git a/vmm/src/vm.rs b/vmm/src/vm.rs index 9e4267e22..380f3edbc 100644 --- a/vmm/src/vm.rs +++ b/vmm/src/vm.rs @@ -1391,13 +1391,6 @@ impl Vm { } pub fn add_device(&mut self, mut device_cfg: DeviceConfig) -> Result { - { - // Validate on a clone of the config - let mut config = self.config.lock().unwrap().clone(); - add_to_config(&mut config.devices, device_cfg.clone()); - config.validate().map_err(Error::ConfigValidation)?; - } - let pci_device_info = self .device_manager .lock() @@ -1422,13 +1415,6 @@ impl Vm { } pub fn add_user_device(&mut self, mut device_cfg: UserDeviceConfig) -> Result { - { - // Validate on a clone of the config - let mut config = self.config.lock().unwrap().clone(); - add_to_config(&mut config.user_devices, device_cfg.clone()); - config.validate().map_err(Error::ConfigValidation)?; - } - let pci_device_info = self .device_manager .lock() @@ -1504,13 +1490,6 @@ impl Vm { } pub fn add_disk(&mut self, mut disk_cfg: DiskConfig) -> Result { - { - // Validate on a clone of the config - let mut config = self.config.lock().unwrap().clone(); - add_to_config(&mut config.disks, disk_cfg.clone()); - config.validate().map_err(Error::ConfigValidation)?; - } - let pci_device_info = self .device_manager .lock() @@ -1535,13 +1514,6 @@ impl Vm { } pub fn add_fs(&mut self, mut fs_cfg: FsConfig) -> Result { - { - // Validate on a clone of the config - let mut config = self.config.lock().unwrap().clone(); - add_to_config(&mut config.fs, fs_cfg.clone()); - config.validate().map_err(Error::ConfigValidation)?; - } - let pci_device_info = self .device_manager .lock() @@ -1566,13 +1538,6 @@ impl Vm { } pub fn add_pmem(&mut self, mut pmem_cfg: PmemConfig) -> Result { - { - // Validate on a clone of the config - let mut config = self.config.lock().unwrap().clone(); - add_to_config(&mut config.pmem, pmem_cfg.clone()); - config.validate().map_err(Error::ConfigValidation)?; - } - let pci_device_info = self .device_manager .lock() @@ -1597,13 +1562,6 @@ impl Vm { } pub fn add_net(&mut self, mut net_cfg: NetConfig) -> Result { - { - // Validate on a clone of the config - let mut config = self.config.lock().unwrap().clone(); - add_to_config(&mut config.net, net_cfg.clone()); - config.validate().map_err(Error::ConfigValidation)?; - } - let pci_device_info = self .device_manager .lock() @@ -1628,17 +1586,6 @@ impl Vm { } pub fn add_vsock(&mut self, mut vsock_cfg: VsockConfig) -> Result { - if self.config.lock().unwrap().vsock.is_some() { - return Err(Error::TooManyVsockDevices); - } - - { - // Validate on a clone of the config - let mut config = self.config.lock().unwrap().clone(); - config.vsock = Some(vsock_cfg.clone()); - config.validate().map_err(Error::ConfigValidation)?; - } - let pci_device_info = self .device_manager .lock()