blob: a893cbab72ca12e04b2cc4b86c6672b25c8c9f2b [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#!/bin/sh
2# ===========================================================
3# usage: tc_control.sh
4# traffic control by tc_uplink and tc_downlink
5
6path_sh=`nv get path_sh`
7. $path_sh/global.sh
8
9echo "Info: tc_tbf $1 $2 start "
10echo "Info: tc_tbf $1 $2 start" >> $test_log
11
12#Á÷¿ØÉÏÏÂÐз§Öµ£¬Îª¿Õ»òΪ0±íʾ²»½øÐÐÁ÷¿Ø£¬ÔÝʱֻʵÏÖÉÏÐеÄtc£¬ÏÂÐн«À´¸ù¾Ýʵ¼ÊÐèÒªÔÙÀ©Õ¹ÊµÏÖ
13UPLINK=`nv get tc_uplink`
14DOWNLINK=`nv get tc_downlink`
15def_cid=`nv get default_cid`
16tc_enable=`nv get tc_enable`
17
18#tc_enable=0£¬Á÷Á¿¿ØÖƹ¦Äܹرգ¬Ö±½ÓÍ˳ö
19if [ "$tc_enable" == "0" ]; then
20 echo "tc_enable=0"
21 echo "tc_enable=0" >> $test_log
22 exit 0
23fi
24
25#ÉÏÏÂÐеijö¿ÚdevÐèÒª¸ù¾Ýʵ¼ÊÇé¿öÑ¡Ôñ
26need_jilian=`nv get need_jilian`
27lanEnable=`nv get LanEnable`
28if [ "$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
34elif [ "$need_jilian" == "0" ]; then
35 IN=`nv get lan_name`
36fi
37
38#˫ջʱ£¬ipv4ºÍipv6µÄĬÈÏÍâÍø¿Ú¿ÉÄܲ»Ò»Ö£¬ËäÈ»¶ÌÆÚÄÚ¶¼²»»áÓÐʵ¼Ê³¡¾°
39OUT4=$defwan_rel
40OUT6=$defwan6_rel
41
42if [ "$lanEnable" == "1" ]; then
43 GATEWAY=`nv get lan_ipaddr`
44fi
45
46echo "IN=$IN, OUT4=$OUT4, OUT6=$OUT6, GATEWAY=$GATEWAY, DOWNLINK=$DOWNLINK, UPLINK=$UPLINK"
47echo "IN=$IN, OUT4=$OUT4, OUT6=$OUT6, GATEWAY=$GATEWAY, DOWNLINK=$DOWNLINK, UPLINK=$UPLINK" >> $test_log
48
49#Çå¿ÕÔ­ÏȵÄÁ÷³Ì¹æÔò
50tc qdisc del dev $IN root
51if [ "$OUT4" != "" ]; then
52 tc qdisc del dev $OUT4 root
53fi
54if [ "$OUT6" != "" -a "$OUT6" != "$OUT4" ]; then
55 echo "clear tc for $OUT6"
56 tc qdisc del dev $OUT6 root
57fi
58
59#¸øÄں˻ָ´¿ìËÙת·¢¼¶±ð
60fastnat_level=`nv get fastnat_level`
61echo "Info: fastnat_level restore to£º$fastnat_level" >> $test_log
62echo $fastnat_level > /proc/net/fastnat_level
63
64ifconfig $IN txqueuelen 10
65if [ "$OUT4" != "" ]; then
66 ifconfig $OUT4 txqueuelen 10
67fi
68if [ "$OUT6" != "" -a "$OUT6" != "$OUT4" ]; then
69 ifconfig $OUT6 txqueuelen 10
70fi
71
72#ÊÊÅä֮ǰµÄ¿Í»§£ºÈç¹û$1²»µÈÓÚdown/DOWN£¬¾Í°´up/UP´¦Àí
73if [ "$1" == "down" -o "$1" == "DOWN" ]; then
74 echo "traffic control down"
75 echo "traffic control down" >> $test_log
76 exit 0
77fi
78
79if [ "$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
83fi
84
85#Ôݶ¨uc/v2¶¼ÐèÒª¹Ø±Õ¿ìËÙת·¢
86
87echo 0 > /proc/net/fastnat_level
88
89if [ "$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
108fi
109
110if [ "$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
126fi