Skip to content

Commit 5ada586

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
libbpf: Add common attr support for map_create
With the previous commit adding common attribute support for BPF_MAP_CREATE, it is now possible to retrieve detailed error messages when map creation fails by using the 'log_buf' field from the common attributes. Extend 'bpf_map_create_opts' with these new fields, 'log_buf', 'log_size' , 'log_level' and 'log_true_size', allowing users to capture and inspect those log messages. Signed-off-by: Leon Hwang <[email protected]>
1 parent b402d84 commit 5ada586

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

tools/lib/bpf/bpf.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,13 @@ int bpf_map_create(enum bpf_map_type map_type,
203203
__u32 key_size,
204204
__u32 value_size,
205205
__u32 max_entries,
206-
const struct bpf_map_create_opts *opts)
206+
struct bpf_map_create_opts *opts)
207207
{
208208
const size_t attr_sz = offsetofend(union bpf_attr, excl_prog_hash_size);
209+
const size_t common_attrs_sz = sizeof(struct bpf_common_attr);
210+
struct bpf_common_attr common_attrs;
209211
union bpf_attr attr;
212+
const char *log_buf;
210213
int fd;
211214

212215
bump_rlimit_memlock();
@@ -239,7 +242,17 @@ int bpf_map_create(enum bpf_map_type map_type,
239242
attr.excl_prog_hash = ptr_to_u64(OPTS_GET(opts, excl_prog_hash, NULL));
240243
attr.excl_prog_hash_size = OPTS_GET(opts, excl_prog_hash_size, 0);
241244

242-
fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
245+
log_buf = OPTS_GET(opts, log_buf, NULL);
246+
if (log_buf && feat_supported(NULL, FEAT_EXTENDED_SYSCALL)) {
247+
memset(&common_attrs, 0, common_attrs_sz);
248+
common_attrs.log_buf = ptr_to_u64(log_buf);
249+
common_attrs.log_size = OPTS_GET(opts, log_size, 0);
250+
common_attrs.log_level = OPTS_GET(opts, log_level, 0);
251+
fd = sys_bpf_ext_fd(BPF_MAP_CREATE, &attr, attr_sz, &common_attrs, common_attrs_sz);
252+
OPTS_SET(opts, log_true_size, common_attrs.log_true_size);
253+
} else {
254+
fd = sys_bpf_fd(BPF_MAP_CREATE, &attr, attr_sz);
255+
}
243256
return libbpf_err_errno(fd);
244257
}
245258

tools/lib/bpf/bpf.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,21 @@ struct bpf_map_create_opts {
5757

5858
const void *excl_prog_hash;
5959
__u32 excl_prog_hash_size;
60+
61+
const char *log_buf;
62+
__u32 log_size;
63+
__u32 log_level;
64+
__u32 log_true_size;
6065
size_t :0;
6166
};
62-
#define bpf_map_create_opts__last_field excl_prog_hash_size
67+
#define bpf_map_create_opts__last_field log_true_size
6368

6469
LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
6570
const char *map_name,
6671
__u32 key_size,
6772
__u32 value_size,
6873
__u32 max_entries,
69-
const struct bpf_map_create_opts *opts);
74+
struct bpf_map_create_opts *opts);
7075

7176
struct bpf_prog_load_opts {
7277
size_t sz; /* size of this struct for forward/backward compatibility */

0 commit comments

Comments
 (0)