blob: b457fbe7b70bc910bf8144bb2aa209c9da26e3bc [file] [log] [blame]
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001/* 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#ifndef _LINUX_BPF_H
8#define _LINUX_BPF_H 1
9
10#include <uapi/linux/bpf.h>
Daniel Borkmann74451e662017-02-16 22:24:50 +010011
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070012#include <linux/workqueue.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070013#include <linux/file.h>
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080014#include <linux/percpu.h>
Zi Shen Lim002245c2016-06-08 21:18:47 -070015#include <linux/err.h>
Daniel Borkmann74451e662017-02-16 22:24:50 +010016#include <linux/rbtree_latch.h>
David S. Millerd6e1e462017-08-19 23:34:03 -070017#include <linux/numa.h>
Jakub Kicinskiab3f0062017-11-03 13:56:17 -070018#include <linux/wait.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070019
Jakub Kicinskicae19272017-12-27 18:39:05 -080020struct bpf_verifier_env;
Daniel Borkmann3b1efb12016-06-15 22:47:14 +020021struct perf_event;
John Fastabend174a79f2017-08-15 22:32:47 -070022struct bpf_prog;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070023struct bpf_map;
John Fastabend4f738ad2018-03-18 12:57:10 -070024struct sock;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070025struct seq_file;
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020026struct btf_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070027
28/* map is generic key/value storage optionally accesible by eBPF programs */
29struct bpf_map_ops {
30 /* funcs callable from userspace (via syscall) */
Jakub Kicinski1110f3a2018-01-11 20:29:03 -080031 int (*map_alloc_check)(union bpf_attr *attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070032 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +020033 void (*map_release)(struct bpf_map *map, struct file *map_file);
34 void (*map_free)(struct bpf_map *map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070035 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
John Fastabendba6b8de2018-04-23 15:39:23 -070036 void (*map_release_uref)(struct bpf_map *map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070037
38 /* funcs callable from userspace and from eBPF programs */
39 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
Alexei Starovoitov3274f522014-11-13 17:36:44 -080040 int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070041 int (*map_delete_elem)(struct bpf_map *map, void *key);
Wang Nan2a36f0b2015-08-06 07:02:33 +000042
43 /* funcs called by prog_array and perf_event_array map */
Daniel Borkmannd056a782016-06-15 22:47:13 +020044 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
45 int fd);
46 void (*map_fd_put_ptr)(void *ptr);
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -070047 u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070048 u32 (*map_fd_sys_lookup_elem)(void *ptr);
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070049 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
50 struct seq_file *m);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +020051 int (*map_check_btf)(const struct bpf_map *map,
52 const struct btf_type *key_type,
53 const struct btf_type *value_type);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070054};
55
56struct bpf_map {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070057 /* The first two cachelines with read-mostly members of which some
Daniel Borkmannbe95a842018-01-09 13:17:44 +010058 * are also accessed in fast-path (e.g. ops, max_entries).
59 */
60 const struct bpf_map_ops *ops ____cacheline_aligned;
61 struct bpf_map *inner_map_meta;
62#ifdef CONFIG_SECURITY
63 void *security;
64#endif
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070065 enum bpf_map_type map_type;
66 u32 key_size;
67 u32 value_size;
68 u32 max_entries;
Alexei Starovoitov6c905982016-03-07 21:57:15 -080069 u32 map_flags;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070070 u32 pages;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070071 u32 id;
Martin KaFai Lau96eabe72017-08-18 11:28:00 -070072 int numa_node;
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -070073 u32 btf_key_type_id;
74 u32 btf_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070075 struct btf *btf;
Alexei Starovoitovb2157392018-01-07 17:33:02 -080076 bool unpriv_array;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070077 /* 55 bytes hole */
Daniel Borkmannbe95a842018-01-09 13:17:44 +010078
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -070079 /* The 3rd and 4th cacheline with misc members to avoid false sharing
Daniel Borkmannbe95a842018-01-09 13:17:44 +010080 * particularly with refcounting.
81 */
82 struct user_struct *user ____cacheline_aligned;
83 atomic_t refcnt;
Daniel Borkmannc9da1612015-11-24 21:28:15 +010084 atomic_t usercnt;
Daniel Borkmannbe95a842018-01-09 13:17:44 +010085 struct work_struct work;
Martin KaFai Lau067cae42017-10-05 21:52:12 -070086 char name[BPF_OBJ_NAME_LEN];
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070087};
88
Jakub Kicinski602144c2018-07-17 10:53:25 -070089struct bpf_offload_dev;
Jakub Kicinskia3884572018-01-11 20:29:09 -080090struct bpf_offloaded_map;
91
92struct bpf_map_dev_ops {
93 int (*map_get_next_key)(struct bpf_offloaded_map *map,
94 void *key, void *next_key);
95 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
96 void *key, void *value);
97 int (*map_update_elem)(struct bpf_offloaded_map *map,
98 void *key, void *value, u64 flags);
99 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
100};
101
102struct bpf_offloaded_map {
103 struct bpf_map map;
104 struct net_device *netdev;
105 const struct bpf_map_dev_ops *dev_ops;
106 void *dev_priv;
107 struct list_head offloads;
108};
109
110static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
111{
112 return container_of(map, struct bpf_offloaded_map, map);
113}
114
Jakub Kicinski0cd3cbe2018-05-03 18:37:08 -0700115static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
116{
117 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
118}
119
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700120static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
121{
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200122 return map->btf && map->ops->map_seq_show_elem;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700123}
124
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200125int map_check_no_btf(const struct bpf_map *map,
126 const struct btf_type *key_type,
127 const struct btf_type *value_type);
128
Jakub Kicinskia3884572018-01-11 20:29:09 -0800129extern const struct bpf_map_ops bpf_map_offload_ops;
130
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700131/* function argument constraints */
132enum bpf_arg_type {
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100133 ARG_DONTCARE = 0, /* unused argument in helper function */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700134
135 /* the following constraints used to prototype
136 * bpf_map_lookup/update/delete_elem() functions
137 */
138 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
139 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
140 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
141
142 /* the following constraints used to prototype bpf_memcmp() and other
143 * functions that access data on eBPF program stack
144 */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800145 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
Gianluca Borellodb1ac492017-11-22 18:32:53 +0000146 ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800147 ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
148 * helper function must fill all bytes or clear
149 * them in error case.
Daniel Borkmann435faee12016-04-13 00:10:51 +0200150 */
151
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -0800152 ARG_CONST_SIZE, /* number of bytes accessed from memory */
153 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100154
Alexei Starovoitov608cd712015-03-26 19:53:57 -0700155 ARG_PTR_TO_CTX, /* pointer to context */
Daniel Borkmann80f1d682015-03-12 17:21:42 +0100156 ARG_ANYTHING, /* any (initialized) argument is ok */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700157};
158
159/* type of values returned from helper functions */
160enum bpf_return_type {
161 RET_INTEGER, /* function returns integer */
162 RET_VOID, /* function doesn't return anything */
Roman Gushchin3e6a4b32018-08-02 14:27:22 -0700163 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700164 RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
165};
166
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700167/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
168 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
169 * instructions after verifying
170 */
171struct bpf_func_proto {
172 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
173 bool gpl_only;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200174 bool pkt_access;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700175 enum bpf_return_type ret_type;
176 enum bpf_arg_type arg1_type;
177 enum bpf_arg_type arg2_type;
178 enum bpf_arg_type arg3_type;
179 enum bpf_arg_type arg4_type;
180 enum bpf_arg_type arg5_type;
181};
182
183/* bpf_context is intentionally undefined structure. Pointer to bpf_context is
184 * the first argument to eBPF programs.
185 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
186 */
187struct bpf_context;
188
189enum bpf_access_type {
190 BPF_READ = 1,
191 BPF_WRITE = 2
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700192};
193
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700194/* types of values stored in eBPF registers */
Edward Creef1174f72017-08-07 15:26:19 +0100195/* Pointer types represent:
196 * pointer
197 * pointer + imm
198 * pointer + (u16) var
199 * pointer + (u16) var + imm
200 * if (range > 0) then [ptr, ptr + range - off) is safe to access
201 * if (id > 0) means that some 'var' was added
202 * if (off > 0) means that 'imm' was added
203 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700204enum bpf_reg_type {
205 NOT_INIT = 0, /* nothing was written into register */
Edward Creef1174f72017-08-07 15:26:19 +0100206 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700207 PTR_TO_CTX, /* reg points to bpf_context */
208 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
209 PTR_TO_MAP_VALUE, /* reg points to map element value */
210 PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
Edward Creef1174f72017-08-07 15:26:19 +0100211 PTR_TO_STACK, /* reg == frame_pointer + offset */
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200212 PTR_TO_PACKET_META, /* skb->data - meta_len */
Edward Creef1174f72017-08-07 15:26:19 +0100213 PTR_TO_PACKET, /* reg points to skb->data */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700214 PTR_TO_PACKET_END, /* skb->data + headlen */
Petar Penkovd58e4682018-09-14 07:46:18 -0700215 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700216};
217
Yonghong Song23994632017-06-22 15:07:39 -0700218/* The information passed from prog-specific *_is_valid_access
219 * back to the verifier.
220 */
221struct bpf_insn_access_aux {
222 enum bpf_reg_type reg_type;
223 int ctx_field_size;
Yonghong Song23994632017-06-22 15:07:39 -0700224};
225
Daniel Borkmannf96da092017-07-02 02:13:27 +0200226static inline void
227bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
228{
229 aux->ctx_field_size = size;
230}
231
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700232struct bpf_prog_ops {
233 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
234 union bpf_attr __user *uattr);
235};
236
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700237struct bpf_verifier_ops {
238 /* return eBPF function prototype for verification */
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700239 const struct bpf_func_proto *
240 (*get_func_proto)(enum bpf_func_id func_id,
241 const struct bpf_prog *prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700242
243 /* return true if 'size' wide access at offset 'off' within bpf_context
244 * with 'type' (read or write) is allowed
245 */
Alexei Starovoitov19de99f2016-06-15 18:25:38 -0700246 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
Andrey Ignatov5e43f892018-03-30 15:08:00 -0700247 const struct bpf_prog *prog,
Yonghong Song23994632017-06-22 15:07:39 -0700248 struct bpf_insn_access_aux *info);
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200249 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
250 const struct bpf_prog *prog);
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +0200251 int (*gen_ld_abs)(const struct bpf_insn *orig,
252 struct bpf_insn *insn_buf);
Daniel Borkmann6b8cc1d2017-01-12 11:51:32 +0100253 u32 (*convert_ctx_access)(enum bpf_access_type type,
254 const struct bpf_insn *src,
255 struct bpf_insn *dst,
Daniel Borkmannf96da092017-07-02 02:13:27 +0200256 struct bpf_prog *prog, u32 *target_size);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700257};
258
Jakub Kicinskicae19272017-12-27 18:39:05 -0800259struct bpf_prog_offload_ops {
260 int (*insn_hook)(struct bpf_verifier_env *env,
261 int insn_idx, int prev_insn_idx);
262};
263
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800264struct bpf_prog_offload {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700265 struct bpf_prog *prog;
266 struct net_device *netdev;
267 void *dev_priv;
268 struct list_head offloads;
269 bool dev_state;
Jakub Kicinskicae19272017-12-27 18:39:05 -0800270 const struct bpf_prog_offload_ops *dev_ops;
Jiong Wangfcfb1262018-01-16 16:05:19 -0800271 void *jited_image;
272 u32 jited_len;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700273};
274
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000275enum bpf_cgroup_storage_type {
276 BPF_CGROUP_STORAGE_SHARED,
277 __BPF_CGROUP_STORAGE_MAX
278};
279
280#define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
281
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700282struct bpf_prog_aux {
283 atomic_t refcnt;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100284 u32 used_map_cnt;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700285 u32 max_ctx_offset;
Alexei Starovoitov87266792017-05-30 13:31:29 -0700286 u32 stack_depth;
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -0700287 u32 id;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800288 u32 func_cnt;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -0800289 bool offload_requested;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800290 struct bpf_prog **func;
291 void *jit_data; /* JIT specific data. arch dependent */
Daniel Borkmann74451e662017-02-16 22:24:50 +0100292 struct latch_tree_node ksym_tnode;
293 struct list_head ksym_lnode;
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700294 const struct bpf_prog_ops *ops;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700295 struct bpf_map **used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700296 struct bpf_prog *prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700297 struct user_struct *user;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700298 u64 load_time; /* ns since boottime */
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000299 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Martin KaFai Lau067cae42017-10-05 21:52:12 -0700300 char name[BPF_OBJ_NAME_LEN];
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700301#ifdef CONFIG_SECURITY
302 void *security;
303#endif
Jakub Kicinski0a9c1992018-01-11 20:29:07 -0800304 struct bpf_prog_offload *offload;
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700305 union {
306 struct work_struct work;
307 struct rcu_head rcu;
308 };
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700309};
310
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700311struct bpf_array {
312 struct bpf_map map;
313 u32 elem_size;
Alexei Starovoitovb2157392018-01-07 17:33:02 -0800314 u32 index_mask;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700315 /* 'ownership' of prog_array is claimed by the first program that
316 * is going to use this map or by the first program which FD is stored
317 * in the map to make sure that all callers and callees have the same
318 * prog_type and JITed flag
319 */
320 enum bpf_prog_type owner_prog_type;
321 bool owner_jited;
322 union {
323 char value[0] __aligned(8);
Wang Nan2a36f0b2015-08-06 07:02:33 +0000324 void *ptrs[0] __aligned(8);
Alexei Starovoitova10423b2016-02-01 22:39:54 -0800325 void __percpu *pptrs[0] __aligned(8);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700326 };
327};
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200328
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700329#define MAX_TAIL_CALL_CNT 32
330
Daniel Borkmann3b1efb12016-06-15 22:47:14 +0200331struct bpf_event_entry {
332 struct perf_event *event;
333 struct file *perf_file;
334 struct file *map_file;
335 struct rcu_head rcu;
336};
337
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700338bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
Daniel Borkmannf1f77142017-01-13 23:38:15 +0100339int bpf_prog_calc_tag(struct bpf_prog *fp);
Daniel Borkmannbd570ff2016-04-18 21:01:24 +0200340
Alexei Starovoitov0756ea32015-06-12 19:39:13 -0700341const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200342
343typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +0200344 unsigned long off, unsigned long len);
Daniel Borkmann555c8a82016-07-14 18:08:05 +0200345
346u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
347 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700348
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -0700349int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
350 union bpf_attr __user *uattr);
351int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
352 union bpf_attr __user *uattr);
353
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700354/* an array of programs to be executed under rcu_lock.
355 *
356 * Typical usage:
357 * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
358 *
359 * the structure returned by bpf_prog_array_alloc() should be populated
360 * with program pointers and the last pointer must be NULL.
361 * The user has to keep refcnt on the program and make sure the program
362 * is removed from the array before bpf_prog_put().
363 * The 'struct bpf_prog_array *' should only be replaced with xchg()
364 * since other cpus are walking the array of pointers in parallel.
365 */
Roman Gushchin394e40a2018-08-02 14:27:21 -0700366struct bpf_prog_array_item {
367 struct bpf_prog *prog;
Roman Gushchin8bad74f2018-09-28 14:45:36 +0000368 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
Roman Gushchin394e40a2018-08-02 14:27:21 -0700369};
370
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700371struct bpf_prog_array {
372 struct rcu_head rcu;
Roman Gushchin394e40a2018-08-02 14:27:21 -0700373 struct bpf_prog_array_item items[0];
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700374};
375
Roman Gushchind29ab6e2018-07-13 12:41:10 -0700376struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700377void bpf_prog_array_free(struct bpf_prog_array __rcu *progs);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -0700378int bpf_prog_array_length(struct bpf_prog_array __rcu *progs);
379int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
380 __u32 __user *prog_ids, u32 cnt);
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700381
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700382void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
383 struct bpf_prog *old_prog);
Yonghong Songf371b302017-12-11 11:39:02 -0800384int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
Yonghong Song3a38bb92018-04-10 09:37:32 -0700385 u32 *prog_ids, u32 request_cnt,
386 u32 *prog_cnt);
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700387int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
388 struct bpf_prog *exclude_prog,
389 struct bpf_prog *include_prog,
390 struct bpf_prog_array **new_array);
391
392#define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null) \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700393 ({ \
Roman Gushchin394e40a2018-08-02 14:27:21 -0700394 struct bpf_prog_array_item *_item; \
395 struct bpf_prog *_prog; \
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700396 struct bpf_prog_array *_array; \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700397 u32 _ret = 1; \
Roman Gushchin6899b322018-04-23 18:09:21 +0100398 preempt_disable(); \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700399 rcu_read_lock(); \
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700400 _array = rcu_dereference(array); \
401 if (unlikely(check_non_null && !_array))\
402 goto _out; \
Roman Gushchin394e40a2018-08-02 14:27:21 -0700403 _item = &_array->items[0]; \
404 while ((_prog = READ_ONCE(_item->prog))) { \
405 bpf_cgroup_storage_set(_item->cgroup_storage); \
406 _ret &= func(_prog, ctx); \
407 _item++; \
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700408 } \
409_out: \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700410 rcu_read_unlock(); \
Roman Gushchin6899b322018-04-23 18:09:21 +0100411 preempt_enable_no_resched(); \
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -0700412 _ret; \
413 })
414
Yonghong Songe87c6bc2017-10-23 23:53:08 -0700415#define BPF_PROG_RUN_ARRAY(array, ctx, func) \
416 __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
417
418#define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func) \
419 __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
420
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800421#ifdef CONFIG_BPF_SYSCALL
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -0800422DECLARE_PER_CPU(int, bpf_prog_active);
423
Chenbo Fengf66e4482017-10-18 13:00:26 -0700424extern const struct file_operations bpf_map_fops;
425extern const struct file_operations bpf_prog_fops;
426
Jakub Kicinski7de16e32017-10-16 16:40:53 -0700427#define BPF_PROG_TYPE(_id, _name) \
428 extern const struct bpf_prog_ops _name ## _prog_ops; \
429 extern const struct bpf_verifier_ops _name ## _verifier_ops;
Johannes Berg40077e02017-04-11 15:34:58 +0200430#define BPF_MAP_TYPE(_id, _ops) \
431 extern const struct bpf_map_ops _ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +0200432#include <linux/bpf_types.h>
433#undef BPF_PROG_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +0200434#undef BPF_MAP_TYPE
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100435
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700436extern const struct bpf_prog_ops bpf_offload_prog_ops;
Jakub Kicinski4f9218a2017-10-16 16:40:55 -0700437extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
438extern const struct bpf_verifier_ops xdp_analyzer_ops;
439
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700440struct bpf_prog *bpf_prog_get(u32 ufd);
Jakub Kicinski248f3462017-11-03 13:56:20 -0700441struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -0800442 bool attach_drv);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100443struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog, int i);
Daniel Borkmannc5405942016-11-09 22:02:34 +0100444void bpf_prog_sub(struct bpf_prog *prog, int i);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100445struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog);
John Fastabenda6f6df62017-08-15 22:32:22 -0700446struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
Daniel Borkmann61e021f2015-03-02 15:21:55 +0100447void bpf_prog_put(struct bpf_prog *prog);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100448int __bpf_prog_charge(struct user_struct *user, u32 pages);
449void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
Daniel Borkmann61e021f2015-03-02 15:21:55 +0100450
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800451void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800452void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -0800453
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100454struct bpf_map *bpf_map_get_with_uref(u32 ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100455struct bpf_map *__bpf_map_get(struct fd f);
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100456struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100457void bpf_map_put_with_uref(struct bpf_map *map);
Daniel Borkmann61e021f2015-03-02 15:21:55 +0100458void bpf_map_put(struct bpf_map *map);
Alexei Starovoitov6c905982016-03-07 21:57:15 -0800459int bpf_map_precharge_memlock(u32 pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700460int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
461void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700462void *bpf_map_area_alloc(size_t size, int numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100463void bpf_map_area_free(void *base);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800464void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
Daniel Borkmann61e021f2015-03-02 15:21:55 +0100465
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700466extern int sysctl_unprivileged_bpf_disabled;
467
Chenbo Feng6e71b042017-10-18 13:00:22 -0700468int bpf_map_new_fd(struct bpf_map *map, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100469int bpf_prog_new_fd(struct bpf_prog *prog);
470
471int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
Chenbo Feng6e71b042017-10-18 13:00:22 -0700472int bpf_obj_get_user(const char __user *pathname, int flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +0100473
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800474int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
475int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
476int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
477 u64 flags);
478int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
479 u64 flags);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200480
Alexei Starovoitov557c0c62016-03-07 21:57:17 -0800481int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800482
Daniel Borkmannd056a782016-06-15 22:47:13 +0200483int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
484 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700485int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -0700486int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
487 void *key, void *value, u64 map_flags);
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -0700488int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
Daniel Borkmannd056a782016-06-15 22:47:13 +0200489
Chenbo Feng6e71b042017-10-18 13:00:22 -0700490int bpf_get_file_flag(int flags);
Martin KaFai Laudcab51f2018-05-22 15:03:31 -0700491int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
492 size_t actual_size);
Chenbo Feng6e71b042017-10-18 13:00:22 -0700493
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800494/* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
495 * forced to use 'long' read/writes to try to atomically copy long counters.
496 * Best-effort only. No barriers here, since it _will_ race with concurrent
497 * updates from BPF programs. Called from bpf syscall and mostly used with
498 * size 8 or 16 bytes, so ask compiler to inline it.
499 */
500static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
501{
502 const long *lsrc = src;
503 long *ldst = dst;
504
505 size /= sizeof(long);
506 while (size--)
507 *ldst++ = *lsrc++;
508}
509
Daniel Borkmann61e021f2015-03-02 15:21:55 +0100510/* verify correctness of eBPF program */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700511int bpf_check(struct bpf_prog **fp, union bpf_attr *attr);
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -0800512void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
John Fastabend46f55cf2017-07-17 21:56:48 -0700513
514/* Map specifics */
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200515struct xdp_buff;
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900516struct sk_buff;
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200517
518struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
John Fastabend46f55cf2017-07-17 21:56:48 -0700519void __dev_map_insert_ctx(struct bpf_map *map, u32 index);
520void __dev_map_flush(struct bpf_map *map);
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200521int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
522 struct net_device *dev_rx);
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900523int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
524 struct bpf_prog *xdp_prog);
John Fastabend46f55cf2017-07-17 21:56:48 -0700525
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200526struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
527void __cpu_map_insert_ctx(struct bpf_map *map, u32 index);
528void __cpu_map_flush(struct bpf_map *map);
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200529int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
530 struct net_device *dev_rx);
531
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700532/* Return map's numa specified by userspace */
533static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
534{
535 return (attr->map_flags & BPF_F_NUMA_NODE) ?
536 attr->numa_node : NUMA_NO_NODE;
537}
538
Al Viro040ee692017-12-02 20:20:38 -0500539struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700540int array_map_alloc_check(union bpf_attr *attr);
Al Viro040ee692017-12-02 20:20:38 -0500541
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200542#else /* !CONFIG_BPF_SYSCALL */
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100543static inline struct bpf_prog *bpf_prog_get(u32 ufd)
544{
545 return ERR_PTR(-EOPNOTSUPP);
546}
547
Jakub Kicinski248f3462017-11-03 13:56:20 -0700548static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
549 enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -0800550 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -0700551{
552 return ERR_PTR(-EOPNOTSUPP);
553}
554
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100555static inline struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog,
556 int i)
Brenden Blancocc2e0b32016-07-20 07:55:52 -0700557{
558 return ERR_PTR(-EOPNOTSUPP);
559}
Daniel Borkmann113214b2016-06-30 17:24:44 +0200560
Daniel Borkmannc5405942016-11-09 22:02:34 +0100561static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
562{
563}
564
Daniel Borkmann0fc174d2015-03-01 12:31:44 +0100565static inline void bpf_prog_put(struct bpf_prog *prog)
566{
567}
Daniel Borkmann6d67942dd2016-11-19 01:45:03 +0100568
569static inline struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -0700570{
571 return ERR_PTR(-EOPNOTSUPP);
572}
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100573
John Fastabenda6f6df62017-08-15 22:32:22 -0700574static inline struct bpf_prog *__must_check
575bpf_prog_inc_not_zero(struct bpf_prog *prog)
576{
577 return ERR_PTR(-EOPNOTSUPP);
578}
579
Daniel Borkmann5ccb0712016-12-18 01:52:58 +0100580static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
581{
582 return 0;
583}
584
585static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
586{
587}
John Fastabend46f55cf2017-07-17 21:56:48 -0700588
Chenbo Feng6e71b042017-10-18 13:00:22 -0700589static inline int bpf_obj_get_user(const char __user *pathname, int flags)
Shmulik Ladkani98589a02017-10-09 15:27:15 +0300590{
591 return -EOPNOTSUPP;
592}
593
John Fastabend46f55cf2017-07-17 21:56:48 -0700594static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
595 u32 key)
596{
597 return NULL;
598}
599
600static inline void __dev_map_insert_ctx(struct bpf_map *map, u32 index)
601{
602}
603
604static inline void __dev_map_flush(struct bpf_map *map)
605{
606}
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200607
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200608struct xdp_buff;
609struct bpf_dtab_netdev;
610
611static inline
Jesper Dangaard Brouer38edddb2018-05-24 16:45:57 +0200612int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
613 struct net_device *dev_rx)
Jesper Dangaard Brouer67f29e02018-05-24 16:45:46 +0200614{
615 return 0;
616}
617
Toshiaki Makita6d5fc192018-06-14 11:07:42 +0900618struct sk_buff;
619
620static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
621 struct sk_buff *skb,
622 struct bpf_prog *xdp_prog)
623{
624 return 0;
625}
626
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200627static inline
628struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
629{
630 return NULL;
631}
632
633static inline void __cpu_map_insert_ctx(struct bpf_map *map, u32 index)
634{
635}
636
637static inline void __cpu_map_flush(struct bpf_map *map)
638{
639}
640
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +0200641static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
642 struct xdp_buff *xdp,
643 struct net_device *dev_rx)
644{
645 return 0;
646}
Al Viro040ee692017-12-02 20:20:38 -0500647
648static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
649 enum bpf_prog_type type)
650{
651 return ERR_PTR(-EOPNOTSUPP);
652}
Daniel Borkmann61e021f2015-03-02 15:21:55 +0100653#endif /* CONFIG_BPF_SYSCALL */
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700654
Jakub Kicinski479321e2017-11-20 15:21:56 -0800655static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
656 enum bpf_prog_type type)
657{
658 return bpf_prog_get_type_dev(ufd, type, false);
659}
660
Al Viro040ee692017-12-02 20:20:38 -0500661bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
662
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700663int bpf_prog_offload_compile(struct bpf_prog *prog);
664void bpf_prog_offload_destroy(struct bpf_prog *prog);
Jakub Kicinski675fc272017-12-27 18:39:09 -0800665int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
666 struct bpf_prog *prog);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700667
Jakub Kicinski52775b32018-01-17 19:13:28 -0800668int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
669
Jakub Kicinskia3884572018-01-11 20:29:09 -0800670int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
671int bpf_map_offload_update_elem(struct bpf_map *map,
672 void *key, void *value, u64 flags);
673int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
674int bpf_map_offload_get_next_key(struct bpf_map *map,
675 void *key, void *next_key);
676
Jakub Kicinski09728262018-07-17 10:53:23 -0700677bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800678
Jakub Kicinski602144c2018-07-17 10:53:25 -0700679struct bpf_offload_dev *bpf_offload_dev_create(void);
680void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
681int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
682 struct net_device *netdev);
683void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
684 struct net_device *netdev);
Jakub Kicinskifd4f2272018-07-17 10:53:26 -0700685bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
Jakub Kicinski9fd7c552018-07-17 10:53:24 -0700686
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700687#if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
688int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
689
Jakub Kicinski0d830032018-05-08 19:37:06 -0700690static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700691{
Jakub Kicinski9a18eed2017-12-27 18:39:04 -0800692 return aux->offload_requested;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700693}
Jakub Kicinskia3884572018-01-11 20:29:09 -0800694
695static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
696{
697 return unlikely(map->ops == &bpf_map_offload_ops);
698}
699
700struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
701void bpf_map_offload_map_free(struct bpf_map *map);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700702#else
703static inline int bpf_prog_offload_init(struct bpf_prog *prog,
704 union bpf_attr *attr)
705{
706 return -EOPNOTSUPP;
707}
708
709static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
710{
711 return false;
712}
Jakub Kicinskia3884572018-01-11 20:29:09 -0800713
714static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
715{
716 return false;
717}
718
719static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
720{
721 return ERR_PTR(-EOPNOTSUPP);
722}
723
724static inline void bpf_map_offload_map_free(struct bpf_map *map)
725{
726}
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700727#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
728
John Fastabend5f103c52018-01-03 17:57:56 -0800729#if defined(CONFIG_STREAM_PARSER) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_INET)
John Fastabend6bdc9c42017-08-16 15:02:32 -0700730struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key);
John Fastabend81110382018-05-14 10:00:17 -0700731struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key);
John Fastabend5a67da22017-09-08 14:00:49 -0700732int sock_map_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type);
Sean Youngfdb5c452018-06-19 00:04:24 +0100733int sockmap_get_from_fd(const union bpf_attr *attr, int type,
734 struct bpf_prog *prog);
John Fastabend6bdc9c42017-08-16 15:02:32 -0700735#else
736static inline struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
737{
738 return NULL;
739}
John Fastabend464bc0f2017-08-28 07:10:04 -0700740
John Fastabend81110382018-05-14 10:00:17 -0700741static inline struct sock *__sock_hash_lookup_elem(struct bpf_map *map,
742 void *key)
743{
744 return NULL;
745}
746
John Fastabend5a67da22017-09-08 14:00:49 -0700747static inline int sock_map_prog(struct bpf_map *map,
748 struct bpf_prog *prog,
749 u32 type)
John Fastabend464bc0f2017-08-28 07:10:04 -0700750{
751 return -EOPNOTSUPP;
752}
Sean Youngfdb5c452018-06-19 00:04:24 +0100753
754static inline int sockmap_get_from_fd(const union bpf_attr *attr, int type,
755 struct bpf_prog *prog)
756{
757 return -EINVAL;
758}
John Fastabend6bdc9c42017-08-16 15:02:32 -0700759#endif
760
Björn Töpelfbfc5042018-05-02 13:01:28 +0200761#if defined(CONFIG_XDP_SOCKETS)
762struct xdp_sock;
763struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key);
764int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
765 struct xdp_sock *xs);
766void __xsk_map_flush(struct bpf_map *map);
767#else
768struct xdp_sock;
769static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
770 u32 key)
771{
772 return NULL;
773}
774
775static inline int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
776 struct xdp_sock *xs)
777{
778 return -EOPNOTSUPP;
779}
780
781static inline void __xsk_map_flush(struct bpf_map *map)
782{
783}
784#endif
785
Martin KaFai Lau5dc4c4b2018-08-08 01:01:24 -0700786#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
787void bpf_sk_reuseport_detach(struct sock *sk);
788int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
789 void *value);
790int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
791 void *value, u64 map_flags);
792#else
793static inline void bpf_sk_reuseport_detach(struct sock *sk)
794{
795}
796
797#ifdef CONFIG_BPF_SYSCALL
798static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
799 void *key, void *value)
800{
801 return -EOPNOTSUPP;
802}
803
804static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
805 void *key, void *value,
806 u64 map_flags)
807{
808 return -EOPNOTSUPP;
809}
810#endif /* CONFIG_BPF_SYSCALL */
811#endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
812
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -0800813/* verifier prototypes for helper functions called from eBPF programs */
Daniel Borkmanna2c83ff2015-03-01 12:31:42 +0100814extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
815extern const struct bpf_func_proto bpf_map_update_elem_proto;
816extern const struct bpf_func_proto bpf_map_delete_elem_proto;
Alexei Starovoitovd0003ec2014-11-13 17:36:49 -0800817
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100818extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +0100819extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
Daniel Borkmann2d0e30c2016-10-21 12:46:33 +0200820extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700821extern const struct bpf_func_proto bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +0200822extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
Alexei Starovoitovffeedaf2015-06-12 19:39:12 -0700823extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
824extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
825extern const struct bpf_func_proto bpf_get_current_comm_proto;
Alexei Starovoitovd5a3b1f2016-02-17 19:58:58 -0800826extern const struct bpf_func_proto bpf_get_stackid_proto;
Yonghong Songc195651e2018-04-28 22:28:08 -0700827extern const struct bpf_func_proto bpf_get_stack_proto;
John Fastabend174a79f2017-08-15 22:32:47 -0700828extern const struct bpf_func_proto bpf_sock_map_update_proto;
John Fastabend81110382018-05-14 10:00:17 -0700829extern const struct bpf_func_proto bpf_sock_hash_update_proto;
Yonghong Songbf6fa2c2018-06-03 15:59:41 -0700830extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +0100831
Roman Gushchincd339432018-08-02 14:27:24 -0700832extern const struct bpf_func_proto bpf_get_local_storage_proto;
833
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200834/* Shared helpers among cBPF and eBPF. */
835void bpf_user_rnd_init_once(void);
836u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
837
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700838#endif /* _LINUX_BPF_H */