Path: blob/main/devices/src/virtio/vhost_user_backend/connection/sys/linux.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.34mod listener;5mod stream;67use std::future::Future;8use std::pin::Pin;910use anyhow::bail;11use anyhow::Result;12use base::warn;13use base::AsRawDescriptor;14use base::RawDescriptor;15use cros_async::Executor;16pub use listener::VhostUserListener;17pub use stream::VhostUserStream;1819use crate::virtio::vhost_user_backend::BackendConnection;20use crate::virtio::vhost_user_backend::VhostUserConnectionTrait;21use crate::virtio::vhost_user_backend::VhostUserDevice;22use crate::virtio::vhost_user_backend::VhostUserDeviceBuilder;2324impl BackendConnection {25pub fn from_opts(26socket: Option<&str>,27socket_path: Option<&str>,28fd: Option<RawDescriptor>,29) -> Result<BackendConnection> {30let socket_path = if let Some(socket_path) = socket_path {31Some(socket_path)32} else if let Some(socket) = socket {33warn!("--socket is deprecated; please use --socket-path instead");34Some(socket)35} else {36None37};3839match (socket_path, fd) {40(Some(socket), None) => {41let listener = VhostUserListener::new(socket)?;42Ok(BackendConnection::Listener(listener))43}44(None, Some(fd)) => {45let stream = VhostUserStream::new_socket_from_fd(fd)?;46Ok(BackendConnection::Stream(stream))47}48(Some(_), Some(_)) => bail!("Cannot specify both a socket path and a file descriptor"),49(None, None) => bail!("Must specify either a socket or a file descriptor"),50}51}5253pub fn run_device(54self,55ex: Executor,56device: Box<dyn VhostUserDeviceBuilder>,57) -> anyhow::Result<()> {58match self {59BackendConnection::Listener(listener) => listener.run_device(ex, device),60BackendConnection::Stream(stream) => stream.run_device(ex, device),61}62}6364pub fn run_backend<'e>(65self,66backend: impl VhostUserDevice + 'static,67ex: &'e Executor,68) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + 'e>> {69match self {70BackendConnection::Listener(listener) => listener.run_backend(backend, ex),71BackendConnection::Stream(stream) => stream.run_backend(backend, ex),72}73}74}7576impl AsRawDescriptor for BackendConnection {77fn as_raw_descriptor(&self) -> RawDescriptor {78match self {79BackendConnection::Listener(listener) => listener.as_raw_descriptor(),80BackendConnection::Stream(stream) => stream.as_raw_descriptor(),81}82}83}848586