blob: 83d779961bb976b52aca0b022373cbb67c35c9af [file] [log] [blame]
developer10cbf2c2022-02-09 10:39:21 +08001#!/bin/sh
2
3OPTIMIZED_FOR="$1"
4CPU_LIST=`cat /proc/interrupts | sed -n '1p'`
5NUM_OF_CPU=0; for i in $CPU_LIST; do NUM_OF_CPU=`expr $NUM_OF_CPU + 1`; done;
6DEFAULT_RPS=0
7
8. /lib/functions.sh
9
10RPS_IF_LIST=""
11NUM_WIFI_CARD=0
12WIFI_RADIO1=0
13WIFI_RADIO2=0
14WIFI_RADIO3=0
developer10d32132022-07-05 15:12:10 +080015WED_ENABLE=0
developer10cbf2c2022-02-09 10:39:21 +080016
developer431102f2023-05-12 10:52:51 +080017WIFI_MODULE_LIST='mt7915e mt7996e'
18
developer10cbf2c2022-02-09 10:39:21 +080019get_if_info()
20{
21 # try to get all wifi and eth net interface.
22 dbg2 "try to get all wifi and eth net interface."
23 NET_IF_LIST=`ls /sys/class/net`
24 for vif in $NET_IF_LIST;
25 do
developerd88f9b62023-02-02 09:51:21 +080026 if [[ "$vif" == "eth"* ]] || \
27 [[ "$vif" == "lan"* ]] || [[ "$vif" == "wan"* ]] || \
28 [[ "$vif" == "wlan"* ]] || [[ "$vif" == "phy"* ]]; then
developer10cbf2c2022-02-09 10:39:21 +080029 RPS_IF_LIST="$RPS_IF_LIST $vif"
30 fi
31 done;
32 dbg2 "RPS_IF_LIST = $RPS_IF_LIST"
33
34 # try to get wifi physical card num.
35 dbg2 "try to get wifi physical card num."
36 VIRTUAL_RADIO_LIST=`ls -l /sys/class/ieee80211/ | awk -F 'devices' '{print $2}' | awk -F 'phy' '{print $1}' | uniq`
37 for v in $VIRTUAL_RADIO_LIST;
38 do
39 NUM_WIFI_CARD=`expr $NUM_WIFI_CARD + 1`
40 dbg2 "physical raido $v"
41 if [[ $v == *"wmac"* ]]; then
42 WIFI_RADIO1=1
43 fi
developer69b0f422023-01-06 15:37:54 +080044
developer10cbf2c2022-02-09 10:39:21 +080045 if [[ $v == *"wbsys"* ]]; then
developer69b0f422023-01-06 15:37:54 +080046 WIFI_RADIO1=1
47 fi
developer10cbf2c2022-02-09 10:39:21 +080048
49 if [[ $v == *"pci0000"* ]]; then
50 WIFI_RADIO2=1
51 fi
52
53 if [[ $v == *"pci0001"* ]]; then
54 WIFI_RADIO3=1
55 fi
56 done;
57
developer431102f2023-05-12 10:52:51 +080058
59 for v in $WIFI_MODULE_LIST;
developer10d32132022-07-05 15:12:10 +080060 do
developer431102f2023-05-12 10:52:51 +080061 if [[ -f "/sys/module/$v/parameters/wed_enable" ]]; then
62 WED_ENABLE_LIST=`cat /sys/module/$v/parameters/wed_enable`
63 dbg2 "wed enable ori info $v $WED_ENABLE_LIST"
64 if [[ $WED_ENABLE_LIST == "Y" ]]; then
65 WED_ENABLE=1
66 fi
developer10d32132022-07-05 15:12:10 +080067 fi
68 done;
developer10cbf2c2022-02-09 10:39:21 +080069 dbg2 "NUM_WIFI_CARD = $NUM_WIFI_CARD"
70 dbg2 "platform wifi enable = $WIFI_RADIO1"
71 dbg2 "pcie1 wifi enable = $WIFI_RADIO2"
72 dbg2 "pcie2 wifi enable = $WIFI_RADIO3"
developer10d32132022-07-05 15:12:10 +080073 dbg2 "WED enable = $WED_ENABLE"
developer10cbf2c2022-02-09 10:39:21 +080074}
75
76# $1: CPU#
77# $2: irq list for added.
78CPU_AFFINITY_ADD()
79{
80 eval oval=\$CPU${1}_AFFINITY
81 eval CPU${1}_AFFINITY=\"\$CPU${1}_AFFINITY $2\"
82}
83
84# $1: CPU#
85# $2: Interface name for added.
86CPU_RPS_ADD()
87{
88 eval oval=\$CPU${1}_RPS
89 eval CPU${1}_RPS=\"\$CPU${1}_RPS $2\"
90 dbg2 "CPU${1}_RPS=\"\$CPU${1}_RPS $2\""
91}
92
developer69b0f422023-01-06 15:37:54 +080093MT7988()
94{
95 num_of_wifi=$1
96 DEFAULT_RPS=0
97
98 #Physical IRQ# setting
developer260bb6d2023-05-24 13:59:37 +080099 #Ethernet RSS feature enables 4 Rx rings
100 eth_irq_rx0=221
101 eth_irq_rx1=222
102 eth_irq_rx2=223
103 eth_irq_rx3=224
104 eth_irq_tx=229
developer69b0f422023-01-06 15:37:54 +0800105 wifi1_irq_pcie0=524288
106 wifi1_irq_pcie1=134742016
107 wifi2_irq_pcie0=
108 wifi2_irq_pcie1=
109
110 if [[ "$WED_ENABLE" -eq "1" ]]; then
111 dbg2 "WED_ENABLE ON irq/iptable setting"
112 #TCP Binding
113 iptables -D FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw
114 iptables -I FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw
115 ip6tables -D FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw
116 ip6tables -I FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw
117 #UDP Binding
118 iptables -D FORWARD -p udp -j FLOWOFFLOAD --hw
119 iptables -I FORWARD -p udp -j FLOWOFFLOAD --hw
120 ip6tables -D FORWARD -p udp -j FLOWOFFLOAD --hw
121 ip6tables -I FORWARD -p udp -j FLOWOFFLOAD --hw
122
123 else
124 dbg2 "WED_ENABLE OFF irq/iptable seting"
125 fi
126
127 for vif in $NET_IF_LIST;
128 do
developerd88f9b62023-02-02 09:51:21 +0800129 if [[ "$vif" == "wlan"* ]] || [[ "$vif" == "phy"* ]]; then
developer69b0f422023-01-06 15:37:54 +0800130 WIFI_IF_LIST="$WIFI_IF_LIST $vif"
131 fi
132 done;
133 dbg2 "$WIFI_IF_LIST = $WIFI_IF_LIST"
134 # Please update the CPU binding in each cases.
135 # CPU#_AFFINITY="add binding irq number here"
136 # CPU#_RPS="add binding interface name here"
137 if [ "$num_of_wifi" = "0" ]; then
developer260bb6d2023-05-24 13:59:37 +0800138 CPU0_AFFINITY="$eth_irq_rx0 $eth_irq_tx"
139 CPU1_AFFINITY="$eth_irq_rx1"
140 CPU2_AFFINITY="$eth_irq_rx2"
141 CPU3_AFFINITY="$eth_irq_rx3"
developer69b0f422023-01-06 15:37:54 +0800142
143 CPU0_RPS="$RPS_IF_LIST"
144 CPU1_RPS="$RPS_IF_LIST"
145 CPU2_RPS="$RPS_IF_LIST"
146 CPU3_RPS="$RPS_IF_LIST"
147 else
148 #we bound all wifi card to cpu0 and bound eth to cpu
149 CPU0_AFFINITY=""
150 CPU1_AFFINITY=""
developer260bb6d2023-05-24 13:59:37 +0800151 CPU2_AFFINITY="$eth_irq_rx0 $eth_irq_rx1 $eth_irq_tx"
152 CPU3_AFFINITY="$eth_irq_rx2 $eth_irq_rx3"
developer69b0f422023-01-06 15:37:54 +0800153
154 CPU0_RPS="$WIFI_IF_LIST"
155 CPU1_RPS="$WIFI_IF_LIST"
developer6d638dd2023-02-21 13:46:21 +0800156 CPU2_RPS=""
developer69b0f422023-01-06 15:37:54 +0800157 CPU3_RPS=""
158 fi
159 dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY"
160 dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY"
161 dbg2 "CPU2_AFFINITY = $CPU2_AFFINITY"
162 dbg2 "CPU3_AFFINITY = $CPU3_AFFINITY"
163}
164
developer10cbf2c2022-02-09 10:39:21 +0800165MT7986()
166{
167 num_of_wifi=$1
168 DEFAULT_RPS=0
169
170 #Physical IRQ# setting
developer260bb6d2023-05-24 13:59:37 +0800171 eth_irq_rx=221
172 eth_irq_tx=229
developer10cbf2c2022-02-09 10:39:21 +0800173 wifi1_irq=
174 wifi2_irq=
175 wifi3_irq=
developer10d32132022-07-05 15:12:10 +0800176
177 if [[ "$WED_ENABLE" -eq "1" ]]; then
178 dbg2 "WED_ENABLE ON irq/iptable setting"
179 #TCP Binding
developer6adfa0e2022-07-06 16:25:53 +0800180 iptables -D FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw
181 iptables -I FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw
182 ip6tables -D FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw
183 ip6tables -I FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw
developer10d32132022-07-05 15:12:10 +0800184 #UDP Binding
developer6adfa0e2022-07-06 16:25:53 +0800185 iptables -D FORWARD -p udp -j FLOWOFFLOAD --hw
186 iptables -I FORWARD -p udp -j FLOWOFFLOAD --hw
187 ip6tables -D FORWARD -p udp -j FLOWOFFLOAD --hw
188 ip6tables -I FORWARD -p udp -j FLOWOFFLOAD --hw
developer10d32132022-07-05 15:12:10 +0800189
190 #AX6000 AX7800 - SOC
191 if [[ "$WIFI_RADIO1" -eq "1" ]]; then
192 wifi1_irq=238
193 fi
194 #AX7800 - PCIE0
195 if [[ "$WIFI_RADIO2" -eq "1" ]]; then
196 wifi2_irq=237
197 fi
198 #AX7800 - PCIE1
199 #if [[ "$WIFI_RADIO3" -eq "1" ]]; then
200 # wifi3_irq=239
201 #fi
202 else
203 dbg2 "WED_ENABLE OFF irq/iptable seting"
204 #AX6000 AX7800 - SOC
205 if [[ "$WIFI_RADIO1" -eq "1" ]]; then
206 wifi1_irq=245
207 fi
208 #AX7800 - PCIE0
209 if [[ "$WIFI_RADIO2" -eq "1" ]]; then
210 wifi2_irq=246
211 fi
212 #AX7800 - PCIE1
213 #if [[ "$WIFI_RADIO3" -eq "1" ]]; then
214 # wifi3_irq=247
215 #fi
developer10cbf2c2022-02-09 10:39:21 +0800216 fi
217 # Please update the CPU binding in each cases.
218 # CPU#_AFFINITY="add binding irq number here"
219 # CPU#_RPS="add binding interface name here"
220 if [ "$num_of_wifi" = "0" ]; then
developer260bb6d2023-05-24 13:59:37 +0800221 CPU0_AFFINITY="$eth_irq_rx"
222 CPU1_AFFINITY="$eth_irq_tx"
developer10cbf2c2022-02-09 10:39:21 +0800223 CPU2_AFFINITY=""
224 CPU3_AFFINITY=""
225
226 CPU0_RPS="$RPS_IF_LIST"
227 CPU1_RPS="$RPS_IF_LIST"
228 CPU2_RPS="$RPS_IF_LIST"
229 CPU3_RPS="$RPS_IF_LIST"
230 else
231 #we bound all wifi card to cpu1 and bound eth to cpu0
developer260bb6d2023-05-24 13:59:37 +0800232 CPU0_AFFINITY="$eth_irq_rx"
233 CPU1_AFFINITY="$eth_irq_tx"
developer10cbf2c2022-02-09 10:39:21 +0800234 CPU2_AFFINITY="$wifi2_irq $wifi3_irq"
235 CPU3_AFFINITY="$wifi1_irq"
236
237 CPU0_RPS="$RPS_IF_LIST"
238 CPU1_RPS="$RPS_IF_LIST"
239 CPU2_RPS="$RPS_IF_LIST"
240 CPU3_RPS="$RPS_IF_LIST"
241 fi
242 dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY"
243 dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY"
244 dbg2 "CPU2_AFFINITY = $CPU2_AFFINITY"
245 dbg2 "CPU3_AFFINITY = $CPU3_AFFINITY"
246}
247
248MT7981()
249{
250 num_of_wifi=$1
251 DEFAULT_RPS=0
252
253 #Physical IRQ# setting
developer260bb6d2023-05-24 13:59:37 +0800254 eth_irq_rx=221
255 eth_irq_tx=229
developer10cbf2c2022-02-09 10:39:21 +0800256 wifi1_irq=
257 wifi2_irq=
258 wifi3_irq=
259 #AX3000
260 if [[ "$WIFI_RADIO1" -eq "1" ]]; then
261 wifi1_irq=245
262 fi
263 # Please update the CPU binding in each cases.
264 # CPU#_AFFINITY="add binding irq number here"
265 # CPU#_RPS="add binding interface name here"
266 if [ "$num_of_wifi" = "0" ]; then
developer260bb6d2023-05-24 13:59:37 +0800267 CPU0_AFFINITY="$eth_irq_rx"
268 CPU1_AFFINITY="$eth_irq_tx"
developer10cbf2c2022-02-09 10:39:21 +0800269
270 CPU0_RPS="$RPS_IF_LIST"
271 CPU1_RPS="$RPS_IF_LIST"
272 else
273 #we bound all wifi card to cpu0 and bound eth to cpu1
274 CPU0_AFFINITY="$wifi1_irq $wifi2_irq $wifi3_irq"
developer260bb6d2023-05-24 13:59:37 +0800275 CPU1_AFFINITY="$eth_irq_rx $eth_irq_tx"
developer10cbf2c2022-02-09 10:39:21 +0800276
277 CPU0_RPS="$RPS_IF_LIST"
278 CPU1_RPS="$RPS_IF_LIST"
279 fi
280 dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY"
281 dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY"
282}
283
284MT7622()
285{
286 num_of_wifi=$1
287 DEFAULT_RPS=0
288
289 #Physical IRQ# setting
290 eth0_irq=224
291 eth1_irq=225
292 wifi1_irq=
293 wifi2_irq=
294 wifi3_irq=
295 #AX1200 AX3200
296 if [[ "$WIFI_RADIO1" -eq "1" ]]; then
297 wifi1_irq=211
298 fi
299 #AX1800 AX3200
300 if [[ "$WIFI_RADIO2" -eq "1" ]]; then
301 wifi2_irq=214
302 fi
303 #AX3600
304 if [[ "$WIFI_RADIO3" -eq "1" ]]; then
305 wifi3_irq=215
306 fi
307
308 # Please update the CPU binding in each cases.
309 # CPU#_AFFINITY="add binding irq number here"
310 # CPU#_RPS="add binding interface name here"
311 if [ "$num_of_wifi" == "0" ]; then
312 CPU0_AFFINITY="$eth0_irq"
313 CPU1_AFFINITY="$eth1_irq"
314
315 CPU0_RPS="$RPS_IF_LIST"
316 CPU1_RPS="$RPS_IF_LIST"
317 else
318 #we bound all wifi card to cpu0 and bound eth to cpu1
319 CPU0_AFFINITY="$wifi1_irq $wifi2_irq $wifi3_irq"
320 CPU1_AFFINITY="$eth0_irq $eth1_irq"
321
322 CPU0_RPS="$RPS_IF_LIST"
323 CPU1_RPS="$RPS_IF_LIST"
324 fi
325
326 dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY"
327 dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY"
328}
329
330setup_model()
331{
332 board=$(board_name)
333 num_of_wifi=$NUM_WIFI_CARD
334
developerd88f9b62023-02-02 09:51:21 +0800335 if [[ $board == *"7988"* ]]; then
developer69b0f422023-01-06 15:37:54 +0800336 dbg "setup_model: MT7988 NUM_WIFI_CARD=$num_of_wifi"
337 MT7988 $num_of_wifi
developerd88f9b62023-02-02 09:51:21 +0800338 elif [[ $board == *"7986"* ]]; then
developer10cbf2c2022-02-09 10:39:21 +0800339 dbg "setup_model: MT7986 NUM_WIFI_CARD=$num_of_wifi"
340 MT7986 $num_of_wifi
developerd88f9b62023-02-02 09:51:21 +0800341 elif [[ $board == *"7981"* ]]; then
developer10cbf2c2022-02-09 10:39:21 +0800342 dbg "setup_model: MT7981 NUM_WIFI_CARD=$num_of_wifi"
343 MT7981 $num_of_wifi
developerd88f9b62023-02-02 09:51:21 +0800344 elif [[ $board == *"7622"* ]]; then
developer10cbf2c2022-02-09 10:39:21 +0800345 dbg "setup_model: MT7622 NUM_WIFI_CARD=$num_of_wifi"
346 MT7622 $num_of_wifi
347 fi
348}
349
350get_virtual_irq()
351{
352 PHY_POS=`expr $NUM_OF_CPU + 3` #physical irq # position in /proc/interrups may vary with the number of CPU up
353 target_phy_irq=$1
354 cat /proc/interrupts | sed 's/:/ /g'| awk '$1 ~ /^[0-9]+$/' | while read line
355 do
356 set -- $line
357 phy_irq=$(eval "echo \$$PHY_POS")
358 if [ $phy_irq == $target_phy_irq ]; then
359 echo $1
360 return
361 fi
362 done
363}
364
365set_rps_cpu_bitmap()
366{
367 dbg2 "# Scan binding interfaces of each cpu"
368 # suppose the value of interface_var is null or hex
369 num=0
370 while [ "$num" -lt "$NUM_OF_CPU" ];do
371 cpu_bit=$((2 ** $num))
372 eval rps_list=\$CPU${num}_RPS
373 dbg2 "# CPU$num: rps_list=$rps_list"
374 for i in $rps_list; do
375 var=${VAR_PREFIX}_${i//-/_}
376 eval ifval=\$$var
377 dbg2 "[var val before] \$$var=$ifval"
378 if [ -z "$ifval" ]; then
379 eval $var=$cpu_bit
380 else
381 eval $var=`expr $ifval + $cpu_bit`
382 fi
383 eval ifval=\$$var
384 dbg2 "[rps val after]$i=$ifval"
385 done
386 num=`expr $num + 1`
387 done
388}
389
390# $1: The default rps value. If rps of the interface is not setup, set $1 to it
391set_rps_cpus()
392{
393 dbg2 "# Setup rps of the interfaces, $RPS_IF_LIST."
394 for i in $RPS_IF_LIST; do
395 var=${VAR_PREFIX}_${i//-/_}
396 eval cpu_map=\$$var
397 if [ -d /sys/class/net/$i ]; then
398 if [ ! -z $cpu_map ]; then
399 cpu_map=`printf '%x' $cpu_map`
400 dbg "echo $cpu_map > /sys/class/net/$i/queues/rx-0/rps_cpus"
401 echo $cpu_map > /sys/class/net/$i/queues/rx-0/rps_cpus
402 elif [ ! -z $1 ]; then
403 dbg "echo $1 > /sys/class/net/$i/queues/rx-0/rps_cpus"
404 echo $1 > /sys/class/net/$i/queues/rx-0/rps_cpus
405 fi
406 fi
407 done
408}
409
410set_smp_affinity()
411{
412 dbg2 "# Setup affinity of each physical irq."
413 num=0
414 while [ "$num" -lt "$NUM_OF_CPU" ];do
415 eval smp_list=\$CPU${num}_AFFINITY
416 for i in $smp_list; do
417 cpu_bit=$((2 ** $num))
418 virq=$(get_virtual_irq $i)
419 #virq=$i
420 dbg2 "irq p2v $i --> $virq"
421 if [ ! -z $virq ]; then
422 dbg "echo $cpu_bit > /proc/irq/$virq/smp_affinity"
423 echo $cpu_bit > /proc/irq/$virq/smp_affinity
424 fi
425 done
426 num=`expr $num + 1`
427 done
428}
429
430if [ "$1" = "dbg" ]; then
431 DBG=1
432elif [ "$1" = "dbg2" ]; then
433 DBG=2
434else
435 DBG=0
436fi
437
438# Usage: dbg "the output string"
439dbg()
440{
441 if [ "$DBG" -ge "1" ]; then
442 echo -e $1
443 fi
444}
445
446# Usage: dbg2 "the output string"
447dbg2()
448{
449 if [ "$DBG" -ge "2" ]; then
450 echo -e $1
451 fi
452}
453
454dbg "# RPS and AFFINITY Setting"
455dbg "# NUM_OF_CPU=$NUM_OF_CPU"
456VAR_PREFIX="autogen"
457get_if_info
458setup_model
459set_rps_cpu_bitmap
460set_rps_cpus $DEFAULT_RPS
461set_smp_affinity
462#end of file