linux網關之流量控制(Qos)iptables+TC進行流量控制 下面是咱們一個子公司的一個linux網關的Qos設置,利用iptables和TC,感受效果很好的。實例1: 流量控制:防火牆上eth0鏈接內網,eth1鏈接外網線路,帶寬爲2.5M,目標:一、內網用戶下載佔用的帶寬最多爲1000kbit/s 而192.168.37.167主192.168.37.168下載帶寬可達到1.5Mbit/s二、內網中的192.168.37.124和192.168.37.140的上傳佔用的帶寬最多爲1.5M,而其它用戶最多爲150Kbit/s(這樣的流量控制後,內網中即便有人使用bt之類的軟件也不怕。由於他的上傳最多隻能佔用150Kbit/s,下載最多1000kbit/s ^-^)#!/bin/shTC="/sbin/tc"LAN_IFACE="eth0"INET_IFACE="eth1"ERP1="192.168.37.167/32"ERP2="192.168.37.168/32"INTERNAL_LAN="192.168.37.0/24"start(){#################### Qos rule on eth0 #########################$TC qdisc add dev eth1 root tbf rate 512kbit lantency 50ms burst 1540if [ "$LAN_IFACE" != "" ];then $TC qdisc add dev $LAN_IFACE root handle 1:0 cbq bandwidth 100Mbit avpkt 1000 cell 8 $TCclass add dev $LAN_IFACE parent 1:0 classid 1:1 cbq bandwidth 100Mbitrate 2.5Mbit weight 3Mbit prio 8 allot 1514 cell 8 maxburst 20 avpkt1000 bounded $TCclass add dev $LAN_IFACE parent 1:1 classid 1:2 cbq bandwidth 100Mbitrate 1500kbit weight 2Mbit prio 6 allot 1514 cell 8 maxburst 20 avpkt1000 $TCclass add dev $LAN_IFACE parent 1:1 classid 1:3 cbq bandwidth 100Mbitrate 1000kbit weight 1Mbit prio 7 allot 1514 cell 8 maxburst 20 avpkt1000 bounded $TC qdisc add dev $LAN_IFACE parent 1:2 handle 20: sfq $TC qdisc add dev $LAN_IFACE parent 1:3 handle 30: sfq $TC filter add dev $LAN_IFACE parent 1:0 protocol ip prio 2 u32 match ip dst $ERP1 flowid 1:2 $TC filter add dev $LAN_IFACE parent 1:0 protocol ip prio 2 u32 match ip dst $ERP2 flowid 1:2 $TC filter add dev $LAN_IFACE parent 1:0 protocol ip prio 4 u32 match ip dst $INTERNAL_LAN flowid 1:3 echo "" echo "" echo "qos rule on eth0 start ...........ok!" echo "" echo ""fi#################### Qos rule on eth1 ########################if [ "$INET_IFACE" != "" ];then iptables -F -t mangle iptables -X -t mangle iptables -Z -t mangle iptables -A PREROUTING -t mangle -s $ERP1 -j MARK --set-mark 1 iptables -A PREROUTING -t mangle -s $ERP2 -j MARK --set-mark 1 iptables -A PREROUTING -t mangle -s 192.168.37.124/32 -j MARK --set-mark 1 iptables -A PREROUTING -t mangle -s 192.168.37.140/32 -j MARK --set-mark 1 iptables -I PREROUTING -t mangle -s $INTERNAL_LAN -j MARK --set-mark 2 $TC qdisc add dev $INET_IFACE root handle 2:0 cbq bandwidth 100Mbit avpkt 1000 cell 8 $TCclass add dev $INET_IFACE parent 2:0 classid 2:1 cbq bandwidth 100Mbitrate 2Mbit weight 1Mbit prio 8 allot 1514 cell 8 maxburst 20 avpkt 1000bounded $TCclass add dev $INET_IFACE parent 2:1 classid 2:2 cbq bandwidth 100Mbitrate 1500kbit weight 150kbit prio 6 allot 1514 cell 8 maxburst 20 avpkt1000 $TCclass add dev $INET_IFACE parent 2:1 classid 2:3 cbq bandwidth 100Mbitrate 150kbit weight 20kbit prio 7 allot 1514 cell 8 maxburst 20 avpkt1000 bounded $TC qdisc add dev $INET_IFACE parent 2:2 handle 20: sfq $TC qdisc add dev $INET_IFACE parent 2:3 handle 30: sfq $TC filter add dev $INET_IFACE parent 2:0 protocol ip prio 1 handle 1 fw classid 2:2 $TC filter add dev $INET_IFACE parent 2:0 protocol ip prio 2 handle 2 fw classid 2:3 echo "" echo "" echo "qos rule on eth1 start ...........ok!" echo "" echo ""fi}stop(){if [ "$LAN_IFACE" != "" ];then $TC qdisc del dev $LAN_IFACE rootfiif [ "$INET_IFACE" != "" ];then $TC qdisc del dev $INET_IFACE rootfiiptables -F -t mangleiptables -X -t mangleiptables -Z -t mangle}status(){echo "show qdisc ............ "echo ""echo ""echo ""$TC -d -s qdiscecho ""echo ""echo "show filter ............ "echo ""echo ""if [ "$LAN_IFACE" != "" ];then $TC -d -s filter ls dev $LAN_IFACEfiecho ""echo ""if [ "$INET_IFACE" != "" ];then $TC -d -s filter ls dev $INET_IFACEfiecho ""echo ""echo "show class ............ "echo ""echo ""if [ "$LAN_IFACE" != "" ];then $TC -d -s class ls dev $LAN_IFACEfiecho ""echo ""if [ "$INET_IFACE" != "" ];then $TC -d -s class ls dev $INET_IFACEfiecho ""echo ""}case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) echo $"Usage:$0 {start|stop|restart|status}" exit 1esac |