bpf.h 27 KB

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