Commit 5921a0fc authored by David S. Miller's avatar David S. Miller

Merge branch 'bpf-selftests'

Daniel Borkmann says:

====================
Move to BPF selftests

This set improves the test_verifier and test_maps suite and moves
it over to a new BPF selftest directory, so we can keep improving
it under kernel selftest umbrella. This also integrates a test
script for checking test_bpf.ko under various JIT options.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 1441dc99 5aa5bd14
......@@ -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
......
......@@ -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() \
......
TARGETS = breakpoints
TARGETS = bpf
TARGETS += breakpoints
TARGETS += capabilities
TARGETS += cpu-hotplug
TARGETS += efivarfs
......
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)
#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__ */
CONFIG_BPF=y
CONFIG_BPF_SYSCALL=y
CONFIG_NET_CLS_BPF=m
CONFIG_BPF_EVENTS=y
CONFIG_TEST_BPF=m
#!/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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment