bpf.h 5.7 KB

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