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/// Various channels and resources used to interact with the VM while it is running.
179struct PetriVmResourcesOpenVmm {
180    log_stream_tasks: Vec<Task<anyhow::Result<()>>>,
181    firmware_event_recv: Receiver<FirmwareEvent>,
182    shutdown_ic_send: Sender<ShutdownRpc>,
183    kvp_ic_send: Sender<hyperv_ic_resources::kvp::KvpConnectRpc>,
184    ged_send: Option<Sender<get_resources::ged::GuestEmulationRequest>>,
185    pipette_listener: PolledSocket<UnixListener>,
186    vtl2_pipette_listener: Option<PolledSocket<UnixListener>>,
187    linux_direct_serial_agent: Option<LinuxDirectSerialAgent>,
188
189    // Externally injected management stuff also needed at runtime.
190    driver: DefaultDriver,
191    openvmm_path: ResolvedArtifact,
192    output_dir: PathBuf,
193
194    // TempPaths that cannot be dropped until the end.
195    vtl2_vsock_path: Option<TempPath>,
196    _vsock_path: TempPath,
197
198    // properties needed at runtime
199    properties: PetriVmProperties,
200}
201
202async fn memdiff_disk(path: &Path) -> anyhow::Result<Resource<DiskHandleKind>> {
203    let disk = open_disk_type(
204        path,
205        OpenDiskOptions {
206            read_only: true,
207            direct: false,
208        },
209    )
210    .await
211    .with_context(|| format!("failed to open disk: {}", path.display()))?;
212    Ok(LayeredDiskHandle {
213        layers: vec![
214            RamDiskLayerHandle {
215                len: None,
216                sector_size: None,
217            }
218            .into_resource()
219            .into(),
220            DiskLayerHandle(disk).into_resource().into(),
221        ],
222    }
223    .into_resource())
224}
225
226fn memdiff_remote_disk(url: &str) -> anyhow::Result<Resource<DiskHandleKind>> {
227    // Strip query parameters and fragments before checking the file extension.
228    let url_path = url.split(['?', '#']).next().unwrap_or(url);
229    let format = if url_path.ends_with(".vhd") || url_path.ends_with(".vmgs") {
230        disk_backend_resources::BlobDiskFormat::FixedVhd1
231    } else {
232        disk_backend_resources::BlobDiskFormat::Flat
233    };
234
235    let cache_dir = super::petri_disk_cache_dir();
236
237    // For VHD1-formatted blobs, let the auto-cache layer derive the cache key
238    // from the VHD's unique ID (a UUID embedded in the footer). This means the
239    // cache automatically invalidates when the image is replaced with a new one,
240    // even if the filename stays the same. For flat-format blobs (e.g. ISOs),
241    // fall back to the URL filename since there's no embedded ID.
242    let cache_key = match format {
243        disk_backend_resources::BlobDiskFormat::FixedVhd1 => None,
244        disk_backend_resources::BlobDiskFormat::Flat => {
245            Some(url_path.rsplit('/').next().unwrap_or(url_path).to_owned())
246        }
247    };
248
249    Ok(LayeredDiskHandle {
250        layers: vec![
251            RamDiskLayerHandle {
252                len: None,
253                sector_size: None,
254            }
255            .into_resource()
256            .into(),
257            DiskLayerDescription {
258                read_cache: true,
259                write_through: false,
260                layer: SqliteAutoCacheDiskLayerHandle {
261                    cache_path: cache_dir,
262                    cache_key,
263                }
264                .into_resource(),
265            },
266            DiskLayerHandle(
267                disk_backend_resources::BlobDiskHandle {
268                    url: url.to_owned(),
269                    format,
270                }
271                .into_resource(),
272            )
273            .into_resource()
274            .into(),
275        ],
276    }
277    .into_resource())
278}
279
280async fn memdiff_vmgs(vmgs: &PetriVmgsResource) -> anyhow::Result<VmgsResource> {
281    async fn convert_disk(disk: &PetriVmgsDisk) -> anyhow::Result<VmgsDisk> {
282        Ok(VmgsDisk {
283            disk: petri_disk_to_openvmm(&disk.disk).await?,
284            encryption_policy: disk.encryption_policy,
285        })
286    }
287
288    Ok(match vmgs {
289        PetriVmgsResource::Disk(disk) => VmgsResource::Disk(convert_disk(disk).await?),
290        PetriVmgsResource::ReprovisionOnFailure(disk) => {
291            VmgsResource::ReprovisionOnFailure(convert_disk(disk).await?)
292        }
293        PetriVmgsResource::Reprovision(disk) => {
294            VmgsResource::Reprovision(convert_disk(disk).await?)
295        }
296        PetriVmgsResource::Ephemeral => VmgsResource::Ephemeral,
297    })
298}
299
300async fn petri_disk_to_openvmm(disk: &Disk) -> anyhow::Result<Resource<DiskHandleKind>> {
301    Ok(match disk {
302        Disk::Memory(len) => LayeredDiskHandle::single_layer(RamDiskLayerHandle {
303            len: Some(*len),
304            sector_size: None,
305        })
306        .into_resource(),
307        Disk::Differencing(DiskPath::Local(path)) => memdiff_disk(path).await?,
308        Disk::Differencing(DiskPath::Remote { url }) => memdiff_remote_disk(url)?,
309        Disk::Persistent(path) => {
310            open_disk_type(
311                path.as_ref(),
312                OpenDiskOptions {
313                    read_only: false,
314                    direct: false,
315                },
316            )
317            .await?
318        }
319        Disk::Temporary(path) => {
320            open_disk_type(
321                path.as_ref(),
322                OpenDiskOptions {
323                    read_only: false,
324                    direct: false,
325                },
326            )
327            .await?
328        }
329    })
330}