mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
vmm: device_manager: Reuse PciDeviceCommonConfig in MetaVirtioDevice
This struct has the same members and it can be reused to reduce complexity now and if other common PCI related fields need to be added in the future. Signed-off-by: Rob Bradford <rbradford@meta.com>
This commit is contained in:
+56
-53
@@ -132,8 +132,8 @@ use crate::serial_manager::{Error as SerialManagerError, SerialManager};
|
|||||||
use crate::vm_config::IvshmemConfig;
|
use crate::vm_config::IvshmemConfig;
|
||||||
use crate::vm_config::{
|
use crate::vm_config::{
|
||||||
ConsoleOutputMode, DEFAULT_IOMMU_ADDRESS_WIDTH_BITS, DEFAULT_PCI_SEGMENT_APERTURE_WEIGHT,
|
ConsoleOutputMode, DEFAULT_IOMMU_ADDRESS_WIDTH_BITS, DEFAULT_PCI_SEGMENT_APERTURE_WEIGHT,
|
||||||
DeviceConfig, DiskConfig, FsConfig, GenericVhostUserConfig, NetConfig, PmemConfig,
|
DeviceConfig, DiskConfig, FsConfig, GenericVhostUserConfig, NetConfig, PciDeviceCommonConfig,
|
||||||
UserDeviceConfig, VdpaConfig, VhostMode, VmConfig, VsockConfig,
|
PmemConfig, UserDeviceConfig, VdpaConfig, VhostMode, VmConfig, VsockConfig,
|
||||||
};
|
};
|
||||||
use crate::{DEVICE_MANAGER_SNAPSHOT_ID, GuestRegionMmap, PciDeviceInfo, device_node};
|
use crate::{DEVICE_MANAGER_SNAPSHOT_ID, GuestRegionMmap, PciDeviceInfo, device_node};
|
||||||
|
|
||||||
@@ -926,12 +926,19 @@ pub enum PciDeviceHandle {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct MetaVirtioDevice {
|
struct MetaVirtioDevice {
|
||||||
virtio_device: Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
virtio_device: Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: bool,
|
pci_common: PciDeviceCommonConfig,
|
||||||
id: String,
|
|
||||||
pci_segment: u16,
|
|
||||||
dma_handler: Option<Arc<dyn ExternalDmaMapping>>,
|
dma_handler: Option<Arc<dyn ExternalDmaMapping>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl MetaVirtioDevice {
|
||||||
|
fn id(&self) -> &str {
|
||||||
|
self.pci_common
|
||||||
|
.id
|
||||||
|
.as_deref()
|
||||||
|
.expect("ID should have been assigned before use")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct AcpiPlatformAddresses {
|
pub struct AcpiPlatformAddresses {
|
||||||
pub pm_timer_address: Option<GenericAddress>,
|
pub pm_timer_address: Option<GenericAddress>,
|
||||||
@@ -1669,24 +1676,25 @@ impl DeviceManager {
|
|||||||
let mut iommu_attached_devices = Vec::new();
|
let mut iommu_attached_devices = Vec::new();
|
||||||
{
|
{
|
||||||
for handle in self.virtio_devices.clone() {
|
for handle in self.virtio_devices.clone() {
|
||||||
let mapping: Option<Arc<IommuMapping>> = if handle.iommu {
|
let mapping: Option<Arc<IommuMapping>> = if handle.pci_common.iommu {
|
||||||
self.iommu_mapping.clone()
|
self.iommu_mapping.clone()
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let id = handle.id().to_owned();
|
||||||
let dev_id = self.add_virtio_pci_device(
|
let dev_id = self.add_virtio_pci_device(
|
||||||
handle.virtio_device,
|
handle.virtio_device,
|
||||||
&mapping,
|
&mapping,
|
||||||
&handle.id,
|
&id,
|
||||||
handle.pci_segment,
|
handle.pci_common.pci_segment,
|
||||||
handle.dma_handler,
|
handle.dma_handler,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Track device BDF for Generic Initiator support
|
// Track device BDF for Generic Initiator support
|
||||||
self.device_id_to_bdf.insert(handle.id.clone(), dev_id);
|
self.device_id_to_bdf.insert(id, dev_id);
|
||||||
|
|
||||||
if handle.iommu {
|
if handle.pci_common.iommu {
|
||||||
iommu_attached_devices.push(dev_id);
|
iommu_attached_devices.push(dev_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2417,9 +2425,11 @@ impl DeviceManager {
|
|||||||
self.virtio_devices.push(MetaVirtioDevice {
|
self.virtio_devices.push(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&virtio_console_device)
|
virtio_device: Arc::clone(&virtio_console_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: console_config.iommu,
|
pci_common: PciDeviceCommonConfig {
|
||||||
id: id.clone(),
|
id: Some(id.clone()),
|
||||||
pci_segment: 0,
|
iommu: console_config.iommu,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -2913,9 +2923,7 @@ impl DeviceManager {
|
|||||||
|
|
||||||
Ok(MetaVirtioDevice {
|
Ok(MetaVirtioDevice {
|
||||||
virtio_device,
|
virtio_device,
|
||||||
iommu: disk_cfg.pci_common.iommu,
|
pci_common: disk_cfg.pci_common.clone(),
|
||||||
id,
|
|
||||||
pci_segment: disk_cfg.pci_common.pci_segment,
|
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -3083,9 +3091,7 @@ impl DeviceManager {
|
|||||||
|
|
||||||
Ok(MetaVirtioDevice {
|
Ok(MetaVirtioDevice {
|
||||||
virtio_device,
|
virtio_device,
|
||||||
iommu: net_cfg.pci_common.iommu,
|
pci_common: net_cfg.pci_common.clone(),
|
||||||
id,
|
|
||||||
pci_segment: net_cfg.pci_common.pci_segment,
|
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -3128,9 +3134,11 @@ impl DeviceManager {
|
|||||||
self.virtio_devices.push(MetaVirtioDevice {
|
self.virtio_devices.push(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&virtio_rng_device)
|
virtio_device: Arc::clone(&virtio_rng_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: rng_config.iommu,
|
pci_common: PciDeviceCommonConfig {
|
||||||
id: id.clone(),
|
id: Some(id.clone()),
|
||||||
pci_segment: 0,
|
iommu: rng_config.iommu,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -3189,9 +3197,7 @@ impl DeviceManager {
|
|||||||
Ok(MetaVirtioDevice {
|
Ok(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&generic_vhost_user_device)
|
virtio_device: Arc::clone(&generic_vhost_user_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: false,
|
pci_common: generic_vhost_user_cfg.pci_common.clone(),
|
||||||
id,
|
|
||||||
pci_segment: generic_vhost_user_cfg.pci_common.pci_segment,
|
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@@ -3255,9 +3261,7 @@ impl DeviceManager {
|
|||||||
Ok(MetaVirtioDevice {
|
Ok(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&virtio_fs_device)
|
virtio_device: Arc::clone(&virtio_fs_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: false,
|
pci_common: fs_cfg.pci_common.clone(),
|
||||||
id,
|
|
||||||
pci_segment: fs_cfg.pci_common.pci_segment,
|
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@@ -3444,9 +3448,7 @@ impl DeviceManager {
|
|||||||
Ok(MetaVirtioDevice {
|
Ok(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&virtio_pmem_device)
|
virtio_device: Arc::clone(&virtio_pmem_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: pmem_cfg.pci_common.iommu,
|
pci_common: pmem_cfg.pci_common.clone(),
|
||||||
id,
|
|
||||||
pci_segment: pmem_cfg.pci_common.pci_segment,
|
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -3515,9 +3517,7 @@ impl DeviceManager {
|
|||||||
Ok(MetaVirtioDevice {
|
Ok(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&vsock_device)
|
virtio_device: Arc::clone(&vsock_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: vsock_cfg.pci_common.iommu,
|
pci_common: vsock_cfg.pci_common.clone(),
|
||||||
id,
|
|
||||||
pci_segment: vsock_cfg.pci_common.pci_segment,
|
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -3571,9 +3571,10 @@ impl DeviceManager {
|
|||||||
self.virtio_devices.push(MetaVirtioDevice {
|
self.virtio_devices.push(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&virtio_mem_device)
|
virtio_device: Arc::clone(&virtio_mem_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: false,
|
pci_common: PciDeviceCommonConfig {
|
||||||
id: memory_zone_id.clone(),
|
id: Some(memory_zone_id.clone()),
|
||||||
pci_segment: 0,
|
..Default::default()
|
||||||
|
},
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -3658,9 +3659,10 @@ impl DeviceManager {
|
|||||||
self.virtio_devices.push(MetaVirtioDevice {
|
self.virtio_devices.push(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&virtio_balloon_device)
|
virtio_device: Arc::clone(&virtio_balloon_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: false,
|
pci_common: PciDeviceCommonConfig {
|
||||||
id: id.clone(),
|
id: Some(id.clone()),
|
||||||
pci_segment: 0,
|
..Default::default()
|
||||||
|
},
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -3697,9 +3699,10 @@ impl DeviceManager {
|
|||||||
self.virtio_devices.push(MetaVirtioDevice {
|
self.virtio_devices.push(MetaVirtioDevice {
|
||||||
virtio_device: Arc::clone(&virtio_watchdog_device)
|
virtio_device: Arc::clone(&virtio_watchdog_device)
|
||||||
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: false,
|
pci_common: PciDeviceCommonConfig {
|
||||||
id: id.clone(),
|
id: Some(id.clone()),
|
||||||
pci_segment: 0,
|
..Default::default()
|
||||||
|
},
|
||||||
dma_handler: None,
|
dma_handler: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -3755,9 +3758,7 @@ impl DeviceManager {
|
|||||||
|
|
||||||
Ok(MetaVirtioDevice {
|
Ok(MetaVirtioDevice {
|
||||||
virtio_device: vdpa_device as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
virtio_device: vdpa_device as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
|
||||||
iommu: vdpa_cfg.pci_common.iommu,
|
pci_common: vdpa_cfg.pci_common.clone(),
|
||||||
id,
|
|
||||||
pci_segment: vdpa_cfg.pci_common.pci_segment,
|
|
||||||
dma_handler: Some(vdpa_mapping),
|
dma_handler: Some(vdpa_mapping),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -4555,7 +4556,7 @@ impl DeviceManager {
|
|||||||
.map_err(DeviceManagerError::UpdateMemoryForVirtioDevice)?;
|
.map_err(DeviceManagerError::UpdateMemoryForVirtioDevice)?;
|
||||||
|
|
||||||
if let Some(dma_handler) = &handle.dma_handler
|
if let Some(dma_handler) = &handle.dma_handler
|
||||||
&& !handle.iommu
|
&& !handle.pci_common.iommu
|
||||||
{
|
{
|
||||||
let gpa = new_region.start_addr().0;
|
let gpa = new_region.start_addr().0;
|
||||||
let size = new_region.len();
|
let size = new_region.len();
|
||||||
@@ -4994,24 +4995,26 @@ impl DeviceManager {
|
|||||||
// for instance.
|
// for instance.
|
||||||
self.virtio_devices.push(handle.clone());
|
self.virtio_devices.push(handle.clone());
|
||||||
|
|
||||||
let mapping: Option<Arc<IommuMapping>> = if handle.iommu {
|
let mapping: Option<Arc<IommuMapping>> = if handle.pci_common.iommu {
|
||||||
self.iommu_mapping.clone()
|
self.iommu_mapping.clone()
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let id = handle.id().to_owned();
|
||||||
let bdf = self.add_virtio_pci_device(
|
let bdf = self.add_virtio_pci_device(
|
||||||
handle.virtio_device,
|
handle.virtio_device,
|
||||||
&mapping,
|
&mapping,
|
||||||
&handle.id,
|
&id,
|
||||||
handle.pci_segment,
|
handle.pci_common.pci_segment,
|
||||||
handle.dma_handler,
|
handle.dma_handler,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Update the PCIU bitmap
|
// Update the PCIU bitmap
|
||||||
self.pci_segments[handle.pci_segment as usize].pci_devices_up |= 1 << bdf.device();
|
self.pci_segments[handle.pci_common.pci_segment as usize].pci_devices_up |=
|
||||||
|
1 << bdf.device();
|
||||||
|
|
||||||
Ok(PciDeviceInfo { id: handle.id, bdf })
|
Ok(PciDeviceInfo { id, bdf })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_iommu_segment(&self, pci_segment_id: u16) -> bool {
|
fn is_iommu_segment(&self, pci_segment_id: u16) -> bool {
|
||||||
@@ -5108,7 +5111,7 @@ impl DeviceManager {
|
|||||||
for handle in &self.virtio_devices {
|
for handle in &self.virtio_devices {
|
||||||
let virtio_device = handle.virtio_device.lock().unwrap();
|
let virtio_device = handle.virtio_device.lock().unwrap();
|
||||||
if let Some(device_counters) = virtio_device.counters() {
|
if let Some(device_counters) = virtio_device.counters() {
|
||||||
counters.insert(handle.id.clone(), device_counters.clone());
|
counters.insert(handle.id().to_owned(), device_counters.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user