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