pci, vmm: Switch to use more generic VfioOps trait

Replace the concrete `VfioContainer` type with the `VfioOps` trait
object for device passthrough. This decouples the VFIO DMA mapping
interface from the legacy VFIO container/group implementation, allowing
it to be extended to support VFIO cdev and iommufd in the future.

Signed-off-by: Bo Chen <bchen@crusoe.ai>
This commit is contained in:
Bo Chen
2025-05-21 21:02:56 +00:00
parent 5bb4ea20a5
commit 7360bfe33a
2 changed files with 7 additions and 9 deletions

View File

@@ -19,9 +19,7 @@ use log::{error, info};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use vfio_bindings::bindings::vfio::*;
use vfio_ioctls::{
VfioContainer, VfioDevice, VfioIrq, VfioRegionInfoCap, VfioRegionSparseMmapArea,
};
use vfio_ioctls::{VfioDevice, VfioIrq, VfioOps, VfioRegionInfoCap, VfioRegionSparseMmapArea};
use vm_allocator::page_size::{
align_page_size_down, align_page_size_up, is_4k_aligned, is_4k_multiple, is_page_size_aligned,
};
@@ -1468,7 +1466,7 @@ pub struct VfioPciDevice {
id: String,
vm: Arc<dyn hypervisor::Vm>,
device: Arc<VfioDevice>,
container: Arc<VfioContainer>,
container: Arc<dyn VfioOps>,
common: VfioCommon,
iommu_attached: bool,
memory_slot_allocator: MemorySlotAllocator,
@@ -1483,7 +1481,7 @@ impl VfioPciDevice {
id: String,
vm: Arc<dyn hypervisor::Vm>,
device: VfioDevice,
container: Arc<VfioContainer>,
container: Arc<dyn VfioOps>,
msi_interrupt_manager: Arc<dyn InterruptManager<GroupConfig = MsiIrqGroupConfig>>,
legacy_interrupt_group: Option<Arc<dyn InterruptSourceGroup>>,
iommu_attached: bool,
@@ -2000,7 +1998,7 @@ impl Migratable for VfioPciDevice {}
/// be used when the caller tries to provide a way to update the mappings
/// associated with a specific VFIO container.
pub struct VfioDmaMapping<M: GuestAddressSpace> {
container: Arc<VfioContainer>,
container: Arc<dyn VfioOps>,
memory: Arc<M>,
mmio_regions: Arc<Mutex<Vec<MmioRegion>>>,
}
@@ -2012,7 +2010,7 @@ impl<M: GuestAddressSpace> VfioDmaMapping<M> {
/// * `memory`: guest memory to mmap.
/// * `mmio_regions`: mmio_regions to mmap.
pub fn new(
container: Arc<VfioContainer>,
container: Arc<dyn VfioOps>,
memory: Arc<M>,
mmio_regions: Arc<Mutex<Vec<MmioRegion>>>,
) -> Self {

View File

@@ -1032,7 +1032,7 @@ pub struct DeviceManager {
// VFIO container
// Only one container can be created, therefore it is stored as part of the
// DeviceManager to be reused.
vfio_container: Option<Arc<VfioContainer>>,
vfio_container: Option<Arc<dyn VfioOps>>,
// Paravirtualized IOMMU
iommu_device: Option<Arc<Mutex<virtio_devices::Iommu>>>,
@@ -3798,7 +3798,7 @@ impl DeviceManager {
self.add_vfio_device(device_cfg)
}
fn create_vfio_container(&self) -> DeviceManagerResult<Arc<VfioContainer>> {
fn create_vfio_container(&self) -> DeviceManagerResult<Arc<dyn VfioOps>> {
let passthrough_device = self
.passthrough_device
.as_ref()