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