Re: [PATCH bpf-next 2/3] selftests/bpf: add libbpf feature-probing API selftests
From: Andrii Nakryiko <hidden>
Date: 2021-12-17 00:42:23
On Thu, Dec 16, 2021 at 4:21 PM Dave Marchevsky [off-list ref] wrote:
On 12/16/21 2:04 AM, Andrii Nakryiko wrote:quoted
Add selftests for prog/map/prog+helper feature probing APIs. Prog and map selftests are designed in such a way that they will always test all the possible prog/map types, based on running kernel's vmlinux BTF enum definition. This way we'll always be sure that when adding new BPF program types or map types, libbpf will be always updated accordingly to be able to feature-detect them. BPF prog_helper selftest will have to be manually extended with interesting and important prog+helper combinations, it's easy, but can't be completely automated. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> ---[...]quoted
+ for (e = btf_enum(t), i = 0, n = btf_vlen(t); i < n; e++, i++) { + const char *prog_type_name = btf__str_by_offset(btf, e->name_off); + enum bpf_prog_type prog_type = (enum bpf_prog_type)e->val; + int res; + + if (prog_type == BPF_PROG_TYPE_UNSPEC) + continue; + + if (!test__start_subtest(prog_type_name)) + continue; + + res = libbpf_probe_bpf_prog_type(prog_type, NULL); + ASSERT_EQ(res, 1, prog_type_name); + }I like how easy BTF makes this. Maybe worth trying to probe one-past-the-end of enum to confirm it fails as expected?
Yeah, sure, not a bad idea, I'll add in v2.
Regardless, Acked-by: Dave Marchevsky <redacted>quoted
+cleanup: + btf__free(btf); +}[...]