#ebpf #bpf #log #logging

aya-log

A logging library for eBPF programs

4 releases

0.1.10 Jun 8, 2022
0.1.9 Apr 23, 2022
0.1.1 Oct 15, 2021

#93 in Unix APIs

Download history 4/week @ 2022-03-14 3/week @ 2022-03-21 1/week @ 2022-04-04 1/week @ 2022-04-11 54/week @ 2022-04-18 110/week @ 2022-04-25 140/week @ 2022-05-02 78/week @ 2022-05-09 322/week @ 2022-05-16 54/week @ 2022-05-23 49/week @ 2022-05-30 143/week @ 2022-06-06 44/week @ 2022-06-13 70/week @ 2022-06-20 42/week @ 2022-06-27

306 downloads per month

MIT/Apache

21KB
413 lines

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:

[dependencies]
aya-log = { git = "https://github.com/aya-rs/aya-log", branch = "main" }

eBPF side

Add aya-log-ebpf to Cargo.toml:

[dependencies]
aya-log-ebpf = { git = "https://github.com/aya-rs/aya-log", branch = "main" }

Example

Here's an example that uses aya-log in conjunction with the simplelog crate to log eBPF messages to the terminal.

User space code

use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
use aya_log::BpfLogger;

TermLogger::init(
    LevelFilter::Debug,
    ConfigBuilder::new()
        .set_target_level(LevelFilter::Error)
        .set_location_level(LevelFilter::Error)
        .build(),
    TerminalMode::Mixed,
    ColorChoice::Auto,
)
.unwrap();

// Will log using the default logger, which is TermLogger in this case
BpfLogger::init(&mut bpf).unwrap();

eBPF code

use aya_log_ebpf::info;

fn try_xdp_firewall(ctx: XdpContext) -> Result<u32, ()> {
    if let Some(port) = tcp_dest_port(&ctx)? {
        if block_port(port) {
            info!(&ctx, "❌ blocked incoming connection on port: {}", port);
            return Ok(XDP_DROP);
        }
    }

    Ok(XDP_PASS)
}

Dependencies

~5–11MB
~197K SLoC