vmm: ivshmem device support

Signed-off-by: Yi Wang <foxywang@tencent.com>
Signed-off-by: Songqian Li <sionli@tencent.com>
This commit is contained in:
Songqian Li
2024-08-29 15:58:50 +08:00
committed by Bo Chen
parent c72414552b
commit 2c282a5a54
7 changed files with 184 additions and 0 deletions

View File

@@ -115,6 +115,8 @@ use crate::interrupt::{LegacyUserspaceInterruptManager, MsiInterruptManager};
use crate::memory_manager::{Error as MemoryManagerError, MemoryManager, MEMORY_MANAGER_ACPI_SIZE};
use crate::pci_segment::PciSegment;
use crate::serial_manager::{Error as SerialManagerError, SerialManager};
#[cfg(feature = "ivshmem")]
use crate::vm_config::IvshmemConfig;
use crate::vm_config::{
ConsoleOutputMode, DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, UserDeviceConfig,
VdpaConfig, VhostMode, VmConfig, VsockConfig, DEFAULT_IOMMU_ADDRESS_WIDTH_BITS,
@@ -140,6 +142,8 @@ const PVMEMCONTROL_DEVICE_NAME: &str = "__pvmemcontrol";
const BALLOON_DEVICE_NAME: &str = "__balloon";
const CONSOLE_DEVICE_NAME: &str = "__console";
const PVPANIC_DEVICE_NAME: &str = "__pvpanic";
#[cfg(feature = "ivshmem")]
const IVSHMEM_DEVICE_NAME: &str = "__ivshmem";
// Devices that the user may name and for which we generate
// identifiers if the user doesn't give one
@@ -632,6 +636,11 @@ pub enum DeviceManagerError {
#[error("Cannot create a PvPanic device")]
PvPanicCreate(#[source] devices::pvpanic::PvPanicError),
#[cfg(feature = "ivshmem")]
/// Cannot create a ivshmem device
#[error("Cannot create a ivshmem device: {0}")]
IvshmemCreate(devices::ivshmem::IvshmemError),
/// Cannot create a RateLimiterGroup
#[error("Cannot create a RateLimiterGroup")]
RateLimiterGroupCreate(#[source] rate_limiter::group::Error),
@@ -1085,6 +1094,10 @@ pub struct DeviceManager {
#[cfg(feature = "fw_cfg")]
fw_cfg: Option<Arc<Mutex<FwCfg>>>,
#[cfg(feature = "ivshmem")]
// ivshmem device
ivshmem_device: Option<Arc<Mutex<devices::IvshmemDevice>>>,
}
fn create_mmio_allocators(
@@ -1351,6 +1364,8 @@ impl DeviceManager {
mmio_regions: Arc::new(Mutex::new(Vec::new())),
#[cfg(feature = "fw_cfg")]
fw_cfg: None,
#[cfg(feature = "ivshmem")]
ivshmem_device: None,
};
let device_manager = Arc::new(Mutex::new(device_manager));
@@ -1474,6 +1489,11 @@ impl DeviceManager {
self.pvpanic_device = self.add_pvpanic_device()?;
}
#[cfg(feature = "ivshmem")]
if let Some(ivshmem) = self.config.clone().lock().unwrap().ivshmem.as_ref() {
self.ivshmem_device = self.add_ivshmem_device(ivshmem)?;
}
Ok(())
}
@@ -4199,6 +4219,43 @@ impl DeviceManager {
Ok(Some(pvpanic_device))
}
#[cfg(feature = "ivshmem")]
fn add_ivshmem_device(
&mut self,
ivshmem_cfg: &IvshmemConfig,
) -> DeviceManagerResult<Option<Arc<Mutex<devices::IvshmemDevice>>>> {
let id = String::from(IVSHMEM_DEVICE_NAME);
let pci_segment_id = 0x0_u16;
info!("Creating ivshmem device {}", id);
let (pci_segment_id, pci_device_bdf, resources) =
self.pci_resources(&id, pci_segment_id)?;
let snapshot = snapshot_from_id(self.snapshot.as_ref(), id.as_str());
let ivshmem_device = Arc::new(Mutex::new(
devices::IvshmemDevice::new(
id.clone(),
ivshmem_cfg.size as u64,
snapshot,
)
.map_err(DeviceManagerError::IvshmemCreate)?,
));
let new_resources = self.add_pci_device(
ivshmem_device.clone(),
ivshmem_device.clone(),
pci_segment_id,
pci_device_bdf,
resources,
)?;
let mut node = device_node!(id, ivshmem_device);
node.resources = new_resources;
node.pci_bdf = Some(pci_device_bdf);
node.pci_device_handle = None;
self.device_tree.lock().unwrap().insert(id, node);
Ok(Some(ivshmem_device))
}
fn pci_resources(
&self,
id: &str,