xref: /linux/tools/testing/selftests/vsock/vmtest.sh (revision 2ed3ce7efbd2854fb7eb76627ccec92362b2345b)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Copyright (c) 2025 Meta Platforms, Inc. and affiliates
5#
6# Dependencies:
7#		* virtme-ng
8#		* busybox-static (used by virtme-ng)
9#		* qemu	(used by virtme-ng)
10
11readonly SCRIPT_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
12readonly KERNEL_CHECKOUT=$(realpath "${SCRIPT_DIR}"/../../../../)
13
14source "${SCRIPT_DIR}"/../kselftest/ktap_helpers.sh
15
16readonly VSOCK_TEST="${SCRIPT_DIR}"/vsock_test
17readonly TEST_GUEST_PORT=51000
18readonly TEST_HOST_PORT=50000
19readonly TEST_HOST_PORT_LISTENER=50001
20readonly SSH_GUEST_PORT=22
21readonly SSH_HOST_PORT=2222
22readonly VSOCK_CID=1234
23readonly WAIT_PERIOD=3
24readonly WAIT_PERIOD_MAX=60
25readonly WAIT_TOTAL=$(( WAIT_PERIOD * WAIT_PERIOD_MAX ))
26readonly QEMU_PIDFILE=$(mktemp /tmp/qemu_vsock_vmtest_XXXX.pid)
27
28# virtme-ng offers a netdev for ssh when using "--ssh", but we also need a
29# control port forwarded for vsock_test.  Because virtme-ng doesn't support
30# adding an additional port to forward to the device created from "--ssh" and
31# virtme-init mistakenly sets identical IPs to the ssh device and additional
32# devices, we instead opt out of using --ssh, add the device manually, and also
33# add the kernel cmdline options that virtme-init uses to setup the interface.
34readonly QEMU_TEST_PORT_FWD="hostfwd=tcp::${TEST_HOST_PORT}-:${TEST_GUEST_PORT}"
35readonly QEMU_SSH_PORT_FWD="hostfwd=tcp::${SSH_HOST_PORT}-:${SSH_GUEST_PORT}"
36readonly QEMU_OPTS="\
37	 -netdev user,id=n0,${QEMU_TEST_PORT_FWD},${QEMU_SSH_PORT_FWD} \
38	 -device virtio-net-pci,netdev=n0 \
39	 -device vhost-vsock-pci,guest-cid=${VSOCK_CID} \
40	 --pidfile ${QEMU_PIDFILE} \
41"
42readonly KERNEL_CMDLINE="\
43	virtme.dhcp net.ifnames=0 biosdevname=0 \
44	virtme.ssh virtme_ssh_channel=tcp virtme_ssh_user=$USER \
45"
46readonly LOG=$(mktemp /tmp/vsock_vmtest_XXXX.log)
47readonly TEST_NAMES=(vm_server_host_client vm_client_host_server vm_loopback)
48readonly TEST_DESCS=(
49	"Run vsock_test in server mode on the VM and in client mode on the host."
50	"Run vsock_test in client mode on the VM and in server mode on the host."
51	"Run vsock_test using the loopback transport in the VM."
52)
53
54VERBOSE=0
55
56usage() {
57	local name
58	local desc
59	local i
60
61	echo
62	echo "$0 [OPTIONS] [TEST]..."
63	echo "If no TEST argument is given, all tests will be run."
64	echo
65	echo "Options"
66	echo "  -b: build the kernel from the current source tree and use it for guest VMs"
67	echo "  -q: set the path to or name of qemu binary"
68	echo "  -v: verbose output"
69	echo
70	echo "Available tests"
71
72	for ((i = 0; i < ${#TEST_NAMES[@]}; i++)); do
73		name=${TEST_NAMES[${i}]}
74		desc=${TEST_DESCS[${i}]}
75		printf "\t%-35s%-35s\n" "${name}" "${desc}"
76	done
77	echo
78
79	exit 1
80}
81
82die() {
83	echo "$*" >&2
84	exit "${KSFT_FAIL}"
85}
86
87vm_ssh() {
88	ssh -q -o UserKnownHostsFile=/dev/null -p ${SSH_HOST_PORT} localhost "$@"
89	return $?
90}
91
92cleanup() {
93	if [[ -s "${QEMU_PIDFILE}" ]]; then
94		pkill -SIGTERM -F "${QEMU_PIDFILE}" > /dev/null 2>&1
95	fi
96
97	# If failure occurred during or before qemu start up, then we need
98	# to clean this up ourselves.
99	if [[ -e "${QEMU_PIDFILE}" ]]; then
100		rm "${QEMU_PIDFILE}"
101	fi
102}
103
104check_args() {
105	local found
106
107	for arg in "$@"; do
108		found=0
109		for name in "${TEST_NAMES[@]}"; do
110			if [[ "${name}" = "${arg}" ]]; then
111				found=1
112				break
113			fi
114		done
115
116		if [[ "${found}" -eq 0 ]]; then
117			echo "${arg} is not an available test" >&2
118			usage
119		fi
120	done
121
122	for arg in "$@"; do
123		if ! command -v > /dev/null "test_${arg}"; then
124			echo "Test ${arg} not found" >&2
125			usage
126		fi
127	done
128}
129
130check_deps() {
131	for dep in vng ${QEMU} busybox pkill ssh; do
132		if [[ ! -x $(command -v "${dep}") ]]; then
133			echo -e "skip:    dependency ${dep} not found!\n"
134			exit "${KSFT_SKIP}"
135		fi
136	done
137
138	if [[ ! -x $(command -v "${VSOCK_TEST}") ]]; then
139		printf "skip:    %s not found!" "${VSOCK_TEST}"
140		printf " Please build the kselftest vsock target.\n"
141		exit "${KSFT_SKIP}"
142	fi
143}
144
145check_vng() {
146	local tested_versions
147	local version
148	local ok
149
150	tested_versions=("1.33" "1.36")
151	version="$(vng --version)"
152
153	ok=0
154	for tv in "${tested_versions[@]}"; do
155		if [[ "${version}" == *"${tv}"* ]]; then
156			ok=1
157			break
158		fi
159	done
160
161	if [[ ! "${ok}" -eq 1 ]]; then
162		printf "warning: vng version '%s' has not been tested and may " "${version}" >&2
163		printf "not function properly.\n\tThe following versions have been tested: " >&2
164		echo "${tested_versions[@]}" >&2
165	fi
166}
167
168handle_build() {
169	if [[ ! "${BUILD}" -eq 1 ]]; then
170		return
171	fi
172
173	if [[ ! -d "${KERNEL_CHECKOUT}" ]]; then
174		echo "-b requires vmtest.sh called from the kernel source tree" >&2
175		exit 1
176	fi
177
178	pushd "${KERNEL_CHECKOUT}" &>/dev/null
179
180	if ! vng --kconfig --config "${SCRIPT_DIR}"/config; then
181		die "failed to generate .config for kernel source tree (${KERNEL_CHECKOUT})"
182	fi
183
184	if ! make -j$(nproc); then
185		die "failed to build kernel from source tree (${KERNEL_CHECKOUT})"
186	fi
187
188	popd &>/dev/null
189}
190
191vm_start() {
192	local logfile=/dev/null
193	local verbose_opt=""
194	local kernel_opt=""
195	local qemu
196
197	qemu=$(command -v "${QEMU}")
198
199	if [[ "${VERBOSE}" -eq 1 ]]; then
200		verbose_opt="--verbose"
201		logfile=/dev/stdout
202	fi
203
204	if [[ "${BUILD}" -eq 1 ]]; then
205		kernel_opt="${KERNEL_CHECKOUT}"
206	fi
207
208	vng \
209		--run \
210		${kernel_opt} \
211		${verbose_opt} \
212		--qemu-opts="${QEMU_OPTS}" \
213		--qemu="${qemu}" \
214		--user root \
215		--append "${KERNEL_CMDLINE}" \
216		--rw  &> ${logfile} &
217
218	if ! timeout ${WAIT_TOTAL} \
219		bash -c 'while [[ ! -s '"${QEMU_PIDFILE}"' ]]; do sleep 1; done; exit 0'; then
220		die "failed to boot VM"
221	fi
222}
223
224vm_wait_for_ssh() {
225	local i
226
227	i=0
228	while true; do
229		if [[ ${i} -gt ${WAIT_PERIOD_MAX} ]]; then
230			die "Timed out waiting for guest ssh"
231		fi
232		if vm_ssh -- true; then
233			break
234		fi
235		i=$(( i + 1 ))
236		sleep ${WAIT_PERIOD}
237	done
238}
239
240# derived from selftests/net/net_helper.sh
241wait_for_listener()
242{
243	local port=$1
244	local interval=$2
245	local max_intervals=$3
246	local protocol=tcp
247	local pattern
248	local i
249
250	pattern=":$(printf "%04X" "${port}") "
251
252	# for tcp protocol additionally check the socket state
253	[ "${protocol}" = "tcp" ] && pattern="${pattern}0A"
254
255	for i in $(seq "${max_intervals}"); do
256		if awk -v pattern="${pattern}" \
257			'BEGIN {rc=1} $2" "$4 ~ pattern {rc=0} END {exit rc}' \
258			/proc/net/"${protocol}"*; then
259			break
260		fi
261		sleep "${interval}"
262	done
263}
264
265vm_wait_for_listener() {
266	local port=$1
267
268	vm_ssh <<EOF
269$(declare -f wait_for_listener)
270wait_for_listener ${port} ${WAIT_PERIOD} ${WAIT_PERIOD_MAX}
271EOF
272}
273
274host_wait_for_listener() {
275	wait_for_listener "${TEST_HOST_PORT_LISTENER}" "${WAIT_PERIOD}" "${WAIT_PERIOD_MAX}"
276
277}
278
279log() {
280	local redirect
281	local prefix
282
283	if [[ ${VERBOSE} -eq 0 ]]; then
284		redirect=/dev/null
285	else
286		redirect=/dev/stdout
287	fi
288
289	prefix="${LOG_PREFIX:-}"
290
291	if [[ "$#" -eq 0 ]]; then
292		if [[ -n "${prefix}" ]]; then
293			awk -v prefix="${prefix}" '{printf "%s: %s\n", prefix, $0}'
294		else
295			cat
296		fi
297	else
298		if [[ -n "${prefix}" ]]; then
299			echo "${prefix}: " "$@"
300		else
301			echo "$@"
302		fi
303	fi | tee -a "${LOG}" > "${redirect}"
304}
305
306log_host() {
307	LOG_PREFIX=host log "$@"
308}
309
310log_guest() {
311	LOG_PREFIX=guest log "$@"
312}
313
314test_vm_server_host_client() {
315
316	vm_ssh -- "${VSOCK_TEST}" \
317		--mode=server \
318		--control-port="${TEST_GUEST_PORT}" \
319		--peer-cid=2 \
320		2>&1 | log_guest &
321
322	vm_wait_for_listener "${TEST_GUEST_PORT}"
323
324	${VSOCK_TEST} \
325		--mode=client \
326		--control-host=127.0.0.1 \
327		--peer-cid="${VSOCK_CID}" \
328		--control-port="${TEST_HOST_PORT}" 2>&1 | log_host
329
330	return $?
331}
332
333test_vm_client_host_server() {
334
335	${VSOCK_TEST} \
336		--mode "server" \
337		--control-port "${TEST_HOST_PORT_LISTENER}" \
338		--peer-cid "${VSOCK_CID}" 2>&1 | log_host &
339
340	host_wait_for_listener
341
342	vm_ssh -- "${VSOCK_TEST}" \
343		--mode=client \
344		--control-host=10.0.2.2 \
345		--peer-cid=2 \
346		--control-port="${TEST_HOST_PORT_LISTENER}" 2>&1 | log_guest
347
348	return $?
349}
350
351test_vm_loopback() {
352	local port=60000 # non-forwarded local port
353
354	vm_ssh -- "${VSOCK_TEST}" \
355		--mode=server \
356		--control-port="${port}" \
357		--peer-cid=1 2>&1 | log_guest &
358
359	vm_wait_for_listener "${port}"
360
361	vm_ssh -- "${VSOCK_TEST}" \
362		--mode=client \
363		--control-host="127.0.0.1" \
364		--control-port="${port}" \
365		--peer-cid=1 2>&1 | log_guest
366
367	return $?
368}
369
370run_test() {
371	local host_oops_cnt_before
372	local host_warn_cnt_before
373	local vm_oops_cnt_before
374	local vm_warn_cnt_before
375	local host_oops_cnt_after
376	local host_warn_cnt_after
377	local vm_oops_cnt_after
378	local vm_warn_cnt_after
379	local name
380	local rc
381
382	host_oops_cnt_before=$(dmesg | grep -c -i 'Oops')
383	host_warn_cnt_before=$(dmesg --level=warn | grep -c -i 'vsock')
384	vm_oops_cnt_before=$(vm_ssh -- dmesg | grep -c -i 'Oops')
385	vm_warn_cnt_before=$(vm_ssh -- dmesg --level=warn | grep -c -i 'vsock')
386
387	name=$(echo "${1}" | awk '{ print $1 }')
388	eval test_"${name}"
389	rc=$?
390
391	host_oops_cnt_after=$(dmesg | grep -i 'Oops' | wc -l)
392	if [[ ${host_oops_cnt_after} -gt ${host_oops_cnt_before} ]]; then
393		echo "FAIL: kernel oops detected on host" | log_host
394		rc=$KSFT_FAIL
395	fi
396
397	host_warn_cnt_after=$(dmesg --level=warn | grep -c -i 'vsock')
398	if [[ ${host_warn_cnt_after} -gt ${host_warn_cnt_before} ]]; then
399		echo "FAIL: kernel warning detected on host" | log_host
400		rc=$KSFT_FAIL
401	fi
402
403	vm_oops_cnt_after=$(vm_ssh -- dmesg | grep -i 'Oops' | wc -l)
404	if [[ ${vm_oops_cnt_after} -gt ${vm_oops_cnt_before} ]]; then
405		echo "FAIL: kernel oops detected on vm" | log_host
406		rc=$KSFT_FAIL
407	fi
408
409	vm_warn_cnt_after=$(vm_ssh -- dmesg --level=warn | grep -c -i 'vsock')
410	if [[ ${vm_warn_cnt_after} -gt ${vm_warn_cnt_before} ]]; then
411		echo "FAIL: kernel warning detected on vm" | log_host
412		rc=$KSFT_FAIL
413	fi
414
415	return "${rc}"
416}
417
418QEMU="qemu-system-$(uname -m)"
419
420while getopts :hvsq:b o
421do
422	case $o in
423	v) VERBOSE=1;;
424	b) BUILD=1;;
425	q) QEMU=$OPTARG;;
426	h|*) usage;;
427	esac
428done
429shift $((OPTIND-1))
430
431trap cleanup EXIT
432
433if [[ ${#} -eq 0 ]]; then
434	ARGS=("${TEST_NAMES[@]}")
435else
436	ARGS=("$@")
437fi
438
439check_args "${ARGS[@]}"
440check_deps
441check_vng
442handle_build
443
444echo "1..${#ARGS[@]}"
445
446log_host "Booting up VM"
447vm_start
448vm_wait_for_ssh
449log_host "VM booted up"
450
451cnt_pass=0
452cnt_fail=0
453cnt_skip=0
454cnt_total=0
455for arg in "${ARGS[@]}"; do
456	run_test "${arg}"
457	rc=$?
458	if [[ ${rc} -eq $KSFT_PASS ]]; then
459		cnt_pass=$(( cnt_pass + 1 ))
460		echo "ok ${cnt_total} ${arg}"
461	elif [[ ${rc} -eq $KSFT_SKIP ]]; then
462		cnt_skip=$(( cnt_skip + 1 ))
463		echo "ok ${cnt_total} ${arg} # SKIP"
464	elif [[ ${rc} -eq $KSFT_FAIL ]]; then
465		cnt_fail=$(( cnt_fail + 1 ))
466		echo "not ok ${cnt_total} ${arg} # exit=$rc"
467	fi
468	cnt_total=$(( cnt_total + 1 ))
469done
470
471echo "SUMMARY: PASS=${cnt_pass} SKIP=${cnt_skip} FAIL=${cnt_fail}"
472echo "Log: ${LOG}"
473
474if [ $((cnt_pass + cnt_skip)) -eq ${cnt_total} ]; then
475	exit "$KSFT_PASS"
476else
477	exit "$KSFT_FAIL"
478fi
479