Skip to main content

openvmm_defs/
rpc.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! RPC types for communicating with the VM worker.
5
6use crate::config::DeviceVtl;
7use guid::Guid;
8use mesh::CancelContext;
9use mesh::MeshPayload;
10use mesh::error::RemoteError;
11use mesh::payload::message::ProtobufMessage;
12use mesh::rpc::FailableRpc;
13use mesh::rpc::Rpc;
14use std::fmt;
15use std::fs::File;
16use vm_resource::Resource;
17use vm_resource::kind::PciDeviceHandleKind;
18use vm_resource::kind::VmbusDeviceHandleKind;
19
20#[derive(MeshPayload)]
21pub enum VmRpc {
22    Save(FailableRpc<(), ProtobufMessage>),
23    Resume(Rpc<(), bool>),
24    Pause(Rpc<(), bool>),
25    ClearHalt(Rpc<(), bool>),
26    Reset(FailableRpc<(), ()>),
27    Nmi(Rpc<u32, ()>),
28    AddVmbusDevice(FailableRpc<(DeviceVtl, Resource<VmbusDeviceHandleKind>), ()>),
29    ConnectHvsock(FailableRpc<(CancelContext, Guid, DeviceVtl), unix_socket::UnixStream>),
30    PulseSaveRestore(Rpc<(), Result<(), PulseSaveRestoreError>>),
31    StartReloadIgvm(FailableRpc<File, ()>),
32    CompleteReloadIgvm(FailableRpc<bool, ()>),
33    ReadMemory(FailableRpc<(u64, usize), Vec<u8>>),
34    WriteMemory(FailableRpc<(u64, Vec<u8>), ()>),
35    /// Updates the command line parameters that will be passed to the boot shim
36    /// on the *next* VM load. This will replace the existing command line parameters.
37    UpdateCliParams(FailableRpc<String, ()>),
38    /// Hot-add a PCIe device to a named port at runtime.
39    /// Tuple is (port_name, device_resource).
40    AddPcieDevice(FailableRpc<(String, Resource<PciDeviceHandleKind>), ()>),
41    /// Hot-remove a PCIe device from a named port at runtime.
42    RemovePcieDevice(FailableRpc<String, ()>),
43    /// Dump VM state (VP registers + memory) to a `.vmrs` file.
44    ///
45    /// The worker pauses the VM internally, collects state, and restores
46    /// the prior running state afterward. The caller provides an open file
47    /// handle to write to (typically a temporary file that gets renamed
48    /// into place on success).
49    DumpState(FailableRpc<File, ()>),
50}
51
52#[derive(Debug, MeshPayload, thiserror::Error)]
53pub enum PulseSaveRestoreError {
54    #[error("reset not supported")]
55    ResetNotSupported,
56    #[error("pulse save+restore failed")]
57    Other(#[source] RemoteError),
58}
59
60impl From<anyhow::Error> for PulseSaveRestoreError {
61    fn from(err: anyhow::Error) -> Self {
62        Self::Other(RemoteError::new(err))
63    }
64}
65
66impl fmt::Debug for VmRpc {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        let s = match self {
69            VmRpc::Reset(_) => "Reset",
70            VmRpc::Save(_) => "Save",
71            VmRpc::Resume(_) => "Resume",
72            VmRpc::Pause(_) => "Pause",
73            VmRpc::ClearHalt(_) => "ClearHalt",
74            VmRpc::Nmi(_) => "Nmi",
75            VmRpc::AddVmbusDevice(_) => "AddVmbusDevice",
76            VmRpc::ConnectHvsock(_) => "ConnectHvsock",
77            VmRpc::PulseSaveRestore(_) => "PulseSaveRestore",
78            VmRpc::StartReloadIgvm(_) => "StartReloadIgvm",
79            VmRpc::CompleteReloadIgvm(_) => "CompleteReloadIgvm",
80            VmRpc::ReadMemory(_) => "ReadMemory",
81            VmRpc::WriteMemory(_) => "WriteMemory",
82            VmRpc::UpdateCliParams(_) => "UpdateCliParams",
83            VmRpc::AddPcieDevice(_) => "AddPcieDevice",
84            VmRpc::RemovePcieDevice(_) => "RemovePcieDevice",
85            VmRpc::DumpState(_) => "DumpState",
86        };
87        f.pad(s)
88    }
89}