From 06487131f949b01be6d462971e0d491b27855469 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 27 Apr 2020 16:19:40 +0200 Subject: [PATCH] vm-virtio: pci: Expect an identifier upon device creation This identifier is chosen from the DeviceManager so that it will manage all identifiers across the VM, which will ensure uniqueness. It is based off the name from the virtio device attached to this transport layer. Signed-off-by: Sebastien Boeuf --- vm-virtio/src/transport/pci_device.rs | 10 +++++++++- vmm/src/device_manager.rs | 18 +++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/vm-virtio/src/transport/pci_device.rs b/vm-virtio/src/transport/pci_device.rs index 8f2a56155..ad323122e 100755 --- a/vm-virtio/src/transport/pci_device.rs +++ b/vm-virtio/src/transport/pci_device.rs @@ -259,6 +259,8 @@ const VIRTIO_PCI_VENDOR_ID: u16 = 0x1af4; const VIRTIO_PCI_DEVICE_ID_BASE: u16 = 0x1040; // Add to device type to get device ID. pub struct VirtioPciDevice { + id: String, + // PCI configuration registers. configuration: PciConfiguration, @@ -308,6 +310,7 @@ pub struct VirtioPciDevice { impl VirtioPciDevice { /// Constructs a new PCI transport for the given virtio device. pub fn new( + id: String, memory: GuestMemoryAtomic, device: Arc>, msix_num: u16, @@ -384,6 +387,7 @@ impl VirtioPciDevice { ); let mut virtio_pci_device = VirtioPciDevice { + id, configuration, common_config: VirtioPciCommonConfig { driver_status: 0, @@ -962,6 +966,10 @@ impl Pausable for VirtioPciDevice { } } -impl Snapshottable for VirtioPciDevice {} +impl Snapshottable for VirtioPciDevice { + fn id(&self) -> String { + self.id.clone() + } +} impl Transportable for VirtioPciDevice {} impl Migratable for VirtioPciDevice {} diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index db468b070..5c02f3860 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -88,6 +88,8 @@ const IOMMU_DEVICE_NAME: &str = "iommu"; #[cfg(feature = "mmio_support")] const VIRTIO_MMIO_DEVICE_NAME_PREFIX: &str = "virtio-mmio"; +#[cfg(feature = "pci_support")] +const VIRTIO_PCI_DEVICE_NAME_PREFIX: &str = "virtio-pci"; /// Errors associated with device manager #[derive(Debug)] @@ -1933,8 +1935,10 @@ impl DeviceManager { pci: &mut PciBus, iommu_mapping: &Option>, interrupt_manager: &Arc>, - id: String, + virtio_device_id: String, ) -> DeviceManagerResult { + let id = format!("{}-{}", VIRTIO_PCI_DEVICE_NAME_PREFIX, virtio_device_id); + // Allows support for one MSI-X vector per queue. It also adds 1 // as we need to take into account the dedicated vector to notify // about a virtio config change. @@ -1972,6 +1976,7 @@ impl DeviceManager { let memory = self.memory_manager.lock().unwrap().guest_memory(); let mut virtio_pci_device = VirtioPciDevice::new( + id, memory, virtio_device, msix_num, @@ -1980,7 +1985,8 @@ impl DeviceManager { ) .map_err(DeviceManagerError::VirtioDevice)?; - let mut allocator = self.address_manager.allocator.lock().unwrap(); + let allocator = self.address_manager.allocator.clone(); + let mut allocator = allocator.lock().unwrap(); let bars = virtio_pci_device .allocate_bars(&mut allocator) .map_err(DeviceManagerError::AllocateBars)?; @@ -2005,10 +2011,10 @@ impl DeviceManager { self.bus_devices .push(Arc::clone(&virtio_pci_device) as Arc>); - if self.pci_id_list.contains_key(&id) { + if self.pci_id_list.contains_key(&virtio_device_id) { return Err(DeviceManagerError::DeviceIdAlreadyInUse); } - self.pci_id_list.insert(id, pci_device_bdf); + self.pci_id_list.insert(virtio_device_id, pci_device_bdf); pci.register_mapping( virtio_pci_device.clone(), @@ -2018,9 +2024,7 @@ impl DeviceManager { ) .map_err(DeviceManagerError::AddPciDevice)?; - let migratable = Arc::clone(&virtio_pci_device) as Arc>; - let id = migratable.lock().unwrap().id(); - self.migratable_devices.push((id, migratable)); + self.add_migratable_device(Arc::clone(&virtio_pci_device) as Arc>); Ok(pci_device_bdf) }