diff mbox series

[bpf-next,v2,2/2] selftests/bpf: Add testcases for pinning to errpath

Message ID 1663313820-29918-2-git-send-email-wangyufen@huawei.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [bpf-next,v2,1/2] libbpf: Add pathname_concat() helper | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 4 maintainers not CCed: shuah@kernel.org linux-kselftest@vger.kernel.org deso@posteo.net mykolal@fb.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 93 exceeds 80 columns WARNING: line length of 94 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on s390x with gcc

Commit Message

wangyufen Sept. 16, 2022, 7:37 a.m. UTC
Add testcases for map and prog pin to errpath.

Signed-off-by: Wang Yufen <wangyufen@huawei.com>
---
 tools/testing/selftests/bpf/prog_tests/pinning.c   | 67 ++++++++++++++++++++++
 .../selftests/bpf/progs/test_pinning_path.c        | 19 ++++++
 2 files changed, 86 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/test_pinning_path.c
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/pinning.c b/tools/testing/selftests/bpf/prog_tests/pinning.c
index d95cee5..ab7780f 100644
--- a/tools/testing/selftests/bpf/prog_tests/pinning.c
+++ b/tools/testing/selftests/bpf/prog_tests/pinning.c
@@ -24,6 +24,61 @@  __u32 get_map_id(struct bpf_object *obj, const char *name)
 	return map_info.id;
 }
 
+static void test_pin_path(void)
+{
+	const char *progfile = "./test_pinning_path.bpf.o";
+	const char *progpinpath = "/sys/fs/bpf/test_pinpath";
+	char errpath[PATH_MAX + 1];
+	char command[64];
+	int prog_fd, err;
+	struct bpf_object *obj;
+	__u32 duration = 0;
+
+	/* Use libbpf 1.0 API mode */
+	libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
+
+	err = bpf_prog_test_load(progfile, BPF_PROG_TYPE_SOCK_OPS, &obj,
+				 &prog_fd);
+	CHECK(err, "bpf_prog_test_load", "err %d errno %d\n", err, errno);
+
+	memset(&errpath, 't', PATH_MAX);
+	err = bpf_object__pin_maps(obj, errpath);
+	if (CHECK(err != -ENAMETOOLONG, "pin maps errpath", "err %d errno %d\n", err, errno))
+		goto out;
+
+	err = bpf_object__pin_maps(obj, progpinpath);
+	if (CHECK(err, "pin maps", "err %d errno %d\n", err, errno))
+		goto out;
+
+	err = bpf_object__pin_programs(obj, errpath);
+	if (CHECK(err != -ENAMETOOLONG, "pin progs errpath", "err %d errno %d\n", err, errno))
+		goto out;
+
+	err = bpf_object__pin_programs(obj, progpinpath);
+	if (CHECK(err, "pin prog", "err %d errno %d\n", err, errno))
+		goto out;
+
+	err = bpf_object__unpin_programs(obj, errpath);
+	if (CHECK(err != -ENAMETOOLONG, "pin progs errpath", "err %d errno %d\n", err, errno))
+		goto out;
+
+	err = bpf_object__unpin_programs(obj, progpinpath);
+	if (CHECK(err, "pin prog", "err %d errno %d\n", err, errno))
+		goto out;
+
+	err = bpf_object__unpin_maps(obj, errpath);
+	if (CHECK(err != -ENAMETOOLONG, "pin maps errpath", "err %d errno %d\n", err, errno))
+		goto out;
+
+	err = bpf_object__unpin_maps(obj, progpinpath);
+	if (CHECK(err, "pin maps", "err %d errno %d\n", err, errno))
+		goto out;
+out:
+	bpf_object__close(obj);
+	sprintf(command, "rm -r %s", progpinpath);
+	system(command);
+}
+
 void test_pinning(void)
 {
 	const char *file_invalid = "./test_pinning_invalid.bpf.o";
@@ -32,6 +87,7 @@  void test_pinning(void)
 	const char *nopinpath2 = "/sys/fs/bpf/nopinmap2";
 	const char *custpath = "/sys/fs/bpf/custom";
 	const char *pinpath = "/sys/fs/bpf/pinmap";
+	char errpath[PATH_MAX + 1];
 	const char *file = "./test_pinning.bpf.o";
 	__u32 map_id, map_id2, duration = 0;
 	struct stat statbuf = {};
@@ -206,7 +262,17 @@  void test_pinning(void)
 
 	bpf_object__close(obj);
 
+	/* test auto-pinning at err path with open opt */
+	memset(&errpath, 't', PATH_MAX);
+	opts.pin_root_path = errpath;
+	obj = bpf_object__open_file(file, &opts);
+	if (CHECK_FAIL(libbpf_get_error(obj) != -ENAMETOOLONG)) {
+		obj = NULL;
+		goto out;
+	}
+
 	/* test auto-pinning at custom path with open opt */
+	opts.pin_root_path = custpath;
 	obj = bpf_object__open_file(file, &opts);
 	if (CHECK_FAIL(libbpf_get_error(obj))) {
 		obj = NULL;
@@ -277,4 +343,5 @@  void test_pinning(void)
 	rmdir(custpath);
 	if (obj)
 		bpf_object__close(obj);
+	test_pin_path();
 }
diff --git a/tools/testing/selftests/bpf/progs/test_pinning_path.c b/tools/testing/selftests/bpf/progs/test_pinning_path.c
new file mode 100644
index 0000000..b4e2099
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_pinning_path.c
@@ -0,0 +1,19 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+struct {
+	__uint(type, BPF_MAP_TYPE_SOCKHASH);
+	__uint(max_entries, 64);
+	__type(key, __u32);
+	__type(value, __u64);
+} sock_ops_map SEC(".maps");
+
+SEC("sockops")
+int bpf_sockmap(struct bpf_sock_ops *skops)
+{
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";