xref: /linux/tools/testing/selftests/bpf/prog_tests/test_bpf_ma.c (revision e6a901a00822659181c93c86d8bbc2a17779fddc)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2023. Huawei Technologies Co., Ltd */
3 #define _GNU_SOURCE
4 #include <sched.h>
5 #include <pthread.h>
6 #include <stdbool.h>
7 #include <bpf/btf.h>
8 #include <test_progs.h>
9 
10 #include "test_bpf_ma.skel.h"
11 
12 static void do_bpf_ma_test(const char *name)
13 {
14 	struct test_bpf_ma *skel;
15 	struct bpf_program *prog;
16 	struct btf *btf;
17 	int i, err, id;
18 	char tname[32];
19 
20 	skel = test_bpf_ma__open();
21 	if (!ASSERT_OK_PTR(skel, "open"))
22 		return;
23 
24 	btf = bpf_object__btf(skel->obj);
25 	if (!ASSERT_OK_PTR(btf, "btf"))
26 		goto out;
27 
28 	for (i = 0; i < ARRAY_SIZE(skel->rodata->data_sizes); i++) {
29 		snprintf(tname, sizeof(tname), "bin_data_%u", skel->rodata->data_sizes[i]);
30 		id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
31 		if (!ASSERT_GT(id, 0, tname))
32 			goto out;
33 		skel->rodata->data_btf_ids[i] = id;
34 	}
35 
36 	for (i = 0; i < ARRAY_SIZE(skel->rodata->percpu_data_sizes); i++) {
37 		snprintf(tname, sizeof(tname), "percpu_bin_data_%u", skel->rodata->percpu_data_sizes[i]);
38 		id = btf__find_by_name_kind(btf, tname, BTF_KIND_STRUCT);
39 		if (!ASSERT_GT(id, 0, tname))
40 			goto out;
41 		skel->rodata->percpu_data_btf_ids[i] = id;
42 	}
43 
44 	prog = bpf_object__find_program_by_name(skel->obj, name);
45 	if (!ASSERT_OK_PTR(prog, "invalid prog name"))
46 		goto out;
47 	bpf_program__set_autoload(prog, true);
48 
49 	err = test_bpf_ma__load(skel);
50 	if (!ASSERT_OK(err, "load"))
51 		goto out;
52 
53 	err = test_bpf_ma__attach(skel);
54 	if (!ASSERT_OK(err, "attach"))
55 		goto out;
56 
57 	skel->bss->pid = getpid();
58 	usleep(1);
59 	ASSERT_OK(skel->bss->err, "test error");
60 out:
61 	test_bpf_ma__destroy(skel);
62 }
63 
64 void test_test_bpf_ma(void)
65 {
66 	if (test__start_subtest("batch_alloc_free"))
67 		do_bpf_ma_test("test_batch_alloc_free");
68 	if (test__start_subtest("free_through_map_free"))
69 		do_bpf_ma_test("test_free_through_map_free");
70 	if (test__start_subtest("batch_percpu_alloc_free"))
71 		do_bpf_ma_test("test_batch_percpu_alloc_free");
72 	if (test__start_subtest("percpu_free_through_map_free"))
73 		do_bpf_ma_test("test_percpu_free_through_map_free");
74 }
75