Overview
This guide walks you through creating your first network monitoring application using eBPF Event Interceptor. You’ll learn how to:- Load the TCP and UDP interceptor libraries
- Define custom eBPF programs
- Capture and process network events in real-time
All examples require root privileges to load eBPF programs into the kernel.
TCP Event Monitoring
Monitor TCP connections using thelibtcpEvent.so library.
Basic TCP Monitor
Define your eBPF program
Write the kernel-side eBPF code that captures TCP state changes:
This eBPF program attaches to the
tcp_set_state kernel function and captures TCP connection events when connections close. It tracks connection lifetime, process information, and traffic statistics.Build and Run
Compile your TCP monitor:Expected Output
When TCP connections close on your system, you’ll see events like:UDP Event Monitoring
Monitor UDP traffic using thelibudpEvent.so library.
Basic UDP Monitor
Build and Run
Expected Output
Key API Functions
Both libraries expose the same core API via dynamic loading:| Function | TCP Signature | UDP Signature | Description |
|---|---|---|---|
AddProbe | void AddProbe(const char *bpf_code) | void AddProbe() | Load eBPF program into kernel |
DequeuePerfEvent | struct tcp_event_t DequeuePerfEvent() | struct udp_event_t DequeuePerfEvent() | Read next event (blocking) |
getStatus | unsigned getStatus() | unsigned getStatus() | Check if probe is ready (1=ready) |
cleanup | void cleanup() | void cleanup() | Detach probe and free resources |
Understanding the Event Structures
TCP Event Fields
UDP Event Fields
Best Practices
Error Handling
Always check return values from
dlopen() and dlsym(). Use dlerror() for detailed error messages.Signal Handling
Implement signal handlers to call
cleanup() on SIGINT/SIGTERM to properly detach probes.Resource Management
Call
dlclose() before exiting to release library resources.Privileges
Run with
sudo or set appropriate capabilities (CAP_BPF, CAP_PERFMON).Next Steps
- Customize eBPF Programs: Modify the eBPF code to filter specific connections or capture additional metrics
- Integrate with Observability: Send events to your telemetry pipeline (Prometheus, Kafka, etc.)
- Performance Tuning: Adjust BPF map sizes and filtering logic for high-traffic environments
- Security Monitoring: Build real-time alerts for suspicious network patterns
For production deployments, consider implementing event buffering, rate limiting, and log rotation to handle high-volume network environments.