Skip to content

Commit 05db443

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
selftests/bpf: Add tests to verify map create failure log
Add tests to verify that the kernel reports the expected error messages when map creation fails. Signed-off-by: Leon Hwang <[email protected]>
1 parent 6e4a3f1 commit 05db443

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

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

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,171 @@ 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+
const int key_size = 4, value_size = 4, max_entries = 1;
222+
char log_buf[128];
223+
int fd;
224+
LIBBPF_OPTS(bpf_log_opts, log_opts);
225+
226+
log_buf[0] = '\0';
227+
log_opts.log_buf = log_buf;
228+
log_opts.log_size = sizeof(log_buf);
229+
log_opts.log_level = BPF_LOG_FIXED;
230+
opts->log_opts = &log_opts;
231+
fd = bpf_map_create(map_type, map_name, key_size, value_size, max_entries, opts);
232+
if (!ASSERT_LT(fd, 0, "bpf_map_create")) {
233+
close(fd);
234+
return;
235+
}
236+
237+
ASSERT_STREQ(log_buf, exp_msg, "log_buf");
238+
ASSERT_EQ(log_opts.log_true_size, strlen(exp_msg) + 1, "log_true_size");
239+
}
240+
241+
static void test_map_create_array(struct bpf_map_create_opts *opts, const char *exp_msg)
242+
{
243+
test_map_create(BPF_MAP_TYPE_ARRAY, "test_map_create", opts, exp_msg);
244+
}
245+
246+
static void test_invalid_vmlinux_value_type_id_struct_ops(void)
247+
{
248+
const char *msg = "btf_vmlinux_value_type_id can only be used with struct_ops maps.\n";
249+
LIBBPF_OPTS(bpf_map_create_opts, opts,
250+
.btf_vmlinux_value_type_id = 1,
251+
);
252+
253+
test_map_create_array(&opts, msg);
254+
}
255+
256+
static void test_invalid_vmlinux_value_type_id_kv_type_id(void)
257+
{
258+
const char *msg = "btf_vmlinux_value_type_id is mutually exclusive with btf_key_type_id and btf_value_type_id.\n";
259+
LIBBPF_OPTS(bpf_map_create_opts, opts,
260+
.btf_vmlinux_value_type_id = 1,
261+
.btf_key_type_id = 1,
262+
);
263+
264+
test_map_create(BPF_MAP_TYPE_STRUCT_OPS, "test_map_create", &opts, msg);
265+
}
266+
267+
static void test_invalid_value_type_id(void)
268+
{
269+
const char *msg = "Invalid btf_value_type_id.\n";
270+
LIBBPF_OPTS(bpf_map_create_opts, opts,
271+
.btf_key_type_id = 1,
272+
);
273+
274+
test_map_create_array(&opts, msg);
275+
}
276+
277+
static void test_invalid_map_extra(void)
278+
{
279+
const char *msg = "Invalid map_extra.\n";
280+
LIBBPF_OPTS(bpf_map_create_opts, opts,
281+
.map_extra = 1,
282+
);
283+
284+
test_map_create_array(&opts, msg);
285+
}
286+
287+
static void test_invalid_numa_node(void)
288+
{
289+
const char *msg = "Invalid numa_node.\n";
290+
LIBBPF_OPTS(bpf_map_create_opts, opts,
291+
.map_flags = BPF_F_NUMA_NODE,
292+
.numa_node = 0xFF,
293+
);
294+
295+
test_map_create_array(&opts, msg);
296+
}
297+
298+
static void test_invalid_map_type(void)
299+
{
300+
const char *msg = "Invalid map_type.\n";
301+
LIBBPF_OPTS(bpf_map_create_opts, opts);
302+
303+
test_map_create(__MAX_BPF_MAP_TYPE, "test_map_create", &opts, msg);
304+
}
305+
306+
static void test_invalid_token_fd(void)
307+
{
308+
const char *msg = "Invalid map_token_fd.\n";
309+
LIBBPF_OPTS(bpf_map_create_opts, opts,
310+
.map_flags = BPF_F_TOKEN_FD,
311+
.token_fd = 0xFF,
312+
);
313+
314+
test_map_create_array(&opts, msg);
315+
}
316+
317+
static void test_invalid_map_name(void)
318+
{
319+
const char *msg = "Invalid map_name.\n";
320+
LIBBPF_OPTS(bpf_map_create_opts, opts);
321+
322+
test_map_create(BPF_MAP_TYPE_ARRAY, "test-!@#", &opts, msg);
323+
}
324+
325+
static void test_invalid_btf_fd(void)
326+
{
327+
const char *msg = "Invalid btf_fd.\n";
328+
LIBBPF_OPTS(bpf_map_create_opts, opts,
329+
.btf_fd = -1,
330+
.btf_key_type_id = 1,
331+
.btf_value_type_id = 1,
332+
);
333+
334+
test_map_create_array(&opts, msg);
335+
}
336+
337+
static void test_excl_prog_hash_size_1(void)
338+
{
339+
const char *msg = "Invalid excl_prog_hash_size.\n";
340+
const char *hash = "DEADCODE";
341+
LIBBPF_OPTS(bpf_map_create_opts, opts,
342+
.excl_prog_hash = hash,
343+
);
344+
345+
test_map_create_array(&opts, msg);
346+
}
347+
348+
static void test_excl_prog_hash_size_2(void)
349+
{
350+
const char *msg = "Invalid excl_prog_hash_size.\n";
351+
LIBBPF_OPTS(bpf_map_create_opts, opts,
352+
.excl_prog_hash_size = 1,
353+
);
354+
355+
test_map_create_array(&opts, msg);
356+
}
357+
358+
void test_map_create_failure(void)
359+
{
360+
if (test__start_subtest("invalid_vmlinux_value_type_id_struct_ops"))
361+
test_invalid_vmlinux_value_type_id_struct_ops();
362+
if (test__start_subtest("invalid_vmlinux_value_type_id_kv_type_id"))
363+
test_invalid_vmlinux_value_type_id_kv_type_id();
364+
if (test__start_subtest("invalid_value_type_id"))
365+
test_invalid_value_type_id();
366+
if (test__start_subtest("invalid_map_extra"))
367+
test_invalid_map_extra();
368+
if (test__start_subtest("invalid_numa_node"))
369+
test_invalid_numa_node();
370+
if (test__start_subtest("invalid_map_type"))
371+
test_invalid_map_type();
372+
if (test__start_subtest("invalid_token_fd"))
373+
test_invalid_token_fd();
374+
if (test__start_subtest("invalid_map_name"))
375+
test_invalid_map_name();
376+
if (test__start_subtest("invalid_btf_fd"))
377+
test_invalid_btf_fd();
378+
if (test__start_subtest("invalid_excl_prog_hash_size_1"))
379+
test_excl_prog_hash_size_1();
380+
if (test__start_subtest("invalid_excl_prog_hash_size_2"))
381+
test_excl_prog_hash_size_2();
382+
}

0 commit comments

Comments
 (0)