一份智能QOS腳本


知識點:shell腳本,TC應用,iptables應用


#!/bin/sh
    #copyright by zhoutao0712
    echo -e '\n
    export UIP="192.168.1."\n
    export NET="192.168.1.0/24"\n
    export UP=40\n
    export DOWN=170\n
    export UPLOADR=2\n
    export UPLOADC=12\n
    export ONLINE=$(cat /proc/net/ipt_hashlimit/game_burst |wc -l)
    export DOWNLOADR=15\n
    export DOWNLOADC=$(($DOWN*((30+ONLINE))/((20+8*ONLINE))))\n' >/tmp/regist_env.sh
    cat /proc/net/ipt_hashlimit/newc >/tmp/old
    cat /tmp/old |wc -l >/tmp/N_old
    . /tmp/regist_env.sh
    insmod imq
    insmod ipt_IMQ
    insmod ipt_length.o
    insmod ipt_hashlimit.o
    ifconfig imq1 up
    ifconfig imq0 up
    iptables -t mangle -N QOSDOWN
    iptables -t mangle -N QOSUP
    iptables -t mangle -I FORWARD -s $NET -j QOSUP
    iptables -t mangle -I FORWARD -d $NET -j QOSDOWN
    iptables -t mangle -A QOSDOWN -j IMQ --todev 0
    iptables -t mangle -A QOSUP -j IMQ --todev 1
//DNS不做限制
    iptables -t mangle -I QOSDOWN -p udp --sport 53 -j RETURN
    iptables -t mangle -I QOSUP -p udp --dport 53 -j RETURN
    iptables -t mangle -N GAME_BURST
//判定大於128的數據包爲遊戲包,並放入GAME_BURST鏈
    iptables -t mangle -A QOSDOWN -m length --length 128: -j GAME_BURST
//遊戲包以目的地址爲準進行流量整形
    iptables -t mangle -A GAME_BURST -m hashlimit --hashlimit 8/sec --hashlimit-burst 100 --hashlimit-mode dstip \
    --hashlimit-name game_burst --hashlimit-htable-gcinterval 5000 --hashlimit-htable-expire 150000 -j RETURN
    iptables -t mangle -A GAME_BURST -m recent --rdest --name game_burst --set
//對http https 數據標記爲255
    iptables -t mangle -A QOSDOWN -p tcp -m mport --sports 80,443 -j BCOUNT
    iptables -t mangle -A QOSDOWN -p tcp -m mport --sports 80,443 -m bcount --range :102400 -j MARK --set-mark-return 255
    iptables -t mangle -A QOSUP -p tcp -m mport --dports 80,443 -j BCOUNT
    iptables -t mangle -A QOSUP -p tcp -m mport --dports 80,443 -m bcount --range :102400 -j MARK --set-mark-return 255
//標記遊戲數據包爲254
    iptables -t mangle -A QOSDOWN -m recent --rdest --name game_burst ! --rcheck  --seconds 8 -j MARK --set-mark-return 254
    iptables -t mangle -A QOSDOWN -j MARK --ipaddr 1
    iptables -t mangle -A QOSUP -j MARK --ipaddr 0
    tc qdisc del dev imq0 root
    tc qdisc del dev imq1 root
    echo -e '\n
    tc qdisc add dev imq0 root handle 1: htb\n
    tc qdisc add dev imq1 root handle 1: htb\n
    tc qdisc add dev imq0 root handle 1: htb\n
    tc qdisc add dev imq1 root handle 1: htb\n
    tc class add dev imq1 parent 1: classid 1:1 htb rate $((UP))kbps\n
    tc class add dev imq0 parent 1: classid 1:1 htb rate $((DOWN))kbps\n
//對頁面訪問進行適當限制
    tc class add dev imq0 parent 1:1 classid 1:255 htb rate $((DOWN*2/10))kbps ceil $((DOWN*8/10))kbps quantum 6000 prio 3\n
    tc filter add dev imq0 parent 1:0 protocol ip prio 5 handle 255 fw flowid 1:255\n
    tc class add dev imq1 parent 1:1 classid 1:255 htb rate $((UP*2/10))kbps ceil $((UP*5/10))kbps quantum 2000 prio 3\n
    tc filter add dev imq1 parent 1:0 protocol ip prio 5 handle 255 fw flowid 1:255\n
