
eBPF trong Linux: Giám sát và networking hiệu năng không cần kernel module
eBPF (extended Berkeley Packet Filter) đang thay đổi cách nhà phát triển và system engineer tương tác với kernel Linux. Ban đầu dùng để lọc gói tin mạng, eBPF bây giờ cho phép chạy chương trình sandboxed trong kernel space một cách an toàn, mở ra khả năng giám sát, tracing, bảo mật và networking mà trước đây chỉ làm được với kernel module.
eBPF là gì và tại sao quan trọng?
eBPF là máy ảo trong kernel Linux thực thi bytecode được verify an toàn. Khác với kernel module truyền thống, eBPF program:
- Không cần recompile kernel hoặc reboot
- Được verify bởi kernel verifier trước khi load (tính an toàn bộ nhớ, vòng lặp vô tận, truy cập con trỏ)
- Có thể attach vào kprobe, uprobe, tracepoint, XDP, cgroup, socket và nhiều hook khác
- Chia sẻ dữ liệu với userspace qua BPF map (hash map, array, ring buffer, perf event array)
Cách thức này biến Linux kernel thành nền tảng programmable – bạn “lập trình kernel” mà không gặp rủi ro crash hệ thống.

Các use case chính của eBPF
1. Observability và Tracing
eBPF cho phép tracing hàm kernel và userspace với overhead gần bằng 0:
- bpftrace: Ngôn ngữ tracing cấp cao giống DTrace
- BCC (BPF Compiler Collection): Thư viện Python/C++ cho tools phức tạp
- Tracing syscall, function latency, cache miss, scheduler behavior
- USD (User Statically Defined Tracing) cho application-level instrumentation
- Custom kprobe for debugging specific kernel functions
2. Networking và XDP (eXpress Data Path)
XDP cho phép xử lý gói tin tại driver network card – trước khi vào kernel network stack:
- DDoS mitigation, load balancing (Cilium, Katran)
- Packet filtering, routing, tunneling (VXLAN, Geneve)
- Service mesh sidecar-less (Cilium)
- Kubernetes CNI performance cao
- Custom XDP programs for specific networking requirements

3. Security (Runtime Security)
eBPF monitor hành vi container/process real-time:
- Detect privilege escalation, file access bất thường, network connection đáng ngờ
- Tools: Falco, Tetragon, Tracee
- Enforce policy qua LSM (Linux Security Module) BPF
- System call filtering cho container sandboxing
- Custom LSM BPF for specialized security requirements
4. Performance Profiling
Continuous profiling với overhead thấp:
- CPU profiling, memory allocation tracking
- Off-CPU analysis (blocked threads)
- Tools: PySpy, Parca, Grafana Pyroscope eBPF integration
- Lock contention, scheduler latency analysis
- Memory leak detection with custom BPF programs
Tooling phổ biến
| Tool | Ngôn ngữ | Use case |
|---|---|---|
| bpftrace | bpftrace DSL | Ad-hoc tracing, one-liners |
| BCC | Python, C++ | Complex tools, dashboards |
| libbpf | C, Rust, Go | Production daemons, CO-RE (Compile Once Run Everywhere) |
| Cilium | Go + eBPF | Kubernetes networking, security, observability |
| Falco | C++ + Lua | Runtime security, threat detection |
CO-RE: Compile Once Run Everywhere
Vấn đề lớn của eBPF sớm là phụ thuộc vào kernel headers của máy target. CO-RE (BPF CO-RE) giải quyết bằng cách:
- Compile eBPF program với BTF (BPF Type Format) – metadata type information
- Runtime relocation dựa trên BTF của kernel target
- Một binary chạy trên mọi kernel version hỗ trợ BTF (Linux 5.2+)

Bắt đầu với eBPF
- Cài
bpftracehoặcbcctrên Ubuntu:sudo apt install bpftrace linux-headers-$(uname -r) - Chạy ví dụ đơn giản:
sudo bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %sn", comm, str(args->filename)); }' - Khám phá BCC tools có sẵn:
execsnoop,opensnoop,biolatency,cachestat - Học libbpf-bootstrap để viết production eBPF application
eBPF không chỉ là công cụ debugging – nó đang trở thành nền tảng infrastructure programmable cho cloud-native, security, và networking. Nếu bạn làm việc với Linux server, Kubernetes, hoặc distributed systems, eBPF là kỹ năng bắt buộc trong toolkit hiện đại. Để bắt đầu, hãy tham khảo eBPF tutorials tại eBPF Foundation và thực hành với các ví dụ trên GitHub. Cộng đồng eBPF rất năng động và luôn sẵn sàng hỗ trợ người mới thông qua Slack channels và GitHub discussions.
