guest_emulation_log/
lib.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Guest Emulation Log - GEL
//!
//! The GEL is the host side of a communication channel that uses VMBUS to
//! send logs from the guest to the host. This is an implementation to support
//! better integration testing within the HvLite CI.

#![expect(missing_docs)]
#![forbid(unsafe_code)]

pub mod resolver;

use async_trait::async_trait;
use serde::Deserialize;
use std::borrow::Cow;
use std::collections::HashMap;
use task_control::StopTask;
use thiserror::Error;
use vmbus_async::async_dgram::AsyncRecvExt;
use vmbus_async::pipe::MessagePipe;
use vmbus_channel::RawAsyncChannel;
use vmbus_channel::bus::ChannelType;
use vmbus_channel::bus::OfferParams;
use vmbus_channel::channel::ChannelOpenError;
use vmbus_channel::gpadl_ring::GpadlRingMem;
use vmbus_channel::simple::SaveRestoreSimpleVmbusDevice;
use vmbus_channel::simple::SimpleVmbusDevice;
use vmbus_ring::RingMem;
use vmcore::save_restore::NoSavedState;
use zerocopy::FromBytes;
use zerocopy::IntoBytes;

#[derive(Debug, Error)]
enum Error {
    #[error("pipe failed")]
    PipeFailure(#[source] std::io::Error),
    #[error("trace message has invalid size {0}")]
    InvalidTraceSize(u16),
    #[error("invalid payload length {0}")]
    InvalidPayloadSize(usize),
}

/// VMBUS device that implements the host side of the Guest Emulation Log protocol.
#[non_exhaustive]
pub struct GuestEmulationLog {}

impl GuestEmulationLog {
    /// Create a new Host side GEL device.
    pub fn new() -> Self {
        Self {}
    }
}

#[async_trait]
impl SimpleVmbusDevice for GuestEmulationLog {
    type Runner = GelChannel;
    type SavedState = NoSavedState;

    fn offer(&self) -> OfferParams {
        OfferParams {
            interface_name: "gel".to_owned(),
            interface_id: get_protocol::GUEST_EMULATION_INTERFACE_TYPE,
            instance_id: get_protocol::GET_LOG_INTERFACE_GUID,
            channel_type: ChannelType::Pipe { message_mode: true },
            ..Default::default()
        }
    }

    fn inspect(&mut self, req: inspect::Request<'_>, task: Option<&mut GelChannel>) {
        let _ = (req, task);
    }

    fn open(
        &mut self,
        channel: RawAsyncChannel<GpadlRingMem>,
        _guest_memory: guestmem::GuestMemory,
    ) -> Result<Self::Runner, ChannelOpenError> {
        let pipe = MessagePipe::new(channel)?;
        Ok(GelChannel::new(pipe))
    }

    async fn run(
        &mut self,
        stop: &mut StopTask<'_>,
        channel: &mut GelChannel,
    ) -> Result<(), task_control::Cancelled> {
        stop.until_stopped(async {
            if let Err(err) = channel.process(self).await {
                tracing::error!(
                    error = &err as &dyn std::error::Error,
                    "trace channel failed"
                );
            }
        })
        .await
    }

    fn supports_save_restore(
        &mut self,
    ) -> Option<
        &mut dyn SaveRestoreSimpleVmbusDevice<SavedState = Self::SavedState, Runner = Self::Runner>,
    > {
        Some(self)
    }
}

impl SaveRestoreSimpleVmbusDevice for GuestEmulationLog {
    fn save_open(&mut self, _runner: &Self::Runner) -> Self::SavedState {
        NoSavedState
    }

    fn restore_open(
        &mut self,
        NoSavedState: Self::SavedState,
        channel: RawAsyncChannel<GpadlRingMem>,
    ) -> Result<Self::Runner, ChannelOpenError> {
        let pipe = MessagePipe::new(channel)?;
        Ok(GelChannel::new(pipe))
    }
}

#[derive(Deserialize, Debug, Default)]
struct TraceFields<'a> {
    #[serde(borrow)]
    message: Option<Cow<'a, str>>,
    #[serde(flatten, borrow)]
    extra: ExtraFields<'a>,
}

