ebpf.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_COLLECTOR_EBPF_H
  3. #define NETDATA_COLLECTOR_EBPF_H 1
  4. #ifndef __FreeBSD__
  5. #include <linux/perf_event.h>
  6. #endif
  7. #include <stdint.h>
  8. #include <errno.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <dlfcn.h>
  15. #include <fcntl.h>
  16. #include <ctype.h>
  17. #include <dirent.h>
  18. // From libnetdata.h
  19. #include "libnetdata/threads/threads.h"
  20. #include "libnetdata/locks/locks.h"
  21. #include "libnetdata/avl/avl.h"
  22. #include "libnetdata/clocks/clocks.h"
  23. #include "libnetdata/config/appconfig.h"
  24. #include "libnetdata/ebpf/ebpf.h"
  25. #include "libnetdata/procfile/procfile.h"
  26. #include "collectors/cgroups.plugin/sys_fs_cgroup.h"
  27. #include "daemon/main.h"
  28. #include "ebpf_apps.h"
  29. #include "ebpf_functions.h"
  30. #include "ebpf_cgroup.h"
  31. #define NETDATA_EBPF_OLD_CONFIG_FILE "ebpf.conf"
  32. #define NETDATA_EBPF_CONFIG_FILE "ebpf.d.conf"
  33. #ifdef LIBBPF_MAJOR_VERSION // BTF code
  34. #include "includes/cachestat.skel.h"
  35. #include "includes/dc.skel.h"
  36. #include "includes/disk.skel.h"
  37. #include "includes/fd.skel.h"
  38. #include "includes/hardirq.skel.h"
  39. #include "includes/mdflush.skel.h"
  40. #include "includes/mount.skel.h"
  41. #include "includes/shm.skel.h"
  42. #include "includes/socket.skel.h"
  43. #include "includes/swap.skel.h"
  44. #include "includes/vfs.skel.h"
  45. extern struct cachestat_bpf *cachestat_bpf_obj;
  46. extern struct dc_bpf *dc_bpf_obj;
  47. extern struct disk_bpf *disk_bpf_obj;
  48. extern struct fd_bpf *fd_bpf_obj;
  49. extern struct hardirq_bpf *hardirq_bpf_obj;
  50. extern struct mount_bpf *mount_bpf_obj;
  51. extern struct mdflush_bpf *mdflush_bpf_obj;
  52. extern struct shm_bpf *shm_bpf_obj;
  53. extern struct socket_bpf *socket_bpf_obj;
  54. extern struct swap_bpf *bpf_obj;
  55. extern struct vfs_bpf *vfs_bpf_obj;
  56. #endif
  57. typedef struct netdata_syscall_stat {
  58. unsigned long bytes; // total number of bytes
  59. uint64_t call; // total number of calls
  60. uint64_t ecall; // number of calls that returned error
  61. struct netdata_syscall_stat *next; // Link list
  62. } netdata_syscall_stat_t;
  63. typedef struct netdata_publish_syscall {
  64. char *dimension;
  65. char *name;
  66. char *algorithm;
  67. unsigned long nbyte;
  68. unsigned long pbyte;
  69. uint64_t ncall;
  70. uint64_t pcall;
  71. uint64_t nerr;
  72. uint64_t perr;
  73. struct netdata_publish_syscall *next;
  74. } netdata_publish_syscall_t;
  75. typedef struct netdata_publish_vfs_common {
  76. long write;
  77. long read;
  78. long running;
  79. long zombie;
  80. } netdata_publish_vfs_common_t;
  81. typedef struct netdata_error_report {
  82. char comm[16];
  83. __u32 pid;
  84. int type;
  85. int err;
  86. } netdata_error_report_t;
  87. typedef struct netdata_ebpf_judy_pid {
  88. ARAL *pid_table;
  89. // Index for PIDs
  90. struct { // support for multiple indexing engines
  91. Pvoid_t JudyLArray; // the hash table
  92. RW_SPINLOCK rw_spinlock; // protect the index
  93. } index;
  94. } netdata_ebpf_judy_pid_t;
  95. typedef struct netdata_ebpf_judy_pid_stats {
  96. char *cmdline;
  97. // Index for Socket timestamp
  98. struct { // support for multiple indexing engines
  99. Pvoid_t JudyLArray; // the hash table
  100. RW_SPINLOCK rw_spinlock; // protect the index
  101. } socket_stats;
  102. } netdata_ebpf_judy_pid_stats_t;
  103. extern ebpf_module_t ebpf_modules[];
  104. enum ebpf_main_index {
  105. EBPF_MODULE_PROCESS_IDX,
  106. EBPF_MODULE_SOCKET_IDX,
  107. EBPF_MODULE_CACHESTAT_IDX,
  108. EBPF_MODULE_SYNC_IDX,
  109. EBPF_MODULE_DCSTAT_IDX,
  110. EBPF_MODULE_SWAP_IDX,
  111. EBPF_MODULE_VFS_IDX,
  112. EBPF_MODULE_FILESYSTEM_IDX,
  113. EBPF_MODULE_DISK_IDX,
  114. EBPF_MODULE_MOUNT_IDX,
  115. EBPF_MODULE_FD_IDX,
  116. EBPF_MODULE_HARDIRQ_IDX,
  117. EBPF_MODULE_SOFTIRQ_IDX,
  118. EBPF_MODULE_OOMKILL_IDX,
  119. EBPF_MODULE_SHM_IDX,
  120. EBPF_MODULE_MDFLUSH_IDX,
  121. EBPF_MODULE_FUNCTION_IDX,
  122. /* THREADS MUST BE INCLUDED BEFORE THIS COMMENT */
  123. EBPF_OPTION_ALL_CHARTS,
  124. EBPF_OPTION_VERSION,
  125. EBPF_OPTION_HELP,
  126. EBPF_OPTION_GLOBAL_CHART,
  127. EBPF_OPTION_RETURN_MODE,
  128. EBPF_OPTION_LEGACY,
  129. EBPF_OPTION_CORE,
  130. EBPF_OPTION_UNITTEST
  131. };
  132. typedef struct ebpf_tracepoint {
  133. bool enabled;
  134. char *class;
  135. char *event;
  136. } ebpf_tracepoint_t;
  137. // Copied from musl header
  138. #ifndef offsetof
  139. #if __GNUC__ > 3
  140. #define offsetof(type, member) __builtin_offsetof(type, member)
  141. #else
  142. #define offsetof(type, member) ((size_t)((char *)&(((type *)0)->member) - (char *)0))
  143. #endif
  144. #endif
  145. // Messages
  146. #define NETDATA_EBPF_DEFAULT_FNT_NOT_FOUND "Cannot find the necessary functions to monitor"
  147. // Chart definitions
  148. #define NETDATA_EBPF_FAMILY "ebpf"
  149. #define NETDATA_EBPF_IP_FAMILY "ip"
  150. #define NETDATA_FILESYSTEM_FAMILY "filesystem"
  151. #define NETDATA_EBPF_MOUNT_GLOBAL_FAMILY "mount_points"
  152. #define NETDATA_EBPF_CHART_TYPE_LINE "line"
  153. #define NETDATA_EBPF_CHART_TYPE_STACKED "stacked"
  154. #define NETDATA_EBPF_MEMORY_GROUP "mem"
  155. #define NETDATA_EBPF_SYSTEM_GROUP "system"
  156. #define NETDATA_SYSTEM_SWAP_SUBMENU "swap"
  157. #define NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU "swap (eBPF)"
  158. #define NETDATA_SYSTEM_IPC_SHM_SUBMENU "ipc shared memory"
  159. #define NETDATA_MONITORING_FAMILY "netdata"
  160. // Statistics charts
  161. #define NETDATA_EBPF_THREADS "ebpf_threads"
  162. #define NETDATA_EBPF_LIFE_TIME "ebpf_life_time"
  163. #define NETDATA_EBPF_LOAD_METHOD "ebpf_load_methods"
  164. #define NETDATA_EBPF_KERNEL_MEMORY "ebpf_kernel_memory"
  165. #define NETDATA_EBPF_HASH_TABLES_LOADED "ebpf_hash_tables_count"
  166. #define NETDATA_EBPF_HASH_TABLES_PER_CORE "ebpf_hash_tables_per_core"
  167. #define NETDATA_EBPF_HASH_TABLES_GLOBAL_ELEMENTS "ebpf_hash_tables_global_elements"
  168. #define NETDATA_EBPF_HASH_TABLES_INSERT_PID_ELEMENTS "ebpf_hash_tables_insert_pid_elements"
  169. #define NETDATA_EBPF_HASH_TABLES_REMOVE_PID_ELEMENTS "ebpf_hash_tables_remove_pid_elements"
  170. // Log file
  171. #define NETDATA_DEVELOPER_LOG_FILE "developer.log"
  172. // Maximum number of processors monitored on perf events
  173. #define NETDATA_MAX_PROCESSOR 512
  174. // Kernel versions calculated with the formula:
  175. // R = MAJOR*65536 + MINOR*256 + PATCH
  176. #define NETDATA_KERNEL_V5_3 328448
  177. #define NETDATA_KERNEL_V4_15 265984
  178. #define EBPF_SYS_CLONE_IDX 11
  179. #define EBPF_MAX_MAPS 32
  180. #define EBPF_DEFAULT_UPDATE_EVERY 10
  181. enum ebpf_algorithms_list {
  182. NETDATA_EBPF_ABSOLUTE_IDX,
  183. NETDATA_EBPF_INCREMENTAL_IDX
  184. };
  185. // Threads
  186. void *ebpf_process_thread(void *ptr);
  187. void *ebpf_socket_thread(void *ptr);
  188. // Common variables
  189. extern pthread_mutex_t lock;
  190. extern pthread_mutex_t ebpf_exit_cleanup;
  191. extern int ebpf_nprocs;
  192. extern int running_on_kernel;
  193. extern int isrh;
  194. extern char *ebpf_plugin_dir;
  195. extern int process_pid_fd;
  196. extern pthread_mutex_t collect_data_mutex;
  197. // Common functions
  198. void ebpf_global_labels(netdata_syscall_stat_t *is,
  199. netdata_publish_syscall_t *pio,
  200. char **dim,
  201. char **name,
  202. int *algorithm,
  203. int end);
  204. void ebpf_write_chart_cmd(char *type,
  205. char *id,
  206. char *suffix,
  207. char *title,
  208. char *units,
  209. char *family,
  210. char *charttype,
  211. char *context,
  212. int order,
  213. int update_every,
  214. char *module);
  215. void ebpf_write_global_dimension(char *name, char *id, char *algorithm);
  216. void ebpf_create_global_dimension(void *ptr, int end);
  217. void ebpf_create_chart(char *type,
  218. char *id,
  219. char *title,
  220. char *units,
  221. char *family,
  222. char *context,
  223. char *charttype,
  224. int order,
  225. void (*ncd)(void *, int),
  226. void *move,
  227. int end,
  228. int update_every,
  229. char *module);
  230. void write_chart_dimension(char *dim, long long value);
  231. void write_count_chart(char *name, char *family, netdata_publish_syscall_t *move, uint32_t end);
  232. void write_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end);
  233. void write_io_chart(char *chart, char *family, char *dwrite, long long vwrite,
  234. char *dread, long long vread);
  235. /**
  236. * Create Chart labels
  237. *
  238. * @param name the label name.
  239. * @param value the label value.
  240. * @param origin the labeel source.
  241. */
  242. static inline void ebpf_create_chart_labels(char *name, char *value, int source)
  243. {
  244. fprintf(stdout, "CLABEL '%s' '%s' %d\n", name, value, source);
  245. }
  246. /**
  247. * Commit label
  248. *
  249. * Write commit label to stdout
  250. */
  251. static inline void ebpf_commit_label()
  252. {
  253. fprintf(stdout, "CLABEL_COMMIT\n");
  254. }
  255. /**
  256. * Write begin command on standard output
  257. *
  258. * @param family the chart family name
  259. * @param name the chart name
  260. * @param metric the chart suffix (used with apps and cgroups)
  261. */
  262. static inline void ebpf_write_begin_chart(char *family, char *name, char *metric)
  263. {
  264. printf("BEGIN %s.%s%s\n", family, name, metric);
  265. }
  266. /**
  267. * Write END command on stdout.
  268. */
  269. static inline void ebpf_write_end_chart()
  270. {
  271. printf("END\n");
  272. }
  273. int ebpf_enable_tracepoint(ebpf_tracepoint_t *tp);
  274. int ebpf_disable_tracepoint(ebpf_tracepoint_t *tp);
  275. uint32_t ebpf_enable_tracepoints(ebpf_tracepoint_t *tps);
  276. void ebpf_pid_file(char *filename, size_t length);
  277. #define EBPF_PROGRAMS_SECTION "ebpf programs"
  278. #define EBPF_COMMON_DIMENSION_PERCENTAGE "%"
  279. #define EBPF_PROGRAMS_SECTION "ebpf programs"
  280. #define EBPF_COMMON_DIMENSION_PERCENTAGE "%"
  281. #define EBPF_COMMON_DIMENSION_CALL "calls/s"
  282. #define EBPF_COMMON_DIMENSION_CONNECTIONS "connections/s"
  283. #define EBPF_COMMON_DIMENSION_BITS "kilobits/s"
  284. #define EBPF_COMMON_DIMENSION_BYTES "bytes/s"
  285. #define EBPF_COMMON_DIMENSION_DIFFERENCE "difference"
  286. #define EBPF_COMMON_DIMENSION_PACKETS "packets"
  287. #define EBPF_COMMON_DIMENSION_FILES "files"
  288. #define EBPF_COMMON_DIMENSION_MILLISECONDS "milliseconds"
  289. #define EBPF_COMMON_DIMENSION_KILLS "kills"
  290. // Common variables
  291. extern int debug_enabled;
  292. extern struct ebpf_pid_stat *ebpf_root_of_pids;
  293. extern ebpf_cgroup_target_t *ebpf_cgroup_pids;
  294. extern char *ebpf_algorithms[];
  295. extern struct config collector_config;
  296. extern netdata_ebpf_cgroup_shm_t shm_ebpf_cgroup;
  297. extern int shm_fd_ebpf_cgroup;
  298. extern sem_t *shm_sem_ebpf_cgroup;
  299. extern pthread_mutex_t mutex_cgroup_shm;
  300. extern size_t ebpf_all_pids_count;
  301. extern ebpf_plugin_stats_t plugin_statistics;
  302. #ifdef LIBBPF_MAJOR_VERSION
  303. extern struct btf *default_btf;
  304. #else
  305. extern void *default_btf;
  306. #endif
  307. // Socket functions and variables
  308. // Common functions
  309. void ebpf_process_create_apps_charts(struct ebpf_module *em, void *ptr);
  310. void ebpf_socket_create_apps_charts(struct ebpf_module *em, void *ptr);
  311. void ebpf_cachestat_create_apps_charts(struct ebpf_module *em, void *root);
  312. void ebpf_one_dimension_write_charts(char *family, char *chart, char *dim, long long v1);
  313. collected_number get_value_from_structure(char *basis, size_t offset);
  314. void ebpf_update_pid_table(ebpf_local_maps_t *pid, ebpf_module_t *em);
  315. void ebpf_write_chart_obsolete(char *type, char *id, char *suffix, char *title, char *units, char *family,
  316. char *charttype, char *context, int order, int update_every);
  317. void write_histogram_chart(char *family, char *name, const netdata_idx_t *hist, char **dimensions, uint32_t end);
  318. void ebpf_update_disabled_plugin_stats(ebpf_module_t *em);
  319. ARAL *ebpf_allocate_pid_aral(char *name, size_t size);
  320. void ebpf_unload_legacy_code(struct bpf_object *objects, struct bpf_link **probe_links);
  321. void ebpf_read_global_table_stats(netdata_idx_t *stats, netdata_idx_t *values, int map_fd,
  322. int maps_per_core, uint32_t begin, uint32_t end);
  323. void **ebpf_judy_insert_unsafe(PPvoid_t arr, Word_t key);
  324. netdata_ebpf_judy_pid_stats_t *ebpf_get_pid_from_judy_unsafe(PPvoid_t judy_array, uint32_t pid);
  325. void parse_network_viewer_section(struct config *cfg);
  326. void ebpf_clean_ip_structure(ebpf_network_viewer_ip_list_t **clean);
  327. void ebpf_clean_port_structure(ebpf_network_viewer_port_list_t **clean);
  328. void ebpf_read_local_addresses_unsafe();
  329. extern ebpf_filesystem_partitions_t localfs[];
  330. extern ebpf_sync_syscalls_t local_syscalls[];
  331. extern bool ebpf_plugin_exit;
  332. void ebpf_stop_threads(int sig);
  333. extern netdata_ebpf_judy_pid_t ebpf_judy_pid;
  334. #define EBPF_MAX_SYNCHRONIZATION_TIME 300
  335. #endif /* NETDATA_COLLECTOR_EBPF_H */