virtio-devices: vsock: improve error handling

On-behalf-of: SAP philipp.schuster@sap.com
Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
This commit is contained in:
Philipp Schuster
2026-06-02 06:52:49 +02:00
committed by Rob Bradford
parent cfc639de35
commit 727b704606
2 changed files with 20 additions and 5 deletions

View File

@@ -16,6 +16,7 @@ mod unix;
use std::os::unix::io::RawFd;
use packet::VsockPacket;
use thiserror::Error;
pub use self::device::Vsock;
pub use self::unix::{VsockUnixBackend, VsockUnixError};
@@ -63,29 +64,43 @@ mod defs {
}
}
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum VsockError {
/// The vsock data/buffer virtio descriptor length is smaller than expected.
#[error("The vsock data/buffer virtio descriptor length is smaller than expected")]
BufDescTooSmall,
/// The vsock data/buffer virtio descriptor is expected, but missing.
#[error("The vsock data/buffer virtio descriptor is expected, but missing")]
BufDescMissing,
/// Chained GuestMemory error.
#[error("Guest memory error")]
GuestMemory,
/// Chained GuestMemory access error.
#[error("Guest memory access error")]
GuestMemoryAccess(#[source] vm_memory::GuestMemoryError),
/// Bounds check failed on guest memory pointer.
#[error("Bounds check failed on guest memory pointer")]
GuestMemoryBounds,
/// The vsock header descriptor length is too small.
#[error("The vsock header descriptor length is too small: {0}")]
HdrDescTooSmall(u32),
/// The vsock header descriptor is expected, but missing.
#[error("The vsock header descriptor is expected, but missing")]
HdrDescMissing,
/// The vsock header `len` field holds an invalid value.
#[error("The vsock header len field holds an invalid value: {0}")]
InvalidPktLen(u32),
/// A data fetch was attempted when no data was available.
#[error("A data fetch was attempted when no data was available")]
NoData,
/// A data buffer was expected for the provided packet, but it is missing.
#[error("A data buffer was expected for the provided packet, but it is missing")]
PktBufMissing,
/// Encountered an unexpected write-only virtio descriptor.
#[error("Encountered an unexpected write-only virtio descriptor")]
UnreadableDescriptor,
/// Encountered an unexpected read-only virtio descriptor.
#[error("Encountered an unexpected read-only virtio descriptor")]
UnwritableDescriptor,
}
type Result<T> = std::result::Result<T, VsockError>;

View File

@@ -290,7 +290,7 @@ impl VsockPacket {
desc_chain
.memory()
.read_slice(hdr.as_mut_slice(), guest_hdr_addr)
.map_err(|_| VsockError::GuestMemory)?;
.map_err(VsockError::GuestMemoryAccess)?;
let mut pkt = Self {
guest_hdr_addr,
@@ -361,7 +361,7 @@ impl VsockPacket {
desc_chain
.memory()
.read_slice(&mut owned[offset..offset + to_copy], desc.addr())
.map_err(|_| VsockError::GuestMemory)?;
.map_err(VsockError::GuestMemoryAccess)?;
offset += to_copy;
}
@@ -435,7 +435,7 @@ impl VsockPacket {
desc_chain
.memory()
.read_slice(hdr.as_mut_slice(), guest_hdr_addr)
.map_err(|_| VsockError::GuestMemory)?;
.map_err(VsockError::GuestMemoryAccess)?;
// Prior to Linux v6.3 there are two descriptors
let (buf_size, buf_addr) = if head.has_next() {
@@ -506,7 +506,7 @@ impl VsockPacket {
guest_mem
.write(self.hdr(), self.guest_hdr_addr)
.map_err(|_| VsockError::GuestMemory)?;
.map_err(VsockError::GuestMemoryAccess)?;
Ok(())
}