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 #3897
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    // The private-memory setting explicitly requested via
175    // `MemoryConfig::private_memory`, preserved so that backend methods which
176    // force shared memory (e.g. `with_hugepages`, `with_memory_backing_file`)
177    // can fail when the caller explicitly asked for private memory rather than
178    // silently downgrading it.
179    requested_private_memory: Option<bool>,
180
181    // Resources that are only used during startup.
182    ged: Option<get_resources::ged::GuestEmulationDeviceHandle>,
183    framebuffer_view: Option<framebuffer::View>,
184
185    // Deferred IOMMU configuration: (rc_name, iommu_config) pairs resolved
186    // against pcie_root_complexes at VM start time.
187    pending_iommu: Vec<(String, openvmm_defs::config::PcieIommuConfig)>,
188}
189/// Various channels and resources used to interact with the VM while it is running.
190struct PetriVmResourcesOpenVmm {
191    log_stream_tasks: Vec<Task<anyhow::Result<()>>>,
192    firmware_event_recv: Receiver<FirmwareEvent>,
193    shutdown_ic_send: Option<Sender<ShutdownRpc>>,
194    kvp_ic_send: Option<Sender<hyperv_ic_resources::kvp::KvpConnectRpc>>,
195    ged_send: Option<Sender<get_resources::ged::GuestEmulationRequest>>,
196    pipette_listener: PolledSocket<UnixListener>,
197    vtl2_pipette_listener: Option<PolledSocket<UnixListener>>,
198    linux_direct_serial_agent: Option<LinuxDirectSerialAgent>,
199
200    /// When set, the host connects to pipette via TCP through consomme
201    /// port forwarding instead of accepting on the Unix socket listener.
202    /// Used for Windows no-vmbus guests where virtio-vsock is unavailable.
203    /// The receiver yields the OS-assigned host port once the consomme
204    /// resolver has bound the socket.
205    tcp_pipette_port: Option<mesh::OneshotReceiver<u16>>,
206
207    // Externally injected management stuff also needed at runtime.
208    driver: DefaultDriver,
209    openvmm_path: ResolvedArtifact,
210    output_dir: PathBuf,
211
212    // TempPaths that cannot be dropped until the end.
213    vtl2_vsock_path: Option<TempPath>,
214    _vsock_path: TempPath,
215
216    // properties needed at runtime
217    properties: PetriVmProperties,
218
219    // vmswitch DirectIO switch port handles, held in the test (parent)
220    // process for the lifetime of the child VMM so the kernel port object
221    // survives until the VMM detaches.
222    #[cfg(windows)]
223    _switch_ports: Vec<vmswitch::kernel::SwitchPort>,
224}
225
226/// Discovers a usable Hyper-V virtual switch for `-net dio` tests.
227///
228/// Tries the well-known Default Switch GUID first (which is provisioned
229/// automatically when Hyper-V is installed). If that switch is not
230/// available (e.g. it was removed, or this host uses a different default
231/// switch SKU), falls back to enumerating all HCN networks and returning
232/// the first one reported.
233///
234/// Returns `None` when no switch can be opened — typically because
235/// Hyper-V is not installed, the user lacks privileges, or
236/// `computenetwork.dll` is missing.
237#[cfg(windows)]
238pub fn find_switch() -> Option<Guid> {
239    if vmswitch::hcn::Network::open(&vmswitch::hcn::DEFAULT_SWITCH).is_ok() {
240        return Some(vmswitch::hcn::DEFAULT_SWITCH);
241    }
242    let networks = match vmswitch::hcn::enumerate_networks() {
243        Ok(n) => n,
244        Err(e) => {
245            tracing::warn!(
246                error = &e as &dyn std::error::Error,
247                "failed to enumerate HCN networks"
248            );
249            return None;
250        }
251    };
252    networks.into_iter().find(|guid| {
253        if let Err(e) = vmswitch::hcn::Network::open(guid) {
254            tracing::debug!(
255                %guid,
256                error = &e as &dyn std::error::Error,
257                "skipping unopenable HCN network"
258            );
259            false
260        } else {
261            true
262        }
263    })
264}
265
266/// Discovers a usable Hyper-V virtual switch.
267///
268/// Always `None` on non-Windows platforms.
269#[cfg(not(windows))]
270pub fn find_switch() -> Option<Guid> {
271    None
272}
273
274async fn memdiff_disk(path: &Path) -> anyhow::Result<Resource<DiskHandleKind>> {
275    let disk = open_disk_type(
276        path,
277        OpenDiskOptions {
278            read_only: true,
279            direct: false,
280        },
281    )
282    .await
283    .with_context(|| format!("failed to open disk: {}", path.display()))?;
284    Ok(LayeredDiskHandle {
285        layers: vec![
286            RamDiskLayerHandle {
287                len: None,
288                sector_size: None,
289            }
290            .into_resource()
291            .into(),
292            DiskLayerHandle(disk).into_resource().into(),
293        ],
294    }
295    .into_resource())
296}
297
298fn memdiff_remote_disk(url: &str) -> anyhow::Result<Resource<DiskHandleKind>> {
299    // Strip query parameters and fragments before checking the file extension.
300    let url_path = url.split(['?', '#']).next().unwrap_or(url);
301    let format = if url_path.ends_with(".vhd") || url_path.ends_with(".vmgs") {
302        disk_backend_resources::BlobDiskFormat::FixedVhd1
303    } else {
304        disk_backend_resources::BlobDiskFormat::Flat
305    };
306
307    let cache_dir = super::petri_disk_cache_dir();
308
309    // For VHD1-formatted blobs, let the auto-cache layer derive the cache key
310    // from the VHD's unique ID (a UUID embedded in the footer). This means the
311    // cache automatically invalidates when the image is replaced with a new one,
312    // even if the filename stays the same. For flat-format blobs (e.g. ISOs),
313    // fall back to the URL filename since there's no embedded ID.
314    let cache_key = match format {
315        disk_backend_resources::BlobDiskFormat::FixedVhd1 => None,
316        disk_backend_resources::BlobDiskFormat::Flat => {
317            Some(url_path.rsplit('/').next().unwrap_or(url_path).to_owned())
318        }
319    };
320
321    Ok(LayeredDiskHandle {
322        layers: vec![
323            RamDiskLayerHandle {
324                len: None,
325                sector_size: None,
326            }
327            .into_resource()
328            .into(),
329            DiskLayerDescription {
330                read_cache: true,
331                write_through: false,
332                layer: SqliteAutoCacheDiskLayerHandle {
333                    cache_path: cache_dir,
334                    cache_key,
335                }
336                .into_resource(),
337            },
338            DiskLayerHandle(
339                disk_backend_resources::BlobDiskHandle {
340                    url: url.to_owned(),
341                    format,
342                }
343                .into_resource(),
344            )
345            .into_resource()
346            .into(),
347        ],
348    }
349    .into_resource())
350}
351
352async fn memdiff_vmgs(vmgs: &PetriVmgsResource) -> anyhow::Result<VmgsResource> {
353    async fn convert_disk(disk: &PetriVmgsDisk) -> anyhow::Result<VmgsDisk> {
354        Ok(VmgsDisk {
355            disk: petri_disk_to_openvmm(&disk.disk).await?,
356            encryption_policy: disk.encryption_policy,
357        })
358    }
359
360    Ok(match vmgs {
361        PetriVmgsResource::Disk(disk) => VmgsResource::Disk(convert_disk(disk).await?),
362        PetriVmgsResource::ReprovisionOnFailure(disk) => {
363            VmgsResource::ReprovisionOnFailure(convert_disk(disk).await?)
364        }
365        PetriVmgsResource::Reprovision(disk) => {
366            VmgsResource::Reprovision(convert_disk(disk).await?)
367        }
368        PetriVmgsResource::Ephemeral => VmgsResource::Ephemeral,
369    })
370}
371
372async fn petri_disk_to_openvmm(disk: &Disk) -> anyhow::Result<Resource<DiskHandleKind>> {
373    Ok(match disk {
374        Disk::Memory(len) => LayeredDiskHandle::single_layer(RamDiskLayerHandle {
375            len: Some(*len),
376            sector_size: None,
377        })
378        .into_resource(),
379        Disk::Differencing(DiskPath::Local(path)) => memdiff_disk(path).await?,
380        Disk::Differencing(DiskPath::Remote { url }) => memdiff_remote_disk(url)?,
381        Disk::Persistent(path) => {
382            open_disk_type(
383                path.as_ref(),
384                OpenDiskOptions {
385                    read_only: false,
386                    direct: false,
387                },
388            )
389            .await?
390        }
391        Disk::Temporary(path) => {
392            open_disk_type(
393                path.as_ref(),
394                OpenDiskOptions {
395                    read_only: false,
396                    direct: false,
397                },
398            )
399            .await?
400        }
401    })
402}