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    AddHvsockListener(FailableRpc<(DeviceVtl, Guid, unix_socket::UnixListener), ()>),
31    RemoveHvsockListener(FailableRpc<(DeviceVtl, Guid), ()>),
32    PulseSaveRestore(Rpc<(), Result<(), PulseSaveRestoreError>>),
33    StartReloadIgvm(FailableRpc<File, ()>),
34    CompleteReloadIgvm(FailableRpc<bool, ()>),
35    ReadMemory(FailableRpc<(u64, usize), Vec<u8>>),
36    WriteMemory(FailableRpc<(u64, Vec<u8>), ()>),
37    /// Updates the command line parameters that will be passed to the boot shim
38    /// on the *next* VM load. This will replace the existing command line parameters.
39    UpdateCliParams(FailableRpc<String, ()>),
40    /// Hot-add a PCIe device to a named port at runtime.
41    /// Tuple is (port_name, device_resource).
42    AddPcieDevice(FailableRpc<(String, Resource<PciDeviceHandleKind>), ()>),
43    /// Hot-remove a PCIe device from a named port at runtime.
44    RemovePcieDevice(FailableRpc<String, ()>),
45    /// Hot-add a VPCI device to VTL0 at runtime with the supplied instance ID.
46    AddVpciDevice(FailableRpc<(Guid, Resource<PciDeviceHandleKind>), ()>),
47    /// Hot-remove a dynamically added VPCI device by instance ID.
48    RemoveVpciDevice(FailableRpc<Guid, ()>),
49    /// Dump VM state (VP registers + memory) to a `.vmrs` file.
50    ///
51    /// The worker pauses the VM internally, collects state, and restores
52    /// the prior running state afterward. The caller provides an open file
53    /// handle to write to (typically a temporary file that gets renamed
54    /// into place on success).
55    DumpState(FailableRpc<File, ()>),
56}
57
58#[derive(Debug, MeshPayload, thiserror::Error)]
59pub enum PulseSaveRestoreError {
60    #[error("reset not supported")]
61    ResetNotSupported,
62    #[error("pulse save+restore failed")]
63    Other(#[source] RemoteError),
64}
65
66impl From<anyhow::Error> for PulseSaveRestoreError {
67    fn from(err: anyhow::Error) -> Self {
68        Self::Other(RemoteError::new(err))
69    }
70}
71
72impl fmt::Debug for VmRpc {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        let s = match self {
75            VmRpc::Reset(_) => "Reset",
76            VmRpc::Save(_) => "Save",
77            VmRpc::Resume(_) => "Resume",
78            VmRpc::Pause(_) => "Pause",
79            VmRpc::ClearHalt(_) => "ClearHalt",
80            VmRpc::Nmi(_) => "Nmi",
81            VmRpc::AddVmbusDevice(_) => "AddVmbusDevice",
82            VmRpc::ConnectHvsock(_) => "ConnectHvsock",
83            VmRpc::AddHvsockListener(_) => "AddHvsockListener",
84            VmRpc::RemoveHvsockListener(_) => "RemoveHvsockListener",
85            VmRpc::PulseSaveRestore(_) => "PulseSaveRestore",
86            VmRpc::StartReloadIgvm(_) => "StartReloadIgvm",
87            VmRpc::CompleteReloadIgvm(_) => "CompleteReloadIgvm",
88            VmRpc::ReadMemory(_) => "ReadMemory",
89            VmRpc::WriteMemory(_) => "WriteMemory",
90            VmRpc::UpdateCliParams(_) => "UpdateCliParams",
91            VmRpc::AddPcieDevice(_) => "AddPcieDevice",
92            VmRpc::RemovePcieDevice(_) => "RemovePcieDevice",
93            VmRpc::AddVpciDevice(_) => "AddVpciDevice",
94            VmRpc::RemoveVpciDevice(_) => "RemoveVpciDevice",
95            VmRpc::DumpState(_) => "DumpState",
96        };
97        f.pad(s)
98    }
99}