2019-08-15 22:45:43 -07:00
|
|
|
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
selftests/bpf: add bpf-gcc support
Now that binutils and gcc support for BPF is upstream, make use of it in
BPF selftests using alu32-like approach. Share as much as possible of
CFLAGS calculation with clang.
Fixes only obvious issues, leaving more complex ones for later:
- Use gcc-provided bpf-helpers.h instead of manually defining the
helpers, change bpf_helpers.h include guard to avoid conflict.
- Include <linux/stddef.h> for __always_inline.
- Add $(OUTPUT)/../usr/include to include path in order to use local
kernel headers instead of system kernel headers when building with O=.
In order to activate the bpf-gcc support, one needs to configure
binutils and gcc with --target=bpf and make them available in $PATH. In
particular, gcc must be installed as `bpf-gcc`, which is the default.
Right now with binutils 25a2915e8dba and gcc r275589 only a handful of
tests work:
# ./test_progs_bpf_gcc
# Summary: 7/39 PASSED, 1 SKIPPED, 98 FAILED
The reason for those failures are as follows:
- Build errors:
- `error: too many function arguments for eBPF` for __always_inline
functions read_str_var and read_map_var - must be inlining issue,
and for process_l3_headers_v6, which relies on optimizing away
function arguments.
- `error: indirect call in function, which are not supported by eBPF`
where there are no obvious indirect calls in the source calls, e.g.
in __encap_ipip_none.
- `error: field 'lock' has incomplete type` for fields of `struct
bpf_spin_lock` type - bpf_spin_lock is re#defined by bpf-helpers.h,
so its usage is sensitive to order of #includes.
- `error: eBPF stack limit exceeded` in sysctl_tcp_mem.
- Load errors:
- Missing object files due to above build errors.
- `libbpf: failed to create map (name: 'test_ver.bss')`.
- `libbpf: object file doesn't contain bpf program`.
- `libbpf: Program '.text' contains unrecognized relo data pointing to
section 0`.
- `libbpf: BTF is required, but is missing or corrupted` - no BTF
support in gcc yet.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Jose E. Marchesi <jose.marchesi@oracle.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-09-12 18:05:43 +02:00
|
|
|
#ifndef __BPF_HELPERS__
|
|
|
|
#define __BPF_HELPERS__
|
2014-12-01 15:06:37 -08:00
|
|
|
|
2019-10-06 20:07:38 -07:00
|
|
|
#include "bpf_helper_defs.h"
|
|
|
|
|
2019-07-05 08:50:10 -07:00
|
|
|
#define __uint(name, val) int (*name)[val]
|
2019-10-03 21:02:11 -07:00
|
|
|
#define __type(name, val) typeof(val) *name
|
2019-07-05 08:50:10 -07:00
|
|
|
|
2019-05-23 14:53:54 +02:00
|
|
|
/* helper macro to print out debug messages */
|
|
|
|
#define bpf_printk(fmt, ...) \
|
|
|
|
({ \
|
|
|
|
char ____fmt[] = fmt; \
|
|
|
|
bpf_trace_printk(____fmt, sizeof(____fmt), \
|
|
|
|
##__VA_ARGS__); \
|
|
|
|
})
|
|
|
|
|
selftests/bpf: add bpf-gcc support
Now that binutils and gcc support for BPF is upstream, make use of it in
BPF selftests using alu32-like approach. Share as much as possible of
CFLAGS calculation with clang.
Fixes only obvious issues, leaving more complex ones for later:
- Use gcc-provided bpf-helpers.h instead of manually defining the
helpers, change bpf_helpers.h include guard to avoid conflict.
- Include <linux/stddef.h> for __always_inline.
- Add $(OUTPUT)/../usr/include to include path in order to use local
kernel headers instead of system kernel headers when building with O=.
In order to activate the bpf-gcc support, one needs to configure
binutils and gcc with --target=bpf and make them available in $PATH. In
particular, gcc must be installed as `bpf-gcc`, which is the default.
Right now with binutils 25a2915e8dba and gcc r275589 only a handful of
tests work:
# ./test_progs_bpf_gcc
# Summary: 7/39 PASSED, 1 SKIPPED, 98 FAILED
The reason for those failures are as follows:
- Build errors:
- `error: too many function arguments for eBPF` for __always_inline
functions read_str_var and read_map_var - must be inlining issue,
and for process_l3_headers_v6, which relies on optimizing away
function arguments.
- `error: indirect call in function, which are not supported by eBPF`
where there are no obvious indirect calls in the source calls, e.g.
in __encap_ipip_none.
- `error: field 'lock' has incomplete type` for fields of `struct
bpf_spin_lock` type - bpf_spin_lock is re#defined by bpf-helpers.h,
so its usage is sensitive to order of #includes.
- `error: eBPF stack limit exceeded` in sysctl_tcp_mem.
- Load errors:
- Missing object files due to above build errors.
- `libbpf: failed to create map (name: 'test_ver.bss')`.
- `libbpf: object file doesn't contain bpf program`.
- `libbpf: Program '.text' contains unrecognized relo data pointing to
section 0`.
- `libbpf: BTF is required, but is missing or corrupted` - no BTF
support in gcc yet.
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Jose E. Marchesi <jose.marchesi@oracle.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
2019-09-12 18:05:43 +02:00
|
|
|
/* helper macro to place programs, maps, license in
|
|
|
|
* different sections in elf_bpf file. Section names
|
|
|
|
* are interpreted by elf_bpf loader
|
|
|
|
*/
|
|
|
|
#define SEC(NAME) __attribute__((section(NAME), used))
|
|
|
|
|
2014-12-01 15:06:37 -08:00
|
|
|
/* a helper structure used by eBPF C program
|
2019-10-08 10:59:37 -07:00
|
|
|
* to describe BPF map attributes to libbpf loader
|
2014-12-01 15:06:37 -08:00
|
|
|
*/
|
|
|
|
struct bpf_map_def {
|
|
|
|
unsigned int type;
|
|
|
|
unsigned int key_size;
|
|
|
|
unsigned int value_size;
|
|
|
|
unsigned int max_entries;
|
2016-03-07 21:57:20 -08:00
|
|
|
unsigned int map_flags;
|
2014-12-01 15:06:37 -08:00
|
|
|
};
|
|
|
|
|
2019-08-07 14:39:52 -07:00
|
|
|
/*
|
2019-10-08 10:59:38 -07:00
|
|
|
* bpf_core_read() abstracts away bpf_probe_read() call and captures offset
|
2019-08-07 14:39:52 -07:00
|
|
|
* relocation for source address using __builtin_preserve_access_index()
|
|
|
|
* built-in, provided by Clang.
|
|
|
|
*
|
|
|
|
* __builtin_preserve_access_index() takes as an argument an expression of
|
|
|
|
* taking an address of a field within struct/union. It makes compiler emit
|
|
|
|
* a relocation, which records BTF type ID describing root struct/union and an
|
|
|
|
* accessor string which describes exact embedded field that was used to take
|
|
|
|
* an address. See detailed description of this relocation format and
|
|
|
|
* semantics in comments to struct bpf_offset_reloc in libbpf_internal.h.
|
|
|
|
*
|
|
|
|
* This relocation allows libbpf to adjust BPF instruction to use correct
|
|
|
|
* actual field offset, based on target kernel BTF type that matches original
|
|
|
|
* (local) BTF, used to record relocation.
|
|
|
|
*/
|
2019-10-08 10:59:38 -07:00
|
|
|
#define bpf_core_read(dst, sz, src) \
|
|
|
|
bpf_probe_read(dst, sz, \
|
|
|
|
(const void *)__builtin_preserve_access_index(src))
|
2019-08-07 14:39:52 -07:00
|
|
|
|
2014-12-01 15:06:37 -08:00
|
|
|
#endif
|