Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / kernel / bpf / test_stub.c
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 const struct bpf_func_proto *test_func_proto(enum bpf_func_id func_id)
22 {
23         switch (func_id) {
24         case BPF_FUNC_map_lookup_elem:
25                 return &bpf_map_lookup_elem_proto;
26         case BPF_FUNC_map_update_elem:
27                 return &bpf_map_update_elem_proto;
28         case BPF_FUNC_map_delete_elem:
29                 return &bpf_map_delete_elem_proto;
30         default:
31                 return NULL;
32         }
33 }
34
35 static const struct bpf_context_access {
36         int size;
37         enum bpf_access_type type;
38 } test_ctx_access[] = {
39         [offsetof(struct bpf_context, arg1)] = {
40                 FIELD_SIZEOF(struct bpf_context, arg1),
41                 BPF_READ
42         },
43         [offsetof(struct bpf_context, arg2)] = {
44                 FIELD_SIZEOF(struct bpf_context, arg2),
45                 BPF_READ
46         },
47 };
48
49 static bool test_is_valid_access(int off, int size, enum bpf_access_type type)
50 {
51         const struct bpf_context_access *access;
52
53         if (off < 0 || off >= ARRAY_SIZE(test_ctx_access))
54                 return false;
55
56         access = &test_ctx_access[off];
57         if (access->size == size && (access->type & type))
58                 return true;
59
60         return false;
61 }
62
63 static struct bpf_verifier_ops test_ops = {
64         .get_func_proto = test_func_proto,
65         .is_valid_access = test_is_valid_access,
66 };
67
68 static struct bpf_prog_type_list tl_prog = {
69         .ops = &test_ops,
70         .type = BPF_PROG_TYPE_UNSPEC,
71 };
72
73 static int __init register_test_ops(void)
74 {
75         bpf_register_prog_type(&tl_prog);
76         return 0;
77 }
78 late_initcall(register_test_ops);