vmm: Simplify adding devices to config

To handle that devices are stored in an Option<Vec<T>> and reduce
duplicated code use generic function to add the devices to the the
struct.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-05-04 15:59:47 +00:00 committed by Sebastien Boeuf
parent f237789753
commit 151668b260

View File

@ -1244,6 +1244,14 @@ impl Vm {
Err(Error::ResizeZone) Err(Error::ResizeZone)
} }
fn add_to_config<T>(devices: &mut Option<Vec<T>>, device: T) {
if let Some(devices) = devices {
devices.push(device);
} else {
*devices = Some(vec![device]);
}
}
pub fn add_device(&mut self, mut _device_cfg: DeviceConfig) -> Result<PciDeviceInfo> { pub fn add_device(&mut self, mut _device_cfg: DeviceConfig) -> Result<PciDeviceInfo> {
let pci_device_info = self let pci_device_info = self
.device_manager .device_manager
@ -1256,11 +1264,7 @@ impl Vm {
// ensure the device would be created in case of a reboot. // ensure the device would be created in case of a reboot.
{ {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
if let Some(devices) = config.devices.as_mut() { Self::add_to_config(&mut config.devices, _device_cfg);
devices.push(_device_cfg);
} else {
config.devices = Some(vec![_device_cfg]);
}
} }
self.device_manager self.device_manager
@ -1330,11 +1334,7 @@ impl Vm {
// ensure the device would be created in case of a reboot. // ensure the device would be created in case of a reboot.
{ {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
if let Some(disks) = config.disks.as_mut() { Self::add_to_config(&mut config.disks, _disk_cfg);
disks.push(_disk_cfg);
} else {
config.disks = Some(vec![_disk_cfg]);
}
} }
self.device_manager self.device_manager
@ -1358,11 +1358,7 @@ impl Vm {
// ensure the device would be created in case of a reboot. // ensure the device would be created in case of a reboot.
{ {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
if let Some(fs_config) = config.fs.as_mut() { Self::add_to_config(&mut config.fs, _fs_cfg);
fs_config.push(_fs_cfg);
} else {
config.fs = Some(vec![_fs_cfg]);
}
} }
self.device_manager self.device_manager
@ -1386,11 +1382,7 @@ impl Vm {
// ensure the device would be created in case of a reboot. // ensure the device would be created in case of a reboot.
{ {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
if let Some(pmem) = config.pmem.as_mut() { Self::add_to_config(&mut config.pmem, _pmem_cfg);
pmem.push(_pmem_cfg);
} else {
config.pmem = Some(vec![_pmem_cfg]);
}
} }
self.device_manager self.device_manager
@ -1414,11 +1406,7 @@ impl Vm {
// ensure the device would be created in case of a reboot. // ensure the device would be created in case of a reboot.
{ {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
if let Some(net) = config.net.as_mut() { Self::add_to_config(&mut config.net, _net_cfg);
net.push(_net_cfg);
} else {
config.net = Some(vec![_net_cfg]);
}
} }
self.device_manager self.device_manager