bpf-loader.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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__END,
  40. };
  41. struct bpf_object;
  42. struct parse_events_term;
  43. #define PERF_BPF_PROBE_GROUP "perf_bpf_probe"
  44. typedef int (*bpf_prog_iter_callback_t)(struct probe_trace_event *tev,
  45. int fd, void *arg);
  46. #ifdef HAVE_LIBBPF_SUPPORT
  47. struct bpf_object *bpf__prepare_load(const char *filename, bool source);
  48. int bpf__strerror_prepare_load(const char *filename, bool source,
  49. int err, char *buf, size_t size);
  50. struct bpf_object *bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz,
  51. const char *name);
  52. void bpf__clear(void);
  53. int bpf__probe(struct bpf_object *obj);
  54. int bpf__unprobe(struct bpf_object *obj);
  55. int bpf__strerror_probe(struct bpf_object *obj, int err,
  56. char *buf, size_t size);
  57. int bpf__load(struct bpf_object *obj);
  58. int bpf__strerror_load(struct bpf_object *obj, int err,
  59. char *buf, size_t size);
  60. int bpf__foreach_tev(struct bpf_object *obj,
  61. bpf_prog_iter_callback_t func, void *arg);
  62. int bpf__config_obj(struct bpf_object *obj, struct parse_events_term *term,
  63. struct perf_evlist *evlist, int *error_pos);
  64. int bpf__strerror_config_obj(struct bpf_object *obj,
  65. struct parse_events_term *term,
  66. struct perf_evlist *evlist,
  67. int *error_pos, int err, char *buf,
  68. size_t size);
  69. int bpf__apply_obj_config(void);
  70. int bpf__strerror_apply_obj_config(int err, char *buf, size_t size);
  71. #else
  72. static inline struct bpf_object *
  73. bpf__prepare_load(const char *filename __maybe_unused,
  74. bool source __maybe_unused)
  75. {
  76. pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
  77. return ERR_PTR(-ENOTSUP);
  78. }
  79. static inline struct bpf_object *
  80. bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
  81. size_t obj_buf_sz __maybe_unused)
  82. {
  83. return ERR_PTR(-ENOTSUP);
  84. }
  85. static inline void bpf__clear(void) { }
  86. static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
  87. static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
  88. static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
  89. static inline int
  90. bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
  91. bpf_prog_iter_callback_t func __maybe_unused,
  92. void *arg __maybe_unused)
  93. {
  94. return 0;
  95. }
  96. static inline int
  97. bpf__config_obj(struct bpf_object *obj __maybe_unused,
  98. struct parse_events_term *term __maybe_unused,
  99. struct perf_evlist *evlist __maybe_unused,
  100. int *error_pos __maybe_unused)
  101. {
  102. return 0;
  103. }
  104. static inline int
  105. bpf__apply_obj_config(void)
  106. {
  107. return 0;
  108. }
  109. static inline int
  110. __bpf_strerror(char *buf, size_t size)
  111. {
  112. if (!size)
  113. return 0;
  114. strncpy(buf,
  115. "ERROR: eBPF object loading is disabled during compiling.\n",
  116. size);
  117. buf[size - 1] = '\0';
  118. return 0;
  119. }
  120. static inline
  121. int bpf__strerror_prepare_load(const char *filename __maybe_unused,
  122. bool source __maybe_unused,
  123. int err __maybe_unused,
  124. char *buf, size_t size)
  125. {
  126. return __bpf_strerror(buf, size);
  127. }
  128. static inline int
  129. bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
  130. int err __maybe_unused,
  131. char *buf, size_t size)
  132. {
  133. return __bpf_strerror(buf, size);
  134. }
  135. static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
  136. int err __maybe_unused,
  137. char *buf, size_t size)
  138. {
  139. return __bpf_strerror(buf, size);
  140. }
  141. static inline int
  142. bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
  143. struct parse_events_term *term __maybe_unused,
  144. struct perf_evlist *evlist __maybe_unused,
  145. int *error_pos __maybe_unused,
  146. int err __maybe_unused,
  147. char *buf, size_t size)
  148. {
  149. return __bpf_strerror(buf, size);
  150. }
  151. static inline int
  152. bpf__strerror_apply_obj_config(int err __maybe_unused,
  153. char *buf, size_t size)
  154. {
  155. return __bpf_strerror(buf, size);
  156. }
  157. #endif
  158. #endif