Skip to content

Commit d95d565

Browse files
Pu LehuiAlexei Starovoitov
authored andcommitted
selftests/bpf: Enable cross platform testing for vmtest
Add support cross platform testing for vmtest. The variable $ARCH in the current script is platform semantics, not kernel semantics. Rename it to $PLATFORM so that we can easily use $ARCH in cross-compilation. And drop `set -u` unbound variable check as we will use CROSS_COMPILE env variable. For now, Using PLATFORM= and CROSS_COMPILE= options will enable cross platform testing: PLATFORM=<platform> CROSS_COMPILE=<toolchain> vmtest.sh -- ./test_progs Tested-by: Eduard Zingerman <[email protected]> Signed-off-by: Pu Lehui <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2294073 commit d95d565

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

tools/testing/selftests/bpf/vmtest.sh

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
#!/bin/bash
22
# SPDX-License-Identifier: GPL-2.0
33

4-
set -u
54
set -e
65

76
# This script currently only works for the following platforms,
87
# as it is based on the VM image used by the BPF CI, which is
98
# available only for these architectures. We can also specify
109
# the local rootfs image generated by the following script:
1110
# https://github.com/libbpf/ci/blob/main/rootfs/mkrootfs_debian.sh
12-
ARCH="$(uname -m)"
13-
case "${ARCH}" in
11+
PLATFORM="${PLATFORM:-$(uname -m)}"
12+
case "${PLATFORM}" in
1413
s390x)
1514
QEMU_BINARY=qemu-system-s390x
1615
QEMU_CONSOLE="ttyS1"
17-
QEMU_FLAGS=(-smp 2)
16+
HOST_FLAGS=(-smp 2 -enable-kvm)
17+
CROSS_FLAGS=(-smp 2)
1818
BZIMAGE="arch/s390/boot/vmlinux"
19+
ARCH="s390"
1920
;;
2021
x86_64)
2122
QEMU_BINARY=qemu-system-x86_64
2223
QEMU_CONSOLE="ttyS0,115200"
23-
QEMU_FLAGS=(-cpu host -smp 8)
24+
HOST_FLAGS=(-cpu host -enable-kvm -smp 8)
25+
CROSS_FLAGS=(-smp 8)
2426
BZIMAGE="arch/x86/boot/bzImage"
27+
ARCH="x86"
2528
;;
2629
aarch64)
2730
QEMU_BINARY=qemu-system-aarch64
2831
QEMU_CONSOLE="ttyAMA0,115200"
29-
QEMU_FLAGS=(-M virt,gic-version=3 -cpu host -smp 8)
32+
HOST_FLAGS=(-M virt,gic-version=3 -cpu host -enable-kvm -smp 8)
33+
CROSS_FLAGS=(-M virt,gic-version=3 -cpu cortex-a76 -smp 8)
3034
BZIMAGE="arch/arm64/boot/Image"
35+
ARCH="arm64"
3136
;;
3237
*)
3338
echo "Unsupported architecture"
@@ -41,7 +46,7 @@ ROOTFS_IMAGE="root.img"
4146
OUTPUT_DIR="$HOME/.bpf_selftests"
4247
KCONFIG_REL_PATHS=("tools/testing/selftests/bpf/config"
4348
"tools/testing/selftests/bpf/config.vm"
44-
"tools/testing/selftests/bpf/config.${ARCH}")
49+
"tools/testing/selftests/bpf/config.${PLATFORM}")
4550
INDEX_URL="https://raw.githubusercontent.com/libbpf/ci/master/INDEX"
4651
NUM_COMPILE_JOBS="$(nproc)"
4752
LOG_FILE_BASE="$(date +"bpf_selftests.%Y-%m-%d_%H-%M-%S")"
@@ -61,6 +66,10 @@ tools/testing/selftests/bpf. e.g:
6166
If no command is specified and a debug shell (-s) is not requested,
6267
"${DEFAULT_COMMAND}" will be run by default.
6368
69+
Using PLATFORM= and CROSS_COMPILE= options will enable cross platform testing:
70+
71+
PLATFORM=<platform> CROSS_COMPILE=<toolchain> $0 -- ./test_progs -t test_lsm
72+
6473
If you build your kernel using KBUILD_OUTPUT= or O= options, these
6574
can be passed as environment variables to the script:
6675
@@ -100,7 +109,7 @@ newest_rootfs_version()
100109
{
101110
{
102111
for file in "${!URLS[@]}"; do
103-
if [[ $file =~ ^"${ARCH}"/libbpf-vmtest-rootfs-(.*)\.tar\.zst$ ]]; then
112+
if [[ $file =~ ^"${PLATFORM}"/libbpf-vmtest-rootfs-(.*)\.tar\.zst$ ]]; then
104113
echo "${BASH_REMATCH[1]}"
105114
fi
106115
done
@@ -112,7 +121,7 @@ download_rootfs()
112121
populate_url_map
113122

114123
local rootfsversion="$(newest_rootfs_version)"
115-
local file="${ARCH}/libbpf-vmtest-rootfs-$rootfsversion.tar.zst"
124+
local file="${PLATFORM}/libbpf-vmtest-rootfs-$rootfsversion.tar.zst"
116125

117126
if [[ ! -v URLS[$file] ]]; then
118127
echo "$file not found" >&2
@@ -253,12 +262,17 @@ EOF
253262
exit 1
254263
fi
255264

265+
if [[ "${PLATFORM}" != "$(uname -m)" ]]; then
266+
QEMU_FLAGS=("${CROSS_FLAGS[@]}")
267+
else
268+
QEMU_FLAGS=("${HOST_FLAGS[@]}")
269+
fi
270+
256271
${QEMU_BINARY} \
257272
-nodefaults \
258273
-display none \
259274
-serial mon:stdio \
260275
"${QEMU_FLAGS[@]}" \
261-
-enable-kvm \
262276
-m 4G \
263277
-drive file="${rootfs_img}",format=raw,index=1,media=disk,if=virtio,cache=none \
264278
-kernel "${kernel_bzimage}" \
@@ -389,14 +403,20 @@ main()
389403

390404
trap 'catch "$?"' EXIT
391405

406+
if [[ "${PLATFORM}" != "$(uname -m)" ]] && [[ -z "${CROSS_COMPILE}" ]]; then
407+
echo "Cross-platform testing needs to specify CROSS_COMPILE"
408+
exit 1
409+
fi
410+
392411
if [[ $# -eq 0 && "${debug_shell}" == "no" ]]; then
393412
echo "No command specified, will run ${DEFAULT_COMMAND} in the vm"
394413
else
395414
command="$@"
396415
fi
397416

398417
local kconfig_file="${OUTPUT_DIR}/latest.config"
399-
local make_command="make -j ${NUM_COMPILE_JOBS} KCONFIG_CONFIG=${kconfig_file}"
418+
local make_command="make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} \
419+
-j ${NUM_COMPILE_JOBS} KCONFIG_CONFIG=${kconfig_file}"
400420

401421
# Figure out where the kernel is being built.
402422
# O takes precedence over KBUILD_OUTPUT.

0 commit comments

Comments
 (0)