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)
|
|
|
|
|
2020-05-29 14:30:18 +00:00
|
|
|
use libc::{self, EFD_NONBLOCK};
|
2020-01-20 16:10:21 +00:00
|
|
|
use log::*;
|
2020-07-07 15:50:13 +00:00
|
|
|
use net_util::{
|
|
|
|
open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio,
|
|
|
|
};
|
2021-05-04 20:55:53 +00:00
|
|
|
use option_parser::Toggle;
|
2020-07-06 15:41:45 +00:00
|
|
|
use option_parser::{OptionParser, OptionParserError};
|
2020-01-20 16:10:21 +00:00
|
|
|
use std::fmt;
|
2022-07-22 11:40:21 +00:00
|
|
|
use std::io;
|
2020-01-20 16:10:21 +00:00
|
|
|
use std::net::Ipv4Addr;
|
2022-07-06 14:08:08 +00:00
|
|
|
use std::ops::Deref;
|
2022-02-02 10:36:47 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
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;
|
2021-02-25 09:46:27 +00:00
|
|
|
use vhost::vhost_user::message::*;
|
2021-05-11 10:18:19 +00:00
|
|
|
use vhost::vhost_user::Listener;
|
2022-02-02 10:36:47 +00:00
|
|
|
use vhost_user_backend::{VhostUserBackendMut, VhostUserDaemon, VringRwLock, VringT};
|
2023-02-21 23:58:46 +00:00
|
|
|
use virtio_bindings::virtio_config::{VIRTIO_F_NOTIFY_ON_EMPTY, VIRTIO_F_VERSION_1};
|
|
|
|
use virtio_bindings::virtio_net::*;
|
2022-07-06 14:08:08 +00:00
|
|
|
use vm_memory::GuestAddressSpace;
|
2022-02-02 10:36:47 +00:00
|
|
|
use vm_memory::{bitmap::AtomicBitmap, GuestMemoryAtomic};
|
|
|
|
use vmm_sys_util::{epoll::EventSet, eventfd::EventFd};
|
|
|
|
|
|
|
|
type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
|
2020-01-20 16:10:21 +00:00
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
2021-05-11 10:18:19 +00:00
|
|
|
type VhostUserBackendResult<T> = std::result::Result<T, std::io::Error>;
|
2020-01-20 16:10:21 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2021-11-30 07:39:19 +00:00
|
|
|
/// Failed to create kill eventfd.
|
2020-01-24 08:07:25 +00:00
|
|
|
CreateKillEventFd(io::Error),
|
2021-11-30 07:39:19 +00:00
|
|
|
/// Failed to parse configuration string.
|
2020-05-08 15:43:56 +00:00
|
|
|
FailedConfigParse(OptionParserError),
|
2020-01-20 16:10:21 +00:00
|
|
|
/// Failed to signal used queue.
|
2020-05-29 14:30:18 +00:00
|
|
|
FailedSignalingUsedQueue(io::Error),
|
2020-01-20 16:10:21 +00:00
|
|
|
/// Failed to handle event other than input event.
|
|
|
|
HandleEventNotEpollIn,
|
|
|
|
/// Failed to handle unknown event.
|
|
|
|
HandleEventUnknownEvent,
|
2021-11-30 07:39:19 +00:00
|
|
|
/// Failed to open tap device.
|
2020-07-07 14:02:18 +00:00
|
|
|
OpenTap(OpenTapError),
|
2021-11-30 07:39:19 +00:00
|
|
|
/// No socket provided.
|
2020-05-08 15:43:56 +00:00
|
|
|
SocketParameterMissing,
|
2021-11-30 07:39:19 +00:00
|
|
|
/// Underlying QueuePair error.
|
2020-07-07 15:50:13 +00:00
|
|
|
NetQueuePair(net_util::NetQueuePairError),
|
2021-11-30 07:39:19 +00:00
|
|
|
/// Failed to register the TAP listener.
|
2021-02-16 21:17:37 +00:00
|
|
|
RegisterTapListener(io::Error),
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 15:11:00 +00:00
|
|
|
pub const SYNTAX: &str = "vhost-user-net backend parameters \
|
2021-05-04 20:55:53 +00:00
|
|
|
\"ip=<ip_addr>,mask=<net_mask>,socket=<socket_path>,client=on|off,\
|
2020-05-08 15:11:00 +00:00
|
|
|
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 {
|
2022-12-14 11:41:15 +00:00
|
|
|
write!(f, "vhost_user_net_error: {self:?}")
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-29 14:30:18 +00:00
|
|
|
net: NetQueuePair,
|
2020-01-20 16:10:21 +00:00
|
|
|
kill_evt: EventFd,
|
|
|
|
}
|
|
|
|
|
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-24 08:07:25 +00:00
|
|
|
kill_evt: EventFd::new(EFD_NONBLOCK).map_err(Error::CreateKillEventFd)?,
|
2020-05-29 14:30:18 +00:00
|
|
|
net: NetQueuePair {
|
2021-06-28 14:28:26 +00:00
|
|
|
tap_for_write_epoll: tap.clone(),
|
2020-05-29 14:30:18 +00:00
|
|
|
tap,
|
|
|
|
rx: RxVirtio::new(),
|
|
|
|
tx: TxVirtio::new(),
|
|
|
|
rx_tap_listening: false,
|
2021-06-28 14:28:26 +00:00
|
|
|
tx_tap_listening: false,
|
2020-05-29 14:30:18 +00:00
|
|
|
epoll_fd: None,
|
2020-06-23 15:28:41 +00:00
|
|
|
counters: NetCounters::default(),
|
2022-02-02 10:36:47 +00:00
|
|
|
tap_rx_event_id: 3,
|
|
|
|
tap_tx_event_id: 4,
|
2021-03-25 21:17:05 +00:00
|
|
|
rx_desc_avail: false,
|
|
|
|
rx_rate_limiter: None,
|
2021-03-17 22:41:52 +00:00
|
|
|
tx_rate_limiter: None,
|
2022-01-26 16:12:15 +00:00
|
|
|
access_platform: None,
|
2020-05-29 14:30:18 +00:00
|
|
|
},
|
2020-01-20 16:10:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-02 10:36:47 +00:00
|
|
|
pub fn set_epoll_fd(&mut self, fd: RawFd) {
|
|
|
|
self.net.epoll_fd = Some(fd);
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>,
|
2022-07-06 14:08:08 +00:00
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-04-08 16:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VhostUserNetBackend {
|
2022-09-26 14:28:40 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-04-08 16:27:26 +00:00
|
|
|
fn new(
|
|
|
|
ip_addr: Ipv4Addr,
|
2020-05-15 09:00:38 +00:00
|
|
|
host_mac: MacAddr,
|
2020-04-08 16:27:26 +00:00
|
|
|
netmask: Ipv4Addr,
|
2022-09-26 14:28:40 +00:00
|
|
|
mtu: Option<u16>,
|
2021-05-26 12:31:00 +00:00
|
|
|
num_queues: usize,
|
2020-04-08 16:27:26 +00:00
|
|
|
queue_size: u16,
|
|
|
|
ifname: Option<&str>,
|
2022-07-06 14:08:08 +00:00
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-04-08 16:27:26 +00:00
|
|
|
) -> Result<Self> {
|
2021-05-26 12:31:00 +00:00
|
|
|
let mut taps = open_tap(
|
2020-05-15 09:00:38 +00:00
|
|
|
ifname,
|
|
|
|
Some(ip_addr),
|
|
|
|
Some(netmask),
|
2020-06-05 11:00:34 +00:00
|
|
|
&mut Some(host_mac),
|
2022-09-26 14:28:40 +00:00
|
|
|
mtu,
|
2020-05-15 09:00:38 +00:00
|
|
|
num_queues / 2,
|
2021-01-27 17:52:22 +00:00
|
|
|
None,
|
2020-05-15 09:00:38 +00:00
|
|
|
)
|
|
|
|
.map_err(Error::OpenTap)?;
|
2020-04-09 14:35:56 +00:00
|
|
|
|
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();
|
2021-05-26 12:31:00 +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,
|
2022-07-06 14:08:08 +00:00
|
|
|
mem,
|
2020-04-08 16:27:26 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-17 22:48:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:36:47 +00:00
|
|
|
impl VhostUserBackendMut<VringRwLock<GuestMemoryAtomic<GuestMemoryMmap>>, AtomicBitmap>
|
|
|
|
for VhostUserNetBackend
|
|
|
|
{
|
2021-05-17 22:48:36 +00:00
|
|
|
fn num_queues(&self) -> usize {
|
|
|
|
self.num_queues
|
|
|
|
}
|
|
|
|
|
|
|
|
fn max_queue_size(&self) -> usize {
|
|
|
|
self.queue_size as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
fn features(&self) -> u64 {
|
2021-05-20 21:37:26 +00:00
|
|
|
1 << VIRTIO_NET_F_GUEST_CSUM
|
|
|
|
| 1 << VIRTIO_NET_F_CSUM
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_TSO4
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_TSO6
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_ECN
|
|
|
|
| 1 << VIRTIO_NET_F_GUEST_UFO
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_TSO4
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_TSO6
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_ECN
|
|
|
|
| 1 << VIRTIO_NET_F_HOST_UFO
|
2021-05-17 22:48:36 +00:00
|
|
|
| 1 << VIRTIO_NET_F_CTRL_VQ
|
|
|
|
| 1 << VIRTIO_NET_F_MQ
|
2021-05-20 21:37:26 +00:00
|
|
|
| 1 << VIRTIO_NET_F_MAC
|
2022-09-26 14:28:40 +00:00
|
|
|
| 1 << VIRTIO_NET_F_MTU
|
2021-05-26 12:31:00 +00:00
|
|
|
| 1 << VIRTIO_F_NOTIFY_ON_EMPTY
|
|
|
|
| 1 << VIRTIO_F_VERSION_1
|
2021-05-17 22:48:36 +00:00
|
|
|
| VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn protocol_features(&self) -> VhostUserProtocolFeatures {
|
|
|
|
VhostUserProtocolFeatures::MQ
|
|
|
|
| VhostUserProtocolFeatures::REPLY_ACK
|
|
|
|
| VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_event_idx(&mut self, _enabled: bool) {}
|
|
|
|
|
|
|
|
fn handle_event(
|
2022-02-02 10:36:47 +00:00
|
|
|
&mut self,
|
2021-05-17 22:48:36 +00:00
|
|
|
device_event: u16,
|
2022-02-02 10:36:47 +00:00
|
|
|
_evset: EventSet,
|
|
|
|
vrings: &[VringRwLock<GuestMemoryAtomic<GuestMemoryMmap>>],
|
2021-05-17 22:48:36 +00:00
|
|
|
thread_id: usize,
|
|
|
|
) -> VhostUserBackendResult<bool> {
|
2021-05-26 12:31:00 +00:00
|
|
|
let mut thread = self.threads[thread_id].lock().unwrap();
|
|
|
|
match device_event {
|
|
|
|
0 => {
|
|
|
|
if !thread.net.rx_tap_listening {
|
|
|
|
net_util::register_listener(
|
|
|
|
thread.net.epoll_fd.unwrap(),
|
|
|
|
thread.net.tap.as_raw_fd(),
|
|
|
|
epoll::Events::EPOLLIN,
|
2021-06-28 13:51:34 +00:00
|
|
|
u64::from(thread.net.tap_rx_event_id),
|
2021-05-26 12:31:00 +00:00
|
|
|
)
|
|
|
|
.map_err(Error::RegisterTapListener)?;
|
|
|
|
thread.net.rx_tap_listening = true;
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 10:36:47 +00:00
|
|
|
1 | 4 => {
|
|
|
|
let mut vring = vrings[1].get_mut();
|
2021-05-26 12:31:00 +00:00
|
|
|
if thread
|
|
|
|
.net
|
2022-07-06 14:08:08 +00:00
|
|
|
.process_tx(self.mem.memory().deref(), vring.get_queue_mut())
|
2021-05-26 12:31:00 +00:00
|
|
|
.map_err(Error::NetQueuePair)?
|
|
|
|
{
|
|
|
|
vring
|
|
|
|
.signal_used_queue()
|
|
|
|
.map_err(Error::FailedSignalingUsedQueue)?
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 10:36:47 +00:00
|
|
|
3 => {
|
|
|
|
let mut vring = vrings[0].get_mut();
|
2021-05-26 12:31:00 +00:00
|
|
|
if thread
|
|
|
|
.net
|
2022-07-06 14:08:08 +00:00
|
|
|
.process_rx(self.mem.memory().deref(), vring.get_queue_mut())
|
2021-05-26 12:31:00 +00:00
|
|
|
.map_err(Error::NetQueuePair)?
|
|
|
|
{
|
|
|
|
vring
|
|
|
|
.signal_used_queue()
|
|
|
|
.map_err(Error::FailedSignalingUsedQueue)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(Error::HandleEventUnknownEvent.into()),
|
2021-05-17 22:48:36 +00:00
|
|
|
}
|
2021-05-26 12:31:00 +00:00
|
|
|
|
|
|
|
Ok(false)
|
2021-05-17 22:48:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:36:47 +00:00
|
|
|
fn exit_event(&self, thread_index: usize) -> Option<EventFd> {
|
|
|
|
Some(
|
2021-05-26 12:31:00 +00:00
|
|
|
self.threads[thread_index]
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.kill_evt
|
|
|
|
.try_clone()
|
|
|
|
.unwrap(),
|
2022-02-02 10:36:47 +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()
|
|
|
|
}
|
2022-02-02 10:36:47 +00:00
|
|
|
|
|
|
|
fn update_memory(
|
|
|
|
&mut self,
|
|
|
|
_mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
|
|
|
) -> VhostUserBackendResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 15:43:56 +00:00
|
|
|
pub struct VhostUserNetBackendConfig {
|
2020-01-20 16:10:21 +00:00
|
|
|
pub ip: Ipv4Addr,
|
2020-05-15 09:00:38 +00:00
|
|
|
pub host_mac: MacAddr,
|
2020-01-20 16:10:21 +00:00
|
|
|
pub mask: Ipv4Addr,
|
2022-09-26 14:28:40 +00:00
|
|
|
pub mtu: Option<u16>,
|
2020-05-08 15:43:56 +00:00
|
|
|
pub socket: String,
|
2020-01-20 16:10:21 +00:00
|
|
|
pub num_queues: usize,
|
|
|
|
pub queue_size: u16,
|
2020-05-08 15:43:56 +00:00
|
|
|
pub tap: Option<String>,
|
2021-05-04 20:55:53 +00:00
|
|
|
pub client: bool,
|
2020-01-20 16:10:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 15:43:56 +00:00
|
|
|
impl VhostUserNetBackendConfig {
|
|
|
|
pub fn parse(backend: &str) -> Result<Self> {
|
|
|
|
let mut parser = OptionParser::new();
|
|
|
|
|
|
|
|
parser
|
|
|
|
.add("tap")
|
|
|
|
.add("ip")
|
2020-05-15 09:00:38 +00:00
|
|
|
.add("host_mac")
|
2020-05-08 15:43:56 +00:00
|
|
|
.add("mask")
|
2022-09-26 14:28:40 +00:00
|
|
|
.add("mtu")
|
2020-05-08 15:43:56 +00:00
|
|
|
.add("queue_size")
|
|
|
|
.add("num_queues")
|
2021-05-04 20:55:53 +00:00
|
|
|
.add("socket")
|
|
|
|
.add("client");
|
2020-05-08 15:43:56 +00:00
|
|
|
|
|
|
|
parser.parse(backend).map_err(Error::FailedConfigParse)?;
|
|
|
|
|
|
|
|
let tap = parser.get("tap");
|
|
|
|
let ip = parser
|
|
|
|
.convert("ip")
|
|
|
|
.map_err(Error::FailedConfigParse)?
|
|
|
|
.unwrap_or_else(|| Ipv4Addr::new(192, 168, 100, 1));
|
2020-05-15 09:00:38 +00:00
|
|
|
let host_mac = parser
|
|
|
|
.convert("host_mac")
|
|
|
|
.map_err(Error::FailedConfigParse)?
|
|
|
|
.unwrap_or_else(MacAddr::local_random);
|
2020-05-08 15:43:56 +00:00
|
|
|
let mask = parser
|
|
|
|
.convert("mask")
|
|
|
|
.map_err(Error::FailedConfigParse)?
|
|
|
|
.unwrap_or_else(|| Ipv4Addr::new(255, 255, 255, 0));
|
2022-09-26 14:28:40 +00:00
|
|
|
let mtu = parser.convert("mtu").map_err(Error::FailedConfigParse)?;
|
2020-05-08 15:43:56 +00:00
|
|
|
let queue_size = parser
|
|
|
|
.convert("queue_size")
|
|
|
|
.map_err(Error::FailedConfigParse)?
|
|
|
|
.unwrap_or(256);
|
|
|
|
let num_queues = parser
|
|
|
|
.convert("num_queues")
|
|
|
|
.map_err(Error::FailedConfigParse)?
|
|
|
|
.unwrap_or(2);
|
|
|
|
let socket = parser.get("socket").ok_or(Error::SocketParameterMissing)?;
|
2021-05-04 20:55:53 +00:00
|
|
|
let client = parser
|
|
|
|
.convert::<Toggle>("client")
|
|
|
|
.map_err(Error::FailedConfigParse)?
|
|
|
|
.unwrap_or(Toggle(false))
|
|
|
|
.0;
|
2020-01-20 16:10:21 +00:00
|
|
|
|
|
|
|
Ok(VhostUserNetBackendConfig {
|
|
|
|
ip,
|
2020-05-15 09:00:38 +00:00
|
|
|
host_mac,
|
2020-01-20 16:10:21 +00:00
|
|
|
mask,
|
2022-09-26 14:28:40 +00:00
|
|
|
mtu,
|
2020-05-08 15:43:56 +00:00
|
|
|
socket,
|
2020-01-20 16:10:21 +00:00
|
|
|
num_queues,
|
|
|
|
queue_size,
|
2020-03-03 06:40:55 +00:00
|
|
|
tap,
|
2021-05-04 20:55:53 +00:00
|
|
|
client,
|
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) => {
|
2022-12-14 11:41:15 +00:00
|
|
|
eprintln!("Failed parsing parameters {e:?}");
|
2020-01-21 15:16:38 +00:00
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-29 08:23:58 +00:00
|
|
|
let tap = backend_config.tap.as_deref();
|
2020-05-08 15:43:56 +00:00
|
|
|
|
2022-07-06 14:08:08 +00:00
|
|
|
let mem = GuestMemoryAtomic::new(GuestMemoryMmap::new());
|
|
|
|
|
2020-01-21 15:16:38 +00:00
|
|
|
let net_backend = Arc::new(RwLock::new(
|
|
|
|
VhostUserNetBackend::new(
|
|
|
|
backend_config.ip,
|
2020-05-15 09:00:38 +00:00
|
|
|
backend_config.host_mac,
|
2020-01-21 15:16:38 +00:00
|
|
|
backend_config.mask,
|
2022-09-26 14:28:40 +00:00
|
|
|
backend_config.mtu,
|
2020-01-21 15:16:38 +00:00
|
|
|
backend_config.num_queues,
|
|
|
|
backend_config.queue_size,
|
2020-05-08 15:43:56 +00:00
|
|
|
tap,
|
2022-07-06 14:08:08 +00:00
|
|
|
mem.clone(),
|
2020-01-21 15:16:38 +00:00
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
));
|
|
|
|
|
2022-02-02 10:36:47 +00:00
|
|
|
let mut net_daemon = VhostUserDaemon::new(
|
|
|
|
"vhost-user-net-backend".to_string(),
|
|
|
|
net_backend.clone(),
|
2022-07-06 14:08:08 +00:00
|
|
|
mem,
|
2022-02-02 10:36:47 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-04-09 14:35:56 +00:00
|
|
|
|
2022-02-02 10:36:47 +00:00
|
|
|
let epoll_handlers = net_daemon.get_epoll_handlers();
|
|
|
|
if epoll_handlers.len() != net_backend.read().unwrap().threads.len() {
|
2020-04-09 14:35:56 +00:00
|
|
|
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
|
|
|
|
2022-02-02 10:36:47 +00:00
|
|
|
for (index, thread) in net_backend.read().unwrap().threads.iter().enumerate() {
|
2020-04-09 14:35:56 +00:00
|
|
|
thread
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
2022-02-02 10:36:47 +00:00
|
|
|
.set_epoll_fd(epoll_handlers[index].as_raw_fd());
|
2020-04-09 14:35:56 +00:00
|
|
|
}
|
2020-01-21 15:16:38 +00:00
|
|
|
|
2021-05-04 20:55:53 +00:00
|
|
|
if let Err(e) = if backend_config.client {
|
|
|
|
net_daemon.start_client(&backend_config.socket)
|
|
|
|
} else {
|
2022-02-02 10:36:47 +00:00
|
|
|
net_daemon.start(Listener::new(&backend_config.socket, true).unwrap())
|
2021-05-04 20:55:53 +00:00
|
|
|
} {
|
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
|
|
|
}
|