Skip to content

Instantly share code, notes, and snippets.

@porjo
Created November 5, 2015 11:18
Show Gist options
  • Save porjo/c3d6e1a002b7acc853a3 to your computer and use it in GitHub Desktop.
Save porjo/c3d6e1a002b7acc853a3 to your computer and use it in GitHub Desktop.
Traffic control script for OpenWRT. Works on 15.05
#!/bin/sh
# Source: https://github.com/klacke/tc-shaper/blob/master/tc-shaper.sh
set -x
TC=$(which tc)
DEV=pppoe-wan
RATEUP=700
# Clear
tc qdisc del dev $DEV root
iptables -t mangle -D POSTROUTING -o $DEV -j SHAPER 2> /dev/null > /dev/null
iptables -t mangle -F SHAPER 2> /dev/null > /dev/null
iptables -t mangle -X SHAPER 2> /dev/null > /dev/null
$TC qdisc add dev $DEV root handle 1: htb default 60
$TC class add dev $DEV parent 1: classid 1:1 htb rate ${RATEUP}kbit
$TC class add dev $DEV parent 1:1 classid 1:10 htb rate $(( $RATEUP/4 ))kbit ceil ${RATEUP}kbit prio 0
$TC class add dev $DEV parent 1:1 classid 1:20 htb rate $(( $RATEUP/4 ))kbit ceil ${RATEUP}kbit prio 1
$TC class add dev $DEV parent 1:1 classid 1:30 htb rate $(( $RATEUP/4 ))kbit ceil ${RATEUP}kbit prio 2
$TC class add dev $DEV parent 1:1 classid 1:40 htb rate $(( $RATEUP/4 ))kbit ceil ${RATEUP}kbit prio 3
$TC qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
$TC qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
$TC qdisc add dev $DEV parent 1:30 handle 30: sfq perturb 10
$TC qdisc add dev $DEV parent 1:40 handle 40: sfq perturb 10
iptables -t mangle -N SHAPER 2> /dev/null
iptables -t mangle -I POSTROUTING -o $DEV -j SHAPER 2> /dev/null > /dev/null
# give "overhead" packets highest priority
iptables -t mangle -A SHAPER -o $DEV -p tcp --syn -m length --length 40:68 -j CLASSIFY --set-class 1:10
iptables -t mangle -A SHAPER -o $DEV -p tcp --tcp-flags ALL SYN,ACK -m length --length 40:68 -j CLASSIFY --set-class 1:10
iptables -t mangle -A SHAPER -o $DEV -p tcp --tcp-flags ALL ACK -m length --length 40:100 -j CLASSIFY --set-class 1:10
iptables -t mangle -A SHAPER -o $DEV -p tcp --tcp-flags ALL RST -j CLASSIFY --set-class 1:10
iptables -t mangle -A SHAPER -o $DEV -p tcp --tcp-flags ALL ACK,RST -j CLASSIFY --set-class 1:10
iptables -t mangle -A SHAPER -o $DEV -p tcp --tcp-flags ALL ACK,FIN -j CLASSIFY --set-class 1:10
# ssh
iptables -t mangle -A SHAPER -o $DEV -p tcp --sport ssh -m length --length 40:300 -j CLASSIFY --set-class 1:20
iptables -t mangle -A SHAPER -o $DEV -p tcp --dport ssh -m length --length 40:300 -j CLASSIFY --set-class 1:20
# DNS
iptables -t mangle -A SHAPER -o $DEV -p udp --dport domain -j CLASSIFY --set-class 1:20
# HTTP
iptables -t mangle -A SHAPER -o $DEV -p tcp --dport http -j CLASSIFY --set-class 1:30
iptables -t mangle -A SHAPER -o $DEV -p tcp --dport https -j CLASSIFY --set-class 1:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment