summary refs log tree commit diff
path: root/devices/src/virtio/controller.rs
blob: a1656c66c42e2d5ea584eedff83b940d2f559c22 (plain) (blame)
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
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! This module implements the virtio wayland used by the guest to access the host's wayland server.
//!
//! The virtio wayland protocol is done over two queues: `in` and `out`. The `in` queue is used for
//! sending commands to the guest that are generated by the host, usually messages from the wayland
//! server. The `out` queue is for commands from the guest, usually requests to allocate shared
//! memory, open a wayland server connection, or send data over an existing connection.
//!
//! Each `WlVfd` represents one virtual file descriptor created by either the guest or the host.
//! Virtual file descriptors contain actual file descriptors, either a shared memory file descriptor
//! or a unix domain socket to the wayland server. In the shared memory case, there is also an
//! associated slot that indicates which KVM memory slot the memory is installed into, as well as a
//! page frame number that the guest can access the memory from.
//!
//! The types starting with `Ctrl` are structures representing the virtio wayland protocol "on the
//! wire." They are decoded and executed in the `execute` function and encoded as some variant of
//! `WlResp` for responses.
//!
//! There is one `WlState` instance that contains every known vfd and the current state of `in`
//! queue. The `in` queue requires extra state to buffer messages to the guest in case the `in`
//! queue is already full. The `WlState` also has a control socket necessary to fulfill certain
//! requests, such as those registering guest memory.
//!
//! The `Worker` is responsible for the poll loop over all possible events, encoding/decoding from
//! the virtio queue, and routing messages in and out of `WlState`. Possible events include the kill
//! event, available descriptors on the `in` or `out` queue, and incoming data on any vfd's socket.

use std::collections::BTreeMap as Map;
use std::os::unix::io::{AsRawFd, RawFd};
use std::path::PathBuf;
use std::sync::Arc;
use std::thread;

use super::resource_bridge::*;
use super::{Interrupt, InterruptProxyEvent, Queue, VirtioDevice, TYPE_WL, VIRTIO_F_VERSION_1};
use crate::MemoryParams;
use vm_control::{MaybeOwnedFd, VmMemoryControlRequestSocket};

use msg_socket::{MsgOnSocket, MsgReceiver, MsgSocket};
use serde::{Deserialize, Serialize};
use sys_util::net::UnixSeqpacket;
use sys_util::{error, EventFd, GuestMemory, PollContext, PollToken, SharedMemory};

#[derive(Debug, MsgOnSocket)]
pub enum MsgOnSocketRequest {
    Create {
        // wayland_paths: Map<String, PathBuf>,
        vm_socket: MaybeOwnedFd<UnixSeqpacket>,
        // resource_bridge: Option<ResourceRequestSocket>,
        memory_params: MemoryParams,
    },
    Activate {
        shm: MaybeOwnedFd<SharedMemory>,
        interrupt: MaybeOwnedFd<UnixSeqpacket>,
        interrupt_resample_evt: MaybeOwnedFd<EventFd>,
        in_queue: Queue,
        out_queue: Queue,
        in_queue_evt: MaybeOwnedFd<EventFd>,
        out_queue_evt: MaybeOwnedFd<EventFd>,
    },
    Kill,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum BincodeRequest {
    WriteConfig { offset: u64, data: Vec<u8> },
}

pub type Request = poly_msg_socket::Value<MsgOnSocketRequest, BincodeRequest>;

impl From<MsgOnSocketRequest> for Request {
    fn from(request: MsgOnSocketRequest) -> Self {
        Self::MsgOnSocket(request)
    }
}

impl From<BincodeRequest> for Request {
    fn from(request: BincodeRequest) -> Self {
        Self::Bincode(request)
    }
}

#[derive(Debug, MsgOnSocket)]
pub enum MsgOnSocketResponse {
    Kill,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct BincodeResponse;

pub type Response = poly_msg_socket::Value<MsgOnSocketResponse, BincodeResponse>;

impl From<MsgOnSocketResponse> for Response {
    fn from(response: MsgOnSocketResponse) -> Self {
        Self::MsgOnSocket(response)
    }
}

impl From<BincodeResponse> for Response {
    fn from(response: BincodeResponse) -> Self {
        Self::Bincode(response)
    }
}

use poly_msg_socket::PolyMsgSocket;
type Socket =
    PolyMsgSocket<MsgOnSocketRequest, MsgOnSocketResponse, BincodeRequest, BincodeResponse>;

const VIRTIO_WL_F_TRANS_FLAGS: u32 = 0x01;

// TODO: support arbitrary number of queues
const QUEUE_SIZE: u16 = 16;
const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE, QUEUE_SIZE];

struct Worker {
    device_socket: Arc<Socket>,
    interrupt: Interrupt,
    interrupt_socket: MsgSocket<(), InterruptProxyEvent>,

    shutdown: bool,
}

impl Worker {
    fn new(
        device_socket: Arc<Socket>,
        interrupt: Interrupt,
        interrupt_socket: MsgSocket<(), InterruptProxyEvent>,
    ) -> Self {
        Self {
            device_socket,
            interrupt,
            interrupt_socket,
            shutdown: false,
        }
    }

    fn handle_response(&mut self) {
        use poly_msg_socket::Value::*;
        match self.device_socket.recv() {
            Ok(MsgOnSocket(MsgOnSocketResponse::Kill)) => {
                self.shutdown = true;
            }

            Ok(Bincode(BincodeResponse)) => unreachable!(),

            Err(e) => {
                error!("recv failed: {:?}", e);
            }
        }
    }

