]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/bpf/helpers.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[mirror_ubuntu-artful-kernel.git] / kernel / bpf / helpers.c
CommitLineData
d0003ec0
AS
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 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 */
12#include <linux/bpf.h>
13#include <linux/rcupdate.h>
03e69b50 14#include <linux/random.h>
c04167ce 15#include <linux/smp.h>
17ca8cbf 16#include <linux/ktime.h>
d0003ec0
AS
17
18/* If kernel subsystem is allowing eBPF programs to call this function,
19 * inside its own verifier_ops->get_func_proto() callback it should return
20 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
21 *
22 * Different map implementations will rely on rcu in map methods
23 * lookup/update/delete, therefore eBPF programs must run under rcu lock
24 * if program is allowed to access maps, so check rcu_read_lock_held in
25 * all three functions.
26 */
27static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
28{
29 /* verifier checked that R1 contains a valid pointer to bpf_map
30 * and R2 points to a program stack and map->key_size bytes were
31 * initialized
32 */
33 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
34 void *key = (void *) (unsigned long) r2;
35 void *value;
36
37 WARN_ON_ONCE(!rcu_read_lock_held());
38
39 value = map->ops->map_lookup_elem(map, key);
40
41 /* lookup() returns either pointer to element value or NULL
42 * which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type
43 */
44 return (unsigned long) value;
45}
46
a2c83fff 47const struct bpf_func_proto bpf_map_lookup_elem_proto = {
3324b584
DB
48 .func = bpf_map_lookup_elem,
49 .gpl_only = false,
50 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
51 .arg1_type = ARG_CONST_MAP_PTR,
52 .arg2_type = ARG_PTR_TO_MAP_KEY,
d0003ec0
AS
53};
54
55static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
56{
57 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
58 void *key = (void *) (unsigned long) r2;
59 void *value = (void *) (unsigned long) r3;
60
61 WARN_ON_ONCE(!rcu_read_lock_held());
62
63 return map->ops->map_update_elem(map, key, value, r4);
64}
65
a2c83fff 66const struct bpf_func_proto bpf_map_update_elem_proto = {
3324b584
DB
67 .func = bpf_map_update_elem,
68 .gpl_only = false,
69 .ret_type = RET_INTEGER,
70 .arg1_type = ARG_CONST_MAP_PTR,
71 .arg2_type = ARG_PTR_TO_MAP_KEY,
72 .arg3_type = ARG_PTR_TO_MAP_VALUE,
73 .arg4_type = ARG_ANYTHING,
d0003ec0
AS
74};
75
76static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
77{
78 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
79 void *key = (void *) (unsigned long) r2;
80
81 WARN_ON_ONCE(!rcu_read_lock_held());
82
83 return map->ops->map_delete_elem(map, key);
84}
85
a2c83fff 86const struct bpf_func_proto bpf_map_delete_elem_proto = {
3324b584
DB
87 .func = bpf_map_delete_elem,
88 .gpl_only = false,
89 .ret_type = RET_INTEGER,
90 .arg1_type = ARG_CONST_MAP_PTR,
91 .arg2_type = ARG_PTR_TO_MAP_KEY,
d0003ec0 92};
03e69b50
DB
93
94static u64 bpf_get_prandom_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
95{
96 return prandom_u32();
97}
98
99const struct bpf_func_proto bpf_get_prandom_u32_proto = {
100 .func = bpf_get_prandom_u32,
101 .gpl_only = false,
102 .ret_type = RET_INTEGER,
103};
c04167ce
DB
104
105static u64 bpf_get_smp_processor_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
106{
107 return raw_smp_processor_id();
108}
109
110const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
111 .func = bpf_get_smp_processor_id,
112 .gpl_only = false,
113 .ret_type = RET_INTEGER,
114};
17ca8cbf
DB
115
116static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
117{
118 /* NMI safe access to clock monotonic */
119 return ktime_get_mono_fast_ns();
120}
121
122const struct bpf_func_proto bpf_ktime_get_ns_proto = {
123 .func = bpf_ktime_get_ns,
124 .gpl_only = true,
125 .ret_type = RET_INTEGER,
126};