misc: arch/riscv64: 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 <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
Philipp Schuster
2025-05-19 09:49:51 +02:00
committed by Rob Bradford
parent d1a406143d
commit a212343908
17 changed files with 179 additions and 174 deletions
+8 -8
View File
@@ -62,25 +62,25 @@ pub const MINIMUM_BLOCK_QUEUE_SIZE: u16 = 2;
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to parse the request: {0}")]
RequestParsing(block::Error),
RequestParsing(#[source] block::Error),
#[error("Failed to execute the request: {0}")]
RequestExecuting(block::ExecuteError),
RequestExecuting(#[source] block::ExecuteError),
#[error("Failed to complete the request: {0}")]
RequestCompleting(block::Error),
RequestCompleting(#[source] block::Error),
#[error("Missing the expected entry in the list of requests")]
MissingEntryRequestList,
#[error("The asynchronous request returned with failure")]
AsyncRequestFailure,
#[error("Failed synchronizing the file: {0}")]
Fsync(AsyncIoError),
Fsync(#[source] AsyncIoError),
#[error("Failed adding used index: {0}")]
QueueAddUsed(virtio_queue::Error),
QueueAddUsed(#[source] virtio_queue::Error),
#[error("Failed creating an iterator over the queue: {0}")]
QueueIterator(virtio_queue::Error),
QueueIterator(#[source] virtio_queue::Error),
#[error("Failed to update request status: {0}")]
RequestStatus(GuestMemoryError),
RequestStatus(#[source] GuestMemoryError),
#[error("Failed to enable notification: {0}")]
QueueEnableNotification(virtio_queue::Error),
QueueEnableNotification(#[source] virtio_queue::Error),
#[error("Failed to get {lock_type:?} lock for disk image {path}: {error}")]
LockDiskImage {
/// The underlying error.
+4 -4
View File
@@ -293,7 +293,7 @@ unsafe impl ByteValued for VirtioIommuFault {}
#[derive(Error, Debug)]
enum Error {
#[error("Guest gave us bad memory addresses: {0}")]
GuestMemory(GuestMemoryError),
GuestMemory(#[source] GuestMemoryError),
#[error("Guest gave us a write only descriptor that protocol says to read from")]
UnexpectedWriteOnlyDescriptor,
#[error("Guest gave us a read only descriptor that protocol says to write to")]
@@ -323,11 +323,11 @@ enum Error {
#[error("Guest sent us invalid PROBE request")]
InvalidProbeRequest,
#[error("Failed to performing external mapping: {0}")]
ExternalMapping(io::Error),
ExternalMapping(#[source] io::Error),
#[error("Failed to performing external unmapping: {0}")]
ExternalUnmapping(io::Error),
ExternalUnmapping(#[source] io::Error),
#[error("Failed adding used index: {0}")]
QueueAddUsed(virtio_queue::Error),
QueueAddUsed(#[source] virtio_queue::Error),
}
struct Request {}
+14 -14
View File
@@ -88,19 +88,19 @@ pub enum ActivateError {
#[error("Failed to activate virtio device")]
BadActivate,
#[error("Failed to clone exit event fd: {0}")]
CloneExitEventFd(std::io::Error),
CloneExitEventFd(#[source] std::io::Error),
#[error("Failed to spawn thread: {0}")]
ThreadSpawn(std::io::Error),
ThreadSpawn(#[source] std::io::Error),
#[error("Failed to setup vhost-user-fs daemon: {0}")]
VhostUserFsSetup(vhost_user::Error),
VhostUserFsSetup(#[source] vhost_user::Error),
#[error("Failed to setup vhost-user daemon: {0}")]
VhostUserSetup(vhost_user::Error),
VhostUserSetup(#[source] vhost_user::Error),
#[error("Failed to create seccomp filter: {0}")]
CreateSeccompFilter(seccompiler::Error),
CreateSeccompFilter(#[source] seccompiler::Error),
#[error("Failed to create rate limiter: {0}")]
CreateRateLimiter(std::io::Error),
CreateRateLimiter(#[source] std::io::Error),
#[error("Failed to activate the vDPA device: {0}")]
ActivateVdpa(vdpa::Error),
ActivateVdpa(#[source] vdpa::Error),
}
pub type ActivateResult = std::result::Result<(), ActivateError>;
@@ -110,21 +110,21 @@ pub type DeviceEventT = u16;
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to single used queue: {0}")]
FailedSignalingUsedQueue(io::Error),
FailedSignalingUsedQueue(#[source] io::Error),
#[error("I/O Error: {0}")]
IoError(io::Error),
IoError(#[source] io::Error),
#[error("Failed to update memory vhost-user: {0}")]
VhostUserUpdateMemory(vhost_user::Error),
VhostUserUpdateMemory(#[source] vhost_user::Error),
#[error("Failed to add memory region vhost-user: {0}")]
VhostUserAddMemoryRegion(vhost_user::Error),
VhostUserAddMemoryRegion(#[source] vhost_user::Error),
#[error("Failed to set shared memory region")]
SetShmRegionsNotSupported,
#[error("Failed to process net queue: {0}")]
NetQueuePair(::net_util::NetQueuePairError),
NetQueuePair(#[source] ::net_util::NetQueuePairError),
#[error("Failed to : {0}")]
QueueAddUsed(virtio_queue::Error),
QueueAddUsed(#[source] virtio_queue::Error),
#[error("Failed to : {0}")]
QueueIterator(virtio_queue::Error),
QueueIterator(#[source] virtio_queue::Error),
}
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
+11 -11
View File
@@ -102,7 +102,7 @@ const VIRTIO_MEM_F_ACPI_PXM: u8 = 0;
#[derive(Error, Debug)]
pub enum Error {
#[error("Guest gave us bad memory addresses: {0}")]
GuestMemory(GuestMemoryError),
GuestMemory(#[source] GuestMemoryError),
#[error("Guest gave us a write only descriptor that protocol says to read from")]
UnexpectedWriteOnlyDescriptor,
#[error("Guest gave us a read only descriptor that protocol says to write to")]
@@ -114,23 +114,23 @@ pub enum Error {
#[error("Guest sent us invalid request")]
InvalidRequest,
#[error("Failed to EventFd write: {0}")]
EventFdWriteFail(std::io::Error),
EventFdWriteFail(#[source] std::io::Error),
#[error("Failed to EventFd try_clone: {0}")]
EventFdTryCloneFail(std::io::Error),
EventFdTryCloneFail(#[source] std::io::Error),
#[error("Failed to MpscRecv: {0}")]
MpscRecvFail(mpsc::RecvError),
MpscRecvFail(#[source] mpsc::RecvError),
#[error("Resize invalid argument: {0}")]
ResizeError(anyhow::Error),
ResizeError(#[source] anyhow::Error),
#[error("Fail to resize trigger: {0}")]
ResizeTriggerFail(DeviceError),
ResizeTriggerFail(#[source] DeviceError),
#[error("Invalid configuration: {0}")]
ValidateError(anyhow::Error),
ValidateError(#[source] anyhow::Error),
#[error("Failed discarding memory range: {0}")]
DiscardMemoryRange(std::io::Error),
DiscardMemoryRange(#[source] std::io::Error),
#[error("Failed DMA mapping: {0}")]
DmaMap(std::io::Error),
DmaMap(#[source] std::io::Error),
#[error("Failed DMA unmapping: {0}")]
DmaUnmap(std::io::Error),
DmaUnmap(#[source] std::io::Error),
#[error("Invalid DMA mapping handler")]
InvalidDmaMappingHandler,
#[error("Not activated by the guest")]
@@ -138,7 +138,7 @@ pub enum Error {
#[error("Unknown request type: {0}")]
UnknownRequestType(u16),
#[error("Failed adding used index: {0}")]
QueueAddUsed(virtio_queue::Error),
QueueAddUsed(#[source] virtio_queue::Error),
}
#[repr(C)]
+3 -3
View File
@@ -154,11 +154,11 @@ pub const TX_RATE_LIMITER_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 6;
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to open taps: {0}")]
OpenTap(OpenTapError),
OpenTap(#[source] OpenTapError),
#[error("Using existing tap: {0}")]
TapError(TapError),
TapError(#[source] TapError),
#[error("Error calling dup() on tap fd: {0}")]
DuplicateTapFd(std::io::Error),
DuplicateTapFd(#[source] std::io::Error),
}
pub type Result<T> = result::Result<T, Error>;
+24 -24
View File
@@ -32,59 +32,59 @@ use crate::{
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed to create vhost-vdpa: {0}")]
CreateVhostVdpa(vhost::Error),
CreateVhostVdpa(#[source] vhost::Error),
#[error("Failed to map DMA range: {0}")]
DmaMap(vhost::Error),
DmaMap(#[source] vhost::Error),
#[error("Failed to unmap DMA range: {0}")]
DmaUnmap(vhost::Error),
DmaUnmap(#[source] vhost::Error),
#[error("Failed to get address range")]
GetAddressRange,
#[error("Failed to get the available index from the virtio queue: {0}")]
GetAvailableIndex(virtio_queue::Error),
GetAvailableIndex(#[source] virtio_queue::Error),
#[error("Get virtio configuration size: {0}")]
GetConfigSize(vhost::Error),
GetConfigSize(#[source] vhost::Error),
#[error("Get virtio device identifier: {0}")]
GetDeviceId(vhost::Error),
GetDeviceId(#[source] vhost::Error),
#[error("Failed to get backend specific features: {0}")]
GetBackendFeatures(vhost::Error),
GetBackendFeatures(#[source] vhost::Error),
#[error("Failed to get virtio features: {0}")]
GetFeatures(vhost::Error),
GetFeatures(#[source] vhost::Error),
#[error("Failed to get the IOVA range: {0}")]
GetIovaRange(vhost::Error),
GetIovaRange(#[source] vhost::Error),
#[error("Failed to get queue size: {0}")]
GetVringNum(vhost::Error),
GetVringNum(#[source] vhost::Error),
#[error("Invalid IOVA range: {0}-{1}")]
InvalidIovaRange(u64, u64),
#[error("Missing VIRTIO_F_ACCESS_PLATFORM feature")]
MissingAccessPlatformVirtioFeature,
#[error("Failed to reset owner: {0}")]
ResetOwner(vhost::Error),
ResetOwner(#[source] vhost::Error),
#[error("Failed to set backend specific features: {0}")]
SetBackendFeatures(vhost::Error),
SetBackendFeatures(#[source] vhost::Error),
#[error("Failed to set backend configuration: {0}")]
SetConfig(vhost::Error),
SetConfig(#[source] vhost::Error),
#[error("Failed to set eventfd notifying about a configuration change: {0}")]
SetConfigCall(vhost::Error),
SetConfigCall(#[source] vhost::Error),
#[error("Failed to set virtio features: {0}")]
SetFeatures(vhost::Error),
SetFeatures(#[source] vhost::Error),
#[error("Failed to set memory table: {0}")]
SetMemTable(vhost::Error),
SetMemTable(#[source] vhost::Error),
#[error("Failed to set owner: {0}")]
SetOwner(vhost::Error),
SetOwner(#[source] vhost::Error),
#[error("Failed to set virtio status: {0}")]
SetStatus(vhost::Error),
SetStatus(#[source] vhost::Error),
#[error("Failed to set vring address: {0}")]
SetVringAddr(vhost::Error),
SetVringAddr(#[source] vhost::Error),
#[error("Failed to set vring base: {0}")]
SetVringBase(vhost::Error),
SetVringBase(#[source] vhost::Error),
#[error("Failed to set vring eventfd when buffer are used: {0}")]
SetVringCall(vhost::Error),
SetVringCall(#[source] vhost::Error),
#[error("Failed to enable/disable vring: {0}")]
SetVringEnable(vhost::Error),
SetVringEnable(#[source] vhost::Error),
#[error("Failed to set vring eventfd when new descriptors are available: {0}")]
SetVringKick(vhost::Error),
SetVringKick(#[source] vhost::Error),
#[error("Failed to set vring size: {0}")]
SetVringNum(vhost::Error),
SetVringNum(#[source] vhost::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
+40 -40
View File
@@ -43,85 +43,85 @@ pub use self::vu_common_ctrl::VhostUserConfig;
#[derive(Error, Debug)]
pub enum Error {
#[error("Failed accepting connection: {0}")]
AcceptConnection(io::Error),
AcceptConnection(#[source] io::Error),
#[error("Invalid available address")]
AvailAddress,
#[error("Queue number is not correct")]
BadQueueNum,
#[error("Failed binding vhost-user socket: {0}")]
BindSocket(io::Error),
BindSocket(#[source] io::Error),
#[error("Creating kill eventfd failed: {0}")]
CreateKillEventFd(io::Error),
CreateKillEventFd(#[source] io::Error),
#[error("Cloning kill eventfd failed: {0}")]
CloneKillEventFd(io::Error),
CloneKillEventFd(#[source] io::Error),
#[error("Invalid descriptor table address")]
DescriptorTableAddress,
#[error("Signal used queue failed: {0}")]
FailedSignalingUsedQueue(io::Error),
FailedSignalingUsedQueue(#[source] io::Error),
#[error("Failed to read vhost eventfd: {0}")]
MemoryRegions(MmapError),
MemoryRegions(#[source] MmapError),
#[error("Failed removing socket path: {0}")]
RemoveSocketPath(io::Error),
RemoveSocketPath(#[source] io::Error),
#[error("Failed to create frontend: {0}")]
VhostUserCreateFrontend(VhostError),
VhostUserCreateFrontend(#[source] VhostError),
#[error("Failed to open vhost device: {0}")]
VhostUserOpen(VhostError),
VhostUserOpen(#[source] VhostError),
#[error("Connection to socket failed")]
VhostUserConnect,
#[error("Get features failed: {0}")]
VhostUserGetFeatures(VhostError),
VhostUserGetFeatures(#[source] VhostError),
#[error("Get queue max number failed: {0}")]
VhostUserGetQueueMaxNum(VhostError),
VhostUserGetQueueMaxNum(#[source] VhostError),
#[error("Get protocol features failed: {0}")]
VhostUserGetProtocolFeatures(VhostError),
VhostUserGetProtocolFeatures(#[source] VhostError),
#[error("Get vring base failed: {0}")]
VhostUserGetVringBase(VhostError),
VhostUserGetVringBase(#[source] VhostError),
#[error("Vhost-user Backend not support vhost-user protocol")]
VhostUserProtocolNotSupport,
#[error("Set owner failed: {0}")]
VhostUserSetOwner(VhostError),
VhostUserSetOwner(#[source] VhostError),
#[error("Reset owner failed: {0}")]
VhostUserResetOwner(VhostError),
VhostUserResetOwner(#[source] VhostError),
#[error("Set features failed: {0}")]
VhostUserSetFeatures(VhostError),
VhostUserSetFeatures(#[source] VhostError),
#[error("Set protocol features failed: {0}")]
VhostUserSetProtocolFeatures(VhostError),
VhostUserSetProtocolFeatures(#[source] VhostError),
#[error("Set mem table failed: {0}")]
VhostUserSetMemTable(VhostError),
VhostUserSetMemTable(#[source] VhostError),
#[error("Set vring num failed: {0}")]
VhostUserSetVringNum(VhostError),
VhostUserSetVringNum(#[source] VhostError),
#[error("Set vring addr failed: {0}")]
VhostUserSetVringAddr(VhostError),
VhostUserSetVringAddr(#[source] VhostError),
#[error("Set vring base failed: {0}")]
VhostUserSetVringBase(VhostError),
VhostUserSetVringBase(#[source] VhostError),
#[error("Set vring call failed: {0}")]
VhostUserSetVringCall(VhostError),
VhostUserSetVringCall(#[source] VhostError),
#[error("Set vring kick failed: {0}")]
VhostUserSetVringKick(VhostError),
VhostUserSetVringKick(#[source] VhostError),
#[error("Set vring enable failed: {0}")]
VhostUserSetVringEnable(VhostError),
VhostUserSetVringEnable(#[source] VhostError),
#[error("Failed to create vhost eventfd: {0}")]
VhostIrqCreate(io::Error),
VhostIrqCreate(#[source] io::Error),
#[error("Failed to read vhost eventfd: {0}")]
VhostIrqRead(io::Error),
VhostIrqRead(#[source] io::Error),
#[error("Failed to read vhost eventfd: {0}")]
VhostUserMemoryRegion(MmapError),
VhostUserMemoryRegion(#[source] MmapError),
#[error("Failed to create the frontend request handler from backend: {0}")]
FrontendReqHandlerCreation(vhost::vhost_user::Error),
FrontendReqHandlerCreation(#[source] vhost::vhost_user::Error),
#[error("Set backend request fd failed: {0}")]
VhostUserSetBackendRequestFd(vhost::Error),
VhostUserSetBackendRequestFd(#[source] vhost::Error),
#[error("Add memory region failed: {0}")]
VhostUserAddMemReg(VhostError),
VhostUserAddMemReg(#[source] VhostError),
#[error("Failed getting the configuration: {0}")]
VhostUserGetConfig(VhostError),
VhostUserGetConfig(#[source] VhostError),
#[error("Failed setting the configuration: {0}")]
VhostUserSetConfig(VhostError),
VhostUserSetConfig(#[source] VhostError),
#[error("Failed getting inflight shm log: {0}")]
VhostUserGetInflight(VhostError),
VhostUserGetInflight(#[source] VhostError),
#[error("Failed setting inflight shm log: {0}")]
VhostUserSetInflight(VhostError),
VhostUserSetInflight(#[source] VhostError),
#[error("Failed setting the log base: {0}")]
VhostUserSetLogBase(VhostError),
VhostUserSetLogBase(#[source] VhostError),
#[error("Invalid used address")]
UsedAddress,
#[error("Invalid features provided from vhost-user backend")]
@@ -131,17 +131,17 @@ pub enum Error {
#[error("Missing IrqFd")]
MissingIrqFd,
#[error("Failed getting the available index: {0}")]
GetAvailableIndex(QueueError),
GetAvailableIndex(#[source] QueueError),
#[error("Migration is not supported by this vhost-user device")]
MigrationNotSupported,
#[error("Failed creating memfd: {0}")]
MemfdCreate(io::Error),
MemfdCreate(#[source] io::Error),
#[error("Failed truncating the file size to the expected size: {0}")]
SetFileSize(io::Error),
SetFileSize(#[source] io::Error),
#[error("Failed to set the seals on the file: {0}")]
SetSeals(io::Error),
SetSeals(#[source] io::Error),
#[error("Failed creating new mmap region: {0}")]
NewMmapRegion(MmapRegionError),
NewMmapRegion(#[source] MmapRegionError),
#[error("Could not find the shm log region")]
MissingShmLogRegion,
}
+3 -3
View File
@@ -49,15 +49,15 @@ const WATCHDOG_TIMEOUT: u64 = WATCHDOG_TIMER_INTERVAL as u64 + 5;
#[derive(Error, Debug)]
enum Error {
#[error("Error programming timer fd: {0}")]
TimerfdSetup(io::Error),
TimerfdSetup(#[source] io::Error),
#[error("Descriptor chain too short")]
DescriptorChainTooShort,
#[error("Failed adding used index: {0}")]
QueueAddUsed(virtio_queue::Error),
QueueAddUsed(#[source] virtio_queue::Error),
#[error("Invalid descriptor")]
InvalidDescriptor,
#[error("Failed to write to guest memory: {0}")]
GuestMemoryWrite(vm_memory::guest_memory::Error),
GuestMemoryWrite(#[source] vm_memory::guest_memory::Error),
}
struct WatchdogEpollHandler {