1bf215546Sopenharmony_ci#!/bin/sh
2bf215546Sopenharmony_ci
3bf215546Sopenharmony_ciset -e
4bf215546Sopenharmony_ci
5bf215546Sopenharmony_ci# If run outside of a deqp-runner invoction (e.g. piglit trace replay), then act
6bf215546Sopenharmony_ci# the same as the first thread in its threadpool.
7bf215546Sopenharmony_ciTHREAD=${DEQP_RUNNER_THREAD:-0}
8bf215546Sopenharmony_ci
9bf215546Sopenharmony_ci#
10bf215546Sopenharmony_ci# Helper to generate CIDs for virtio-vsock based communication with processes
11bf215546Sopenharmony_ci# running inside crosvm guests.
12bf215546Sopenharmony_ci#
13bf215546Sopenharmony_ci# A CID is a 32-bit Context Identifier to be assigned to a crosvm instance
14bf215546Sopenharmony_ci# and must be unique across the host system. For this purpose, let's take
15bf215546Sopenharmony_ci# the least significant 25 bits from CI_JOB_ID as a base and generate a 7-bit
16bf215546Sopenharmony_ci# prefix number to handle up to 128 concurrent crosvm instances per job runner.
17bf215546Sopenharmony_ci#
18bf215546Sopenharmony_ci# As a result, the following variables are set:
19bf215546Sopenharmony_ci#  - VSOCK_CID: the crosvm unique CID to be passed as a run argument
20bf215546Sopenharmony_ci#
21bf215546Sopenharmony_ci#  - VSOCK_STDOUT, VSOCK_STDERR: the port numbers the guest should accept
22bf215546Sopenharmony_ci#    vsock connections on in order to transfer output messages
23bf215546Sopenharmony_ci#
24bf215546Sopenharmony_ci#  - VM_TEMP_DIR: the temporary directory path used to pass additional
25bf215546Sopenharmony_ci#    context data towards the guest
26bf215546Sopenharmony_ci#
27bf215546Sopenharmony_ciset_vsock_context() {
28bf215546Sopenharmony_ci    [ -n "${CI_JOB_ID}" ] || {
29bf215546Sopenharmony_ci        echo "Missing or unset CI_JOB_ID env variable" >&2
30bf215546Sopenharmony_ci        exit 1
31bf215546Sopenharmony_ci    }
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci    VM_TEMP_DIR="/tmp-vm.${THREAD}"
34bf215546Sopenharmony_ci    # Clear out any leftover files from a previous run.
35bf215546Sopenharmony_ci    rm -rf $VM_TEMP_DIR
36bf215546Sopenharmony_ci    mkdir $VM_TEMP_DIR || return 1
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci    VSOCK_CID=$(((CI_JOB_ID & 0x1ffffff) | ((${THREAD} & 0x7f) << 25)))
39bf215546Sopenharmony_ci    VSOCK_STDOUT=5001
40bf215546Sopenharmony_ci    VSOCK_STDERR=5002
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci    return 0
43bf215546Sopenharmony_ci}
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci# The dEQP binary needs to run from the directory it's in
46bf215546Sopenharmony_ciif [ -n "${1##*.sh}" ] && [ -z "${1##*"deqp"*}" ]; then
47bf215546Sopenharmony_ci    DEQP_BIN_DIR=$(dirname "$1")
48bf215546Sopenharmony_ci    export DEQP_BIN_DIR
49bf215546Sopenharmony_cifi
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ciVM_SOCKET=crosvm-${THREAD}.sock
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci# Terminate any existing crosvm, if a previous invocation of this shell script
54bf215546Sopenharmony_ci# was terminated due to timeouts.  This "vm stop" may fail if the crosvm died
55bf215546Sopenharmony_ci# without cleaning itself up.
56bf215546Sopenharmony_ciif [ -e $VM_SOCKET ]; then
57bf215546Sopenharmony_ci   crosvm stop $VM_SOCKET || rm -rf $VM_SOCKET
58bf215546Sopenharmony_ci   # Wait for socats from that invocation to drain
59bf215546Sopenharmony_ci   sleep 5
60bf215546Sopenharmony_cifi
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ciset_vsock_context || { echo "Could not generate crosvm vsock CID" >&2; exit 1; }
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci# Securely pass the current variables to the crosvm environment
65bf215546Sopenharmony_ciecho "Variables passed through:"
66bf215546Sopenharmony_ciSCRIPT_DIR=$(readlink -en "${0%/*}")
67bf215546Sopenharmony_ci${SCRIPT_DIR}/common/generate-env.sh | tee ${VM_TEMP_DIR}/crosvm-env.sh
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci# Set the crosvm-script as the arguments of the current script
70bf215546Sopenharmony_ciecho "$@" > ${VM_TEMP_DIR}/crosvm-script.sh
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci# Setup networking
73bf215546Sopenharmony_ci/usr/sbin/iptables-legacy -w -t nat -A POSTROUTING -o eth0 -j MASQUERADE
74bf215546Sopenharmony_ciecho 1 > /proc/sys/net/ipv4/ip_forward
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci# Start background processes to receive output from guest
77bf215546Sopenharmony_cisocat -u vsock-connect:${VSOCK_CID}:${VSOCK_STDERR},retry=200,interval=0.1 stderr &
78bf215546Sopenharmony_cisocat -u vsock-connect:${VSOCK_CID}:${VSOCK_STDOUT},retry=200,interval=0.1 stdout &
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci# Prepare to start crosvm
81bf215546Sopenharmony_ciunset DISPLAY
82bf215546Sopenharmony_ciunset XDG_RUNTIME_DIR
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ciCROSVM_KERN_ARGS="quiet console=null root=my_root rw rootfstype=virtiofs ip=192.168.30.2::192.168.30.1:255.255.255.0:crosvm:eth0"
85bf215546Sopenharmony_ciCROSVM_KERN_ARGS="${CROSVM_KERN_ARGS} init=${SCRIPT_DIR}/crosvm-init.sh -- ${VSOCK_STDOUT} ${VSOCK_STDERR} ${VM_TEMP_DIR}"
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci[ "${CROSVM_GALLIUM_DRIVER}" = "llvmpipe" ] && \
88bf215546Sopenharmony_ci    CROSVM_LIBGL_ALWAYS_SOFTWARE=true || CROSVM_LIBGL_ALWAYS_SOFTWARE=false
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ciset +e -x
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci# We aren't testing the host driver here, so we don't need to validate NIR on the host
93bf215546Sopenharmony_ciNIR_DEBUG="novalidate" \
94bf215546Sopenharmony_ciLIBGL_ALWAYS_SOFTWARE=${CROSVM_LIBGL_ALWAYS_SOFTWARE} \
95bf215546Sopenharmony_ciGALLIUM_DRIVER=${CROSVM_GALLIUM_DRIVER} \
96bf215546Sopenharmony_cicrosvm run \
97bf215546Sopenharmony_ci    --gpu "${CROSVM_GPU_ARGS}" -m 4096 -c 2 --disable-sandbox \
98bf215546Sopenharmony_ci    --shared-dir /:my_root:type=fs:writeback=true:timeout=60:cache=always \
99bf215546Sopenharmony_ci    --host_ip "192.168.30.1" --netmask "255.255.255.0" --mac "AA:BB:CC:00:00:12" \
100bf215546Sopenharmony_ci    -s $VM_SOCKET \
101bf215546Sopenharmony_ci    --cid ${VSOCK_CID} -p "${CROSVM_KERN_ARGS}" \
102bf215546Sopenharmony_ci    /lava-files/${KERNEL_IMAGE_NAME:-bzImage} > ${VM_TEMP_DIR}/crosvm 2>&1
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ciCROSVM_RET=$?
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci[ ${CROSVM_RET} -eq 0 ] && {
107bf215546Sopenharmony_ci    # The actual return code is the crosvm guest script's exit code
108bf215546Sopenharmony_ci    CROSVM_RET=$(cat ${VM_TEMP_DIR}/exit_code 2>/dev/null)
109bf215546Sopenharmony_ci    # Force error when the guest script's exit code is not available
110bf215546Sopenharmony_ci    CROSVM_RET=${CROSVM_RET:-1}
111bf215546Sopenharmony_ci}
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci# Show crosvm output on error to help with debugging
114bf215546Sopenharmony_ci[ ${CROSVM_RET} -eq 0 ] || {
115bf215546Sopenharmony_ci    set +x
116bf215546Sopenharmony_ci    echo "Dumping crosvm output.." >&2
117bf215546Sopenharmony_ci    cat ${VM_TEMP_DIR}/crosvm >&2
118bf215546Sopenharmony_ci    set -x
119bf215546Sopenharmony_ci}
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ciexit ${CROSVM_RET}
122