xref: /aosp_15_r20/kernel/tests/net/test/run_net_test.sh (revision 2f2c4c7ab4226c71756b9c31670392fdd6887c4f)
1*2f2c4c7aSAndroid Build Coastguard Worker#!/bin/bash
2*2f2c4c7aSAndroid Build Coastguard Worker
3*2f2c4c7aSAndroid Build Coastguard Worker# Builds mysteriously fail if stdout is non-blocking.
4*2f2c4c7aSAndroid Build Coastguard Workerfixup_ptys() {
5*2f2c4c7aSAndroid Build Coastguard Worker  python3 << 'EOF'
6*2f2c4c7aSAndroid Build Coastguard Workerimport fcntl, os, sys
7*2f2c4c7aSAndroid Build Coastguard Workerfd = sys.stdout.fileno()
8*2f2c4c7aSAndroid Build Coastguard Workerflags = fcntl.fcntl(fd, fcntl.F_GETFL)
9*2f2c4c7aSAndroid Build Coastguard Workerflags &= ~(fcntl.FASYNC | os.O_NONBLOCK | os.O_APPEND)
10*2f2c4c7aSAndroid Build Coastguard Workerfcntl.fcntl(fd, fcntl.F_SETFL, flags)
11*2f2c4c7aSAndroid Build Coastguard WorkerEOF
12*2f2c4c7aSAndroid Build Coastguard Worker}
13*2f2c4c7aSAndroid Build Coastguard Worker
14*2f2c4c7aSAndroid Build Coastguard Worker# Common kernel options
15*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS=" ANDROID GKI_NET_XFRM_HACKS"
16*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS DEBUG_SPINLOCK DEBUG_ATOMIC_SLEEP DEBUG_MUTEXES DEBUG_RT_MUTEXES"
17*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS WARN_ALL_UNSEEDED_RANDOM IKCONFIG IKCONFIG_PROC"
18*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS DEVTMPFS DEVTMPFS_MOUNT FHANDLE"
19*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IPV6 IPV6_ROUTER_PREF IPV6_MULTIPLE_TABLES IPV6_ROUTE_INFO"
20*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS TUN SYN_COOKIES IP_ADVANCED_ROUTER IP_MULTIPLE_TABLES"
21*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER NETFILTER_ADVANCED NETFILTER_XTABLES"
22*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_MARK NETFILTER_XT_TARGET_MARK"
23*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IP_NF_IPTABLES IP_NF_MANGLE IP_NF_FILTER"
24*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IP6_NF_IPTABLES IP6_NF_MANGLE IP6_NF_FILTER INET6_IPCOMP"
25*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IPV6_OPTIMISTIC_DAD"
26*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IPV6_ROUTE_INFO IPV6_ROUTER_PREF"
27*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_TARGET_IDLETIMER"
28*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_TARGET_NFLOG"
29*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_MATCH_POLICY"
30*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_MATCH_QUOTA"
31*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_MATCH_QUOTA2"
32*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_MATCH_QUOTA2_LOG"
33*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_MATCH_SOCKET"
34*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_XT_MATCH_QTAGUID"
35*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS INET_DIAG INET_UDP_DIAG INET_DIAG_DESTROY"
36*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IP_SCTP"
37*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IP_NF_TARGET_REJECT IP_NF_TARGET_REJECT_SKERR"
38*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IP6_NF_TARGET_REJECT IP6_NF_TARGET_REJECT_SKERR"
39*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NET_KEY XFRM_USER XFRM_STATISTICS CRYPTO_CBC"
40*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS CRYPTO_CTR CRYPTO_HMAC CRYPTO_AES CRYPTO_SHA1"
41*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS CRYPTO_XCBC CRYPTO_CHACHA20POLY1305"
42*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS CRYPTO_USER INET_ESP INET_XFRM_MODE_TRANSPORT"
43*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS INET_XFRM_MODE_TUNNEL INET6_ESP"
44*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS INET6_XFRM_MODE_TRANSPORT INET6_XFRM_MODE_TUNNEL"
45*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS CRYPTO_SHA256 CRYPTO_SHA512 CRYPTO_AES_X86_64 CRYPTO_NULL"
46*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS CRYPTO_GCM CRYPTO_ECHAINIV NET_IPVTI"
47*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS DUMMY"
48*2f2c4c7aSAndroid Build Coastguard Worker
49*2f2c4c7aSAndroid Build Coastguard Worker# Kernel version specific options
50*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS XFRM_INTERFACE"                # Various device kernels
51*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS XFRM_MIGRATE"                  # Added in 5.10
52*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS CGROUP_BPF"                    # Added in android-4.9
53*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NF_SOCKET_IPV4 NF_SOCKET_IPV6" # Added in 4.9
54*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS INET_SCTP_DIAG"                # Added in 4.7
55*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS SOCK_CGROUP_DATA"              # Added in 4.5
56*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS CRYPTO_ECHAINIV"               # Added in 4.1
57*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS BPF_SYSCALL"                   # Added in 3.18
58*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IPV6_VTI"                      # Added in 3.13
59*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IPV6_PRIVACY"                  # Removed in 3.12
60*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS NETFILTER_TPROXY"              # Removed in 3.11
61*2f2c4c7aSAndroid Build Coastguard Worker
62*2f2c4c7aSAndroid Build Coastguard Worker# UML specific options
63*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS BLK_DEV_UBD HOSTFS"
64*2f2c4c7aSAndroid Build Coastguard Worker
65*2f2c4c7aSAndroid Build Coastguard Worker# QEMU specific options
66*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS PCI VIRTIO VIRTIO_PCI VIRTIO_BLK NET_9P NET_9P_VIRTIO 9P_FS"
67*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS CRYPTO_DEV_VIRTIO SERIAL_8250 SERIAL_8250_PCI"
68*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS SERIAL_8250_CONSOLE PCI_HOST_GENERIC SERIAL_AMBA_PL011"
69*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS SERIAL_AMBA_PL011_CONSOLE"
70*2f2c4c7aSAndroid Build Coastguard Worker
71*2f2c4c7aSAndroid Build Coastguard Worker# Obsolete options present at some time in Android kernels
72*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS IP_NF_TARGET_REJECT_SKERR IP6_NF_TARGET_REJECT_SKERR"
73*2f2c4c7aSAndroid Build Coastguard Worker
74*2f2c4c7aSAndroid Build Coastguard Worker# b/262323440 - UML *sometimes* seems to have issues with:
75*2f2c4c7aSAndroid Build Coastguard Worker#   UPSTREAM: hardening: Clarify Kconfig text for auto-var-init
76*2f2c4c7aSAndroid Build Coastguard Worker# which is in 4.14.~299/4.19.~266 LTS and which does:
77*2f2c4c7aSAndroid Build Coastguard Worker#   prompt "Initialize kernel stack variables at function entry"
78*2f2c4c7aSAndroid Build Coastguard Worker#   default GCC_PLUGIN_STRUCTLEAK_BYREF_ALL if COMPILE_TEST && GCC_PLUGINS
79*2f2c4c7aSAndroid Build Coastguard Worker#   default INIT_STACK_ALL_PATTERN if COMPILE_TEST && CC_HAS_AUTO_VAR_INIT_PATTERN
80*2f2c4c7aSAndroid Build Coastguard Worker# + default INIT_STACK_ALL_ZERO if CC_HAS_AUTO_VAR_INIT_PATTERN
81*2f2c4c7aSAndroid Build Coastguard Worker#   default INIT_STACK_NONE
82*2f2c4c7aSAndroid Build Coastguard Worker# and thus presumably switches from INIT_STACK_NONE to INIT_STACK_ALL_ZERO
83*2f2c4c7aSAndroid Build Coastguard Worker#
84*2f2c4c7aSAndroid Build Coastguard Worker# My guess it that this is triggering some sort of UML and/or compiler bug...
85*2f2c4c7aSAndroid Build Coastguard Worker# Let's just turn it off... we don't care that much.
86*2f2c4c7aSAndroid Build Coastguard WorkerOPTIONS="$OPTIONS INIT_STACK_NONE"
87*2f2c4c7aSAndroid Build Coastguard Worker
88*2f2c4c7aSAndroid Build Coastguard Worker# These two break the flo kernel due to differences in -Werror on recent GCC.
89*2f2c4c7aSAndroid Build Coastguard WorkerDISABLE_OPTIONS=" REISERFS_FS ANDROID_PMEM"
90*2f2c4c7aSAndroid Build Coastguard Worker
91*2f2c4c7aSAndroid Build Coastguard Worker# How many TAP interfaces to create to provide the VM with real network access
92*2f2c4c7aSAndroid Build Coastguard Worker# via the host. This requires privileges (e.g., root access) on the host.
93*2f2c4c7aSAndroid Build Coastguard Worker#
94*2f2c4c7aSAndroid Build Coastguard Worker# This is not needed to run the tests, but can be used, for example, to allow
95*2f2c4c7aSAndroid Build Coastguard Worker# the VM to update system packages, or to write tests that need access to a
96*2f2c4c7aSAndroid Build Coastguard Worker# real network. The VM does not set up networking by default, but it contains a
97*2f2c4c7aSAndroid Build Coastguard Worker# DHCP client and has the ability to use IPv6 autoconfiguration. This script
98*2f2c4c7aSAndroid Build Coastguard Worker# does not perform any host-level setup beyond configuring tap interfaces;
99*2f2c4c7aSAndroid Build Coastguard Worker# configuring IPv4 NAT and/or IPv6 router advertisements or ND proxying must
100*2f2c4c7aSAndroid Build Coastguard Worker# be done separately.
101*2f2c4c7aSAndroid Build Coastguard WorkerNUMTAPINTERFACES=0
102*2f2c4c7aSAndroid Build Coastguard Worker
103*2f2c4c7aSAndroid Build Coastguard Worker# The root filesystem disk image we'll use.
104*2f2c4c7aSAndroid Build Coastguard WorkerROOTFS=${ROOTFS:-net_test.rootfs.20221014}
105*2f2c4c7aSAndroid Build Coastguard WorkerCOMPRESSED_ROOTFS=$ROOTFS.xz
106*2f2c4c7aSAndroid Build Coastguard WorkerURL=https://dl.google.com/dl/android/$COMPRESSED_ROOTFS
107*2f2c4c7aSAndroid Build Coastguard Worker
108*2f2c4c7aSAndroid Build Coastguard Worker# Parse arguments and figure out which test to run.
109*2f2c4c7aSAndroid Build Coastguard WorkerARCH=${ARCH:-um}
110*2f2c4c7aSAndroid Build Coastguard WorkerJ=${J:-$(nproc)}
111*2f2c4c7aSAndroid Build Coastguard WorkerMAKE="make"
112*2f2c4c7aSAndroid Build Coastguard WorkerOUT_DIR=$(readlink -f ${OUT_DIR:-.})
113*2f2c4c7aSAndroid Build Coastguard WorkerKERNEL_DIR=$(readlink -f ${KERNEL_DIR:-.})
114*2f2c4c7aSAndroid Build Coastguard Workerif [ "$OUT_DIR" != "$KERNEL_DIR" ]; then
115*2f2c4c7aSAndroid Build Coastguard Worker    MAKE="$MAKE O=$OUT_DIR"
116*2f2c4c7aSAndroid Build Coastguard Workerfi
117*2f2c4c7aSAndroid Build Coastguard WorkerSCRIPT_DIR=$(dirname $(readlink -f $0))
118*2f2c4c7aSAndroid Build Coastguard WorkerCONFIG_SCRIPT=${KERNEL_DIR}/scripts/config
119*2f2c4c7aSAndroid Build Coastguard WorkerCONFIG_FILE=${OUT_DIR}/.config
120*2f2c4c7aSAndroid Build Coastguard Workerconsolemode=
121*2f2c4c7aSAndroid Build Coastguard Workernetconfig=
122*2f2c4c7aSAndroid Build Coastguard Workertestmode=
123*2f2c4c7aSAndroid Build Coastguard Workercmdline=
124*2f2c4c7aSAndroid Build Coastguard Workernowrite=1
125*2f2c4c7aSAndroid Build Coastguard Workernobuild=0
126*2f2c4c7aSAndroid Build Coastguard Workernorun=0
127*2f2c4c7aSAndroid Build Coastguard Worker
128*2f2c4c7aSAndroid Build Coastguard Workerif [[ ! -f "${KERNEL_DIR}/Makefile" ]]; then
129*2f2c4c7aSAndroid Build Coastguard Worker  echo "No kernel Makefile found. Are you running this from a kernel directory?"
130*2f2c4c7aSAndroid Build Coastguard Worker  exit 1
131*2f2c4c7aSAndroid Build Coastguard Workerfi
132*2f2c4c7aSAndroid Build Coastguard Worker
133*2f2c4c7aSAndroid Build Coastguard WorkerKVER_MAJOR="$(sed -rn 's@^ *VERSION *= *([0-9]+)$@\1@p'    < "${KERNEL_DIR}/Makefile")"
134*2f2c4c7aSAndroid Build Coastguard WorkerKVER_MINOR="$(sed -rn 's@^ *PATCHLEVEL *= *([0-9]+)$@\1@p' < "${KERNEL_DIR}/Makefile")"
135*2f2c4c7aSAndroid Build Coastguard WorkerKVER_LEVEL="$(sed -rn 's@^ *SUBLEVEL *= *([0-9]+)$@\1@p'   < "${KERNEL_DIR}/Makefile")"
136*2f2c4c7aSAndroid Build Coastguard WorkerKVER="${KVER_MAJOR}.${KVER_MINOR}.${KVER_LEVEL}"
137*2f2c4c7aSAndroid Build Coastguard Workerecho "Detected kernel version ${KVER}"
138*2f2c4c7aSAndroid Build Coastguard Worker
139*2f2c4c7aSAndroid Build Coastguard Workerif [[ -z "${DEFCONFIG:-}" ]]; then
140*2f2c4c7aSAndroid Build Coastguard Worker  case "${ARCH}" in
141*2f2c4c7aSAndroid Build Coastguard Worker    um)
142*2f2c4c7aSAndroid Build Coastguard Worker      export DEFCONFIG=defconfig
143*2f2c4c7aSAndroid Build Coastguard Worker      ;;
144*2f2c4c7aSAndroid Build Coastguard Worker    arm64)
145*2f2c4c7aSAndroid Build Coastguard Worker      if [[ -e arch/arm64/configs/gki_defconfig ]]; then
146*2f2c4c7aSAndroid Build Coastguard Worker        export DEFCONFIG=gki_defconfig
147*2f2c4c7aSAndroid Build Coastguard Worker      elif [[ -e arch/arm64/configs/cuttlefish_defconfig ]]; then
148*2f2c4c7aSAndroid Build Coastguard Worker        export DEFCONFIG=cuttlefish_defconfig
149*2f2c4c7aSAndroid Build Coastguard Worker      fi
150*2f2c4c7aSAndroid Build Coastguard Worker      ;;
151*2f2c4c7aSAndroid Build Coastguard Worker    x86_64)
152*2f2c4c7aSAndroid Build Coastguard Worker      if [[ -e arch/x86/configs/gki_defconfig ]]; then
153*2f2c4c7aSAndroid Build Coastguard Worker        export DEFCONFIG=gki_defconfig
154*2f2c4c7aSAndroid Build Coastguard Worker      elif [[ -e arch/x86/configs/x86_64_cuttlefish_defconfig ]]; then
155*2f2c4c7aSAndroid Build Coastguard Worker        export DEFCONFIG=x86_64_cuttlefish_defconfig
156*2f2c4c7aSAndroid Build Coastguard Worker      fi
157*2f2c4c7aSAndroid Build Coastguard Worker  esac
158*2f2c4c7aSAndroid Build Coastguard Workerfi
159*2f2c4c7aSAndroid Build Coastguard Worker
160*2f2c4c7aSAndroid Build Coastguard Workerif tty >/dev/null; then
161*2f2c4c7aSAndroid Build Coastguard Worker  verbose=
162*2f2c4c7aSAndroid Build Coastguard Workerelse
163*2f2c4c7aSAndroid Build Coastguard Worker  verbose=1
164*2f2c4c7aSAndroid Build Coastguard Workerfi
165*2f2c4c7aSAndroid Build Coastguard Worker
166*2f2c4c7aSAndroid Build Coastguard Workertest=all_tests.sh
167*2f2c4c7aSAndroid Build Coastguard Workerwhile [[ -n "$1" ]]; do
168*2f2c4c7aSAndroid Build Coastguard Worker  if [[ "$1" == "--builder" || "$1" == "-b" ]]; then
169*2f2c4c7aSAndroid Build Coastguard Worker    consolemode="con=null,fd:1"
170*2f2c4c7aSAndroid Build Coastguard Worker    testmode=builder
171*2f2c4c7aSAndroid Build Coastguard Worker    shift
172*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ "$1" == "--readwrite" || "$1" == "--rw" ]]; then
173*2f2c4c7aSAndroid Build Coastguard Worker    nowrite=0
174*2f2c4c7aSAndroid Build Coastguard Worker    shift
175*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ "$1" == "--readonly" ||  "$1" == "--ro" ]]; then
176*2f2c4c7aSAndroid Build Coastguard Worker    nowrite=1
177*2f2c4c7aSAndroid Build Coastguard Worker    shift
178*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ "$1" == "--nobuild" ]]; then
179*2f2c4c7aSAndroid Build Coastguard Worker    nobuild=1
180*2f2c4c7aSAndroid Build Coastguard Worker    shift
181*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ "$1" == "--norun" ]]; then
182*2f2c4c7aSAndroid Build Coastguard Worker    norun=1
183*2f2c4c7aSAndroid Build Coastguard Worker    shift
184*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ "$1" == "--verbose" ]]; then
185*2f2c4c7aSAndroid Build Coastguard Worker    verbose=1
186*2f2c4c7aSAndroid Build Coastguard Worker    shift
187*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ "$1" == "--noverbose" ]]; then
188*2f2c4c7aSAndroid Build Coastguard Worker    verbose=
189*2f2c4c7aSAndroid Build Coastguard Worker    shift
190*2f2c4c7aSAndroid Build Coastguard Worker  else
191*2f2c4c7aSAndroid Build Coastguard Worker    test=$1
192*2f2c4c7aSAndroid Build Coastguard Worker    break  # Arguments after the test file are passed to the test itself.
193*2f2c4c7aSAndroid Build Coastguard Worker  fi
194*2f2c4c7aSAndroid Build Coastguard Workerdone
195*2f2c4c7aSAndroid Build Coastguard Worker
196*2f2c4c7aSAndroid Build Coastguard Worker# Check that test file exists and is readable
197*2f2c4c7aSAndroid Build Coastguard Workertest_file=$SCRIPT_DIR/$test
198*2f2c4c7aSAndroid Build Coastguard Workerif [[ ! -e $test_file ]]; then
199*2f2c4c7aSAndroid Build Coastguard Worker  echo "test file '${test_file}' does not exist"
200*2f2c4c7aSAndroid Build Coastguard Worker  exit 1
201*2f2c4c7aSAndroid Build Coastguard Workerfi
202*2f2c4c7aSAndroid Build Coastguard Worker
203*2f2c4c7aSAndroid Build Coastguard Workerif [[ ! -x $test_file ]]; then
204*2f2c4c7aSAndroid Build Coastguard Worker  echo "test file '${test_file}' is not executable"
205*2f2c4c7aSAndroid Build Coastguard Worker  exit 1
206*2f2c4c7aSAndroid Build Coastguard Workerfi
207*2f2c4c7aSAndroid Build Coastguard Worker
208*2f2c4c7aSAndroid Build Coastguard Worker# Collect trailing arguments to pass to $test
209*2f2c4c7aSAndroid Build Coastguard Workertest_args=${@:2}
210*2f2c4c7aSAndroid Build Coastguard Worker
211*2f2c4c7aSAndroid Build Coastguard Workerfunction isRunningTest() {
212*2f2c4c7aSAndroid Build Coastguard Worker  ! (( norun ))
213*2f2c4c7aSAndroid Build Coastguard Worker}
214*2f2c4c7aSAndroid Build Coastguard Worker
215*2f2c4c7aSAndroid Build Coastguard Workerfunction isBuildOnly() {
216*2f2c4c7aSAndroid Build Coastguard Worker  (( norun )) && ! (( nobuild ))
217*2f2c4c7aSAndroid Build Coastguard Worker}
218*2f2c4c7aSAndroid Build Coastguard Worker
219*2f2c4c7aSAndroid Build Coastguard Workerif ! isRunningTest && ! isBuildOnly; then
220*2f2c4c7aSAndroid Build Coastguard Worker  echo "Usage:" >&2
221*2f2c4c7aSAndroid Build Coastguard Worker  echo "  $0 [--builder] [--readonly|--ro|--readwrite|--rw] [--nobuild] [--verbose] [<test>]" >&2
222*2f2c4c7aSAndroid Build Coastguard Worker  echo "      - if [<test>] is not specified, run all_tests.sh" >&2
223*2f2c4c7aSAndroid Build Coastguard Worker  echo "  $0 --norun" >&2
224*2f2c4c7aSAndroid Build Coastguard Worker  exit 1
225*2f2c4c7aSAndroid Build Coastguard Workerfi
226*2f2c4c7aSAndroid Build Coastguard Worker
227*2f2c4c7aSAndroid Build Coastguard Workercd $OUT_DIR
228*2f2c4c7aSAndroid Build Coastguard Workerecho Running tests from: `pwd`
229*2f2c4c7aSAndroid Build Coastguard Worker
230*2f2c4c7aSAndroid Build Coastguard Workerset -e
231*2f2c4c7aSAndroid Build Coastguard Worker
232*2f2c4c7aSAndroid Build Coastguard Worker# Check if we need to uncompress the disk image.
233*2f2c4c7aSAndroid Build Coastguard Worker# We use xz because it compresses better: to 42M vs 72M (gzip) / 62M (bzip2).
234*2f2c4c7aSAndroid Build Coastguard Workercd $SCRIPT_DIR
235*2f2c4c7aSAndroid Build Coastguard Workerif [ ! -f $ROOTFS ]; then
236*2f2c4c7aSAndroid Build Coastguard Worker  echo "Deleting $COMPRESSED_ROOTFS" >&2
237*2f2c4c7aSAndroid Build Coastguard Worker  rm -f $COMPRESSED_ROOTFS
238*2f2c4c7aSAndroid Build Coastguard Worker  echo "Downloading $URL" >&2
239*2f2c4c7aSAndroid Build Coastguard Worker  wget -nv $URL
240*2f2c4c7aSAndroid Build Coastguard Worker  echo "Uncompressing $COMPRESSED_ROOTFS" >&2
241*2f2c4c7aSAndroid Build Coastguard Worker  unxz $COMPRESSED_ROOTFS
242*2f2c4c7aSAndroid Build Coastguard Workerfi
243*2f2c4c7aSAndroid Build Coastguard Workerif ! [[ "${ROOTFS}" =~ ^/ ]]; then
244*2f2c4c7aSAndroid Build Coastguard Worker  ROOTFS="${SCRIPT_DIR}/${ROOTFS}"
245*2f2c4c7aSAndroid Build Coastguard Workerfi
246*2f2c4c7aSAndroid Build Coastguard Workerecho "Using $ROOTFS"
247*2f2c4c7aSAndroid Build Coastguard Workercd -
248*2f2c4c7aSAndroid Build Coastguard Worker
249*2f2c4c7aSAndroid Build Coastguard Worker# If network access was requested, create NUMTAPINTERFACES tap interfaces on
250*2f2c4c7aSAndroid Build Coastguard Worker# the host, and prepare UML command line params to use them. The interfaces are
251*2f2c4c7aSAndroid Build Coastguard Worker# called <user>TAP0, <user>TAP1, on the host, and eth0, eth1, ..., in the VM.
252*2f2c4c7aSAndroid Build Coastguard Workerif (( $NUMTAPINTERFACES > 0 )); then
253*2f2c4c7aSAndroid Build Coastguard Worker  user=${USER:0:10}
254*2f2c4c7aSAndroid Build Coastguard Worker  tapinterfaces=
255*2f2c4c7aSAndroid Build Coastguard Worker  for id in $(seq 0 $(( NUMTAPINTERFACES - 1 )) ); do
256*2f2c4c7aSAndroid Build Coastguard Worker    tap=${user}TAP$id
257*2f2c4c7aSAndroid Build Coastguard Worker    tapinterfaces="$tapinterfaces $tap"
258*2f2c4c7aSAndroid Build Coastguard Worker    mac=$(printf fe:fd:00:00:00:%02x $id)
259*2f2c4c7aSAndroid Build Coastguard Worker    if [ "$ARCH" == "um" ]; then
260*2f2c4c7aSAndroid Build Coastguard Worker      netconfig="$netconfig eth$id=tuntap,$tap,$mac"
261*2f2c4c7aSAndroid Build Coastguard Worker    else
262*2f2c4c7aSAndroid Build Coastguard Worker      netconfig="$netconfig -netdev tap,id=hostnet$id,ifname=$tap,script=no,downscript=no"
263*2f2c4c7aSAndroid Build Coastguard Worker      netconfig="$netconfig -device virtio-net-pci,netdev=hostnet$id,id=net$id,mac=$mac"
264*2f2c4c7aSAndroid Build Coastguard Worker    fi
265*2f2c4c7aSAndroid Build Coastguard Worker  done
266*2f2c4c7aSAndroid Build Coastguard Worker
267*2f2c4c7aSAndroid Build Coastguard Worker  for tap in $tapinterfaces; do
268*2f2c4c7aSAndroid Build Coastguard Worker    if ! ip link list $tap > /dev/null; then
269*2f2c4c7aSAndroid Build Coastguard Worker      echo "Creating tap interface $tap" >&2
270*2f2c4c7aSAndroid Build Coastguard Worker      sudo tunctl -u $USER -t $tap
271*2f2c4c7aSAndroid Build Coastguard Worker      sudo ip link set $tap up
272*2f2c4c7aSAndroid Build Coastguard Worker    fi
273*2f2c4c7aSAndroid Build Coastguard Worker  done
274*2f2c4c7aSAndroid Build Coastguard Workerfi
275*2f2c4c7aSAndroid Build Coastguard Worker
276*2f2c4c7aSAndroid Build Coastguard Workerif [[ -n "${KERNEL_BINARY:-}" ]]; then
277*2f2c4c7aSAndroid Build Coastguard Worker  nobuild=1
278*2f2c4c7aSAndroid Build Coastguard Workerelse
279*2f2c4c7aSAndroid Build Coastguard Worker  # Set default KERNEL_BINARY location if it was not provided.
280*2f2c4c7aSAndroid Build Coastguard Worker  if [ "$ARCH" == "um" ]; then
281*2f2c4c7aSAndroid Build Coastguard Worker    KERNEL_BINARY=./linux
282*2f2c4c7aSAndroid Build Coastguard Worker  elif [ "$ARCH" == "i386" -o "$ARCH" == "x86_64" -o "$ARCH" == "x86" ]; then
283*2f2c4c7aSAndroid Build Coastguard Worker    KERNEL_BINARY=./arch/x86/boot/bzImage
284*2f2c4c7aSAndroid Build Coastguard Worker  elif [ "$ARCH" == "arm64" ]; then
285*2f2c4c7aSAndroid Build Coastguard Worker    KERNEL_BINARY=./arch/arm64/boot/Image.gz
286*2f2c4c7aSAndroid Build Coastguard Worker  fi
287*2f2c4c7aSAndroid Build Coastguard Workerfi
288*2f2c4c7aSAndroid Build Coastguard Worker
289*2f2c4c7aSAndroid Build Coastguard Workerif ((nobuild == 0)); then
290*2f2c4c7aSAndroid Build Coastguard Worker  make_flags=
291*2f2c4c7aSAndroid Build Coastguard Worker  if [ "$ARCH" == "um" ]; then
292*2f2c4c7aSAndroid Build Coastguard Worker    # Exporting ARCH=um SUBARCH=x86_64 doesn't seem to work, as it
293*2f2c4c7aSAndroid Build Coastguard Worker    # "sometimes" (?) results in a 32-bit kernel.
294*2f2c4c7aSAndroid Build Coastguard Worker    make_flags="$make_flags ARCH=$ARCH SUBARCH=${SUBARCH:-x86_64} CROSS_COMPILE= "
295*2f2c4c7aSAndroid Build Coastguard Worker  fi
296*2f2c4c7aSAndroid Build Coastguard Worker  if [[ -n "${CC:-}" ]]; then
297*2f2c4c7aSAndroid Build Coastguard Worker    # The CC flag is *not* inherited from the environment, so it must be
298*2f2c4c7aSAndroid Build Coastguard Worker    # passed in on the command line.
299*2f2c4c7aSAndroid Build Coastguard Worker    make_flags="$make_flags CC=$CC"
300*2f2c4c7aSAndroid Build Coastguard Worker  fi
301*2f2c4c7aSAndroid Build Coastguard Worker
302*2f2c4c7aSAndroid Build Coastguard Worker  # If there's no kernel config at all, create one or UML won't work.
303*2f2c4c7aSAndroid Build Coastguard Worker  [ -f $CONFIG_FILE ] || (cd $KERNEL_DIR && $MAKE $make_flags $DEFCONFIG)
304*2f2c4c7aSAndroid Build Coastguard Worker
305*2f2c4c7aSAndroid Build Coastguard Worker  # Enable the kernel config options listed in $OPTIONS.
306*2f2c4c7aSAndroid Build Coastguard Worker  $CONFIG_SCRIPT --file $CONFIG_FILE ${OPTIONS// / -e }
307*2f2c4c7aSAndroid Build Coastguard Worker
308*2f2c4c7aSAndroid Build Coastguard Worker  # Increase acceptable frame size.
309*2f2c4c7aSAndroid Build Coastguard Worker  $CONFIG_SCRIPT --file $CONFIG_FILE --set-val FRAME_WARN 3172
310*2f2c4c7aSAndroid Build Coastguard Worker
311*2f2c4c7aSAndroid Build Coastguard Worker  # Disable the kernel config options listed in $DISABLE_OPTIONS.
312*2f2c4c7aSAndroid Build Coastguard Worker  $CONFIG_SCRIPT --file $CONFIG_FILE ${DISABLE_OPTIONS// / -d }
313*2f2c4c7aSAndroid Build Coastguard Worker
314*2f2c4c7aSAndroid Build Coastguard Worker  echo "Running: $MAKE $make_flags olddefconfig"
315*2f2c4c7aSAndroid Build Coastguard Worker  $MAKE $make_flags olddefconfig
316*2f2c4c7aSAndroid Build Coastguard Worker
317*2f2c4c7aSAndroid Build Coastguard Worker  # Compile the kernel.
318*2f2c4c7aSAndroid Build Coastguard Worker  if [ "$ARCH" == "um" ]; then
319*2f2c4c7aSAndroid Build Coastguard Worker    echo "Running: $MAKE -j$J $make_flags linux"
320*2f2c4c7aSAndroid Build Coastguard Worker    $MAKE -j$J $make_flags linux
321*2f2c4c7aSAndroid Build Coastguard Worker  else
322*2f2c4c7aSAndroid Build Coastguard Worker    echo "Running: $MAKE -j$J $make_flags"
323*2f2c4c7aSAndroid Build Coastguard Worker    $MAKE -j$J $make_flags
324*2f2c4c7aSAndroid Build Coastguard Worker  fi
325*2f2c4c7aSAndroid Build Coastguard Workerfi
326*2f2c4c7aSAndroid Build Coastguard Worker
327*2f2c4c7aSAndroid Build Coastguard Workerif (( norun == 1 )); then
328*2f2c4c7aSAndroid Build Coastguard Worker  exit 0
329*2f2c4c7aSAndroid Build Coastguard Workerfi
330*2f2c4c7aSAndroid Build Coastguard Worker
331*2f2c4c7aSAndroid Build Coastguard Workerif (( nowrite == 1 )); then
332*2f2c4c7aSAndroid Build Coastguard Worker  cmdline="ro"
333*2f2c4c7aSAndroid Build Coastguard Workerfi
334*2f2c4c7aSAndroid Build Coastguard Worker
335*2f2c4c7aSAndroid Build Coastguard Workerif (( verbose == 1 )); then
336*2f2c4c7aSAndroid Build Coastguard Worker  cmdline="$cmdline verbose=1"
337*2f2c4c7aSAndroid Build Coastguard Workerfi
338*2f2c4c7aSAndroid Build Coastguard Worker
339*2f2c4c7aSAndroid Build Coastguard Workercmdline="$cmdline panic=1 init=/sbin/net_test.sh"
340*2f2c4c7aSAndroid Build Coastguard Workercmdline="$cmdline net_test_args=\"$test_args\" net_test_mode=$testmode"
341*2f2c4c7aSAndroid Build Coastguard Worker
342*2f2c4c7aSAndroid Build Coastguard Worker# Experience shows that we need at least 128 bits of entropy for the
343*2f2c4c7aSAndroid Build Coastguard Worker# kernel's crng init to complete (before it fully initializes stuff behaves
344*2f2c4c7aSAndroid Build Coastguard Worker# *weirdly* and there's plenty of kernel warnings and some tests even fail),
345*2f2c4c7aSAndroid Build Coastguard Worker# hence net_test.sh needs at least 32 hex chars (which is the amount of hex
346*2f2c4c7aSAndroid Build Coastguard Worker# in a single random UUID) provided to it on the kernel cmdline.
347*2f2c4c7aSAndroid Build Coastguard Worker#
348*2f2c4c7aSAndroid Build Coastguard Worker# Just to be safe, we'll pass in 384 bits, and we'll do this as a random
349*2f2c4c7aSAndroid Build Coastguard Worker# 64 character base64 seed (because this is shorter than base16).
350*2f2c4c7aSAndroid Build Coastguard Worker# We do this by getting *three* random UUIDs and concatenating their hex
351*2f2c4c7aSAndroid Build Coastguard Worker# digits into an *even* length hex encoded string, which we then convert
352*2f2c4c7aSAndroid Build Coastguard Worker# into base64.
353*2f2c4c7aSAndroid Build Coastguard Workerentropy="$(cat /proc/sys/kernel/random{/,/,/}uuid | tr -d '\n-')"
354*2f2c4c7aSAndroid Build Coastguard Workerentropy="$(xxd -r -p <<< "${entropy}" | base64 -w 0)"
355*2f2c4c7aSAndroid Build Coastguard Workercmdline="${cmdline} random.trust_cpu=on entropy=${entropy}"
356*2f2c4c7aSAndroid Build Coastguard Worker
357*2f2c4c7aSAndroid Build Coastguard Workerif [ "$ARCH" == "um" ]; then
358*2f2c4c7aSAndroid Build Coastguard Worker  # Get the absolute path to the test file that's being run.
359*2f2c4c7aSAndroid Build Coastguard Worker  cmdline="$cmdline net_test=/host$SCRIPT_DIR/$test"
360*2f2c4c7aSAndroid Build Coastguard Worker
361*2f2c4c7aSAndroid Build Coastguard Worker  # We'd use UML's /proc/exitcode feature to communicate errors on test failure,
362*2f2c4c7aSAndroid Build Coastguard Worker  # if not for UML having a tendency to crash during shutdown,
363*2f2c4c7aSAndroid Build Coastguard Worker  # so instead use an extra serial line we'll redirect to an open fd...
364*2f2c4c7aSAndroid Build Coastguard Worker  cmdline="$cmdline exitcode=/dev/ttyS3"
365*2f2c4c7aSAndroid Build Coastguard Worker
366*2f2c4c7aSAndroid Build Coastguard Worker  # Map the --readonly flag to UML block device names
367*2f2c4c7aSAndroid Build Coastguard Worker  if ((nowrite == 0)); then
368*2f2c4c7aSAndroid Build Coastguard Worker    blockdevice=ubda
369*2f2c4c7aSAndroid Build Coastguard Worker  else
370*2f2c4c7aSAndroid Build Coastguard Worker    blockdevice=ubdar
371*2f2c4c7aSAndroid Build Coastguard Worker  fi
372*2f2c4c7aSAndroid Build Coastguard Worker
373*2f2c4c7aSAndroid Build Coastguard Worker  # Create a temp file for 'serial line 3' for return code.
374*2f2c4c7aSAndroid Build Coastguard Worker  SSL3="$(mktemp)"
375*2f2c4c7aSAndroid Build Coastguard Worker
376*2f2c4c7aSAndroid Build Coastguard Worker  exitcode=0
377*2f2c4c7aSAndroid Build Coastguard Worker  $KERNEL_BINARY >&2 3>"${SSL3}" umid=net_test mem=512M \
378*2f2c4c7aSAndroid Build Coastguard Worker    $blockdevice=$ROOTFS $netconfig $consolemode ssl3=null,fd:3 $cmdline \
379*2f2c4c7aSAndroid Build Coastguard Worker  || exitcode=$?
380*2f2c4c7aSAndroid Build Coastguard Worker
381*2f2c4c7aSAndroid Build Coastguard Worker  # Return to beginning of line (via carriage return) after the above newline moved us down.
382*2f2c4c7aSAndroid Build Coastguard Worker  echo -en '\r'
383*2f2c4c7aSAndroid Build Coastguard Worker  # re-enable: 'postprocess output' and 'translate newline to carriage return-newline'
384*2f2c4c7aSAndroid Build Coastguard Worker  stty opost onlcr || :
385*2f2c4c7aSAndroid Build Coastguard Worker
386*2f2c4c7aSAndroid Build Coastguard Worker  if [[ "${exitcode}" == 134 && -s "${SSL3}" && "$(tr -d '\r' < "${SSL3}")" == 0 ]]; then
387*2f2c4c7aSAndroid Build Coastguard Worker    # Sometimes the tests all pass, but UML crashes during the shutdown process itself.
388*2f2c4c7aSAndroid Build Coastguard Worker    # As such we can't actually rely on the /proc/exitcode returned value.
389*2f2c4c7aSAndroid Build Coastguard Worker    echo "Warning: UML appears to have crashed after successfully executing the tests." 1>&2
390*2f2c4c7aSAndroid Build Coastguard Worker  elif [[ "${exitcode}" != 0 ]]; then
391*2f2c4c7aSAndroid Build Coastguard Worker    echo "Warning: UML exited with ${exitcode} instead of zero." 1>&2
392*2f2c4c7aSAndroid Build Coastguard Worker  fi
393*2f2c4c7aSAndroid Build Coastguard Worker
394*2f2c4c7aSAndroid Build Coastguard Worker  if [[ -s "${SSL3}" ]]; then
395*2f2c4c7aSAndroid Build Coastguard Worker    exitcode="$(tr -d '\r' < "${SSL3}")"
396*2f2c4c7aSAndroid Build Coastguard Worker    echo "Info: retrieved exit code ${exitcode}." 1>&2
397*2f2c4c7aSAndroid Build Coastguard Worker  fi
398*2f2c4c7aSAndroid Build Coastguard Worker
399*2f2c4c7aSAndroid Build Coastguard Worker  rm -f "${SSL3}"
400*2f2c4c7aSAndroid Build Coastguard Worker  unset SSL3
401*2f2c4c7aSAndroid Build Coastguard Worker
402*2f2c4c7aSAndroid Build Coastguard Worker  # UML is kind of crazy in how guest syscalls work.  It requires host kernel
403*2f2c4c7aSAndroid Build Coastguard Worker  # to not be in vsyscall=none mode.
404*2f2c4c7aSAndroid Build Coastguard Worker  if [[ "${exitcode}" != '0' ]]; then
405*2f2c4c7aSAndroid Build Coastguard Worker    {
406*2f2c4c7aSAndroid Build Coastguard Worker      # Hopefully one of these exists
407*2f2c4c7aSAndroid Build Coastguard Worker      cat /proc/config || :
408*2f2c4c7aSAndroid Build Coastguard Worker      zcat /proc/config.gz || :
409*2f2c4c7aSAndroid Build Coastguard Worker      cat "/boot/config-$(uname -r)" || :
410*2f2c4c7aSAndroid Build Coastguard Worker      zcat "/boot/config-$(uname -r).gz" || :
411*2f2c4c7aSAndroid Build Coastguard Worker    } 2>/dev/null \
412*2f2c4c7aSAndroid Build Coastguard Worker    | egrep -q '^CONFIG_LEGACY_VSYSCALL_NONE=y' \
413*2f2c4c7aSAndroid Build Coastguard Worker    && ! egrep -q '(^| )vsyscall=(native|emulate|xonly)( |$)' /proc/cmdline \
414*2f2c4c7aSAndroid Build Coastguard Worker    && {
415*2f2c4c7aSAndroid Build Coastguard Worker      echo -e "\r"
416*2f2c4c7aSAndroid Build Coastguard Worker      echo -e "-----=====-----\r"
417*2f2c4c7aSAndroid Build Coastguard Worker      echo -e "If above you saw a 'net_test.sh[1]: segfault at ...' followed by\r"
418*2f2c4c7aSAndroid Build Coastguard Worker      echo -e "'Kernel panic - not syncing: Attempted to kill init!' then please\r"
419*2f2c4c7aSAndroid Build Coastguard Worker      echo -e "set 'vsyscall=emulate' on *host* kernel command line.\r"
420*2f2c4c7aSAndroid Build Coastguard Worker      echo -e "On Linux 5.2+ you can instead use the slightly safer 'vsyscall=xonly'.\r"
421*2f2c4c7aSAndroid Build Coastguard Worker      echo -e "(for example via GRUB_CMDLINE_LINUX in /etc/default/grub)\r"
422*2f2c4c7aSAndroid Build Coastguard Worker      echo -e "-----=====-----\r"
423*2f2c4c7aSAndroid Build Coastguard Worker    }
424*2f2c4c7aSAndroid Build Coastguard Worker  fi
425*2f2c4c7aSAndroid Build Coastguard Workerelse
426*2f2c4c7aSAndroid Build Coastguard Worker  # We boot into the filesystem image directly in all cases
427*2f2c4c7aSAndroid Build Coastguard Worker  cmdline="$cmdline root=/dev/vda"
428*2f2c4c7aSAndroid Build Coastguard Worker
429*2f2c4c7aSAndroid Build Coastguard Worker  # The path is stripped by the 9p export; we don't need SCRIPT_DIR
430*2f2c4c7aSAndroid Build Coastguard Worker  cmdline="$cmdline net_test=/host/$test"
431*2f2c4c7aSAndroid Build Coastguard Worker
432*2f2c4c7aSAndroid Build Coastguard Worker  # Map the --readonly flag to a QEMU block device flag
433*2f2c4c7aSAndroid Build Coastguard Worker  if ((nowrite > 0)); then
434*2f2c4c7aSAndroid Build Coastguard Worker    blockdevice=",readonly=on"
435*2f2c4c7aSAndroid Build Coastguard Worker  else
436*2f2c4c7aSAndroid Build Coastguard Worker    blockdevice=
437*2f2c4c7aSAndroid Build Coastguard Worker  fi
438*2f2c4c7aSAndroid Build Coastguard Worker  blockdevice="-drive file=$ROOTFS,format=raw,if=none,id=drive-virtio-disk0$blockdevice"
439*2f2c4c7aSAndroid Build Coastguard Worker  blockdevice="$blockdevice -device virtio-blk-pci,drive=drive-virtio-disk0"
440*2f2c4c7aSAndroid Build Coastguard Worker
441*2f2c4c7aSAndroid Build Coastguard Worker  # Pass through our current console/screen size to inner shell session
442*2f2c4c7aSAndroid Build Coastguard Worker  read rows cols < <(stty size 2>/dev/null)
443*2f2c4c7aSAndroid Build Coastguard Worker  [[ -z "${rows}" ]] || cmdline="${cmdline} console_rows=${rows}"
444*2f2c4c7aSAndroid Build Coastguard Worker  [[ -z "${cols}" ]] || cmdline="${cmdline} console_cols=${cols}"
445*2f2c4c7aSAndroid Build Coastguard Worker  unset rows cols
446*2f2c4c7aSAndroid Build Coastguard Worker
447*2f2c4c7aSAndroid Build Coastguard Worker  # QEMU has no way to modify its exitcode; simulate it with a serial port.
448*2f2c4c7aSAndroid Build Coastguard Worker  #
449*2f2c4c7aSAndroid Build Coastguard Worker  # Choose to do it this way over writing a file to /host, because QEMU will
450*2f2c4c7aSAndroid Build Coastguard Worker  # initialize the 'exitcode' file for us, it avoids unnecessary writes to the
451*2f2c4c7aSAndroid Build Coastguard Worker  # host filesystem (which is normally not written to) and it allows us to
452*2f2c4c7aSAndroid Build Coastguard Worker  # communicate an exit code back in cases we do not have /host mounted.
453*2f2c4c7aSAndroid Build Coastguard Worker  #
454*2f2c4c7aSAndroid Build Coastguard Worker  if [ "$ARCH" == "i386" -o "$ARCH" == "x86_64" -o "$ARCH" == "x86" ]; then
455*2f2c4c7aSAndroid Build Coastguard Worker    # Assume we have hardware-accelerated virtualization support for amd64
456*2f2c4c7aSAndroid Build Coastguard Worker    qemu="qemu-system-x86_64 -machine pc,accel=kvm -cpu host"
457*2f2c4c7aSAndroid Build Coastguard Worker
458*2f2c4c7aSAndroid Build Coastguard Worker    # We know 'ttyS0' will be our serial port on x86 from the hard-coded
459*2f2c4c7aSAndroid Build Coastguard Worker    # '-serial mon:stdio' flag below
460*2f2c4c7aSAndroid Build Coastguard Worker    cmdline="$cmdline console=ttyS0"
461*2f2c4c7aSAndroid Build Coastguard Worker
462*2f2c4c7aSAndroid Build Coastguard Worker    # The assignment of 'ttyS1' here is magical; we know ttyS0 was used up
463*2f2c4c7aSAndroid Build Coastguard Worker    # by '-serial mon:stdio', and so this second serial port will be 'ttyS1'
464*2f2c4c7aSAndroid Build Coastguard Worker    cmdline="$cmdline exitcode=/dev/ttyS1"
465*2f2c4c7aSAndroid Build Coastguard Worker  elif [ "$ARCH" == "arm64" ]; then
466*2f2c4c7aSAndroid Build Coastguard Worker    # This uses a software model CPU, based on cortex-a57
467*2f2c4c7aSAndroid Build Coastguard Worker    qemu="qemu-system-aarch64 -machine virt -cpu cortex-a57"
468*2f2c4c7aSAndroid Build Coastguard Worker
469*2f2c4c7aSAndroid Build Coastguard Worker    # We know 'ttyAMA0' will be our serial port on arm64 from the hard-coded
470*2f2c4c7aSAndroid Build Coastguard Worker    # '-serial mon:stdio' flag below
471*2f2c4c7aSAndroid Build Coastguard Worker    cmdline="$cmdline console=ttyAMA0"
472*2f2c4c7aSAndroid Build Coastguard Worker
473*2f2c4c7aSAndroid Build Coastguard Worker    # The kernel will print messages via a virtual ARM serial port (ttyAMA0),
474*2f2c4c7aSAndroid Build Coastguard Worker    # but for command line consistency with x86, we put the exitcode serial
475*2f2c4c7aSAndroid Build Coastguard Worker    # port on the PCI bus, and it will be the only one.
476*2f2c4c7aSAndroid Build Coastguard Worker    cmdline="$cmdline exitcode=/dev/ttyS0"
477*2f2c4c7aSAndroid Build Coastguard Worker  fi
478*2f2c4c7aSAndroid Build Coastguard Worker
479*2f2c4c7aSAndroid Build Coastguard Worker  $qemu >&2 -name net_test -m 512 \
480*2f2c4c7aSAndroid Build Coastguard Worker    -kernel $KERNEL_BINARY \
481*2f2c4c7aSAndroid Build Coastguard Worker    -no-user-config -nodefaults -no-reboot \
482*2f2c4c7aSAndroid Build Coastguard Worker    -display none -nographic -serial mon:stdio -parallel none \
483*2f2c4c7aSAndroid Build Coastguard Worker    -smp 4,sockets=4,cores=1,threads=1 \
484*2f2c4c7aSAndroid Build Coastguard Worker    -device virtio-rng-pci \
485*2f2c4c7aSAndroid Build Coastguard Worker    -chardev file,id=exitcode,path=exitcode \
486*2f2c4c7aSAndroid Build Coastguard Worker    -device pci-serial,chardev=exitcode \
487*2f2c4c7aSAndroid Build Coastguard Worker    -fsdev local,security_model=mapped-xattr,id=fsdev0,fmode=0644,dmode=0755,path=$SCRIPT_DIR \
488*2f2c4c7aSAndroid Build Coastguard Worker    -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=host \
489*2f2c4c7aSAndroid Build Coastguard Worker    $blockdevice $netconfig -append "$cmdline"
490*2f2c4c7aSAndroid Build Coastguard Worker  [[ -s exitcode ]] && exitcode=`cat exitcode | tr -d '\r'` || exitcode=1
491*2f2c4c7aSAndroid Build Coastguard Worker  rm -f exitcode
492*2f2c4c7aSAndroid Build Coastguard Workerfi
493*2f2c4c7aSAndroid Build Coastguard Worker
494*2f2c4c7aSAndroid Build Coastguard Worker# UML reliably screws up the ptys, QEMU probably can as well...
495*2f2c4c7aSAndroid Build Coastguard Workerfixup_ptys
496*2f2c4c7aSAndroid Build Coastguard Workerstty sane || :
497*2f2c4c7aSAndroid Build Coastguard Workertput smam || :
498*2f2c4c7aSAndroid Build Coastguard Worker
499*2f2c4c7aSAndroid Build Coastguard Workerecho "Returning exit code ${exitcode}." 1>&2
500*2f2c4c7aSAndroid Build Coastguard Workerexit "${exitcode}"
501