From ff9c8b847f5315c5b39b9317d1afec2ed596f997 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 27 Apr 2020 10:37:56 +0200 Subject: [PATCH] vmm: Always generate the next device name Even in the context of "mmio" feature, we need the next device name to be generated as we need to identify virtio-mmio devices to support snapshot and restore functionalities. Signed-off-by: Sebastien Boeuf --- vmm/src/device_manager.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index b09945a27..4fe9e6b80 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -40,7 +40,6 @@ use std::any::Any; use std::collections::HashMap; use std::fs::{File, OpenOptions}; use std::io::{self, sink, stdout, Seek, SeekFrom}; -#[cfg(feature = "pci_support")] use std::num::Wrapping; use std::os::unix::fs::OpenOptionsExt; use std::path::PathBuf; @@ -582,6 +581,9 @@ pub struct DeviceManager { // Backends that have been spawned vhost_user_backends: Vec, + // Counter to keep track of the consumed device IDs. + device_id_cnt: Wrapping, + // Keep a reference to the PCI bus #[cfg(feature = "pci_support")] pci_bus: Option>>, @@ -610,10 +612,6 @@ pub struct DeviceManager { #[cfg(feature = "pci_support")] pci_id_list: HashMap, - // Counter to keep track of the consumed device IDs. - #[cfg(feature = "pci_support")] - device_id_cnt: Wrapping, - // Hashmap of PCI b/d/f to their corresponding Arc>. #[cfg(feature = "pci_support")] pci_devices: HashMap>, @@ -702,6 +700,7 @@ impl DeviceManager { bus_devices, vmm_path, vhost_user_backends: Vec::new(), + device_id_cnt: Wrapping(0), #[cfg(feature = "pci_support")] pci_bus: None, #[cfg(feature = "pci_support")] @@ -717,8 +716,6 @@ impl DeviceManager { #[cfg(feature = "pci_support")] pci_id_list: HashMap::new(), #[cfg(feature = "pci_support")] - device_id_cnt: Wrapping(0), - #[cfg(feature = "pci_support")] pci_devices: HashMap::new(), }; @@ -1185,7 +1182,7 @@ impl DeviceManager { disk_cfg: &mut DiskConfig, ) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option)> { if disk_cfg.id.is_none() { - disk_cfg.id = self.next_device_name(DISK_DEVICE_NAME_PREFIX)?; + disk_cfg.id = Some(self.next_device_name(DISK_DEVICE_NAME_PREFIX)?); } if disk_cfg.vhost_user { @@ -1338,7 +1335,7 @@ impl DeviceManager { net_cfg: &mut NetConfig, ) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option)> { if net_cfg.id.is_none() { - net_cfg.id = self.next_device_name(NET_DEVICE_NAME_PREFIX)?; + net_cfg.id = Some(self.next_device_name(NET_DEVICE_NAME_PREFIX)?); } if net_cfg.vhost_user { @@ -1450,7 +1447,7 @@ impl DeviceManager { fs_cfg: &mut FsConfig, ) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option)> { if fs_cfg.id.is_none() { - fs_cfg.id = self.next_device_name(FS_DEVICE_NAME_PREFIX)?; + fs_cfg.id = Some(self.next_device_name(FS_DEVICE_NAME_PREFIX)?); } if let Some(fs_sock) = fs_cfg.sock.to_str() { @@ -1552,7 +1549,7 @@ impl DeviceManager { pmem_cfg: &mut PmemConfig, ) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option)> { if pmem_cfg.id.is_none() { - pmem_cfg.id = self.next_device_name(PMEM_DEVICE_NAME_PREFIX)?; + pmem_cfg.id = Some(self.next_device_name(PMEM_DEVICE_NAME_PREFIX)?); } let (custom_flags, set_len) = if pmem_cfg.file.is_dir() { @@ -1673,7 +1670,7 @@ impl DeviceManager { vsock_cfg: &mut VsockConfig, ) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option)> { if vsock_cfg.id.is_none() { - vsock_cfg.id = self.next_device_name(VSOCK_DEVICE_NAME_PREFIX)?; + vsock_cfg.id = Some(self.next_device_name(VSOCK_DEVICE_NAME_PREFIX)?); } let socket_path = vsock_cfg @@ -1758,12 +1755,17 @@ impl DeviceManager { } #[cfg(not(feature = "pci_support"))] - fn next_device_name(&mut self, _prefix: &str) -> DeviceManagerResult> { - Ok(None) + fn next_device_name(&mut self, prefix: &str) -> DeviceManagerResult { + // Generate the temporary name. + let name = format!("{}{}", prefix, self.device_id_cnt); + // Increment the counter. + self.device_id_cnt += Wrapping(1); + + Ok(name) } #[cfg(feature = "pci_support")] - fn next_device_name(&mut self, prefix: &str) -> DeviceManagerResult> { + fn next_device_name(&mut self, prefix: &str) -> DeviceManagerResult { let start_id = self.device_id_cnt; loop { // Generate the temporary name. @@ -1772,7 +1774,7 @@ impl DeviceManager { self.device_id_cnt += Wrapping(1); // Check if the name is already in use. if !self.pci_id_list.contains_key(&name) { - return Ok(Some(name)); + return Ok(name); } if self.device_id_cnt == start_id { @@ -1868,8 +1870,8 @@ impl DeviceManager { id.clone() } else { let id = self.next_device_name(VFIO_DEVICE_NAME_PREFIX)?; - device_cfg.id = id.clone(); - id.unwrap() + device_cfg.id = Some(id.clone()); + id }; self.pci_id_list.insert(vfio_name, pci_device_bdf);