From d6bf1f5eb0970b2e5cb736d52011c99dbc39c066 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 28 Nov 2022 11:44:40 +0100 Subject: [PATCH] pci: Move VfioCommon creation to a dedicated function This is some preliminatory work for moving both VfioUser and Vfio to the new restore design. Signed-off-by: Sebastien Boeuf --- pci/src/vfio.rs | 86 +++++++++++++++++++++++++++----------------- pci/src/vfio_user.rs | 51 ++++++-------------------- 2 files changed, 64 insertions(+), 73 deletions(-) diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index 7c59ef6e5..2b6a3cb3f 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -406,6 +406,53 @@ pub(crate) struct VfioCommon { } impl VfioCommon { + pub(crate) fn new( + msi_interrupt_manager: Arc>, + legacy_interrupt_group: Option>, + vfio_wrapper: Arc, + subclass: &dyn PciSubclass, + bdf: PciBdf, + restoring: bool, + ) -> Result { + let configuration = PciConfiguration::new( + 0, + 0, + 0, + PciClassCode::Other, + subclass, + None, + PciHeaderType::Device, + 0, + 0, + None, + None, + ); + + let mut vfio_common = VfioCommon { + mmio_regions: Vec::new(), + configuration, + interrupt: Interrupt { + intx: None, + msi: None, + msix: None, + }, + msi_interrupt_manager, + legacy_interrupt_group, + vfio_wrapper, + patches: HashMap::new(), + }; + + // No need to parse capabilities from the device if on the restore path. + // The initialization will be performed later when restore() will be + // called. + if !restoring { + vfio_common.parse_capabilities(bdf); + vfio_common.initialize_legacy_interrupt()?; + } + + Ok(vfio_common) + } + pub(crate) fn allocate_bars( &mut self, allocator: &Arc>, @@ -1226,43 +1273,16 @@ impl VfioPciDevice { let device = Arc::new(device); device.reset(); - let configuration = PciConfiguration::new( - 0, - 0, - 0, - PciClassCode::Other, - &PciVfioSubclass::VfioSubclass, - None, - PciHeaderType::Device, - 0, - 0, - None, - None, - ); - let vfio_wrapper = VfioDeviceWrapper::new(Arc::clone(&device)); - let mut common = VfioCommon { - mmio_regions: Vec::new(), - configuration, - interrupt: Interrupt { - intx: None, - msi: None, - msix: None, - }, + let common = VfioCommon::new( msi_interrupt_manager, legacy_interrupt_group, - vfio_wrapper: Arc::new(vfio_wrapper) as Arc, - patches: HashMap::new(), - }; - - // No need to parse capabilities from the device if on the restore path. - // The initialization will be performed later when restore() will be - // called. - if !restoring { - common.parse_capabilities(bdf); - common.initialize_legacy_interrupt()?; - } + Arc::new(vfio_wrapper) as Arc, + &PciVfioSubclass::VfioSubclass, + bdf, + restoring, + )?; let vfio_pci_device = VfioPciDevice { id, diff --git a/pci/src/vfio_user.rs b/pci/src/vfio_user.rs index 473476dc0..b1f572bbd 100644 --- a/pci/src/vfio_user.rs +++ b/pci/src/vfio_user.rs @@ -3,15 +3,12 @@ // SPDX-License-Identifier: Apache-2.0 // -use crate::vfio::{Interrupt, UserMemoryRegion, Vfio, VfioCommon, VfioError}; +use crate::vfio::{UserMemoryRegion, Vfio, VfioCommon, VfioError}; use crate::{BarReprogrammingParams, PciBarConfiguration, VfioPciError}; -use crate::{ - PciBdf, PciClassCode, PciConfiguration, PciDevice, PciDeviceError, PciHeaderType, PciSubclass, -}; +use crate::{PciBdf, PciDevice, PciDeviceError, PciSubclass}; use anyhow::anyhow; use hypervisor::HypervisorVmError; use std::any::Any; -use std::collections::HashMap; use std::os::unix::prelude::AsRawFd; use std::ptr::null_mut; use std::sync::{Arc, Barrier, Mutex}; @@ -51,6 +48,8 @@ pub enum VfioUserPciDeviceError { DmaUnmap(#[source] VfioUserError), #[error("Failed to initialize legacy interrupts: {0}")] InitializeLegacyInterrupts(#[source] VfioPciError), + #[error("Failed to create VfioCommon: {0}")] + CreateVfioCommon(#[source] VfioPciError), } #[derive(Copy, Clone)] @@ -76,20 +75,6 @@ impl VfioUserPciDevice { restoring: bool, memory_slot: Arc u32 + Send + Sync>, ) -> Result { - // This is used for the BAR and capabilities only - let configuration = PciConfiguration::new( - 0, - 0, - 0, - PciClassCode::Other, - &PciVfioUserSubclass::VfioUserSubclass, - None, - PciHeaderType::Device, - 0, - 0, - None, - None, - ); let resettable = client.lock().unwrap().resettable(); if resettable { client @@ -103,29 +88,15 @@ impl VfioUserPciDevice { client: client.clone(), }; - let mut common = VfioCommon { - mmio_regions: Vec::new(), - configuration, - interrupt: Interrupt { - intx: None, - msi: None, - msix: None, - }, + let common = VfioCommon::new( msi_interrupt_manager, legacy_interrupt_group, - vfio_wrapper: Arc::new(vfio_wrapper) as Arc, - patches: HashMap::new(), - }; - - // No need to parse capabilities from the device if on the restore path. - // The initialization will be performed later when restore() will be - // called. - if !restoring { - common.parse_capabilities(bdf); - common - .initialize_legacy_interrupt() - .map_err(VfioUserPciDeviceError::InitializeLegacyInterrupts)?; - } + Arc::new(vfio_wrapper) as Arc, + &PciVfioUserSubclass::VfioUserSubclass, + bdf, + restoring, + ) + .map_err(VfioUserPciDeviceError::CreateVfioCommon)?; Ok(Self { id,