hvlite_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::VmbusDeviceHandleKind;
18
19#[derive(MeshPayload)]
20pub enum VmRpc {
21    Save(FailableRpc<(), ProtobufMessage>),
22    Resume(Rpc<(), bool>),
23    Pause(Rpc<(), bool>),
24    ClearHalt(Rpc<(), bool>),
25    Reset(FailableRpc<(), ()>),
26    Nmi(Rpc<u32, ()>),
27    AddVmbusDevice(FailableRpc<(DeviceVtl, Resource<VmbusDeviceHandleKind>), ()>),
28    ConnectHvsock(FailableRpc<(CancelContext, Guid, DeviceVtl), unix_socket::UnixStream>),
29    PulseSaveRestore(Rpc<(), Result<(), PulseSaveRestoreError>>),
30    StartReloadIgvm(FailableRpc<File, ()>),
31    CompleteReloadIgvm(FailableRpc<bool, ()>),
32    ReadMemory(FailableRpc<(u64, usize), Vec<u8>>),
33    WriteMemory(FailableRpc<(u64, Vec<u8>), ()>),
34    /// Updates the command line parameters that will be passed to the boot shim
35    /// on the *next* VM load. This will replace the existing command line parameters.
36    UpdateCliParams(FailableRpc<String, ()>),
37}
38
39#[derive(Debug, MeshPayload, thiserror::Error)]
40pub enum PulseSaveRestoreError {
41    #[error("reset not supported")]
42    ResetNotSupported,
43    #[error("pulse save+restore failed")]
44    Other(#[source] RemoteError),
45}
46
47impl From<anyhow::Error> for PulseSaveRestoreError {
48    fn from(err: anyhow::Error) -> Self {
49        Self::Other(RemoteError::new(err))
50    }
51}
52
53impl fmt::Debug for VmRpc {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        let s = match self {
56            VmRpc::Reset(_) => "Reset",
57            VmRpc::Save(_) => "Save",
58            VmRpc::Resume(_) => "Resume",
59            VmRpc::Pause(_) => "Pause",
60            VmRpc::ClearHalt(_) => "ClearHalt",
61            VmRpc::Nmi(_) => "Nmi",
62            VmRpc::AddVmbusDevice(_) => "AddVmbusDevice",
63            VmRpc::ConnectHvsock(_) => "ConnectHvsock",
64            VmRpc::PulseSaveRestore(_) => "PulseSaveRestore",
65            VmRpc::StartReloadIgvm(_) => "StartReloadIgvm",
66            VmRpc::CompleteReloadIgvm(_) => "CompleteReloadIgvm",
67            VmRpc::ReadMemory(_) => "ReadMemory",
68            VmRpc::WriteMemory(_) => "WriteMemory",
69            VmRpc::UpdateCliParams(_) => "UpdateCliParams",
70        };
71        f.pad(s)
72    }
73}