tcpstates(8)
π¦ NAME π¦
okay so the manual name here is tcpstates(8) π and it's a tool that traces TCP session state changes with durations π₯. It's built with Linux eBPF/bcc, so it's giving low-level and I'm obsessed.
π SYNOPSIS π
tcpstates [-h] [-T] [-t] [-w] [-s] [-D PORTS] [-L PORTS] [-Y] [-4 | -6]
πΈ DESCRIPTION πΈ
okay so this tool traces TCP session state changes while it's running πβ¨, and it prints the details, including how long the session spent in each state. This is the move when you need to figure out why TCP connections are laggy π. Is the time going to the ESTABLISHED state (data transfer) π , or to the initialization state (SYN_SENT), or somewhere else? Iykyk.
This tool works using the sock:inet_sock_set_state tracepoint, which was added in Linux 4.16 π§. Linux 4.16 also added extra state transitions so that every TCP transition can be observed by this tracepoint. Main character energy π.
Only TCP state changes get traced, so the overhead is way lower than typical send/receive tracing. Lowkey genius π«Ά.
Since this uses BPF, only the root user can use this tool. π Don't even try it as a regular user bestie.
π§Έ REQUIREMENTS π§Έ
You'll need CONFIG_BPF and bcc, plus the sock:inet_sock_set_state tracepoint π₯.
β¨ OPTIONS β¨
-hβ Prints the usage message π.-sβ Gives you comma separated values output, so it's parseable π.-tβ Adds a timestamp column, in seconds β±οΈ.-Tβ Adds a time column, inHH:MM:SSformat π°οΈ.-wβ Wide column output, so IPv6 addresses actually fit π¦.-L PORTSβ A comma-separated list of local ports to trace. This gets filtered in-kernel, so it's fast π.-D PORTSβ A comma-separated list of destination ports to trace. Also filtered in-kernel π .-Yβ Logs session state changes to the systemd journal π.-4β Traces the IPv4 family only π.-6β Traces the IPv6 family only π.
π EXAMPLES π
Trace all TCP sessions, and show all state changes:
# tcpstates
Include a timestamp column, and wide column output:
# tcpstates -tw
Trace connections to local ports 80 and 81 only:
# tcpstates -L 80,81
Trace connections to remote port 80 only:
# tcpstates -D 80
Trace IPv4 family only:
# tcpstates -4
Trace IPv6 family only:
# tcpstates -6
π FIELDS π
TIMEβ Time of the change, inHH:MM:SSformat β°.TIME(s)β Time of the change, in seconds π.C-PIDβ The current on-CPU process ID. This may show the process that owns the TCP session if the state change runs in synchronous process context. Otherwise it's likely to show the kernel, since it was an asynchronous state change π€.C-COMMβ The current on-CPU process name. Same deal asC-PID: it may show the session's owner in synchronous process context, and otherwise it's likely the kernel (asynchronous state change) π.IPβ The IP address family, either 4 or 6 π.LADDRβ The local IP address π .RADDRβ The remote IP address π.LPORTβ The local port πͺ.RPORTβ The remote port πͺ.OLDSTATEβ The previous TCP state β¬ οΈ.NEWSTATEβ The new TCP state β‘οΈ.MSβ How long this state lasted, in milliseconds β³.
π· OVERHEAD π·
This traces the kernel TCP set state function, which should get called way less often than send/receive tracing, so the overhead should be lower π. The tool's overhead depends on the rate of new TCP sessions. If that's high (over 10,000 per second), there may be noticeable overhead, just from printing 10k lines of formatted output per second π.
You can find the rate of new TCP sessions using sar -n TCP 1, and by adding the active/s and passive/s columns π.
As always, test and understand this tool's overhead for your type of workload before using it in production π«Ά. No cap, don't skip this step.
π¦ SOURCE π¦
This is from bcc π:
https://github.com/iovisor/bcc
Also look in the bcc distribution for a companion _examples.txt file. It has example usage, output, and commentary for this tool π.
π OS π
Linux π§
π§ͺ STABILITY π§ͺ
Unstable - in development. Still cooking π©βπ³.
π©βπ» AUTHOR π©βπ»
Brendan Gregg π
π SEE ALSO π
tcpaccept(8), tcpconnect(8), tcptop(8), tcplife(8) π
Bestie, that's the whole tea β¨: tcpstates(8), user commands, dated 2018-03-20. Stay iconic and keep your sessions lowkey fast π ππ¦
π¦ where this page comes from
- alpine 3.24: bcc-doc 0.36.1-r1 Β· Apache-2.0
- nixpkgs unstable: bcc 0.37.0
- opensuse-leap 16.0: bcc-tools 0.35.0-160000.2.2 Β· Apache-2.0
- opensuse-leap 16.0: bcc-tools 0.35.0-160000.2.2 Β· Apache-2.0
- opensuse-leap 16.0: bcc-tools 0.35.0-160000.2.2 Β· Apache-2.0
- opensuse-leap 16.0: bcc-tools 0.35.0-160000.2.2 Β· Apache-2.0
- π girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms