Skip to content
GthulhuPublic

About

Community fork for kyleseneker/tinybpf

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

tinybpf

tinybpf

Write eBPF programs in Go. Compile with TinyGo. Run in the Linux kernel.

CI Go Report Card License


Status: Experimental 0.x project, solo-maintained. Expect breaking changes to the CLI, config schema, public API, and generated code between minor versions. Pin a specific version if you depend on it. Only the latest TinyGo release is supported; see the support policy.

Why tinybpf?

eBPF lets sandboxed programs run inside the Linux kernel -- but today those programs must be written in C or Rust, even when the rest of your stack is Go.

tinybpf lets you write both the kernel-side BPF program and the userspace loader in Go. No C, no CGo, no bpf2go code generation from C sources. One language, one toolchain.

graph LR
    A["Go source"] --> B["TinyGo"]
    B --> C["LLVM IR"]
    C --> D["tinybpf"]
    D --> E["bpf.o"]
    E --> F["tinybpf generate"]
    F --> G["typed Go loader"]
Loading

The output is a standard BPF ELF object compatible with cilium/ebpf, libbpf, and bpftool. tinybpf generate then reads that object and produces type-safe Go structs for loading it -- no string-based map lookups, no unsafe.Pointer casts.

Quick start

# A module with tinybpf as a Go tool: the CLI and the bpf package at one version
mkdir my_probe && cd my_probe
go mod init my_probe
go get -tool github.com/kyleseneker/tinybpf/cmd/tinybpf@latest

# Scaffold, build the BPF object, generate its loader, and run
go tool tinybpf init my_probe
make build
make generate
go mod tidy
go build -o my_probe .
sudo ./my_probe
tracing execs; press Ctrl+C to stop
exec by pid 48213
exec by pid 48214

This needs the toolchain in Prerequisites. On macOS, every step up to go build works (build with GOOS=linux); loading the program needs Linux.

tinybpf init writes the project into the current directory: tinybpf.json, bpf/<name>.go, bpf/<name>_stub.go, gen.go, main.go, and a Makefile that runs go tool tinybpf (set TINYBPF=tinybpf to use a tinybpf on your PATH). The generated bpf/my_probe.go looks like this (trimmed). The bpf package declares the kernel's helpers, map definitions, and register layouts, as bpf_helpers.h and bpf_tracing.h do for C:

//go:build tinygo

package main

import (
    "unsafe"

    "github.com/kyleseneker/tinybpf/bpf"
)

var events = bpf.MapDef{Type: bpf.MapTypeRingbuf, MaxEntries: 1 << 24}

//export my_probe
func my_probe(ctx unsafe.Pointer) int32 {
    pid := uint32(bpf.GetCurrentPidTgid() >> 32)
    bpf.RingbufOutput(unsafe.Pointer(&events), unsafe.Pointer(&pid), 4, 0)
    return 0
}

func main() {}

make generate runs tinybpf generate, which writes a typed loader for the object, and main.go loads and attaches the program with it:

// Generated by tinybpf generate: typed fields, no string lookups.
objs, err := Load()
defer objs.Close()

// objs.MyProbe is a *ebpf.Program; use it directly with cilium/ebpf.
tp, err := link.Tracepoint("syscalls", "sys_enter_execve", objs.MyProbe, nil)

The loader embeds the object when it is in or below the loader's directory, as build/my_probe.bpf.o is here; otherwise Load takes the object's path. Field names like MyProbe come from the program's function name (my_probe), converted from snake_case to PascalCase. For complete programs, see the examples.

Prerequisites

You need Go 1.26+, the latest TinyGo (0.44), and an LLVM no older than the one TinyGo bundles (22): llvm-link, opt, llc, plus llvm-ar / llvm-objcopy for archive and object inputs, and pahole for --btf. The canonical version list lives in Support Matrix. The LLVM tools must be on your PATH.

go tool tinybpf doctor checks the toolchain. From a clone of this repository, make setup installs it:

make setup    # install all dependencies
make doctor   # verify toolchain

See Toolchain Install for details.

Examples

Tracing and observability

Example Hook type What it demonstrates
minimal Tracepoint Port of libbpf-bootstrap's minimal, the place to start: a tracepoint on write(), a variable the loader sets, bpf_printk
kprobe Kprobe pair Port of libbpf-bootstrap's kprobe: kprobe and kretprobe on one function, per-architecture pt_regs, bpf_printk
uprobe Uprobe Port of libbpf-bootstrap's uprobe: uprobes on Go functions, Go's register ABI, return probes at each return instruction
fentry Fentry pair Port of libbpf-bootstrap's fentry: fentry and fexit on one function, typed BTF arguments, direct CO-RE reads
bootstrap Tracepoint pair Port of libbpf-bootstrap's bootstrap: process lifetimes, ring buffer, CO-RE, loader-set variable
execsnoop Tracepoint pair Port of libbpf-tools execsnoop: per-pid map state, argv capture, CO-RE pointer read
attach-types Syscall kprobe, kprobe, fentry, tracepoint, BTF tracepoint, raw tracepoint Port of learning-ebpf's chapter 7 hello: one exec through six attach types, typed and untyped arguments, verifier logs
profile Perf event Port of libbpf-bootstrap's profile: per-CPU sampling, kernel and user stacks, symbolization

Networking and policy

Example Hook type What it demonstrates
cgroup_skb Cgroup SKB Port of cilium/ebpf's cgroup_skb: egress packet count on the cgroup v2 root, array map, 64-bit atomic increment
xdp XDP Port of cilium/ebpf's xdp: per-source packet counts in an LRU hash map, direct packet access, atomic increments
sockfilter Socket filter Port of libbpf-bootstrap's sockfilter: raw packet socket, bpf_skb_load_bytes, ring buffer
tc TC Port of libbpf-bootstrap's tc: clsact qdisc and direct-action filter, direct packet access

Security

Example Hook type What it demonstrates
lsm LSM Port of libbpf-bootstrap's lsm: deny bpf(), earlier programs' verdict, program chosen per kernel

Scheduling

Example Hook type What it demonstrates
sched_ext struct_ops Port of cilium/ebpf's sched_ext: a struct_ops map declared with bpf.StructOps and attached through a link, an object with no programs
sched_ext_counter struct_ops A sched_ext scheduler with a Go running operation: a struct_ops program, its typed arguments, atomic counts
qumun struct_ops Port of qumun's BPF scheduler: a sched_ext scheduler with a userspace policy, ring buffers both ways, typed maps with kptrs and a timer, callbacks, optional kfuncs, kernel variables

Kfuncs

Example Hook type What it demonstrates
kfunc-task Fentry BTF-resolved kfunc calls (bpf_task_from_pid)

Build and run any example:

make example NAME=minimal
sudo ./examples/minimal/scripts/run.sh

Each example has its own README with attach/run details.

Library usage

Use tinybpf as a Go library to compile BPF objects programmatically:

result, err := tinybpf.Build(context.Background(), tinybpf.Request{
    Package: "./bpf",
    Output:  "probe.bpf.o",
})

See the Request documentation for all options.

How it compares

tinybpf bpf2go (cilium/ebpf) Aya (Rust)
BPF program language Go C Rust
Loader language Go Go Rust
CGo dependency No No No
Type-safe loader codegen Yes Yes Yes
Loader library cilium/ebpf cilium/ebpf aya
Maturity Experimental (0.x, solo-maintained) Mature, widely used in production Mature, widely used in production

tinybpf is for teams that want to write eBPF in Go instead of maintaining a C/Go split.

Documentation

Document Description
Toolchain Install Toolchain prerequisites and install
Writing Go for eBPF Language constraints, helpers, CO-RE, kfuncs
CLI Reference Every command, flag, and default
Config Reference tinybpf.json schema and merge rules
Troubleshooting Setup issues, pipeline errors, verifier debugging
Architecture Pipeline design and the 10-pass IR transformation
Project Layout Package map
Support Matrix Tested toolchain versions and platforms

License

MIT

About

Community fork for kyleseneker/tinybpf

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages