1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
4 #include <time.h>
5 
6 #include "struct_ops_module.skel.h"
7 
8 static void check_map_info(struct bpf_map_info *info)
9 {
10 	struct bpf_btf_info btf_info;
11 	char btf_name[256];
12 	u32 btf_info_len = sizeof(btf_info);
13 	int err, fd;
14 
15 	fd = bpf_btf_get_fd_by_id(info->btf_vmlinux_id);
16 	if (!ASSERT_GE(fd, 0, "get_value_type_btf_obj_fd"))
17 		return;
18 
19 	memset(&btf_info, 0, sizeof(btf_info));
20 	btf_info.name = ptr_to_u64(btf_name);
21 	btf_info.name_len = sizeof(btf_name);
22 	err = bpf_btf_get_info_by_fd(fd, &btf_info, &btf_info_len);
23 	if (!ASSERT_OK(err, "get_value_type_btf_obj_info"))
24 		goto cleanup;
25 
26 	if (!ASSERT_EQ(strcmp(btf_name, "bpf_testmod"), 0, "get_value_type_btf_obj_name"))
27 		goto cleanup;
28 
29 cleanup:
30 	close(fd);
31 }
32 
33 static int attach_ops_and_check(struct struct_ops_module *skel,
34 				struct bpf_map *map,
35 				int expected_test_2_result)
36 {
37 	struct bpf_link *link;
38 
39 	link = bpf_map__attach_struct_ops(map);
40 	ASSERT_OK_PTR(link, "attach_test_mod_1");
41 	if (!link)
42 		return -1;
43 
44 	/* test_{1,2}() would be called from bpf_dummy_reg() in bpf_testmod.c */
45 	ASSERT_EQ(skel->bss->test_1_result, 0xdeadbeef, "test_1_result");
46 	ASSERT_EQ(skel->bss->test_2_result, expected_test_2_result, "test_2_result");
47 
48 	bpf_link__destroy(link);
49 	return 0;
50 }
51 
52 static void test_struct_ops_load(void)
53 {
54 	struct struct_ops_module *skel;
55 	struct bpf_map_info info = {};
56 	int err;
57 	u32 len;
58 
59 	skel = struct_ops_module__open();
60 	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
61 		return;
62 
63 	skel->struct_ops.testmod_1->data = 13;
64 	skel->struct_ops.testmod_1->test_2 = skel->progs.test_3;
65 	/* Since test_2() is not being used, it should be disabled from
66 	 * auto-loading, or it will fail to load.
67 	 */
68 	bpf_program__set_autoload(skel->progs.test_2, false);
69 
70 	err = struct_ops_module__load(skel);
71 	if (!ASSERT_OK(err, "struct_ops_module_load"))
72 		goto cleanup;
73 
74 	len = sizeof(info);
75 	err = bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.testmod_1), &info,
76 				     &len);
77 	if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
78 		goto cleanup;
79 
80 	check_map_info(&info);
81 	/* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c
82 	 *
83 	 * In bpf_testmod.c it will pass 4 and 13 (the value of data) to
84 	 * .test_2.  So, the value of test_2_result should be 20 (4 + 13 +
85 	 * 3).
86 	 */
87 	if (!attach_ops_and_check(skel, skel->maps.testmod_1, 20))
88 		goto cleanup;
89 	if (!attach_ops_and_check(skel, skel->maps.testmod_2, 12))
90 		goto cleanup;
91 
92 cleanup:
93 	struct_ops_module__destroy(skel);
94 }
95 
96 void serial_test_struct_ops_module(void)
97 {
98 	if (test__start_subtest("test_struct_ops_load"))
99 		test_struct_ops_load();
100 }
101 
102