Skip to content

Instantly share code, notes, and snippets.

@Maffsie
Created December 29, 2014 05:01
Show Gist options
  • Save Maffsie/4a567f5d451fc0953757 to your computer and use it in GitHub Desktop.
Save Maffsie/4a567f5d451fc0953757 to your computer and use it in GitHub Desktop.
IF="eth0.2"
_proto() { grep -Ei "^$1 " /etc/protocols|awk '{print $2}'; }
_prio() { iseq $1 highest&&echo 10;iseq $1 high&&echo 15;iseq $1 bulk&&echo 20;iseq $1 low&&echo 30; }
_tc() { /usr/sbin/tc $@; }
_qdisc() { act=$1;shift;_tc qdisc $act dev $IF $@; }
_add_qdisc() { _qdisc add $@; }
_add_class() { par=$1;cid=$2;shift 2;_tc class add dev $IF parent $par classid $cid $@; }
_add_filter() { dst=$1;shift;_tc filter add dev $IF protocol ip parent 1:0 prio 1 u32 match $@ flowid 1:$dst; }
iseq() { [ "$1" == "$2" ];return $?; }
clean() { _qdisc del root >/dev/null 2>&1;_qdisc del ingress >/dev/null 2>&1; }
start() { defprio=`_prio $1`;stdrate=$2;peak=$(($2+$3));_add_qdisc root handle 1: hfsc default $defprio;_add_class 1: 1:1 hfsc sc rate ${peak}kbps ul rate ${stdrate}kbps; }
sfq() { hdl=`_prio $1`;ptb=$2;_add_qdisc parent 1:$hdl handle $hdl: sfq perturb $ptb; }
priority() { prio=`_prio $1`;lat=$2;stdrate=$3;peak=$(($3+$4));shift 4;_add_class 1:1 1:$prio hfsc sc $@ dmax $lat rate ${peak}kbps ul rate ${stdrate}kbps; }
classify() {
prio="";cmd="";tgt="";hex="0xffff"
iseq $1 host&&cmd=dst&&hex="";iseq $1 port&&cmd=dport;iseq $1 proto&&cmd=protocol&&hex="0xff"&&tgt=`_proto $2`;shift
[ -z "$tgt" ]&&tgt="$1";shift;prio=`_prio $1`;shift
[ -z "$cmd" -o -z "$tgt" -o -z "$prio" ] && exit 1
_add_filter $prio ip $cmd $tgt $hex
}
clean;iseq -d $1 && exit
start bulk 90 15
priority highest 1ms 92 13
priority high 5ms 80 10
priority bulk 15ms 70 25 umax 1500
priority low 50ms 35 25 umax 1500
classify proto icmp highest
classify proto udp high
classify port 22 highest
classify port 9418 highest
classify host 91.212.90.200/32 highest
sfq highest 10
sfq high 10
sfq bulk 10
sfq low 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment