qapi 0.15.0

QEMU QMP and Guest Agent API
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#[cfg(feature = "qapi-qmp")]
use qapi_qmp::{QmpMessage, QmpMessageAny, QapiCapabilities, QMPCapability};

use qapi_spec::Response;
use crate::{Any, Execute, ExecuteResult, Command};

use std::collections::BTreeMap;
use std::convert::TryInto;
use std::marker::Unpin;
use std::sync::{Arc, Mutex as StdMutex, atomic::{AtomicUsize, AtomicBool, Ordering}};
use std::task::{Context, Poll};
use std::pin::Pin;
use std::io;
use futures::channel::oneshot;
use futures::task::AtomicWaker;
use futures::lock::Mutex;
use futures::{Future, FutureExt, Sink, SinkExt, Stream};
use serde::Deserialize;
use log::{trace, info, warn};

#[cfg(feature = "tokio-util")]
mod codec;

#[cfg(feature = "tokio")]
mod tokio;
#[cfg(feature = "tokio")]
pub use self::tokio::*;

#[cfg(feature = "tower-service")]
mod tower;

pub struct QapiStream<R, W> {
    service: QapiService<W>,
    events: QapiEvents<R>,
}

impl<R, W> QapiStream<R, W> {
    pub fn with_parts(service: QapiService<W>, events: QapiEvents<R>) -> Self {
        Self {
            service,
            events,
        }
    }

    pub fn into_parts(self) -> (QapiService<W>, QapiEvents<R>) {
        (self.service, self.events)
    }

    #[cfg(feature = "async-tokio-spawn")]
    pub fn spawn_tokio(self) -> (QapiService<W>, ::tokio::task::JoinHandle<()>) where
        QapiEvents<R>: Future<Output=io::Result<()>> + Send + 'static,
    {
        let handle = self.events.spawn_tokio();
        (self.service, handle)
    }

    pub fn execute<'a, C: Command + 'a>(&'a mut self, command: C) -> impl Future<Output=ExecuteResult<C>> + 'a where
        QapiEvents<R>: Future<Output=io::Result<()>> + Unpin,
        W: Sink<Execute<C, u32>, Error=io::Error> + Unpin
    {
        let execute = self.service.execute(command).fuse();

        async move {
            futures::pin_mut!(execute);

            futures::select_biased! {
                res = execute => res,
                res = (&mut self.events).fuse() => {
                    res?;
                    Err(io::Error::new(io::ErrorKind::UnexpectedEof, "unexpected EOF when executing command").into())
                },
            }
        }
    }
}

#[cfg(feature = "qapi-qmp")]
pub struct QmpStreamNegotiation<S, W> {
    pub stream: QapiStream<S, W>,
    pub capabilities: QapiCapabilities,
}

#[cfg(feature = "qapi-qmp")]
impl<S, W> QmpStreamNegotiation<S, W> where
    QapiEvents<S>: Future<Output=io::Result<()>> + Unpin,
    W: Sink<Execute<qapi_qmp::qmp_capabilities, u32>, Error=io::Error> + Unpin,
{
    pub async fn negotiate_caps<C>(mut self, caps: C) -> io::Result<QapiStream<S, W>> where
        C: IntoIterator<Item=QMPCapability>,
    {
        let _ = self.stream.execute(qapi_qmp::qmp_capabilities {
            enable: Some(caps.into_iter().collect()),
        }).await?;

        Ok(self.stream)
    }

    pub async fn negotiate(self) -> io::Result<QapiStream<S, W>> {
        self.negotiate_caps(std::iter::empty()).await
    }
}

type QapiCommandMap = BTreeMap<u32, oneshot::Sender<Result<Any, qapi_spec::Error>>>;

pub struct QapiService<W> {
    shared: Arc<QapiShared>,
    write: Arc<Mutex<W>>,
    id_counter: AtomicUsize,
}

impl<W> QapiService<W> {
    #[cfg(feature = "tokio")]
    fn new(write: W, shared: Arc<QapiShared>) -> Self {
        QapiService {
            shared,
            write: Mutex::new(write).into(),
            id_counter: AtomicUsize::new(0),
        }
    }

    fn next_oob_id(&self) -> u32 {
        self.id_counter.fetch_add(1, Ordering::Relaxed) as _
    }

    fn command_id(&self) -> Option<u32> {
        if self.shared.supports_oob {
            Some(self.next_oob_id())
        } else {
            None
        }
    }

