bpf.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. struct perf_event;
  17. struct bpf_prog;
  18. struct bpf_map;
  19. /* map is generic key/value storage optionally accesible by eBPF programs */
  20. struct bpf_map_ops {
  21. /* funcs callable from userspace (via syscall) */
  22. struct bpf_map *(*map_alloc)(union bpf_attr *attr);
  23. void (*map_release)(struct bpf_map *map, struct file *map_file);
  24. void (*map_free)(struct bpf_map *map);
  25. int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
  26. /* funcs callable from userspace and from eBPF programs */
  27. void *(*map_lookup_elem)(struct bpf_map *map, void *key);
  28. int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
  29. int (*map_delete_elem)(struct bpf_map *map, void *key);
  30. /* funcs called by prog_array and perf_event_array map */
  31. void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
  32. int fd);
  33. void (*map_fd_put_ptr)(void *ptr);
  34. u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
  35. u32 (*map_fd_sys_lookup_elem)(void *ptr);
  36. };
  37. struct bpf_map {
  38. atomic_t refcnt;
  39. enum bpf_map_type map_type;
  40. u32 key_size;
  41. u32 value_size;
  42. u32 max_entries;
  43. u32 map_flags;
  44. u32 pages;
  45. u32 id;
  46. int numa_node;
  47. struct user_struct *user;
  48. const struct bpf_map_ops *ops;
  49. struct work_struct work;
  50. atomic_t usercnt;
  51. struct bpf_map *inner_map_meta;
  52. u8 name[BPF_OBJ_NAME_LEN];
  53. };
  54. /* function argument constraints */
  55. enum bpf_arg_type {
  56. ARG_DONTCARE = 0, /* unused argument in helper function */
  57. /* the following constraints used to prototype
  58. * bpf_map_lookup/update/delete_elem() functions
  59. */
  60. ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
  61. ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
  62. ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
  63. /* the following constraints used to prototype bpf_memcmp() and other
  64. * functions that access data on eBPF program stack
  65. */
  66. ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
  67. ARG_PTR_TO_UNINIT_MEM, /* pointer to memory does not need to be initialized,
  68. * helper function must fill all bytes or clear
  69. * them in error case.
  70. */
  71. ARG_CONST_SIZE, /* number of bytes accessed from memory */
  72. ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
  73. ARG_PTR_TO_CTX, /* pointer to context */
  74. ARG_ANYTHING, /* any (initialized) argument is ok */
  75. };
  76. /* type of values returned from helper functions */
  77. enum bpf_return_type {
  78. RET_INTEGER, /* function returns integer */
  79. RET_VOID, /* function doesn't return anything */
  80. RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
  81. };
  82. /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
  83. * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
  84. * instructions after verifying
  85. */
  86. struct bpf_func_proto {
  87. u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  88. bool gpl_only;
  89. bool pkt_access;
  90. enum bpf_return_type ret_type;
  91. enum bpf_arg_type arg1_type;
  92. enum bpf_arg_type arg2_type;
  93. enum bpf_arg_type arg3_type;
  94. enum bpf_arg_type arg4_type;
  95. enum bpf_arg_type arg5_type;
  96. };
  97. /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
  98. * the first argument to eBPF programs.
  99. * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
  100. */
  101. struct bpf_context;
  102. enum bpf_access_type {
  103. BPF_READ = 1,
  104. BPF_WRITE = 2
  105. };
  106. /* types of values stored in eBPF registers */
  107. /* Pointer types represent:
  108. * pointer
  109. * pointer + imm
  110. * pointer + (u16) var
  111. * pointer + (u16) var + imm
  112. * if (range > 0) then [ptr, ptr + range - off) is safe to access
  113. * if (id > 0) means that some 'var' was added
  114. * if (off > 0) means that 'imm' was added
  115. */
  116. enum bpf_reg_type {
  117. NOT_INIT = 0, /* nothing was written into register */
  118. SCALAR_VALUE, /* reg doesn't contain a valid pointer */
  119. PTR_TO_CTX, /* reg points to bpf_context */
  120. CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
  121. PTR_TO_MAP_VALUE, /* reg points to map element value */
  122. PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */
  123. PTR_TO_STACK, /* reg == frame_pointer + offset */
  124. PTR_TO_PACKET_META, /* skb->data - meta_len */
  125. PTR_TO_PACKET, /* reg points to skb->data */
  126. PTR_TO_PACKET_END, /* skb->data + headlen */
  127. };
  128. /* The information passed from prog-specific *_is_valid_access
  129. * back to the verifier.
  130. */
  131. struct bpf_insn_access_aux {
  132. enum bpf_reg_type reg_type;
  133. int ctx_field_size;
  134. };
  135. static inline void
  136. bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
  137. {
  138. aux->ctx_field_size = size;
  139. }
  140. struct bpf_verifier_ops {
  141. /* return eBPF function prototype for verification */
  142. const struct bpf_func_proto *(*get_func_proto)(enum bpf_func_id func_id);
  143. /* return true if 'size' wide access at offset 'off' within bpf_context
  144. * with 'type' (read or write) is allowed
  145. */
  146. bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
  147. struct bpf_insn_access_aux *info);
  148. int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
  149. const struct bpf_prog *prog);
  150. u32 (*convert_ctx_access)(enum bpf_access_type type,
  151. const struct bpf_insn *src,
  152. struct bpf_insn *dst,
  153. struct bpf_prog *prog, u32 *target_size);
  154. int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
  155. union bpf_attr __user *uattr);
  156. };
  157. struct bpf_prog_aux {
  158. atomic_t refcnt;
  159. u32 used_map_cnt;
  160. u32 max_ctx_offset;
  161. u32 stack_depth;
  162. u32 id;
  163. struct latch_tree_node ksym_tnode;
  164. struct list_head ksym_lnode;
  165. const struct bpf_verifier_ops *ops;
  166. struct bpf_map **used_maps;
  167. struct bpf_prog *prog;
  168. struct user_struct *user;
  169. u64 load_time; /* ns since boottime */
  170. u8 name[BPF_OBJ_NAME_LEN];
  171. union {
  172. struct work_struct work;
  173. struct rcu_head rcu;
  174. };
  175. };
  176. struct bpf_array {
  177. struct bpf_map map;
  178. u32 elem_size;
  179. /* 'ownership' of prog_array is claimed by the first program that
  180. * is going to use this map or by the first program which FD is stored
  181. * in the map to make sure that all callers and callees have the same
  182. * prog_type and JITed flag
  183. */
  184. enum bpf_prog_type owner_prog_type;
  185. bool owner_jited;
  186. union {
  187. char value[0] __aligned(8);
  188. void *ptrs[0] __aligned(8);
  189. void __percpu *pptrs[0] __aligned(8);
  190. };
  191. };
  192. #define MAX_TAIL_CALL_CNT 32
  193. struct bpf_event_entry {
  194. struct perf_event *event;
  195. struct file *perf_file;
  196. struct file *map_file;
  197. struct rcu_head rcu;
  198. };
  199. u64 bpf_tail_call(u64 ctx, u64 r2, u64 index, u64 r4, u64 r5);
  200. u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  201. bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
  202. int bpf_prog_calc_tag(struct bpf_prog *fp);
  203. const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
  204. typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
  205. unsigned long off, unsigned long len);
  206. u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
  207. void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
  208. int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
  209. union bpf_attr __user *uattr);
  210. int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
  211. union bpf_attr __user *uattr);
  212. #ifdef CONFIG_BPF_SYSCALL
  213. DECLARE_PER_CPU(int, bpf_prog_active);
  214. #define BPF_PROG_TYPE(_id, _ops) \
  215. extern const struct bpf_verifier_ops _ops;
  216. #define BPF_MAP_TYPE(_id, _ops) \
  217. extern const struct bpf_map_ops _ops;
  218. #include <linux/bpf_types.h>
  219. #undef BPF_PROG_TYPE
  220. #undef BPF_MAP_TYPE
  221. struct bpf_prog *bpf_prog_get(u32 ufd);
  222. struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type);
  223. struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog, int i);
  224. void bpf_prog_sub(struct bpf_prog *prog, int i);
  225. struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog);
  226. struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
  227. void bpf_prog_put(struct bpf_prog *prog);
  228. int __bpf_prog_charge(struct user_struct *user, u32 pages);
  229. void __bpf_prog_uncharge(struct user_struct *user, u32 pages);
  230. struct bpf_map *bpf_map_get_with_uref(u32 ufd);
  231. struct bpf_map *__bpf_map_get(struct fd f);
  232. struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref);
  233. void bpf_map_put_with_uref(struct bpf_map *map);
  234. void bpf_map_put(struct bpf_map *map);
  235. int bpf_map_precharge_memlock(u32 pages);
  236. void *bpf_map_area_alloc(size_t size, int numa_node);
  237. void bpf_map_area_free(void *base);
  238. extern int sysctl_unprivileged_bpf_disabled;
  239. int bpf_map_new_fd(struct bpf_map *map);
  240. int bpf_prog_new_fd(struct bpf_prog *prog);
  241. int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
  242. int bpf_obj_get_user(const char __user *pathname);
  243. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
  244. int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
  245. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  246. u64 flags);
  247. int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
  248. u64 flags);
  249. int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
  250. int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
  251. void *key, void *value, u64 map_flags);
  252. int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
  253. void bpf_fd_array_map_clear(struct bpf_map *map);
  254. int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
  255. void *key, void *value, u64 map_flags);
  256. int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
  257. /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
  258. * forced to use 'long' read/writes to try to atomically copy long counters.
  259. * Best-effort only. No barriers here, since it _will_ race with concurrent
  260. * updates from BPF programs. Called from bpf syscall and mostly used with
  261. * size 8 or 16 bytes, so ask compiler to inline it.
  262. */
  263. static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
  264. {
  265. const long *lsrc = src;
  266. long *ldst = dst;
  267. size /= sizeof(long);
  268. while (size--)
  269. *ldst++ = *lsrc++;
  270. }
  271. /* verify correctness of eBPF program */
  272. int bpf_check(struct bpf_prog **fp, union bpf_attr *attr);
  273. /* Map specifics */
  274. struct net_device *__dev_map_lookup_elem(struct bpf_map *map, u32 key);
  275. void __dev_map_insert_ctx(struct bpf_map *map, u32 index);
  276. void __dev_map_flush(struct bpf_map *map);
  277. /* Return map's numa specified by userspace */
  278. static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
  279. {
  280. return (attr->map_flags & BPF_F_NUMA_NODE) ?
  281. attr->numa_node : NUMA_NO_NODE;
  282. }
  283. #else
  284. static inline struct bpf_prog *bpf_prog_get(u32 ufd)
  285. {
  286. return ERR_PTR(-EOPNOTSUPP);
  287. }
  288. static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
  289. enum bpf_prog_type type)
  290. {
  291. return ERR_PTR(-EOPNOTSUPP);
  292. }
  293. static inline struct bpf_prog * __must_check bpf_prog_add(struct bpf_prog *prog,
  294. int i)
  295. {
  296. return ERR_PTR(-EOPNOTSUPP);
  297. }
  298. static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
  299. {
  300. }
  301. static inline void bpf_prog_put(struct bpf_prog *prog)
  302. {
  303. }
  304. static inline struct bpf_prog * __must_check bpf_prog_inc(struct bpf_prog *prog)
  305. {
  306. return ERR_PTR(-EOPNOTSUPP);
  307. }
  308. static inline struct bpf_prog *__must_check
  309. bpf_prog_inc_not_zero(struct bpf_prog *prog)
  310. {
  311. return ERR_PTR(-EOPNOTSUPP);
  312. }
  313. static inline int __bpf_prog_charge(struct user_struct *user, u32 pages)
  314. {
  315. return 0;
  316. }
  317. static inline void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
  318. {
  319. }
  320. static inline struct net_device *__dev_map_lookup_elem(struct bpf_map *map,
  321. u32 key)
  322. {
  323. return NULL;
  324. }
  325. static inline void __dev_map_insert_ctx(struct bpf_map *map, u32 index)
  326. {
  327. }
  328. static inline void __dev_map_flush(struct bpf_map *map)
  329. {
  330. }
  331. #endif /* CONFIG_BPF_SYSCALL */
  332. #if defined(CONFIG_STREAM_PARSER) && defined(CONFIG_BPF_SYSCALL)
  333. struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key);
  334. int sock_map_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type);
  335. #else
  336. static inline struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
  337. {
  338. return NULL;
  339. }
  340. static inline int sock_map_prog(struct bpf_map *map,
  341. struct bpf_prog *prog,
  342. u32 type)
  343. {
  344. return -EOPNOTSUPP;
  345. }
  346. #endif
  347. /* verifier prototypes for helper functions called from eBPF programs */
  348. extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
  349. extern const struct bpf_func_proto bpf_map_update_elem_proto;
  350. extern const struct bpf_func_proto bpf_map_delete_elem_proto;
  351. extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
  352. extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
  353. extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
  354. extern const struct bpf_func_proto bpf_tail_call_proto;
  355. extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
  356. extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
  357. extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
  358. extern const struct bpf_func_proto bpf_get_current_comm_proto;
  359. extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
  360. extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
  361. extern const struct bpf_func_proto bpf_get_stackid_proto;
  362. extern const struct bpf_func_proto bpf_sock_map_update_proto;
  363. /* Shared helpers among cBPF and eBPF. */
  364. void bpf_user_rnd_init_once(void);
  365. u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  366. #endif /* _LINUX_BPF_H */