From e3fd40312640644b302eb55d7e208f9b349c42fa Mon Sep 17 00:00:00 2001 From: "alex.lyn" Date: Sun, 26 Nov 2023 00:19:36 +0800 Subject: [PATCH] runtime-rs: enhancement of spdk volume. (1) Add enum DirectVolumeType for direct volumes. (2) Reimplement spdk volume into direct_volume and do alignment of rawblock volume. Fixes: #8300 Signed-off-by: alex.lyn --- .../resource/src/volume/direct_volume.rs | 39 +++++++++++++++---- .../src/volume/direct_volumes/spdk_volume.rs | 38 +++++++----------- .../crates/resource/src/volume/mod.rs | 11 +----- 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/runtime-rs/crates/resource/src/volume/direct_volume.rs b/src/runtime-rs/crates/resource/src/volume/direct_volume.rs index 51f2006b5c..70569d4ad5 100644 --- a/src/runtime-rs/crates/resource/src/volume/direct_volume.rs +++ b/src/runtime-rs/crates/resource/src/volume/direct_volume.rs @@ -14,12 +14,25 @@ use hypervisor::device::device_manager::DeviceManager; use crate::volume::{ direct_volumes::{ - get_direct_volume_path, rawblock_volume, volume_mount_info, KATA_DIRECT_VOLUME_TYPE, + get_direct_volume_path, rawblock_volume, spdk_volume, volume_mount_info, + KATA_DIRECT_VOLUME_TYPE, KATA_SPDK_VOLUME_TYPE, KATA_SPOOL_VOLUME_TYPE, }, utils::KATA_MOUNT_BIND_TYPE, Volume, }; +enum DirectVolumeType { + RawBlock, + Spdk, +} + +fn to_volume_type(volume_type: &str) -> DirectVolumeType { + match volume_type { + KATA_SPDK_VOLUME_TYPE | KATA_SPOOL_VOLUME_TYPE => DirectVolumeType::Spdk, + _ => DirectVolumeType::RawBlock, + } +} + pub(crate) async fn handle_direct_volume( d: &RwLock, m: &oci::Mount, @@ -59,11 +72,18 @@ pub(crate) async fn handle_direct_volume( } }; - let direct_volume: Arc = Arc::new( - rawblock_volume::RawblockVolume::new(d, m, &mount_info, read_only, sid) - .await - .with_context(|| format!("new sid {:?} rawblock volume {:?}", &sid, m))?, - ); + let direct_volume: Arc = match to_volume_type(mount_info.volume_type.as_str()) { + DirectVolumeType::RawBlock => Arc::new( + rawblock_volume::RawblockVolume::new(d, m, &mount_info, read_only, sid) + .await + .with_context(|| format!("new sid {:?} rawblock volume {:?}", &sid, m))?, + ), + DirectVolumeType::Spdk => Arc::new( + spdk_volume::SPDKVolume::new(d, m, &mount_info, read_only, sid) + .await + .with_context(|| format!("create spdk volume {:?}", m))?, + ), + }; Ok(Some(direct_volume)) } @@ -71,7 +91,12 @@ pub(crate) async fn handle_direct_volume( pub(crate) fn is_direct_volume(m: &oci::Mount) -> Result { let mount_type = m.r#type.as_str(); // Filter the non-bind volume and non-direct-vol volume - let vol_types = [KATA_MOUNT_BIND_TYPE, KATA_DIRECT_VOLUME_TYPE]; + let vol_types = [ + KATA_MOUNT_BIND_TYPE, + KATA_DIRECT_VOLUME_TYPE, + KATA_SPDK_VOLUME_TYPE, + KATA_SPOOL_VOLUME_TYPE, + ]; if !vol_types.contains(&mount_type) { return Ok(false); } 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 0e66918ca6..7e28de9cab 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 @@ -6,6 +6,7 @@ use anyhow::{anyhow, Context, Result}; use async_trait::async_trait; +use kata_types::mount::DirectVolumeMountInfo; use nix::sys::{stat, stat::SFlag}; use tokio::sync::RwLock; @@ -18,7 +19,7 @@ use hypervisor::{ }; use crate::volume::{ - direct_volumes::{volume_mount_info, KATA_SPDK_VOLUME_TYPE, KATA_SPOOL_VOLUME_TYPE}, + direct_volumes::{KATA_SPDK_VOLUME_TYPE, KATA_SPOOL_VOLUME_TYPE}, utils::{generate_shared_path, DEFAULT_VOLUME_FS_TYPE}, Volume, }; @@ -35,26 +36,23 @@ impl SPDKVolume { pub(crate) async fn new( d: &RwLock, m: &oci::Mount, + mount_info: &DirectVolumeMountInfo, read_only: bool, sid: &str, ) -> Result { - let mnt_src: &str = &m.source; - - // deserde Information from mountinfo.json - let v = volume_mount_info(mnt_src).context("deserde information from mountinfo.json")?; - let device = match v.volume_type.as_str() { + let device = match mount_info.volume_type.as_str() { KATA_SPDK_VOLUME_TYPE => { - if v.device.starts_with("spdk://") { - v.device.clone() + if mount_info.device.starts_with("spdk://") { + mount_info.device.clone() } else { - format!("spdk://{}", v.device.as_str()) + format!("spdk://{}", mount_info.device.as_str()) } } KATA_SPOOL_VOLUME_TYPE => { - if v.device.starts_with("spool://") { - v.device.clone() + if mount_info.device.starts_with("spool://") { + mount_info.device.clone() } else { - format!("spool://{}", v.device.as_str()) + format!("spool://{}", mount_info.device.as_str()) } } _ => return Err(anyhow!("mountinfo.json is invalid")), @@ -82,12 +80,12 @@ impl SPDKVolume { ..Default::default() }; - if let Some(num) = v.metadata.get("num_queues") { + if let Some(num) = mount_info.metadata.get("num_queues") { vhu_blk_config.num_queues = num .parse::() .context("num queues parse usize failed.")?; } - if let Some(size) = v.metadata.get("queue_size") { + if let Some(size) = mount_info.metadata.get("queue_size") { vhu_blk_config.queue_size = size .parse::() .context("num queues parse u32 failed.")?; @@ -125,7 +123,7 @@ impl SPDKVolume { storage.mount_point = guest_path.clone(); if m.r#type != "bind" { - storage.fs_type = v.fs_type.clone(); + storage.fs_type = mount_info.fs_type.clone(); } else { storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string(); } @@ -179,13 +177,3 @@ impl Volume for SPDKVolume { Ok(Some(self.device_id.clone())) } } - -pub(crate) fn is_spdk_volume(m: &oci::Mount) -> bool { - // spdkvol or spoolvol will share the same implementation - let vol_types = [KATA_SPDK_VOLUME_TYPE, KATA_SPOOL_VOLUME_TYPE]; - if vol_types.contains(&m.r#type.as_str()) { - return true; - } - - false -} diff --git a/src/runtime-rs/crates/resource/src/volume/mod.rs b/src/runtime-rs/crates/resource/src/volume/mod.rs index f4857fa72d..6c40d93485 100644 --- a/src/runtime-rs/crates/resource/src/volume/mod.rs +++ b/src/runtime-rs/crates/resource/src/volume/mod.rs @@ -15,10 +15,7 @@ pub mod direct_volume; use crate::volume::direct_volume::is_direct_volume; pub mod direct_volumes; -use direct_volumes::{ - spdk_volume::{is_spdk_volume, SPDKVolume}, - vfio_volume::{is_vfio_volume, VfioVolume}, -}; +use direct_volumes::vfio_volume::{is_vfio_volume, VfioVolume}; use std::{sync::Arc, vec::Vec}; @@ -99,12 +96,6 @@ impl VolumeResource { .await .with_context(|| format!("new vfio volume {:?}", m))?, ) - } else if is_spdk_volume(m) { - Arc::new( - SPDKVolume::new(d, m, read_only, sid) - .await - .with_context(|| format!("create spdk volume {:?}", m))?, - ) } else if let Some(options) = get_huge_page_option(m).context("failed to check huge page")? {