bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
/*
* Testsuite for eBPF verifier
*
* Copyright ( c ) 2014 PLUMgrid , http : //plumgrid.com
2017-12-15 04:55:07 +03:00
* Copyright ( c ) 2017 Facebook
2018-10-02 23:35:38 +03:00
* Copyright ( c ) 2018 Covalent IO , Inc . http : //covalent.io
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation .
*/
2016-10-17 15:28:36 +03:00
bpf: fix byte order test in test_verifier
We really must check with #if __BYTE_ORDER == XYZ instead of
just presence of #ifdef __LITTLE_ENDIAN. I noticed that when
actually running this on big endian machine, the latter test
resolves to true for user space, same for #ifdef __BIG_ENDIAN.
E.g., looking at endian.h from libc, both are also defined
there, so we really must test this against __BYTE_ORDER instead
for proper insns selection. For the kernel, such checks are
fine though e.g. see 13da9e200fe4 ("Revert "endian: #define
__BYTE_ORDER"") and 415586c9e6d3 ("UAPI: fix endianness conditionals
in M32R's asm/stat.h") for some more context, but not for
user space. Lets also make sure to properly include endian.h.
After that, suite passes for me:
./test_verifier: ELF 64-bit MSB executable, [...]
Linux foo 4.13.0-rc3+ #4 SMP Fri Aug 4 06:59:30 EDT 2017 s390x s390x s390x GNU/Linux
Before fix: Summary: 505 PASSED, 11 FAILED
After fix: Summary: 516 PASSED, 0 FAILED
Fixes: 18f3d6be6be1 ("selftests/bpf: Add test cases to test narrower ctx field loads")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong <yhs@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-04 23:24:41 +03:00
# include <endian.h>
2017-03-11 09:05:55 +03:00
# include <asm/types.h>
# include <linux/types.h>
2017-02-10 02:21:44 +03:00
# include <stdint.h>
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
# include <stdio.h>
2017-02-10 02:21:44 +03:00
# include <stdlib.h>
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
# include <unistd.h>
# include <errno.h>
# include <string.h>
2015-03-13 21:57:43 +03:00
# include <stddef.h>
2015-10-08 08:23:23 +03:00
# include <stdbool.h>
2016-10-17 15:28:36 +03:00
# include <sched.h>
2018-01-27 01:33:48 +03:00
# include <limits.h>
2019-01-03 02:58:35 +03:00
# include <assert.h>
2016-10-17 15:28:36 +03:00
2017-02-10 02:21:37 +03:00
# include <sys/capability.h>
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2016-10-17 15:28:36 +03:00
# include <linux/unistd.h>
# include <linux/filter.h>
# include <linux/bpf_perf_event.h>
# include <linux/bpf.h>
2018-01-18 03:52:03 +03:00
# include <linux/if_ether.h>
2019-02-01 02:40:07 +03:00
# include <linux/btf.h>
2016-10-17 15:28:36 +03:00
2017-02-10 02:21:38 +03:00
# include <bpf/bpf.h>
2019-01-28 20:21:16 +03:00
# include <bpf/libbpf.h>
2017-02-10 02:21:38 +03:00
2017-03-31 03:24:04 +03:00
# ifdef HAVE_GENHDR
# include "autoconf.h"
# else
# if defined(__i386) || defined(__x86_64) || defined(__s390x__) || defined(__aarch64__)
# define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 1
# endif
# endif
2018-02-27 00:34:32 +03:00
# include "bpf_rlimit.h"
2018-05-15 00:22:34 +03:00
# include "bpf_rand.h"
2018-08-08 11:01:27 +03:00
# include "bpf_util.h"
2019-04-27 02:39:49 +03:00
# include "test_btf.h"
2016-10-17 15:28:36 +03:00
# include "../../../include/linux/filter.h"
2018-05-04 02:08:13 +03:00
# define MAX_INSNS BPF_MAXINSNS
2019-04-02 07:27:49 +03:00
# define MAX_TEST_INSNS 1000000
2016-10-17 15:28:36 +03:00
# define MAX_FIXUPS 8
2019-04-27 02:39:49 +03:00
# define MAX_NR_MAPS 18
2018-12-20 09:13:03 +03:00
# define MAX_TEST_RUNS 8
2018-01-18 03:52:03 +03:00
# define POINTER_VALUE 0xcafe4all
# define TEST_DATA_LEN 64
2015-10-08 08:23:23 +03:00
2017-03-31 03:24:04 +03:00
# define F_NEEDS_EFFICIENT_UNALIGNED_ACCESS (1 << 0)
2017-05-25 02:05:09 +03:00
# define F_LOAD_WITH_STRICT_ALIGNMENT (1 << 1)
2017-03-31 03:24:04 +03:00
2018-02-15 00:50:36 +03:00
# define UNPRIV_SYSCTL "kernel / unprivileged_bpf_disabled"
static bool unpriv_disabled = false ;
2019-01-28 20:21:16 +03:00
static int skips ;
2018-02-15 00:50:36 +03:00
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
struct bpf_test {
const char * descr ;
struct bpf_insn insns [ MAX_INSNS ] ;
2019-04-02 07:27:49 +03:00
struct bpf_insn * fill_insns ;
2018-10-09 04:04:53 +03:00
int fixup_map_hash_8b [ MAX_FIXUPS ] ;
int fixup_map_hash_48b [ MAX_FIXUPS ] ;
int fixup_map_hash_16b [ MAX_FIXUPS ] ;
int fixup_map_array_48b [ MAX_FIXUPS ] ;
2018-10-09 04:04:54 +03:00
int fixup_map_sockmap [ MAX_FIXUPS ] ;
int fixup_map_sockhash [ MAX_FIXUPS ] ;
int fixup_map_xskmap [ MAX_FIXUPS ] ;
int fixup_map_stacktrace [ MAX_FIXUPS ] ;
2018-06-03 00:06:31 +03:00
int fixup_prog1 [ MAX_FIXUPS ] ;
int fixup_prog2 [ MAX_FIXUPS ] ;
2017-03-22 20:00:35 +03:00
int fixup_map_in_map [ MAX_FIXUPS ] ;
2018-08-03 00:27:28 +03:00
int fixup_cgroup_storage [ MAX_FIXUPS ] ;
2018-09-28 17:45:53 +03:00
int fixup_percpu_cgroup_storage [ MAX_FIXUPS ] ;
2019-02-01 02:40:07 +03:00
int fixup_map_spin_lock [ MAX_FIXUPS ] ;
bpf, selftest: test {rd, wr}only flags and direct value access
Extend test_verifier with various test cases around the two kernel
extensions, that is, {rd,wr}only map support as well as direct map
value access. All passing, one skipped due to xskmap not present
on test machine:
# ./test_verifier
[...]
#948/p XDP pkt read, pkt_meta' <= pkt_data, bad access 1 OK
#949/p XDP pkt read, pkt_meta' <= pkt_data, bad access 2 OK
#950/p XDP pkt read, pkt_data <= pkt_meta', good access OK
#951/p XDP pkt read, pkt_data <= pkt_meta', bad access 1 OK
#952/p XDP pkt read, pkt_data <= pkt_meta', bad access 2 OK
Summary: 1410 PASSED, 1 SKIPPED, 0 FAILED
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-10 00:20:16 +03:00
int fixup_map_array_ro [ MAX_FIXUPS ] ;
int fixup_map_array_wo [ MAX_FIXUPS ] ;
int fixup_map_array_small [ MAX_FIXUPS ] ;
2019-04-27 02:39:49 +03:00
int fixup_sk_storage_map [ MAX_FIXUPS ] ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
const char * errstr ;
2015-10-08 08:23:23 +03:00
const char * errstr_unpriv ;
2018-12-13 22:42:32 +03:00
uint32_t retval , retval_unpriv , insn_processed ;
2019-04-02 07:27:49 +03:00
int prog_len ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
enum {
2015-10-08 08:23:23 +03:00
UNDEF ,
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
ACCEPT ,
REJECT
2015-10-08 08:23:23 +03:00
} result , result_unpriv ;
2015-06-04 20:11:54 +03:00
enum bpf_prog_type prog_type ;
2017-03-31 03:24:04 +03:00
uint8_t flags ;
2018-05-04 02:08:13 +03:00
__u8 data [ TEST_DATA_LEN ] ;
void ( * fill_helper ) ( struct bpf_test * self ) ;
2018-12-20 09:13:03 +03:00
uint8_t runs ;
struct {
uint32_t retval , retval_unpriv ;
union {
__u8 data [ TEST_DATA_LEN ] ;
__u64 data64 [ TEST_DATA_LEN / 8 ] ;
} ;
} retvals [ MAX_TEST_RUNS ] ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
} ;
2016-09-28 17:54:32 +03:00
/* Note we want this to be 64 bit aligned so that the end of our array is
* actually the end of the structure .
*/
# define MAX_ENTRIES 11
2016-10-17 15:28:36 +03:00
2016-09-28 17:54:32 +03:00
struct test_val {
2016-10-17 15:28:36 +03:00
unsigned int index ;
2016-09-28 17:54:32 +03:00
int foo [ MAX_ENTRIES ] ;
} ;
2018-04-24 16:08:19 +03:00
struct other_val {
long long foo ;
long long bar ;
} ;
2018-05-04 02:08:13 +03:00
static void bpf_fill_ld_abs_vlan_push_pop ( struct bpf_test * self )
{
2019-04-02 07:27:49 +03:00
/* test: {skb->data[0], vlan_push} x 51 + {skb->data[0], vlan_pop} x 51 */
2018-05-04 02:08:13 +03:00
# define PUSH_CNT 51
2019-04-02 07:27:49 +03:00
/* jump range is limited to 16 bit. PUSH_CNT of ld_abs needs room */
unsigned int len = ( 1 < < 15 ) - PUSH_CNT * 2 * 5 * 6 ;
struct bpf_insn * insn = self - > fill_insns ;
2018-05-04 02:08:13 +03:00
int i = 0 , j , k = 0 ;
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_6 , BPF_REG_1 ) ;
loop :
for ( j = 0 ; j < PUSH_CNT ; j + + ) {
insn [ i + + ] = BPF_LD_ABS ( BPF_B , 0 ) ;
insn [ i ] = BPF_JMP_IMM ( BPF_JNE , BPF_REG_0 , 0x34 , len - i - 2 ) ;
i + + ;
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_1 , BPF_REG_6 ) ;
insn [ i + + ] = BPF_MOV64_IMM ( BPF_REG_2 , 1 ) ;
insn [ i + + ] = BPF_MOV64_IMM ( BPF_REG_3 , 2 ) ;
insn [ i + + ] = BPF_RAW_INSN ( BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
BPF_FUNC_skb_vlan_push ) ,
insn [ i ] = BPF_JMP_IMM ( BPF_JNE , BPF_REG_0 , 0 , len - i - 2 ) ;
i + + ;
}
for ( j = 0 ; j < PUSH_CNT ; j + + ) {
insn [ i + + ] = BPF_LD_ABS ( BPF_B , 0 ) ;
insn [ i ] = BPF_JMP_IMM ( BPF_JNE , BPF_REG_0 , 0x34 , len - i - 2 ) ;
i + + ;
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_1 , BPF_REG_6 ) ;
insn [ i + + ] = BPF_RAW_INSN ( BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
BPF_FUNC_skb_vlan_pop ) ,
insn [ i ] = BPF_JMP_IMM ( BPF_JNE , BPF_REG_0 , 0 , len - i - 2 ) ;
i + + ;
}
if ( + + k < 5 )
goto loop ;
for ( ; i < len - 1 ; i + + )
insn [ i ] = BPF_ALU32_IMM ( BPF_MOV , BPF_REG_0 , 0xbef ) ;
insn [ len - 1 ] = BPF_EXIT_INSN ( ) ;
2019-04-02 07:27:49 +03:00
self - > prog_len = len ;
2018-05-04 02:08:13 +03:00
}
static void bpf_fill_jump_around_ld_abs ( struct bpf_test * self )
{
2019-04-02 07:27:49 +03:00
struct bpf_insn * insn = self - > fill_insns ;
/* jump range is limited to 16 bit. every ld_abs is replaced by 6 insns */
unsigned int len = ( 1 < < 15 ) / 6 ;
2018-05-04 02:08:13 +03:00
int i = 0 ;
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_6 , BPF_REG_1 ) ;
insn [ i + + ] = BPF_LD_ABS ( BPF_B , 0 ) ;
insn [ i ] = BPF_JMP_IMM ( BPF_JEQ , BPF_REG_0 , 10 , len - i - 2 ) ;
i + + ;
while ( i < len - 1 )
insn [ i + + ] = BPF_LD_ABS ( BPF_B , 1 ) ;
insn [ i ] = BPF_EXIT_INSN ( ) ;
2019-04-02 07:27:49 +03:00
self - > prog_len = i + 1 ;
2018-05-04 02:08:13 +03:00
}
2018-05-15 00:22:34 +03:00
static void bpf_fill_rand_ld_dw ( struct bpf_test * self )
{
2019-04-02 07:27:49 +03:00
struct bpf_insn * insn = self - > fill_insns ;
2018-05-15 00:22:34 +03:00
uint64_t res = 0 ;
int i = 0 ;
insn [ i + + ] = BPF_MOV32_IMM ( BPF_REG_0 , 0 ) ;
while ( i < self - > retval ) {
uint64_t val = bpf_semi_rand_get ( ) ;
struct bpf_insn tmp [ 2 ] = { BPF_LD_IMM64 ( BPF_REG_1 , val ) } ;
res ^ = val ;
insn [ i + + ] = tmp [ 0 ] ;
insn [ i + + ] = tmp [ 1 ] ;
insn [ i + + ] = BPF_ALU64_REG ( BPF_XOR , BPF_REG_0 , BPF_REG_1 ) ;
}
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_1 , BPF_REG_0 ) ;
insn [ i + + ] = BPF_ALU64_IMM ( BPF_RSH , BPF_REG_1 , 32 ) ;
insn [ i + + ] = BPF_ALU64_REG ( BPF_XOR , BPF_REG_0 , BPF_REG_1 ) ;
insn [ i ] = BPF_EXIT_INSN ( ) ;
2019-04-02 07:27:49 +03:00
self - > prog_len = i + 1 ;
2018-05-15 00:22:34 +03:00
res ^ = ( res > > 32 ) ;
self - > retval = ( uint32_t ) res ;
}
2019-04-13 00:41:32 +03:00
/* test the sequence of 1k jumps */
static void bpf_fill_scale1 ( struct bpf_test * self )
{
struct bpf_insn * insn = self - > fill_insns ;
int i = 0 , k = 0 ;
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_6 , BPF_REG_1 ) ;
/* test to check that the sequence of 1024 jumps is acceptable */
while ( k + + < 1024 ) {
insn [ i + + ] = BPF_RAW_INSN ( BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
BPF_FUNC_get_prandom_u32 ) ;
insn [ i + + ] = BPF_JMP_IMM ( BPF_JGT , BPF_REG_0 , bpf_semi_rand_get ( ) , 2 ) ;
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_1 , BPF_REG_10 ) ;
insn [ i + + ] = BPF_STX_MEM ( BPF_DW , BPF_REG_1 , BPF_REG_6 ,
- 8 * ( k % 64 + 1 ) ) ;
}
/* every jump adds 1024 steps to insn_processed, so to stay exactly
* within 1 m limit add MAX_TEST_INSNS - 1025 MOVs and 1 EXIT
*/
while ( i < MAX_TEST_INSNS - 1025 )
insn [ i + + ] = BPF_ALU32_IMM ( BPF_MOV , BPF_REG_0 , 42 ) ;
insn [ i ] = BPF_EXIT_INSN ( ) ;
self - > prog_len = i + 1 ;
self - > retval = 42 ;
}
/* test the sequence of 1k jumps in inner most function (function depth 8)*/
static void bpf_fill_scale2 ( struct bpf_test * self )
{
struct bpf_insn * insn = self - > fill_insns ;
int i = 0 , k = 0 ;
# define FUNC_NEST 7
for ( k = 0 ; k < FUNC_NEST ; k + + ) {
insn [ i + + ] = BPF_CALL_REL ( 1 ) ;
insn [ i + + ] = BPF_EXIT_INSN ( ) ;
}
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_6 , BPF_REG_1 ) ;
/* test to check that the sequence of 1024 jumps is acceptable */
while ( k + + < 1024 ) {
insn [ i + + ] = BPF_RAW_INSN ( BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
BPF_FUNC_get_prandom_u32 ) ;
insn [ i + + ] = BPF_JMP_IMM ( BPF_JGT , BPF_REG_0 , bpf_semi_rand_get ( ) , 2 ) ;
insn [ i + + ] = BPF_MOV64_REG ( BPF_REG_1 , BPF_REG_10 ) ;
insn [ i + + ] = BPF_STX_MEM ( BPF_DW , BPF_REG_1 , BPF_REG_6 ,
- 8 * ( k % ( 64 - 4 * FUNC_NEST ) + 1 ) ) ;
}
/* every jump adds 1024 steps to insn_processed, so to stay exactly
* within 1 m limit add MAX_TEST_INSNS - 1025 MOVs and 1 EXIT
*/
while ( i < MAX_TEST_INSNS - 1025 )
insn [ i + + ] = BPF_ALU32_IMM ( BPF_MOV , BPF_REG_0 , 42 ) ;
insn [ i ] = BPF_EXIT_INSN ( ) ;
self - > prog_len = i + 1 ;
self - > retval = 42 ;
}
static void bpf_fill_scale ( struct bpf_test * self )
{
switch ( self - > retval ) {
case 1 :
return bpf_fill_scale1 ( self ) ;
case 2 :
return bpf_fill_scale2 ( self ) ;
default :
self - > prog_len = 0 ;
break ;
}
}
2018-10-02 23:35:38 +03:00
/* BPF_SK_LOOKUP contains 13 instructions, if you need to fix up maps */
2019-03-22 04:54:04 +03:00
# define BPF_SK_LOOKUP(func) \
2018-10-02 23:35:38 +03:00
/* struct bpf_sock_tuple tuple = {} */ \
BPF_MOV64_IMM ( BPF_REG_2 , 0 ) , \
BPF_STX_MEM ( BPF_W , BPF_REG_10 , BPF_REG_2 , - 8 ) , \
BPF_STX_MEM ( BPF_DW , BPF_REG_10 , BPF_REG_2 , - 16 ) , \
BPF_STX_MEM ( BPF_DW , BPF_REG_10 , BPF_REG_2 , - 24 ) , \
BPF_STX_MEM ( BPF_DW , BPF_REG_10 , BPF_REG_2 , - 32 ) , \
BPF_STX_MEM ( BPF_DW , BPF_REG_10 , BPF_REG_2 , - 40 ) , \
BPF_STX_MEM ( BPF_DW , BPF_REG_10 , BPF_REG_2 , - 48 ) , \
2019-03-22 04:54:04 +03:00
/* sk = func(ctx, &tuple, sizeof tuple, 0, 0) */ \
2018-10-02 23:35:38 +03:00
BPF_MOV64_REG ( BPF_REG_2 , BPF_REG_10 ) , \
BPF_ALU64_IMM ( BPF_ADD , BPF_REG_2 , - 48 ) , \
BPF_MOV64_IMM ( BPF_REG_3 , sizeof ( struct bpf_sock_tuple ) ) , \
BPF_MOV64_IMM ( BPF_REG_4 , 0 ) , \
BPF_MOV64_IMM ( BPF_REG_5 , 0 ) , \
2019-03-22 04:54:04 +03:00
BPF_EMIT_CALL ( BPF_FUNC_ # # func )
2018-10-02 23:35:38 +03:00
2019-01-26 20:26:13 +03:00
/* BPF_DIRECT_PKT_R2 contains 7 instructions, it initializes default return
* value into 0 and does necessary preparation for direct packet access
* through r2 . The allowed access range is 8 bytes .
*/
# define BPF_DIRECT_PKT_R2 \
BPF_MOV64_IMM ( BPF_REG_0 , 0 ) , \
BPF_LDX_MEM ( BPF_W , BPF_REG_2 , BPF_REG_1 , \
offsetof ( struct __sk_buff , data ) ) , \
BPF_LDX_MEM ( BPF_W , BPF_REG_3 , BPF_REG_1 , \
offsetof ( struct __sk_buff , data_end ) ) , \
BPF_MOV64_REG ( BPF_REG_4 , BPF_REG_2 ) , \
BPF_ALU64_IMM ( BPF_ADD , BPF_REG_4 , 8 ) , \
BPF_JMP_REG ( BPF_JLE , BPF_REG_4 , BPF_REG_3 , 1 ) , \
BPF_EXIT_INSN ( )
/* BPF_RAND_UEXT_R7 contains 4 instructions, it initializes R7 into a random
* positive u32 , and zero - extend it into 64 - bit .
*/
# define BPF_RAND_UEXT_R7 \
BPF_RAW_INSN ( BPF_JMP | BPF_CALL , 0 , 0 , 0 , \
BPF_FUNC_get_prandom_u32 ) , \
BPF_MOV64_REG ( BPF_REG_7 , BPF_REG_0 ) , \
BPF_ALU64_IMM ( BPF_LSH , BPF_REG_7 , 33 ) , \
BPF_ALU64_IMM ( BPF_RSH , BPF_REG_7 , 33 )
/* BPF_RAND_SEXT_R7 contains 5 instructions, it initializes R7 into a random
* negative u32 , and sign - extend it into 64 - bit .
*/
# define BPF_RAND_SEXT_R7 \
BPF_RAW_INSN ( BPF_JMP | BPF_CALL , 0 , 0 , 0 , \
BPF_FUNC_get_prandom_u32 ) , \
BPF_MOV64_REG ( BPF_REG_7 , BPF_REG_0 ) , \
BPF_ALU64_IMM ( BPF_OR , BPF_REG_7 , 0x80000000 ) , \
BPF_ALU64_IMM ( BPF_LSH , BPF_REG_7 , 32 ) , \
BPF_ALU64_IMM ( BPF_ARSH , BPF_REG_7 , 32 )
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
static struct bpf_test tests [ ] = {
2019-01-26 02:24:42 +03:00
# define FILL_ARRAY
# include <verifier/tests.h>
# undef FILL_ARRAY
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
} ;
2016-10-17 15:28:36 +03:00
static int probe_filter_length ( const struct bpf_insn * fp )
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
{
2016-10-17 15:28:36 +03:00
int len ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
for ( len = MAX_INSNS - 1 ; len > 0 ; - - len )
if ( fp [ len ] . code ! = 0 | | fp [ len ] . imm ! = 0 )
break ;
return len + 1 ;
}
2019-01-28 20:21:17 +03:00
static bool skip_unsupported_map ( enum bpf_map_type map_type )
{
if ( ! bpf_probe_map_type ( map_type , 0 ) ) {
printf ( " SKIP (unsupported map type %d) \n " , map_type ) ;
skips + + ;
return true ;
}
return false ;
}
bpf, selftest: test {rd, wr}only flags and direct value access
Extend test_verifier with various test cases around the two kernel
extensions, that is, {rd,wr}only map support as well as direct map
value access. All passing, one skipped due to xskmap not present
on test machine:
# ./test_verifier
[...]
#948/p XDP pkt read, pkt_meta' <= pkt_data, bad access 1 OK
#949/p XDP pkt read, pkt_meta' <= pkt_data, bad access 2 OK
#950/p XDP pkt read, pkt_data <= pkt_meta', good access OK
#951/p XDP pkt read, pkt_data <= pkt_meta', bad access 1 OK
#952/p XDP pkt read, pkt_data <= pkt_meta', bad access 2 OK
Summary: 1410 PASSED, 1 SKIPPED, 0 FAILED
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-10 00:20:16 +03:00
static int __create_map ( uint32_t type , uint32_t size_key ,
uint32_t size_value , uint32_t max_elem ,
uint32_t extra_flags )
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
{
2016-10-17 15:28:36 +03:00
int fd ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2018-06-03 00:06:31 +03:00
fd = bpf_create_map ( type , size_key , size_value , max_elem ,
bpf, selftest: test {rd, wr}only flags and direct value access
Extend test_verifier with various test cases around the two kernel
extensions, that is, {rd,wr}only map support as well as direct map
value access. All passing, one skipped due to xskmap not present
on test machine:
# ./test_verifier
[...]
#948/p XDP pkt read, pkt_meta' <= pkt_data, bad access 1 OK
#949/p XDP pkt read, pkt_meta' <= pkt_data, bad access 2 OK
#950/p XDP pkt read, pkt_data <= pkt_meta', good access OK
#951/p XDP pkt read, pkt_data <= pkt_meta', bad access 1 OK
#952/p XDP pkt read, pkt_data <= pkt_meta', bad access 2 OK
Summary: 1410 PASSED, 1 SKIPPED, 0 FAILED
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-10 00:20:16 +03:00
( type = = BPF_MAP_TYPE_HASH ?
BPF_F_NO_PREALLOC : 0 ) | extra_flags ) ;
2019-01-28 20:21:17 +03:00
if ( fd < 0 ) {
if ( skip_unsupported_map ( type ) )
return - 1 ;
2016-10-17 15:28:36 +03:00
printf ( " Failed to create hash map '%s'! \n " , strerror ( errno ) ) ;
2019-01-28 20:21:17 +03:00
}
2015-10-08 08:23:23 +03:00
2016-10-17 15:28:36 +03:00
return fd ;
2015-10-08 08:23:23 +03:00
}
bpf, selftest: test {rd, wr}only flags and direct value access
Extend test_verifier with various test cases around the two kernel
extensions, that is, {rd,wr}only map support as well as direct map
value access. All passing, one skipped due to xskmap not present
on test machine:
# ./test_verifier
[...]
#948/p XDP pkt read, pkt_meta' <= pkt_data, bad access 1 OK
#949/p XDP pkt read, pkt_meta' <= pkt_data, bad access 2 OK
#950/p XDP pkt read, pkt_data <= pkt_meta', good access OK
#951/p XDP pkt read, pkt_data <= pkt_meta', bad access 1 OK
#952/p XDP pkt read, pkt_data <= pkt_meta', bad access 2 OK
Summary: 1410 PASSED, 1 SKIPPED, 0 FAILED
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-10 00:20:16 +03:00
static int create_map ( uint32_t type , uint32_t size_key ,
uint32_t size_value , uint32_t max_elem )
{
return __create_map ( type , size_key , size_value , max_elem , 0 ) ;
}
2019-01-03 02:58:35 +03:00
static void update_map ( int fd , int index )
{
struct test_val value = {
. index = ( 6 + 1 ) * sizeof ( int ) ,
. foo [ 6 ] = 0xabcdef12 ,
} ;
assert ( ! bpf_map_update_elem ( fd , & index , & value , 0 ) ) ;
}
2018-12-11 02:25:04 +03:00
static int create_prog_dummy1 ( enum bpf_prog_type prog_type )
2018-02-27 00:34:33 +03:00
{
struct bpf_insn prog [ ] = {
BPF_MOV64_IMM ( BPF_REG_0 , 42 ) ,
BPF_EXIT_INSN ( ) ,
} ;
2018-10-02 23:35:37 +03:00
return bpf_load_program ( prog_type , prog ,
2018-02-27 00:34:33 +03:00
ARRAY_SIZE ( prog ) , " GPL " , 0 , NULL , 0 ) ;
}
2018-12-11 02:25:04 +03:00
static int create_prog_dummy2 ( enum bpf_prog_type prog_type , int mfd , int idx )
2018-02-27 00:34:33 +03:00
{
struct bpf_insn prog [ ] = {
BPF_MOV64_IMM ( BPF_REG_3 , idx ) ,
BPF_LD_MAP_FD ( BPF_REG_2 , mfd ) ,
BPF_RAW_INSN ( BPF_JMP | BPF_CALL , 0 , 0 , 0 ,
BPF_FUNC_tail_call ) ,
BPF_MOV64_IMM ( BPF_REG_0 , 41 ) ,
BPF_EXIT_INSN ( ) ,
} ;
2018-10-02 23:35:37 +03:00
return bpf_load_program ( prog_type , prog ,
2018-02-27 00:34:33 +03:00
ARRAY_SIZE ( prog ) , " GPL " , 0 , NULL , 0 ) ;
}
2018-12-11 02:25:04 +03:00
static int create_prog_array ( enum bpf_prog_type prog_type , uint32_t max_elem ,
2018-10-02 23:35:37 +03:00
int p1key )
2015-10-08 08:23:23 +03:00
{
2018-06-03 00:06:31 +03:00
int p2key = 1 ;
2018-02-27 00:34:33 +03:00
int mfd , p1fd , p2fd ;
2015-10-08 08:23:23 +03:00
2018-02-27 00:34:33 +03:00
mfd = bpf_create_map ( BPF_MAP_TYPE_PROG_ARRAY , sizeof ( int ) ,
2018-06-03 00:06:31 +03:00
sizeof ( int ) , max_elem , 0 ) ;
2018-02-27 00:34:33 +03:00
if ( mfd < 0 ) {
2019-01-28 20:21:17 +03:00
if ( skip_unsupported_map ( BPF_MAP_TYPE_PROG_ARRAY ) )
return - 1 ;
2016-10-17 15:28:36 +03:00
printf ( " Failed to create prog array '%s'! \n " , strerror ( errno ) ) ;
2018-02-27 00:34:33 +03:00
return - 1 ;
}
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2018-10-02 23:35:37 +03:00
p1fd = create_prog_dummy1 ( prog_type ) ;
p2fd = create_prog_dummy2 ( prog_type , mfd , p2key ) ;
2018-02-27 00:34:33 +03:00
if ( p1fd < 0 | | p2fd < 0 )
goto out ;
if ( bpf_map_update_elem ( mfd , & p1key , & p1fd , BPF_ANY ) < 0 )
goto out ;
if ( bpf_map_update_elem ( mfd , & p2key , & p2fd , BPF_ANY ) < 0 )
goto out ;
close ( p2fd ) ;
close ( p1fd ) ;
return mfd ;
out :
close ( p2fd ) ;
close ( p1fd ) ;
close ( mfd ) ;
return - 1 ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
}
2017-03-22 20:00:35 +03:00
static int create_map_in_map ( void )
{
int inner_map_fd , outer_map_fd ;
inner_map_fd = bpf_create_map ( BPF_MAP_TYPE_ARRAY , sizeof ( int ) ,
sizeof ( int ) , 1 , 0 ) ;
if ( inner_map_fd < 0 ) {
2019-01-28 20:21:17 +03:00
if ( skip_unsupported_map ( BPF_MAP_TYPE_ARRAY ) )
return - 1 ;
2017-03-22 20:00:35 +03:00
printf ( " Failed to create array '%s'! \n " , strerror ( errno ) ) ;
return inner_map_fd ;
}
2017-09-28 00:37:54 +03:00
outer_map_fd = bpf_create_map_in_map ( BPF_MAP_TYPE_ARRAY_OF_MAPS , NULL ,
2017-03-22 20:00:35 +03:00
sizeof ( int ) , inner_map_fd , 1 , 0 ) ;
2019-01-28 20:21:17 +03:00
if ( outer_map_fd < 0 ) {
if ( skip_unsupported_map ( BPF_MAP_TYPE_ARRAY_OF_MAPS ) )
return - 1 ;
2017-03-22 20:00:35 +03:00
printf ( " Failed to create array of maps '%s'! \n " ,
strerror ( errno ) ) ;
2019-01-28 20:21:17 +03:00
}
2017-03-22 20:00:35 +03:00
close ( inner_map_fd ) ;
return outer_map_fd ;
}
2018-09-28 17:45:53 +03:00
static int create_cgroup_storage ( bool percpu )
2018-08-03 00:27:28 +03:00
{
2018-09-28 17:45:53 +03:00
enum bpf_map_type type = percpu ? BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE :
BPF_MAP_TYPE_CGROUP_STORAGE ;
2018-08-03 00:27:28 +03:00
int fd ;
2018-09-28 17:45:53 +03:00
fd = bpf_create_map ( type , sizeof ( struct bpf_cgroup_storage_key ) ,
2018-08-03 00:27:28 +03:00
TEST_DATA_LEN , 0 , 0 ) ;
2019-01-28 20:21:17 +03:00
if ( fd < 0 ) {
if ( skip_unsupported_map ( type ) )
return - 1 ;
2018-09-28 17:45:53 +03:00
printf ( " Failed to create cgroup storage '%s'! \n " ,
strerror ( errno ) ) ;
2019-01-28 20:21:17 +03:00
}
2018-08-03 00:27:28 +03:00
return fd ;
}
2019-02-01 02:40:07 +03:00
/* struct bpf_spin_lock {
* int val ;
* } ;
* struct val {
* int cnt ;
* struct bpf_spin_lock l ;
* } ;
*/
static const char btf_str_sec [ ] = " \0 bpf_spin_lock \0 val \0 cnt \0 l " ;
static __u32 btf_raw_types [ ] = {
/* 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; */
} ;
static int load_btf ( void )
{
struct btf_header hdr = {
. magic = BTF_MAGIC ,
. version = BTF_VERSION ,
. hdr_len = sizeof ( struct btf_header ) ,
. type_len = sizeof ( btf_raw_types ) ,
. str_off = sizeof ( btf_raw_types ) ,
. str_len = sizeof ( btf_str_sec ) ,
} ;
void * ptr , * raw_btf ;
int btf_fd ;
ptr = raw_btf = malloc ( sizeof ( hdr ) + sizeof ( btf_raw_types ) +
sizeof ( btf_str_sec ) ) ;
memcpy ( ptr , & hdr , sizeof ( hdr ) ) ;
ptr + = sizeof ( hdr ) ;
memcpy ( ptr , btf_raw_types , hdr . type_len ) ;
ptr + = hdr . type_len ;
memcpy ( ptr , btf_str_sec , hdr . str_len ) ;
ptr + = hdr . str_len ;
btf_fd = bpf_load_btf ( raw_btf , ptr - raw_btf , 0 , 0 , 0 ) ;
free ( raw_btf ) ;
if ( btf_fd < 0 )
return - 1 ;
return btf_fd ;
}
static int create_map_spin_lock ( void )
{
struct bpf_create_map_attr attr = {
. name = " test_map " ,
. map_type = BPF_MAP_TYPE_ARRAY ,
. key_size = 4 ,
. value_size = 8 ,
. max_entries = 1 ,
. btf_key_type_id = 1 ,
. btf_value_type_id = 3 ,
} ;
int fd , btf_fd ;
btf_fd = load_btf ( ) ;
if ( btf_fd < 0 )
return - 1 ;
attr . btf_fd = btf_fd ;
fd = bpf_create_map_xattr ( & attr ) ;
if ( fd < 0 )
printf ( " Failed to create map with spin_lock \n " ) ;
return fd ;
}
2019-04-27 02:39:49 +03:00
static int create_sk_storage_map ( void )
{
struct bpf_create_map_attr attr = {
. name = " test_map " ,
. map_type = BPF_MAP_TYPE_SK_STORAGE ,
. key_size = 4 ,
. value_size = 8 ,
. max_entries = 0 ,
. map_flags = BPF_F_NO_PREALLOC ,
. btf_key_type_id = 1 ,
. btf_value_type_id = 3 ,
} ;
int fd , btf_fd ;
btf_fd = load_btf ( ) ;
if ( btf_fd < 0 )
return - 1 ;
attr . btf_fd = btf_fd ;
fd = bpf_create_map_xattr ( & attr ) ;
close ( attr . btf_fd ) ;
if ( fd < 0 )
printf ( " Failed to create sk_storage_map \n " ) ;
return fd ;
}
2018-05-04 02:08:13 +03:00
static char bpf_vlog [ UINT_MAX > > 8 ] ;
2016-10-17 15:28:36 +03:00
2018-12-11 02:25:04 +03:00
static void do_test_fixup ( struct bpf_test * test , enum bpf_prog_type prog_type ,
2018-10-02 23:35:37 +03:00
struct bpf_insn * prog , int * map_fds )
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
{
2018-10-09 04:04:53 +03:00
int * fixup_map_hash_8b = test - > fixup_map_hash_8b ;
int * fixup_map_hash_48b = test - > fixup_map_hash_48b ;
int * fixup_map_hash_16b = test - > fixup_map_hash_16b ;
int * fixup_map_array_48b = test - > fixup_map_array_48b ;
2018-10-09 04:04:54 +03:00
int * fixup_map_sockmap = test - > fixup_map_sockmap ;
int * fixup_map_sockhash = test - > fixup_map_sockhash ;
int * fixup_map_xskmap = test - > fixup_map_xskmap ;
int * fixup_map_stacktrace = test - > fixup_map_stacktrace ;
2018-06-03 00:06:31 +03:00
int * fixup_prog1 = test - > fixup_prog1 ;
int * fixup_prog2 = test - > fixup_prog2 ;
2017-03-22 20:00:35 +03:00
int * fixup_map_in_map = test - > fixup_map_in_map ;
2018-08-03 00:27:28 +03:00
int * fixup_cgroup_storage = test - > fixup_cgroup_storage ;
2018-09-28 17:45:53 +03:00
int * fixup_percpu_cgroup_storage = test - > fixup_percpu_cgroup_storage ;
2019-02-01 02:40:07 +03:00
int * fixup_map_spin_lock = test - > fixup_map_spin_lock ;
bpf, selftest: test {rd, wr}only flags and direct value access
Extend test_verifier with various test cases around the two kernel
extensions, that is, {rd,wr}only map support as well as direct map
value access. All passing, one skipped due to xskmap not present
on test machine:
# ./test_verifier
[...]
#948/p XDP pkt read, pkt_meta' <= pkt_data, bad access 1 OK
#949/p XDP pkt read, pkt_meta' <= pkt_data, bad access 2 OK
#950/p XDP pkt read, pkt_data <= pkt_meta', good access OK
#951/p XDP pkt read, pkt_data <= pkt_meta', bad access 1 OK
#952/p XDP pkt read, pkt_data <= pkt_meta', bad access 2 OK
Summary: 1410 PASSED, 1 SKIPPED, 0 FAILED
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-10 00:20:16 +03:00
int * fixup_map_array_ro = test - > fixup_map_array_ro ;
int * fixup_map_array_wo = test - > fixup_map_array_wo ;
int * fixup_map_array_small = test - > fixup_map_array_small ;
2019-04-27 02:39:49 +03:00
int * fixup_sk_storage_map = test - > fixup_sk_storage_map ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2019-04-02 07:27:49 +03:00
if ( test - > fill_helper ) {
test - > fill_insns = calloc ( MAX_TEST_INSNS , sizeof ( struct bpf_insn ) ) ;
2018-05-04 02:08:13 +03:00
test - > fill_helper ( test ) ;
2019-04-02 07:27:49 +03:00
}
2018-05-04 02:08:13 +03:00
2016-10-17 15:28:36 +03:00
/* Allocating HTs with 1 elem is fine here, since we only test
* for verifier and not do a runtime lookup , so the only thing
* that really matters is value size in this case .
*/
2018-10-09 04:04:53 +03:00
if ( * fixup_map_hash_8b ) {
2018-06-03 00:06:31 +03:00
map_fds [ 0 ] = create_map ( BPF_MAP_TYPE_HASH , sizeof ( long long ) ,
sizeof ( long long ) , 1 ) ;
2016-10-17 15:28:36 +03:00
do {
2018-10-09 04:04:53 +03:00
prog [ * fixup_map_hash_8b ] . imm = map_fds [ 0 ] ;
fixup_map_hash_8b + + ;
} while ( * fixup_map_hash_8b ) ;
2016-10-17 15:28:36 +03:00
}
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2018-10-09 04:04:53 +03:00
if ( * fixup_map_hash_48b ) {
2018-06-03 00:06:31 +03:00
map_fds [ 1 ] = create_map ( BPF_MAP_TYPE_HASH , sizeof ( long long ) ,
sizeof ( struct test_val ) , 1 ) ;
2016-10-17 15:28:36 +03:00
do {
2018-10-09 04:04:53 +03:00
prog [ * fixup_map_hash_48b ] . imm = map_fds [ 1 ] ;
fixup_map_hash_48b + + ;
} while ( * fixup_map_hash_48b ) ;
2016-10-17 15:28:36 +03:00
}
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2018-10-09 04:04:53 +03:00
if ( * fixup_map_hash_16b ) {
2018-06-03 00:06:31 +03:00
map_fds [ 2 ] = create_map ( BPF_MAP_TYPE_HASH , sizeof ( long long ) ,
sizeof ( struct other_val ) , 1 ) ;
2018-04-24 16:08:19 +03:00
do {
2018-10-09 04:04:53 +03:00
prog [ * fixup_map_hash_16b ] . imm = map_fds [ 2 ] ;
fixup_map_hash_16b + + ;
} while ( * fixup_map_hash_16b ) ;
2018-04-24 16:08:19 +03:00
}
2018-10-09 04:04:53 +03:00
if ( * fixup_map_array_48b ) {
2018-06-03 00:06:31 +03:00
map_fds [ 3 ] = create_map ( BPF_MAP_TYPE_ARRAY , sizeof ( int ) ,
sizeof ( struct test_val ) , 1 ) ;
2019-01-03 02:58:35 +03:00
update_map ( map_fds [ 3 ] , 0 ) ;
2018-06-03 00:06:31 +03:00
do {
2018-10-09 04:04:53 +03:00
prog [ * fixup_map_array_48b ] . imm = map_fds [ 3 ] ;
fixup_map_array_48b + + ;
} while ( * fixup_map_array_48b ) ;
2018-06-03 00:06:31 +03:00
}
if ( * fixup_prog1 ) {
2018-10-02 23:35:37 +03:00
map_fds [ 4 ] = create_prog_array ( prog_type , 4 , 0 ) ;
2018-06-03 00:06:31 +03:00
do {
prog [ * fixup_prog1 ] . imm = map_fds [ 4 ] ;
fixup_prog1 + + ;
} while ( * fixup_prog1 ) ;
}
if ( * fixup_prog2 ) {
2018-10-02 23:35:37 +03:00
map_fds [ 5 ] = create_prog_array ( prog_type , 8 , 7 ) ;
2016-10-17 15:28:36 +03:00
do {
2018-06-03 00:06:31 +03:00
prog [ * fixup_prog2 ] . imm = map_fds [ 5 ] ;
fixup_prog2 + + ;
} while ( * fixup_prog2 ) ;
2016-10-17 15:28:36 +03:00
}
2017-03-22 20:00:35 +03:00
if ( * fixup_map_in_map ) {
2018-06-03 00:06:31 +03:00
map_fds [ 6 ] = create_map_in_map ( ) ;
2017-03-22 20:00:35 +03:00
do {
2018-06-03 00:06:31 +03:00
prog [ * fixup_map_in_map ] . imm = map_fds [ 6 ] ;
2017-03-22 20:00:35 +03:00
fixup_map_in_map + + ;
} while ( * fixup_map_in_map ) ;
}
2018-08-03 00:27:28 +03:00
if ( * fixup_cgroup_storage ) {
2018-09-28 17:45:53 +03:00
map_fds [ 7 ] = create_cgroup_storage ( false ) ;
2018-08-03 00:27:28 +03:00
do {
prog [ * fixup_cgroup_storage ] . imm = map_fds [ 7 ] ;
fixup_cgroup_storage + + ;
} while ( * fixup_cgroup_storage ) ;
}
2018-09-28 17:45:53 +03:00
if ( * fixup_percpu_cgroup_storage ) {
map_fds [ 8 ] = create_cgroup_storage ( true ) ;
do {
prog [ * fixup_percpu_cgroup_storage ] . imm = map_fds [ 8 ] ;
fixup_percpu_cgroup_storage + + ;
} while ( * fixup_percpu_cgroup_storage ) ;
}
2018-10-09 04:04:54 +03:00
if ( * fixup_map_sockmap ) {
map_fds [ 9 ] = create_map ( BPF_MAP_TYPE_SOCKMAP , sizeof ( int ) ,
sizeof ( int ) , 1 ) ;
do {
prog [ * fixup_map_sockmap ] . imm = map_fds [ 9 ] ;
fixup_map_sockmap + + ;
} while ( * fixup_map_sockmap ) ;
}
if ( * fixup_map_sockhash ) {
map_fds [ 10 ] = create_map ( BPF_MAP_TYPE_SOCKHASH , sizeof ( int ) ,
sizeof ( int ) , 1 ) ;
do {
prog [ * fixup_map_sockhash ] . imm = map_fds [ 10 ] ;
fixup_map_sockhash + + ;
} while ( * fixup_map_sockhash ) ;
}
if ( * fixup_map_xskmap ) {
map_fds [ 11 ] = create_map ( BPF_MAP_TYPE_XSKMAP , sizeof ( int ) ,
sizeof ( int ) , 1 ) ;
do {
prog [ * fixup_map_xskmap ] . imm = map_fds [ 11 ] ;
fixup_map_xskmap + + ;
} while ( * fixup_map_xskmap ) ;
}
if ( * fixup_map_stacktrace ) {
map_fds [ 12 ] = create_map ( BPF_MAP_TYPE_STACK_TRACE , sizeof ( u32 ) ,
sizeof ( u64 ) , 1 ) ;
do {
prog [ * fixup_map_stacktrace ] . imm = map_fds [ 12 ] ;
fixup_map_stacktrace + + ;
2018-12-07 07:14:11 +03:00
} while ( * fixup_map_stacktrace ) ;
2018-10-09 04:04:54 +03:00
}
2019-02-01 02:40:07 +03:00
if ( * fixup_map_spin_lock ) {
map_fds [ 13 ] = create_map_spin_lock ( ) ;
do {
prog [ * fixup_map_spin_lock ] . imm = map_fds [ 13 ] ;
fixup_map_spin_lock + + ;
} while ( * fixup_map_spin_lock ) ;
}
bpf, selftest: test {rd, wr}only flags and direct value access
Extend test_verifier with various test cases around the two kernel
extensions, that is, {rd,wr}only map support as well as direct map
value access. All passing, one skipped due to xskmap not present
on test machine:
# ./test_verifier
[...]
#948/p XDP pkt read, pkt_meta' <= pkt_data, bad access 1 OK
#949/p XDP pkt read, pkt_meta' <= pkt_data, bad access 2 OK
#950/p XDP pkt read, pkt_data <= pkt_meta', good access OK
#951/p XDP pkt read, pkt_data <= pkt_meta', bad access 1 OK
#952/p XDP pkt read, pkt_data <= pkt_meta', bad access 2 OK
Summary: 1410 PASSED, 1 SKIPPED, 0 FAILED
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2019-04-10 00:20:16 +03:00
if ( * fixup_map_array_ro ) {
map_fds [ 14 ] = __create_map ( BPF_MAP_TYPE_ARRAY , sizeof ( int ) ,
sizeof ( struct test_val ) , 1 ,
BPF_F_RDONLY_PROG ) ;
update_map ( map_fds [ 14 ] , 0 ) ;
do {
prog [ * fixup_map_array_ro ] . imm = map_fds [ 14 ] ;
fixup_map_array_ro + + ;
} while ( * fixup_map_array_ro ) ;
}
if ( * fixup_map_array_wo ) {
map_fds [ 15 ] = __create_map ( BPF_MAP_TYPE_ARRAY , sizeof ( int ) ,
sizeof ( struct test_val ) , 1 ,
BPF_F_WRONLY_PROG ) ;
update_map ( map_fds [ 15 ] , 0 ) ;
do {
prog [ * fixup_map_array_wo ] . imm = map_fds [ 15 ] ;
fixup_map_array_wo + + ;
} while ( * fixup_map_array_wo ) ;
}
if ( * fixup_map_array_small ) {
map_fds [ 16 ] = __create_map ( BPF_MAP_TYPE_ARRAY , sizeof ( int ) ,
1 , 1 , 0 ) ;
update_map ( map_fds [ 16 ] , 0 ) ;
do {
prog [ * fixup_map_array_small ] . imm = map_fds [ 16 ] ;
fixup_map_array_small + + ;
} while ( * fixup_map_array_small ) ;
}
2019-04-27 02:39:49 +03:00
if ( * fixup_sk_storage_map ) {
map_fds [ 17 ] = create_sk_storage_map ( ) ;
do {
prog [ * fixup_sk_storage_map ] . imm = map_fds [ 17 ] ;
fixup_sk_storage_map + + ;
} while ( * fixup_sk_storage_map ) ;
}
2016-10-17 15:28:36 +03:00
}
2015-10-08 08:23:23 +03:00
2018-11-01 02:05:55 +03:00
static int set_admin ( bool admin )
{
cap_t caps ;
const cap_value_t cap_val = CAP_SYS_ADMIN ;
int ret = - 1 ;
caps = cap_get_proc ( ) ;
if ( ! caps ) {
perror ( " cap_get_proc " ) ;
return - 1 ;
}
if ( cap_set_flag ( caps , CAP_EFFECTIVE , 1 , & cap_val ,
admin ? CAP_SET : CAP_CLEAR ) ) {
perror ( " cap_set_flag " ) ;
goto out ;
}
if ( cap_set_proc ( caps ) ) {
perror ( " cap_set_proc " ) ;
goto out ;
}
ret = 0 ;
out :
if ( cap_free ( caps ) )
perror ( " cap_free " ) ;
return ret ;
}
2018-12-20 09:13:03 +03:00
static int do_prog_test_run ( int fd_prog , bool unpriv , uint32_t expected_val ,
void * data , size_t size_data )
{
__u8 tmp [ TEST_DATA_LEN < < 2 ] ;
__u32 size_tmp = sizeof ( tmp ) ;
uint32_t retval ;
int err ;
if ( unpriv )
set_admin ( true ) ;
err = bpf_prog_test_run ( fd_prog , 1 , data , size_data ,
tmp , & size_tmp , & retval , NULL ) ;
if ( unpriv )
set_admin ( false ) ;
if ( err & & errno ! = 524 /*ENOTSUPP*/ & & errno ! = EPERM ) {
printf ( " Unexpected bpf_prog_test_run error " ) ;
return err ;
}
if ( ! err & & retval ! = expected_val & &
expected_val ! = POINTER_VALUE ) {
printf ( " FAIL retval %d != %d " , retval , expected_val ) ;
return 1 ;
}
return 0 ;
}
2016-10-17 15:28:36 +03:00
static void do_test_single ( struct bpf_test * test , bool unpriv ,
int * passes , int * errors )
{
2018-12-01 08:08:26 +03:00
int fd_prog , expected_ret , alignment_prevented_execution ;
2018-05-04 02:08:13 +03:00
int prog_len , prog_type = test - > prog_type ;
2016-10-17 15:28:36 +03:00
struct bpf_insn * prog = test - > insns ;
2018-12-20 09:13:03 +03:00
int run_errs , run_successes ;
2017-03-22 20:00:35 +03:00
int map_fds [ MAX_NR_MAPS ] ;
2016-10-17 15:28:36 +03:00
const char * expected_err ;
2019-01-28 20:21:17 +03:00
int fixup_skips ;
2018-12-01 08:08:26 +03:00
__u32 pflags ;
2018-01-18 03:52:03 +03:00
int i , err ;
2017-03-22 20:00:35 +03:00
for ( i = 0 ; i < MAX_NR_MAPS ; i + + )
map_fds [ i ] = - 1 ;
2016-09-28 17:54:32 +03:00
2018-10-02 23:35:37 +03:00
if ( ! prog_type )
prog_type = BPF_PROG_TYPE_SOCKET_FILTER ;
2019-01-28 20:21:17 +03:00
fixup_skips = skips ;
2018-10-02 23:35:37 +03:00
do_test_fixup ( test , prog_type , prog , map_fds ) ;
2019-04-02 07:27:49 +03:00
if ( test - > fill_insns ) {
prog = test - > fill_insns ;
prog_len = test - > prog_len ;
} else {
prog_len = probe_filter_length ( prog ) ;
}
2019-01-28 20:21:17 +03:00
/* If there were some map skips during fixup due to missing bpf
* features , skip this test .
*/
if ( fixup_skips ! = skips )
return ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2018-12-01 08:08:26 +03:00
pflags = 0 ;
if ( test - > flags & F_LOAD_WITH_STRICT_ALIGNMENT )
pflags | = BPF_F_STRICT_ALIGNMENT ;
if ( test - > flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS )
pflags | = BPF_F_ANY_ALIGNMENT ;
fd_prog = bpf_verify_program ( prog_type , prog , prog_len , pflags ,
2019-04-02 07:27:49 +03:00
" GPL " , 0 , bpf_vlog , sizeof ( bpf_vlog ) , 4 ) ;
2019-01-28 20:21:16 +03:00
if ( fd_prog < 0 & & ! bpf_probe_prog_type ( prog_type , 0 ) ) {
printf ( " SKIP (unsupported program type %d) \n " , prog_type ) ;
skips + + ;
goto close_fds ;
}
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2016-10-17 15:28:36 +03:00
expected_ret = unpriv & & test - > result_unpriv ! = UNDEF ?
test - > result_unpriv : test - > result ;
expected_err = unpriv & & test - > errstr_unpriv ?
test - > errstr_unpriv : test - > errstr ;
2017-03-31 03:24:04 +03:00
2018-12-01 08:08:26 +03:00
alignment_prevented_execution = 0 ;
2016-10-17 15:28:36 +03:00
if ( expected_ret = = ACCEPT ) {
2018-12-01 08:08:26 +03:00
if ( fd_prog < 0 ) {
2016-10-17 15:28:36 +03:00
printf ( " FAIL \n Failed to load prog '%s'! \n " ,
strerror ( errno ) ) ;
goto fail_log ;
}
2018-12-01 08:08:26 +03:00
# ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
if ( fd_prog > = 0 & &
2018-12-20 09:13:03 +03:00
( test - > flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS ) )
2018-12-01 08:08:26 +03:00
alignment_prevented_execution = 1 ;
# endif
2016-10-17 15:28:36 +03:00
} else {
if ( fd_prog > = 0 ) {
printf ( " FAIL \n Unexpected success to load! \n " ) ;
goto fail_log ;
}
2018-12-01 08:08:26 +03:00
if ( ! strstr ( bpf_vlog , expected_err ) ) {
2018-02-15 00:50:34 +03:00
printf ( " FAIL \n Unexpected error message! \n \t EXP: %s \n \t RES: %s \n " ,
expected_err , bpf_vlog ) ;
2016-10-17 15:28:36 +03:00
goto fail_log ;
}
}
2015-10-08 08:23:23 +03:00
2018-12-13 22:42:32 +03:00
if ( test - > insn_processed ) {
uint32_t insn_processed ;
char * proc ;
proc = strstr ( bpf_vlog , " processed " ) ;
insn_processed = atoi ( proc + 10 ) ;
if ( test - > insn_processed ! = insn_processed ) {
printf ( " FAIL \n Unexpected insn_processed %u vs %u \n " ,
insn_processed , test - > insn_processed ) ;
2018-01-18 03:52:03 +03:00
goto fail_log ;
}
2018-12-13 22:42:32 +03:00
}
2018-12-20 09:13:03 +03:00
run_errs = 0 ;
run_successes = 0 ;
if ( ! alignment_prevented_execution & & fd_prog > = 0 ) {
uint32_t expected_val ;
int i ;
if ( ! test - > runs ) {
expected_val = unpriv & & test - > retval_unpriv ?
test - > retval_unpriv : test - > retval ;
err = do_prog_test_run ( fd_prog , unpriv , expected_val ,
test - > data , sizeof ( test - > data ) ) ;
if ( err )
run_errs + + ;
else
run_successes + + ;
2018-01-18 03:52:03 +03:00
}
2018-12-20 09:13:03 +03:00
for ( i = 0 ; i < test - > runs ; i + + ) {
if ( unpriv & & test - > retvals [ i ] . retval_unpriv )
expected_val = test - > retvals [ i ] . retval_unpriv ;
else
expected_val = test - > retvals [ i ] . retval ;
err = do_prog_test_run ( fd_prog , unpriv , expected_val ,
test - > retvals [ i ] . data ,
sizeof ( test - > retvals [ i ] . data ) ) ;
if ( err ) {
printf ( " (run %d/%d) " , i + 1 , test - > runs ) ;
run_errs + + ;
} else {
run_successes + + ;
}
2018-01-18 03:52:03 +03:00
}
}
2018-12-20 09:13:03 +03:00
if ( ! run_errs ) {
( * passes ) + + ;
if ( run_successes > 1 )
printf ( " %d cases " , run_successes ) ;
printf ( " OK " ) ;
if ( alignment_prevented_execution )
printf ( " (NOTE: not executed due to unknown alignment) " ) ;
printf ( " \n " ) ;
} else {
printf ( " \n " ) ;
goto fail_log ;
}
2016-10-17 15:28:36 +03:00
close_fds :
2019-04-02 07:27:49 +03:00
if ( test - > fill_insns )
free ( test - > fill_insns ) ;
2016-10-17 15:28:36 +03:00
close ( fd_prog ) ;
2017-03-22 20:00:35 +03:00
for ( i = 0 ; i < MAX_NR_MAPS ; i + + )
close ( map_fds [ i ] ) ;
2016-10-17 15:28:36 +03:00
sched_yield ( ) ;
return ;
fail_log :
( * errors ) + + ;
printf ( " %s " , bpf_vlog ) ;
goto close_fds ;
}
2015-10-08 08:23:23 +03:00
2017-02-10 02:21:37 +03:00
static bool is_admin ( void )
{
cap_t caps ;
cap_flag_value_t sysadmin = CAP_CLEAR ;
const cap_value_t cap_val = CAP_SYS_ADMIN ;
2017-03-11 09:05:55 +03:00
# ifdef CAP_IS_SUPPORTED
2017-02-10 02:21:37 +03:00
if ( ! CAP_IS_SUPPORTED ( CAP_SETFCAP ) ) {
perror ( " cap_get_flag " ) ;
return false ;
}
2017-03-11 09:05:55 +03:00
# endif
2017-02-10 02:21:37 +03:00
caps = cap_get_proc ( ) ;
if ( ! caps ) {
perror ( " cap_get_proc " ) ;
return false ;
}
if ( cap_get_flag ( caps , cap_val , CAP_EFFECTIVE , & sysadmin ) )
perror ( " cap_get_flag " ) ;
if ( cap_free ( caps ) )
perror ( " cap_free " ) ;
return ( sysadmin = = CAP_SET ) ;
}
2018-02-15 00:50:36 +03:00
static void get_unpriv_disabled ( )
{
char buf [ 2 ] ;
FILE * fd ;
fd = fopen ( " /proc/sys/ " UNPRIV_SYSCTL , " r " ) ;
2018-05-17 20:39:31 +03:00
if ( ! fd ) {
perror ( " fopen /proc/sys/ " UNPRIV_SYSCTL ) ;
unpriv_disabled = true ;
return ;
}
2018-02-15 00:50:36 +03:00
if ( fgets ( buf , 2 , fd ) = = buf & & atoi ( buf ) )
unpriv_disabled = true ;
fclose ( fd ) ;
}
2018-10-24 23:05:43 +03:00
static bool test_as_unpriv ( struct bpf_test * test )
{
return ! test - > prog_type | |
test - > prog_type = = BPF_PROG_TYPE_SOCKET_FILTER | |
test - > prog_type = = BPF_PROG_TYPE_CGROUP_SKB ;
}
2016-10-17 15:28:36 +03:00
static int do_test ( bool unpriv , unsigned int from , unsigned int to )
{
2019-01-28 20:21:16 +03:00
int i , passes = 0 , errors = 0 ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2016-10-17 15:28:36 +03:00
for ( i = from ; i < to ; i + + ) {
struct bpf_test * test = & tests [ i ] ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
2016-10-17 15:28:36 +03:00
/* Program types that are not supported by non-root we
* skip right away .
*/
2018-10-24 23:05:43 +03:00
if ( test_as_unpriv ( test ) & & unpriv_disabled ) {
2018-02-15 00:50:36 +03:00
printf ( " #%d/u %s SKIP \n " , i , test - > descr ) ;
skips + + ;
2018-10-24 23:05:43 +03:00
} else if ( test_as_unpriv ( test ) ) {
2017-02-10 02:21:37 +03:00
if ( ! unpriv )
set_admin ( false ) ;
printf ( " #%d/u %s " , i , test - > descr ) ;
do_test_single ( test , true , & passes , & errors ) ;
if ( ! unpriv )
set_admin ( true ) ;
}
2016-10-17 15:28:36 +03:00
2018-02-15 00:50:35 +03:00
if ( unpriv ) {
printf ( " #%d/p %s SKIP \n " , i , test - > descr ) ;
skips + + ;
} else {
2017-02-10 02:21:37 +03:00
printf ( " #%d/p %s " , i , test - > descr ) ;
do_test_single ( test , false , & passes , & errors ) ;
}
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
}
2018-02-15 00:50:35 +03:00
printf ( " Summary: %d PASSED, %d SKIPPED, %d FAILED \n " , passes ,
skips , errors ) ;
2017-06-13 16:17:19 +03:00
return errors ? EXIT_FAILURE : EXIT_SUCCESS ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
}
2016-10-17 15:28:36 +03:00
int main ( int argc , char * * argv )
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
{
2016-10-17 15:28:36 +03:00
unsigned int from = 0 , to = ARRAY_SIZE ( tests ) ;
2017-02-10 02:21:37 +03:00
bool unpriv = ! is_admin ( ) ;
2016-10-17 15:28:36 +03:00
if ( argc = = 3 ) {
unsigned int l = atoi ( argv [ argc - 2 ] ) ;
unsigned int u = atoi ( argv [ argc - 1 ] ) ;
if ( l < to & & u < to ) {
from = l ;
to = u + 1 ;
}
} else if ( argc = = 2 ) {
unsigned int t = atoi ( argv [ argc - 1 ] ) ;
if ( t < to ) {
from = t ;
to = t + 1 ;
}
}
2015-10-08 08:23:23 +03:00
2018-02-15 00:50:36 +03:00
get_unpriv_disabled ( ) ;
if ( unpriv & & unpriv_disabled ) {
printf ( " Cannot run as unprivileged user with sysctl %s. \n " ,
UNPRIV_SYSCTL ) ;
return EXIT_FAILURE ;
}
2018-05-15 00:22:34 +03:00
bpf_semi_rand_init ( ) ;
2016-10-17 15:28:36 +03:00
return do_test ( unpriv , from , to ) ;
bpf: mini eBPF library, test stubs and verifier testsuite
1.
the library includes a trivial set of BPF syscall wrappers:
int bpf_create_map(int key_size, int value_size, int max_entries);
int bpf_update_elem(int fd, void *key, void *value);
int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type,
const struct sock_filter_int *insns, int insn_len,
const char *license);
bpf_prog_load() stores verifier log into global bpf_log_buf[] array
and BPF_*() macros to build instructions
2.
test stubs configure eBPF infra with 'unspec' map and program types.
These are fake types used by user space testsuite only.
3.
verifier tests valid and invalid programs and expects predefined
error log messages from kernel.
40 tests so far.
$ sudo ./test_verifier
#0 add+sub+mul OK
#1 unreachable OK
#2 unreachable2 OK
#3 out of range jump OK
#4 out of range jump2 OK
#5 test1 ld_imm64 OK
...
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-09-26 11:17:07 +04:00
}