netifd: refactor packet steering init
[openwrt/openwrt.git] / package / network / config / netifd / files / usr / libexec / network / packet-steering.sh
1 #!/bin/sh
2 NPROCS="$(grep -c "^processor.*:" /proc/cpuinfo)"
3 [ "$NPROCS" -gt 1 ] || exit
4
5 PROC_MASK="$(( (1 << $NPROCS) - 1 ))"
6
7 find_irq_cpu() {
8 local dev="$1"
9 local match="$(grep -m 1 "$dev\$" /proc/interrupts)"
10 local cpu=0
11
12 [ -n "$match" ] && {
13 set -- $match
14 shift
15 for cur in $(seq 1 $NPROCS); do
16 [ "$1" -gt 0 ] && {
17 cpu=$(($cur - 1))
18 break
19 }
20 shift
21 done
22 }
23
24 echo "$cpu"
25 }
26
27 set_hex_val() {
28 local file="$1"
29 local val="$2"
30 val="$(printf %x "$val")"
31 [ -n "$DEBUG" ] && echo "$file = $val"
32 echo "$val" > "$file"
33 }
34
35 packet_steering="$(uci get "network.@globals[0].packet_steering")"
36 [ "$packet_steering" != 1 ] && exit 0
37
38 exec 512>/var/lock/smp_tune.lock
39 flock 512 || exit 1
40
41 [ -e "/usr/libexec/platform/packet-steering.sh" ] && {
42 /usr/libexec/platform/packet-steering.sh
43 exit 0
44 }
45
46 for dev in /sys/class/net/*; do
47 [ -d "$dev" ] || continue
48
49 # ignore virtual interfaces
50 [ -n "$(ls "${dev}/" | grep '^lower_')" ] && continue
51 [ -d "${dev}/device" ] || continue
52
53 device="$(readlink "${dev}/device")"
54 device="$(basename "$device")"
55 irq_cpu="$(find_irq_cpu "$device")"
56 irq_cpu_mask="$((1 << $irq_cpu))"
57
58 for q in ${dev}/queues/tx-*; do
59 set_hex_val "$q/xps_cpus" "$PROC_MASK"
60 done
61
62 # ignore dsa slave ports for RPS
63 subsys="$(readlink "${dev}/device/subsystem")"
64 subsys="$(basename "$subsys")"
65 [ "$subsys" = "mdio_bus" ] && continue
66
67 for q in ${dev}/queues/rx-*; do
68 set_hex_val "$q/rps_cpus" "$PROC_MASK"
69 done
70 done