net_util: Derive thiserror::Error

Signed-off-by: Bo Chen <chen.bo@intel.com>
This commit is contained in:
Bo Chen 2022-08-10 14:58:36 -07:00 committed by Rob Bradford
parent 6a1e637a46
commit f39b08f21f
6 changed files with 42 additions and 35 deletions

1
Cargo.lock generated
View File

@ -610,6 +610,7 @@ dependencies = [
"rate_limiter",
"serde",
"serde_json",
"thiserror",
"versionize",
"versionize_derive",
"virtio-bindings",

View File

@ -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"

View File

@ -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),
}

View File

@ -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),
}

View File

@ -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<AtomicU64>,
}
#[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),
}

View File

@ -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),
}