Skip to main content
The TCP Event Interceptor captures TCP connection lifecycle events using eBPF kernel probes. It tracks connections from establishment to closure, recording network statistics including bytes transferred, segments sent/received, and connection metadata.

Overview

The TCP tracer attaches to the tcp_set_state kernel function to monitor TCP state transitions. When a connection closes (TCP_CLOSE state), it generates an event containing complete connection statistics.

Event Structure

Each TCP event contains the following fields:

Initializing the TCP Tracer

1

Load the shared library

Load the TCP event interceptor library using dlopen:
2

Resolve the AddProbe function

Get the AddProbe function to attach the BPF program:
3

Resolve the DequeuePerfEvent function

Get the event dequeue function:
4

Resolve additional functions

Get status checking and cleanup functions:
5

Attach the BPF probe

Call AddProbe with your BPF program:
The BPF program should attach to tcp_set_state and define the event structure matching the kernel’s TCP socket fields.
6

Wait for initialization

Wait for the tracer to be ready:

Complete Monitoring Example

Here’s a complete example based on the test implementation:

Interpreting Event Data

Network Statistics

rx_b

Total bytes received on the connection (from tcp_sock->bytes_received)

tx_b

Total bytes acknowledged/transmitted (from tcp_sock->bytes_acked)

tcpi_segs_out

Number of TCP segments sent (from tcp_sock->data_segs_out)

tcpi_segs_in

Number of TCP segments received (from tcp_sock->data_segs_in)

Process Information

  • pid: Process ID that owns the socket
  • UserId: User ID of the process
  • task: Process name (up to 128 characters)
The process information is captured at connection establishment or close. For long-lived connections, the process may have changed ownership.

Connection Endpoints

  • family: AF_INET (2) for IPv4, AF_INET6 (10) for IPv6
  • SADDR/SPT: Source IP address and port
  • DADDR/DPT: Destination IP address and port
Ports are in host byte order. The source port is from sk->__sk_common.skc_num, and the destination port is converted from network byte order using ntohs().

Timestamps

  • EventTime: Nanosecond timestamp when the connection closed
The timestamp is adjusted to account for system boot time, providing an absolute wall-clock time rather than a monotonic kernel time.

IPv4 and IPv6 Support

The tracer automatically handles both IPv4 and IPv6 connections:
Addresses are automatically converted to string format in the event structure. The TCP tracer includes netlink socket diagnostics support for enriching connection data. This provides additional TCP metrics beyond what’s available from the BPF hooks.
See common.h for the complete anu_tcp_info structure which mirrors the kernel’s TCP info structure with fields like RTT, retransmissions, congestion window, and more.

Cleanup and Shutdown

1

Setup signal handler

Register a signal handler for graceful shutdown:
2

Call cleanup function

The cleanup() function detaches all kprobes and releases BPF resources:
3

Close library handle

Close the dynamic library:

Example Output

When running the TCP tracer, you’ll see output like this:

Best Practices

Error Handling

Always check return values from dlsym() and handle errors appropriately.

Signal Handling

Implement proper signal handling to ensure cleanup is called before exit.

Event Processing

Events are queued internally. Process them promptly to avoid queue overflow.

Root Privileges

eBPF programs require root or CAP_BPF capabilities to load and attach.

Troubleshooting

Ensure the library is installed at /opt/RealTimeKql/lib/libtcpEvent.so. If installed elsewhere, update the SOFILE path.
eBPF requires elevated privileges. Run with sudo or grant CAP_BPF capability:
Verify the probe attached successfully by checking kernel logs:
Events are only generated when TCP connections close.
For short-lived connections, process information may be captured at different times. The tracer attempts to preserve the original process that established the connection.

Next Steps

UDP Monitoring

Learn how to monitor UDP traffic

Building from Source

Build and customize the interceptor

Testing

Run tests and verify functionality

API Reference

Detailed TCP API documentation