#ebpf #bpf #log #logging

archived aya-log-common

A logging library for eBPF programs

5 releases

Uses new Rust 2021

0.1.13 Nov 16, 2022
0.1.12 Nov 16, 2022
0.1.11 Sep 1, 2022
0.1.10 Jun 8, 2022
0.1.1 Oct 15, 2021

#43 in #ebpf

Download history 50/week @ 2022-08-13 46/week @ 2022-08-20 77/week @ 2022-08-27 89/week @ 2022-09-03 198/week @ 2022-09-10 210/week @ 2022-09-17 48/week @ 2022-09-24 74/week @ 2022-10-01 68/week @ 2022-10-08 62/week @ 2022-10-15 56/week @ 2022-10-22 68/week @ 2022-10-29 89/week @ 2022-11-05 142/week @ 2022-11-12 100/week @ 2022-11-19 93/week @ 2022-11-26

442 downloads per month
Used in aya-log

MIT/Apache

8KB
171 lines

This repo has been moved. It is now located at https://github.com/aya-rs/aya

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

~0.2–5.5MB
~97K SLoC