    fn interrupt(&self) {
        use InterruptProxyEvent::*;
        match self.interrupt_socket.recv() {
            Ok(SignalUsedQueue(value)) => self.interrupt.signal_used_queue(value).unwrap(),
            Ok(SignalConfigChanged) => self.interrupt.signal_config_changed().unwrap(),
            Ok(InterruptResample) => self.interrupt.interrupt_resample().unwrap(),

            Err(e) => {
                eprintln!("recv error: {}", e);
                panic!("recv error: {}", e)
            }
        }
    }

    fn kill(&self) {
        if let Err(e) = self.device_socket.send(poly_msg_socket::Value::MsgOnSocket(
            MsgOnSocketRequest::Kill,
        )) {
            error!("failed to send Kill message: {}", e);
        }
    }

    fn run(mut self, kill_evt: EventFd) {
        #[derive(Debug, PollToken)]
        enum Token {
            Device,
            Interrupt,
            Kill,
        }

        let poll_ctx: PollContext<Token> = match PollContext::build_with(&[
            (&*self.device_socket, Token::Device),
            (&self.interrupt_socket, Token::Interrupt),
            (&kill_evt, Token::Kill),
        ]) {
            Ok(pc) => pc,
            Err(e) => {
                error!("failed creating PollContext: {}", e);
                return;
            }
        };

        while !self.shutdown {
            let events = match poll_ctx.wait() {
                Ok(v) => v,
                Err(e) => {
                    error!("failed polling for events: {}", e);
                    break;
                }
            };

            for event in &events {
                match event.token() {
                    Token::Device => self.handle_response(),
                    Token::Interrupt => self.interrupt(),
                    Token::Kill => self.kill(),
                }
            }
        }
    }
}

pub struct Controller {
    kill_evt: Option<EventFd>,
    worker_thread: Option<thread::JoinHandle<()>>,
    use_transition_flags: bool,
    socket: Arc<Socket>,
}

impl Controller {
    pub fn create(
        wayland_paths: Map<String, PathBuf>,
        vm_socket: VmMemoryControlRequestSocket,
        resource_bridge: Option<ResourceRequestSocket>,
        memory_params: MemoryParams,
        socket: Socket,
    ) -> Result<Controller, poly_msg_socket::Error> {
        socket.send(MsgOnSocketRequest::Create {
            // wayland_paths,
            vm_socket: MaybeOwnedFd::new_borrowed(&vm_socket),
            // resource_bridge,
            memory_params,
        })?;

        Ok(Controller {
            kill_evt: None,
            worker_thread: None,
            use_transition_flags: false,
            socket: Arc::new(socket),
        })
    }
}

impl Drop for Controller {
    fn drop(&mut self) {
        if let Some(kill_evt) = self.kill_evt.take() {
            // Ignore the result because there is nothing we can do about it.
            let _ = kill_evt.write(1);
        }

        if let Some(worker_thread) = self.worker_thread.take() {
            let _ = worker_thread.join();
        }
    }
}

impl VirtioDevice for Controller {
    fn keep_fds(&self) -> Vec<RawFd> {
        let mut keep_fds = Vec::new();

        if let Some(ref kill_evt) = self.kill_evt {
            keep_fds.push(kill_evt.as_raw_fd());
        }

        keep_fds.push(self.socket.as_raw_fd());

        keep_fds
    }

    fn device_type(&self) -> u32 {
        TYPE_WL
    }

    fn queue_max_sizes(&self) -> &[u16] {
        QUEUE_SIZES
    }

    fn features(&self) -> u64 {
        1 << VIRTIO_WL_F_TRANS_FLAGS | 1 << VIRTIO_F_VERSION_1
    }

    fn ack_features(&mut self, value: u64) {
        if value & (1 << VIRTIO_WL_F_TRANS_FLAGS) != 0 {
            self.use_transition_flags = true;
        }
    }

    fn activate(
        &mut self,
        mem: GuestMemory,
        interrupt: Interrupt,
        mut queues: Vec<Queue>,
        queue_evts: Vec<EventFd>,
    ) {
        if queues.len() != QUEUE_SIZES.len() || queue_evts.len() != QUEUE_SIZES.len() {
            return;
        }

        let (self_kill_evt, kill_evt) = match EventFd::new().and_then(|e| Ok((e.try_clone()?, e))) {
            Ok(v) => v,
            Err(e) => {
                error!("failed creating kill EventFd pair: {}", e);
                return;
            }
        };
        self.kill_evt = Some(self_kill_evt);

        let use_transition_flags = self.use_transition_flags;

        let (ours, theirs) = UnixSeqpacket::pair().expect("pair failed");

        if let Err(e) = self.socket.send(MsgOnSocketRequest::Activate {
            shm: MaybeOwnedFd::new_borrowed(&mem),
            interrupt: MaybeOwnedFd::new_borrowed(&theirs),
            interrupt_resample_evt: MaybeOwnedFd::new_borrowed(interrupt.get_resample_evt()),
            in_queue: queues.remove(0),
            out_queue: queues.remove(0),
            in_queue_evt: MaybeOwnedFd::new_borrowed(&queue_evts[0]),
            out_queue_evt: MaybeOwnedFd::new_borrowed(&queue_evts[1]),
        }) {
            error!("failed to send Activate: {}", e);
            return;
        }

        let socket = Arc::clone(&self.socket);
        let worker_result = thread::Builder::new()
            .name("virtio_wl".to_string())
            .spawn(move || {
                Worker::new(socket, interrupt, MsgSocket::new(ours)).run(kill_evt);
            });

        match worker_result {
            Err(e) => {
                error!("failed to spawn virtio_wl worker: {}", e);
                return;
            }
            Ok(join_handle) => {
                self.worker_thread = Some(join_handle);
            }
        }
    }
}