#[derive(Deserialize, Debug, Default)]
#[serde(transparent)]
struct ExtraFields<'a> {
    #[serde(borrow)]
    map: HashMap<Cow<'a, str>, serde_json::value::Value>,
}

impl std::fmt::Display for ExtraFields<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut map = f.debug_map();
        for (k, v) in &self.map {
            map.entry(k, &format_args!("{}", v));
        }
        map.finish()
    }
}

#[derive(Deserialize, Debug)]
struct TraceData<'a> {
    #[serde(borrow)]
    target: Cow<'a, str>,
    #[serde(borrow)]
    level: Cow<'a, str>,
    #[serde(default, borrow)]
    fields: TraceFields<'a>,
    #[serde(flatten, borrow)]
    extra: ExtraFields<'a>,
}

/// The GEL task.
pub struct GelChannel<T: RingMem = GpadlRingMem> {
    channel: MessagePipe<T>,
}

impl<T: RingMem + Unpin> GelChannel<T> {
    fn new(channel: MessagePipe<T>) -> Self {
        Self { channel }
    }

    async fn process(&mut self, _state: &mut GuestEmulationLog) -> Result<(), Error> {
        let mut buffer = vec![0; get_protocol::TRACE_LOGGING_NOTIFICATION_MAX_SIZE];
        loop {
            let n = self
                .channel
                .recv(buffer.as_mut_bytes())
                .await
                .map_err(Error::PipeFailure)?;

            if n == 0 {
                break;
            }

            let buffer = &buffer[..n];

            let (header, buffer) =
                get_protocol::TraceLoggingNotificationHeader::read_from_prefix(buffer)
                    .map_err(|_| Error::InvalidPayloadSize(n))?; // TODO: zerocopy: map_err (https://github.com/microsoft/openvmm/issues/759)

            let message = buffer
                .get(
                    header.message.offset as usize
                        ..(header.message.offset + header.message.size) as usize,
                )
                .ok_or(Error::InvalidTraceSize(n as u16))?;

            match serde_json::from_slice::<'_, TraceData<'_>>(message) {
                Ok(data) => match &*data.level {
                    "ERROR" => {
                        tracing::error!(
                            target: "paravisor_log",
                            inner_target = &*data.target,
                            message = data.fields.message.as_deref(),
                            fields = %data.fields.extra,
                            extra = %data.extra,
                        )
                    }
                    "WARN" => {
                        tracing::warn!(
                            target: "paravisor_log",
                            inner_target = &*data.target,
                            message = data.fields.message.as_deref(),
                            fields = %data.fields.extra,
                            extra = %data.extra,
                        )
                    }
                    "INFO" => {
                        tracing::info!(
                            target: "paravisor_log",
                            inner_target = &*data.target,
                            message = data.fields.message.as_deref(),
                            fields = %data.fields.extra,
                            extra = %data.extra,
                        )
                    }
                    "DEBUG" => {
                        tracing::debug!(
                            target: "paravisor_log",
                            inner_target = &*data.target,
                            message = data.fields.message.as_deref(),
                            fields = %data.fields.extra,
                            extra = %data.extra,
                        )
                    }
                    "TRACE" => {
                        tracing::trace!(
                            target: "paravisor_log",
                            inner_target = &*data.target,
                            message = data.fields.message.as_deref(),
                            fields = %data.fields.extra,
                            extra = %data.extra,
                        )
                    }
                    some_level => {
                        tracing::info!(
                            target: "paravisor_log",
                            inner_level = some_level,
                            inner_target = &*data.target,
                            message = data.fields.message.as_deref(),
                            fields = %data.fields.extra,
                            extra = %data.extra,
                        )
                    }
                },
                Err(err) => {
                    tracing::warn!(
                        target: "paravisor_log",
                        inner_level = ?header.level,
                        error = &err as &dyn std::error::Error,
                        message = String::from_utf8_lossy(message).as_ref(),
                        "failed to parse trace",
                    )
                }
            }
        }
        Ok(())
    }
}