libbpf.h 9.0 KB

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