xref: /linux/tools/testing/selftests/bpf/test_kmod.sh (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4# Kselftest framework requirement - SKIP code is 4.
5ksft_skip=4
6
7msg="skip all tests:"
8if [ "$(id -u)" != "0" ]; then
9	echo $msg please run this as root >&2
10	exit $ksft_skip
11fi
12
13if [ "$building_out_of_srctree" ]; then
14	# We are in linux-build/kselftest/bpf
15	OUTPUT=../../
16else
17	# We are in linux/tools/testing/selftests/bpf
18	OUTPUT=../../../../
19fi
20
21test_run()
22{
23	sysctl -w net.core.bpf_jit_enable=$1 2>&1 > /dev/null
24	sysctl -w net.core.bpf_jit_harden=$2 2>&1 > /dev/null
25
26	echo "[ JIT enabled:$1 hardened:$2 ]"
27	dmesg -C
28	if [ -f ${OUTPUT}/lib/test_bpf.ko ]; then
29		insmod ${OUTPUT}/lib/test_bpf.ko 2> /dev/null
30		if [ $? -ne 0 ]; then
31			rc=1
32		fi
33	else
34		# Use modprobe dry run to check for missing test_bpf module
35		if ! /sbin/modprobe -q -n test_bpf; then
36			echo "test_bpf: [SKIP]"
37		elif /sbin/modprobe -q test_bpf; then
38			echo "test_bpf: ok"
39		else
40			echo "test_bpf: [FAIL]"
41			rc=1
42		fi
43	fi
44	rmmod  test_bpf 2> /dev/null
45	dmesg | grep FAIL
46}
47
48test_save()
49{
50	JE=`sysctl -n net.core.bpf_jit_enable`
51	JH=`sysctl -n net.core.bpf_jit_harden`
52}
53
54test_restore()
55{
56	sysctl -w net.core.bpf_jit_enable=$JE 2>&1 > /dev/null
57	sysctl -w net.core.bpf_jit_harden=$JH 2>&1 > /dev/null
58}
59
60rc=0
61test_save
62test_run 0 0
63test_run 1 0
64test_run 1 1
65test_run 1 2
66test_restore
67exit $rc
68