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 <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn 2023-11-26 00:19:36 +08:00
parent f973729029
commit e3fd403126
3 changed files with 46 additions and 42 deletions

View File

@ -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<DeviceManager>,
m: &oci::Mount,
@ -59,11 +72,18 @@ pub(crate) async fn handle_direct_volume(
}
};
let direct_volume: Arc<dyn Volume> = Arc::new(
let direct_volume: Arc<dyn Volume> = 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<bool> {
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);
}

View File

@ -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<DeviceManager>,
m: &oci::Mount,
mount_info: &DirectVolumeMountInfo,
read_only: bool,
sid: &str,
) -> Result<Self> {
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::<usize>()
.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::<u32>()
.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
}

View File

@ -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")?
{