diff --git a/vsock/src/main.rs b/vsock/src/main.rs index 5a43591..4ec8655 100644 --- a/vsock/src/main.rs +++ b/vsock/src/main.rs @@ -16,9 +16,36 @@ use std::{ }; use vhost::{vhost_user, vhost_user::Listener}; use vhost_user_backend::VhostUserDaemon; -use vhu_vsock::{VhostUserVsockBackend, VsockArgs, VsockConfig}; +use vhu_vsock::{Error, Result, VhostUserVsockBackend, VsockConfig}; use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap}; +#[derive(Parser, Debug)] +#[clap(version, about, long_about = None)] +struct VsockArgs { + /// Context identifier of the guest which uniquely identifies the device for its lifetime. + #[clap(long, default_value_t = 3)] + guest_cid: u64, + + /// Unix socket to which a hypervisor conencts to and sets up the control path with the device. + #[clap(long)] + socket: String, + + /// Unix socket to which a host-side application connects to. + #[clap(long)] + uds_path: String, +} + +impl TryFrom for VsockConfig { + type Error = Error; + + fn try_from(cmd_args: VsockArgs) -> Result { + let socket = cmd_args.socket.trim().to_string(); + let uds_path = cmd_args.uds_path.trim().to_string(); + + Ok(VsockConfig::new(cmd_args.guest_cid, socket, uds_path)) + } +} + /// This is the public API through which an external program starts the /// vhost-user-vsock backend server. pub(crate) fn start_backend_server(vsock_config: VsockConfig) { @@ -75,6 +102,29 @@ fn main() { mod tests { use super::*; + impl VsockArgs { + fn from_args(guest_cid: u64, socket: &str, uds_path: &str) -> Self { + VsockArgs { + guest_cid, + socket: socket.to_string(), + uds_path: uds_path.to_string(), + } + } + } + + #[test] + fn test_vsock_config_setup() { + let vsock_args = VsockArgs::from_args(3, "/tmp/vhost4.socket", "/tmp/vm4.vsock"); + + let vsock_config = VsockConfig::try_from(vsock_args); + assert!(vsock_config.is_ok()); + + let vsock_config = vsock_config.unwrap(); + assert_eq!(vsock_config.get_guest_cid(), 3); + assert_eq!(vsock_config.get_socket_path(), "/tmp/vhost4.socket"); + assert_eq!(vsock_config.get_uds_path(), "/tmp/vm4.vsock"); + } + #[test] fn test_vsock_server() { const CID: u64 = 3; diff --git a/vsock/src/vhu_vsock.rs b/vsock/src/vhu_vsock.rs index c004907..c1a628b 100644 --- a/vsock/src/vhu_vsock.rs +++ b/vsock/src/vhu_vsock.rs @@ -1,8 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause use super::vhu_vsock_thread::*; -use clap::Parser; -use std::convert::TryFrom; use std::{io, result, sync::Mutex, u16, u32, u64, u8}; use thiserror::Error as ThisError; use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures}; @@ -130,22 +128,6 @@ impl std::convert::From for std::io::Error { } } -#[derive(Parser, Debug)] -#[clap(version, about, long_about = None)] -pub(crate) struct VsockArgs { - /// Context identifier of the guest which uniquely identifies the device for its lifetime. - #[clap(long, default_value_t = 3)] - guest_cid: u64, - - /// Unix socket to which a hypervisor connects to and sets up the control path with the device. - #[clap(long)] - socket: String, - - /// Unix socket to which a host-side application connects to. - #[clap(long)] - uds_path: String, -} - #[derive(Debug, Clone)] /// This structure is the public API through which an external program /// is allowed to configure the backend. @@ -184,17 +166,6 @@ impl VsockConfig { } } -impl TryFrom for VsockConfig { - type Error = Error; - - fn try_from(cmd_args: VsockArgs) -> Result { - let socket = cmd_args.socket.trim().to_string(); - let uds_path = cmd_args.uds_path.trim().to_string(); - - Ok(VsockConfig::new(cmd_args.guest_cid, socket, uds_path)) - } -} - /// A local port and peer port pair used to retrieve /// the corresponding connection. #[derive(Hash, PartialEq, Eq, Debug, Clone)] @@ -351,39 +322,19 @@ mod tests { use vhost_user_backend::VringT; use vm_memory::GuestAddress; - impl VsockArgs { - fn from_args(guest_cid: u64, socket: &str, uds_path: &str) -> Self { - VsockArgs { - guest_cid, - socket: socket.to_string(), - uds_path: uds_path.to_string(), - } - } - } - - #[test] - fn test_vsock_config_setup() { - let vsock_args = VsockArgs::from_args(3, "/tmp/vhost4.socket", "/tmp/vm4.vsock"); - - let vsock_config = VsockConfig::try_from(vsock_args); - assert!(vsock_config.is_ok()); - - let vsock_config = vsock_config.unwrap(); - assert_eq!(vsock_config.get_guest_cid(), 3); - assert_eq!(vsock_config.get_socket_path(), "/tmp/vhost4.socket"); - assert_eq!(vsock_config.get_uds_path(), "/tmp/vm4.vsock"); - } - #[test] fn test_vsock_backend() { const CID: u64 = 3; const VHOST_SOCKET_PATH: &str = "test_vsock_backend.socket"; const VSOCK_SOCKET_PATH: &str = "test_vsock_backend.vsock"; - let vsock_args = VsockArgs::from_args(CID, VHOST_SOCKET_PATH, VSOCK_SOCKET_PATH); - let vsock_config = VsockConfig::try_from(vsock_args); + let vsock_config = VsockConfig::new( + CID, + VHOST_SOCKET_PATH.to_string(), + VSOCK_SOCKET_PATH.to_string(), + ); - let vsock_backend = VhostUserVsockBackend::new(vsock_config.unwrap()); + let vsock_backend = VhostUserVsockBackend::new(vsock_config); assert!(vsock_backend.is_ok()); let mut vsock_backend = vsock_backend.unwrap(); @@ -444,17 +395,22 @@ mod tests { const VHOST_SOCKET_PATH: &str = "test_vsock_backend_failures.socket"; const VSOCK_SOCKET_PATH: &str = "test_vsock_backend_failures.vsock"; - let vsock_args = - VsockArgs::from_args(CID, "/sys/not_allowed.socket", "/sys/not_allowed.vsock"); - let vsock_config = VsockConfig::try_from(vsock_args); + let vsock_config = VsockConfig::new( + CID, + "/sys/not_allowed.socket".to_string(), + "/sys/not_allowed.vsock".to_string(), + ); - let vsock_backend = VhostUserVsockBackend::new(vsock_config.unwrap()); + let vsock_backend = VhostUserVsockBackend::new(vsock_config); assert!(vsock_backend.is_err()); - let vsock_args = VsockArgs::from_args(CID, VHOST_SOCKET_PATH, VSOCK_SOCKET_PATH); - let vsock_config = VsockConfig::try_from(vsock_args); + let vsock_config = VsockConfig::new( + CID, + VHOST_SOCKET_PATH.to_string(), + VSOCK_SOCKET_PATH.to_string(), + ); - let mut vsock_backend = VhostUserVsockBackend::new(vsock_config.unwrap()).unwrap(); + let mut vsock_backend = VhostUserVsockBackend::new(vsock_config).unwrap(); let mem = GuestMemoryAtomic::new( GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), 0x10000)]).unwrap(), );