As you may know, BPF CO-RE is not supported by Aya from Rust code.
Supporting CO-RE directly from Rust code is a consequent work as it requires
modifying the rustc
target to compile to bpf and this is in the pipe of Aya team.
However, a work around can be made by defining so-called shim functions in C
to access structures you need CO-RE support for. These functions are
then linked to your eBPF binary using bpf-linker
, but this is totally
transparent to you. The only things you need to do are:
- defining your shim functions in C
- create a
build.rs
to compile the shim to eBPF - use it in Rust via
extern C
functions.
This repo gives you examples on how to define shim functions.
- Install a rust stable toolchain:
rustup install stable
- Install a rust nightly toolchain with the rust-src component:
rustup toolchain install nightly --component rust-src
- Install custom bpf-linker (see next section)
!!!! IMPORTANT: This might not be true for ever as bpf-linker
will be
updated so that you don't have to do all that custom installation
Making shim working requires embedding BTF information (used for relocation)
inside your eBPF program. However the way Rust generates Debugging Information
used for BTF (because a part of your program is Rust) makes the Linux kernel
un-happy when trying to load/relocate your program. So, to generate BTF
information your kernel will be happy with, it requires both LLVM to be patched
and bpf-linker
to be patched to process LLVM Debugging Information
in such a way your eBPF binary contains BTF information the Linux kernel can
handle.
Basically, to install a bpf-linker
which will work with this example and with
C-shim in general, you should follow the instructions here: https://github.com/vadorovsky/aya-btf-maps-experiments. Right after you can find a set of condensed instructions to achieve the same goal.
- clone forked llvm:
git clone https://github.com/vadorovsky/llvm-project.git
cd llvm-project
git checkout bpf-fixes
mkdir build && cd build
- build llvm:
CC=clang CXX=clang++ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_PARALLEL_LINK_JOBS=1 -DLLVM_ENABLE_LLD=1 -GNinja ../llvm/
!!! That process will replace your current bpf-linker
so if you want
to revert those changes go back to the original procedure of installing bpf-linker
(i.e. cargo install bpf-linker
).
- Make sure you have built a custom LLVM
- clone forked
bpf-linker
:git clone https://github.com/vadorovsky/bpf-linker.git
cd bpf-linker
git checkout fix-di
- Install custom
bpf-linker
:LLVM_SYS_160_PREFIX=/path/to/llvm-project/build cargo install --path . --no-default-features --features system-llvm bpf-linker
cargo xtask build-ebpf
To perform a release build you can use the --release
flag.
You may also change the target architecture with the --target
flag.
cargo build
RUST_LOG=info cargo xtask run