2 # SPDX-License-Identifier: GPL-2.0
4 # OVS kernel module self tests
6 trap ovs_exit_sig EXIT TERM INT ERR
8 # Kselftest framework requirement - SKIP code is 4.
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 netlink_checks ovsnl: validate netlink attrs and settings
21 upcall_interfaces ovs: test the upcall interfaces
22 drop_reason drop: test drop reasons are emitted"
25 [ $VERBOSE = 0 ] || echo $*
31 info "adding sandbox '$1'"
38 local d="$ovs_base"/$1
43 mkdir "$d" || return 1
48 [ -e ${ovs_dir}/cleanup ] && . "$ovs_dir/cleanup"
52 echo "$1" > ${ovs_dir}/cleanup.tmp
53 cat ${ovs_dir}/cleanup >> ${ovs_dir}/cleanup.tmp
54 mv ${ovs_dir}/cleanup.tmp ${ovs_dir}/cleanup
60 ovs_dir=$ovs_base${1:+/$1}; export ovs_dir
62 test -e ${ovs_dir}/cleanup || : > ${ovs_dir}/cleanup
66 if test "X$2" != X; then
67 (ovs_setenv $1; shift; "$@" >> ${ovs_dir}/debug.log)
74 info "Adding DP/Bridge IF: sbx:$1 dp:$2 {$3, $4, $5}"
77 ovs_sbx "$sbxname" python3 $ovs_base/ovs-dpctl.py add-dp $*
78 on_exit "ovs_sbx $sbxname python3 $ovs_base/ovs-dpctl.py del-dp $1;"
82 info "Adding IF to DP: br:$2 if:$3"
83 if [ "$4" != "-u" ]; then
84 ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py add-if "$2" "$3" \
87 python3 $ovs_base/ovs-dpctl.py add-if \
88 -u "$2" "$3" >$ovs_dir/$3.out 2>$ovs_dir/$3.err &
90 on_exit "ovs_sbx $1 kill -TERM $pid 2>/dev/null"
95 info "Deleting IF from DP: br:$2 if:$3"
96 ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py del-if "$2" "$3" || return 1
99 ovs_netns_spawn_daemon() {
104 info "spawning cmd: $*"
105 ip netns exec $netns $* >> $ovs_dir/stdout 2>> $ovs_dir/stderr &
107 ovs_sbx "$sbx" on_exit "kill -TERM $pid 2>/dev/null"
110 ovs_add_netns_and_veths () {
111 info "Adding netns attached: sbx:$1 dp:$2 {$3, $4, $5}"
112 ovs_sbx "$1" ip netns add "$3" || return 1
113 on_exit "ovs_sbx $1 ip netns del $3"
114 ovs_sbx "$1" ip link add "$4" type veth peer name "$5" || return 1
115 on_exit "ovs_sbx $1 ip link del $4 >/dev/null 2>&1"
116 ovs_sbx "$1" ip link set "$4" up || return 1
117 ovs_sbx "$1" ip link set "$5" netns "$3" || return 1
118 ovs_sbx "$1" ip netns exec "$3" ip link set "$5" up || return 1
120 if [ "$6" != "" ]; then
121 ovs_sbx "$1" ip netns exec "$3" ip addr add "$6" dev "$5" \
125 if [ "$7" != "-u" ]; then
126 ovs_add_if "$1" "$2" "$4" || return 1
128 ovs_add_if "$1" "$2" "$4" -u || return 1
131 [ $TRACING -eq 1 ] && ovs_netns_spawn_daemon "$1" "$ns" \
132 tcpdump -i any -s 65535
138 info "Adding flow to DP: sbx:$1 br:$2 flow:$3 act:$4"
139 ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py add-flow "$2" "$3" "$4"
140 if [ $? -ne 0 ]; then
141 echo "Flow [ $3 : $4 ] failed" >> ${ovs_dir}/debug.log
148 info "Deleting all flows from DP: sbx:$1 br:$2"
149 ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py del-flows "$2"
153 ovs_drop_record_and_run () {
157 perf record -a -q -e skb:kfree_skb -o ${ovs_dir}/perf.data $* \
158 >> ${ovs_dir}/stdout 2>> ${ovs_dir}/stderr
162 ovs_drop_reason_count()
166 local perf_output=`perf script -i ${ovs_dir}/perf.data -F trace:event,trace`
167 local pattern="skb:kfree_skb:.*reason: $reason"
169 return `echo "$perf_output" | grep "$pattern" | wc -l`
174 echo "$0 [OPTIONS] [TEST]..."
175 echo "If no TEST argument is given, all tests will be run."
178 echo " -t: capture traffic via tcpdump"
180 echo " -p: pause on failure"
182 echo "Available tests${tests}"
187 # - drop packets and verify the right drop reason is reported
189 which perf >/dev/null 2>&1 || return $ksft_skip
191 sbx_add "test_drop_reason" || return $?
193 ovs_add_dp "test_drop_reason" dropreason || return 1
195 info "create namespaces"
196 for ns in client server; do
197 ovs_add_netns_and_veths "test_drop_reason" "dropreason" "$ns" \
198 "${ns:0:1}0" "${ns:0:1}1" || return 1
201 # Setup client namespace
202 ip netns exec client ip addr add 172.31.110.10/24 dev c1
203 ip netns exec client ip link set c1 up
205 # Setup server namespace
206 ip netns exec server ip addr add 172.31.110.20/24 dev s1
207 ip netns exec server ip link set s1 up
209 # Check if drop reasons can be sent
210 ovs_add_flow "test_drop_reason" dropreason \
211 'in_port(1),eth(),eth_type(0x0806),arp()' 'drop(10)' 2>/dev/null
213 info "no support for drop reasons - skipping"
218 ovs_del_flows "test_drop_reason" dropreason
221 ovs_add_flow "test_drop_reason" dropreason \
222 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
223 ovs_add_flow "test_drop_reason" dropreason \
224 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
226 # Allow client ICMP traffic but drop return path
227 ovs_add_flow "test_drop_reason" dropreason \
228 "in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10,proto=1),icmp()" '2'
229 ovs_add_flow "test_drop_reason" dropreason \
230 "in_port(2),eth(),eth_type(0x0800),ipv4(src=172.31.110.20,proto=1),icmp()" 'drop'
232 ovs_drop_record_and_run "test_drop_reason" ip netns exec client ping -c 2 172.31.110.20
233 ovs_drop_reason_count 0x30001 # OVS_DROP_FLOW_ACTION
234 if [[ "$?" -ne "2" ]]; then
235 info "Did not detect expected drops: $?"
239 # Drop UDP 6000 traffic with an explicit action and an error code.
240 ovs_add_flow "test_drop_reason" dropreason \
241 "in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10,proto=17),udp(dst=6000)" \
243 # Drop UDP 7000 traffic with an explicit action with no 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=7000)" \
248 ovs_drop_record_and_run \
249 "test_drop_reason" ip netns exec client nc -i 1 -zuv 172.31.110.20 6000
250 ovs_drop_reason_count 0x30004 # OVS_DROP_EXPLICIT_ACTION_ERROR
251 if [[ "$?" -ne "1" ]]; then
252 info "Did not detect expected explicit error drops: $?"
256 ovs_drop_record_and_run \
257 "test_drop_reason" ip netns exec client nc -i 1 -zuv 172.31.110.20 7000
258 ovs_drop_reason_count 0x30003 # OVS_DROP_EXPLICIT_ACTION
259 if [[ "$?" -ne "1" ]]; then
260 info "Did not detect expected explicit drops: $?"
268 # - client has 1500 byte MTU
269 # - server has 1500 byte MTU
270 # - send ARP ping between two ns
273 which arping >/dev/null 2>&1 || return $ksft_skip
275 sbx_add "test_arp_ping" || return $?
277 ovs_add_dp "test_arp_ping" arpping || return 1
279 info "create namespaces"
280 for ns in client server; do
281 ovs_add_netns_and_veths "test_arp_ping" "arpping" "$ns" \
282 "${ns:0:1}0" "${ns:0:1}1" || return 1
285 # Setup client namespace
286 ip netns exec client ip addr add 172.31.110.10/24 dev c1
287 ip netns exec client ip link set c1 up
288 HW_CLIENT=`ip netns exec client ip link show dev c1 | grep -E 'link/ether [0-9a-f:]+' | awk '{print $2;}'`
289 info "Client hwaddr: $HW_CLIENT"
291 # Setup server namespace
292 ip netns exec server ip addr add 172.31.110.20/24 dev s1
293 ip netns exec server ip link set s1 up
294 HW_SERVER=`ip netns exec server ip link show dev s1 | grep -E 'link/ether [0-9a-f:]+' | awk '{print $2;}'`
295 info "Server hwaddr: $HW_SERVER"
297 ovs_add_flow "test_arp_ping" arpping \
298 "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
299 ovs_add_flow "test_arp_ping" arpping \
300 "in_port(2),eth(),eth_type(0x0806),arp()" '1' || return 1
302 ovs_sbx "test_arp_ping" ip netns exec client arping -I c1 172.31.110.20 -c 1 || return 1
308 # - client has 1500 byte MTU
309 # - server has 1500 byte MTU
310 # - use ICMP to ping in each direction
311 # - only allow CT state stuff to pass through new in c -> s
312 test_ct_connect_v4 () {
314 which nc >/dev/null 2>/dev/null || return $ksft_skip
316 sbx_add "test_ct_connect_v4" || return $?
318 ovs_add_dp "test_ct_connect_v4" ct4 || return 1
319 info "create namespaces"
320 for ns in client server; do
321 ovs_add_netns_and_veths "test_ct_connect_v4" "ct4" "$ns" \
322 "${ns:0:1}0" "${ns:0:1}1" || return 1
325 ip netns exec client ip addr add 172.31.110.10/24 dev c1
326 ip netns exec client ip link set c1 up
327 ip netns exec server ip addr add 172.31.110.20/24 dev s1
328 ip netns exec server ip link set s1 up
330 # Add forwarding for ARP and ip packets - completely wildcarded
331 ovs_add_flow "test_ct_connect_v4" ct4 \
332 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
333 ovs_add_flow "test_ct_connect_v4" ct4 \
334 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
335 ovs_add_flow "test_ct_connect_v4" ct4 \
336 'ct_state(-trk),eth(),eth_type(0x0800),ipv4()' \
337 'ct(commit),recirc(0x1)' || return 1
338 ovs_add_flow "test_ct_connect_v4" ct4 \
339 'recirc_id(0x1),ct_state(+trk+new),in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' \
341 ovs_add_flow "test_ct_connect_v4" ct4 \
342 'recirc_id(0x1),ct_state(+trk+est),in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' \
344 ovs_add_flow "test_ct_connect_v4" ct4 \
345 'recirc_id(0x1),ct_state(+trk+est),in_port(2),eth(),eth_type(0x0800),ipv4(dst=172.31.110.10)' \
347 ovs_add_flow "test_ct_connect_v4" ct4 \
348 'recirc_id(0x1),ct_state(+trk+inv),eth(),eth_type(0x0800),ipv4()' 'drop' || \
352 ovs_sbx "test_ct_connect_v4" ip netns exec client ping 172.31.110.20 -c 3 || return 1
354 # create an echo server in 'server'
356 ovs_netns_spawn_daemon "test_ct_connect_v4" "server" \
358 ovs_sbx "test_ct_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.20 4443 || return 1
360 # Now test in the other direction (should fail)
362 ovs_netns_spawn_daemon "test_ct_connect_v4" "client" \
364 ovs_sbx "test_ct_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.10 4443
366 info "ct connect to client was successful"
375 # - client has 1500 byte MTU
376 # - server has 1500 byte MTU
377 # - use ICMP to ping in each direction
380 sbx_add "test_connect_v4" || return $?
382 ovs_add_dp "test_connect_v4" cv4 || return 1
384 info "create namespaces"
385 for ns in client server; do
386 ovs_add_netns_and_veths "test_connect_v4" "cv4" "$ns" \
387 "${ns:0:1}0" "${ns:0:1}1" || return 1
391 ip netns exec client ip addr add 172.31.110.10/24 dev c1
392 ip netns exec client ip link set c1 up
393 ip netns exec server ip addr add 172.31.110.20/24 dev s1
394 ip netns exec server ip link set s1 up
396 # Add forwarding for ARP and ip packets - completely wildcarded
397 ovs_add_flow "test_connect_v4" cv4 \
398 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
399 ovs_add_flow "test_connect_v4" cv4 \
400 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
401 ovs_add_flow "test_connect_v4" cv4 \
402 'in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' '2' || return 1
403 ovs_add_flow "test_connect_v4" cv4 \
404 'in_port(2),eth(),eth_type(0x0800),ipv4(src=172.31.110.20)' '1' || return 1
407 ovs_sbx "test_connect_v4" ip netns exec client ping 172.31.110.20 -c 3 || return 1
413 # nat_connect_v4 test
414 # - client has 1500 byte MTU
415 # - server has 1500 byte MTU
416 # - use ICMP to ping in each direction
417 # - only allow CT state stuff to pass through new in c -> s
418 test_nat_connect_v4 () {
419 which nc >/dev/null 2>/dev/null || return $ksft_skip
421 sbx_add "test_nat_connect_v4" || return $?
423 ovs_add_dp "test_nat_connect_v4" nat4 || return 1
424 info "create namespaces"
425 for ns in client server; do
426 ovs_add_netns_and_veths "test_nat_connect_v4" "nat4" "$ns" \
427 "${ns:0:1}0" "${ns:0:1}1" || return 1
430 ip netns exec client ip addr add 172.31.110.10/24 dev c1
431 ip netns exec client ip link set c1 up
432 ip netns exec server ip addr add 172.31.110.20/24 dev s1
433 ip netns exec server ip link set s1 up
435 ip netns exec client ip route add default via 172.31.110.20
437 ovs_add_flow "test_nat_connect_v4" nat4 \
438 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
439 ovs_add_flow "test_nat_connect_v4" nat4 \
440 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
441 ovs_add_flow "test_nat_connect_v4" nat4 \
442 "ct_state(-trk),in_port(1),eth(),eth_type(0x0800),ipv4(dst=192.168.0.20)" \
443 "ct(commit,nat(dst=172.31.110.20)),recirc(0x1)"
444 ovs_add_flow "test_nat_connect_v4" nat4 \
445 "ct_state(-trk),in_port(2),eth(),eth_type(0x0800),ipv4()" \
446 "ct(commit,nat),recirc(0x2)"
448 ovs_add_flow "test_nat_connect_v4" nat4 \
449 "recirc_id(0x1),ct_state(+trk-inv),in_port(1),eth(),eth_type(0x0800),ipv4()" "2"
450 ovs_add_flow "test_nat_connect_v4" nat4 \
451 "recirc_id(0x2),ct_state(+trk-inv),in_port(2),eth(),eth_type(0x0800),ipv4()" "1"
454 ovs_sbx "test_nat_connect_v4" ip netns exec client ping 192.168.0.20 -c 3 || return 1
456 # create an echo server in 'server'
458 ovs_netns_spawn_daemon "test_nat_connect_v4" "server" \
460 ovs_sbx "test_nat_connect_v4" ip netns exec client nc -i 1 -zv 192.168.0.20 4443 || return 1
462 # Now test in the other direction (should fail)
464 ovs_netns_spawn_daemon "test_nat_connect_v4" "client" \
466 ovs_sbx "test_nat_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.10 4443
468 info "connect to client was successful"
478 # - check no warning with "old version" simulation
479 test_netlink_checks () {
480 sbx_add "test_netlink_checks" || return 1
482 info "setting up new DP"
483 ovs_add_dp "test_netlink_checks" nv0 || return 1
485 PRE_TEST=$(dmesg | grep -E "RIP: [0-9a-fA-Fx]+:ovs_dp_cmd_new\+")
486 ovs_add_dp "test_netlink_checks" nv0 -V 0 || return 1
487 POST_TEST=$(dmesg | grep -E "RIP: [0-9a-fA-Fx]+:ovs_dp_cmd_new\+")
488 if [ "$PRE_TEST" != "$POST_TEST" ]; then
489 info "failed - gen warning"
493 ovs_add_netns_and_veths "test_netlink_checks" nv0 left left0 l0 || \
495 ovs_add_netns_and_veths "test_netlink_checks" nv0 right right0 r0 || \
497 [ $(python3 $ovs_base/ovs-dpctl.py show nv0 | grep port | \
500 ovs_del_if "test_netlink_checks" nv0 right0 || return 1
501 [ $(python3 $ovs_base/ovs-dpctl.py show nv0 | grep port | \
505 ERR_MSG="Flow actions may not be safe on all matching packets"
506 PRE_TEST=$(dmesg | grep -c "${ERR_MSG}")
507 ovs_add_flow "test_netlink_checks" nv0 \
508 'in_port(1),eth(),eth_type(0x0806),arp()' 'drop(0),2' \
509 &> /dev/null && return 1
510 POST_TEST=$(dmesg | grep -c "${ERR_MSG}")
511 if [ "$PRE_TEST" == "$POST_TEST" ]; then
512 info "failed - error not generated"
518 test_upcall_interfaces() {
519 sbx_add "test_upcall_interfaces" || return 1
521 info "setting up new DP"
522 ovs_add_dp "test_upcall_interfaces" ui0 -V 2:1 || return 1
524 ovs_add_netns_and_veths "test_upcall_interfaces" ui0 upc left0 l0 \
525 172.31.110.1/24 -u || return 1
528 info "sending arping"
529 ip netns exec upc arping -I l0 172.31.110.20 -c 1 \
530 >$ovs_dir/arping.stdout 2>$ovs_dir/arping.stderr
532 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
541 if ! lsmod | grep openvswitch >/dev/null 2>&1; then
542 stdbuf -o0 printf "TEST: %-60s [NOMOD]\n" "${tdesc}"
546 if python3 ovs-dpctl.py -h 2>&1 | \
547 grep -E "Need to (install|upgrade) the python" >/dev/null 2>&1; then
548 stdbuf -o0 printf "TEST: %-60s [PYLIB]\n" "${tdesc}"
551 printf "TEST: %-60s [START]\n" "${tname}"
558 if [ $ret -eq 0 ]; then
559 printf "TEST: %-60s [ OK ]\n" "${tdesc}"
562 elif [ $ret -eq 1 ]; then
563 printf "TEST: %-60s [FAIL]\n" "${tdesc}"
564 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
566 echo "Pausing. Logs in $ovs_dir/. Hit enter to continue"
570 [ "${PAUSE_ON_FAIL}" = "yes" ] || rm -rf "$ovs_dir"
572 elif [ $ret -eq $ksft_skip ]; then
573 printf "TEST: %-60s [SKIP]\n" "${tdesc}"
574 elif [ $ret -eq 2 ]; then
584 [ $all_skipped = true ] && [ $exitcode=$ksft_skip ] && exitcode=0
588 [ $all_skipped = true ] && exitcode=$ksft_skip
607 p) PAUSE_ON_FAIL=yes;;
609 t) if which tcpdump > /dev/null 2>&1; then
612 echo "=== tcpdump not available, tracing disabled"
624 # Check first that all requested tests are available before running any
625 command -v > /dev/null "test_${arg}" || { echo "=== Test ${arg} not found"; usage; }
630 for t in ${tests}; do
631 [ "${name}" = "" ] && name="${t}" && continue
632 [ "${desc}" = "" ] && desc="${t}"
636 [ "${arg}" != "${arg#--*}" ] && continue
637 [ "${arg}" = "${name}" ] && run_this=1 && break
640 if [ $run_this -eq 1 ]; then
641 run_test "${name}" "${desc}"