Skip to content

Instantly share code, notes, and snippets.

@mykola2312
Created October 31, 2025 06:37
Show Gist options
  • Select an option

  • Save mykola2312/b3c2a3ed9e2c3d5dbe3f7fb12975cc93 to your computer and use it in GitHub Desktop.

Select an option

Save mykola2312/b3c2a3ed9e2c3d5dbe3f7fb12975cc93 to your computer and use it in GitHub Desktop.
HTB Traffic Shaper
#!/bin/bash
set -e
IF="eth0"
PARENT_RATE="200mbit"
PARENT_CEIL="200mbit"
MARK_HIGH=244010 # T1
MARK_T2=244020 # T2
MARK_T3=244030 # T3
MARK_T4=244040 # T4
RATE_HIGH="25mbit" # class 10
RATE_T2="10mbit" # class 20
RATE_T3="10mbit" # class 40
RATE_T4="10mbit" # class 70
RATE_DEFAULT="256kbit" # class 30 (unmatched traffic)
# Ceil for all classes. Everyone can borrow up to the parent when idle
CEIL_ALL="200mbit"
PRIO_HIGH=0
PRIO_T2=1
PRIO_DEFAULT=2
PRIO_T3=3
PRIO_T4=7
QUANTUM=15140
BURST_PARENT="64k"
BURST_CHILD="64k"
# Clean
tc qdisc del dev "$IF" root 2>/dev/null || true
tc qdisc del dev "$IF" ingress 2>/dev/null || true
# Root HTB qdisc
tc qdisc add dev "$IF" root handle 1: htb default 30 r2q 10
# Root class 1:1
tc class add dev "$IF" parent 1: classid 1:1 htb \
rate "$PARENT_RATE" ceil "$PARENT_CEIL" \
burst "$BURST_PARENT" cburst "$BURST_PARENT"
# 1:10 = HIGH / T1
# 1:20 = T2
# 1:30 = DEFAULT
# 1:40 = T3
# 1:70 = T4 (lowest)
# High priority / T1
tc class add dev "$IF" parent 1:1 classid 1:10 htb \
rate "$RATE_HIGH" ceil "$CEIL_ALL" \
burst "$BURST_CHILD" cburst "$BURST_CHILD" \
quantum "$QUANTUM" prio "$PRIO_HIGH"
# T2
tc class add dev "$IF" parent 1:1 classid 1:20 htb \
rate "$RATE_T2" ceil "$CEIL_ALL" \
burst "$BURST_CHILD" cburst "$BURST_CHILD" \
quantum "$QUANTUM" prio "$PRIO_T2"
# Default
tc class add dev "$IF" parent 1:1 classid 1:30 htb \
rate "$RATE_DEFAULT" ceil "$CEIL_ALL" \
burst "$BURST_CHILD" cburst "$BURST_CHILD" \
quantum "$QUANTUM" prio "$PRIO_DEFAULT"
# T3
tc class add dev "$IF" parent 1:1 classid 1:40 htb \
rate "$RATE_T3" ceil "$CEIL_ALL" \
burst "$BURST_CHILD" cburst "$BURST_CHILD" \
quantum "$QUANTUM" prio "$PRIO_T3"
# T4 (lowest)
tc class add dev "$IF" parent 1:1 classid 1:70 htb \
rate "$RATE_T4" ceil "$CEIL_ALL" \
burst "$BURST_CHILD" cburst "$BURST_CHILD" \
quantum "$QUANTUM" prio "$PRIO_T4"
tc qdisc add dev "$IF" parent 1:10 handle 110: fq_codel
tc qdisc add dev "$IF" parent 1:20 handle 120: fq_codel
tc qdisc add dev "$IF" parent 1:30 handle 130: fq_codel
tc qdisc add dev "$IF" parent 1:40 handle 140: fq_codel
tc qdisc add dev "$IF" parent 1:70 handle 170: fq_codel
# High / T1 traffic -> 1:10
tc filter add dev "$IF" parent 1: protocol all prio 10 handle $MARK_HIGH fw classid 1:10
# T2 traffic -> 1:20
tc filter add dev "$IF" parent 1: protocol all prio 20 handle $MARK_T2 fw classid 1:20
# T3 traffic -> 1:40
tc filter add dev "$IF" parent 1: protocol all prio 30 handle $MARK_T3 fw classid 1:40
# T4 (lowest) traffic -> 1:70
tc filter add dev "$IF" parent 1: protocol all prio 40 handle $MARK_T4 fw classid 1:70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment