From 28e2318a35b12eab2eb73f6db54527a79f0f1774 Mon Sep 17 00:00:00 2001 From: Alex Lyn Date: Fri, 10 Jul 2026 15:47:55 +0800 Subject: [PATCH] runtime-rs: Consolidate virtio-blk shared types into BlockModern module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the block device type definitions shared between BlockDevice and BlockDeviceModern — VIRTIO_BLOCK_{PCI,MMIO,CCW}/VIRTIO_PMEM constants, KATA_*_DEV_TYPE aliases, and the BlockDeviceAio / BlockDeviceFormat enums — from virtio_blk.rs into virtio_blk_modern.rs, so they live alongside the BlockModern handler that will replace the legacy BlockDevice. Add a `format: BlockDeviceFormat` field to BlockConfigModern to match the legacy BlockConfig. Update mod.rs re-exports accordingly. No behavior change; pure relocation plus the new field. Signed-off-by: Alex Lyn --- .../hypervisor/src/device/driver/mod.rs | 13 ++-- .../src/device/driver/virtio_blk.rs | 64 +------------------ .../src/device/driver/virtio_blk_modern.rs | 61 +++++++++++++++++- 3 files changed, 69 insertions(+), 69 deletions(-) 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 af6e3d3104..9593e51c0b 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/mod.rs @@ -28,12 +28,8 @@ pub use vfio_device::{ }; pub use vhost_user::{VhostUserConfig, VhostUserDevice, VhostUserType}; pub use vhost_user_net::VhostUserNetDevice; -pub use virtio_blk::{ - BlockConfig, BlockDevice, BlockDeviceAio, BlockDeviceFormat, 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_blk_modern::{BlockConfigModern, BlockDeviceModern, BlockDeviceModernHandle}; +pub use virtio_blk::{BlockConfig, BlockDevice,}; +pub use virtio_blk_modern::{BlockConfigModern, BlockDeviceFormat, BlockDeviceAio, BlockDeviceModern, BlockDeviceModernHandle, VIRTIO_BLOCK_CCW, VIRTIO_BLOCK_MMIO, VIRTIO_BLOCK_PCI, VIRTIO_PMEM,}; pub use virtio_fs::{ ShareFsConfig, ShareFsDevice, ShareFsMountConfig, ShareFsMountOperation, ShareFsMountType, }; @@ -41,3 +37,8 @@ pub use virtio_net::{Address, NetworkConfig, NetworkDevice}; pub use virtio_vsock::{ HybridVsockConfig, HybridVsockDevice, VsockConfig, VsockDevice, DEFAULT_GUEST_VSOCK_CID, }; +pub use kata_types::device::{ + DRIVER_BLK_CCW_TYPE as KATA_CCW_DEV_TYPE, DRIVER_BLK_MMIO_TYPE as KATA_MMIO_BLK_DEV_TYPE, + DRIVER_BLK_PCI_TYPE as KATA_BLK_DEV_TYPE, DRIVER_NVDIMM_TYPE as KATA_NVDIMM_DEV_TYPE, + DRIVER_SCSI_TYPE as KATA_SCSI_DEV_TYPE, +}; 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 aeed6c768f..e666a675ac 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 @@ -4,6 +4,8 @@ // SPDX-License-Identifier: Apache-2.0 // +use crate::BlockDeviceAio; +use crate::BlockDeviceFormat; use crate::device::pci_path::PciPath; use crate::device::topology::PCIeTopology; use crate::device::util::do_decrease_count; @@ -13,68 +15,6 @@ use crate::device::DeviceType; use crate::Hypervisor as hypervisor; use anyhow::{Context, Result}; use async_trait::async_trait; -pub use kata_types::device::{ - DRIVER_BLK_CCW_TYPE as KATA_CCW_DEV_TYPE, DRIVER_BLK_MMIO_TYPE as KATA_MMIO_BLK_DEV_TYPE, - DRIVER_BLK_PCI_TYPE as KATA_BLK_DEV_TYPE, DRIVER_NVDIMM_TYPE as KATA_NVDIMM_DEV_TYPE, - DRIVER_SCSI_TYPE as KATA_SCSI_DEV_TYPE, -}; - -/// VIRTIO_BLOCK_PCI indicates block driver is virtio-pci based -pub const VIRTIO_BLOCK_PCI: &str = "virtio-blk-pci"; -pub const VIRTIO_BLOCK_MMIO: &str = "virtio-blk-mmio"; -pub const VIRTIO_BLOCK_CCW: &str = "virtio-blk-ccw"; -pub const VIRTIO_PMEM: &str = "virtio-pmem"; - -#[derive(Clone, Copy, Debug, Default)] -pub enum BlockDeviceAio { - // IoUring is the Linux io_uring I/O implementation. - #[default] - IoUring, - - // Native is the native Linux AIO implementation. - Native, - - // Threads is the pthread asynchronous I/O implementation. - Threads, -} - -impl BlockDeviceAio { - pub fn new(aio: &str) -> Self { - match aio { - "native" => BlockDeviceAio::Native, - "threads" => BlockDeviceAio::Threads, - _ => BlockDeviceAio::IoUring, - } - } -} - -impl std::fmt::Display for BlockDeviceAio { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let to_string = match *self { - BlockDeviceAio::Native => "native".to_string(), - BlockDeviceAio::Threads => "threads".to_string(), - _ => "iouring".to_string(), - }; - write!(f, "{to_string}") - } -} - -#[derive(Debug, Clone, Default, PartialEq, Eq)] -pub enum BlockDeviceFormat { - #[default] - Raw, - Vmdk, -} - -impl std::fmt::Display for BlockDeviceFormat { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let to_string = match *self { - BlockDeviceFormat::Raw => "raw".to_string(), - BlockDeviceFormat::Vmdk => "vmdk".to_string(), - }; - write!(f, "{to_string}") - } -} #[derive(Debug, Clone, Default)] pub struct BlockConfig { diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk_modern.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk_modern.rs index 60459ecc41..acd65a9cda 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk_modern.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk_modern.rs @@ -6,7 +6,6 @@ use std::sync::Arc; use tokio::sync::Mutex; -use crate::device::driver::BlockDeviceAio; use crate::device::pci_path::PciPath; use crate::device::topology::PCIeTopology; use crate::device::util::do_decrease_count; @@ -17,6 +16,63 @@ use crate::Hypervisor as hypervisor; use anyhow::{Context, Result}; use async_trait::async_trait; +/// VIRTIO_BLOCK_PCI indicates block driver is virtio-pci based +pub const VIRTIO_BLOCK_PCI: &str = "virtio-blk-pci"; +pub const VIRTIO_BLOCK_MMIO: &str = "virtio-blk-mmio"; +pub const VIRTIO_BLOCK_CCW: &str = "virtio-blk-ccw"; +pub const VIRTIO_PMEM: &str = "virtio-pmem"; + +#[derive(Clone, Copy, Debug, Default)] +pub enum BlockDeviceAio { + // IoUring is the Linux io_uring I/O implementation. + #[default] + IoUring, + + // Native is the native Linux AIO implementation. + Native, + + // Threads is the pthread asynchronous I/O implementation. + Threads, +} + +impl BlockDeviceAio { + pub fn new(aio: &str) -> Self { + match aio { + "native" => BlockDeviceAio::Native, + "threads" => BlockDeviceAio::Threads, + _ => BlockDeviceAio::IoUring, + } + } +} + +impl std::fmt::Display for BlockDeviceAio { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let to_string = match *self { + BlockDeviceAio::Native => "native".to_string(), + BlockDeviceAio::Threads => "threads".to_string(), + _ => "iouring".to_string(), + }; + write!(f, "{to_string}") + } +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub enum BlockDeviceFormat { + #[default] + Raw, + Vmdk, +} + +impl std::fmt::Display for BlockDeviceFormat { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let to_string = match *self { + BlockDeviceFormat::Raw => "raw".to_string(), + BlockDeviceFormat::Vmdk => "vmdk".to_string(), + }; + write!(f, "{to_string}") + } +} + #[derive(Debug, Clone, Default)] pub struct BlockConfigModern { /// Path of the drive. @@ -32,6 +88,9 @@ pub struct BlockConfigModern { /// Don't close `path_on_host` file when dropping the device. pub no_drop: bool, + /// raw, vmdk, etc. And default to raw if not set. + pub format: BlockDeviceFormat, + /// Specifies cache-related options for block devices. /// Denotes whether use of O_DIRECT (bypass the host page cache) is enabled. /// If not set, use configurarion block_device_cache_direct.