Skip to main content

qualia_core_db/net/qdnf/cells/
af_xdp.rs

1//! AF_XDP acceleration is not admitted on this Linux userspace (E10.6).
2//!
3//! Opening returns [`QdnfError::PlatformUnsupported`]. There is no CAP path,
4//! no UMEM registration, and no silent fallback to a fake zero-copy NIC
5//! (IPC / in-process loop). Portable bearer semantics must pass first.
6
7use crate::net::qdnf::errors::QdnfError;
8
9/// POSIX `IFNAMSIZ` (including the trailing NUL).
10const IFNAMSIZ: usize = 16;
11
12fn validate_ifname(interface: &[u8]) -> Result<(), QdnfError> {
13    if interface.is_empty() || interface.len() >= IFNAMSIZ {
14        Err(QdnfError::Range)
15    } else {
16        Ok(())
17    }
18}
19
20/// AF_XDP endpoint. Construction is fail-closed; no live socket is held.
21#[derive(Debug)]
22pub struct AfXdp {
23    _private: (),
24}
25
26impl AfXdp {
27    /// Open an AF_XDP socket. Always [`QdnfError::PlatformUnsupported`] after a
28    /// valid interface name. Never returns Ok on this host.
29    pub fn open(interface: &[u8]) -> Result<Self, QdnfError> {
30        validate_ifname(interface)?;
31        Err(QdnfError::PlatformUnsupported)
32    }
33}
34
35/// Module-level open. Same fail-closed gate as [`AfXdp::open`].
36pub fn open_af_xdp(interface: &[u8]) -> Result<AfXdp, QdnfError> {
37    AfXdp::open(interface)
38}
39
40/// AF_XDP never falls back to IPC, loopback, or a synthetic zero-copy NIC.
41#[inline]
42pub const fn silent_zero_copy_fallback() -> bool {
43    false
44}
45
46/// UMEM is never registered by this module.
47#[inline]
48pub const fn umem_registered() -> bool {
49    false
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::net::qdnf::errors::QdnfError;
56
57    #[test]
58    fn open_fails_closed_platform_unsupported() {
59        assert_eq!(
60            AfXdp::open(b"eth0").unwrap_err(),
61            QdnfError::PlatformUnsupported
62        );
63        assert_eq!(
64            open_af_xdp(b"lo").unwrap_err(),
65            QdnfError::PlatformUnsupported
66        );
67        assert!(!silent_zero_copy_fallback());
68        assert!(!umem_registered());
69    }
70
71    #[test]
72    fn open_never_ok_and_never_fake_zero_copy() {
73        let names: [&[u8]; 3] = [b"eth0", b"enp0s1", b"wlan0"];
74        let mut i = 0usize;
75        while i < names.len() {
76            match AfXdp::open(names[i]) {
77                Err(QdnfError::PlatformUnsupported) => {}
78                other => panic!("AF_XDP must fail closed, got {other:?}"),
79            }
80            i += 1;
81        }
82        assert!(!silent_zero_copy_fallback());
83        assert!(!umem_registered());
84    }
85
86    #[test]
87    fn invalid_ifname_is_range_not_success() {
88        assert_eq!(AfXdp::open(b"").unwrap_err(), QdnfError::Range);
89        assert_eq!(AfXdp::open(&[b'x'; 16]).unwrap_err(), QdnfError::Range);
90        assert!(AfXdp::open(b"eth0").is_err());
91    }
92}