libbpf.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #ifndef __BPF_LIBBPF_H
  9. #define __BPF_LIBBPF_H
  10. #include <stdio.h>
  11. #include <stdbool.h>
  12. #include <linux/err.h>
  13. enum libbpf_errno {
  14. __LIBBPF_ERRNO__START = 4000,
  15. /* Something wrong in libelf */
  16. LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  17. LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
  18. LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
  19. LIBBPF_ERRNO__ENDIAN, /* Endian missmatch */
  20. LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
  21. LIBBPF_ERRNO__RELOC, /* Relocation failed */
  22. LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
  23. LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
  24. LIBBPF_ERRNO__PROG2BIG, /* Program too big */
  25. LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
  26. __LIBBPF_ERRNO__END,
  27. };
  28. int libbpf_strerror(int err, char *buf, size_t size);
  29. /*
  30. * In include/linux/compiler-gcc.h, __printf is defined. However
  31. * it should be better if libbpf.h doesn't depend on Linux header file.
  32. * So instead of __printf, here we use gcc attribute directly.
  33. */
  34. typedef int (*libbpf_print_fn_t)(const char *, ...)
  35. __attribute__((format(printf, 1, 2)));
  36. void libbpf_set_print(libbpf_print_fn_t warn,
  37. libbpf_print_fn_t info,
  38. libbpf_print_fn_t debug);
  39. /* Hide internal to user */
  40. struct bpf_object;
  41. struct bpf_object *bpf_object__open(const char *path);
  42. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  43. size_t obj_buf_sz,
  44. const char *name);
  45. void bpf_object__close(struct bpf_object *object);
  46. /* Load/unload object into/from kernel */
  47. int bpf_object__load(struct bpf_object *obj);
  48. int bpf_object__unload(struct bpf_object *obj);
  49. const char *bpf_object__get_name(struct bpf_object *obj);
  50. unsigned int bpf_object__get_kversion(struct bpf_object *obj);
  51. struct bpf_object *bpf_object__next(struct bpf_object *prev);
  52. #define bpf_object__for_each_safe(pos, tmp) \
  53. for ((pos) = bpf_object__next(NULL), \
  54. (tmp) = bpf_object__next(pos); \
  55. (pos) != NULL; \
  56. (pos) = (tmp), (tmp) = bpf_object__next(tmp))
  57. /* Accessors of bpf_program. */
  58. struct bpf_program;
  59. struct bpf_program *bpf_program__next(struct bpf_program *prog,
  60. struct bpf_object *obj);
  61. #define bpf_object__for_each_program(pos, obj) \
  62. for ((pos) = bpf_program__next(NULL, (obj)); \
  63. (pos) != NULL; \
  64. (pos) = bpf_program__next((pos), (obj)))
  65. typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
  66. void *);
  67. int bpf_program__set_private(struct bpf_program *prog, void *priv,
  68. bpf_program_clear_priv_t clear_priv);
  69. int bpf_program__get_private(struct bpf_program *prog,
  70. void **ppriv);
  71. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
  72. int bpf_program__fd(struct bpf_program *prog);
  73. struct bpf_insn;
  74. /*
  75. * Libbpf allows callers to adjust BPF programs before being loaded
  76. * into kernel. One program in an object file can be transform into
  77. * multiple variants to be attached to different code.
  78. *
  79. * bpf_program_prep_t, bpf_program__set_prep and bpf_program__nth_fd
  80. * are APIs for this propose.
  81. *
  82. * - bpf_program_prep_t:
  83. * It defines 'preprocessor', which is a caller defined function
  84. * passed to libbpf through bpf_program__set_prep(), and will be
  85. * called before program is loaded. The processor should adjust
  86. * the program one time for each instances according to the number
  87. * passed to it.
  88. *
  89. * - bpf_program__set_prep:
  90. * Attachs a preprocessor to a BPF program. The number of instances
  91. * whould be created is also passed through this function.
  92. *
  93. * - bpf_program__nth_fd:
  94. * After the program is loaded, get resuling fds from bpf program for
  95. * each instances.
  96. *
  97. * If bpf_program__set_prep() is not used, the program whould be loaded
  98. * without adjustment during bpf_object__load(). The program has only
  99. * one instance. In this case bpf_program__fd(prog) is equal to
  100. * bpf_program__nth_fd(prog, 0).
  101. */
  102. struct bpf_prog_prep_result {
  103. /*
  104. * If not NULL, load new instruction array.
  105. * If set to NULL, don't load this instance.
  106. */
  107. struct bpf_insn *new_insn_ptr;
  108. int new_insn_cnt;
  109. /* If not NULL, result fd is set to it */
  110. int *pfd;
  111. };
  112. /*
  113. * Parameters of bpf_program_prep_t:
  114. * - prog: The bpf_program being loaded.
  115. * - n: Index of instance being generated.
  116. * - insns: BPF instructions array.
  117. * - insns_cnt:Number of instructions in insns.
  118. * - res: Output parameter, result of transformation.
  119. *
  120. * Return value:
  121. * - Zero: pre-processing success.
  122. * - Non-zero: pre-processing, stop loading.
  123. */
  124. typedef int (*bpf_program_prep_t)(struct bpf_program *prog, int n,
  125. struct bpf_insn *insns, int insns_cnt,
  126. struct bpf_prog_prep_result *res);
  127. int bpf_program__set_prep(struct bpf_program *prog, int nr_instance,
  128. bpf_program_prep_t prep);
  129. int bpf_program__nth_fd(struct bpf_program *prog, int n);
  130. /*
  131. * We don't need __attribute__((packed)) now since it is
  132. * unnecessary for 'bpf_map_def' because they are all aligned.
  133. * In addition, using it will trigger -Wpacked warning message,
  134. * and will be treated as an error due to -Werror.
  135. */
  136. struct bpf_map_def {
  137. unsigned int type;
  138. unsigned int key_size;
  139. unsigned int value_size;
  140. unsigned int max_entries;
  141. };
  142. /*
  143. * There is another 'struct bpf_map' in include/linux/map.h. However,
  144. * it is not a uapi header so no need to consider name clash.
  145. */
  146. struct bpf_map;
  147. struct bpf_map *
  148. bpf_object__get_map_by_name(struct bpf_object *obj, const char *name);
  149. struct bpf_map *
  150. bpf_map__next(struct bpf_map *map, struct bpf_object *obj);
  151. #define bpf_map__for_each(pos, obj) \
  152. for ((pos) = bpf_map__next(NULL, (obj)); \
  153. (pos) != NULL; \
  154. (pos) = bpf_map__next((pos), (obj)))
  155. int bpf_map__get_fd(struct bpf_map *map);
  156. int bpf_map__get_def(struct bpf_map *map, struct bpf_map_def *pdef);
  157. const char *bpf_map__get_name(struct bpf_map *map);
  158. typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
  159. int bpf_map__set_private(struct bpf_map *map, void *priv,
  160. bpf_map_clear_priv_t clear_priv);
  161. int bpf_map__get_private(struct bpf_map *map, void **ppriv);
  162. #endif