diff --git a/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs b/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs index 953e434863..8864bade94 100644 --- a/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs +++ b/src/runtime-rs/crates/runtimes/virt_container/src/sandbox.rs @@ -4,6 +4,7 @@ // SPDX-License-Identifier: Apache-2.0 // +use crate::health_check::HealthCheck; use agent::kata::KataAgent; use agent::types::KernelModule; use agent::{ @@ -20,6 +21,7 @@ use common::{ }; use containerd_shim_protos::events::task::{TaskExit, TaskOOM}; +use hypervisor::PortDeviceConfig; use hypervisor::VsockConfig; use hypervisor::HYPERVISOR_FIRECRACKER; use hypervisor::HYPERVISOR_REMOTE; @@ -47,8 +49,6 @@ use strum::Display; use tokio::sync::{mpsc::Sender, Mutex, RwLock}; use tracing::instrument; -use crate::health_check::HealthCheck; - pub(crate) const VIRTCONTAINER: &str = "virt_container"; pub struct SandboxRestoreArgs { @@ -166,9 +166,42 @@ impl VirtSandbox { resource_configs.push(ResourceConfig::Protection(protection_dev_config)); } + // prepare pcie port device config + if let Some(port_dev_config) = self.prepare_pcie_port_devices().await { + resource_configs.push(ResourceConfig::PortDevice(port_dev_config)); + } + Ok(resource_configs) } + async fn prepare_pcie_port_devices(&self) -> Option { + // Fetch the device manager and read the PCIe topology + let device_manager = self.resource_manager.get_device_manager().await; + let dm = device_manager.read().await; + + // Get the PCIe topology and port information + match dm.get_pcie_topology().and_then(|t| t.get_pcie_port()) { + Some((port_type, total_ports)) if total_ports > 0 => { + info!( + sl!(), + "Preparing PCIe {:?} with {} devices for VM.", port_type, total_ports + ); + Some(PortDeviceConfig::new(port_type, total_ports)) + } + Some((_, 0)) => { + info!(sl!(), "No PCIe ports available for VM."); + None + } + _ => { + info!( + sl!(), + "Invalid PCIe configuration or no topology available." + ); + None + } + } + } + async fn prepare_network_resource( &self, network_env: &SandboxNetworkEnv,