Skip to main content
eBPF - Safely extend the kernel at runtime

eBPF Deep Dive

eBPF (extended Berkeley Packet Filter) is the technology revolutionizing observability, networking, and security in Linux. Companies like Datadog, Grafana, and Cloudflare use eBPF extensively. Mastering it is essential for infrastructure and observability engineering roles.
Interview Frequency: Very High (core skill for observability roles)
Key Topics: BPF architecture, program types, maps, verifier, bpftrace
Time to Master: 18-20 hours

What is eBPF?

eBPF allows running sandboxed programs in the Linux kernel without changing kernel source code or loading kernel modules. eBPF Lifecycle

eBPF Program Types

Different attach points for different use cases:

Core Program Types

Kprobes vs Tracepoints

Available Tracepoints


BPF Maps

Maps are key-value stores shared between eBPF programs and user space.

Map Types

Map Declaration (libbpf style)

Ring Buffer vs Perf Buffer


The BPF Verifier

The verifier ensures eBPF programs are safe to run in the kernel.

Verifier Checks

Common Verifier Errors

Verifier Debugging


Helper Functions

eBPF programs can call kernel-provided helper functions.

Common Helpers

Available Helpers Per Program Type


BPF CO-RE (Compile Once, Run Everywhere)

CO-RE solves the problem of kernel version differences.

CO-RE Code Example


bpftrace - High-Level Tracing

bpftrace is the easiest way to write eBPF programs.

bpftrace Basics

bpftrace One-Liners for Observability

bpftrace Variables


Production eBPF Tools

BCC (BPF Compiler Collection)

libbpf-based Tools


XDP (eXpress Data Path)

XDP allows packet processing at the network driver level.

XDP Program

XDP Return Codes


Lab Exercises

Objective: Write basic tracing scripts
Objective: Create a complete eBPF program with libbpf
Build with:
Objective: Profile production workloads

Interview Questions

Answer:The verifier ensures eBPF programs are safe to run in kernel context.Why necessary:
  • eBPF runs with kernel privileges
  • Bugs could crash the kernel or leak data
  • Must guarantee termination (no infinite loops)
  • Must prevent out-of-bounds memory access
Key checks:
  1. Control flow: No unbounded loops, reachable exit
  2. Memory safety: All accesses bounds-checked
  3. Type safety: Correct helper function arguments
  4. Privilege: Capability checks for dangerous operations
Limitations:
  • Some valid programs rejected (false negatives)
  • Complex loop bounds hard to prove
  • Instruction count limits
Answer:Best practices:
  • Use tracepoints when available (stable, efficient)
  • Use kprobes for specific functions not covered
  • CO-RE helps with kprobe portability
  • Document kprobe usage for maintenance
Answer:Approach:
  1. Identify entry/exit points:
  1. Break down latency:
  • Trace syscalls (read, write, connect)
  • Trace specific functions (DB queries, cache lookups)
  • Measure off-CPU time (blocking)
  1. Identify bottlenecks:
  1. Production-safe approach:
  • Start with low-overhead tracepoints
  • Sample rather than trace all events
  • Use ring buffer for event collection
  • Set reasonable map sizes
Answer:What is XDP:
  • Runs eBPF at network driver level
  • Before sk_buff allocation (very early)
  • Near-native speed packet processing
Use cases:
  • DDoS mitigation (drop malicious packets)
  • Load balancing (Facebook’s Katran)
  • Packet filtering (Cloudflare)
  • Traffic steering
Performance:
  • Can process 10M+ packets per second per core
  • 10-100x faster than iptables for simple rules
Limitations:
  • Limited packet modification capabilities
  • Driver must support XDP
  • Complex protocols need network stack
Comparison with TC:
  • XDP: Earlier, faster, limited
  • TC: After sk_buff, full networking features

Key Takeaways

eBPF Safety

Verifier ensures programs are safe before running in kernel

Maps for Data

BPF maps enable data sharing between kernel and user space

CO-RE Portability

Compile once, run everywhere with BTF and libbpf

bpftrace Power

High-level scripting for quick observability tasks

Interview Deep-Dive

Strong Answer:
  • I would use two tracepoint programs: tracepoint/block/block_rq_issue to record when a block I/O request is dispatched to the device driver, and tracepoint/block/block_rq_complete to record when it completes. On issue, I would store the timestamp keyed by (dev, sector) in a BPF hash map. On completion, I would look up the start time, compute the delta, and emit a latency event.
  • For per-container attribution, I would use bpf_get_current_cgroup_id() at the issue tracepoint to capture the cgroup ID of the process that initiated the I/O. I would store this alongside the timestamp in the hash map, so the completion handler can attribute the latency to the correct container even though the completion runs in interrupt context (where the “current” task is arbitrary).
  • For efficient data export, I would use a BPF_MAP_TYPE_PERCPU_HASH map keyed by (cgroup_id, latency_bucket) to build a per-container latency histogram directly in kernel space. The user-space agent reads this map periodically (every 5-10 seconds), aggregates across CPUs, and exports to Prometheus. This approach avoids per-event ring buffer overhead.
  • For production safety: bounded map sizes (10240 entries), PERCPU maps to avoid lock contention, and the programs attach to stable tracepoints (not kprobes) for kernel version compatibility.
