Revamp and fix libbpf's feature-probing APIs
Subsystems (5)
Andrii Nakryiko
maintainerv2 cover Revamp and fix libbpf's feature-probing APIs
Fix and improve libbpf feature-probing APIs. Name them consistently and deprecated previous inconsistently named APIs.
v1->v2:
- fixed misleading comment and added acks (Dave).
Andrii Nakryiko (3):
libbpf: rework feature-probing APIs selftests/bpf: add libbpf feature-probing API selftests
bpftool: reimplement large insn size limit feature probing
tools/bpf/bpftool/feature.c | 26 +- tools/lib/bpf/libbpf.h | 52 +++- tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/libbpf_probes.c | 235 ++++++++++++++---- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/libbpf_probes.c | 124 +++++++++ 6 files changed, 385 insertions(+), 57 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v2 1/3 libbpf: rework feature-probing APIs
Create three extensible alternatives to inconsistently named feature-probing APIs:
- libbpf_probe_bpf_prog_type() instead of bpf_probe_prog_type();
- libbpf_probe_bpf_map_type() instead of bpf_probe_map_type();
- libbpf_probe_bpf_helper() instead of bpf_probe_helper().
Set up return values such that libbpf can report errors (e.g., if some combination of input arguments isn't possible to validate, etc), in addition to whether the feature is supported (return value 1) or not supported (return value 0).
Also schedule deprecation of those three APIs. Also schedule deprecation of bpf_probe_large_insn_limit().
Also fix all the existing detection logic for various program and map types that never worked:
- BPF_PROG_TYPE_LIRC_MODE2;
- BPF_PROG_TYPE_TRACING;
- BPF_PROG_TYPE_LSM;
- BPF_PROG_TYPE_EXT;
- BPF_PROG_TYPE_SYSCALL;
- BPF_PROG_TYPE_STRUCT_OPS;
- BPF_MAP_TYPE_STRUCT_OPS;
- BPF_MAP_TYPE_BLOOM_FILTER.
Above prog/map types needed special setups and detection logic to work. Subsequent patch adds selftests that will make sure that all the detection logic keeps working for all current and future program and map types, avoiding otherwise inevitable bit rot.
[0] Closes: https://github.com/libbpf/libbpf/issues/312
--- tools/lib/bpf/libbpf.h | 52 +++++++- tools/lib/bpf/libbpf.map | 3 + tools/lib/bpf/libbpf_probes.c | 235 ++++++++++++++++++++++++++-------- 3 files changed, 236 insertions(+), 54 deletions(-)
tools/lib/bpf/libbpf.h +48 -4
@@ -1052,13 +1052,57 @@ bpf_prog_linfo__lfind(const struct bpf_prog_linfo *prog_linfo,* user, causing subsequent probes to fail. In this case, the caller may want * to adjust that limit with setrlimit(). */ -LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, -__u32 ifindex); +LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_prog_type() instead") +LIBBPF_API bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex); +LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_map_type() instead") LIBBPF_API bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex); -LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, -enum bpf_prog_type prog_type,__u32 ifindex); +LIBBPF_DEPRECATED_SINCE(0, 8, "use libbpf_probe_bpf_helper() instead") +LIBBPF_API bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type, __u32 ifindex); +LIBBPF_DEPRECATED_SINCE(0, 8, "implement your own or use bpftool for feature detection") LIBBPF_API bool bpf_probe_large_insn_limit(__u32 ifindex); +/** +* @brief **libbpf_probe_bpf_prog_type()** detects if host kernel supports +* BPF programs of a given type. +* @param prog_type BPF program type to detect kernel support for +* @param opts reserved for future extensibility, should be NULL +* @return 1, if given program type is supported; 0, if given program type is +* not supported; negative error code if feature detection failed or can't be +* performed +* +* Make sure the process has required set of CAP_* permissions (or runs as +* root) when performing feature checking. +*/ +LIBBPF_API int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts); +/** +* @brief **libbpf_probe_bpf_map_type()** detects if host kernel supports +* BPF maps of a given type. +* @param map_type BPF map type to detect kernel support for +* @param opts reserved for future extensibility, should be NULL +* @return 1, if given map type is supported; 0, if given map type is +* not supported; negative error code if feature detection failed or can't be +* performed +* +* Make sure the process has required set of CAP_* permissions (or runs as +* root) when performing feature checking. +*/ +LIBBPF_API int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts); +/** +* @brief **libbpf_probe_bpf_helper()** detects if host kernel supports the +* use of a given BPF helper from specified BPF program type. +* @param prog_type BPF program type used to check the support of BPF helper +* @param helper_id BPF helper ID (enum bpf_func_id) to check support for +* @param opts reserved for future extensibility, should be NULL +* @return 1, if given combination of program type and helper is supported; 0, +* if the combination is not supported; negative error code if feature +* detection for provided input arguments failed or can't be performed +* +* Make sure the process has required set of CAP_* permissions (or runs as +* root) when performing feature checking. +*/ +LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, +enum bpf_func_id helper_id,const void *opts); +/* * Get bpf_prog_info in continuous memory *
tools/lib/bpf/libbpf.map +3 -0
@@ -427,5 +427,8 @@ LIBBPF_0.7.0 { bpf_program__log_level; bpf_program__set_log_buf; bpf_program__set_log_level; + libbpf_probe_bpf_helper; + libbpf_probe_bpf_map_type; + libbpf_probe_bpf_prog_type; libbpf_set_memlock_rlim_max; };
tools/lib/bpf/libbpf_probes.c +185 -50
345 lines not on this page · raw message
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v2 2/3 selftests/bpf: add libbpf feature-probing API selftests
In reply to Andrii NakryikoAdd 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.
--- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/libbpf_probes.c | 124 ++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/libbpf_probes.c
tools/testing/selftests/bpf/config +2 -0
@@ -38,7 +38,9 @@ CONFIG_IPV6_SIT=m CONFIG_BPF_JIT=y CONFIG_BPF_LSM=y CONFIG_SECURITY=y +CONFIG_RC_CORE=y CONFIG_LIRC=y +CONFIG_BPF_LIRC_MODE2=y CONFIG_IMA=y CONFIG_SECURITYFS=y CONFIG_IMA_WRITE_POLICY=y
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c new file +124 -0
124 lines not on this page · raw message
Signature · 1 line
2.30.2
Andrii Nakryiko
maintainerPATCH v2 3/3 bpftool: reimplement large insn size limit feature probing
In reply to Andrii NakryikoReimplement bpf_probe_large_insn_limit() in bpftool, as that libbpf API is scheduled for deprecation in v0.8.
--- tools/bpf/bpftool/feature.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)
tools/bpf/bpftool/feature.c +23 -3
@@ -642,12 +642,32 @@ probe_helpers_for_progtype(enum bpf_prog_type prog_type, bool supported_type,printf ("\n" ); } -static void -probe_large_insn_limit (const char *define_prefix,__u32 ifindex) +/* +* Probe for availability of kernel commit (5.3): +* +* c04c0d2b968a ("bpf: increase complexity limit and maximum program size") +*/ +static void probe_large_insn_limit (const char *define_prefix,__u32 ifindex) { +LIBBPF_OPTS (bpf_prog_load_opts, opts, + .prog_ifindex = ifindex, + ); +struct bpf_insn insns[BPF_MAXINSNS +1 ];bool res; +int i, fd; + +for (i =0 ; i <BPF_MAXINSNS ; i++) + insns[i] =BPF_MOV64_IMM (BPF_REG_0 ,1 ); + insns[BPF_MAXINSNS ] =BPF_EXIT_INSN (); + + errno =0 ; + fd =bpf_prog_load (BPF_PROG_TYPE_SCHED_CLS ,NULL ,"GPL" , + insns,ARRAY_SIZE (insns), &opts); + res = fd >=0 || (errno !=E2BIG && errno !=EINVAL ); + +if (fd >=0 ) +close (fd); - res =bpf_probe_large_insn_limit (ifindex); print_bool_feature("have_large_insn_limit", "Large program size limit", "LARGE_INSN_LIMIT",
Signature · 1 line
2.30.2
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 9 lines · On Fri, 17 Dec 2021 09:11:59 -0800 you wrote:
Fix and improve libbpf feature-probing APIs. Name them consistently and deprecated previous inconsistently named APIs.
v1->v2:
- fixed misleading comment and added acks (Dave).Cc Dave Marchevsky <[email protected]>[...]
Here is the summary with links:
- [v2,bpf-next,1/3] libbpf: rework feature-probing APIs
https://git.kernel.org/bpf/bpf-next/c/878d8def0603 - [v2,bpf-next,2/3] selftests/bpf: add libbpf feature-probing API selftests https://git.kernel.org/bpf/bpf-next/c/5a8ea82f9d25 - [v2,bpf-next,3/3] bpftool: reimplement large insn size limit feature probing https://git.kernel.org/bpf/bpf-next/c/e967a20a8fab
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html