bpf.h 25 KB

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