Skip to main content

petri/vm/openvmm/
mod.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Code managing the lifetime of a `PetriVmOpenVmm`. All VMs live the same lifecycle:
5//! * A `PetriVmConfigOpenVmm` is built for the given firmware and architecture in `construct`.
6//! * The configuration is optionally modified from the defaults using the helpers in `modify`.
7//! * The `PetriVmOpenVmm` is started by the code in `start`.
8//! * The VM is interacted with through the methods in `runtime`.
9//! * The VM is either shut down by the code in `runtime`, or gets dropped and cleaned up automatically.
10
11mod construct;
12#[cfg(target_os = "linux")]
13mod hugetlb;
14mod modify;
15mod runtime;
16mod start;
17
18#[cfg(target_os = "linux")]
19pub use hugetlb::HUGETLB_2MB_PAGE_SIZE;
20#[cfg(target_os = "linux")]
21pub use hugetlb::ensure_2mb_hugetlb_pages;
22pub use runtime::OpenVmmFramebufferAccess;
23pub use runtime::OpenVmmInspector;
24pub use runtime::PetriVmOpenVmm;
25
26use crate::Disk;
27use crate::DiskPath;
28use crate::Firmware;
29use crate::ModifyFn;
30use crate::OpenHclServicingFlags;
31use crate::OpenvmmLogConfig;
32use crate::PetriLogFile;
33use crate::PetriVmConfig;
34use crate::PetriVmResources;
35use crate::PetriVmRuntimeConfig;
36use crate::PetriVmgsDisk;
37use crate::PetriVmgsResource;
38use crate::PetriVmmBackend;
39use crate::VmmQuirks;
40use crate::linux_direct_serial_agent::LinuxDirectSerialAgent;
41use crate::vm::PetriVmProperties;
42use anyhow::Context;
43use async_trait::async_trait;
44use disk_backend_resources::DiskLayerDescription;
45use disk_backend_resources::LayeredDiskHandle;
46use disk_backend_resources::layer::DiskLayerHandle;
47use disk_backend_resources::layer::RamDiskLayerHandle;
48use disk_backend_resources::layer::SqliteAutoCacheDiskLayerHandle;
49use get_resources::ged::FirmwareEvent;
50use guid::Guid;
51use hyperv_ic_resources::shutdown::ShutdownRpc;
52use mesh::Receiver;
53use mesh::Sender;
54use net_backend_resources::mac_address::MacAddress;
55use openvmm_defs::config::Config;
56use openvmm_helpers::disk::OpenDiskOptions;
57use openvmm_helpers::disk::open_disk_type;
58use pal_async::DefaultDriver;
59use pal_async::socket::PolledSocket;
60use pal_async::task::Task;
61use petri_artifacts_common::tags::GuestQuirksInner;
62use petri_artifacts_common::tags::MachineArch;
63use petri_artifacts_core::ArtifactResolver;
64use petri_artifacts_core::ResolvedArtifact;
65use std::path::Path;
66use std::path::PathBuf;
67use std::sync::Arc;
68use std::time::Duration;
69use tempfile::TempPath;
70use unix_socket::UnixListener;
71use vm_resource::IntoResource;
72use vm_resource::Resource;
73use vm_resource::kind::DiskHandleKind;
74use vmgs_resources::VmgsDisk;
75use vmgs_resources::VmgsResource;
76
77/// The instance guid for the MANA nic automatically added when specifying `PetriVmConfigOpenVmm::with_nic`
78const MANA_INSTANCE: Guid = guid::guid!("f9641cf4-d915-4743-a7d8-efa75db7b85a");
79
80/// The MAC address used by the NIC assigned with [`PetriVmConfigOpenVmm::with_nic`].
81pub const NIC_MAC_ADDRESS: MacAddress = MacAddress::new([0x00, 0x15, 0x5D, 0x12, 0x12, 0x12]);
82
83/// OpenVMM Petri Backend
84#[derive(Debug)]
85pub struct OpenVmmPetriBackend {
86    openvmm_path: ResolvedArtifact,
87}
88
89#[async_trait]
90impl PetriVmmBackend for OpenVmmPetriBackend {
91    type VmmConfig = PetriVmConfigOpenVmm;
92    type VmRuntime = PetriVmOpenVmm;
93
94    fn check_compat(firmware: &Firmware, arch: MachineArch) -> bool {
95        arch == MachineArch::host()
96            && !(firmware.is_openhcl() && (!cfg!(windows) || arch == MachineArch::Aarch64))
97            && !(firmware.is_pcat() && arch == MachineArch::Aarch64)
98    }
99
100    fn quirks(firmware: &Firmware) -> (GuestQuirksInner, VmmQuirks) {
101        (
102            firmware.quirks().openvmm,
103            VmmQuirks {
104                // Workaround for #1684
105                flaky_boot: firmware.is_pcat().then_some(Duration::from_secs(15)),
106            },
107        )
108    }
109
110    fn default_servicing_flags() -> OpenHclServicingFlags {
111        OpenHclServicingFlags {
112            enable_nvme_keepalive: true,
113            enable_mana_keepalive: true,
114            override_version_checks: false,
115            stop_timeout_hint_secs: None,
116        }
117    }
118
119    fn create_guest_dump_disk() -> anyhow::Result<
120        Option<(
121            Arc<TempPath>,
122            Box<dyn FnOnce() -> anyhow::Result<Box<dyn fatfs::ReadWriteSeek>>>,
123        )>,
124    > {
125        Ok(None) // TODO #2403
126    }
127
128    fn new(resolver: &ArtifactResolver<'_>) -> Self {
129        OpenVmmPetriBackend {
130            openvmm_path: resolver
131                .require(petri_artifacts_vmm_test::artifacts::OPENVMM_NATIVE)
132                .erase(),
133        }
134    }
135
136    async fn run(
137        self,
138        config: PetriVmConfig,
139        modify_vmm_config: Option<ModifyFn<Self::VmmConfig>>,
140        resources: &PetriVmResources,
141        properties: PetriVmProperties,
142    ) -> anyhow::Result<(Self::VmRuntime, PetriVmRuntimeConfig)> {
143        let mut config =
144            PetriVmConfigOpenVmm::new(&self.openvmm_path, config, resources, properties).await?;
145
146        if let Some(f) = modify_vmm_config {
147            config = f.0(config);
148        }
149
150        config.run().await
151    }
152}
153
154/// Configuration state for a test VM.
155pub struct PetriVmConfigOpenVmm {
156    // Direct configuration related information.
157    runtime_config: PetriVmRuntimeConfig,
158    arch: MachineArch,
159    host_log_levels: Option<OpenvmmLogConfig>,
160    config: Config,
161
162    // Mesh host
163    mesh: mesh_process::Mesh,
164
165    // Runtime resources
166    resources: PetriVmResourcesOpenVmm,
167
168    // Logging
169    openvmm_log_file: PetriLogFile,
170
171    // File-backed guest memory.
172    memory_backing_file: Option<PathBuf>,
173
174    // Resources that are only used during startup.
175    ged: Option<get_resources::ged::GuestEmulationDeviceHandle>,
176    framebuffer_view: Option<framebuffer::View>,
177
178    // Deferred IOMMU configuration: (rc_name, iommu_config) pairs resolved
179    // against pcie_root_complexes at VM start time.
180    pending_iommu: Vec<(String, openvmm_defs::config::PcieIommuConfig)>,
181}
182/// Various channels and resources used to interact with the VM while it is running.
183struct PetriVmResourcesOpenVmm {
184    log_stream_tasks: Vec<Task<anyhow::Result<()>>>,
185    firmware_event_recv: Receiver<FirmwareEvent>,
186    shutdown_ic_send: Option<Sender<ShutdownRpc>>,
187    kvp_ic_send: Option<Sender<hyperv_ic_resources::kvp::KvpConnectRpc>>,
188    ged_send: Option<Sender<get_resources::ged::GuestEmulationRequest>>,
189    pipette_listener: PolledSocket<UnixListener>,
190    vtl2_pipette_listener: Option<PolledSocket<UnixListener>>,
191    linux_direct_serial_agent: Option<LinuxDirectSerialAgent>,
192
193    /// When set, the host connects to pipette via TCP through consomme
194    /// port forwarding instead of accepting on the Unix socket listener.
195    /// Used for Windows no-vmbus guests where virtio-vsock is unavailable.
196    /// The receiver yields the OS-assigned host port once the consomme
197    /// resolver has bound the socket.
198    tcp_pipette_port: Option<mesh::OneshotReceiver<u16>>,
199
200    // Externally injected management stuff also needed at runtime.
201    driver: DefaultDriver,
202    openvmm_path: ResolvedArtifact,
203    output_dir: PathBuf,
204
205    // TempPaths that cannot be dropped until the end.
206    vtl2_vsock_path: Option<TempPath>,
207    _vsock_path: TempPath,
208
209    // properties needed at runtime
210    properties: PetriVmProperties,
211
212    // vmswitch DirectIO switch port handles, held in the test (parent)
213    // process for the lifetime of the child VMM so the kernel port object
214    // survives until the VMM detaches.
215    #[cfg(windows)]
216    _switch_ports: Vec<vmswitch::kernel::SwitchPort>,
217}
218
219/// Discovers a usable Hyper-V virtual switch for `-net dio` tests.
220///
221/// Tries the well-known Default Switch GUID first (which is provisioned
222/// automatically when Hyper-V is installed). If that switch is not
223/// available (e.g. it was removed, or this host uses a different default
224/// switch SKU), falls back to enumerating all HCN networks and returning
225/// the first one reported.
226///
227/// Returns `None` when no switch can be opened — typically because
228/// Hyper-V is not installed, the user lacks privileges, or
229/// `computenetwork.dll` is missing.
230#[cfg(windows)]
231pub fn find_switch() -> Option<Guid> {
232    if vmswitch::hcn::Network::open(&vmswitch::hcn::DEFAULT_SWITCH).is_ok() {
233        return Some(vmswitch::hcn::DEFAULT_SWITCH);
234    }
235    let networks = match vmswitch::hcn::enumerate_networks() {
236        Ok(n) => n,
237        Err(e) => {
238            tracing::warn!(
239                error = &e as &dyn std::error::Error,
240                "failed to enumerate HCN networks"
241            );
242            return None;
243        }
244    };
245    networks.into_iter().find(|guid| {
246        if let Err(e) = vmswitch::hcn::Network::open(guid) {
247            tracing::debug!(
248                %guid,
249                error = &e as &dyn std::error::Error,
250                "skipping unopenable HCN network"
251            );
252            false
253        } else {
254            true
255        }
256    })
257}
258
259/// Discovers a usable Hyper-V virtual switch.
260///
261/// Always `None` on non-Windows platforms.
262#[cfg(not(windows))]
263pub fn find_switch() -> Option<Guid> {
264    None
265}
266
267async fn memdiff_disk(path: &Path) -> anyhow::Result<Resource<DiskHandleKind>> {
268    let disk = open_disk_type(
269        path,
270        OpenDiskOptions {
271            read_only: true,
272            direct: false,
273        },
274    )
275    .await
276    .with_context(|| format!("failed to open disk: {}", path.display()))?;
277    Ok(LayeredDiskHandle {
278        layers: vec![
279            RamDiskLayerHandle {
280                len: None,
281                sector_size: None,
282            }
283            .into_resource()
284            .into(),
285            DiskLayerHandle(disk).into_resource().into(),
286        ],
287    }
288    .into_resource())
289}
290
291fn memdiff_remote_disk(url: &str) -> anyhow::Result<Resource<DiskHandleKind>> {
292    // Strip query parameters and fragments before checking the file extension.
293    let url_path = url.split(['?', '#']).next().unwrap_or(url);
294    let format = if url_path.ends_with(".vhd") || url_path.ends_with(".vmgs") {
295        disk_backend_resources::BlobDiskFormat::FixedVhd1
296    } else {
297        disk_backend_resources::BlobDiskFormat::Flat
298    };
299
300    let cache_dir = super::petri_disk_cache_dir();
301
302    // For VHD1-formatted blobs, let the auto-cache layer derive the cache key
303    // from the VHD's unique ID (a UUID embedded in the footer). This means the
304    // cache automatically invalidates when the image is replaced with a new one,
305    // even if the filename stays the same. For flat-format blobs (e.g. ISOs),
306    // fall back to the URL filename since there's no embedded ID.
307    let cache_key = match format {
308        disk_backend_resources::BlobDiskFormat::FixedVhd1 => None,
309        disk_backend_resources::BlobDiskFormat::Flat => {
310            Some(url_path.rsplit('/').next().unwrap_or(url_path).to_owned())
311        }
312    };
313
314    Ok(LayeredDiskHandle {
315        layers: vec![
316            RamDiskLayerHandle {
317                len: None,
318                sector_size: None,
319            }
320            .into_resource()
321            .into(),
322            DiskLayerDescription {
323                read_cache: true,
324                write_through: false,
325                layer: SqliteAutoCacheDiskLayerHandle {
326                    cache_path: cache_dir,
327                    cache_key,
328                }
329                .into_resource(),
330            },
331            DiskLayerHandle(
332                disk_backend_resources::BlobDiskHandle {
333                    url: url.to_owned(),
334                    format,
335                }
336                .into_resource(),
337            )
338            .into_resource()
339            .into(),
340        ],
341    }
342    .into_resource())
343}
344
345async fn memdiff_vmgs(vmgs: &PetriVmgsResource) -> anyhow::Result<VmgsResource> {
346    async fn convert_disk(disk: &PetriVmgsDisk) -> anyhow::Result<VmgsDisk> {
347        Ok(VmgsDisk {
348            disk: petri_disk_to_openvmm(&disk.disk).await?,
349            encryption_policy: disk.encryption_policy,
350        })
351    }
352
353    Ok(match vmgs {
354        PetriVmgsResource::Disk(disk) => VmgsResource::Disk(convert_disk(disk).await?),
355        PetriVmgsResource::ReprovisionOnFailure(disk) => {
356            VmgsResource::ReprovisionOnFailure(convert_disk(disk).await?)
357        }
358        PetriVmgsResource::Reprovision(disk) => {
359            VmgsResource::Reprovision(convert_disk(disk).await?)
360        }
361        PetriVmgsResource::Ephemeral => VmgsResource::Ephemeral,
362    })
363}
364
365async fn petri_disk_to_openvmm(disk: &Disk) -> anyhow::Result<Resource<DiskHandleKind>> {
366    Ok(match disk {
367        Disk::Memory(len) => LayeredDiskHandle::single_layer(RamDiskLayerHandle {
368            len: Some(*len),
369            sector_size: None,
370        })
371        .into_resource(),
372        Disk::Differencing(DiskPath::Local(path)) => memdiff_disk(path).await?,
373        Disk::Differencing(DiskPath::Remote { url }) => memdiff_remote_disk(url)?,
374        Disk::Persistent(path) => {
375            open_disk_type(
376                path.as_ref(),
377                OpenDiskOptions {
378                    read_only: false,
379                    direct: false,
380                },
381            )
382            .await?
383        }
384        Disk::Temporary(path) => {
385            open_disk_type(
386                path.as_ref(),
387                OpenDiskOptions {
388                    read_only: false,
389                    direct: false,
390                },
391            )
392            .await?
393        }
394    })
395}