bpf.h 24 KB

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