bpf-loader.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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)(struct probe_trace_event *tev,
  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_tev(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. #else
  73. static inline struct bpf_object *
  74. bpf__prepare_load(const char *filename __maybe_unused,
  75. bool source __maybe_unused)
  76. {
  77. pr_debug("ERROR: eBPF object loading is disabled during compiling.\n");
  78. return ERR_PTR(-ENOTSUP);
  79. }
  80. static inline struct bpf_object *
  81. bpf__prepare_load_buffer(void *obj_buf __maybe_unused,
  82. size_t obj_buf_sz __maybe_unused)
  83. {
  84. return ERR_PTR(-ENOTSUP);
  85. }
  86. static inline void bpf__clear(void) { }
  87. static inline int bpf__probe(struct bpf_object *obj __maybe_unused) { return 0;}
  88. static inline int bpf__unprobe(struct bpf_object *obj __maybe_unused) { return 0;}
  89. static inline int bpf__load(struct bpf_object *obj __maybe_unused) { return 0; }
  90. static inline int
  91. bpf__foreach_tev(struct bpf_object *obj __maybe_unused,
  92. bpf_prog_iter_callback_t func __maybe_unused,
  93. void *arg __maybe_unused)
  94. {
  95. return 0;
  96. }
  97. static inline int
  98. bpf__config_obj(struct bpf_object *obj __maybe_unused,
  99. struct parse_events_term *term __maybe_unused,
  100. struct perf_evlist *evlist __maybe_unused,
  101. int *error_pos __maybe_unused)
  102. {
  103. return 0;
  104. }
  105. static inline int
  106. bpf__apply_obj_config(void)
  107. {
  108. return 0;
  109. }
  110. static inline int
  111. __bpf_strerror(char *buf, size_t size)
  112. {
  113. if (!size)
  114. return 0;
  115. strncpy(buf,
  116. "ERROR: eBPF object loading is disabled during compiling.\n",
  117. size);
  118. buf[size - 1] = '\0';
  119. return 0;
  120. }
  121. static inline
  122. int bpf__strerror_prepare_load(const char *filename __maybe_unused,
  123. bool source __maybe_unused,
  124. int err __maybe_unused,
  125. char *buf, size_t size)
  126. {
  127. return __bpf_strerror(buf, size);
  128. }
  129. static inline int
  130. bpf__strerror_probe(struct bpf_object *obj __maybe_unused,
  131. int err __maybe_unused,
  132. char *buf, size_t size)
  133. {
  134. return __bpf_strerror(buf, size);
  135. }
  136. static inline int bpf__strerror_load(struct bpf_object *obj __maybe_unused,
  137. int err __maybe_unused,
  138. char *buf, size_t size)
  139. {
  140. return __bpf_strerror(buf, size);
  141. }
  142. static inline int
  143. bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused,
  144. struct parse_events_term *term __maybe_unused,
  145. struct perf_evlist *evlist __maybe_unused,
  146. int *error_pos __maybe_unused,
  147. int err __maybe_unused,
  148. char *buf, size_t size)
  149. {
  150. return __bpf_strerror(buf, size);
  151. }
  152. static inline int
  153. bpf__strerror_apply_obj_config(int err __maybe_unused,
  154. char *buf, size_t size)
  155. {
  156. return __bpf_strerror(buf, size);
  157. }
  158. #endif
  159. #endif