developer | 10cbf2c | 2022-02-09 10:39:21 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | OPTIMIZED_FOR="$1" |
| 4 | CPU_LIST=`cat /proc/interrupts | sed -n '1p'` |
| 5 | NUM_OF_CPU=0; for i in $CPU_LIST; do NUM_OF_CPU=`expr $NUM_OF_CPU + 1`; done; |
| 6 | DEFAULT_RPS=0 |
| 7 | |
| 8 | . /lib/functions.sh |
| 9 | |
| 10 | RPS_IF_LIST="" |
| 11 | NUM_WIFI_CARD=0 |
| 12 | WIFI_RADIO1=0 |
| 13 | WIFI_RADIO2=0 |
| 14 | WIFI_RADIO3=0 |
developer | 10d3213 | 2022-07-05 15:12:10 +0800 | [diff] [blame] | 15 | WED_ENABLE=0 |
developer | 10cbf2c | 2022-02-09 10:39:21 +0800 | [diff] [blame] | 16 | |
| 17 | get_if_info() |
| 18 | { |
| 19 | # try to get all wifi and eth net interface. |
| 20 | dbg2 "try to get all wifi and eth net interface." |
| 21 | NET_IF_LIST=`ls /sys/class/net` |
| 22 | for vif in $NET_IF_LIST; |
| 23 | do |
| 24 | if [[ "$vif" == "eth0*" ]] || [[ "$vif" == "eth1" ]] || \ |
| 25 | [[ "$vif" == "lan*" ]] || [[ "$vif" == "wan*" ]] || \ |
developer | 7273f5f | 2022-12-12 17:18:16 +0800 | [diff] [blame] | 26 | [[ "$vif" == "wlan*" ]] || [[ "$vif" == "phy*" ]]; then |
developer | 10cbf2c | 2022-02-09 10:39:21 +0800 | [diff] [blame] | 27 | RPS_IF_LIST="$RPS_IF_LIST $vif" |
| 28 | fi |
| 29 | done; |
| 30 | dbg2 "RPS_IF_LIST = $RPS_IF_LIST" |
| 31 | |
| 32 | # try to get wifi physical card num. |
| 33 | dbg2 "try to get wifi physical card num." |
| 34 | VIRTUAL_RADIO_LIST=`ls -l /sys/class/ieee80211/ | awk -F 'devices' '{print $2}' | awk -F 'phy' '{print $1}' | uniq` |
| 35 | for v in $VIRTUAL_RADIO_LIST; |
| 36 | do |
| 37 | NUM_WIFI_CARD=`expr $NUM_WIFI_CARD + 1` |
| 38 | dbg2 "physical raido $v" |
| 39 | if [[ $v == *"wmac"* ]]; then |
| 40 | WIFI_RADIO1=1 |
| 41 | fi |
| 42 | if [[ $v == *"wbsys"* ]]; then |
| 43 | WIFI_RADIO1=1 |
| 44 | fi |
| 45 | |
| 46 | if [[ $v == *"pci0000"* ]]; then |
| 47 | WIFI_RADIO2=1 |
| 48 | fi |
| 49 | |
| 50 | if [[ $v == *"pci0001"* ]]; then |
| 51 | WIFI_RADIO3=1 |
| 52 | fi |
| 53 | done; |
| 54 | |
developer | 10d3213 | 2022-07-05 15:12:10 +0800 | [diff] [blame] | 55 | WED_ENABLE_LIST=`cat /sys/module/mt7915e/parameters/wed_enable` |
| 56 | for v in $WED_ENABLE_LIST; |
| 57 | do |
| 58 | dbg2 "wed enable ori info $v" |
| 59 | if [[ $v == "Y" ]]; then |
| 60 | WED_ENABLE=1 |
| 61 | fi |
| 62 | done; |
developer | 10cbf2c | 2022-02-09 10:39:21 +0800 | [diff] [blame] | 63 | dbg2 "NUM_WIFI_CARD = $NUM_WIFI_CARD" |
| 64 | dbg2 "platform wifi enable = $WIFI_RADIO1" |
| 65 | dbg2 "pcie1 wifi enable = $WIFI_RADIO2" |
| 66 | dbg2 "pcie2 wifi enable = $WIFI_RADIO3" |
developer | 10d3213 | 2022-07-05 15:12:10 +0800 | [diff] [blame] | 67 | dbg2 "WED enable = $WED_ENABLE" |
developer | 10cbf2c | 2022-02-09 10:39:21 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | # $1: CPU# |
| 71 | # $2: irq list for added. |
| 72 | CPU_AFFINITY_ADD() |
| 73 | { |
| 74 | eval oval=\$CPU${1}_AFFINITY |
| 75 | eval CPU${1}_AFFINITY=\"\$CPU${1}_AFFINITY $2\" |
| 76 | } |
| 77 | |
| 78 | # $1: CPU# |
| 79 | # $2: Interface name for added. |
| 80 | CPU_RPS_ADD() |
| 81 | { |
| 82 | eval oval=\$CPU${1}_RPS |
| 83 | eval CPU${1}_RPS=\"\$CPU${1}_RPS $2\" |
| 84 | dbg2 "CPU${1}_RPS=\"\$CPU${1}_RPS $2\"" |
| 85 | } |
| 86 | |
| 87 | MT7986() |
| 88 | { |
| 89 | num_of_wifi=$1 |
| 90 | DEFAULT_RPS=0 |
| 91 | |
| 92 | #Physical IRQ# setting |
| 93 | eth0_irq=229 |
| 94 | eth1_irq=230 |
| 95 | wifi1_irq= |
| 96 | wifi2_irq= |
| 97 | wifi3_irq= |
developer | 10d3213 | 2022-07-05 15:12:10 +0800 | [diff] [blame] | 98 | |
| 99 | if [[ "$WED_ENABLE" -eq "1" ]]; then |
| 100 | dbg2 "WED_ENABLE ON irq/iptable setting" |
| 101 | #TCP Binding |
developer | 6adfa0e | 2022-07-06 16:25:53 +0800 | [diff] [blame] | 102 | iptables -D FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw |
| 103 | iptables -I FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw |
| 104 | ip6tables -D FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw |
| 105 | ip6tables -I FORWARD -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw |
developer | 10d3213 | 2022-07-05 15:12:10 +0800 | [diff] [blame] | 106 | #UDP Binding |
developer | 6adfa0e | 2022-07-06 16:25:53 +0800 | [diff] [blame] | 107 | iptables -D FORWARD -p udp -j FLOWOFFLOAD --hw |
| 108 | iptables -I FORWARD -p udp -j FLOWOFFLOAD --hw |
| 109 | ip6tables -D FORWARD -p udp -j FLOWOFFLOAD --hw |
| 110 | ip6tables -I FORWARD -p udp -j FLOWOFFLOAD --hw |
developer | 10d3213 | 2022-07-05 15:12:10 +0800 | [diff] [blame] | 111 | |
| 112 | #AX6000 AX7800 - SOC |
| 113 | if [[ "$WIFI_RADIO1" -eq "1" ]]; then |
| 114 | wifi1_irq=238 |
| 115 | fi |
| 116 | #AX7800 - PCIE0 |
| 117 | if [[ "$WIFI_RADIO2" -eq "1" ]]; then |
| 118 | wifi2_irq=237 |
| 119 | fi |
| 120 | #AX7800 - PCIE1 |
| 121 | #if [[ "$WIFI_RADIO3" -eq "1" ]]; then |
| 122 | # wifi3_irq=239 |
| 123 | #fi |
| 124 | else |
| 125 | dbg2 "WED_ENABLE OFF irq/iptable seting" |
| 126 | #AX6000 AX7800 - SOC |
| 127 | if [[ "$WIFI_RADIO1" -eq "1" ]]; then |
| 128 | wifi1_irq=245 |
| 129 | fi |
| 130 | #AX7800 - PCIE0 |
| 131 | if [[ "$WIFI_RADIO2" -eq "1" ]]; then |
| 132 | wifi2_irq=246 |
| 133 | fi |
| 134 | #AX7800 - PCIE1 |
| 135 | #if [[ "$WIFI_RADIO3" -eq "1" ]]; then |
| 136 | # wifi3_irq=247 |
| 137 | #fi |
developer | 10cbf2c | 2022-02-09 10:39:21 +0800 | [diff] [blame] | 138 | fi |
| 139 | # Please update the CPU binding in each cases. |
| 140 | # CPU#_AFFINITY="add binding irq number here" |
| 141 | # CPU#_RPS="add binding interface name here" |
| 142 | if [ "$num_of_wifi" = "0" ]; then |
| 143 | CPU0_AFFINITY="$eth0_irq" |
| 144 | CPU1_AFFINITY="$eth1_irq" |
| 145 | CPU2_AFFINITY="" |
| 146 | CPU3_AFFINITY="" |
| 147 | |
| 148 | CPU0_RPS="$RPS_IF_LIST" |
| 149 | CPU1_RPS="$RPS_IF_LIST" |
| 150 | CPU2_RPS="$RPS_IF_LIST" |
| 151 | CPU3_RPS="$RPS_IF_LIST" |
| 152 | else |
| 153 | #we bound all wifi card to cpu1 and bound eth to cpu0 |
| 154 | CPU0_AFFINITY="$eth0_irq" |
| 155 | CPU1_AFFINITY="$eth1_irq" |
| 156 | CPU2_AFFINITY="$wifi2_irq $wifi3_irq" |
| 157 | CPU3_AFFINITY="$wifi1_irq" |
| 158 | |
| 159 | CPU0_RPS="$RPS_IF_LIST" |
| 160 | CPU1_RPS="$RPS_IF_LIST" |
| 161 | CPU2_RPS="$RPS_IF_LIST" |
| 162 | CPU3_RPS="$RPS_IF_LIST" |
| 163 | fi |
| 164 | dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY" |
| 165 | dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY" |
| 166 | dbg2 "CPU2_AFFINITY = $CPU2_AFFINITY" |
| 167 | dbg2 "CPU3_AFFINITY = $CPU3_AFFINITY" |
| 168 | } |
| 169 | |
| 170 | MT7981() |
| 171 | { |
| 172 | num_of_wifi=$1 |
| 173 | DEFAULT_RPS=0 |
| 174 | |
| 175 | #Physical IRQ# setting |
| 176 | eth0_irq=229 |
| 177 | eth1_irq=230 |
| 178 | wifi1_irq= |
| 179 | wifi2_irq= |
| 180 | wifi3_irq= |
| 181 | #AX3000 |
| 182 | if [[ "$WIFI_RADIO1" -eq "1" ]]; then |
| 183 | wifi1_irq=245 |
| 184 | fi |
| 185 | # Please update the CPU binding in each cases. |
| 186 | # CPU#_AFFINITY="add binding irq number here" |
| 187 | # CPU#_RPS="add binding interface name here" |
| 188 | if [ "$num_of_wifi" = "0" ]; then |
| 189 | CPU0_AFFINITY="$eth0_irq" |
| 190 | CPU1_AFFINITY="$eth1_irq" |
| 191 | |
| 192 | CPU0_RPS="$RPS_IF_LIST" |
| 193 | CPU1_RPS="$RPS_IF_LIST" |
| 194 | else |
| 195 | #we bound all wifi card to cpu0 and bound eth to cpu1 |
| 196 | CPU0_AFFINITY="$wifi1_irq $wifi2_irq $wifi3_irq" |
| 197 | CPU1_AFFINITY="$eth0_irq $eth1_irq" |
| 198 | |
| 199 | CPU0_RPS="$RPS_IF_LIST" |
| 200 | CPU1_RPS="$RPS_IF_LIST" |
| 201 | fi |
| 202 | dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY" |
| 203 | dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY" |
| 204 | } |
| 205 | |
| 206 | MT7622() |
| 207 | { |
| 208 | num_of_wifi=$1 |
| 209 | DEFAULT_RPS=0 |
| 210 | |
| 211 | #Physical IRQ# setting |
| 212 | eth0_irq=224 |
| 213 | eth1_irq=225 |
| 214 | wifi1_irq= |
| 215 | wifi2_irq= |
| 216 | wifi3_irq= |
| 217 | #AX1200 AX3200 |
| 218 | if [[ "$WIFI_RADIO1" -eq "1" ]]; then |
| 219 | wifi1_irq=211 |
| 220 | fi |
| 221 | #AX1800 AX3200 |
| 222 | if [[ "$WIFI_RADIO2" -eq "1" ]]; then |
| 223 | wifi2_irq=214 |
| 224 | fi |
| 225 | #AX3600 |
| 226 | if [[ "$WIFI_RADIO3" -eq "1" ]]; then |
| 227 | wifi3_irq=215 |
| 228 | fi |
| 229 | |
| 230 | # Please update the CPU binding in each cases. |
| 231 | # CPU#_AFFINITY="add binding irq number here" |
| 232 | # CPU#_RPS="add binding interface name here" |
| 233 | if [ "$num_of_wifi" == "0" ]; then |
| 234 | CPU0_AFFINITY="$eth0_irq" |
| 235 | CPU1_AFFINITY="$eth1_irq" |
| 236 | |
| 237 | CPU0_RPS="$RPS_IF_LIST" |
| 238 | CPU1_RPS="$RPS_IF_LIST" |
| 239 | else |
| 240 | #we bound all wifi card to cpu0 and bound eth to cpu1 |
| 241 | CPU0_AFFINITY="$wifi1_irq $wifi2_irq $wifi3_irq" |
| 242 | CPU1_AFFINITY="$eth0_irq $eth1_irq" |
| 243 | |
| 244 | CPU0_RPS="$RPS_IF_LIST" |
| 245 | CPU1_RPS="$RPS_IF_LIST" |
| 246 | fi |
| 247 | |
| 248 | dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY" |
| 249 | dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY" |
| 250 | } |
| 251 | |
| 252 | setup_model() |
| 253 | { |
| 254 | board=$(board_name) |
| 255 | num_of_wifi=$NUM_WIFI_CARD |
| 256 | |
| 257 | if [[ $board == "*7986*" ]]; then |
| 258 | dbg "setup_model: MT7986 NUM_WIFI_CARD=$num_of_wifi" |
| 259 | MT7986 $num_of_wifi |
| 260 | elif [[ $board == "*7981*" ]]; then |
| 261 | dbg "setup_model: MT7981 NUM_WIFI_CARD=$num_of_wifi" |
| 262 | MT7981 $num_of_wifi |
| 263 | elif [[ $board == "*7622*" ]]; then |
| 264 | dbg "setup_model: MT7622 NUM_WIFI_CARD=$num_of_wifi" |
| 265 | MT7622 $num_of_wifi |
| 266 | fi |
| 267 | } |
| 268 | |
| 269 | get_virtual_irq() |
| 270 | { |
| 271 | PHY_POS=`expr $NUM_OF_CPU + 3` #physical irq # position in /proc/interrups may vary with the number of CPU up |
| 272 | target_phy_irq=$1 |
| 273 | cat /proc/interrupts | sed 's/:/ /g'| awk '$1 ~ /^[0-9]+$/' | while read line |
| 274 | do |
| 275 | set -- $line |
| 276 | phy_irq=$(eval "echo \$$PHY_POS") |
| 277 | if [ $phy_irq == $target_phy_irq ]; then |
| 278 | echo $1 |
| 279 | return |
| 280 | fi |
| 281 | done |
| 282 | } |
| 283 | |
| 284 | set_rps_cpu_bitmap() |
| 285 | { |
| 286 | dbg2 "# Scan binding interfaces of each cpu" |
| 287 | # suppose the value of interface_var is null or hex |
| 288 | num=0 |
| 289 | while [ "$num" -lt "$NUM_OF_CPU" ];do |
| 290 | cpu_bit=$((2 ** $num)) |
| 291 | eval rps_list=\$CPU${num}_RPS |
| 292 | dbg2 "# CPU$num: rps_list=$rps_list" |
| 293 | for i in $rps_list; do |
| 294 | var=${VAR_PREFIX}_${i//-/_} |
| 295 | eval ifval=\$$var |
| 296 | dbg2 "[var val before] \$$var=$ifval" |
| 297 | if [ -z "$ifval" ]; then |
| 298 | eval $var=$cpu_bit |
| 299 | else |
| 300 | eval $var=`expr $ifval + $cpu_bit` |
| 301 | fi |
| 302 | eval ifval=\$$var |
| 303 | dbg2 "[rps val after]$i=$ifval" |
| 304 | done |
| 305 | num=`expr $num + 1` |
| 306 | done |
| 307 | } |
| 308 | |
| 309 | # $1: The default rps value. If rps of the interface is not setup, set $1 to it |
| 310 | set_rps_cpus() |
| 311 | { |
| 312 | dbg2 "# Setup rps of the interfaces, $RPS_IF_LIST." |
| 313 | for i in $RPS_IF_LIST; do |
| 314 | var=${VAR_PREFIX}_${i//-/_} |
| 315 | eval cpu_map=\$$var |
| 316 | if [ -d /sys/class/net/$i ]; then |
| 317 | if [ ! -z $cpu_map ]; then |
| 318 | cpu_map=`printf '%x' $cpu_map` |
| 319 | dbg "echo $cpu_map > /sys/class/net/$i/queues/rx-0/rps_cpus" |
| 320 | echo $cpu_map > /sys/class/net/$i/queues/rx-0/rps_cpus |
| 321 | elif [ ! -z $1 ]; then |
| 322 | dbg "echo $1 > /sys/class/net/$i/queues/rx-0/rps_cpus" |
| 323 | echo $1 > /sys/class/net/$i/queues/rx-0/rps_cpus |
| 324 | fi |
| 325 | fi |
| 326 | done |
| 327 | } |
| 328 | |
| 329 | set_smp_affinity() |
| 330 | { |
| 331 | dbg2 "# Setup affinity of each physical irq." |
| 332 | num=0 |
| 333 | while [ "$num" -lt "$NUM_OF_CPU" ];do |
| 334 | eval smp_list=\$CPU${num}_AFFINITY |
| 335 | for i in $smp_list; do |
| 336 | cpu_bit=$((2 ** $num)) |
| 337 | virq=$(get_virtual_irq $i) |
| 338 | #virq=$i |
| 339 | dbg2 "irq p2v $i --> $virq" |
| 340 | if [ ! -z $virq ]; then |
| 341 | dbg "echo $cpu_bit > /proc/irq/$virq/smp_affinity" |
| 342 | echo $cpu_bit > /proc/irq/$virq/smp_affinity |
| 343 | fi |
| 344 | done |
| 345 | num=`expr $num + 1` |
| 346 | done |
| 347 | } |
| 348 | |
| 349 | if [ "$1" = "dbg" ]; then |
| 350 | DBG=1 |
| 351 | elif [ "$1" = "dbg2" ]; then |
| 352 | DBG=2 |
| 353 | else |
| 354 | DBG=0 |
| 355 | fi |
| 356 | |
| 357 | # Usage: dbg "the output string" |
| 358 | dbg() |
| 359 | { |
| 360 | if [ "$DBG" -ge "1" ]; then |
| 361 | echo -e $1 |
| 362 | fi |
| 363 | } |
| 364 | |
| 365 | # Usage: dbg2 "the output string" |
| 366 | dbg2() |
| 367 | { |
| 368 | if [ "$DBG" -ge "2" ]; then |
| 369 | echo -e $1 |
| 370 | fi |
| 371 | } |
| 372 | |
| 373 | dbg "# RPS and AFFINITY Setting" |
| 374 | dbg "# NUM_OF_CPU=$NUM_OF_CPU" |
| 375 | VAR_PREFIX="autogen" |
| 376 | get_if_info |
| 377 | setup_model |
| 378 | set_rps_cpu_bitmap |
| 379 | set_rps_cpus $DEFAULT_RPS |
| 380 | set_smp_affinity |
| 381 | #end of file |