From Kernel Internals to Production Systems — A Complete Guide with Working Code
Introduction: The Kernel Is Now Programmable
- The Promise of This Book
- Who This Book Is For
- How This Book Is Structured
- A Note on Code Examples
- Getting Started
Chapter 1: What Is eBPF and Why It Matters
- From Packet Filtering to Kernel Programming: A Brief History
- The eBPF Virtual Machine: Architecture at a Glance
- Why eBPF Changed Everything: Safety, Performance, and Flexibility
- The Modern eBPF Ecosystem: Tools, Languages, and Use Cases
- What You Will Build in This Book
- Chapter Summary
Chapter 2: Your First eBPF Program – Hello World
- Setting Up Your Development Environment
- The Anatomy of a Minimal eBPF Program
- Building and Loading: Clang, libbpf, and bpftool
- Reading the Output and Understanding the Lifecycle
- Common First-Time Pitfalls and How to Fix Them
- Chapter Summary
- Exercises
Chapter 3: The eBPF Instruction Set and Virtual Machine
- The eBPF Register File and Stack
- Instruction Categories: Arithmetic, Memory, Calls, Jumps, and Exits
- JIT Compilation: From eBPF Bytes to Native Code
- Reading Compiler Output: Understanding Generated eBPF Instructions
- Performance Characteristics of the eBPF VM
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 4: The Verifier – Safety Without Sandboxing
- What the Verifier Checks: Bounds, Types, and Control Flow
- State Machine Analysis and Path Exploration
- Reading Verification Logs and Decoding Errors
- Common Verification Failures and How to Fix Them
- Advanced Verifier Internals: Value Tracking and Scalar Ranges
- Unprivileged BPF: Security Model and Limitations
- Performance Implications of Verification
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 5: BPF Maps – The Data Layer
- Map Types Overview: Hash, Array, Per-CPU, LRU, Ringbuf, and More
- Creating and Using Maps from eBPF and User Space
- Advanced Map Patterns: Aggregation, Counters, and Histograms
- Map Performance: Sizing, Collisions, and Memory Overhead
- Complete Working Example: A Traffic Counter with Hash Maps
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 6: BPF Helper Functions – Talking to the Kernel
- How Helpers Work: The Calling Convention and ABI
- Time, Random, and Logging Helpers
- Networking Helpers: Socket Operations, Packet Manipulation, and Routing
- Tracing Helpers: Reading Kernel Memory, Registers, and Stack Traces
- Map Operation Helpers and User-Space Communication
- Complete Working Example: A Helper Function Playground
- Complete Working Example: Process Execution Monitor with Helpers
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 7: BPF Type Format (BTF) and CO-RE
- The Portability Problem: Kernel Version Fragmentation
- BTF Internals: Encoding Type Information in ELF Objects
- CO-RE Relocations: How libbpf Patches Programs at Load Time
- Writing CO-RE-Compatible Programs with libbpf Skeletons
- Debugging BTF and CO-RE Issues
- Split BTF: Out-of-Tree Modules and Distilled Base BTF
- struct_ops: Extending Kernel Subsystems with eBPF
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 8: Tracing with eBPF – kprobes, uprobes, and Tracepoints
- Kernel Probes (kprobes): Attaching to Any Kernel Function
- User-Space Probes (uprobes): Observing Applications Without Modification
- Tracepoints: Stable, Documented Kernel Instrumentation
- Raw Tracepoints and Fentry/Fexit for High-Performance Tracing
- Complete Working Project: A System Call Latency Analyzer
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 9: Networking with eBPF – XDP and TC Hooks
- The Linux Networking Stack and Where eBPF Fits
- XDP: Zero-Copy Packet Processing at Line Rate
- TC Hooks: Traffic Control and Classification
- Socket Filters: Application-Level Network Observability
- Complete Working Project: A DDoS Mitigator with XDP
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 10: Advanced Tracing – Perf Events, Ring Buffers, and cgroups
- Perf Event Arrays vs. Ring Buffers: When to Use Each
- High-Throughput Tracing with Ring Buffers
- cgroup-Based Attachment: Programs That Follow Workloads
- BPF Program Types Beyond Tracing and Networking
- Complete Working Project: A Container Resource Monitor
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 11: Security with eBPF – LSM, Audit, and Policy Enforcement
- BPF LSM: Making Security Policies Programmable
- File System Access Monitoring and Control
- Network Security: Connection Tracking and Policy Enforcement
- Audit Logging with eBPF
- Complete Working Project: A Runtime Intrusion Detection System
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 12: Performance Analysis, Debugging, and Profiling
- CPU Profiling with Hardware Counters and eBPF
- I/O Latency Analysis and Bottleneck Identification
- Lock Contention and Synchronization Debugging
- Memory Allocation Tracking and Leak Detection
- Complete Working Project: A Full-Stack Performance Dashboard
- Chapter Summary
- Exercises
- Chapter Summary
Chapter 13: bpftrace, bpftool, and the Developer Toolchain
- bpftool: Inspecting, Loading, and Managing eBPF Programs
- bpftrace: Rapid Prototyping with a Domain-Specific Language
- BCC Tools: The Classic eBPF Toolkit
- Building Custom Tools: When to Use libbpf vs. bpftrace
- Debugging Techniques: Verifier Logs, Crash Dumps, and Live Inspection
- Chapter Summary
Chapter 14: Production Systems – Containers, Kubernetes, and Real-World Patterns
- Unprivileged BPF and Security Tokens in Production
- eBPF in Container Environments: Namespaces, cgroups, and Isolation
- Kubernetes Integration: CNI, Observability, and Service Meshes
- Building Production Observability Pipelines
- Reliability, Scalability, and Operational Concerns
- Case Studies: Cilium, Pixie, Tetragon, and Others
- Container Runtime Hardening with eBPF
- Chapter Summary
Chapter 15: Where eBPF Is Headed – The Future
- Upcoming Kernel Features and Recent Developments
- Beyond Linux: eBPF on Windows, macOS, and Other Platforms
- User-Space eBPF (userspace_bpf) and New Frontiers
- Limitations and Open Research Questions
- Building Your eBPF Expertise: Learning Path and Resources
- Chapter Summary
Conclusion: The Programmable Kernel Era
Appendix A: Exercise Solutions
- Chapter 2 Solutions
- Chapter 3 Solutions
- Chapter 4 Solutions
- Chapter 5 Solutions
- Chapter 6 Solutions
- Chapter 7 Solutions
- Chapter 8 Solutions
- Chapter 9 Solutions
- Chapter 10 Solutions
- Chapter 11 Solutions
- Chapter 12 Solutions
Appendix B: Glossary
Appendix C: Quick Reference Tables
- C.1 BPF Helper Functions by Category
- C.2 BPF Map Types
- C.3 BPF Program Types and Attachment
- C.4 Kernel Version Feature Matrix
Appendix D: Index
- A
- B
- C
- D
- E
- F
- G
- H
- I
- J
- K
- L
- M
- N
- O
- P
- R
- S
- T
- U
- V
- X
