bpf.h 23 KB

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