xref: /aosp_15_r20/external/crosvm/devices/src/virtio/vhost/user/device/mod.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2021 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 mod block;
6 mod connection;
7 #[cfg(feature = "gpu")]
8 pub mod gpu;
9 mod handler;
10 #[cfg(feature = "net")]
11 mod net;
12 #[cfg(feature = "audio")]
13 pub mod snd;
14 
15 pub use block::run_block_device;
16 pub use block::Options as BlockOptions;
17 pub use connection::sys::VhostUserListener;
18 pub use connection::sys::VhostUserStream;
19 pub use connection::VhostUserConnectionTrait;
20 use cros_async::Executor;
21 #[cfg(feature = "gpu")]
22 pub use gpu::run_gpu_device;
23 #[cfg(feature = "gpu")]
24 pub use gpu::Options as GpuOptions;
25 pub use handler::VhostBackendReqConnectionState;
26 pub use handler::VhostUserDevice;
27 #[cfg(feature = "net")]
28 pub use net::run_net_device;
29 #[cfg(feature = "net")]
30 pub use net::NetBackend;
31 #[cfg(feature = "net")]
32 pub use net::Options as NetOptions;
33 #[cfg(feature = "audio")]
34 pub use snd::run_snd_device;
35 #[cfg(feature = "audio")]
36 pub use snd::Options as SndOptions;
37 
38 pub use crate::virtio::vhost::user::device::connection::BackendConnection;
39 
40 cfg_if::cfg_if! {
41     if #[cfg(any(target_os = "android", target_os = "linux"))] {
42         mod console;
43         mod fs;
44         mod vsock;
45         mod wl;
46 
47         pub use vsock::{run_vsock_device, Options as VsockOptions, VhostUserVsockDevice};
48         pub use wl::{run_wl_device, parse_wayland_sock, Options as WlOptions};
49         pub use console::{create_vu_console_device, run_console_device, Options as ConsoleOptions};
50         pub use fs::{run_fs_device, Options as FsOptions};
51     } else if #[cfg(windows)] {
52         #[cfg(all(feature = "net", feature = "slirp"))]
53         pub use net::sys::windows::NetBackendConfig;
54     }
55 }
56 
57 /// A trait for not-yet-built vhost-user devices.
58 ///
59 /// Upon being given an [[Executor]], a builder can be converted into a [[vmm_vhost::Backend]],
60 /// which can then process the requests from the front-end.
61 ///
62 /// We don't build the device directly to ensure that the device only starts threads in the jailed
63 /// process, not in the main process. [[VhostUserDeviceBuilder::build()]] is called only after
64 /// jailing, which ensures that any operations by the device are done in the jailed process.
65 ///
66 /// TODO: Ideally this would return a [[VhostUserDevice]] instead of [[vmm_vhost::Backend]]. Only
67 /// the vhost-user vhost-vsock device uses the latter and it can probably be migrated to
68 /// [[VhostUserDevice]].
69 pub trait VhostUserDeviceBuilder {
70     /// Create the vhost-user device.
71     ///
72     /// `ex` is an executor the device can use to schedule its tasks.
build(self: Box<Self>, ex: &Executor) -> anyhow::Result<Box<dyn vmm_vhost::Backend>>73     fn build(self: Box<Self>, ex: &Executor) -> anyhow::Result<Box<dyn vmm_vhost::Backend>>;
74 }
75