Skip to content

Commit 3c731eb

Browse files
Alexei Starovoitovdavem330
authored andcommitted
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 <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 17a5267 commit 3c731eb

File tree

7 files changed

+948
-1
lines changed

7 files changed

+948
-1
lines changed

kernel/bpf/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
obj-y := core.o syscall.o verifier.o
2+
3+
ifdef CONFIG_TEST_BPF
4+
obj-y += test_stub.o
5+
endif

kernel/bpf/test_stub.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2+
*
3+
* This program is free software; you can redistribute it and/or
4+
* modify it under the terms of version 2 of the GNU General Public
5+
* License as published by the Free Software Foundation.
6+
*/
7+
#include <linux/kernel.h>
8+
#include <linux/types.h>
9+
#include <linux/slab.h>
10+
#include <linux/err.h>
11+
#include <linux/bpf.h>
12+
13+
/* test stubs for BPF_MAP_TYPE_UNSPEC and for BPF_PROG_TYPE_UNSPEC
14+
* to be used by user space verifier testsuite
15+
*/
16+
struct bpf_context {
17+
u64 arg1;
18+
u64 arg2;
19+
};
20+
21+
static u64 test_func(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
22+
{
23+
return 0;
24+
}
25+
26+
static struct bpf_func_proto test_funcs[] = {
27+
[BPF_FUNC_unspec] = {
28+
.func = test_func,
29+
.gpl_only = true,
30+
.ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
31+
.arg1_type = ARG_CONST_MAP_PTR,
32+
.arg2_type = ARG_PTR_TO_MAP_KEY,
33+
},
34+
};
35+
36+
static const struct bpf_func_proto *test_func_proto(enum bpf_func_id func_id)
37+
{
38+
if (func_id < 0 || func_id >= ARRAY_SIZE(test_funcs))
39+
return NULL;
40+
return &test_funcs[func_id];
41+
}
42+
43+
static const struct bpf_context_access {
44+
int size;
45+
enum bpf_access_type type;
46+
} test_ctx_access[] = {
47+
[offsetof(struct bpf_context, arg1)] = {
48+
FIELD_SIZEOF(struct bpf_context, arg1),
49+
BPF_READ
50+
},
51+
[offsetof(struct bpf_context, arg2)] = {
52+
FIELD_SIZEOF(struct bpf_context, arg2),
53+
BPF_READ
54+
},
55+
};
56+
57+
static bool test_is_valid_access(int off, int size, enum bpf_access_type type)
58+
{
59+
const struct bpf_context_access *access;
60+
61+
if (off < 0 || off >= ARRAY_SIZE(test_ctx_access))
62+
return false;
63+
64+
access = &test_ctx_access[off];
65+
if (access->size == size && (access->type & type))
66+
return true;
67+
68+
return false;
69+
}
70+
71+
static struct bpf_verifier_ops test_ops = {
72+
.get_func_proto = test_func_proto,
73+
.is_valid_access = test_is_valid_access,
74+
};
75+
76+
static struct bpf_prog_type_list tl_prog = {
77+
.ops = &test_ops,
78+
.type = BPF_PROG_TYPE_UNSPEC,
79+
};
80+
81+
static struct bpf_map *test_map_alloc(union bpf_attr *attr)
82+
{
83+
struct bpf_map *map;
84+
85+
map = kzalloc(sizeof(*map), GFP_USER);
86+
if (!map)
87+
return ERR_PTR(-ENOMEM);
88+
89+
map->key_size = attr->key_size;
90+
map->value_size = attr->value_size;
91+
map->max_entries = attr->max_entries;
92+
return map;
93+
}
94+
95+
static void test_map_free(struct bpf_map *map)
96+
{
97+
kfree(map);
98+
}
99+
100+
static struct bpf_map_ops test_map_ops = {
101+
.map_alloc = test_map_alloc,
102+
.map_free = test_map_free,
103+
};
104+
105+
static struct bpf_map_type_list tl_map = {
106+
.ops = &test_map_ops,
107+
.type = BPF_MAP_TYPE_UNSPEC,
108+
};
109+
110+
static int __init register_test_ops(void)
111+
{
112+
bpf_register_map_type(&tl_map);
113+
bpf_register_prog_type(&tl_prog);
114+
return 0;
115+
}
116+
late_initcall(register_test_ops);

lib/Kconfig.debug

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,8 @@ config TEST_BPF
16721672
against the BPF interpreter or BPF JIT compiler depending on the
16731673
current setting. This is in particular useful for BPF JIT compiler
16741674
development, but also to run regression tests against changes in
1675-
the interpreter code.
1675+
the interpreter code. It also enables test stubs for eBPF maps and
1676+
verifier used by user space verifier testsuite.
16761677

16771678
If unsure, say N.
16781679

samples/bpf/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# kbuild trick to avoid linker error. Can be omitted if a module is built.
2+
obj- := dummy.o
3+
4+
# List of programs to build
5+
hostprogs-y := test_verifier
6+
7+
test_verifier-objs := test_verifier.o libbpf.o
8+
9+
# Tell kbuild to always build the programs
10+
always := $(hostprogs-y)
11+
12+
HOSTCFLAGS += -I$(objtree)/usr/include

samples/bpf/libbpf.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* eBPF mini library */
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <linux/unistd.h>
5+
#include <unistd.h>
6+
#include <string.h>
7+
#include <linux/netlink.h>
8+
#include <linux/bpf.h>
9+
#include <errno.h>
10+
#include "libbpf.h"
11+
12+
static __u64 ptr_to_u64(void *ptr)
13+
{
14+
return (__u64) (unsigned long) ptr;
15+
}
16+
17+
int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
18+
int max_entries)
19+
{
20+
union bpf_attr attr = {
21+
.map_type = map_type,
22+
.key_size = key_size,
23+
.value_size = value_size,
24+
.max_entries = max_entries
25+
};
26+
27+
return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
28+
}
29+
30+
int bpf_update_elem(int fd, void *key, void *value)
31+
{
32+
union bpf_attr attr = {
33+
.map_fd = fd,
34+
.key = ptr_to_u64(key),
35+
.value = ptr_to_u64(value),
36+
};
37+
38+
return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
39+
}
40+
41+
int bpf_lookup_elem(int fd, void *key, void *value)
42+
{
43+
union bpf_attr attr = {
44+
.map_fd = fd,
45+
.key = ptr_to_u64(key),
46+
.value = ptr_to_u64(value),
47+
};
48+
49+
return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
50+
}
51+
52+
int bpf_delete_elem(int fd, void *key)
53+
{
54+
union bpf_attr attr = {
55+
.map_fd = fd,
56+
.key = ptr_to_u64(key),
57+
};
58+
59+
return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
60+
}
61+
62+
int bpf_get_next_key(int fd, void *key, void *next_key)
63+
{
64+
union bpf_attr attr = {
65+
.map_fd = fd,
66+
.key = ptr_to_u64(key),
67+
.next_key = ptr_to_u64(next_key),
68+
};
69+
70+
return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
71+
}
72+
73+
#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
74+
75+
char bpf_log_buf[LOG_BUF_SIZE];
76+
77+
int bpf_prog_load(enum bpf_prog_type prog_type,
78+
const struct bpf_insn *insns, int prog_len,
79+
const char *license)
80+
{
81+
union bpf_attr attr = {
82+
.prog_type = prog_type,
83+
.insns = ptr_to_u64((void *) insns),
84+
.insn_cnt = prog_len / sizeof(struct bpf_insn),
85+
.license = ptr_to_u64((void *) license),
86+
.log_buf = ptr_to_u64(bpf_log_buf),
87+
.log_size = LOG_BUF_SIZE,
88+
.log_level = 1,
89+
};
90+
91+
bpf_log_buf[0] = 0;
92+
93+
return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
94+
}

0 commit comments

Comments
 (0)