From a0a89b13464ea74a08bb167947777434fc9f1f24 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Tue, 23 Feb 2021 15:01:31 +0100 Subject: [PATCH] pci, vmm: Move to upstream vfio-ioctls crate This commit moves both pci and vmm code from the internal vfio-ioctls crate to the upstream one from the rust-vmm project. Signed-off-by: Sebastien Boeuf --- Cargo.lock | 2 +- Cargo.toml | 4 ++++ pci/Cargo.toml | 2 +- pci/src/vfio.rs | 44 ++++++++++++++++++++++++++------------- vm-device/Cargo.toml | 2 +- vmm/Cargo.toml | 2 +- vmm/src/device_manager.rs | 10 ++++----- 7 files changed, 42 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69631d092..2a662eafc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1324,7 +1324,7 @@ dependencies = [ [[package]] name = "vfio-ioctls" version = "0.1.0" -source = "git+https://github.com/cloud-hypervisor/vfio-ioctls?branch=ch#6ea1b934bbff9102bbdc80c23a357a48645cd722" +source = "git+https://github.com/rust-vmm/vfio-ioctls?branch=master#95e70655e34ad942f5ca8d3bc450d7dec4964514" dependencies = [ "byteorder", "kvm-bindings", diff --git a/Cargo.toml b/Cargo.toml index 106594c7c..cadde480b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,10 @@ clap = { version = "2.33.3", features = ["wrap_help"] } [patch.'https://github.com/rust-vmm/vhost'] vhost_rs = { git = "https://github.com/cloud-hypervisor/vhost", branch = "ch", package = "vhost", features = ["vhost-user-master", "vhost-user-slave"] } +[patch.crates-io] +kvm-bindings = { git = "https://github.com/cloud-hypervisor/kvm-bindings", branch = "ch", features = ["with-serde", "fam-wrappers"] } +kvm-ioctls = { git = "https://github.com/cloud-hypervisor/kvm-ioctls", branch = "ch" } + [dev-dependencies] credibility = "0.1.3" dirs = "3.0.1" diff --git a/pci/Cargo.toml b/pci/Cargo.toml index af4da4ca6..67eecb6b8 100644 --- a/pci/Cargo.toml +++ b/pci/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" anyhow = "1.0" byteorder = "1.3.4" hypervisor = { path = "../hypervisor" } -vfio-ioctls = { git = "https://github.com/cloud-hypervisor/vfio-ioctls", branch = "ch" } +vfio-ioctls = { git = "https://github.com/rust-vmm/vfio-ioctls", branch = "master" } vmm-sys-util = ">=0.3.1" libc = "0.2.86" log = "0.4.14" diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index 7d5b373fe..173178270 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -18,15 +18,15 @@ use std::ptr::null_mut; use std::sync::{Arc, Barrier}; use std::{fmt, io, result}; use vfio_bindings::bindings::vfio::*; -use vfio_ioctls::{VfioDevice, VfioError}; +use vfio_ioctls::{VfioContainer, VfioDevice, VfioError}; use vm_allocator::SystemAllocator; use vm_device::interrupt::{ InterruptIndex, InterruptManager, InterruptSourceGroup, MsiIrqGroupConfig, }; use vm_device::BusDevice; use vm_memory::{ - Address, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap, GuestRegionMmap, - GuestUsize, + Address, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap, + GuestMemoryRegion, GuestRegionMmap, GuestUsize, }; use vmm_sys_util::eventfd::EventFd; @@ -298,11 +298,13 @@ impl VfioPciConfig { pub struct VfioPciDevice { vm: Arc, device: Arc, + container: Arc, vfio_pci_configuration: VfioPciConfig, configuration: PciConfiguration, mmio_regions: Vec, interrupt: Interrupt, mem: GuestMemoryAtomic, + iommu_attached: bool, } impl VfioPciDevice { @@ -310,9 +312,11 @@ impl VfioPciDevice { pub fn new( vm: &Arc, device: VfioDevice, + container: Arc, msi_interrupt_manager: &Arc>, legacy_interrupt_group: Option>>, mem: GuestMemoryAtomic, + iommu_attached: bool, ) -> Result { let device = Arc::new(device); device.reset(); @@ -335,6 +339,7 @@ impl VfioPciDevice { let mut vfio_pci_device = VfioPciDevice { vm: vm.clone(), device, + container, configuration, vfio_pci_configuration, mmio_regions: Vec::new(), @@ -344,6 +349,7 @@ impl VfioPciDevice { msix: None, }, mem, + iommu_attached, }; vfio_pci_device.parse_capabilities(msi_interrupt_manager); @@ -723,9 +729,17 @@ impl VfioPciDevice { } pub fn update_memory(&self, new_region: &Arc) -> Result<()> { - self.device - .extend_dma_map(new_region) - .map_err(VfioPciError::UpdateMemory) + if !self.iommu_attached { + self.container + .vfio_dma_map( + new_region.start_addr().raw_value(), + new_region.len() as u64, + new_region.as_ptr() as u64, + ) + .map_err(VfioPciError::UpdateMemory)?; + } + + Ok(()) } pub fn mmio_regions(&self) -> Vec { @@ -753,10 +767,11 @@ impl Drop for VfioPciDevice { self.disable_intx(); } - if self - .device - .unset_dma_map(self.mem.memory().deref()) - .is_err() + if !self.iommu_attached + && self + .container + .vfio_unmap_guest_memory(self.mem.memory().deref()) + .is_err() { error!("failed to remove all guest memory regions from iommu table"); } @@ -963,10 +978,11 @@ impl PciDevice for VfioPciDevice { } } - if self - .device - .setup_dma_map(self.mem.memory().deref()) - .is_err() + if !self.iommu_attached + && self + .container + .vfio_map_guest_memory(self.mem.memory().deref()) + .is_err() { error!("failed to add all guest memory regions into iommu table"); } diff --git a/vm-device/Cargo.toml b/vm-device/Cargo.toml index e27890129..0dc6891f9 100644 --- a/vm-device/Cargo.toml +++ b/vm-device/Cargo.toml @@ -10,7 +10,7 @@ thiserror = "1.0" serde = {version = ">=1.0.27", features = ["rc"] } serde_derive = ">=1.0.27" serde_json = ">=1.0.9" -vfio-ioctls = { git = "https://github.com/cloud-hypervisor/vfio-ioctls", branch = "ch" } +vfio-ioctls = { git = "https://github.com/rust-vmm/vfio-ioctls", branch = "master" } vm-memory = { version = "0.5.0", features = ["backend-mmap"] } vmm-sys-util = ">=0.3.1" diff --git a/vmm/Cargo.toml b/vmm/Cargo.toml index 2e79b9fa4..4d90d7676 100644 --- a/vmm/Cargo.toml +++ b/vmm/Cargo.toml @@ -40,7 +40,7 @@ serde_json = ">=1.0.9" signal-hook = "0.3.6" thiserror = "1.0" url = "2.2.1" -vfio-ioctls = { git = "https://github.com/cloud-hypervisor/vfio-ioctls", branch = "ch" } +vfio-ioctls = { git = "https://github.com/rust-vmm/vfio-ioctls", branch = "master" } virtio-devices = { path = "../virtio-devices" } vm-allocator = { path = "../vm-allocator" } vm-device = { path = "../vm-device" } diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 75f7c27d8..418c7552c 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -2691,12 +2691,8 @@ impl DeviceManager { .map_err(DeviceManagerError::VfioCreate)?, ); - let vfio_device = VfioDevice::new( - &device_cfg.path, - Arc::clone(&vfio_container), - device_cfg.iommu, - ) - .map_err(DeviceManagerError::VfioCreate)?; + let vfio_device = VfioDevice::new(&device_cfg.path, Arc::clone(&vfio_container)) + .map_err(DeviceManagerError::VfioCreate)?; if device_cfg.iommu { if let Some(iommu) = &self.iommu_device { @@ -2731,9 +2727,11 @@ impl DeviceManager { let mut vfio_pci_device = VfioPciDevice::new( &self.address_manager.vm, vfio_device, + vfio_container, &self.msi_interrupt_manager, legacy_interrupt_group, memory, + device_cfg.iommu, ) .map_err(DeviceManagerError::VfioPciCreate)?;