libbpf.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <stdint.h>
  25. #include <stdbool.h>
  26. #include <sys/types.h> // for size_t
  27. #include <linux/bpf.h>
  28. enum libbpf_errno {
  29. __LIBBPF_ERRNO__START = 4000,
  30. /* Something wrong in libelf */
  31. LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  32. LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
  33. LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
  34. LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
  35. LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
  36. LIBBPF_ERRNO__RELOC, /* Relocation failed */
  37. LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
  38. LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
  39. LIBBPF_ERRNO__PROG2BIG, /* Program too big */
  40. LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
  41. LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
  42. LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
  43. LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
  44. __LIBBPF_ERRNO__END,
  45. };
  46. int libbpf_strerror(int err, char *buf, size_t size);
  47. /*
  48. * In include/linux/compiler-gcc.h, __printf is defined. However
  49. * it should be better if libbpf.h doesn't depend on Linux header file.
  50. * So instead of __printf, here we use gcc attribute directly.
  51. */
  52. typedef int (*libbpf_print_fn_t)(const char *, ...)
  53. __attribute__((format(printf, 1, 2)));
  54. void libbpf_set_print(libbpf_print_fn_t warn,
  55. libbpf_print_fn_t info,
  56. libbpf_print_fn_t debug);
  57. /* Hide internal to user */
  58. struct bpf_object;
  59. struct bpf_object *bpf_object__open(const char *path);
  60. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  61. size_t obj_buf_sz,
  62. const char *name);
  63. int bpf_object__pin(struct bpf_object *object, const char *path);
  64. void bpf_object__close(struct bpf_object *object);
  65. /* Load/unload object into/from kernel */
  66. int bpf_object__load(struct bpf_object *obj);
  67. int bpf_object__unload(struct bpf_object *obj);
  68. const char *bpf_object__name(struct bpf_object *obj);
  69. unsigned int bpf_object__kversion(struct bpf_object *obj);
  70. struct bpf_object *bpf_object__next(struct bpf_object *prev);
  71. #define bpf_object__for_each_safe(pos, tmp) \
  72. for ((pos) = bpf_object__next(NULL), \
  73. (tmp) = bpf_object__next(pos); \
  74. (pos) != NULL; \
  75. (pos) = (tmp), (tmp) = bpf_object__next(tmp))
  76. typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
  77. int bpf_object__set_priv(struct bpf_object *obj, void *priv,
  78. bpf_object_clear_priv_t clear_priv);
  79. void *bpf_object__priv(struct bpf_object *prog);
  80. /* Accessors of bpf_program. */
  81. struct bpf_program;
  82. struct bpf_program *bpf_program__next(struct bpf_program *prog,
  83. struct bpf_object *obj);
  84. #define bpf_object__for_each_program(pos, obj) \
  85. for ((pos) = bpf_program__next(NULL, (obj)); \
  86. (pos) != NULL; \
  87. (pos) = bpf_program__next((pos), (obj)))
  88. typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
  89. void *);
  90. int bpf_program__set_priv(struct bpf_program *prog, void *priv,
  91. bpf_program_clear_priv_t clear_priv);
  92. void *bpf_program__priv(struct bpf_program *prog);
  93. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
  94. int bpf_program__fd(struct bpf_program *prog);
  95. int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
  96. int instance);
  97. int bpf_program__pin(struct bpf_program *prog, const char *path);
  98. struct bpf_insn;
  99. /*
  100. * Libbpf allows callers to adjust BPF programs before being loaded
  101. * into kernel. One program in an object file can be transform into
  102. * multiple variants to be attached to different code.
  103. *
  104. * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
  105. * are APIs for this propose.
  106. *
  107. * - bpf_program_prep_t:
  108. * It defines 'preprocessor', which is a caller defined function
  109. * passed to libbpf through bpf_program__set_prep(), and will be
  110. * called before program is loaded. The processor should adjust
  111. * the program one time for each instances according to the number
  112. * passed to it.
  113. *
  114. * - bpf_program__set_prep:
  115. * Attachs a preprocessor to a BPF program. The number of instances
  116. * whould be created is also passed through this function.
  117. *
  118. * - bpf_program__nth_fd:
  119. * After the program is loaded, get resuling fds from bpf program for
  120. * each instances.
  121. *
  122. * If bpf_program__set_prep() is not used, the program whould be loaded
  123. * without adjustment during bpf_object__load(). The program has only
  124. * one instance. In this case bpf_program__fd(prog) is equal to
  125. * bpf_program__nth_fd(prog, 0).
  126. */
  127. struct bpf_prog_prep_result {
  128. /*
  129. * If not NULL, load new instruction array.
  130. * If set to NULL, don't load this instance.
  131. */
  132. struct bpf_insn *new_insn_ptr;
  133. int new_insn_cnt;
  134. /* If not NULL, result fd is set to it */
  135. int *pfd;
  136. };
  137. /*
  138. * Parameters of bpf_program_prep_t:
  139. * - prog: The bpf_program being loaded.
  140. * - n: Index of instance being generated.
  141. * - insns: BPF instructions array.
  142. * - insns_cnt:Number of instructions in insns.
  143. * - res: Output parameter, result of transformation.
  144. *
  145. * Return value:
  146. * - Zero: pre-processing success.
  147. * - Non-zero: pre-processing, stop loading.
  148. */
  149. typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
  150. struct bpf_insn *insns, int insns_cnt,
  151. struct bpf_prog_prep_result *res);
  152. int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
  153. bpf_program_prep_t prep);
  154. int bpf_program__nth_fd(struct bpf_program *prog, int n);
  155. /*
  156. * Adjust type of bpf program. Default is kprobe.
  157. */
  158. int bpf_program__set_socket_filter(struct bpf_program *prog);
  159. int bpf_program__set_tracepoint(struct bpf_program *prog);
  160. int bpf_program__set_kprobe(struct bpf_program *prog);
  161. int bpf_program__set_sched_cls(struct bpf_program *prog);
  162. int bpf_program__set_sched_act(struct bpf_program *prog);
  163. int bpf_program__set_xdp(struct bpf_program *prog);
  164. int bpf_program__set_perf_event(struct bpf_program *prog);
  165. void bpf_program__set_type(struct bpf_program *prog, enum bpf_prog_type type);
  166. bool bpf_program__is_socket_filter(struct bpf_program *prog);
  167. bool bpf_program__is_tracepoint(struct bpf_program *prog);
  168. bool bpf_program__is_kprobe(struct bpf_program *prog);
  169. bool bpf_program__is_sched_cls(struct bpf_program *prog);
  170. bool bpf_program__is_sched_act(struct bpf_program *prog);
  171. bool bpf_program__is_xdp(struct bpf_program *prog);
  172. bool bpf_program__is_perf_event(struct bpf_program *prog);
  173. /*
  174. * We don't need __attribute__((packed)) now since it is
  175. * unnecessary for 'bpf_map_def' because they are all aligned.
  176. * In addition, using it will trigger -Wpacked warning message,
  177. * and will be treated as an error due to -Werror.
  178. */
  179. struct bpf_map_def {
  180. unsigned int type;
  181. unsigned int key_size;
  182. unsigned int value_size;
  183. unsigned int max_entries;
  184. unsigned int map_flags;
  185. };
  186. /*
  187. * There is another 'struct bpf_map' in include/linux/map.h. However,
  188. * it is not a uapi header so no need to consider name clash.
  189. */
  190. struct bpf_map;
  191. struct bpf_map *
  192. bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
  193. /*
  194. * Get bpf_map through the offset of corresponding struct bpf_map_def
  195. * in the bpf object file.
  196. */
  197. struct bpf_map *
  198. bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
  199. struct bpf_map *
  200. bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
  201. #define bpf_map__for_each(pos, obj) \
  202. for ((pos) = bpf_map__next(NULL, (obj)); \
  203. (pos) != NULL; \
  204. (pos) = bpf_map__next((pos), (obj)))
  205. int bpf_map__fd(struct bpf_map *map);
  206. const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
  207. const char *bpf_map__name(struct bpf_map *map);
  208. typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
  209. int bpf_map__set_priv(struct bpf_map *map, void *priv,
  210. bpf_map_clear_priv_t clear_priv);
  211. void *bpf_map__priv(struct bpf_map *map);
  212. int bpf_map__pin(struct bpf_map *map, const char *path);
  213. long libbpf_get_error(const void *ptr);
  214. int bpf_prog_load(const char *file, enum bpf_prog_type type,
  215. struct bpf_object **pobj, int *prog_fd);
  216. int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
  217. #endif