bpf.h 21 KB

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