1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4readonly ksft_skip=4
5readonly cpus=$(nproc)
6ret=0
7
8[ $cpus -gt 2 ] || exit $ksft_skip
9
10readonly INITIAL_RPS_DEFAULT_MASK=$(cat /proc/sys/net/core/rps_default_mask)
11readonly TAG="$(mktemp -u XXXXXX)"
12readonly VETH="veth${TAG}"
13readonly NETNS="ns-${TAG}"
14
15setup() {
16	ip netns add "${NETNS}"
17	ip -netns "${NETNS}" link set lo up
18}
19
20cleanup() {
21	echo $INITIAL_RPS_DEFAULT_MASK > /proc/sys/net/core/rps_default_mask
22	ip netns del $NETNS
23}
24
25chk_rps() {
26	local rps_mask expected_rps_mask=$4
27	local dev_name=$3
28	local netns=$2
29	local cmd="cat"
30	local msg=$1
31
32	[ -n "$netns" ] && cmd="ip netns exec $netns $cmd"
33
34	rps_mask=$($cmd /sys/class/net/$dev_name/queues/rx-0/rps_cpus)
35	printf "%-60s" "$msg"
36	if [ $rps_mask -eq $expected_rps_mask ]; then
37		echo "[ ok ]"
38	else
39		echo "[fail] expected $expected_rps_mask found $rps_mask"
40		ret=1
41	fi
42}
43
44trap cleanup EXIT
45
46echo 0 > /proc/sys/net/core/rps_default_mask
47setup
48chk_rps "empty rps_default_mask" $NETNS lo 0
49cleanup
50
51echo 1 > /proc/sys/net/core/rps_default_mask
52setup
53chk_rps "changing rps_default_mask dont affect existing devices" "" lo $INITIAL_RPS_DEFAULT_MASK
54
55echo 3 > /proc/sys/net/core/rps_default_mask
56chk_rps "changing rps_default_mask dont affect existing netns" $NETNS lo 0
57
58ip link add name $VETH type veth peer netns $NETNS name $VETH
59ip link set dev $VETH up
60ip -n $NETNS link set dev $VETH up
61chk_rps "changing rps_default_mask affect newly created devices" "" $VETH 3
62chk_rps "changing rps_default_mask don't affect newly child netns[II]" $NETNS $VETH 0
63ip link del dev $VETH
64ip netns del $NETNS
65
66setup
67chk_rps "rps_default_mask is 0 by default in child netns" "$NETNS" lo 0
68
69ip netns exec $NETNS sysctl -qw net.core.rps_default_mask=1
70ip link add name $VETH type veth peer netns $NETNS name $VETH
71chk_rps "changing rps_default_mask in child ns don't affect the main one" "" lo $INITIAL_RPS_DEFAULT_MASK
72chk_rps "changing rps_default_mask in child ns affects new childns devices" $NETNS $VETH 1
73chk_rps "changing rps_default_mask in child ns don't affect existing devices" $NETNS lo 0
74
75exit $ret
76