libbpf.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Common eBPF ELF object loading operations.
  3. *
  4. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  5. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  6. * Copyright (C) 2015 Huawei Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License (not later!)
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, see <http://www.gnu.org/licenses>
  20. */
  21. #ifndef __BPF_LIBBPF_H
  22. #define __BPF_LIBBPF_H
  23. #include <stdio.h>
  24. #include <stdbool.h>
  25. #include <linux/err.h>
  26. enum libbpf_errno {
  27. __LIBBPF_ERRNO__START = 4000,
  28. /* Something wrong in libelf */
  29. LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  30. LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
  31. LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
  32. LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
  33. LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
  34. LIBBPF_ERRNO__RELOC, /* Relocation failed */
  35. LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
  36. LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
  37. LIBBPF_ERRNO__PROG2BIG, /* Program too big */
  38. LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
  39. LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
  40. __LIBBPF_ERRNO__END,
  41. };
  42. int libbpf_strerror(int err, char *buf, size_t size);
  43. /*
  44. * In include/linux/compiler-gcc.h, __printf is defined. However
  45. * it should be better if libbpf.h doesn't depend on Linux header file.
  46. * So instead of __printf, here we use gcc attribute directly.
  47. */
  48. typedef int (*libbpf_print_fn_t)(const char *, ...)
  49. __attribute__((format(printf, 1, 2)));
  50. void libbpf_set_print(libbpf_print_fn_t warn,
  51. libbpf_print_fn_t info,
  52. libbpf_print_fn_t debug);
  53. /* Hide internal to user */
  54. struct bpf_object;
  55. struct bpf_object *bpf_object__open(const char *path);
  56. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  57. size_t obj_buf_sz,
  58. const char *name);
  59. void bpf_object__close(struct bpf_object *object);
  60. /* Load/unload object into/from kernel */
  61. int bpf_object__load(struct bpf_object *obj);
  62. int bpf_object__unload(struct bpf_object *obj);
  63. const char *bpf_object__name(struct bpf_object *obj);
  64. unsigned int bpf_object__kversion(struct bpf_object *obj);
  65. struct bpf_object *bpf_object__next(struct bpf_object *prev);
  66. #define bpf_object__for_each_safe(pos, tmp) \
  67. for ((pos) = bpf_object__next(NULL), \
  68. (tmp) = bpf_object__next(pos); \
  69. (pos) != NULL; \
  70. (pos) = (tmp), (tmp) = bpf_object__next(tmp))
  71. /* Accessors of bpf_program. */
  72. struct bpf_program;
  73. struct bpf_program *bpf_program__next(struct bpf_program *prog,
  74. struct bpf_object *obj);
  75. #define bpf_object__for_each_program(pos, obj) \
  76. for ((pos) = bpf_program__next(NULL, (obj)); \
  77. (pos) != NULL; \
  78. (pos) = bpf_program__next((pos), (obj)))
  79. typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
  80. void *);
  81. int bpf_program__set_priv(struct bpf_program *prog, void *priv,
  82. bpf_program_clear_priv_t clear_priv);
  83. void *bpf_program__priv(struct bpf_program *prog);
  84. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
  85. int bpf_program__fd(struct bpf_program *prog);
  86. struct bpf_insn;
  87. /*
  88. * Libbpf allows callers to adjust BPF programs before being loaded
  89. * into kernel. One program in an object file can be transform into
  90. * multiple variants to be attached to different code.
  91. *
  92. * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
  93. * are APIs for this propose.
  94. *
  95. * - bpf_program_prep_t:
  96. * It defines 'preprocessor', which is a caller defined function
  97. * passed to libbpf through bpf_program__set_prep(), and will be
  98. * called before program is loaded. The processor should adjust
  99. * the program one time for each instances according to the number
  100. * passed to it.
  101. *
  102. * - bpf_program__set_prep:
  103. * Attachs a preprocessor to a BPF program. The number of instances
  104. * whould be created is also passed through this function.
  105. *
  106. * - bpf_program__nth_fd:
  107. * After the program is loaded, get resuling fds from bpf program for
  108. * each instances.
  109. *
  110. * If bpf_program__set_prep() is not used, the program whould be loaded
  111. * without adjustment during bpf_object__load(). The program has only
  112. * one instance. In this case bpf_program__fd(prog) is equal to
  113. * bpf_program__nth_fd(prog, 0).
  114. */
  115. struct bpf_prog_prep_result {
  116. /*
  117. * If not NULL, load new instruction array.
  118. * If set to NULL, don't load this instance.
  119. */
  120. struct bpf_insn *new_insn_ptr;
  121. int new_insn_cnt;
  122. /* If not NULL, result fd is set to it */
  123. int *pfd;
  124. };
  125. /*
  126. * Parameters of bpf_program_prep_t:
  127. * - prog: The bpf_program being loaded.
  128. * - n: Index of instance being generated.
  129. * - insns: BPF instructions array.
  130. * - insns_cnt:Number of instructions in insns.
  131. * - res: Output parameter, result of transformation.
  132. *
  133. * Return value:
  134. * - Zero: pre-processing success.
  135. * - Non-zero: pre-processing, stop loading.
  136. */
  137. typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
  138. struct bpf_insn *insns, int insns_cnt,
  139. struct bpf_prog_prep_result *res);
  140. int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
  141. bpf_program_prep_t prep);
  142. int bpf_program__nth_fd(struct bpf_program *prog, int n);
  143. /*
  144. * Adjust type of bpf program. Default is kprobe.
  145. */
  146. int bpf_program__set_tracepoint(struct bpf_program *prog);
  147. int bpf_program__set_kprobe(struct bpf_program *prog);
  148. bool bpf_program__is_tracepoint(struct bpf_program *prog);
  149. bool bpf_program__is_kprobe(struct bpf_program *prog);
  150. /*
  151. * We don't need __attribute__((packed)) now since it is
  152. * unnecessary for 'bpf_map_def' because they are all aligned.
  153. * In addition, using it will trigger -Wpacked warning message,
  154. * and will be treated as an error due to -Werror.
  155. */
  156. struct bpf_map_def {
  157. unsigned int type;
  158. unsigned int key_size;
  159. unsigned int value_size;
  160. unsigned int max_entries;
  161. };
  162. /*
  163. * There is another 'struct bpf_map' in include/linux/map.h. However,
  164. * it is not a uapi header so no need to consider name clash.
  165. */
  166. struct bpf_map;
  167. struct bpf_map *
  168. bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
  169. struct bpf_map *
  170. bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
  171. #define bpf_map__for_each(pos, obj) \
  172. for ((pos) = bpf_map__next(NULL, (obj)); \
  173. (pos) != NULL; \
  174. (pos) = bpf_map__next((pos), (obj)))
  175. int bpf_map__fd(struct bpf_map *map);
  176. const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
  177. const char *bpf_map__name(struct bpf_map *map);
  178. typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
  179. int bpf_map__set_priv(struct bpf_map *map, void *priv,
  180. bpf_map_clear_priv_t clear_priv);
  181. void *bpf_map__priv(struct bpf_map *map);
  182. #endif