eBPF Architecture Overview
ML Defender’s sniffer uses eBPF (Extended Berkeley Packet Filter) with XDP (eXpress Data Path) for high-performance packet capture at the kernel level.Why eBPF/XDP?
Performance
<1 μs packet processing latency
Efficiency
Zero-copy kernel → userspace transfer
Safety
Verified by kernel before loading
eBPF Program Structure
Source File
Location:sniffer/src/kernel/sniffer.bpf.c
From source/sniffer/src/kernel/sniffer.bpf.c:1-105:
BPF Maps
From source/sniffer/src/kernel/sniffer.bpf.c:114-149:XDP Packet Processing
Main XDP Hook
From source/sniffer/src/kernel/sniffer.bpf.c:193-300:Filter Logic
From source/sniffer/src/kernel/sniffer.bpf.c:153-174:512-Byte Payload Capture
Why 512 Bytes?
From source/TESTING.md:382-405:- PE Executable Detection: MZ header + DOS stub fits in first 128 bytes
- Ransomware Patterns: Ransom notes, .onion URLs, bitcoin addresses typically in first 512 bytes
- Crypto API Calls:
CryptEncrypt,CryptDecryptstrings detectable - Performance: 512 bytes = optimal balance (ring buffer size vs. analysis depth)
Payload Extraction
From source/sniffer/src/kernel/sniffer.bpf.c:250-280:The
#pragma unroll directive is critical for eBPF verifier approval. It ensures the loop is unrolled at compile time, allowing the verifier to prove bounded memory access.Ring Buffer Communication
Ring Buffer Architecture
Userspace Consumer
Location:sniffer/src/userspace/ring_consumer.cpp
Zero-Copy Benefits
Performance
No memcpy() from kernel to userspace
Latency
<1 μs kernel → userspace transfer
CPU Efficiency
Minimal CPU for data transfer
Memory
Shared memory between kernel/userspace
Performance Characteristics
Benchmark Results
From source/TESTING.md:272-296:Throughput Capacity
From source/TESTING.md:275-280:- Peak sustained: 82.35 events/second (17-hour test)
- Stress burst: 120 events/second
- Theoretical max: 1M+ packets/second (limited by network, not eBPF)
Memory Footprint
Dual-NIC Deployment
Interface Modes
From source/sniffer/src/kernel/sniffer.bpf.c:52-74:Configuration Example
sniffer.json
Compilation and Loading
eBPF Compilation
From source/sniffer/CMakeLists.txt:Loading eBPF Program
sniffer/src/main.cpp
Debugging eBPF Programs
Verify eBPF Object
Runtime Inspection
Trace Events
Limitations and Constraints
eBPF Verifier Constraints
Stack Size Limit: 512 bytes
Stack Size Limit: 512 bytes
eBPF programs have a limited stack. Use BPF maps for larger data structures.
Instruction Limit: 1M instructions
Instruction Limit: 1M instructions
Complex programs may exceed the verifier’s instruction limit. Use tail calls to chain programs.
No Unbounded Loops
No Unbounded Loops
All loops must be bounded and verifiable.
Pointer Arithmetic Restrictions
Pointer Arithmetic Restrictions
All pointer accesses must be bounds-checked before use.
Next Steps
ML Training
Train models for the 83-feature pipeline
Stress Testing
Validate eBPF performance under load
Performance Tuning
Optimize ring buffer and XDP settings
Troubleshooting
Debug eBPF loading and runtime issues