lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # =========================================================== |
| 3 | # usage: tc_control.sh |
| 4 | # traffic control by tc_uplink and tc_downlink |
| 5 | |
| 6 | path_sh=`nv get path_sh` |
| 7 | . $path_sh/global.sh |
| 8 | |
| 9 | echo "Info: tc_tbf $1 $2 start " |
| 10 | echo "Info: tc_tbf $1 $2 start" >> $test_log |
| 11 | |
| 12 | #Á÷¿ØÉÏÏÂÐз§Öµ£¬Îª¿Õ»òΪ0±íʾ²»½øÐÐÁ÷¿Ø£¬ÔÝʱֻʵÏÖÉÏÐеÄtc£¬ÏÂÐн«À´¸ù¾Ýʵ¼ÊÐèÒªÔÙÀ©Õ¹ÊµÏÖ |
| 13 | UPLINK=`nv get tc_uplink` |
| 14 | DOWNLINK=`nv get tc_downlink` |
| 15 | def_cid=`nv get default_cid` |
| 16 | tc_enable=`nv get tc_enable` |
| 17 | |
| 18 | #tc_enable=0£¬Á÷Á¿¿ØÖƹ¦Äܹرգ¬Ö±½ÓÍ˳ö |
| 19 | if [ "$tc_enable" == "0" ]; then |
| 20 | echo "tc_enable=0" |
| 21 | echo "tc_enable=0" >> $test_log |
| 22 | exit 0 |
| 23 | fi |
| 24 | |
| 25 | #ÉÏÏÂÐеijö¿ÚdevÐèÒª¸ù¾Ýʵ¼ÊÇé¿öÑ¡Ôñ |
| 26 | need_jilian=`nv get need_jilian` |
| 27 | lanEnable=`nv get LanEnable` |
| 28 | if [ "$need_jilian" == "1" ]; then |
| 29 | if [ "$lanEnable" == "1" ]; then |
| 30 | IN=`nv get lan_name` |
| 31 | elif [ "$lanEnable" == "0" ]; then |
| 32 | IN=`nv get "ps_ext"$def_cid` |
| 33 | fi |
| 34 | elif [ "$need_jilian" == "0" ]; then |
| 35 | IN=`nv get lan_name` |
| 36 | fi |
| 37 | |
| 38 | #˫ջʱ£¬ipv4ºÍipv6µÄĬÈÏÍâÍø¿Ú¿ÉÄܲ»Ò»Ö£¬ËäÈ»¶ÌÆÚÄÚ¶¼²»»áÓÐʵ¼Ê³¡¾° |
| 39 | OUT4=$defwan_rel |
| 40 | OUT6=$defwan6_rel |
| 41 | |
| 42 | if [ "$lanEnable" == "1" ]; then |
| 43 | GATEWAY=`nv get lan_ipaddr` |
| 44 | fi |
| 45 | |
| 46 | echo "IN=$IN, OUT4=$OUT4, OUT6=$OUT6, GATEWAY=$GATEWAY, DOWNLINK=$DOWNLINK, UPLINK=$UPLINK" |
| 47 | echo "IN=$IN, OUT4=$OUT4, OUT6=$OUT6, GATEWAY=$GATEWAY, DOWNLINK=$DOWNLINK, UPLINK=$UPLINK" >> $test_log |
| 48 | |
| 49 | #Çå¿ÕÔÏȵÄÁ÷³Ì¹æÔò |
| 50 | tc qdisc del dev $IN root |
| 51 | if [ "$OUT4" != "" ]; then |
| 52 | tc qdisc del dev $OUT4 root |
| 53 | fi |
| 54 | if [ "$OUT6" != "" -a "$OUT6" != "$OUT4" ]; then |
| 55 | echo "clear tc for $OUT6" |
| 56 | tc qdisc del dev $OUT6 root |
| 57 | fi |
| 58 | |
| 59 | #¸øÄں˻ָ´¿ìËÙת·¢¼¶±ð |
| 60 | fastnat_level=`nv get fastnat_level` |
| 61 | echo "Info: fastnat_level restore to£º$fastnat_level" >> $test_log |
| 62 | echo $fastnat_level > /proc/net/fastnat_level |
| 63 | |
| 64 | ifconfig $IN txqueuelen 10 |
| 65 | if [ "$OUT4" != "" ]; then |
| 66 | ifconfig $OUT4 txqueuelen 10 |
| 67 | fi |
| 68 | if [ "$OUT6" != "" -a "$OUT6" != "$OUT4" ]; then |
| 69 | ifconfig $OUT6 txqueuelen 10 |
| 70 | fi |
| 71 | |
| 72 | #ÊÊÅä֮ǰµÄ¿Í»§£ºÈç¹û$1²»µÈÓÚdown/DOWN£¬¾Í°´up/UP´¦Àí |
| 73 | if [ "$1" == "down" -o "$1" == "DOWN" ]; then |
| 74 | echo "traffic control down" |
| 75 | echo "traffic control down" >> $test_log |
| 76 | exit 0 |
| 77 | fi |
| 78 | |
| 79 | if [ "$DOWNLINK" == "" -o "$DOWNLINK" == "0" ] && [ "$UPLINK" == "" -o "$UPLINK" == "0" ]; then |
| 80 | echo "no need to traffic control" |
| 81 | echo "no need to traffic control" >> $test_log |
| 82 | exit 0 |
| 83 | fi |
| 84 | |
| 85 | #Ôݶ¨uc/v2¶¼ÐèÒª¹Ø±Õ¿ìËÙת·¢ |
| 86 | |
| 87 | echo 0 > /proc/net/fastnat_level |
| 88 | |
| 89 | if [ "$DOWNLINK" != "0" -a "$DOWNLINK" != "" ]; then |
| 90 | echo "traffic control for down" |
| 91 | echo "traffic control for down" >> $test_log |
| 92 | |
| 93 | LOCAL=`nv get tc_local` |
| 94 | SUM=`expr ${DOWNLINK} + ${LOCAL}` |
| 95 | echo "LOCAL=$LOCAL, SUM=$SUM" |
| 96 | echo "LOCAL=$LOCAL, SUM=$SUM" >> $test_log |
| 97 | |
| 98 | ifconfig $IN txqueuelen 1000 |
| 99 | |
| 100 | #ÏÞËٵĴóСµ¥Î»ËäÈ»ÊÇbps£¬µ«Êµ¼ÊÊÇ×Ö½Ú |
| 101 | tc qdisc add dev $IN root handle 1: htb default 20 |
| 102 | tc class add dev $IN parent 1: classid 1:1 htb rate ${SUM}bps |
| 103 | tc class add dev $IN parent 1:1 classid 1:20 htb rate ${DOWNLINK}bps |
| 104 | tc class add dev $IN parent 1:1 classid 1:10 htb rate ${LOCAL}bps |
| 105 | tc qdisc add dev $IN parent 1:10 handle 10: sfq perturb 10 |
| 106 | tc qdisc add dev $IN parent 1:20 handle 20: sfq perturb 10 |
| 107 | tc filter add dev $IN protocol ip parent 1:0 prio 1 u32 match ip src ${GATEWAY}/32 match ip sport 80 0xffff flowid 1:10 |
| 108 | fi |
| 109 | |
| 110 | if [ "$UPLINK" != "0" -a "$UPLINK" != "" ]; then |
| 111 | if [ "$OUT4" != "" ]; then |
| 112 | echo "traffic control for up - ipv4" |
| 113 | echo "traffic control for up - ipv4" >> $test_log |
| 114 | ifconfig $OUT4 txqueuelen 1000 |
| 115 | tc qdisc add dev $OUT4 root handle 1: htb default 1 |
| 116 | tc class add dev $OUT4 parent 1: classid 1:1 htb rate ${UPLINK}bps |
| 117 | fi |
| 118 | |
| 119 | if [ "$OUT6" != "" -a "$OUT6" != "$OUT4" ]; then |
| 120 | echo "traffic control for up - ipv6" |
| 121 | echo "traffic control for up - ipv6" >> $test_log |
| 122 | ifconfig $OUT6 txqueuelen 1000 |
| 123 | tc qdisc add dev $OUT6 root handle 1: htb default 1 |
| 124 | tc class add dev $OUT6 parent 1: classid 1:1 htb rate ${UPLINK}bps |
| 125 | fi |
| 126 | fi |