From 0144bc27c69148c2aa6430cd5a2e3115f27e5ed1 Mon Sep 17 00:00:00 2001 From: Alex Lyn Date: Wed, 20 Aug 2025 20:36:48 +0800 Subject: [PATCH] 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 --- .../hypervisor/src/device/device_manager.rs | 32 +++++++++++-------- .../hypervisor/src/device/driver/mod.rs | 6 ++-- .../src/device/driver/virtio_blk.rs | 2 +- .../crates/resource/src/cpu_mem/swap.rs | 4 ++- .../crates/resource/src/manager_inner.rs | 2 +- .../resource/src/rootfs/block_rootfs.rs | 2 +- .../resource/src/volume/block_volume.rs | 2 +- .../volume/direct_volumes/rawblock_volume.rs | 2 +- .../src/volume/direct_volumes/spdk_volume.rs | 2 +- 9 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs index 1b01e5738f..d009a5a2e1 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs @@ -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) -> String { +pub async fn get_block_driver(d: &RwLock) -> 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, diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs index 71a24ef8f6..a108a9a90f 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs @@ -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, diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs index 555464c4c8..dffce0b307 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs @@ -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] diff --git a/src/runtime-rs/crates/resource/src/cpu_mem/swap.rs b/src/runtime-rs/crates/resource/src/cpu_mem/swap.rs index 7a4a5e5cb4..58ef429511 100644 --- a/src/runtime-rs/crates/resource/src/cpu_mem/swap.rs +++ b/src/runtime-rs/crates/resource/src/cpu_mem/swap.rs @@ -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, diff --git a/src/runtime-rs/crates/resource/src/manager_inner.rs b/src/runtime-rs/crates/resource/src/manager_inner.rs index c898da56d1..2ceb74ca28 100644 --- a/src/runtime-rs/crates/resource/src/manager_inner.rs +++ b/src/runtime-rs/crates/resource/src/manager_inner.rs @@ -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() }); diff --git a/src/runtime-rs/crates/resource/src/rootfs/block_rootfs.rs b/src/runtime-rs/crates/resource/src/rootfs/block_rootfs.rs index 55cbe78d9a..bf3e1b5f49 100644 --- a/src/runtime-rs/crates/resource/src/rootfs/block_rootfs.rs +++ b/src/runtime-rs/crates/resource/src/rootfs/block_rootfs.rs @@ -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, diff --git a/src/runtime-rs/crates/resource/src/volume/block_volume.rs b/src/runtime-rs/crates/resource/src/volume/block_volume.rs index 96a35eff7a..d96b9b3688 100644 --- a/src/runtime-rs/crates/resource/src/volume/block_volume.rs +++ b/src/runtime-rs/crates/resource/src/volume/block_volume.rs @@ -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, diff --git a/src/runtime-rs/crates/resource/src/volume/direct_volumes/rawblock_volume.rs b/src/runtime-rs/crates/resource/src/volume/direct_volumes/rawblock_volume.rs index 298461267f..918e43074e 100644 --- a/src/runtime-rs/crates/resource/src/volume/direct_volumes/rawblock_volume.rs +++ b/src/runtime-rs/crates/resource/src/volume/direct_volumes/rawblock_volume.rs @@ -36,7 +36,7 @@ impl RawblockVolume { read_only: bool, sid: &str, ) -> Result { - 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 { diff --git a/src/runtime-rs/crates/resource/src/volume/direct_volumes/spdk_volume.rs b/src/runtime-rs/crates/resource/src/volume/direct_volumes/spdk_volume.rs index bb771e1bed..1695a205fa 100644 --- a/src/runtime-rs/crates/resource/src/volume/direct_volumes/spdk_volume.rs +++ b/src/runtime-rs/crates/resource/src/volume/direct_volumes/spdk_volume.rs @@ -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,