1mod 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 pub fn min_queues(&self) -> usize {
191 match self {
192 DeviceType::Net => 3, DeviceType::Block => 1, DeviceType::Console => 2, DeviceType::Rng => 1, DeviceType::Balloon => 2, DeviceType::Scsi => 3, DeviceType::P9 => 1, DeviceType::Gpu => 2, DeviceType::Input => 2, DeviceType::Vsock => 3, DeviceType::Iommu => 2, DeviceType::Sound => 4, DeviceType::Fs => 2, DeviceType::Pmem => 1, DeviceType::Mac80211HwSim => 2, DeviceType::VideoEncoder => 2, DeviceType::VideoDecoder => 2, DeviceType::Scmi => 2, DeviceType::Wl => 2, DeviceType::Tpm => 1, DeviceType::Pvclock => 1, DeviceType::Media => 2, }
215 }
216}
217
218impl 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
248pub 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
272pub 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
284pub enum VirtioDeviceType {
289 Regular,
291 VhostUser,
293}
294
295impl VirtioDeviceType {
296 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
306pub(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
315pub(crate) enum StoppedWorker<Q> {
318 WithQueues(Box<Q>),
320
321 AlreadyStopped,
323
324 MissingQueues,
328}