1#!/bin/bash
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
37	# In case there is more than 32 CPUs we need to remove commas from masks
38	rps_mask=${rps_mask//,}
39	expected_rps_mask=${expected_rps_mask//,}
40	if [ $rps_mask -eq $expected_rps_mask ]; then
41		echo "[ ok ]"
42	else
43		echo "[fail] expected $expected_rps_mask found $rps_mask"
44		ret=1
45	fi
46}
47
48trap cleanup EXIT
49
50echo 0 > /proc/sys/net/core/rps_default_mask
51setup
52chk_rps "empty rps_default_mask" $NETNS lo 0
53cleanup
54
55echo 1 > /proc/sys/net/core/rps_default_mask
56setup
57chk_rps "changing rps_default_mask dont affect existing devices" "" lo $INITIAL_RPS_DEFAULT_MASK
58
59echo 3 > /proc/sys/net/core/rps_default_mask
60chk_rps "changing rps_default_mask dont affect existing netns" $NETNS lo 0
61
62ip link add name $VETH type veth peer netns $NETNS name $VETH
63ip link set dev $VETH up
64ip -n $NETNS link set dev $VETH up
65chk_rps "changing rps_default_mask affect newly created devices" "" $VETH 3
66chk_rps "changing rps_default_mask don't affect newly child netns[II]" $NETNS $VETH 0
67ip link del dev $VETH
68ip netns del $NETNS
69
70setup
71chk_rps "rps_default_mask is 0 by default in child netns" "$NETNS" lo 0
72
73ip netns exec $NETNS sysctl -qw net.core.rps_default_mask=1
74ip link add name $VETH type veth peer netns $NETNS name $VETH
75chk_rps "changing rps_default_mask in child ns don't affect the main one" "" lo $INITIAL_RPS_DEFAULT_MASK
76chk_rps "changing rps_default_mask in child ns affects new childns devices" $NETNS $VETH 1
77chk_rps "changing rps_default_mask in child ns don't affect existing devices" $NETNS lo 0
78
79exit $ret
80