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
// Copyright 2021 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

mod block;
mod connection;
#[cfg(feature = "gpu")]
pub mod gpu;
mod handler;
#[cfg(feature = "net")]
mod net;
#[cfg(feature = "audio")]
pub mod snd;

pub use block::run_block_device;
pub use block::Options as BlockOptions;
pub use connection::sys::VhostUserListener;
pub use connection::sys::VhostUserStream;
pub use connection::VhostUserConnectionTrait;
use cros_async::Executor;
#[cfg(feature = "gpu")]
pub use gpu::run_gpu_device;
#[cfg(feature = "gpu")]
pub use gpu::Options as GpuOptions;
pub use handler::VhostBackendReqConnectionState;
pub use handler::VhostUserDevice;
#[cfg(feature = "net")]
pub use net::run_net_device;
#[cfg(feature = "net")]
pub use net::NetBackend;
#[cfg(feature = "net")]
pub use net::Options as NetOptions;
#[cfg(feature = "audio")]
pub use snd::run_snd_device;
#[cfg(feature = "audio")]
pub use snd::Options as SndOptions;

pub use crate::virtio::vhost::user::device::connection::BackendConnection;

cfg_if::cfg_if! {
    if #[cfg(any(target_os = "android", target_os = "linux"))] {
        mod console;
        mod fs;
        mod vsock;
        mod wl;

        pub use vsock::{run_vsock_device, Options as VsockOptions, VhostUserVsockDevice};
        pub use wl::{run_wl_device, parse_wayland_sock, Options as WlOptions};
        pub use console::{create_vu_console_device, run_console_device, Options as ConsoleOptions};
        pub use fs::{run_fs_device, Options as FsOptions};
    } else if #[cfg(windows)] {
        #[cfg(all(feature = "net", feature = "slirp"))]
        pub use net::sys::windows::NetBackendConfig;
    }
}

/// A trait for not-yet-built vhost-user devices.
///
/// Upon being given an [[Executor]], a builder can be converted into a [[vmm_vhost::Backend]],
/// which can then process the requests from the front-end.
///
/// We don't build the device directly to ensure that the device only starts threads in the jailed
/// process, not in the main process. [[VhostUserDeviceBuilder::build()]] is called only after
/// jailing, which ensures that any operations by the device are done in the jailed process.
///
/// TODO: Ideally this would return a [[VhostUserDevice]] instead of [[vmm_vhost::Backend]]. Only
/// the vhost-user vhost-vsock device uses the latter and it can probably be migrated to
/// [[VhostUserDevice]].
pub trait VhostUserDeviceBuilder {
    /// Create the vhost-user device.
    ///
    /// `ex` is an executor the device can use to schedule its tasks.
    fn build(self: Box<Self>, ex: &Executor) -> anyhow::Result<Box<dyn vmm_vhost::Backend>>;
}