libbpf.h 11 KB

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