mirror of
https://github.com/armbian/linux-cix.git
synced 2026-01-06 12:30:45 -08:00
bpf: add initial suite for selftests
Add a start of a test suite for kernel selftests. This moves test_verifier and test_maps over to tools/testing/selftests/bpf/ along with various code improvements and also adds a script for invoking test_bpf module. The test suite can simply be run via selftest framework, f.e.: # cd tools/testing/selftests/bpf/ # make # make run_tests Both test_verifier and test_maps were kind of misplaced in samples/bpf/ directory and we were looking into adding them to selftests for a while now, so it can be picked up by kbuild bot et al and hopefully also get more exposure and thus new test case additions. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
committed by
David S. Miller
parent
1a776b9ce8
commit
5aa5bd14c5
@@ -2521,6 +2521,8 @@ L: netdev@vger.kernel.org
|
||||
L: linux-kernel@vger.kernel.org
|
||||
S: Supported
|
||||
F: kernel/bpf/
|
||||
F: tools/testing/selftests/bpf/
|
||||
F: lib/test_bpf.c
|
||||
|
||||
BROADCOM B44 10/100 ETHERNET DRIVER
|
||||
M: Michael Chan <michael.chan@broadcom.com>
|
||||
@@ -8413,7 +8415,6 @@ F: include/uapi/linux/net_namespace.h
|
||||
F: tools/net/
|
||||
F: tools/testing/selftests/net/
|
||||
F: lib/random32.c
|
||||
F: lib/test_bpf.c
|
||||
|
||||
NETWORKING [IPv4/IPv6]
|
||||
M: "David S. Miller" <davem@davemloft.net>
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
obj- := dummy.o
|
||||
|
||||
# List of programs to build
|
||||
hostprogs-y := test_verifier test_maps
|
||||
hostprogs-y += sock_example
|
||||
hostprogs-y += fds_example
|
||||
hostprogs-y += sockex1
|
||||
@@ -28,8 +27,6 @@ hostprogs-y += test_current_task_under_cgroup
|
||||
hostprogs-y += trace_event
|
||||
hostprogs-y += sampleip
|
||||
|
||||
test_verifier-objs := test_verifier.o libbpf.o
|
||||
test_maps-objs := test_maps.o libbpf.o
|
||||
sock_example-objs := sock_example.o libbpf.o
|
||||
fds_example-objs := bpf_load.o libbpf.o fds_example.o
|
||||
sockex1-objs := bpf_load.o libbpf.o sockex1_user.o
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -218,6 +218,30 @@
|
||||
.off = OFF, \
|
||||
.imm = IMM })
|
||||
|
||||
/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */
|
||||
|
||||
#define BPF_LD_IMM64(DST, IMM) \
|
||||
BPF_LD_IMM64_RAW(DST, 0, IMM)
|
||||
|
||||
#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \
|
||||
((struct bpf_insn) { \
|
||||
.code = BPF_LD | BPF_DW | BPF_IMM, \
|
||||
.dst_reg = DST, \
|
||||
.src_reg = SRC, \
|
||||
.off = 0, \
|
||||
.imm = (__u32) (IMM) }), \
|
||||
((struct bpf_insn) { \
|
||||
.code = 0, /* zero is reserved opcode */ \
|
||||
.dst_reg = 0, \
|
||||
.src_reg = 0, \
|
||||
.off = 0, \
|
||||
.imm = ((__u64) (IMM)) >> 32 })
|
||||
|
||||
/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */
|
||||
|
||||
#define BPF_LD_MAP_FD(DST, MAP_FD) \
|
||||
BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD)
|
||||
|
||||
/* Program exit */
|
||||
|
||||
#define BPF_EXIT_INSN() \
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
TARGETS = breakpoints
|
||||
TARGETS = bpf
|
||||
TARGETS += breakpoints
|
||||
TARGETS += capabilities
|
||||
TARGETS += cpu-hotplug
|
||||
TARGETS += efivarfs
|
||||
|
||||
2
tools/testing/selftests/bpf/.gitignore
vendored
Normal file
2
tools/testing/selftests/bpf/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
test_verifier
|
||||
test_maps
|
||||
13
tools/testing/selftests/bpf/Makefile
Normal file
13
tools/testing/selftests/bpf/Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
CFLAGS += -Wall -O2
|
||||
|
||||
test_objs = test_verifier test_maps
|
||||
|
||||
TEST_PROGS := test_verifier test_maps test_kmod.sh
|
||||
TEST_FILES := $(test_objs)
|
||||
|
||||
all: $(test_objs)
|
||||
|
||||
include ../lib.mk
|
||||
|
||||
clean:
|
||||
$(RM) $(test_objs)
|
||||
108
tools/testing/selftests/bpf/bpf_sys.h
Normal file
108
tools/testing/selftests/bpf/bpf_sys.h
Normal file
@@ -0,0 +1,108 @@
|
||||
#ifndef __BPF_SYS__
|
||||
#define __BPF_SYS__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <sys/syscall.h>
|
||||
|
||||
#include <linux/bpf.h>
|
||||
|
||||
static inline __u64 bpf_ptr_to_u64(const void *ptr)
|
||||
{
|
||||
return (__u64)(unsigned long) ptr;
|
||||
}
|
||||
|
||||
static inline int bpf(int cmd, union bpf_attr *attr, unsigned int size)
|
||||
{
|
||||
#ifdef __NR_bpf
|
||||
return syscall(__NR_bpf, cmd, attr, size);
|
||||
#else
|
||||
fprintf(stderr, "No bpf syscall, kernel headers too old?\n");
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline int bpf_map_lookup(int fd, const void *key, void *value)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_fd = fd;
|
||||
attr.key = bpf_ptr_to_u64(key);
|
||||
attr.value = bpf_ptr_to_u64(value);
|
||||
|
||||
return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_map_update(int fd, const void *key, const void *value,
|
||||
uint64_t flags)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_fd = fd;
|
||||
attr.key = bpf_ptr_to_u64(key);
|
||||
attr.value = bpf_ptr_to_u64(value);
|
||||
attr.flags = flags;
|
||||
|
||||
return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_map_delete(int fd, const void *key)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_fd = fd;
|
||||
attr.key = bpf_ptr_to_u64(key);
|
||||
|
||||
return bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_map_next_key(int fd, const void *key, void *next_key)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_fd = fd;
|
||||
attr.key = bpf_ptr_to_u64(key);
|
||||
attr.next_key = bpf_ptr_to_u64(next_key);
|
||||
|
||||
return bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_map_create(enum bpf_map_type type, uint32_t size_key,
|
||||
uint32_t size_value, uint32_t max_elem,
|
||||
uint32_t flags)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.map_type = type;
|
||||
attr.key_size = size_key;
|
||||
attr.value_size = size_value;
|
||||
attr.max_entries = max_elem;
|
||||
attr.map_flags = flags;
|
||||
|
||||
return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
static inline int bpf_prog_load(enum bpf_prog_type type,
|
||||
const struct bpf_insn *insns, size_t size_insns,
|
||||
const char *license, char *log, size_t size_log)
|
||||
{
|
||||
union bpf_attr attr = {};
|
||||
|
||||
attr.prog_type = type;
|
||||
attr.insns = bpf_ptr_to_u64(insns);
|
||||
attr.insn_cnt = size_insns / sizeof(struct bpf_insn);
|
||||
attr.license = bpf_ptr_to_u64(license);
|
||||
|
||||
if (size_log > 0) {
|
||||
attr.log_buf = bpf_ptr_to_u64(log);
|
||||
attr.log_size = size_log;
|
||||
attr.log_level = 1;
|
||||
log[0] = 0;
|
||||
}
|
||||
|
||||
return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
|
||||
}
|
||||
|
||||
#endif /* __BPF_SYS__ */
|
||||
5
tools/testing/selftests/bpf/config
Normal file
5
tools/testing/selftests/bpf/config
Normal file
@@ -0,0 +1,5 @@
|
||||
CONFIG_BPF=y
|
||||
CONFIG_BPF_SYSCALL=y
|
||||
CONFIG_NET_CLS_BPF=m
|
||||
CONFIG_BPF_EVENTS=y
|
||||
CONFIG_TEST_BPF=m
|
||||
39
tools/testing/selftests/bpf/test_kmod.sh
Executable file
39
tools/testing/selftests/bpf/test_kmod.sh
Executable file
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
|
||||
SRC_TREE=../../../../
|
||||
|
||||
test_run()
|
||||
{
|
||||
sysctl -w net.core.bpf_jit_enable=$1 2>&1 > /dev/null
|
||||
sysctl -w net.core.bpf_jit_harden=$2 2>&1 > /dev/null
|
||||
|
||||
echo "[ JIT enabled:$1 hardened:$2 ]"
|
||||
dmesg -C
|
||||
insmod $SRC_TREE/lib/test_bpf.ko 2> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
rc=1
|
||||
fi
|
||||
rmmod test_bpf 2> /dev/null
|
||||
dmesg | grep FAIL
|
||||
}
|
||||
|
||||
test_save()
|
||||
{
|
||||
JE=`sysctl -n net.core.bpf_jit_enable`
|
||||
JH=`sysctl -n net.core.bpf_jit_harden`
|
||||
}
|
||||
|
||||
test_restore()
|
||||
{
|
||||
sysctl -w net.core.bpf_jit_enable=$JE 2>&1 > /dev/null
|
||||
sysctl -w net.core.bpf_jit_harden=$JH 2>&1 > /dev/null
|
||||
}
|
||||
|
||||
rc=0
|
||||
test_save
|
||||
test_run 0 0
|
||||
test_run 1 0
|
||||
test_run 1 1
|
||||
test_run 1 2
|
||||
test_restore
|
||||
exit $rc
|
||||
525
tools/testing/selftests/bpf/test_maps.c
Normal file
525
tools/testing/selftests/bpf/test_maps.c
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user