|
| 1 | +// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause |
| 2 | +mod stream; |
| 3 | +mod vhu_video; |
| 4 | +mod vhu_video_thread; |
| 5 | +mod video; |
| 6 | +mod video_backends; |
| 7 | + |
| 8 | +use std::path::PathBuf; |
| 9 | +use std::sync::{Arc, RwLock}; |
| 10 | + |
| 11 | +use clap::Parser; |
| 12 | +use log::{info, warn}; |
| 13 | +use thiserror::Error as ThisError; |
| 14 | +use vhost::{vhost_user, vhost_user::Listener}; |
| 15 | +use vhost_user_backend::VhostUserDaemon; |
| 16 | +use vhu_video::{BackendType, VuVideoBackend}; |
| 17 | +use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap}; |
| 18 | + |
| 19 | +pub(crate) type Result<T> = std::result::Result<T, Error>; |
| 20 | + |
| 21 | +#[derive(Debug, ThisError)] |
| 22 | +pub(crate) enum Error { |
| 23 | + #[error("Could not create backend: {0}")] |
| 24 | + CouldNotCreateBackend(vhu_video::VuVideoError), |
| 25 | + #[error("Could not create daemon: {0}")] |
| 26 | + CouldNotCreateDaemon(vhost_user_backend::Error), |
| 27 | + #[error("Failed creating listener: {0}")] |
| 28 | + FailedCreatingListener(vhost_user::Error), |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Clone, Parser, Debug)] |
| 32 | +#[clap(author, version, about, long_about = None)] |
| 33 | +struct VideoArgs { |
| 34 | + /// Unix socket to which a hypervisor connects to and sets up the control path with the device. |
| 35 | + #[clap(short, long)] |
| 36 | + socket_path: PathBuf, |
| 37 | + |
| 38 | + /// Path to the video device file. Defaults to /dev/video0. |
| 39 | + #[clap(short = 'd', long, default_value = "/dev/video0")] |
| 40 | + v4l2_device: PathBuf, |
| 41 | + |
| 42 | + /// Video backend to be used. |
| 43 | + #[clap(short, long)] |
| 44 | + #[clap(value_enum)] |
| 45 | + backend: BackendType, |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug, Eq, PartialEq)] |
| 49 | +pub(crate) struct VuVideoConfig { |
| 50 | + pub socket_path: PathBuf, |
| 51 | + pub v4l2_device: PathBuf, |
| 52 | + pub backend: BackendType, |
| 53 | +} |
| 54 | + |
| 55 | +impl From<VideoArgs> for VuVideoConfig { |
| 56 | + fn from(args: VideoArgs) -> Self { |
| 57 | + // Divide available bandwidth by the number of threads in order |
| 58 | + // to avoid overwhelming the HW. |
| 59 | + Self { |
| 60 | + socket_path: args.socket_path.to_owned(), |
| 61 | + v4l2_device: args.v4l2_device.to_owned(), |
| 62 | + backend: args.backend, |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +pub(crate) fn start_backend(config: VuVideoConfig) -> Result<()> { |
| 68 | + loop { |
| 69 | + info!("Starting backend"); |
| 70 | + let vu_video_backend = Arc::new(RwLock::new( |
| 71 | + VuVideoBackend::new(config.v4l2_device.as_path(), config.backend.to_owned()) |
| 72 | + .map_err(Error::CouldNotCreateBackend)?, |
| 73 | + )); |
| 74 | + |
| 75 | + let mut daemon = VhostUserDaemon::new( |
| 76 | + String::from("vhost-device-video"), |
| 77 | + vu_video_backend.clone(), |
| 78 | + GuestMemoryAtomic::new(GuestMemoryMmap::new()), |
| 79 | + ) |
| 80 | + .map_err(Error::CouldNotCreateDaemon)?; |
| 81 | + |
| 82 | + let mut vring_workers = daemon.get_epoll_handlers(); |
| 83 | + for thread in vu_video_backend.read().unwrap().threads.iter() { |
| 84 | + thread |
| 85 | + .lock() |
| 86 | + .unwrap() |
| 87 | + .set_vring_workers(vring_workers.remove(0)); |
| 88 | + } |
| 89 | + |
| 90 | + daemon |
| 91 | + .start(Listener::new(&config.socket_path, true).map_err(Error::FailedCreatingListener)?) |
| 92 | + .expect("Stargin daemon"); |
| 93 | + |
| 94 | + match daemon.wait() { |
| 95 | + Ok(()) => { |
| 96 | + info!("Stopping cleanly"); |
| 97 | + } |
| 98 | + Err(vhost_user_backend::Error::HandleRequest(vhost_user::Error::PartialMessage)) => { |
| 99 | + info!( |
| 100 | + "vhost-user connection closed with partial message. |
| 101 | + If the VM is shutting down, this is expected behavior; |
| 102 | + otherwise, it might be a bug." |
| 103 | + ); |
| 104 | + } |
| 105 | + Err(e) => { |
| 106 | + warn!("Error running daemon: {:?} -> {}", e, e.to_string()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + vu_video_backend |
| 111 | + .read() |
| 112 | + .unwrap() |
| 113 | + .exit_event |
| 114 | + .write(1) |
| 115 | + .expect("Shutting down worker thread"); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +fn main() -> Result<()> { |
| 120 | + env_logger::init(); |
| 121 | + |
| 122 | + start_backend(VuVideoConfig::try_from(VideoArgs::parse()).unwrap()) |
| 123 | +} |
0 commit comments