libbpf.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. /*
  3. * Common eBPF ELF object loading operations.
  4. *
  5. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  6. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  7. * Copyright (C) 2015 Huawei Inc.
  8. */
  9. #ifndef __LIBBPF_LIBBPF_H
  10. #define __LIBBPF_LIBBPF_H
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. #include <sys/types.h> // for size_t
  15. #include <linux/bpf.h>
  16. #ifndef LIBBPF_API
  17. #define LIBBPF_API __attribute__((visibility("default")))
  18. #endif
  19. enum libbpf_errno {
  20. __LIBBPF_ERRNO__START = 4000,
  21. /* Something wrong in libelf */
  22. LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  23. LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
  24. LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
  25. LIBBPF_ERRNO__ENDIAN, /* Endian mismatch */
  26. LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
  27. LIBBPF_ERRNO__RELOC, /* Relocation failed */
  28. LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
  29. LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
  30. LIBBPF_ERRNO__PROG2BIG, /* Program too big */
  31. LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
  32. LIBBPF_ERRNO__PROGTYPE, /* Kernel doesn't support this program type */
  33. LIBBPF_ERRNO__WRNGPID, /* Wrong pid in netlink message */
  34. LIBBPF_ERRNO__INVSEQ, /* Invalid netlink sequence */
  35. LIBBPF_ERRNO__NLPARSE, /* netlink parsing error */
  36. __LIBBPF_ERRNO__END,
  37. };
  38. LIBBPF_API int libbpf_strerror(int err, char *buf, size_t size);
  39. /*
  40. * __printf is defined in include/linux/compiler-gcc.h. However,
  41. * it would be better if libbpf.h didn't depend on Linux header files.
  42. * So instead of __printf, here we use gcc attribute directly.
  43. */
  44. typedef int (*libbpf_print_fn_t)(const char *, ...)
  45. __attribute__((format(printf, 1, 2)));
  46. LIBBPF_API void libbpf_set_print(libbpf_print_fn_t warn,
  47. libbpf_print_fn_t info,
  48. libbpf_print_fn_t debug);
  49. /* Hide internal to user */
  50. struct bpf_object;
  51. struct bpf_object_open_attr {
  52. const char *file;
  53. enum bpf_prog_type prog_type;
  54. };
  55. LIBBPF_API struct bpf_object *bpf_object__open(const char *path);
  56. LIBBPF_API struct bpf_object *
  57. bpf_object__open_xattr(struct bpf_object_open_attr *attr);
  58. struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
  59. int flags);
  60. LIBBPF_API struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  61. size_t obj_buf_sz,
  62. const char *name);
  63. LIBBPF_API int bpf_object__pin(struct bpf_object *object, const char *path);
  64. LIBBPF_API void bpf_object__close(struct bpf_object *object);
  65. /* Load/unload object into/from kernel */
  66. LIBBPF_API int bpf_object__load(struct bpf_object *obj);
  67. LIBBPF_API int bpf_object__unload(struct bpf_object *obj);
  68. LIBBPF_API const char *bpf_object__name(struct bpf_object *obj);
  69. LIBBPF_API unsigned int bpf_object__kversion(struct bpf_object *obj);
  70. LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);
  71. LIBBPF_API struct bpf_program *
  72. bpf_object__find_program_by_title(struct bpf_object *obj, const char *title);
  73. LIBBPF_API struct bpf_object *bpf_object__next(struct bpf_object *prev);
  74. #define bpf_object__for_each_safe(pos, tmp) \
  75. for ((pos) = bpf_object__next(NULL), \
  76. (tmp) = bpf_object__next(pos); \
  77. (pos) != NULL; \
  78. (pos) = (tmp), (tmp) = bpf_object__next(tmp))
  79. typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *);
  80. LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
  81. bpf_object_clear_priv_t clear_priv);
  82. LIBBPF_API void *bpf_object__priv(struct bpf_object *prog);
  83. LIBBPF_API int
  84. libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
  85. enum bpf_attach_type *expected_attach_type);
  86. LIBBPF_API int libbpf_attach_type_by_name(const char *name,
  87. enum bpf_attach_type *attach_type);
  88. /* Accessors of bpf_program */
  89. struct bpf_program;
  90. LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
  91. struct bpf_object *obj);
  92. #define bpf_object__for_each_program(pos, obj) \
  93. for ((pos) = bpf_program__next(NULL, (obj)); \
  94. (pos) != NULL; \
  95. (pos) = bpf_program__next((pos), (obj)))
  96. typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
  97. void *);
  98. LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
  99. bpf_program_clear_priv_t clear_priv);
  100. LIBBPF_API void *bpf_program__priv(struct bpf_program *prog);
  101. LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
  102. __u32 ifindex);
  103. LIBBPF_API const char *bpf_program__title(struct bpf_program *prog,
  104. bool needs_copy);
  105. LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
  106. __u32 kern_version);
  107. LIBBPF_API int bpf_program__fd(struct bpf_program *prog);
  108. LIBBPF_API int bpf_program__pin_instance(struct bpf_program *prog,
  109. const char *path,
  110. int instance);
  111. LIBBPF_API int bpf_program__pin(struct bpf_program *prog, const char *path);
  112. LIBBPF_API void bpf_program__unload(struct bpf_program *prog);
  113. struct bpf_insn;
  114. /*
  115. * Libbpf allows callers to adjust BPF programs before being loaded
  116. * into kernel. One program in an object file can be transformed into
  117. * multiple variants to be attached to different hooks.
  118. *
  119. * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
  120. * form an API for this purpose.
  121. *
  122. * - bpf_program_prep_t:
  123. * Defines a 'preprocessor', which is a caller defined function
  124. * passed to libbpf through bpf_program__set_prep(), and will be
  125. * called before program is loaded. The processor should adjust
  126. * the program one time for each instance according to the instance id
  127. * passed to it.
  128. *
  129. * - bpf_program__set_prep:
  130. * Attaches a preprocessor to a BPF program. The number of instances
  131. * that should be created is also passed through this function.
  132. *
  133. * - bpf_program__nth_fd:
  134. * After the program is loaded, get resulting FD of a given instance
  135. * of the BPF program.
  136. *
  137. * If bpf_program__set_prep() is not used, the program would be loaded
  138. * without adjustment during bpf_object__load(). The program has only
  139. * one instance. In this case bpf_program__fd(prog) is equal to
  140. * bpf_program__nth_fd(prog, 0).
  141. */
  142. struct bpf_prog_prep_result {
  143. /*
  144. * If not NULL, load new instruction array.
  145. * If set to NULL, don't load this instance.
  146. */
  147. struct bpf_insn *new_insn_ptr;
  148. int new_insn_cnt;
  149. /* If not NULL, result FD is written to it. */
  150. int *pfd;
  151. };
  152. /*
  153. * Parameters of bpf_program_prep_t:
  154. * - prog: The bpf_program being loaded.
  155. * - n: Index of instance being generated.
  156. * - insns: BPF instructions array.
  157. * - insns_cnt:Number of instructions in insns.
  158. * - res: Output parameter, result of transformation.
  159. *
  160. * Return value:
  161. * - Zero: pre-processing success.
  162. * - Non-zero: pre-processing error, stop loading.
  163. */
  164. typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
  165. struct bpf_insn *insns, int insns_cnt,
  166. struct bpf_prog_prep_result *res);
  167. LIBBPF_API int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
  168. bpf_program_prep_t prep);
  169. LIBBPF_API int bpf_program__nth_fd(struct bpf_program *prog, int n);
  170. /*
  171. * Adjust type of BPF program. Default is kprobe.
  172. */
  173. LIBBPF_API int bpf_program__set_socket_filter(struct bpf_program *prog);
  174. LIBBPF_API int bpf_program__set_tracepoint(struct bpf_program *prog);
  175. LIBBPF_API int bpf_program__set_raw_tracepoint(struct bpf_program *prog);
  176. LIBBPF_API int bpf_program__set_kprobe(struct bpf_program *prog);
  177. LIBBPF_API int bpf_program__set_sched_cls(struct bpf_program *prog);
  178. LIBBPF_API int bpf_program__set_sched_act(struct bpf_program *prog);
  179. LIBBPF_API int bpf_program__set_xdp(struct bpf_program *prog);
  180. LIBBPF_API int bpf_program__set_perf_event(struct bpf_program *prog);
  181. LIBBPF_API void bpf_program__set_type(struct bpf_program *prog,
  182. enum bpf_prog_type type);
  183. LIBBPF_API void
  184. bpf_program__set_expected_attach_type(struct bpf_program *prog,
  185. enum bpf_attach_type type);
  186. LIBBPF_API bool bpf_program__is_socket_filter(struct bpf_program *prog);
  187. LIBBPF_API bool bpf_program__is_tracepoint(struct bpf_program *prog);
  188. LIBBPF_API bool bpf_program__is_raw_tracepoint(struct bpf_program *prog);
  189. LIBBPF_API bool bpf_program__is_kprobe(struct bpf_program *prog);
  190. LIBBPF_API bool bpf_program__is_sched_cls(struct bpf_program *prog);
  191. LIBBPF_API bool bpf_program__is_sched_act(struct bpf_program *prog);
  192. LIBBPF_API bool bpf_program__is_xdp(struct bpf_program *prog);
  193. LIBBPF_API bool bpf_program__is_perf_event(struct bpf_program *prog);
  194. /*
  195. * No need for __attribute__((packed)), all members of 'bpf_map_def'
  196. * are all aligned. In addition, using __attribute__((packed))
  197. * would trigger a -Wpacked warning message, and lead to an error
  198. * if -Werror is set.
  199. */
  200. struct bpf_map_def {
  201. unsigned int type;
  202. unsigned int key_size;
  203. unsigned int value_size;
  204. unsigned int max_entries;
  205. unsigned int map_flags;
  206. };
  207. /*
  208. * The 'struct bpf_map' in include/linux/bpf.h is internal to the kernel,
  209. * so no need to worry about a name clash.
  210. */
  211. struct bpf_map;
  212. LIBBPF_API struct bpf_map *
  213. bpf_object__find_map_by_name(struct bpf_object *obj, const char *name);
  214. /*
  215. * Get bpf_map through the offset of corresponding struct bpf_map_def
  216. * in the BPF object file.
  217. */
  218. LIBBPF_API struct bpf_map *
  219. bpf_object__find_map_by_offset(struct bpf_object *obj, size_t offset);
  220. LIBBPF_API struct bpf_map *
  221. bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
  222. #define bpf_map__for_each(pos, obj) \
  223. for ((pos) = bpf_map__next(NULL, (obj)); \
  224. (pos) != NULL; \
  225. (pos) = bpf_map__next((pos), (obj)))
  226. LIBBPF_API int bpf_map__fd(struct bpf_map *map);
  227. LIBBPF_API const struct bpf_map_def *bpf_map__def(struct bpf_map *map);
  228. LIBBPF_API const char *bpf_map__name(struct bpf_map *map);
  229. LIBBPF_API __u32 bpf_map__btf_key_type_id(const struct bpf_map *map);
  230. LIBBPF_API __u32 bpf_map__btf_value_type_id(const struct bpf_map *map);
  231. typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
  232. LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
  233. bpf_map_clear_priv_t clear_priv);
  234. LIBBPF_API void *bpf_map__priv(struct bpf_map *map);
  235. LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
  236. LIBBPF_API bool bpf_map__is_offload_neutral(struct bpf_map *map);
  237. LIBBPF_API void bpf_map__set_ifindex(struct bpf_map *map, __u32 ifindex);
  238. LIBBPF_API int bpf_map__pin(struct bpf_map *map, const char *path);
  239. LIBBPF_API long libbpf_get_error(const void *ptr);
  240. struct bpf_prog_load_attr {
  241. const char *file;
  242. enum bpf_prog_type prog_type;
  243. enum bpf_attach_type expected_attach_type;
  244. int ifindex;
  245. };
  246. LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
  247. struct bpf_object **pobj, int *prog_fd);
  248. LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
  249. struct bpf_object **pobj, int *prog_fd);
  250. LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
  251. enum bpf_perf_event_ret {
  252. LIBBPF_PERF_EVENT_DONE = 0,
  253. LIBBPF_PERF_EVENT_ERROR = -1,
  254. LIBBPF_PERF_EVENT_CONT = -2,
  255. };
  256. struct perf_event_header;
  257. typedef enum bpf_perf_event_ret
  258. (*bpf_perf_event_print_t)(struct perf_event_header *hdr,
  259. void *private_data);
  260. LIBBPF_API enum bpf_perf_event_ret
  261. bpf_perf_event_read_simple(void *mmap_mem, size_t mmap_size, size_t page_size,
  262. void **copy_mem, size_t *copy_size,
  263. bpf_perf_event_print_t fn, void *private_data);
  264. struct nlattr;
  265. typedef int (*libbpf_dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
  266. int libbpf_netlink_open(unsigned int *nl_pid);
  267. int libbpf_nl_get_link(int sock, unsigned int nl_pid,
  268. libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie);
  269. int libbpf_nl_get_class(int sock, unsigned int nl_pid, int ifindex,
  270. libbpf_dump_nlmsg_t dump_class_nlmsg, void *cookie);
  271. int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
  272. libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie);
  273. int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
  274. libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie);
  275. #endif /* __LIBBPF_LIBBPF_H */