xref: /cloud-hypervisor/vhost_user_net/src/lib.rs (revision eea9bcea38e0c5649f444c829f3a4f9c22aa486c)
1 // Copyright 2019 Intel Corporation. All Rights Reserved.
2 //
3 // Portions Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 //
5 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
6 //
7 // SPDX-License-Identifier: (Apache-2.0 AND BSD-3-Clause)
8 
9 #![allow(clippy::significant_drop_in_scrutinee)]
10 
11 use libc::{self, EFD_NONBLOCK};
12 use log::*;
13 use net_util::{
14     open_tap, MacAddr, NetCounters, NetQueuePair, OpenTapError, RxVirtio, Tap, TxVirtio,
15 };
16 use option_parser::Toggle;
17 use option_parser::{OptionParser, OptionParserError};
18 use std::fmt;
19 use std::io;
20 use std::net::Ipv4Addr;
21 use std::ops::Deref;
22 use std::os::unix::io::{AsRawFd, RawFd};
23 use std::process;
24 use std::sync::{Arc, Mutex, RwLock};
25 use std::vec::Vec;
26 use vhost::vhost_user::message::*;
27 use vhost::vhost_user::Listener;
28 use vhost_user_backend::{VhostUserBackendMut, VhostUserDaemon, VringRwLock, VringT};
29 use virtio_bindings::bindings::virtio_net::*;
30 use vm_memory::GuestAddressSpace;
31 use vm_memory::{bitmap::AtomicBitmap, GuestMemoryAtomic};
32 use vmm_sys_util::{epoll::EventSet, eventfd::EventFd};
33 
34 type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
35 
36 pub type Result<T> = std::result::Result<T, Error>;
37 type VhostUserBackendResult<T> = std::result::Result<T, std::io::Error>;
38 
39 #[derive(Debug)]
40 pub enum Error {
41     /// Failed to create kill eventfd.
42     CreateKillEventFd(io::Error),
43     /// Failed to parse configuration string.
44     FailedConfigParse(OptionParserError),
45     /// Failed to signal used queue.
46     FailedSignalingUsedQueue(io::Error),
47     /// Failed to handle event other than input event.
48     HandleEventNotEpollIn,
49     /// Failed to handle unknown event.
50     HandleEventUnknownEvent,
51     /// Failed to open tap device.
52     OpenTap(OpenTapError),
53     /// No socket provided.
54     SocketParameterMissing,
55     /// Underlying QueuePair error.
56     NetQueuePair(net_util::NetQueuePairError),
57     /// Failed to register the TAP listener.
58     RegisterTapListener(io::Error),
59 }
60 
61 pub const SYNTAX: &str = "vhost-user-net backend parameters \
62 \"ip=<ip_addr>,mask=<net_mask>,socket=<socket_path>,client=on|off,\
63 num_queues=<number_of_queues>,queue_size=<size_of_each_queue>,tap=<if_name>\"";
64 
65 impl fmt::Display for Error {
66     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67         write!(f, "vhost_user_net_error: {:?}", self)
68     }
69 }
70 
71 impl std::error::Error for Error {}
72 
73 impl std::convert::From<Error> for std::io::Error {
74     fn from(e: Error) -> Self {
75         std::io::Error::new(io::ErrorKind::Other, e)
76     }
77 }
78 
79 struct VhostUserNetThread {
80     net: NetQueuePair,
81     kill_evt: EventFd,
82 }
83 
84 impl VhostUserNetThread {
85     /// Create a new virtio network device with the given TAP interface.
86     fn new(tap: Tap) -> Result<Self> {
87         Ok(VhostUserNetThread {
88             kill_evt: EventFd::new(EFD_NONBLOCK).map_err(Error::CreateKillEventFd)?,
89             net: NetQueuePair {
90                 tap_for_write_epoll: tap.clone(),
91                 tap,
92                 rx: RxVirtio::new(),
93                 tx: TxVirtio::new(),
94                 rx_tap_listening: false,
95                 tx_tap_listening: false,
96                 epoll_fd: None,
97                 counters: NetCounters::default(),
98                 tap_rx_event_id: 3,
99                 tap_tx_event_id: 4,
100                 rx_desc_avail: false,
101                 rx_rate_limiter: None,
102                 tx_rate_limiter: None,
103                 access_platform: None,
104             },
105         })
106     }
107 
108     pub fn set_epoll_fd(&mut self, fd: RawFd) {
109         self.net.epoll_fd = Some(fd);
110     }
111 }
112 
113 pub struct VhostUserNetBackend {
114     threads: Vec<Mutex<VhostUserNetThread>>,
115     num_queues: usize,
116     queue_size: u16,
117     queues_per_thread: Vec<u64>,
118     mem: GuestMemoryAtomic<GuestMemoryMmap>,
119 }
120 
121 impl VhostUserNetBackend {
122     #[allow(clippy::too_many_arguments)]
123     fn new(
124         ip_addr: Ipv4Addr,
125         host_mac: MacAddr,
126         netmask: Ipv4Addr,
127         mtu: Option<u16>,
128         num_queues: usize,
129         queue_size: u16,
130         ifname: Option<&str>,
131         mem: GuestMemoryAtomic<GuestMemoryMmap>,
132     ) -> Result<Self> {
133         let mut taps = open_tap(
134             ifname,
135             Some(ip_addr),
136             Some(netmask),
137             &mut Some(host_mac),
138             mtu,
139             num_queues / 2,
140             None,
141         )
142         .map_err(Error::OpenTap)?;
143 
144         let mut queues_per_thread = Vec::new();
145         let mut threads = Vec::new();
146         for (i, tap) in taps.drain(..).enumerate() {
147             let thread = Mutex::new(VhostUserNetThread::new(tap)?);
148             threads.push(thread);
149             queues_per_thread.push(0b11 << (i * 2));
150         }
151 
152         Ok(VhostUserNetBackend {
153             threads,
154             num_queues,
155             queue_size,
156             queues_per_thread,
157             mem,
158         })
159     }
160 }
161 
162 impl VhostUserBackendMut<VringRwLock<GuestMemoryAtomic<GuestMemoryMmap>>, AtomicBitmap>
163     for VhostUserNetBackend
164 {
165     fn num_queues(&self) -> usize {
166         self.num_queues
167     }
168 
169     fn max_queue_size(&self) -> usize {
170         self.queue_size as usize
171     }
172 
173     fn features(&self) -> u64 {
174         1 << VIRTIO_NET_F_GUEST_CSUM
175             | 1 << VIRTIO_NET_F_CSUM
176             | 1 << VIRTIO_NET_F_GUEST_TSO4
177             | 1 << VIRTIO_NET_F_GUEST_TSO6
178             | 1 << VIRTIO_NET_F_GUEST_ECN
179             | 1 << VIRTIO_NET_F_GUEST_UFO
180             | 1 << VIRTIO_NET_F_HOST_TSO4
181             | 1 << VIRTIO_NET_F_HOST_TSO6
182             | 1 << VIRTIO_NET_F_HOST_ECN
183             | 1 << VIRTIO_NET_F_HOST_UFO
184             | 1 << VIRTIO_NET_F_CTRL_VQ
185             | 1 << VIRTIO_NET_F_MQ
186             | 1 << VIRTIO_NET_F_MAC
187             | 1 << VIRTIO_NET_F_MTU
188             | 1 << VIRTIO_F_NOTIFY_ON_EMPTY
189             | 1 << VIRTIO_F_VERSION_1
190             | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits()
191     }
192 
193     fn protocol_features(&self) -> VhostUserProtocolFeatures {
194         VhostUserProtocolFeatures::MQ
195             | VhostUserProtocolFeatures::REPLY_ACK
196             | VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS
197     }
198 
199     fn set_event_idx(&mut self, _enabled: bool) {}
200 
201     fn handle_event(
202         &mut self,
203         device_event: u16,
204         _evset: EventSet,
205         vrings: &[VringRwLock<GuestMemoryAtomic<GuestMemoryMmap>>],
206         thread_id: usize,
207     ) -> VhostUserBackendResult<bool> {
208         let mut thread = self.threads[thread_id].lock().unwrap();
209         match device_event {
210             0 => {
211                 if !thread.net.rx_tap_listening {
212                     net_util::register_listener(
213                         thread.net.epoll_fd.unwrap(),
214                         thread.net.tap.as_raw_fd(),
215                         epoll::Events::EPOLLIN,
216                         u64::from(thread.net.tap_rx_event_id),
217                     )
218                     .map_err(Error::RegisterTapListener)?;
219                     thread.net.rx_tap_listening = true;
220                 }
221             }
222             1 | 4 => {
223                 let mut vring = vrings[1].get_mut();
224                 if thread
225                     .net
226                     .process_tx(self.mem.memory().deref(), vring.get_queue_mut())
227                     .map_err(Error::NetQueuePair)?
228                 {
229                     vring
230                         .signal_used_queue()
231                         .map_err(Error::FailedSignalingUsedQueue)?
232                 }
233             }
234             3 => {
235                 let mut vring = vrings[0].get_mut();
236                 if thread
237                     .net
238                     .process_rx(self.mem.memory().deref(), vring.get_queue_mut())
239                     .map_err(Error::NetQueuePair)?
240                 {
241                     vring
242                         .signal_used_queue()
243                         .map_err(Error::FailedSignalingUsedQueue)?
244                 }
245             }
246             _ => return Err(Error::HandleEventUnknownEvent.into()),
247         }
248 
249         Ok(false)
250     }
251 
252     fn exit_event(&self, thread_index: usize) -> Option<EventFd> {
253         Some(
254             self.threads[thread_index]
255                 .lock()
256                 .unwrap()
257                 .kill_evt
258                 .try_clone()
259                 .unwrap(),
260         )
261     }
262 
263     fn queues_per_thread(&self) -> Vec<u64> {
264         self.queues_per_thread.clone()
265     }
266 
267     fn update_memory(
268         &mut self,
269         _mem: GuestMemoryAtomic<GuestMemoryMmap>,
270     ) -> VhostUserBackendResult<()> {
271         Ok(())
272     }
273 }
274 
275 pub struct VhostUserNetBackendConfig {
276     pub ip: Ipv4Addr,
277     pub host_mac: MacAddr,
278     pub mask: Ipv4Addr,
279     pub mtu: Option<u16>,
280     pub socket: String,
281     pub num_queues: usize,
282     pub queue_size: u16,
283     pub tap: Option<String>,
284     pub client: bool,
285 }
286 
287 impl VhostUserNetBackendConfig {
288     pub fn parse(backend: &str) -> Result<Self> {
289         let mut parser = OptionParser::new();
290 
291         parser
292             .add("tap")
293             .add("ip")
294             .add("host_mac")
295             .add("mask")
296             .add("mtu")
297             .add("queue_size")
298             .add("num_queues")
299             .add("socket")
300             .add("client");
301 
302         parser.parse(backend).map_err(Error::FailedConfigParse)?;
303 
304         let tap = parser.get("tap");
305         let ip = parser
306             .convert("ip")
307             .map_err(Error::FailedConfigParse)?
308             .unwrap_or_else(|| Ipv4Addr::new(192, 168, 100, 1));
309         let host_mac = parser
310             .convert("host_mac")
311             .map_err(Error::FailedConfigParse)?
312             .unwrap_or_else(MacAddr::local_random);
313         let mask = parser
314             .convert("mask")
315             .map_err(Error::FailedConfigParse)?
316             .unwrap_or_else(|| Ipv4Addr::new(255, 255, 255, 0));
317         let mtu = parser.convert("mtu").map_err(Error::FailedConfigParse)?;
318         let queue_size = parser
319             .convert("queue_size")
320             .map_err(Error::FailedConfigParse)?
321             .unwrap_or(256);
322         let num_queues = parser
323             .convert("num_queues")
324             .map_err(Error::FailedConfigParse)?
325             .unwrap_or(2);
326         let socket = parser.get("socket").ok_or(Error::SocketParameterMissing)?;
327         let client = parser
328             .convert::<Toggle>("client")
329             .map_err(Error::FailedConfigParse)?
330             .unwrap_or(Toggle(false))
331             .0;
332 
333         Ok(VhostUserNetBackendConfig {
334             ip,
335             host_mac,
336             mask,
337             mtu,
338             socket,
339             num_queues,
340             queue_size,
341             tap,
342             client,
343         })
344     }
345 }
346 
347 pub fn start_net_backend(backend_command: &str) {
348     let backend_config = match VhostUserNetBackendConfig::parse(backend_command) {
349         Ok(config) => config,
350         Err(e) => {
351             eprintln!("Failed parsing parameters {:?}", e);
352             process::exit(1);
353         }
354     };
355 
356     let tap = backend_config.tap.as_deref();
357 
358     let mem = GuestMemoryAtomic::new(GuestMemoryMmap::new());
359 
360     let net_backend = Arc::new(RwLock::new(
361         VhostUserNetBackend::new(
362             backend_config.ip,
363             backend_config.host_mac,
364             backend_config.mask,
365             backend_config.mtu,
366             backend_config.num_queues,
367             backend_config.queue_size,
368             tap,
369             mem.clone(),
370         )
371         .unwrap(),
372     ));
373 
374     let mut net_daemon = VhostUserDaemon::new(
375         "vhost-user-net-backend".to_string(),
376         net_backend.clone(),
377         mem,
378     )
379     .unwrap();
380 
381     let epoll_handlers = net_daemon.get_epoll_handlers();
382     if epoll_handlers.len() != net_backend.read().unwrap().threads.len() {
383         error!("Number of vring workers must be identical to the number of backend threads");
384         process::exit(1);
385     }
386 
387     for (index, thread) in net_backend.read().unwrap().threads.iter().enumerate() {
388         thread
389             .lock()
390             .unwrap()
391             .set_epoll_fd(epoll_handlers[index].as_raw_fd());
392     }
393 
394     if let Err(e) = if backend_config.client {
395         net_daemon.start_client(&backend_config.socket)
396     } else {
397         net_daemon.start(Listener::new(&backend_config.socket, true).unwrap())
398     } {
399         error!(
400             "failed to start daemon for vhost-user-net with error: {:?}",
401             e
402         );
403         process::exit(1);
404     }
405 
406     if let Err(e) = net_daemon.wait() {
407         error!("Error from the main thread: {:?}", e);
408     }
409 
410     for thread in net_backend.read().unwrap().threads.iter() {
411         if let Err(e) = thread.lock().unwrap().kill_evt.write(1) {
412             error!("Error shutting down worker thread: {:?}", e)
413         }
414     }
415 }
416