Follow-up: What happens if the hash map fills up because I/O requests are issued faster than they complete?Follow-up Answer:
  • If the (dev, sector) tracking map fills up, bpf_map_update_elem() returns -ENOSPC and the entry is silently dropped. The corresponding completion event will not find a matching start timestamp and will skip that I/O. This means we lose visibility into some requests during extreme load, but the program remains safe and does not crash or block I/O. To mitigate, I would size the map based on the expected maximum I/O queue depth across all devices (for NVMe with 64K queue depth per queue, this could be large). I would also add a per-CPU counter for dropped entries so the monitoring system can alert when we are losing data.
Strong Answer:
  • The BPF verifier is a static analyzer that runs at program load time (before any execution) and simulates every possible execution path through the program. It maintains a state machine tracking the type, value range, and liveness of each register and stack slot at every instruction.
  • Key invariants enforced: First, termination — every loop must have a provable upper bound. The verifier tracks loop iterations and rejects programs where it cannot prove the loop exits within a bounded number of iterations. Second, memory safety — every pointer dereference must be preceded by a bounds check. If bpf_map_lookup_elem() returns a pointer, the verifier marks it as “possibly NULL” and requires an explicit NULL check before dereferencing. Third, type safety — the verifier tracks which registers contain pointers to map values, packet data, stack, or scalars, and ensures they are used correctly (you cannot pass a packet pointer where a map pointer is expected). Fourth, privilege — certain helper functions require CAP_BPF or CAP_SYS_ADMIN.
  • A real rejection scenario: you write a program that iterates over a linked list of variable length in packet data. Even if you add if (i >= MAX_ITERATIONS) break;, the verifier might reject it because the packet pointer arithmetic creates too many possible states. The verifier explores states exponentially, and complex pointer arithmetic with conditional branches can exceed the instruction count limit (1 million verified instructions). The fix is to restructure the code to reduce branching complexity, use bpf_loop() helper (kernel 5.17+), or split the logic into multiple tail-called programs.
Follow-up: How does BPF CO-RE (Compile Once, Run Everywhere) interact with the verifier to enable cross-kernel portability?Follow-up Answer:
  • CO-RE works by embedding relocation information in the compiled BPF program’s ELF file. When you use BPF_CORE_READ(task, pid), the compiler emits a relocation record saying “access field pid at offset X in struct task_struct.” At load time, libbpf reads the running kernel’s BTF (BPF Type Format) data to find the actual offset of pid in the current kernel’s task_struct, which may differ from the compile-time offset. libbpf patches the BPF instructions to use the correct offset before submitting to the verifier. The verifier then sees a program with correct offsets for the running kernel and validates it normally. If the field does not exist at all (e.g., a field removed in a newer kernel), libbpf can detect this and handle it gracefully (returning a default value or disabling that part of the program).
Strong Answer:
  • XDP (eXpress Data Path) programs run at the earliest possible point in the network receive path, before the kernel allocates an sk_buff structure. They operate on raw xdp_md contexts with direct packet data access. TC BPF programs run later, after sk_buff allocation, at the traffic control layer in both ingress and egress paths.
  • Performance difference is significant: XDP can process 10-20 million packets per second per core because it avoids the overhead of sk_buff allocation (~200-300 cycles per packet). TC processes 2-5 million packets per second per core, which is still much faster than iptables but slower than XDP.
  • I would choose XDP for: DDoS mitigation (drop malicious packets before they consume memory), load balancing (redirect packets to different NICs or CPUs), and simple packet filtering (firewalling at line rate). Facebook’s Katran load balancer and Cloudflare’s DDoS mitigation use XDP.
  • I would choose TC for: more complex packet manipulation (full sk_buff available with all parsed headers), egress path processing (XDP only works on ingress), container networking (Cilium uses TC BPF for pod-to-pod traffic because it needs access to socket-level metadata), and when compatibility with the full networking stack is needed (TC programs can interact with connection tracking, netfilter marks, etc.).
  • XDP limitations: cannot modify packets that need fragmentation (no access to GSO), cannot directly interact with the socket layer, requires NIC driver support for native mode (falls back to generic/slower mode otherwise).
Follow-up: How does AF_XDP enable zero-copy packet processing, and when would you use it instead of regular XDP?Follow-up Answer:
  • AF_XDP is a socket type that works with XDP to deliver raw packets directly to user space without kernel copying. An XDP program uses XDP_REDIRECT with bpf_redirect_map() to send packets to an XSKMAP (XDP socket map). The user-space application creates an AF_XDP socket with shared UMEM (user memory), where packet data is DMA’d directly from the NIC into user-space-accessible memory. This achieves true zero-copy: the packet data is never copied by the kernel. I would use AF_XDP when the application needs to process every packet (not just filter/drop), such as custom protocol implementations, high-frequency trading network stacks, or DPDK-like applications that want kernel bypass without the complexity of a full DPDK setup. The trade-off versus pure XDP is that AF_XDP requires user-space processing latency, while XDP programs run to completion in the kernel.

Next: Tracing Infrastructure →