From 08604ac6a8b92643a77bc33abfd88dbb60d42cd8 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Fri, 6 Mar 2020 14:18:07 +0100 Subject: [PATCH] vmm: Store PCI devices as Any devices from DeviceManager As we try to keep track of every PCI device related to the VM, we don't want to have separate lists depending on the concrete type associated with the PciDevice trait. Also, we want to be able to cast the actual type into any trait or concrete type. The most efficient way to solve all these issues is to store every device as an Arc. This gives the ability to downcast into the appropriate concrete type, and then to cast back into any trait that we might need. Signed-off-by: Sebastien Boeuf --- vmm/src/device_manager.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index f7e330fe3..e8bb07a6b 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -33,6 +33,8 @@ use pci::{ DeviceRelocation, PciBarRegionType, PciBus, PciConfigIo, PciConfigMmio, PciDevice, PciRoot, }; use qcow::{self, ImageType, QcowFile}; +#[cfg(feature = "pci_support")] +use std::any::Any; use std::collections::HashMap; use std::fs::{File, OpenOptions}; use std::io::{self, sink, stdout}; @@ -479,6 +481,10 @@ pub struct DeviceManager { // 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>, } impl DeviceManager { @@ -582,6 +588,8 @@ impl DeviceManager { pci_id_list: HashMap::new(), #[cfg(feature = "pci_support")] device_id_cnt: Wrapping(0), + #[cfg(feature = "pci_support")] + pci_devices: HashMap::new(), }; device_manager @@ -1494,6 +1502,10 @@ impl DeviceManager { pci.add_device(vfio_pci_device.clone()) .map_err(DeviceManagerError::AddPciDevice)?; + self.pci_devices.insert( + pci_device_bdf, + Arc::clone(&vfio_pci_device) as Arc, + ); self.bus_devices .push(Arc::clone(&vfio_pci_device) as Arc>);