[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <d9baed0b-c272-4970-8a46-87f12757ee87@kernel.org>
Date: Sat, 1 Nov 2025 16:59:11 +0000
From: Quentin Monnet <qmo@...nel.org>
To: Harshit Mogalapalli <harshit.m.mogalapalli@...cle.com>,
bpf@...r.kernel.org
Cc: alan.maguire@...cle.com, Alexei Starovoitov <ast@...nel.org>,
Daniel Borkmann <daniel@...earbox.net>, Andrii Nakryiko <andrii@...nel.org>,
Martin KaFai Lau <martin.lau@...ux.dev>, Eduard Zingerman
<eddyz87@...il.com>, Song Liu <song@...nel.org>,
Yonghong Song <yonghong.song@...ux.dev>,
John Fastabend <john.fastabend@...il.com>, KP Singh <kpsingh@...nel.org>,
Stanislav Fomichev <sdf@...ichev.me>, Hao Luo <haoluo@...gle.com>,
Jiri Olsa <jolsa@...nel.org>, Shuah Khan <shuah@...nel.org>,
linux-kernel@...r.kernel.org, linux-kselftest@...r.kernel.org
Subject: Re: [PATCH v2 1/2] bpftool: Print map ID upon creation and support
JSON output
2025-10-30 14:06 UTC-0700 ~ Harshit Mogalapalli
<harshit.m.mogalapalli@...cle.com>
> 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: https://github.com/libbpf/bpftool/issues/121
> Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@...cle.com>
> ---
> tools/bpf/bpftool/map.c | 21 +++++++++++++++++----
> 1 file changed, 17 insertions(+), 4 deletions(-)
>
> diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
> index c9de44a45778..80c96b33b553 100644
> --- a/tools/bpf/bpftool/map.c
> +++ b/tools/bpf/bpftool/map.c
> @@ -1251,6 +1251,8 @@ static int do_create(int argc, char **argv)
> LIBBPF_OPTS(bpf_map_create_opts, attr);
> enum bpf_map_type map_type = BPF_MAP_TYPE_UNSPEC;
> __u32 key_size = 0, value_size = 0, max_entries = 0;
> + struct bpf_map_info map_info = {};
> + __u32 map_info_len = sizeof(map_info);
> const char *map_name = NULL;
> const char *pinfile;
> int err = -1, fd;
> @@ -1353,13 +1355,24 @@ static int do_create(int argc, char **argv)
> }
>
> err = do_pin_fd(fd, pinfile);
> - close(fd);
> if (err)
> - goto exit;
> + goto close_fd;
>
> - if (json_output)
> - jsonw_null(json_wtr);
> + err = bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len);
> + if (err) {
> + p_err("Failed to fetch map info: %s\n", strerror(errno));
Nit: Please remove the line break ('\n') here, p_err() already adds it.
> + goto close_fd;
> + }
>
> + if (json_output) {
> + jsonw_start_object(json_wtr);
> + jsonw_int_field(json_wtr, "id", map_info.id);
> + jsonw_end_object(json_wtr);
I've been wondering if we should have some parent object like
'{"map_created": { "id": 15 }}' in case we later add more output, but I
can't really see a good use case at the moment, I'm probably
overthinking it... So I'm good with the current output. Thanks for this!
With the line break removed:
Reviewed-by: Quentin Monnet <qmo@...nel.org>
Powered by blists - more mailing lists