bpf-loader.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
  3. * Copyright (C) 2015, Huawei Inc.
  4. */
  5. #ifndef __BPF_LOADER_H
  6. #define __BPF_LOADER_H
  7. #include <linux/compiler.h>
  8. #include <linux/err.h>
  9. #include <string.h>
  10. #include <bpf/libbpf.h>
  11. #include "probe-event.h"
  12. #include "evlist.h"
  13. #include "debug.h"
  14. enum bpf_loader_errno {
  15. __BPF_LOADER_ERRNO__START = __LIBBPF_ERRNO__START - 100,
  16. /* Invalid config string */
  17. BPF_LOADER_ERRNO__CONFIG = __BPF_LOADER_ERRNO__START,
  18. BPF_LOADER_ERRNO__GROUP, /* Invalid group name */
  19. BPF_LOADER_ERRNO__EVENTNAME, /* Event name is missing */
  20. BPF_LOADER_ERRNO__INTERNAL, /* BPF loader internal error */
  21. BPF_LOADER_ERRNO__COMPILE, /* Error when compiling BPF scriptlet */
  22. BPF_LOADER_ERRNO__PROGCONF_TERM,/* Invalid program config term in config string */
  23. BPF_LOADER_ERRNO__PROLOGUE, /* Failed to generate prologue */
  24. BPF_LOADER_ERRNO__PROLOGUE2BIG, /* Prologue too big for program */
  25. BPF_LOADER_ERRNO__PROLOGUEOOB, /* Offset out of bound for prologue */
  26. BPF_LOADER_ERRNO__OBJCONF_OPT, /* Invalid object config option */
  27. BPF_LOADER_ERRNO__OBJCONF_CONF, /* Config value not set (lost '=')) */
  28. BPF_LOADER_ERRNO__OBJCONF_MAP_OPT, /* Invalid object map config option */
  29. BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST, /* Target map not exist */
  30. BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE, /* Incorrect value type for map */
  31. BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE, /* Incorrect map type */
  32. BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE, /* Incorrect map key size */
  33. BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE,/* Incorrect map value size */
  34. BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT, /* Event not found for map setting */
  35. BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE, /* Invalid map size for event setting */
  36. BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM, /* Event dimension too large */
  37. BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH, /* Doesn't support inherit event */
  38. BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE, /* Wrong event type for map */
  39. BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG, /* Index too large */
  40. __BPF_LOADER_ERRNO__END,
  41. };
  42. struct bpf_object;
  43. struct parse_events_term;
  44. #define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
  45. typedef int (*bpf_prog_iter_callback_t)(const char *group, const char *event,
  46. int fd, void *arg);
  47. #ifdef HAVE_LIBBPF_SUPPORT
  48. struct bpf_object *bpf__prepare_load(const char *filename, bool source);
  49. int bpf__strerror_prepare_load(const char *filename, bool source,
  50. int err, char *buf, size_t size);
  51. struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
  52. const char *name);
  53. void bpf__clear(void);
  54. int bpf__probe(struct bpf_object *obj);
  55. int bpf__unprobe(struct bpf_object *obj);
  56. int bpf__strerror_probe(struct bpf_object *obj, int err,
  57. char *buf, size_t size);
  58. int bpf__load(struct bpf_object *obj);
  59. int bpf__strerror_load(struct bpf_object *obj, int err,
  60. char *buf, size_t size);
  61. int bpf__foreach_event(struct bpf_object *obj,
  62. bpf_prog_iter_callback_t func, void *arg);
  63. int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
  64. struct perf_evlist *evlist, int *error_pos);
  65. int bpf__strerror_config_obj(struct bpf_object *obj,
  66. struct parse_events_term *term,
  67. struct perf_evlist *evlist,
  68. int *error_pos, int err, char *buf,
  69. size_t size);
  70. int bpf__apply_obj_config(void);
  71. int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
  72. int bpf__setup_stdout(struct perf_evlist *evlist);
  73. int bpf__strerror_setup_stdout(struct perf_evlist *evlist, int err,
  74. char *buf, size_t size);
  75. #else
  76. static inline struct bpf_object *
  77. bpf__prepare_load(const char *filename __maybe_unused,
  78. bool source __maybe_unused)
  79. {
  80. pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
  81. return ERR_PTR(-ENOTSUP);
  82. }
  83. static inline struct bpf_object *
  84. bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
  85. size_t obj_buf_sz __maybe_unused)
  86. {
  87. return ERR_PTR(-ENOTSUP);
  88. }
  89. static inline void bpf__clear(void) { }
  90. static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
  91. static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
  92. static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
  93. static inline int
  94. bpf__foreach_event(struct bpf_object *obj __maybe_unused,
  95. bpf_prog_iter_callback_t func __maybe_unused,
  96. void *arg __maybe_unused)
  97. {
  98. return 0;
  99. }
  100. static inline int
  101. bpf__config_obj(struct bpf_object *obj __maybe_unused,
  102. struct parse_events_term *term __maybe_unused,
  103. struct perf_evlist *evlist __maybe_unused,
  104. int *error_pos __maybe_unused)
  105. {
  106. return 0;
  107. }
  108. static inline int
  109. bpf__apply_obj_config(void)
  110. {
  111. return 0;
  112. }
  113. static inline int
  114. bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused)
  115. {
  116. return 0;
  117. }
  118. static inline int
  119. __bpf_strerror(char *buf, size_t size)
  120. {
  121. if (!size)
  122. return 0;
  123. strncpy(buf,
  124. "ERROR: eBPF object loading is disabled during compiling.\n",
  125. size);
  126. buf[size - 1] = '\0';
  127. return 0;
  128. }
  129. static inline
  130. int bpf__strerror_prepare_load(const char *filename __maybe_unused,
  131. bool source __maybe_unused,
  132. int err __maybe_unused,
  133. char *buf, size_t size)
  134. {
  135. return __bpf_strerror(buf, size);
  136. }
  137. static inline int
  138. bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
  139. int err __maybe_unused,
  140. char *buf, size_t size)
  141. {
  142. return __bpf_strerror(buf, size);
  143. }
  144. static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
  145. int err __maybe_unused,
  146. char *buf, size_t size)
  147. {
  148. return __bpf_strerror(buf, size);
  149. }
  150. static inline int
  151. bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
  152. struct parse_events_term *term __maybe_unused,
  153. struct perf_evlist *evlist __maybe_unused,
  154. int *error_pos __maybe_unused,
  155. int err __maybe_unused,
  156. char *buf, size_t size)
  157. {
  158. return __bpf_strerror(buf, size);
  159. }
  160. static inline int
  161. bpf__strerror_apply_obj_config(int err __maybe_unused,
  162. char *buf, size_t size)
  163. {
  164. return __bpf_strerror(buf, size);
  165. }
  166. static inline int
  167. bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused,
  168. int err __maybe_unused, char *buf,
  169. size_t size)
  170. {
  171. return __bpf_strerror(buf, size);
  172. }
  173. #endif
  174. #endif