bpf.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. struct bpf_map;
  14. /* map is generic key/value storage optionally accesible by eBPF programs */
  15. struct bpf_map_ops {
  16. /* funcs callable from userspace (via syscall) */
  17. struct bpf_map *(*map_alloc)(union bpf_attr *attr);
  18. void (*map_free)(struct bpf_map *);
  19. int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
  20. /* funcs callable from userspace and from eBPF programs */
  21. void *(*map_lookup_elem)(struct bpf_map *map, void *key);
  22. int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
  23. int (*map_delete_elem)(struct bpf_map *map, void *key);
  24. /* funcs called by prog_array and perf_event_array map */
  25. void *(*map_fd_get_ptr) (struct bpf_map *map, int fd);
  26. void (*map_fd_put_ptr) (void *ptr);
  27. };
  28. struct bpf_map {
  29. atomic_t refcnt;
  30. enum bpf_map_type map_type;
  31. u32 key_size;
  32. u32 value_size;
  33. u32 max_entries;
  34. u32 map_flags;
  35. u32 pages;
  36. struct user_struct *user;
  37. const struct bpf_map_ops *ops;
  38. struct work_struct work;
  39. atomic_t usercnt;
  40. };
  41. struct bpf_map_type_list {
  42. struct list_head list_node;
  43. const struct bpf_map_ops *ops;
  44. enum bpf_map_type type;
  45. };
  46. /* function argument constraints */
  47. enum bpf_arg_type {
  48. ARG_DONTCARE = 0, /* unused argument in helper function */
  49. /* the following constraints used to prototype
  50. * bpf_map_lookup/update/delete_elem() functions
  51. */
  52. ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
  53. ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
  54. ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
  55. /* the following constraints used to prototype bpf_memcmp() and other
  56. * functions that access data on eBPF program stack
  57. */
  58. ARG_PTR_TO_STACK, /* any pointer to eBPF program stack */
  59. ARG_CONST_STACK_SIZE, /* number of bytes accessed from stack */
  60. ARG_CONST_STACK_SIZE_OR_ZERO, /* number of bytes accessed from stack or 0 */
  61. ARG_PTR_TO_CTX, /* pointer to context */
  62. ARG_ANYTHING, /* any (initialized) argument is ok */
  63. };
  64. /* type of values returned from helper functions */
  65. enum bpf_return_type {
  66. RET_INTEGER, /* function returns integer */
  67. RET_VOID, /* function doesn't return anything */
  68. RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
  69. };
  70. /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
  71. * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
  72. * instructions after verifying
  73. */
  74. struct bpf_func_proto {
  75. u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  76. bool gpl_only;
  77. enum bpf_return_type ret_type;
  78. enum bpf_arg_type arg1_type;
  79. enum bpf_arg_type arg2_type;
  80. enum bpf_arg_type arg3_type;
  81. enum bpf_arg_type arg4_type;
  82. enum bpf_arg_type arg5_type;
  83. };
  84. /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
  85. * the first argument to eBPF programs.
  86. * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
  87. */
  88. struct bpf_context;
  89. enum bpf_access_type {
  90. BPF_READ = 1,
  91. BPF_WRITE = 2
  92. };
  93. struct bpf_prog;
  94. struct bpf_verifier_ops {
  95. /* return eBPF function prototype for verification */
  96. const struct bpf_func_proto *(*get_func_proto)(enum bpf_func_id func_id);
  97. /* return true if 'size' wide access at offset 'off' within bpf_context
  98. * with 'type' (read or write) is allowed
  99. */
  100. bool (*is_valid_access)(int off, int size, enum bpf_access_type type);
  101. u32 (*convert_ctx_access)(enum bpf_access_type type, int dst_reg,
  102. int src_reg, int ctx_off,
  103. struct bpf_insn *insn, struct bpf_prog *prog);
  104. };
  105. struct bpf_prog_type_list {
  106. struct list_head list_node;
  107. const struct bpf_verifier_ops *ops;
  108. enum bpf_prog_type type;
  109. };
  110. struct bpf_prog_aux {
  111. atomic_t refcnt;
  112. u32 used_map_cnt;
  113. u32 max_ctx_offset;
  114. const struct bpf_verifier_ops *ops;
  115. struct bpf_map **used_maps;
  116. struct bpf_prog *prog;
  117. struct user_struct *user;
  118. union {
  119. struct work_struct work;
  120. struct rcu_head rcu;
  121. };
  122. };
  123. struct bpf_array {
  124. struct bpf_map map;
  125. u32 elem_size;
  126. /* 'ownership' of prog_array is claimed by the first program that
  127. * is going to use this map or by the first program which FD is stored
  128. * in the map to make sure that all callers and callees have the same
  129. * prog_type and JITed flag
  130. */
  131. enum bpf_prog_type owner_prog_type;
  132. bool owner_jited;
  133. union {
  134. char value[0] __aligned(8);
  135. void *ptrs[0] __aligned(8);
  136. void __percpu *pptrs[0] __aligned(8);
  137. };
  138. };
  139. #define MAX_TAIL_CALL_CNT 32
  140. u64 bpf_tail_call(u64 ctx, u64 r2, u64 index, u64 r4, u64 r5);
  141. u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  142. void bpf_fd_array_map_clear(struct bpf_map *map);
  143. bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
  144. const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
  145. #ifdef CONFIG_BPF_SYSCALL
  146. DECLARE_PER_CPU(int, bpf_prog_active);
  147. void bpf_register_prog_type(struct bpf_prog_type_list *tl);
  148. void bpf_register_map_type(struct bpf_map_type_list *tl);
  149. struct bpf_prog *bpf_prog_get(u32 ufd);
  150. void bpf_prog_put(struct bpf_prog *prog);
  151. void bpf_prog_put_rcu(struct bpf_prog *prog);
  152. struct bpf_map *bpf_map_get_with_uref(u32 ufd);
  153. struct bpf_map *__bpf_map_get(struct fd f);
  154. void bpf_map_inc(struct bpf_map *map, bool uref);
  155. void bpf_map_put_with_uref(struct bpf_map *map);
  156. void bpf_map_put(struct bpf_map *map);
  157. int bpf_map_precharge_memlock(u32 pages);
  158. extern int sysctl_unprivileged_bpf_disabled;
  159. int bpf_map_new_fd(struct bpf_map *map);
  160. int bpf_prog_new_fd(struct bpf_prog *prog);
  161. int bpf_obj_pin_user(u32 ufd, const char __user *pathname);
  162. int bpf_obj_get_user(const char __user *pathname);
  163. int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
  164. int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
  165. int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
  166. u64 flags);
  167. int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
  168. u64 flags);
  169. int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
  170. /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
  171. * forced to use 'long' read/writes to try to atomically copy long counters.
  172. * Best-effort only. No barriers here, since it _will_ race with concurrent
  173. * updates from BPF programs. Called from bpf syscall and mostly used with
  174. * size 8 or 16 bytes, so ask compiler to inline it.
  175. */
  176. static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
  177. {
  178. const long *lsrc = src;
  179. long *ldst = dst;
  180. size /= sizeof(long);
  181. while (size--)
  182. *ldst++ = *lsrc++;
  183. }
  184. /* verify correctness of eBPF program */
  185. int bpf_check(struct bpf_prog **fp, union bpf_attr *attr);
  186. #else
  187. static inline void bpf_register_prog_type(struct bpf_prog_type_list *tl)
  188. {
  189. }
  190. static inline struct bpf_prog *bpf_prog_get(u32 ufd)
  191. {
  192. return ERR_PTR(-EOPNOTSUPP);
  193. }
  194. static inline void bpf_prog_put(struct bpf_prog *prog)
  195. {
  196. }
  197. #endif /* CONFIG_BPF_SYSCALL */
  198. /* verifier prototypes for helper functions called from eBPF programs */
  199. extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
  200. extern const struct bpf_func_proto bpf_map_update_elem_proto;
  201. extern const struct bpf_func_proto bpf_map_delete_elem_proto;
  202. extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
  203. extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
  204. extern const struct bpf_func_proto bpf_tail_call_proto;
  205. extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
  206. extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
  207. extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
  208. extern const struct bpf_func_proto bpf_get_current_comm_proto;
  209. extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
  210. extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
  211. extern const struct bpf_func_proto bpf_get_stackid_proto;
  212. /* Shared helpers among cBPF and eBPF. */
  213. void bpf_user_rnd_init_once(void);
  214. u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  215. #endif /* _LINUX_BPF_H */