Chinaunix首页 | 论坛 | 博客
  • 博客访问: 246410
  • 博文数量: 79
  • 博客积分: 1942
  • 博客等级: 上尉
  • 技术积分: 910
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-19 16:17
文章分类

全部博文(79)

文章存档

2011年(1)

2010年(50)

2009年(28)

分类: LINUX

2010-05-17 20:40:35

#!/bin/bash
# OUTdoor interface
GREEN_DEV=eth1
# INdoor interface
RED_DEV=eth0
# Upload rate 250kb/s
UPRATE=2000
# Download rate 80kb/s
DOWNRATE=640
start(){
#clean existing down and uplink qdiscs,put the errors to /var/log/htb_log
 tc qdisc del dev $RED_DEV root  2>/dev/null >/dev/null
 tc qdisc del dev $GREEN_DEV root 2>/dev/null >/dev/null
 tc qdisc del dev $RED_DEV ingress 2>/dev/null >/dev/null
 tc qdisc del dev $GREEN_DEV ingress 2>/dev/null >/dev/null
 
 tc qdisc add dev $GREEN_DEV root handle 2: htb
 tc qdisc add dev $RED_DEV root handle 2: htb
 tc class add dev $GREEN_DEV parent 2: classid 2:1 htb rate 256kbit
 tc class add dev $RED_DEV parent 2: classid 2:1 htb rate 256kbit
 i=5;
 while [ $i -le 25 ]
 do
  tc class add dev $GREEN_DEV parent 2:1 classid 2:2$i htb rate ${DOWNRATE}kbit ceil ${DOWNRATE}kbit burst 15k
  tc class add dev $RED_DEV parent 2:1 classid 2:2$i htb rate ${UPRATE}kbit ceil ${UPRATE}kbit burst 15k
  tc qdisc add dev $GREEN_DEV parent 2:2$i handle 2$i: sfq
  tc qdisc add dev $RED_DEV parent 2:2$i handle 2$i: sfq
  tc filter add dev $GREEN_DEV parent 2:0 protocol ip prio 1 u32 match ip dst 192.168.1.$i flowid 2:2$i
  tc filter add dev $RED_DEV parent 2:0 protocol ip prio 1 u32 match ip dst 192.168.1.$i flowid 2:2$i
  i=`expr $i + 1`
 done
}
stop(){ 
#clean existing down and uplink qdiscs,put the errors to /var/log/htb_log
 tc qdisc del dev $RED_DEV root  2>/dev/null >/dev/null
 tc qdisc del dev $GREEN_DEV root 2>/dev/null >/dev/null
}
status() {
        echo "Show qdisc $RED_DEV (Display Upload queue):"
        tc -s qdisc show dev $RED_DEV
        echo "Show class $RED_DEV (Display Upload class):"
        tc class show dev $RED_DEV
        echo "*********************************UP/DOWN*********************************"
 echo "Show qdisc $GREEN_DEV (Display Download queue):"
        tc -s qdisc show dev $GREEN_DEV
        echo "Show class $GREEN_DEV (Display Download class):"
        tc class show dev $GREEN_DEV
}
usage(){
 echo "(usage): `basename $0` [start | stop | restart | status]"
 echo ""
 echo "Argument: functions:"
 echo "start  start traffic-control"
 echo "stop  stop traffic-control"
 echo "restart  restart traffic-control"
 echo "status  show detail of traffic"
}
# Argument
kernel=`uname -r|cut -b 1-3`
case "$kernel" in
 2.4|2.6)
  case "$1" in
   start)
    ( start && echo "TC started!" ) || echo "Error."
    exit 0
    ;;
   stop)
    ( stop && echo "TC stopped!" ) || echo "Error."
    exit 0
    ;;
   restart)
    ( start && echo "TC restarted!" ) || echo "Error."
    exit 0
    ;;
   status)
    status
    ;;
   *)
    usage
    exit 1
    ;;
  esac
   ;;
 *)
  echo " (!) Error: Unsupported Kernel!"
  exit 1
  ;;
esac
阅读(850) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~