Documentation Index
Fetch the complete documentation index at: https://mintlify.com/microsoft/eBPF-Event-Interceptor/llms.txt
Use this file to discover all available pages before exploring further.
Purpose
The TCP Event API provides a high-performance, low-overhead mechanism for monitoring TCP connection state changes and network statistics in real-time using eBPF (extended Berkeley Packet Filter) technology. The library intercepts kernel TCP state transitions and correlates them with process information to deliver comprehensive network telemetry.Library Information
Library Location:/opt/RealTimeKql/lib/libtcpEvent.so
Version: 1.03a (tcpTracer Ver 1.03e)
Dependencies:
- BCC (BPF Compiler Collection)
- pthread library
- Linux kernel with eBPF support
Header Files
To use the TCP Event API in your application, include the main header file:common.h- Data structure definitions (event_t, tcp_event_t, anu_tcp_info)
Basic Usage Workflow
1. Initialize the BPF Probe
AddProbe() function spawns a detached thread that:
- Initializes the BPF subsystem
- Attaches a kprobe to the
tcp_set_statekernel function - Opens a perf buffer for event collection
- Begins polling for TCP state change events
2. Wait for Initialization
3. Consume Events
DequeuePerfEvent() blocks until an event is available and returns the next TCP event from the internal queue.
4. Cleanup
Architecture
Event Collection Pipeline
- Kernel Probe: BPF probe attached to
tcp_set_state()captures TCP state transitions - Perf Buffer: Events flow through a perf ring buffer from kernel to userspace
- Event Queue: Userspace handler (
handle_output) adds events to a deque (max 1024 events) - Netlink Enrichment: Parallel thread uses netlink socket diagnostics to gather TCP statistics
- Consumer API:
DequeuePerfEvent()provides blocking access to enriched events
Thread Architecture
The library uses three concurrent threads:| Thread | Purpose | Created By |
|---|---|---|
| BPF Poller | Polls perf buffer for kernel events | AddProbe() / setupBPF() |
| Netlink Probe | Queries TCP socket statistics via netlink | DequeuePerfEvent() (lazy init) |
| Consumer Thread | Your application thread calling DequeuePerfEvent() | Your code |
Thread Safety
The library uses multiple synchronization primitives:pthread_mutex_t mtx: Protects the event deque during enqueue/dequeue operationspthread_cond_t cond: Signals waiting consumers when new events arrivepthread_mutex_t mapMu: Guards the pointer map for event memory managementpthread_rwlock_t rwlock: Protects the initialization status flag
Thread-Safe Operations
DequeuePerfEvent(): Thread-safe, uses mutex and condition variablegetStatus(): Thread-safe, uses read-write lockcleanup(): Safe to call from main thread, cancels background threads
Important Notes
Event Flow Control
Queue Overflow Behavior:- When queue size exceeds 1024, oldest event is dropped (FIFO)
- Dropped event memory is properly reclaimed
- Console warning:
"Shedding TCP events.."
- Netlink thread sleeps for 6.2 seconds (
NETLINKNAP) between polling cycles - If queue > 1024, netlink events are discarded with warning:
"Shedding TCP (Netlink) events.."
Supported Protocol Families
- AF_INET: IPv4 connections (addresses in SADDR/DADDR as dotted-quad strings)
- AF_INET6: IPv6 connections (addresses in SADDR/DADDR as colon-hex strings)
Kernel Compatibility
Requires Linux kernel with:- eBPF/BPF support (kernel 4.1+)
- Kprobe support
tcp_set_statesymbol (standard in modern kernels)- Netlink socket diagnostics (
NETLINK_SOCK_DIAG)
Performance Characteristics
- Overhead: Minimal kernel overhead (eBPF in-kernel filtering)
- Event Latency: Near real-time (microsecond-level timestamps)
- Memory: ~128KB per 1024 queued events
- CPU: Polling thread consumes minimal CPU when idle