Skip to main content

laminar_core/xdp/
error.rs

1//! XDP error types.
2
3use std::path::PathBuf;
4
5/// Errors that can occur during XDP operations.
6#[derive(Debug, thiserror::Error)]
7pub enum XdpError {
8    /// XDP is not available on this platform.
9    #[error("XDP not available on this platform")]
10    NotAvailable,
11
12    /// XDP program file not found.
13    #[error("XDP program not found: {0}")]
14    ProgramNotFound(PathBuf),
15
16    /// BPF map not found in XDP program.
17    #[error("BPF map not found: {0}")]
18    MapNotFound(String),
19
20    /// Network interface not found.
21    #[error("Network interface not found: {0}")]
22    InterfaceNotFound(String),
23
24    /// Failed to load BPF program.
25    #[error("Failed to load BPF program: {0}")]
26    LoadFailed(String),
27
28    /// Failed to attach XDP program.
29    #[error("Failed to attach XDP program: {0}")]
30    AttachFailed(String),
31
32    /// Failed to detach XDP program.
33    #[error("Failed to detach XDP program: {0}")]
34    DetachFailed(String),
35
36    /// Failed to update BPF map.
37    #[error("Failed to update BPF map: {0}")]
38    MapUpdateFailed(String),
39
40    /// Failed to read BPF map.
41    #[error("Failed to read BPF map: {0}")]
42    MapReadFailed(String),
43
44    /// Permission denied (requires `CAP_NET_ADMIN` or root).
45    #[error("Permission denied: XDP requires CAP_NET_ADMIN or root")]
46    PermissionDenied,
47
48    /// Invalid configuration.
49    #[error("Invalid configuration: {0}")]
50    InvalidConfig(String),
51
52    /// I/O error.
53    #[error("I/O error: {0}")]
54    Io(#[from] std::io::Error),
55}
56
57impl XdpError {
58    /// Returns true if this is a permission error.
59    #[must_use]
60    pub fn is_permission_error(&self) -> bool {
61        matches!(self, XdpError::PermissionDenied)
62    }
63
64    /// Returns true if XDP is not available.
65    #[must_use]
66    pub fn is_not_available(&self) -> bool {
67        matches!(self, XdpError::NotAvailable)
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_error_display() {
77        let err = XdpError::NotAvailable;
78        assert_eq!(err.to_string(), "XDP not available on this platform");
79
80        let err = XdpError::InterfaceNotFound("eth0".to_string());
81        assert_eq!(err.to_string(), "Network interface not found: eth0");
82    }
83
84    #[test]
85    fn test_is_permission_error() {
86        assert!(XdpError::PermissionDenied.is_permission_error());
87        assert!(!XdpError::NotAvailable.is_permission_error());
88    }
89
90    #[test]
91    fn test_is_not_available() {
92        assert!(XdpError::NotAvailable.is_not_available());
93        assert!(!XdpError::PermissionDenied.is_not_available());
94    }
95}