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