Skip to content

Instantly share code, notes, and snippets.

@eatnumber1
Created October 21, 2013 03:57
Show Gist options
  • Save eatnumber1/7078456 to your computer and use it in GitHub Desktop.
Save eatnumber1/7078456 to your computer and use it in GitHub Desktop.
How I configure HFSC on my Linux router
#!/bin/zsh
emulate -L zsh
setopt err_exit warn_create_global no_unset
#setopt xtrace
#typeset -i UL_MAX=215040
#typeset -i UL_MAX=20480 # kbit/s
#typeset -i UL_MAX=16384 # kbit/s
typeset -i UL_MAX=17408 # kbit/s
#typeset -i UL_MAX=1800
#(( UL_MAX = UL_MAX * 1024 / 8 )) || :
(( UL_MAX = UL_MAX * 1024 )) || :
#typeset -i DL_MAX=107520 # kbit/s
typeset -i DL_MAX=96768 # kbit/s
(( DL_MAX = DL_MAX * 1024 )) || :
function _rate {
float perc="$1"
float max="${2:-${RATE_MAX}}"
(( ret = max * ( perc / 100.0 ) )) || :
}
function _tc {
typeset -a opts
typeset -a args
args=( "$@" )
integer i
for (( i=1; i < ${#args}; i++ )); do
local arg="${args[$i]}"
if [[ $arg[1] == "-" ]]; then
opts+=( "$arg" )
args[$i]=()
shift
elif [[ $arg == rate% ]]; then
integer ret
_rate $args[$i+1]
args[$i+1]=$ret
args[$i]="rate"
fi
done
set -- "$args[@]"
local object="$1" cmd="$2"
shift 2
command tc "$opts[@]" "$object" "$cmd" dev ${interface:-wan} "$@"
}
function ingress-stop {
local interface=lan
_tc qdisc del root
}
function ingress-start {
local interface=lan
_tc qdisc add root handle 1: hfsc default 1
_tc class add parent 1: classid 1:1 hfsc ls rate $DL_MAX ul rate $DL_MAX
_tc qdisc add parent 1:1 handle 10: fq_codel
}
function _rate_set {
float ret
_rate "$@"
RATE_MAX=$ret
}
function _dscp-af11 {
_tc class add parent 1:10 classid 1:11 hfsc sc rate% 50
_tc qdisc add parent 1:11 handle 11: codel ecn
}
function _dscp-af12 {
_tc class add parent 1:10 classid 1:12 hfsc sc rate% 15
_tc qdisc add parent 1:12 handle 12: codel ecn
}
function _dscp-af13 {
_tc class add parent 1:10 classid 1:13 hfsc sc rate% 12.5
_tc qdisc add parent 1:13 handle 13: codel ecn
}
function _dscp-class1 {
float RATE_MAX=$RATE_MAX
_rate_set 6.25
_tc class add parent 1:1 classid 1:10 hfsc sc rate% 100
_dscp-af11
_dscp-af12
_dscp-af13
}
function _dscp-af21 {
_tc class add parent 1:20 classid 1:21 hfsc sc rate% 50
_tc qdisc add parent 1:21 handle 21: fq_codel ecn
}
function _dscp-af22 {
_tc class add parent 1:20 classid 1:22 hfsc sc rate% 25
_tc qdisc add parent 1:22 handle 22: fq_codel ecn
}
function _dscp-af23 {
_tc class add parent 1:20 classid 1:23 hfsc sc rate% 12.5
_tc qdisc add parent 1:23 handle 23: fq_codel ecn
}
function _dscp-class2 {
float RATE_MAX=$RATE_MAX
_rate_set 12.5
_tc class add parent 1:1 classid 1:20 hfsc sc rate% 100
_dscp-af21
_dscp-af22
_dscp-af23
}
function _dscp-af31 {
_tc class add parent 1:30 classid 1:31 hfsc sc rate% 50
_tc qdisc add parent 1:31 handle 31: fq_codel ecn
}
function _dscp-af32 {
_tc class add parent 1:30 classid 1:32 hfsc sc rate% 25
_tc qdisc add parent 1:32 handle 32: fq_codel ecn
}
function _dscp-af33 {
_tc class add parent 1:30 classid 1:33 hfsc sc rate% 12.5
_tc qdisc add parent 1:33 handle 33: fq_codel ecn
}
function _dscp-class3 {
float RATE_MAX=$RATE_MAX
_rate_set 25
_tc class add parent 1:1 classid 1:30 hfsc sc rate% 100
_dscp-af31
_dscp-af32
_dscp-af33
}
function _dscp-af41 {
_tc class add parent 1:40 classid 1:41 hfsc sc rate% 50
_tc qdisc add parent 1:41 handle 41: fq_codel ecn
}
function _dscp-af42 {
_tc class add parent 1:40 classid 1:42 hfsc sc rate% 25
_tc qdisc add parent 1:42 handle 42: fq_codel ecn
}
function _dscp-af43 {
_tc class add parent 1:40 classid 1:43 hfsc sc rate% 12.5
_tc qdisc add parent 1:43 handle 43: fq_codel ecn
}
function _dscp-class4 {
float RATE_MAX=$RATE_MAX
_rate_set 50
_tc class add parent 1:1 classid 1:40 hfsc sc rate% 100
_dscp-af41
_dscp-af42
_dscp-af43
}
function _dscp {
_dscp-class1
_dscp-class2
_dscp-class3
_dscp-class4
}
function egress-start {
local interface=wan
float RATE_MAX=$UL_MAX
_tc qdisc add root handle 1: hfsc default 32
_tc class add parent 1: classid 1:1 hfsc ls rate $UL_MAX ul rate $UL_MAX
_dscp
}
function egress-stop {
_tc qdisc del root
}
function start {
egress-start
ingress-start
}
function stop {
egress-stop
ingress-stop
}
function status {
_tc -s class show
_tc -s qdisc show
}
function restart {
stop || :
start
}
zmodload -F zsh/parameter +p:functions
if (( $+functions[$1] )) && [[ ${1[1]} != "_" ]]; then
"$1"
else
printf "Usage: %s start|stop|restart|status\n" "$0" >&2
exit 1
fi
# vim:ts=4 sw=4 sts=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment