From 8f9ffe54171597a0fac3e472f63f59f0c2a489f1 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Tue, 4 May 2021 22:55:53 +0200 Subject: [PATCH] vhost_user_net: Add client mode Relying on latest changes from vhost_user_backend crate, this patch enables the client mode support for vhost-user-net backend. In this mode, the backend behaves as a client instead of the default mode where it behaves as the server. Signed-off-by: Sebastien Boeuf --- vhost_user_net/src/lib.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/vhost_user_net/src/lib.rs b/vhost_user_net/src/lib.rs index 49322708d..625edd455 100644 --- a/vhost_user_net/src/lib.rs +++ b/vhost_user_net/src/lib.rs @@ -16,6 +16,7 @@ use log::*; use net_util::{ open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio, }; +use option_parser::Toggle; use option_parser::{OptionParser, OptionParserError}; use std::fmt; use std::io::{self}; @@ -75,7 +76,7 @@ pub enum Error { } pub const SYNTAX: &str = "vhost-user-net backend parameters \ -\"ip=,mask=,socket=,\ +\"ip=,mask=,socket=,client=on|off,\ num_queues=,queue_size=,tap=\""; impl fmt::Display for Error { @@ -278,6 +279,7 @@ pub struct VhostUserNetBackendConfig { pub num_queues: usize, pub queue_size: u16, pub tap: Option, + pub client: bool, } impl VhostUserNetBackendConfig { @@ -291,7 +293,8 @@ impl VhostUserNetBackendConfig { .add("mask") .add("queue_size") .add("num_queues") - .add("socket"); + .add("socket") + .add("client"); parser.parse(backend).map_err(Error::FailedConfigParse)?; @@ -317,6 +320,11 @@ impl VhostUserNetBackendConfig { .map_err(Error::FailedConfigParse)? .unwrap_or(2); let socket = parser.get("socket").ok_or(Error::SocketParameterMissing)?; + let client = parser + .convert::("client") + .map_err(Error::FailedConfigParse)? + .unwrap_or(Toggle(false)) + .0; Ok(VhostUserNetBackendConfig { ip, @@ -326,6 +334,7 @@ impl VhostUserNetBackendConfig { num_queues, queue_size, tap, + client, }) } } @@ -353,8 +362,6 @@ pub fn start_net_backend(backend_command: &str) { .unwrap(), )); - let listener = Listener::new(&backend_config.socket, true).unwrap(); - let mut net_daemon = VhostUserDaemon::new("vhost-user-net-backend".to_string(), net_backend.clone()).unwrap(); @@ -372,7 +379,11 @@ pub fn start_net_backend(backend_command: &str) { .set_vring_worker(Some(vring_workers.remove(0))); } - if let Err(e) = net_daemon.start_server(listener) { + if let Err(e) = if backend_config.client { + net_daemon.start_client(&backend_config.socket) + } else { + net_daemon.start_server(Listener::new(&backend_config.socket, true).unwrap()) + } { error!( "failed to start daemon for vhost-user-net with error: {:?}", e