tcpconnect(8)

πŸ“¦ version 0.36.1, in alpine 3.24, nixpkgs unstable, opensuse-leap 16.0

πŸ’– tcpconnect(8) πŸ’–

Bestie, let me explain the tcpconnect tool πŸ¦‹ It's the manual page for the tcpconnect command, and it's from the Linux eBPF/bcc toolkit ✨


πŸ‘‘ NAME πŸ‘‘

tcpconnect is a tool that traces active TCP connections (connect()) πŸ”₯ It uses Linux eBPF/bcc, so it's giving low-level energy πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

tcpconnect [-h] [-c] [-t] [-p PID] [-P PORT] [-4 | -6] [-L] [-u UID]
[-U] [--cgroupmap MAPPATH] [--mntnsmap MAPPATH] [-d]

✨ DESCRIPTION ✨

okay so this tool traces active TCP connections, like the ones made with a connect() syscall πŸ’œ Passive ones from accept() don't count, they're the quiet ones πŸ™ˆ It's super useful for general troubleshooting when you want to see what connections the local server is starting 🧚

All connection attempts get traced, even the ones that ultimately fail πŸ’€ Tea spill: failed ones are still in the log πŸ“

Under the hood, it traces the kernel tcp_v4_connect() and tcp_v6_connect() functions with dynamic tracing. Heads up, it'll need updating if those functions ever change 😭

When you pass the -d or --dns option, the tool also tries to match each connect call with the most recent DNS query for the IP it connected to 🫢 It does this by tracing the kernel udp_recvmsg() function to collect DNS responses 🌸

⚠️ Since this uses BPF, only the root user can run this tool. Not you, not me, only root πŸ’€

🌈 REQUIREMENTS 🌈

You need CONFIG_BPF and bcc, no cap 🧠

If you're using the -d or --dns option, you also need the dnslib and cachetools python packages installed πŸŽ€ You can grab them with pip3, or with apt on Ubuntu 18.04+ using the python3-dnslib and python3-cachetools packages ✨


πŸ¦‹ OPTIONS πŸ¦‹

  • -h β€” Prints the usage message. Lowkey the most helpful one πŸ™ˆ
  • -t β€” Adds a timestamp column πŸ•°οΈ
  • -c β€” Counts connects per source IP and destination IP/port πŸ“Š
  • -p PID β€” Only traces this process ID. The filtering happens in-kernel, so it's efficient πŸ”₯
  • -P PORT β€” A comma-separated list of destination ports to trace. Filtering is in-kernel too 🎯
  • -4 β€” Traces the IPv4 family only 🌸
  • -6 β€” Traces the IPv6 family only 🌈
  • -L β€” Adds an LPORT column for the source port πŸ’…
  • -U β€” Adds a UID column 🫢
  • -u UID β€” Only traces this UID. Filtered in-kernel πŸ‘‘
  • --cgroupmap MAPPATH β€” Only traces cgroups in this BPF map. Filtered in-kernel 🧚
  • --mntnsmap MAPPATH β€” Only traces mount namespaces in this BPF map. Filtered in-kernel ✨
  • -d β€” Shows the most recent DNS query for the IP address in the connect call. It's probably related to the TCP connection details in the other columns, but it's not guaranteed, so don't bet your life on it πŸ™ˆ It works by tracing the udp_recvmsg kernel function and tracking DNS responses the server receives. It only supports UDP DNS packets up to 512 bytes in length. The Python code keeps a cache of 10k DNS responses in memory for up to 24 hours 🧠

If the time difference in milliseconds between when the system received a DNS response and when a connect syscall was traced using an IP from that response is greater than 100ms, the tool reports that delta after the query ⏱️ Most apps should have pretty short deltas. A long delay could be anomalous activity, or it could mean the DNS name requested got misattributed to the IP the connect syscall is using 😭

⚠️ The -d option may not be used with the count feature (option -c). Don't mix them, bestie πŸ’€

πŸ’– EXAMPLES πŸ’–

Trace all active TCP connections:

# tcpconnect

Trace all TCP connects, and include timestamps:

# tcpconnect -t

Trace all TCP connects, and include the most recent matching DNS query for each connected IP:

# tcpconnect -d

Trace PID 181 only:

# tcpconnect -p 181

Trace ports 80 and 81 only:

# tcpconnect -P 80,81

Trace IPv4 family only:

# tcpconnect -4

Trace IPv6 family only:

# tcpconnect -6

Trace all TCP connects, and include LPORT:

# tcpconnect -L

Trace all TCP connects, and include UID:

# tcpconnect -U

Trace UID 1000 only:

# tcpconnect -u 1000

Count connects per src ip and dest ip/port:

# tcpconnect -c

Trace a set of cgroups only (see special_filtering.md from bcc sources for more details):

# tcpconnect --cgroupmap /sys/fs/bpf/test01

Trace a set of mount namespaces only (see special_filtering.md from bcc sources for more details):

# tcpconnect --mntnsmap /sys/fs/bpf/mnt_ns_set

🧚 FIELDS 🧚

  • TIME(s) β€” The time of the call, in seconds ⏰
  • UID β€” User ID πŸͺͺ
  • PID β€” Process ID πŸ†”
  • COMM β€” Process name 🏷️
  • IP β€” IP address family (4 or 6) 🌐
  • SADDR β€” Source IP address πŸ“€
  • LPORT β€” Source port πŸšͺ
  • DADDR β€” Destination IP address πŸ“₯
  • DPORT β€” Destination port 🎯
  • CONNECTS β€” Accumulated active connections since start πŸ“ˆ
  • QUERY β€” Shows the most recent DNS query for the IP address in the connect call. It's likely related to the other TCP connection details, but not guaranteed πŸ”

πŸ”₯ OVERHEAD πŸ”₯

This tool traces the kernel tcp_v[46]_connect functions and prints output for each event. The rate is usually pretty low (under 1000/s), so the overhead should be negligible 🫧 But if you have an app making a high rate of connect()s, like a proxy server, test and understand the overhead before you use it πŸ™ˆ

If you're using the -d option to track DNS requests, this tool traces the udp_recvmsg function and generates an event for any packet from UDP port 53. Each event carries up to 512 bytes of the UDP packet payload. Most apps don't use UDP heavily, so the overhead of tracing udp_recvmsg should be negligible too. But if your app receives lots of UDP packets, test and understand the overhead of tracing every received UDP message πŸ‘€ Also, running this on a DNS server will have higher overhead than average, because every DNS response packet gets copied to userspace 😭


🌸 SOURCE 🌸

This is from bcc πŸ’–

https://github.com/iovisor/bcc

Also look in the bcc distribution for a companion _examples.txt file with example usage, output, and commentary for this tool πŸ“š


🌈 OS 🌈

Linux 🐧


πŸ§ͺ STABILITY πŸ§ͺ

Unstable, it's still in development 🚧


πŸ’œ AUTHOR πŸ’œ

Brendan Gregg πŸ‘‘


πŸ¦‹ SEE ALSO πŸ¦‹

tcptracer(8), tcpaccept(8), funccount(8), tcpdump(8) 🫢


Okay bestie, that's the whole tea β˜•πŸ’… This page is from the USER COMMANDS section, dated 2020-02-20, and it's tcpconnect(8) πŸŽ€ Stay iconic and go trace some connects πŸ¦‹βœ¨