Ebpf engineering

From j4flmao/agent-skills · View interactive version

Ebpf engineering is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about eBPF programs, libbpf, CO-RE, BTF, kprobes, fentry, tracepoints, uprobes, XDP to drop packets, tc BPF, BPF maps, ring buffers, verifier errors, bpftrace, cilium/ebpf or aya. Produces loadable eBPF programs and loaders. Not for Cilium cluster setup (use cilium-ebpf) or de

How to use this skill

Skill file

eBPF Engineering

Purpose

eBPF runs verified, sandboxed programs inside the Linux kernel at kernel events (function entry, tracepoints, packets, syscalls, LSM hooks) without writing a kernel module. This skill covers choosing the hook, writing CO-RE programs that load on many kernels, passing data through maps, satisfying the verifier, and shipping a loader.

Agent Protocol

Trigger

"eBPF", "libbpf", "CO-RE", "BTF", "kprobe", "fentry", "tracepoint", "uprobe", "XDP", "tc BPF", "BPF map", "ring buffer", "verifier", "bpftrace", "cilium/ebpf", "aya".

Input Context

Output Artifact

BPF C program, loader code, build commands, verifier-error diagnosis. No file unless requested.

Response Format

Min kernel: {version} (BTF: yes/no)
Hook: {program type + attach point}
Maps: {name | type | key -> value | max_entries}
BPF code: {snippet}
Loader: {snippet / skeleton usage}
Verifier notes: {bounds checks, loops, stack}

1. Model

2. Choosing the Hook

| Need | Program type | Notes |

|------|-------------|-------|

| Trace a kernel function | fentry/fexit (BPF trampoline, ≥ 5.5) | faster than kprobes, typed args via BTF |

| Trace any kernel function on older kernels | kprobe/kretprobe | unstable: functions get inlined/renamed |

| Stable kernel events | tracepoints / tp_btf raw tracepoints | prefer over kprobes when one exists |

| User-space functions | uprobe/uretprobe, USDT | e.g. TLS library calls; per-process overhead on each hit |

| Packet processing at the driver | XDP | earliest hook; XDP_DROP/PASS/TX/REDIRECT; see references/xdp-networking.md |

| Packet processing with skb metadata | tc (BPF_PROG_TYPE_SCHED_CLS, tcx links ≥ 6.6) | ingress + egress |

| Security policy | BPF LSM (lsm/ hooks, ≥ 5.7, lsm=...,bpf boot param) | can deny operations |

| Per-cgroup socket/network control | cgroup programs | connect, bind, sockops |

3. CO-RE Program Example

// execsnoop.bpf.c — log process execs via a stable tracepoint
#include "vmlinux.h"                  // bpftool btf dump file /sys/kernel/btf/vmlinux format c
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>

struct event { u32 pid; u32 ppid; char comm[16]; };

struct {
    __uint(type, BPF_MAP_TYPE_RINGBUF);
    __uint(max_entries, 256 * 1024);   // bytes, power of two, multiple of page size
} events SEC(".maps");

SEC("tp/sched/sched_process_exec")
int handle_exec(struct trace_event_raw_sched_process_exec *ctx)
{
    struct event *e = bpf_ringbuf_reserve(&events, sizeof(*e), 0);
    if (!e)
        return 0;                                   // buffer full: drop, count it in a map
    struct task_struct *task = (struct task_struct *)bpf_get_current_task();
    e->pid  = bpf_get_current_pid_tgid() >> 32;
    e->ppid = BPF_CORE_READ(task, real_parent, tgid);  // CO-RE relocated field access
    bpf_get_current_comm(e->comm, sizeof(e->comm));
    bpf_ringbuf_submit(e, 0);
    return 0;
}

char LICENSE[] SEC("license") = "GPL";

Build and load with a skeleton:

clang -O2 -g -target bpf -D__TARGET_ARCH_x86 -c execsnoop.bpf.c -o execsnoop.bpf.o
bpftool gen skeleton execsnoop.bpf.o > execsnoop.skel.h
struct execsnoop_bpf *skel = execsnoop_bpf__open_and_load();
if (!skel) { fprintf(stderr, "load failed: %s\n", strerror(errno)); return 1; }
if (execsnoop_bpf__attach(skel)) { /* handle error */ }
struct ring_buffer *rb = ring_buffer__new(bpf_map__fd(skel->maps.events), on_event, NULL, NULL);
while (!stop) ring_buffer__poll(rb, 100 /* ms */);

4. Maps

| Type | Use |

|------|-----|

| HASH / LRU_HASH | per-key state (per-PID, per-flow); LRU evicts under pressure |

| ARRAY / PERCPU_ARRAY | counters, config, scratch buffers (per-CPU avoids atomics and the 512-byte stack limit) |

| PERCPU_HASH | high-rate counters without contention |

| RINGBUF (≥ 5.8) | events to user space, ordered across CPUs, shared buffer — prefer over PERF_EVENT_ARRAY |

| LPM_TRIE | IP prefix matching |

| PROG_ARRAY | tail calls |

| DEVMAP / CPUMAP / XSKMAP | XDP redirect targets / AF_XDP |

5. Verifier Survival Guide

struct ethhdr *eth = (void *)(long)ctx->data;
if ((void *)(eth + 1) > (void *)(long)ctx->data_end)
    return XDP_PASS;

6. Quick Tracing with bpftrace

bpftrace -e 'tracepoint:syscalls:sys_enter_openat { @[comm] = count(); }'
bpftrace -e 'kprobe:vfs_read { @start[tid] = nsecs; } kretprobe:vfs_read /@start[tid]/ { @us = hist((nsecs - @start[tid]) / 1000); delete(@start[tid]); }'

Use bpftrace for investigation; write libbpf/CO-RE programs for anything that ships.

7. Production Concerns

8. Anti-Patterns

9. Checklist

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.