Skip to content

A simple pure hello world demo of writing an ebpf filter in rust

Notifications You must be signed in to change notification settings

fzakaria/rust-ebpf-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust eBPF Hello World

This is a minimal example of how to write an eBPF filter in rust natively. This "hello-world" draws on a lot of inspiration from this blog post however I found the need to use gobpf unecessary.

The blog uses gobpf simply from what I can surmise to leverage gobpf ability to load ebpf programs for kprobes. gobpf uses the section names in a well understood format to attach the filter to the correct function.

Building

The following will has rustc emit the LLVM IR instead of produce a full library. The LLVM IR can then be fed into llc which can generate the correct eBPF object ELF file.

cargo rustc --release -- --emit=llvm-ir
cp target/release/deps/rust_ebpf_demo-*.ll rust_ebpf_demo.ll
cargo rustc --release -- --emit=llvm-bc
cp target/release/deps/rust_ebpf_demo-*.bc rust_ebpf_demo.bc
llc rust_ebpf_demo.bc -march=bpf -filetype=obj -o rust_ebpf_demo.o

Testing

In order to see the test you have to make sure you have the trace pipe enabled.

echo 1 > /sys/kernel/debug/tracing/tracing_on

# Once enabled you can do:
sudo cat /sys/kernel/debug/tracing/trace_pipe 

About

A simple pure hello world demo of writing an ebpf filter in rust

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published