bpf.h 11 KB

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