From f39b08f21f0a395200a13234e31a2ec16b31c7e3 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Wed, 10 Aug 2022 14:58:36 -0700 Subject: [PATCH] net_util: Derive thiserror::Error Signed-off-by: Bo Chen --- Cargo.lock | 1 + net_util/Cargo.toml | 1 + net_util/src/lib.rs | 5 +++-- net_util/src/open_tap.rs | 25 +++++++++++++------------ net_util/src/queue_pair.rs | 27 ++++++++++++++------------- net_util/src/tap.rs | 18 ++++++++++-------- 6 files changed, 42 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 23e64ecc5..6f7a7f84d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -610,6 +610,7 @@ dependencies = [ "rate_limiter", "serde", "serde_json", + "thiserror", "versionize", "versionize_derive", "virtio-bindings", diff --git a/net_util/Cargo.toml b/net_util/Cargo.toml index 6cadfa605..49b62efa0 100644 --- a/net_util/Cargo.toml +++ b/net_util/Cargo.toml @@ -12,6 +12,7 @@ log = "0.4.17" net_gen = { path = "../net_gen" } rate_limiter = { path = "../rate_limiter" } serde = "1.0.143" +thiserror = "1.0.32" versionize = "0.1.6" versionize_derive = "0.1.4" virtio-bindings = "0.1.0" diff --git a/net_util/src/lib.rs b/net_util/src/lib.rs index c62a0aaab..47136cc4d 100644 --- a/net_util/src/lib.rs +++ b/net_util/src/lib.rs @@ -18,6 +18,7 @@ use std::io::Error as IoError; use std::os::raw::c_uint; use std::os::unix::io::{FromRawFd, RawFd}; use std::{io, mem, net}; +use thiserror::Error; use versionize::{VersionMap, Versionize, VersionizeResult}; use versionize_derive::Versionize; use virtio_bindings::bindings::virtio_net::{ @@ -35,9 +36,9 @@ pub use open_tap::{open_tap, Error as OpenTapError}; pub use queue_pair::{NetCounters, NetQueuePair, NetQueuePairError, RxVirtio, TxVirtio}; pub use tap::{Error as TapError, Tap}; -#[derive(Debug)] +#[derive(Error, Debug)] pub enum Error { - /// Failed to create a socket. + #[error("Failed to create a socket: {0}")] CreateSocket(IoError), } diff --git a/net_util/src/open_tap.rs b/net_util/src/open_tap.rs index 73cde8c43..431ac8c17 100644 --- a/net_util/src/open_tap.rs +++ b/net_util/src/open_tap.rs @@ -6,30 +6,31 @@ use super::{vnet_hdr_len, MacAddr, Tap, TapError}; use std::net::Ipv4Addr; use std::path::Path; use std::{fs, io}; +use thiserror::Error; -#[derive(Debug)] +#[derive(Error, Debug)] pub enum Error { - /// Failed to convert an hexadecimal string into an integer. + #[error("Failed to convert an hexadecimal string into an integer: {0}")] ConvertHexStringToInt(std::num::ParseIntError), - /// Error related to the multiqueue support (no support TAP side). + #[error("Error related to the multiqueue support (no support TAP side).")] MultiQueueNoTapSupport, - /// Error related to the multiqueue support (no support device side). + #[error("Error related to the multiqueue support (no support device side).")] MultiQueueNoDeviceSupport, - /// Failed to read the TAP flags from sysfs. + #[error("Failed to read the TAP flags from sysfs: {0}")] ReadSysfsTunFlags(io::Error), - /// Open tap device failed. + #[error("Open tap device failed: {0}")] TapOpen(TapError), - /// Setting tap IP failed. + #[error("Setting tap IP failed: {0}")] TapSetIp(TapError), - /// Setting tap netmask failed. + #[error("Setting tap netmask failed: {0}")] TapSetNetmask(TapError), - /// Setting MAC address failed + #[error("Setting MAC address failed: {0}")] TapSetMac(TapError), - /// Getting MAC address failed + #[error("Getting MAC address failed: {0}")] TapGetMac(TapError), - /// Setting vnet header size failed. + #[error("Setting vnet header size failed: {0}")] TapSetVnetHdrSize(TapError), - /// Enabling tap interface failed. + #[error("Enabling tap interface failed: {0}")] TapEnable(TapError), } diff --git a/net_util/src/queue_pair.rs b/net_util/src/queue_pair.rs index 37a3b2d21..615a33ef3 100644 --- a/net_util/src/queue_pair.rs +++ b/net_util/src/queue_pair.rs @@ -10,6 +10,7 @@ use std::num::Wrapping; use std::os::unix::io::{AsRawFd, RawFd}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::Arc; +use thiserror::Error; use virtio_queue::{Queue, QueueOwnedT, QueueT}; use vm_memory::{Bytes, GuestMemory}; use vm_virtio::{AccessPlatform, Translatable}; @@ -285,31 +286,31 @@ pub struct NetCounters { pub rx_frames: Arc, } -#[derive(Debug)] +#[derive(Error, Debug)] pub enum NetQueuePairError { - /// No memory configured + #[error("No memory configured.")] NoMemoryConfigured, - /// Error registering listener + #[error("Error registering listener: {0}")] RegisterListener(io::Error), - /// Error unregistering listener + #[error("Error unregistering listener: {0}")] UnregisterListener(io::Error), - /// Error writing to the TAP device + #[error("Error writing to the TAP device: {0}")] WriteTap(io::Error), - /// Error reading from the TAP device + #[error("Error reading from the TAP device: {0}")] ReadTap(io::Error), - /// Error related to guest memory + #[error("Error related to guest memory: {0}")] GuestMemory(vm_memory::GuestMemoryError), - /// Returned an error while iterating through the queue + #[error("Returned an error while iterating through the queue: {0}")] QueueIteratorFailed(virtio_queue::Error), - /// Descriptor chain is too short + #[error("Descriptor chain is too short.")] DescriptorChainTooShort, - /// Descriptor chain does not contain valid descriptors + #[error("Descriptor chain does not contain valid descriptors.")] DescriptorChainInvalid, - /// Failed to determine if queue needed notification + #[error("Failed to determine if queue needed notification: {0}")] QueueNeedsNotification(virtio_queue::Error), - /// Failed to enable notification on the queue + #[error("Failed to enable notification on the queue: {0}")] QueueEnableNotification(virtio_queue::Error), - /// Failed to add used index to the queue + #[error("Failed to add used index to the queue: {0}")] QueueAddUsed(virtio_queue::Error), } diff --git a/net_util/src/tap.rs b/net_util/src/tap.rs index 195569c7b..b212fb963 100644 --- a/net_util/src/tap.rs +++ b/net_util/src/tap.rs @@ -15,24 +15,26 @@ use std::io::{Error as IoError, Read, Result as IoResult, Write}; use std::net; use std::os::raw::*; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; +use thiserror::Error; use vmm_sys_util::ioctl::{ioctl_with_mut_ref, ioctl_with_ref, ioctl_with_val}; -#[derive(Debug)] +#[derive(Error, Debug)] pub enum Error { - /// Couldn't open /dev/net/tun. + #[error("Couldn't open /dev/net/tun: {0}")] OpenTun(IoError), - /// Unable to configure tap interface. + #[error("Unable to configure tap interface: {0}")] ConfigureTap(IoError), - /// Unable to retrieve features. + #[error("Unable to retrieve features: {0}")] GetFeatures(IoError), - /// Missing multiqueue support in the kernel. + #[error("Missing multiqueue support in the kernel.")] MultiQueueKernelSupport, - /// ioctl failed. + #[error("ioctl failed: {0}")] IoctlError(IoError), - /// Failed to create a socket. + #[error("Failed to create a socket: {0}")] NetUtil(NetUtilError), + #[error("Invalid interface name.")] InvalidIfname, - /// Error parsing MAC data + #[error("Error parsing MAC data: {0}")] MacParsing(IoError), }