blob: bd7f5988ed9cca0f4973388f68741444b0cbfcb8 [file] [log] [blame]
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -08001/* 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>
Daniel Borkmann03e69b52015-03-14 02:27:16 +010014#include <linux/random.h>
Daniel Borkmannc04167c2015-03-14 02:27:17 +010015#include <linux/smp.h>
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080016
17/* If kernel subsystem is allowing eBPF programs to call this function,
18 * inside its own verifier_ops->get_func_proto() callback it should return
19 * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
20 *
21 * Different map implementations will rely on rcu in map methods
22 * lookup/update/delete, therefore eBPF programs must run under rcu lock
23 * if program is allowed to access maps, so check rcu_read_lock_held in
24 * all three functions.
25 */
26static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
27{
28 /* verifier checked that R1 contains a valid pointer to bpf_map
29 * and R2 points to a program stack and map->key_size bytes were
30 * initialized
31 */
32 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
33 void *key = (void *) (unsigned long) r2;
34 void *value;
35
36 WARN_ON_ONCE(!rcu_read_lock_held());
37
38 value = map->ops->map_lookup_elem(map, key);
39
40 /* lookup() returns either pointer to element value or NULL
41 * which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type
42 */
43 return (unsigned long) value;
44}
45
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010046const struct bpf_func_proto bpf_map_lookup_elem_proto = {
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080047 .func = bpf_map_lookup_elem,
48 .gpl_only = false,
49 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
50 .arg1_type = ARG_CONST_MAP_PTR,
51 .arg2_type = ARG_PTR_TO_MAP_KEY,
52};
53
54static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
55{
56 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
57 void *key = (void *) (unsigned long) r2;
58 void *value = (void *) (unsigned long) r3;
59
60 WARN_ON_ONCE(!rcu_read_lock_held());
61
62 return map->ops->map_update_elem(map, key, value, r4);
63}
64
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010065const struct bpf_func_proto bpf_map_update_elem_proto = {
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080066 .func = bpf_map_update_elem,
67 .gpl_only = false,
68 .ret_type = RET_INTEGER,
69 .arg1_type = ARG_CONST_MAP_PTR,
70 .arg2_type = ARG_PTR_TO_MAP_KEY,
71 .arg3_type = ARG_PTR_TO_MAP_VALUE,
72 .arg4_type = ARG_ANYTHING,
73};
74
75static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
76{
77 struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
78 void *key = (void *) (unsigned long) r2;
79
80 WARN_ON_ONCE(!rcu_read_lock_held());
81
82 return map->ops->map_delete_elem(map, key);
83}
84
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +010085const struct bpf_func_proto bpf_map_delete_elem_proto = {
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -080086 .func = bpf_map_delete_elem,
87 .gpl_only = false,
88 .ret_type = RET_INTEGER,
89 .arg1_type = ARG_CONST_MAP_PTR,
90 .arg2_type = ARG_PTR_TO_MAP_KEY,
91};
Daniel Borkmann03e69b52015-03-14 02:27:16 +010092
93static u64 bpf_get_prandom_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
94{
95 return prandom_u32();
96}
97
98const struct bpf_func_proto bpf_get_prandom_u32_proto = {
99 .func = bpf_get_prandom_u32,
100 .gpl_only = false,
101 .ret_type = RET_INTEGER,
102};
Daniel Borkmannc04167c2015-03-14 02:27:17 +0100103
104static u64 bpf_get_smp_processor_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
105{
106 return raw_smp_processor_id();
107}
108
109const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
110 .func = bpf_get_smp_processor_id,
111 .gpl_only = false,
112 .ret_type = RET_INTEGER,
113};