1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0 3# 4# OVS kernel module self tests 5 6# Kselftest framework requirement - SKIP code is 4. 7ksft_skip=4 8 9PAUSE_ON_FAIL=no 10VERBOSE=0 11TRACING=0 12 13tests=" 14 arp_ping eth-arp: Basic arp ping between two NS 15 ct_connect_v4 ip4-ct-xon: Basic ipv4 tcp connection using ct 16 connect_v4 ip4-xon: Basic ipv4 ping between two NS 17 nat_connect_v4 ip4-nat-xon: Basic ipv4 tcp connection via NAT 18 netlink_checks ovsnl: validate netlink attrs and settings 19 upcall_interfaces ovs: test the upcall interfaces" 20 21info() { 22 [ $VERBOSE = 0 ] || echo $* 23} 24 25ovs_base=`pwd` 26sbxs= 27sbx_add () { 28 info "adding sandbox '$1'" 29 30 sbxs="$sbxs $1" 31 32 NO_BIN=0 33 34 # Create sandbox. 35 local d="$ovs_base"/$1 36 if [ -e $d ]; then 37 info "removing $d" 38 rm -rf "$d" 39 fi 40 mkdir "$d" || return 1 41 ovs_setenv $1 42} 43 44ovs_exit_sig() { 45 [ -e ${ovs_dir}/cleanup ] && . "$ovs_dir/cleanup" 46} 47 48on_exit() { 49 echo "$1" > ${ovs_dir}/cleanup.tmp 50 cat ${ovs_dir}/cleanup >> ${ovs_dir}/cleanup.tmp 51 mv ${ovs_dir}/cleanup.tmp ${ovs_dir}/cleanup 52} 53 54ovs_setenv() { 55 sandbox=$1 56 57 ovs_dir=$ovs_base${1:+/$1}; export ovs_dir 58 59 test -e ${ovs_dir}/cleanup || : > ${ovs_dir}/cleanup 60} 61 62ovs_sbx() { 63 if test "X$2" != X; then 64 (ovs_setenv $1; shift; "$@" >> ${ovs_dir}/debug.log) 65 else 66 ovs_setenv $1 67 fi 68} 69 70ovs_add_dp () { 71 info "Adding DP/Bridge IF: sbx:$1 dp:$2 {$3, $4, $5}" 72 sbxname="$1" 73 shift 74 ovs_sbx "$sbxname" python3 $ovs_base/ovs-dpctl.py add-dp $* 75 on_exit "ovs_sbx $sbxname python3 $ovs_base/ovs-dpctl.py del-dp $1;" 76} 77 78ovs_add_if () { 79 info "Adding IF to DP: br:$2 if:$3" 80 if [ "$4" != "-u" ]; then 81 ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py add-if "$2" "$3" \ 82 || return 1 83 else 84 python3 $ovs_base/ovs-dpctl.py add-if \ 85 -u "$2" "$3" >$ovs_dir/$3.out 2>$ovs_dir/$3.err & 86 pid=$! 87 on_exit "ovs_sbx $1 kill -TERM $pid 2>/dev/null" 88 fi 89} 90 91ovs_del_if () { 92 info "Deleting IF from DP: br:$2 if:$3" 93 ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py del-if "$2" "$3" || return 1 94} 95 96ovs_netns_spawn_daemon() { 97 sbx=$1 98 shift 99 netns=$1 100 shift 101 info "spawning cmd: $*" 102 ip netns exec $netns $* >> $ovs_dir/stdout 2>> $ovs_dir/stderr & 103 pid=$! 104 ovs_sbx "$sbx" on_exit "kill -TERM $pid 2>/dev/null" 105} 106 107ovs_add_netns_and_veths () { 108 info "Adding netns attached: sbx:$1 dp:$2 {$3, $4, $5}" 109 ovs_sbx "$1" ip netns add "$3" || return 1 110 on_exit "ovs_sbx $1 ip netns del $3" 111 ovs_sbx "$1" ip link add "$4" type veth peer name "$5" || return 1 112 on_exit "ovs_sbx $1 ip link del $4 >/dev/null 2>&1" 113 ovs_sbx "$1" ip link set "$4" up || return 1 114 ovs_sbx "$1" ip link set "$5" netns "$3" || return 1 115 ovs_sbx "$1" ip netns exec "$3" ip link set "$5" up || return 1 116 117 if [ "$6" != "" ]; then 118 ovs_sbx "$1" ip netns exec "$3" ip addr add "$6" dev "$5" \ 119 || return 1 120 fi 121 122 if [ "$7" != "-u" ]; then 123 ovs_add_if "$1" "$2" "$4" || return 1 124 else 125 ovs_add_if "$1" "$2" "$4" -u || return 1 126 fi 127 128 [ $TRACING -eq 1 ] && ovs_netns_spawn_daemon "$1" "$ns" \ 129 tcpdump -i any -s 65535 130 131 return 0 132} 133 134ovs_add_flow () { 135 info "Adding flow to DP: sbx:$1 br:$2 flow:$3 act:$4" 136 ovs_sbx "$1" python3 $ovs_base/ovs-dpctl.py add-flow "$2" "$3" "$4" 137 if [ $? -ne 0 ]; then 138 echo "Flow [ $3 : $4 ] failed" >> ${ovs_dir}/debug.log 139 return 1 140 fi 141 return 0 142} 143 144usage() { 145 echo 146 echo "$0 [OPTIONS] [TEST]..." 147 echo "If no TEST argument is given, all tests will be run." 148 echo 149 echo "Options" 150 echo " -t: capture traffic via tcpdump" 151 echo " -v: verbose" 152 echo " -p: pause on failure" 153 echo 154 echo "Available tests${tests}" 155 exit 1 156} 157 158# arp_ping test 159# - client has 1500 byte MTU 160# - server has 1500 byte MTU 161# - send ARP ping between two ns 162test_arp_ping () { 163 164 which arping >/dev/null 2>&1 || return $ksft_skip 165 166 sbx_add "test_arp_ping" || return $? 167 168 ovs_add_dp "test_arp_ping" arpping || return 1 169 170 info "create namespaces" 171 for ns in client server; do 172 ovs_add_netns_and_veths "test_arp_ping" "arpping" "$ns" \ 173 "${ns:0:1}0" "${ns:0:1}1" || return 1 174 done 175 176 # Setup client namespace 177 ip netns exec client ip addr add 172.31.110.10/24 dev c1 178 ip netns exec client ip link set c1 up 179 HW_CLIENT=`ip netns exec client ip link show dev c1 | grep -E 'link/ether [0-9a-f:]+' | awk '{print $2;}'` 180 info "Client hwaddr: $HW_CLIENT" 181 182 # Setup server namespace 183 ip netns exec server ip addr add 172.31.110.20/24 dev s1 184 ip netns exec server ip link set s1 up 185 HW_SERVER=`ip netns exec server ip link show dev s1 | grep -E 'link/ether [0-9a-f:]+' | awk '{print $2;}'` 186 info "Server hwaddr: $HW_SERVER" 187 188 ovs_add_flow "test_arp_ping" arpping \ 189 "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 190 ovs_add_flow "test_arp_ping" arpping \ 191 "in_port(2),eth(),eth_type(0x0806),arp()" '1' || return 1 192 193 ovs_sbx "test_arp_ping" ip netns exec client arping -I c1 172.31.110.20 -c 1 || return 1 194 195 return 0 196} 197 198# ct_connect_v4 test 199# - client has 1500 byte MTU 200# - server has 1500 byte MTU 201# - use ICMP to ping in each direction 202# - only allow CT state stuff to pass through new in c -> s 203test_ct_connect_v4 () { 204 205 which nc >/dev/null 2>/dev/null || return $ksft_skip 206 207 sbx_add "test_ct_connect_v4" || return $? 208 209 ovs_add_dp "test_ct_connect_v4" ct4 || return 1 210 info "create namespaces" 211 for ns in client server; do 212 ovs_add_netns_and_veths "test_ct_connect_v4" "ct4" "$ns" \ 213 "${ns:0:1}0" "${ns:0:1}1" || return 1 214 done 215 216 ip netns exec client ip addr add 172.31.110.10/24 dev c1 217 ip netns exec client ip link set c1 up 218 ip netns exec server ip addr add 172.31.110.20/24 dev s1 219 ip netns exec server ip link set s1 up 220 221 # Add forwarding for ARP and ip packets - completely wildcarded 222 ovs_add_flow "test_ct_connect_v4" ct4 \ 223 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1 224 ovs_add_flow "test_ct_connect_v4" ct4 \ 225 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1 226 ovs_add_flow "test_ct_connect_v4" ct4 \ 227 'ct_state(-trk),eth(),eth_type(0x0800),ipv4()' \ 228 'ct(commit),recirc(0x1)' || return 1 229 ovs_add_flow "test_ct_connect_v4" ct4 \ 230 'recirc_id(0x1),ct_state(+trk+new),in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' \ 231 '2' || return 1 232 ovs_add_flow "test_ct_connect_v4" ct4 \ 233 'recirc_id(0x1),ct_state(+trk+est),in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' \ 234 '2' || return 1 235 ovs_add_flow "test_ct_connect_v4" ct4 \ 236 'recirc_id(0x1),ct_state(+trk+est),in_port(2),eth(),eth_type(0x0800),ipv4(dst=172.31.110.10)' \ 237 '1' || return 1 238 ovs_add_flow "test_ct_connect_v4" ct4 \ 239 'recirc_id(0x1),ct_state(+trk+inv),eth(),eth_type(0x0800),ipv4()' 'drop' || \ 240 return 1 241 242 # do a ping 243 ovs_sbx "test_ct_connect_v4" ip netns exec client ping 172.31.110.20 -c 3 || return 1 244 245 # create an echo server in 'server' 246 echo "server" | \ 247 ovs_netns_spawn_daemon "test_ct_connect_v4" "server" \ 248 nc -lvnp 4443 249 ovs_sbx "test_ct_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.20 4443 || return 1 250 251 # Now test in the other direction (should fail) 252 echo "client" | \ 253 ovs_netns_spawn_daemon "test_ct_connect_v4" "client" \ 254 nc -lvnp 4443 255 ovs_sbx "test_ct_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.10 4443 256 if [ $? == 0 ]; then 257 info "ct connect to client was successful" 258 return 1 259 fi 260 261 info "done..." 262 return 0 263} 264 265# connect_v4 test 266# - client has 1500 byte MTU 267# - server has 1500 byte MTU 268# - use ICMP to ping in each direction 269test_connect_v4 () { 270 271 sbx_add "test_connect_v4" || return $? 272 273 ovs_add_dp "test_connect_v4" cv4 || return 1 274 275 info "create namespaces" 276 for ns in client server; do 277 ovs_add_netns_and_veths "test_connect_v4" "cv4" "$ns" \ 278 "${ns:0:1}0" "${ns:0:1}1" || return 1 279 done 280 281 282 ip netns exec client ip addr add 172.31.110.10/24 dev c1 283 ip netns exec client ip link set c1 up 284 ip netns exec server ip addr add 172.31.110.20/24 dev s1 285 ip netns exec server ip link set s1 up 286 287 # Add forwarding for ARP and ip packets - completely wildcarded 288 ovs_add_flow "test_connect_v4" cv4 \ 289 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1 290 ovs_add_flow "test_connect_v4" cv4 \ 291 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1 292 ovs_add_flow "test_connect_v4" cv4 \ 293 'in_port(1),eth(),eth_type(0x0800),ipv4(src=172.31.110.10)' '2' || return 1 294 ovs_add_flow "test_connect_v4" cv4 \ 295 'in_port(2),eth(),eth_type(0x0800),ipv4(src=172.31.110.20)' '1' || return 1 296 297 # do a ping 298 ovs_sbx "test_connect_v4" ip netns exec client ping 172.31.110.20 -c 3 || return 1 299 300 info "done..." 301 return 0 302} 303 304# nat_connect_v4 test 305# - client has 1500 byte MTU 306# - server has 1500 byte MTU 307# - use ICMP to ping in each direction 308# - only allow CT state stuff to pass through new in c -> s 309test_nat_connect_v4 () { 310 which nc >/dev/null 2>/dev/null || return $ksft_skip 311 312 sbx_add "test_nat_connect_v4" || return $? 313 314 ovs_add_dp "test_nat_connect_v4" nat4 || return 1 315 info "create namespaces" 316 for ns in client server; do 317 ovs_add_netns_and_veths "test_nat_connect_v4" "nat4" "$ns" \ 318 "${ns:0:1}0" "${ns:0:1}1" || return 1 319 done 320 321 ip netns exec client ip addr add 172.31.110.10/24 dev c1 322 ip netns exec client ip link set c1 up 323 ip netns exec server ip addr add 172.31.110.20/24 dev s1 324 ip netns exec server ip link set s1 up 325 326 ip netns exec client ip route add default via 172.31.110.20 327 328 ovs_add_flow "test_nat_connect_v4" nat4 \ 329 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1 330 ovs_add_flow "test_nat_connect_v4" nat4 \ 331 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1 332 ovs_add_flow "test_nat_connect_v4" nat4 \ 333 "ct_state(-trk),in_port(1),eth(),eth_type(0x0800),ipv4(dst=192.168.0.20)" \ 334 "ct(commit,nat(dst=172.31.110.20)),recirc(0x1)" 335 ovs_add_flow "test_nat_connect_v4" nat4 \ 336 "ct_state(-trk),in_port(2),eth(),eth_type(0x0800),ipv4()" \ 337 "ct(commit,nat),recirc(0x2)" 338 339 ovs_add_flow "test_nat_connect_v4" nat4 \ 340 "recirc_id(0x1),ct_state(+trk-inv),in_port(1),eth(),eth_type(0x0800),ipv4()" "2" 341 ovs_add_flow "test_nat_connect_v4" nat4 \ 342 "recirc_id(0x2),ct_state(+trk-inv),in_port(2),eth(),eth_type(0x0800),ipv4()" "1" 343 344 # do a ping 345 ovs_sbx "test_nat_connect_v4" ip netns exec client ping 192.168.0.20 -c 3 || return 1 346 347 # create an echo server in 'server' 348 echo "server" | \ 349 ovs_netns_spawn_daemon "test_nat_connect_v4" "server" \ 350 nc -lvnp 4443 351 ovs_sbx "test_nat_connect_v4" ip netns exec client nc -i 1 -zv 192.168.0.20 4443 || return 1 352 353 # Now test in the other direction (should fail) 354 echo "client" | \ 355 ovs_netns_spawn_daemon "test_nat_connect_v4" "client" \ 356 nc -lvnp 4443 357 ovs_sbx "test_nat_connect_v4" ip netns exec client nc -i 1 -zv 172.31.110.10 4443 358 if [ $? == 0 ]; then 359 info "connect to client was successful" 360 return 1 361 fi 362 363 info "done..." 364 return 0 365} 366 367# netlink_validation 368# - Create a dp 369# - check no warning with "old version" simulation 370test_netlink_checks () { 371 sbx_add "test_netlink_checks" || return 1 372 373 info "setting up new DP" 374 ovs_add_dp "test_netlink_checks" nv0 || return 1 375 # now try again 376 PRE_TEST=$(dmesg | grep -E "RIP: [0-9a-fA-Fx]+:ovs_dp_cmd_new\+") 377 ovs_add_dp "test_netlink_checks" nv0 -V 0 || return 1 378 POST_TEST=$(dmesg | grep -E "RIP: [0-9a-fA-Fx]+:ovs_dp_cmd_new\+") 379 if [ "$PRE_TEST" != "$POST_TEST" ]; then 380 info "failed - gen warning" 381 return 1 382 fi 383 384 ovs_add_netns_and_veths "test_netlink_checks" nv0 left left0 l0 || \ 385 return 1 386 ovs_add_netns_and_veths "test_netlink_checks" nv0 right right0 r0 || \ 387 return 1 388 [ $(python3 $ovs_base/ovs-dpctl.py show nv0 | grep port | \ 389 wc -l) == 3 ] || \ 390 return 1 391 ovs_del_if "test_netlink_checks" nv0 right0 || return 1 392 [ $(python3 $ovs_base/ovs-dpctl.py show nv0 | grep port | \ 393 wc -l) == 2 ] || \ 394 return 1 395 396 return 0 397} 398 399test_upcall_interfaces() { 400 sbx_add "test_upcall_interfaces" || return 1 401 402 info "setting up new DP" 403 ovs_add_dp "test_upcall_interfaces" ui0 -V 2:1 || return 1 404 405 ovs_add_netns_and_veths "test_upcall_interfaces" ui0 upc left0 l0 \ 406 172.31.110.1/24 -u || return 1 407 408 sleep 1 409 info "sending arping" 410 ip netns exec upc arping -I l0 172.31.110.20 -c 1 \ 411 >$ovs_dir/arping.stdout 2>$ovs_dir/arping.stderr 412 413 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 414 return 0 415} 416 417run_test() { 418 ( 419 tname="$1" 420 tdesc="$2" 421 422 if ! lsmod | grep openvswitch >/dev/null 2>&1; then 423 stdbuf -o0 printf "TEST: %-60s [NOMOD]\n" "${tdesc}" 424 return $ksft_skip 425 fi 426 427 if python3 ovs-dpctl.py -h 2>&1 | \ 428 grep "Need to install the python" >/dev/null 2>&1; then 429 stdbuf -o0 printf "TEST: %-60s [PYLIB]\n" "${tdesc}" 430 return $ksft_skip 431 fi 432 printf "TEST: %-60s [START]\n" "${tname}" 433 434 unset IFS 435 436 eval test_${tname} 437 ret=$? 438 439 if [ $ret -eq 0 ]; then 440 printf "TEST: %-60s [ OK ]\n" "${tdesc}" 441 ovs_exit_sig 442 rm -rf "$ovs_dir" 443 elif [ $ret -eq 1 ]; then 444 printf "TEST: %-60s [FAIL]\n" "${tdesc}" 445 if [ "${PAUSE_ON_FAIL}" = "yes" ]; then 446 echo 447 echo "Pausing. Logs in $ovs_dir/. Hit enter to continue" 448 read a 449 fi 450 ovs_exit_sig 451 [ "${PAUSE_ON_FAIL}" = "yes" ] || rm -rf "$ovs_dir" 452 exit 1 453 elif [ $ret -eq $ksft_skip ]; then 454 printf "TEST: %-60s [SKIP]\n" "${tdesc}" 455 elif [ $ret -eq 2 ]; then 456 rm -rf test_${tname} 457 run_test "$1" "$2" 458 fi 459 460 return $ret 461 ) 462 ret=$? 463 case $ret in 464 0) 465 [ $all_skipped = true ] && [ $exitcode=$ksft_skip ] && exitcode=0 466 all_skipped=false 467 ;; 468 $ksft_skip) 469 [ $all_skipped = true ] && exitcode=$ksft_skip 470 ;; 471 *) 472 all_skipped=false 473 exitcode=1 474 ;; 475 esac 476 477 return $ret 478} 479 480 481exitcode=0 482desc=0 483all_skipped=true 484 485while getopts :pvt o 486do 487 case $o in 488 p) PAUSE_ON_FAIL=yes;; 489 v) VERBOSE=1;; 490 t) if which tcpdump > /dev/null 2>&1; then 491 TRACING=1 492 else 493 echo "=== tcpdump not available, tracing disabled" 494 fi 495 ;; 496 *) usage;; 497 esac 498done 499shift $(($OPTIND-1)) 500 501IFS=" 502" 503 504for arg do 505 # Check first that all requested tests are available before running any 506 command -v > /dev/null "test_${arg}" || { echo "=== Test ${arg} not found"; usage; } 507done 508 509name="" 510desc="" 511for t in ${tests}; do 512 [ "${name}" = "" ] && name="${t}" && continue 513 [ "${desc}" = "" ] && desc="${t}" 514 515 run_this=1 516 for arg do 517 [ "${arg}" != "${arg#--*}" ] && continue 518 [ "${arg}" = "${name}" ] && run_this=1 && break 519 run_this=0 520 done 521 if [ $run_this -eq 1 ]; then 522 run_test "${name}" "${desc}" 523 fi 524 name="" 525 desc="" 526done 527 528exit ${exitcode} 529