Skip to main content

rdma_io/
error.rs

1//! Error types for the RDMA safe API.
2
3use std::io;
4
5/// Result type alias using [`Error`].
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors returned by RDMA operations.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    /// An ibverbs call returned an error (typically sets `errno`).
12    #[error("ibverbs error: {0}")]
13    Verbs(#[from] io::Error),
14
15    /// No RDMA devices found on this system.
16    #[error("no RDMA devices found")]
17    NoDevices,
18
19    /// The requested device was not found.
20    #[error("device not found: {0}")]
21    DeviceNotFound(String),
22
23    /// An invalid argument was supplied.
24    #[error("invalid argument: {0}")]
25    InvalidArg(String),
26
27    /// A work completion finished with an error status.
28    #[error("work completion error: {status} (vendor_err={vendor_err:#x})")]
29    WorkCompletion {
30        /// The WC status code.
31        status: u32,
32        /// Vendor-specific error code.
33        vendor_err: u32,
34    },
35
36    /// Non-blocking operation would block (EAGAIN/EWOULDBLOCK).
37    #[error("operation would block")]
38    WouldBlock,
39}
40
41/// Convert a C return code (0 = success, negative = -errno) to a `Result`.
42pub(crate) fn from_ret(ret: i32) -> Result<()> {
43    if ret == 0 {
44        Ok(())
45    } else {
46        Err(Error::Verbs(io::Error::from_raw_os_error(-ret)))
47    }
48}
49
50/// Convert a C return code (-1 = failure, errno set) to a `Result`.
51///
52/// Used for rdma_cm functions which return -1 on error and set `errno`,
53/// unlike ibverbs functions which return negative errno values directly.
54pub(crate) fn from_ret_errno(ret: i32) -> Result<()> {
55    if ret == 0 {
56        Ok(())
57    } else {
58        Err(Error::Verbs(io::Error::last_os_error()))
59    }
60}
61
62/// Convert a nullable pointer return to `Result`, using `errno` on failure.
63pub(crate) fn from_ptr<T>(ptr: *mut T) -> Result<*mut T> {
64    if ptr.is_null() {
65        Err(Error::Verbs(io::Error::last_os_error()))
66    } else {
67        Ok(ptr)
68    }
69}