runtime-rs: enhancement of vfio volume.

Reimplement vfio volume into direct_volume and do alignment
of rawblock/spdk volume.

Fixes: #8300

Signed-off-by: alex.lyn <alex.lyn@antgroup.com>
This commit is contained in:
alex.lyn 2023-11-26 00:25:28 +08:00
parent e3fd403126
commit fe68f25bea
3 changed files with 18 additions and 31 deletions

View File

@ -14,8 +14,9 @@ use hypervisor::device::device_manager::DeviceManager;
use crate::volume::{ use crate::volume::{
direct_volumes::{ direct_volumes::{
get_direct_volume_path, rawblock_volume, spdk_volume, volume_mount_info, get_direct_volume_path, rawblock_volume, spdk_volume, vfio_volume, volume_mount_info,
KATA_DIRECT_VOLUME_TYPE, KATA_SPDK_VOLUME_TYPE, KATA_SPOOL_VOLUME_TYPE, KATA_DIRECT_VOLUME_TYPE, KATA_SPDK_VOLUME_TYPE, KATA_SPOOL_VOLUME_TYPE,
KATA_VFIO_VOLUME_TYPE,
}, },
utils::KATA_MOUNT_BIND_TYPE, utils::KATA_MOUNT_BIND_TYPE,
Volume, Volume,
@ -24,11 +25,13 @@ use crate::volume::{
enum DirectVolumeType { enum DirectVolumeType {
RawBlock, RawBlock,
Spdk, Spdk,
Vfio,
} }
fn to_volume_type(volume_type: &str) -> DirectVolumeType { fn to_volume_type(volume_type: &str) -> DirectVolumeType {
match volume_type { match volume_type {
KATA_SPDK_VOLUME_TYPE | KATA_SPOOL_VOLUME_TYPE => DirectVolumeType::Spdk, KATA_SPDK_VOLUME_TYPE | KATA_SPOOL_VOLUME_TYPE => DirectVolumeType::Spdk,
KATA_VFIO_VOLUME_TYPE => DirectVolumeType::Vfio,
_ => DirectVolumeType::RawBlock, _ => DirectVolumeType::RawBlock,
} }
} }
@ -83,6 +86,11 @@ pub(crate) async fn handle_direct_volume(
.await .await
.with_context(|| format!("create spdk volume {:?}", m))?, .with_context(|| format!("create spdk volume {:?}", m))?,
), ),
DirectVolumeType::Vfio => Arc::new(
vfio_volume::VfioVolume::new(d, m, &mount_info, read_only, sid)
.await
.with_context(|| format!("new vfio volume {:?}", m))?,
),
}; };
Ok(Some(direct_volume)) Ok(Some(direct_volume))
@ -94,6 +102,7 @@ pub(crate) fn is_direct_volume(m: &oci::Mount) -> Result<bool> {
let vol_types = [ let vol_types = [
KATA_MOUNT_BIND_TYPE, KATA_MOUNT_BIND_TYPE,
KATA_DIRECT_VOLUME_TYPE, KATA_DIRECT_VOLUME_TYPE,
KATA_VFIO_VOLUME_TYPE,
KATA_SPDK_VOLUME_TYPE, KATA_SPDK_VOLUME_TYPE,
KATA_SPOOL_VOLUME_TYPE, KATA_SPOOL_VOLUME_TYPE,
]; ];

View File

@ -4,7 +4,7 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
use anyhow::{anyhow, Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait; use async_trait::async_trait;
use tokio::sync::RwLock; use tokio::sync::RwLock;
@ -15,9 +15,9 @@ use hypervisor::{
}, },
get_vfio_device, VfioConfig, get_vfio_device, VfioConfig,
}; };
use kata_types::mount::DirectVolumeMountInfo;
use crate::volume::{ use crate::volume::{
direct_volumes::{volume_mount_info, KATA_VFIO_VOLUME_TYPE},
utils::{generate_shared_path, DEFAULT_VOLUME_FS_TYPE}, utils::{generate_shared_path, DEFAULT_VOLUME_FS_TYPE},
Volume, Volume,
}; };
@ -33,19 +33,13 @@ impl VfioVolume {
pub(crate) async fn new( pub(crate) async fn new(
d: &RwLock<DeviceManager>, d: &RwLock<DeviceManager>,
m: &oci::Mount, m: &oci::Mount,
mount_info: &DirectVolumeMountInfo,
read_only: bool, read_only: bool,
sid: &str, sid: &str,
) -> Result<Self> { ) -> 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")?;
if v.volume_type != KATA_VFIO_VOLUME_TYPE {
return Err(anyhow!("volume type is invalid"));
}
// support both /dev/vfio/X and BDF<DDDD:BB:DD.F> or BDF<BB:DD.F> // support both /dev/vfio/X and BDF<DDDD:BB:DD.F> or BDF<BB:DD.F>
let vfio_device = get_vfio_device(v.device).context("get vfio device failed.")?; let vfio_device =
get_vfio_device(mount_info.device.clone()).context("get vfio device failed.")?;
let vfio_dev_config = &mut VfioConfig { let vfio_dev_config = &mut VfioConfig {
host_path: vfio_device.clone(), host_path: vfio_device.clone(),
dev_type: "b".to_string(), dev_type: "b".to_string(),
@ -84,14 +78,14 @@ impl VfioVolume {
storage.mount_point = guest_path.clone(); storage.mount_point = guest_path.clone();
if m.r#type != "bind" { if m.r#type != "bind" {
storage.fs_type = v.fs_type.clone(); storage.fs_type = mount_info.fs_type.clone();
} else { } else {
storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string(); storage.fs_type = DEFAULT_VOLUME_FS_TYPE.to_string();
} }
let mount = oci::Mount { let mount = oci::Mount {
destination: m.destination.clone(), destination: m.destination.clone(),
r#type: v.fs_type, r#type: mount_info.fs_type.clone(),
source: guest_path, source: guest_path,
options: m.options.clone(), options: m.options.clone(),
}; };
@ -132,11 +126,3 @@ impl Volume for VfioVolume {
Ok(Some(self.device_id.clone())) Ok(Some(self.device_id.clone()))
} }
} }
pub(crate) fn is_vfio_volume(m: &oci::Mount) -> bool {
if m.r#type == KATA_VFIO_VOLUME_TYPE {
return true;
}
false
}

