xref: /linux/tools/testing/selftests/net/lib.sh (revision 7b8e9264f55a9c320f398e337d215e68cca50131)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4net_dir=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
5source "$net_dir/lib/sh/defer.sh"
6
7##############################################################################
8# Defines
9
10: "${WAIT_TIMEOUT:=20}"
11
12# Whether to pause on after a failure.
13: "${PAUSE_ON_FAIL:=no}"
14
15BUSYWAIT_TIMEOUT=$((WAIT_TIMEOUT * 1000)) # ms
16
17# Kselftest framework constants.
18ksft_pass=0
19ksft_fail=1
20ksft_xfail=2
21ksft_skip=4
22
23# namespace list created by setup_ns
24NS_LIST=()
25
26# Exit status to return at the end. Set in case one of the tests fails.
27EXIT_STATUS=0
28# Per-test return value. Clear at the beginning of each test.
29RET=0
30
31##############################################################################
32# Helpers
33
34__ksft_status_merge()
35{
36	local a=$1; shift
37	local b=$1; shift
38	local -A weights
39	local weight=0
40
41	local i
42	for i in "$@"; do
43		weights[$i]=$((weight++))
44	done
45
46	if [[ ${weights[$a]} -ge ${weights[$b]} ]]; then
47		echo "$a"
48		return 0
49	else
50		echo "$b"
51		return 1
52	fi
53}
54
55ksft_status_merge()
56{
57	local a=$1; shift
58	local b=$1; shift
59
60	__ksft_status_merge "$a" "$b" \
61		$ksft_pass $ksft_xfail $ksft_skip $ksft_fail
62}
63
64ksft_exit_status_merge()
65{
66	local a=$1; shift
67	local b=$1; shift
68
69	__ksft_status_merge "$a" "$b" \
70		$ksft_xfail $ksft_pass $ksft_skip $ksft_fail
71}
72
73loopy_wait()
74{
75	local sleep_cmd=$1; shift
76	local timeout_ms=$1; shift
77
78	local start_time="$(date -u +%s%3N)"
79	while true
80	do
81		local out
82		if out=$("$@"); then
83			echo -n "$out"
84			return 0
85		fi
86
87		local current_time="$(date -u +%s%3N)"
88		if ((current_time - start_time > timeout_ms)); then
89			echo -n "$out"
90			return 1
91		fi
92
93		$sleep_cmd
94	done
95}
96
97busywait()
98{
99	local timeout_ms=$1; shift
100
101	loopy_wait : "$timeout_ms" "$@"
102}
103
104# timeout in seconds
105slowwait()
106{
107	local timeout_sec=$1; shift
108
109	loopy_wait "sleep 0.1" "$((timeout_sec * 1000))" "$@"
110}
111
112until_counter_is()
113{
114	local expr=$1; shift
115	local current=$("$@")
116
117	echo $((current))
118	((current $expr))
119}
120
121busywait_for_counter()
122{
123	local timeout=$1; shift
124	local delta=$1; shift
125
126	local base=$("$@")
127	busywait "$timeout" until_counter_is ">= $((base + delta))" "$@"
128}
129
130slowwait_for_counter()
131{
132	local timeout=$1; shift
133	local delta=$1; shift
134
135	local base=$("$@")
136	slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@"
137}
138
139# Check for existence of tools which are built as part of selftests
140# but may also already exist in $PATH
141check_gen_prog()
142{
143	local prog_name=$1; shift
144
145	if ! which $prog_name >/dev/null 2>/dev/null; then
146		PATH=$PWD:$PATH
147		if ! which $prog_name >/dev/null; then
148			echo "'$prog_name' command not found; skipping tests"
149			exit $ksft_skip
150		fi
151	fi
152}
153
154remove_ns_list()
155{
156	local item=$1
157	local ns
158	local ns_list=("${NS_LIST[@]}")
159	NS_LIST=()
160
161	for ns in "${ns_list[@]}"; do
162		if [ "${ns}" != "${item}" ]; then
163			NS_LIST+=("${ns}")
164		fi
165	done
166}
167
168cleanup_ns()
169{
170	local ns=""
171	local ret=0
172
173	for ns in "$@"; do
174		[ -z "${ns}" ] && continue
175		ip netns pids "${ns}" 2> /dev/null | xargs -r kill || true
176		ip netns delete "${ns}" &> /dev/null || true
177		if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then
178			echo "Warn: Failed to remove namespace $ns"
179			ret=1
180		else
181			remove_ns_list "${ns}"
182		fi
183	done
184
185	return $ret
186}
187
188cleanup_all_ns()
189{
190	cleanup_ns "${NS_LIST[@]}"
191}
192
193# setup netns with given names as prefix. e.g
194# setup_ns local remote
195setup_ns()
196{
197	local ns_name=""
198	local ns_list=()
199	for ns_name in "$@"; do
200		# avoid conflicts with local var: internal error
201		if [ "${ns_name}" = "ns_name" ]; then
202			echo "Failed to setup namespace '${ns_name}': invalid name"
203			cleanup_ns "${ns_list[@]}"
204			exit $ksft_fail
205		fi
206
207		# Some test may setup/remove same netns multi times
208		if [ -z "${!ns_name}" ]; then
209			eval "${ns_name}=${ns_name,,}-$(mktemp -u XXXXXX)"
210		else
211			cleanup_ns "${!ns_name}"
212		fi
213
214		if ! ip netns add "${!ns_name}"; then
215			echo "Failed to create namespace $ns_name"
216			cleanup_ns "${ns_list[@]}"
217			return $ksft_skip
218		fi
219		ip -n "${!ns_name}" link set lo up
220		ip netns exec "${!ns_name}" sysctl -wq net.ipv4.conf.all.rp_filter=0
221		ip netns exec "${!ns_name}" sysctl -wq net.ipv4.conf.default.rp_filter=0
222		ns_list+=("${!ns_name}")
223	done
224	NS_LIST+=("${ns_list[@]}")
225}
226
227# Create netdevsim with given id and net namespace.
228create_netdevsim() {
229    local id="$1"
230    local ns="$2"
231
232    modprobe netdevsim &> /dev/null
233    udevadm settle
234
235    echo "$id 1" | ip netns exec $ns tee /sys/bus/netdevsim/new_device >/dev/null
236    local dev=$(ip netns exec $ns ls /sys/bus/netdevsim/devices/netdevsim$id/net)
237    ip -netns $ns link set dev $dev name nsim$id
238    ip -netns $ns link set dev nsim$id up
239
240    echo nsim$id
241}
242
243create_netdevsim_port() {
244    local nsim_id="$1"
245    local ns="$2"
246    local port_id="$3"
247    local perm_addr="$4"
248    local orig_dev
249    local new_dev
250    local nsim_path
251
252    nsim_path="/sys/bus/netdevsim/devices/netdevsim$nsim_id"
253
254    echo "$port_id $perm_addr" | ip netns exec "$ns" tee "$nsim_path"/new_port > /dev/null || return 1
255
256    orig_dev=$(ip netns exec "$ns" find "$nsim_path"/net/ -maxdepth 1 -name 'e*' | tail -n 1)
257    orig_dev=$(basename "$orig_dev")
258    new_dev="nsim${nsim_id}p$port_id"
259
260    ip -netns "$ns" link set dev "$orig_dev" name "$new_dev"
261    ip -netns "$ns" link set dev "$new_dev" up
262
263    echo "$new_dev"
264}
265
266# Remove netdevsim with given id.
267cleanup_netdevsim() {
268    local id="$1"
269
270    if [ -d "/sys/bus/netdevsim/devices/netdevsim$id/net" ]; then
271        echo "$id" > /sys/bus/netdevsim/del_device
272    fi
273}
274
275tc_rule_stats_get()
276{
277	local dev=$1; shift
278	local pref=$1; shift
279	local dir=${1:-ingress}; shift
280	local selector=${1:-.packets}; shift
281
282	tc -j -s filter show dev $dev $dir pref $pref \
283	    | jq ".[] | select(.options.actions) |
284		  .options.actions[].stats$selector"
285}
286
287tc_rule_handle_stats_get()
288{
289	local id=$1; shift
290	local handle=$1; shift
291	local selector=${1:-.packets}; shift
292	local netns=${1:-""}; shift
293
294	tc $netns -j -s filter show $id \
295	    | jq ".[] | select(.options.handle == $handle) | \
296		  .options.actions[0].stats$selector"
297}
298
299# attach a qdisc with two children match/no-match and a flower filter to match
300tc_set_flower_counter() {
301	local -r ns=$1
302	local -r ipver=$2
303	local -r dev=$3
304	local -r flower_expr=$4
305
306	tc -n $ns qdisc add dev $dev root handle 1: prio bands 2 \
307			priomap 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
308
309	tc -n $ns qdisc add dev $dev parent 1:1 handle 11: pfifo
310	tc -n $ns qdisc add dev $dev parent 1:2 handle 12: pfifo
311
312	tc -n $ns filter add dev $dev parent 1: protocol ipv$ipver \
313			flower $flower_expr classid 1:2
314}
315
316tc_get_flower_counter() {
317	local -r ns=$1
318	local -r dev=$2
319
320	tc -n $ns -j -s qdisc show dev $dev handle 12: | jq .[0].packets
321}
322
323ret_set_ksft_status()
324{
325	local ksft_status=$1; shift
326	local msg=$1; shift
327
328	RET=$(ksft_status_merge $RET $ksft_status)
329	if (( $? )); then
330		retmsg=$msg
331	fi
332}
333
334log_test_result()
335{
336	local test_name=$1; shift
337	local opt_str=$1; shift
338	local result=$1; shift
339	local retmsg=$1
340
341	printf "TEST: %-60s  [%s]\n" "$test_name $opt_str" "$result"
342	if [[ $retmsg ]]; then
343		printf "\t%s\n" "$retmsg"
344	fi
345}
346
347pause_on_fail()
348{
349	if [[ $PAUSE_ON_FAIL == yes ]]; then
350		echo "Hit enter to continue, 'q' to quit"
351		read a
352		[[ $a == q ]] && exit 1
353	fi
354}
355
356handle_test_result_pass()
357{
358	local test_name=$1; shift
359	local opt_str=$1; shift
360
361	log_test_result "$test_name" "$opt_str" " OK "
362}
363
364handle_test_result_fail()
365{
366	local test_name=$1; shift
367	local opt_str=$1; shift
368
369	log_test_result "$test_name" "$opt_str" FAIL "$retmsg"
370	pause_on_fail
371}
372
373handle_test_result_xfail()
374{
375	local test_name=$1; shift
376	local opt_str=$1; shift
377
378	log_test_result "$test_name" "$opt_str" XFAIL "$retmsg"
379	pause_on_fail
380}
381
382handle_test_result_skip()
383{
384	local test_name=$1; shift
385	local opt_str=$1; shift
386
387	log_test_result "$test_name" "$opt_str" SKIP "$retmsg"
388}
389
390log_test()
391{
392	local test_name=$1
393	local opt_str=$2
394
395	if [[ $# -eq 2 ]]; then
396		opt_str="($opt_str)"
397	fi
398
399	if ((RET == ksft_pass)); then
400		handle_test_result_pass "$test_name" "$opt_str"
401	elif ((RET == ksft_xfail)); then
402		handle_test_result_xfail "$test_name" "$opt_str"
403	elif ((RET == ksft_skip)); then
404		handle_test_result_skip "$test_name" "$opt_str"
405	else
406		handle_test_result_fail "$test_name" "$opt_str"
407	fi
408
409	EXIT_STATUS=$(ksft_exit_status_merge $EXIT_STATUS $RET)
410	return $RET
411}
412
413log_test_skip()
414{
415	RET=$ksft_skip retmsg= log_test "$@"
416}
417
418log_test_xfail()
419{
420	RET=$ksft_xfail retmsg= log_test "$@"
421}
422
423log_info()
424{
425	local msg=$1
426
427	echo "INFO: $msg"
428}
429
430tests_run()
431{
432	local current_test
433
434	for current_test in ${TESTS:-$ALL_TESTS}; do
435		in_defer_scope \
436			$current_test
437	done
438}
439
440# Whether FAILs should be interpreted as XFAILs. Internal.
441FAIL_TO_XFAIL=
442
443check_err()
444{
445	local err=$1
446	local msg=$2
447
448	if ((err)); then
449		if [[ $FAIL_TO_XFAIL = yes ]]; then
450			ret_set_ksft_status $ksft_xfail "$msg"
451		else
452			ret_set_ksft_status $ksft_fail "$msg"
453		fi
454	fi
455}
456
457check_fail()
458{
459	local err=$1
460	local msg=$2
461
462	check_err $((!err)) "$msg"
463}
464
465check_err_fail()
466{
467	local should_fail=$1; shift
468	local err=$1; shift
469	local what=$1; shift
470
471	if ((should_fail)); then
472		check_fail $err "$what succeeded, but should have failed"
473	else
474		check_err $err "$what failed"
475	fi
476}
477
478xfail()
479{
480	FAIL_TO_XFAIL=yes "$@"
481}
482
483xfail_on_slow()
484{
485	if [[ $KSFT_MACHINE_SLOW = yes ]]; then
486		FAIL_TO_XFAIL=yes "$@"
487	else
488		"$@"
489	fi
490}
491
492omit_on_slow()
493{
494	if [[ $KSFT_MACHINE_SLOW != yes ]]; then
495		"$@"
496	fi
497}
498
499xfail_on_veth()
500{
501	local dev=$1; shift
502	local kind
503
504	kind=$(ip -j -d link show dev $dev |
505			jq -r '.[].linkinfo.info_kind')
506	if [[ $kind = veth ]]; then
507		FAIL_TO_XFAIL=yes "$@"
508	else
509		"$@"
510	fi
511}
512
513mac_get()
514{
515	local if_name=$1
516
517	ip -j link show dev $if_name | jq -r '.[]["address"]'
518}
519
520kill_process()
521{
522	local pid=$1; shift
523
524	# Suppress noise from killing the process.
525	{ kill $pid && wait $pid; } 2>/dev/null
526}
527
528check_command()
529{
530	local cmd=$1; shift
531
532	if [[ ! -x "$(command -v "$cmd")" ]]; then
533		log_test_skip "$cmd not installed"
534		return $EXIT_STATUS
535	fi
536}
537
538require_command()
539{
540	local cmd=$1; shift
541
542	if ! check_command "$cmd"; then
543		exit $EXIT_STATUS
544	fi
545}
546
547adf_ip_link_add()
548{
549	local name=$1; shift
550
551	ip link add name "$name" "$@" && \
552		defer ip link del dev "$name"
553}
554
555adf_ip_link_set_master()
556{
557	local member=$1; shift
558	local master=$1; shift
559
560	ip link set dev "$member" master "$master" && \
561		defer ip link set dev "$member" nomaster
562}
563
564adf_ip_link_set_addr()
565{
566	local name=$1; shift
567	local addr=$1; shift
568
569	local old_addr=$(mac_get "$name")
570	ip link set dev "$name" address "$addr" && \
571		defer ip link set dev "$name" address "$old_addr"
572}
573
574ip_link_has_flag()
575{
576	local name=$1; shift
577	local flag=$1; shift
578
579	local state=$(ip -j link show "$name" |
580		      jq --arg flag "$flag" 'any(.[].flags.[]; . == $flag)')
581	[[ $state == true ]]
582}
583
584ip_link_is_up()
585{
586	ip_link_has_flag "$1" UP
587}
588
589adf_ip_link_set_up()
590{
591	local name=$1; shift
592
593	if ! ip_link_is_up "$name"; then
594		ip link set dev "$name" up && \
595			defer ip link set dev "$name" down
596	fi
597}
598
599adf_ip_link_set_down()
600{
601	local name=$1; shift
602
603	if ip_link_is_up "$name"; then
604		ip link set dev "$name" down && \
605			defer ip link set dev "$name" up
606	fi
607}
608
609adf_ip_addr_add()
610{
611	local name=$1; shift
612
613	ip addr add dev "$name" "$@" && \
614		defer ip addr del dev "$name" "$@"
615}
616
617adf_ip_route_add()
618{
619	ip route add "$@" && \
620		defer ip route del "$@"
621}
622
623adf_bridge_vlan_add()
624{
625	bridge vlan add "$@" && \
626		defer bridge vlan del "$@"
627}
628
629wait_local_port_listen()
630{
631	local listener_ns="${1}"
632	local port="${2}"
633	local protocol="${3}"
634	local pattern
635	local i
636
637	pattern=":$(printf "%04X" "${port}") "
638
639	# for tcp protocol additionally check the socket state
640	[ ${protocol} = "tcp" ] && pattern="${pattern}0A"
641	for i in $(seq 10); do
642		if ip netns exec "${listener_ns}" awk '{print $2" "$4}' \
643		   /proc/net/"${protocol}"* | grep -q "${pattern}"; then
644			break
645		fi
646		sleep 0.1
647	done
648}
649
650cmd_jq()
651{
652	local cmd=$1
653	local jq_exp=$2
654	local jq_opts=$3
655	local ret
656	local output
657
658	output="$($cmd)"
659	# it the command fails, return error right away
660	ret=$?
661	if [[ $ret -ne 0 ]]; then
662		return $ret
663	fi
664	output=$(echo $output | jq -r $jq_opts "$jq_exp")
665	ret=$?
666	if [[ $ret -ne 0 ]]; then
667		return $ret
668	fi
669	echo $output
670	# return success only in case of non-empty output
671	[ ! -z "$output" ]
672}
673