bpf.h 21 KB

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