2019-01-17 22:27:53 +07: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 22:27:55 +07:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2019-01-17 22:27:53 +07: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 22:27:55 +07:00
|
|
|
#include <net/if.h>
|
2019-01-17 22:27:53 +07:00
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
2019-04-27 06:39:44 +07:00
|
|
|
#include <linux/btf.h>
|
2019-01-17 22:27:53 +07:00
|
|
|
#include <linux/filter.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
|
|
|
|
#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-11 04:13:15 +07:00
|
|
|
#include "libbpf_internal.h"
|
2019-01-17 22:27:53 +07:00
|
|
|
|
libbpf: Poison kernel-only integer types
It's been a recurring issue with types like u32 slipping into libbpf source
code accidentally. This is not detected during builds inside kernel source
tree, but becomes a compilation error in libbpf's Github repo. Libbpf is
supposed to use only __{s,u}{8,16,32,64} typedefs, so poison {s,u}{8,16,32,64}
explicitly in every .c file. Doing that in a bit more centralized way, e.g.,
inside libbpf_internal.h breaks selftests, which are both using kernel u32 and
libbpf_internal.h.
This patch also fixes a new u32 occurence in libbpf.c, added recently.
Fixes: 590a00888250 ("bpf: libbpf: Add STRUCT_OPS support")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20200110181916.271446-1-andriin@fb.com
2020-01-11 01:19:16 +07:00
|
|
|
/* make sure libbpf doesn't use kernel-only integer typedefs */
|
|
|
|
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
|
|
|
|
|
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 22:27:55 +07:00
|
|
|
static bool grep(const char *buffer, const char *pattern)
|
|
|
|
{
|
|
|
|
return !!strstr(buffer, pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_vendor_id(int ifindex)
|
|
|
|
{
|
|
|
|
char ifname[IF_NAMESIZE], path[64], buf[8];
|
|
|
|
ssize_t len;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (!if_indextoname(ifindex, ifname))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "/sys/class/net/%s/device/vendor", ifname);
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
len = read(fd, buf, sizeof(buf));
|
|
|
|
close(fd);
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
if (len >= (ssize_t)sizeof(buf))
|
|
|
|
return -1;
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
|
|
|
return strtol(buf, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2019-01-17 22:27:53 +07:00
|
|
|
static int get_kernel_version(void)
|
|
|
|
{
|
|
|
|
int version, subversion, patchlevel;
|
|
|
|
struct utsname utsn;
|
|
|
|
|
|
|
|
/* Return 0 on failure, and attempt to probe with empty kversion */
|
|
|
|
if (uname(&utsn))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (sscanf(utsn.release, "%d.%d.%d",
|
|
|
|
&version, &subversion, &patchlevel) != 3)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (version << 16) + (subversion << 8) + patchlevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
|
|
|
|
size_t insns_cnt, char *buf, size_t buf_len, __u32 ifindex)
|
|
|
|
{
|
|
|
|
struct bpf_load_program_attr xattr = {};
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
switch (prog_type) {
|
|
|
|
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
|
|
|
|
xattr.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
|
|
|
|
break;
|
|
|
|
case BPF_PROG_TYPE_KPROBE:
|
|
|
|
xattr.kern_version = get_kernel_version();
|
|
|
|
break;
|
|
|
|
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-27 01:49:50 +07:00
|
|
|
case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
|
2019-01-17 22:27:53 +07:00
|
|
|
case BPF_PROG_TYPE_LWT_SEG6LOCAL:
|
|
|
|
case BPF_PROG_TYPE_LIRC_MODE2:
|
|
|
|
case BPF_PROG_TYPE_SK_REUSEPORT:
|
|
|
|
case BPF_PROG_TYPE_FLOW_DISSECTOR:
|
2019-03-09 00:15:26 +07:00
|
|
|
case BPF_PROG_TYPE_CGROUP_SYSCTL:
|
2019-06-28 03:38:49 +07:00
|
|
|
case BPF_PROG_TYPE_CGROUP_SOCKOPT:
|
2019-10-31 05:32:12 +07:00
|
|
|
case BPF_PROG_TYPE_TRACING:
|
bpf: libbpf: Add STRUCT_OPS support
This patch adds BPF STRUCT_OPS support to libbpf.
The only sec_name convention is SEC(".struct_ops") to identify the
struct_ops implemented in BPF,
e.g. To implement a tcp_congestion_ops:
SEC(".struct_ops")
struct tcp_congestion_ops dctcp = {
.init = (void *)dctcp_init, /* <-- a bpf_prog */
/* ... some more func prts ... */
.name = "bpf_dctcp",
};
Each struct_ops is defined as a global variable under SEC(".struct_ops")
as above. libbpf creates a map for each variable and the variable name
is the map's name. Multiple struct_ops is supported under
SEC(".struct_ops").
In the bpf_object__open phase, libbpf will look for the SEC(".struct_ops")
section and find out what is the btf-type the struct_ops is
implementing. Note that the btf-type here is referring to
a type in the bpf_prog.o's btf. A "struct bpf_map" is added
by bpf_object__add_map() as other maps do. It will then
collect (through SHT_REL) where are the bpf progs that the
func ptrs are referring to. No btf_vmlinux is needed in
the open phase.
In the bpf_object__load phase, the map-fields, which depend
on the btf_vmlinux, are initialized (in bpf_map__init_kern_struct_ops()).
It will also set the prog->type, prog->attach_btf_id, and
prog->expected_attach_type. Thus, the prog's properties do
not rely on its section name.
[ Currently, the bpf_prog's btf-type ==> btf_vmlinux's btf-type matching
process is as simple as: member-name match + btf-kind match + size match.
If these matching conditions fail, libbpf will reject.
The current targeting support is "struct tcp_congestion_ops" which
most of its members are function pointers.
The member ordering of the bpf_prog's btf-type can be different from
the btf_vmlinux's btf-type. ]
Then, all obj->maps are created as usual (in bpf_object__create_maps()).
Once the maps are created and prog's properties are all set,
the libbpf will proceed to load all the progs.
bpf_map__attach_struct_ops() is added to register a struct_ops
map to a kernel subsystem.
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200109003514.3856730-1-kafai@fb.com
2020-01-09 07:35:14 +07:00
|
|
|
case BPF_PROG_TYPE_STRUCT_OPS:
|
2020-01-21 07:53:47 +07:00
|
|
|
case BPF_PROG_TYPE_EXT:
|
2020-03-29 07:43:49 +07:00
|
|
|
case BPF_PROG_TYPE_LSM:
|
2019-01-17 22:27:53 +07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xattr.prog_type = prog_type;
|
|
|
|
xattr.insns = insns;
|
|
|
|
xattr.insns_cnt = insns_cnt;
|
|
|
|
xattr.license = "GPL";
|
|
|
|
xattr.prog_ifindex = ifindex;
|
|
|
|
|
|
|
|
fd = bpf_load_program_xattr(&xattr, buf, buf_len);
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bpf_probe_prog_type(enum bpf_prog_type prog_type, __u32 ifindex)
|
|
|
|
{
|
|
|
|
struct bpf_insn insns[2] = {
|
|
|
|
BPF_MOV64_IMM(BPF_REG_0, 0),
|
|
|
|
BPF_EXIT_INSN()
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ifindex && prog_type == BPF_PROG_TYPE_SCHED_CLS)
|
|
|
|
/* nfp returns -EINVAL on exit(0) with TC offload */
|
|
|
|
insns[0].imm = 2;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
probe_load(prog_type, insns, ARRAY_SIZE(insns), NULL, 0, ifindex);
|
|
|
|
|
|
|
|
return errno != EINVAL && errno != EOPNOTSUPP;
|
|
|
|
}
|
2019-01-17 22:27:54 +07:00
|
|
|
|
2019-05-30 01:31:09 +07:00
|
|
|
int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
|
|
|
|
const char *str_sec, size_t str_len)
|
2019-04-27 06:39:44 +07: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-11 04:13:15 +07: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);
|
|
|
|
|
|
|
|
btf_fd = bpf_load_btf(raw_btf, btf_len, NULL, 0, false);
|
|
|
|
|
|
|
|
free(raw_btf);
|
2019-05-30 01:31:09 +07: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-11 04:13:15 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int load_sk_storage_btf(void)
|
|
|
|
{
|
|
|
|
const char strs[] = "\0bpf_spin_lock\0val\0cnt\0l";
|
2019-04-27 06:39:44 +07: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-11 04:13:15 +07:00
|
|
|
__u32 types[] = {
|
2019-04-27 06:39:44 +07: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-30 01:31:09 +07: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-11 04:13:15 +07:00
|
|
|
strs, sizeof(strs));
|
2019-04-27 06:39:44 +07:00
|
|
|
}
|
|
|
|
|
2019-01-17 22:27:54 +07:00
|
|
|
bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)
|
|
|
|
{
|
|
|
|
int key_size, value_size, max_entries, map_flags;
|
2019-04-27 06:39:44 +07:00
|
|
|
__u32 btf_key_type_id = 0, btf_value_type_id = 0;
|
2019-01-17 22:27:54 +07:00
|
|
|
struct bpf_create_map_attr attr = {};
|
2019-04-27 06:39:44 +07:00
|
|
|
int fd = -1, btf_fd = -1, fd_inner;
|
2019-01-17 22:27:54 +07:00
|
|
|
|
|
|
|
key_size = sizeof(__u32);
|
|
|
|
value_size = sizeof(__u32);
|
|
|
|
max_entries = 1;
|
|
|
|
map_flags = 0;
|
|
|
|
|
|
|
|
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);
|
|
|
|
map_flags = BPF_F_NO_PREALLOC;
|
|
|
|
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-27 06:39:44 +07:00
|
|
|
case BPF_MAP_TYPE_SK_STORAGE:
|
|
|
|
btf_key_type_id = 1;
|
|
|
|
btf_value_type_id = 3;
|
|
|
|
value_size = 8;
|
|
|
|
max_entries = 0;
|
|
|
|
map_flags = BPF_F_NO_PREALLOC;
|
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-11 04:13:15 +07:00
|
|
|
btf_fd = load_sk_storage_btf();
|
2019-04-27 06:39:44 +07:00
|
|
|
if (btf_fd < 0)
|
|
|
|
return false;
|
|
|
|
break;
|
2019-01-17 22:27:54 +07:00
|
|
|
case BPF_MAP_TYPE_UNSPEC:
|
|
|
|
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 23:06:57 +07:00
|
|
|
case BPF_MAP_TYPE_DEVMAP_HASH:
|
2019-01-17 22:27:54 +07: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:
|
bpf: libbpf: Add STRUCT_OPS support
This patch adds BPF STRUCT_OPS support to libbpf.
The only sec_name convention is SEC(".struct_ops") to identify the
struct_ops implemented in BPF,
e.g. To implement a tcp_congestion_ops:
SEC(".struct_ops")
struct tcp_congestion_ops dctcp = {
.init = (void *)dctcp_init, /* <-- a bpf_prog */
/* ... some more func prts ... */
.name = "bpf_dctcp",
};
Each struct_ops is defined as a global variable under SEC(".struct_ops")
as above. libbpf creates a map for each variable and the variable name
is the map's name. Multiple struct_ops is supported under
SEC(".struct_ops").
In the bpf_object__open phase, libbpf will look for the SEC(".struct_ops")
section and find out what is the btf-type the struct_ops is
implementing. Note that the btf-type here is referring to
a type in the bpf_prog.o's btf. A "struct bpf_map" is added
by bpf_object__add_map() as other maps do. It will then
collect (through SHT_REL) where are the bpf progs that the
func ptrs are referring to. No btf_vmlinux is needed in
the open phase.
In the bpf_object__load phase, the map-fields, which depend
on the btf_vmlinux, are initialized (in bpf_map__init_kern_struct_ops()).
It will also set the prog->type, prog->attach_btf_id, and
prog->expected_attach_type. Thus, the prog's properties do
not rely on its section name.
[ Currently, the bpf_prog's btf-type ==> btf_vmlinux's btf-type matching
process is as simple as: member-name match + btf-kind match + size match.
If these matching conditions fail, libbpf will reject.
The current targeting support is "struct tcp_congestion_ops" which
most of its members are function pointers.
The member ordering of the bpf_prog's btf-type can be different from
the btf_vmlinux's btf-type. ]
Then, all obj->maps are created as usual (in bpf_object__create_maps()).
Once the maps are created and prog's properties are all set,
the libbpf will proceed to load all the progs.
bpf_map__attach_struct_ops() is added to register a struct_ops
map to a kernel subsystem.
Signed-off-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200109003514.3856730-1-kafai@fb.com
2020-01-09 07:35:14 +07:00
|
|
|
case BPF_MAP_TYPE_STRUCT_OPS:
|
2019-01-17 22:27:54 +07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
|
|
|
|
map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
|
|
|
|
/* TODO: probe for device, once libbpf has a function to create
|
|
|
|
* map-in-map for offload
|
|
|
|
*/
|
|
|
|
if (ifindex)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fd_inner = bpf_create_map(BPF_MAP_TYPE_HASH,
|
|
|
|
sizeof(__u32), sizeof(__u32), 1, 0);
|
|
|
|
if (fd_inner < 0)
|
|
|
|
return false;
|
|
|
|
fd = bpf_create_map_in_map(map_type, NULL, sizeof(__u32),
|
|
|
|
fd_inner, 1, 0);
|
|
|
|
close(fd_inner);
|
|
|
|
} else {
|
|
|
|
/* Note: No other restriction on map type probes for offload */
|
|
|
|
attr.map_type = map_type;
|
|
|
|
attr.key_size = key_size;
|
|
|
|
attr.value_size = value_size;
|
|
|
|
attr.max_entries = max_entries;
|
|
|
|
attr.map_flags = map_flags;
|
|
|
|
attr.map_ifindex = ifindex;
|
2019-04-27 06:39:44 +07:00
|
|
|
if (btf_fd >= 0) {
|
|
|
|
attr.btf_fd = btf_fd;
|
|
|
|
attr.btf_key_type_id = btf_key_type_id;
|
|
|
|
attr.btf_value_type_id = btf_value_type_id;
|
|
|
|
}
|
2019-01-17 22:27:54 +07:00
|
|
|
|
|
|
|
fd = bpf_create_map_xattr(&attr);
|
|
|
|
}
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2019-04-27 06:39:44 +07:00
|
|
|
if (btf_fd >= 0)
|
|
|
|
close(btf_fd);
|
2019-01-17 22:27:54 +07:00
|
|
|
|
|
|
|
return fd >= 0;
|
|
|
|
}
|
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 22:27:55 +07:00
|
|
|
|
|
|
|
bool bpf_probe_helper(enum bpf_func_id id, enum bpf_prog_type prog_type,
|
|
|
|
__u32 ifindex)
|
|
|
|
{
|
|
|
|
struct bpf_insn insns[2] = {
|
|
|
|
BPF_EMIT_CALL(id),
|
|
|
|
BPF_EXIT_INSN()
|
|
|
|
};
|
|
|
|
char buf[4096] = {};
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
probe_load(prog_type, insns, ARRAY_SIZE(insns), buf, sizeof(buf),
|
|
|
|
ifindex);
|
|
|
|
res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ");
|
|
|
|
|
|
|
|
if (ifindex) {
|
|
|
|
switch (get_vendor_id(ifindex)) {
|
|
|
|
case 0x19ee: /* Netronome specific */
|
|
|
|
res = res && !grep(buf, "not supported by FW") &&
|
|
|
|
!grep(buf, "unsupported function id");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2020-01-08 23:23:52 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Probe for availability of kernel commit (5.3):
|
|
|
|
*
|
|
|
|
* c04c0d2b968a ("bpf: increase complexity limit and maximum program size")
|
|
|
|
*/
|
|
|
|
bool bpf_probe_large_insn_limit(__u32 ifindex)
|
|
|
|
{
|
|
|
|
struct bpf_insn insns[BPF_MAXINSNS + 1];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < BPF_MAXINSNS; i++)
|
|
|
|
insns[i] = BPF_MOV64_IMM(BPF_REG_0, 1);
|
|
|
|
insns[BPF_MAXINSNS] = BPF_EXIT_INSN();
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
probe_load(BPF_PROG_TYPE_SCHED_CLS, insns, ARRAY_SIZE(insns), NULL, 0,
|
|
|
|
ifindex);
|
|
|
|
|
|
|
|
return errno != E2BIG && errno != EINVAL;
|
|
|
|
}
|