microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
petri/src/worker.rs
79lines · modecode
| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | use hvlite_defs::config::Config; |
| 4 | use hvlite_defs::rpc::PulseSaveRestoreError; |
| 5 | use hvlite_defs::rpc::VmRpc; |
| 6 | use hvlite_defs::worker::VmWorkerParameters; |
| 7 | use hvlite_defs::worker::VM_WORKER; |
| 8 | use mesh::rpc::RpcSend; |
| 9 | use mesh_worker::WorkerHandle; |
| 10 | use mesh_worker::WorkerHost; |
| 11 | use vmm_core_defs::HaltReason; |
| 12 | |
| 13 | pub(crate) struct Worker { |
| 14 | handle: WorkerHandle, |
| 15 | rpc: mesh::Sender<VmRpc>, |
| 16 | } |
| 17 | |
| 18 | impl Worker { |
| 19 | pub(crate) async fn launch( |
| 20 | host: &WorkerHost, |
| 21 | cfg: Config, |
| 22 | ) -> anyhow::Result<(Self, mesh::Receiver<HaltReason>)> { |
| 23 | let (vm_rpc, rpc_recv) = mesh::channel(); |
| 24 | let (notify_send, notify_recv) = mesh::channel(); |
| 25 | |
| 26 | let params = VmWorkerParameters { |
| 27 | hypervisor: None, |
| 28 | cfg, |
| 29 | saved_state: None, |
| 30 | rpc: rpc_recv, |
| 31 | notify: notify_send, |
| 32 | }; |
| 33 | let vm_worker = host.launch_worker(VM_WORKER, params).await?; |
| 34 | |
| 35 | Ok(( |
| 36 | Self { |
| 37 | handle: vm_worker, |
| 38 | rpc: vm_rpc, |
| 39 | }, |
| 40 | notify_recv, |
| 41 | )) |
| 42 | } |
| 43 | |
| 44 | pub(crate) async fn resume(&self) -> Result<bool, mesh::RecvError> { |
| 45 | self.rpc.call(VmRpc::Resume, ()).await |
| 46 | } |
| 47 | |
| 48 | pub(crate) async fn reset(&self) -> anyhow::Result<()> { |
| 49 | self.rpc.call(VmRpc::Reset, ()).await??; |
| 50 | Ok(()) |
| 51 | } |
| 52 | |
| 53 | pub(crate) async fn pulse_save_restore( |
| 54 | &self, |
| 55 | ) -> Result<Result<(), PulseSaveRestoreError>, mesh::RecvError> { |
| 56 | self.rpc.call(VmRpc::PulseSaveRestore, ()).await |
| 57 | } |
| 58 | |
| 59 | pub(crate) async fn restart_openhcl( |
| 60 | &self, |
| 61 | send: &mesh::Sender<get_resources::ged::GuestEmulationRequest>, |
| 62 | file: std::fs::File, |
| 63 | ) -> anyhow::Result<()> { |
| 64 | hvlite_helpers::underhill::service_underhill(&self.rpc, send, file).await |
| 65 | } |
| 66 | |
| 67 | pub(crate) async fn inspect_all(&self) -> String { |
| 68 | let mut inspection = inspect::inspect("", &self.handle); |
| 69 | inspection.resolve().await; |
| 70 | let results = inspection.results(); |
| 71 | format!("{results:#}",) |
| 72 | } |
| 73 | |
| 74 | pub(crate) async fn shutdown(mut self) -> anyhow::Result<()> { |
| 75 | self.handle.stop(); |
| 76 | self.handle.join().await?; |
| 77 | Ok(()) |
| 78 | } |
| 79 | } |
| 80 | |