Skip to content

Commit

Permalink
Move start_backend from main.rs into lib.rs
Browse files Browse the repository at this point in the history
This was an oversight, for the user to be able to use the crate as a
library, the start_backend should be part of the public API.

Signed-off-by: Matej Hrica <[email protected]>
  • Loading branch information
mtjhrc committed Dec 19, 2024
1 parent 66bc9e5 commit e7cdf1a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 59 deletions.
52 changes: 49 additions & 3 deletions staging/vhost-device-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,26 @@ pub mod protocol;
#[cfg(target_env = "gnu")]
pub mod virtio_gpu;

use std::fmt::{Display, Formatter};
use std::{
fmt::{Display, Formatter},
path::Path,
};

use bitflags::bitflags;
use clap::ValueEnum;
use log::info;
#[cfg(feature = "gfxstream")]
use rutabaga_gfx::{RUTABAGA_CAPSET_GFXSTREAM_GLES, RUTABAGA_CAPSET_GFXSTREAM_VULKAN};
use rutabaga_gfx::{RUTABAGA_CAPSET_VENUS, RUTABAGA_CAPSET_VIRGL, RUTABAGA_CAPSET_VIRGL2};
use thiserror::Error as ThisError;
use vhost_user_backend::VhostUserDaemon;
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};

use crate::device::VhostUserGpuBackend;

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub enum GpuMode {
#[value(name="virglrenderer", alias("virgl-renderer"))]
#[value(name = "virglrenderer", alias("virgl-renderer"))]
VirglRenderer,
#[cfg(feature = "gfxstream")]
Gfxstream,
Expand Down Expand Up @@ -230,9 +238,35 @@ impl GpuConfig {
}
}

#[derive(Debug, ThisError)]
pub enum StartError {
#[error("Could not create backend: {0}")]
CouldNotCreateBackend(device::Error),
#[error("Could not create daemon: {0}")]
CouldNotCreateDaemon(vhost_user_backend::Error),
#[error("Fatal error: {0}")]
ServeFailed(vhost_user_backend::Error),
}

pub fn start_backend(socket_path: &Path, config: GpuConfig) -> Result<(), StartError> {
info!("Starting backend");
let backend = VhostUserGpuBackend::new(config).map_err(StartError::CouldNotCreateBackend)?;

let mut daemon = VhostUserDaemon::new(
"vhost-device-gpu-backend".to_string(),
backend.clone(),
GuestMemoryAtomic::new(GuestMemoryMmap::new()),
)
.map_err(StartError::CouldNotCreateDaemon)?;

backend.set_epoll_handler(&daemon.get_epoll_handlers());

daemon.serve(socket_path).map_err(StartError::ServeFailed)?;
Ok(())
}

#[cfg(test)]
mod tests {
#[cfg(feature = "gfxstream")]
use assert_matches::assert_matches;

use super::*;
Expand Down Expand Up @@ -316,4 +350,16 @@ mod tests {
let output = format!("{capset}");
assert_eq!(output, "virgl, virgl2, venus")
}

#[test]
fn test_fail_listener() {
// This will fail the listeners and thread will panic.
let socket_name = Path::new("/proc/-1/nonexistent");
let config = GpuConfig::new(GpuMode::VirglRenderer, None, GpuFlags::default()).unwrap();

assert_matches!(
start_backend(socket_name, config).unwrap_err(),
StartError::ServeFailed(_)
);
}
}
60 changes: 4 additions & 56 deletions staging/vhost-device-gpu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@
// rutabaga library is available for musl.
#[cfg(target_env = "gnu")]
pub mod gnu_main {
use std::{
path::{Path, PathBuf},
process::exit,
};
use std::{path::PathBuf, process::exit};

use clap::{ArgAction, Parser, ValueEnum};
use log::{error, info};
use thiserror::Error as ThisError;
use vhost_device_gpu::{
device, device::VhostUserGpuBackend, GpuCapset, GpuConfig, GpuFlags, GpuMode,
};
use vhost_user_backend::VhostUserDaemon;
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
use log::error;
use vhost_device_gpu::{start_backend, GpuCapset, GpuConfig, GpuFlags, GpuMode};

#[derive(ValueEnum, Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u64)]
Expand Down Expand Up @@ -116,18 +108,6 @@ pub mod gnu_main {
use_surfaceless: bool,
}

type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, ThisError)]
pub enum Error {
#[error("Could not create backend: {0}")]
CouldNotCreateBackend(device::Error),
#[error("Could not create daemon: {0}")]
CouldNotCreateDaemon(vhost_user_backend::Error),
#[error("Fatal error: {0}")]
ServeFailed(vhost_user_backend::Error),
}

impl From<GpuFlagsArgs> for GpuFlags {
fn from(args: GpuFlagsArgs) -> Self {
GpuFlags {
Expand All @@ -139,23 +119,6 @@ pub mod gnu_main {
}
}

pub fn start_backend(socket_path: &Path, config: GpuConfig) -> Result<()> {
info!("Starting backend");
let backend = VhostUserGpuBackend::new(config).map_err(Error::CouldNotCreateBackend)?;

let mut daemon = VhostUserDaemon::new(
"vhost-device-gpu-backend".to_string(),
backend.clone(),
GuestMemoryAtomic::new(GuestMemoryMmap::new()),
)
.map_err(Error::CouldNotCreateDaemon)?;

backend.set_epoll_handler(&daemon.get_epoll_handlers());

daemon.serve(socket_path).map_err(Error::ServeFailed)?;
Ok(())
}

pub fn main() {
env_logger::init();

Expand Down Expand Up @@ -190,26 +153,11 @@ fn main() {}
#[cfg(target_env = "gnu")]
#[cfg(test)]
mod tests {
use std::path::Path;

use assert_matches::assert_matches;
use clap::ValueEnum;
use vhost_device_gpu::{GpuCapset, GpuConfig, GpuFlags, GpuMode};
use vhost_device_gpu::GpuCapset;

use super::gnu_main::*;

#[test]
fn test_fail_listener() {
// This will fail the listeners and thread will panic.
let socket_name = Path::new("/proc/-1/nonexistent");
let config = GpuConfig::new(GpuMode::VirglRenderer, None, GpuFlags::default()).unwrap();

assert_matches!(
start_backend(socket_name, config).unwrap_err(),
Error::ServeFailed(_)
);
}

#[test]
fn test_capset_enum_in_sync_with_capset_bitset() {
// Convert each GpuCapset into CapsetName
Expand Down

0 comments on commit e7cdf1a

Please sign in to comment.