petri/
worker.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::OpenHclServicingFlags;
use get_resources::ged::GuestServicingFlags;
use hvlite_defs::config::Config;
use hvlite_defs::rpc::PulseSaveRestoreError;
use hvlite_defs::rpc::VmRpc;
use hvlite_defs::worker::VM_WORKER;
use hvlite_defs::worker::VmWorkerParameters;
use mesh::rpc::RpcError;
use mesh::rpc::RpcSend;
use mesh_worker::WorkerHandle;
use mesh_worker::WorkerHost;
use vmm_core_defs::HaltReason;

pub(crate) struct Worker {
    handle: WorkerHandle,
    rpc: mesh::Sender<VmRpc>,
}

impl Worker {
    pub(crate) async fn launch(
        host: &WorkerHost,
        cfg: Config,
    ) -> anyhow::Result<(Self, mesh::Receiver<HaltReason>)> {
        let (vm_rpc, rpc_recv) = mesh::channel();
        let (notify_send, notify_recv) = mesh::channel();

        let params = VmWorkerParameters {
            hypervisor: None,
            cfg,
            saved_state: None,
            rpc: rpc_recv,
            notify: notify_send,
        };
        let vm_worker = host.launch_worker(VM_WORKER, params).await?;

        Ok((
            Self {
                handle: vm_worker,
                rpc: vm_rpc,
            },
            notify_recv,
        ))
    }

    pub(crate) async fn resume(&self) -> Result<bool, RpcError> {
        self.rpc.call(VmRpc::Resume, ()).await
    }

    pub(crate) async fn reset(&self) -> anyhow::Result<()> {
        self.rpc.call(VmRpc::Reset, ()).await??;
        Ok(())
    }

    pub(crate) async fn pulse_save_restore(&self) -> Result<(), RpcError<PulseSaveRestoreError>> {
        self.rpc.call_failable(VmRpc::PulseSaveRestore, ()).await
    }

    pub(crate) async fn restart_openhcl(
        &self,
        send: &mesh::Sender<get_resources::ged::GuestEmulationRequest>,
        flags: OpenHclServicingFlags,
        file: std::fs::File,
    ) -> anyhow::Result<()> {
        hvlite_helpers::underhill::service_underhill(
            &self.rpc,
            send,
            GuestServicingFlags {
                nvme_keepalive: flags.enable_nvme_keepalive,
            },
            file,
        )
        .await
    }

    pub(crate) async fn inspect_all(&self) -> String {
        let mut inspection = inspect::inspect("", &self.handle);
        inspection.resolve().await;
        let results = inspection.results();
        format!("{results:#}",)
    }

    pub(crate) async fn shutdown(mut self) -> anyhow::Result<()> {
        self.handle.stop();
        self.handle.join().await?;
        Ok(())
    }
}