From: Stanislav Fomichev <sdf@google.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
"David S. Miller" <davem@davemloft.net>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
YiFei Zhu <zhuyifei@google.com>,
YiFei Zhu <zhuyifei1999@gmail.com>
Subject: Re: [PATCH bpf-next v3 5/8] bpftool: support dumping metadata
Date: Tue, 8 Sep 2020 13:53:08 -0700 [thread overview]
Message-ID: <CAKH8qBvkRrRWGX8HjKuCCoE1x2BB7tXmyJv1HotEyp7D_D+mLg@mail.gmail.com> (raw)
In-Reply-To: <CAEf4BzZcb+CKwL72mgC5B+2wAi8hfT_OoVUNZCcZjKgu4zRxiA@mail.gmail.com>
On Wed, Sep 2, 2020 at 10:00 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Aug 28, 2020 at 12:37 PM Stanislav Fomichev <sdf@google.com> wrote:
> >
> > From: YiFei Zhu <zhuyifei@google.com>
> >
> > Added a flag "--metadata" to `bpftool prog list` to dump the metadata
> > contents. For some formatting some BTF code is put directly in the
> > metadata dumping. Sanity checks on the map and the kind of the btf_type
> > to make sure we are actually dumping what we are expecting.
> >
> > A helper jsonw_reset is added to json writer so we can reuse the same
> > json writer without having extraneous commas.
> >
> > Sample output:
> >
> > $ bpftool prog --metadata
> > 6: cgroup_skb name prog tag bcf7977d3b93787c gpl
> > [...]
> > btf_id 4
> > metadata:
> > metadata_a = "foo"
> > metadata_b = 1
> >
> > $ bpftool prog --metadata --json --pretty
> > [{
> > "id": 6,
> > [...]
> > "btf_id": 4,
> > "metadata": {
> > "metadata_a": "foo",
> > "metadata_b": 1
> > }
> > }
> > ]
> >
> > Cc: YiFei Zhu <zhuyifei1999@gmail.com>
> > Signed-off-by: YiFei Zhu <zhuyifei@google.com>
> > Signed-off-by: Stanislav Fomichev <sdf@google.com>
> > ---
> > tools/bpf/bpftool/json_writer.c | 6 ++
> > tools/bpf/bpftool/json_writer.h | 3 +
> > tools/bpf/bpftool/main.c | 10 +++
> > tools/bpf/bpftool/main.h | 1 +
> > tools/bpf/bpftool/prog.c | 130 ++++++++++++++++++++++++++++++++
> > 5 files changed, 150 insertions(+)
> >
>
> [...]
>
> > +
> > + if (bpf_map_lookup_elem(map_fd, &key, value)) {
> > + p_err("metadata map lookup failed: %s", strerror(errno));
> > + goto out_free;
> > + }
> > +
> > + err = btf__get_from_id(map_info.btf_id, &btf);
>
> what if the map has no btf_id associated (e.g., because of an old
> kernel?); why fail in this case?
Thank you for the review, coming back at it a bit late :-(
This functionality is guarded by --metadata bpftool flag (off by default).
In case of no btf_id, it might be helpful to show why we don't have
the metadata rather than just quietly failing.
WDYT?
> > + if (err || !btf) {
> > + p_err("metadata BTF get failed: %s", strerror(-err));
> > + goto out_free;
> > + }
> > +
> > + t_datasec = btf__type_by_id(btf, map_info.btf_value_type_id);
> > + if (BTF_INFO_KIND(t_datasec->info) != BTF_KIND_DATASEC) {
>
> btf_is_datasec(t_datasec)
>
> > + p_err("bad metadata BTF");
> > + goto out_free;
> > + }
> > +
> > + vlen = BTF_INFO_VLEN(t_datasec->info);
>
> btf_vlen(t_datasec)
>
> > + vsi = (struct btf_var_secinfo *)(t_datasec + 1);
>
> btf_var_secinfos(t_datasec)
>
> > +
> > + /* We don't proceed to check the kinds of the elements of the DATASEC.
> > + * The verifier enforce then to be BTF_KIND_VAR.
>
> typo: then -> them
>
> > + */
> > +
> > + if (json_output) {
> > + struct btf_dumper d = {
> > + .btf = btf,
> > + .jw = json_wtr,
> > + .is_plain_text = false,
> > + };
> > +
> > + jsonw_name(json_wtr, "metadata");
> > +
> > + jsonw_start_object(json_wtr);
> > + for (i = 0; i < vlen; i++) {
>
> nit: doing ++vsi here
Agreed with all the above, except this one.
It feels like it's safer to do [i] in case somebody adds a 'continue'
clause later and we miss that '++vsi'.
Let me know if you feel strongly about it.
> > + t_var = btf__type_by_id(btf, vsi[i].type);
>
> and vsi->type here and below would look a bit cleaner
>
> > +
> > + jsonw_name(json_wtr, btf__name_by_offset(btf, t_var->name_off));
> > + err = btf_dumper_type(&d, t_var->type, value + vsi[i].offset);
> > + if (err) {
> > + p_err("btf dump failed");
> > + break;
> > + }
> > + }
> > + jsonw_end_object(json_wtr);
>
> [...]
next prev parent reply other threads:[~2020-09-08 20:53 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-28 19:35 [PATCH bpf-next v3 0/8] Allow storage of flexible metadata information for eBPF programs Stanislav Fomichev
2020-08-28 19:35 ` [PATCH bpf-next v3 1/8] bpf: Mutex protect used_maps array and count Stanislav Fomichev
2020-08-28 19:35 ` [PATCH bpf-next v3 2/8] bpf: Add BPF_PROG_BIND_MAP syscall Stanislav Fomichev
2020-09-03 2:15 ` Andrii Nakryiko
2020-08-28 19:35 ` [PATCH bpf-next v3 3/8] libbpf: Add BPF_PROG_BIND_MAP syscall and use it on .metadata section Stanislav Fomichev
2020-09-03 2:31 ` Andrii Nakryiko
2020-09-04 1:29 ` Alexei Starovoitov
2020-09-04 23:18 ` Andrii Nakryiko
2020-09-07 8:49 ` Toke Høiland-Jørgensen
2020-09-08 15:19 ` Stanislav Fomichev
2020-09-08 18:20 ` Andrii Nakryiko
2020-09-08 18:10 ` Andrii Nakryiko
2020-09-09 10:58 ` Toke Høiland-Jørgensen
2020-09-09 16:34 ` Andrii Nakryiko
2020-09-08 17:44 ` Andrey Ignatov
2020-09-08 18:24 ` Andrii Nakryiko
2020-08-28 19:35 ` [PATCH bpf-next v3 4/8] libbpf: implement bpf_prog_find_metadata Stanislav Fomichev
2020-08-28 21:10 ` Toke Høiland-Jørgensen
2020-08-31 15:40 ` sdf
2020-09-01 22:58 ` Alexei Starovoitov
2020-09-02 9:43 ` Toke Høiland-Jørgensen
2020-09-02 21:08 ` Alexei Starovoitov
2020-09-02 21:33 ` Toke Høiland-Jørgensen
2020-08-28 19:36 ` [PATCH bpf-next v3 5/8] bpftool: support dumping metadata Stanislav Fomichev
2020-09-03 5:00 ` Andrii Nakryiko
2020-09-08 20:53 ` Stanislav Fomichev [this message]
2020-09-08 22:35 ` Andrii Nakryiko
2020-09-08 22:49 ` Stanislav Fomichev
2020-08-28 19:36 ` [PATCH bpf-next v3 6/8] bpftool: support metadata internal map in gen skeleton Stanislav Fomichev
2020-08-28 19:36 ` [PATCH bpf-next v3 7/8] bpftool: mention --metadata in the documentation Stanislav Fomichev
2020-08-28 19:36 ` [PATCH bpf-next v3 8/8] selftests/bpf: Test load and dump metadata with btftool and skel Stanislav Fomichev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAKH8qBvkRrRWGX8HjKuCCoE1x2BB7tXmyJv1HotEyp7D_D+mLg@mail.gmail.com \
--to=sdf@google.com \
--cc=andrii.nakryiko@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=netdev@vger.kernel.org \
--cc=zhuyifei1999@gmail.com \
--cc=zhuyifei@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).