Path: blob/main/devices/src/virtio/vhost_user_backend/connection.rs
5394 views
// Copyright 2022 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34pub mod sys;5use std::any::Any;6use std::pin::Pin;78use cros_async::Executor;9use futures::Future;1011use crate::virtio::vhost_user_backend::handler::DeviceRequestHandler;12use crate::virtio::vhost_user_backend::handler::VhostUserDevice;13use crate::virtio::vhost_user_backend::VhostUserDeviceBuilder;14use crate::virtio::vhost_user_backend::VhostUserListener;15use crate::virtio::vhost_user_backend::VhostUserStream;1617pub enum BackendConnection {18Listener(VhostUserListener),19Stream(VhostUserStream),20}2122/// Trait that the platform-specific type `VhostUserConnection` needs to implement. It contains all23/// the methods that are ok to call from non-platform specific code.24pub trait VhostUserConnectionTrait {25/// Take and return resources owned by the parent process in case of a incoming fork.26///27/// This method needs to be called only if you are going to use the connection in a jailed child28/// process. In this case, the connection will belong to the child and the parent will drop it,29/// but the child may lack the rights to drop some resources created at construction time. One30/// such example is the socket file of a regular vhost-user device, that cannot be dropped by31/// the child unless it gets extra permissions.32///33/// This method returns an opaque object that, upon being dropped, will free these resources.34/// That way, the child process does not need extra rights to clear them, and the parent can35/// drop the connection after forking and just need to keep that object alive until the child36/// exits to do housekeeping properly.37///38/// The default implementation returns nothing as that's what most connection would need anyway.39fn take_parent_process_resources(&mut self) -> Option<Box<dyn Any>> {40None41}4243/// Returns a `Future` that processes requests for `handler`. The future exits when the44/// front-end side disconnects or an error occurs.45fn run_req_handler<'e>(46self,47handler: Box<dyn vmm_vhost::Backend>,48ex: &'e Executor,49) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + 'e>>;5051/// Returns a `Future` that will process requests from `backend` when polled. The future exits52/// when the front-end side disconnects or an error occurs.53///54/// This is a legacy way to run devices - prefer `run_device`.55fn run_backend<'e>(56self,57backend: impl VhostUserDevice + 'static,58ex: &'e Executor,59) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + 'e>>60where61Self: Sized,62{63self.run_req_handler(Box::new(DeviceRequestHandler::new(backend)), ex)64}6566/// Start processing requests for a `VhostUserDevice` on `connection`. Returns when the67/// front-end side disconnects or an error occurs.68fn run_device(self, ex: Executor, device: Box<dyn VhostUserDeviceBuilder>) -> anyhow::Result<()>69where70Self: Sized,71{72ex.run_until(self.run_req_handler(device.build(&ex).unwrap(), &ex))?73}74}757677