From 9b60fcdc39721bffb6c7d526263bb52e85c3701b Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Thu, 9 Jan 2020 18:51:10 +0100 Subject: [PATCH] msix: Add VmFd to MsixConfig Because MsixConfig will be responsible for updating the KVM GSI routes at some point, it must have access to the VmFd to invoke the KVM ioctl KVM_SET_GSI_ROUTING. Signed-off-by: Sebastien Boeuf --- Cargo.lock | 1 + pci/src/msix.rs | 5 ++++- vfio/src/vfio_pci.rs | 2 +- vm-virtio/Cargo.toml | 1 + vm-virtio/src/transport/pci_device.rs | 8 +++++++- vmm/src/device_manager.rs | 1 + 6 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a940ee42d..5b0d5c720 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1110,6 +1110,7 @@ dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "devices 0.1.0", "epoll 4.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kvm-ioctls 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "net_gen 0.1.0", diff --git a/pci/src/msix.rs b/pci/src/msix.rs index 13f4addc3..0994e7cb1 100644 --- a/pci/src/msix.rs +++ b/pci/src/msix.rs @@ -11,6 +11,7 @@ use std::sync::Arc; use crate::device::InterruptParameters; use crate::{InterruptDelivery, InterruptRoute, PciCapability, PciCapabilityID}; use byteorder::{ByteOrder, LittleEndian}; +use kvm_ioctls::VmFd; use vm_allocator::SystemAllocator; use vm_memory::ByteValued; @@ -53,13 +54,14 @@ pub struct MsixConfig { pub table_entries: Vec, pub pba_entries: Vec, pub irq_routes: Vec, + _vm_fd: Arc, interrupt_cb: Option>, masked: bool, enabled: bool, } impl MsixConfig { - pub fn new(msix_vectors: u16, allocator: &mut SystemAllocator) -> Self { + pub fn new(msix_vectors: u16, allocator: &mut SystemAllocator, vm_fd: Arc) -> Self { assert!(msix_vectors <= MAX_MSIX_VECTORS_PER_DEVICE); let mut table_entries: Vec = Vec::new(); @@ -77,6 +79,7 @@ impl MsixConfig { table_entries, pba_entries, irq_routes, + _vm_fd: vm_fd, interrupt_cb: None, masked: false, enabled: false, diff --git a/vfio/src/vfio_pci.rs b/vfio/src/vfio_pci.rs index 40facf4f6..2a6453e4d 100644 --- a/vfio/src/vfio_pci.rs +++ b/vfio/src/vfio_pci.rs @@ -412,7 +412,7 @@ impl VfioPciDevice { table, pba, }; - let msix_config = MsixConfig::new(msix_cap.table_size(), allocator); + let msix_config = MsixConfig::new(msix_cap.table_size(), allocator, self.vm_fd.clone()); self.interrupt.msix = Some(VfioMsix { bar: msix_config, diff --git a/vm-virtio/Cargo.toml b/vm-virtio/Cargo.toml index c5df246de..9f73c8387 100644 --- a/vm-virtio/Cargo.toml +++ b/vm-virtio/Cargo.toml @@ -14,6 +14,7 @@ arc-swap = ">=0.4.4" byteorder = "1.3.2" devices = { path = "../devices" } epoll = ">=4.0.1" +kvm-ioctls = "0.4.0" libc = "0.2.60" log = "0.4.8" net_gen = { path = "../net_gen" } diff --git a/vm-virtio/src/transport/pci_device.rs b/vm-virtio/src/transport/pci_device.rs index 704285807..e63428f70 100755 --- a/vm-virtio/src/transport/pci_device.rs +++ b/vm-virtio/src/transport/pci_device.rs @@ -15,6 +15,7 @@ extern crate vmm_sys_util; use arc_swap::ArcSwap; use devices::BusDevice; +use kvm_ioctls::VmFd; use libc::EFD_NONBLOCK; use pci::{ BarReprogrammingParams, InterruptDelivery, InterruptParameters, MsixCap, MsixConfig, @@ -257,6 +258,7 @@ impl VirtioPciDevice { msix_num: u16, iommu_mapping_cb: Option>, allocator: &mut SystemAllocator, + vm_fd: &Arc, ) -> Result { let device_clone = device.clone(); let locked_device = device_clone.lock().unwrap(); @@ -277,7 +279,11 @@ impl VirtioPciDevice { let pci_device_id = VIRTIO_PCI_DEVICE_ID_BASE + locked_device.device_type() as u16; let (msix_config, msix_config_clone) = if msix_num > 0 { - let msix_config = Arc::new(Mutex::new(MsixConfig::new(msix_num, allocator))); + let msix_config = Arc::new(Mutex::new(MsixConfig::new( + msix_num, + allocator, + vm_fd.clone(), + ))); let msix_config_clone = msix_config.clone(); (Some(msix_config), Some(msix_config_clone)) } else { diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index fa75ce1c6..3348ebf20 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1473,6 +1473,7 @@ impl DeviceManager { msix_num, iommu_mapping_cb, &mut allocator, + vm_fd, ) .map_err(DeviceManagerError::VirtioDevice)?;