From a4a0751051296c1683bf864a72406220b0cc4e50 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Tue, 4 May 2021 22:27:11 +0200 Subject: [PATCH] vhost_user_backend: Add client mode support Introduce some new code to support running a vhost-user backend as a client instead of the default server mode. Signed-off-by: Sebastien Boeuf --- vhost_user_backend/src/lib.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/vhost_user_backend/src/lib.rs b/vhost_user_backend/src/lib.rs index f6fc4453d..e6bdfa0c8 100644 --- a/vhost_user_backend/src/lib.rs +++ b/vhost_user_backend/src/lib.rs @@ -20,6 +20,7 @@ use vhost::vhost_user::message::{ VhostUserSingleMemoryRegion, VhostUserVirtioFeatures, VhostUserVringAddrFlags, VhostUserVringState, }; +use vhost::vhost_user::SlaveReqHandler; use vhost::vhost_user::{ Error as VhostUserError, Listener, Result as VhostUserResult, SlaveFsCacheReq, SlaveListener, VhostUserSlaveReqHandlerMut, @@ -153,7 +154,7 @@ impl VhostUserDaemon { }) } - /// Connect to the vhost-user socket and run a dedicated thread handling + /// Listen to the vhost-user socket and run a dedicated thread handling /// all requests coming through this socket. This runs in an infinite loop /// that should be terminating once the other end of the socket (the VMM) /// disconnects. @@ -178,6 +179,27 @@ impl VhostUserDaemon { Ok(()) } + /// Connect to the vhost-user socket and run a dedicated thread handling + /// all requests coming through this socket. This runs in an infinite loop + /// that should be terminating once the other end of the socket (the VMM) + /// hangs up. + pub fn start_client(&mut self, socket_path: &str) -> Result<()> { + let mut slave_handler = SlaveReqHandler::connect(socket_path, self.handler.clone()) + .map_err(Error::CreateSlaveReqHandler)?; + let handle = thread::Builder::new() + .name(self.name.clone()) + .spawn(move || loop { + slave_handler + .handle_request() + .map_err(Error::HandleRequest)?; + }) + .map_err(Error::StartDaemon)?; + + self.main_thread = Some(handle); + + Ok(()) + } + /// Wait for the thread handling the vhost-user socket connection to /// terminate. pub fn wait(&mut self) -> Result<()> {