From 9fd187c9fe5ce11f25391297b5d3f360db1031f0 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 21 Jan 2020 15:16:38 +0000 Subject: [PATCH] vhost_user_net: Split launching backend into its own function Split the basic launching functionality into its own function in the newly added vhost_user_net crate. Signed-off-by: Rob Bradford --- Cargo.lock | 1 - src/bin/vhost_user_net.rs | 61 ++------------------------------------- vhost_user_net/src/lib.rs | 55 ++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f03e6f39e..15b918adb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1060,7 +1060,6 @@ name = "vhost_user_net" version = "0.1.0" dependencies = [ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "epoll 4.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/bin/vhost_user_net.rs b/src/bin/vhost_user_net.rs index ee58a7c05..4ead55dd8 100644 --- a/src/bin/vhost_user_net.rs +++ b/src/bin/vhost_user_net.rs @@ -8,16 +8,10 @@ #[macro_use(crate_version, crate_authors)] extern crate clap; -extern crate log; -extern crate vhost_user_backend; extern crate vhost_user_net; use clap::{App, Arg}; -use epoll; -use std::process; -use std::sync::{Arc, RwLock}; -use vhost_user_backend::VhostUserDaemon; -use vhost_user_net::{VhostUserNetBackend, VhostUserNetBackendConfig}; +use vhost_user_net::start_net_backend; fn main() { let cmd_arguments = App::new("vhost-user-net backend") @@ -38,55 +32,6 @@ fn main() { ) .get_matches(); - let vhost_user_net_backend = cmd_arguments.value_of("net-backend").unwrap(); - - let backend_config = match VhostUserNetBackendConfig::parse(vhost_user_net_backend) { - Ok(config) => config, - Err(e) => { - println!("Failed parsing parameters {:?}", e); - process::exit(1); - } - }; - - let net_backend = Arc::new(RwLock::new( - VhostUserNetBackend::new( - backend_config.ip, - backend_config.mask, - backend_config.num_queues, - backend_config.queue_size, - ) - .unwrap(), - )); - - let mut net_daemon = VhostUserDaemon::new( - "vhost-user-net-backend".to_string(), - backend_config.sock.to_string(), - net_backend.clone(), - ) - .unwrap(); - - let (kill_index, kill_evt_fd) = net_backend.read().unwrap().get_kill_event(); - let vring_worker = net_daemon.get_vring_worker(); - - if let Err(e) = - vring_worker.register_listener(kill_evt_fd, epoll::Events::EPOLLIN, u64::from(kill_index)) - { - println!("failed to register listener for kill event: {:?}", e); - process::exit(1); - } - - net_backend - .write() - .unwrap() - .set_vring_worker(Some(vring_worker)); - - if let Err(e) = net_daemon.start() { - println!( - "failed to start daemon for vhost-user-net with error: {:?}", - e - ); - process::exit(1); - } - - net_daemon.wait().unwrap(); + let backend_command = cmd_arguments.value_of("net-backend").unwrap(); + start_net_backend(backend_command); } diff --git a/vhost_user_net/src/lib.rs b/vhost_user_net/src/lib.rs index 5c66a11ac..7c2a52159 100644 --- a/vhost_user_net/src/lib.rs +++ b/vhost_user_net/src/lib.rs @@ -23,11 +23,12 @@ use std::io::{self}; use std::net::Ipv4Addr; use std::os::unix::io::AsRawFd; use std::os::unix::io::RawFd; +use std::process; use std::sync::{Arc, RwLock}; use std::vec::Vec; use vhost_rs::vhost_user::message::*; use vhost_rs::vhost_user::Error as VhostUserError; -use vhost_user_backend::{VhostUserBackend, Vring, VringWorker}; +use vhost_user_backend::{VhostUserBackend, VhostUserDaemon, Vring, VringWorker}; use virtio_bindings::bindings::virtio_net::*; use vm_memory::GuestMemoryMmap; use vm_virtio::net_util::{open_tap, RxVirtio, TxVirtio}; @@ -432,3 +433,55 @@ impl<'a> VhostUserNetBackendConfig<'a> { }) } } + +pub fn start_net_backend(backend_command: &str) { + let backend_config = match VhostUserNetBackendConfig::parse(backend_command) { + Ok(config) => config, + Err(e) => { + println!("Failed parsing parameters {:?}", e); + process::exit(1); + } + }; + + let net_backend = Arc::new(RwLock::new( + VhostUserNetBackend::new( + backend_config.ip, + backend_config.mask, + backend_config.num_queues, + backend_config.queue_size, + ) + .unwrap(), + )); + + let mut net_daemon = VhostUserDaemon::new( + "vhost-user-net-backend".to_string(), + backend_config.sock.to_string(), + net_backend.clone(), + ) + .unwrap(); + + let (kill_index, kill_evt_fd) = net_backend.read().unwrap().get_kill_event(); + let vring_worker = net_daemon.get_vring_worker(); + + if let Err(e) = + vring_worker.register_listener(kill_evt_fd, epoll::Events::EPOLLIN, u64::from(kill_index)) + { + println!("failed to register listener for kill event: {:?}", e); + process::exit(1); + } + + net_backend + .write() + .unwrap() + .set_vring_worker(Some(vring_worker)); + + if let Err(e) = net_daemon.start() { + println!( + "failed to start daemon for vhost-user-net with error: {:?}", + e + ); + process::exit(1); + } + + net_daemon.wait().unwrap(); +}