ebpf.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "daemon/main.h"
  27. #include "ebpf_apps.h"
  28. typedef struct netdata_syscall_stat {
  29. unsigned long bytes; // total number of bytes
  30. uint64_t call; // total number of calls
  31. uint64_t ecall; // number of calls that returned error
  32. struct netdata_syscall_stat *next; // Link list
  33. } netdata_syscall_stat_t;
  34. typedef uint64_t netdata_idx_t;
  35. typedef struct netdata_publish_syscall {
  36. char *dimension;
  37. char *name;
  38. unsigned long nbyte;
  39. unsigned long pbyte;
  40. uint64_t ncall;
  41. uint64_t pcall;
  42. uint64_t nerr;
  43. uint64_t perr;
  44. struct netdata_publish_syscall *next;
  45. } netdata_publish_syscall_t;
  46. typedef struct netdata_publish_vfs_common {
  47. long write;
  48. long read;
  49. long running;
  50. long zombie;
  51. } netdata_publish_vfs_common_t;
  52. typedef struct netdata_error_report {
  53. char comm[16];
  54. __u32 pid;
  55. int type;
  56. int err;
  57. } netdata_error_report_t;
  58. extern ebpf_module_t ebpf_modules[];
  59. #define EBPF_MODULE_PROCESS_IDX 0
  60. #define EBPF_MODULE_SOCKET_IDX 1
  61. // Copied from musl header
  62. #ifndef offsetof
  63. #if __GNUC__ > 3
  64. #define offsetof(type, member) __builtin_offsetof(type, member)
  65. #else
  66. #define offsetof(type, member) ((size_t)((char *)&(((type *)0)->member) - (char *)0))
  67. #endif
  68. #endif
  69. // Chart defintions
  70. #define NETDATA_EBPF_FAMILY "ebpf"
  71. // Log file
  72. #define NETDATA_DEVELOPER_LOG_FILE "developer.log"
  73. // Maximum number of processors monitored on perf events
  74. #define NETDATA_MAX_PROCESSOR 512
  75. // Kernel versions calculated with the formula:
  76. // R = MAJOR*65536 + MINOR*256 + PATCH
  77. #define NETDATA_KERNEL_V5_3 328448
  78. #define NETDATA_KERNEL_V4_15 265984
  79. #define EBPF_SYS_CLONE_IDX 11
  80. #define EBPF_MAX_MAPS 32
  81. // Threads
  82. extern void *ebpf_process_thread(void *ptr);
  83. extern void *ebpf_socket_thread(void *ptr);
  84. // Common variables
  85. extern pthread_mutex_t lock;
  86. extern int close_ebpf_plugin;
  87. extern int ebpf_nprocs;
  88. extern int running_on_kernel;
  89. extern char *ebpf_plugin_dir;
  90. extern char kernel_string[64];
  91. extern pthread_mutex_t collect_data_mutex;
  92. extern pthread_cond_t collect_data_cond_var;
  93. // Common functions
  94. extern void ebpf_global_labels(netdata_syscall_stat_t *is,
  95. netdata_publish_syscall_t *pio,
  96. char **dim,
  97. char **name,
  98. int end);
  99. extern void ebpf_write_chart_cmd(char *type,
  100. char *id,
  101. char *title,
  102. char *units,
  103. char *family,
  104. char *charttype,
  105. int order);
  106. extern void ebpf_write_global_dimension(char *n, char *d);
  107. extern void ebpf_create_global_dimension(void *ptr, int end);
  108. extern void ebpf_create_chart(char *type,
  109. char *id,
  110. char *title,
  111. char *units,
  112. char *family,
  113. int order,
  114. void (*ncd)(void *, int),
  115. void *move,
  116. int end);
  117. extern void write_begin_chart(char *family, char *name);
  118. extern void write_chart_dimension(char *dim, long long value);
  119. extern void write_count_chart(char *name, char *family, netdata_publish_syscall_t *move, uint32_t end);
  120. extern void write_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end);
  121. extern void write_io_chart(char *chart, char *family, char *dwrite, char *dread, netdata_publish_vfs_common_t *pvc);
  122. extern void fill_ebpf_data(ebpf_data_t *ef);
  123. extern void ebpf_create_charts_on_apps(char *name,
  124. char *title,
  125. char *units,
  126. char *family,
  127. int order,
  128. struct target *root);
  129. extern void write_end_chart();
  130. #define EBPF_GLOBAL_SECTION "global"
  131. #define EBPF_PROGRAMS_SECTION "ebpf programs"
  132. #define EBPF_NETWORK_VIEWER_SECTION "network connections"
  133. #define EBPF_SERVICE_NAME_SECTION "service name"
  134. #define EBPF_COMMON_DIMENSION_CALL "calls/s"
  135. #define EBPF_COMMON_DIMENSION_BYTESS "bytes/s"
  136. #define EBPF_COMMON_DIMENSION_DIFFERENCE "difference"
  137. #define EBPF_COMMON_DIMENSION_PACKETS "packets"
  138. // Common variables
  139. extern char *ebpf_user_config_dir;
  140. extern char *ebpf_stock_config_dir;
  141. extern int debug_enabled;
  142. extern struct pid_stat *root_of_pids;
  143. // Socket functions and variables
  144. // Common functions
  145. extern void ebpf_socket_create_apps_charts(ebpf_module_t *em, struct target *root);
  146. extern collected_number get_value_from_structure(char *basis, size_t offset);
  147. extern struct pid_stat *root_of_pids;
  148. extern ebpf_process_stat_t *global_process_stat;
  149. extern size_t all_pids_count;
  150. extern int update_every;
  151. extern uint32_t finalized_threads;
  152. #define EBPF_MAX_SYNCHRONIZATION_TIME 300
  153. #endif /* NETDATA_COLLECTOR_EBPF_H */