1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
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.
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>
13 /* test stubs for BPF_MAP_TYPE_UNSPEC and for BPF_PROG_TYPE_UNSPEC
14 * to be used by user space verifier testsuite
21 static u64
test_func(u64 r1
, u64 r2
, u64 r3
, u64 r4
, u64 r5
)
26 static struct bpf_func_proto test_funcs
[] = {
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
,
36 static const struct bpf_func_proto
*test_func_proto(enum bpf_func_id func_id
)
38 if (func_id
< 0 || func_id
>= ARRAY_SIZE(test_funcs
))
40 return &test_funcs
[func_id
];
43 static const struct bpf_context_access
{
45 enum bpf_access_type type
;
46 } test_ctx_access
[] = {
47 [offsetof(struct bpf_context
, arg1
)] = {
48 FIELD_SIZEOF(struct bpf_context
, arg1
),
51 [offsetof(struct bpf_context
, arg2
)] = {
52 FIELD_SIZEOF(struct bpf_context
, arg2
),
57 static bool test_is_valid_access(int off
, int size
, enum bpf_access_type type
)
59 const struct bpf_context_access
*access
;
61 if (off
< 0 || off
>= ARRAY_SIZE(test_ctx_access
))
64 access
= &test_ctx_access
[off
];
65 if (access
->size
== size
&& (access
->type
& type
))
71 static struct bpf_verifier_ops test_ops
= {
72 .get_func_proto
= test_func_proto
,
73 .is_valid_access
= test_is_valid_access
,
76 static struct bpf_prog_type_list tl_prog
= {
78 .type
= BPF_PROG_TYPE_UNSPEC
,
81 static struct bpf_map
*test_map_alloc(union bpf_attr
*attr
)
85 map
= kzalloc(sizeof(*map
), GFP_USER
);
87 return ERR_PTR(-ENOMEM
);
89 map
->key_size
= attr
->key_size
;
90 map
->value_size
= attr
->value_size
;
91 map
->max_entries
= attr
->max_entries
;
95 static void test_map_free(struct bpf_map
*map
)
100 static struct bpf_map_ops test_map_ops
= {
101 .map_alloc
= test_map_alloc
,
102 .map_free
= test_map_free
,
105 static struct bpf_map_type_list tl_map
= {
106 .ops
= &test_map_ops
,
107 .type
= BPF_MAP_TYPE_UNSPEC
,
110 static int __init
register_test_ops(void)
112 bpf_register_map_type(&tl_map
);
113 bpf_register_prog_type(&tl_prog
);
116 late_initcall(register_test_ops
);