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 vsock;
25 #[cfg(feature = "virtio_wl")]
26 mod wl;
27
28 pub use vsock::{run_vsock_device, Options as VsockOptions, VhostUserVsockDevice};
29 #[cfg(feature = "virtio_wl")]
30 pub use wl::{run_wl_device, Options as WlOptions};
31 } else if #[cfg(windows)] {
32 }
33}
34
35/// A trait for not-yet-built vhost-user devices.
36///
37/// Upon being given an [[Executor]], a builder can be converted into a [[vmm_vhost::Backend]],
38/// which can then process the requests from the front-end.
39///
40/// We don't build the device directly to ensure that the device only starts threads in the jailed
41/// process, not in the main process. [[VhostUserDeviceBuilder::build()]] is called only after
42/// jailing, which ensures that any operations by the device are done in the jailed process.
43///
44/// TODO: Ideally this would return a [[VhostUserDevice]] instead of [[vmm_vhost::Backend]]. Only
45/// the vhost-user vhost-vsock device uses the latter and it can probably be migrated to
46/// [[VhostUserDevice]].
47pub trait VhostUserDeviceBuilder {
48 /// Create the vhost-user device.
49 ///
50 /// `ex` is an executor the device can use to schedule its tasks.
51 fn build(self: Box<Self>, ex: &Executor) -> anyhow::Result<Box<dyn vmm_vhost::Backend>>;
52}