2019-01-17 15:27:53 +00:00
|
|
|
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
|
|
|
|
/* Copyright (c) 2019 Netronome Systems, Inc. */
|
|
|
|
|
|
|
|
#include <errno.h>
|
tools: bpftool: add probes for eBPF helper functions
Similarly to what was done for program types and map types, add a set of
probes to test the availability of the different eBPF helper functions
on the current system.
For each known program type, all known helpers are tested, in order to
establish a compatibility matrix. Output is provided as a set of lists
of available helpers, one per program type.
Sample output:
# bpftool feature probe kernel
...
Scanning eBPF helper functions...
eBPF helpers supported for program type socket_filter:
- bpf_map_lookup_elem
- bpf_map_update_elem
- bpf_map_delete_elem
...
eBPF helpers supported for program type kprobe:
- bpf_map_lookup_elem
- bpf_map_update_elem
- bpf_map_delete_elem
...
# bpftool --json --pretty feature probe kernel
{
...
"helpers": {
"socket_filter_available_helpers": ["bpf_map_lookup_elem", \
"bpf_map_update_elem","bpf_map_delete_elem", ...
],
"kprobe_available_helpers": ["bpf_map_lookup_elem", \
"bpf_map_update_elem","bpf_map_delete_elem", ...
],
...
}
}
v5:
- In libbpf.map, move global symbol to the new LIBBPF_0.0.2 section.
v4:
- Use "enum bpf_func_id" instead of "__u32" in bpf_probe_helper()
declaration for the type of the argument used to pass the id of
the helper to probe.
- Undef BPF_HELPER_MAKE_ENTRY after using it.
v3:
- Do not pass kernel version from bpftool to libbpf probes (kernel
version for testing program with kprobes is retrieved directly from
libbpf).
- Dump one list of available helpers per program type (instead of one
list of compatible program types per helper).
v2:
- Move probes from bpftool to libbpf.
- Test all program types for each helper, print a list of working prog
types for each helper.
- Fall back on include/uapi/linux/bpf.h for names and ids of helpers.
- Remove C-style macros output from this patch.
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-01-17 15:27:55 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2019-01-17 15:27:53 +00:00
|
|
|
#include <unistd.h>
|
tools: bpftool: add probes for eBPF helper functions
Similarly to what was done for program types and map types, add a set of
probes to test the availability of the different eBPF helper functions
on the current system.
For each known program type, all known helpers are tested, in order to
establish a compatibility matrix. Output is provided as a set of lists
of available helpers, one per program type.
Sample output:
# bpftool feature probe kernel
...
Scanning eBPF helper functions...
eBPF helpers supported for program type socket_filter:
- bpf_map_lookup_elem
- bpf_map_update_elem
- bpf_map_delete_elem
...
eBPF helpers supported for program type kprobe:
- bpf_map_lookup_elem
- bpf_map_update_elem
- bpf_map_delete_elem
...
# bpftool --json --pretty feature probe kernel
{
...
"helpers": {
"socket_filter_available_helpers": ["bpf_map_lookup_elem", \
"bpf_map_update_elem","bpf_map_delete_elem", ...
],
"kprobe_available_helpers": ["bpf_map_lookup_elem", \
"bpf_map_update_elem","bpf_map_delete_elem", ...
],
...
}
}
v5:
- In libbpf.map, move global symbol to the new LIBBPF_0.0.2 section.
v4:
- Use "enum bpf_func_id" instead of "__u32" in bpf_probe_helper()
declaration for the type of the argument used to pass the id of
the helper to probe.
- Undef BPF_HELPER_MAKE_ENTRY after using it.
v3:
- Do not pass kernel version from bpftool to libbpf probes (kernel
version for testing program with kprobes is retrieved directly from
libbpf).
- Dump one list of available helpers per program type (instead of one
list of compatible program types per helper).
v2:
- Move probes from bpftool to libbpf.
- Test all program types for each helper, print a list of working prog
types for each helper.
- Fall back on include/uapi/linux/bpf.h for names and ids of helpers.
- Remove C-style macros output from this patch.
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-01-17 15:27:55 +00:00
|
|
|
#include <net/if.h>
|
2019-01-17 15:27:53 +00:00
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
2019-04-26 23:39:44 +00:00
|
|
|
#include <linux/btf.h>
|
2019-01-17 15:27:53 +00:00
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <linux/kernel.h>
|
2023-02-03 23:48:42 +00:00
|
|
|
#include <linux/version.h>
|
2019-01-17 15:27:53 +00:00
|
|
|
|
|
|
|
#include "bpf.h"
|
|
|
|
#include "libbpf.h"
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
#include "libbpf_internal.h"
|
2019-01-17 15:27:53 +00:00
|
|
|
|
2023-02-03 23:48:42 +00:00
|
|
|
/* On Ubuntu LINUX_VERSION_CODE doesn't correspond to info.release,
|
|
|
|
* but Ubuntu provides /proc/version_signature file, as described at
|
|
|
|
* https://ubuntu.com/kernel, with an example contents below, which we
|
|
|
|
* can use to get a proper LINUX_VERSION_CODE.
|
|
|
|
*
|
|
|
|
* Ubuntu 5.4.0-12.15-generic 5.4.8
|
|
|
|
*
|
|
|
|
* In the above, 5.4.8 is what kernel is actually expecting, while
|
|
|
|
* uname() call will return 5.4.0 in info.release.
|
|
|
|
*/
|
|
|
|
static __u32 get_ubuntu_kernel_version(void)
|
|
|
|
{
|
|
|
|
const char *ubuntu_kver_file = "/proc/version_signature";
|
|
|
|
__u32 major, minor, patch;
|
|
|
|
int ret;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (faccessat(AT_FDCWD, ubuntu_kver_file, R_OK, AT_EACCESS) != 0)
|
|
|
|
return 0;
|
|
|
|
|
2023-05-25 22:13:10 +00:00
|
|
|
f = fopen(ubuntu_kver_file, "re");
|
2023-02-03 23:48:42 +00:00
|
|
|
if (!f)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = fscanf(f, "%*s %*s %u.%u.%u\n", &major, &minor, &patch);
|
|
|
|
fclose(f);
|
|
|
|
if (ret != 3)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return KERNEL_VERSION(major, minor, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On Debian LINUX_VERSION_CODE doesn't correspond to info.release.
|
|
|
|
* Instead, it is provided in info.version. An example content of
|
|
|
|
* Debian 10 looks like the below.
|
|
|
|
*
|
|
|
|
* utsname::release 4.19.0-22-amd64
|
|
|
|
* utsname::version #1 SMP Debian 4.19.260-1 (2022-09-29)
|
|
|
|
*
|
|
|
|
* In the above, 4.19.260 is what kernel is actually expecting, while
|
|
|
|
* uname() call will return 4.19.0 in info.release.
|
|
|
|
*/
|
|
|
|
static __u32 get_debian_kernel_version(struct utsname *info)
|
|
|
|
{
|
|
|
|
__u32 major, minor, patch;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = strstr(info->version, "Debian ");
|
|
|
|
if (!p) {
|
|
|
|
/* This is not a Debian kernel. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sscanf(p, "Debian %u.%u.%u", &major, &minor, &patch) != 3)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return KERNEL_VERSION(major, minor, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
__u32 get_kernel_version(void)
|
|
|
|
{
|
|
|
|
__u32 major, minor, patch, version;
|
|
|
|
struct utsname info;
|
|
|
|
|
|
|
|
/* Check if this is an Ubuntu kernel. */
|
|
|
|
version = get_ubuntu_kernel_version();
|
|
|
|
if (version != 0)
|
|
|
|
return version;
|
|
|
|
|
|
|
|
uname(&info);
|
|
|
|
|
|
|
|
/* Check if this is a Debian kernel. */
|
|
|
|
version = get_debian_kernel_version(&info);
|
|
|
|
if (version != 0)
|
|
|
|
return version;
|
|
|
|
|
|
|
|
if (sscanf(info.release, "%u.%u.%u", &major, &minor, &patch) != 3)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return KERNEL_VERSION(major, minor, patch);
|
|
|
|
}
|
|
|
|
|
2021-12-17 17:12:00 +00:00
|
|
|
static int probe_prog_load(enum bpf_prog_type prog_type,
|
|
|
|
const struct bpf_insn *insns, size_t insns_cnt,
|
2022-06-27 21:15:16 +00:00
|
|
|
char *log_buf, size_t log_buf_sz)
|
2019-01-17 15:27:53 +00:00
|
|
|
{
|
2021-12-17 17:12:00 +00:00
|
|
|
LIBBPF_OPTS(bpf_prog_load_opts, opts,
|
|
|
|
.log_buf = log_buf,
|
|
|
|
.log_size = log_buf_sz,
|
|
|
|
.log_level = log_buf ? 1 : 0,
|
|
|
|
);
|
|
|
|
int fd, err, exp_err = 0;
|
|
|
|
const char *exp_msg = NULL;
|
|
|
|
char buf[4096];
|
2019-01-17 15:27:53 +00:00
|
|
|
|
|
|
|
switch (prog_type) {
|
|
|
|
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
|
2021-11-03 22:08:37 +00:00
|
|
|
opts.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
|
2019-01-17 15:27:53 +00:00
|
|
|
break;
|
2021-07-28 22:58:25 +00:00
|
|
|
case BPF_PROG_TYPE_CGROUP_SOCKOPT:
|
2021-11-03 22:08:37 +00:00
|
|
|
opts.expected_attach_type = BPF_CGROUP_GETSOCKOPT;
|
2021-07-28 22:58:25 +00:00
|
|
|
break;
|
2020-07-17 10:35:33 +00:00
|
|
|
case BPF_PROG_TYPE_SK_LOOKUP:
|
2021-11-03 22:08:37 +00:00
|
|
|
opts.expected_attach_type = BPF_SK_LOOKUP;
|
2020-07-17 10:35:33 +00:00
|
|
|
break;
|
2019-01-17 15:27:53 +00:00
|
|
|
case BPF_PROG_TYPE_KPROBE:
|
2021-11-03 22:08:37 +00:00
|
|
|
opts.kern_version = get_kernel_version();
|
2019-01-17 15:27:53 +00:00
|
|
|
break;
|
2021-12-17 17:12:00 +00:00
|
|
|
case BPF_PROG_TYPE_LIRC_MODE2:
|
|
|
|
opts.expected_attach_type = BPF_LIRC_MODE2;
|
|
|
|
break;
|
|
|
|
case BPF_PROG_TYPE_TRACING:
|
|
|
|
case BPF_PROG_TYPE_LSM:
|
|
|
|
opts.log_buf = buf;
|
|
|
|
opts.log_size = sizeof(buf);
|
|
|
|
opts.log_level = 1;
|
|
|
|
if (prog_type == BPF_PROG_TYPE_TRACING)
|
|
|
|
opts.expected_attach_type = BPF_TRACE_FENTRY;
|
|
|
|
else
|
|
|
|
opts.expected_attach_type = BPF_MODIFY_RETURN;
|
|
|
|
opts.attach_btf_id = 1;
|
|
|
|
|
|
|
|
exp_err = -EINVAL;
|
|
|
|
exp_msg = "attach_btf_id 1 is not a function";
|
|
|
|
break;
|
|
|
|
case BPF_PROG_TYPE_EXT:
|
|
|
|
opts.log_buf = buf;
|
|
|
|
opts.log_size = sizeof(buf);
|
|
|
|
opts.log_level = 1;
|
|
|
|
opts.attach_btf_id = 1;
|
|
|
|
|
|
|
|
exp_err = -EINVAL;
|
|
|
|
exp_msg = "Cannot replace kernel functions";
|
|
|
|
break;
|
|
|
|
case BPF_PROG_TYPE_SYSCALL:
|
|
|
|
opts.prog_flags = BPF_F_SLEEPABLE;
|
|
|
|
break;
|
|
|
|
case BPF_PROG_TYPE_STRUCT_OPS:
|
|
|
|
exp_err = -524; /* -ENOTSUPP */
|
|
|
|
break;
|
2019-01-17 15:27:53 +00:00
|
|
|
case BPF_PROG_TYPE_UNSPEC:
|
|
|
|
case BPF_PROG_TYPE_SOCKET_FILTER:
|
|
|
|
case BPF_PROG_TYPE_SCHED_CLS:
|
|
|
|
case BPF_PROG_TYPE_SCHED_ACT:
|
|
|
|
case BPF_PROG_TYPE_TRACEPOINT:
|
|
|
|
case BPF_PROG_TYPE_XDP:
|
|
|
|
case BPF_PROG_TYPE_PERF_EVENT:
|
|
|
|
case BPF_PROG_TYPE_CGROUP_SKB:
|
|
|
|
case BPF_PROG_TYPE_CGROUP_SOCK:
|
|
|
|
case BPF_PROG_TYPE_LWT_IN:
|
|
|
|
case BPF_PROG_TYPE_LWT_OUT:
|
|
|
|
case BPF_PROG_TYPE_LWT_XMIT:
|
|
|
|
case BPF_PROG_TYPE_SOCK_OPS:
|
|
|
|
case BPF_PROG_TYPE_SK_SKB:
|
|
|
|
case BPF_PROG_TYPE_CGROUP_DEVICE:
|
|
|
|
case BPF_PROG_TYPE_SK_MSG:
|
|
|
|
case BPF_PROG_TYPE_RAW_TRACEPOINT:
|
2019-04-26 18:49:50 +00:00
|
|
|
case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
|
2019-01-17 15:27:53 +00:00
|
|
|
case BPF_PROG_TYPE_LWT_SEG6LOCAL:
|
|
|
|
case BPF_PROG_TYPE_SK_REUSEPORT:
|
|
|
|
case BPF_PROG_TYPE_FLOW_DISSECTOR:
|
2019-03-08 17:15:26 +00:00
|
|
|
case BPF_PROG_TYPE_CGROUP_SYSCTL:
|
2023-06-05 13:14:45 +00:00
|
|
|
break;
|
2023-04-21 17:02:58 +00:00
|
|
|
case BPF_PROG_TYPE_NETFILTER:
|
2023-06-05 13:14:45 +00:00
|
|
|
opts.expected_attach_type = BPF_NETFILTER;
|
2019-01-17 15:27:53 +00:00
|
|
|
break;
|
2021-12-17 17:12:00 +00:00
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
2019-01-17 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 17:12:00 +00:00
|
|
|
fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insns_cnt, &opts);
|
|
|
|
err = -errno;
|
2019-01-17 15:27:53 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2021-12-17 17:12:00 +00:00
|
|
|
if (exp_err) {
|
|
|
|
if (fd >= 0 || err != exp_err)
|
|
|
|
return 0;
|
|
|
|
if (exp_msg && !strstr(buf, exp_msg))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return fd >= 0 ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts)
|
|
|
|
{
|
|
|
|
struct bpf_insn insns[] = {
|
|
|
|
BPF_MOV64_IMM(BPF_REG_0, 0),
|
|
|
|
BPF_EXIT_INSN()
|
|
|
|
};
|
|
|
|
const size_t insn_cnt = ARRAY_SIZE(insns);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (opts)
|
|
|
|
return libbpf_err(-EINVAL);
|
|
|
|
|
2022-06-27 21:15:16 +00:00
|
|
|
ret = probe_prog_load(prog_type, insns, insn_cnt, NULL, 0);
|
2021-12-17 17:12:00 +00:00
|
|
|
return libbpf_err(ret);
|
2019-01-17 15:27:53 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 18:31:09 +00:00
|
|
|
int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
|
|
|
|
const char *str_sec, size_t str_len)
|
2019-04-26 23:39:44 +00:00
|
|
|
{
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
struct btf_header hdr = {
|
|
|
|
.magic = BTF_MAGIC,
|
|
|
|
.version = BTF_VERSION,
|
|
|
|
.hdr_len = sizeof(struct btf_header),
|
|
|
|
.type_len = types_len,
|
|
|
|
.str_off = types_len,
|
|
|
|
.str_len = str_len,
|
|
|
|
};
|
|
|
|
int btf_fd, btf_len;
|
|
|
|
__u8 *raw_btf;
|
|
|
|
|
|
|
|
btf_len = hdr.hdr_len + hdr.type_len + hdr.str_len;
|
|
|
|
raw_btf = malloc(btf_len);
|
|
|
|
if (!raw_btf)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(raw_btf, &hdr, sizeof(hdr));
|
|
|
|
memcpy(raw_btf + hdr.hdr_len, raw_types, hdr.type_len);
|
|
|
|
memcpy(raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len);
|
|
|
|
|
2021-12-09 19:38:30 +00:00
|
|
|
btf_fd = bpf_btf_load(raw_btf, btf_len, NULL);
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
|
|
|
|
free(raw_btf);
|
2019-05-29 18:31:09 +00:00
|
|
|
return btf_fd;
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 18:29:17 +00:00
|
|
|
static int load_local_storage_btf(void)
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
{
|
|
|
|
const char strs[] = "\0bpf_spin_lock\0val\0cnt\0l";
|
2019-04-26 23:39:44 +00:00
|
|
|
/* struct bpf_spin_lock {
|
|
|
|
* int val;
|
|
|
|
* };
|
|
|
|
* struct val {
|
|
|
|
* int cnt;
|
|
|
|
* struct bpf_spin_lock l;
|
|
|
|
* };
|
|
|
|
*/
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
__u32 types[] = {
|
2019-04-26 23:39:44 +00:00
|
|
|
/* int */
|
|
|
|
BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
|
|
|
|
/* struct bpf_spin_lock */ /* [2] */
|
|
|
|
BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 4),
|
|
|
|
BTF_MEMBER_ENC(15, 1, 0), /* int val; */
|
|
|
|
/* struct val */ /* [3] */
|
|
|
|
BTF_TYPE_ENC(15, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
|
|
|
|
BTF_MEMBER_ENC(19, 1, 0), /* int cnt; */
|
|
|
|
BTF_MEMBER_ENC(23, 2, 32),/* struct bpf_spin_lock l; */
|
|
|
|
};
|
|
|
|
|
2019-05-29 18:31:09 +00:00
|
|
|
return libbpf__load_raw_btf((char *)types, sizeof(types),
|
libbpf: detect supported kernel BTF features and sanitize BTF
Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.
This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
- BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
- BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
- BTF_KIND_VAR -> BTF_KIND_INT
- BTF_KIND_DATASEC -> BTF_KIND_STRUCT
Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.
v2->v3:
- remove duplicate #defines from libbpf_util.h
v1->v2:
- add internal libbpf_internal.h w/ common stuff
- switch SK storage BTF to use new libbpf__probe_raw_btf()
Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-05-10 21:13:15 +00:00
|
|
|
strs, sizeof(strs));
|
2019-04-26 23:39:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 21:15:16 +00:00
|
|
|
static int probe_map_create(enum bpf_map_type map_type)
|
2019-01-17 15:27:54 +00:00
|
|
|
{
|
2021-12-17 17:12:00 +00:00
|
|
|
LIBBPF_OPTS(bpf_map_create_opts, opts);
|
|
|
|
int key_size, value_size, max_entries;
|
2019-04-26 23:39:44 +00:00
|
|
|
__u32 btf_key_type_id = 0, btf_value_type_id = 0;
|
2022-08-01 02:51:09 +00:00
|
|
|
int fd = -1, btf_fd = -1, fd_inner = -1, exp_err = 0, err = 0;
|
2021-12-17 17:12:00 +00:00
|
|
|
|
2019-01-17 15:27:54 +00:00
|
|
|
key_size = sizeof(__u32);
|
|
|
|
value_size = sizeof(__u32);
|
|
|
|
max_entries = 1;
|
|
|
|
|
|
|
|
switch (map_type) {
|
|
|
|
case BPF_MAP_TYPE_STACK_TRACE:
|
|
|
|
value_size = sizeof(__u64);
|
|
|
|
break;
|
|
|
|
case BPF_MAP_TYPE_LPM_TRIE:
|
|
|
|
key_size = sizeof(__u64);
|
|
|
|
value_size = sizeof(__u64);
|
2021-12-17 17:12:00 +00:00
|
|
|
opts.map_flags = BPF_F_NO_PREALLOC;
|
2019-01-17 15:27:54 +00:00
|
|
|
break;
|
|
|
|
case BPF_MAP_TYPE_CGROUP_STORAGE:
|
|
|
|
case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
|
|
|
|
key_size = sizeof(struct bpf_cgroup_storage_key);
|
|
|
|
value_size = sizeof(__u64);
|
|
|
|
max_entries = 0;
|
|
|
|
break;
|
|
|
|
case BPF_MAP_TYPE_QUEUE:
|
|
|
|
case BPF_MAP_TYPE_STACK:
|
|
|
|
key_size = 0;
|
|
|
|
break;
|
2019-04-26 23:39:44 +00:00
|
|
|
case BPF_MAP_TYPE_SK_STORAGE:
|
2020-08-25 18:29:17 +00:00
|
|
|
case BPF_MAP_TYPE_INODE_STORAGE:
|
2020-11-06 10:37:41 +00:00
|
|
|
case BPF_MAP_TYPE_TASK_STORAGE:
|
2022-10-26 04:28:56 +00:00
|
|
|
case BPF_MAP_TYPE_CGRP_STORAGE:
|
2019-04-26 23:39:44 +00:00
|
|
|
btf_key_type_id = 1;
|
|
|
|
btf_value_type_id = 3;
|
|
|
|
value_size = 8;
|
|
|
|
max_entries = 0;
|
2021-12-17 17:12:00 +00:00
|
|
|
opts.map_flags = BPF_F_NO_PREALLOC;
|
2020-08-25 18:29:17 +00:00
|
|
|
btf_fd = load_local_storage_btf();
|
2019-04-26 23:39:44 +00:00
|
|
|
if (btf_fd < 0)
|
2021-12-17 17:12:00 +00:00
|
|
|
return btf_fd;
|
2019-04-26 23:39:44 +00:00
|
|
|
break;
|
libbpf: Add BPF ring buffer support
Declaring and instantiating BPF ring buffer doesn't require any changes to
libbpf, as it's just another type of maps. So using existing BTF-defined maps
syntax with __uint(type, BPF_MAP_TYPE_RINGBUF) and __uint(max_elements,
<size-of-ring-buf>) is all that's necessary to create and use BPF ring buffer.
This patch adds BPF ring buffer consumer to libbpf. It is very similar to
perf_buffer implementation in terms of API, but also attempts to fix some
minor problems and inconveniences with existing perf_buffer API.
ring_buffer support both single ring buffer use case (with just using
ring_buffer__new()), as well as allows to add more ring buffers, each with its
own callback and context. This allows to efficiently poll and consume
multiple, potentially completely independent, ring buffers, using single
epoll instance.
The latter is actually a problem in practice for applications
that are using multiple sets of perf buffers. They have to create multiple
instances for struct perf_buffer and poll them independently or in a loop,
each approach having its own problems (e.g., inability to use a common poll
timeout). struct ring_buffer eliminates this problem by aggregating many
independent ring buffer instances under the single "ring buffer manager".
Second, perf_buffer's callback can't return error, so applications that need
to stop polling due to error in data or data signalling the end, have to use
extra mechanisms to signal that polling has to stop. ring_buffer's callback
can return error, which will be passed through back to user code and can be
acted upon appropariately.
Two APIs allow to consume ring buffer data:
- ring_buffer__poll(), which will wait for data availability notification
and will consume data only from reported ring buffer(s); this API allows
to efficiently use resources by reading data only when it becomes
available;
- ring_buffer__consume(), will attempt to read new records regardless of
data availablity notification sub-system. This API is useful for cases
when lowest latency is required, in expense of burning CPU resources.
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200529075424.3139988-3-andriin@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-05-29 07:54:21 +00:00
|
|
|
case BPF_MAP_TYPE_RINGBUF:
|
bpf: Add libbpf logic for user-space ring buffer
Now that all of the logic is in place in the kernel to support user-space
produced ring buffers, we can add the user-space logic to libbpf. This
patch therefore adds the following public symbols to libbpf:
struct user_ring_buffer *
user_ring_buffer__new(int map_fd,
const struct user_ring_buffer_opts *opts);
void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
__u32 size, int timeout_ms);
void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
void user_ring_buffer__discard(struct user_ring_buffer *rb,
void user_ring_buffer__free(struct user_ring_buffer *rb);
A user-space producer must first create a struct user_ring_buffer * object
with user_ring_buffer__new(), and can then reserve samples in the
ring buffer using one of the following two symbols:
void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
__u32 size, int timeout_ms);
With user_ring_buffer__reserve(), a pointer to a 'size' region of the ring
buffer will be returned if sufficient space is available in the buffer.
user_ring_buffer__reserve_blocking() provides similar semantics, but will
block for up to 'timeout_ms' in epoll_wait if there is insufficient space
in the buffer. This function has the guarantee from the kernel that it will
receive at least one event-notification per invocation to
bpf_ringbuf_drain(), provided that at least one sample is drained, and the
BPF program did not pass the BPF_RB_NO_WAKEUP flag to bpf_ringbuf_drain().
Once a sample is reserved, it must either be committed to the ring buffer
with user_ring_buffer__submit(), or discarded with
user_ring_buffer__discard().
Signed-off-by: David Vernet <void@manifault.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220920000100.477320-4-void@manifault.com
2022-09-20 00:00:59 +00:00
|
|
|
case BPF_MAP_TYPE_USER_RINGBUF:
|
libbpf: Add BPF ring buffer support
Declaring and instantiating BPF ring buffer doesn't require any changes to
libbpf, as it's just another type of maps. So using existing BTF-defined maps
syntax with __uint(type, BPF_MAP_TYPE_RINGBUF) and __uint(max_elements,
<size-of-ring-buf>) is all that's necessary to create and use BPF ring buffer.
This patch adds BPF ring buffer consumer to libbpf. It is very similar to
perf_buffer implementation in terms of API, but also attempts to fix some
minor problems and inconveniences with existing perf_buffer API.
ring_buffer support both single ring buffer use case (with just using
ring_buffer__new()), as well as allows to add more ring buffers, each with its
own callback and context. This allows to efficiently poll and consume
multiple, potentially completely independent, ring buffers, using single
epoll instance.
The latter is actually a problem in practice for applications
that are using multiple sets of perf buffers. They have to create multiple
instances for struct perf_buffer and poll them independently or in a loop,
each approach having its own problems (e.g., inability to use a common poll
timeout). struct ring_buffer eliminates this problem by aggregating many
independent ring buffer instances under the single "ring buffer manager".
Second, perf_buffer's callback can't return error, so applications that need
to stop polling due to error in data or data signalling the end, have to use
extra mechanisms to signal that polling has to stop. ring_buffer's callback
can return error, which will be passed through back to user code and can be
acted upon appropariately.
Two APIs allow to consume ring buffer data:
- ring_buffer__poll(), which will wait for data availability notification
and will consume data only from reported ring buffer(s); this API allows
to efficiently use resources by reading data only when it becomes
available;
- ring_buffer__consume(), will attempt to read new records regardless of
data availablity notification sub-system. This API is useful for cases
when lowest latency is required, in expense of burning CPU resources.
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200529075424.3139988-3-andriin@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-05-29 07:54:21 +00:00
|
|
|
key_size = 0;
|
|
|
|
value_size = 0;
|
2022-11-16 07:23:48 +00:00
|
|
|
max_entries = sysconf(_SC_PAGE_SIZE);
|
libbpf: Add BPF ring buffer support
Declaring and instantiating BPF ring buffer doesn't require any changes to
libbpf, as it's just another type of maps. So using existing BTF-defined maps
syntax with __uint(type, BPF_MAP_TYPE_RINGBUF) and __uint(max_elements,
<size-of-ring-buf>) is all that's necessary to create and use BPF ring buffer.
This patch adds BPF ring buffer consumer to libbpf. It is very similar to
perf_buffer implementation in terms of API, but also attempts to fix some
minor problems and inconveniences with existing perf_buffer API.
ring_buffer support both single ring buffer use case (with just using
ring_buffer__new()), as well as allows to add more ring buffers, each with its
own callback and context. This allows to efficiently poll and consume
multiple, potentially completely independent, ring buffers, using single
epoll instance.
The latter is actually a problem in practice for applications
that are using multiple sets of perf buffers. They have to create multiple
instances for struct perf_buffer and poll them independently or in a loop,
each approach having its own problems (e.g., inability to use a common poll
timeout). struct ring_buffer eliminates this problem by aggregating many
independent ring buffer instances under the single "ring buffer manager".
Second, perf_buffer's callback can't return error, so applications that need
to stop polling due to error in data or data signalling the end, have to use
extra mechanisms to signal that polling has to stop. ring_buffer's callback
can return error, which will be passed through back to user code and can be
acted upon appropariately.
Two APIs allow to consume ring buffer data:
- ring_buffer__poll(), which will wait for data availability notification
and will consume data only from reported ring buffer(s); this API allows
to efficiently use resources by reading data only when it becomes
available;
- ring_buffer__consume(), will attempt to read new records regardless of
data availablity notification sub-system. This API is useful for cases
when lowest latency is required, in expense of burning CPU resources.
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20200529075424.3139988-3-andriin@fb.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2020-05-29 07:54:21 +00:00
|
|
|
break;
|
2021-12-17 17:12:00 +00:00
|
|
|
case BPF_MAP_TYPE_STRUCT_OPS:
|
|
|
|
/* we'll get -ENOTSUPP for invalid BTF type ID for struct_ops */
|
|
|
|
opts.btf_vmlinux_value_type_id = 1;
|
|
|
|
exp_err = -524; /* -ENOTSUPP */
|
|
|
|
break;
|
|
|
|
case BPF_MAP_TYPE_BLOOM_FILTER:
|
|
|
|
key_size = 0;
|
|
|
|
max_entries = 1;
|
|
|
|
break;
|
2019-01-17 15:27:54 +00:00
|
|
|
case BPF_MAP_TYPE_HASH:
|
|
|
|
case BPF_MAP_TYPE_ARRAY:
|
|
|
|
case BPF_MAP_TYPE_PROG_ARRAY:
|
|
|
|
case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
|
|
|
|
case BPF_MAP_TYPE_PERCPU_HASH:
|
|
|
|
case BPF_MAP_TYPE_PERCPU_ARRAY:
|
|
|
|
case BPF_MAP_TYPE_CGROUP_ARRAY:
|
|
|
|
case BPF_MAP_TYPE_LRU_HASH:
|
|
|
|
case BPF_MAP_TYPE_LRU_PERCPU_HASH:
|
|
|
|
case BPF_MAP_TYPE_ARRAY_OF_MAPS:
|
|
|
|
case BPF_MAP_TYPE_HASH_OF_MAPS:
|
|
|
|
case BPF_MAP_TYPE_DEVMAP:
|
2019-07-26 16:06:57 +00:00
|
|
|
case BPF_MAP_TYPE_DEVMAP_HASH:
|
2019-01-17 15:27:54 +00:00
|
|
|
case BPF_MAP_TYPE_SOCKMAP:
|
|
|
|
case BPF_MAP_TYPE_CPUMAP:
|
|
|
|
case BPF_MAP_TYPE_XSKMAP:
|
|
|
|
case BPF_MAP_TYPE_SOCKHASH:
|
|
|
|
case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
|
|
|
|
break;
|
2021-12-17 17:12:00 +00:00
|
|
|
case BPF_MAP_TYPE_UNSPEC:
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
2019-01-17 15:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
|
|
|
|
map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
|
2021-11-24 19:32:31 +00:00
|
|
|
fd_inner = bpf_map_create(BPF_MAP_TYPE_HASH, NULL,
|
|
|
|
sizeof(__u32), sizeof(__u32), 1, NULL);
|
2019-01-17 15:27:54 +00:00
|
|
|
if (fd_inner < 0)
|
2021-12-17 17:12:00 +00:00
|
|
|
goto cleanup;
|
2021-11-24 19:32:31 +00:00
|
|
|
|
|
|
|
opts.inner_map_fd = fd_inner;
|
2021-12-17 17:12:00 +00:00
|
|
|
}
|
2019-01-17 15:27:54 +00:00
|
|
|
|
2021-12-17 17:12:00 +00:00
|
|
|
if (btf_fd >= 0) {
|
|
|
|
opts.btf_fd = btf_fd;
|
|
|
|
opts.btf_key_type_id = btf_key_type_id;
|
|
|
|
opts.btf_value_type_id = btf_value_type_id;
|
2019-01-17 15:27:54 +00:00
|
|
|
}
|
2021-12-17 17:12:00 +00:00
|
|
|
|
|
|
|
fd = bpf_map_create(map_type, NULL, key_size, value_size, max_entries, &opts);
|
|
|
|
err = -errno;
|
|
|
|
|
|
|
|
cleanup:
|
2019-01-17 15:27:54 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2021-12-17 17:12:00 +00:00
|
|
|
if (fd_inner >= 0)
|
|
|
|
close(fd_inner);
|
2019-04-26 23:39:44 +00:00
|
|
|
if (btf_fd >= 0)
|
|
|
|
close(btf_fd);
|
2019-01-17 15:27:54 +00:00
|
|
|
|
2021-12-17 17:12:00 +00:00
|
|
|
if (exp_err)
|
|
|
|
return fd < 0 && err == exp_err ? 1 : 0;
|
|
|
|
else
|
|
|
|
return fd >= 0 ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int libbpf_probe_bpf_map_type(enum bpf_map_type map_type, const void *opts)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (opts)
|
|
|
|
return libbpf_err(-EINVAL);
|
|
|
|
|
2022-06-27 21:15:16 +00:00
|
|
|
ret = probe_map_create(map_type);
|
2021-12-17 17:12:00 +00:00
|
|
|
return libbpf_err(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type, enum bpf_func_id helper_id,
|
|
|
|
const void *opts)
|
|
|
|
{
|
|
|
|
struct bpf_insn insns[] = {
|
|
|
|
BPF_EMIT_CALL((__u32)helper_id),
|
|
|
|
BPF_EXIT_INSN(),
|
|
|
|
};
|
|
|
|
const size_t insn_cnt = ARRAY_SIZE(insns);
|
|
|
|
char buf[4096];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (opts)
|
|
|
|
return libbpf_err(-EINVAL);
|
|
|
|
|
|
|
|
/* we can't successfully load all prog types to check for BPF helper
|
|
|
|
* support, so bail out with -EOPNOTSUPP error
|
|
|
|
*/
|
|
|
|
switch (prog_type) {
|
|
|
|
case BPF_PROG_TYPE_TRACING:
|
|
|
|
case BPF_PROG_TYPE_EXT:
|
|
|
|
case BPF_PROG_TYPE_LSM:
|
|
|
|
case BPF_PROG_TYPE_STRUCT_OPS:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
2022-06-27 21:15:16 +00:00
|
|
|
ret = probe_prog_load(prog_type, insns, insn_cnt, buf, sizeof(buf));
|
2021-12-17 17:12:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return libbpf_err(ret);
|
|
|
|
|
|
|
|
/* If BPF verifier doesn't recognize BPF helper ID (enum bpf_func_id)
|
|
|
|
* at all, it will emit something like "invalid func unknown#181".
|
|
|
|
* If BPF verifier recognizes BPF helper but it's not supported for
|
|
|
|
* given BPF program type, it will emit "unknown func bpf_sys_bpf#166".
|
|
|
|
* In both cases, provided combination of BPF program type and BPF
|
|
|
|
* helper is not supported by the kernel.
|
|
|
|
* In all other cases, probe_prog_load() above will either succeed (e.g.,
|
|
|
|
* because BPF helper happens to accept no input arguments or it
|
|
|
|
* accepts one input argument and initial PTR_TO_CTX is fine for
|
|
|
|
* that), or we'll get some more specific BPF verifier error about
|
|
|
|
* some unsatisfied conditions.
|
|
|
|
*/
|
|
|
|
if (ret == 0 && (strstr(buf, "invalid func ") || strstr(buf, "unknown func ")))
|
|
|
|
return 0;
|
|
|
|
return 1; /* assume supported */
|
2019-01-17 15:27:54 +00:00
|
|
|
}
|