bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH v4 bpf-next 2/4] bpf: Add BPF program and map iterators as built-in BPF programs.
Date: Wed, 29 Jul 2020 11:21:35 -0700	[thread overview]
Message-ID: <CAEf4BzaJcAerczLS+nHPX5KpvNjfAB6Ushmy3HFyu5OJbKH9+Q@mail.gmail.com> (raw)
In-Reply-To: <20200724203830.81531-3-alexei.starovoitov@gmail.com>

On Fri, Jul 24, 2020 at 1:39 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> From: Alexei Starovoitov <ast@kernel.org>
>
> The program and map iterators work similar to seq_file-s.
> Once the program is pinned in bpffs it can be read with "cat" tool
> to print human readable output. In this case about BPF programs and maps.
> For example:
> $ cat /sys/fs/bpf/progs.debug
>   id name            attached
>    5 dump_bpf_map    bpf_iter_bpf_map
>    6 dump_bpf_prog   bpf_iter_bpf_prog
> $ cat /sys/fs/bpf/maps.debug
>   id name            pages
>    3 iterator.rodata     2
>
> To avoid kernel build dependency on clang 10 separate bpf skeleton generation
> into manual "make" step and instead check-in generated .skel.h into git.
>
> Unlike 'bpftool prog show' in-kernel BTF name is used (when available)
> to print full name of BPF program instead of 16-byte truncated name.
>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---

Tiny bug below, otherwise looks good.

Acked-by: Andrii Nakryiko <andriin@fb.com>


>  kernel/bpf/preload/iterators/.gitignore       |   2 +
>  kernel/bpf/preload/iterators/Makefile         |  57 +++
>  kernel/bpf/preload/iterators/README           |   4 +
>  kernel/bpf/preload/iterators/iterators.bpf.c  | 118 +++++
>  kernel/bpf/preload/iterators/iterators.skel.h | 411 ++++++++++++++++++
>  5 files changed, 592 insertions(+)
>  create mode 100644 kernel/bpf/preload/iterators/.gitignore
>  create mode 100644 kernel/bpf/preload/iterators/Makefile
>  create mode 100644 kernel/bpf/preload/iterators/README
>  create mode 100644 kernel/bpf/preload/iterators/iterators.bpf.c
>  create mode 100644 kernel/bpf/preload/iterators/iterators.skel.h
>

[...]

> +
> +static const char *get_name(struct btf *btf, long btf_id, const char *fallback)
> +{
> +       struct btf_type **types, *t;
> +       unsigned int name_off;
> +       const char *str;
> +
> +       if (!btf)
> +               return fallback;
> +       str = btf->strings;
> +       types = btf->types;
> +       bpf_probe_read_kernel(&t, sizeof(t), types + btf_id);
> +       name_off = BPF_CORE_READ(t, name_off);
> +       if (name_off > btf->hdr.str_len)

>= here?

> +               return fallback;
> +       return str + name_off;
> +}
> +
> +SEC("iter/bpf_map")
> +int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
> +{
> +       struct seq_file *seq = ctx->meta->seq;
> +       __u64 seq_num = ctx->meta->seq_num;
> +       struct bpf_map *map = ctx->map;
> +
> +       if (!map)
> +               return 0;
> +
> +       if (seq_num == 0)
> +               BPF_SEQ_PRINTF(seq, "  id name             pages\n");
> +
> +       BPF_SEQ_PRINTF(seq, "%4u %-16s%6d\n", map->id, map->name, map->memory.pages);

map->memory.pages won't be meaningful, once Roman's patches removing
RLIMIT_MEMLOCK usage land, so might just drop them now

> +       return 0;
> +}
> +
> +SEC("iter/bpf_prog")
> +int dump_bpf_prog(struct bpf_iter__bpf_prog *ctx)
> +{
> +       struct seq_file *seq = ctx->meta->seq;
> +       __u64 seq_num = ctx->meta->seq_num;
> +       struct bpf_prog *prog = ctx->prog;
> +       struct bpf_prog_aux *aux;
> +
> +       if (!prog)
> +               return 0;
> +
> +       aux = prog->aux;
> +       if (seq_num == 0)
> +               BPF_SEQ_PRINTF(seq, "  id name             attached\n");
> +
> +       BPF_SEQ_PRINTF(seq, "%4u %-16s %s %s\n", aux->id,
> +                      get_name(aux->btf, aux->func_info[0].type_id, aux->name),
> +                      aux->attach_func_name, aux->linked_prog->aux->name);
> +       return 0;
> +}
> +char LICENSE[] SEC("license") = "GPL";


[...]

  reply	other threads:[~2020-07-29 18:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-24 20:38 [PATCH v4 bpf-next 0/4] bpf: Populate bpffs with map and prog iterators Alexei Starovoitov
2020-07-24 20:38 ` [PATCH v4 bpf-next 1/4] bpf: Factor out bpf_link_get_by_id() helper Alexei Starovoitov
2020-07-29 22:25   ` Song Liu
2020-07-24 20:38 ` [PATCH v4 bpf-next 2/4] bpf: Add BPF program and map iterators as built-in BPF programs Alexei Starovoitov
2020-07-29 18:21   ` Andrii Nakryiko [this message]
2020-07-24 20:38 ` [PATCH v4 bpf-next 3/4] bpf: Add kernel module with user mode driver that populates bpffs Alexei Starovoitov
2020-07-29 23:06   ` Daniel Borkmann
2020-07-24 20:38 ` [PATCH v4 bpf-next 4/4] selftests/bpf: Add bpffs preload test Alexei Starovoitov

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=CAEf4BzaJcAerczLS+nHPX5KpvNjfAB6Ushmy3HFyu5OJbKH9+Q@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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).