From fd5cfb75f397ff75f64c777c0500b928890811d9 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Mon, 19 May 2025 09:50:35 +0200 Subject: [PATCH] misc: net_util: streamline #[source] and Error This streamlines the code base to follow best practices for error handling in Rust: Each error struct implements std::error::Error (most due via thiserror::Error derive macro) and sets its source accordingly. This allows future work that nicely prints the error chains, for example. So far, the convention is that each error prints its sub error as part of its Display::fmt() impl. Signed-off-by: Philipp Schuster On-behalf-of: SAP philipp.schuster@sap.com --- net_util/src/lib.rs | 2 +- net_util/src/open_tap.rs | 18 +++++++++--------- net_util/src/queue_pair.rs | 18 +++++++++--------- net_util/src/tap.rs | 12 ++++++------ 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/net_util/src/lib.rs b/net_util/src/lib.rs index 554ea7134..90a6e1080 100644 --- a/net_util/src/lib.rs +++ b/net_util/src/lib.rs @@ -41,7 +41,7 @@ pub use tap::{Error as TapError, Tap}; #[derive(Error, Debug)] pub enum Error { #[error("Failed to create a socket: {0}")] - CreateSocket(IoError), + CreateSocket(#[source] IoError), } pub type Result = std::result::Result; diff --git a/net_util/src/open_tap.rs b/net_util/src/open_tap.rs index c652f0c40..28be56c6f 100644 --- a/net_util/src/open_tap.rs +++ b/net_util/src/open_tap.rs @@ -13,27 +13,27 @@ use super::{vnet_hdr_len, MacAddr, Tap, TapError}; #[derive(Error, Debug)] pub enum Error { #[error("Failed to convert an hexadecimal string into an integer: {0}")] - ConvertHexStringToInt(std::num::ParseIntError), + ConvertHexStringToInt(#[source] std::num::ParseIntError), #[error("Error related to the multiqueue support (no support TAP side)")] MultiQueueNoTapSupport, #[error("Error related to the multiqueue support (no support device side)")] MultiQueueNoDeviceSupport, #[error("Failed to read the TAP flags from sysfs: {0}")] - ReadSysfsTunFlags(io::Error), + ReadSysfsTunFlags(#[source] io::Error), #[error("Open tap device failed: {0}")] - TapOpen(TapError), + TapOpen(#[source] TapError), #[error("Setting tap IP and/or netmask failed: {0}")] - TapSetIpNetmask(TapError), + TapSetIpNetmask(#[source] TapError), #[error("Setting MAC address failed: {0}")] - TapSetMac(TapError), + TapSetMac(#[source] TapError), #[error("Getting MAC address failed: {0}")] - TapGetMac(TapError), + TapGetMac(#[source] TapError), #[error("Setting vnet header size failed: {0}")] - TapSetVnetHdrSize(TapError), + TapSetVnetHdrSize(#[source] TapError), #[error("Setting MTU failed: {0}")] - TapSetMtu(TapError), + TapSetMtu(#[source] TapError), #[error("Enabling tap interface failed: {0}")] - TapEnable(TapError), + TapEnable(#[source] TapError), } type Result = std::result::Result; diff --git a/net_util/src/queue_pair.rs b/net_util/src/queue_pair.rs index e9df54e16..5a9bd41fb 100644 --- a/net_util/src/queue_pair.rs +++ b/net_util/src/queue_pair.rs @@ -354,27 +354,27 @@ pub enum NetQueuePairError { #[error("No memory configured")] NoMemoryConfigured, #[error("Error registering listener: {0}")] - RegisterListener(io::Error), + RegisterListener(#[source] io::Error), #[error("Error unregistering listener: {0}")] - UnregisterListener(io::Error), + UnregisterListener(#[source] io::Error), #[error("Error writing to the TAP device: {0}")] - WriteTap(io::Error), + WriteTap(#[source] io::Error), #[error("Error reading from the TAP device: {0}")] - ReadTap(io::Error), + ReadTap(#[source] io::Error), #[error("Error related to guest memory: {0}")] - GuestMemory(vm_memory::GuestMemoryError), + GuestMemory(#[source] vm_memory::GuestMemoryError), #[error("Returned an error while iterating through the queue: {0}")] - QueueIteratorFailed(virtio_queue::Error), + QueueIteratorFailed(#[source] virtio_queue::Error), #[error("Descriptor chain is too short")] DescriptorChainTooShort, #[error("Descriptor chain does not contain valid descriptors")] DescriptorChainInvalid, #[error("Failed to determine if queue needed notification: {0}")] - QueueNeedsNotification(virtio_queue::Error), + QueueNeedsNotification(#[source] virtio_queue::Error), #[error("Failed to enable notification on the queue: {0}")] - QueueEnableNotification(virtio_queue::Error), + QueueEnableNotification(#[source] virtio_queue::Error), #[error("Failed to add used index to the queue: {0}")] - QueueAddUsed(virtio_queue::Error), + QueueAddUsed(#[source] virtio_queue::Error), #[error("Descriptor with invalid virtio-net header")] DescriptorInvalidHeader, #[error("Invalid virtio-net header")] diff --git a/net_util/src/tap.rs b/net_util/src/tap.rs index 665e8a72f..877acadde 100644 --- a/net_util/src/tap.rs +++ b/net_util/src/tap.rs @@ -23,21 +23,21 @@ use crate::mac::MAC_ADDR_LEN; #[derive(Error, Debug)] pub enum Error { #[error("Couldn't open /dev/net/tun: {0}")] - OpenTun(IoError), + OpenTun(#[source] IoError), #[error("Unable to configure tap interface: {0}")] - ConfigureTap(IoError), + ConfigureTap(#[source] IoError), #[error("Unable to retrieve features: {0}")] - GetFeatures(IoError), + GetFeatures(#[source] IoError), #[error("Missing multiqueue support in the kernel")] MultiQueueKernelSupport, #[error("ioctl ({0}) failed: {1}")] - IoctlError(c_ulong, IoError), + IoctlError(c_ulong, #[source] IoError), #[error("Failed to create a socket: {0}")] - NetUtil(NetUtilError), + NetUtil(#[source] NetUtilError), #[error("Invalid interface name")] InvalidIfname, #[error("Error parsing MAC data: {0}")] - MacParsing(IoError), + MacParsing(#[source] IoError), #[error("Invalid netmask")] InvalidNetmask, }