1fb763c40SRob Bradford // Copyright 2019 Intel Corporation. All Rights Reserved.
2fb763c40SRob Bradford //
3fb763c40SRob Bradford // Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4fb763c40SRob Bradford //
5fb763c40SRob Bradford // Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
6fb763c40SRob Bradford //
7fb763c40SRob Bradford // SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause)
8fb763c40SRob Bradford
9dce82a34SGregory Anders use std::net::{IpAddr, Ipv4Addr};
10a423bf13SSebastien Boeuf use std::ops::Deref;
1171148e1fSSebastien Boeuf use std::os::unix::io::{AsRawFd, RawFd};
128f434df1SSebastien Boeuf use std::sync::{Arc, Mutex, RwLock};
13a3dcaedfSPhilipp Schuster use std::{io, process};
1488a9f799SRob Bradford
1588a9f799SRob Bradford use libc::EFD_NONBLOCK;
1688a9f799SRob Bradford use log::*;
1788a9f799SRob Bradford use net_util::{
1888a9f799SRob Bradford open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio,
1988a9f799SRob Bradford };
2061e57e1cSRuoqing He use option_parser::{OptionParser, OptionParserError, Toggle};
21a3dcaedfSPhilipp Schuster use thiserror::Error;
22fa8fcf5fSSebastien Boeuf use vhost::vhost_user::message::*;
23b8f5911cSRob Bradford use vhost::vhost_user::Listener;
24d10f20ebSRob Bradford use vhost_user_backend::bitmap::BitmapMmapRegion;
2571148e1fSSebastien Boeuf use vhost_user_backend::{VhostUserBackendMut, VhostUserDaemon, VringRwLock, VringT};
261d55de9cSdependabot[bot] use virtio_bindings::virtio_config::{VIRTIO_F_NOTIFY_ON_EMPTY, VIRTIO_F_VERSION_1};
271d55de9cSdependabot[bot] use virtio_bindings::virtio_net::*;
2861e57e1cSRuoqing He use vm_memory::{GuestAddressSpace, GuestMemoryAtomic};
2961e57e1cSRuoqing He use vmm_sys_util::epoll::EventSet;
3061e57e1cSRuoqing He use vmm_sys_util::eventfd::EventFd;
3171148e1fSSebastien Boeuf
32d10f20ebSRob Bradford type GuestMemoryMmap = vm_memory::GuestMemoryMmap<BitmapMmapRegion>;
33fb763c40SRob Bradford
34fb763c40SRob Bradford pub type Result<T> = std::result::Result<T, Error>;
35b8f5911cSRob Bradford type VhostUserBackendResult<T> = std::result::Result<T, std::io::Error>;
36fb763c40SRob Bradford
37a3dcaedfSPhilipp Schuster #[derive(Error, Debug)]
38fb763c40SRob Bradford pub enum Error {
39bc827ee3SZiye Yang /// Failed to create kill eventfd.
40*9bf15ed2SPhilipp Schuster #[error("Failed to create kill eventfd")]
41a3dcaedfSPhilipp Schuster CreateKillEventFd(#[source] io::Error),
42bc827ee3SZiye Yang /// Failed to parse configuration string.
43*9bf15ed2SPhilipp Schuster #[error("Failed to parse configuration string")]
44a3dcaedfSPhilipp Schuster FailedConfigParse(#[source] OptionParserError),
45fb763c40SRob Bradford /// Failed to signal used queue.
46*9bf15ed2SPhilipp Schuster #[error("Failed to signal used queue")]
47a3dcaedfSPhilipp Schuster FailedSignalingUsedQueue(#[source] io::Error),
48fb763c40SRob Bradford /// Failed to handle event other than input event.
49a3dcaedfSPhilipp Schuster #[error("Failed to handle event other than input event")]
50fb763c40SRob Bradford HandleEventNotEpollIn,
51fb763c40SRob Bradford /// Failed to handle unknown event.
52a3dcaedfSPhilipp Schuster #[error("Failed to handle unknown event")]
53fb763c40SRob Bradford HandleEventUnknownEvent,
54bc827ee3SZiye Yang /// Failed to open tap device.
55*9bf15ed2SPhilipp Schuster #[error("Failed to open tap device")]
56a3dcaedfSPhilipp Schuster OpenTap(#[source] OpenTapError),
57bc827ee3SZiye Yang /// No socket provided.
58a3dcaedfSPhilipp Schuster #[error("No socket provided")]
59592de97fSRob Bradford SocketParameterMissing,
60bc827ee3SZiye Yang /// Underlying QueuePair error.
61*9bf15ed2SPhilipp Schuster #[error("Underlying QueuePair error")]
62a3dcaedfSPhilipp Schuster NetQueuePair(#[source] net_util::NetQueuePairError),
63bc827ee3SZiye Yang /// Failed to register the TAP listener.
64*9bf15ed2SPhilipp Schuster #[error("Failed to register the TAP listener")]
65a3dcaedfSPhilipp Schuster RegisterTapListener(#[source] io::Error),
66fb763c40SRob Bradford }
67fb763c40SRob Bradford
683220292dSRob Bradford pub const SYNTAX: &str = "vhost-user-net backend parameters \
698f9ffe54SSebastien Boeuf \"ip=<ip_addr>,mask=<net_mask>,socket=<socket_path>,client=on|off,\
703220292dSRob Bradford num_queues=<number_of_queues>,queue_size=<size_of_each_queue>,tap=<if_name>\"";
713220292dSRob Bradford
72fb763c40SRob Bradford impl std::convert::From<Error> for std::io::Error {
from(e: Error) -> Self73fb763c40SRob Bradford fn from(e: Error) -> Self {
74ea4693a0SJinank Jain std::io::Error::other(e)
75fb763c40SRob Bradford }
76fb763c40SRob Bradford }
77fb763c40SRob Bradford
788f434df1SSebastien Boeuf struct VhostUserNetThread {
79cc51fdb8SRob Bradford net: NetQueuePair,
80fb763c40SRob Bradford kill_evt: EventFd,
81fb763c40SRob Bradford }
82fb763c40SRob Bradford
838f434df1SSebastien Boeuf impl VhostUserNetThread {
84fb763c40SRob Bradford /// Create a new virtio network device with the given TAP interface.
new(tap: Tap) -> Result<Self>851a0a2c01SSebastien Boeuf fn new(tap: Tap) -> Result<Self> {
868f434df1SSebastien Boeuf Ok(VhostUserNetThread {
874587cc7fSSebastien Boeuf kill_evt: EventFd::new(EFD_NONBLOCK).map_err(Error::CreateKillEventFd)?,
88cc51fdb8SRob Bradford net: NetQueuePair {
89b45264afSRob Bradford tap_for_write_epoll: tap.clone(),
901a0a2c01SSebastien Boeuf tap,
91b927dceeSSebastien Boeuf rx: RxVirtio::new(),
92b927dceeSSebastien Boeuf tx: TxVirtio::new(),
93b927dceeSSebastien Boeuf rx_tap_listening: false,
94b45264afSRob Bradford tx_tap_listening: false,
95cc51fdb8SRob Bradford epoll_fd: None,
962b4a0358SRob Bradford counters: NetCounters::default(),
9771148e1fSSebastien Boeuf tap_rx_event_id: 3,
9871148e1fSSebastien Boeuf tap_tx_event_id: 4,
9932ad4982SBo Chen rx_desc_avail: false,
10032ad4982SBo Chen rx_rate_limiter: None,
101b176ddfeSBo Chen tx_rate_limiter: None,
1024becb11aSSebastien Boeuf access_platform: None,
103cc51fdb8SRob Bradford },
104fb763c40SRob Bradford })
105fb763c40SRob Bradford }
106fb763c40SRob Bradford
set_epoll_fd(&mut self, fd: RawFd)10771148e1fSSebastien Boeuf pub fn set_epoll_fd(&mut self, fd: RawFd) {
10871148e1fSSebastien Boeuf self.net.epoll_fd = Some(fd);
109fb763c40SRob Bradford }
110fb763c40SRob Bradford }
111fb763c40SRob Bradford
1128f434df1SSebastien Boeuf pub struct VhostUserNetBackend {
113b927dceeSSebastien Boeuf threads: Vec<Mutex<VhostUserNetThread>>,
1148f434df1SSebastien Boeuf num_queues: usize,
1158f434df1SSebastien Boeuf queue_size: u16,
1166565e478SSebastien Boeuf queues_per_thread: Vec<u64>,
117a423bf13SSebastien Boeuf mem: GuestMemoryAtomic<GuestMemoryMmap>,
1188f434df1SSebastien Boeuf }
1198f434df1SSebastien Boeuf
1208f434df1SSebastien Boeuf impl VhostUserNetBackend {
1213bf3cca7SSebastien Boeuf #[allow(clippy::too_many_arguments)]
new( ip_addr: IpAddr, host_mac: MacAddr, netmask: IpAddr, mtu: Option<u16>, num_queues: usize, queue_size: u16, ifname: Option<&str>, mem: GuestMemoryAtomic<GuestMemoryMmap>, ) -> Result<Self>1228f434df1SSebastien Boeuf fn new(
123dce82a34SGregory Anders ip_addr: IpAddr,
1241b8b5ac1SRob Bradford host_mac: MacAddr,
125dce82a34SGregory Anders netmask: IpAddr,
1263bf3cca7SSebastien Boeuf mtu: Option<u16>,
127a174ad01SSebastien Boeuf num_queues: usize,
1288f434df1SSebastien Boeuf queue_size: u16,
1298f434df1SSebastien Boeuf ifname: Option<&str>,
130a423bf13SSebastien Boeuf mem: GuestMemoryAtomic<GuestMemoryMmap>,
1318f434df1SSebastien Boeuf ) -> Result<Self> {
132a174ad01SSebastien Boeuf let mut taps = open_tap(
1331b8b5ac1SRob Bradford ifname,
1341b8b5ac1SRob Bradford Some(ip_addr),
1351b8b5ac1SRob Bradford Some(netmask),
1369b71ba20SRob Bradford &mut Some(host_mac),
1373bf3cca7SSebastien Boeuf mtu,
1381b8b5ac1SRob Bradford num_queues / 2,
1395db9b0ecSRob Bradford None,
1401b8b5ac1SRob Bradford )
141b927dceeSSebastien Boeuf .map_err(Error::OpenTap)?;
142b927dceeSSebastien Boeuf
1436565e478SSebastien Boeuf let mut queues_per_thread = Vec::new();
144b927dceeSSebastien Boeuf let mut threads = Vec::new();
145a174ad01SSebastien Boeuf for (i, tap) in taps.drain(..).enumerate() {
1461a0a2c01SSebastien Boeuf let thread = Mutex::new(VhostUserNetThread::new(tap)?);
147b927dceeSSebastien Boeuf threads.push(thread);
1486565e478SSebastien Boeuf queues_per_thread.push(0b11 << (i * 2));
149b927dceeSSebastien Boeuf }
1508f434df1SSebastien Boeuf
1518f434df1SSebastien Boeuf Ok(VhostUserNetBackend {
152b927dceeSSebastien Boeuf threads,
1538f434df1SSebastien Boeuf num_queues,
1548f434df1SSebastien Boeuf queue_size,
1556565e478SSebastien Boeuf queues_per_thread,
156a423bf13SSebastien Boeuf mem,
1578f434df1SSebastien Boeuf })
1588f434df1SSebastien Boeuf }
159a174ad01SSebastien Boeuf }
1608f434df1SSebastien Boeuf
1614d7a4c59SBo Chen impl VhostUserBackendMut for VhostUserNetBackend {
162d10f20ebSRob Bradford type Bitmap = BitmapMmapRegion;
1634d7a4c59SBo Chen type Vring = VringRwLock<GuestMemoryAtomic<GuestMemoryMmap>>;
1644d7a4c59SBo Chen
num_queues(&self) -> usize165a174ad01SSebastien Boeuf fn num_queues(&self) -> usize {
166a174ad01SSebastien Boeuf self.num_queues
167a174ad01SSebastien Boeuf }
168a174ad01SSebastien Boeuf
max_queue_size(&self) -> usize169a174ad01SSebastien Boeuf fn max_queue_size(&self) -> usize {
170a174ad01SSebastien Boeuf self.queue_size as usize
171a174ad01SSebastien Boeuf }
172a174ad01SSebastien Boeuf
features(&self) -> u64173a174ad01SSebastien Boeuf fn features(&self) -> u64 {
1741485e172SRob Bradford (1 << VIRTIO_NET_F_GUEST_CSUM)
1751485e172SRob Bradford | (1 << VIRTIO_NET_F_CSUM)
1761485e172SRob Bradford | (1 << VIRTIO_NET_F_GUEST_TSO4)
1771485e172SRob Bradford | (1 << VIRTIO_NET_F_GUEST_TSO6)
1781485e172SRob Bradford | (1 << VIRTIO_NET_F_GUEST_ECN)
1791485e172SRob Bradford | (1 << VIRTIO_NET_F_GUEST_UFO)
1801485e172SRob Bradford | (1 << VIRTIO_NET_F_HOST_TSO4)
1811485e172SRob Bradford | (1 << VIRTIO_NET_F_HOST_TSO6)
1821485e172SRob Bradford | (1 << VIRTIO_NET_F_HOST_ECN)
1832624f17fSRob Bradford | (1 << VIRTIO_NET_F_HOST_UFO)
1842624f17fSRob Bradford | (1 << VIRTIO_NET_F_CTRL_VQ)
1852624f17fSRob Bradford | (1 << VIRTIO_NET_F_MQ)
1862624f17fSRob Bradford | (1 << VIRTIO_NET_F_MAC)
1872624f17fSRob Bradford | (1 << VIRTIO_NET_F_MTU)
1882624f17fSRob Bradford | (1 << VIRTIO_F_NOTIFY_ON_EMPTY)
1892624f17fSRob Bradford | (1 << VIRTIO_F_VERSION_1)
190a174ad01SSebastien Boeuf | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits()
191a174ad01SSebastien Boeuf }
192a174ad01SSebastien Boeuf
protocol_features(&self) -> VhostUserProtocolFeatures193a174ad01SSebastien Boeuf fn protocol_features(&self) -> VhostUserProtocolFeatures {
194a174ad01SSebastien Boeuf VhostUserProtocolFeatures::MQ
195a174ad01SSebastien Boeuf | VhostUserProtocolFeatures::REPLY_ACK
196a174ad01SSebastien Boeuf | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS
197a174ad01SSebastien Boeuf }
198a174ad01SSebastien Boeuf
set_event_idx(&mut self, _enabled: bool)199a174ad01SSebastien Boeuf fn set_event_idx(&mut self, _enabled: bool) {}
200a174ad01SSebastien Boeuf
handle_event( &mut self, device_event: u16, _evset: EventSet, vrings: &[VringRwLock<GuestMemoryAtomic<GuestMemoryMmap>>], thread_id: usize, ) -> VhostUserBackendResult<()>201a174ad01SSebastien Boeuf fn handle_event(
20271148e1fSSebastien Boeuf &mut self,
203fb763c40SRob Bradford device_event: u16,
20471148e1fSSebastien Boeuf _evset: EventSet,
20571148e1fSSebastien Boeuf vrings: &[VringRwLock<GuestMemoryAtomic<GuestMemoryMmap>>],
2061a0a2c01SSebastien Boeuf thread_id: usize,
2074d7a4c59SBo Chen ) -> VhostUserBackendResult<()> {
2081a0a2c01SSebastien Boeuf let mut thread = self.threads[thread_id].lock().unwrap();
209fb763c40SRob Bradford match device_event {
21082d0cdffSSebastien Boeuf 0 => {
2114ed0e1a3SSebastien Boeuf if !thread.net.rx_tap_listening {
2124ed0e1a3SSebastien Boeuf net_util::register_listener(
2134ed0e1a3SSebastien Boeuf thread.net.epoll_fd.unwrap(),
2144ed0e1a3SSebastien Boeuf thread.net.tap.as_raw_fd(),
2154ed0e1a3SSebastien Boeuf epoll::Events::EPOLLIN,
216d9680c4cSRob Bradford u64::from(thread.net.tap_rx_event_id),
2174ed0e1a3SSebastien Boeuf )
2184ed0e1a3SSebastien Boeuf .map_err(Error::RegisterTapListener)?;
2194ed0e1a3SSebastien Boeuf thread.net.rx_tap_listening = true;
220fb763c40SRob Bradford }
221fb763c40SRob Bradford }
22271148e1fSSebastien Boeuf 1 | 4 => {
22371148e1fSSebastien Boeuf let mut vring = vrings[1].get_mut();
224cc51fdb8SRob Bradford if thread
225cc51fdb8SRob Bradford .net
226a423bf13SSebastien Boeuf .process_tx(self.mem.memory().deref(), vring.get_queue_mut())
227cc51fdb8SRob Bradford .map_err(Error::NetQueuePair)?
228cc51fdb8SRob Bradford {
229cc51fdb8SRob Bradford vring
230cc51fdb8SRob Bradford .signal_used_queue()
231cc51fdb8SRob Bradford .map_err(Error::FailedSignalingUsedQueue)?
232cc51fdb8SRob Bradford }
233fb763c40SRob Bradford }
23471148e1fSSebastien Boeuf 3 => {
23571148e1fSSebastien Boeuf let mut vring = vrings[0].get_mut();
236cc51fdb8SRob Bradford if thread
237cc51fdb8SRob Bradford .net
238a423bf13SSebastien Boeuf .process_rx(self.mem.memory().deref(), vring.get_queue_mut())
239cc51fdb8SRob Bradford .map_err(Error::NetQueuePair)?
240fb763c40SRob Bradford {
241cc51fdb8SRob Bradford vring
242cc51fdb8SRob Bradford .signal_used_queue()
243cc51fdb8SRob Bradford .map_err(Error::FailedSignalingUsedQueue)?
244fb763c40SRob Bradford }
245fb763c40SRob Bradford }
246fb763c40SRob Bradford _ => return Err(Error::HandleEventUnknownEvent.into()),
247fb763c40SRob Bradford }
248fb763c40SRob Bradford
2494d7a4c59SBo Chen Ok(())
250fb763c40SRob Bradford }
251700df9ebSRob Bradford
exit_event(&self, thread_index: usize) -> Option<EventFd>25271148e1fSSebastien Boeuf fn exit_event(&self, thread_index: usize) -> Option<EventFd> {
25371148e1fSSebastien Boeuf Some(
254cfffb7edSSebastien Boeuf self.threads[thread_index]
255b927dceeSSebastien Boeuf .lock()
256b927dceeSSebastien Boeuf .unwrap()
257b927dceeSSebastien Boeuf .kill_evt
258b927dceeSSebastien Boeuf .try_clone()
259b927dceeSSebastien Boeuf .unwrap(),
26071148e1fSSebastien Boeuf )
261700df9ebSRob Bradford }
2626565e478SSebastien Boeuf
queues_per_thread(&self) -> Vec<u64>2636565e478SSebastien Boeuf fn queues_per_thread(&self) -> Vec<u64> {
2646565e478SSebastien Boeuf self.queues_per_thread.clone()
2656565e478SSebastien Boeuf }
26671148e1fSSebastien Boeuf
update_memory( &mut self, _mem: GuestMemoryAtomic<GuestMemoryMmap>, ) -> VhostUserBackendResult<()>26771148e1fSSebastien Boeuf fn update_memory(
26871148e1fSSebastien Boeuf &mut self,
26971148e1fSSebastien Boeuf _mem: GuestMemoryAtomic<GuestMemoryMmap>,
27071148e1fSSebastien Boeuf ) -> VhostUserBackendResult<()> {
27171148e1fSSebastien Boeuf Ok(())
27271148e1fSSebastien Boeuf }
273fb763c40SRob Bradford }
274fb763c40SRob Bradford
275592de97fSRob Bradford pub struct VhostUserNetBackendConfig {
276dce82a34SGregory Anders pub ip: IpAddr,
2771b8b5ac1SRob Bradford pub host_mac: MacAddr,
278dce82a34SGregory Anders pub mask: IpAddr,
2793bf3cca7SSebastien Boeuf pub mtu: Option<u16>,
280592de97fSRob Bradford pub socket: String,
281fb763c40SRob Bradford pub num_queues: usize,
282fb763c40SRob Bradford pub queue_size: u16,
283592de97fSRob Bradford pub tap: Option<String>,
2848f9ffe54SSebastien Boeuf pub client: bool,
285fb763c40SRob Bradford }
286fb763c40SRob Bradford
287592de97fSRob Bradford impl VhostUserNetBackendConfig {
parse(backend: &str) -> Result<Self>288592de97fSRob Bradford pub fn parse(backend: &str) -> Result<Self> {
289592de97fSRob Bradford let mut parser = OptionParser::new();
290fb763c40SRob Bradford
291592de97fSRob Bradford parser
292592de97fSRob Bradford .add("tap")
293592de97fSRob Bradford .add("ip")
2941b8b5ac1SRob Bradford .add("host_mac")
295592de97fSRob Bradford .add("mask")
2963bf3cca7SSebastien Boeuf .add("mtu")
297592de97fSRob Bradford .add("queue_size")
298592de97fSRob Bradford .add("num_queues")
2998f9ffe54SSebastien Boeuf .add("socket")
3008f9ffe54SSebastien Boeuf .add("client");
301fb763c40SRob Bradford
302592de97fSRob Bradford parser.parse(backend).map_err(Error::FailedConfigParse)?;
303fb763c40SRob Bradford
304592de97fSRob Bradford let tap = parser.get("tap");
305592de97fSRob Bradford let ip = parser
306592de97fSRob Bradford .convert("ip")
307592de97fSRob Bradford .map_err(Error::FailedConfigParse)?
308dce82a34SGregory Anders .unwrap_or_else(|| IpAddr::V4(Ipv4Addr::new(192, 168, 100, 1)));
3091b8b5ac1SRob Bradford let host_mac = parser
3101b8b5ac1SRob Bradford .convert("host_mac")
3111b8b5ac1SRob Bradford .map_err(Error::FailedConfigParse)?
3121b8b5ac1SRob Bradford .unwrap_or_else(MacAddr::local_random);
313592de97fSRob Bradford let mask = parser
314592de97fSRob Bradford .convert("mask")
315592de97fSRob Bradford .map_err(Error::FailedConfigParse)?
316dce82a34SGregory Anders .unwrap_or_else(|| IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0)));
3173bf3cca7SSebastien Boeuf let mtu = parser.convert("mtu").map_err(Error::FailedConfigParse)?;
318592de97fSRob Bradford let queue_size = parser
319592de97fSRob Bradford .convert("queue_size")
320592de97fSRob Bradford .map_err(Error::FailedConfigParse)?
321592de97fSRob Bradford .unwrap_or(256);
322592de97fSRob Bradford let num_queues = parser
323592de97fSRob Bradford .convert("num_queues")
324592de97fSRob Bradford .map_err(Error::FailedConfigParse)?
325592de97fSRob Bradford .unwrap_or(2);
326592de97fSRob Bradford let socket = parser.get("socket").ok_or(Error::SocketParameterMissing)?;
3278f9ffe54SSebastien Boeuf let client = parser
3288f9ffe54SSebastien Boeuf .convert::<Toggle>("client")
3298f9ffe54SSebastien Boeuf .map_err(Error::FailedConfigParse)?
3308f9ffe54SSebastien Boeuf .unwrap_or(Toggle(false))
3318f9ffe54SSebastien Boeuf .0;
332fb763c40SRob Bradford
333fb763c40SRob Bradford Ok(VhostUserNetBackendConfig {
334fb763c40SRob Bradford ip,
3351b8b5ac1SRob Bradford host_mac,
336fb763c40SRob Bradford mask,
3373bf3cca7SSebastien Boeuf mtu,
338592de97fSRob Bradford socket,
339fb763c40SRob Bradford num_queues,
340fb763c40SRob Bradford queue_size,
34163417362SCathy Zhang tap,
3428f9ffe54SSebastien Boeuf client,
343fb763c40SRob Bradford })
344fb763c40SRob Bradford }
345fb763c40SRob Bradford }
3469fd187c9SRob Bradford
start_net_backend(backend_command: &str)3479fd187c9SRob Bradford pub fn start_net_backend(backend_command: &str) {
3489fd187c9SRob Bradford let backend_config = match VhostUserNetBackendConfig::parse(backend_command) {
3499fd187c9SRob Bradford Ok(config) => config,
3509fd187c9SRob Bradford Err(e) => {
3515e527294SRob Bradford eprintln!("Failed parsing parameters {e:?}");
3529fd187c9SRob Bradford process::exit(1);
3539fd187c9SRob Bradford }
3549fd187c9SRob Bradford };
3559fd187c9SRob Bradford
35673e8fd4dSSebastien Boeuf let tap = backend_config.tap.as_deref();
357592de97fSRob Bradford
358a423bf13SSebastien Boeuf let mem = GuestMemoryAtomic::new(GuestMemoryMmap::new());
359a423bf13SSebastien Boeuf
3609fd187c9SRob Bradford let net_backend = Arc::new(RwLock::new(
3619fd187c9SRob Bradford VhostUserNetBackend::new(
3629fd187c9SRob Bradford backend_config.ip,
3631b8b5ac1SRob Bradford backend_config.host_mac,
3649fd187c9SRob Bradford backend_config.mask,
3653bf3cca7SSebastien Boeuf backend_config.mtu,
3669fd187c9SRob Bradford backend_config.num_queues,
3679fd187c9SRob Bradford backend_config.queue_size,
368592de97fSRob Bradford tap,
369a423bf13SSebastien Boeuf mem.clone(),
3709fd187c9SRob Bradford )
3719fd187c9SRob Bradford .unwrap(),
3729fd187c9SRob Bradford ));
3739fd187c9SRob Bradford
37471148e1fSSebastien Boeuf let mut net_daemon = VhostUserDaemon::new(
37571148e1fSSebastien Boeuf "vhost-user-net-backend".to_string(),
37671148e1fSSebastien Boeuf net_backend.clone(),
377a423bf13SSebastien Boeuf mem,
37871148e1fSSebastien Boeuf )
37971148e1fSSebastien Boeuf .unwrap();
3809fd187c9SRob Bradford
38171148e1fSSebastien Boeuf let epoll_handlers = net_daemon.get_epoll_handlers();
38271148e1fSSebastien Boeuf if epoll_handlers.len() != net_backend.read().unwrap().threads.len() {
383b927dceeSSebastien Boeuf error!("Number of vring workers must be identical to the number of backend threads");
384b927dceeSSebastien Boeuf process::exit(1);
385b927dceeSSebastien Boeuf }
386b927dceeSSebastien Boeuf
38771148e1fSSebastien Boeuf for (index, thread) in net_backend.read().unwrap().threads.iter().enumerate() {
388b927dceeSSebastien Boeuf thread
3898f434df1SSebastien Boeuf .lock()
3908f434df1SSebastien Boeuf .unwrap()
39171148e1fSSebastien Boeuf .set_epoll_fd(epoll_handlers[index].as_raw_fd());
392b927dceeSSebastien Boeuf }
3939fd187c9SRob Bradford
3948f9ffe54SSebastien Boeuf if let Err(e) = if backend_config.client {
3958f9ffe54SSebastien Boeuf net_daemon.start_client(&backend_config.socket)
3968f9ffe54SSebastien Boeuf } else {
39771148e1fSSebastien Boeuf net_daemon.start(Listener::new(&backend_config.socket, true).unwrap())
3988f9ffe54SSebastien Boeuf } {
399b927dceeSSebastien Boeuf error!(
4009fd187c9SRob Bradford "failed to start daemon for vhost-user-net with error: {:?}",
4019fd187c9SRob Bradford e
4029fd187c9SRob Bradford );
4039fd187c9SRob Bradford process::exit(1);
4049fd187c9SRob Bradford }
4059fd187c9SRob Bradford
40697ab767aSRob Bradford if let Err(e) = net_daemon.wait() {
40797ab767aSRob Bradford error!("Error from the main thread: {:?}", e);
40897ab767aSRob Bradford }
40997ab767aSRob Bradford
410b927dceeSSebastien Boeuf for thread in net_backend.read().unwrap().threads.iter() {
411b927dceeSSebastien Boeuf if let Err(e) = thread.lock().unwrap().kill_evt.write(1) {
41297ab767aSRob Bradford error!("Error shutting down worker thread: {:?}", e)
41397ab767aSRob Bradford }
4149fd187c9SRob Bradford }
415b927dceeSSebastien Boeuf }
416