Path: blob/master/tools/testing/selftests/hid/vmtest.sh
29524 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.02#3# Copyright (c) 2025 Red Hat4# Copyright (c) 2025 Meta Platforms, Inc. and affiliates5#6# Dependencies:7# * virtme-ng8# * busybox-static (used by virtme-ng)9# * qemu (used by virtme-ng)1011readonly SCRIPT_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"12readonly KERNEL_CHECKOUT=$(realpath "${SCRIPT_DIR}"/../../../../)1314source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh1516readonly HID_BPF_TEST="${SCRIPT_DIR}"/hid_bpf17readonly HIDRAW_TEST="${SCRIPT_DIR}"/hidraw18readonly HID_BPF_PROGS="${KERNEL_CHECKOUT}/drivers/hid/bpf/progs"19readonly SSH_GUEST_PORT=2220readonly WAIT_PERIOD=321readonly WAIT_PERIOD_MAX=6022readonly WAIT_TOTAL=$(( WAIT_PERIOD * WAIT_PERIOD_MAX ))23readonly QEMU_PIDFILE=$(mktemp /tmp/qemu_hid_vmtest_XXXX.pid)2425readonly QEMU_OPTS="\26--pidfile ${QEMU_PIDFILE} \27"28readonly KERNEL_CMDLINE=""29readonly LOG=$(mktemp /tmp/hid_vmtest_XXXX.log)30readonly TEST_NAMES=(vm_hid_bpf vm_hidraw vm_pytest)31readonly TEST_DESCS=(32"Run hid_bpf tests in the VM."33"Run hidraw tests in the VM."34"Run the hid-tools test-suite in the VM."35)3637VERBOSE=038SHELL_MODE=039BUILD_HOST=""40BUILD_HOST_PODMAN_CONTAINER_NAME=""4142usage() {43local name44local desc45local i4647echo48echo "$0 [OPTIONS] [TEST]... [-- tests-args]"49echo "If no TEST argument is given, all tests will be run."50echo51echo "Options"52echo " -b: build the kernel from the current source tree and use it for guest VMs"53echo " -H: hostname for remote build host (used with -b)"54echo " -p: podman container name for remote build host (used with -b)"55echo " Example: -H beefyserver -p vng"56echo " -q: set the path to or name of qemu binary"57echo " -s: start a shell in the VM instead of running tests"58echo " -v: more verbose output (can be repeated multiple times)"59echo60echo "Available tests"6162for ((i = 0; i < ${#TEST_NAMES[@]}; i++)); do63name=${TEST_NAMES[${i}]}64desc=${TEST_DESCS[${i}]}65printf "\t%-35s%-35s\n" "${name}" "${desc}"66done67echo6869exit 170}7172die() {73echo "$*" >&274exit "${KSFT_FAIL}"75}7677vm_ssh() {78# vng --ssh-client keeps shouting "Warning: Permanently added 'virtme-ng%22'79# (ED25519) to the list of known hosts.",80# So replace the command with what's actually called and add the "-q" option81stdbuf -oL ssh -q \82-F ${HOME}/.cache/virtme-ng/.ssh/virtme-ng-ssh.conf \83-l root virtme-ng%${SSH_GUEST_PORT} \84"$@"85return $?86}8788cleanup() {89if [[ -s "${QEMU_PIDFILE}" ]]; then90pkill -SIGTERM -F "${QEMU_PIDFILE}" > /dev/null 2>&191fi9293# If failure occurred during or before qemu start up, then we need94# to clean this up ourselves.95if [[ -e "${QEMU_PIDFILE}" ]]; then96rm "${QEMU_PIDFILE}"97fi98}99100check_args() {101local found102103for arg in "$@"; do104found=0105for name in "${TEST_NAMES[@]}"; do106if [[ "${name}" = "${arg}" ]]; then107found=1108break109fi110done111112if [[ "${found}" -eq 0 ]]; then113echo "${arg} is not an available test" >&2114usage115fi116done117118for arg in "$@"; do119if ! command -v > /dev/null "test_${arg}"; then120echo "Test ${arg} not found" >&2121usage122fi123done124}125126check_deps() {127for dep in vng ${QEMU} busybox pkill ssh pytest; do128if [[ ! -x $(command -v "${dep}") ]]; then129echo -e "skip: dependency ${dep} not found!\n"130exit "${KSFT_SKIP}"131fi132done133134if [[ ! -x $(command -v "${HID_BPF_TEST}") ]]; then135printf "skip: %s not found!" "${HID_BPF_TEST}"136printf " Please build the kselftest hid_bpf target.\n"137exit "${KSFT_SKIP}"138fi139140if [[ ! -x $(command -v "${HIDRAW_TEST}") ]]; then141printf "skip: %s not found!" "${HIDRAW_TEST}"142printf " Please build the kselftest hidraw target.\n"143exit "${KSFT_SKIP}"144fi145}146147check_vng() {148local tested_versions149local version150local ok151152tested_versions=("1.36" "1.37")153version="$(vng --version)"154155ok=0156for tv in "${tested_versions[@]}"; do157if [[ "${version}" == *"${tv}"* ]]; then158ok=1159break160fi161done162163if [[ ! "${ok}" -eq 1 ]]; then164printf "warning: vng version '%s' has not been tested and may " "${version}" >&2165printf "not function properly.\n\tThe following versions have been tested: " >&2166echo "${tested_versions[@]}" >&2167fi168}169170handle_build() {171if [[ ! "${BUILD}" -eq 1 ]]; then172return173fi174175if [[ ! -d "${KERNEL_CHECKOUT}" ]]; then176echo "-b requires vmtest.sh called from the kernel source tree" >&2177exit 1178fi179180pushd "${KERNEL_CHECKOUT}" &>/dev/null181182if ! vng --kconfig --config "${SCRIPT_DIR}"/config; then183die "failed to generate .config for kernel source tree (${KERNEL_CHECKOUT})"184fi185186local vng_args=("-v" "--config" "${SCRIPT_DIR}/config" "--build")187188if [[ -n "${BUILD_HOST}" ]]; then189vng_args+=("--build-host" "${BUILD_HOST}")190fi191192if [[ -n "${BUILD_HOST_PODMAN_CONTAINER_NAME}" ]]; then193vng_args+=("--build-host-exec-prefix" \194"podman exec -ti ${BUILD_HOST_PODMAN_CONTAINER_NAME}")195fi196197if ! vng "${vng_args[@]}"; then198die "failed to build kernel from source tree (${KERNEL_CHECKOUT})"199fi200201if ! make -j$(nproc) -C "${HID_BPF_PROGS}"; then202die "failed to build HID bpf objects from source tree (${HID_BPF_PROGS})"203fi204205if ! make -j$(nproc) -C "${SCRIPT_DIR}"; then206die "failed to build HID selftests from source tree (${SCRIPT_DIR})"207fi208209popd &>/dev/null210}211212vm_start() {213local logfile=/dev/null214local verbose_opt=""215local kernel_opt=""216local qemu217218qemu=$(command -v "${QEMU}")219220if [[ "${VERBOSE}" -eq 2 ]]; then221verbose_opt="--verbose"222logfile=/dev/stdout223fi224225# If we are running from within the kernel source tree, use the kernel source tree226# as the kernel to boot, otherwise use the currently running kernel.227if [[ "$(realpath "$(pwd)")" == "${KERNEL_CHECKOUT}"* ]]; then228kernel_opt="${KERNEL_CHECKOUT}"229fi230231vng \232--run \233${kernel_opt} \234${verbose_opt} \235--qemu-opts="${QEMU_OPTS}" \236--qemu="${qemu}" \237--user root \238--append "${KERNEL_CMDLINE}" \239--ssh "${SSH_GUEST_PORT}" \240--rw &> ${logfile} &241242local vng_pid=$!243local elapsed=0244245while [[ ! -s "${QEMU_PIDFILE}" ]]; do246if ! kill -0 "${vng_pid}" 2>/dev/null; then247echo "vng process (PID ${vng_pid}) exited early, check logs for details" >&2248die "failed to boot VM"249fi250251if [[ ${elapsed} -ge ${WAIT_TOTAL} ]]; then252echo "Timed out after ${WAIT_TOTAL} seconds waiting for VM to boot" >&2253die "failed to boot VM"254fi255256sleep 1257elapsed=$((elapsed + 1))258done259}260261vm_wait_for_ssh() {262local i263264i=0265while true; do266if [[ ${i} -gt ${WAIT_PERIOD_MAX} ]]; then267die "Timed out waiting for guest ssh"268fi269if vm_ssh -- true; then270break271fi272i=$(( i + 1 ))273sleep ${WAIT_PERIOD}274done275}276277vm_mount_bpffs() {278vm_ssh -- mount bpffs -t bpf /sys/fs/bpf279}280281__log_stdin() {282stdbuf -oL awk '{ printf "%s:\t%s\n","'"${prefix}"'", $0; fflush() }'283}284285__log_args() {286echo "$*" | awk '{ printf "%s:\t%s\n","'"${prefix}"'", $0 }'287}288289log() {290local verbose="$1"291shift292293local prefix="$1"294295shift296local redirect=297if [[ ${verbose} -le 0 ]]; then298redirect=/dev/null299else300redirect=/dev/stdout301fi302303if [[ "$#" -eq 0 ]]; then304__log_stdin | tee -a "${LOG}" > ${redirect}305else306__log_args "$@" | tee -a "${LOG}" > ${redirect}307fi308}309310log_setup() {311log $((VERBOSE-1)) "setup" "$@"312}313314log_host() {315local testname=$1316317shift318log $((VERBOSE-1)) "test:${testname}:host" "$@"319}320321log_guest() {322local testname=$1323324shift325log ${VERBOSE} "# test:${testname}" "$@"326}327328test_vm_hid_bpf() {329local testname="${FUNCNAME[0]#test_}"330331vm_ssh -- "${HID_BPF_TEST}" \3322>&1 | log_guest "${testname}"333334return ${PIPESTATUS[0]}335}336337test_vm_hidraw() {338local testname="${FUNCNAME[0]#test_}"339340vm_ssh -- "${HIDRAW_TEST}" \3412>&1 | log_guest "${testname}"342343return ${PIPESTATUS[0]}344}345346test_vm_pytest() {347local testname="${FUNCNAME[0]#test_}"348349shift350351vm_ssh -- pytest ${SCRIPT_DIR}/tests --color=yes "$@" \3522>&1 | log_guest "${testname}"353354return ${PIPESTATUS[0]}355}356357run_test() {358local vm_oops_cnt_before359local vm_warn_cnt_before360local vm_oops_cnt_after361local vm_warn_cnt_after362local name363local rc364365vm_oops_cnt_before=$(vm_ssh -- dmesg | grep -c -i 'Oops')366vm_error_cnt_before=$(vm_ssh -- dmesg --level=err | wc -l)367368name=$(echo "${1}" | awk '{ print $1 }')369eval test_"${name}" "$@"370rc=$?371372vm_oops_cnt_after=$(vm_ssh -- dmesg | grep -i 'Oops' | wc -l)373if [[ ${vm_oops_cnt_after} -gt ${vm_oops_cnt_before} ]]; then374echo "FAIL: kernel oops detected on vm" | log_host "${name}"375rc=$KSFT_FAIL376fi377378vm_error_cnt_after=$(vm_ssh -- dmesg --level=err | wc -l)379if [[ ${vm_error_cnt_after} -gt ${vm_error_cnt_before} ]]; then380echo "FAIL: kernel error detected on vm" | log_host "${name}"381vm_ssh -- dmesg --level=err | log_host "${name}"382rc=$KSFT_FAIL383fi384385return "${rc}"386}387388QEMU="qemu-system-$(uname -m)"389390while getopts :hvsbq:H:p: o391do392case $o in393v) VERBOSE=$((VERBOSE+1));;394s) SHELL_MODE=1;;395b) BUILD=1;;396q) QEMU=$OPTARG;;397H) BUILD_HOST=$OPTARG;;398p) BUILD_HOST_PODMAN_CONTAINER_NAME=$OPTARG;;399h|*) usage;;400esac401done402shift $((OPTIND-1))403404trap cleanup EXIT405406PARAMS=""407408if [[ ${#} -eq 0 ]]; then409ARGS=("${TEST_NAMES[@]}")410else411ARGS=()412COUNT=0413for arg in $@; do414COUNT=$((COUNT+1))415if [[ x"$arg" == x"--" ]]; then416break417fi418ARGS+=($arg)419done420shift $COUNT421PARAMS="$@"422fi423424if [[ "${SHELL_MODE}" -eq 0 ]]; then425check_args "${ARGS[@]}"426echo "1..${#ARGS[@]}"427fi428check_deps429check_vng430handle_build431432log_setup "Booting up VM"433vm_start434vm_wait_for_ssh435vm_mount_bpffs436log_setup "VM booted up"437438if [[ "${SHELL_MODE}" -eq 1 ]]; then439log_setup "Starting interactive shell in VM"440echo "Starting shell in VM. Use 'exit' to quit and shutdown the VM."441CURRENT_DIR="$(pwd)"442vm_ssh -t -- "cd '${CURRENT_DIR}' && exec bash -l"443exit "$KSFT_PASS"444fi445446cnt_pass=0447cnt_fail=0448cnt_skip=0449cnt_total=0450for arg in "${ARGS[@]}"; do451run_test "${arg}" "${PARAMS}"452rc=$?453if [[ ${rc} -eq $KSFT_PASS ]]; then454cnt_pass=$(( cnt_pass + 1 ))455echo "ok ${cnt_total} ${arg}"456elif [[ ${rc} -eq $KSFT_SKIP ]]; then457cnt_skip=$(( cnt_skip + 1 ))458echo "ok ${cnt_total} ${arg} # SKIP"459elif [[ ${rc} -eq $KSFT_FAIL ]]; then460cnt_fail=$(( cnt_fail + 1 ))461echo "not ok ${cnt_total} ${arg} # exit=$rc"462fi463cnt_total=$(( cnt_total + 1 ))464done465466echo "SUMMARY: PASS=${cnt_pass} SKIP=${cnt_skip} FAIL=${cnt_fail}"467echo "Log: ${LOG}"468469if [ $((cnt_pass + cnt_skip)) -eq ${cnt_total} ]; then470exit "$KSFT_PASS"471else472exit "$KSFT_FAIL"473fi474475476