To support the extended 'bpf()' syscall introduced in the previous commit, this patch adds the following APIs: 1. *Internal:* * 'sys_bpf_extended()' * 'sys_bpf_fd_extended()' These wrap the raw 'syscall()' interface to support passing extended attributes. 2. *Exported:* * 'probe_sys_bpf_extended()' This function checks whether the running kernel supports the extended 'bpf()' syscall with common attributes. Signed-off-by: Leon Hwang --- tools/lib/bpf/bpf.c | 44 +++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 1 + tools/lib/bpf/features.c | 8 ++++++ tools/lib/bpf/libbpf.map | 2 ++ tools/lib/bpf/libbpf_internal.h | 2 ++ 5 files changed, 57 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index ab40dbf9f020..de0c5c6c8ae6 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -69,6 +69,50 @@ static inline __u64 ptr_to_u64(const void *ptr) return (__u64) (unsigned long) ptr; } +static inline int sys_bpf_extended(enum bpf_cmd cmd, union bpf_attr *attr, + unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + cmd = common_attrs ? cmd | BPF_COMMON_ATTRS : cmd & ~BPF_COMMON_ATTRS; + return syscall(__NR_bpf, cmd, attr, size, common_attrs, size_common); +} + +static inline int sys_bpf_fd_extended(enum bpf_cmd cmd, union bpf_attr *attr, + unsigned int size, + struct bpf_common_attr *common_attrs, + unsigned int size_common) +{ + int fd; + + fd = sys_bpf_extended(cmd, attr, size, common_attrs, size_common); + return ensure_good_fd(fd); +} + +int probe_sys_bpf_extended(int token_fd) +{ + const size_t attr_sz = offsetofend(union bpf_attr, prog_token_fd); + struct bpf_common_attr common_attrs; + struct bpf_insn insns[] = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + union bpf_attr attr; + + memset(&attr, 0, attr_sz); + attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; + attr.license = ptr_to_u64("GPL"); + attr.insns = ptr_to_u64(insns); + attr.insn_cnt = (__u32)ARRAY_SIZE(insns); + attr.prog_token_fd = token_fd; + if (token_fd) + attr.prog_flags |= BPF_F_TOKEN_FD; + libbpf_strlcpy(attr.prog_name, "libbpf_sysbpftest", sizeof(attr.prog_name)); + memset(&common_attrs, 0, sizeof(common_attrs)); + + return sys_bpf_fd_extended(BPF_PROG_LOAD, &attr, attr_sz, &common_attrs, sizeof(common_attrs)); +} + static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr, unsigned int size) { diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 7252150e7ad3..38819071ecbe 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -35,6 +35,7 @@ extern "C" { #endif +LIBBPF_API int probe_sys_bpf_extended(int token_fd); LIBBPF_API int libbpf_set_memlock_rlim(size_t memlock_bytes); struct bpf_map_create_opts { diff --git a/tools/lib/bpf/features.c b/tools/lib/bpf/features.c index 760657f5224c..a63172c6343d 100644 --- a/tools/lib/bpf/features.c +++ b/tools/lib/bpf/features.c @@ -507,6 +507,11 @@ static int probe_kern_arg_ctx_tag(int token_fd) return probe_fd(prog_fd); } +static int probe_kern_extended_syscall(int token_fd) +{ + return probe_fd(probe_sys_bpf_extended(token_fd)); +} + typedef int (*feature_probe_fn)(int /* token_fd */); static struct kern_feature_cache feature_cache; @@ -582,6 +587,9 @@ static struct kern_feature_desc { [FEAT_BTF_QMARK_DATASEC] = { "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec, }, + [FEAT_EXTENDED_SYSCALL] = { + "Kernel supports extended syscall", probe_kern_extended_syscall, + }, }; bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id) diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index d7bd463e7017..83d3d1af5ed1 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -448,4 +448,6 @@ LIBBPF_1.6.0 { } LIBBPF_1.5.0; LIBBPF_1.7.0 { + global: + probe_sys_bpf_extended; } LIBBPF_1.6.0; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index 477a3b3389a0..497d845339a6 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -380,6 +380,8 @@ enum kern_feature_id { FEAT_ARG_CTX_TAG, /* Kernel supports '?' at the front of datasec names */ FEAT_BTF_QMARK_DATASEC, + /* Kernel supports extended syscall */ + FEAT_EXTENDED_SYSCALL, __FEAT_CNT, }; -- 2.50.1