Skip to content

Instantly share code, notes, and snippets.

@IronSavior
Created January 22, 2021 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IronSavior/4694af4ca92311a3d5c581a64d55b285 to your computer and use it in GitHub Desktop.
Save IronSavior/4694af4ca92311a3d5c581a64d55b285 to your computer and use it in GitHub Desktop.
Old traffic shaper script
#!/bin/bash
# Heavy Metal Traffic Control (HMTC)
# Erik Elmore 2005-06-11
# Based on concepts from WonderShaper
### Configuration ###
# Set this to your WAN interface
INTERFACE=eth0
# Upstream Parameters #
# True maximal upstream rate in kbits/second
MAXTXRATE=384
# Limit upstream rate to this percentage of the max rate
TXFACTOR=90
# Guaranteed percentage for high priority class
HIGHBWFACTOR=60
# Guaranteed percentage for default class
NORMBWFACTOR=30
# Upstream burst in kbits for default class
#TXBURST=8k
# Guaranteed bandwidth for low priority class in kbits/second
LOWBWRATE=1
# Downstream Parameters #
# True maximal downstream rate in kbits/second
MAXRXRATE=4000
# Police ingress at this percentage of the max rate
RXFACTOR=85
# Set downstream burst in kbits
RXBURST=15k
# Comment this line out after the script has been configured
# echo "Traffic control script requires configuration" && exit
### End of configuration ###
TXRATE=$[$MAXTXRATE*$TXFACTOR/100]
HIGHBWRATE=$[$TXRATE*$HIGHBWFACTOR/100]
NORMBWRATE=$[$TXRATE*$NORMBWFACTOR/100]
RXRATE=$[$MAXRXRATE*$RXFACTOR/100]
if [ "$1" = "status" ]; then
echo "Configuration for $INTERFACE:"
echo "Upstream bandwidth limited to $TXRATE kbps (${TXFACTOR}% of $MAXTXRATE kbps)"
echo "$HIGHBWRATE kbps (${HIGHBWFACTOR}% of upstream bandwidth limit) guaranteed for high priority"
echo "$NORMBWRATE kbit/s (${NORMBWFACTOR}% of upstream bandwidth limit) guaranteed for normal priority"
echo "$LOWBWRATE kbit/s is guaranteed for low priority"
echo "$[$TXRATE-$HIGHBWRATE-$NORMBWRATE] kbit/s not committed ($[100-$HIGHBWFACTOR-$NORMBWFACTOR]% of $TXRATE kbit/s)"
echo
echo "Downstream bandwidth policed at $RXRATE kbit/s (${RXFACTOR}% of $MAXRXRATE)"
echo
echo "Qdiscs for $INTERFACE:"
tc -s qdisc ls dev $INTERFACE
echo
echo "Classes for $INTERFACE:"
tc -s class ls dev $INTERFACE
exit
fi
# Erase all existing TC settings, suppress output.
tc qdisc del dev $INTERFACE root > /dev/null 2>&1
tc qdisc del dev $INTERFACE ingress > /dev/null 2>&1
if [ "$1" = "stop" ]; then exit; fi
### Egress shaping ###
# Create root HTB qdisc, default to 1:20
tc qdisc add dev $INTERFACE root handle 1: htb \
default 30
# Limit egress speed to TXRATE
tc class add dev $INTERFACE parent 1: classid 1:1 htb \
rate ${TXRATE}kbit
# Create high priority class 1:10
tc class add dev $INTERFACE parent 1:1 classid 1:10 htb \
rate ${HIGHBWRATE}kbit \
ceil ${TXRATE}kbit \
prio 1
# Create normal class 1:20
tc class add dev $INTERFACE parent 1:1 classid 1:20 htb \
rate ${NORMBWRATE}kbit \
ceil ${TXRATE}kbit \
prio 2
# Create low priority class 1:30
tc class add dev $INTERFACE parent 1:1 classid 1:30 htb \
rate ${LOWBWRATE}kbit \
ceil ${TXRATE}kbit \
prio 3
# Packets with ToS "Minimum Delay" (0x10) flag get high priority
tc filter add dev $INTERFACE parent 1:0 protocol ip prio 10 u32 \
match ip tos 0x10 0x10 \
flowid 1:10
# ICMP packets get high priority
tc filter add dev $INTERFACE parent 1:0 protocol ip prio 10 u32 \
match ip protocol 0x01 0xff \
flowid 1:10
# ACK packets get high priority
tc filter add dev $INTERFACE parent 1: protocol ip prio 10 u32 \
match ip protocol 6 0xff \
match u8 0x05 0x0f at 0 \
match u16 0x0000 0xffc0 at 2 \
match u8 0x10 0xff at 33 \
flowid 1:10
# Packets with ToS "Maximum Throughput" (0x08) flag get low priority
tc filter add dev $INTERFACE parent 1:0 protocol ip prio 14 u32 \
match ip tos 0x08 0x08 \
flowid 1:20
# All other traffic gets normal priority
#tc filter add dev $INTERFACE parent 1: protocol ip prio 18 u32 \
# match ip dst 0.0.0.0/0 \
# flowid 1:20
# Enable stochastic fairness for each class
tc qdisc add dev $INTERFACE parent 1:10 handle 10: sfq perturb 10
tc qdisc add dev $INTERFACE parent 1:20 handle 20: sfq perturb 10
tc qdisc add dev $INTERFACE parent 1:30 handle 30: sfq perturb 10
### Ingress policing ###
# Enable ingress qdisc
tc qdisc add dev $INTERFACE handle ffff: ingress
# ICMP packets are never dropped
tc filter add dev $INTERFACE parent ffff: protocol ip prio 40 u32 \
match ip protocol 0x01 0xff \
flowid :1
# UDP packets are never dropped
tc filter add dev $INTERFACE parent ffff: protocol ip prio 40 u32 \
match ip protocol 0x11 0xff \
flowid :1
# Ventrilo packets are never dropped
tc filter add dev $INTERFACE parent ffff: protocol ip prio 40 u32 \
match ip sport 3784 0xffff \
flowid :1
# Guild Wars packets are never dropped
tc filter add dev $INTERFACE parent ffff: protocol ip prio 40 u32 \
match ip sport 6112 0xffff \
flowid :1
# Add other rules based on port, change XXXX to the appropriate value
#tc filter add dev $INTERFACE parent ffff: protocol ip prio 40 u32 \
# match ip sport XXXX 0xffff \
# flowid :1
# Drop all other packets coming in faster than RXRATE
tc filter add dev $INTERFACE parent ffff: protocol ip prio 50 u32 \
match ip src 0.0.0.0/0 \
police rate ${RXRATE}kbit \
burst $RXBURST \
drop \
flowid :1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment