You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Go to file
Dave Tucker 79343c445a examples: Add an XDP example
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
4 years ago
.cargo added support for armv7-unknown-linux-gnueabi and armv7-unknown-linux-gnueabihf 4 years ago
.github/workflows examples: Add an XDP example 4 years ago
aya fix: pass BTF object by reference in order to allow multiple eBPF programs to share it and save memory (closes #30). (#31) 4 years ago
aya-gen aya-gen: fix clippy warnings 4 years ago
bpf bpf: Add set method for Array 4 years ago
examples examples: Add an XDP example 4 years ago
xtask examples: Add an XDP example 4 years ago
.gitignore ignore .vscode 4 years ago
CONTRIBUTING.md Add CONTRIBUTING.md 4 years ago
Cargo.toml Add aya-gen 4 years ago
LICENSE-APACHE Add license files 4 years ago
LICENSE-MIT Add license files 4 years ago
README.md examples: Add an XDP example 4 years ago
rustfmt.toml Add rustfmt.toml 4 years ago

README.md

Aya

Crates.io License Build status

API docs | Chat

Overview

eBPF is a technology that allows running user-supplied programs inside the Linux kernel. For more info see https://ebpf.io/what-is-ebpf.

Aya is an eBPF library built with a focus on operability and developer experience. It does not rely on libbpf nor bcc - it's built from the ground up purely in Rust, using only the libc crate to execute syscalls. With BTF support and when linked with musl, it offers a true compile once, run everywhere solution, where a single self-contained binary can be deployed on many linux distributions and kernel versions.

Some of the major features provided include:

  • Support for the BPF Type Format (BTF), which is transparently enabled when supported by the target kernel. This allows eBPF programs compiled against one kernel version to run on different kernel versions without the need to recompile.
  • Support for function call relocation and global data maps, which allows eBPF programs to make function calls and use global variables and initializers.
  • Async support with both tokio and async-std.
  • Easy to deploy and fast to build: aya doesn't require a kernel build or compiled headers, and not even a C toolchain; a release build completes in a matter of seconds.

Example

Aya supports a large chunk of the eBPF API. The following example shows how to use a BPF_PROG_TYPE_CGROUP_SKB program with aya:

use std::fs::File;
use std::convert::TryInto;
use aya::Bpf;
use aya::programs::{CgroupSkb, CgroupSkbAttachType};

// load the BPF code
let mut bpf = Bpf::load_file("bpf.o")?;

// get the `ingress_filter` program compiled into `bpf.o`. 
let ingress: &mut CgroupSkb = bpf.program_mut("ingress_filter")?.try_into()?;

// load the program into the kernel
ingress.load()?;

// attach th 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)?;

Examples

There are some examples of how to use Aya to write BPF programs and userspace program in the examples directory. These can be compiled by running cargo xtask examples. The BPF examples require you to have a rust nightly toolchain installed.

cargo xtask examples

Run

XDP

sudo ./examples/target/debug/xdp_userspace ./examples/target/bpfel-unknown-none/release/xdp wlp2s0

Community

Join the conversation on Discord to discuss anything related to aya.

Contributing

Please see the contributing guide.

License

Aya is distributed under the terms of either the MIT license or the Apache License (version 2.0), at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.