|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | | -set -euo pipefail |
4 | | - |
| 3 | +# Get the directory of this script |
5 | 4 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
6 | 5 | . "${SCRIPT_DIR}/common.sh" |
7 | 6 |
|
8 | 7 | check_platform |
9 | 8 |
|
| 9 | +RET=0 |
| 10 | + |
| 11 | +backend="${VNET_BACKEND:-tap}" |
| 12 | + |
| 13 | +if [[ "${backend}" != "tap" ]]; then |
| 14 | + print_error "Unsupported virtio-net backend for this test: ${backend}" |
| 15 | + exit 2 |
| 16 | +fi |
| 17 | + |
10 | 18 | if [[ "${OS_TYPE}" != "Linux" ]]; then |
11 | | - print_warning "Skipping virtio-net test on non-Linux host" |
| 19 | + print_warning "Skipping virtio-net TAP test on non-Linux host" |
12 | 20 | exit 0 |
13 | 21 | fi |
14 | 22 |
|
15 | 23 | register_cleanup cleanup_emulator |
16 | 24 |
|
17 | 25 | TIMEOUT=${BOOT_TIMEOUT:-60} |
18 | 26 |
|
| 27 | +COLOR_G='\e[32;01m' # Green |
| 28 | +COLOR_R='\e[31;01m' # Red |
| 29 | +COLOR_Y='\e[33;01m' # Yellow |
| 30 | +COLOR_N='\e[0m' # No color |
| 31 | + |
| 32 | +MESSAGES=("${COLOR_G}OK!" |
| 33 | + "${COLOR_R}Fail to boot" |
| 34 | + "${COLOR_R}Fail to login" |
| 35 | + "${COLOR_R}Fail to run commands" |
| 36 | + "${COLOR_R}Fail to ping host TAP" |
| 37 | +) |
| 38 | + |
| 39 | +OPTS_BASE=" -k build/linux-image/Image -i build/linux-image/rootfs.cpio" |
| 40 | +RUN_LINUX="sudo -E build/rv32emu ${OPTS_BASE} -x vnet:tap" |
| 41 | + |
| 42 | +printf "${COLOR_Y}===== Test option: ${OPTS_BASE} -x vnet:tap =====${COLOR_N}\n" |
| 43 | + |
19 | 44 | ASSERT expect <<- DONE |
20 | | - set timeout ${TIMEOUT} |
21 | | - set tap_if "" |
22 | | -
|
23 | | - spawn sudo -E build/rv32emu \ |
24 | | - -k build/linux-image/Image \ |
25 | | - -i build/linux-image/rootfs.cpio \ |
26 | | - -x vnet:tap |
27 | | -
|
28 | | - expect { |
29 | | - -re {allocated TAP interface: (tap[0-9]+)} { |
30 | | - set tap_if \$expect_out(1,string) |
31 | | - exp_continue |
32 | | - } |
33 | | - "buildroot login:" { |
34 | | - if { "\$tap_if" == "" } { |
35 | | - set tap_if [exec sh -c {ip -o link show | awk -F': ' '\$2 ~ /^tap[0-9]+/ {print \$2; exit}'}] |
36 | | - } |
37 | | -
|
38 | | - exec sudo ip addr replace 192.168.100.1/24 dev \$tap_if |
39 | | - exec sudo ip link set \$tap_if up |
40 | | -
|
41 | | - send "root\\r" |
42 | | - } |
43 | | - timeout { |
44 | | - exit 1 |
45 | | - } |
46 | | - } |
47 | | -
|
48 | | - expect "# " |
49 | | - send "readlink /sys/bus/virtio/devices/virtio0/driver\\r" |
50 | | - expect { |
51 | | - "virtio_net" {} |
52 | | - timeout { exit 2 } |
53 | | - } |
54 | | -
|
55 | | - expect "# " |
56 | | - send "ip link set eth0 up\\r" |
57 | | -
|
58 | | - expect "# " |
59 | | - send "ip addr add 192.168.100.2/24 dev eth0\\r" |
60 | | -
|
61 | | - expect "# " |
62 | | - send "ip addr show eth0\\r" |
63 | | - expect { |
64 | | - "192.168.100.2/24" {} |
65 | | - timeout { exit 3 } |
66 | | - } |
67 | | -
|
68 | | - expect "# " |
69 | | - send "ping -c 3 -W 5 192.168.100.1\r" |
70 | | - expect { |
71 | | - -re {3 packets transmitted, 3 packets received|3 packets transmitted, 3 received} { |
72 | | - send "\x01" |
73 | | - send "x" |
74 | | - exit 0 |
75 | | - } |
76 | | - timeout { |
77 | | - exit 4 |
78 | | - } |
| 45 | + set timeout ${TIMEOUT} |
| 46 | + set tap_if "" |
| 47 | +
|
| 48 | + spawn ${RUN_LINUX} |
| 49 | +
|
| 50 | + expect { |
| 51 | + -re {allocated TAP interface: (tap[0-9]+)} { |
| 52 | + set tap_if \$expect_out(1,string) |
| 53 | + exp_continue |
| 54 | + } |
| 55 | + "buildroot login:" { |
| 56 | + if { "\$tap_if" == "" } { |
| 57 | + set tap_if [exec sh -c {ip -o link show | awk -F': ' '\$2 ~ /^tap[0-9]+/ {print \$2; exit}'}] |
| 58 | + } |
| 59 | +
|
| 60 | + exec sudo ip addr replace 192.168.100.1/24 dev \$tap_if |
| 61 | + exec sudo ip link set \$tap_if up |
| 62 | +
|
| 63 | + send "root\\r" |
| 64 | + } |
| 65 | + timeout { |
| 66 | + exit 1 |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + expect "# " |
| 71 | + send "readlink /sys/bus/virtio/devices/virtio0/driver\\r" |
| 72 | + expect { |
| 73 | + "virtio_net" {} |
| 74 | + timeout { exit 2 } |
| 75 | + } |
| 76 | +
|
| 77 | + expect "# " |
| 78 | + send "ip link set eth0 up\\r" |
| 79 | +
|
| 80 | + expect "# " |
| 81 | + send "ip addr add 192.168.100.2/24 dev eth0\\r" |
| 82 | +
|
| 83 | + expect "# " |
| 84 | + send "ip addr show eth0\\r" |
| 85 | + expect { |
| 86 | + "192.168.100.2/24" {} |
| 87 | + timeout { exit 3 } |
| 88 | + } |
| 89 | +
|
| 90 | + expect "# " |
| 91 | + send "ping -c 3 -W 5 192.168.100.1\\r" |
| 92 | + expect { |
| 93 | + -re {3 packets transmitted, 3 packets received|3 packets transmitted, 3 received} { |
| 94 | + send "\\x01" |
| 95 | + send "x" |
| 96 | + exit 0 |
| 97 | + } |
| 98 | + -re {3 packets transmitted, 0 packets received|3 packets transmitted, 0 received|100% packet loss} { |
| 99 | + exit 4 |
| 100 | + } |
| 101 | + timeout { |
| 102 | + exit 4 |
| 103 | + } |
| 104 | + } |
79 | 105 | DONE |
80 | 106 |
|
81 | | -print_success "virtio-net boot test passed" |
| 107 | +ret=$? |
| 108 | +RET=$((${RET} + ${ret})) |
| 109 | +cleanup |
| 110 | + |
| 111 | +printf "\nVirtio-net TAP Test: [ ${MESSAGES[$ret]}${COLOR_N} ]\n" |
| 112 | + |
| 113 | +exit ${RET} |
0 commit comments