From 1fb0e441a73ce8a7bae407fb254043fabbaa5811 Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Tue, 11 Oct 2022 15:37:19 +0530 Subject: [PATCH 1/4] Make types private to crate These are binary crates and their types aren't required to be exposed out of the crates. Make them private to the crate. Signed-off-by: Viresh Kumar --- gpio/src/backend.rs | 2 +- i2c/src/i2c.rs | 22 +++++++++++----------- i2c/src/main.rs | 2 +- i2c/src/vhu_i2c.rs | 4 ++-- rng/src/main.rs | 6 +++--- rng/src/vhu_rng.rs | 6 +++--- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/gpio/src/backend.rs b/gpio/src/backend.rs index 7734dbd..1ade0d6 100644 --- a/gpio/src/backend.rs +++ b/gpio/src/backend.rs @@ -23,7 +23,7 @@ pub(crate) type Result = std::result::Result; #[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}")] diff --git a/i2c/src/i2c.rs b/i2c/src/i2c.rs index 1a3529f..a2f88e4 100644 --- a/i2c/src/i2c.rs +++ b/i2c/src/i2c.rs @@ -27,7 +27,7 @@ type Result = std::result::Result; #[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 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 { +pub(crate) struct I2cAdapter { device: D, adapter_no: u32, smbus: bool, @@ -507,7 +507,7 @@ impl I2cAdapter { /// I2C map and helpers pub(crate) const MAX_I2C_VDEV: usize = 1 << 7; -pub struct I2cMap { +pub(crate) struct I2cMap { adapters: Vec>, device_map: HashMap, } @@ -569,7 +569,7 @@ impl I2cMap { } #[cfg(test)] -pub mod tests { +pub(crate) mod tests { use super::*; use vmm_sys_util::tempfile::TempFile; diff --git a/i2c/src/main.rs b/i2c/src/main.rs index 0dee159..1bc391e 100644 --- a/i2c/src/main.rs +++ b/i2c/src/main.rs @@ -26,7 +26,7 @@ type Result = std::result::Result; #[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")] diff --git a/i2c/src/vhu_i2c.rs b/i2c/src/vhu_i2c.rs index 41c4152..2d0a472 100644 --- a/i2c/src/vhu_i2c.rs +++ b/i2c/src/vhu_i2c.rs @@ -38,7 +38,7 @@ type VhostUserBackendResult = std::result::Result; #[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 +94,7 @@ struct VirtioI2cInHdr { } unsafe impl ByteValued for VirtioI2cInHdr {} -pub struct VhostUserI2cBackend { +pub(crate) struct VhostUserI2cBackend { i2c_map: Arc>, event_idx: bool, pub exit_event: EventFd, diff --git a/rng/src/main.rs b/rng/src/main.rs index 20851ae..ea4f457 100644 --- a/rng/src/main.rs +++ b/rng/src/main.rs @@ -26,7 +26,7 @@ type Result = std::result::Result; #[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 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)); diff --git a/rng/src/vhu_rng.rs b/rng/src/vhu_rng.rs index f32914f..31606c4 100644 --- a/rng/src/vhu_rng.rs +++ b/rng/src/vhu_rng.rs @@ -34,7 +34,7 @@ type RngDescriptorChain = DescriptorChain 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 +84,7 @@ impl VuRngTimerConfig { } } -pub struct VuRngBackend { +pub(crate) struct VuRngBackend { event_idx: bool, timer: VuRngTimerConfig, rng_source: Arc>, From 57162428cbe5fd9a6d1a953eab265193c561bbef Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Tue, 11 Oct 2022 15:39:56 +0530 Subject: [PATCH 2/4] Remove unused types Started getting warnings for unused definitions as soon as the types are made private to the crate. Remove them now. Signed-off-by: Viresh Kumar --- i2c/src/main.rs | 2 -- rng/src/vhu_rng.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/i2c/src/main.rs b/i2c/src/main.rs index 1bc391e..9c42395 100644 --- a/i2c/src/main.rs +++ b/i2c/src/main.rs @@ -29,8 +29,6 @@ type Result = std::result::Result; 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}")] diff --git a/rng/src/vhu_rng.rs b/rng/src/vhu_rng.rs index 31606c4..7facbb6 100644 --- a/rng/src/vhu_rng.rs +++ b/rng/src/vhu_rng.rs @@ -55,8 +55,6 @@ pub(crate) enum VuRngError { UnexpectedRngSourceError, #[error("Previous Time value is later than current time")] UnexpectedTimerValue, - #[error("Unexpected VirtQueue error")] - UnexpectedVirtQueueError, } impl convert::From for io::Error { From d1ac89588c51babed613e8bb500abb31ed621e0a Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Tue, 11 Oct 2022 15:42:45 +0530 Subject: [PATCH 3/4] rng: Rename daemon to align with rest of the crate Make daemon name follow what's used for i2c and gpio. Signed-off-by: Viresh Kumar --- rng/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rng/src/main.rs b/rng/src/main.rs index ea4f457..aa7270a 100644 --- a/rng/src/main.rs +++ b/rng/src/main.rs @@ -119,7 +119,7 @@ pub(crate) 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()), ) From 2f27f73e580f5ce9bd04de45c17ffd4fd4955040 Mon Sep 17 00:00:00 2001 From: Viresh Kumar Date: Tue, 11 Oct 2022 16:09:20 +0530 Subject: [PATCH 4/4] Use std::io::Result instead of defining new type Directly use std::io::Result instead of redefining it. Signed-off-by: Viresh Kumar --- gpio/src/vhu_gpio.rs | 13 ++++++------- i2c/src/vhu_i2c.rs | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/gpio/src/vhu_gpio.rs b/gpio/src/vhu_gpio.rs index f9f206b..b3984d9 100644 --- a/gpio/src/vhu_gpio.rs +++ b/gpio/src/vhu_gpio.rs @@ -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 = std::result::Result; -type VhostUserBackendResult = std::result::Result; #[derive(Copy, Clone, Debug, PartialEq, ThisError)] /// Errors related to vhost-device-gpio-daemon. @@ -416,10 +418,7 @@ impl VhostUserBackendMut dbg!(self.event_idx = enabled); } - fn update_memory( - &mut self, - mem: GuestMemoryAtomic, - ) -> VhostUserBackendResult<()> { + fn update_memory(&mut self, mem: GuestMemoryAtomic) -> IoResult<()> { self.mem = Some(mem); Ok(()) } @@ -430,7 +429,7 @@ impl VhostUserBackendMut evset: EventSet, vrings: &[VringRwLock], _thread_id: usize, - ) -> VhostUserBackendResult { + ) -> IoResult { if evset != EventSet::IN { return Err(Error::HandleEventNotEpollIn.into()); } diff --git a/i2c/src/vhu_i2c.rs b/i2c/src/vhu_i2c.rs index 2d0a472..db45ace 100644 --- a/i2c/src/vhu_i2c.rs +++ b/i2c/src/vhu_i2c.rs @@ -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,7 +37,6 @@ const QUEUE_SIZE: usize = 1024; const NUM_QUEUES: usize = 1; type Result = std::result::Result; -type VhostUserBackendResult = std::result::Result; #[derive(Copy, Clone, Debug, Eq, PartialEq, ThisError)] /// Errors related to vhost-device-i2c daemon. @@ -300,10 +302,7 @@ impl VhostUserBackendMut dbg!(self.event_idx = enabled); } - fn update_memory( - &mut self, - mem: GuestMemoryAtomic, - ) -> VhostUserBackendResult<()> { + fn update_memory(&mut self, mem: GuestMemoryAtomic) -> IoResult<()> { self.mem = Some(mem); Ok(()) } @@ -314,7 +313,7 @@ impl VhostUserBackendMut evset: EventSet, vrings: &[VringRwLock], _thread_id: usize, - ) -> VhostUserBackendResult { + ) -> IoResult { if evset != EventSet::IN { return Err(Error::HandleEventNotEpollIn.into()); }