Commit d95d5651 authored by Pu Lehui's avatar Pu Lehui Committed by Alexei Starovoitov

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: default avatarEduard Zingerman <eddyz87@gmail.com>
Signed-off-by: default avatarPu Lehui <pulehui@huawei.com>
Link: https://lore.kernel.org/r/20240905081401.1894789-7-pulehui@huaweicloud.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 2294073d
#!/bin/bash #!/bin/bash
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
set -u
set -e set -e
# This script currently only works for the following platforms, # This script currently only works for the following platforms,
...@@ -9,25 +8,31 @@ set -e ...@@ -9,25 +8,31 @@ set -e
# available only for these architectures. We can also specify # available only for these architectures. We can also specify
# the local rootfs image generated by the following script: # the local rootfs image generated by the following script:
# https://github.com/libbpf/ci/blob/main/rootfs/mkrootfs_debian.sh # https://github.com/libbpf/ci/blob/main/rootfs/mkrootfs_debian.sh
ARCH="$(uname -m)" PLATFORM="${PLATFORM:-$(uname -m)}"
case "${ARCH}" in case "${PLATFORM}" in
s390x) s390x)
QEMU_BINARY=qemu-system-s390x QEMU_BINARY=qemu-system-s390x
QEMU_CONSOLE="ttyS1" QEMU_CONSOLE="ttyS1"
QEMU_FLAGS=(-smp 2) HOST_FLAGS=(-smp 2 -enable-kvm)
CROSS_FLAGS=(-smp 2)
BZIMAGE="arch/s390/boot/vmlinux" BZIMAGE="arch/s390/boot/vmlinux"
ARCH="s390"
;; ;;
x86_64) x86_64)
QEMU_BINARY=qemu-system-x86_64 QEMU_BINARY=qemu-system-x86_64
QEMU_CONSOLE="ttyS0,115200" QEMU_CONSOLE="ttyS0,115200"
QEMU_FLAGS=(-cpu host -smp 8) HOST_FLAGS=(-cpu host -enable-kvm -smp 8)
CROSS_FLAGS=(-smp 8)
BZIMAGE="arch/x86/boot/bzImage" BZIMAGE="arch/x86/boot/bzImage"
ARCH="x86"
;; ;;
aarch64) aarch64)
QEMU_BINARY=qemu-system-aarch64 QEMU_BINARY=qemu-system-aarch64
QEMU_CONSOLE="ttyAMA0,115200" QEMU_CONSOLE="ttyAMA0,115200"
QEMU_FLAGS=(-M virt,gic-version=3 -cpu host -smp 8) HOST_FLAGS=(-M virt,gic-version=3 -cpu host -enable-kvm -smp 8)
CROSS_FLAGS=(-M virt,gic-version=3 -cpu cortex-a76 -smp 8)
BZIMAGE="arch/arm64/boot/Image" BZIMAGE="arch/arm64/boot/Image"
ARCH="arm64"
;; ;;
*) *)
echo "Unsupported architecture" echo "Unsupported architecture"
...@@ -41,7 +46,7 @@ ROOTFS_IMAGE="root.img" ...@@ -41,7 +46,7 @@ ROOTFS_IMAGE="root.img"
OUTPUT_DIR="$HOME/.bpf_selftests" OUTPUT_DIR="$HOME/.bpf_selftests"
KCONFIG_REL_PATHS=("tools/testing/selftests/bpf/config" KCONFIG_REL_PATHS=("tools/testing/selftests/bpf/config"
"tools/testing/selftests/bpf/config.vm" "tools/testing/selftests/bpf/config.vm"
"tools/testing/selftests/bpf/config.${ARCH}") "tools/testing/selftests/bpf/config.${PLATFORM}")
INDEX_URL="https://raw.githubusercontent.com/libbpf/ci/master/INDEX" INDEX_URL="https://raw.githubusercontent.com/libbpf/ci/master/INDEX"
NUM_COMPILE_JOBS="$(nproc)" NUM_COMPILE_JOBS="$(nproc)"
LOG_FILE_BASE="$(date +"bpf_selftests.%Y-%m-%d_%H-%M-%S")" LOG_FILE_BASE="$(date +"bpf_selftests.%Y-%m-%d_%H-%M-%S")"
...@@ -61,6 +66,10 @@ tools/testing/selftests/bpf. e.g: ...@@ -61,6 +66,10 @@ tools/testing/selftests/bpf. e.g:
If no command is specified and a debug shell (-s) is not requested, If no command is specified and a debug shell (-s) is not requested,
"${DEFAULT_COMMAND}" will be run by default. "${DEFAULT_COMMAND}" will be run by default.
Using PLATFORM= and CROSS_COMPILE= options will enable cross platform testing:
PLATFORM=<platform> CROSS_COMPILE=<toolchain> $0 -- ./test_progs -t test_lsm
If you build your kernel using KBUILD_OUTPUT= or O= options, these If you build your kernel using KBUILD_OUTPUT= or O= options, these
can be passed as environment variables to the script: can be passed as environment variables to the script:
...@@ -100,7 +109,7 @@ newest_rootfs_version() ...@@ -100,7 +109,7 @@ newest_rootfs_version()
{ {
{ {
for file in "${!URLS[@]}"; do for file in "${!URLS[@]}"; do
if [[ $file =~ ^"${ARCH}"/libbpf-vmtest-rootfs-(.*)\.tar\.zst$ ]]; then if [[ $file =~ ^"${PLATFORM}"/libbpf-vmtest-rootfs-(.*)\.tar\.zst$ ]]; then
echo "${BASH_REMATCH[1]}" echo "${BASH_REMATCH[1]}"
fi fi
done done
...@@ -112,7 +121,7 @@ download_rootfs() ...@@ -112,7 +121,7 @@ download_rootfs()
populate_url_map populate_url_map
local rootfsversion="$(newest_rootfs_version)" local rootfsversion="$(newest_rootfs_version)"
local file="${ARCH}/libbpf-vmtest-rootfs-$rootfsversion.tar.zst" local file="${PLATFORM}/libbpf-vmtest-rootfs-$rootfsversion.tar.zst"
if [[ ! -v URLS[$file] ]]; then if [[ ! -v URLS[$file] ]]; then
echo "$file not found" >&2 echo "$file not found" >&2
...@@ -253,12 +262,17 @@ EOF ...@@ -253,12 +262,17 @@ EOF
exit 1 exit 1
fi fi
if [[ "${PLATFORM}" != "$(uname -m)" ]]; then
QEMU_FLAGS=("${CROSS_FLAGS[@]}")
else
QEMU_FLAGS=("${HOST_FLAGS[@]}")
fi
${QEMU_BINARY} \ ${QEMU_BINARY} \
-nodefaults \ -nodefaults \
-display none \ -display none \
-serial mon:stdio \ -serial mon:stdio \
"${QEMU_FLAGS[@]}" \ "${QEMU_FLAGS[@]}" \
-enable-kvm \
-m 4G \ -m 4G \
-drive file="${rootfs_img}",format=raw,index=1,media=disk,if=virtio,cache=none \ -drive file="${rootfs_img}",format=raw,index=1,media=disk,if=virtio,cache=none \
-kernel "${kernel_bzimage}" \ -kernel "${kernel_bzimage}" \
...@@ -389,6 +403,11 @@ main() ...@@ -389,6 +403,11 @@ main()
trap 'catch "$?"' EXIT trap 'catch "$?"' EXIT
if [[ "${PLATFORM}" != "$(uname -m)" ]] && [[ -z "${CROSS_COMPILE}" ]]; then
echo "Cross-platform testing needs to specify CROSS_COMPILE"
exit 1
fi
if [[ $# -eq 0 && "${debug_shell}" == "no" ]]; then if [[ $# -eq 0 && "${debug_shell}" == "no" ]]; then
echo "No command specified, will run ${DEFAULT_COMMAND} in the vm" echo "No command specified, will run ${DEFAULT_COMMAND} in the vm"
else else
...@@ -396,7 +415,8 @@ main() ...@@ -396,7 +415,8 @@ main()
fi fi
local kconfig_file="${OUTPUT_DIR}/latest.config" local kconfig_file="${OUTPUT_DIR}/latest.config"
local make_command="make -j ${NUM_COMPILE_JOBS} KCONFIG_CONFIG=${kconfig_file}" local make_command="make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} \
-j ${NUM_COMPILE_JOBS} KCONFIG_CONFIG=${kconfig_file}"
# Figure out where the kernel is being built. # Figure out where the kernel is being built.
# O takes precedence over KBUILD_OUTPUT. # O takes precedence over KBUILD_OUTPUT.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment