Skip to main content

Overview

The TCP Event API uses several data structures to represent network events and TCP statistics. These structures are defined in common.h and use #pragma pack(push, 1) to ensure consistent memory layout.

event_t (Internal)

Internal representation of TCP events as received from the BPF probe. This structure is not directly exposed to consumers but is transformed into tcp_event_t.
uint64_t
Event timestamp in nanoseconds (boot-relative, adjusted to epoch before delivery to consumer).Example: 1234567890123456789 (nanoseconds since boot)
uint64_t
Timestamp in microseconds (internal use).
uint32_t
Process ID of the process that owns the TCP connection.Example: 12345
uint32_t
User ID (UID) of the process owner.Example: 1000
unsigned __int128
Source IP address in binary form (128-bit to accommodate IPv6).
  • For IPv4: Only lower 32 bits are used
  • For IPv6: Full 128 bits contain the address
unsigned __int128
Destination IP address in binary form (128-bit to accommodate IPv6).
  • For IPv4: Only lower 32 bits are used
  • For IPv6: Full 128 bits contain the address
uint64_t
Total bytes received on this TCP connection.Example: 1048576 (1 MB)
uint64_t
Total bytes transmitted on this TCP connection.Example: 524288 (512 KB)
uint32_t
Total TCP segments sent (RFC4898 tcpEStatsPerfSegsOut).Example: 1024
uint32_t
Total TCP segments received (RFC4898 tcpEStatsPerfSegsIn).Example: 2048
uint64_t
Time span in microseconds (internal use).
uint16_t
Address family indicator:
  • AF_INET (2): IPv4
  • AF_INET6 (10): IPv6
Example: 2 (IPv4)
uint16_t
Source port number.Example: 443 (HTTPS)
uint16_t
Destination port number.Example: 52341 (ephemeral client port)
char[128]
Process command name (null-terminated string, max 128 bytes including null).Example: "nginx", "python3"

tcp_event_t (Consumer-Facing)

The structure returned by DequeuePerfEvent() to application code. This is the primary data structure for consuming TCP events.
uint64_t
Event timestamp in nanoseconds since Unix epoch (adjusted from boot-relative time).Example: 1678901234567890123 (nanoseconds since Jan 1, 1970)Conversion: To seconds: EventTime / 1000000000
uint32_t
Process ID of the process that owns the TCP connection.Example: 12345
uint32_t
User ID (UID) of the process owner.Example: 1000 (regular user), 0 (root)
uint64_t
Total bytes received on this TCP connection (cumulative).Example: 1048576 (1 MiB)
uint64_t
Total bytes transmitted on this TCP connection (cumulative).Example: 524288 (512 KiB)
uint32_t
Total TCP segments sent (RFC4898 tcpEStatsPerfSegsOut).Example: 1024
uint32_t
Total TCP segments received (RFC4898 tcpEStatsPerfSegsIn).Example: 2048
uint16_t
Address family:
  • AF_INET (2): IPv4 connection
  • AF_INET6 (10): IPv6 connection
Example: 2
uint16_t
Source port number (host byte order).Example: 443, 80, 22
uint16_t
Destination port number (host byte order).Example: 52341 (ephemeral port)
char[128]
Process command name (null-terminated string).Example: "nginx", "postgres", "python3"
char[128]
Source IP address as a human-readable string.
  • IPv4: Dotted-quad notation
  • IPv6: Colon-hexadecimal notation
Examples:
  • "192.168.1.100" (IPv4)
  • "2001:db8::1" (IPv6)
char[128]
Destination IP address as a human-readable string.
  • IPv4: Dotted-quad notation
  • IPv6: Colon-hexadecimal notation
Examples:
  • "10.0.0.5" (IPv4)
  • "fe80::a00:27ff:fe4e:66a1" (IPv6 link-local)

Usage Example

Output:

anu_tcp_info

Comprehensive TCP connection statistics structure obtained via netlink socket diagnostics. Contains extensive metrics about TCP connection state, performance, and behavior.

Usage Note

The anu_tcp_info structure is populated from netlink socket diagnostics (INET_DIAG_INFO). Fields like tcpi_bytes_sent and tcpi_bytes_received are extracted and copied to the tcp_event_t.tx_b and tcp_event_t.rx_b fields respectively.

TCP State Enum

TCP connection states as defined in the Linux kernel.
1
Connection is established and data can flow.
2
Client sent SYN, waiting for SYN-ACK.
3
Server received SYN, sent SYN-ACK, waiting for ACK.
4
Connection closing, FIN sent, waiting for ACK.
5
FIN acknowledged, waiting for peer’s FIN.
6
Connection closed, waiting for delayed packets.
7
Connection is closed.
8
Peer closed connection, waiting for local close.
9
Waiting for final ACK after sending FIN.
10
Socket is listening for incoming connections.
11
Both sides closing simultaneously.
12
New SYN received (SYN cookies).
13
Sentinel value (total number of states).

State Filtering

The library monitors TCP connections in specific states. The netlink probe filters for:
Monitored States: All except SYN_RECV, TIME_WAIT, and CLOSE Rationale: Focuses on active connections with meaningful data transfer, excluding transient handshake and teardown states.

Memory Layout

All structures use #pragma pack(push, 1) to ensure no padding:
This guarantees consistent memory layout across different compilers and architectures, critical for:
  • Binary compatibility with BPF programs
  • Interoperability with kernel netlink structures
  • Serialization/deserialization
Due to packed structures, accessing fields may be slower on architectures requiring aligned access. This is acceptable for the relatively low frequency of TCP state change events.