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<dyn Any + Send + Sync>. 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 <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-03-06 14:18:07 +01:00 committed by Rob Bradford
parent 0f99d3f7cc
commit 08604ac6a8

View File

@ -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<usize>,
// Hashmap of PCI b/d/f to their corresponding Arc<Mutex<dyn PciDevice>>.
#[cfg(feature = "pci_support")]
pci_devices: HashMap<u32, Arc<dyn Any + Send + Sync>>,
}
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<dyn Any + Send + Sync>,
);
self.bus_devices
.push(Arc::clone(&vfio_pci_device) as Arc<Mutex<dyn BusDevice>>);