xref: /linux/tools/testing/selftests/bpf/prog_tests/fsession_test.c (revision c17ee635fd3a482b2ad2bf5e269755c2eae5f25e)
1f7afef56SMenglong Dong // SPDX-License-Identifier: GPL-2.0
2f7afef56SMenglong Dong /* Copyright (c) 2025 ChinaTelecom */
3f7afef56SMenglong Dong #include <test_progs.h>
4f7afef56SMenglong Dong #include "fsession_test.skel.h"
5f7afef56SMenglong Dong 
6f7afef56SMenglong Dong static int check_result(struct fsession_test *skel)
7f7afef56SMenglong Dong {
8f7afef56SMenglong Dong 	LIBBPF_OPTS(bpf_test_run_opts, topts);
9f7afef56SMenglong Dong 	int err, prog_fd;
10f7afef56SMenglong Dong 
11f7afef56SMenglong Dong 	/* Trigger test function calls */
12f7afef56SMenglong Dong 	prog_fd = bpf_program__fd(skel->progs.test1);
13f7afef56SMenglong Dong 	err = bpf_prog_test_run_opts(prog_fd, &topts);
14f7afef56SMenglong Dong 	if (!ASSERT_OK(err, "test_run_opts err"))
15f7afef56SMenglong Dong 		return err;
16f7afef56SMenglong Dong 	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
17f7afef56SMenglong Dong 		return topts.retval;
18f7afef56SMenglong Dong 
19f7afef56SMenglong Dong 	for (int i = 0; i < sizeof(*skel->bss) / sizeof(__u64); i++) {
20f7afef56SMenglong Dong 		if (!ASSERT_EQ(((__u64 *)skel->bss)[i], 1, "test_result"))
21f7afef56SMenglong Dong 			return -EINVAL;
22f7afef56SMenglong Dong 	}
23f7afef56SMenglong Dong 
24f7afef56SMenglong Dong 	return 0;
25f7afef56SMenglong Dong }
26f7afef56SMenglong Dong 
27f7afef56SMenglong Dong static void test_fsession_basic(void)
28f7afef56SMenglong Dong {
29f7afef56SMenglong Dong 	struct fsession_test *skel = NULL;
30f7afef56SMenglong Dong 	int err;
31f7afef56SMenglong Dong 
32*8798902fSLeon Hwang 	skel = fsession_test__open();
33*8798902fSLeon Hwang 	if (!ASSERT_OK_PTR(skel, "fsession_test__open"))
34*8798902fSLeon Hwang 		return;
35*8798902fSLeon Hwang 
36*8798902fSLeon Hwang 	err = fsession_test__load(skel);
37*8798902fSLeon Hwang 	if (err == -EOPNOTSUPP) {
38*8798902fSLeon Hwang 		test__skip();
39*8798902fSLeon Hwang 		goto cleanup;
40*8798902fSLeon Hwang 	}
41*8798902fSLeon Hwang 	if (!ASSERT_OK(err, "fsession_test__load"))
42f7afef56SMenglong Dong 		goto cleanup;
43f7afef56SMenglong Dong 
44f7afef56SMenglong Dong 	err = fsession_test__attach(skel);
45f7afef56SMenglong Dong 	if (!ASSERT_OK(err, "fsession_attach"))
46f7afef56SMenglong Dong 		goto cleanup;
47f7afef56SMenglong Dong 
48f7afef56SMenglong Dong 	check_result(skel);
49f7afef56SMenglong Dong cleanup:
50f7afef56SMenglong Dong 	fsession_test__destroy(skel);
51f7afef56SMenglong Dong }
52f7afef56SMenglong Dong 
53f7afef56SMenglong Dong static void test_fsession_reattach(void)
54f7afef56SMenglong Dong {
55f7afef56SMenglong Dong 	struct fsession_test *skel = NULL;
56f7afef56SMenglong Dong 	int err;
57f7afef56SMenglong Dong 
58*8798902fSLeon Hwang 	skel = fsession_test__open();
59*8798902fSLeon Hwang 	if (!ASSERT_OK_PTR(skel, "fsession_test__open"))
60*8798902fSLeon Hwang 		return;
61*8798902fSLeon Hwang 
62*8798902fSLeon Hwang 	err = fsession_test__load(skel);
63*8798902fSLeon Hwang 	if (err == -EOPNOTSUPP) {
64*8798902fSLeon Hwang 		test__skip();
65*8798902fSLeon Hwang 		goto cleanup;
66*8798902fSLeon Hwang 	}
67*8798902fSLeon Hwang 	if (!ASSERT_OK(err, "fsession_test__load"))
68f7afef56SMenglong Dong 		goto cleanup;
69f7afef56SMenglong Dong 
70f7afef56SMenglong Dong 	/* first attach */
71f7afef56SMenglong Dong 	err = fsession_test__attach(skel);
72f7afef56SMenglong Dong 	if (!ASSERT_OK(err, "fsession_first_attach"))
73f7afef56SMenglong Dong 		goto cleanup;
74f7afef56SMenglong Dong 
75f7afef56SMenglong Dong 	if (check_result(skel))
76f7afef56SMenglong Dong 		goto cleanup;
77f7afef56SMenglong Dong 
78f7afef56SMenglong Dong 	/* detach */
79f7afef56SMenglong Dong 	fsession_test__detach(skel);
80f7afef56SMenglong Dong 
81f7afef56SMenglong Dong 	/* reset counters */
82f7afef56SMenglong Dong 	memset(skel->bss, 0, sizeof(*skel->bss));
83f7afef56SMenglong Dong 
84f7afef56SMenglong Dong 	/* second attach */
85f7afef56SMenglong Dong 	err = fsession_test__attach(skel);
86f7afef56SMenglong Dong 	if (!ASSERT_OK(err, "fsession_second_attach"))
87f7afef56SMenglong Dong 		goto cleanup;
88f7afef56SMenglong Dong 
89f7afef56SMenglong Dong 	if (check_result(skel))
90f7afef56SMenglong Dong 		goto cleanup;
91f7afef56SMenglong Dong 
92f7afef56SMenglong Dong cleanup:
93f7afef56SMenglong Dong 	fsession_test__destroy(skel);
94f7afef56SMenglong Dong }
95f7afef56SMenglong Dong 
968909b3fbSMenglong Dong static void test_fsession_cookie(void)
978909b3fbSMenglong Dong {
988909b3fbSMenglong Dong 	struct fsession_test *skel = NULL;
998909b3fbSMenglong Dong 	int err;
1008909b3fbSMenglong Dong 
1018909b3fbSMenglong Dong 	skel = fsession_test__open();
1028909b3fbSMenglong Dong 	if (!ASSERT_OK_PTR(skel, "fsession_test__open"))
1038909b3fbSMenglong Dong 		goto cleanup;
1048909b3fbSMenglong Dong 
1058909b3fbSMenglong Dong 	/*
1068909b3fbSMenglong Dong 	 * The test_fsession_basic() will test the session cookie with
1078909b3fbSMenglong Dong 	 * bpf_get_func_ip() case, so we need only check
1088909b3fbSMenglong Dong 	 * the cookie without bpf_get_func_ip() case here
1098909b3fbSMenglong Dong 	 */
1108909b3fbSMenglong Dong 	bpf_program__set_autoload(skel->progs.test6, false);
1118909b3fbSMenglong Dong 
1128909b3fbSMenglong Dong 	err = fsession_test__load(skel);
113*8798902fSLeon Hwang 	if (err == -EOPNOTSUPP) {
114*8798902fSLeon Hwang 		test__skip();
115*8798902fSLeon Hwang 		goto cleanup;
116*8798902fSLeon Hwang 	}
1178909b3fbSMenglong Dong 	if (!ASSERT_OK(err, "fsession_test__load"))
1188909b3fbSMenglong Dong 		goto cleanup;
1198909b3fbSMenglong Dong 
1208909b3fbSMenglong Dong 	err = fsession_test__attach(skel);
1218909b3fbSMenglong Dong 	if (!ASSERT_OK(err, "fsession_attach"))
1228909b3fbSMenglong Dong 		goto cleanup;
1238909b3fbSMenglong Dong 
1248909b3fbSMenglong Dong 	skel->bss->test6_entry_result = 1;
1258909b3fbSMenglong Dong 	skel->bss->test6_exit_result = 1;
1268909b3fbSMenglong Dong 
1278909b3fbSMenglong Dong 	check_result(skel);
1288909b3fbSMenglong Dong cleanup:
1298909b3fbSMenglong Dong 	fsession_test__destroy(skel);
1308909b3fbSMenglong Dong }
1318909b3fbSMenglong Dong 
132f7afef56SMenglong Dong void test_fsession_test(void)
133f7afef56SMenglong Dong {
134f7afef56SMenglong Dong 	if (test__start_subtest("fsession_test"))
135f7afef56SMenglong Dong 		test_fsession_basic();
136f7afef56SMenglong Dong 	if (test__start_subtest("fsession_reattach"))
137f7afef56SMenglong Dong 		test_fsession_reattach();
1388909b3fbSMenglong Dong 	if (test__start_subtest("fsession_cookie"))
1398909b3fbSMenglong Dong 		test_fsession_cookie();
140f7afef56SMenglong Dong }
141