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(feature = "vtpm")]
22mod tpm;
23#[cfg(any(feature = "video-decoder", feature = "video-encoder"))]
24mod video;
25mod virtio_device;
26mod virtio_mmio_device;
27mod virtio_pci_common_config;
28mod virtio_pci_device;
29
30pub mod block;
31pub mod console;
32#[cfg(feature = "gpu")]
33pub mod gpu;
34#[cfg(all(unix, feature = "media"))]
35pub mod media;
36pub mod resource_bridge;
37pub mod scsi;
38#[cfg(feature = "audio")]
39pub mod snd;
40pub mod vhost;
41pub mod vhost_user_backend;
42pub mod vhost_user_frontend;
43pub mod vsock;
44
45pub use vmm_vhost::SharedMemoryRegion;
46
47#[cfg(feature = "balloon")]
48pub use self::balloon::Balloon;
49#[cfg(feature = "balloon")]
50pub use self::balloon::BalloonFeatures;
51pub use self::block::BlockAsync;
52pub use self::console::Console;
53pub use self::descriptor_chain::DescriptorChain;
54pub use self::descriptor_chain::DescriptorChainIter;
55pub use self::descriptor_utils::create_descriptor_chain;
56pub use self::descriptor_utils::DescriptorType;
57pub use self::descriptor_utils::Reader;
58pub use self::descriptor_utils::Writer;
59#[cfg(feature = "gpu")]
60pub use self::gpu::DisplayBackend;
61#[cfg(feature = "gpu")]
62pub use self::gpu::Gpu;
63#[cfg(feature = "gpu")]
64pub use self::gpu::GpuDisplayMode;
65#[cfg(feature = "gpu")]
66pub use self::gpu::GpuDisplayParameters;
67#[cfg(feature = "gpu")]
68pub use self::gpu::GpuMode;
69#[cfg(feature = "gpu")]
70pub use self::gpu::GpuMouseMode;
71#[cfg(feature = "gpu")]
72pub use self::gpu::GpuParameters;
73#[cfg(feature = "gpu")]
74pub use self::gpu::GpuWsi;
75pub use self::interrupt::Interrupt;
76pub use self::interrupt::InterruptSnapshot;
77pub use self::iommu::ipc_memory_mapper;
78pub use self::iommu::memory_mapper;
79pub use self::iommu::Iommu;
80pub use self::iommu::IommuError;
81#[cfg(feature = "net")]
82pub use self::net::Net;
83#[cfg(feature = "net")]
84pub use self::net::NetError;
85#[cfg(feature = "net")]
86pub use self::net::NetParameters;
87#[cfg(feature = "net")]
88pub use self::net::NetParametersMode;
89pub use self::queue::split_descriptor_chain::Desc;
90pub use self::queue::split_descriptor_chain::SplitDescriptorChain;
91pub use self::queue::PeekedDescriptorChain;
92pub use self::queue::Queue;
93pub use self::queue::QueueConfig;
94pub use self::scsi::Controller as ScsiController;
95pub use self::scsi::DiskConfig as ScsiDiskConfig;
96#[cfg(feature = "vtpm")]
97pub use self::tpm::Tpm;
98#[cfg(feature = "vtpm")]
99pub use self::tpm::TpmBackend;
100pub use self::vhost_user_frontend::VhostUserFrontend;
101#[cfg(any(feature = "video-decoder", feature = "video-encoder"))]
102pub use self::video::VideoDevice;
103pub use self::virtio_device::SharedMemoryMapper;
104pub use self::virtio_device::SharedMemoryPrepareType;
105pub use self::virtio_device::VirtioDevice;
106pub use self::virtio_mmio_device::VirtioMmioDevice;
107pub use self::virtio_pci_device::PciCapabilityType;
108pub use self::virtio_pci_device::VirtioPciCap;
109pub use self::virtio_pci_device::VirtioPciDevice;
110pub use self::virtio_pci_device::VirtioPciShmCap;
111#[cfg(feature = "pvclock")]
112pub use self::DeviceType::Pvclock;
113
114cfg_if::cfg_if! {
115 if #[cfg(any(target_os = "android", target_os = "linux"))] {
116 mod p9;
117 mod pmem;
118
119 pub mod wl;
120 pub mod fs;
121
122 pub use self::iommu::sys::linux::vfio_wrapper;
123 #[cfg(feature = "net")]
124 pub use self::net::VhostNetParameters;
125 #[cfg(feature = "net")]
126 pub use self::net::VHOST_NET_DEFAULT_PATH;
127 pub use self::p9::P9;
128 pub use self::pmem::Pmem;
129 pub use self::pmem::PmemConfig;
130 pub use self::pmem::MemSlotConfig;
131 #[cfg(feature = "audio")]
132 pub use self::snd::new_sound;
133 pub use self::wl::Wl;
134 } else if #[cfg(windows)] {
135 pub use self::vsock::Vsock;
136 } else {
137 compile_error!("Unsupported platform");
138 }
139}
140
141use std::cmp;
142use std::convert::TryFrom;
143
144use futures::channel::oneshot;
145use hypervisor::ProtectionType;
146use serde::Deserialize;
147use serde::Serialize;
148use virtio_sys::virtio_config::VIRTIO_F_ACCESS_PLATFORM;
149use virtio_sys::virtio_config::VIRTIO_F_SUSPEND;
150use virtio_sys::virtio_config::VIRTIO_F_VERSION_1;
151use virtio_sys::virtio_ids;
152use virtio_sys::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
153
154const DEVICE_RESET: u32 = 0x0;
155
156const INTERRUPT_STATUS_USED_RING: u32 = 0x1;
157const INTERRUPT_STATUS_CONFIG_CHANGED: u32 = 0x2;
158
159const VIRTIO_MSI_NO_VECTOR: u16 = 0xffff;
160
161#[derive(Copy, Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
162#[serde(rename_all = "kebab-case")]
163#[repr(u32)]
164pub enum DeviceType {
165 Net = virtio_ids::VIRTIO_ID_NET,
166 Block = virtio_ids::VIRTIO_ID_BLOCK,
167 Console = virtio_ids::VIRTIO_ID_CONSOLE,
168 Rng = virtio_ids::VIRTIO_ID_RNG,
169 Balloon = virtio_ids::VIRTIO_ID_BALLOON,
170 Scsi = virtio_ids::VIRTIO_ID_SCSI,
171 #[serde(rename = "9p")]
172 P9 = virtio_ids::VIRTIO_ID_9P,
173 Gpu = virtio_ids::VIRTIO_ID_GPU,
174 Input = virtio_ids::VIRTIO_ID_INPUT,
175 Vsock = virtio_ids::VIRTIO_ID_VSOCK,
176 Iommu = virtio_ids::VIRTIO_ID_IOMMU,
177 Sound = virtio_ids::VIRTIO_ID_SOUND,
178 Fs = virtio_ids::VIRTIO_ID_FS,
179 Pmem = virtio_ids::VIRTIO_ID_PMEM,
180 #[serde(rename = "mac80211-hwsim")]
181 Mac80211HwSim = virtio_ids::VIRTIO_ID_MAC80211_HWSIM,
182 VideoEncoder = virtio_ids::VIRTIO_ID_VIDEO_ENCODER,
183 VideoDecoder = virtio_ids::VIRTIO_ID_VIDEO_DECODER,
184 Scmi = virtio_ids::VIRTIO_ID_SCMI,
185 Wl = virtio_ids::VIRTIO_ID_WL,
186 Tpm = virtio_ids::VIRTIO_ID_TPM,
187 Pvclock = virtio_ids::VIRTIO_ID_PVCLOCK,
188 Media = virtio_ids::VIRTIO_ID_MEDIA,
189}
190
191impl DeviceType {
192 pub fn min_queues(&self) -> usize {
197 match self {
198 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, }
221 }
222}
223
224impl std::fmt::Display for DeviceType {
226 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
227 match &self {
228 DeviceType::Net => write!(f, "net"),
229 DeviceType::Block => write!(f, "block"),
230 DeviceType::Console => write!(f, "console"),
231 DeviceType::Rng => write!(f, "rng"),
232 DeviceType::Balloon => write!(f, "balloon"),
233 DeviceType::Scsi => write!(f, "scsi"),
234 DeviceType::P9 => write!(f, "9p"),
235 DeviceType::Input => write!(f, "input"),
236 DeviceType::Gpu => write!(f, "gpu"),
237 DeviceType::Vsock => write!(f, "vsock"),
238 DeviceType::Iommu => write!(f, "iommu"),
239 DeviceType::Sound => write!(f, "sound"),
240 DeviceType::Fs => write!(f, "fs"),
241 DeviceType::Pmem => write!(f, "pmem"),
242 DeviceType::Wl => write!(f, "wl"),
243 DeviceType::Tpm => write!(f, "tpm"),
244 DeviceType::Pvclock => write!(f, "pvclock"),
245 DeviceType::VideoDecoder => write!(f, "video-decoder"),
246 DeviceType::VideoEncoder => write!(f, "video-encoder"),
247 DeviceType::Mac80211HwSim => write!(f, "mac80211-hwsim"),
248 DeviceType::Scmi => write!(f, "scmi"),
249 DeviceType::Media => write!(f, "media"),
250 }
251 }
252}
253
254pub fn copy_config(dst: &mut [u8], dst_offset: u64, src: &[u8], src_offset: u64) {
264 if let Ok(dst_offset) = usize::try_from(dst_offset) {
265 if let Ok(src_offset) = usize::try_from(src_offset) {
266 if let Some(dst_slice) = dst.get_mut(dst_offset..) {
267 if let Some(src_slice) = src.get(src_offset..) {
268 let len = cmp::min(dst_slice.len(), src_slice.len());
269 let dst_subslice = &mut dst_slice[0..len];
270 let src_subslice = &src_slice[0..len];
271 dst_subslice.copy_from_slice(src_subslice);
272 }
273 }
274 }
275 }
276}
277
278pub fn base_features(protection_type: ProtectionType) -> u64 {
280 let mut features: u64 =
281 1 << VIRTIO_F_VERSION_1 | 1 << VIRTIO_RING_F_EVENT_IDX | 1 << VIRTIO_F_SUSPEND;
282
283 if protection_type != ProtectionType::Unprotected {
284 features |= 1 << VIRTIO_F_ACCESS_PLATFORM;
285 }
286
287 features
288}
289
290pub enum VirtioDeviceType {
295 Regular,
297 VhostUser,
299}
300
301impl VirtioDeviceType {
302 pub fn seccomp_policy_file(&self, base: &str) -> String {
305 match self {
306 VirtioDeviceType::Regular => format!("{base}_device"),
307 VirtioDeviceType::VhostUser => format!("{base}_device_vhost_user"),
308 }
309 }
310}
311
312pub(crate) fn create_stop_oneshot(tx_vec: &mut Vec<oneshot::Sender<()>>) -> oneshot::Receiver<()> {
316 let (stop_tx, stop_rx) = futures::channel::oneshot::channel();
317 tx_vec.push(stop_tx);
318 stop_rx
319}
320
321pub(crate) enum StoppedWorker<Q> {
324 WithQueues(Box<Q>),
326
327 AlreadyStopped,
329
330 MissingQueues,
334}