6
# Helper to generate CIDs for virtio-vsock based communication with processes
7
# running inside crosvm guests.
9
# A CID is a 32-bit Context Identifier to be assigned to a crosvm instance
10
# and must be unique across the host system. For this purpose, let's take
11
# the least significant 25 bits from CI_JOB_ID as a base and generate a 7-bit
12
# prefix number to handle up to 128 concurrent crosvm instances per job runner.
14
# As a result, the following variables are set:
15
# - VSOCK_CID: the crosvm unique CID to be passed as a run argument
17
# - VSOCK_STDOUT, VSOCK_STDERR: the port numbers the guest should accept
18
# vsock connections on in order to transfer output messages
20
# - VSOCK_TEMP_DIR: the temporary directory path used to pass additional
21
# context data towards the guest
24
[ -n "${CI_JOB_ID}" ] || {
25
echo "Missing or unset CI_JOB_ID env variable" >&2
29
local dir_prefix="/tmp-vsock."
33
while [ ${cid_prefix} -lt 128 ]; do
34
VSOCK_TEMP_DIR=${dir_prefix}${cid_prefix}
35
mkdir "${VSOCK_TEMP_DIR}" >/dev/null 2>&1 && break || unset VSOCK_TEMP_DIR
36
cid_prefix=$((cid_prefix + 1))
39
[ -n "${VSOCK_TEMP_DIR}" ] || return 1
41
VSOCK_CID=$(((CI_JOB_ID & 0x1ffffff) | ((cid_prefix & 0x7f) << 25)))
48
# The dEQP binary needs to run from the directory it's in
49
if [ -n "${1##*.sh}" ] && [ -z "${1##*"deqp"*}" ]; then
50
DEQP_BIN_DIR=$(dirname "$1")
54
set_vsock_context || { echo "Could not generate crosvm vsock CID" >&2; exit 1; }
56
# Ensure cleanup on script exit
57
trap 'exit ${exit_code}' INT TERM
58
trap 'exit_code=$?; [ -z "${CROSVM_PID}${SOCAT_PIDS}" ] || kill ${CROSVM_PID} ${SOCAT_PIDS} >/dev/null 2>&1 || true; rm -rf ${VSOCK_TEMP_DIR}' EXIT
60
# Securely pass the current variables to the crosvm environment
61
echo "Variables passed through:"
62
SCRIPT_DIR=$(readlink -en "${0%/*}")
63
${SCRIPT_DIR}/common/generate-env.sh | tee ${VSOCK_TEMP_DIR}/crosvm-env.sh
65
# Set the crosvm-script as the arguments of the current script
66
echo "$@" > ${VSOCK_TEMP_DIR}/crosvm-script.sh
69
/usr/sbin/iptables-legacy -w -t nat -A POSTROUTING -o eth0 -j MASQUERADE
70
echo 1 > /proc/sys/net/ipv4/ip_forward
72
# Start background processes to receive output from guest
73
socat -u vsock-connect:${VSOCK_CID}:${VSOCK_STDERR},retry=200,interval=0.1 stderr &
75
socat -u vsock-connect:${VSOCK_CID}:${VSOCK_STDOUT},retry=200,interval=0.1 stdout &
76
SOCAT_PIDS="${SOCAT_PIDS} $!"
78
# Prepare to start crosvm
82
CROSVM_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"
83
CROSVM_KERN_ARGS="${CROSVM_KERN_ARGS} init=${SCRIPT_DIR}/crosvm-init.sh -- ${VSOCK_STDOUT} ${VSOCK_STDERR} ${VSOCK_TEMP_DIR}"
85
[ "${CROSVM_GALLIUM_DRIVER}" = "llvmpipe" ] && \
86
CROSVM_LIBGL_ALWAYS_SOFTWARE=true || CROSVM_LIBGL_ALWAYS_SOFTWARE=false
90
# We aren't testing the host driver here, so we don't need to validate NIR on the host
91
NIR_DEBUG="novalidate" \
92
LIBGL_ALWAYS_SOFTWARE=${CROSVM_LIBGL_ALWAYS_SOFTWARE} \
93
GALLIUM_DRIVER=${CROSVM_GALLIUM_DRIVER} \
95
--gpu "${CROSVM_GPU_ARGS}" -m 4096 -c 2 --disable-sandbox \
96
--shared-dir /:my_root:type=fs:writeback=true:timeout=60:cache=always \
97
--host_ip "192.168.30.1" --netmask "255.255.255.0" --mac "AA:BB:CC:00:00:12" \
98
--cid ${VSOCK_CID} -p "${CROSVM_KERN_ARGS}" \
99
/lava-files/${KERNEL_IMAGE_NAME:-bzImage} > ${VSOCK_TEMP_DIR}/crosvm 2>&1 &
101
# Wait for crosvm process to terminate
107
[ ${CROSVM_RET} -eq 0 ] && {
108
# socat background processes terminate gracefully on remote peers exit
111
# The actual return code is the crosvm guest script's exit code
112
CROSVM_RET=$(cat ${VSOCK_TEMP_DIR}/exit_code 2>/dev/null)
113
# Force error when the guest script's exit code is not available
114
CROSVM_RET=${CROSVM_RET:-1}
117
# Show crosvm output on error to help with debugging
118
[ ${CROSVM_RET} -eq 0 ] || {
120
echo "Dumping crosvm output.." >&2
121
cat ${VSOCK_TEMP_DIR}/crosvm >&2