View File

@ -13,9 +13,7 @@ pub mod utils;
pub mod direct_volume; pub mod direct_volume;
use crate::volume::direct_volume::is_direct_volume; use crate::volume::direct_volume::is_direct_volume;
pub mod direct_volumes; pub mod direct_volumes;
use direct_volumes::vfio_volume::{is_vfio_volume, VfioVolume};
use std::{sync::Arc, vec::Vec}; use std::{sync::Arc, vec::Vec};
@ -82,7 +80,7 @@ impl VolumeResource {
.with_context(|| format!("new block volume {:?}", m))?, .with_context(|| format!("new block volume {:?}", m))?,
) )
} else if is_direct_volume(m)? { } else if is_direct_volume(m)? {
// handle rawblock volume // handle direct volumes
match direct_volume::handle_direct_volume(d, m, read_only, sid) match direct_volume::handle_direct_volume(d, m, read_only, sid)
.await .await
.context("handle direct volume")? .context("handle direct volume")?
@ -90,12 +88,6 @@ impl VolumeResource {
Some(directvol) => directvol, Some(directvol) => directvol,
None => continue, None => continue,
} }
} else if is_vfio_volume(m) {
Arc::new(
VfioVolume::new(d, m, read_only, sid)
.await
.with_context(|| format!("new vfio volume {:?}", m))?,
)
} else if let Some(options) = } else if let Some(options) =
get_huge_page_option(m).context("failed to check huge page")? get_huge_page_option(m).context("failed to check huge page")?
{ {