Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: net: use setup_ns for bareudp testing

Switch bareudp testing to use setup_ns, which sets up rp_filter by default.
This allows us to remove the manual rp_filter configuration from the script.

Additionally, since setup_ns handles namespace naming and cleanup, we no
longer need a separate cleanup function. We also move the trap setup earlier
in the script, before the test setup begins.

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://patch.msgid.link/20250508081910.84216-4-liuhangbin@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Hangbin Liu and committed by
Jakub Kicinski
69ea46e7 50ad88d5

+7 -42
+7 -42
tools/testing/selftests/net/bareudp.sh
··· 106 106 # | | 107 107 # +-----------------------------------------------------------------------+ 108 108 109 + . ./lib.sh 110 + 109 111 ERR=4 # Return 4 by default, which is the SKIP code for kselftest 110 112 PING6="ping" 111 113 PAUSE_ON_FAIL="no" 112 114 113 - readonly NS0=$(mktemp -u ns0-XXXXXXXX) 114 - readonly NS1=$(mktemp -u ns1-XXXXXXXX) 115 - readonly NS2=$(mktemp -u ns2-XXXXXXXX) 116 - readonly NS3=$(mktemp -u ns3-XXXXXXXX) 117 - 118 115 # Exit the script after having removed the network namespaces it created 119 - # 120 - # Parameters: 121 - # 122 - # * The list of network namespaces to delete before exiting. 123 - # 124 116 exit_cleanup() 125 117 { 126 - for ns in "$@"; do 127 - ip netns delete "${ns}" 2>/dev/null || true 128 - done 118 + cleanup_all_ns 129 119 130 120 if [ "${ERR}" -eq 4 ]; then 131 121 echo "Error: Setting up the testing environment failed." >&2 ··· 130 140 # namespaces created by this script are deleted. 131 141 create_namespaces() 132 142 { 133 - ip netns add "${NS0}" || exit_cleanup 134 - ip netns add "${NS1}" || exit_cleanup "${NS0}" 135 - ip netns add "${NS2}" || exit_cleanup "${NS0}" "${NS1}" 136 - ip netns add "${NS3}" || exit_cleanup "${NS0}" "${NS1}" "${NS2}" 137 - } 138 - 139 - # The trap function handler 140 - # 141 - exit_cleanup_all() 142 - { 143 - exit_cleanup "${NS0}" "${NS1}" "${NS2}" "${NS3}" 143 + setup_ns NS0 NS1 NS2 NS3 || exit_cleanup 144 144 } 145 145 146 146 # Configure a network interface using a host route ··· 168 188 # 169 189 setup_underlay() 170 190 { 171 - for ns in "${NS0}" "${NS1}" "${NS2}" "${NS3}"; do 172 - ip -netns "${ns}" link set dev lo up 173 - done; 174 - 175 191 ip link add name veth01 netns "${NS0}" type veth peer name veth10 netns "${NS1}" 176 192 ip link add name veth12 netns "${NS1}" type veth peer name veth21 netns "${NS2}" 177 193 ip link add name veth23 netns "${NS2}" type veth peer name veth32 netns "${NS3}" ··· 210 234 ip netns exec "${NS2}" sysctl -qw net.ipv4.ip_forward=1 211 235 ip -netns "${NS1}" route add 192.0.2.100/32 via 192.0.2.10 212 236 ip -netns "${NS2}" route add 192.0.2.103/32 via 192.0.2.33 213 - 214 - # The intermediate namespaces don't have routes for the reverse path, 215 - # as it will be handled by tc. So we need to ensure that rp_filter is 216 - # not going to block the traffic. 217 - ip netns exec "${NS1}" sysctl -qw net.ipv4.conf.all.rp_filter=0 218 - ip netns exec "${NS2}" sysctl -qw net.ipv4.conf.all.rp_filter=0 219 - ip netns exec "${NS1}" sysctl -qw net.ipv4.conf.default.rp_filter=0 220 - ip netns exec "${NS2}" sysctl -qw net.ipv4.conf.default.rp_filter=0 221 237 } 222 238 223 239 setup_overlay_ipv6() ··· 489 521 490 522 check_features 491 523 492 - # Create namespaces before setting up the exit trap. 493 - # Otherwise, exit_cleanup_all() could delete namespaces that were not created 494 - # by this script. 495 - create_namespaces 496 - 497 524 set -e 498 - trap exit_cleanup_all EXIT 525 + trap exit_cleanup EXIT 526 + 527 + create_namespaces 499 528 500 529 setup_underlay 501 530 setup_overlay_ipv4