aya-log - a logging library for eBPF programs
Overview
aya-log is a logging library for eBPF programs written using aya. Think of
it as the log crate for eBPF.
Installation
User space
Add aya-log to Cargo.toml:
[]
= { = "https://github.com/aya-rs/aya", = "main" }
eBPF side
Add aya-log-ebpf to Cargo.toml:
[]
= { = "https://github.com/aya-rs/aya", = "main" }
Example
Here's an example that uses aya-log in conjunction with the env_logger crate
to log eBPF messages to the terminal.
User space code
use EbpfLogger;
init;
// Will log using the default logger, which is TermLogger in this case
let logger = init.unwrap;
let mut logger = with_interest.unwrap;
spawn;
eBPF code
use info;
Disabling log levels at load-time
eBPF instruction budgets are tight. Even if a log statement never executes at
runtime, the verifier must still evaluate its instructions unless it can prove
they're unreachable. aya-log now exposes a global AYA_LOG_LEVEL inside the
eBPF object allowing you to selectively enable levels before the program is
loaded.
By default all bits are set (all logging enabled). To disable all logging:
let mut bpf = new
.override_global
.load_file?;
# Ok::
Enable only Error and Warn:
let level = Warn as u8;
let mut bpf = new
.override_global
.load_file?;
Because the level is placed in global read-only data, the verifier sees the disabled branch as unreachable and prunes the logging instructions, reducing overall instruction count and avoiding potential instruction limit issues.