bpf.h 11 KB

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