refactor(vfio): let VfioPciDev::new take a configured device

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
This commit is contained in:
Changyuan Lyu 2024-10-27 17:24:17 -07:00 committed by Lencerf
parent 727d081e21
commit 339fb32e6e
3 changed files with 12 additions and 23 deletions

View file

@ -415,7 +415,7 @@ fn main_run(args: RunArgs) -> Result<(), Error> {
for (index, vfio) in args.vfio.into_iter().enumerate() {
let param: VfioParam =
serde_aco::from_args(&vfio, &objects).context(error::ParseArg { arg: vfio })?;
vm.add_vfio_dev(format!("vfio-{index}"), param)
vm.add_vfio_dev(format!("vfio-{index}").into(), param)
.context(error::CreateDevice)?;
}

View file

@ -44,8 +44,7 @@ use crate::vfio::bindings::{
};
use crate::vfio::cdev::Cdev;
use crate::vfio::device::Device;
use crate::vfio::iommu::Ioas;
use crate::vfio::{error, Result, VfioParam};
use crate::vfio::{error, Result};
use crate::{align_down, align_up, assign_bits, mask_bits, mem};
fn round_up_range(range: Range<usize>) -> Range<usize> {
@ -397,18 +396,8 @@ impl<M> VfioPciDev<M>
where
M: MsiSender,
{
pub fn new(
name: impl Into<Arc<str>>,
param: &VfioParam,
ioas: Arc<Ioas>,
msi_sender: M,
) -> Result<VfioPciDev<M>> {
let mut dev = Cdev::new(&param.cdev)?;
dev.attach_iommu_ioas(ioas)?;
let cdev = Arc::new(VfioCdev {
dev,
name: name.into(),
});
pub fn new(name: Arc<str>, dev: Cdev, msi_sender: M) -> Result<VfioPciDev<M>> {
let cdev = Arc::new(VfioCdev { dev, name });
let region_config = cdev.dev.get_region_info(VfioPciRegion::CONFIG.raw())?;

View file

@ -45,6 +45,8 @@ use crate::mem::{MemRegion, MemRegionType};
use crate::pci::bus::PciBus;
use crate::pci::{Bdf, PciDevice};
#[cfg(target_os = "linux")]
use crate::vfio::cdev::Cdev;
#[cfg(target_os = "linux")]
use crate::vfio::iommu::UpdateIommuIoas;
#[cfg(target_os = "linux")]
use crate::vfio::iommu::{Ioas, Iommu};
@ -303,12 +305,7 @@ where
#[cfg(target_os = "linux")]
impl Machine<Kvm> {
pub fn add_vfio_dev(
&mut self,
name: impl Into<Arc<str>>,
param: VfioParam,
) -> Result<(), Error> {
let name = name.into();
pub fn add_vfio_dev(&mut self, name: Arc<str>, param: VfioParam) -> Result<(), Error> {
let iommu = if let Some(iommu) = &self.iommu {
iommu.clone()
} else {
@ -340,8 +337,11 @@ impl Machine<Kvm> {
u32::from(bdf.0),
)?;
let dev = Arc::new(VfioPciDev::new(name.clone(), &param, ioas, msi_sender)?);
let pci_dev = PciDevice::new(name, dev);
let mut cdev = Cdev::new(&param.cdev)?;
cdev.attach_iommu_ioas(ioas)?;
let dev = VfioPciDev::new(name.clone(), cdev, msi_sender)?;
let pci_dev = PciDevice::new(name, Arc::new(dev));
self.add_pci_dev(Some(bdf), pci_dev)?;
Ok(())
}