From 5629b7454f64fb99e321bc731903565c6a6a8299 Mon Sep 17 00:00:00 2001 From: Huang Jianan Date: Fri, 21 Apr 2023 15:07:03 +0800 Subject: [PATCH] dragonball: support vhost-user-fs in device manager This patch implements the virtio-fs device used for filesystem sharing and heavily based on the vhost-user protocol. Signed-off-by: Sebastien Boeuf Signed-off-by: Eryu Guan Signed-off-by: Huang Jianan Signed-off-by: Qinqi Qu --- src/dragonball/Cargo.toml | 1 + src/dragonball/src/api/v1/vmm_action.rs | 12 +++---- .../src/device_manager/fs_dev_mgr.rs | 32 +++++++++++++++++++ src/dragonball/src/device_manager/mod.rs | 12 +++---- src/dragonball/src/error.rs | 2 +- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/dragonball/Cargo.toml b/src/dragonball/Cargo.toml index 95680d8dee..b0b59aea82 100644 --- a/src/dragonball/Cargo.toml +++ b/src/dragonball/Cargo.toml @@ -64,3 +64,4 @@ virtio-fs = ["dbs-virtio-devices/virtio-fs-pro", "virtio-queue", "atomic-guest-m virtio-mem = ["dbs-virtio-devices/virtio-mem", "virtio-queue", "atomic-guest-memory"] virtio-balloon = ["dbs-virtio-devices/virtio-balloon", "virtio-queue"] vhost-net = ["dbs-virtio-devices/vhost-net"] +vhost-user-fs = ["dbs-virtio-devices/vhost-user-fs"] \ No newline at end of file diff --git a/src/dragonball/src/api/v1/vmm_action.rs b/src/dragonball/src/api/v1/vmm_action.rs index 97214cb4e3..4f436ef25f 100644 --- a/src/dragonball/src/api/v1/vmm_action.rs +++ b/src/dragonball/src/api/v1/vmm_action.rs @@ -30,7 +30,7 @@ pub use crate::device_manager::balloon_dev_mgr::{BalloonDeviceConfigInfo, Balloo pub use crate::device_manager::blk_dev_mgr::{ BlockDeviceConfigInfo, BlockDeviceConfigUpdateInfo, BlockDeviceError, BlockDeviceMgr, }; -#[cfg(feature = "virtio-fs")] +#[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] pub use crate::device_manager::fs_dev_mgr::{ FsDeviceConfigInfo, FsDeviceConfigUpdateInfo, FsDeviceError, FsDeviceMgr, FsMountConfigInfo, }; @@ -110,7 +110,7 @@ pub enum VmmActionError { /// Vhost-net device relared errors. VhostNet(#[source] VhostNetDeviceError), - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] /// The action `InsertFsDevice` failed either because of bad user input or an internal error. #[error("virtio-fs device error: {0}")] FsDevice(#[source] FsDeviceError), @@ -204,7 +204,7 @@ pub enum VmmAction { /// https://github.com/kata-containers/kata-containers/issues/8327 UpdateNetworkInterface(VirtioNetDeviceConfigUpdateInfo), - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] /// Add a new shared fs device or update one that already exists using the /// `FsDeviceConfig` as input. This action can only be called before the microVM has /// booted. @@ -331,14 +331,14 @@ impl VmmService { VmmAction::UpdateNetworkInterface(netif_update) => { self.update_net_rate_limiters(vmm, netif_update) } - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] VmmAction::InsertFsDevice(fs_cfg) => self.add_fs_device(vmm, fs_cfg), #[cfg(feature = "virtio-fs")] VmmAction::ManipulateFsBackendFs(fs_mount_cfg) => { self.manipulate_fs_backend_fs(vmm, fs_mount_cfg) } - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] VmmAction::UpdateFsDevice(fs_update_cfg) => { self.update_fs_rate_limiters(vmm, fs_update_cfg) } @@ -712,7 +712,7 @@ impl VmmService { .map_err(VmmActionError::VhostNet) } - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] #[instrument(skip(self))] fn add_fs_device(&mut self, vmm: &mut Vmm, config: FsDeviceConfigInfo) -> VmmRequestResult { let vm = vmm.get_vm_mut().ok_or(VmmActionError::InvalidVMID)?; diff --git a/src/dragonball/src/device_manager/fs_dev_mgr.rs b/src/dragonball/src/device_manager/fs_dev_mgr.rs index 771f2b9245..b80817520b 100644 --- a/src/dragonball/src/device_manager/fs_dev_mgr.rs +++ b/src/dragonball/src/device_manager/fs_dev_mgr.rs @@ -360,6 +360,8 @@ impl FsDeviceMgr { ) -> std::result::Result { match &config.mode as &str { VIRTIO_FS_MODE => Self::attach_virtio_fs_devices(config, ctx, epoll_mgr), + #[cfg(feature = "vhost-user-fs")] + VHOSTUSER_FS_MODE => Self::attach_vhostuser_fs_devices(config, ctx, epoll_mgr), _ => Err(FsDeviceError::CreateFsDevice(virtio::Error::InvalidInput)), } } @@ -427,6 +429,36 @@ impl FsDeviceMgr { Ok(device) } + #[cfg(feature = "vhost-user-fs")] + fn attach_vhostuser_fs_devices( + config: &FsDeviceConfigInfo, + ctx: &mut DeviceOpContext, + epoll_mgr: EpollManager, + ) -> std::result::Result { + slog::info!( + ctx.logger(), + "attach vhost-fs device"; + "subsystem" => "vhost-fs", + "tag" => &config.tag, + "dax_window_size" => &config.cache_size, + "sock_path" => &config.sock_path, + ); + + let device = Box::new( + virtio::vhost::vhost_user::fs::VhostUserFs::new( + config.sock_path.clone(), + config.tag.clone(), + config.num_queues, + config.queue_size, + config.cache_size, + epoll_mgr, + ) + .map_err(FsDeviceError::CreateFsDevice)?, + ); + + Ok(device) + } + /// Attach a backend fs to a VirtioFs device or detach a backend /// fs from a Virtiofs device pub fn manipulate_backend_fs( diff --git a/src/dragonball/src/device_manager/mod.rs b/src/dragonball/src/device_manager/mod.rs index 6ec463dd1d..bedcd20793 100644 --- a/src/dragonball/src/device_manager/mod.rs +++ b/src/dragonball/src/device_manager/mod.rs @@ -80,10 +80,10 @@ pub mod virtio_net_dev_mgr; #[cfg(feature = "virtio-net")] use self::virtio_net_dev_mgr::VirtioNetDeviceMgr; -#[cfg(feature = "virtio-fs")] +#[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] /// virtio-block device manager pub mod fs_dev_mgr; -#[cfg(feature = "virtio-fs")] +#[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] use self::fs_dev_mgr::FsDeviceMgr; #[cfg(feature = "virtio-fs")] mod memory_region_handler; @@ -535,7 +535,7 @@ pub struct DeviceManager { #[cfg(feature = "virtio-net")] pub(crate) virtio_net_manager: VirtioNetDeviceMgr, - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] fs_manager: Arc>, #[cfg(feature = "virtio-mem")] @@ -576,7 +576,7 @@ impl DeviceManager { block_manager: BlockDeviceMgr::default(), #[cfg(feature = "virtio-net")] virtio_net_manager: VirtioNetDeviceMgr::default(), - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] fs_manager: Arc::new(Mutex::new(FsDeviceMgr::default())), #[cfg(feature = "virtio-mem")] mem_manager: MemDeviceMgr::default(), @@ -733,7 +733,7 @@ impl DeviceManager { .attach_devices(&mut ctx) .map_err(StartMicroVmError::BlockDeviceError)?; - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] { let mut fs_manager = self.fs_manager.lock().unwrap(); fs_manager @@ -1154,7 +1154,7 @@ mod tests { legacy_manager: None, #[cfg(feature = "virtio-blk")] block_manager: BlockDeviceMgr::default(), - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] fs_manager: Arc::new(Mutex::new(FsDeviceMgr::default())), #[cfg(feature = "virtio-net")] virtio_net_manager: VirtioNetDeviceMgr::default(), diff --git a/src/dragonball/src/error.rs b/src/dragonball/src/error.rs index 126c1dd9fa..bca8f686ef 100644 --- a/src/dragonball/src/error.rs +++ b/src/dragonball/src/error.rs @@ -184,7 +184,7 @@ pub enum StartMicroVmError { #[error("virtio-net errors: {0}")] VirtioNetDeviceError(#[source] device_manager::virtio_net_dev_mgr::VirtioNetDeviceError), - #[cfg(feature = "virtio-fs")] + #[cfg(any(feature = "virtio-fs", feature = "vhost-user-fs"))] /// Virtio-fs errors. #[error("virtio-fs errors: {0}")] FsDeviceError(#[source] device_manager::fs_dev_mgr::FsDeviceError),