bpf-loader.h 6.5 KB

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