mirror of
https://github.com/rust-vmm/vhost-device.git
synced 2025-12-30 17:49:08 +00:00
commit
947dbe904d
@ -23,7 +23,7 @@ pub(crate) type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, ThisError)]
|
||||
/// Errors related to low level GPIO helpers
|
||||
pub enum Error {
|
||||
pub(crate) enum Error {
|
||||
#[error("Invalid socket count: {0}")]
|
||||
SocketCountInvalid(usize),
|
||||
#[error("Socket count ({0}) doesn't match device count {1}")]
|
||||
|
||||
@ -10,7 +10,10 @@ use std::mem::size_of;
|
||||
use std::slice::from_raw_parts;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::thread::{spawn, JoinHandle};
|
||||
use std::{convert, io};
|
||||
use std::{
|
||||
convert,
|
||||
io::{self, Result as IoResult},
|
||||
};
|
||||
|
||||
use thiserror::Error as ThisError;
|
||||
use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures};
|
||||
@ -44,7 +47,6 @@ const REQUEST_QUEUE: u16 = 0;
|
||||
const EVENT_QUEUE: u16 = 1;
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
type VhostUserBackendResult<T> = std::result::Result<T, std::io::Error>;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, ThisError)]
|
||||
/// Errors related to vhost-device-gpio-daemon.
|
||||
@ -416,10 +418,7 @@ impl<D: 'static + GpioDevice + Sync + Send> VhostUserBackendMut<VringRwLock, ()>
|
||||
dbg!(self.event_idx = enabled);
|
||||
}
|
||||
|
||||
fn update_memory(
|
||||
&mut self,
|
||||
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
||||
) -> VhostUserBackendResult<()> {
|
||||
fn update_memory(&mut self, mem: GuestMemoryAtomic<GuestMemoryMmap>) -> IoResult<()> {
|
||||
self.mem = Some(mem);
|
||||
Ok(())
|
||||
}
|
||||
@ -430,7 +429,7 @@ impl<D: 'static + GpioDevice + Sync + Send> VhostUserBackendMut<VringRwLock, ()>
|
||||
evset: EventSet,
|
||||
vrings: &[VringRwLock],
|
||||
_thread_id: usize,
|
||||
) -> VhostUserBackendResult<bool> {
|
||||
) -> IoResult<bool> {
|
||||
if evset != EventSet::IN {
|
||||
return Err(Error::HandleEventNotEpollIn.into());
|
||||
}
|
||||
|
||||
@ -27,7 +27,7 @@ type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, ThisError)]
|
||||
/// Errors related to low level i2c helpers
|
||||
pub enum Error {
|
||||
pub(crate) enum Error {
|
||||
#[error("Incorrect message length for {0} operation: {1}")]
|
||||
MessageLengthInvalid(&'static str, usize),
|
||||
#[error("Invalid SMBUS command: {0}")]
|
||||
@ -74,7 +74,7 @@ const I2C_FUNC_SMBUS_ALL: u64 =
|
||||
I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA;
|
||||
|
||||
/// I2C protocol definitions
|
||||
pub const I2C_M_RD: u16 = 0x0001; // read data, from slave to master
|
||||
pub(crate) const I2C_M_RD: u16 = 0x0001; // read data, from slave to master
|
||||
|
||||
/// Copied (partially) from Linux's include/uapi/linux/i2c.h
|
||||
///
|
||||
@ -115,7 +115,7 @@ struct I2cMsg {
|
||||
|
||||
/// This is the structure as used in the I2C_RDWR ioctl call
|
||||
#[repr(C)]
|
||||
pub struct I2cRdwrIoctlData {
|
||||
pub(crate) struct I2cRdwrIoctlData {
|
||||
msgs: *mut I2cMsg,
|
||||
nmsgs: u32,
|
||||
}
|
||||
@ -157,14 +157,14 @@ impl I2cSmbusData {
|
||||
|
||||
/// This is the structure as used in the I2C_SMBUS ioctl call
|
||||
#[repr(C)]
|
||||
pub struct I2cSmbusIoctlData {
|
||||
pub(crate) struct I2cSmbusIoctlData {
|
||||
read_write: u8,
|
||||
command: u8,
|
||||
size: u32,
|
||||
data: *mut I2cSmbusData,
|
||||
}
|
||||
|
||||
pub struct SmbusMsg {
|
||||
pub(crate) struct SmbusMsg {
|
||||
read_write: u8,
|
||||
command: u8,
|
||||
size: u32,
|
||||
@ -284,7 +284,7 @@ impl SmbusMsg {
|
||||
}
|
||||
|
||||
/// I2C definitions
|
||||
pub struct I2cReq {
|
||||
pub(crate) struct I2cReq {
|
||||
pub addr: u16,
|
||||
pub flags: u16,
|
||||
pub len: u16,
|
||||
@ -297,7 +297,7 @@ pub struct I2cReq {
|
||||
/// be used outside of this crate. The purpose of this trait is to provide a
|
||||
/// mock implementation for the I2C driver so that we can test the I2C
|
||||
/// functionality without the need of a physical device.
|
||||
pub trait I2cDevice {
|
||||
pub(crate) trait I2cDevice {
|
||||
// Open the device specified by the adapter number.
|
||||
fn open(device_path: &str, adapter_no: u32) -> Result<Self>
|
||||
where
|
||||
@ -322,7 +322,7 @@ pub trait I2cDevice {
|
||||
/// A physical I2C device. This structure can only be initialized on hosts
|
||||
/// where `/dev/i2c-XX` is available.
|
||||
#[derive(Debug)]
|
||||
pub struct PhysDevice {
|
||||
pub(crate) struct PhysDevice {
|
||||
file: File,
|
||||
adapter_no: u32,
|
||||
}
|
||||
@ -425,7 +425,7 @@ impl I2cDevice for PhysDevice {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct I2cAdapter<D: I2cDevice> {
|
||||
pub(crate) struct I2cAdapter<D: I2cDevice> {
|
||||
device: D,
|
||||
adapter_no: u32,
|
||||
smbus: bool,
|
||||
@ -507,7 +507,7 @@ impl<D: I2cDevice> I2cAdapter<D> {
|
||||
/// I2C map and helpers
|
||||
pub(crate) const MAX_I2C_VDEV: usize = 1 << 7;
|
||||
|
||||
pub struct I2cMap<D: I2cDevice> {
|
||||
pub(crate) struct I2cMap<D: I2cDevice> {
|
||||
adapters: Vec<I2cAdapter<D>>,
|
||||
device_map: HashMap<u16, usize>,
|
||||
}
|
||||
@ -569,7 +569,7 @@ impl<D: I2cDevice> I2cMap<D> {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use vmm_sys_util::tempfile::TempFile;
|
||||
|
||||
|
||||
@ -26,11 +26,9 @@ type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, PartialEq, ThisError)]
|
||||
/// Errors related to low level i2c helpers
|
||||
pub enum Error {
|
||||
pub(crate) enum Error {
|
||||
#[error("Invalid socket count: {0}")]
|
||||
SocketCountInvalid(usize),
|
||||
#[error("Invalid device list")]
|
||||
DeviceListInvalid,
|
||||
#[error("Duplicate adapter detected: {0}")]
|
||||
AdapterDuplicate(u32),
|
||||
#[error("Invalid client address: {0}")]
|
||||
|
||||
@ -8,7 +8,10 @@
|
||||
use log::warn;
|
||||
use std::mem::size_of;
|
||||
use std::sync::Arc;
|
||||
use std::{convert, io};
|
||||
use std::{
|
||||
convert,
|
||||
io::{self, Result as IoResult},
|
||||
};
|
||||
|
||||
use thiserror::Error as ThisError;
|
||||
use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures};
|
||||
@ -34,11 +37,10 @@ const QUEUE_SIZE: usize = 1024;
|
||||
const NUM_QUEUES: usize = 1;
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
type VhostUserBackendResult<T> = std::result::Result<T, std::io::Error>;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, ThisError)]
|
||||
/// Errors related to vhost-device-i2c daemon.
|
||||
pub enum Error {
|
||||
pub(crate) enum Error {
|
||||
#[error("Failed to handle event, didn't match EPOLLIN")]
|
||||
HandleEventNotEpollIn,
|
||||
#[error("Failed to handle unknown event")]
|
||||
@ -94,7 +96,7 @@ struct VirtioI2cInHdr {
|
||||
}
|
||||
unsafe impl ByteValued for VirtioI2cInHdr {}
|
||||
|
||||
pub struct VhostUserI2cBackend<D: I2cDevice> {
|
||||
pub(crate) struct VhostUserI2cBackend<D: I2cDevice> {
|
||||
i2c_map: Arc<I2cMap<D>>,
|
||||
event_idx: bool,
|
||||
pub exit_event: EventFd,
|
||||
@ -300,10 +302,7 @@ impl<D: 'static + I2cDevice + Sync + Send> VhostUserBackendMut<VringRwLock, ()>
|
||||
dbg!(self.event_idx = enabled);
|
||||
}
|
||||
|
||||
fn update_memory(
|
||||
&mut self,
|
||||
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
||||
) -> VhostUserBackendResult<()> {
|
||||
fn update_memory(&mut self, mem: GuestMemoryAtomic<GuestMemoryMmap>) -> IoResult<()> {
|
||||
self.mem = Some(mem);
|
||||
Ok(())
|
||||
}
|
||||
@ -314,7 +313,7 @@ impl<D: 'static + I2cDevice + Sync + Send> VhostUserBackendMut<VringRwLock, ()>
|
||||
evset: EventSet,
|
||||
vrings: &[VringRwLock],
|
||||
_thread_id: usize,
|
||||
) -> VhostUserBackendResult<bool> {
|
||||
) -> IoResult<bool> {
|
||||
if evset != EventSet::IN {
|
||||
return Err(Error::HandleEventNotEpollIn.into());
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@ type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, ThisError)]
|
||||
/// Errors related to vhost-device-rng daemon.
|
||||
pub enum Error {
|
||||
pub(crate) enum Error {
|
||||
#[error("RNG source file doesn't exists or can't be accessed")]
|
||||
AccessRngSourceFile,
|
||||
#[error("Period is too big: {0}")]
|
||||
@ -62,7 +62,7 @@ struct RngArgs {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct VuRngConfig {
|
||||
pub(crate) struct VuRngConfig {
|
||||
pub period_ms: u128,
|
||||
pub max_bytes: usize,
|
||||
pub count: u32,
|
||||
@ -98,7 +98,7 @@ impl TryFrom<RngArgs> for VuRngConfig {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_backend(config: VuRngConfig) -> Result<()> {
|
||||
pub(crate) fn start_backend(config: VuRngConfig) -> Result<()> {
|
||||
let mut handles = Vec::new();
|
||||
let file = File::open(&config.rng_source).map_err(|_| Error::AccessRngSourceFile)?;
|
||||
let random_file = Arc::new(Mutex::new(file));
|
||||
@ -119,7 +119,7 @@ pub fn start_backend(config: VuRngConfig) -> Result<()> {
|
||||
));
|
||||
|
||||
let mut daemon = VhostUserDaemon::new(
|
||||
String::from("vhost-user-RNG-daemon"),
|
||||
String::from("vhost-device-rng-backend"),
|
||||
Arc::clone(&vu_rng_backend),
|
||||
GuestMemoryAtomic::new(GuestMemoryMmap::new()),
|
||||
)
|
||||
|
||||
@ -34,7 +34,7 @@ type RngDescriptorChain = DescriptorChain<GuestMemoryLoadGuard<GuestMemoryMmap<(
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, ThisError)]
|
||||
/// Errors related to vhost-device-rng daemon.
|
||||
pub enum VuRngError {
|
||||
pub(crate) enum VuRngError {
|
||||
#[error("Descriptor not found")]
|
||||
DescriptorNotFound,
|
||||
#[error("Notification send failed")]
|
||||
@ -55,8 +55,6 @@ pub enum VuRngError {
|
||||
UnexpectedRngSourceError,
|
||||
#[error("Previous Time value is later than current time")]
|
||||
UnexpectedTimerValue,
|
||||
#[error("Unexpected VirtQueue error")]
|
||||
UnexpectedVirtQueueError,
|
||||
}
|
||||
|
||||
impl convert::From<VuRngError> for io::Error {
|
||||
@ -66,7 +64,7 @@ impl convert::From<VuRngError> for io::Error {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct VuRngTimerConfig {
|
||||
pub(crate) struct VuRngTimerConfig {
|
||||
period_ms: u128,
|
||||
period_start: Instant,
|
||||
max_bytes: usize,
|
||||
@ -84,7 +82,7 @@ impl VuRngTimerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VuRngBackend<T: Read> {
|
||||
pub(crate) struct VuRngBackend<T: Read> {
|
||||
event_idx: bool,
|
||||
timer: VuRngTimerConfig,
|
||||
rng_source: Arc<Mutex<T>>,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user