runtime-rs: initialize pcie topology in Device Manager

Add a pcie_topology field to DeviceManager and initialize
pcie_topology when ResourceManager calls DeviceManager's new()
with TopologyConfigInfo.

Fixes: #7218

Signed-off-by: alex.lyn <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn 2023-12-27 15:57:23 +08:00
parent b42548b8e1
commit ea69c17008
3 changed files with 22 additions and 8 deletions

View File

@ -8,6 +8,7 @@ use std::{collections::HashMap, sync::Arc};
use anyhow::{anyhow, Context, Result};
use kata_sys_util::rand::RandomBytes;
use kata_types::config::hypervisor::TopologyConfigInfo;
use tokio::sync::{Mutex, RwLock};
use crate::{
@ -94,15 +95,20 @@ pub struct DeviceManager {
devices: HashMap<String, ArcMutexDevice>,
hypervisor: Arc<dyn Hypervisor>,
shared_info: SharedInfo,
pcie_topology: Option<PCIeTopology>,
}
impl DeviceManager {
pub async fn new(hypervisor: Arc<dyn Hypervisor>) -> Result<Self> {
pub async fn new(
hypervisor: Arc<dyn Hypervisor>,
topo_config: Option<&TopologyConfigInfo>,
) -> Result<Self> {
let devices = HashMap::<String, ArcMutexDevice>::new();
Ok(DeviceManager {
devices,
hypervisor,
shared_info: SharedInfo::new().await,
pcie_topology: PCIeTopology::new(topo_config),
})
}
@ -120,10 +126,11 @@ impl DeviceManager {
.devices
.get(device_id)
.context("failed to find device")?;
let mut device_guard = device.lock().await;
// attach device
let result = device_guard
.attach(&mut None::<&mut PCIeTopology>, self.hypervisor.as_ref())
.attach(&mut self.pcie_topology.as_mut(), self.hypervisor.as_ref())
.await;
// handle attach error
if let Err(e) = result {
@ -165,7 +172,7 @@ impl DeviceManager {
if let Some(dev) = self.devices.get(device_id) {
let mut device_guard = dev.lock().await;
let result = match device_guard
.detach(&mut None::<&mut PCIeTopology>, self.hypervisor.as_ref())
.detach(&mut self.pcie_topology.as_mut(), self.hypervisor.as_ref())
.await
{
Ok(index) => {
@ -605,6 +612,7 @@ mod tests {
BlockConfig, KATA_BLK_DEV_TYPE,
};
use anyhow::{anyhow, Context, Result};
use kata_types::config::hypervisor::TopologyConfigInfo;
use std::sync::Arc;
use tests_utils::load_test_config;
use tokio::sync::RwLock;
@ -612,6 +620,7 @@ mod tests {
async fn new_device_manager() -> Result<Arc<RwLock<DeviceManager>>> {
let hypervisor_name: &str = "qemu";
let toml_config = load_test_config(hypervisor_name.to_owned())?;
let topo_config = TopologyConfigInfo::new(&toml_config);
let hypervisor_config = toml_config
.hypervisor
.get(hypervisor_name)
@ -623,7 +632,7 @@ mod tests {
.await;
let dm = Arc::new(RwLock::new(
DeviceManager::new(Arc::new(hypervisor))
DeviceManager::new(Arc::new(hypervisor), topo_config.as_ref())
.await
.context("device manager")?,
));

View File

@ -17,7 +17,7 @@ use hypervisor::{
},
BlockConfig, Hypervisor, VfioConfig,
};
use kata_types::config::TomlConfig;
use kata_types::config::{hypervisor::TopologyConfigInfo, TomlConfig};
use kata_types::mount::Mount;
use oci::{Linux, LinuxCpu, LinuxResources};
use persist::sandbox_persist::Persist;
@ -59,8 +59,9 @@ impl ResourceManagerInner {
toml_config: Arc<TomlConfig>,
init_size_manager: InitialSizeManager,
) -> Result<Self> {
let topo_config = TopologyConfigInfo::new(&toml_config);
// create device manager
let dev_manager = DeviceManager::new(hypervisor.clone())
let dev_manager = DeviceManager::new(hypervisor.clone(), topo_config.as_ref())
.await
.context("failed to create device manager")?;
@ -510,12 +511,14 @@ impl Persist for ResourceManagerInner {
sid: resource_args.sid.clone(),
config: resource_args.config,
};
let topo_config = TopologyConfigInfo::new(&args.config);
Ok(Self {
sid: resource_args.sid,
agent: resource_args.agent,
hypervisor: resource_args.hypervisor.clone(),
device_manager: Arc::new(RwLock::new(
DeviceManager::new(resource_args.hypervisor).await?,
DeviceManager::new(resource_args.hypervisor, topo_config.as_ref()).await?,
)),
network: None,
share_fs: None,

View File

@ -9,6 +9,7 @@ mod tests {
use std::sync::Arc;
use anyhow::{anyhow, Context, Result};
use kata_types::config::hypervisor::TopologyConfigInfo;
use netlink_packet_route::MACVLAN_MODE_PRIVATE;
use scopeguard::defer;
use tests_utils::load_test_config;
@ -29,6 +30,7 @@ mod tests {
async fn get_device_manager() -> Result<Arc<RwLock<DeviceManager>>> {
let hypervisor_name: &str = "qemu";
let toml_config = load_test_config(hypervisor_name.to_owned())?;
let topo_config = TopologyConfigInfo::new(&toml_config);
let hypervisor_config = toml_config
.hypervisor
.get(hypervisor_name)
@ -40,7 +42,7 @@ mod tests {
.await;
let dm = Arc::new(RwLock::new(
DeviceManager::new(Arc::new(hypervisor))
DeviceManager::new(Arc::new(hypervisor), topo_config.as_ref())
.await
.context("device manager")?,
));