//保障遊戲的最低帶寬
    tc class add dev imq0 parent 1:1 classid 1:254 htb rate $((DOWN*6/10))kbps prio 0\n
    tc filter add dev imq0 parent 1:0 protocol ip prio 2 handle 254 fw flowid 1:254\n
    tc qdisc add dev imq0 parent 1:254 handle 254: bfifo limit 10kb\n' >/tmp/pref_qos
    . /tmp/pref_qos
    iptables -t mangle -N CONNLMT
    iptables -t mangle -N NEWLMT
    iptables -t mangle -I FORWARD -m state --state NEW -s $NET -p ! icmp -j CONNLMT //對內網發起的連接給予限制
    iptables -t mangle -A CONNLMT -j NEWLMT
//整形流量,對其他數據包進行限速
    iptables -t mangle -A NEWLMT -m hashlimit --hashlimit-name newconn --hashlimit 15/sec \
    --hashlimit-mode srcip --hashlimit-htable-gcinterval 5000 --hashlimit-htable-expire 20000 -j RETURN
    iptables -t mangle -A NEWLMT -j DROP
//排除DNS HTTP等包的限制
    iptables -t mangle -A CONNLMT -p tcp -m mport --dports 20:23,25,80,110,443 -j RETURN
    iptables -t mangle -A CONNLMT -p udp -m mport --dports 53,4000:4050 -j RETURN
//限制連接數量
    iptables -t mangle -A CONNLMT -p tcp -m connlimit --connlimit-above 150 -j DROP
    iptables -t mangle -A CONNLMT -p ! tcp -m connlimit --connlimit-above 120 -j DROP
    echo -e '\n
    i=1\n
    while [ $i -le $N_del ]\n
    do\n
    ip_i=$(sed -n ${i}p /tmp/del_list)\n
    handle_i=$(echo $ip_i|cut -d'.' -f4)\n
    tc filter del dev imq0 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n
    tc class del dev imq0 parent 1:1 classid 1:$handle_i\n
    tc filter del dev imq1 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n
    tc class del dev imq1 parent 1:1 classid 1:$handle_i\n
    i=`expr $i + 1`\n
    done\n' >/tmp/del_qos
    echo -e '\n
    i=1\n
    while [ $i -le $N_add ]\n
    do\n
    ip_i=$(sed -n ${i}p /tmp/add_list)\n
    handle_i=$(echo $ip_i|cut -d'.' -f4)\n
    //單IP下載限速
    tc class add dev imq0 parent 1:1 classid 1:$handle_i htb rate $((DOWNLOADR))kbps ceil $((DOWNLOADC))kbps quantum 2000 prio 5\n
    tc qdisc add dev imq0 parent 1:$handle_i handle $handle_i sfq perturb 15\n
    //過濾器對每個IP進行分類
    tc filter add dev imq0 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n
    //同上
    tc class add dev imq1 parent 1:1 classid 1:$handle_i htb rate $((UPLOADR))kbps ceil $((UPLOADC))kbps quantum 1500 prio 5\n
    tc qdisc add dev imq1 parent 1:$handle_i handle $handle_i bfifo limit 6kb\n
    tc filter add dev imq1 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n
    i=`expr $i + 1`\n
    done\n' >/tmp/add_qos
    echo -e '\n
    i=1\n
    while [ $i -le $N_new ]\n
    do\n
    ip_i=$(sed -n ${i}p /tmp/new)\n
    handle_i=$(echo $ip_i|cut -d'.' -f4)\n
    tc class replace dev imq0 parent 1:1 classid 1:$handle_i htb rate $((DOWNLOADR))kbps ceil $((DOWNLOADC))kbps quantum 2000 prio 5\n
    tc qdisc replace dev imq0 parent 1:$handle_i handle $handle_i sfq perturb 15\n
    tc filter replace dev imq0 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n
    tc class replace dev imq1 parent 1:1 classid 1:$handle_i htb rate $((UPLOADR))kbps ceil $((UPLOADC))kbps quantum 1500 prio 5\n
    tc qdisc replace dev imq1 parent 1:$handle_i handle $handle_i bfifo limit 6kb\n
    tc filter replace dev imq1 parent 1:0 protocol ip pref 100 handle $handle_i fw classid 1:$handle_i\n
    i=`expr $i + 1`\n
    done\n' >/tmp/replace_qos


代碼出處:http://bbs.dualwan.cn/thread-20122-1-1.html

openwrt 改良版:

http://code.google.com/p/openwrt-dreambox/source/browse/luci/trunk/applications/luci-qosv4/root/usr/bin/qosv4?r=22

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章