1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
// Copyright 2019 Intel Corporation. All Rights Reserved.
// Copyright 2019-2021 Alibaba Cloud Computing. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//! A simple framework to run a vhost-user backend service.
#[macro_use]
extern crate log;
use std::fmt::{Display, Formatter};
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::thread;
use vhost::vhost_user::{BackendListener, BackendReqHandler, Error as VhostUserError, Listener};
use vm_memory::mmap::NewBitmap;
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
use self::handler::VhostUserHandler;
mod backend;
pub use self::backend::{VhostUserBackend, VhostUserBackendMut};
mod event_loop;
pub use self::event_loop::VringEpollHandler;
mod handler;
pub use self::handler::VhostUserHandlerError;
mod vring;
pub use self::vring::{
VringMutex, VringRwLock, VringState, VringStateGuard, VringStateMutGuard, VringT,
};
/// An alias for `GuestMemoryAtomic<GuestMemoryMmap<B>>` to simplify code.
type GM<B> = GuestMemoryAtomic<GuestMemoryMmap<B>>;
#[derive(Debug)]
/// Errors related to vhost-user daemon.
pub enum Error {
/// Failed to create a new vhost-user handler.
NewVhostUserHandler(VhostUserHandlerError),
/// Failed creating vhost-user backend listener.
CreateBackendListener(VhostUserError),
/// Failed creating vhost-user backend handler.
CreateBackendReqHandler(VhostUserError),
/// Failed creating listener socket
CreateVhostUserListener(VhostUserError),
/// Failed starting daemon thread.
StartDaemon(std::io::Error),
/// Failed waiting for daemon thread.
WaitDaemon(std::boxed::Box<dyn std::any::Any + std::marker::Send>),
/// Failed handling a vhost-user request.
HandleRequest(VhostUserError),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
Error::NewVhostUserHandler(e) => write!(f, "cannot create vhost user handler: {}", e),
Error::CreateBackendListener(e) => write!(f, "cannot create backend listener: {}", e),
Error::CreateBackendReqHandler(e) => {
write!(f, "cannot create backend req handler: {}", e)
}
Error::CreateVhostUserListener(e) => {
write!(f, "cannot create vhost-user listener: {}", e)
}
Error::StartDaemon(e) => write!(f, "failed to start daemon: {}", e),
Error::WaitDaemon(_e) => write!(f, "failed to wait for daemon exit"),
Error::HandleRequest(e) => write!(f, "failed to handle request: {}", e),
}
}
}
/// Result of vhost-user daemon operations.
pub type Result<T> = std::result::Result<T, Error>;
/// Implement a simple framework to run a vhost-user service daemon.
///
/// This structure is the public API the backend is allowed to interact with in order to run
/// a fully functional vhost-user daemon.
pub struct VhostUserDaemon<T: VhostUserBackend> {
name: String,
handler: Arc<Mutex<VhostUserHandler<T>>>,
main_thread: Option<thread::JoinHandle<Result<()>>>,
}
impl<T> VhostUserDaemon<T>
where
T: VhostUserBackend + Clone + 'static,
T::Bitmap: NewBitmap + Clone + Send + Sync,
T::Vring: Clone + Send + Sync,
{
/// Create the daemon instance, providing the backend implementation of `VhostUserBackend`.
///
/// Under the hood, this will start a dedicated thread responsible for listening onto
/// registered event. Those events can be vring events or custom events from the backend,
/// but they get to be registered later during the sequence.
pub fn new(
name: String,
backend: T,
atomic_mem: GuestMemoryAtomic<GuestMemoryMmap<T::Bitmap>>,
) -> Result<Self> {
let handler = Arc::new(Mutex::new(
VhostUserHandler::new(backend, atomic_mem).map_err(Error::NewVhostUserHandler)?,
));
Ok(VhostUserDaemon {
name,
handler,
main_thread: None,
})
}
/// Run a dedicated thread handling all requests coming through the socket.
/// This runs in an infinite loop that should be terminating once the other
/// end of the socket (the VMM) hangs up.
///
/// This function is the common code for starting a new daemon, no matter if
/// it acts as a client or a server.
fn start_daemon(
&mut self,
mut handler: BackendReqHandler<Mutex<VhostUserHandler<T>>>,
) -> Result<()> {
let handle = thread::Builder::new()
.name(self.name.clone())
.spawn(move || loop {
handler.handle_request().map_err(Error::HandleRequest)?;
})
.map_err(Error::StartDaemon)?;
self.main_thread = Some(handle);
Ok(())
}
/// Connect to the vhost-user socket and run a dedicated thread handling
/// all requests coming through this socket. This runs in an infinite loop
/// that should be terminating once the other end of the socket (the VMM)
/// hangs up.
pub fn start_client(&mut self, socket_path: &str) -> Result<()> {
let backend_handler = BackendReqHandler::connect(socket_path, self.handler.clone())
.map_err(Error::CreateBackendReqHandler)?;
self.start_daemon(backend_handler)
}
/// Listen to the vhost-user socket and run a dedicated thread handling all requests coming
/// through this socket.
///
/// This runs in an infinite loop that should be terminating once the other end of the socket
/// (the VMM) disconnects.
///
/// *Note:* A convenience function [VhostUserDaemon::serve] exists that
/// may be a better option than this for simple use-cases.
// TODO: the current implementation has limitations that only one incoming connection will be
// handled from the listener. Should it be enhanced to support reconnection?
pub fn start(&mut self, listener: Listener) -> Result<()> {
let mut backend_listener = BackendListener::new(listener, self.handler.clone())
.map_err(Error::CreateBackendListener)?;
let backend_handler = self.accept(&mut backend_listener)?;
self.start_daemon(backend_handler)
}
fn accept(
&self,
backend_listener: &mut BackendListener<Mutex<VhostUserHandler<T>>>,
) -> Result<BackendReqHandler<Mutex<VhostUserHandler<T>>>> {
loop {
match backend_listener.accept() {
Err(e) => return Err(Error::CreateBackendListener(e)),
Ok(Some(v)) => return Ok(v),
Ok(None) => continue,
}
}
}
/// Wait for the thread handling the vhost-user socket connection to terminate.
///
/// *Note:* A convenience function [VhostUserDaemon::serve] exists that
/// may be a better option than this for simple use-cases.
pub fn wait(&mut self) -> Result<()> {
if let Some(handle) = self.main_thread.take() {
match handle.join().map_err(Error::WaitDaemon)? {
Ok(()) => Ok(()),
Err(Error::HandleRequest(VhostUserError::SocketBroken(_))) => Ok(()),
Err(e) => Err(e),
}
} else {
Ok(())
}
}
/// Bind to socket, handle a single connection and shutdown
///
/// This is a convenience function that provides an easy way to handle the
/// following actions without needing to call the low-level functions:
/// - Create a listener
/// - Start listening
/// - Handle a single event
/// - Send the exit event to all handler threads
///
/// Internal `Err` results that indicate a device disconnect will be treated
/// as success and `Ok(())` will be returned in those cases.
///
/// *Note:* See [VhostUserDaemon::start] and [VhostUserDaemon::wait] if you
/// need more flexibility.
pub fn serve<P: AsRef<Path>>(&mut self, socket: P) -> Result<()> {
let listener = Listener::new(socket, true).map_err(Error::CreateVhostUserListener)?;
self.start(listener)?;
let result = self.wait();
// Regardless of the result, we want to signal worker threads to exit
self.handler.lock().unwrap().send_exit_event();
// For this convenience function we are not treating certain "expected"
// outcomes as error. Disconnects and partial messages can be usual
// behaviour seen from quitting guests.
match &result {
Err(e) => match e {
Error::HandleRequest(VhostUserError::Disconnected) => Ok(()),
Error::HandleRequest(VhostUserError::PartialMessage) => Ok(()),
_ => result,
},
_ => result,
}
}
/// Retrieve the vring epoll handler.
///
/// This is necessary to perform further actions like registering and unregistering some extra
/// event file descriptors.
pub fn get_epoll_handlers(&self) -> Vec<Arc<VringEpollHandler<T>>> {
// Do not expect poisoned lock.
self.handler.lock().unwrap().get_epoll_handlers()
}
}
#[cfg(test)]
mod tests {
use super::backend::tests::MockVhostBackend;
use super::*;
use libc::EAGAIN;
use std::os::unix::net::{UnixListener, UnixStream};
use std::sync::Barrier;
use std::time::Duration;
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap};
#[test]
fn test_new_daemon() {
let mem = GuestMemoryAtomic::new(
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
let handlers = daemon.get_epoll_handlers();
assert_eq!(handlers.len(), 2);
let barrier = Arc::new(Barrier::new(2));
let tmpdir = tempfile::tempdir().unwrap();
let path = tmpdir.path().join("socket");
thread::scope(|s| {
s.spawn(|| {
barrier.wait();
let socket = UnixStream::connect(&path).unwrap();
barrier.wait();
drop(socket)
});
let listener = Listener::new(&path, false).unwrap();
barrier.wait();
daemon.start(listener).unwrap();
barrier.wait();
// Above process generates a `HandleRequest(PartialMessage)` error.
daemon.wait().unwrap_err();
daemon.wait().unwrap();
});
}
#[test]
fn test_new_daemon_client() {
let mem = GuestMemoryAtomic::new(
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
let handlers = daemon.get_epoll_handlers();
assert_eq!(handlers.len(), 2);
let barrier = Arc::new(Barrier::new(2));
let tmpdir = tempfile::tempdir().unwrap();
let path = tmpdir.path().join("socket");
thread::scope(|s| {
s.spawn(|| {
let listener = UnixListener::bind(&path).unwrap();
barrier.wait();
let (stream, _) = listener.accept().unwrap();
barrier.wait();
drop(stream)
});
barrier.wait();
daemon
.start_client(path.as_path().to_str().unwrap())
.unwrap();
barrier.wait();
// Above process generates a `HandleRequest(PartialMessage)` error.
daemon.wait().unwrap_err();
daemon.wait().unwrap();
});
}
#[test]
fn test_daemon_serve() {
let mem = GuestMemoryAtomic::new(
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend.clone(), mem).unwrap();
let tmpdir = tempfile::tempdir().unwrap();
let socket_path = tmpdir.path().join("socket");
thread::scope(|s| {
s.spawn(|| {
let _ = daemon.serve(&socket_path);
});
// We have no way to wait for when the server becomes available...
// So we will have to spin!
while !socket_path.exists() {
thread::sleep(Duration::from_millis(10));
}
// Check that no exit events got triggered yet
for thread_id in 0..backend.queues_per_thread().len() {
let fd = backend.exit_event(thread_id).unwrap();
// Reading from exit fd should fail since nothing was written yet
assert_eq!(
fd.read().unwrap_err().raw_os_error().unwrap(),
EAGAIN,
"exit event should not have been raised yet!"
);
}
let socket = UnixStream::connect(&socket_path).unwrap();
// disconnect immediately again
drop(socket);
});
// Check that exit events got triggered
let backend = backend.lock().unwrap();
for thread_id in 0..backend.queues_per_thread().len() {
let fd = backend.exit_event(thread_id).unwrap();
assert!(fd.read().is_ok(), "No exit event was raised!");
}
}
}