Skip to content

Commit 6f5ca16

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
selftests/bpf: Add cases to test map create failure log
As kernel is able to report log when fail to create map, add test cases to check those logs. Signed-off-by: Leon Hwang <[email protected]>
1 parent 5ada586 commit 6f5ca16

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

tools/testing/selftests/bpf/prog_tests/map_init.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,143 @@ void test_map_init(void)
212212
if (test__start_subtest("pcpu_lru_map_init"))
213213
test_pcpu_lru_map_init();
214214
}
215+
216+
#define BPF_LOG_FIXED 8
217+
218+
static void test_map_create(enum bpf_map_type map_type, const char *map_name,
219+
struct bpf_map_create_opts *opts, const char *exp_msg)
220+
{
221+
char log_buf[128];
222+
int fd;
223+
224+
log_buf[0] = '\0';
225+
opts->log_buf = log_buf;
226+
opts->log_size = sizeof(log_buf);
227+
opts->log_level = BPF_LOG_FIXED;
228+
fd = bpf_map_create(map_type, map_name, 4, 4, 1, opts);
229+
if (!ASSERT_LT(fd, 0, "bpf_map_create")) {
230+
close(fd);
231+
return;
232+
}
233+
234+
ASSERT_STREQ(log_buf, exp_msg, "log_buf");
235+
ASSERT_EQ(opts->log_true_size, strlen(exp_msg) + 1, "log_true_size");
236+
}
237+
238+
static void test_map_create_array(struct bpf_map_create_opts *opts, const char *exp_msg)
239+
{
240+
test_map_create(BPF_MAP_TYPE_ARRAY, "test_map_create", opts, exp_msg);
241+
}
242+
243+
static void test_invalid_vmlinux_value_type_id_struct_ops(void)
244+
{
245+
const char *msg = "btf_vmlinux_value_type_id can only be used with struct_ops maps.\n";
246+
LIBBPF_OPTS(bpf_map_create_opts, opts,
247+
.btf_vmlinux_value_type_id = 1,
248+
);
249+
250+
test_map_create_array(&opts, msg);
251+
}
252+
253+
static void test_invalid_vmlinux_value_type_id_kv_type_id(void)
254+
{
255+
const char *msg = "btf_vmlinux_value_type_id is mutually exclusive with btf_key_type_id and btf_value_type_id.\n";
256+
LIBBPF_OPTS(bpf_map_create_opts, opts,
257+
.btf_vmlinux_value_type_id = 1,
258+
.btf_key_type_id = 1,
259+
);
260+
261+
test_map_create(BPF_MAP_TYPE_STRUCT_OPS, "test_map_create", &opts, msg);
262+
}
263+
264+
static void test_invalid_value_type_id(void)
265+
{
266+
const char *msg = "Invalid btf_value_type_id.\n";
267+
LIBBPF_OPTS(bpf_map_create_opts, opts,
268+
.btf_key_type_id = 1,
269+
);
270+
271+
test_map_create_array(&opts, msg);
272+
}
273+
274+
static void test_invalid_map_extra(void)
275+
{
276+
const char *msg = "Invalid map_extra.\n";
277+
LIBBPF_OPTS(bpf_map_create_opts, opts,
278+
.map_extra = 1,
279+
);
280+
281+
test_map_create_array(&opts, msg);
282+
}
283+
284+
static void test_invalid_numa_node(void)
285+
{
286+
const char *msg = "Invalid numa_node.\n";
287+
LIBBPF_OPTS(bpf_map_create_opts, opts,
288+
.map_flags = BPF_F_NUMA_NODE,
289+
.numa_node = 0xFF,
290+
);
291+
292+
test_map_create_array(&opts, msg);
293+
}
294+
295+
static void test_invalid_map_type(void)
296+
{
297+
const char *msg = "Invalid map_type.\n";
298+
LIBBPF_OPTS(bpf_map_create_opts, opts);
299+
300+
test_map_create(__MAX_BPF_MAP_TYPE, "test_map_create", &opts, msg);
301+
}
302+
303+
static void test_invalid_token_fd(void)
304+
{
305+
const char *msg = "Invalid map_token_fd.\n";
306+
LIBBPF_OPTS(bpf_map_create_opts, opts,
307+
.map_flags = BPF_F_TOKEN_FD,
308+
.token_fd = 0xFF,
309+
);
310+
311+
test_map_create_array(&opts, msg);
312+
}
313+
314+
static void test_invalid_map_name(void)
315+
{
316+
const char *msg = "Invalid map_name.\n";
317+
LIBBPF_OPTS(bpf_map_create_opts, opts);
318+
319+
test_map_create(BPF_MAP_TYPE_ARRAY, "test-!@#", &opts, msg);
320+
}
321+
322+
static void test_invalid_btf_fd(void)
323+
{
324+
const char *msg = "Invalid btf_fd.\n";
325+
LIBBPF_OPTS(bpf_map_create_opts, opts,
326+
.btf_fd = -1,
327+
.btf_key_type_id = 1,
328+
.btf_value_type_id = 1,
329+
);
330+
331+
test_map_create_array(&opts, msg);
332+
}
333+
334+
void test_map_create_failure(void)
335+
{
336+
if (test__start_subtest("invalid_vmlinux_value_type_id_struct_ops"))
337+
test_invalid_vmlinux_value_type_id_struct_ops();
338+
if (test__start_subtest("invalid_vmlinux_value_type_id_kv_type_id"))
339+
test_invalid_vmlinux_value_type_id_kv_type_id();
340+
if (test__start_subtest("invalid_value_type_id"))
341+
test_invalid_value_type_id();
342+
if (test__start_subtest("invalid_map_extra"))
343+
test_invalid_map_extra();
344+
if (test__start_subtest("invalid_numa_node"))
345+
test_invalid_numa_node();
346+
if (test__start_subtest("invalid_map_type"))
347+
test_invalid_map_type();
348+
if (test__start_subtest("invalid_token_fd"))
349+
test_invalid_token_fd();
350+
if (test__start_subtest("invalid_map_name"))
351+
test_invalid_map_name();
352+
if (test__start_subtest("invalid_btf_fd"))
353+
test_invalid_btf_fd();
354+
}

0 commit comments

Comments
 (0)