2020-01-20 16:10:21 +00:00
|
|
|
// Copyright 2019 Intel Corporation. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause)
|
|
|
|
|
|
|
|
extern crate log;
|
|
|
|
extern crate net_util;
|
|
|
|
extern crate vhost_rs;
|
|
|
|
extern crate vhost_user_backend;
|
|
|
|
extern crate vm_virtio;
|
|
|
|
|
|
|
|
use epoll;
|
|
|
|
use libc::{self, EAGAIN, EFD_NONBLOCK};
|
|
|
|
use log::*;
|
|
|
|
use net_util::Tap;
|
|
|
|
use std::fmt;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::io::{self};
|
|
|
|
use std::net::Ipv4Addr;
|
|
|
|
use std::os::unix::io::AsRawFd;
|
2020-01-21 15:16:38 +00:00
|
|
|
use std::process;
|
2020-04-08 16:27:26 +00:00
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2020-01-20 16:10:21 +00:00
|
|
|
use std::vec::Vec;
|
|
|
|
use vhost_rs::vhost_user::message::*;
|
|
|
|
use vhost_rs::vhost_user::Error as VhostUserError;
|
2020-01-21 15:16:38 +00:00
|
|
|
use vhost_user_backend::{VhostUserBackend, VhostUserDaemon, Vring, VringWorker};
|
2020-01-20 16:10:21 +00:00
|
|
|
use virtio_bindings::bindings::virtio_net::*;
|
|
|
|
use vm_memory::GuestMemoryMmap;
|
|
|
|
use vm_virtio::net_util::{open_tap, RxVirtio, TxVirtio};
|
|
|
|
use vm_virtio::Queue;
|
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
pub type VhostUserResult<T> = std::result::Result<T, VhostUserError>;
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
pub type VhostUserBackendResult<T> = std::result::Result<T, std::io::Error>;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// Failed to activate device.
|
|
|
|
BadActivate,
|
|
|
|
/// Failed to create kill eventfd
|
2020-01-24 08:07:25 +00:00
|
|
|
CreateKillEventFd(io::Error),
|
2020-01-20 16:10:21 +00:00
|
|
|
/// Failed to add event.
|
|
|
|
EpollCtl(io::Error),
|
|
|
|
/// Fail to wait event.
|
|
|
|
EpollWait(io::Error),
|
|
|
|
/// Failed to create EventFd.
|
|
|
|
EpollCreateFd,
|
|
|
|
/// Failed to read Tap.
|
|
|
|
FailedReadTap,
|
|
|
|
/// Failed to signal used queue.
|
|
|
|
FailedSignalingUsedQueue,
|
|
|
|
/// Failed to handle event other than input event.
|
|
|
|
HandleEventNotEpollIn,
|
|
|
|
/// Failed to handle unknown event.
|
|
|
|
HandleEventUnknownEvent,
|
|
|
|
/// Invalid vring address.
|
|
|
|
InvalidVringAddr,
|
|
|
|
/// No vring call fd to notify.
|
|
|
|
NoVringCallFdNotify,
|
|
|
|
/// No memory configured.
|
|
|
|
NoMemoryConfigured,
|
|
|
|
/// Failed to parse sock parameter.
|
|
|
|
ParseSockParam,
|
|
|
|
/// Failed to parse ip parameter.
|
2020-01-24 08:07:25 +00:00
|
|
|
ParseIpParam(std::net::AddrParseError),
|
2020-01-20 16:10:21 +00:00
|
|
|
/// Failed to parse mask parameter.
|
2020-01-24 08:07:25 +00:00
|
|
|
ParseMaskParam(std::net::AddrParseError),
|
2020-01-20 16:10:21 +00:00
|
|
|
/// Failed to parse queue number.
|
2020-01-24 08:07:25 +00:00
|
|
|
ParseQueueNumParam(std::num::ParseIntError),
|
2020-01-20 16:10:21 +00:00
|
|
|
/// Failed to parse queue size.
|
2020-01-24 08:07:25 +00:00
|
|
|
ParseQueueSizeParam(std::num::ParseIntError),
|
2020-01-20 16:10:21 +00:00
|
|
|
/// Open tap device failed.
|
|
|
|
OpenTap(vm_virtio::net_util::Error),
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:11:00 +00:00
|
|
|
pub const SYNTAX: &str = "vhost-user-net backend parameters \
|
|
|
|
\"ip=<ip_addr>,mask=<net_mask>,sock=<socket_path>,\
|
|
|
|
num_queues=<number_of_queues>,queue_size=<size_of_each_queue>,tap=<if_name>\"";
|
|
|
|
|
2020-01-20 16:10:21 +00:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "vhost_user_net_error: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Error {}
|
|
|
|
|
|
|
|
impl std::convert::From<Error> for std::io::Error {
|
|
|
|
fn from(e: Error) -> Self {
|
|
|
|
std::io::Error::new(io::ErrorKind::Other, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 16:27:26 +00:00
|
|
|
struct VhostUserNetThread {
|
2020-01-20 16:10:21 +00:00
|
|
|
mem: Option<GuestMemoryMmap>,
|
|
|
|
vring_worker: Option<Arc<VringWorker>>,
|
|
|
|
kill_evt: EventFd,
|
2020-04-09 16:20:38 +00:00
|
|
|
tap: Tap,
|
2020-04-09 14:35:56 +00:00
|
|
|
rx: RxVirtio,
|
|
|
|
tx: TxVirtio,
|
|
|
|
rx_tap_listening: bool,
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:27:26 +00:00
|
|
|
impl VhostUserNetThread {
|
2020-01-20 16:10:21 +00:00
|
|
|
/// Create a new virtio network device with the given TAP interface.
|
2020-04-09 16:20:38 +00:00
|
|
|
fn new(tap: Tap) -> Result<Self> {
|
2020-04-08 16:27:26 +00:00
|
|
|
Ok(VhostUserNetThread {
|
2020-01-20 16:10:21 +00:00
|
|
|
mem: None,
|
|
|
|
vring_worker: None,
|
2020-01-24 08:07:25 +00:00
|
|
|
kill_evt: EventFd::new(EFD_NONBLOCK).map_err(Error::CreateKillEventFd)?,
|
2020-04-09 16:20:38 +00:00
|
|
|
tap,
|
2020-04-09 14:35:56 +00:00
|
|
|
rx: RxVirtio::new(),
|
|
|
|
tx: TxVirtio::new(),
|
|
|
|
rx_tap_listening: false,
|
2020-01-20 16:10:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copies a single frame from `self.rx.frame_buf` into the guest. Returns true
|
|
|
|
// if a buffer was used, and false if the frame must be deferred until a buffer
|
|
|
|
// is made available by the driver.
|
2020-04-09 14:35:56 +00:00
|
|
|
fn rx_single_frame(&mut self, mut queue: &mut Queue) -> Result<bool> {
|
2020-01-20 16:10:21 +00:00
|
|
|
let mem = self.mem.as_ref().ok_or(Error::NoMemoryConfigured)?;
|
|
|
|
|
|
|
|
let next_desc = queue.iter(&mem).next();
|
|
|
|
|
|
|
|
if next_desc.is_none() {
|
|
|
|
// Queue has no available descriptors
|
2020-04-09 14:35:56 +00:00
|
|
|
if self.rx_tap_listening {
|
2020-01-20 16:10:21 +00:00
|
|
|
self.vring_worker
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
2020-04-09 16:20:38 +00:00
|
|
|
.unregister_listener(self.tap.as_raw_fd(), epoll::Events::EPOLLIN, 2)
|
2020-01-20 16:10:21 +00:00
|
|
|
.unwrap();
|
2020-04-09 14:35:56 +00:00
|
|
|
self.rx_tap_listening = false;
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
let write_complete = self.rx.process_desc_chain(&mem, next_desc, &mut queue);
|
2020-01-20 16:10:21 +00:00
|
|
|
|
|
|
|
Ok(write_complete)
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
fn process_rx(&mut self, vring: &mut Vring) -> Result<()> {
|
2020-01-20 16:10:21 +00:00
|
|
|
// Read as many frames as possible.
|
|
|
|
loop {
|
2020-04-09 14:35:56 +00:00
|
|
|
match self.read_tap() {
|
2020-01-20 16:10:21 +00:00
|
|
|
Ok(count) => {
|
2020-04-09 14:35:56 +00:00
|
|
|
self.rx.bytes_read = count;
|
|
|
|
if !self.rx_single_frame(&mut vring.mut_queue())? {
|
|
|
|
self.rx.deferred_frame = true;
|
2020-01-20 16:10:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
// The tap device is non-blocking, so any error aside from EAGAIN is
|
|
|
|
// unexpected.
|
|
|
|
match e.raw_os_error() {
|
|
|
|
Some(err) if err == EAGAIN => (),
|
|
|
|
_ => {
|
|
|
|
error!("Failed to read tap: {:?}", e);
|
|
|
|
return Err(Error::FailedReadTap);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 14:35:56 +00:00
|
|
|
if self.rx.deferred_irqs {
|
|
|
|
self.rx.deferred_irqs = false;
|
2020-01-20 16:10:21 +00:00
|
|
|
vring.signal_used_queue().unwrap();
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
fn resume_rx(&mut self, vring: &mut Vring) -> Result<()> {
|
|
|
|
if self.rx.deferred_frame {
|
|
|
|
if self.rx_single_frame(&mut vring.mut_queue())? {
|
|
|
|
self.rx.deferred_frame = false;
|
2020-01-20 16:10:21 +00:00
|
|
|
// process_rx() was interrupted possibly before consuming all
|
|
|
|
// packets in the tap; try continuing now.
|
2020-04-09 14:35:56 +00:00
|
|
|
self.process_rx(vring)
|
|
|
|
} else if self.rx.deferred_irqs {
|
|
|
|
self.rx.deferred_irqs = false;
|
2020-01-20 16:10:21 +00:00
|
|
|
vring.signal_used_queue().unwrap();
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
fn process_tx(&mut self, mut queue: &mut Queue) -> Result<()> {
|
2020-01-20 16:10:21 +00:00
|
|
|
let mem = self.mem.as_ref().ok_or(Error::NoMemoryConfigured)?;
|
|
|
|
|
2020-04-09 16:20:38 +00:00
|
|
|
self.tx.process_desc_chain(&mem, &mut self.tap, &mut queue);
|
2020-01-20 16:10:21 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
fn read_tap(&mut self) -> io::Result<usize> {
|
2020-04-09 16:20:38 +00:00
|
|
|
self.tap.read(&mut self.rx.frame_buf)
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_vring_worker(&mut self, vring_worker: Option<Arc<VringWorker>>) {
|
|
|
|
self.vring_worker = vring_worker;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 16:27:26 +00:00
|
|
|
pub struct VhostUserNetBackend {
|
2020-04-09 14:35:56 +00:00
|
|
|
threads: Vec<Mutex<VhostUserNetThread>>,
|
2020-04-08 16:27:26 +00:00
|
|
|
num_queues: usize,
|
|
|
|
queue_size: u16,
|
2020-04-09 20:59:13 +00:00
|
|
|
queues_per_thread: Vec<u64>,
|
2020-04-08 16:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VhostUserNetBackend {
|
|
|
|
fn new(
|
|
|
|
ip_addr: Ipv4Addr,
|
|
|
|
netmask: Ipv4Addr,
|
|
|
|
num_queues: usize,
|
|
|
|
queue_size: u16,
|
|
|
|
ifname: Option<&str>,
|
|
|
|
) -> Result<Self> {
|
2020-04-09 14:35:56 +00:00
|
|
|
let mut taps = open_tap(ifname, Some(ip_addr), Some(netmask), num_queues / 2)
|
|
|
|
.map_err(Error::OpenTap)?;
|
|
|
|
|
2020-04-09 20:59:13 +00:00
|
|
|
let mut queues_per_thread = Vec::new();
|
2020-04-09 14:35:56 +00:00
|
|
|
let mut threads = Vec::new();
|
2020-04-09 20:59:13 +00:00
|
|
|
for (i, tap) in taps.drain(..).enumerate() {
|
2020-04-09 16:20:38 +00:00
|
|
|
let thread = Mutex::new(VhostUserNetThread::new(tap)?);
|
2020-04-09 14:35:56 +00:00
|
|
|
threads.push(thread);
|
2020-04-09 20:59:13 +00:00
|
|
|
queues_per_thread.push(0b11 << (i * 2));
|
2020-04-09 14:35:56 +00:00
|
|
|
}
|
2020-04-08 16:27:26 +00:00
|
|
|
|
|
|
|
Ok(VhostUserNetBackend {
|
2020-04-09 14:35:56 +00:00
|
|
|
threads,
|
2020-04-08 16:27:26 +00:00
|
|
|
num_queues,
|
|
|
|
queue_size,
|
2020-04-09 20:59:13 +00:00
|
|
|
queues_per_thread,
|
2020-04-08 16:27:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:10:21 +00:00
|
|
|
impl VhostUserBackend for VhostUserNetBackend {
|
|
|
|
fn num_queues(&self) -> usize {
|
|
|
|
self.num_queues
|
|
|
|
}
|
|
|
|
|
|
|
|
fn max_queue_size(&self) -> usize {
|
|
|
|
self.queue_size as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
fn features(&self) -> u64 {
|
|
|
|
1 << VIRTIO_NET_F_GUEST_CSUM
|
|
|
|
| 1 << VIRTIO_NET_F_CSUM
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_TSO4
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_UFO
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_TSO4
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_UFO
|
|
|
|
| 1 << VIRTIO_F_VERSION_1
|
|
|
|
| VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn protocol_features(&self) -> VhostUserProtocolFeatures {
|
|
|
|
VhostUserProtocolFeatures::all()
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:27:47 +00:00
|
|
|
fn set_event_idx(&mut self, _enabled: bool) {}
|
|
|
|
|
2020-01-20 16:10:21 +00:00
|
|
|
fn update_memory(&mut self, mem: GuestMemoryMmap) -> VhostUserBackendResult<()> {
|
2020-04-09 14:35:56 +00:00
|
|
|
for thread in self.threads.iter() {
|
|
|
|
thread.lock().unwrap().mem = Some(mem.clone());
|
|
|
|
}
|
2020-01-20 16:10:21 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_event(
|
2020-04-09 11:14:05 +00:00
|
|
|
&self,
|
2020-01-20 16:10:21 +00:00
|
|
|
device_event: u16,
|
|
|
|
evset: epoll::Events,
|
|
|
|
vrings: &[Arc<RwLock<Vring>>],
|
2020-04-09 16:20:38 +00:00
|
|
|
thread_id: usize,
|
2020-01-20 16:10:21 +00:00
|
|
|
) -> VhostUserBackendResult<bool> {
|
|
|
|
if evset != epoll::Events::EPOLLIN {
|
|
|
|
return Err(Error::HandleEventNotEpollIn.into());
|
|
|
|
}
|
|
|
|
|
2020-04-09 16:20:38 +00:00
|
|
|
let mut thread = self.threads[thread_id].lock().unwrap();
|
2020-01-20 16:10:21 +00:00
|
|
|
match device_event {
|
2020-04-16 16:57:29 +00:00
|
|
|
0 => {
|
2020-04-09 16:20:38 +00:00
|
|
|
thread.resume_rx(&mut vrings[0].write().unwrap())?;
|
2020-01-20 16:10:21 +00:00
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
if !thread.rx_tap_listening {
|
2020-04-08 16:27:26 +00:00
|
|
|
thread.vring_worker.as_ref().unwrap().register_listener(
|
2020-04-09 16:20:38 +00:00
|
|
|
thread.tap.as_raw_fd(),
|
2020-01-20 16:10:21 +00:00
|
|
|
epoll::Events::EPOLLIN,
|
2020-04-09 16:20:38 +00:00
|
|
|
2,
|
2020-01-20 16:10:21 +00:00
|
|
|
)?;
|
2020-04-09 14:35:56 +00:00
|
|
|
thread.rx_tap_listening = true;
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 16:57:29 +00:00
|
|
|
1 => {
|
2020-04-09 16:20:38 +00:00
|
|
|
thread.process_tx(&mut vrings[1].write().unwrap().mut_queue())?;
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
2020-04-16 16:57:29 +00:00
|
|
|
2 => {
|
2020-04-09 16:20:38 +00:00
|
|
|
let mut vring = vrings[0].write().unwrap();
|
2020-04-09 14:35:56 +00:00
|
|
|
if thread.rx.deferred_frame
|
2020-01-20 16:10:21 +00:00
|
|
|
// Process a deferred frame first if available. Don't read from tap again
|
|
|
|
// until we manage to receive this deferred frame.
|
|
|
|
{
|
2020-04-09 14:35:56 +00:00
|
|
|
if thread.rx_single_frame(&mut vring.mut_queue())? {
|
|
|
|
thread.rx.deferred_frame = false;
|
|
|
|
thread.process_rx(&mut vring)?;
|
|
|
|
} else if thread.rx.deferred_irqs {
|
|
|
|
thread.rx.deferred_irqs = false;
|
2020-01-20 16:10:21 +00:00
|
|
|
vring.signal_used_queue()?;
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-09 14:35:56 +00:00
|
|
|
thread.process_rx(&mut vring)?;
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(Error::HandleEventUnknownEvent.into()),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(false)
|
|
|
|
}
|
2020-02-11 11:27:03 +00:00
|
|
|
|
2020-04-09 15:49:11 +00:00
|
|
|
fn exit_event(&self, thread_index: usize) -> Option<(EventFd, Option<u16>)> {
|
|
|
|
// The exit event is placed after the queues and the tap event, which
|
|
|
|
// is event index 3.
|
2020-04-08 16:27:26 +00:00
|
|
|
Some((
|
2020-04-09 15:49:11 +00:00
|
|
|
self.threads[thread_index]
|
2020-04-09 14:35:56 +00:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.kill_evt
|
|
|
|
.try_clone()
|
|
|
|
.unwrap(),
|
2020-04-09 15:49:11 +00:00
|
|
|
Some(3),
|
2020-04-08 16:27:26 +00:00
|
|
|
))
|
2020-02-11 11:27:03 +00:00
|
|
|
}
|
2020-04-09 20:59:13 +00:00
|
|
|
|
|
|
|
fn queues_per_thread(&self) -> Vec<u64> {
|
|
|
|
self.queues_per_thread.clone()
|
|
|
|
}
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct VhostUserNetBackendConfig<'a> {
|
|
|
|
pub ip: Ipv4Addr,
|
|
|
|
pub mask: Ipv4Addr,
|
|
|
|
pub sock: &'a str,
|
|
|
|
pub num_queues: usize,
|
|
|
|
pub queue_size: u16,
|
2020-03-03 06:40:55 +00:00
|
|
|
pub tap: Option<&'a str>,
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> VhostUserNetBackendConfig<'a> {
|
|
|
|
pub fn parse(backend: &'a str) -> Result<Self> {
|
|
|
|
let params_list: Vec<&str> = backend.split(',').collect();
|
|
|
|
|
|
|
|
let mut ip_str: &str = "";
|
|
|
|
let mut mask_str: &str = "";
|
|
|
|
let mut sock: &str = "";
|
|
|
|
let mut num_queues_str: &str = "";
|
|
|
|
let mut queue_size_str: &str = "";
|
2020-03-03 06:40:55 +00:00
|
|
|
let mut tap_str: &str = "";
|
2020-01-20 16:10:21 +00:00
|
|
|
|
|
|
|
for param in params_list.iter() {
|
|
|
|
if param.starts_with("ip=") {
|
|
|
|
ip_str = ¶m[3..];
|
|
|
|
} else if param.starts_with("mask=") {
|
|
|
|
mask_str = ¶m[5..];
|
|
|
|
} else if param.starts_with("sock=") {
|
|
|
|
sock = ¶m[5..];
|
|
|
|
} else if param.starts_with("num_queues=") {
|
|
|
|
num_queues_str = ¶m[11..];
|
|
|
|
} else if param.starts_with("queue_size=") {
|
|
|
|
queue_size_str = ¶m[11..];
|
2020-03-03 06:40:55 +00:00
|
|
|
} else if param.starts_with("tap=") {
|
|
|
|
tap_str = ¶m[4..];
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut ip: Ipv4Addr = Ipv4Addr::new(192, 168, 100, 1);
|
|
|
|
let mut mask: Ipv4Addr = Ipv4Addr::new(255, 255, 255, 0);
|
|
|
|
let mut num_queues: usize = 2;
|
|
|
|
let mut queue_size: u16 = 256;
|
2020-03-03 06:40:55 +00:00
|
|
|
let mut tap: Option<&str> = None;
|
2020-01-20 16:10:21 +00:00
|
|
|
|
|
|
|
if sock.is_empty() {
|
|
|
|
return Err(Error::ParseSockParam);
|
|
|
|
}
|
|
|
|
if !ip_str.is_empty() {
|
2020-01-24 08:07:25 +00:00
|
|
|
ip = ip_str.parse().map_err(Error::ParseIpParam)?;
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
if !mask_str.is_empty() {
|
2020-01-24 08:07:25 +00:00
|
|
|
mask = mask_str.parse().map_err(Error::ParseMaskParam)?;
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
if !num_queues_str.is_empty() {
|
2020-01-24 08:07:25 +00:00
|
|
|
num_queues = num_queues_str.parse().map_err(Error::ParseQueueNumParam)?;
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
if !queue_size_str.is_empty() {
|
2020-01-24 08:07:25 +00:00
|
|
|
queue_size = queue_size_str.parse().map_err(Error::ParseQueueSizeParam)?;
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
2020-03-03 06:40:55 +00:00
|
|
|
if !tap_str.is_empty() {
|
|
|
|
tap = Some(tap_str);
|
|
|
|
}
|
2020-01-20 16:10:21 +00:00
|
|
|
|
|
|
|
Ok(VhostUserNetBackendConfig {
|
|
|
|
ip,
|
|
|
|
mask,
|
|
|
|
sock,
|
|
|
|
num_queues,
|
|
|
|
queue_size,
|
2020-03-03 06:40:55 +00:00
|
|
|
tap,
|
2020-01-20 16:10:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-21 15:16:38 +00:00
|
|
|
|
|
|
|
pub fn start_net_backend(backend_command: &str) {
|
|
|
|
let backend_config = match VhostUserNetBackendConfig::parse(backend_command) {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(e) => {
|
2020-04-09 14:35:56 +00:00
|
|
|
eprintln!("Failed parsing parameters {:?}", e);
|
2020-01-21 15:16:38 +00:00
|
|
|
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,
|
2020-03-03 06:40:55 +00:00
|
|
|
backend_config.tap,
|
2020-01-21 15:16:38 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
));
|
|
|
|
|
|
|
|
let mut net_daemon = VhostUserDaemon::new(
|
|
|
|
"vhost-user-net-backend".to_string(),
|
|
|
|
backend_config.sock.to_string(),
|
|
|
|
net_backend.clone(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
let mut vring_workers = net_daemon.get_vring_workers();
|
|
|
|
|
|
|
|
if vring_workers.len() != net_backend.read().unwrap().threads.len() {
|
|
|
|
error!("Number of vring workers must be identical to the number of backend threads");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
2020-01-21 15:16:38 +00:00
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
for thread in net_backend.read().unwrap().threads.iter() {
|
|
|
|
thread
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.set_vring_worker(Some(vring_workers.remove(0)));
|
|
|
|
}
|
2020-01-21 15:16:38 +00:00
|
|
|
|
|
|
|
if let Err(e) = net_daemon.start() {
|
2020-04-09 14:35:56 +00:00
|
|
|
error!(
|
2020-01-21 15:16:38 +00:00
|
|
|
"failed to start daemon for vhost-user-net with error: {:?}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
|
2020-02-06 12:17:59 +00:00
|
|
|
if let Err(e) = net_daemon.wait() {
|
|
|
|
error!("Error from the main thread: {:?}", e);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:35:56 +00:00
|
|
|
for thread in net_backend.read().unwrap().threads.iter() {
|
|
|
|
if let Err(e) = thread.lock().unwrap().kill_evt.write(1) {
|
|
|
|
error!("Error shutting down worker thread: {:?}", e)
|
|
|
|
}
|
2020-02-06 12:17:59 +00:00
|
|
|
}
|
2020-01-21 15:16:38 +00:00
|
|
|
}
|