ebpf.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/time.h>
  3. #include <sys/resource.h>
  4. #include <ifaddrs.h>
  5. #include "ebpf.h"
  6. #include "ebpf_socket.h"
  7. #include "libnetdata/required_dummies.h"
  8. /*****************************************************************
  9. *
  10. * GLOBAL VARIABLES
  11. *
  12. *****************************************************************/
  13. char *ebpf_plugin_dir = PLUGINS_DIR;
  14. static char *ebpf_configured_log_dir = LOG_DIR;
  15. char *ebpf_algorithms[] = {"absolute", "incremental"};
  16. static int thread_finished = 0;
  17. int close_ebpf_plugin = 0;
  18. struct config collector_config = { .first_section = NULL,
  19. .last_section = NULL,
  20. .mutex = NETDATA_MUTEX_INITIALIZER,
  21. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  22. .rwlock = AVL_LOCK_INITIALIZER } };
  23. int running_on_kernel = 0;
  24. int ebpf_nprocs;
  25. int isrh = 0;
  26. uint32_t finalized_threads = 1;
  27. pthread_mutex_t lock;
  28. pthread_mutex_t collect_data_mutex;
  29. pthread_cond_t collect_data_cond_var;
  30. ebpf_module_t ebpf_modules[] = {
  31. { .thread_name = "process", .config_name = "process", .enabled = 0, .start_routine = ebpf_process_thread,
  32. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  33. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  34. .apps_routine = ebpf_process_create_apps_charts, .maps = NULL,
  35. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &process_config,
  36. .config_file = NETDATA_PROCESS_CONFIG_FILE,
  37. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_10,
  38. .load = EBPF_LOAD_LEGACY, .targets = NULL},
  39. { .thread_name = "socket", .config_name = "socket", .enabled = 0, .start_routine = ebpf_socket_thread,
  40. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  41. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  42. .apps_routine = ebpf_socket_create_apps_charts, .maps = NULL,
  43. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &socket_config,
  44. .config_file = NETDATA_NETWORK_CONFIG_FILE,
  45. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  46. .load = EBPF_LOAD_LEGACY, .targets = socket_targets},
  47. { .thread_name = "cachestat", .config_name = "cachestat", .enabled = 0, .start_routine = ebpf_cachestat_thread,
  48. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  49. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  50. .apps_routine = ebpf_cachestat_create_apps_charts, .maps = NULL,
  51. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &cachestat_config,
  52. .config_file = NETDATA_CACHESTAT_CONFIG_FILE,
  53. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18|
  54. NETDATA_V5_4 | NETDATA_V5_15 | NETDATA_V5_16,
  55. .load = EBPF_LOAD_LEGACY, .targets = cachestat_targets},
  56. { .thread_name = "sync", .config_name = "sync", .enabled = 0, .start_routine = ebpf_sync_thread,
  57. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  58. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  59. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &sync_config,
  60. .config_file = NETDATA_SYNC_CONFIG_FILE,
  61. // All syscalls have the same kernels
  62. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  63. .load = EBPF_LOAD_LEGACY, .targets = sync_targets},
  64. { .thread_name = "dc", .config_name = "dc", .enabled = 0, .start_routine = ebpf_dcstat_thread,
  65. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  66. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  67. .apps_routine = ebpf_dcstat_create_apps_charts, .maps = NULL,
  68. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &dcstat_config,
  69. .config_file = NETDATA_DIRECTORY_DCSTAT_CONFIG_FILE,
  70. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  71. .load = EBPF_LOAD_LEGACY, .targets = dc_targets},
  72. { .thread_name = "swap", .config_name = "swap", .enabled = 0, .start_routine = ebpf_swap_thread,
  73. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  74. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  75. .apps_routine = ebpf_swap_create_apps_charts, .maps = NULL,
  76. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &swap_config,
  77. .config_file = NETDATA_DIRECTORY_SWAP_CONFIG_FILE,
  78. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  79. .load = EBPF_LOAD_LEGACY, .targets = swap_targets},
  80. { .thread_name = "vfs", .config_name = "vfs", .enabled = 0, .start_routine = ebpf_vfs_thread,
  81. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  82. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  83. .apps_routine = ebpf_vfs_create_apps_charts, .maps = NULL,
  84. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &vfs_config,
  85. .config_file = NETDATA_DIRECTORY_VFS_CONFIG_FILE,
  86. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  87. .load = EBPF_LOAD_LEGACY, .targets = NULL},
  88. { .thread_name = "filesystem", .config_name = "filesystem", .enabled = 0, .start_routine = ebpf_filesystem_thread,
  89. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  90. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  91. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fs_config,
  92. .config_file = NETDATA_FILESYSTEM_CONFIG_FILE,
  93. //We are setting kernels as zero, because we load eBPF programs according the kernel running.
  94. .kernels = 0, .load = EBPF_LOAD_LEGACY, .targets = NULL },
  95. { .thread_name = "disk", .config_name = "disk", .enabled = 0, .start_routine = ebpf_disk_thread,
  96. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  97. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  98. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &disk_config,
  99. .config_file = NETDATA_DISK_CONFIG_FILE,
  100. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  101. .load = EBPF_LOAD_LEGACY, .targets = NULL},
  102. { .thread_name = "mount", .config_name = "mount", .enabled = 0, .start_routine = ebpf_mount_thread,
  103. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  104. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  105. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &mount_config,
  106. .config_file = NETDATA_MOUNT_CONFIG_FILE,
  107. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  108. .load = EBPF_LOAD_LEGACY, .targets = mount_targets},
  109. { .thread_name = "fd", .config_name = "fd", .enabled = 0, .start_routine = ebpf_fd_thread,
  110. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  111. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  112. .apps_routine = ebpf_fd_create_apps_charts, .maps = NULL,
  113. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fd_config,
  114. .config_file = NETDATA_FD_CONFIG_FILE,
  115. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_11,
  116. .load = EBPF_LOAD_LEGACY, .targets = NULL},
  117. { .thread_name = "hardirq", .config_name = "hardirq", .enabled = 0, .start_routine = ebpf_hardirq_thread,
  118. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  119. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  120. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &hardirq_config,
  121. .config_file = NETDATA_HARDIRQ_CONFIG_FILE,
  122. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  123. .load = EBPF_LOAD_LEGACY, .targets = NULL},
  124. { .thread_name = "softirq", .config_name = "softirq", .enabled = 0, .start_routine = ebpf_softirq_thread,
  125. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  126. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  127. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &softirq_config,
  128. .config_file = NETDATA_SOFTIRQ_CONFIG_FILE,
  129. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  130. .load = EBPF_LOAD_LEGACY, .targets = NULL},
  131. { .thread_name = "oomkill", .config_name = "oomkill", .enabled = 0, .start_routine = ebpf_oomkill_thread,
  132. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  133. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  134. .apps_routine = ebpf_oomkill_create_apps_charts, .maps = NULL,
  135. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &oomkill_config,
  136. .config_file = NETDATA_OOMKILL_CONFIG_FILE,
  137. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  138. .load = EBPF_LOAD_LEGACY, .targets = NULL},
  139. { .thread_name = "shm", .config_name = "shm", .enabled = 0, .start_routine = ebpf_shm_thread,
  140. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  141. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  142. .apps_routine = ebpf_shm_create_apps_charts, .maps = NULL,
  143. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &shm_config,
  144. .config_file = NETDATA_DIRECTORY_SHM_CONFIG_FILE,
  145. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  146. .load = EBPF_LOAD_LEGACY, .targets = shm_targets},
  147. { .thread_name = "mdflush", .config_name = "mdflush", .enabled = 0, .start_routine = ebpf_mdflush_thread,
  148. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = CONFIG_BOOLEAN_NO,
  149. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  150. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &mdflush_config,
  151. .config_file = NETDATA_DIRECTORY_MDFLUSH_CONFIG_FILE,
  152. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  153. .load = EBPF_LOAD_LEGACY, .targets = NULL},
  154. { .thread_name = NULL, .enabled = 0, .start_routine = NULL, .update_every = EBPF_DEFAULT_UPDATE_EVERY,
  155. .global_charts = 0, .apps_charts = CONFIG_BOOLEAN_NO, .cgroup_charts = CONFIG_BOOLEAN_NO,
  156. .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = 0, .names = NULL,
  157. .cfg = NULL, .config_name = NULL, .kernels = 0, .load = EBPF_LOAD_LEGACY, .targets = NULL},
  158. };
  159. // Link with apps.plugin
  160. ebpf_process_stat_t *global_process_stat = NULL;
  161. // Link with cgroup.plugin
  162. netdata_ebpf_cgroup_shm_t shm_ebpf_cgroup = {NULL, NULL};
  163. int shm_fd_ebpf_cgroup = -1;
  164. sem_t *shm_sem_ebpf_cgroup = SEM_FAILED;
  165. pthread_mutex_t mutex_cgroup_shm;
  166. //Network viewer
  167. ebpf_network_viewer_options_t network_viewer_opt;
  168. // Statistic
  169. ebpf_plugin_stats_t plugin_statistics = {.core = 0, .legacy = 0, .running = 0, .threads = 0, .tracepoints = 0,
  170. .probes = 0, .retprobes = 0, .trampolines = 0};
  171. #ifdef LIBBPF_MAJOR_VERSION
  172. struct btf *default_btf = NULL;
  173. #endif
  174. char *btf_path = NULL;
  175. /*****************************************************************
  176. *
  177. * FUNCTIONS USED TO CLEAN MEMORY AND OPERATE SYSTEM FILES
  178. *
  179. *****************************************************************/
  180. /**
  181. * Clean Loaded Events
  182. *
  183. * This function cleans the events previous loaded on Linux.
  184. void clean_loaded_events()
  185. {
  186. int event_pid;
  187. for (event_pid = 0; ebpf_modules[event_pid].probes; event_pid++)
  188. clean_kprobe_events(NULL, (int)ebpf_modules[event_pid].thread_id, ebpf_modules[event_pid].probes);
  189. }
  190. */
  191. /**
  192. * Close the collector gracefully
  193. *
  194. * @param sig is the signal number used to close the collector
  195. */
  196. static void ebpf_exit(int sig)
  197. {
  198. close_ebpf_plugin = 1;
  199. static int remove_pid = 0;
  200. // When both threads were not finished case I try to go in front this address, the collector will crash
  201. if (!thread_finished) {
  202. return;
  203. }
  204. if (ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled) {
  205. ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled = 0;
  206. clean_socket_apps_structures();
  207. freez(socket_bandwidth_curr);
  208. }
  209. if (ebpf_modules[EBPF_MODULE_CACHESTAT_IDX].enabled) {
  210. ebpf_modules[EBPF_MODULE_CACHESTAT_IDX].enabled = 0;
  211. clean_cachestat_pid_structures();
  212. freez(cachestat_pid);
  213. }
  214. if (ebpf_modules[EBPF_MODULE_DCSTAT_IDX].enabled) {
  215. ebpf_modules[EBPF_MODULE_DCSTAT_IDX].enabled = 0;
  216. clean_dcstat_pid_structures();
  217. freez(dcstat_pid);
  218. }
  219. if (ebpf_modules[EBPF_MODULE_SWAP_IDX].enabled) {
  220. ebpf_modules[EBPF_MODULE_SWAP_IDX].enabled = 0;
  221. clean_swap_pid_structures();
  222. freez(swap_pid);
  223. }
  224. if (ebpf_modules[EBPF_MODULE_VFS_IDX].enabled) {
  225. ebpf_modules[EBPF_MODULE_VFS_IDX].enabled = 0;
  226. clean_vfs_pid_structures();
  227. freez(vfs_pid);
  228. }
  229. if (ebpf_modules[EBPF_MODULE_FD_IDX].enabled) {
  230. ebpf_modules[EBPF_MODULE_FD_IDX].enabled = 0;
  231. clean_fd_pid_structures();
  232. freez(fd_pid);
  233. }
  234. if (ebpf_modules[EBPF_MODULE_SHM_IDX].enabled) {
  235. ebpf_modules[EBPF_MODULE_SHM_IDX].enabled = 0;
  236. clean_shm_pid_structures();
  237. freez(shm_pid);
  238. }
  239. ebpf_close_cgroup_shm();
  240. ebpf_clean_cgroup_pids();
  241. /*
  242. int ret = fork();
  243. if (ret < 0) // error
  244. error("Cannot fork(), so I won't be able to clean %skprobe_events", NETDATA_DEBUGFS);
  245. else if (!ret) { // child
  246. int i;
  247. for (i = getdtablesize(); i >= 0; --i)
  248. close(i);
  249. int fd = open("/dev/null", O_RDWR, 0);
  250. if (fd != -1) {
  251. dup2(fd, STDIN_FILENO);
  252. dup2(fd, STDOUT_FILENO);
  253. dup2(fd, STDERR_FILENO);
  254. }
  255. if (fd > 2)
  256. close(fd);
  257. int sid = setsid();
  258. if (sid >= 0) {
  259. debug(D_EXIT, "Wait for father %d die", getpid());
  260. sleep_usec(200000); // Sleep 200 milliseconds to father dies.
  261. clean_loaded_events();
  262. } else {
  263. error("Cannot become session id leader, so I won't try to clean kprobe_events.\n");
  264. }
  265. } else { // parent
  266. exit(0);
  267. }
  268. */
  269. #ifdef LIBBPF_MAJOR_VERSION
  270. if (default_btf) {
  271. btf__free(default_btf);
  272. default_btf = NULL;
  273. }
  274. #endif
  275. if (!remove_pid) {
  276. remove_pid = 1;
  277. char filename[FILENAME_MAX + 1];
  278. ebpf_pid_file(filename, FILENAME_MAX);
  279. if (unlink(filename))
  280. error("Cannot remove PID file %s", filename);
  281. }
  282. exit(sig);
  283. }
  284. /*****************************************************************
  285. *
  286. * FUNCTIONS TO CREATE CHARTS
  287. *
  288. *****************************************************************/
  289. /**
  290. * Get a value from a structure.
  291. *
  292. * @param basis it is the first address of the structure
  293. * @param offset it is the offset of the data you want to access.
  294. * @return
  295. */
  296. collected_number get_value_from_structure(char *basis, size_t offset)
  297. {
  298. collected_number *value = (collected_number *)(basis + offset);
  299. collected_number ret = (collected_number)llabs(*value);
  300. // this reset is necessary to avoid keep a constant value while processing is not executing a task
  301. *value = 0;
  302. return ret;
  303. }
  304. /**
  305. * Write begin command on standard output
  306. *
  307. * @param family the chart family name
  308. * @param name the chart name
  309. */
  310. void write_begin_chart(char *family, char *name)
  311. {
  312. printf("BEGIN %s.%s\n", family, name);
  313. }
  314. /**
  315. * Write END command on stdout.
  316. */
  317. inline void write_end_chart()
  318. {
  319. printf("END\n");
  320. }
  321. /**
  322. * Write set command on standard output
  323. *
  324. * @param dim the dimension name
  325. * @param value the value for the dimension
  326. */
  327. void write_chart_dimension(char *dim, long long value)
  328. {
  329. printf("SET %s = %lld\n", dim, value);
  330. }
  331. /**
  332. * Call the necessary functions to create a chart.
  333. *
  334. * @param name the chart name
  335. * @param family the chart family
  336. * @param move the pointer with the values that will be published
  337. * @param end the number of values that will be written on standard output
  338. *
  339. * @return It returns a variable that maps the charts that did not have zero values.
  340. */
  341. void write_count_chart(char *name, char *family, netdata_publish_syscall_t *move, uint32_t end)
  342. {
  343. write_begin_chart(family, name);
  344. uint32_t i = 0;
  345. while (move && i < end) {
  346. write_chart_dimension(move->name, move->ncall);
  347. move = move->next;
  348. i++;
  349. }
  350. write_end_chart();
  351. }
  352. /**
  353. * Call the necessary functions to create a chart.
  354. *
  355. * @param name the chart name
  356. * @param family the chart family
  357. * @param move the pointer with the values that will be published
  358. * @param end the number of values that will be written on standard output
  359. */
  360. void write_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end)
  361. {
  362. write_begin_chart(family, name);
  363. int i = 0;
  364. while (move && i < end) {
  365. write_chart_dimension(move->name, move->nerr);
  366. move = move->next;
  367. i++;
  368. }
  369. write_end_chart();
  370. }
  371. /**
  372. * Write charts
  373. *
  374. * Write the current information to publish the charts.
  375. *
  376. * @param family chart family
  377. * @param chart chart id
  378. * @param dim dimension name
  379. * @param v1 value.
  380. */
  381. void ebpf_one_dimension_write_charts(char *family, char *chart, char *dim, long long v1)
  382. {
  383. write_begin_chart(family, chart);
  384. write_chart_dimension(dim, v1);
  385. write_end_chart();
  386. }
  387. /**
  388. * Call the necessary functions to create a chart.
  389. *
  390. * @param chart the chart name
  391. * @param family the chart family
  392. * @param dwrite the dimension name
  393. * @param vwrite the value for previous dimension
  394. * @param dread the dimension name
  395. * @param vread the value for previous dimension
  396. *
  397. * @return It returns a variable that maps the charts that did not have zero values.
  398. */
  399. void write_io_chart(char *chart, char *family, char *dwrite, long long vwrite, char *dread, long long vread)
  400. {
  401. write_begin_chart(family, chart);
  402. write_chart_dimension(dwrite, vwrite);
  403. write_chart_dimension(dread, vread);
  404. write_end_chart();
  405. }
  406. /**
  407. * Write chart cmd on standard output
  408. *
  409. * @param type chart type
  410. * @param id chart id
  411. * @param title chart title
  412. * @param units units label
  413. * @param family group name used to attach the chart on dashboard
  414. * @param charttype chart type
  415. * @param context chart context
  416. * @param order chart order
  417. * @param update_every update interval used by plugin
  418. * @param module chart module name, this is the eBPF thread.
  419. */
  420. void ebpf_write_chart_cmd(char *type, char *id, char *title, char *units, char *family,
  421. char *charttype, char *context, int order, int update_every, char *module)
  422. {
  423. printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d '' 'ebpf.plugin' '%s'\n",
  424. type,
  425. id,
  426. title,
  427. units,
  428. (family)?family:"",
  429. (context)?context:"",
  430. (charttype)?charttype:"",
  431. order,
  432. update_every,
  433. module);
  434. }
  435. /**
  436. * Write chart cmd on standard output
  437. *
  438. * @param type chart type
  439. * @param id chart id
  440. * @param title chart title
  441. * @param units units label
  442. * @param family group name used to attach the chart on dashboard
  443. * @param charttype chart type
  444. * @param context chart context
  445. * @param order chart order
  446. * @param update_every value to overwrite the update frequency set by the server.
  447. */
  448. void ebpf_write_chart_obsolete(char *type, char *id, char *title, char *units, char *family,
  449. char *charttype, char *context, int order, int update_every)
  450. {
  451. printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d 'obsolete'\n",
  452. type,
  453. id,
  454. title,
  455. units,
  456. (family)?family:"",
  457. (context)?context:"",
  458. (charttype)?charttype:"",
  459. order,
  460. update_every);
  461. }
  462. /**
  463. * Write the dimension command on standard output
  464. *
  465. * @param name the dimension name
  466. * @param id the dimension id
  467. * @param algo the dimension algorithm
  468. */
  469. void ebpf_write_global_dimension(char *name, char *id, char *algorithm)
  470. {
  471. printf("DIMENSION %s %s %s 1 1\n", name, id, algorithm);
  472. }
  473. /**
  474. * Call ebpf_write_global_dimension to create the dimensions for a specific chart
  475. *
  476. * @param ptr a pointer to a structure of the type netdata_publish_syscall_t
  477. * @param end the number of dimensions for the structure ptr
  478. */
  479. void ebpf_create_global_dimension(void *ptr, int end)
  480. {
  481. netdata_publish_syscall_t *move = ptr;
  482. int i = 0;
  483. while (move && i < end) {
  484. ebpf_write_global_dimension(move->name, move->dimension, move->algorithm);
  485. move = move->next;
  486. i++;
  487. }
  488. }
  489. /**
  490. * Call write_chart_cmd to create the charts
  491. *
  492. * @param type chart type
  493. * @param id chart id
  494. * @param title chart title
  495. * @param units axis label
  496. * @param family group name used to attach the chart on dashboard
  497. * @param context chart context
  498. * @param charttype chart type
  499. * @param order order number of the specified chart
  500. * @param ncd a pointer to a function called to create dimensions
  501. * @param move a pointer for a structure that has the dimensions
  502. * @param end number of dimensions for the chart created
  503. * @param update_every update interval used with chart.
  504. * @param module chart module name, this is the eBPF thread.
  505. */
  506. void ebpf_create_chart(char *type,
  507. char *id,
  508. char *title,
  509. char *units,
  510. char *family,
  511. char *context,
  512. char *charttype,
  513. int order,
  514. void (*ncd)(void *, int),
  515. void *move,
  516. int end,
  517. int update_every,
  518. char *module)
  519. {
  520. ebpf_write_chart_cmd(type, id, title, units, family, charttype, context, order, update_every, module);
  521. if (ncd) {
  522. ncd(move, end);
  523. }
  524. }
  525. /**
  526. * Create charts on apps submenu
  527. *
  528. * @param id the chart id
  529. * @param title the value displayed on vertical axis.
  530. * @param units the value displayed on vertical axis.
  531. * @param family Submenu that the chart will be attached on dashboard.
  532. * @param charttype chart type
  533. * @param order the chart order
  534. * @param algorithm the algorithm used by dimension
  535. * @param root structure used to create the dimensions.
  536. * @param update_every update interval used by plugin
  537. * @param module chart module name, this is the eBPF thread.
  538. */
  539. void ebpf_create_charts_on_apps(char *id, char *title, char *units, char *family, char *charttype, int order,
  540. char *algorithm, struct target *root, int update_every, char *module)
  541. {
  542. struct target *w;
  543. ebpf_write_chart_cmd(NETDATA_APPS_FAMILY, id, title, units, family, charttype, NULL, order,
  544. update_every, module);
  545. for (w = root; w; w = w->next) {
  546. if (unlikely(w->exposed))
  547. fprintf(stdout, "DIMENSION %s '' %s 1 1\n", w->name, algorithm);
  548. }
  549. }
  550. /**
  551. * Call the necessary functions to create a name.
  552. *
  553. * @param family family name
  554. * @param name chart name
  555. * @param hist0 histogram values
  556. * @param dimensions dimension values.
  557. * @param end number of bins that will be sent to Netdata.
  558. *
  559. * @return It returns a variable that maps the charts that did not have zero values.
  560. */
  561. void write_histogram_chart(char *family, char *name, const netdata_idx_t *hist, char **dimensions, uint32_t end)
  562. {
  563. write_begin_chart(family, name);
  564. uint32_t i;
  565. for (i = 0; i < end; i++) {
  566. write_chart_dimension(dimensions[i], (long long) hist[i]);
  567. }
  568. write_end_chart();
  569. fflush(stdout);
  570. }
  571. /*****************************************************************
  572. *
  573. * FUNCTIONS TO DEFINE OPTIONS
  574. *
  575. *****************************************************************/
  576. /**
  577. * Define labels used to generate charts
  578. *
  579. * @param is structure with information about number of calls made for a function.
  580. * @param pio structure used to generate charts.
  581. * @param dim a pointer for the dimensions name
  582. * @param name a pointer for the tensor with the name of the functions.
  583. * @param algorithm a vector with the algorithms used to make the charts
  584. * @param end the number of elements in the previous 4 arguments.
  585. */
  586. void ebpf_global_labels(netdata_syscall_stat_t *is, netdata_publish_syscall_t *pio, char **dim,
  587. char **name, int *algorithm, int end)
  588. {
  589. int i;
  590. netdata_syscall_stat_t *prev = NULL;
  591. netdata_publish_syscall_t *publish_prev = NULL;
  592. for (i = 0; i < end; i++) {
  593. if (prev) {
  594. prev->next = &is[i];
  595. }
  596. prev = &is[i];
  597. pio[i].dimension = dim[i];
  598. pio[i].name = name[i];
  599. pio[i].algorithm = strdupz(ebpf_algorithms[algorithm[i]]);
  600. if (publish_prev) {
  601. publish_prev->next = &pio[i];
  602. }
  603. publish_prev = &pio[i];
  604. }
  605. }
  606. /**
  607. * Define thread mode for all ebpf program.
  608. *
  609. * @param lmode the mode that will be used for them.
  610. */
  611. static inline void ebpf_set_thread_mode(netdata_run_mode_t lmode)
  612. {
  613. int i;
  614. for (i = 0; ebpf_modules[i].thread_name; i++) {
  615. ebpf_modules[i].mode = lmode;
  616. }
  617. }
  618. /**
  619. * Enable specific charts selected by user.
  620. *
  621. * @param em the structure that will be changed
  622. * @param disable_apps the status about the apps charts.
  623. * @param disable_cgroup the status about the cgroups charts.
  624. */
  625. static inline void ebpf_enable_specific_chart(struct ebpf_module *em, int disable_apps, int disable_cgroup)
  626. {
  627. em->enabled = CONFIG_BOOLEAN_YES;
  628. // oomkill stores data inside apps submenu, so it always need to have apps_enabled for plugin to create
  629. // its chart, without this comparison eBPF.plugin will try to store invalid data when apps is disabled.
  630. if (!disable_apps || !strcmp(em->thread_name, "oomkill")) {
  631. em->apps_charts = CONFIG_BOOLEAN_YES;
  632. }
  633. if (!disable_cgroup) {
  634. em->cgroup_charts = CONFIG_BOOLEAN_YES;
  635. }
  636. em->global_charts = CONFIG_BOOLEAN_YES;
  637. }
  638. /**
  639. * Enable all charts
  640. *
  641. * @param apps what is the current status of apps
  642. * @param cgroups what is the current status of cgroups
  643. */
  644. static inline void ebpf_enable_all_charts(int apps, int cgroups)
  645. {
  646. int i;
  647. for (i = 0; ebpf_modules[i].thread_name; i++) {
  648. ebpf_enable_specific_chart(&ebpf_modules[i], apps, cgroups);
  649. }
  650. }
  651. /**
  652. * Disable all Global charts
  653. *
  654. * Disable charts
  655. */
  656. static inline void disable_all_global_charts()
  657. {
  658. int i;
  659. for (i = 0; ebpf_modules[i].thread_name; i++) {
  660. ebpf_modules[i].enabled = 0;
  661. ebpf_modules[i].global_charts = 0;
  662. }
  663. }
  664. /**
  665. * Enable the specified chart group
  666. *
  667. * @param idx the index of ebpf_modules that I am enabling
  668. * @param disable_apps should I keep apps charts?
  669. */
  670. static inline void ebpf_enable_chart(int idx, int disable_apps, int disable_cgroup)
  671. {
  672. int i;
  673. for (i = 0; ebpf_modules[i].thread_name; i++) {
  674. if (i == idx) {
  675. ebpf_enable_specific_chart(&ebpf_modules[i], disable_apps, disable_cgroup);
  676. break;
  677. }
  678. }
  679. }
  680. /**
  681. * Disable APPs
  682. *
  683. * Disable charts for apps loading only global charts.
  684. */
  685. static inline void ebpf_disable_apps()
  686. {
  687. int i;
  688. for (i = 0; ebpf_modules[i].thread_name; i++) {
  689. ebpf_modules[i].apps_charts = 0;
  690. }
  691. }
  692. /**
  693. * Disable Cgroups
  694. *
  695. * Disable charts for apps loading only global charts.
  696. */
  697. static inline void ebpf_disable_cgroups()
  698. {
  699. int i;
  700. for (i = 0; ebpf_modules[i].thread_name; i++) {
  701. ebpf_modules[i].cgroup_charts = 0;
  702. }
  703. }
  704. /**
  705. * Update Disabled Plugins
  706. *
  707. * This function calls ebpf_update_stats to update statistics for collector.
  708. *
  709. * @param em a pointer to `struct ebpf_module`
  710. */
  711. void ebpf_update_disabled_plugin_stats(ebpf_module_t *em)
  712. {
  713. pthread_mutex_lock(&lock);
  714. ebpf_update_stats(&plugin_statistics, em);
  715. pthread_mutex_unlock(&lock);
  716. }
  717. /**
  718. * Print help on standard error for user knows how to use the collector.
  719. */
  720. void ebpf_print_help()
  721. {
  722. const time_t t = time(NULL);
  723. struct tm ct;
  724. struct tm *test = localtime_r(&t, &ct);
  725. int year;
  726. if (test)
  727. year = ct.tm_year;
  728. else
  729. year = 0;
  730. fprintf(stderr,
  731. "\n"
  732. " Netdata ebpf.plugin %s\n"
  733. " Copyright (C) 2016-%d Costa Tsaousis <costa@tsaousis.gr>\n"
  734. " Released under GNU General Public License v3 or later.\n"
  735. " All rights reserved.\n"
  736. "\n"
  737. " This eBPF.plugin is a data collector plugin for netdata.\n"
  738. "\n"
  739. " This plugin only accepts long options with one or two dashes. The available command line options are:\n"
  740. "\n"
  741. " SECONDS Set the data collection frequency.\n"
  742. "\n"
  743. " [-]-help Show this help.\n"
  744. "\n"
  745. " [-]-version Show software version.\n"
  746. "\n"
  747. " [-]-global Disable charts per application and cgroup.\n"
  748. "\n"
  749. " [-]-all Enable all chart groups (global, apps, and cgroup), unless -g is also given.\n"
  750. "\n"
  751. " [-]-cachestat Enable charts related to process run time.\n"
  752. "\n"
  753. " [-]-dcstat Enable charts related to directory cache.\n"
  754. "\n"
  755. " [-]-disk Enable charts related to disk monitoring.\n"
  756. "\n"
  757. " [-]-filesystem Enable chart related to filesystem run time.\n"
  758. "\n"
  759. " [-]-hardirq Enable chart related to hard IRQ latency.\n"
  760. "\n"
  761. " [-]-mdflush Enable charts related to multi-device flush.\n"
  762. "\n"
  763. " [-]-mount Enable charts related to mount monitoring.\n"
  764. "\n"
  765. " [-]-net Enable network viewer charts.\n"
  766. "\n"
  767. " [-]-oomkill Enable chart related to OOM kill tracking.\n"
  768. "\n"
  769. " [-]-process Enable charts related to process run time.\n"
  770. "\n"
  771. " [-]-return Run the collector in return mode.\n"
  772. "\n"
  773. " [-]-shm Enable chart related to shared memory tracking.\n"
  774. "\n"
  775. " [-]-softirq Enable chart related to soft IRQ latency.\n"
  776. "\n"
  777. " [-]-sync Enable chart related to sync run time.\n"
  778. "\n"
  779. " [-]-swap Enable chart related to swap run time.\n"
  780. "\n"
  781. " [-]-vfs Enable chart related to vfs run time.\n"
  782. "\n"
  783. " [-]-legacy Load legacy eBPF programs.\n"
  784. "\n"
  785. " [-]-core Use CO-RE when available(Working in progress).\n"
  786. "\n",
  787. VERSION,
  788. (year >= 116) ? year + 1900 : 2020);
  789. }
  790. /*****************************************************************
  791. *
  792. * TRACEPOINT MANAGEMENT FUNCTIONS
  793. *
  794. *****************************************************************/
  795. /**
  796. * Enable a tracepoint.
  797. *
  798. * @return 0 on success, -1 on error.
  799. */
  800. int ebpf_enable_tracepoint(ebpf_tracepoint_t *tp)
  801. {
  802. int test = ebpf_is_tracepoint_enabled(tp->class, tp->event);
  803. // err?
  804. if (test == -1) {
  805. return -1;
  806. }
  807. // disabled?
  808. else if (test == 0) {
  809. // enable it then.
  810. if (ebpf_enable_tracing_values(tp->class, tp->event)) {
  811. return -1;
  812. }
  813. }
  814. // enabled now or already was.
  815. tp->enabled = true;
  816. return 0;
  817. }
  818. /**
  819. * Disable a tracepoint if it's enabled.
  820. *
  821. * @return 0 on success, -1 on error.
  822. */
  823. int ebpf_disable_tracepoint(ebpf_tracepoint_t *tp)
  824. {
  825. int test = ebpf_is_tracepoint_enabled(tp->class, tp->event);
  826. // err?
  827. if (test == -1) {
  828. return -1;
  829. }
  830. // enabled?
  831. else if (test == 1) {
  832. // disable it then.
  833. if (ebpf_disable_tracing_values(tp->class, tp->event)) {
  834. return -1;
  835. }
  836. }
  837. // disable now or already was.
  838. tp->enabled = false;
  839. return 0;
  840. }
  841. /**
  842. * Enable multiple tracepoints on a list of tracepoints which end when the
  843. * class is NULL.
  844. *
  845. * @return the number of successful enables.
  846. */
  847. uint32_t ebpf_enable_tracepoints(ebpf_tracepoint_t *tps)
  848. {
  849. uint32_t cnt = 0;
  850. for (int i = 0; tps[i].class != NULL; i++) {
  851. if (ebpf_enable_tracepoint(&tps[i]) == -1) {
  852. infoerr("failed to enable tracepoint %s:%s",
  853. tps[i].class, tps[i].event);
  854. }
  855. else {
  856. cnt += 1;
  857. }
  858. }
  859. return cnt;
  860. }
  861. /*****************************************************************
  862. *
  863. * AUXILIARY FUNCTIONS USED DURING INITIALIZATION
  864. *
  865. *****************************************************************/
  866. /**
  867. * Read Local Ports
  868. *
  869. * Parse /proc/net/{tcp,udp} and get the ports Linux is listening.
  870. *
  871. * @param filename the proc file to parse.
  872. * @param proto is the magic number associated to the protocol file we are reading.
  873. */
  874. static void read_local_ports(char *filename, uint8_t proto)
  875. {
  876. procfile *ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  877. if (!ff)
  878. return;
  879. ff = procfile_readall(ff);
  880. if (!ff)
  881. return;
  882. size_t lines = procfile_lines(ff), l;
  883. netdata_passive_connection_t values = {.counter = 0, .tgid = 0, .pid = 0};
  884. for(l = 0; l < lines ;l++) {
  885. size_t words = procfile_linewords(ff, l);
  886. // This is header or end of file
  887. if (unlikely(words < 14))
  888. continue;
  889. // https://elixir.bootlin.com/linux/v5.7.8/source/include/net/tcp_states.h
  890. // 0A = TCP_LISTEN
  891. if (strcmp("0A", procfile_lineword(ff, l, 5)))
  892. continue;
  893. // Read local port
  894. uint16_t port = (uint16_t)strtol(procfile_lineword(ff, l, 2), NULL, 16);
  895. update_listen_table(htons(port), proto, &values);
  896. }
  897. procfile_close(ff);
  898. }
  899. /**
  900. * Read Local addresseses
  901. *
  902. * Read the local address from the interfaces.
  903. */
  904. static void read_local_addresses()
  905. {
  906. struct ifaddrs *ifaddr, *ifa;
  907. if (getifaddrs(&ifaddr) == -1) {
  908. error("Cannot get the local IP addresses, it is no possible to do separation between inbound and outbound connections");
  909. return;
  910. }
  911. char *notext = { "No text representation" };
  912. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  913. if (ifa->ifa_addr == NULL)
  914. continue;
  915. if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6))
  916. continue;
  917. ebpf_network_viewer_ip_list_t *w = callocz(1, sizeof(ebpf_network_viewer_ip_list_t));
  918. int family = ifa->ifa_addr->sa_family;
  919. w->ver = (uint8_t) family;
  920. char text[INET6_ADDRSTRLEN];
  921. if (family == AF_INET) {
  922. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  923. w->first.addr32[0] = in->sin_addr.s_addr;
  924. w->last.addr32[0] = in->sin_addr.s_addr;
  925. if (inet_ntop(AF_INET, w->first.addr8, text, INET_ADDRSTRLEN)) {
  926. w->value = strdupz(text);
  927. w->hash = simple_hash(text);
  928. } else {
  929. w->value = strdupz(notext);
  930. w->hash = simple_hash(notext);
  931. }
  932. } else {
  933. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  934. memcpy(w->first.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  935. memcpy(w->last.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  936. if (inet_ntop(AF_INET6, w->first.addr8, text, INET_ADDRSTRLEN)) {
  937. w->value = strdupz(text);
  938. w->hash = simple_hash(text);
  939. } else {
  940. w->value = strdupz(notext);
  941. w->hash = simple_hash(notext);
  942. }
  943. }
  944. fill_ip_list((family == AF_INET)?&network_viewer_opt.ipv4_local_ip:&network_viewer_opt.ipv6_local_ip,
  945. w,
  946. "selector");
  947. }
  948. freeifaddrs(ifaddr);
  949. }
  950. /**
  951. * Start Pthread Variable
  952. *
  953. * This function starts all pthread variables.
  954. *
  955. * @return It returns 0 on success and -1.
  956. */
  957. int ebpf_start_pthread_variables()
  958. {
  959. pthread_mutex_init(&lock, NULL);
  960. pthread_mutex_init(&collect_data_mutex, NULL);
  961. if (pthread_cond_init(&collect_data_cond_var, NULL)) {
  962. thread_finished++;
  963. error("Cannot start conditional variable to control Apps charts.");
  964. return -1;
  965. }
  966. return 0;
  967. }
  968. /**
  969. * Am I collecting PIDs?
  970. *
  971. * Test if eBPF plugin needs to collect PID information.
  972. *
  973. * @return It returns 1 if at least one thread needs to collect the data, or zero otherwise.
  974. */
  975. static inline uint32_t ebpf_am_i_collect_pids()
  976. {
  977. uint32_t ret = 0;
  978. int i;
  979. for (i = 0; ebpf_modules[i].thread_name; i++) {
  980. ret |= ebpf_modules[i].cgroup_charts | ebpf_modules[i].apps_charts;
  981. }
  982. return ret;
  983. }
  984. /**
  985. * Allocate the vectors used for all threads.
  986. */
  987. static void ebpf_allocate_common_vectors()
  988. {
  989. if (unlikely(!ebpf_am_i_collect_pids())) {
  990. return;
  991. }
  992. all_pids = callocz((size_t)pid_max, sizeof(struct pid_stat *));
  993. global_process_stat = callocz((size_t)ebpf_nprocs, sizeof(ebpf_process_stat_t));
  994. }
  995. /**
  996. * Define how to load the ebpf programs
  997. *
  998. * @param ptr the option given by users
  999. */
  1000. static inline void how_to_load(char *ptr)
  1001. {
  1002. if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_RETURN))
  1003. ebpf_set_thread_mode(MODE_RETURN);
  1004. else if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_DEFAULT))
  1005. ebpf_set_thread_mode(MODE_ENTRY);
  1006. else
  1007. error("the option %s for \"ebpf load mode\" is not a valid option.", ptr);
  1008. }
  1009. /**
  1010. * Update interval
  1011. *
  1012. * Update default interval with value from user
  1013. *
  1014. * @param update_every value to overwrite the update frequency set by the server.
  1015. */
  1016. static void ebpf_update_interval(int update_every)
  1017. {
  1018. int i;
  1019. int value = (int) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_UPDATE_EVERY,
  1020. update_every);
  1021. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1022. ebpf_modules[i].update_every = value;
  1023. }
  1024. }
  1025. /**
  1026. * Update PID table size
  1027. *
  1028. * Update default size with value from user
  1029. */
  1030. static void ebpf_update_table_size()
  1031. {
  1032. int i;
  1033. uint32_t value = (uint32_t) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION,
  1034. EBPF_CFG_PID_SIZE, ND_EBPF_DEFAULT_PID_SIZE);
  1035. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1036. ebpf_modules[i].pid_map_size = value;
  1037. }
  1038. }
  1039. /**
  1040. * Set Load mode
  1041. *
  1042. * @param load default load mode.
  1043. */
  1044. static inline void ebpf_set_load_mode(netdata_ebpf_load_mode_t load)
  1045. {
  1046. #ifdef LIBBPF_MAJOR_VERSION
  1047. if (load == EBPF_LOAD_CORE || load == EBPF_LOAD_PLAY_DICE) {
  1048. load = (!default_btf) ? EBPF_LOAD_LEGACY : EBPF_LOAD_CORE;
  1049. }
  1050. #else
  1051. load = EBPF_LOAD_LEGACY;
  1052. #endif
  1053. int i;
  1054. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1055. // TO DO: Use `load` variable after we change all threads.
  1056. ebpf_modules[i].load = EBPF_LOAD_LEGACY; // load ;
  1057. }
  1058. }
  1059. /**
  1060. * Update mode
  1061. *
  1062. * @param str value read from configuration file.
  1063. */
  1064. static inline void epbf_update_load_mode(char *str)
  1065. {
  1066. netdata_ebpf_load_mode_t load = epbf_convert_string_to_load_mode(str);
  1067. ebpf_set_load_mode(load);
  1068. }
  1069. /**
  1070. * Read collector values
  1071. *
  1072. * @param disable_apps variable to store information related to apps.
  1073. * @param disable_cgroups variable to store information related to cgroups.
  1074. * @param update_every value to overwrite the update frequency set by the server.
  1075. */
  1076. static void read_collector_values(int *disable_apps, int *disable_cgroups, int update_every)
  1077. {
  1078. // Read global section
  1079. char *value;
  1080. if (appconfig_exists(&collector_config, EBPF_GLOBAL_SECTION, "load")) // Backward compatibility
  1081. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, "load",
  1082. EBPF_CFG_LOAD_MODE_DEFAULT);
  1083. else
  1084. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE,
  1085. EBPF_CFG_LOAD_MODE_DEFAULT);
  1086. how_to_load(value);
  1087. btf_path = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_PROGRAM_PATH,
  1088. EBPF_DEFAULT_BTF_PATH);
  1089. #ifdef LIBBPF_MAJOR_VERSION
  1090. default_btf = ebpf_load_btf_file(btf_path, EBPF_DEFAULT_BTF_FILE);
  1091. #endif
  1092. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_TYPE_FORMAT, EBPF_CFG_DEFAULT_PROGRAM);
  1093. epbf_update_load_mode(value);
  1094. ebpf_update_interval(update_every);
  1095. ebpf_update_table_size();
  1096. // This is kept to keep compatibility
  1097. uint32_t enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, "disable apps",
  1098. CONFIG_BOOLEAN_NO);
  1099. if (!enabled) {
  1100. // Apps is a positive sentence, so we need to invert the values to disable apps.
  1101. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
  1102. CONFIG_BOOLEAN_YES);
  1103. enabled = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  1104. }
  1105. *disable_apps = (int)enabled;
  1106. // Cgroup is a positive sentence, so we need to invert the values to disable apps.
  1107. // We are using the same pattern for cgroup and apps
  1108. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_CGROUP, CONFIG_BOOLEAN_NO);
  1109. *disable_cgroups = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  1110. // Read ebpf programs section
  1111. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  1112. ebpf_modules[EBPF_MODULE_PROCESS_IDX].config_name, CONFIG_BOOLEAN_YES);
  1113. int started = 0;
  1114. if (enabled) {
  1115. ebpf_enable_chart(EBPF_MODULE_PROCESS_IDX, *disable_apps, *disable_cgroups);
  1116. started++;
  1117. }
  1118. // This is kept to keep compatibility
  1119. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network viewer",
  1120. CONFIG_BOOLEAN_NO);
  1121. if (!enabled)
  1122. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  1123. ebpf_modules[EBPF_MODULE_SOCKET_IDX].config_name,
  1124. CONFIG_BOOLEAN_NO);
  1125. if (enabled) {
  1126. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, *disable_apps, *disable_cgroups);
  1127. // Read network viewer section if network viewer is enabled
  1128. // This is kept here to keep backward compatibility
  1129. parse_network_viewer_section(&collector_config);
  1130. parse_service_name_section(&collector_config);
  1131. started++;
  1132. }
  1133. // This is kept to keep compatibility
  1134. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connection monitoring",
  1135. CONFIG_BOOLEAN_NO);
  1136. if (!enabled)
  1137. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connections",
  1138. CONFIG_BOOLEAN_NO);
  1139. ebpf_modules[EBPF_MODULE_SOCKET_IDX].optional = (int)enabled;
  1140. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "cachestat",
  1141. CONFIG_BOOLEAN_NO);
  1142. if (enabled) {
  1143. ebpf_enable_chart(EBPF_MODULE_CACHESTAT_IDX, *disable_apps, *disable_cgroups);
  1144. started++;
  1145. }
  1146. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "sync",
  1147. CONFIG_BOOLEAN_YES);
  1148. if (enabled) {
  1149. ebpf_enable_chart(EBPF_MODULE_SYNC_IDX, *disable_apps, *disable_cgroups);
  1150. started++;
  1151. }
  1152. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "dcstat",
  1153. CONFIG_BOOLEAN_NO);
  1154. if (enabled) {
  1155. ebpf_enable_chart(EBPF_MODULE_DCSTAT_IDX, *disable_apps, *disable_cgroups);
  1156. started++;
  1157. }
  1158. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "swap",
  1159. CONFIG_BOOLEAN_NO);
  1160. if (enabled) {
  1161. ebpf_enable_chart(EBPF_MODULE_SWAP_IDX, *disable_apps, *disable_cgroups);
  1162. started++;
  1163. }
  1164. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "vfs",
  1165. CONFIG_BOOLEAN_NO);
  1166. if (enabled) {
  1167. ebpf_enable_chart(EBPF_MODULE_VFS_IDX, *disable_apps, *disable_cgroups);
  1168. started++;
  1169. }
  1170. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "filesystem",
  1171. CONFIG_BOOLEAN_NO);
  1172. if (enabled) {
  1173. ebpf_enable_chart(EBPF_MODULE_FILESYSTEM_IDX, *disable_apps, *disable_cgroups);
  1174. started++;
  1175. }
  1176. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "disk",
  1177. CONFIG_BOOLEAN_NO);
  1178. if (enabled) {
  1179. ebpf_enable_chart(EBPF_MODULE_DISK_IDX, *disable_apps, *disable_cgroups);
  1180. started++;
  1181. }
  1182. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "mount",
  1183. CONFIG_BOOLEAN_YES);
  1184. if (enabled) {
  1185. ebpf_enable_chart(EBPF_MODULE_MOUNT_IDX, *disable_apps, *disable_cgroups);
  1186. started++;
  1187. }
  1188. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "fd",
  1189. CONFIG_BOOLEAN_YES);
  1190. if (enabled) {
  1191. ebpf_enable_chart(EBPF_MODULE_FD_IDX, *disable_apps, *disable_cgroups);
  1192. started++;
  1193. }
  1194. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "hardirq",
  1195. CONFIG_BOOLEAN_YES);
  1196. if (enabled) {
  1197. ebpf_enable_chart(EBPF_MODULE_HARDIRQ_IDX, *disable_apps, *disable_cgroups);
  1198. started++;
  1199. }
  1200. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "softirq",
  1201. CONFIG_BOOLEAN_YES);
  1202. if (enabled) {
  1203. ebpf_enable_chart(EBPF_MODULE_SOFTIRQ_IDX, *disable_apps, *disable_cgroups);
  1204. started++;
  1205. }
  1206. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "oomkill",
  1207. CONFIG_BOOLEAN_YES);
  1208. if (enabled) {
  1209. ebpf_enable_chart(EBPF_MODULE_OOMKILL_IDX, *disable_apps, *disable_cgroups);
  1210. started++;
  1211. }
  1212. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "shm",
  1213. CONFIG_BOOLEAN_YES);
  1214. if (enabled) {
  1215. ebpf_enable_chart(EBPF_MODULE_SHM_IDX, *disable_apps, *disable_cgroups);
  1216. started++;
  1217. }
  1218. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "mdflush",
  1219. CONFIG_BOOLEAN_NO);
  1220. if (enabled) {
  1221. ebpf_enable_chart(EBPF_MODULE_MDFLUSH_IDX, *disable_apps, *disable_cgroups);
  1222. started++;
  1223. }
  1224. if (!started){
  1225. ebpf_enable_all_charts(*disable_apps, *disable_cgroups);
  1226. // Read network viewer section
  1227. // This is kept here to keep backward compatibility
  1228. parse_network_viewer_section(&collector_config);
  1229. parse_service_name_section(&collector_config);
  1230. }
  1231. }
  1232. /**
  1233. * Load collector config
  1234. *
  1235. * @param path the path where the file ebpf.conf is stored.
  1236. * @param disable_apps variable to store the information about apps plugin status.
  1237. * @param disable_cgroups variable to store the information about cgroups plugin status.
  1238. * @param update_every value to overwrite the update frequency set by the server.
  1239. *
  1240. * @return 0 on success and -1 otherwise.
  1241. */
  1242. static int load_collector_config(char *path, int *disable_apps, int *disable_cgroups, int update_every)
  1243. {
  1244. char lpath[4096];
  1245. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_CONFIG_FILE);
  1246. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  1247. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_OLD_CONFIG_FILE);
  1248. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  1249. return -1;
  1250. }
  1251. }
  1252. read_collector_values(disable_apps, disable_cgroups, update_every);
  1253. return 0;
  1254. }
  1255. /**
  1256. * Set global variables reading environment variables
  1257. */
  1258. void set_global_variables()
  1259. {
  1260. // Get environment variables
  1261. ebpf_plugin_dir = getenv("NETDATA_PLUGINS_DIR");
  1262. if (!ebpf_plugin_dir)
  1263. ebpf_plugin_dir = PLUGINS_DIR;
  1264. ebpf_user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  1265. if (!ebpf_user_config_dir)
  1266. ebpf_user_config_dir = CONFIG_DIR;
  1267. ebpf_stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  1268. if (!ebpf_stock_config_dir)
  1269. ebpf_stock_config_dir = LIBCONFIG_DIR;
  1270. ebpf_configured_log_dir = getenv("NETDATA_LOG_DIR");
  1271. if (!ebpf_configured_log_dir)
  1272. ebpf_configured_log_dir = LOG_DIR;
  1273. ebpf_nprocs = (int)sysconf(_SC_NPROCESSORS_ONLN);
  1274. if (ebpf_nprocs > NETDATA_MAX_PROCESSOR) {
  1275. ebpf_nprocs = NETDATA_MAX_PROCESSOR;
  1276. }
  1277. isrh = get_redhat_release();
  1278. pid_max = get_system_pid_max();
  1279. running_on_kernel = ebpf_get_kernel_version();
  1280. }
  1281. /**
  1282. * Load collector config
  1283. */
  1284. static inline void ebpf_load_thread_config()
  1285. {
  1286. int i;
  1287. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1288. ebpf_update_module(&ebpf_modules[i]);
  1289. }
  1290. }
  1291. /**
  1292. * Parse arguments given from user.
  1293. *
  1294. * @param argc the number of arguments
  1295. * @param argv the pointer to the arguments
  1296. */
  1297. static void ebpf_parse_args(int argc, char **argv)
  1298. {
  1299. int disable_apps = 0;
  1300. int disable_cgroups = 1;
  1301. int freq = 0;
  1302. int option_index = 0;
  1303. uint64_t select_threads = 0;
  1304. static struct option long_options[] = {
  1305. {"process", no_argument, 0, 0 },
  1306. {"net", no_argument, 0, 0 },
  1307. {"cachestat", no_argument, 0, 0 },
  1308. {"sync", no_argument, 0, 0 },
  1309. {"dcstat", no_argument, 0, 0 },
  1310. {"swap", no_argument, 0, 0 },
  1311. {"vfs", no_argument, 0, 0 },
  1312. {"filesystem", no_argument, 0, 0 },
  1313. {"disk", no_argument, 0, 0 },
  1314. {"mount", no_argument, 0, 0 },
  1315. {"filedescriptor", no_argument, 0, 0 },
  1316. {"hardirq", no_argument, 0, 0 },
  1317. {"softirq", no_argument, 0, 0 },
  1318. {"oomkill", no_argument, 0, 0 },
  1319. {"shm", no_argument, 0, 0 },
  1320. {"mdflush", no_argument, 0, 0 },
  1321. /* INSERT NEW THREADS BEFORE THIS COMMENT TO KEEP COMPATIBILITY WITH enum ebpf_module_indexes */
  1322. {"all", no_argument, 0, 0 },
  1323. {"version", no_argument, 0, 0 },
  1324. {"help", no_argument, 0, 0 },
  1325. {"global", no_argument, 0, 0 },
  1326. {"return", no_argument, 0, 0 },
  1327. {"legacy", no_argument, 0, 0 },
  1328. {"core", no_argument, 0, 0 },
  1329. {0, 0, 0, 0}
  1330. };
  1331. memset(&network_viewer_opt, 0, sizeof(network_viewer_opt));
  1332. network_viewer_opt.max_dim = NETDATA_NV_CAP_VALUE;
  1333. if (argc > 1) {
  1334. int n = (int)str2l(argv[1]);
  1335. if (n > 0) {
  1336. freq = n;
  1337. }
  1338. }
  1339. if (!freq)
  1340. freq = EBPF_DEFAULT_UPDATE_EVERY;
  1341. if (load_collector_config(ebpf_user_config_dir, &disable_apps, &disable_cgroups, freq)) {
  1342. info(
  1343. "Does not have a configuration file inside `%s/ebpf.d.conf. It will try to load stock file.",
  1344. ebpf_user_config_dir);
  1345. if (load_collector_config(ebpf_stock_config_dir, &disable_apps, &disable_cgroups, freq)) {
  1346. info("Does not have a stock file. It is starting with default options.");
  1347. }
  1348. }
  1349. ebpf_load_thread_config();
  1350. while (1) {
  1351. int c = getopt_long_only(argc, argv, "", long_options, &option_index);
  1352. if (c == -1)
  1353. break;
  1354. switch (option_index) {
  1355. case EBPF_MODULE_PROCESS_IDX: {
  1356. select_threads |= 1<<EBPF_MODULE_PROCESS_IDX;
  1357. #ifdef NETDATA_INTERNAL_CHECKS
  1358. info("EBPF enabling \"PROCESS\" charts, because it was started with the option \"[-]-process\".");
  1359. #endif
  1360. break;
  1361. }
  1362. case EBPF_MODULE_SOCKET_IDX: {
  1363. select_threads |= 1<<EBPF_MODULE_SOCKET_IDX;
  1364. #ifdef NETDATA_INTERNAL_CHECKS
  1365. info("EBPF enabling \"NET\" charts, because it was started with the option \"[-]-net\".");
  1366. #endif
  1367. break;
  1368. }
  1369. case EBPF_MODULE_CACHESTAT_IDX: {
  1370. select_threads |= 1<<EBPF_MODULE_CACHESTAT_IDX;
  1371. #ifdef NETDATA_INTERNAL_CHECKS
  1372. info("EBPF enabling \"CACHESTAT\" charts, because it was started with the option \"[-]-cachestat\".");
  1373. #endif
  1374. break;
  1375. }
  1376. case EBPF_MODULE_SYNC_IDX: {
  1377. select_threads |= 1<<EBPF_MODULE_SYNC_IDX;
  1378. #ifdef NETDATA_INTERNAL_CHECKS
  1379. info("EBPF enabling \"SYNC\" chart, because it was started with the option \"[-]-sync\".");
  1380. #endif
  1381. break;
  1382. }
  1383. case EBPF_MODULE_DCSTAT_IDX: {
  1384. select_threads |= 1<<EBPF_MODULE_DCSTAT_IDX;
  1385. #ifdef NETDATA_INTERNAL_CHECKS
  1386. info("EBPF enabling \"DCSTAT\" charts, because it was started with the option \"[-]-dcstat\".");
  1387. #endif
  1388. break;
  1389. }
  1390. case EBPF_MODULE_SWAP_IDX: {
  1391. select_threads |= 1<<EBPF_MODULE_SWAP_IDX;
  1392. #ifdef NETDATA_INTERNAL_CHECKS
  1393. info("EBPF enabling \"SWAP\" chart, because it was started with the option \"[-]-swap\".");
  1394. #endif
  1395. break;
  1396. }
  1397. case EBPF_MODULE_VFS_IDX: {
  1398. select_threads |= 1<<EBPF_MODULE_VFS_IDX;
  1399. #ifdef NETDATA_INTERNAL_CHECKS
  1400. info("EBPF enabling \"VFS\" chart, because it was started with the option \"[-]-vfs\".");
  1401. #endif
  1402. break;
  1403. }
  1404. case EBPF_MODULE_FILESYSTEM_IDX: {
  1405. select_threads |= 1<<EBPF_MODULE_FILESYSTEM_IDX;
  1406. #ifdef NETDATA_INTERNAL_CHECKS
  1407. info("EBPF enabling \"FILESYSTEM\" chart, because it was started with the option \"[-]-filesystem\".");
  1408. #endif
  1409. break;
  1410. }
  1411. case EBPF_MODULE_DISK_IDX: {
  1412. select_threads |= 1<<EBPF_MODULE_DISK_IDX;
  1413. #ifdef NETDATA_INTERNAL_CHECKS
  1414. info("EBPF enabling \"DISK\" chart, because it was started with the option \"[-]-disk\".");
  1415. #endif
  1416. break;
  1417. }
  1418. case EBPF_MODULE_MOUNT_IDX: {
  1419. select_threads |= 1<<EBPF_MODULE_MOUNT_IDX;
  1420. #ifdef NETDATA_INTERNAL_CHECKS
  1421. info("EBPF enabling \"MOUNT\" chart, because it was started with the option \"[-]-mount\".");
  1422. #endif
  1423. break;
  1424. }
  1425. case EBPF_MODULE_FD_IDX: {
  1426. select_threads |= 1<<EBPF_MODULE_FD_IDX;
  1427. #ifdef NETDATA_INTERNAL_CHECKS
  1428. info("EBPF enabling \"FILEDESCRIPTOR\" chart, because it was started with the option \"[-]-filedescriptor\".");
  1429. #endif
  1430. break;
  1431. }
  1432. case EBPF_MODULE_HARDIRQ_IDX: {
  1433. select_threads |= 1<<EBPF_MODULE_HARDIRQ_IDX;
  1434. #ifdef NETDATA_INTERNAL_CHECKS
  1435. info("EBPF enabling \"HARDIRQ\" chart, because it was started with the option \"[-]-hardirq\".");
  1436. #endif
  1437. break;
  1438. }
  1439. case EBPF_MODULE_SOFTIRQ_IDX: {
  1440. select_threads |= 1<<EBPF_MODULE_SOFTIRQ_IDX;
  1441. #ifdef NETDATA_INTERNAL_CHECKS
  1442. info("EBPF enabling \"SOFTIRQ\" chart, because it was started with the option \"[-]-softirq\".");
  1443. #endif
  1444. break;
  1445. }
  1446. case EBPF_MODULE_OOMKILL_IDX: {
  1447. select_threads |= 1<<EBPF_MODULE_OOMKILL_IDX;
  1448. #ifdef NETDATA_INTERNAL_CHECKS
  1449. info("EBPF enabling \"OOMKILL\" chart, because it was started with the option \"[-]-oomkill\".");
  1450. #endif
  1451. break;
  1452. }
  1453. case EBPF_MODULE_SHM_IDX: {
  1454. select_threads |= 1<<EBPF_MODULE_SHM_IDX;
  1455. #ifdef NETDATA_INTERNAL_CHECKS
  1456. info("EBPF enabling \"SHM\" chart, because it was started with the option \"[-]-shm\".");
  1457. #endif
  1458. break;
  1459. }
  1460. case EBPF_MODULE_MDFLUSH_IDX: {
  1461. select_threads |= 1<<EBPF_MODULE_MDFLUSH_IDX;
  1462. #ifdef NETDATA_INTERNAL_CHECKS
  1463. info("EBPF enabling \"MDFLUSH\" chart, because it was started with the option \"[-]-mdflush\".");
  1464. #endif
  1465. break;
  1466. }
  1467. case EBPF_OPTION_ALL_CHARTS: {
  1468. disable_apps = 0;
  1469. disable_cgroups = 0;
  1470. #ifdef NETDATA_INTERNAL_CHECKS
  1471. info("EBPF running with all chart groups, because it was started with the option \"[-]-all\".");
  1472. #endif
  1473. break;
  1474. }
  1475. case EBPF_OPTION_VERSION: {
  1476. printf("ebpf.plugin %s\n", VERSION);
  1477. exit(0);
  1478. }
  1479. case EBPF_OPTION_HELP: {
  1480. ebpf_print_help();
  1481. exit(0);
  1482. }
  1483. case EBPF_OPTION_GLOBAL_CHART: {
  1484. disable_apps = 1;
  1485. disable_cgroups = 1;
  1486. #ifdef NETDATA_INTERNAL_CHECKS
  1487. info("EBPF running with global chart group, because it was started with the option \"[-]-global\".");
  1488. #endif
  1489. break;
  1490. }
  1491. case EBPF_OPTION_RETURN_MODE: {
  1492. ebpf_set_thread_mode(MODE_RETURN);
  1493. #ifdef NETDATA_INTERNAL_CHECKS
  1494. info("EBPF running in \"RETURN\" mode, because it was started with the option \"[-]-return\".");
  1495. #endif
  1496. break;
  1497. }
  1498. case EBPF_OPTION_LEGACY: {
  1499. ebpf_set_load_mode(EBPF_LOAD_LEGACY);
  1500. #ifdef NETDATA_INTERNAL_CHECKS
  1501. info("EBPF running with \"LEGACY\" code, because it was started with the option \"[-]-legacy\".");
  1502. #endif
  1503. break;
  1504. }
  1505. case EBPF_OPTION_CORE: {
  1506. ebpf_set_load_mode(EBPF_LOAD_CORE);
  1507. #ifdef NETDATA_INTERNAL_CHECKS
  1508. info("EBPF running with \"CO-RE\" code, because it was started with the option \"[-]-core\".");
  1509. #endif
  1510. break;
  1511. }
  1512. default: {
  1513. break;
  1514. }
  1515. }
  1516. }
  1517. if (disable_apps || disable_cgroups) {
  1518. if (disable_apps)
  1519. ebpf_disable_apps();
  1520. if (disable_cgroups)
  1521. ebpf_disable_cgroups();
  1522. }
  1523. if (select_threads) {
  1524. disable_all_global_charts();
  1525. uint64_t idx;
  1526. for (idx = 0; idx < EBPF_OPTION_ALL_CHARTS; idx++) {
  1527. if (select_threads & 1<<idx)
  1528. ebpf_enable_specific_chart(&ebpf_modules[idx], disable_apps, disable_cgroups);
  1529. }
  1530. }
  1531. // Load apps_groups.conf
  1532. if (ebpf_read_apps_groups_conf(
  1533. &apps_groups_default_target, &apps_groups_root_target, ebpf_user_config_dir, "groups")) {
  1534. info("Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'",
  1535. ebpf_user_config_dir, ebpf_stock_config_dir);
  1536. if (ebpf_read_apps_groups_conf(
  1537. &apps_groups_default_target, &apps_groups_root_target, ebpf_stock_config_dir, "groups")) {
  1538. error("Cannot read process groups '%s/apps_groups.conf'. There are no internal defaults. Failing.",
  1539. ebpf_stock_config_dir);
  1540. thread_finished++;
  1541. ebpf_exit(1);
  1542. }
  1543. } else
  1544. info("Loaded config file '%s/apps_groups.conf'", ebpf_user_config_dir);
  1545. }
  1546. /*****************************************************************
  1547. *
  1548. * COLLECTOR ENTRY POINT
  1549. *
  1550. *****************************************************************/
  1551. /**
  1552. * Update PID file
  1553. *
  1554. * Update the content of PID file
  1555. *
  1556. * @param filename is the full name of the file.
  1557. * @param pid that identifies the process
  1558. */
  1559. static void ebpf_update_pid_file(char *filename, pid_t pid)
  1560. {
  1561. FILE *fp = fopen(filename, "w");
  1562. if (!fp)
  1563. return;
  1564. fprintf(fp, "%d", pid);
  1565. fclose(fp);
  1566. }
  1567. /**
  1568. * Get Process Name
  1569. *
  1570. * Get process name from /proc/PID/status
  1571. *
  1572. * @param pid that identifies the process
  1573. */
  1574. static char *ebpf_get_process_name(pid_t pid)
  1575. {
  1576. char *name = NULL;
  1577. char filename[FILENAME_MAX + 1];
  1578. snprintfz(filename, FILENAME_MAX, "/proc/%d/status", pid);
  1579. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  1580. if(unlikely(!ff)) {
  1581. error("Cannot open %s", filename);
  1582. return name;
  1583. }
  1584. ff = procfile_readall(ff);
  1585. if(unlikely(!ff))
  1586. return name;
  1587. unsigned long i, lines = procfile_lines(ff);
  1588. for(i = 0; i < lines ; i++) {
  1589. char *cmp = procfile_lineword(ff, i, 0);
  1590. if (!strcmp(cmp, "Name:")) {
  1591. name = strdupz(procfile_lineword(ff, i, 1));
  1592. break;
  1593. }
  1594. }
  1595. procfile_close(ff);
  1596. return name;
  1597. }
  1598. /**
  1599. * Read Previous PID
  1600. *
  1601. * @param filename is the full name of the file.
  1602. *
  1603. * @return It returns the PID used during previous execution on success or 0 otherwise
  1604. */
  1605. static pid_t ebpf_read_previous_pid(char *filename)
  1606. {
  1607. FILE *fp = fopen(filename, "r");
  1608. if (!fp)
  1609. return 0;
  1610. char buffer[64];
  1611. size_t length = fread(buffer, sizeof(*buffer), 63, fp);
  1612. pid_t old_pid = 0;
  1613. if (length) {
  1614. if (length > 63)
  1615. length = 63;
  1616. buffer[length] = '\0';
  1617. old_pid = (pid_t)str2uint32_t(buffer);
  1618. }
  1619. fclose(fp);
  1620. return old_pid;
  1621. }
  1622. /**
  1623. * Kill previous process
  1624. *
  1625. * Kill previous process whether it was not closed.
  1626. *
  1627. * @param filename is the full name of the file.
  1628. * @param pid that identifies the process
  1629. */
  1630. static void ebpf_kill_previous_process(char *filename, pid_t pid)
  1631. {
  1632. pid_t old_pid = ebpf_read_previous_pid(filename);
  1633. if (!old_pid)
  1634. return;
  1635. // Process is not running
  1636. char *prev_name = ebpf_get_process_name(old_pid);
  1637. if (!prev_name)
  1638. return;
  1639. char *current_name = ebpf_get_process_name(pid);
  1640. if (!strcmp(prev_name, current_name))
  1641. kill(old_pid, SIGKILL);
  1642. freez(prev_name);
  1643. freez(current_name);
  1644. // wait few microseconds before start new plugin
  1645. sleep_usec(USEC_PER_MS * 300);
  1646. }
  1647. /**
  1648. * PID file
  1649. *
  1650. * Write the filename for PID inside the given vector.
  1651. *
  1652. * @param filename vector where we will store the name.
  1653. * @param length number of bytes available in filename vector
  1654. */
  1655. void ebpf_pid_file(char *filename, size_t length)
  1656. {
  1657. snprintfz(filename, length, "%s%s/ebpf.d/ebpf.pid", netdata_configured_host_prefix, ebpf_plugin_dir);
  1658. }
  1659. /**
  1660. * Manage PID
  1661. *
  1662. * This function kills another instance of eBPF whether it is necessary and update the file content.
  1663. *
  1664. * @param pid that identifies the process
  1665. */
  1666. static void ebpf_manage_pid(pid_t pid)
  1667. {
  1668. char filename[FILENAME_MAX + 1];
  1669. ebpf_pid_file(filename, FILENAME_MAX);
  1670. ebpf_kill_previous_process(filename, pid);
  1671. ebpf_update_pid_file(filename, pid);
  1672. }
  1673. /**
  1674. * Entry point
  1675. *
  1676. * @param argc the number of arguments
  1677. * @param argv the pointer to the arguments
  1678. *
  1679. * @return it returns 0 on success and another integer otherwise
  1680. */
  1681. int main(int argc, char **argv)
  1682. {
  1683. clocks_init();
  1684. set_global_variables();
  1685. ebpf_parse_args(argc, argv);
  1686. ebpf_manage_pid(getpid());
  1687. if (!has_condition_to_run(running_on_kernel)) {
  1688. error("The current collector cannot run on this kernel.");
  1689. return 2;
  1690. }
  1691. if (!am_i_running_as_root()) {
  1692. error(
  1693. "ebpf.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities..",
  1694. (unsigned int)getuid(), (unsigned int)geteuid());
  1695. return 3;
  1696. }
  1697. // set name
  1698. program_name = "ebpf.plugin";
  1699. // disable syslog
  1700. error_log_syslog = 0;
  1701. // set errors flood protection to 100 logs per hour
  1702. error_log_errors_per_period = 100;
  1703. error_log_throttle_period = 3600;
  1704. struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
  1705. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  1706. error("Setrlimit(RLIMIT_MEMLOCK)");
  1707. return 4;
  1708. }
  1709. signal(SIGINT, ebpf_exit);
  1710. signal(SIGTERM, ebpf_exit);
  1711. signal(SIGPIPE, ebpf_exit);
  1712. if (ebpf_start_pthread_variables()) {
  1713. thread_finished++;
  1714. error("Cannot start mutex to control overall charts.");
  1715. ebpf_exit(5);
  1716. }
  1717. ebpf_allocate_common_vectors();
  1718. #ifdef LIBBPF_MAJOR_VERSION
  1719. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  1720. #endif
  1721. read_local_addresses();
  1722. read_local_ports("/proc/net/tcp", IPPROTO_TCP);
  1723. read_local_ports("/proc/net/tcp6", IPPROTO_TCP);
  1724. read_local_ports("/proc/net/udp", IPPROTO_UDP);
  1725. read_local_ports("/proc/net/udp6", IPPROTO_UDP);
  1726. struct netdata_static_thread ebpf_threads[] = {
  1727. {"EBPF PROCESS", NULL, NULL, 1,
  1728. NULL, NULL, ebpf_modules[EBPF_MODULE_PROCESS_IDX].start_routine},
  1729. {"EBPF SOCKET" , NULL, NULL, 1,
  1730. NULL, NULL, ebpf_modules[EBPF_MODULE_SOCKET_IDX].start_routine},
  1731. {"EBPF CACHESTAT" , NULL, NULL, 1,
  1732. NULL, NULL, ebpf_modules[EBPF_MODULE_CACHESTAT_IDX].start_routine},
  1733. {"EBPF SYNC" , NULL, NULL, 1,
  1734. NULL, NULL, ebpf_modules[EBPF_MODULE_SYNC_IDX].start_routine},
  1735. {"EBPF DCSTAT" , NULL, NULL, 1,
  1736. NULL, NULL, ebpf_modules[EBPF_MODULE_DCSTAT_IDX].start_routine},
  1737. {"EBPF SWAP" , NULL, NULL, 1,
  1738. NULL, NULL, ebpf_modules[EBPF_MODULE_SWAP_IDX].start_routine},
  1739. {"EBPF VFS" , NULL, NULL, 1,
  1740. NULL, NULL, ebpf_modules[EBPF_MODULE_VFS_IDX].start_routine},
  1741. {"EBPF FILESYSTEM" , NULL, NULL, 1,
  1742. NULL, NULL, ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].start_routine},
  1743. {"EBPF DISK" , NULL, NULL, 1,
  1744. NULL, NULL, ebpf_modules[EBPF_MODULE_DISK_IDX].start_routine},
  1745. {"EBPF MOUNT" , NULL, NULL, 1,
  1746. NULL, NULL, ebpf_modules[EBPF_MODULE_MOUNT_IDX].start_routine},
  1747. {"EBPF FD" , NULL, NULL, 1,
  1748. NULL, NULL, ebpf_modules[EBPF_MODULE_FD_IDX].start_routine},
  1749. {"EBPF HARDIRQ" , NULL, NULL, 1,
  1750. NULL, NULL, ebpf_modules[EBPF_MODULE_HARDIRQ_IDX].start_routine},
  1751. {"EBPF SOFTIRQ" , NULL, NULL, 1,
  1752. NULL, NULL, ebpf_modules[EBPF_MODULE_SOFTIRQ_IDX].start_routine},
  1753. {"EBPF OOMKILL" , NULL, NULL, 1,
  1754. NULL, NULL, ebpf_modules[EBPF_MODULE_OOMKILL_IDX].start_routine},
  1755. {"EBPF SHM" , NULL, NULL, 1,
  1756. NULL, NULL, ebpf_modules[EBPF_MODULE_SHM_IDX].start_routine},
  1757. {"EBPF MDFLUSH" , NULL, NULL, 1,
  1758. NULL, NULL, ebpf_modules[EBPF_MODULE_MDFLUSH_IDX].start_routine},
  1759. {NULL , NULL, NULL, 0,
  1760. NULL, NULL, NULL}
  1761. };
  1762. //clean_loaded_events();
  1763. int i;
  1764. for (i = 0; ebpf_threads[i].name != NULL; i++) {
  1765. struct netdata_static_thread *st = &ebpf_threads[i];
  1766. st->thread = mallocz(sizeof(netdata_thread_t));
  1767. ebpf_module_t *em = &ebpf_modules[i];
  1768. em->thread_id = i;
  1769. netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_JOINABLE, st->start_routine, em);
  1770. }
  1771. for (i = 0; ebpf_threads[i].name != NULL; i++) {
  1772. struct netdata_static_thread *st = &ebpf_threads[i];
  1773. netdata_thread_join(*st->thread, NULL);
  1774. }
  1775. thread_finished++;
  1776. ebpf_exit(0);
  1777. return 0;
  1778. }