bpf.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_ANYTHING, /* any (initialized) argument is ok */
  53. };
  54. /* type of values returned from helper functions */
  55. enum bpf_return_type {
  56. RET_INTEGER, /* function returns integer */
  57. RET_VOID, /* function doesn't return anything */
  58. RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
  59. };
  60. /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
  61. * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
  62. * instructions after verifying
  63. */
  64. struct bpf_func_proto {
  65. u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  66. bool gpl_only;
  67. enum bpf_return_type ret_type;
  68. enum bpf_arg_type arg1_type;
  69. enum bpf_arg_type arg2_type;
  70. enum bpf_arg_type arg3_type;
  71. enum bpf_arg_type arg4_type;
  72. enum bpf_arg_type arg5_type;
  73. };
  74. /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
  75. * the first argument to eBPF programs.
  76. * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
  77. */
  78. struct bpf_context;
  79. enum bpf_access_type {
  80. BPF_READ = 1,
  81. BPF_WRITE = 2
  82. };
  83. struct bpf_verifier_ops {
  84. /* return eBPF function prototype for verification */
  85. const struct bpf_func_proto *(*get_func_proto)(enum bpf_func_id func_id);
  86. /* return true if 'size' wide access at offset 'off' within bpf_context
  87. * with 'type' (read or write) is allowed
  88. */
  89. bool (*is_valid_access)(int off, int size, enum bpf_access_type type);
  90. u32 (*convert_ctx_access)(int dst_reg, int src_reg, int ctx_off,
  91. struct bpf_insn *insn);
  92. };
  93. struct bpf_prog_type_list {
  94. struct list_head list_node;
  95. const struct bpf_verifier_ops *ops;
  96. enum bpf_prog_type type;
  97. };
  98. struct bpf_prog;
  99. struct bpf_prog_aux {
  100. atomic_t refcnt;
  101. u32 used_map_cnt;
  102. const struct bpf_verifier_ops *ops;
  103. struct bpf_map **used_maps;
  104. struct bpf_prog *prog;
  105. struct work_struct work;
  106. };
  107. #ifdef CONFIG_BPF_SYSCALL
  108. void bpf_register_prog_type(struct bpf_prog_type_list *tl);
  109. void bpf_register_map_type(struct bpf_map_type_list *tl);
  110. struct bpf_prog *bpf_prog_get(u32 ufd);
  111. void bpf_prog_put(struct bpf_prog *prog);
  112. struct bpf_map *bpf_map_get(struct fd f);
  113. void bpf_map_put(struct bpf_map *map);
  114. /* verify correctness of eBPF program */
  115. int bpf_check(struct bpf_prog **fp, union bpf_attr *attr);
  116. #else
  117. static inline void bpf_register_prog_type(struct bpf_prog_type_list *tl)
  118. {
  119. }
  120. static inline struct bpf_prog *bpf_prog_get(u32 ufd)
  121. {
  122. return ERR_PTR(-EOPNOTSUPP);
  123. }
  124. static inline void bpf_prog_put(struct bpf_prog *prog)
  125. {
  126. }
  127. #endif /* CONFIG_BPF_SYSCALL */
  128. /* verifier prototypes for helper functions called from eBPF programs */
  129. extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
  130. extern const struct bpf_func_proto bpf_map_update_elem_proto;
  131. extern const struct bpf_func_proto bpf_map_delete_elem_proto;
  132. extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
  133. extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
  134. #endif /* _LINUX_BPF_H */