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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
// Copyright 2019 Intel Corporation. All Rights Reserved.
// Copyright 2019-2021 Alibaba Cloud. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//! Traits for vhost user backend servers to implement virtio data plain services.
//!
//! Define two traits for vhost user backend servers to implement virtio data plane services.
//! The only difference between the two traits is mutability. The [VhostUserBackend] trait is
//! designed with interior mutability, so the implementor may choose the suitable way to protect
//! itself from concurrent accesses. The [VhostUserBackendMut] is designed without interior
//! mutability, and an implementation of:
//! ```ignore
//! impl<T: VhostUserBackendMut> VhostUserBackend for RwLock<T> { }
//! ```
//! is provided for convenience.
//!
//! [VhostUserBackend]: trait.VhostUserBackend.html
//! [VhostUserBackendMut]: trait.VhostUserBackendMut.html
use std::fs::File;
use std::io::Result;
use std::ops::Deref;
use std::sync::{Arc, Mutex, RwLock};
use vhost::vhost_user::message::{
VhostTransferStateDirection, VhostTransferStatePhase, VhostUserProtocolFeatures,
};
use vhost::vhost_user::Backend;
use vm_memory::bitmap::Bitmap;
use vmm_sys_util::epoll::EventSet;
use vmm_sys_util::eventfd::EventFd;
#[cfg(feature = "gpu-socket")]
use vhost::vhost_user::GpuBackend;
use super::vring::VringT;
use super::GM;
/// Trait with interior mutability for vhost user backend servers to implement concrete services.
///
/// To support multi-threading and asynchronous IO, we enforce `Send + Sync` bound.
pub trait VhostUserBackend: Send + Sync {
type Bitmap: Bitmap + 'static;
type Vring: VringT<GM<Self::Bitmap>>;
/// Get number of queues supported.
fn num_queues(&self) -> usize;
/// Get maximum queue size supported.
fn max_queue_size(&self) -> usize;
/// Get available virtio features.
fn features(&self) -> u64;
/// Set acknowledged virtio features.
fn acked_features(&self, _features: u64) {}
/// Get available vhost protocol features.
fn protocol_features(&self) -> VhostUserProtocolFeatures;
/// Enable or disable the virtio EVENT_IDX feature
fn set_event_idx(&self, enabled: bool);
/// Get virtio device configuration.
///
/// A default implementation is provided as we cannot expect all backends to implement this
/// function.
fn get_config(&self, _offset: u32, _size: u32) -> Vec<u8> {
Vec::new()
}
/// Set virtio device configuration.
///
/// A default implementation is provided as we cannot expect all backends to implement this
/// function.
fn set_config(&self, _offset: u32, _buf: &[u8]) -> Result<()> {
Ok(())
}
/// Update guest memory regions.
fn update_memory(&self, mem: GM<Self::Bitmap>) -> Result<()>;
/// Set handler for communicating with the frontend by the backend communication channel.
///
/// A default implementation is provided as we cannot expect all backends to implement this
/// function.
fn set_backend_req_fd(&self, _backend: Backend) {}
#[cfg(feature = "gpu-socket")]
/// Set handler for communicating with the frontend by the gpu specific backend communication
/// channel.
///
/// This method only exits when the crate feature gpu-socket is enabled, because this is only
/// useful for a gpu device.
fn set_gpu_socket(&self, _gpu_backend: GpuBackend);
/// Get the map to map queue index to worker thread index.
///
/// A return value of [2, 2, 4] means: the first two queues will be handled by worker thread 0,
/// the following two queues will be handled by worker thread 1, and the last four queues will
/// be handled by worker thread 2.
fn queues_per_thread(&self) -> Vec<u64> {
vec![0xffff_ffff]
}
/// Provide an optional exit EventFd for the specified worker thread.
///
/// The returned `EventFd` will be monitored for IO events. When the
/// returned EventFd is written to, the worker thread will exit.
fn exit_event(&self, _thread_index: usize) -> Option<EventFd> {
None
}
/// Handle IO events for backend registered file descriptors.
///
/// This function gets called if the backend registered some additional listeners onto specific
/// file descriptors. The library can handle virtqueues on its own, but does not know what to
/// do with events happening on custom listeners.
fn handle_event(
&self,
device_event: u16,
evset: EventSet,
vrings: &[Self::Vring],
thread_id: usize,
) -> Result<()>;
/// Initiate transfer of internal state for the purpose of migration to/from the back-end.
///
/// Depending on `direction`, the state should either be saved (i.e. serialized and written to
/// `file`) or loaded (i.e. read from `file` and deserialized). The back-end can choose to use
/// a different channel than file. If so, it must return a File that the front-end can use.
/// Note that this function must not block during transfer, i.e. I/O to/from `file` must be
/// done outside of this function.
fn set_device_state_fd(
&self,
_direction: VhostTransferStateDirection,
_phase: VhostTransferStatePhase,
_file: File,
) -> Result<Option<File>> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"back end does not support state transfer",
))
}
/// After transferring internal state, check for any resulting errors, including potential
/// deserialization errors when loading state.
///
/// Although this function return a `Result`, the front-end will not receive any details about
/// this error.
fn check_device_state(&self) -> Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"back end does not support state transfer",
))
}
}
/// Trait without interior mutability for vhost user backend servers to implement concrete services.
pub trait VhostUserBackendMut: Send + Sync {
type Bitmap: Bitmap + 'static;
type Vring: VringT<GM<Self::Bitmap>>;
/// Get number of queues supported.
fn num_queues(&self) -> usize;
/// Get maximum queue size supported.
fn max_queue_size(&self) -> usize;
/// Get available virtio features.
fn features(&self) -> u64;
/// Set acknowledged virtio features.
fn acked_features(&mut self, _features: u64) {}
/// Get available vhost protocol features.
fn protocol_features(&self) -> VhostUserProtocolFeatures;
/// Enable or disable the virtio EVENT_IDX feature
fn set_event_idx(&mut self, enabled: bool);
/// Get virtio device configuration.
///
/// A default implementation is provided as we cannot expect all backends to implement this
/// function.
fn get_config(&self, _offset: u32, _size: u32) -> Vec<u8> {
Vec::new()
}
/// Set virtio device configuration.
///
/// A default implementation is provided as we cannot expect all backends to implement this
/// function.
fn set_config(&mut self, _offset: u32, _buf: &[u8]) -> Result<()> {
Ok(())
}
/// Update guest memory regions.
fn update_memory(&mut self, mem: GM<Self::Bitmap>) -> Result<()>;
/// Set handler for communicating with the frontend by the backend communication channel.
///
/// A default implementation is provided as we cannot expect all backends to implement this
/// function.
fn set_backend_req_fd(&mut self, _backend: Backend) {}
#[cfg(feature = "gpu-socket")]
/// Set handler for communicating with the frontend by the gpu specific backend communication
/// channel.
///
/// This method only exits when the crate feature gpu-socket is enabled, because this is only
/// useful for a gpu device.
fn set_gpu_socket(&mut self, gpu_backend: GpuBackend);
/// Get the map to map queue index to worker thread index.
///
/// A return value of [2, 2, 4] means: the first two queues will be handled by worker thread 0,
/// the following two queues will be handled by worker thread 1, and the last four queues will
/// be handled by worker thread 2.
fn queues_per_thread(&self) -> Vec<u64> {
vec![0xffff_ffff]
}
/// Provide an optional exit EventFd for the specified worker thread.
///
/// If an (`EventFd`, `token`) pair is returned, the returned `EventFd` will be monitored for IO
/// events by using epoll with the specified `token`. When the returned EventFd is written to,
/// the worker thread will exit.
fn exit_event(&self, _thread_index: usize) -> Option<EventFd> {
None
}
/// Handle IO events for backend registered file descriptors.
///
/// This function gets called if the backend registered some additional listeners onto specific
/// file descriptors. The library can handle virtqueues on its own, but does not know what to
/// do with events happening on custom listeners.
fn handle_event(
&mut self,
device_event: u16,
evset: EventSet,
vrings: &[Self::Vring],
thread_id: usize,
) -> Result<()>;
/// Initiate transfer of internal state for the purpose of migration to/from the back-end.
///
/// Depending on `direction`, the state should either be saved (i.e. serialized and written to
/// `file`) or loaded (i.e. read from `file` and deserialized). Note that this function must
/// not block during transfer, i.e. I/O to/from `file` must be done outside of this function.
fn set_device_state_fd(
&mut self,
_direction: VhostTransferStateDirection,
_phase: VhostTransferStatePhase,
_file: File,
) -> Result<Option<File>> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"back end does not support state transfer",
))
}
/// After transferring internal state, check for any resulting errors, including potential
/// deserialization errors when loading state.
///
/// Although this function return a `Result`, the front-end will not receive any details about
/// this error.
fn check_device_state(&self) -> Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Unsupported,
"back end does not support state transfer",
))
}
}
impl<T: VhostUserBackend> VhostUserBackend for Arc<T> {
type Bitmap = T::Bitmap;
type Vring = T::Vring;
fn num_queues(&self) -> usize {
self.deref().num_queues()
}
fn max_queue_size(&self) -> usize {
self.deref().max_queue_size()
}
fn features(&self) -> u64 {
self.deref().features()
}
fn acked_features(&self, features: u64) {
self.deref().acked_features(features)
}
fn protocol_features(&self) -> VhostUserProtocolFeatures {
self.deref().protocol_features()
}
fn set_event_idx(&self, enabled: bool) {
self.deref().set_event_idx(enabled)
}
fn get_config(&self, offset: u32, size: u32) -> Vec<u8> {
self.deref().get_config(offset, size)
}
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
self.deref().set_config(offset, buf)
}
fn update_memory(&self, mem: GM<Self::Bitmap>) -> Result<()> {
self.deref().update_memory(mem)
}
fn set_backend_req_fd(&self, backend: Backend) {
self.deref().set_backend_req_fd(backend)
}
#[cfg(feature = "gpu-socket")]
fn set_gpu_socket(&self, gpu_backend: GpuBackend) {
self.deref().set_gpu_socket(gpu_backend)
}
fn queues_per_thread(&self) -> Vec<u64> {
self.deref().queues_per_thread()
}
fn exit_event(&self, thread_index: usize) -> Option<EventFd> {
self.deref().exit_event(thread_index)
}
fn handle_event(
&self,
device_event: u16,
evset: EventSet,
vrings: &[Self::Vring],
thread_id: usize,
) -> Result<()> {
self.deref()
.handle_event(device_event, evset, vrings, thread_id)
}
fn set_device_state_fd(
&self,
direction: VhostTransferStateDirection,
phase: VhostTransferStatePhase,
file: File,
) -> Result<Option<File>> {
self.deref().set_device_state_fd(direction, phase, file)
}
fn check_device_state(&self) -> Result<()> {
self.deref().check_device_state()
}
}
impl<T: VhostUserBackendMut> VhostUserBackend for Mutex<T> {
type Bitmap = T::Bitmap;
type Vring = T::Vring;
fn num_queues(&self) -> usize {
self.lock().unwrap().num_queues()
}
fn max_queue_size(&self) -> usize {
self.lock().unwrap().max_queue_size()
}
fn features(&self) -> u64 {
self.lock().unwrap().features()
}
fn acked_features(&self, features: u64) {
self.lock().unwrap().acked_features(features)
}
fn protocol_features(&self) -> VhostUserProtocolFeatures {
self.lock().unwrap().protocol_features()
}
fn set_event_idx(&self, enabled: bool) {
self.lock().unwrap().set_event_idx(enabled)
}
fn get_config(&self, offset: u32, size: u32) -> Vec<u8> {
self.lock().unwrap().get_config(offset, size)
}
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
self.lock().unwrap().set_config(offset, buf)
}
fn update_memory(&self, mem: GM<Self::Bitmap>) -> Result<()> {
self.lock().unwrap().update_memory(mem)
}
fn set_backend_req_fd(&self, backend: Backend) {
self.lock().unwrap().set_backend_req_fd(backend)
}
#[cfg(feature = "gpu-socket")]
fn set_gpu_socket(&self, gpu_backend: GpuBackend) {
self.lock().unwrap().set_gpu_socket(gpu_backend)
}
fn queues_per_thread(&self) -> Vec<u64> {
self.lock().unwrap().queues_per_thread()
}
fn exit_event(&self, thread_index: usize) -> Option<EventFd> {
self.lock().unwrap().exit_event(thread_index)
}
fn handle_event(
&self,
device_event: u16,
evset: EventSet,
vrings: &[Self::Vring],
thread_id: usize,
) -> Result<()> {
self.lock()
.unwrap()
.handle_event(device_event, evset, vrings, thread_id)
}
fn set_device_state_fd(
&self,
direction: VhostTransferStateDirection,
phase: VhostTransferStatePhase,
file: File,
) -> Result<Option<File>> {
self.lock()
.unwrap()
.set_device_state_fd(direction, phase, file)
}
fn check_device_state(&self) -> Result<()> {
self.lock().unwrap().check_device_state()
}
}
impl<T: VhostUserBackendMut> VhostUserBackend for RwLock<T> {
type Bitmap = T::Bitmap;
type Vring = T::Vring;
fn num_queues(&self) -> usize {
self.read().unwrap().num_queues()
}
fn max_queue_size(&self) -> usize {
self.read().unwrap().max_queue_size()
}
fn features(&self) -> u64 {
self.read().unwrap().features()
}
fn acked_features(&self, features: u64) {
self.write().unwrap().acked_features(features)
}
fn protocol_features(&self) -> VhostUserProtocolFeatures {
self.read().unwrap().protocol_features()
}
fn set_event_idx(&self, enabled: bool) {
self.write().unwrap().set_event_idx(enabled)
}
fn get_config(&self, offset: u32, size: u32) -> Vec<u8> {
self.read().unwrap().get_config(offset, size)
}
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
self.write().unwrap().set_config(offset, buf)
}
fn update_memory(&self, mem: GM<Self::Bitmap>) -> Result<()> {
self.write().unwrap().update_memory(mem)
}
fn set_backend_req_fd(&self, backend: Backend) {
self.write().unwrap().set_backend_req_fd(backend)
}
#[cfg(feature = "gpu-socket")]
fn set_gpu_socket(&self, gpu_backend: GpuBackend) {
self.write().unwrap().set_gpu_socket(gpu_backend)
}
fn queues_per_thread(&self) -> Vec<u64> {
self.read().unwrap().queues_per_thread()
}
fn exit_event(&self, thread_index: usize) -> Option<EventFd> {
self.read().unwrap().exit_event(thread_index)
}
fn handle_event(
&self,
device_event: u16,
evset: EventSet,
vrings: &[Self::Vring],
thread_id: usize,
) -> Result<()> {
self.write()
.unwrap()
.handle_event(device_event, evset, vrings, thread_id)
}
fn set_device_state_fd(
&self,
direction: VhostTransferStateDirection,
phase: VhostTransferStatePhase,
file: File,
) -> Result<Option<File>> {
self.write()
.unwrap()
.set_device_state_fd(direction, phase, file)
}
fn check_device_state(&self) -> Result<()> {
self.read().unwrap().check_device_state()
}
}
#[cfg(test)]
pub mod tests {
use super::*;
use crate::VringRwLock;
use libc::EFD_NONBLOCK;
use std::sync::Mutex;
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap};
pub struct MockVhostBackend {
events: u64,
event_idx: bool,
acked_features: u64,
exit_event_fds: Vec<EventFd>,
}
impl MockVhostBackend {
pub fn new() -> Self {
let mut backend = MockVhostBackend {
events: 0,
event_idx: false,
acked_features: 0,
exit_event_fds: vec![],
};
// Create a event_fd for each thread. We make it NONBLOCKing in
// order to allow tests maximum flexibility in checking whether
// signals arrived or not.
backend.exit_event_fds = (0..backend.queues_per_thread().len())
.map(|_| EventFd::new(EFD_NONBLOCK).unwrap())
.collect();
backend
}
}
impl VhostUserBackendMut for MockVhostBackend {
type Bitmap = ();
type Vring = VringRwLock;
fn num_queues(&self) -> usize {
2
}
fn max_queue_size(&self) -> usize {
256
}
fn features(&self) -> u64 {
0xffff_ffff_ffff_ffff
}
fn acked_features(&mut self, features: u64) {
self.acked_features = features;
}
fn protocol_features(&self) -> VhostUserProtocolFeatures {
VhostUserProtocolFeatures::all()
}
fn set_event_idx(&mut self, enabled: bool) {
self.event_idx = enabled;
}
fn get_config(&self, offset: u32, size: u32) -> Vec<u8> {
assert_eq!(offset, 0x200);
assert_eq!(size, 8);
vec![0xa5u8; 8]
}
fn set_config(&mut self, offset: u32, buf: &[u8]) -> Result<()> {
assert_eq!(offset, 0x200);
assert_eq!(buf.len(), 8);
assert_eq!(buf, &[0xa5u8; 8]);
Ok(())
}
fn update_memory(&mut self, _atomic_mem: GuestMemoryAtomic<GuestMemoryMmap>) -> Result<()> {
Ok(())
}
fn set_backend_req_fd(&mut self, _backend: Backend) {}
#[cfg(feature = "gpu-socket")]
fn set_gpu_socket(&mut self, _gpu_backend: GpuBackend) {}
fn queues_per_thread(&self) -> Vec<u64> {
vec![1, 1]
}
fn exit_event(&self, thread_index: usize) -> Option<EventFd> {
Some(
self.exit_event_fds
.get(thread_index)?
.try_clone()
.expect("Could not clone exit eventfd"),
)
}
fn handle_event(
&mut self,
_device_event: u16,
_evset: EventSet,
_vrings: &[VringRwLock],
_thread_id: usize,
) -> Result<()> {
self.events += 1;
Ok(())
}
}
#[test]
fn test_new_mock_backend_mutex() {
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
assert_eq!(backend.num_queues(), 2);
assert_eq!(backend.max_queue_size(), 256);
assert_eq!(backend.features(), 0xffff_ffff_ffff_ffff);
assert_eq!(
backend.protocol_features(),
VhostUserProtocolFeatures::all()
);
assert_eq!(backend.queues_per_thread(), [1, 1]);
assert_eq!(backend.get_config(0x200, 8), vec![0xa5; 8]);
backend.set_config(0x200, &[0xa5; 8]).unwrap();
backend.acked_features(0xffff);
assert_eq!(backend.lock().unwrap().acked_features, 0xffff);
backend.set_event_idx(true);
assert!(backend.lock().unwrap().event_idx);
let _ = backend.exit_event(0).unwrap();
let mem = GuestMemoryAtomic::new(
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
backend.update_memory(mem).unwrap();
}
#[test]
fn test_new_mock_backend_rwlock() {
let backend = Arc::new(RwLock::new(MockVhostBackend::new()));
assert_eq!(backend.num_queues(), 2);
assert_eq!(backend.max_queue_size(), 256);
assert_eq!(backend.features(), 0xffff_ffff_ffff_ffff);
assert_eq!(
backend.protocol_features(),
VhostUserProtocolFeatures::all()
);
assert_eq!(backend.queues_per_thread(), [1, 1]);
assert_eq!(backend.get_config(0x200, 8), vec![0xa5; 8]);
backend.set_config(0x200, &[0xa5; 8]).unwrap();
backend.acked_features(0xffff);
assert_eq!(backend.read().unwrap().acked_features, 0xffff);
backend.set_event_idx(true);
assert!(backend.read().unwrap().event_idx);
let _ = backend.exit_event(0).unwrap();
let mem = GuestMemoryAtomic::new(
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
backend.update_memory(mem.clone()).unwrap();
let vring = VringRwLock::new(mem, 0x1000).unwrap();
backend
.handle_event(0x1, EventSet::IN, &[vring], 0)
.unwrap();
}
}