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    /// Hot-add a VPCI device to VTL0 at runtime with the supplied instance ID.
44    AddVpciDevice(FailableRpc<(Guid, Resource<PciDeviceHandleKind>), ()>),
45    /// Hot-remove a dynamically added VPCI device by instance ID.
46    RemoveVpciDevice(FailableRpc<Guid, ()>),
47    /// Dump VM state (VP registers + memory) to a `.vmrs` file.
48    ///
49    /// The worker pauses the VM internally, collects state, and restores
50    /// the prior running state afterward. The caller provides an open file
51    /// handle to write to (typically a temporary file that gets renamed
52    /// into place on success).
53    DumpState(FailableRpc<File, ()>),
54}
55
56#[derive(Debug, MeshPayload, thiserror::Error)]
57pub enum PulseSaveRestoreError {
58    #[error("reset not supported")]
59    ResetNotSupported,
60    #[error("pulse save+restore failed")]
61    Other(#[source] RemoteError),
62}
63
64impl From<anyhow::Error> for PulseSaveRestoreError {
65    fn from(err: anyhow::Error) -> Self {
66        Self::Other(RemoteError::new(err))
67    }
68}
69
70impl fmt::Debug for VmRpc {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        let s = match self {
73            VmRpc::Reset(_) => "Reset",
74            VmRpc::Save(_) => "Save",
75            VmRpc::Resume(_) => "Resume",
76            VmRpc::Pause(_) => "Pause",
77            VmRpc::ClearHalt(_) => "ClearHalt",
78            VmRpc::Nmi(_) => "Nmi",
79            VmRpc::AddVmbusDevice(_) => "AddVmbusDevice",
80            VmRpc::ConnectHvsock(_) => "ConnectHvsock",
81            VmRpc::PulseSaveRestore(_) => "PulseSaveRestore",
82            VmRpc::StartReloadIgvm(_) => "StartReloadIgvm",
83            VmRpc::CompleteReloadIgvm(_) => "CompleteReloadIgvm",
84            VmRpc::ReadMemory(_) => "ReadMemory",
85            VmRpc::WriteMemory(_) => "WriteMemory",
86            VmRpc::UpdateCliParams(_) => "UpdateCliParams",
87            VmRpc::AddPcieDevice(_) => "AddPcieDevice",
88            VmRpc::RemovePcieDevice(_) => "RemovePcieDevice",
89            VmRpc::AddVpciDevice(_) => "AddVpciDevice",
90            VmRpc::RemoveVpciDevice(_) => "RemoveVpciDevice",
91            VmRpc::DumpState(_) => "DumpState",
92        };
93        f.pad(s)
94    }
95}