Egress shaping on a VPS is usually done badly or not at all. Without a queue discipline, a burst of uploads fills the provider’s buffer, latency to every interactive user climbs, and everyone blames the network. A small HTB tree with fq_codel leaves gives you a hard ceiling per class, low queuing delay, and counters you can read at any time.
Confirming your shaping options
You need CAP_NET_ADMIN inside the guest and the sch_htb and sch_fq_codel modules available. Check with tc qdisc show dev eth0 and modprobe sch_htb. Most KVM providers permit this; container plans often do not, and tc will fail with an operation-not-permitted error.
Know your real uplink. Shaping at exactly the advertised port speed produces drops upstream where the provider polices, which is worse than shaping slightly below it. Set the HTB root rate to 90% to 95% of measured upload. Measure it first with a single iperf3 -c upload run, not with the plan’s marketing figure. On a 1 Gbit plan that typically means a root rate of 900 Mbit, not 1000.
The HTB tree layout
A practical design has one root class at the measured rate, then child classes for traffic types: interactive SSH and DNS at low latency, HTTP responses at a guaranteed rate, and bulk backup at a capped rate. HTB allocates unused bandwidth to classes that need it, so the sum of class rates can exceed the root rate as long as ceilings are set correctly.
Structure: root qdisc handle 1: htb default 30, then a class 1:1 at the uplink rate, with children 1:10 for interactive, 1:20 for web, and 1:30 for bulk. Give interactive a rate of 5% with a 2 Mbit ceiling, web 60%, and bulk the remainder. Each leaf gets fq_codel, never htb’s own queue. HTB’s built-in fifo has no flow hashing and produces head-of-line blocking when one connection saturates.
Writing the commands
Apply from a shell script so the change is reproducible after reboot. Start with tc qdisc del dev eth0 root, ignoring the error if nothing is attached, then tc qdisc add dev eth0 root handle 1: htb default 30. Add the parent class with tc class add dev eth0 parent 1: classid 1:1 htb rate 95mbit ceil 95mbit.
Then add each leaf and its fq_codel qdisc: tc qdisc add dev eth0 parent 1:30 handle 30: fq_codel limit 10240 flows 1024 quantum 1514 target 5ms interval 100ms. The limit of 10240 packets and interval of 100 ms are the defaults worth starting from; lower the target to 5 ms for all leaves. Classify traffic with tc filter add dev eth0 parent 1: protocol ip prio 1 u32 match ip dport 22 0xffff flowid 1:10.
Classifying without breaking connections
Destination-port filters are cheap and effective. Match SSH, DNS, and NTP to the interactive class, match your application ports to the web class, and leave everything else in the default bulk class. Avoid deep packet inspection; on a VPS, u32 or flower matches cost almost nothing while netfilter-based classifiers add a kernel dependency and conntrack load.
For application-level classification without port guessing, have the service mark packets itself with setsockopt(SO_MARK) or iptables -j MARK, then match marks in tc with match meta mark 0x10. This keeps the classifier aligned with intent even when ports change.
Reading the counters
Run tc -s qdisc show dev eth0 and watch for dropped and overlimits. Drops inside an fq_codel leaf are expected under saturation and mean the queue was full; drops at the root HTB class mean the sum of child demand exceeds the uplink and shaping is working. A steady climb in HTB drops with interactive traffic intact is a healthy sign.
Check per-class statistics with tc -s class show dev eth0. If the bulk class shows far more than its rate in bytes over a minute, the ceiling is misconfigured or traffic is landing in the wrong class. Verify with tc -s filter show dev eth0 that no rule after the match you expect is being hit instead.
Measuring the effect on latency
Start a saturating upload and simultaneously ping an external host. Without shaping, RTT often triples because the provider buffers the excess. With HTB plus fq_codel and a correctly set root rate, the added delay should stay under 20 ms. Record both numbers; the ping under load is the real proof the shaping works.
Use ping -i 0.2 -c 300 1.1.1.1 > ping.log during the upload, then read min, avg, and max from the summary. A max of 60 ms under load against 20 ms idle indicates either an incorrect root rate or a provider-side shaper that is policing below your configured limit. Repeat the test with ten parallel uploads instead of one, since a single stream can hide queue growth behind TCP’s own pacing.
Persisting and testing the configuration
Write the script to /usr/local/sbin/tc-shape.sh, make it executable, and call it from a systemd unit with After=network-online.target. Do not rely on rc.local, which many images no longer run. Add an idempotent guard so the script removes an existing root qdisc before adding a new one.
After a reboot, confirm with tc -s qdisc show dev eth0 that the tree is present and counters are moving. Then run the upload-and-ping test again. If behaviour differs from the manual run, the unit started before the interface had a link, a common cause of silent failure. Compare shaping against unmanaged VPS hosting defaults and, if you need more headroom for bulk transfers, review the dedicated bandwidth options in this server comparison.



Leave a Reply
You must be logged in to post a comment.