#!/bin/sh
if [ "$1" = "stop" ];then
   tc qdisc del dev ens5 root;
   echo "stop success..."
elif [ "$1" = "help" ];then
   echo "./limitRate.sh stop （停止限速）"
   echo "./limitRate.sh 参数1 参数2 参数3 参数4（参数代表：网卡总速 下载限速 允许峰值 限速断口），流量单位:Mbit"
   echo "./limitRate.sh 无参数则默认 100 90 90 9080"
else
   totalRate=$1
   limitRate=$2
   limitMaxRate=$3
   limitPort=$4
   if [ ! $totalRate ];then
      totalRate=100
      echo "totalRate默认100Mbit..."
   fi
   if [ ! $limitRate ];then
      limitRate=85
      echo "limitRate默认90Mbit..."
   fi
   if [ ! $limitMaxRate ];then
      limitMaxRate=85
      echo "limitMaxRate默认90Mbit..."
   fi
   if [ ! $limitPort ];then
      limitPort=9080
      echo "limitPort默认9080..."
   fi
   tc qdisc add dev ens5 root handle 1: htb default 20;
   tc class add dev ens5 parent 1:0 classid 1:1 htb  rate ${totalRate}"Mbit";
   tc class add dev ens5 parent 1:1 classid 1:20 htb rate ${limitRate}"Mbit" ceil ${limitMaxRate}"Mbit" ;
   tc qdisc add dev ens5 parent 1:20 handle 20: sfq perturb 10;
   tc filter add dev ens5 protocol ip parent 1:0 prio 1 u32 match ip dport ${limitPort} 0xffff flowid 1:20
   echo "limitRate start..."
fi
