bpf.h 25 KB

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