Re: [RFC bpf-next 3/3] selftests/bpf: verify module-specific types can be shown via bpf_snprintf_btf
From: Andrii Nakryiko
Date: Sat Nov 14 2020 - 02:03:27 EST
On Fri, Nov 13, 2020 at 10:11 AM Alan Maguire <alan.maguire@xxxxxxxxxx> wrote:
>
> Verify that specifying a module name in "struct btf_ptr *" along
> with a type id of a module-specific type will succeed.
>
> veth_stats_rx() is chosen because its function signature consists
> of a module-specific type "struct veth_stats" and a kernel-specific
> one "struct net_device".
>
> Signed-off-by: Alan Maguire <alan.maguire@xxxxxxxxxx>
> ---
> .../selftests/bpf/prog_tests/snprintf_btf_mod.c | 96 ++++++++++++++++++++++
> tools/testing/selftests/bpf/progs/btf_ptr.h | 1 +
> tools/testing/selftests/bpf/progs/veth_stats_rx.c | 73 ++++++++++++++++
> 3 files changed, 170 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/prog_tests/snprintf_btf_mod.c
> create mode 100644 tools/testing/selftests/bpf/progs/veth_stats_rx.c
>
[...]
> + err = veth_stats_rx__load(skel);
> + if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
> + goto cleanup;
> +
> + bss = skel->bss;
> +
> + bss->veth_stats_btf_id = btf__find_by_name(veth_btf, "veth_stats");
This is really awkward that this needs to be done from user-space.
Libbpf will be able to do this regardless of whether the type is in
vmlinux or kernel module. See my comments on patch #1.
> +
> + if (CHECK(bss->veth_stats_btf_id <= 0, "find 'struct veth_stats'",
> + "could not find 'struct veth_stats' in veth BTF: %d",
> + bss->veth_stats_btf_id))
> + goto cleanup;
> +
[...]
> + btf_ids[0] = veth_stats_btf_id;
> + ptrs[0] = (void *)PT_REGS_PARM1_CORE(ctx);
> +#if __has_builtin(__builtin_btf_type_id)
nit: there are a bunch of selftests that just assume we have this
built-in, so I don't think you need to guard it with #if here.
> + btf_ids[1] = bpf_core_type_id_kernel(struct net_device);
> + ptrs[1] = (void *)PT_REGS_PARM2_CORE(ctx);
> +#endif
[...]