vmm/devices/virtio/mod.rs
1// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
5// Use of this source code is governed by a BSD-style license that can be
6// found in the THIRD-PARTY file.
7
8//! Implements virtio devices, queues, and transport mechanisms.
9
10use std::any::Any;
11
12use self::queue::QueueError;
13use crate::devices::virtio::net::TapError;
14
15pub mod balloon;
16pub mod block;
17pub mod device;
18pub mod generated;
19mod iov_deque;
20pub mod iovec;
21pub mod mem;
22pub mod net;
23pub mod persist;
24pub mod pmem;
25pub mod queue;
26pub mod rng;
27pub mod test_utils;
28pub mod transport;
29pub mod vhost_user;
30pub mod vhost_user_metrics;
31pub mod vsock;
32
33/// When the driver initializes the device, it lets the device know about the
34/// completed stages using the Device Status Field.
35///
36/// These following consts are defined in the order in which the bits would
37/// typically be set by the driver. INIT -> ACKNOWLEDGE -> DRIVER and so on.
38///
39/// This module is a 1:1 mapping for the Device Status Field in the virtio 1.0
40/// specification, section 2.1.
41mod device_status {
42 pub const INIT: u32 = 0;
43 pub const ACKNOWLEDGE: u32 = 1;
44 pub const DRIVER: u32 = 2;
45 pub const FAILED: u32 = 128;
46 pub const FEATURES_OK: u32 = 8;
47 pub const DRIVER_OK: u32 = 4;
48 pub const DEVICE_NEEDS_RESET: u32 = 64;
49}
50
51/// Offset from the base MMIO address of a virtio device used by the guest to notify the device of
52/// queue events.
53pub const NOTIFY_REG_OFFSET: u32 = 0x50;
54
55/// Errors triggered when activating a VirtioDevice.
56#[derive(Debug, thiserror::Error, displaydoc::Display)]
57pub enum ActivateError {
58 /// Wrong number of queue for virtio device: expected {expected}, got {got}
59 QueueMismatch { expected: usize, got: usize },
60 /// Failed to write to activate eventfd
61 EventFd,
62 /// Vhost user: {0}
63 VhostUser(vhost_user::VhostUserError),
64 /// Setting tap interface offload flags failed: {0}
65 TapSetOffload(TapError),
66 /// Error setting pointers in the queue: (0)
67 QueueMemoryError(QueueError),
68 /// The driver didn't acknowledge a required feature: {0}
69 RequiredFeatureNotAcked(&'static str),
70}
71
72/// Trait that helps in upcasting an object to Any
73pub trait AsAny {
74 /// Return the immutable any encapsulated object.
75 fn as_any(&self) -> &dyn Any;
76
77 /// Return the mutable encapsulated any object.
78 fn as_mut_any(&mut self) -> &mut dyn Any;
79}
80
81impl<T: Any> AsAny for T {
82 fn as_any(&self) -> &dyn Any {
83 self
84 }
85
86 fn as_mut_any(&mut self) -> &mut dyn Any {
87 self
88 }
89}