xref: /linux/tools/testing/selftests/bpf/prog_tests/fexit_test.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 #include "fexit_test.lskel.h"
5 #include "fexit_many_args.skel.h"
6 
fexit_test_common(struct fexit_test_lskel * fexit_skel)7 static int fexit_test_common(struct fexit_test_lskel *fexit_skel)
8 {
9 	int err, prog_fd, i;
10 	int link_fd;
11 	__u64 *result;
12 	LIBBPF_OPTS(bpf_test_run_opts, topts);
13 
14 	err = fexit_test_lskel__attach(fexit_skel);
15 	if (!ASSERT_OK(err, "fexit_attach"))
16 		return err;
17 
18 	/* Check that already linked program can't be attached again. */
19 	link_fd = fexit_test_lskel__test1__attach(fexit_skel);
20 	if (!ASSERT_LT(link_fd, 0, "fexit_attach_link"))
21 		return -1;
22 
23 	prog_fd = fexit_skel->progs.test1.prog_fd;
24 	err = bpf_prog_test_run_opts(prog_fd, &topts);
25 	ASSERT_OK(err, "test_run");
26 	ASSERT_EQ(topts.retval, 0, "test_run");
27 
28 	result = (__u64 *)fexit_skel->bss;
29 	for (i = 0; i < sizeof(*fexit_skel->bss) / sizeof(__u64); i++) {
30 		if (!ASSERT_EQ(result[i], 1, "fexit_result"))
31 			return -1;
32 	}
33 
34 	fexit_test_lskel__detach(fexit_skel);
35 
36 	/* zero results for re-attach test */
37 	memset(fexit_skel->bss, 0, sizeof(*fexit_skel->bss));
38 	return 0;
39 }
40 
fexit_test(void)41 static void fexit_test(void)
42 {
43 	struct fexit_test_lskel *fexit_skel = NULL;
44 	int err;
45 
46 	fexit_skel = fexit_test_lskel__open_and_load();
47 	if (!ASSERT_OK_PTR(fexit_skel, "fexit_skel_load"))
48 		goto cleanup;
49 
50 	err = fexit_test_common(fexit_skel);
51 	if (!ASSERT_OK(err, "fexit_first_attach"))
52 		goto cleanup;
53 
54 	err = fexit_test_common(fexit_skel);
55 	ASSERT_OK(err, "fexit_second_attach");
56 
57 cleanup:
58 	fexit_test_lskel__destroy(fexit_skel);
59 }
60 
fexit_many_args(void)61 static void fexit_many_args(void)
62 {
63 	struct fexit_many_args *fexit_skel = NULL;
64 	int err;
65 
66 	fexit_skel = fexit_many_args__open_and_load();
67 	if (!ASSERT_OK_PTR(fexit_skel, "fexit_many_args_skel_load"))
68 		goto cleanup;
69 
70 	err = fexit_many_args__attach(fexit_skel);
71 	if (!ASSERT_OK(err, "fexit_many_args_attach"))
72 		goto cleanup;
73 
74 	ASSERT_OK(trigger_module_test_read(1), "trigger_read");
75 
76 	ASSERT_EQ(fexit_skel->bss->test1_result, 1,
77 		  "fexit_many_args_result1");
78 	ASSERT_EQ(fexit_skel->bss->test2_result, 1,
79 		  "fexit_many_args_result2");
80 	ASSERT_EQ(fexit_skel->bss->test3_result, 1,
81 		  "fexit_many_args_result3");
82 
83 cleanup:
84 	fexit_many_args__destroy(fexit_skel);
85 }
86 
test_fexit_test(void)87 void test_fexit_test(void)
88 {
89 	if (test__start_subtest("fexit"))
90 		fexit_test();
91 	if (test__start_subtest("fexit_many_args"))
92 		fexit_many_args();
93 }
94