runtime-rs: Refactor block device config

Support aio, block driver and other block device info.
match block aio mode and its cache mode.

Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
Alex Lyn 2025-08-20 20:36:48 +08:00
parent d1d62dc984
commit 0144bc27c6
9 changed files with 30 additions and 24 deletions

View File

@ -8,15 +8,15 @@ use std::{collections::HashMap, sync::Arc};
use anyhow::{anyhow, Context, Result};
use kata_sys_util::rand::RandomBytes;
use kata_types::config::hypervisor::{TopologyConfigInfo, VIRTIO_SCSI};
use kata_types::config::hypervisor::{BlockDeviceInfo, TopologyConfigInfo, VIRTIO_SCSI};
use tokio::sync::{Mutex, RwLock};
use crate::{
vhost_user_blk::VhostUserBlkDevice, BlockConfig, BlockDevice, HybridVsockDevice, Hypervisor,
NetworkDevice, PCIePortDevice, ProtectionDevice, ShareFsDevice, VfioDevice, VhostUserConfig,
VhostUserNetDevice, VsockDevice, KATA_BLK_DEV_TYPE, KATA_CCW_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE,
KATA_NVDIMM_DEV_TYPE, KATA_SCSI_DEV_TYPE, VIRTIO_BLOCK_CCW, VIRTIO_BLOCK_MMIO,
VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
vhost_user_blk::VhostUserBlkDevice, BlockConfig, BlockDevice, BlockDeviceAio,
HybridVsockDevice, Hypervisor, NetworkDevice, PCIePortDevice, ProtectionDevice, ShareFsDevice,
VfioDevice, VhostUserConfig, VhostUserNetDevice, VsockDevice, KATA_BLK_DEV_TYPE,
KATA_CCW_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE, KATA_NVDIMM_DEV_TYPE, KATA_SCSI_DEV_TYPE,
VIRTIO_BLOCK_CCW, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
};
use super::{
@ -117,12 +117,8 @@ impl DeviceManager {
self.pcie_topology.clone()
}
async fn get_block_driver(&self) -> String {
self.hypervisor
.hypervisor_config()
.await
.blockdev_info
.block_device_driver
async fn get_block_driver(&self) -> BlockDeviceInfo {
self.hypervisor.hypervisor_config().await.blockdev_info
}
async fn try_add_device(&mut self, device_id: &str) -> Result<()> {
@ -498,6 +494,14 @@ impl DeviceManager {
.context("failed to get host path")?;
}
let block_device_aio = self.get_block_driver().await.block_device_aio;
block_config.is_direct = if BlockDeviceAio::Native == BlockDeviceAio::new(&block_device_aio)
{
Some(true)
} else {
None
};
Ok(Arc::new(Mutex::new(BlockDevice::new(
device_id,
block_config,
@ -623,7 +627,7 @@ pub async fn do_update_device(
Ok(())
}
pub async fn get_block_driver(d: &RwLock<DeviceManager>) -> String {
pub async fn get_block_driver(d: &RwLock<DeviceManager>) -> BlockDeviceInfo {
d.read().await.get_block_driver().await
}
@ -670,7 +674,7 @@ mod tests {
assert!(dm.is_ok());
let d = dm.unwrap();
let block_driver = get_block_driver(&d).await;
let block_driver = get_block_driver(&d).await.block_device_driver;
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
path_on_host: "/dev/dddzzz".to_string(),
driver_option: block_driver,

View File

@ -24,9 +24,9 @@ pub use vfio::{
pub use vhost_user::{VhostUserConfig, VhostUserDevice, VhostUserType};
pub use vhost_user_net::VhostUserNetDevice;
pub use virtio_blk::{
BlockConfig, BlockDevice, KATA_BLK_DEV_TYPE, KATA_CCW_DEV_TYPE, KATA_MMIO_BLK_DEV_TYPE,
KATA_NVDIMM_DEV_TYPE, KATA_SCSI_DEV_TYPE, VIRTIO_BLOCK_CCW, VIRTIO_BLOCK_MMIO,
VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
BlockConfig, BlockDevice, BlockDeviceAio, KATA_BLK_DEV_TYPE, KATA_CCW_DEV_TYPE,
KATA_MMIO_BLK_DEV_TYPE, KATA_NVDIMM_DEV_TYPE, KATA_SCSI_DEV_TYPE, VIRTIO_BLOCK_CCW,
VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM,
};
pub use virtio_fs::{
ShareFsConfig, ShareFsDevice, ShareFsMountConfig, ShareFsMountOperation, ShareFsMountType,

View File

@ -25,7 +25,7 @@ pub const KATA_CCW_DEV_TYPE: &str = "ccw";
pub const KATA_NVDIMM_DEV_TYPE: &str = "nvdimm";
pub const KATA_SCSI_DEV_TYPE: &str = "scsi";
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BlockDeviceAio {
// IoUring is the Linux io_uring I/O implementation.
#[default]

View File

@ -156,7 +156,9 @@ impl SwapTask {
let swap_path = swap_path.to_string_lossy().to_string();
// Add swap file to sandbox
let block_driver = get_block_driver(&self.device_manager).await;
let block_driver = get_block_driver(&self.device_manager)
.await
.block_device_driver;
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
path_on_host: swap_path.clone(),
driver_option: block_driver,

View File

@ -396,7 +396,7 @@ impl ResourceManagerInner {
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
major: d.major(),
minor: d.minor(),
driver_option: block_driver,
driver_option: block_driver.block_device_driver,
..Default::default()
});

View File

@ -50,7 +50,7 @@ impl BlockRootfs {
fs::create_dir_all(&host_path)
.map_err(|e| anyhow!("failed to create rootfs dir {}: {:?}", host_path, e))?;
let block_driver = get_block_driver(d).await;
let block_driver = get_block_driver(d).await.block_device_driver;
let block_device_config = &mut BlockConfig {
major: stat::major(dev_id) as i64,

View File

@ -39,7 +39,7 @@ impl BlockVolume {
Some(path) => path,
None => return Err(anyhow!("mount source path is empty")),
};
let block_driver = get_block_driver(d).await;
let block_driver = get_block_driver(d).await.block_device_driver;
let fstat = stat::stat(mnt_src).context(format!("stat {}", mnt_src.display()))?;
let block_device_config = BlockConfig {
major: stat::major(fstat.st_rdev) as i64,

View File

@ -36,7 +36,7 @@ impl RawblockVolume {
read_only: bool,
sid: &str,
) -> Result<Self> {
let block_driver = get_block_driver(d).await;
let block_driver = get_block_driver(d).await.block_device_driver;
// check volume type
if mount_info.volume_type != KATA_DIRECT_VOLUME_TYPE {

View File

@ -74,7 +74,7 @@ impl SPDKVolume {
}
}
let block_driver = get_block_driver(d).await;
let block_driver = get_block_driver(d).await.block_device_driver;
let vhu_blk_config = &mut VhostUserConfig {
socket_path: device,