devices/virtio/vhost_user_backend/mod.rs
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
5pub mod connection;
6#[cfg(feature = "gpu")]
7pub mod gpu;
8pub mod handler;
9pub mod params;
10pub use connection::sys::VhostUserListener;
11pub use connection::sys::VhostUserStream;
12pub use connection::VhostUserConnectionTrait;
13use cros_async::Executor;
14#[cfg(feature = "gpu")]
15pub use gpu::run_gpu_device;
16#[cfg(feature = "gpu")]
17pub use gpu::Options as GpuOptions;
18pub use handler::VhostUserDevice;
19
20pub use crate::virtio::vhost_user_backend::connection::BackendConnection;
21
22cfg_if::cfg_if! {
23 if #[cfg(any(target_os = "android", target_os = "linux"))] {
24 mod fs;
25 mod vsock;
26 #[cfg(feature = "virtio_wl")]
27 mod wl;
28
29 pub use vsock::{run_vsock_device, Options as VsockOptions, VhostUserVsockDevice};
30 #[cfg(feature = "virtio_wl")]
31 pub use wl::{run_wl_device, Options as WlOptions};
32 pub use fs::{run_fs_device, Options as FsOptions};
33 } else if #[cfg(windows)] {
34 }
35}
36
37/// A trait for not-yet-built vhost-user devices.
38///
39/// Upon being given an [[Executor]], a builder can be converted into a [[vmm_vhost::Backend]],
40/// which can then process the requests from the front-end.
41///
42/// We don't build the device directly to ensure that the device only starts threads in the jailed
43/// process, not in the main process. [[VhostUserDeviceBuilder::build()]] is called only after
44/// jailing, which ensures that any operations by the device are done in the jailed process.
45///
46/// TODO: Ideally this would return a [[VhostUserDevice]] instead of [[vmm_vhost::Backend]]. Only
47/// the vhost-user vhost-vsock device uses the latter and it can probably be migrated to
48/// [[VhostUserDevice]].
49pub trait VhostUserDeviceBuilder {
50 /// Create the vhost-user device.
51 ///
52 /// `ex` is an executor the device can use to schedule its tasks.
53 fn build(self: Box<Self>, ex: &Executor) -> anyhow::Result<Box<dyn vmm_vhost::Backend>>;
54}