A Linux box with the default pfifo_fast qdisc and no shaping will, under sustained outbound saturation, build queues in driver buffers, in switch buffers, in modem buffers — and latency goes from 20 ms to 500+ ms for every flow on the egress NIC. The classical name for this is bufferbloat, and tc is the tool to fix it. This article walks through a working HTB hierarchy that caps total egress just below your link speed, divides it by class, and runs FQ-CoDel (or CAKE) inside each class so latency stays low for interactive flows while bulk transfers fill the rest.
How to verify
Inspect qdisc, classes, and filters:
tc -s qdisc show dev eth0
tc -s class show dev eth0
tc filter show dev eth0
tc -s -d qdisc show dev eth0
ip -s link show eth0
ss -tn state established
ping -c 30 8.8.8.8 & curl -o /dev/null http://big-download/file
Run the ping in parallel with a bulk transfer over the same NIC: with proper shaping, ping RTT stays within ±10ms of idle; without, it climbs proportional to the buffer depth.
What’s happening
The Linux traffic control subsystem operates on egress queues (mostly — ingress is more limited). Three concepts:
- qdisc (queueing discipline) — the algorithm that schedules packets out of a queue.
pfifo_fast,fq_codel,cake,htb,prio,tbf, etc. - class — a sub-queue inside a classful qdisc (HTB, HFSC, CBQ). Each class can have its own rate, ceiling, and child qdisc.
- filter — rule that classifies packets into a class. Matches on iptables marks, source/dest IP, port, or BPF.
HTB (Hierarchical Token Bucket) gives every class a guaranteed rate and a maximum ceil. When a class is below its rate it borrows from its parent up to ceil. The shape limits aggregate throughput per class.
CoDel (Controlled Delay) and FQ-CoDel (Fair Queueing with CoDel) are the modern AQMs (active queue management). FQ-CoDel maintains per-flow queues and drops from the head of the longest one when sojourn time exceeds 5 ms. CAKE goes further — it combines shaping, AQM, fair queueing, and DSCP-aware classification into a single qdisc. For routers, CAKE is often the simpler answer; for servers, FQ-CoDel inside HTB classes gives more control.
The key operational fact: you must shape below the slowest hop’s actual capacity. If your modem caps at 100 Mbps but the buffer is 200 ms deep, shaping at 95 Mbps on the Linux router keeps the buffer empty downstream and AQM happens on the Linux box where you control it. Shape too high (say 110 Mbps) and the modem buffers up again and you are back to bufferbloat.
The procedure
-
Identify the egress device and its real-world capacity. For an internet uplink, run a speed test, take 95% of the result. Assume 100 Mbps measured, target 95 Mbps shape.
-
Clear existing qdiscs on the device.
tc qdisc del dev eth0 root 2>/dev/null -
Install a CAKE qdisc for the simple case. If the link is a single homogeneous egress, CAKE alone is often enough:
tc qdisc replace dev eth0 root cake bandwidth 95mbit besteffortbesteffortsays “do not use DSCP for prioritisation, just fair-queue and shape”. For DSCP-aware behaviour usediffserv4ordiffserv8. -
HTB with FQ-CoDel for finer control. Three classes:
priorityfor interactive (SSH, VoIP, ping),defaultfor normal,bulkfor large downloads.# Root qdisc and HTB shell tc qdisc add dev eth0 root handle 1: htb default 30 tc class add dev eth0 parent 1: classid 1:1 htb rate 95mbit ceil 95mbit # Priority — small share guaranteed, can use it all if alone tc class add dev eth0 parent 1:1 classid 1:10 htb rate 10mbit ceil 95mbit prio 0 tc qdisc add dev eth0 parent 1:10 handle 10: fq_codel target 2ms # Default — most traffic tc class add dev eth0 parent 1:1 classid 1:20 htb rate 70mbit ceil 95mbit prio 1 tc qdisc add dev eth0 parent 1:20 handle 20: fq_codel target 5ms # Bulk — large downloads, can use what's left tc class add dev eth0 parent 1:1 classid 1:30 htb rate 15mbit ceil 95mbit prio 2 tc qdisc add dev eth0 parent 1:30 handle 30: fq_codel target 5ms -
Filter packets into classes. Two approaches: mark with iptables and match on fwmark, or use
tc filterwith cgroup/u32/bpf matches. Marking is the most portable:# Mark interactive iptables -t mangle -A POSTROUTING -o eth0 -p icmp -j MARK --set-mark 1 iptables -t mangle -A POSTROUTING -o eth0 -p tcp --dport 22 -j MARK --set-mark 1 iptables -t mangle -A POSTROUTING -o eth0 -p udp --dport 5060 -j MARK --set-mark 1 # Mark bulk (rsync, scp, common backup ports) iptables -t mangle -A POSTROUTING -o eth0 -p tcp -m multiport --dports 873,5000 -j MARK --set-mark 3 # tc filters tc filter add dev eth0 parent 1: protocol ip handle 1 fw flowid 1:10 tc filter add dev eth0 parent 1: protocol ip handle 3 fw flowid 1:30 -
Test under load. Saturate the bulk class with a download, and ping the gateway:
curl -o /dev/null http://server/big-file.iso & ping -c 100 192.0.2.1With shaping and FQ-CoDel, ping should stay within ±5ms of baseline; without, it will jump to 200+ms.
-
Persist via tc-systemd or a wrapper script. systemd has no native tc unit; the simplest pattern is a
[Service]that runs the script inExecStartandtc qdisc delinExecStop. Alternatively, NetworkManager dispatcher scripts run on link-up.
Operational notes
- Ingress shaping is harder. The kernel
ifb(Intermediate Functional Block) device lets you redirect ingress traffic through an egress-shaped pseudo-NIC; in practice we just shape egress on the upstream side wherever possible. - HTB rate accuracy depends on
r2q. The default fits most cases but very low-rate or very high-rate classes need tuning; checktc -s class showforlent,borrowed,giants,tokenscounters. - CAKE auto-rate is convenient but not magic. It cannot shape what it cannot measure; you still must give it a
bandwidthfigure for the link unless the modem is plain Ethernet at line-rate. - DSCP markings rarely survive the internet. ISPs reset DSCP at their edge. Internal QoS based on DSCP works fine within your network; do not expect it to follow packets across the public internet.
- CPU cost is real on high-speed NICs. Per-flow queueing at 10G+ needs RSS and CPU affinity tuning; without that, single-CPU softirq becomes the bottleneck before the qdisc has a chance to be useful.
In the engagements we run, the typical egress setup is CAKE with besteffort diffserv4 for the simple case, and HTB+FQ-CoDel when we need separate guaranteed lanes for VoIP or interactive shell traffic. The ping-RTT-during-saturation check is part of our standard onboarding for any client with a constrained uplink; it catches bufferbloat that nobody noticed before. The patterns live in the same Ansible role library the managed-operations practice operates day-to-day.