ebpf.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_EBPF_H
  3. #define NETDATA_EBPF_H 1
  4. #include <bpf/bpf.h>
  5. #include <bpf/libbpf.h>
  6. #ifdef LIBBPF_DEPRECATED
  7. #include <bpf/btf.h>
  8. #include <linux/btf.h>
  9. #endif
  10. #include <stdlib.h> // Necessary for stdtoul
  11. #define NETDATA_DEBUGFS "/sys/kernel/debug/tracing/"
  12. #define NETDATA_KALLSYMS "/proc/kallsyms"
  13. // Config files
  14. #define EBPF_GLOBAL_SECTION "global"
  15. #define EBPF_CFG_LOAD_MODE "ebpf load mode"
  16. #define EBPF_CFG_LOAD_MODE_DEFAULT "entry"
  17. #define EBPF_CFG_LOAD_MODE_RETURN "return"
  18. #define EBPF_MAX_MODE_LENGTH 6
  19. #define EBPF_CFG_TYPE_FORMAT "ebpf type format"
  20. #define EBPF_CFG_DEFAULT_PROGRAM "auto"
  21. #define EBPF_CFG_CORE_PROGRAM "CO-RE"
  22. #define EBPF_CFG_LEGACY_PROGRAM "legacy"
  23. #define EBPF_CFG_CORE_ATTACH "ebpf co-re tracing"
  24. #define EBPF_CFG_ATTACH_TRAMPOLINE "trampoline"
  25. #define EBPF_CFG_ATTACH_TRACEPOINT "tracepoint"
  26. #define EBPF_CFG_ATTACH_PROBE "probe"
  27. #define EBPF_CFG_PROGRAM_PATH "btf path"
  28. #define EBPF_CFG_UPDATE_EVERY "update every"
  29. #define EBPF_CFG_PID_SIZE "pid table size"
  30. #define EBPF_CFG_APPLICATION "apps"
  31. #define EBPF_CFG_CGROUP "cgroups"
  32. #define EBPF_COMMON_FNCT_CLEAN_UP "release_task"
  33. /**
  34. * The RedHat magic number was got doing:
  35. *
  36. * 1797 = 7*256 + 5
  37. *
  38. * For more details, please, read /usr/include/linux/version.h
  39. * in any Red Hat installation.
  40. */
  41. #define NETDATA_MINIMUM_RH_VERSION 1797
  42. /**
  43. * 2048 = 8*256 + 0
  44. */
  45. #define NETDATA_RH_8 2048
  46. /**
  47. * Kernel Version
  48. *
  49. * Kernel versions are calculated using the following formula:
  50. *
  51. * VERSION = LINUX_VERSION_MAJOR*65536 + LINUX_VERSION_PATCHLEVEL*256 + LINUX_VERSION_SUBLEVEL
  52. *
  53. * Where LINUX_VERSION_MAJOR, LINUX_VERSION_PATCHLEVEL, and LINUX_VERSION_SUBLEVEL are extracted
  54. * from /usr/include/linux/version.h.
  55. *
  56. * LINUX_VERSION_SUBLEVEL has the maximum value 255, but linux can have more SUBLEVELS.
  57. *
  58. */
  59. enum netdata_ebpf_kernel_versions {
  60. NETDATA_EBPF_KERNEL_4_11 = 264960, // 264960 = 4 * 65536 + 15 * 256
  61. NETDATA_EBPF_KERNEL_4_15 = 265984, // 265984 = 4 * 65536 + 15 * 256
  62. NETDATA_EBPF_KERNEL_4_17 = 266496, // 266496 = 4 * 65536 + 17 * 256
  63. NETDATA_EBPF_KERNEL_5_0 = 327680, // 327680 = 5 * 65536 + 0 * 256
  64. NETDATA_EBPF_KERNEL_5_10 = 330240, // 330240 = 5 * 65536 + 10 * 256
  65. NETDATA_EBPF_KERNEL_5_11 = 330496, // 330240 = 5 * 65536 + 11 * 256
  66. NETDATA_EBPF_KERNEL_5_15 = 331520, // 331520 = 5 * 65536 + 15 * 256
  67. NETDATA_EBPF_KERNEL_5_16 = 331776 // 331776 = 5 * 65536 + 16 * 256
  68. };
  69. enum netdata_kernel_flag {
  70. NETDATA_V3_10 = 1 << 0,
  71. NETDATA_V4_14 = 1 << 1,
  72. NETDATA_V4_16 = 1 << 2,
  73. NETDATA_V4_18 = 1 << 3,
  74. NETDATA_V5_4 = 1 << 4,
  75. NETDATA_V5_10 = 1 << 5,
  76. NETDATA_V5_11 = 1 << 6,
  77. NETDATA_V5_15 = 1 << 7,
  78. NETDATA_V5_16 = 1 << 8
  79. };
  80. enum netdata_kernel_idx {
  81. NETDATA_IDX_V3_10,
  82. NETDATA_IDX_V4_14,
  83. NETDATA_IDX_V4_16,
  84. NETDATA_IDX_V4_18,
  85. NETDATA_IDX_V5_4 ,
  86. NETDATA_IDX_V5_10,
  87. NETDATA_IDX_V5_11,
  88. NETDATA_IDX_V5_15,
  89. NETDATA_IDX_V5_16
  90. };
  91. #define NETDATA_IDX_STR_V3_10 "3.10"
  92. #define NETDATA_IDX_STR_V4_14 "4.14"
  93. #define NETDATA_IDX_STR_V4_16 "4.16"
  94. #define NETDATA_IDX_STR_V4_18 "4.18"
  95. #define NETDATA_IDX_STR_V5_4 "5.4"
  96. #define NETDATA_IDX_STR_V5_10 "5.10"
  97. #define NETDATA_IDX_STR_V5_11 "5.11"
  98. #define NETDATA_IDX_STR_V5_15 "5.15"
  99. #define NETDATA_IDX_STR_V5_16 "5.16"
  100. /**
  101. * Minimum value has relationship with libbpf support.
  102. */
  103. #define NETDATA_MINIMUM_EBPF_KERNEL NETDATA_EBPF_KERNEL_4_11
  104. #define VERSION_STRING_LEN 256
  105. #define EBPF_KERNEL_REJECT_LIST_FILE "ebpf_kernel_reject_list.txt"
  106. #define ND_EBPF_DEFAULT_MIN_PID 1U
  107. #define ND_EBPF_MAP_FD_NOT_INITIALIZED (int)-1
  108. typedef struct ebpf_addresses {
  109. char *function;
  110. uint32_t hash;
  111. // We use long as address, because it matches system length
  112. unsigned long addr;
  113. } ebpf_addresses_t;
  114. extern char *ebpf_user_config_dir;
  115. extern char *ebpf_stock_config_dir;
  116. typedef struct ebpf_data {
  117. int *map_fd;
  118. char *kernel_string;
  119. uint32_t running_on_kernel;
  120. int isrh;
  121. } ebpf_data_t;
  122. typedef enum {
  123. MODE_RETURN = 0, // This attaches kprobe when the function returns
  124. MODE_DEVMODE, // This stores log given description about the errors raised
  125. MODE_ENTRY // This attaches kprobe when the function is called
  126. } netdata_run_mode_t;
  127. #define ND_EBPF_DEFAULT_PID_SIZE 32768U
  128. enum netdata_ebpf_map_type {
  129. NETDATA_EBPF_MAP_STATIC = 0,
  130. NETDATA_EBPF_MAP_RESIZABLE = 1,
  131. NETDATA_EBPF_MAP_CONTROLLER = 2,
  132. NETDATA_EBPF_MAP_CONTROLLER_UPDATED = 4,
  133. NETDATA_EBPF_MAP_PID = 8
  134. };
  135. enum netdata_controller {
  136. NETDATA_CONTROLLER_APPS_ENABLED,
  137. NETDATA_CONTROLLER_END
  138. };
  139. typedef struct ebpf_local_maps {
  140. char *name;
  141. uint32_t internal_input;
  142. uint32_t user_input;
  143. uint32_t type;
  144. int map_fd;
  145. } ebpf_local_maps_t;
  146. typedef struct ebpf_specify_name {
  147. char *program_name;
  148. char *function_to_attach;
  149. char *optional;
  150. bool retprobe;
  151. } ebpf_specify_name_t;
  152. typedef enum netdata_ebpf_load_mode {
  153. EBPF_LOAD_LEGACY, // Select legacy mode, this means we will load binaries
  154. EBPF_LOAD_CORE, // When CO-RE is used, it is necessary to use the souce code
  155. EBPF_LOAD_PLAY_DICE // Take a look on environment and choose the best option
  156. } netdata_ebpf_load_mode_t;
  157. typedef enum netdata_ebpf_program_loaded {
  158. EBPF_LOAD_PROBE, // Attach probes on targets
  159. EBPF_LOAD_RETPROBE, // Attach retprobes on targets
  160. EBPF_LOAD_TRACEPOINT, // This stores log given description about the errors raised
  161. EBPF_LOAD_TRAMPOLINE, // This attaches kprobe when the function is called
  162. } netdata_ebpf_program_loaded_t;
  163. typedef struct netdata_ebpf_targets {
  164. char *name;
  165. netdata_ebpf_program_loaded_t mode;
  166. } netdata_ebpf_targets_t;
  167. typedef struct ebpf_plugin_stats {
  168. // Load options
  169. uint32_t legacy; // Legacy codes
  170. uint32_t core; // CO-RE codes, this means we are using source code compiled.
  171. uint32_t threads; // Total number of threads
  172. uint32_t running; // total number of threads running
  173. uint32_t probes; // Number of kprobes loaded
  174. uint32_t retprobes; // Number of kretprobes loaded
  175. uint32_t tracepoints; // Number of tracepoints used
  176. uint32_t trampolines; // Number of trampolines used
  177. } ebpf_plugin_stats_t;
  178. typedef struct ebpf_module {
  179. const char *thread_name;
  180. const char *config_name;
  181. int enabled;
  182. void *(*start_routine)(void *);
  183. int update_every;
  184. int global_charts;
  185. int apps_charts;
  186. int cgroup_charts;
  187. netdata_run_mode_t mode;
  188. uint32_t thread_id;
  189. int optional;
  190. void (*apps_routine)(struct ebpf_module *em, void *ptr);
  191. ebpf_local_maps_t *maps;
  192. ebpf_specify_name_t *names;
  193. uint32_t pid_map_size;
  194. struct config *cfg;
  195. const char *config_file;
  196. uint64_t kernels;
  197. netdata_ebpf_load_mode_t load;
  198. netdata_ebpf_targets_t *targets;
  199. } ebpf_module_t;
  200. extern int ebpf_get_kernel_version();
  201. extern int get_redhat_release();
  202. extern int has_condition_to_run(int version);
  203. extern char *ebpf_kernel_suffix(int version, int isrh);
  204. extern struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, int kver, int is_rhf,
  205. struct bpf_object **obj);
  206. extern void ebpf_mount_config_name(char *filename, size_t length, char *path, const char *config);
  207. extern int ebpf_load_config(struct config *config, char *filename);
  208. extern void ebpf_update_module(ebpf_module_t *em);
  209. extern void ebpf_update_names(ebpf_specify_name_t *opt, ebpf_module_t *em);
  210. extern char *ebpf_find_symbol(char *search);
  211. extern void ebpf_load_addresses(ebpf_addresses_t *fa, int fd);
  212. extern void ebpf_fill_algorithms(int *algorithms, size_t length, int algorithm);
  213. extern char **ebpf_fill_histogram_dimension(size_t maximum);
  214. extern void ebpf_update_stats(ebpf_plugin_stats_t *report, ebpf_module_t *em);
  215. extern void ebpf_update_controller(int fd, ebpf_module_t *em);
  216. extern void ebpf_update_map_size(struct bpf_map *map, ebpf_local_maps_t *lmap, ebpf_module_t *em, const char *map_name);
  217. // Histogram
  218. #define NETDATA_EBPF_HIST_MAX_BINS 24UL
  219. #define NETDATA_DISK_MAX 256U
  220. #define NETDATA_DISK_HISTOGRAM_LENGTH (NETDATA_DISK_MAX * NETDATA_EBPF_HIST_MAX_BINS)
  221. typedef struct netdata_ebpf_histogram {
  222. char *name;
  223. char *title;
  224. int order;
  225. uint64_t histogram[NETDATA_EBPF_HIST_MAX_BINS];
  226. } netdata_ebpf_histogram_t;
  227. extern void ebpf_histogram_dimension_cleanup(char **ptr, size_t length);
  228. // Tracepoint helpers
  229. // For more information related to tracepoints read https://www.kernel.org/doc/html/latest/trace/tracepoints.html
  230. extern int ebpf_is_tracepoint_enabled(char *subsys, char *eventname);
  231. extern int ebpf_enable_tracing_values(char *subsys, char *eventname);
  232. extern int ebpf_disable_tracing_values(char *subsys, char *eventname);
  233. // BTF Section
  234. #define EBPF_DEFAULT_BTF_FILE "vmlinux"
  235. #define EBPF_DEFAULT_BTF_PATH "/sys/kernel/btf"
  236. #define EBPF_DEFAULT_ERROR_MSG "Cannot open or load BPF file for thread"
  237. // BTF helpers
  238. #define NETDATA_EBPF_MAX_SYSCALL_LENGTH 255
  239. extern netdata_ebpf_load_mode_t epbf_convert_string_to_load_mode(char *str);
  240. extern netdata_ebpf_program_loaded_t ebpf_convert_core_type(char *str, netdata_run_mode_t lmode);
  241. extern void ebpf_select_host_prefix(char *output, size_t length, char *syscall, int kver);
  242. #ifdef LIBBPF_MAJOR_VERSION
  243. extern void ebpf_adjust_thread_load(ebpf_module_t *mod, struct btf *file);
  244. extern struct btf *ebpf_parse_btf_file(const char *filename);
  245. extern struct btf *ebpf_load_btf_file(char *path, char *filename);
  246. extern int ebpf_is_function_inside_btf(struct btf *file, char *function);
  247. #endif
  248. #endif /* NETDATA_EBPF_H */