• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/epoll.h>
4 #include <util/bpf-loader.h>
5 #include <util/evlist.h>
6 #include "tests.h"
7 #include "llvm.h"
8 #include "debug.h"
9 #define NR_ITERS       111
10 
11 #ifdef HAVE_LIBBPF_SUPPORT
12 
epoll_pwait_loop(void)13 static int epoll_pwait_loop(void)
14 {
15 	int i;
16 
17 	/* Should fail NR_ITERS times */
18 	for (i = 0; i < NR_ITERS; i++)
19 		epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
20 	return 0;
21 }
22 
23 static struct {
24 	enum test_llvm__testcase prog_id;
25 	const char *desc;
26 	const char *name;
27 	const char *msg_compile_fail;
28 	const char *msg_load_fail;
29 	int (*target_func)(void);
30 	int expect_result;
31 } bpf_testcase_table[] = {
32 	{
33 		LLVM_TESTCASE_BASE,
34 		"Test basic BPF filtering",
35 		"[basic_bpf_test]",
36 		"fix 'perf test LLVM' first",
37 		"load bpf object failed",
38 		&epoll_pwait_loop,
39 		(NR_ITERS + 1) / 2,
40 	},
41 };
42 
do_test(struct bpf_object * obj,int (* func)(void),int expect)43 static int do_test(struct bpf_object *obj, int (*func)(void),
44 		   int expect)
45 {
46 	struct record_opts opts = {
47 		.target = {
48 			.uid = UINT_MAX,
49 			.uses_mmap = true,
50 		},
51 		.freq	      = 0,
52 		.mmap_pages   = 256,
53 		.default_interval = 1,
54 	};
55 
56 	char pid[16];
57 	char sbuf[STRERR_BUFSIZE];
58 	struct perf_evlist *evlist;
59 	int i, ret = TEST_FAIL, err = 0, count = 0;
60 
61 	struct parse_events_evlist parse_evlist;
62 	struct parse_events_error parse_error;
63 
64 	bzero(&parse_error, sizeof(parse_error));
65 	bzero(&parse_evlist, sizeof(parse_evlist));
66 	parse_evlist.error = &parse_error;
67 	INIT_LIST_HEAD(&parse_evlist.list);
68 
69 	err = parse_events_load_bpf_obj(&parse_evlist, &parse_evlist.list, obj);
70 	if (err || list_empty(&parse_evlist.list)) {
71 		pr_debug("Failed to add events selected by BPF\n");
72 		if (!err)
73 			return TEST_FAIL;
74 	}
75 
76 	snprintf(pid, sizeof(pid), "%d", getpid());
77 	pid[sizeof(pid) - 1] = '\0';
78 	opts.target.tid = opts.target.pid = pid;
79 
80 	/* Instead of perf_evlist__new_default, don't add default events */
81 	evlist = perf_evlist__new();
82 	if (!evlist) {
83 		pr_debug("No ehough memory to create evlist\n");
84 		return TEST_FAIL;
85 	}
86 
87 	err = perf_evlist__create_maps(evlist, &opts.target);
88 	if (err < 0) {
89 		pr_debug("Not enough memory to create thread/cpu maps\n");
90 		goto out_delete_evlist;
91 	}
92 
93 	perf_evlist__splice_list_tail(evlist, &parse_evlist.list);
94 	evlist->nr_groups = parse_evlist.nr_groups;
95 
96 	perf_evlist__config(evlist, &opts);
97 
98 	err = perf_evlist__open(evlist);
99 	if (err < 0) {
100 		pr_debug("perf_evlist__open: %s\n",
101 			 strerror_r(errno, sbuf, sizeof(sbuf)));
102 		goto out_delete_evlist;
103 	}
104 
105 	err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
106 	if (err < 0) {
107 		pr_debug("perf_evlist__mmap: %s\n",
108 			 strerror_r(errno, sbuf, sizeof(sbuf)));
109 		goto out_delete_evlist;
110 	}
111 
112 	perf_evlist__enable(evlist);
113 	(*func)();
114 	perf_evlist__disable(evlist);
115 
116 	for (i = 0; i < evlist->nr_mmaps; i++) {
117 		union perf_event *event;
118 
119 		while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
120 			const u32 type = event->header.type;
121 
122 			if (type == PERF_RECORD_SAMPLE)
123 				count ++;
124 		}
125 	}
126 
127 	if (count != expect)
128 		pr_debug("BPF filter result incorrect\n");
129 
130 	ret = TEST_OK;
131 
132 out_delete_evlist:
133 	perf_evlist__delete(evlist);
134 	return ret;
135 }
136 
137 static struct bpf_object *
prepare_bpf(void * obj_buf,size_t obj_buf_sz,const char * name)138 prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
139 {
140 	struct bpf_object *obj;
141 
142 	obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
143 	if (IS_ERR(obj)) {
144 		pr_debug("Compile BPF program failed.\n");
145 		return NULL;
146 	}
147 	return obj;
148 }
149 
__test__bpf(int idx)150 static int __test__bpf(int idx)
151 {
152 	int ret;
153 	void *obj_buf;
154 	size_t obj_buf_sz;
155 	struct bpf_object *obj;
156 
157 	ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
158 				       bpf_testcase_table[idx].prog_id,
159 				       true);
160 	if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
161 		pr_debug("Unable to get BPF object, %s\n",
162 			 bpf_testcase_table[idx].msg_compile_fail);
163 		if (idx == 0)
164 			return TEST_SKIP;
165 		else
166 			return TEST_FAIL;
167 	}
168 
169 	obj = prepare_bpf(obj_buf, obj_buf_sz,
170 			  bpf_testcase_table[idx].name);
171 	if (!obj) {
172 		ret = TEST_FAIL;
173 		goto out;
174 	}
175 
176 	ret = do_test(obj,
177 		      bpf_testcase_table[idx].target_func,
178 		      bpf_testcase_table[idx].expect_result);
179 out:
180 	free(obj_buf);
181 	bpf__clear();
182 	return ret;
183 }
184 
test__bpf(void)185 int test__bpf(void)
186 {
187 	unsigned int i;
188 	int err;
189 
190 	if (geteuid() != 0) {
191 		pr_debug("Only root can run BPF test\n");
192 		return TEST_SKIP;
193 	}
194 
195 	for (i = 0; i < ARRAY_SIZE(bpf_testcase_table); i++) {
196 		err = __test__bpf(i);
197 
198 		if (err != TEST_OK)
199 			return err;
200 	}
201 
202 	return TEST_OK;
203 }
204 
205 #else
test__bpf(void)206 int test__bpf(void)
207 {
208 	pr_debug("Skip BPF test because BPF support is not compiled\n");
209 	return TEST_SKIP;
210 }
211 #endif
212