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*" ]] || \ |
| 26 | [[ "$vif" == "wlan*" ]]; then |
| 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 |
| 102 | iptables -I FORWARD 1 -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw |
| 103 | ip6tables -I FORWARD 1 -p tcp -m conntrack --ctstate RELATED,ESTABLISHED -j FLOWOFFLOAD --hw |
| 104 | #UDP Binding |
| 105 | iptables -I FORWARD 1 -p udp -j FLOWOFFLOAD --hw |
| 106 | ip6tables -I FORWARD 1 -p udp -j FLOWOFFLOAD --hw |
| 107 | |
| 108 | #AX6000 AX7800 - SOC |
| 109 | if [[ "$WIFI_RADIO1" -eq "1" ]]; then |
| 110 | wifi1_irq=238 |
| 111 | fi |
| 112 | #AX7800 - PCIE0 |
| 113 | if [[ "$WIFI_RADIO2" -eq "1" ]]; then |
| 114 | wifi2_irq=237 |
| 115 | fi |
| 116 | #AX7800 - PCIE1 |
| 117 | #if [[ "$WIFI_RADIO3" -eq "1" ]]; then |
| 118 | # wifi3_irq=239 |
| 119 | #fi |
| 120 | else |
| 121 | dbg2 "WED_ENABLE OFF irq/iptable seting" |
| 122 | #AX6000 AX7800 - SOC |
| 123 | if [[ "$WIFI_RADIO1" -eq "1" ]]; then |
| 124 | wifi1_irq=245 |
| 125 | fi |
| 126 | #AX7800 - PCIE0 |
| 127 | if [[ "$WIFI_RADIO2" -eq "1" ]]; then |
| 128 | wifi2_irq=246 |
| 129 | fi |
| 130 | #AX7800 - PCIE1 |
| 131 | #if [[ "$WIFI_RADIO3" -eq "1" ]]; then |
| 132 | # wifi3_irq=247 |
| 133 | #fi |
developer | 10cbf2c | 2022-02-09 10:39:21 +0800 | [diff] [blame] | 134 | fi |
| 135 | # Please update the CPU binding in each cases. |
| 136 | # CPU#_AFFINITY="add binding irq number here" |
| 137 | # CPU#_RPS="add binding interface name here" |
| 138 | if [ "$num_of_wifi" = "0" ]; then |
| 139 | CPU0_AFFINITY="$eth0_irq" |
| 140 | CPU1_AFFINITY="$eth1_irq" |
| 141 | CPU2_AFFINITY="" |
| 142 | CPU3_AFFINITY="" |
| 143 | |
| 144 | CPU0_RPS="$RPS_IF_LIST" |
| 145 | CPU1_RPS="$RPS_IF_LIST" |
| 146 | CPU2_RPS="$RPS_IF_LIST" |
| 147 | CPU3_RPS="$RPS_IF_LIST" |
| 148 | else |
| 149 | #we bound all wifi card to cpu1 and bound eth to cpu0 |
| 150 | CPU0_AFFINITY="$eth0_irq" |
| 151 | CPU1_AFFINITY="$eth1_irq" |
| 152 | CPU2_AFFINITY="$wifi2_irq $wifi3_irq" |
| 153 | CPU3_AFFINITY="$wifi1_irq" |
| 154 | |
| 155 | CPU0_RPS="$RPS_IF_LIST" |
| 156 | CPU1_RPS="$RPS_IF_LIST" |
| 157 | CPU2_RPS="$RPS_IF_LIST" |
| 158 | CPU3_RPS="$RPS_IF_LIST" |
| 159 | fi |
| 160 | dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY" |
| 161 | dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY" |
| 162 | dbg2 "CPU2_AFFINITY = $CPU2_AFFINITY" |
| 163 | dbg2 "CPU3_AFFINITY = $CPU3_AFFINITY" |
| 164 | } |
| 165 | |
| 166 | MT7981() |
| 167 | { |
| 168 | num_of_wifi=$1 |
| 169 | DEFAULT_RPS=0 |
| 170 | |
| 171 | #Physical IRQ# setting |
| 172 | eth0_irq=229 |
| 173 | eth1_irq=230 |
| 174 | wifi1_irq= |
| 175 | wifi2_irq= |
| 176 | wifi3_irq= |
| 177 | #AX3000 |
| 178 | if [[ "$WIFI_RADIO1" -eq "1" ]]; then |
| 179 | wifi1_irq=245 |
| 180 | fi |
| 181 | # Please update the CPU binding in each cases. |
| 182 | # CPU#_AFFINITY="add binding irq number here" |
| 183 | # CPU#_RPS="add binding interface name here" |
| 184 | if [ "$num_of_wifi" = "0" ]; then |
| 185 | CPU0_AFFINITY="$eth0_irq" |
| 186 | CPU1_AFFINITY="$eth1_irq" |
| 187 | |
| 188 | CPU0_RPS="$RPS_IF_LIST" |
| 189 | CPU1_RPS="$RPS_IF_LIST" |
| 190 | else |
| 191 | #we bound all wifi card to cpu0 and bound eth to cpu1 |
| 192 | CPU0_AFFINITY="$wifi1_irq $wifi2_irq $wifi3_irq" |
| 193 | CPU1_AFFINITY="$eth0_irq $eth1_irq" |
| 194 | |
| 195 | CPU0_RPS="$RPS_IF_LIST" |
| 196 | CPU1_RPS="$RPS_IF_LIST" |
| 197 | fi |
| 198 | dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY" |
| 199 | dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY" |
| 200 | } |
| 201 | |
| 202 | MT7622() |
| 203 | { |
| 204 | num_of_wifi=$1 |
| 205 | DEFAULT_RPS=0 |
| 206 | |
| 207 | #Physical IRQ# setting |
| 208 | eth0_irq=224 |
| 209 | eth1_irq=225 |
| 210 | wifi1_irq= |
| 211 | wifi2_irq= |
| 212 | wifi3_irq= |
| 213 | #AX1200 AX3200 |
| 214 | if [[ "$WIFI_RADIO1" -eq "1" ]]; then |
| 215 | wifi1_irq=211 |
| 216 | fi |
| 217 | #AX1800 AX3200 |
| 218 | if [[ "$WIFI_RADIO2" -eq "1" ]]; then |
| 219 | wifi2_irq=214 |
| 220 | fi |
| 221 | #AX3600 |
| 222 | if [[ "$WIFI_RADIO3" -eq "1" ]]; then |
| 223 | wifi3_irq=215 |
| 224 | fi |
| 225 | |
| 226 | # Please update the CPU binding in each cases. |
| 227 | # CPU#_AFFINITY="add binding irq number here" |
| 228 | # CPU#_RPS="add binding interface name here" |
| 229 | if [ "$num_of_wifi" == "0" ]; then |
| 230 | CPU0_AFFINITY="$eth0_irq" |
| 231 | CPU1_AFFINITY="$eth1_irq" |
| 232 | |
| 233 | CPU0_RPS="$RPS_IF_LIST" |
| 234 | CPU1_RPS="$RPS_IF_LIST" |
| 235 | else |
| 236 | #we bound all wifi card to cpu0 and bound eth to cpu1 |
| 237 | CPU0_AFFINITY="$wifi1_irq $wifi2_irq $wifi3_irq" |
| 238 | CPU1_AFFINITY="$eth0_irq $eth1_irq" |
| 239 | |
| 240 | CPU0_RPS="$RPS_IF_LIST" |
| 241 | CPU1_RPS="$RPS_IF_LIST" |
| 242 | fi |
| 243 | |
| 244 | dbg2 "CPU0_AFFINITY = $CPU0_AFFINITY" |
| 245 | dbg2 "CPU1_AFFINITY = $CPU1_AFFINITY" |
| 246 | } |
| 247 | |
| 248 | setup_model() |
| 249 | { |
| 250 | board=$(board_name) |
| 251 | num_of_wifi=$NUM_WIFI_CARD |
| 252 | |
| 253 | if [[ $board == "*7986*" ]]; then |
| 254 | dbg "setup_model: MT7986 NUM_WIFI_CARD=$num_of_wifi" |
| 255 | MT7986 $num_of_wifi |
| 256 | elif [[ $board == "*7981*" ]]; then |
| 257 | dbg "setup_model: MT7981 NUM_WIFI_CARD=$num_of_wifi" |
| 258 | MT7981 $num_of_wifi |
| 259 | elif [[ $board == "*7622*" ]]; then |
| 260 | dbg "setup_model: MT7622 NUM_WIFI_CARD=$num_of_wifi" |
| 261 | MT7622 $num_of_wifi |
| 262 | fi |
| 263 | } |
| 264 | |
| 265 | get_virtual_irq() |
| 266 | { |
| 267 | PHY_POS=`expr $NUM_OF_CPU + 3` #physical irq # position in /proc/interrups may vary with the number of CPU up |
| 268 | target_phy_irq=$1 |
| 269 | cat /proc/interrupts | sed 's/:/ /g'| awk '$1 ~ /^[0-9]+$/' | while read line |
| 270 | do |
| 271 | set -- $line |
| 272 | phy_irq=$(eval "echo \$$PHY_POS") |
| 273 | if [ $phy_irq == $target_phy_irq ]; then |
| 274 | echo $1 |
| 275 | return |
| 276 | fi |
| 277 | done |
| 278 | } |
| 279 | |
| 280 | set_rps_cpu_bitmap() |
| 281 | { |
| 282 | dbg2 "# Scan binding interfaces of each cpu" |
| 283 | # suppose the value of interface_var is null or hex |
| 284 | num=0 |
| 285 | while [ "$num" -lt "$NUM_OF_CPU" ];do |
| 286 | cpu_bit=$((2 ** $num)) |
| 287 | eval rps_list=\$CPU${num}_RPS |
| 288 | dbg2 "# CPU$num: rps_list=$rps_list" |
| 289 | for i in $rps_list; do |
| 290 | var=${VAR_PREFIX}_${i//-/_} |
| 291 | eval ifval=\$$var |
| 292 | dbg2 "[var val before] \$$var=$ifval" |
| 293 | if [ -z "$ifval" ]; then |
| 294 | eval $var=$cpu_bit |
| 295 | else |
| 296 | eval $var=`expr $ifval + $cpu_bit` |
| 297 | fi |
| 298 | eval ifval=\$$var |
| 299 | dbg2 "[rps val after]$i=$ifval" |
| 300 | done |
| 301 | num=`expr $num + 1` |
| 302 | done |
| 303 | } |
| 304 | |
| 305 | # $1: The default rps value. If rps of the interface is not setup, set $1 to it |
| 306 | set_rps_cpus() |
| 307 | { |
| 308 | dbg2 "# Setup rps of the interfaces, $RPS_IF_LIST." |
| 309 | for i in $RPS_IF_LIST; do |
| 310 | var=${VAR_PREFIX}_${i//-/_} |
| 311 | eval cpu_map=\$$var |
| 312 | if [ -d /sys/class/net/$i ]; then |
| 313 | if [ ! -z $cpu_map ]; then |
| 314 | cpu_map=`printf '%x' $cpu_map` |
| 315 | dbg "echo $cpu_map > /sys/class/net/$i/queues/rx-0/rps_cpus" |
| 316 | echo $cpu_map > /sys/class/net/$i/queues/rx-0/rps_cpus |
| 317 | elif [ ! -z $1 ]; then |
| 318 | dbg "echo $1 > /sys/class/net/$i/queues/rx-0/rps_cpus" |
| 319 | echo $1 > /sys/class/net/$i/queues/rx-0/rps_cpus |
| 320 | fi |
| 321 | fi |
| 322 | done |
| 323 | } |
| 324 | |
| 325 | set_smp_affinity() |
| 326 | { |
| 327 | dbg2 "# Setup affinity of each physical irq." |
| 328 | num=0 |
| 329 | while [ "$num" -lt "$NUM_OF_CPU" ];do |
| 330 | eval smp_list=\$CPU${num}_AFFINITY |
| 331 | for i in $smp_list; do |
| 332 | cpu_bit=$((2 ** $num)) |
| 333 | virq=$(get_virtual_irq $i) |
| 334 | #virq=$i |
| 335 | dbg2 "irq p2v $i --> $virq" |
| 336 | if [ ! -z $virq ]; then |
| 337 | dbg "echo $cpu_bit > /proc/irq/$virq/smp_affinity" |
| 338 | echo $cpu_bit > /proc/irq/$virq/smp_affinity |
| 339 | fi |
| 340 | done |
| 341 | num=`expr $num + 1` |
| 342 | done |
| 343 | } |
| 344 | |
| 345 | if [ "$1" = "dbg" ]; then |
| 346 | DBG=1 |
| 347 | elif [ "$1" = "dbg2" ]; then |
| 348 | DBG=2 |
| 349 | else |
| 350 | DBG=0 |
| 351 | fi |
| 352 | |
| 353 | # Usage: dbg "the output string" |
| 354 | dbg() |
| 355 | { |
| 356 | if [ "$DBG" -ge "1" ]; then |
| 357 | echo -e $1 |
| 358 | fi |
| 359 | } |
| 360 | |
| 361 | # Usage: dbg2 "the output string" |
| 362 | dbg2() |
| 363 | { |
| 364 | if [ "$DBG" -ge "2" ]; then |
| 365 | echo -e $1 |
| 366 | fi |
| 367 | } |
| 368 | |
| 369 | dbg "# RPS and AFFINITY Setting" |
| 370 | dbg "# NUM_OF_CPU=$NUM_OF_CPU" |
| 371 | VAR_PREFIX="autogen" |
| 372 | get_if_info |
| 373 | setup_model |
| 374 | set_rps_cpu_bitmap |
| 375 | set_rps_cpus $DEFAULT_RPS |
| 376 | set_smp_affinity |
| 377 | #end of file |