Overview
Event collection is the core function of the eBPF Event Interceptor. This page details how network events flow from kernel kprobes through perf buffers to user space queues, including data structures, attribution mechanisms, and performance optimizations.Both TCP and UDP interceptors follow similar collection patterns but differ in event structures and enrichment strategies.
Event Data Structures
TCP Event Structure
Defined intcpEvent/common.h:19-36:
Using
unsigned __int128 for addresses allows storing both IPv4 (32-bit) and IPv6 (128-bit) addresses in a single field.TCP Consumer Event Structure
Defined intcpEvent/common.h:39-54:
- IP addresses converted from binary to strings (e.g., “192.168.1.1”)
- Timestamp adjusted from boot time to epoch time
- Simplified fields relevant to consumers
UDP Event Structures
Defined inudpEvent/common.h:25-41:
Event Capture Flow
TCP Event Capture
UDP Event Capture
Kprobe Attachment and Event Generation
TCP Kprobe Implementation
The TCP interceptor uses a single kprobe ontcp_set_state (event.cc:187-194):
- Socket addresses and ports from
struct sock* - PID/UID from current process context
- Timestamp from
bpf_ktime_get_ns() - TCP state transition information
UDP Kprobe Implementation
The UDP interceptor attaches 9 kprobes to track stateless operations:UDP Kprobe Details
UDP Kprobe Details
1. Connection Tracking (2 probes):Called when application uses Capture bytes sent via Entry probe saves socket pointer, return probe captures bytes received.4. Cleanup (1 probe):Submits final statistics when socket is closed.
connect() on UDP socket (optional for UDP).2. Send Operations (2 probes):sendto() or send().3. Receive Operations (4 probes):Perf Buffer Mechanics
Buffer Declaration
TCP eBPF program:Opening Perf Buffer
From event.cc:196-203:handle_output callback is invoked for every event:
Polling Loop
From event.cc:216-219:poll_perf_buffer() internally uses epoll() to efficiently wait for events without busy-waiting.Event Queue Management
Queue Configuration
Both libraries usestd::deque with a maximum size:
Queue Protection
Multi-threaded access requires synchronization:Enqueue with Shedding
From event.cc:67-88:Dequeue with Blocking
From event.cc:113-169:DequeuePerfEvent() blocks indefinitely until an event is available. This design simplifies consumer code but requires careful shutdown handling.Netlink Socket Diagnostics (TCP)
TCP events are enriched with detailed statistics via Linux netlink socket diagnostics.Architecture
Netlink Probe Thread
From event.cc:293-309:Finding Socket Inodes
From event.cc:328-386:Requesting Socket Statistics
From event.cc:414-466:Parsing Netlink Responses
From event.cc:520-646:Netlink events are created independently of kprobe events, providing periodic snapshots of active connections even without state changes.
Custom TCP Info Structure
From common.h:75-148:The custom
anu_tcp_info struct extends standard tcp_info to include tcpi_bytes_sent, which is essential for accurate bandwidth accounting.Process Attribution Mechanism
Reading Process Command Line
From event.cc:661-681:eBPF Process Context
Inside eBPF programs, process information comes from BPF helpers:Command names from eBPF are limited to 16 characters (TASK_COMM_LEN). The netlink enrichment path reads full command lines from /proc.
Event Deduplication and Cleanup
TCP Memory Tracking
From event.cc:29-30:PtrMap prevents double-free errors:
UDP Socket Tracking
From udpTracer.cc:84-86 (in eBPF program):magic: Correlates entry and return probes for same syscallotherHash: Accumulates per-socket statistics across multiple operations
Cleanup on Socket Destruction
From udpTracer.cc:182-204 (eBPF program):When a UDP socket closes,
udp_destruct_sock is called, triggering final statistics submission and map cleanup.Performance Optimizations
Event Batching
Perf buffers naturally batch events for efficient transfer:- Kernel accumulates events in ring buffer
poll_perf_buffer()retrieves multiple events per syscall- Callback invoked once per event
Lock Granularity
- Event producers (kprobes + netlink)
- Event consumers (DequeuePerfEvent)
- Memory tracking (destroyEventPtr)
Zero-Copy Address Handling
Conditional netlink Polling
From event.cc:117-121:DequeuePerfEvent() call, avoiding overhead if never consumed.
Timestamp Synchronization
Boot Time Calculation
From event.cc:258-283:Timestamp Adjustment
From event.cc:128-133:bpf_ktime_get_ns() returns monotonic time since boot. Adding the boot epoch time converts to wall-clock time for consumer applications.Complete Event Flow Example
TCP Connection Event
UDP Send Event
Error Handling and Edge Cases
Short-Lived Processes
Problem: Process exits before /proc read Mitigation:IPv4 vs IPv6 Detection
Queue Overflow
Detection:MAXQSIZE or process events faster
Next Steps
Getting Started
Build and run the eBPF Event Interceptor
TCP API Reference
Learn how to consume TCP events in your application
UDP API Reference
Learn how to consume UDP events in your application