Mental model
Extending OS observability or packet routing traditionally forced choosing between slow user-space proxies (extra syscalls + context switches) and dangerous custom kernel modules (one null pointer dereference crashes the entire Linux host). eBPF (Extended Berkeley Packet Filter) solves both safely cleanly by compiling small C/Rust event hooks into restricted bytecode audited rigorously upfront by the Linux Kernel Verifier before JIT-compiling directly into native CPU machine code attached safely onto syscalls, sockets, kprobes, or userspace uprobes.
Learning outcomes
- Explain exact static safety guarantees enforced upfront by the Linux eBPF Verifier (bounded DAG/loops, pointer null/bounds checking, restricted helper APIs).
- Choose properly across
tracepoints(stable ABI kernel hooks),kprobes(arbitrary kernel internals),uprobes(userspace binaries like OpenSSLSSL_write), andXDP(ultra-fast NIC driver packet hooks). - Avoid verifier complexity explosions and context-switch overhead by aggregating histograms cleanly inside in-kernel BPF Maps / Ring Buffers (
bpf_ringbuf).
Theory
Why does eBPF unlock zero-change production visibility (Cilium CNI, Pixie, bpftrace, Parca) without restarting customer containers? Instead of attaching LD_PRELOAD wrappers or terminating TLS early inside user proxies, eBPF hooks run inside kernel execution context synchronously right when events trigger (TCP retransmits, cgroup OOMs, file opens, SSL plaintext buffer writes):
| Hook / Component | Execution Attachment Point | Stability & Performance Overhead | Recommended Production Use |
|---|---|---|---|
Kernel tracepoint / raw_tp | Fixed named static kernel probes (sched:sched_switch, syscalls:sys_enter_openat) | Highest stability (stable kernel ABI), lowest overhead | Continuous fleet telemetry cleanly across Linux upgrades |
Dynamic kprobe / fentry | Arbitrary internal kernel symbol entry/exit (fentry uses trampolines < 30ns!) | Fast (fentry beats legacy int3 kprobe cleanly); symbol names shift across kernels! | Kernel latency debugging & deep TCP/NVMe path forensics |
Userspace uprobe | Any userspace symbol (libssl.so:SSL_write, node, postgres C func) | Highest overhead (user -> kernel trap per call!) | Zero-code TLS plaintext debugging or occasional function tracing (avoid hot inner loops!) |
BPF Maps & Ring Buffer (BPF_MAP_TYPE_RINGBUF) | Shared kernel-to-user lock-free memory structures | Eliminates streaming millions of raw log strings to user space | Aggregate latency histograms in kernel; stream strictly filtered exceptions to daemon |
Crucially, CO-RE (Compile Once – Run Everywhere via BTF vmlinux.h) eliminates distributing bloated Clang/LLVM toolchains onto production Kubernetes nodes solely cleanly to recompile probes against specific kernel headers cleanly!
c/* Minimal CO-RE eBPF hook tracing slow disk / syscall latency via ktime + RingBuf concepts */#include <linux/bpf.h>#include <bpf/bpf_helpers.h>struct event_t {__u32 pid;__u64 latency_ns;};struct {__uint(type, BPF_MAP_TYPE_HASH);__uint(max_entries, 10240);__type(key, __u32);__type(value, __u64);} start_ts SEC(".maps");SEC("tracepoint/syscalls/sys_enter_fsync")int on_fsync_enter(void *ctx) {__u32 pid = (__u32)(bpf_get_current_pid_tgid() >> 32);__u64 ts = bpf_ktime_get_ns();bpf_map_update_elem(&start_ts, &pid, &ts, BPF_ANY);return 0;}
Trade-offs
Using eBPF cleanly replaces heavy user-space sidecars (iptables chains, Envoy hops for basic L3/L4 policies, intrusive APM language agents) with sub-microsecond kernel-native tracing and Cilium XDP packet forwarding cleanly across polyglot Rust/Go/Python/Node fleets identically. However, strict Verifier state-exploration bounds disallow dynamic recursion, arbitrary unbounded loops (must use bpf_loop or compile-time unrolled caps), and unverified pointer math—meaning complex filtering/aggregation belongs safely cleanly inside user-space collector daemons reading from shared eBPF Ring Buffers rather than crammed inside single mega-probes cleanly.
Failure modes and misconceptions
- Hitting eBPF Verifier Instruction / Loop Complexity Limits: Writing nested dynamic loops or deep pointer branches triggers
R0 !read_ok/processed 1000001 insns limitverifier rejections on load; split state across Maps or push heavy parsing to user space cleanly. - Attaching
uprobesonto Hot Inner Loops Called Millions of Times/Sec: Injecting userspace breakpoints onto tight allocator (malloc) or parsing loops incurs painful kernel trap context switches, multiplying request latency cleanly under load! - Relying on Un-Versioned
kprobeInternals WithoutBTF / CO-RE: Hardcoding struct offsets breaks cleanly whenever minor cloud Linux kernel patches roll across worker nodes. - Per-Event
bpf_trace_printkInside Production: Dumping text streams synchronously cleanly into globaltrace_pipestalls collectors cleanly instead of using shared per-CPU maps orBPF_MAP_TYPE_RINGBUF.
Decision scenario
A Kubernetes security & SRE platform wants to capture TCP SYN drops, monitor every SSL_write TLS call selectively on suspect outbound pods cleanly without modifying Go/Python binaries, and record fsync() stalls cleanly without node kernel panics! Deploy CO-RE BTF-backed kernel tracepoint histograms cleanly for fleet TCP/disk telemetry, use BPF_MAP_TYPE_RINGBUF cleanly to alert solely anomalous tail drops (> 50 ms), and restrict OpenSSL uprobes strictly onto targeted debug sessions cleanly rather than global hot paths.