aya-rs/aya

GitHub: aya-rs/aya

Aya 是一个用 Rust 编写的 eBPF 库,不依赖 libbpf 或 C 工具链,提供了高性能、易操作且支持 CO-RE 的内核程序开发解决方案。

Stars: 4444 | Forks: 417

# [![Aya](https://static.pigsec.cn/wp-content/uploads/repos/2026/04/6ec54d2b6d053025.svg)](https://aya-rs.dev) [![Crates.io](https://img.shields.io/crates/v/aya.svg?style=for-the-badge&logo=rust)][crates-url] ![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue?style=for-the-badge) [![Build status](https://img.shields.io/github/actions/workflow/status/aya-rs/aya/ci.yml?style=for-the-badge)][build-url] [![Book](https://img.shields.io/badge/read%20the-book-9cf.svg?style=for-the-badge&logo=mdbook)][book-url] [![Gurubase](https://img.shields.io/badge/Gurubase-Ask%20Aya%20Guru-006BFF?style=for-the-badge)][gurubase-url] ## API 文档 [![Unreleased Documentation](https://img.shields.io/badge/docs-unreleased-red.svg?style=for-the-badge&logo=docsdotrs)][git-api-docs] [![Documentaiton](https://img.shields.io/badge/docs-released-blue.svg?style=for-the-badge&logo=docsdotrs)][api-docs] ## 概述 eBPF 是一项允许在 Linux 内核内部运行用户提供的程序的技术。更多信息请参见 [What is eBPF](https://ebpf.io/what-is-ebpf)。 Aya 是一个注重可操作性和开发者体验的 eBPF 库。它不依赖 [libbpf] 也不依赖 [bcc] —— 它完全使用 Rust 从零开始构建,仅使用 [libc] crate 来执行系统调用。在支持 BTF 并与 musl 链接时,它提供了真正的 [compile once, run everywhere solution][co-re](一次编译,随处运行的解决方案),即单个独立的二进制文件可以部署在许多 Linux 发行版和内核版本上。 提供的一些主要功能包括: * 支持 **BPF Type Format** (BTF),当目标内核支持时会自动启用。这允许针对一个内核版本编译的 eBPF 程序在不同的内核版本上运行,而无需重新编译。 * 支持函数调用重定位和全局数据映射,这使得 eBPF 程序能够进行 **函数调用** 并使用 **全局变量和初始化器**。 * 使用 [tokio] 和 [async-std] 的 **异步支持**。 * 易于部署且构建快速:aya 不需要构建内核或编译头文件,甚至不需要 C 工具链;一次 release 构建仅需几秒钟即可完成。 ### 示例 Aya 支持大部分 eBPF API。以下示例展示了如何将 aya 与 `BPF_PROG_TYPE_CGROUP_SKB` 程序一起使用: ``` use std::fs::File; use aya::Ebpf; use aya::programs::{CgroupSkb, CgroupSkbAttachType, CgroupAttachMode}; // load the BPF code let mut ebpf = Ebpf::load_file("ebpf.o")?; // get the `ingress_filter` program compiled into `ebpf.o`. let ingress: &mut CgroupSkb = ebpf.program_mut("ingress_filter")?.try_into()?; // load the program into the kernel ingress.load()?; // attach the program to the root cgroup. `ingress_filter` will be called for all // incoming packets. let cgroup = File::open("/sys/fs/cgroup/unified")?; ingress.attach(cgroup, CgroupSkbAttachType::Ingress, CgroupAttachMode::AllowOverride)?; ``` ## 贡献 请参阅 [贡献指南](https://github.com/aya-rs/aya/blob/main/CONTRIBUTING.md)。 ## 许可证 Aya 根据 [MIT license] 或 [Apache License](版本 2.0)的条款进行分发,由您自行选择。 除非您明确声明,否则您根据 Apache-2.0 许可证的定义有意提交以包含在此 crate 中的任何贡献,均应按上述方式获得双重许可,且不附带任何其他条款或条件。
标签:API集成, Async 异步, bcc 替代, BTF, Cgroup, Compile Once Run Everywhere, Docker镜像, libbpf 替代, Linux 内核, musl, Rust, Tokio, 可观测性, 可视化界面, 子域名生成, 安全渗透, 开发者体验, 性能分析, 系统编程, 网络安全, 网络流量审计, 网络追踪, 通知系统, 隐私保护