bpf.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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);
  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. struct bpf_map_ops *ops;
  31. struct work_struct work;
  32. };
  33. struct bpf_map_type_list {
  34. struct list_head list_node;
  35. struct bpf_map_ops *ops;
  36. enum bpf_map_type type;
  37. };
  38. void bpf_register_map_type(struct bpf_map_type_list *tl);
  39. void bpf_map_put(struct bpf_map *map);
  40. struct bpf_map *bpf_map_get(struct fd f);
  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_ANYTHING, /* any (initialized) argument is ok */
  56. };
  57. /* type of values returned from helper functions */
  58. enum bpf_return_type {
  59. RET_INTEGER, /* function returns integer */
  60. RET_VOID, /* function doesn't return anything */
  61. RET_PTR_TO_MAP_VALUE_OR_NULL, /* returns a pointer to map elem value or NULL */
  62. };
  63. /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
  64. * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
  65. * instructions after verifying
  66. */
  67. struct bpf_func_proto {
  68. u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
  69. bool gpl_only;
  70. enum bpf_return_type ret_type;
  71. enum bpf_arg_type arg1_type;
  72. enum bpf_arg_type arg2_type;
  73. enum bpf_arg_type arg3_type;
  74. enum bpf_arg_type arg4_type;
  75. enum bpf_arg_type arg5_type;
  76. };
  77. /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
  78. * the first argument to eBPF programs.
  79. * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
  80. */
  81. struct bpf_context;
  82. enum bpf_access_type {
  83. BPF_READ = 1,
  84. BPF_WRITE = 2
  85. };
  86. struct bpf_verifier_ops {
  87. /* return eBPF function prototype for verification */
  88. const struct bpf_func_proto *(*get_func_proto)(enum bpf_func_id func_id);
  89. /* return true if 'size' wide access at offset 'off' within bpf_context
  90. * with 'type' (read or write) is allowed
  91. */
  92. bool (*is_valid_access)(int off, int size, enum bpf_access_type type);
  93. };
  94. struct bpf_prog_type_list {
  95. struct list_head list_node;
  96. struct bpf_verifier_ops *ops;
  97. enum bpf_prog_type type;
  98. };
  99. void bpf_register_prog_type(struct bpf_prog_type_list *tl);
  100. struct bpf_prog;
  101. struct bpf_prog_aux {
  102. atomic_t refcnt;
  103. bool is_gpl_compatible;
  104. enum bpf_prog_type prog_type;
  105. struct bpf_verifier_ops *ops;
  106. struct bpf_map **used_maps;
  107. u32 used_map_cnt;
  108. struct bpf_prog *prog;
  109. struct work_struct work;
  110. };
  111. void bpf_prog_put(struct bpf_prog *prog);
  112. struct bpf_prog *bpf_prog_get(u32 ufd);
  113. /* verify correctness of eBPF program */
  114. int bpf_check(struct bpf_prog *fp, union bpf_attr *attr);
  115. #endif /* _LINUX_BPF_H */