2 # SPDX-License-Identifier: GPL-2.0
4 # This test is designed for testing the new VRF strict_mode functionality.
9 # identifies the "init" network namespace which is often called root network
11 INIT_NETNS_NAME="init"
13 PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}
15 TESTS="init testns mix"
23 if [ ${rc} -eq ${expected} ]; then
24 nsuccess=$((nsuccess+1))
25 printf "\n TEST: %-60s [ OK ]\n" "${msg}"
29 printf "\n TEST: %-60s [FAIL]\n" "${msg}"
30 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
32 echo "hit enter to continue, 'q' to quit"
34 [ "$a" = "q" ] && exit 1
39 print_log_test_results()
41 if [ "$TESTS" != "none" ]; then
42 printf "\nTests passed: %3d\n" ${nsuccess}
43 printf "Tests failed: %3d\n" ${nfail}
50 echo "################################################################################"
51 echo "TEST SECTION: $*"
52 echo "################################################################################"
60 if [ "${nsname}" != "${INIT_NETNS_NAME}" ]; then
61 nsarg="-netns ${nsname}"
70 local nsarg="$(ip_expand_args ${nsname})"
72 ip ${nsarg} -o link show type vrf | wc -l
75 count_vrf_by_table_id()
79 local nsarg="$(ip_expand_args ${nsname})"
81 ip ${nsarg} -d -o link show type vrf | grep "table ${tableid}" | wc -l
89 local nsarg="$(ip_expand_args ${nsname})"
91 ip ${nsarg} link add ${vrfname} type vrf table ${vrftable} &>/dev/null
102 add_vrf ${nsname} ${vrfname} ${vrftable}; rc=$?
104 cnt=$(count_vrf_by_table_id ${nsname} ${vrftable})
106 log_test ${rc} 0 "${nsname}: add vrf ${vrfname}, ${cnt} vrfs for table ${vrftable}"
109 add_vrf_and_check_fail()
117 add_vrf ${nsname} ${vrfname} ${vrftable}; rc=$?
119 cnt=$(count_vrf_by_table_id ${nsname} ${vrftable})
121 log_test ${rc} 2 "${nsname}: CANNOT add vrf ${vrfname}, ${cnt} vrfs for table ${vrftable}"
128 local nsarg="$(ip_expand_args ${nsname})"
130 ip ${nsarg} link del ${vrfname}
131 log_test $? 0 "${nsname}: remove vrf ${vrfname}"
134 config_vrf_and_check()
139 local nsarg="$(ip_expand_args ${nsname})"
141 ip ${nsarg} link set dev ${vrfname} up && \
142 ip ${nsarg} addr add ${addr} dev ${vrfname}
143 log_test $? 0 "${nsname}: vrf ${vrfname} up, addr ${addr}"
153 if [ "${nsname}" != "${INIT_NETNS_NAME}" ]; then
154 # a custom network namespace is provided
155 nsexec="ip netns exec ${nsname}"
158 rval="$(${nsexec} bash -c "cat /proc/sys/net/vrf/strict_mode" | \
159 grep -E "^[0-1]$")" &> /dev/null
160 if [ $? -ne 0 ]; then
166 # on success, rval can be only 0 or 1; on error, rval is equal to 255
171 read_strict_mode_compare_and_check()
177 res="$(read_strict_mode ${nsname})"
178 log_test ${res} ${expected} "${nsname}: check strict_mode=${res}"
187 if [ "${nsname}" != "${INIT_NETNS_NAME}" ]; then
188 # a custom network namespace is provided
189 nsexec="ip netns exec ${nsname}"
192 ${nsexec} bash -c "echo ${val} >/proc/sys/net/vrf/strict_mode" &>/dev/null
199 set_strict_mode ${nsname} 1
202 disable_strict_mode()
206 set_strict_mode ${nsname} 0
209 disable_strict_mode_and_check()
213 disable_strict_mode ${nsname}
214 log_test $? 0 "${nsname}: disable strict_mode (=0)"
217 enable_strict_mode_and_check()
221 enable_strict_mode ${nsname}
222 log_test $? 0 "${nsname}: enable strict_mode (=1)"
225 enable_strict_mode_and_check_fail()
229 enable_strict_mode ${nsname}
230 log_test $? 1 "${nsname}: CANNOT enable strict_mode"
233 strict_mode_check_default()
239 vrfcnt=$(vrf_count ${nsname})
240 strictmode=$(read_strict_mode ${nsname})
241 log_test ${strictmode} 0 "${nsname}: strict_mode=0 by default, ${vrfcnt} vrfs"
253 ip netns del $testns 2>/dev/null
255 ip link del vrf100 2>/dev/null
256 ip link del vrf101 2>/dev/null
257 ip link del vrf102 2>/dev/null
259 echo 0 >/proc/sys/net/vrf/strict_mode 2>/dev/null
262 vrf_strict_mode_tests_init()
264 log_section "VRF strict_mode test on init network namespace"
266 vrf_strict_mode_check_support init
268 strict_mode_check_default init
270 add_vrf_and_check init vrf100 100
271 config_vrf_and_check init 172.16.100.1/24 vrf100
273 enable_strict_mode_and_check init
275 add_vrf_and_check_fail init vrf101 100
277 disable_strict_mode_and_check init
279 add_vrf_and_check init vrf101 100
280 config_vrf_and_check init 172.16.101.1/24 vrf101
282 enable_strict_mode_and_check_fail init
284 del_vrf_and_check init vrf101
286 enable_strict_mode_and_check init
288 add_vrf_and_check init vrf102 102
289 config_vrf_and_check init 172.16.102.1/24 vrf102
291 # the strict_modle is enabled in the init
294 vrf_strict_mode_tests_testns()
296 log_section "VRF strict_mode test on testns network namespace"
298 vrf_strict_mode_check_support $testns
300 strict_mode_check_default $testns
302 enable_strict_mode_and_check $testns
304 add_vrf_and_check $testns vrf100 100
305 config_vrf_and_check $testns 10.0.100.1/24 vrf100
307 add_vrf_and_check_fail $testns vrf101 100
309 add_vrf_and_check_fail $testns vrf102 100
311 add_vrf_and_check $testns vrf200 200
313 disable_strict_mode_and_check $testns
315 add_vrf_and_check $testns vrf101 100
317 add_vrf_and_check $testns vrf102 100
319 #the strict_mode is disabled in the $testns
322 vrf_strict_mode_tests_mix()
324 log_section "VRF strict_mode test mixing init and testns network namespaces"
326 read_strict_mode_compare_and_check init 1
328 read_strict_mode_compare_and_check $testns 0
330 del_vrf_and_check $testns vrf101
332 del_vrf_and_check $testns vrf102
334 disable_strict_mode_and_check init
336 enable_strict_mode_and_check $testns
338 enable_strict_mode_and_check init
339 enable_strict_mode_and_check init
341 disable_strict_mode_and_check $testns
342 disable_strict_mode_and_check $testns
344 read_strict_mode_compare_and_check init 1
346 read_strict_mode_compare_and_check $testns 0
349 ################################################################################
357 -t <test> Test(s) to run (default: all)
362 ################################################################################
365 while getopts ":t:h" opt; do
373 vrf_strict_mode_check_support()
379 output="$(lsmod | grep '^vrf' | awk '{print $1}')"
380 if [ -z "${output}" ]; then
381 modinfo vrf || return $?
384 # we do not care about the value of the strict_mode; we only check if
385 # the strict_mode parameter is available or not.
386 read_strict_mode ${nsname} &>/dev/null; rc=$?
387 log_test ${rc} 0 "${nsname}: net.vrf.strict_mode is available"
392 if [ "$(id -u)" -ne 0 ];then
393 echo "SKIP: Need root privileges"
397 if [ ! -x "$(command -v ip)" ]; then
398 echo "SKIP: Could not run test without ip tool"
402 modprobe vrf &>/dev/null
403 if [ ! -e /proc/sys/net/vrf/strict_mode ]; then
404 echo "SKIP: vrf sysctl does not exist"
414 vrf_strict_mode_tests_init|init) vrf_strict_mode_tests_init;;
415 vrf_strict_mode_tests_testns|testns) vrf_strict_mode_tests_testns;;
416 vrf_strict_mode_tests_mix|mix) vrf_strict_mode_tests_mix;;
418 help) echo "Test names: $TESTS"; exit 0;;
424 print_log_test_results