Skip to content

Commit 7f8089e

Browse files
harshimogalapalliKernel Patches Daemon
authored andcommitted
bpftool: Print map ID upon creation and support JSON output
It is useful to print map ID on successful creation. JSON case: $ ./bpftool -j map create /sys/fs/bpf/test_map4 type hash key 4 value 8 entries 128 name map4 {"id":12} Generic case: $ ./bpftool map create /sys/fs/bpf/test_map5 type hash key 4 value 8 entries 128 name map5 Map successfully created with ID: 15 Bpftool Issue: libbpf/bpftool#121 Signed-off-by: Harshit Mogalapalli <[email protected]> Acked-by: Yonghong Song <[email protected]>
1 parent c942b08 commit 7f8089e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

tools/bpf/bpftool/map.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,8 @@ static int do_create(int argc, char **argv)
12511251
LIBBPF_OPTS(bpf_map_create_opts, attr);
12521252
enum bpf_map_type map_type = BPF_MAP_TYPE_UNSPEC;
12531253
__u32 key_size = 0, value_size = 0, max_entries = 0;
1254+
struct bpf_map_info map_info = {};
1255+
__u32 map_info_len = sizeof(map_info);
12541256
const char *map_name = NULL;
12551257
const char *pinfile;
12561258
int err = -1, fd;
@@ -1353,13 +1355,24 @@ static int do_create(int argc, char **argv)
13531355
}
13541356

13551357
err = do_pin_fd(fd, pinfile);
1356-
close(fd);
13571358
if (err)
1358-
goto exit;
1359+
goto close_fd;
13591360

1360-
if (json_output)
1361-
jsonw_null(json_wtr);
1361+
err = bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
1362+
if (err) {
1363+
p_err("Failed to fetch map info: %s\n", strerror(errno));
1364+
goto close_fd;
1365+
}
13621366

1367+
if (json_output) {
1368+
jsonw_start_object(json_wtr);
1369+
jsonw_int_field(json_wtr, "id", map_info.id);
1370+
jsonw_end_object(json_wtr);
1371+
} else {
1372+
printf("Map successfully created with ID: %u\n", map_info.id);
1373+
}
1374+
close_fd:
1375+
close(fd);
13631376
exit:
13641377
if (attr.inner_map_fd > 0)
13651378
close(attr.inner_map_fd);

0 commit comments

Comments
 (0)