My most memorable on-call shift involved an intermittent packet loss issue on our API gateway cluster that only surfaced during peak traffic at 15,000 req/s. Running tcpdump to capture all packets caused disk I/O and CPU usage to spike, making the bottleneck even worse. Running ss only gave point-in-time snapshots, making it impossible to catch short-lived connections or packet retransmissions as they happened. After that incident, I switched to bcc/eBPF to observe the Linux kernel directly in real time with less than 1–2% CPU overhead.
Diagnose TCP Issues in 5 Minutes with bcc-tools
On Ubuntu/Debian or RHEL/Rocky Linux, you just need to install the bcc-tools (or bpfcc-tools) package along with the kernel headers matching your current kernel version.
# On Ubuntu / Debian
sudo apt update
sudo apt install -y bpfcc-tools linux-headers-$(uname -r)
# On RHEL / Rocky Linux / AlmaLinux
sudo dnf install -y bcc-tools kernel-devel-$(uname -r)
One common gotcha on Ubuntu/Debian is that the command names have a -bpfcc suffix (such as tcplife-bpfcc). If you prefer using the original names without the suffix, you can run the scripts directly from /usr/share/bcc/tools/. On RHEL/Rocky, the toolset is also located in that same directory.
Once installed, try running these three essential commands:
# 1. See which processes are actively opening outbound TCP connections
sudo tcpconnect-bpfcc
# 2. Trace TCP connection lifespans (duration, TX/RX traffic)
sudo tcplife-bpfcc
# 3. Capture TCP packet retransmissions in real time
sudo tcpretrans-bpfcc
Here is real output from tcplife-bpfcc on a node running Nginx and Redis:
PID COMM LADDR LPORT RADDR RPORT TX_KB RX_KB MS
14203 nginx 10.0.1.15 48912 10.0.1.22 6379 1 4 0.85
14205 python3 10.0.1.15 51204 172.16.0.8 443 2 18 142.30
The output shows that the python3 process (PID 14205) connected to 172.16.0.8:443, sent 2 KB, received 18 KB, and closed the connection after 142.30 ms. Clean and concise.
How bcc/eBPF Works and How to Read the Metrics
Traditional tcpdump copies every packet from kernel space to user space before filtering it, which burns a lot of CPU when traffic reaches tens of thousands of packets per second. By contrast, eBPF runs verified bytecode directly inside the kernel via hooks such as kprobes and tracepoints. Meanwhile, bcc (BPF Compiler Collection) compiles and loads those eBPF programs using Python or C.
All counting and aggregation happen inside the kernel, and only the summarized results are sent to your terminal. The three tool groups below are the first things I open whenever I’m on call.
1. Tracing Connection Lifecycles: tcpconnect, tcpaccept, and tcplife
- tcpconnect: Hooks into
tcp_v4_connectandtcp_v6_connectto log every outbound connection. It helps you quickly spot connection leaks or apps reaching out to unexpected IP addresses. - tcpaccept: Traces inbound connections as soon as the kernel accepts them.
- tcplife: Prints a single line when a connection closes, including
TX_KB,RX_KB, and lifespan inMS. Seeing a service open thousands of database connections per minute that each live for only 1–2 ms? Chances are the code is missing connection pooling.
2. Measuring Handshake Latency and RTT with tcpconnlat & tcprtt
When CPU and RAM still have plenty of headroom but users keep reporting a slow site, check latency at the TCP layer right away:
# Print only connections taking longer than 50ms to complete the TCP handshake
sudo tcpconnlat-bpfcc 50
# Print a Round-Trip Time (RTT) distribution histogram every 5 seconds for 15 seconds
sudo tcprtt-bpfcc -i 5 -d 15
tcprtt outputs an ASCII histogram like this:
msecs : count distribution
0 -> 1 : 1240 |****************************************|
2 -> 3 : 310 |********** |
4 -> 7 : 45 |* |
8 -> 15 : 12 | |
16 -> 31 : 89 |** |
Most packets fall under 1 ms, meaning the internal LAN is completely healthy. However, that cluster of 89 packets spiking to 16–31 ms is a clear sign of a micro-burst or localized congestion on a network segment.
3. Catching Packet Loss Red-Handed with tcpretrans
Going back to the packet loss incident at the start: whenever a packet is dropped in transit, the Linux TCP stack retransmits it. tcpretrans-bpfcc attaches directly to the tcp:tcp_retransmit_skb tracepoint to report retransmissions as they happen:
sudo tcpretrans-bpfcc -c
The -c flag aggregates total retransmission counts per IP/Port pair instead of flooding your terminal. By correlating spikes in tcpretrans with your application logs, you can immediately pinpoint which node is dropping packets and in which direction.
Filtering Noise and Writing Custom bcc Python Scripts
Production servers typically run dozens of containers side by side, so running these commands unfiltered will scroll past your screen fast. Use flags to filter by PID, port, or destination IP:
# Trace only outbound connections from PID 2841
sudo tcpconnect-bpfcc -p 2841
# Measure handshake latency only to ports 3306 (MySQL) and 6379 (Redis)
sudo tcpconnlat-bpfcc -P 3306,6379
# Measure RTT specifically to the payment gateway IP for 10 seconds
sudo tcprtt-bpfcc -a 203.0.113.50 -i 10 -d 10
Sometimes I need a quick count of retransmissions by destination port to see which service (like Redis on 6379 or an external API on 443) is having trouble. One small caveat: the tcp:tcp_retransmit_skb tracepoint often runs in timer softirq context, so bpf_get_current_pid_tgid() won’t reflect the actual application PID. Instead, we can read the dport field directly from the tracepoint arguments in about 25 lines of Python using the bcc library:
#!/usr/bin/env python3
from time import sleep
from bcc import BPF
bpf_program = """
#include <uapi/linux/ptrace.h>
BPF_HASH(retrans_by_port, u16, u64);
TRACEPOINT_PROBE(tcp, tcp_retransmit_skb) {
u16 dport = args->dport;
u64 zero = 0, *val;
val = retrans_by_port.lookup_or_try_init(&dport, &zero);
if (val) {
(*val)++;
}
return 0;
}
"""
b = BPF(text=bpf_program)
print("Counting TCP retransmits by destination port for 10 seconds...")
sleep(10)
for k, v in b["retrans_by_port"].items():
print(f"Port {k.value}: {v.value} retransmits")
This script uses BPF_HASH to increment counters directly in kernel memory. After 10 seconds, Python reads the hash table just once. As a result, CPU usage stays steady even when the network is dropping thousands of packets per second.
Practical Tips for Running bcc/eBPF in Production
- Check your kernel version first: eBPF works reliably on Linux kernel 4.9 and newer, and ideally 5.x+. On older OSes like CentOS 7 (kernel 3.10), many bcc scripts will fail with missing tracepoints or compilation errors.
- Aggregate data inside the kernel: Tools that trace connection open/close events like
tcpconnectare very lightweight. However, if you hook into per-packet functions liketcp_sendmsgon a 10 Gbps NIC, pushing every event to user space will still consume significant CPU. Always use histograms or hash maps to aggregate counts in the kernel first. - Running in Docker and Kubernetes: To debug from a sidecar container, mount
/sys/kernel/debug,/lib/modules(read-only), and/usr/src. For permissions, grantCAP_BPFandCAP_PERFMON(kernel 5.8+, plusCAP_SYS_ADMINif kprobes are needed) or use--privilegedduring urgent incidents. - Pick the right tool for each step: Use
bcc/eBPFto isolate the port or process, measure RTT, and catch retransmissions. Only reach foriperf3afterward to test hardware bandwidth limits, orWiresharkwhen you actually need to inspect packet payloads.