    fn command_response<C: Command>(receiver: oneshot::Receiver<Result<Any, qapi_spec::Error>>) -> impl Future<Output=ExecuteResult<C>> {
        receiver.map(|res| match res {
            Ok(Ok(res)) => C::Ok::deserialize(&res)
                .map_err(io::Error::from).map_err(From::from),
            Ok(Err(e)) => Err(e.into()),
            Err(_cancelled) => Err(io::Error::new(io::ErrorKind::UnexpectedEof, "QAPI stream disconnected").into()),
        })
    }

    pub fn execute<C: Command>(&self, command: C) -> impl Future<Output=ExecuteResult<C>> where
        W: Sink<Execute<C, u32>, Error=io::Error> + Unpin
    {
        let id = self.command_id();
        let sink = self.write.clone();
        let shared = self.shared.clone();
        let command = Execute::new(command, id);

        async move {
            let mut sink = sink.lock().await;
            let receiver = shared.command_insert(id.unwrap_or_default());

            sink.send(command).await?;
            if id.is_some() {
                // retain write lock only if id/oob execution isn't supported
                drop(sink)
            }

            Self::command_response::<C>(receiver).await
        }
    }

    /*pub async fn execute_oob<C: Command>(&self, command: C) -> io::Result<ExecuteResult<C>> {
        /* TODO: should we assert C::ALLOW_OOB here and/or at the type level?
         * If oob isn't supported should we fall back to serial execution or error?
         */
        self.execute_(command, true).await
    }*/

    #[cfg(feature = "qapi-qga")]
    pub fn guest_sync(&self, sync_value: i32) -> impl Future<Output=Result<(), crate::ExecuteError>> where
        W: Sink<Execute<qapi_qga::guest_sync, u32>, Error=io::Error> + Unpin
    {
        let id = sync_value.into();
        self.execute(qapi_qga::guest_sync {
            id,
        }).map(move |res| res.and_then(|res| if res == id {
            Ok(())
        } else {
            Err(io::Error::new(io::ErrorKind::InvalidData, "QGA sync failed").into())
        }))
    }

    fn stop(&self) {
        let mut commands = self.shared.commands.lock().unwrap();
        if self.shared.abandoned.load(Ordering::Relaxed) {
            self.shared.stop();
        }
        commands.abandoned = true;
    }
}

impl<W> Drop for QapiService<W> {
    fn drop(&mut self) {
        self.stop();
    }
}

#[derive(Default)]
struct QapiSharedCommands {
    pending: QapiCommandMap,
    abandoned: bool,
}

struct QapiShared {
    commands: StdMutex<QapiSharedCommands>,
    stop_waker: AtomicWaker,
    stop: AtomicBool,
    abandoned: AtomicBool,
    supports_oob: bool,
}

impl QapiShared {
    #[cfg(feature = "tokio")]
    fn new(supports_oob: bool) -> Self {
        Self {
            commands: Default::default(),
            stop_waker: Default::default(),
            stop: Default::default(),
            abandoned: Default::default(),
            supports_oob,
        }
    }

    fn stop(&self) {
        self.stop.store(true, Ordering::Relaxed);
        self.stop_waker.wake();
    }

    fn is_stopped(&self) -> bool {
        self.stop.load(Ordering::Relaxed)
    }

    fn poll_next<T, P: FnOnce(&mut Context) -> Poll<Option<T>>>(&self, cx: &mut Context, poll: P) -> Poll<Option<T>> {
        if self.is_stopped() {
            return Poll::Ready(None)
        }

        // attempt to complete the future
        match poll(cx) {
            Poll::Ready(res) => {
                if res.is_none() {
                    self.stop.store(true, Ordering::Relaxed);
                }
                Poll::Ready(res)
            },
            Poll::Pending => {
                self.stop_waker.register(cx.waker());
                if self.is_stopped() {
                    Poll::Ready(None)
                } else {
                    Poll::Pending
                }
            },
        }
    }

    fn command_remove(&self, id: u32) -> Option<oneshot::Sender<Result<Any, qapi_spec::Error>>> {
        let mut commands = self.commands.lock().unwrap();
        commands.pending.remove(&id)
    }

    fn command_insert(&self, id: u32) -> oneshot::Receiver<Result<Any, qapi_spec::Error>> {
        let (sender, receiver) = oneshot::channel();
        let mut commands = self.commands.lock().unwrap();
        if !commands.abandoned {
            // otherwise sender is dropped immediately
            if let Some(_prev) = commands.pending.insert(id, sender) {
                panic!("QAPI duplicate command id {:?}, this should not happen", id);
            }
        }
        receiver
    }
}

#[must_use]
pub struct QapiEvents<S> {
    stream: S,
    shared: Arc<QapiShared>,
}

impl<S> QapiEvents<S> {
    pub fn release(&self) -> Result<(), ()> {
        let commands = self.shared.commands.lock().unwrap();
        if commands.abandoned {
            Err(())
        } else {
            self.shared.abandoned.store(true, Ordering::Relaxed);
            Ok(())
        }
    }

    pub async fn into_future(self) -> () where
        Self: Future<Output=io::Result<()>>,
    {
        if self.release().is_err() {
            info!("QAPI service abandoned before spawning");
            return
        }

        match self.await {
            Ok(()) => (),
            Err(e) =>
                warn!("QAPI stream closed with error {:?}", e),
        }
    }

    pub fn spawn<SP: futures::task::Spawn>(self, spawn: SP) -> Result<(), futures::task::SpawnError> where
        Self: Future<Output=io::Result<()>> + Send + 'static,
        S: 'static
    {
        use futures::task::SpawnExt;

        spawn.spawn(self.into_future())
    }

    #[cfg(feature = "async-tokio-spawn")]
    pub fn spawn_tokio(self) -> ::tokio::task::JoinHandle<()> where
        Self: Future<Output=io::Result<()>> + Send + 'static,
        S: 'static
    {
        ::tokio::spawn(self.into_future())
    }
}

impl<S> Drop for QapiEvents<S> {
    fn drop(&mut self) {
        let mut commands = self.shared.commands.lock().unwrap();
        commands.pending.clear();
        commands.abandoned = true;
    }
}

fn response_id<T>(res: &Response<T>, supports_oob: bool) -> io::Result<u32> {
    match (res.id().and_then(|id| id.as_u64()), supports_oob) {
        (Some(id), true) =>
            id.try_into().map_err(|e|
                io::Error::new(io::ErrorKind::InvalidData, e)
            ),
        (None, false) =>
            Ok(Default::default()),
        (None, true) =>
            Err(io::Error::new(io::ErrorKind::InvalidData, format!("QAPI expected response with numeric ID, got {:?}", res.id()))),
        (Some(..), false) =>
            Err(io::Error::new(io::ErrorKind::InvalidData, format!("QAPI expected response without ID, got {:?}", res.id()))),
    }
}

fn handle_response(shared: &QapiShared, res: Response<Any>) -> io::Result<()> {
    let id = response_id(&res, shared.supports_oob)?;

    if let Some(sender) = shared.command_remove(id) {
        sender.send(res.result()).map_err(|_e|
            io::Error::new(io::ErrorKind::InvalidData, format!("failed to send response for ID {:?}", id))
        )
    } else {
        Err(io::Error::new(io::ErrorKind::InvalidData, format!("unknown QAPI response with ID {:?}", res.id())))
    }
}

impl<M, S> Future for QapiEvents<S> where
    S: Stream<Item=io::Result<M>>,
    M: TryInto<Response<Any>>,
{
    type Output = io::Result<()>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = unsafe { self.get_unchecked_mut() };
        let stream = unsafe { Pin::new_unchecked(&mut this.stream) };
        let shared = &this.shared;

        shared.poll_next(cx, |cx| Poll::Ready(Some(match futures::ready!(stream.poll_next(cx)) {
            None => return Poll::Ready(None),
            Some(Err(e)) => Err(e),
            Some(Ok(res)) => match res.try_into() {
                Ok(res) => match handle_response(shared, res) {
                    Err(e) => Err(e),
                    Ok(()) => {
                        cx.waker().wake_by_ref(); // TODO: I've seen this not work with tokio?
                        return Poll::Pending
                    },
                },
                Err(..) => {
                    trace!("Ignoring QAPI event");
                    cx.waker().wake_by_ref(); // TODO: I've seen this not work with tokio?
                    return Poll::Pending
                },
            },
        }))).map(|res| res.unwrap_or(Ok(())))
    }
}

#[cfg(feature = "qapi-qmp")]
impl<S: Stream<Item=io::Result<QmpMessageAny>>> Stream for QapiEvents<S> {
    type Item = io::Result<qapi_qmp::Event>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = unsafe { self.get_unchecked_mut() };
        let stream = unsafe { Pin::new_unchecked(&mut this.stream) };
        let shared = &this.shared;

        shared.poll_next(cx, |cx| Poll::Ready(match futures::ready!(stream.poll_next(cx)) {
            None => None, // eof
            Some(Err(e)) => Some(Err(e)),
            Some(Ok(QmpMessage::Event(e))) => Some(Ok(e)),
            Some(Ok(QmpMessage::Response(res))) => match handle_response(shared, res) {
                Err(e) => Some(Err(e)),
                Ok(()) => {
                    cx.waker().wake_by_ref(); // TODO: I've seen this not work with tokio?
                    return Poll::Pending
                },
            },
        }))
    }
}