aboutsummaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/ipvtap_test.sh
blob: 354ca7ce8584d8ada17839ad0bde817ce0ef7727 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Simple tests for ipvtap


#
# The testing environment looks this way:
#
# |------HNS-------|     |------PHY-------|
# |      veth<----------------->veth      |
# |------|--|------|     |----------------|
#        |  |
#        |  |            |-----TST0-------|
#        |  |------------|----ipvlan      |
#        |               |----------------|
#        |
#        |               |-----TST1-------|
#        |---------------|----ipvlan      |
#                        |----------------|
#

ALL_TESTS="
	test_ip_set
"

source lib.sh

DEBUG=0

VETH_HOST=vethtst.h
VETH_PHY=vethtst.p

NS_COUNT=32
IP_ITERATIONS=1024
IPSET_TIMEOUT="60s"

ns_run() {
	ns=$1
	shift
	if [[ "$ns" == "global" ]]; then
		"$@" >/dev/null
	else
		ip netns exec "$ns" "$@" >/dev/null
	fi
}

test_ip_setup_env() {
	setup_ns NS_PHY
	setup_ns HST_NS

	# setup simulated other-host (phy) and host itself
	ns_run "$HST_NS" ip link add $VETH_HOST type veth peer name $VETH_PHY \
		netns "$NS_PHY" >/dev/null
	ns_run "$HST_NS" ip link set $VETH_HOST up
	ns_run "$NS_PHY" ip link set $VETH_PHY up

	for ((i=0; i<NS_COUNT; i++)); do
		setup_ns ipvlan_ns_$i
		ns="ipvlan_ns_$i"
		if [ "$DEBUG" = "1" ]; then
			echo "created NS ${!ns}"
		fi
		if ! ns_run "$HST_NS" ip link add netns ${!ns} ipvlan0 \
		    link $VETH_HOST \
		    type ipvtap mode l2 bridge; then
			exit_error "FAIL: Failed to configure ipvlan link."
		fi
	done
}

test_ip_cleanup_env() {
	ns_run "$HST_NS" ip link del $VETH_HOST
	cleanup_all_ns
}

exit_error() {
	echo "$1"
	exit $ksft_fail
}

rnd() {
	echo $(( RANDOM % 32 + 16 ))
}

test_ip_set_thread() {
	# Here we are trying to create some IP conflicts between namespaces.
	# If just add/remove IP, nothing interesting will happen.
	# But if add random IP and then remove random IP,
	# eventually conflicts start to apear.
	ip link set ipvlan0 up
	for ((i=0; i<IP_ITERATIONS; i++)); do
		v=$(rnd)
		ip a a "172.25.0.$v/24" dev ipvlan0 2>/dev/null
		ip a a "fc00::$v/64" dev ipvlan0 2>/dev/null
		v=$(rnd)
		ip a d "172.25.0.$v/24" dev ipvlan0 2>/dev/null
		ip a d "fc00::$v/64" dev ipvlan0 2>/dev/null
	done
}

test_ip_set() {
	RET=0

	trap test_ip_cleanup_env EXIT

	test_ip_setup_env

	declare -A ns_pids
	for ((i=0; i<NS_COUNT; i++)); do
		ns="ipvlan_ns_$i"
		ns_run ${!ns} timeout "$IPSET_TIMEOUT" \
			bash -c "$0 test_ip_set_thread"&
		ns_pids[$i]=$!
	done

	for ((i=0; i<NS_COUNT; i++)); do
		wait "${ns_pids[$i]}"
	done

	declare -A all_ips
	for ((i=0; i<NS_COUNT; i++)); do
		ns="ipvlan_ns_$i"
		ip_output=$(ip netns exec ${!ns} ip a l dev ipvlan0 | grep inet)
		while IFS= read -r nsip_out; do
			if [[ -z $nsip_out ]]; then
				continue;
			fi
			nsip=$(awk '{print $2}' <<< "$nsip_out")
			if [[ -v all_ips[$nsip] ]]; then
				RET=$ksft_fail
				log_test "conflict for $nsip"
				return "$RET"
			else
				all_ips[$nsip]=$i
			fi
		done <<< "$ip_output"
	done

	if [ "$DEBUG" = "1" ]; then
		for key in "${!all_ips[@]}"; do
			echo "$key: ${all_ips[$key]}"
		done
	fi

	trap - EXIT
	test_ip_cleanup_env

	log_test "test multithreaded ip set"
}

if [[ "$1" == "-d" ]]; then
	DEBUG=1
	shift
fi

if [[ "$1" == "-t" ]]; then
	shift
	TESTS="$*"
fi

if [[ "$1" == "test_ip_set_thread" ]]; then
	test_ip_set_thread
else
	require_command ip

	tests_run
fi