xref: /linux/tools/testing/selftests/bpf/test_bpftool_metadata.sh (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4# Kselftest framework requirement - SKIP code is 4.
5ksft_skip=4
6
7BPF_FILE_USED="metadata_used.bpf.o"
8BPF_FILE_UNUSED="metadata_unused.bpf.o"
9
10TESTNAME=bpftool_metadata
11BPF_FS=$(awk '$3 == "bpf" {print $2; exit}' /proc/mounts)
12BPF_DIR=$BPF_FS/test_$TESTNAME
13
14_cleanup()
15{
16	set +e
17	rm -rf $BPF_DIR 2> /dev/null
18}
19
20cleanup_skip()
21{
22	echo "selftests: $TESTNAME [SKIP]"
23	_cleanup
24
25	exit $ksft_skip
26}
27
28cleanup()
29{
30	if [ "$?" = 0 ]; then
31		echo "selftests: $TESTNAME [PASS]"
32	else
33		echo "selftests: $TESTNAME [FAILED]"
34	fi
35	_cleanup
36}
37
38if [ $(id -u) -ne 0 ]; then
39	echo "selftests: $TESTNAME [SKIP] Need root privileges"
40	exit $ksft_skip
41fi
42
43if [ -z "$BPF_FS" ]; then
44	echo "selftests: $TESTNAME [SKIP] Could not run test without bpffs mounted"
45	exit $ksft_skip
46fi
47
48if ! bpftool version > /dev/null 2>&1; then
49	echo "selftests: $TESTNAME [SKIP] Could not run test without bpftool"
50	exit $ksft_skip
51fi
52
53set -e
54
55trap cleanup_skip EXIT
56
57mkdir $BPF_DIR
58
59trap cleanup EXIT
60
61bpftool prog load $BPF_FILE_UNUSED $BPF_DIR/unused
62
63METADATA_PLAIN="$(bpftool prog)"
64echo "$METADATA_PLAIN" | grep 'a = "foo"' > /dev/null
65echo "$METADATA_PLAIN" | grep 'b = 1' > /dev/null
66
67bpftool prog --json | grep '"metadata":{"a":"foo","b":1}' > /dev/null
68
69bpftool map | grep 'metadata.rodata' > /dev/null
70
71rm $BPF_DIR/unused
72
73bpftool prog load $BPF_FILE_USED $BPF_DIR/used
74
75METADATA_PLAIN="$(bpftool prog)"
76echo "$METADATA_PLAIN" | grep 'a = "bar"' > /dev/null
77echo "$METADATA_PLAIN" | grep 'b = 2' > /dev/null
78
79bpftool prog --json | grep '"metadata":{"a":"bar","b":2}' > /dev/null
80
81bpftool map | grep 'metadata.rodata' > /dev/null
82
83rm $BPF_DIR/used
84
85exit 0
86