zenith-ebpf 0.1.0

Zenith eBPF 程序管理:预编译字节码嵌入(include_bytes!)、libbpf-rs 高性能加载、bpf_link 原子挂载、双 Bank 热更新
//! Zenith eBPF - eBPF 程序管理与 XDP 支持
//!
//! 本 crate 提供极致优化的 eBPF 程序管理能力,包括:
//! - 预编译字节码嵌入(include_bytes!)
//! - libbpf-rs 高性能加载
//! - bpf_link 原子挂载
//! - 双 Bank 热更新(零丢包切换)
//!
//! ## 架构设计
//!
//! ```text
//! +---------------------------------------------------------+
//! |                    用户态 Rust                          |
//! |  +-------------+  +-------------+  +-----------------+  |
//! |  | XdpLoader   |  | BpfMaps     |  | DualBankManager |  |
//! |  +------+------+  +------+------+  +--------+--------+  |
//! |         |                |                  |            |
//! |         +----------------+------------------+            |
//! |                          |                               |
//! |              +-----------+-----------+                  |
//! |              |     libbpf-rs         |                  |
//! |              +-----------+-----------+                  |
//! +--------------------------+------------------------------+
//!                            | bpf_link
//! +--------------------------+------------------------------+
//! |                    内核态 eBPF                           |
//! |              +-----------+-----------+                  |
//! |              |    XDP eBPF 程序      |                  |
//! |              |  (预编译字节码加载)    |                  |
//! |              +-----------+-----------+                  |
//! |                          |                               |
//! |              +-----------+-----------+                  |
//! |              |   BPF Maps (XSKMAP等)  |                  |
//! |              +-----------------------+                  |
//! +---------------------------------------------------------+
//! ```

#![deny(unsafe_code)]
#![deny(missing_debug_implementations)]
#![warn(missing_docs)]
// eBPF/libbpf 仅 Linux 可用:非 Linux 目标下本 crate 整体为空,
// 依赖方(zenith-runtime 等)以同样的 cfg(target_os = "linux") 门控使用点。
#![cfg(target_os = "linux")]

pub mod attach;
pub mod bank;
pub mod error;
pub mod maps;
pub mod xdp_attach;
pub mod xdp_prog;

pub use attach::{
    AttachPoint, AttachedHandle, CgroupAttachType, TcDirection, attach_program, detach_program,
};
pub use bank::{BankId, DualBankManager};
pub use error::{AttachError, EbpfError, LoadError, ManagerError, MapError};
pub use maps::{BpfMaps, ExpectationConfig, StatId};
pub use xdp_attach::{XdpAttachMode, query_xdp_mode};
pub use xdp_prog::{XdpLoader, XdpProgramName};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bank_id_values() {
        assert_eq!(BankId::A.as_u32(), 0);
        assert_eq!(BankId::B.as_u32(), 1);
    }

    #[test]
    fn test_stat_id_values() {
        assert_eq!(StatId::RxPackets.as_u32(), 0);
        assert_eq!(StatId::DropNoXsk.as_u32(), 5);
    }
}