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 void test_struct_ops_load(void)
34 {
35 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
36 	struct struct_ops_module *skel;
37 	struct bpf_map_info info = {};
38 	struct bpf_link *link;
39 	int err;
40 	u32 len;
41 
42 	skel = struct_ops_module__open_opts(&opts);
43 	if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
44 		return;
45 
46 	err = struct_ops_module__load(skel);
47 	if (!ASSERT_OK(err, "struct_ops_module_load"))
48 		goto cleanup;
49 
50 	len = sizeof(info);
51 	err = bpf_map_get_info_by_fd(bpf_map__fd(skel->maps.testmod_1), &info,
52 				     &len);
53 	if (!ASSERT_OK(err, "bpf_map_get_info_by_fd"))
54 		goto cleanup;
55 
56 	link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
57 	ASSERT_OK_PTR(link, "attach_test_mod_1");
58 
59 	/* test_2() will be called from bpf_dummy_reg() in bpf_testmod.c */
60 	ASSERT_EQ(skel->bss->test_2_result, 7, "test_2_result");
61 
62 	bpf_link__destroy(link);
63 
64 	check_map_info(&info);
65 
66 cleanup:
67 	struct_ops_module__destroy(skel);
68 }
69 
70 void serial_test_struct_ops_module(void)
71 {
72 	if (test__start_subtest("test_struct_ops_load"))
73 		test_struct_ops_load();
74 }
75 
76