xref: /linux/tools/testing/selftests/net/rps_default_mask.sh (revision 06d07429858317ded2db7986113a9e0129cd599b)
1*0719b533SJakub Kicinski#!/bin/bash
2c12e0d5fSPaolo Abeni# SPDX-License-Identifier: GPL-2.0
3c12e0d5fSPaolo Abeni
4c12e0d5fSPaolo Abenireadonly ksft_skip=4
5c12e0d5fSPaolo Abenireadonly cpus=$(nproc)
6c12e0d5fSPaolo Abeniret=0
7c12e0d5fSPaolo Abeni
8c12e0d5fSPaolo Abeni[ $cpus -gt 2 ] || exit $ksft_skip
9c12e0d5fSPaolo Abeni
10c12e0d5fSPaolo Abenireadonly INITIAL_RPS_DEFAULT_MASK=$(cat /proc/sys/net/core/rps_default_mask)
113a7d84eaSPaolo Abenireadonly TAG="$(mktemp -u XXXXXX)"
123a7d84eaSPaolo Abenireadonly VETH="veth${TAG}"
133a7d84eaSPaolo Abenireadonly NETNS="ns-${TAG}"
14c12e0d5fSPaolo Abeni
15c12e0d5fSPaolo Abenisetup() {
16c12e0d5fSPaolo Abeni	ip netns add "${NETNS}"
17c12e0d5fSPaolo Abeni	ip -netns "${NETNS}" link set lo up
18c12e0d5fSPaolo Abeni}
19c12e0d5fSPaolo Abeni
20c12e0d5fSPaolo Abenicleanup() {
21c12e0d5fSPaolo Abeni	echo $INITIAL_RPS_DEFAULT_MASK > /proc/sys/net/core/rps_default_mask
22c12e0d5fSPaolo Abeni	ip netns del $NETNS
23c12e0d5fSPaolo Abeni}
24c12e0d5fSPaolo Abeni
25c12e0d5fSPaolo Abenichk_rps() {
263a7d84eaSPaolo Abeni	local rps_mask expected_rps_mask=$4
273a7d84eaSPaolo Abeni	local dev_name=$3
283a7d84eaSPaolo Abeni	local netns=$2
293a7d84eaSPaolo Abeni	local cmd="cat"
30c12e0d5fSPaolo Abeni	local msg=$1
31c12e0d5fSPaolo Abeni
323a7d84eaSPaolo Abeni	[ -n "$netns" ] && cmd="ip netns exec $netns $cmd"
333a7d84eaSPaolo Abeni
343a7d84eaSPaolo Abeni	rps_mask=$($cmd /sys/class/net/$dev_name/queues/rx-0/rps_cpus)
35c12e0d5fSPaolo Abeni	printf "%-60s" "$msg"
36*0719b533SJakub Kicinski
37*0719b533SJakub Kicinski	# In case there is more than 32 CPUs we need to remove commas from masks
38*0719b533SJakub Kicinski	rps_mask=${rps_mask//,}
39*0719b533SJakub Kicinski	expected_rps_mask=${expected_rps_mask//,}
40c12e0d5fSPaolo Abeni	if [ $rps_mask -eq $expected_rps_mask ]; then
41c12e0d5fSPaolo Abeni		echo "[ ok ]"
42c12e0d5fSPaolo Abeni	else
43c12e0d5fSPaolo Abeni		echo "[fail] expected $expected_rps_mask found $rps_mask"
44c12e0d5fSPaolo Abeni		ret=1
45c12e0d5fSPaolo Abeni	fi
46c12e0d5fSPaolo Abeni}
47c12e0d5fSPaolo Abeni
48c12e0d5fSPaolo Abenitrap cleanup EXIT
49c12e0d5fSPaolo Abeni
50c12e0d5fSPaolo Abeniecho 0 > /proc/sys/net/core/rps_default_mask
51c12e0d5fSPaolo Abenisetup
523a7d84eaSPaolo Abenichk_rps "empty rps_default_mask" $NETNS lo 0
53c12e0d5fSPaolo Abenicleanup
54c12e0d5fSPaolo Abeni
55c12e0d5fSPaolo Abeniecho 1 > /proc/sys/net/core/rps_default_mask
56c12e0d5fSPaolo Abenisetup
573a7d84eaSPaolo Abenichk_rps "changing rps_default_mask dont affect existing devices" "" lo $INITIAL_RPS_DEFAULT_MASK
58c12e0d5fSPaolo Abeni
59c12e0d5fSPaolo Abeniecho 3 > /proc/sys/net/core/rps_default_mask
603a7d84eaSPaolo Abenichk_rps "changing rps_default_mask dont affect existing netns" $NETNS lo 0
61c12e0d5fSPaolo Abeni
623a7d84eaSPaolo Abeniip link add name $VETH type veth peer netns $NETNS name $VETH
633a7d84eaSPaolo Abeniip link set dev $VETH up
643a7d84eaSPaolo Abeniip -n $NETNS link set dev $VETH up
653a7d84eaSPaolo Abenichk_rps "changing rps_default_mask affect newly created devices" "" $VETH 3
663a7d84eaSPaolo Abenichk_rps "changing rps_default_mask don't affect newly child netns[II]" $NETNS $VETH 0
6738e058ccSHangbin Liuip link del dev $VETH
683a7d84eaSPaolo Abeniip netns del $NETNS
693a7d84eaSPaolo Abeni
703a7d84eaSPaolo Abenisetup
713a7d84eaSPaolo Abenichk_rps "rps_default_mask is 0 by default in child netns" "$NETNS" lo 0
723a7d84eaSPaolo Abeni
733a7d84eaSPaolo Abeniip netns exec $NETNS sysctl -qw net.core.rps_default_mask=1
743a7d84eaSPaolo Abeniip link add name $VETH type veth peer netns $NETNS name $VETH
753a7d84eaSPaolo Abenichk_rps "changing rps_default_mask in child ns don't affect the main one" "" lo $INITIAL_RPS_DEFAULT_MASK
763a7d84eaSPaolo Abenichk_rps "changing rps_default_mask in child ns affects new childns devices" $NETNS $VETH 1
773a7d84eaSPaolo Abenichk_rps "changing rps_default_mask in child ns don't affect existing devices" $NETNS lo 0
783a7d84eaSPaolo Abeni
79c12e0d5fSPaolo Abeniexit $ret
80