bpf.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. struct bpf_map;
  13. /* map is generic key/value storage optionally accesible by eBPF programs */
  14. struct bpf_map_ops {
  15. /* funcs callable from userspace (via syscall) */
  16. struct bpf_map *(*map_alloc)(union bpf_attr *attr);
  17. void (*map_free)(struct bpf_map *);
  18. int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
  19. /* funcs callable from userspace and from eBPF programs */
  20. void *(*map_lookup_elem)(struct bpf_map *map, void *key);
  21. int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
  22. int (*map_delete_elem)(struct bpf_map *map, void *key);
  23. /* funcs called by prog_array and perf_event_array map */
  24. void *(*map_fd_get_ptr) (struct bpf_map *map, int fd);
  25. void (*map_fd_put_ptr) (void *ptr);
  26. };
  27. struct bpf_map {
  28. atomic_t refcnt;
  29. enum bpf_map_type map_type;
  30. u32 key_size;
  31. u32 value_size;
  32. u32 max_entries;
  33. const struct bpf_map_ops *ops;
  34. struct work_struct work;
  35. };
  36. struct bpf_map_type_list {
  37. struct list_head list_node;
  38. const struct bpf_map_ops *ops;
  39. enum bpf_map_type type;
  40. };
  41. /* function argument constraints */
  42. enum bpf_arg_type {
  43. ARG_DONTCARE = 0, /* unused argument in helper function */
  44. /* the following constraints used to prototype
  45. * bpf_map_lookup/update/delete_elem() functions
  46. */
  47. ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
  48. ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
  49. ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
  50. /* the following constraints used to prototype bpf_memcmp() and other
  51. * functions that access data on eBPF program stack
  52. */
  53. ARG_PTR_TO_STACK, /* any pointer to eBPF program stack */
  54. ARG_CONST_STACK_SIZE, /* number of bytes accessed from stack */
  55. ARG_PTR_TO_CTX, /* pointer to context */
  56. ARG_ANYTHING, /* any (initialized) argument is ok */
  57. };
  58. /* type of values returned from helper functions */
  59. enum bpf_return_type {
  60. RET_INTEGER, /* function returns integer */
  61. RET_VOID, /* function doesn't return anything */
  62. RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
  63. };
  64. /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
  65. * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
  66. * instructions after verifying
  67. */
  68. struct bpf_func_proto {
  69. u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  70. bool gpl_only;
  71. enum bpf_return_type ret_type;
  72. enum bpf_arg_type arg1_type;
  73. enum bpf_arg_type arg2_type;
  74. enum bpf_arg_type arg3_type;
  75. enum bpf_arg_type arg4_type;
  76. enum bpf_arg_type arg5_type;
  77. };
  78. /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
  79. * the first argument to eBPF programs.
  80. * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
  81. */
  82. struct bpf_context;
  83. enum bpf_access_type {
  84. BPF_READ = 1,
  85. BPF_WRITE = 2
  86. };
  87. struct bpf_verifier_ops {
  88. /* return eBPF function prototype for verification */
  89. const struct bpf_func_proto *(*get_func_proto)(enum bpf_func_id func_id);
  90. /* return true if 'size' wide access at offset 'off' within bpf_context
  91. * with 'type' (read or write) is allowed
  92. */
  93. bool (*is_valid_access)(int off, int size, enum bpf_access_type type);
  94. u32 (*convert_ctx_access)(enum bpf_access_type type, int dst_reg,
  95. int src_reg, int ctx_off,
  96. struct bpf_insn *insn);
  97. };
  98. struct bpf_prog_type_list {
  99. struct list_head list_node;
  100. const struct bpf_verifier_ops *ops;
  101. enum bpf_prog_type type;
  102. };
  103. struct bpf_prog;
  104. struct bpf_prog_aux {
  105. atomic_t refcnt;
  106. u32 used_map_cnt;
  107. const struct bpf_verifier_ops *ops;
  108. struct bpf_map **used_maps;
  109. struct bpf_prog *prog;
  110. union {
  111. struct work_struct work;
  112. struct rcu_head rcu;
  113. };
  114. };
  115. struct bpf_array {
  116. struct bpf_map map;
  117. u32 elem_size;
  118. /* 'ownership' of prog_array is claimed by the first program that
  119. * is going to use this map or by the first program which FD is stored
  120. * in the map to make sure that all callers and callees have the same
  121. * prog_type and JITed flag
  122. */
  123. enum bpf_prog_type owner_prog_type;
  124. bool owner_jited;
  125. union {
  126. char value[0] __aligned(8);
  127. void *ptrs[0] __aligned(8);
  128. };
  129. };
  130. #define MAX_TAIL_CALL_CNT 32
  131. u64 bpf_tail_call(u64 ctx, u64 r2, u64 index, u64 r4, u64 r5);
  132. void bpf_fd_array_map_clear(struct bpf_map *map);
  133. bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
  134. const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
  135. #ifdef CONFIG_BPF_SYSCALL
  136. void bpf_register_prog_type(struct bpf_prog_type_list *tl);
  137. void bpf_register_map_type(struct bpf_map_type_list *tl);
  138. struct bpf_prog *bpf_prog_get(u32 ufd);
  139. void bpf_prog_put(struct bpf_prog *prog);
  140. void bpf_prog_put_rcu(struct bpf_prog *prog);
  141. struct bpf_map *bpf_map_get(struct fd f);
  142. void bpf_map_put(struct bpf_map *map);
  143. /* verify correctness of eBPF program */
  144. int bpf_check(struct bpf_prog **fp, union bpf_attr *attr);
  145. #else
  146. static inline void bpf_register_prog_type(struct bpf_prog_type_list *tl)
  147. {
  148. }
  149. static inline struct bpf_prog *bpf_prog_get(u32 ufd)
  150. {
  151. return ERR_PTR(-EOPNOTSUPP);
  152. }
  153. static inline void bpf_prog_put(struct bpf_prog *prog)
  154. {
  155. }
  156. #endif /* CONFIG_BPF_SYSCALL */
  157. /* verifier prototypes for helper functions called from eBPF programs */
  158. extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
  159. extern const struct bpf_func_proto bpf_map_update_elem_proto;
  160. extern const struct bpf_func_proto bpf_map_delete_elem_proto;
  161. extern const struct bpf_func_proto bpf_perf_event_read_proto;
  162. extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
  163. extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
  164. extern const struct bpf_func_proto bpf_tail_call_proto;
  165. extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
  166. extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
  167. extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
  168. extern const struct bpf_func_proto bpf_get_current_comm_proto;
  169. extern const struct bpf_func_proto bpf_skb_vlan_push_proto;
  170. extern const struct bpf_func_proto bpf_skb_vlan_pop_proto;
  171. /* Shared helpers among cBPF and eBPF. */
  172. void bpf_user_rnd_init_once(void);
  173. u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  174. #endif /* _LINUX_BPF_H */