devices/virtio/
mod.rs

1// Copyright 2017 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Implements virtio devices, queues, and transport mechanisms.
6
7mod async_utils;
8#[cfg(feature = "balloon")]
9mod balloon;
10mod descriptor_chain;
11mod descriptor_utils;
12pub mod device_constants;
13pub mod input;
14mod interrupt;
15mod iommu;
16#[cfg(feature = "net")]
17pub mod net;
18#[cfg(feature = "pvclock")]
19pub mod pvclock;
20mod queue;
21#[cfg(any(feature = "video-decoder", feature = "video-encoder"))]
22mod video;
23mod virtio_device;
24mod virtio_mmio_device;
25mod virtio_pci_common_config;
26mod virtio_pci_device;
27
28pub mod block;
29pub mod console;
30#[cfg(feature = "gpu")]
31pub mod gpu;
32#[cfg(all(unix, feature = "media"))]
33pub mod media;
34pub mod resource_bridge;
35pub mod scsi;
36#[cfg(feature = "audio")]
37pub mod snd;
38pub mod vhost;
39pub mod vhost_user_backend;
40pub mod vhost_user_frontend;
41pub mod vsock;
42
43pub use vmm_vhost::SharedMemoryRegion;
44
45#[cfg(feature = "balloon")]
46pub use self::balloon::Balloon;
47#[cfg(feature = "balloon")]
48pub use self::balloon::BalloonFeatures;
49pub use self::block::BlockAsync;
50pub use self::console::Console;
51pub use self::descriptor_chain::DescriptorChain;
52pub use self::descriptor_chain::DescriptorChainIter;
53pub use self::descriptor_utils::create_descriptor_chain;
54pub use self::descriptor_utils::DescriptorType;
55pub use self::descriptor_utils::Reader;
56pub use self::descriptor_utils::Writer;
57#[cfg(feature = "gpu")]
58pub use self::gpu::DisplayBackend;
59#[cfg(feature = "gpu")]
60pub use self::gpu::Gpu;
61#[cfg(feature = "gpu")]
62pub use self::gpu::GpuDisplayMode;
63#[cfg(feature = "gpu")]
64pub use self::gpu::GpuDisplayParameters;
65#[cfg(feature = "gpu")]
66pub use self::gpu::GpuMode;
67#[cfg(feature = "gpu")]
68pub use self::gpu::GpuMouseMode;
69#[cfg(feature = "gpu")]
70pub use self::gpu::GpuParameters;
71#[cfg(feature = "gpu")]
72pub use self::gpu::GpuWsi;
73pub use self::interrupt::Interrupt;
74pub use self::interrupt::InterruptSnapshot;
75pub use self::iommu::ipc_memory_mapper;
76pub use self::iommu::memory_mapper;
77pub use self::iommu::Iommu;
78pub use self::iommu::IommuError;
79#[cfg(feature = "net")]
80pub use self::net::Net;
81#[cfg(feature = "net")]
82pub use self::net::NetError;
83#[cfg(feature = "net")]
84pub use self::net::NetParameters;
85#[cfg(feature = "net")]
86pub use self::net::NetParametersMode;
87pub use self::queue::split_descriptor_chain::Desc;
88pub use self::queue::split_descriptor_chain::SplitDescriptorChain;
89pub use self::queue::PeekedDescriptorChain;
90pub use self::queue::Queue;
91pub use self::queue::QueueConfig;
92pub use self::scsi::Controller as ScsiController;
93pub use self::scsi::DiskConfig as ScsiDiskConfig;
94pub use self::vhost_user_frontend::VhostUserFrontend;
95#[cfg(any(feature = "video-decoder", feature = "video-encoder"))]
96pub use self::video::VideoDevice;
97pub use self::virtio_device::SharedMemoryMapper;
98pub use self::virtio_device::SharedMemoryPrepareType;
99pub use self::virtio_device::VirtioDevice;
100pub use self::virtio_mmio_device::VirtioMmioDevice;
101pub use self::virtio_pci_device::PciCapabilityType;
102pub use self::virtio_pci_device::VirtioPciCap;
103pub use self::virtio_pci_device::VirtioPciDevice;
104pub use self::virtio_pci_device::VirtioPciShmCap;
105#[cfg(feature = "pvclock")]
106pub use self::DeviceType::Pvclock;
107
108cfg_if::cfg_if! {
109    if #[cfg(any(target_os = "android", target_os = "linux"))] {
110        mod p9;
111        mod pmem;
112
113        pub mod wl;
114        pub mod fs;
115
116        pub use self::iommu::sys::linux::vfio_wrapper;
117        #[cfg(feature = "net")]
118        pub use self::net::VhostNetParameters;
119        #[cfg(feature = "net")]
120        pub use self::net::VHOST_NET_DEFAULT_PATH;
121        pub use self::p9::P9;
122        pub use self::pmem::Pmem;
123        pub use self::pmem::PmemConfig;
124        pub use self::pmem::MemSlotConfig;
125        #[cfg(feature = "audio")]
126        pub use self::snd::new_sound;
127        pub use self::wl::Wl;
128    } else if #[cfg(windows)] {
129        pub use self::vsock::Vsock;
130    } else {
131        compile_error!("Unsupported platform");
132    }
133}
134
135use std::cmp;
136use std::convert::TryFrom;
137
138use futures::channel::oneshot;
139use hypervisor::ProtectionType;
140use serde::Deserialize;
141use serde::Serialize;
142use virtio_sys::virtio_config::VIRTIO_F_ACCESS_PLATFORM;
143use virtio_sys::virtio_config::VIRTIO_F_SUSPEND;
144use virtio_sys::virtio_config::VIRTIO_F_VERSION_1;
145use virtio_sys::virtio_ids;
146use virtio_sys::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
147
148const DEVICE_RESET: u32 = 0x0;
149
150const INTERRUPT_STATUS_USED_RING: u32 = 0x1;
151const INTERRUPT_STATUS_CONFIG_CHANGED: u32 = 0x2;
152
153const VIRTIO_MSI_NO_VECTOR: u16 = 0xffff;
154
155#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
156#[serde(rename_all = "kebab-case")]
157#[repr(u32)]
158pub enum DeviceType {
159    Net = virtio_ids::VIRTIO_ID_NET,
160    Block = virtio_ids::VIRTIO_ID_BLOCK,
161    Console = virtio_ids::VIRTIO_ID_CONSOLE,
162    Rng = virtio_ids::VIRTIO_ID_RNG,
163    Balloon = virtio_ids::VIRTIO_ID_BALLOON,
164    Scsi = virtio_ids::VIRTIO_ID_SCSI,
165    #[serde(rename = "9p")]
166    P9 = virtio_ids::VIRTIO_ID_9P,
167    Gpu = virtio_ids::VIRTIO_ID_GPU,
168    Input = virtio_ids::VIRTIO_ID_INPUT,
169    Vsock = virtio_ids::VIRTIO_ID_VSOCK,
170    Iommu = virtio_ids::VIRTIO_ID_IOMMU,
171    Sound = virtio_ids::VIRTIO_ID_SOUND,
172    Fs = virtio_ids::VIRTIO_ID_FS,
173    Pmem = virtio_ids::VIRTIO_ID_PMEM,
174    #[serde(rename = "mac80211-hwsim")]
175    Mac80211HwSim = virtio_ids::VIRTIO_ID_MAC80211_HWSIM,
176    VideoEncoder = virtio_ids::VIRTIO_ID_VIDEO_ENCODER,
177    VideoDecoder = virtio_ids::VIRTIO_ID_VIDEO_DECODER,
178    Scmi = virtio_ids::VIRTIO_ID_SCMI,
179    Wl = virtio_ids::VIRTIO_ID_WL,
180    Tpm = virtio_ids::VIRTIO_ID_TPM,
181    Pvclock = virtio_ids::VIRTIO_ID_PVCLOCK,
182    Media = virtio_ids::VIRTIO_ID_MEDIA,
183}
184
185impl DeviceType {
186    /// Returns the minimum number of queues that a device of the corresponding type must support.
187    ///
188    /// Note that this does not mean a driver must activate these queues, only that they must be
189    /// implemented by a spec-compliant device.
190    pub fn min_queues(&self) -> usize {
191        match self {
192            DeviceType::Net => 3,           // rx, tx (TODO: b/314353246: ctrl is optional)
193            DeviceType::Block => 1,         // request queue
194            DeviceType::Console => 2,       // receiveq, transmitq
195            DeviceType::Rng => 1,           // request queue
196            DeviceType::Balloon => 2,       // inflateq, deflateq
197            DeviceType::Scsi => 3,          // controlq, eventq, request queue
198            DeviceType::P9 => 1,            // request queue
199            DeviceType::Gpu => 2,           // controlq, cursorq
200            DeviceType::Input => 2,         // eventq, statusq
201            DeviceType::Vsock => 3,         // rx, tx, event
202            DeviceType::Iommu => 2,         // requestq, eventq
203            DeviceType::Sound => 4,         // controlq, eventq, txq, rxq
204            DeviceType::Fs => 2,            // hiprio, request queue
205            DeviceType::Pmem => 1,          // request queue
206            DeviceType::Mac80211HwSim => 2, // tx, rx
207            DeviceType::VideoEncoder => 2,  // cmdq, eventq
208            DeviceType::VideoDecoder => 2,  // cmdq, eventq
209            DeviceType::Scmi => 2,          // cmdq, eventq
210            DeviceType::Wl => 2,            // in, out
211            DeviceType::Tpm => 1,           // request queue
212            DeviceType::Pvclock => 1,       // request queue
213            DeviceType::Media => 2,         // commandq, eventq
214        }
215    }
216}
217
218/// Prints a string representation of the given virtio device type.
219impl std::fmt::Display for DeviceType {
220    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
221        match &self {
222            DeviceType::Net => write!(f, "net"),
223            DeviceType::Block => write!(f, "block"),
224            DeviceType::Console => write!(f, "console"),
225            DeviceType::Rng => write!(f, "rng"),
226            DeviceType::Balloon => write!(f, "balloon"),
227            DeviceType::Scsi => write!(f, "scsi"),
228            DeviceType::P9 => write!(f, "9p"),
229            DeviceType::Input => write!(f, "input"),
230            DeviceType::Gpu => write!(f, "gpu"),
231            DeviceType::Vsock => write!(f, "vsock"),
232            DeviceType::Iommu => write!(f, "iommu"),
233            DeviceType::Sound => write!(f, "sound"),
234            DeviceType::Fs => write!(f, "fs"),
235            DeviceType::Pmem => write!(f, "pmem"),
236            DeviceType::Wl => write!(f, "wl"),
237            DeviceType::Tpm => write!(f, "tpm"),
238            DeviceType::Pvclock => write!(f, "pvclock"),
239            DeviceType::VideoDecoder => write!(f, "video-decoder"),
240            DeviceType::VideoEncoder => write!(f, "video-encoder"),
241            DeviceType::Mac80211HwSim => write!(f, "mac80211-hwsim"),
242            DeviceType::Scmi => write!(f, "scmi"),
243            DeviceType::Media => write!(f, "media"),
244        }
245    }
246}
247
248/// Copy virtio device configuration data from a subslice of `src` to a subslice of `dst`.
249/// Unlike std::slice::copy_from_slice(), this function copies as much as possible within
250/// the common subset of the two slices, truncating the requested range instead of
251/// panicking if the slices do not match in size.
252///
253/// `dst_offset` and `src_offset` specify the starting indexes of the `dst` and `src`
254/// slices, respectively; if either index is out of bounds, this function is a no-op
255/// rather than panicking.  This makes it safe to call with arbitrary user-controlled
256/// inputs.
257pub fn copy_config(dst: &mut [u8], dst_offset: u64, src: &[u8], src_offset: u64) {
258    if let Ok(dst_offset) = usize::try_from(dst_offset) {
259        if let Ok(src_offset) = usize::try_from(src_offset) {
260            if let Some(dst_slice) = dst.get_mut(dst_offset..) {
261                if let Some(src_slice) = src.get(src_offset..) {
262                    let len = cmp::min(dst_slice.len(), src_slice.len());
263                    let dst_subslice = &mut dst_slice[0..len];
264                    let src_subslice = &src_slice[0..len];
265                    dst_subslice.copy_from_slice(src_subslice);
266                }
267            }
268        }
269    }
270}
271
272/// Returns the set of reserved base features common to all virtio devices.
273pub fn base_features(protection_type: ProtectionType) -> u64 {
274    let mut features: u64 =
275        1 << VIRTIO_F_VERSION_1 | 1 << VIRTIO_RING_F_EVENT_IDX | 1 << VIRTIO_F_SUSPEND;
276
277    if protection_type != ProtectionType::Unprotected {
278        features |= 1 << VIRTIO_F_ACCESS_PLATFORM;
279    }
280
281    features
282}
283
284/// Type of virtio transport.
285///
286/// The virtio protocol can be transported by several means, which affects a few things for device
287/// creation - for instance, the seccomp policy we need to use when jailing the device.
288pub enum VirtioDeviceType {
289    /// A regular (in-VMM) virtio device.
290    Regular,
291    /// Socket-backed vhost-user device.
292    VhostUser,
293}
294
295impl VirtioDeviceType {
296    /// Returns the seccomp policy file that we will want to load for device `base`, depending on
297    /// the virtio transport type.
298    pub fn seccomp_policy_file(&self, base: &str) -> String {
299        match self {
300            VirtioDeviceType::Regular => format!("{base}_device"),
301            VirtioDeviceType::VhostUser => format!("{base}_device_vhost_user"),
302        }
303    }
304}
305
306/// Creates a oneshot channel, returning the rx end and adding the tx end to the
307/// provided `Vec`. Useful for creating oneshots that signal a virtqueue future
308/// to stop processing and exit.
309pub(crate) fn create_stop_oneshot(tx_vec: &mut Vec<oneshot::Sender<()>>) -> oneshot::Receiver<()> {
310    let (stop_tx, stop_rx) = futures::channel::oneshot::channel();
311    tx_vec.push(stop_tx);
312    stop_rx
313}
314
315/// When we request to stop the worker, this represents the terminal state
316/// for the thread (if it exists).
317pub(crate) enum StoppedWorker<Q> {
318    /// Worker stopped successfully & returned its queues.
319    WithQueues(Box<Q>),
320
321    /// Worker wasn't running when the stop was requested.
322    AlreadyStopped,
323
324    /// Worker was running but did not successfully return its queues. Something
325    /// has gone wrong (and will be in the error log). In the case of a device
326    /// reset this is fine since the next activation will replace the queues.
327    MissingQueues,
328}