~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/include/linux/bpf.h

Version: ~ [ linux-5.18 ] ~ [ linux-5.17.9 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.41 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.117 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.195 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.244 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.280 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.315 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 #ifndef _LINUX_BPF_H
  8 #define _LINUX_BPF_H 1
  9 
 10 #include <uapi/linux/bpf.h>
 11 
 12 #include <linux/workqueue.h>
 13 #include <linux/file.h>
 14 #include <linux/percpu.h>
 15 #include <linux/err.h>
 16 #include <linux/rbtree_latch.h>
 17 #include <linux/numa.h>
 18 #include <linux/wait.h>
 19 
 20 struct bpf_verifier_env;
 21 struct perf_event;
 22 struct bpf_prog;
 23 struct bpf_map;
 24 struct sock;
 25 struct seq_file;
 26 struct btf;
 27 struct btf_type;
 28 
 29 /* map is generic key/value storage optionally accesible by eBPF programs */
 30 struct bpf_map_ops {
 31         /* funcs callable from userspace (via syscall) */
 32         int (*map_alloc_check)(union bpf_attr *attr);
 33         struct bpf_map *(*map_alloc)(union bpf_attr *attr);
 34         void (*map_release)(struct bpf_map *map, struct file *map_file);
 35         void (*map_free)(struct bpf_map *map);
 36         int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
 37         void (*map_release_uref)(struct bpf_map *map);
 38         void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
 39 
 40         /* funcs callable from userspace and from eBPF programs */
 41         void *(*map_lookup_elem)(struct bpf_map *map, void *key);
 42         int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
 43         int (*map_delete_elem)(struct bpf_map *map, void *key);
 44         int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
 45         int (*map_pop_elem)(struct bpf_map *map, void *value);
 46         int (*map_peek_elem)(struct bpf_map *map, void *value);
 47 
 48         /* funcs called by prog_array and perf_event_array map */
 49         void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
 50                                 int fd);
 51         void (*map_fd_put_ptr)(void *ptr);
 52         u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
 53         u32 (*map_fd_sys_lookup_elem)(void *ptr);
 54         void (*map_seq_show_elem)(struct bpf_map *map, void *key,
 55                                   struct seq_file *m);
 56         int (*map_check_btf)(const struct bpf_map *map,
 57                              const struct btf *btf,
 58                              const struct btf_type *key_type,
 59                              const struct btf_type *value_type);
 60 };
 61 
 62 struct bpf_map {
 63         /* The first two cachelines with read-mostly members of which some
 64          * are also accessed in fast-path (e.g. ops, max_entries).
 65          */
 66         const struct bpf_map_ops *ops ____cacheline_aligned;
 67         struct bpf_map *inner_map_meta;
 68 #ifdef CONFIG_SECURITY
 69         void *security;
 70 #endif
 71         enum bpf_map_type map_type;
 72         u32 key_size;
 73         u32 value_size;
 74         u32 max_entries;
 75         u32 map_flags;
 76         u32 pages;
 77         u32 id;
 78         int numa_node;
 79         u32 btf_key_type_id;
 80         u32 btf_value_type_id;
 81         struct btf *btf;
 82         bool unpriv_array;
 83         /* 55 bytes hole */
 84 
 85         /* The 3rd and 4th cacheline with misc members to avoid false sharing
 86          * particularly with refcounting.
 87          */
 88         struct user_struct *user ____cacheline_aligned;
 89         atomic_t refcnt;
 90         atomic_t usercnt;
 91         struct work_struct work;
 92         char name[BPF_OBJ_NAME_LEN];
 93 };
 94 
 95 struct bpf_offload_dev;
 96 struct bpf_offloaded_map;
 97 
 98 struct bpf_map_dev_ops {
 99         int (*map_get_next_key)(struct bpf_offloaded_map *map,
100                                 void *key, void *next_key);
101         int (*map_lookup_elem)(struct bpf_offloaded_map *map,
102                                void *key, void *value);
103         int (*map_update_elem)(struct bpf_offloaded_map *map,
104                                void *key, void *value, u64 flags);
105         int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
106 };
107 
108 struct bpf_offloaded_map {
109         struct bpf_map map;
110         struct net_device *netdev;
111         const struct bpf_map_dev_ops *dev_ops;
112         void *dev_priv;
113         struct list_head offloads;
114 };
115 
116 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
117 {
118         return container_of(map, struct bpf_offloaded_map, map);
119 }
120 
121 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
122 {
123         return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
124 }
125 
126 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
127 {
128         return map->btf && map->ops->map_seq_show_elem;
129 }
130 
131 int map_check_no_btf(const struct bpf_map *map,
132                      const struct btf *btf,
133                      const struct btf_type *key_type,
134                      const struct btf_type *value_type);
135 
136 extern const struct bpf_map_ops bpf_map_offload_ops;
137 
138 /* function argument constraints */
139 enum bpf_arg_type {
140         ARG_DONTCARE = 0,       /* unused argument in helper function */
141 
142         /* the following constraints used to prototype
143          * bpf_map_lookup/update/delete_elem() functions
144          */
145         ARG_CONST_MAP_PTR,      /* const argument used as pointer to bpf_map */
146         ARG_PTR_TO_MAP_KEY,     /* pointer to stack used as map key */
147         ARG_PTR_TO_MAP_VALUE,   /* pointer to stack used as map value */
148         ARG_PTR_TO_UNINIT_MAP_VALUE,    /* pointer to valid memory used to store a map value */
149 
150         /* the following constraints used to prototype bpf_memcmp() and other
151          * functions that access data on eBPF program stack
152          */
153         ARG_PTR_TO_MEM,         /* pointer to valid memory (stack, packet, map value) */
154         ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */
155         ARG_PTR_TO_UNINIT_MEM,  /* pointer to memory does not need to be initialized,
156                                  * helper function must fill all bytes or clear
157                                  * them in error case.
158                                  */
159 
160         ARG_CONST_SIZE,         /* number of bytes accessed from memory */
161         ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
162 
163         ARG_PTR_TO_CTX,         /* pointer to context */
164         ARG_ANYTHING,           /* any (initialized) argument is ok */
165         ARG_PTR_TO_SOCKET,      /* pointer to bpf_sock */
166 };
167 
168 /* type of values returned from helper functions */
169 enum bpf_return_type {
170         RET_INTEGER,                    /* function returns integer */
171         RET_VOID,                       /* function doesn't return anything */
172         RET_PTR_TO_MAP_VALUE,           /* returns a pointer to map elem value */
173         RET_PTR_TO_MAP_VALUE_OR_NULL,   /* returns a pointer to map elem value or NULL */
174         RET_PTR_TO_SOCKET_OR_NULL,      /* returns a pointer to a socket or NULL */
175 };
176 
177 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
178  * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
179  * instructions after verifying
180  */
181 struct bpf_func_proto {
182         u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
183         bool gpl_only;
184         bool pkt_access;
185         enum bpf_return_type ret_type;
186         enum bpf_arg_type arg1_type;
187         enum bpf_arg_type arg2_type;
188         enum bpf_arg_type arg3_type;
189         enum bpf_arg_type arg4_type;
190         enum bpf_arg_type arg5_type;
191 };
192 
193 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
194  * the first argument to eBPF programs.
195  * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
196  */
197 struct bpf_context;
198 
199 enum bpf_access_type {
200         BPF_READ = 1,
201         BPF_WRITE = 2
202 };
203 
204 /* types of values stored in eBPF registers */
205 /* Pointer types represent:
206  * pointer
207  * pointer + imm
208  * pointer + (u16) var
209  * pointer + (u16) var + imm
210  * if (range > 0) then [ptr, ptr + range - off) is safe to access
211  * if (id > 0) means that some 'var' was added
212  * if (off > 0) means that 'imm' was added
213  */
214 enum bpf_reg_type {
215         NOT_INIT = 0,            /* nothing was written into register */
216         SCALAR_VALUE,            /* reg doesn't contain a valid pointer */
217         PTR_TO_CTX,              /* reg points to bpf_context */
218         CONST_PTR_TO_MAP,        /* reg points to struct bpf_map */
219         PTR_TO_MAP_VALUE,        /* reg points to map element value */
220         PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
221         PTR_TO_STACK,            /* reg == frame_pointer + offset */
222         PTR_TO_PACKET_META,      /* skb->data - meta_len */
223         PTR_TO_PACKET,           /* reg points to skb->data */
224         PTR_TO_PACKET_END,       /* skb->data + headlen */
225         PTR_TO_FLOW_KEYS,        /* reg points to bpf_flow_keys */
226         PTR_TO_SOCKET,           /* reg points to struct bpf_sock */
227         PTR_TO_SOCKET_OR_NULL,   /* reg points to struct bpf_sock or NULL */
228 };
229 
230 /* The information passed from prog-specific *_is_valid_access
231  * back to the verifier.
232  */
233 struct bpf_insn_access_aux {
234         enum bpf_reg_type reg_type;
235         int ctx_field_size;
236 };
237 
238 static inline void
239 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
240 {
241         aux->ctx_field_size = size;
242 }
243 
244 struct bpf_prog_ops {
245         int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
246                         union bpf_attr __user *uattr);
247 };
248 
249 struct bpf_verifier_ops {
250         /* return eBPF function prototype for verification */
251         const struct bpf_func_proto *
252         (*get_func_proto)(enum bpf_func_id func_id,
253                           const struct bpf_prog *prog);
254 
255         /* return true if 'size' wide access at offset 'off' within bpf_context
256          * with 'type' (read or write) is allowed
257          */
258         bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
259                                 const struct bpf_prog *prog,
260                                 struct bpf_insn_access_aux *info);
261         int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
262                             const struct bpf_prog *prog);
263         int (*gen_ld_abs)(const struct bpf_insn *orig,
264                           struct bpf_insn *insn_buf);
265         u32 (*convert_ctx_access)(enum bpf_access_type type,
266                                   const struct bpf_insn *src,
267                                   struct bpf_insn *dst,
268                                   struct bpf_prog *prog, u32 *target_size);
269 };
270 
271 struct bpf_prog_offload_ops {
272         int (*insn_hook)(struct bpf_verifier_env *env,
273                          int insn_idx, int prev_insn_idx);
274         int (*finalize)(struct bpf_verifier_env *env);
275         int (*prepare)(struct bpf_prog *prog);
276         int (*translate)(struct bpf_prog *prog);
277         void (*destroy)(struct bpf_prog *prog);
278 };
279 
280 struct bpf_prog_offload {
281         struct bpf_prog         *prog;
282         struct net_device       *netdev;
283         struct bpf_offload_dev  *offdev;
284         void                    *dev_priv;
285         struct list_head        offloads;
286         bool                    dev_state;
287         void                    *jited_image;
288         u32                     jited_len;
289 };
290 
291 enum bpf_cgroup_storage_type {
292         BPF_CGROUP_STORAGE_SHARED,
293         BPF_CGROUP_STORAGE_PERCPU,
294         __BPF_CGROUP_STORAGE_MAX
295 };
296 
297 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
298 
299 struct bpf_prog_aux {
300         atomic_t refcnt;
301         u32 used_map_cnt;
302         u32 max_ctx_offset;
303         u32 max_pkt_offset;
304         u32 stack_depth;
305         u32 id;
306         u32 func_cnt; /* used by non-func prog as the number of func progs */
307         u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
308         bool offload_requested;
309         struct bpf_prog **func;
310         void *jit_data; /* JIT specific data. arch dependent */
311         struct latch_tree_node ksym_tnode;
312         struct list_head ksym_lnode;
313         const struct bpf_prog_ops *ops;
314         struct bpf_map **used_maps;
315         struct bpf_prog *prog;
316         struct user_struct *user;
317         u64 load_time; /* ns since boottime */
318         struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
319         char name[BPF_OBJ_NAME_LEN];
320 #ifdef CONFIG_SECURITY
321         void *security;
322 #endif
323         struct bpf_prog_offload *offload;
324         struct btf *btf;
325         struct bpf_func_info *func_info;
326         /* bpf_line_info loaded from userspace.  linfo->insn_off
327          * has the xlated insn offset.
328          * Both the main and sub prog share the same linfo.
329          * The subprog can access its first linfo by
330          * using the linfo_idx.
331          */
332         struct bpf_line_info *linfo;
333         /* jited_linfo is the jited addr of the linfo.  It has a
334          * one to one mapping to linfo:
335          * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
336          * Both the main and sub prog share the same jited_linfo.
337          * The subprog can access its first jited_linfo by
338          * using the linfo_idx.
339          */
340         void **jited_linfo;
341         u32 func_info_cnt;
342         u32 nr_linfo;
343         /* subprog can use linfo_idx to access its first linfo and
344          * jited_linfo.
345          * main prog always has linfo_idx == 0
346          */
347         u32 linfo_idx;
348         union {
349                 struct work_struct work;
350                 struct rcu_head rcu;
351         };
352 };
353 
354 struct bpf_array {
355         struct bpf_map map;
356         u32 elem_size;
357         u32 index_mask;
358         /* 'ownership' of prog_array is claimed by the first program that
359          * is going to use this map or by the first program which FD is stored
360          * in the map to make sure that all callers and callees have the same
361          * prog_type and JITed flag
362          */
363         enum bpf_prog_type owner_prog_type;
364         bool owner_jited;
365         union {
366                 char value[0] __aligned(8);
367                 void *ptrs[0] __aligned(8);
368                 void __percpu *pptrs[0] __aligned(8);
369         };
370 };
371 
372 #define MAX_TAIL_CALL_CNT 32
373 
374 struct bpf_event_entry {
375         struct perf_event *event;
376         struct file *perf_file;
377         struct file *map_file;
378         struct rcu_head rcu;
379 };
380 
381 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
382 int bpf_prog_calc_tag(struct bpf_prog *fp);
383 
384 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
385 
386 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
387                                         unsigned long off, unsigned long len);
388 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
389                                         const struct bpf_insn *src,
390                                         struct bpf_insn *dst,
391                                         struct bpf_prog *prog,
392                                         u32 *target_size);
393 
394 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
395                      void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
396 
397 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
398                           union bpf_attr __user *uattr);
399 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
400                           union bpf_attr __user *uattr);
401 
402 /* an array of programs to be executed under rcu_lock.
403  *
404  * Typical usage:
405  * ret = BPF_PROG_RUN_ARRAY(&bpf_prog_array, ctx, BPF_PROG_RUN);
406  *
407  * the structure returned by bpf_prog_array_alloc() should be populated
408  * with program pointers and the last pointer must be NULL.
409  * The user has to keep refcnt on the program and make sure the program
410  * is removed from the array before bpf_prog_put().
411  * The 'struct bpf_prog_array *' should only be replaced with xchg()
412  * since other cpus are walking the array of pointers in parallel.
413  */
414 struct bpf_prog_array_item {
415         struct bpf_prog *prog;
416         struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
417 };
418 
419 struct bpf_prog_array {
420         struct rcu_head rcu;
421         struct bpf_prog_array_item items[0];
422 };
423 
424 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
425 void bpf_prog_array_free(struct bpf_prog_array __rcu *progs);
426 int bpf_prog_array_length(struct bpf_prog_array __rcu *progs);
427 int bpf_prog_array_copy_to_user(struct bpf_prog_array __rcu *progs,
428                                 __u32 __user *prog_ids, u32 cnt);
429 
430 void bpf_prog_array_delete_safe(struct bpf_prog_array __rcu *progs,
431                                 struct bpf_prog *old_prog);
432 int bpf_prog_array_copy_info(struct bpf_prog_array __rcu *array,
433                              u32 *prog_ids, u32 request_cnt,
434                              u32 *prog_cnt);
435 int bpf_prog_array_copy(struct bpf_prog_array __rcu *old_array,
436                         struct bpf_prog *exclude_prog,
437                         struct bpf_prog *include_prog,
438                         struct bpf_prog_array **new_array);
439 
440 #define __BPF_PROG_RUN_ARRAY(array, ctx, func, check_non_null)  \
441         ({                                              \
442                 struct bpf_prog_array_item *_item;      \
443                 struct bpf_prog *_prog;                 \
444                 struct bpf_prog_array *_array;          \
445                 u32 _ret = 1;                           \
446                 preempt_disable();                      \
447                 rcu_read_lock();                        \
448                 _array = rcu_dereference(array);        \
449                 if (unlikely(check_non_null && !_array))\
450                         goto _out;                      \
451                 _item = &_array->items[0];              \
452                 while ((_prog = READ_ONCE(_item->prog))) {              \
453                         bpf_cgroup_storage_set(_item->cgroup_storage);  \
454                         _ret &= func(_prog, ctx);       \
455                         _item++;                        \
456                 }                                       \
457 _out:                                                   \
458                 rcu_read_unlock();                      \
459                 preempt_enable();                       \
460                 _ret;                                   \
461          })
462 
463 #define BPF_PROG_RUN_ARRAY(array, ctx, func)            \
464         __BPF_PROG_RUN_ARRAY(array, ctx, func, false)
465 
466 #define BPF_PROG_RUN_ARRAY_CHECK(array, ctx, func)      \
467         __BPF_PROG_RUN_ARRAY(array, ctx, func, true)
468 
469 #ifdef CONFIG_BPF_SYSCALL
470 DECLARE_PER_CPU(int, bpf_prog_active);
471 
472 extern const struct file_operations bpf_map_fops;
473 extern const struct file_operations bpf_prog_fops;
474 
475 #define BPF_PROG_TYPE(_id, _name) \
476         extern const struct bpf_prog_ops _name ## _prog_ops; \
477         extern const struct bpf_verifier_ops _name ## _verifier_ops;
478 #define BPF_MAP_TYPE(_id, _ops) \
479         extern const struct bpf_map_ops _ops;
480 #include <linux/bpf_types.h>
481 #undef BPF_PROG_TYPE
482 #undef BPF_MAP_TYPE
483 
484 extern const struct bpf_prog_ops bpf_offload_prog_ops;
485 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
486 extern const struct bpf_verifier_ops xdp_analyzer_ops;
487 
488 struct bpf_prog *bpf_prog_get(u32 ufd);
489 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
490                                        bool attach_drv);
491 struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog, int i);
492 void bpf_prog_sub(struct bpf_prog *prog, int i);
493 struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog);
494 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
495 void bpf_prog_put(struct bpf_prog *prog);
496 int __bpf_prog_charge(struct user_struct *user, u32 pages);
497 void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
498 
499 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock);
500 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock);
501 
502 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
503 struct bpf_map *__bpf_map_get(struct fd f);
504 struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
505 void bpf_map_put_with_uref(struct bpf_map *map);
506 void bpf_map_put(struct bpf_map *map);
507 int bpf_map_precharge_memlock(u32 pages);
508 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
509 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
510 void *bpf_map_area_alloc(size_t size, int numa_node);
511 void bpf_map_area_free(void *base);
512 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
513 
514 extern int sysctl_unprivileged_bpf_disabled;
515 
516 int bpf_map_new_fd(struct bpf_map *map, int flags);
517 int bpf_prog_new_fd(struct bpf_prog *prog);
518 
519 int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
520 int bpf_obj_get_user(const char __user *pathname, int flags);
521 
522 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
523 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
524 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
525                            u64 flags);
526 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
527                             u64 flags);
528 
529 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
530 
531 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
532                                  void *key, void *value, u64 map_flags);
533 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
534 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
535                                 void *key, void *value, u64 map_flags);
536 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
537 
538 int bpf_get_file_flag(int flags);
539 int bpf_check_uarg_tail_zero(void __user *uaddr, size_t expected_size,
540                              size_t actual_size);
541 
542 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
543  * forced to use 'long' read/writes to try to atomically copy long counters.
544  * Best-effort only.  No barriers here, since it _will_ race with concurrent
545  * updates from BPF programs. Called from bpf syscall and mostly used with
546  * size 8 or 16 bytes, so ask compiler to inline it.
547  */
548 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
549 {
550         const long *lsrc = src;
551         long *ldst = dst;
552 
553         size /= sizeof(long);
554         while (size--)
555                 *ldst++ = *lsrc++;
556 }
557 
558 /* verify correctness of eBPF program */
559 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr,
560               union bpf_attr __user *uattr);
561 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
562 
563 /* Map specifics */
564 struct xdp_buff;
565 struct sk_buff;
566 
567 struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
568 void __dev_map_insert_ctx(struct bpf_map *map, u32 index);
569 void __dev_map_flush(struct bpf_map *map);
570 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
571                     struct net_device *dev_rx);
572 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
573                              struct bpf_prog *xdp_prog);
574 
575 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key);
576 void __cpu_map_insert_ctx(struct bpf_map *map, u32 index);
577 void __cpu_map_flush(struct bpf_map *map);
578 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp,
579                     struct net_device *dev_rx);
580 
581 /* Return map's numa specified by userspace */
582 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
583 {
584         return (attr->map_flags & BPF_F_NUMA_NODE) ?
585                 attr->numa_node : NUMA_NO_NODE;
586 }
587 
588 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
589 int array_map_alloc_check(union bpf_attr *attr);
590 
591 #else /* !CONFIG_BPF_SYSCALL */
592 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
593 {
594         return ERR_PTR(-EOPNOTSUPP);
595 }
596 
597 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
598                                                      enum bpf_prog_type type,
599                                                      bool attach_drv)
600 {
601         return ERR_PTR(-EOPNOTSUPP);
602 }
603 
604 static inline struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog,
605                                                           int i)
606 {
607         return ERR_PTR(-EOPNOTSUPP);
608 }
609 
610 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
611 {
612 }
613 
614 static inline void bpf_prog_put(struct bpf_prog *prog)
615 {
616 }
617 
618 static inline struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog)
619 {
620         return ERR_PTR(-EOPNOTSUPP);
621 }
622 
623 static inline struct bpf_prog *__must_check
624 bpf_prog_inc_not_zero(struct bpf_prog *prog)
625 {
626         return ERR_PTR(-EOPNOTSUPP);
627 }
628 
629 static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
630 {
631         return 0;
632 }
633 
634 static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
635 {
636 }
637 
638 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
639 {
640         return -EOPNOTSUPP;
641 }
642 
643 static inline struct net_device  *__dev_map_lookup_elem(struct bpf_map *map,
644                                                        u32 key)
645 {
646         return NULL;
647 }
648 
649 static inline void __dev_map_insert_ctx(struct bpf_map *map, u32 index)
650 {
651 }
652 
653 static inline void __dev_map_flush(struct bpf_map *map)
654 {
655 }
656 
657 struct xdp_buff;
658 struct bpf_dtab_netdev;
659 
660 static inline
661 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
662                     struct net_device *dev_rx)
663 {
664         return 0;
665 }
666 
667 struct sk_buff;
668 
669 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
670                                            struct sk_buff *skb,
671                                            struct bpf_prog *xdp_prog)
672 {
673         return 0;
674 }
675 
676 static inline
677 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
678 {
679         return NULL;
680 }
681 
682 static inline void __cpu_map_insert_ctx(struct bpf_map *map, u32 index)
683 {
684 }
685 
686 static inline void __cpu_map_flush(struct bpf_map *map)
687 {
688 }
689 
690 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
691                                   struct xdp_buff *xdp,
692                                   struct net_device *dev_rx)
693 {
694         return 0;
695 }
696 
697 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
698                                 enum bpf_prog_type type)
699 {
700         return ERR_PTR(-EOPNOTSUPP);
701 }
702 #endif /* CONFIG_BPF_SYSCALL */
703 
704 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
705                                                  enum bpf_prog_type type)
706 {
707         return bpf_prog_get_type_dev(ufd, type, false);
708 }
709 
710 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
711 
712 int bpf_prog_offload_compile(struct bpf_prog *prog);
713 void bpf_prog_offload_destroy(struct bpf_prog *prog);
714 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
715                                struct bpf_prog *prog);
716 
717 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
718 
719 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
720 int bpf_map_offload_update_elem(struct bpf_map *map,
721                                 void *key, void *value, u64 flags);
722 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
723 int bpf_map_offload_get_next_key(struct bpf_map *map,
724                                  void *key, void *next_key);
725 
726 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
727 
728 struct bpf_offload_dev *
729 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops);
730 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
731 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
732                                     struct net_device *netdev);
733 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
734                                        struct net_device *netdev);
735 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
736 
737 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
738 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
739 
740 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
741 {
742         return aux->offload_requested;
743 }
744 
745 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
746 {
747         return unlikely(map->ops == &bpf_map_offload_ops);
748 }
749 
750 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
751 void bpf_map_offload_map_free(struct bpf_map *map);
752 #else
753 static inline int bpf_prog_offload_init(struct bpf_prog *prog,
754                                         union bpf_attr *attr)
755 {
756         return -EOPNOTSUPP;
757 }
758 
759 static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
760 {
761         return false;
762 }
763 
764 static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
765 {
766         return false;
767 }
768 
769 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
770 {
771         return ERR_PTR(-EOPNOTSUPP);
772 }
773 
774 static inline void bpf_map_offload_map_free(struct bpf_map *map)
775 {
776 }
777 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
778 
779 #if defined(CONFIG_BPF_STREAM_PARSER)
780 int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
781 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
782 #else
783 static inline int sock_map_prog_update(struct bpf_map *map,
784                                        struct bpf_prog *prog, u32 which)
785 {
786         return -EOPNOTSUPP;
787 }
788 
789 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
790                                        struct bpf_prog *prog)
791 {
792         return -EINVAL;
793 }
794 #endif
795 
796 #if defined(CONFIG_XDP_SOCKETS)
797 struct xdp_sock;
798 struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key);
799 int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
800                        struct xdp_sock *xs);
801 void __xsk_map_flush(struct bpf_map *map);
802 #else
803 struct xdp_sock;
804 static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
805                                                      u32 key)
806 {
807         return NULL;
808 }
809 
810 static inline int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp,
811                                      struct xdp_sock *xs)
812 {
813         return -EOPNOTSUPP;
814 }
815 
816 static inline void __xsk_map_flush(struct bpf_map *map)
817 {
818 }
819 #endif
820 
821 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
822 void bpf_sk_reuseport_detach(struct sock *sk);
823 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
824                                        void *value);
825 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
826                                        void *value, u64 map_flags);
827 #else
828 static inline void bpf_sk_reuseport_detach(struct sock *sk)
829 {
830 }
831 
832 #ifdef CONFIG_BPF_SYSCALL
833 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
834                                                      void *key, void *value)
835 {
836         return -EOPNOTSUPP;
837 }
838 
839 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
840                                                      void *key, void *value,
841                                                      u64 map_flags)
842 {
843         return -EOPNOTSUPP;
844 }
845 #endif /* CONFIG_BPF_SYSCALL */
846 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
847 
848 /* verifier prototypes for helper functions called from eBPF programs */
849 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
850 extern const struct bpf_func_proto bpf_map_update_elem_proto;
851 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
852 extern const struct bpf_func_proto bpf_map_push_elem_proto;
853 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
854 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
855 
856 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
857 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
858 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
859 extern const struct bpf_func_proto bpf_tail_call_proto;
860 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
861 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
862 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
863 extern const struct bpf_func_proto bpf_get_current_comm_proto;
864 extern const struct bpf_func_proto bpf_get_stackid_proto;
865 extern const struct bpf_func_proto bpf_get_stack_proto;
866 extern const struct bpf_func_proto bpf_sock_map_update_proto;
867 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
868 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
869 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
870 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
871 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
872 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
873 
874 extern const struct bpf_func_proto bpf_get_local_storage_proto;
875 
876 /* Shared helpers among cBPF and eBPF. */
877 void bpf_user_rnd_init_once(void);
878 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
879 
880 #if defined(CONFIG_NET)
881 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
882                               struct bpf_insn_access_aux *info);
883 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
884                                 const struct bpf_insn *si,
885                                 struct bpf_insn *insn_buf,
886                                 struct bpf_prog *prog,
887                                 u32 *target_size);
888 #else
889 static inline bool bpf_sock_is_valid_access(int off, int size,
890                                             enum bpf_access_type type,
891                                             struct bpf_insn_access_aux *info)
892 {
893         return false;
894 }
895 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
896                                               const struct bpf_insn *si,
897                                               struct bpf_insn *insn_buf,
898                                               struct bpf_prog *prog,
899                                               u32 *target_size)
900 {
901         return 0;
902 }
903 #endif
904 
905 #endif /* _LINUX_BPF_H */
906 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp