| # Copyright (C) 1997-2026, 2026 Free Software Foundation, Inc. | |
| # | |
| # This file is part of DejaGnu. | |
| # | |
| # DejaGnu is free software: you can redistribute it and/or modify it | |
| # under the terms of the GNU General Public License as published by | |
| # the Free Software Foundation, either version 3 of the License, or | |
| # (at your option) any later version. | |
| # | |
| # DejaGnu is distributed in the hope that it will be useful, but | |
| # WITHOUT ANY WARRANTY; without even the implied warranty of | |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| # General Public License for more details. | |
| # | |
| # You should have received a copy of the GNU General Public License | |
| # along with DejaGnu. If not, see <http://www.gnu.org/licenses/>. | |
| # Baseboard for runtime testing of BPF programs in the GCC testsuite. | |
| # Uses bpf-vmtest-tool(https://gcc.gnu.org/wiki/BPFRunTimeTests) | |
| # Load procedures from common libraries. | |
| load_lib "standard.exp" | |
| # list of toolchains that are supported on this board. | |
| set_board_info target_install {bpf-unknown-none} | |
| # bpf does not need -lm | |
| set_board_info mathlib "" | |
| #timeout for bpf-vmtest-tool | |
| set timeout 10 | |
| proc find_python3 {} { | |
| global timeout | |
| foreach candidate {python3 python} { | |
| set path [which $candidate] | |
| if { $path ne 0 } { | |
| set status [local_exec "$path -V" "" "" $timeout] | |
| set exit_code [lindex $status 0] | |
| set output [lindex $status 1] | |
| if { $exit_code == 0 } { | |
| set version [lindex [split [string trim $output] " "] 1 ] | |
| verbose -log "Found Python $version at $path" 2 | |
| if { [package vcompare $version 3.9.0] >= 0 } { | |
| verbose -log "Using Python interpreter at $path" 1 | |
| return $path | |
| } else { | |
| verbose -log \ | |
| "Python 3.9 or newer is required; still looking..." 2 | |
| continue | |
| } | |
| } else { | |
| verbose -log \ | |
| "Python $path rejected -V with exit code $exit_code" 2 | |
| continue | |
| } | |
| } else { | |
| verbose -log "\'$candidate\' not found in \$PATH" 2 | |
| } | |
| } | |
| perror "Python 3.9 or higher was not found in \$PATH" | |
| exit 1 | |
| } | |
| if {![info exists PYTHON3]} { | |
| set PYTHON3 [find_python3] | |
| verbose -log "Found Python3 interpreter: $PYTHON3" 1 | |
| } else { | |
| verbose -log "Using Python3 interpreter: $PYTHON3" 1 | |
| } | |
| # Validate required environment variables upfront to fail fast rather | |
| # than letting individual test cases fail | |
| if {![info exists env(VMTEST_DIR)]} { | |
| perror "VMTEST_DIR needs to be set for bpf-vmtest-tool" | |
| exit 1 | |
| } | |
| # Process variables passed from arguments and apply defaults | |
| if {![info exists KERNEL_VERSION]} { | |
| set KERNEL_VERSION 6.15 | |
| verbose -log "Using default kernel version: $KERNEL_VERSION" 1 | |
| } | |
| if {![info exists LOG_LEVEL]} { | |
| set LOG_LEVEL ERROR | |
| verbose -log "Using default log level: $LOG_LEVEL" 1 | |
| } | |
| if {![info exists BPF_VMTEST_PY]} { | |
| global srcdir | |
| set BPF_VMTEST_PY "$srcdir/../../contrib/bpf-vmtest-tool/main.py" | |
| verbose -log "Using default bpf-vmtest-tool location: $BPF_VMTEST_PY" 1 | |
| } | |
| proc bpf-vmtest_compile {source destfile type options} { | |
| global KERNEL_VERSION | |
| global LOG_LEVEL | |
| global PYTHON3 | |
| global BPF_VMTEST_PY | |
| global timeout | |
| set opts "" | |
| foreach i $options { | |
| if {[regexp "^compiler=" $i]} { | |
| regsub "^compiler=" $i "" tmp | |
| if { $tmp eq "" } { | |
| return "bpf-vmtest_compile: No compiler to compile with" | |
| } | |
| set compiler [ lindex [split $tmp " "] 0 ] | |
| set compiler_flags [ lindex [split $tmp " "] 1 ] | |
| append opts " $compiler_flags" | |
| } | |
| if {[regexp "^additional_flags=" $i]} { | |
| regsub "^additional_flags=" $i "" tmp | |
| set tmp [string trim $tmp] | |
| append opts " $tmp" | |
| } | |
| } | |
| if { $type eq "preprocess" } { | |
| append opts " -E" | |
| } elseif { $type eq "assembly" } { | |
| append opts " -S" | |
| } elseif { $type eq "object" } { | |
| append opts " -c" | |
| } elseif { $type eq "executable" } { | |
| # For the executable case, we generate a BPF object. The final | |
| # userland executable (which includes the loader) that is executed | |
| # inside qemu VM is created in the bpf-vmtest_load proc | |
| # by the bpf-vmtest-tool using the kernel-specific libbpf and bpftool. | |
| append opts " -c" | |
| } | |
| #clean up spaces | |
| set opts [string trim $opts] | |
| # compile through the tool to use the kernel-specific vmlinux.h | |
| set args [list "BPF_CFLAGS=$opts" "BPF_CC=$compiler" \ | |
| $PYTHON3 $BPF_VMTEST_PY --log-level $LOG_LEVEL \ | |
| bpf compile $source -k $KERNEL_VERSION -o $destfile ] | |
| set status [local_exec "env $args" "" "" $timeout] | |
| set exit_code [lindex $status 0] | |
| set output [lindex $status 1] | |
| verbose -log "Executed 'env $args', exit_code $exit_code" 1 | |
| if { $exit_code > 0 } { | |
| verbose -log "Couldn't compile $source" 1 | |
| return $output | |
| } | |
| if {$output ne ""} { | |
| verbose -log -- $output 2 | |
| } | |
| return $output | |
| } | |
| proc bpf-vmtest_load {dest prog args} { | |
| global KERNEL_VERSION | |
| global LOG_LEVEL | |
| global PYTHON3 | |
| global BPF_VMTEST_PY | |
| global timeout | |
| set args [list $BPF_VMTEST_PY --log-level $LOG_LEVEL \ | |
| vmtest -k $KERNEL_VERSION --bpf-obj $prog ] | |
| set status [local_exec "$PYTHON3 $args" "" "" $timeout] | |
| set exit_code [lindex $status 0] | |
| set output [lindex $status 1] | |
| verbose -log "Executed '$PYTHON3 $args', exit_code $exit_code" 1 | |
| if { $exit_code > 0 } { | |
| verbose -log "Couldn't execute $prog" 1 | |
| return "unresolved" | |
| } | |
| if {$output ne ""} { | |
| verbose -log -- $output 2 | |
| } | |
| if { $exit_code == 0 } { | |
| return [list "pass" $output] | |
| } else { | |
| return [list "fail" $output] | |
| } | |
| } |