Networking

Tracing packet drops with eBPF, no kernel patches required

"The network is slow" is not a diagnosis. With kfree_skb tracepoints and a three-line bpftrace script, you can replace it with a drop reason, a stack and a count that fits in a ticket.

Most packet loss in a Linux host never reaches a wire. It is dropped in the receive path, in a qdisc, in a netfilter rule, or because an application socket buffer was full. The traditional toolbar - ifconfig, interface counters, a quick tcpdump - tells you that packets are missing, not who discarded them. eBPF closes that gap without recompiling anything.

Counters lie by omission

ip -s link aggregates drops per interface. A drop inside the TCP receive path increments TcpExt counters, a qdisc overflow increments a different one, and a socket buffer overflow shows up nowhere useful. When a service loses 0.3 percent of requests, those counters are not enough to pick a fix.

Attach to kfree_skb and read the reason

Since kernel 5.17, the kernel carries an enum skb_drop_reason into kfree_skb. The tracepoint exposes it, which means one probe answers the question directly. On Ubuntu 24.04 the tooling is a two-package install: apt install bpftrace linux-tools-common.

# summarise drop reasons for ten seconds
bpftrace -e 'tracepoint:skb:kfree_skb {
  @reasons[args->reason] = count();
}
interval:s:10 { exit(); }'

# 10s output on a misbehaving edge node
# @reasons[tcp_v4_rcv]: 4281
# @reasons[netif_receive_skb]: 913
# @reasons[socket_full]: 268

socket_full would have sent us to the application, not the NIC. That single label changes the entire investigation. If the reason is tcp_v4_rcv, the next step is checking whether a connection tracking table filled up. If it is netif_receive_skb, the routing and qdisc configuration is the suspect.

From probe to runbook

We do not run ad hoc probes during an incident. The runbook has three steps, each with a copy-paste command, and it ends with a decision rather than a dashboard link:

  • Classify. Count drop reasons for ten seconds and capture the top three.
  • Attribute. For the top reason, collect a bounded kernel stack (kstack(5)) to identify the call site.
  • Confirm. Cross-check qdisc and socket counters with tc -s qdisc show dev eth0 and nstat -az | grep -i drop.

Each step produces a line that can be pasted into the incident channel, which is what shortens the handoff at 04:00 more than any graph.

Bound everything

eBPF probes run inside the kernel. A map keyed by full stacks on a 400k packets-per-second interface will consume memory quickly. Use kstack(5), not unbounded stacks, and always gate probes behind a short interval so a forgotten session cannot outlive the incident.

The practical result: mean time to name a drop reason fell below ten minutes for the common cases, and the "network" bucket in our incident tracker is now small enough to argue about.

Newer noteOptimizing Linux TCP congestion control