1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3#
4# OVS kernel module self tests
5
6trap ovs_exit_sig EXIT TERM INT ERR
7
8# Kselftest framework requirement - SKIP code is 4.
9ksft_skip=4
10
11PAUSE_ON_FAIL=no
12VERBOSE=0
13TRACING=0
14
15tests="
16	arp_ping				eth-arp: Basic arp ping between two NS
17	ct_connect_v4				ip4-ct-xon: Basic ipv4 tcp connection using ct
18	connect_v4				ip4-xon: Basic ipv4 ping between two NS
19	nat_connect_v4				ip4-nat-xon: Basic ipv4 tcp connection via NAT
20	nat_related_v4				ip4-nat-related: ICMP related matches work with SNAT
21	netlink_checks				ovsnl: validate netlink attrs and settings
22	upcall_interfaces			ovs: test the upcall interfaces
23	drop_reason				drop: test drop reasons are emitted"
24
25info() {
26    [ $VERBOSE = 0 ] || echo $*
27}
28
29ovs_base=`pwd`
30sbxs=
31sbx_add () {
32	info "adding sandbox '$1'"
33
34	sbxs="$sbxs $1"
35
36	NO_BIN=0
37
38	# Create sandbox.
39	local d="$ovs_base"/$1
40	if [ -e $d ]; then
41		info "removing $d"
42		rm -rf "$d"
43	fi
44	mkdir "$d" || return 1
45	ovs_setenv $1
46}
47
48ovs_exit_sig() {
49	[ -e ${ovs_dir}/cleanup ] && . "$ovs_dir/cleanup"
50}
51
52on_exit() {
53	echo "$1" > ${ovs_dir}/cleanup.tmp
54	cat ${ovs_dir}/cleanup >> ${ovs_dir}/cleanup.tmp
55	mv ${ovs_dir}/cleanup.tmp ${ovs_dir}/cleanup
56}
57
58ovs_setenv() {
59	sandbox=$1
60
61	ovs_dir=$ovs_base${1:+/$1}; export ovs_dir
62
63	test -e ${ovs_dir}/cleanup || : > ${ovs_dir}/cleanup
64}
65
66ovs_sbx() {
67	if test "X$2" != X; then
68		(ovs_setenv $1; shift; "$@" >> ${ovs_dir}/debug.log)
69	else
70		ovs_setenv $1
71	fi
72}
73
74ovs_add_dp () {
75	info "Adding DP/Bridge IF: sbx:$1 dp:$2 {$3, $4, $5}"
76	sbxname="$1"
77	shift
78	ovs_sbx "$sbxname" python3 $ovs_base/ovs-dpctl.py add-dp $*
79	on_exit "ovs_sbx $sbxname python3 $ovs_base/ovs-dpctl.py del-dp $1;"
80}
81
82ovs_add_if () {
83	info "Adding IF to DP: br:$2 if:$3"
84	if [ "$4" != "-u" ]; then
85		ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py add-if "$2" "$3" \
86		    || return 1
87	else
88		python3 $ovs_base/ovs-dpctl.py add-if \
89		    -u "$2" "$3" >$ovs_dir/$3.out 2>$ovs_dir/$3.err &
90		pid=$!
91		on_exit "ovs_sbx $1 kill -TERM $pid 2>/dev/null"
92	fi
93}
94
95ovs_del_if () {
96	info "Deleting IF from DP: br:$2 if:$3"
97	ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py del-if "$2" "$3" || return 1
98}
99
100ovs_netns_spawn_daemon() {
101	sbx=$1
102	shift
103	netns=$1
104	shift
105	info "spawning cmd: $*"
106	ip netns exec $netns $*  >> $ovs_dir/stdout  2>> $ovs_dir/stderr &
107	pid=$!
108	ovs_sbx "$sbx" on_exit "kill -TERM $pid 2>/dev/null"
109}
110
111ovs_add_netns_and_veths () {
112	info "Adding netns attached: sbx:$1 dp:$2 {$3, $4, $5}"
113	ovs_sbx "$1" ip netns add "$3" || return 1
114	on_exit "ovs_sbx $1 ip netns del $3"
115	ovs_sbx "$1" ip link add "$4" type veth peer name "$5" || return 1
116	on_exit "ovs_sbx $1 ip link del $4 >/dev/null 2>&1"
117	ovs_sbx "$1" ip link set "$4" up || return 1
118	ovs_sbx "$1" ip link set "$5" netns "$3" || return 1
119	ovs_sbx "$1" ip netns exec "$3" ip link set "$5" up || return 1
120
121	if [ "$6" != "" ]; then
122		ovs_sbx "$1" ip netns exec "$3" ip addr add "$6" dev "$5" \
123		    || return 1
124	fi
125
126	if [ "$7" != "-u" ]; then
127		ovs_add_if "$1" "$2" "$4" || return 1
128	else
129		ovs_add_if "$1" "$2" "$4" -u || return 1
130	fi
131
132	[ $TRACING -eq 1 ] && ovs_netns_spawn_daemon "$1" "$ns" \
133			tcpdump -i any -s 65535
134
135	return 0
136}
137
138ovs_add_flow () {
139	info "Adding flow to DP: sbx:$1 br:$2 flow:$3 act:$4"
140	ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py add-flow "$2" "$3" "$4"
141	if [ $? -ne 0 ]; then
142		echo "Flow [ $3 : $4 ] failed" >> ${ovs_dir}/debug.log
143		return 1
144	fi
145	return 0
146}
147
148ovs_del_flows () {
149	info "Deleting all flows from DP: sbx:$1 br:$2"
150	ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py del-flows "$2"
151	return 0
152}
153
154ovs_drop_record_and_run () {
155	local sbx=$1
156	shift
157
158	perf record -a -q -e skb:kfree_skb -o ${ovs_dir}/perf.data $* \
159		>> ${ovs_dir}/stdout 2>> ${ovs_dir}/stderr
160	return $?
161}
162
163ovs_drop_reason_count()
164{
165	local reason=$1
166
167	local perf_output=`perf script -i ${ovs_dir}/perf.data -F trace:event,trace`
168	local pattern="skb:kfree_skb:.*reason: $reason"
169
170	return `echo "$perf_output" | grep "$pattern" | wc -l`
171}
172
173usage() {
174	echo
175	echo "$0 [OPTIONS] [TEST]..."
176	echo "If no TEST argument is given, all tests will be run."
177	echo
178	echo "Options"
179	echo "  -t: capture traffic via tcpdump"
180	echo "  -v: verbose"
181	echo "  -p: pause on failure"
182	echo
183	echo "Available tests${tests}"
184	exit 1
185}
186
187# drop_reason test
188# - drop packets and verify the right drop reason is reported
189test_drop_reason() {
190	which perf >/dev/null 2>&1 || return $ksft_skip
191
192	sbx_add "test_drop_reason" || return $?
193
194	ovs_add_dp "test_drop_reason" dropreason || return 1
195
196	info "create namespaces"
197	for ns in client server; do
198		ovs_add_netns_and_veths "test_drop_reason" "dropreason" "$ns" \
199			"${ns:0:1}0" "${ns:0:1}1" || return 1
200	done
201
202	# Setup client namespace
203	ip netns exec client ip addr add 172.31.110.10/24 dev c1
204	ip netns exec client ip link set c1 up
205
206	# Setup server namespace
207	ip netns exec server ip addr add 172.31.110.20/24 dev s1
208	ip netns exec server ip link set s1 up
209
210	# Check if drop reasons can be sent
211	ovs_add_flow "test_drop_reason" dropreason \
212		'in_port(1),eth(),eth_type(0x0806),arp()' 'drop(10)' 2>/dev/null
213	if [ $? == 1 ]; then
214		info "no support for drop reasons - skipping"
215		ovs_exit_sig
216		return $ksft_skip
217	fi
218
219	ovs_del_flows "test_drop_reason" dropreason
220
221	# Allow ARP
222	ovs_add_flow "test_drop_reason" dropreason \
223		'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
224	ovs_add_flow "test_drop_reason" dropreason \
225		'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
226
227	# Allow client ICMP traffic but drop return path
228	ovs_add_flow "test_drop_reason" dropreason \
229		"in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10,proto=1),icmp()" '2'
230	ovs_add_flow "test_drop_reason" dropreason \
231		"in_port(2),eth(),eth_type(0x0800),ipv4(src=172.31.110.20,proto=1),icmp()" 'drop'
232
233	ovs_drop_record_and_run "test_drop_reason" ip netns exec client ping -c 2 172.31.110.20
234	ovs_drop_reason_count 0x30001 # OVS_DROP_FLOW_ACTION
235	if [[ "$?" -ne "2" ]]; then
236		info "Did not detect expected drops: $?"
237		return 1
238	fi
239
240	# Drop UDP 6000 traffic with an explicit action and an error code.
241	ovs_add_flow "test_drop_reason" dropreason \
242		"in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10,proto=17),udp(dst=6000)" \
243                'drop(42)'
244	# Drop UDP 7000 traffic with an explicit action with no error code.
245	ovs_add_flow "test_drop_reason" dropreason \
246		"in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10,proto=17),udp(dst=7000)" \
247                'drop(0)'
248
249	ovs_drop_record_and_run \
250            "test_drop_reason" ip netns exec client nc -i 1 -zuv 172.31.110.20 6000
251	ovs_drop_reason_count 0x30004 # OVS_DROP_EXPLICIT_ACTION_ERROR
252	if [[ "$?" -ne "1" ]]; then
253		info "Did not detect expected explicit error drops: $?"
254		return 1
255	fi
256
257	ovs_drop_record_and_run \
258            "test_drop_reason" ip netns exec client nc -i 1 -zuv 172.31.110.20 7000
259	ovs_drop_reason_count 0x30003 # OVS_DROP_EXPLICIT_ACTION
260	if [[ "$?" -ne "1" ]]; then
261		info "Did not detect expected explicit drops: $?"
262		return 1
263	fi
264
265	return 0
266}
267
268# arp_ping test
269# - client has 1500 byte MTU
270# - server has 1500 byte MTU
271# - send ARP ping between two ns
272test_arp_ping () {
273
274	which arping >/dev/null 2>&1 || return $ksft_skip
275
276	sbx_add "test_arp_ping" || return $?
277
278	ovs_add_dp "test_arp_ping" arpping || return 1
279
280	info "create namespaces"
281	for ns in client server; do
282		ovs_add_netns_and_veths "test_arp_ping" "arpping" "$ns" \
283		    "${ns:0:1}0" "${ns:0:1}1" || return 1
284	done
285
286	# Setup client namespace
287	ip netns exec client ip addr add 172.31.110.10/24 dev c1
288	ip netns exec client ip link set c1 up
289	HW_CLIENT=`ip netns exec client ip link show dev c1 | grep -E 'link/ether [0-9a-f:]+' | awk '{print $2;}'`
290	info "Client hwaddr: $HW_CLIENT"
291
292	# Setup server namespace
293	ip netns exec server ip addr add 172.31.110.20/24 dev s1
294	ip netns exec server ip link set s1 up
295	HW_SERVER=`ip netns exec server ip link show dev s1 | grep -E 'link/ether [0-9a-f:]+' | awk '{print $2;}'`
296	info "Server hwaddr: $HW_SERVER"
297
298	ovs_add_flow "test_arp_ping" arpping \
299		"in_port(1),eth(),eth_type(0x0806),arp(sip=172.31.110.10,tip=172.31.110.20,sha=$HW_CLIENT,tha=ff:ff:ff:ff:ff:ff)" '2' || return 1
300	ovs_add_flow "test_arp_ping" arpping \
301		"in_port(2),eth(),eth_type(0x0806),arp()" '1' || return 1
302
303	ovs_sbx "test_arp_ping" ip netns exec client arping -I c1 172.31.110.20 -c 1 || return 1
304
305	return 0
306}
307
308# ct_connect_v4 test
309#  - client has 1500 byte MTU
310#  - server has 1500 byte MTU
311#  - use ICMP to ping in each direction
312#  - only allow CT state stuff to pass through new in c -> s
313test_ct_connect_v4 () {
314
315	which nc >/dev/null 2>/dev/null || return $ksft_skip
316
317	sbx_add "test_ct_connect_v4" || return $?
318
319	ovs_add_dp "test_ct_connect_v4" ct4 || return 1
320	info "create namespaces"
321	for ns in client server; do
322		ovs_add_netns_and_veths "test_ct_connect_v4" "ct4" "$ns" \
323		    "${ns:0:1}0" "${ns:0:1}1" || return 1
324	done
325
326	ip netns exec client ip addr add 172.31.110.10/24 dev c1
327	ip netns exec client ip link set c1 up
328	ip netns exec server ip addr add 172.31.110.20/24 dev s1
329	ip netns exec server ip link set s1 up
330
331	# Add forwarding for ARP and ip packets - completely wildcarded
332	ovs_add_flow "test_ct_connect_v4" ct4 \
333		'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
334	ovs_add_flow "test_ct_connect_v4" ct4 \
335		'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
336	ovs_add_flow "test_ct_connect_v4" ct4 \
337		     'ct_state(-trk),eth(),eth_type(0x0800),ipv4()' \
338		     'ct(commit),recirc(0x1)' || return 1
339	ovs_add_flow "test_ct_connect_v4" ct4 \
340		     'recirc_id(0x1),ct_state(+trk+new),in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' \
341		     '2' || return 1
342	ovs_add_flow "test_ct_connect_v4" ct4 \
343		     'recirc_id(0x1),ct_state(+trk+est),in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' \
344		     '2' || return 1
345	ovs_add_flow "test_ct_connect_v4" ct4 \
346		     'recirc_id(0x1),ct_state(+trk+est),in_port(2),eth(),eth_type(0x0800),ipv4(dst=172.31.110.10)' \
347		     '1' || return 1
348	ovs_add_flow "test_ct_connect_v4" ct4 \
349		     'recirc_id(0x1),ct_state(+trk+inv),eth(),eth_type(0x0800),ipv4()' 'drop' || \
350		     return 1
351
352	# do a ping
353	ovs_sbx "test_ct_connect_v4" ip netns exec client ping 172.31.110.20 -c 3 || return 1
354
355	# create an echo server in 'server'
356	echo "server" | \
357		ovs_netns_spawn_daemon "test_ct_connect_v4" "server" \
358				nc -lvnp 4443
359	ovs_sbx "test_ct_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.20 4443 || return 1
360
361	# Now test in the other direction (should fail)
362	echo "client" | \
363		ovs_netns_spawn_daemon "test_ct_connect_v4" "client" \
364				nc -lvnp 4443
365	ovs_sbx "test_ct_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.10 4443
366	if [ $? == 0 ]; then
367	   info "ct connect to client was successful"
368	   return 1
369	fi
370
371	info "done..."
372	return 0
373}
374
375# connect_v4 test
376#  - client has 1500 byte MTU
377#  - server has 1500 byte MTU
378#  - use ICMP to ping in each direction
379test_connect_v4 () {
380
381	sbx_add "test_connect_v4" || return $?
382
383	ovs_add_dp "test_connect_v4" cv4 || return 1
384
385	info "create namespaces"
386	for ns in client server; do
387		ovs_add_netns_and_veths "test_connect_v4" "cv4" "$ns" \
388		    "${ns:0:1}0" "${ns:0:1}1" || return 1
389	done
390
391
392	ip netns exec client ip addr add 172.31.110.10/24 dev c1
393	ip netns exec client ip link set c1 up
394	ip netns exec server ip addr add 172.31.110.20/24 dev s1
395	ip netns exec server ip link set s1 up
396
397	# Add forwarding for ARP and ip packets - completely wildcarded
398	ovs_add_flow "test_connect_v4" cv4 \
399		'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
400	ovs_add_flow "test_connect_v4" cv4 \
401		'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
402	ovs_add_flow "test_connect_v4" cv4 \
403		'in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' '2' || return 1
404	ovs_add_flow "test_connect_v4" cv4 \
405		'in_port(2),eth(),eth_type(0x0800),ipv4(src=172.31.110.20)' '1' || return 1
406
407	# do a ping
408	ovs_sbx "test_connect_v4" ip netns exec client ping 172.31.110.20 -c 3 || return 1
409
410	info "done..."
411	return 0
412}
413
414# nat_connect_v4 test
415#  - client has 1500 byte MTU
416#  - server has 1500 byte MTU
417#  - use ICMP to ping in each direction
418#  - only allow CT state stuff to pass through new in c -> s
419test_nat_connect_v4 () {
420	which nc >/dev/null 2>/dev/null || return $ksft_skip
421
422	sbx_add "test_nat_connect_v4" || return $?
423
424	ovs_add_dp "test_nat_connect_v4" nat4 || return 1
425	info "create namespaces"
426	for ns in client server; do
427		ovs_add_netns_and_veths "test_nat_connect_v4" "nat4" "$ns" \
428		    "${ns:0:1}0" "${ns:0:1}1" || return 1
429	done
430
431	ip netns exec client ip addr add 172.31.110.10/24 dev c1
432	ip netns exec client ip link set c1 up
433	ip netns exec server ip addr add 172.31.110.20/24 dev s1
434	ip netns exec server ip link set s1 up
435
436	ip netns exec client ip route add default via 172.31.110.20
437
438	ovs_add_flow "test_nat_connect_v4" nat4 \
439		'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
440	ovs_add_flow "test_nat_connect_v4" nat4 \
441		'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
442	ovs_add_flow "test_nat_connect_v4" nat4 \
443		"ct_state(-trk),in_port(1),eth(),eth_type(0x0800),ipv4(dst=192.168.0.20)" \
444		"ct(commit,nat(dst=172.31.110.20)),recirc(0x1)"
445	ovs_add_flow "test_nat_connect_v4" nat4 \
446		"ct_state(-trk),in_port(2),eth(),eth_type(0x0800),ipv4()" \
447		"ct(commit,nat),recirc(0x2)"
448
449	ovs_add_flow "test_nat_connect_v4" nat4 \
450		"recirc_id(0x1),ct_state(+trk-inv),in_port(1),eth(),eth_type(0x0800),ipv4()" "2"
451	ovs_add_flow "test_nat_connect_v4" nat4 \
452		"recirc_id(0x2),ct_state(+trk-inv),in_port(2),eth(),eth_type(0x0800),ipv4()" "1"
453
454	# do a ping
455	ovs_sbx "test_nat_connect_v4" ip netns exec client ping 192.168.0.20 -c 3 || return 1
456
457	# create an echo server in 'server'
458	echo "server" | \
459		ovs_netns_spawn_daemon "test_nat_connect_v4" "server" \
460				nc -lvnp 4443
461	ovs_sbx "test_nat_connect_v4" ip netns exec client nc -i 1 -zv 192.168.0.20 4443 || return 1
462
463	# Now test in the other direction (should fail)
464	echo "client" | \
465		ovs_netns_spawn_daemon "test_nat_connect_v4" "client" \
466				nc -lvnp 4443
467	ovs_sbx "test_nat_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.10 4443
468	if [ $? == 0 ]; then
469	   info "connect to client was successful"
470	   return 1
471	fi
472
473	info "done..."
474	return 0
475}
476
477# nat_related_v4 test
478#  - client->server ip packets go via SNAT
479#  - client solicits ICMP destination unreachable packet from server
480#  - undo NAT for ICMP reply and test dst ip has been updated
481test_nat_related_v4 () {
482	which nc >/dev/null 2>/dev/null || return $ksft_skip
483
484	sbx_add "test_nat_related_v4" || return $?
485
486	ovs_add_dp "test_nat_related_v4" natrelated4 || return 1
487	info "create namespaces"
488	for ns in client server; do
489		ovs_add_netns_and_veths "test_nat_related_v4" "natrelated4" "$ns" \
490			"${ns:0:1}0" "${ns:0:1}1" || return 1
491	done
492
493	ip netns exec client ip addr add 172.31.110.10/24 dev c1
494	ip netns exec client ip link set c1 up
495	ip netns exec server ip addr add 172.31.110.20/24 dev s1
496	ip netns exec server ip link set s1 up
497
498	ip netns exec server ip route add 192.168.0.20/32 via 172.31.110.10
499
500	# Allow ARP
501	ovs_add_flow "test_nat_related_v4" natrelated4 \
502		"in_port(1),eth(),eth_type(0x0806),arp()" "2" || return 1
503	ovs_add_flow "test_nat_related_v4" natrelated4 \
504		"in_port(2),eth(),eth_type(0x0806),arp()" "1" || return 1
505
506	# Allow IP traffic from client->server, rewrite source IP with SNAT to 192.168.0.20
507	ovs_add_flow "test_nat_related_v4" natrelated4 \
508		"ct_state(-trk),in_port(1),eth(),eth_type(0x0800),ipv4(dst=172.31.110.20)" \
509		"ct(commit,nat(src=192.168.0.20)),recirc(0x1)" || return 1
510	ovs_add_flow "test_nat_related_v4" natrelated4 \
511		"recirc_id(0x1),ct_state(+trk-inv),in_port(1),eth(),eth_type(0x0800),ipv4()" \
512		"2" || return 1
513
514	# Allow related ICMP responses back from server and undo NAT to restore original IP
515	# Drop any ICMP related packets where dst ip hasn't been restored back to original IP
516	ovs_add_flow "test_nat_related_v4" natrelated4 \
517		"ct_state(-trk),in_port(2),eth(),eth_type(0x0800),ipv4()" \
518		"ct(commit,nat),recirc(0x2)" || return 1
519	ovs_add_flow "test_nat_related_v4" natrelated4 \
520		"recirc_id(0x2),ct_state(+rel+trk),in_port(2),eth(),eth_type(0x0800),ipv4(src=172.31.110.20,dst=172.31.110.10,proto=1),icmp()" \
521		"1" || return 1
522	ovs_add_flow "test_nat_related_v4" natrelated4 \
523		"recirc_id(0x2),ct_state(+rel+trk),in_port(2),eth(),eth_type(0x0800),ipv4(dst=192.168.0.20,proto=1),icmp()" \
524		"drop" || return 1
525
526	# Solicit destination unreachable response from server
527	ovs_sbx "test_nat_related_v4" ip netns exec client \
528		bash -c "echo a | nc -u -w 1 172.31.110.20 10000"
529
530	# Check to make sure no packets matched the drop rule with incorrect dst ip
531	python3 "$ovs_base/ovs-dpctl.py" dump-flows natrelated4 \
532		| grep "drop" | grep "packets:0" >/dev/null || return 1
533
534	info "done..."
535	return 0
536}
537
538# netlink_validation
539# - Create a dp
540# - check no warning with "old version" simulation
541test_netlink_checks () {
542	sbx_add "test_netlink_checks" || return 1
543
544	info "setting up new DP"
545	ovs_add_dp "test_netlink_checks" nv0 || return 1
546	# now try again
547	PRE_TEST=$(dmesg | grep -E "RIP: [0-9a-fA-Fx]+:ovs_dp_cmd_new\+")
548	ovs_add_dp "test_netlink_checks" nv0 -V 0 || return 1
549	POST_TEST=$(dmesg | grep -E "RIP: [0-9a-fA-Fx]+:ovs_dp_cmd_new\+")
550	if [ "$PRE_TEST" != "$POST_TEST" ]; then
551		info "failed - gen warning"
552		return 1
553	fi
554
555	ovs_add_netns_and_veths "test_netlink_checks" nv0 left left0 l0 || \
556	    return 1
557	ovs_add_netns_and_veths "test_netlink_checks" nv0 right right0 r0 || \
558	    return 1
559	[ $(python3 $ovs_base/ovs-dpctl.py show nv0 | grep port | \
560	    wc -l) == 3 ] || \
561	      return 1
562	ovs_del_if "test_netlink_checks" nv0 right0 || return 1
563	[ $(python3 $ovs_base/ovs-dpctl.py show nv0 | grep port | \
564	    wc -l) == 2 ] || \
565	      return 1
566
567	info "Checking clone depth"
568	ERR_MSG="Flow actions may not be safe on all matching packets"
569	PRE_TEST=$(dmesg | grep -c "${ERR_MSG}")
570	ovs_add_flow "test_netlink_checks" nv0 \
571		'in_port(1),eth(),eth_type(0x800),ipv4()' \
572		'clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(clone(drop)))))))))))))))))' \
573		>/dev/null 2>&1 && return 1
574	POST_TEST=$(dmesg | grep -c "${ERR_MSG}")
575
576	if [ "$PRE_TEST" == "$POST_TEST" ]; then
577		info "failed - clone depth too large"
578		return 1
579	fi
580
581	PRE_TEST=$(dmesg | grep -c "${ERR_MSG}")
582	ovs_add_flow "test_netlink_checks" nv0 \
583		'in_port(1),eth(),eth_type(0x0806),arp()' 'drop(0),2' \
584		&> /dev/null && return 1
585	POST_TEST=$(dmesg | grep -c "${ERR_MSG}")
586	if [ "$PRE_TEST" == "$POST_TEST" ]; then
587		info "failed - error not generated"
588		return 1
589	fi
590	return 0
591}
592
593test_upcall_interfaces() {
594	sbx_add "test_upcall_interfaces" || return 1
595
596	info "setting up new DP"
597	ovs_add_dp "test_upcall_interfaces" ui0 -V 2:1 || return 1
598
599	ovs_add_netns_and_veths "test_upcall_interfaces" ui0 upc left0 l0 \
600	    172.31.110.1/24 -u || return 1
601
602	sleep 1
603	info "sending arping"
604	ip netns exec upc arping -I l0 172.31.110.20 -c 1 \
605	    >$ovs_dir/arping.stdout 2>$ovs_dir/arping.stderr
606
607	grep -E "MISS upcall\[0/yes\]: .*arp\(sip=172.31.110.1,tip=172.31.110.20,op=1,sha=" $ovs_dir/left0.out >/dev/null 2>&1 || return 1
608	return 0
609}
610
611run_test() {
612	(
613	tname="$1"
614	tdesc="$2"
615
616	if ! lsmod | grep openvswitch >/dev/null 2>&1; then
617		stdbuf -o0 printf "TEST: %-60s  [NOMOD]\n" "${tdesc}"
618		return $ksft_skip
619	fi
620
621	if python3 ovs-dpctl.py -h 2>&1 | \
622	     grep -E "Need to (install|upgrade) the python" >/dev/null 2>&1; then
623		stdbuf -o0 printf "TEST: %-60s  [PYLIB]\n" "${tdesc}"
624		return $ksft_skip
625	fi
626	printf "TEST: %-60s  [START]\n" "${tname}"
627
628	unset IFS
629
630	eval test_${tname}
631	ret=$?
632
633	if [ $ret -eq 0 ]; then
634		printf "TEST: %-60s  [ OK ]\n" "${tdesc}"
635		ovs_exit_sig
636		rm -rf "$ovs_dir"
637	elif [ $ret -eq 1 ]; then
638		printf "TEST: %-60s  [FAIL]\n" "${tdesc}"
639		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
640			echo
641			echo "Pausing. Logs in $ovs_dir/. Hit enter to continue"
642			read a
643		fi
644		ovs_exit_sig
645		[ "${PAUSE_ON_FAIL}" = "yes" ] || rm -rf "$ovs_dir"
646		exit 1
647	elif [ $ret -eq $ksft_skip ]; then
648		printf "TEST: %-60s  [SKIP]\n" "${tdesc}"
649	elif [ $ret -eq 2 ]; then
650		rm -rf test_${tname}
651		run_test "$1" "$2"
652	fi
653
654	return $ret
655	)
656	ret=$?
657	case $ret in
658		0)
659			[ $all_skipped = true ] && [ $exitcode=$ksft_skip ] && exitcode=0
660			all_skipped=false
661		;;
662		$ksft_skip)
663			[ $all_skipped = true ] && exitcode=$ksft_skip
664		;;
665		*)
666			all_skipped=false
667			exitcode=1
668		;;
669	esac
670
671	return $ret
672}
673
674
675exitcode=0
676desc=0
677all_skipped=true
678
679while getopts :pvt o
680do
681	case $o in
682	p) PAUSE_ON_FAIL=yes;;
683	v) VERBOSE=1;;
684	t) if which tcpdump > /dev/null 2>&1; then
685		TRACING=1
686	   else
687		echo "=== tcpdump not available, tracing disabled"
688	   fi
689	   ;;
690	*) usage;;
691	esac
692done
693shift $(($OPTIND-1))
694
695IFS="	
696"
697
698for arg do
699	# Check first that all requested tests are available before running any
700	command -v > /dev/null "test_${arg}" || { echo "=== Test ${arg} not found"; usage; }
701done
702
703name=""
704desc=""
705for t in ${tests}; do
706	[ "${name}" = "" ]	&& name="${t}"	&& continue
707	[ "${desc}" = "" ]	&& desc="${t}"
708
709	run_this=1
710	for arg do
711		[ "${arg}" != "${arg#--*}" ] && continue
712		[ "${arg}" = "${name}" ] && run_this=1 && break
713		run_this=0
714	done
715	if [ $run_this -eq 1 ]; then
716		run_test "${name}" "${desc}"
717	fi
718	name=""
719	desc=""
720done
721
722exit ${exitcode}
723