ebpf.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682
  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 "ebpf_unittest.h"
  8. #include "libnetdata/required_dummies.h"
  9. /*****************************************************************
  10. *
  11. * GLOBAL VARIABLES
  12. *
  13. *****************************************************************/
  14. char *ebpf_plugin_dir = PLUGINS_DIR;
  15. static char *ebpf_configured_log_dir = LOG_DIR;
  16. char *ebpf_algorithms[] = {"absolute", "incremental"};
  17. struct config collector_config = { .first_section = NULL,
  18. .last_section = NULL,
  19. .mutex = NETDATA_MUTEX_INITIALIZER,
  20. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  21. .rwlock = AVL_LOCK_INITIALIZER } };
  22. int running_on_kernel = 0;
  23. int ebpf_nprocs;
  24. int isrh = 0;
  25. int main_thread_id = 0;
  26. int process_pid_fd = -1;
  27. pthread_mutex_t lock;
  28. pthread_mutex_t ebpf_exit_cleanup;
  29. pthread_mutex_t collect_data_mutex;
  30. struct netdata_static_thread cgroup_integration_thread = {
  31. .name = "EBPF CGROUP INT",
  32. .config_section = NULL,
  33. .config_name = NULL,
  34. .env_name = NULL,
  35. .enabled = 1,
  36. .thread = NULL,
  37. .init_routine = NULL,
  38. .start_routine = NULL
  39. };
  40. ebpf_module_t ebpf_modules[] = {
  41. { .thread_name = "process", .config_name = "process", .enabled = 0, .start_routine = ebpf_process_thread,
  42. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  43. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  44. .apps_routine = ebpf_process_create_apps_charts, .maps = NULL,
  45. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &process_config,
  46. .config_file = NETDATA_PROCESS_CONFIG_FILE,
  47. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_10 |
  48. NETDATA_V5_14,
  49. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  50. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  51. { .thread_name = "socket", .config_name = "socket", .enabled = 0, .start_routine = ebpf_socket_thread,
  52. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  53. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  54. .apps_routine = ebpf_socket_create_apps_charts, .maps = NULL,
  55. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &socket_config,
  56. .config_file = NETDATA_NETWORK_CONFIG_FILE,
  57. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  58. .load = EBPF_LOAD_LEGACY, .targets = socket_targets, .probe_links = NULL, .objects = NULL,
  59. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  60. { .thread_name = "cachestat", .config_name = "cachestat", .enabled = 0, .start_routine = ebpf_cachestat_thread,
  61. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  62. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  63. .apps_routine = ebpf_cachestat_create_apps_charts, .maps = cachestat_maps,
  64. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &cachestat_config,
  65. .config_file = NETDATA_CACHESTAT_CONFIG_FILE,
  66. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18|
  67. NETDATA_V5_4 | NETDATA_V5_14 | NETDATA_V5_15 | NETDATA_V5_16,
  68. .load = EBPF_LOAD_LEGACY, .targets = cachestat_targets, .probe_links = NULL, .objects = NULL,
  69. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  70. { .thread_name = "sync", .config_name = "sync", .enabled = 0, .start_routine = ebpf_sync_thread,
  71. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  72. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  73. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &sync_config,
  74. .config_file = NETDATA_SYNC_CONFIG_FILE,
  75. // All syscalls have the same kernels
  76. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  77. .load = EBPF_LOAD_LEGACY, .targets = sync_targets, .probe_links = NULL, .objects = NULL,
  78. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  79. { .thread_name = "dc", .config_name = "dc", .enabled = 0, .start_routine = ebpf_dcstat_thread,
  80. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  81. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  82. .apps_routine = ebpf_dcstat_create_apps_charts, .maps = dcstat_maps,
  83. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &dcstat_config,
  84. .config_file = NETDATA_DIRECTORY_DCSTAT_CONFIG_FILE,
  85. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  86. .load = EBPF_LOAD_LEGACY, .targets = dc_targets, .probe_links = NULL, .objects = NULL,
  87. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  88. { .thread_name = "swap", .config_name = "swap", .enabled = 0, .start_routine = ebpf_swap_thread,
  89. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  90. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  91. .apps_routine = ebpf_swap_create_apps_charts, .maps = NULL,
  92. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &swap_config,
  93. .config_file = NETDATA_DIRECTORY_SWAP_CONFIG_FILE,
  94. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  95. .load = EBPF_LOAD_LEGACY, .targets = swap_targets, .probe_links = NULL, .objects = NULL,
  96. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  97. { .thread_name = "vfs", .config_name = "vfs", .enabled = 0, .start_routine = ebpf_vfs_thread,
  98. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  99. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  100. .apps_routine = ebpf_vfs_create_apps_charts, .maps = NULL,
  101. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &vfs_config,
  102. .config_file = NETDATA_DIRECTORY_VFS_CONFIG_FILE,
  103. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  104. .load = EBPF_LOAD_LEGACY, .targets = vfs_targets, .probe_links = NULL, .objects = NULL,
  105. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  106. { .thread_name = "filesystem", .config_name = "filesystem", .enabled = 0, .start_routine = ebpf_filesystem_thread,
  107. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  108. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  109. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fs_config,
  110. .config_file = NETDATA_FILESYSTEM_CONFIG_FILE,
  111. //We are setting kernels as zero, because we load eBPF programs according the kernel running.
  112. .kernels = 0, .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  113. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES },
  114. { .thread_name = "disk", .config_name = "disk", .enabled = 0, .start_routine = ebpf_disk_thread,
  115. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  116. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  117. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &disk_config,
  118. .config_file = NETDATA_DISK_CONFIG_FILE,
  119. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  120. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  121. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  122. { .thread_name = "mount", .config_name = "mount", .enabled = 0, .start_routine = ebpf_mount_thread,
  123. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  124. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  125. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &mount_config,
  126. .config_file = NETDATA_MOUNT_CONFIG_FILE,
  127. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  128. .load = EBPF_LOAD_LEGACY, .targets = mount_targets, .probe_links = NULL, .objects = NULL,
  129. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  130. { .thread_name = "fd", .config_name = "fd", .enabled = 0, .start_routine = ebpf_fd_thread,
  131. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  132. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  133. .apps_routine = ebpf_fd_create_apps_charts, .maps = NULL,
  134. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fd_config,
  135. .config_file = NETDATA_FD_CONFIG_FILE,
  136. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_11 |
  137. NETDATA_V5_14,
  138. .load = EBPF_LOAD_LEGACY, .targets = fd_targets, .probe_links = NULL, .objects = NULL,
  139. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  140. { .thread_name = "hardirq", .config_name = "hardirq", .enabled = 0, .start_routine = ebpf_hardirq_thread,
  141. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  142. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  143. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &hardirq_config,
  144. .config_file = NETDATA_HARDIRQ_CONFIG_FILE,
  145. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  146. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  147. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  148. { .thread_name = "softirq", .config_name = "softirq", .enabled = 0, .start_routine = ebpf_softirq_thread,
  149. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  150. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  151. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &softirq_config,
  152. .config_file = NETDATA_SOFTIRQ_CONFIG_FILE,
  153. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  154. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  155. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  156. { .thread_name = "oomkill", .config_name = "oomkill", .enabled = 0, .start_routine = ebpf_oomkill_thread,
  157. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  158. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  159. .apps_routine = ebpf_oomkill_create_apps_charts, .maps = NULL,
  160. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &oomkill_config,
  161. .config_file = NETDATA_OOMKILL_CONFIG_FILE,
  162. .kernels = NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  163. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  164. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  165. { .thread_name = "shm", .config_name = "shm", .enabled = 0, .start_routine = ebpf_shm_thread,
  166. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  167. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  168. .apps_routine = ebpf_shm_create_apps_charts, .maps = NULL,
  169. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &shm_config,
  170. .config_file = NETDATA_DIRECTORY_SHM_CONFIG_FILE,
  171. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  172. .load = EBPF_LOAD_LEGACY, .targets = shm_targets, .probe_links = NULL, .objects = NULL,
  173. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  174. { .thread_name = "mdflush", .config_name = "mdflush", .enabled = 0, .start_routine = ebpf_mdflush_thread,
  175. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  176. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  177. .apps_routine = NULL, .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &mdflush_config,
  178. .config_file = NETDATA_DIRECTORY_MDFLUSH_CONFIG_FILE,
  179. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  180. .load = EBPF_LOAD_LEGACY, .targets = mdflush_targets, .probe_links = NULL, .objects = NULL,
  181. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  182. { .thread_name = NULL, .enabled = 0, .start_routine = NULL, .update_every = EBPF_DEFAULT_UPDATE_EVERY,
  183. .global_charts = 0, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO, .apps_level = NETDATA_APPS_NOT_SET,
  184. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .apps_routine = NULL, .maps = NULL,
  185. .pid_map_size = 0, .names = NULL, .cfg = NULL, .config_name = NULL, .kernels = 0, .load = EBPF_LOAD_LEGACY,
  186. .targets = NULL, .probe_links = NULL, .objects = NULL, .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  187. };
  188. struct netdata_static_thread ebpf_threads[] = {
  189. {
  190. .name = "EBPF PROCESS",
  191. .config_section = NULL,
  192. .config_name = NULL,
  193. .env_name = NULL,
  194. .enabled = 1,
  195. .thread = NULL,
  196. .init_routine = NULL,
  197. .start_routine = NULL
  198. },
  199. {
  200. .name = "EBPF SOCKET",
  201. .config_section = NULL,
  202. .config_name = NULL,
  203. .env_name = NULL,
  204. .enabled = 1,
  205. .thread = NULL,
  206. .init_routine = NULL,
  207. .start_routine = NULL
  208. },
  209. {
  210. .name = "EBPF CACHESTAT",
  211. .config_section = NULL,
  212. .config_name = NULL,
  213. .env_name = NULL,
  214. .enabled = 1,
  215. .thread = NULL,
  216. .init_routine = NULL,
  217. .start_routine = NULL
  218. },
  219. {
  220. .name = "EBPF SYNC",
  221. .config_section = NULL,
  222. .config_name = NULL,
  223. .env_name = NULL,
  224. .enabled = 1,
  225. .thread = NULL,
  226. .init_routine = NULL,
  227. .start_routine = NULL
  228. },
  229. {
  230. .name = "EBPF DCSTAT",
  231. .config_section = NULL,
  232. .config_name = NULL,
  233. .env_name = NULL,
  234. .enabled = 1,
  235. .thread = NULL,
  236. .init_routine = NULL,
  237. .start_routine = NULL
  238. },
  239. {
  240. .name = "EBPF SWAP",
  241. .config_section = NULL,
  242. .config_name = NULL,
  243. .env_name = NULL,
  244. .enabled = 1,
  245. .thread = NULL,
  246. .init_routine = NULL,
  247. .start_routine = NULL
  248. },
  249. {
  250. .name = "EBPF VFS",
  251. .config_section = NULL,
  252. .config_name = NULL,
  253. .env_name = NULL,
  254. .enabled = 1,
  255. .thread = NULL,
  256. .init_routine = NULL,
  257. .start_routine = NULL
  258. },
  259. {
  260. .name = "EBPF FILESYSTEM",
  261. .config_section = NULL,
  262. .config_name = NULL,
  263. .env_name = NULL,
  264. .enabled = 1,
  265. .thread = NULL,
  266. .init_routine = NULL,
  267. .start_routine = NULL
  268. },
  269. {
  270. .name = "EBPF DISK",
  271. .config_section = NULL,
  272. .config_name = NULL,
  273. .env_name = NULL,
  274. .enabled = 1,
  275. .thread = NULL,
  276. .init_routine = NULL,
  277. .start_routine = NULL
  278. },
  279. {
  280. .name = "EBPF MOUNT",
  281. .config_section = NULL,
  282. .config_name = NULL,
  283. .env_name = NULL,
  284. .enabled = 1,
  285. .thread = NULL,
  286. .init_routine = NULL,
  287. .start_routine = NULL
  288. },
  289. {
  290. .name = "EBPF FD",
  291. .config_section = NULL,
  292. .config_name = NULL,
  293. .env_name = NULL,
  294. .enabled = 1,
  295. .thread = NULL,
  296. .init_routine = NULL,
  297. .start_routine = NULL
  298. },
  299. {
  300. .name = "EBPF HARDIRQ",
  301. .config_section = NULL,
  302. .config_name = NULL,
  303. .env_name = NULL,
  304. .enabled = 1,
  305. .thread = NULL,
  306. .init_routine = NULL,
  307. .start_routine = NULL
  308. },
  309. {
  310. .name = "EBPF SOFTIRQ",
  311. .config_section = NULL,
  312. .config_name = NULL,
  313. .env_name = NULL,
  314. .enabled = 1,
  315. .thread = NULL,
  316. .init_routine = NULL,
  317. .start_routine = NULL
  318. },
  319. {
  320. .name = "EBPF OOMKILL",
  321. .config_section = NULL,
  322. .config_name = NULL,
  323. .env_name = NULL,
  324. .enabled = 1,
  325. .thread = NULL,
  326. .init_routine = NULL,
  327. .start_routine = NULL
  328. },
  329. {
  330. .name = "EBPF SHM",
  331. .config_section = NULL,
  332. .config_name = NULL,
  333. .env_name = NULL,
  334. .enabled = 1,
  335. .thread = NULL,
  336. .init_routine = NULL,
  337. .start_routine = NULL
  338. },
  339. {
  340. .name = "EBPF MDFLUSH",
  341. .config_section = NULL,
  342. .config_name = NULL,
  343. .env_name = NULL,
  344. .enabled = 1,
  345. .thread = NULL,
  346. .init_routine = NULL,
  347. .start_routine = NULL
  348. },
  349. {
  350. .name = NULL,
  351. .config_section = NULL,
  352. .config_name = NULL,
  353. .env_name = NULL,
  354. .enabled = 0,
  355. .thread = NULL,
  356. .init_routine = NULL,
  357. .start_routine = NULL
  358. },
  359. };
  360. ebpf_filesystem_partitions_t localfs[] =
  361. {{.filesystem = "ext4",
  362. .optional_filesystem = NULL,
  363. .family = "ext4",
  364. .objects = NULL,
  365. .probe_links = NULL,
  366. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  367. .enabled = CONFIG_BOOLEAN_YES,
  368. .addresses = {.function = NULL, .addr = 0},
  369. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  370. .fs_maps = NULL,
  371. .fs_obj = NULL,
  372. .functions = { "ext4_file_read_iter",
  373. "ext4_file_write_iter",
  374. "ext4_file_open",
  375. "ext4_sync_file",
  376. NULL }},
  377. {.filesystem = "xfs",
  378. .optional_filesystem = NULL,
  379. .family = "xfs",
  380. .objects = NULL,
  381. .probe_links = NULL,
  382. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  383. .enabled = CONFIG_BOOLEAN_YES,
  384. .addresses = {.function = NULL, .addr = 0},
  385. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  386. .fs_maps = NULL,
  387. .fs_obj = NULL,
  388. .functions = { "xfs_file_read_iter",
  389. "xfs_file_write_iter",
  390. "xfs_file_open",
  391. "xfs_file_fsync",
  392. NULL }},
  393. {.filesystem = "nfs",
  394. .optional_filesystem = "nfs4",
  395. .family = "nfs",
  396. .objects = NULL,
  397. .probe_links = NULL,
  398. .flags = NETDATA_FILESYSTEM_ATTR_CHARTS,
  399. .enabled = CONFIG_BOOLEAN_YES,
  400. .addresses = {.function = NULL, .addr = 0},
  401. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  402. .fs_maps = NULL,
  403. .fs_obj = NULL,
  404. .functions = { "nfs_file_read",
  405. "nfs_file_write",
  406. "nfs_open",
  407. "nfs_getattr",
  408. NULL }}, // // "nfs4_file_open" - not present on all kernels
  409. {.filesystem = "zfs",
  410. .optional_filesystem = NULL,
  411. .family = "zfs",
  412. .objects = NULL,
  413. .probe_links = NULL,
  414. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  415. .enabled = CONFIG_BOOLEAN_YES,
  416. .addresses = {.function = NULL, .addr = 0},
  417. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  418. .fs_maps = NULL,
  419. .fs_obj = NULL,
  420. .functions = { "zpl_iter_read",
  421. "zpl_iter_write",
  422. "zpl_open",
  423. "zpl_fsync",
  424. NULL }},
  425. {.filesystem = "btrfs",
  426. .optional_filesystem = NULL,
  427. .family = "btrfs",
  428. .objects = NULL,
  429. .probe_links = NULL,
  430. .flags = NETDATA_FILESYSTEM_FILL_ADDRESS_TABLE,
  431. .enabled = CONFIG_BOOLEAN_YES,
  432. .addresses = {.function = "btrfs_file_operations", .addr = 0},
  433. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_10,
  434. .fs_maps = NULL,
  435. .fs_obj = NULL,
  436. .functions = { "btrfs_file_read_iter",
  437. "btrfs_file_write_iter",
  438. "btrfs_file_open",
  439. "btrfs_sync_file",
  440. NULL }},
  441. {.filesystem = NULL,
  442. .optional_filesystem = NULL,
  443. .family = NULL,
  444. .objects = NULL,
  445. .probe_links = NULL,
  446. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  447. .enabled = CONFIG_BOOLEAN_YES,
  448. .addresses = {.function = NULL, .addr = 0},
  449. .kernels = 0, .fs_maps = NULL, .fs_obj = NULL}};
  450. ebpf_sync_syscalls_t local_syscalls[] = {
  451. {.syscall = NETDATA_SYSCALLS_SYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  452. #ifdef LIBBPF_MAJOR_VERSION
  453. .sync_obj = NULL,
  454. #endif
  455. .sync_maps = NULL
  456. },
  457. {.syscall = NETDATA_SYSCALLS_SYNCFS, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  458. #ifdef LIBBPF_MAJOR_VERSION
  459. .sync_obj = NULL,
  460. #endif
  461. .sync_maps = NULL
  462. },
  463. {.syscall = NETDATA_SYSCALLS_MSYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  464. #ifdef LIBBPF_MAJOR_VERSION
  465. .sync_obj = NULL,
  466. #endif
  467. .sync_maps = NULL
  468. },
  469. {.syscall = NETDATA_SYSCALLS_FSYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  470. #ifdef LIBBPF_MAJOR_VERSION
  471. .sync_obj = NULL,
  472. #endif
  473. .sync_maps = NULL
  474. },
  475. {.syscall = NETDATA_SYSCALLS_FDATASYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  476. #ifdef LIBBPF_MAJOR_VERSION
  477. .sync_obj = NULL,
  478. #endif
  479. .sync_maps = NULL
  480. },
  481. {.syscall = NETDATA_SYSCALLS_SYNC_FILE_RANGE, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  482. #ifdef LIBBPF_MAJOR_VERSION
  483. .sync_obj = NULL,
  484. #endif
  485. .sync_maps = NULL
  486. },
  487. {.syscall = NULL, .enabled = CONFIG_BOOLEAN_NO, .objects = NULL, .probe_links = NULL,
  488. #ifdef LIBBPF_MAJOR_VERSION
  489. .sync_obj = NULL,
  490. #endif
  491. .sync_maps = NULL
  492. }
  493. };
  494. // Link with cgroup.plugin
  495. netdata_ebpf_cgroup_shm_t shm_ebpf_cgroup = {NULL, NULL};
  496. int shm_fd_ebpf_cgroup = -1;
  497. sem_t *shm_sem_ebpf_cgroup = SEM_FAILED;
  498. pthread_mutex_t mutex_cgroup_shm;
  499. //Network viewer
  500. ebpf_network_viewer_options_t network_viewer_opt;
  501. // Statistic
  502. ebpf_plugin_stats_t plugin_statistics = {.core = 0, .legacy = 0, .running = 0, .threads = 0, .tracepoints = 0,
  503. .probes = 0, .retprobes = 0, .trampolines = 0, .memlock_kern = 0,
  504. .hash_tables = 0};
  505. #ifdef LIBBPF_MAJOR_VERSION
  506. struct btf *default_btf = NULL;
  507. struct cachestat_bpf *cachestat_bpf_obj = NULL;
  508. struct dc_bpf *dc_bpf_obj = NULL;
  509. struct disk_bpf *disk_bpf_obj = NULL;
  510. struct fd_bpf *fd_bpf_obj = NULL;
  511. struct hardirq_bpf *hardirq_bpf_obj = NULL;
  512. struct mdflush_bpf *mdflush_bpf_obj = NULL;
  513. struct mount_bpf *mount_bpf_obj = NULL;
  514. struct shm_bpf *shm_bpf_obj = NULL;
  515. struct socket_bpf *socket_bpf_obj = NULL;
  516. struct swap_bpf *bpf_obj = NULL;
  517. struct vfs_bpf *vfs_bpf_obj = NULL;
  518. #else
  519. void *default_btf = NULL;
  520. #endif
  521. char *btf_path = NULL;
  522. /*****************************************************************
  523. *
  524. * FUNCTIONS USED TO ALLOCATE APPS/CGROUP MEMORIES (ARAL)
  525. *
  526. *****************************************************************/
  527. /**
  528. * Allocate PID ARAL
  529. *
  530. * Allocate memory using ARAL functions to speed up processing.
  531. *
  532. * @param name the internal name used for allocated region.
  533. * @param size size of each element inside allocated space
  534. *
  535. * @return It returns the address on success and NULL otherwise.
  536. */
  537. ARAL *ebpf_allocate_pid_aral(char *name, size_t size)
  538. {
  539. static size_t max_elements = NETDATA_EBPF_ALLOC_MAX_PID;
  540. if (max_elements < NETDATA_EBPF_ALLOC_MIN_ELEMENTS) {
  541. netdata_log_error("Number of elements given is too small, adjusting it for %d", NETDATA_EBPF_ALLOC_MIN_ELEMENTS);
  542. max_elements = NETDATA_EBPF_ALLOC_MIN_ELEMENTS;
  543. }
  544. return aral_create(name, size,
  545. 0, max_elements,
  546. NULL, NULL, NULL, false, false);
  547. }
  548. /*****************************************************************
  549. *
  550. * FUNCTIONS USED TO CLEAN MEMORY AND OPERATE SYSTEM FILES
  551. *
  552. *****************************************************************/
  553. /**
  554. * Wait to avoid possible coredumps while process is closing.
  555. */
  556. static inline void ebpf_check_before2go()
  557. {
  558. int i = EBPF_OPTION_ALL_CHARTS;
  559. usec_t max = USEC_PER_SEC, step = 200000;
  560. while (i && max) {
  561. max -= step;
  562. sleep_usec(step);
  563. i = 0;
  564. int j;
  565. pthread_mutex_lock(&ebpf_exit_cleanup);
  566. for (j = 0; ebpf_modules[j].thread_name != NULL; j++) {
  567. if (ebpf_modules[j].enabled == NETDATA_THREAD_EBPF_RUNNING)
  568. i++;
  569. }
  570. pthread_mutex_unlock(&ebpf_exit_cleanup);
  571. }
  572. if (i) {
  573. netdata_log_error("eBPF cannot unload all threads on time, but it will go away");
  574. }
  575. }
  576. /**
  577. * Close the collector gracefully
  578. */
  579. static void ebpf_exit()
  580. {
  581. #ifdef LIBBPF_MAJOR_VERSION
  582. pthread_mutex_lock(&ebpf_exit_cleanup);
  583. if (default_btf) {
  584. btf__free(default_btf);
  585. default_btf = NULL;
  586. }
  587. pthread_mutex_unlock(&ebpf_exit_cleanup);
  588. #endif
  589. char filename[FILENAME_MAX + 1];
  590. ebpf_pid_file(filename, FILENAME_MAX);
  591. if (unlink(filename))
  592. netdata_log_error("Cannot remove PID file %s", filename);
  593. #ifdef NETDATA_INTERNAL_CHECKS
  594. netdata_log_error("Good bye world! I was PID %d", main_thread_id);
  595. #endif
  596. fprintf(stdout, "EXIT\n");
  597. fflush(stdout);
  598. ebpf_check_before2go();
  599. pthread_mutex_lock(&mutex_cgroup_shm);
  600. if (shm_ebpf_cgroup.header) {
  601. ebpf_unmap_cgroup_shared_memory();
  602. shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
  603. }
  604. pthread_mutex_unlock(&mutex_cgroup_shm);
  605. exit(0);
  606. }
  607. /**
  608. * Unload loegacy code
  609. *
  610. * @param objects objects loaded from eBPF programs
  611. * @param probe_links links from loader
  612. */
  613. void ebpf_unload_legacy_code(struct bpf_object *objects, struct bpf_link **probe_links)
  614. {
  615. if (!probe_links || !objects)
  616. return;
  617. struct bpf_program *prog;
  618. size_t j = 0 ;
  619. bpf_object__for_each_program(prog, objects) {
  620. bpf_link__destroy(probe_links[j]);
  621. j++;
  622. }
  623. freez(probe_links);
  624. if (objects)
  625. bpf_object__close(objects);
  626. }
  627. /**
  628. * Unload Unique maps
  629. *
  630. * This function unload all BPF maps from threads using one unique BPF object.
  631. */
  632. static void ebpf_unload_unique_maps()
  633. {
  634. int i;
  635. for (i = 0; ebpf_modules[i].thread_name; i++) {
  636. // These threads are cleaned with other functions
  637. if (i > EBPF_MODULE_SOCKET_IDX)
  638. continue;
  639. if (ebpf_modules[i].enabled != NETDATA_THREAD_EBPF_STOPPED) {
  640. if (ebpf_modules[i].enabled != NETDATA_THREAD_EBPF_NOT_RUNNING)
  641. netdata_log_error("Cannot unload maps for thread %s, because it is not stopped.", ebpf_modules[i].thread_name);
  642. continue;
  643. }
  644. if (ebpf_modules[i].load == EBPF_LOAD_LEGACY) {
  645. ebpf_unload_legacy_code(ebpf_modules[i].objects, ebpf_modules[i].probe_links);
  646. continue;
  647. }
  648. if (i == EBPF_MODULE_SOCKET_IDX) {
  649. #ifdef LIBBPF_MAJOR_VERSION
  650. if (socket_bpf_obj)
  651. socket_bpf__destroy(socket_bpf_obj);
  652. #endif
  653. }
  654. }
  655. }
  656. /**
  657. * Unload filesystem maps
  658. *
  659. * This function unload all BPF maps from filesystem thread.
  660. */
  661. static void ebpf_unload_filesystems()
  662. {
  663. if (ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].enabled == NETDATA_THREAD_EBPF_NOT_RUNNING ||
  664. ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].enabled == NETDATA_THREAD_EBPF_RUNNING ||
  665. ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].load != EBPF_LOAD_LEGACY)
  666. return;
  667. int i;
  668. for (i = 0; localfs[i].filesystem != NULL; i++) {
  669. if (!localfs[i].objects)
  670. continue;
  671. ebpf_unload_legacy_code(localfs[i].objects, localfs[i].probe_links);
  672. }
  673. }
  674. /**
  675. * Unload sync maps
  676. *
  677. * This function unload all BPF maps from sync thread.
  678. */
  679. static void ebpf_unload_sync()
  680. {
  681. if (ebpf_modules[EBPF_MODULE_SYNC_IDX].enabled == NETDATA_THREAD_EBPF_NOT_RUNNING ||
  682. ebpf_modules[EBPF_MODULE_SYNC_IDX].enabled == NETDATA_THREAD_EBPF_RUNNING)
  683. return;
  684. int i;
  685. for (i = 0; local_syscalls[i].syscall != NULL; i++) {
  686. if (!local_syscalls[i].enabled)
  687. continue;
  688. #ifdef LIBBPF_MAJOR_VERSION
  689. if (local_syscalls[i].sync_obj) {
  690. sync_bpf__destroy(local_syscalls[i].sync_obj);
  691. continue;
  692. }
  693. #endif
  694. ebpf_unload_legacy_code(local_syscalls[i].objects, local_syscalls[i].probe_links);
  695. }
  696. }
  697. int ebpf_exit_plugin = 0;
  698. /**
  699. * Close the collector gracefully
  700. *
  701. * @param sig is the signal number used to close the collector
  702. */
  703. static void ebpf_stop_threads(int sig)
  704. {
  705. UNUSED(sig);
  706. static int only_one = 0;
  707. // Child thread should be closed by itself.
  708. pthread_mutex_lock(&ebpf_exit_cleanup);
  709. if (main_thread_id != gettid() || only_one) {
  710. pthread_mutex_unlock(&ebpf_exit_cleanup);
  711. return;
  712. }
  713. only_one = 1;
  714. int i;
  715. for (i = 0; ebpf_modules[i].thread_name != NULL; i++) {
  716. if (ebpf_modules[i].enabled == NETDATA_THREAD_EBPF_RUNNING) {
  717. netdata_thread_cancel(*ebpf_modules[i].thread->thread);
  718. #ifdef NETDATA_DEV_MODE
  719. netdata_log_info("Sending cancel for thread %s", ebpf_modules[i].thread_name);
  720. #endif
  721. }
  722. }
  723. pthread_mutex_unlock(&ebpf_exit_cleanup);
  724. pthread_mutex_lock(&mutex_cgroup_shm);
  725. netdata_thread_cancel(*cgroup_integration_thread.thread);
  726. #ifdef NETDATA_DEV_MODE
  727. netdata_log_info("Sending cancel for thread %s", cgroup_integration_thread.name);
  728. #endif
  729. pthread_mutex_unlock(&mutex_cgroup_shm);
  730. ebpf_exit_plugin = 1;
  731. ebpf_check_before2go();
  732. pthread_mutex_lock(&ebpf_exit_cleanup);
  733. ebpf_unload_unique_maps();
  734. ebpf_unload_filesystems();
  735. ebpf_unload_sync();
  736. pthread_mutex_unlock(&ebpf_exit_cleanup);
  737. ebpf_exit();
  738. }
  739. /*****************************************************************
  740. *
  741. * FUNCTIONS TO CREATE CHARTS
  742. *
  743. *****************************************************************/
  744. /**
  745. * Create apps charts
  746. *
  747. * Call ebpf_create_chart to create the charts on apps submenu.
  748. *
  749. * @param root a pointer for the targets.
  750. */
  751. static void ebpf_create_apps_charts(struct ebpf_target *root)
  752. {
  753. if (unlikely(!ebpf_all_pids))
  754. return;
  755. struct ebpf_target *w;
  756. int newly_added = 0;
  757. for (w = root; w; w = w->next) {
  758. if (w->target)
  759. continue;
  760. if (unlikely(w->processes && (debug_enabled || w->debug_enabled))) {
  761. struct ebpf_pid_on_target *pid_on_target;
  762. fprintf(
  763. stderr, "ebpf.plugin: target '%s' has aggregated %u process%s:", w->name, w->processes,
  764. (w->processes == 1) ? "" : "es");
  765. for (pid_on_target = w->root_pid; pid_on_target; pid_on_target = pid_on_target->next) {
  766. fprintf(stderr, " %d", pid_on_target->pid);
  767. }
  768. fputc('\n', stderr);
  769. }
  770. if (!w->exposed && w->processes) {
  771. newly_added++;
  772. w->exposed = 1;
  773. if (debug_enabled || w->debug_enabled)
  774. debug_log_int("%s just added - regenerating charts.", w->name);
  775. }
  776. }
  777. if (!newly_added)
  778. return;
  779. int counter;
  780. for (counter = 0; ebpf_modules[counter].thread_name; counter++) {
  781. ebpf_module_t *current = &ebpf_modules[counter];
  782. if (current->enabled == NETDATA_THREAD_EBPF_RUNNING && current->apps_charts && current->apps_routine)
  783. current->apps_routine(current, root);
  784. }
  785. }
  786. /**
  787. * Get a value from a structure.
  788. *
  789. * @param basis it is the first address of the structure
  790. * @param offset it is the offset of the data you want to access.
  791. * @return
  792. */
  793. collected_number get_value_from_structure(char *basis, size_t offset)
  794. {
  795. collected_number *value = (collected_number *)(basis + offset);
  796. collected_number ret = (collected_number)llabs(*value);
  797. // this reset is necessary to avoid keep a constant value while processing is not executing a task
  798. *value = 0;
  799. return ret;
  800. }
  801. /**
  802. * Write begin command on standard output
  803. *
  804. * @param family the chart family name
  805. * @param name the chart name
  806. */
  807. void write_begin_chart(char *family, char *name)
  808. {
  809. printf("BEGIN %s.%s\n", family, name);
  810. }
  811. /**
  812. * Write END command on stdout.
  813. */
  814. inline void write_end_chart()
  815. {
  816. printf("END\n");
  817. }
  818. /**
  819. * Write set command on standard output
  820. *
  821. * @param dim the dimension name
  822. * @param value the value for the dimension
  823. */
  824. void write_chart_dimension(char *dim, long long value)
  825. {
  826. printf("SET %s = %lld\n", dim, value);
  827. }
  828. /**
  829. * Call the necessary functions to create a chart.
  830. *
  831. * @param name the chart name
  832. * @param family the chart family
  833. * @param move the pointer with the values that will be published
  834. * @param end the number of values that will be written on standard output
  835. *
  836. * @return It returns a variable that maps the charts that did not have zero values.
  837. */
  838. void write_count_chart(char *name, char *family, netdata_publish_syscall_t *move, uint32_t end)
  839. {
  840. write_begin_chart(family, name);
  841. uint32_t i = 0;
  842. while (move && i < end) {
  843. write_chart_dimension(move->name, move->ncall);
  844. move = move->next;
  845. i++;
  846. }
  847. write_end_chart();
  848. }
  849. /**
  850. * Call the necessary functions to create a chart.
  851. *
  852. * @param name the chart name
  853. * @param family the chart family
  854. * @param move the pointer with the values that will be published
  855. * @param end the number of values that will be written on standard output
  856. */
  857. void write_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end)
  858. {
  859. write_begin_chart(family, name);
  860. int i = 0;
  861. while (move && i < end) {
  862. write_chart_dimension(move->name, move->nerr);
  863. move = move->next;
  864. i++;
  865. }
  866. write_end_chart();
  867. }
  868. /**
  869. * Write charts
  870. *
  871. * Write the current information to publish the charts.
  872. *
  873. * @param family chart family
  874. * @param chart chart id
  875. * @param dim dimension name
  876. * @param v1 value.
  877. */
  878. void ebpf_one_dimension_write_charts(char *family, char *chart, char *dim, long long v1)
  879. {
  880. write_begin_chart(family, chart);
  881. write_chart_dimension(dim, v1);
  882. write_end_chart();
  883. }
  884. /**
  885. * Call the necessary functions to create a chart.
  886. *
  887. * @param chart the chart name
  888. * @param family the chart family
  889. * @param dwrite the dimension name
  890. * @param vwrite the value for previous dimension
  891. * @param dread the dimension name
  892. * @param vread the value for previous dimension
  893. *
  894. * @return It returns a variable that maps the charts that did not have zero values.
  895. */
  896. void write_io_chart(char *chart, char *family, char *dwrite, long long vwrite, char *dread, long long vread)
  897. {
  898. write_begin_chart(family, chart);
  899. write_chart_dimension(dwrite, vwrite);
  900. write_chart_dimension(dread, vread);
  901. write_end_chart();
  902. }
  903. /**
  904. * Write chart cmd on standard output
  905. *
  906. * @param type chart type
  907. * @param id chart id
  908. * @param title chart title
  909. * @param units units label
  910. * @param family group name used to attach the chart on dashboard
  911. * @param charttype chart type
  912. * @param context chart context
  913. * @param order chart order
  914. * @param update_every update interval used by plugin
  915. * @param module chart module name, this is the eBPF thread.
  916. */
  917. void ebpf_write_chart_cmd(char *type, char *id, char *title, char *units, char *family,
  918. char *charttype, char *context, int order, int update_every, char *module)
  919. {
  920. printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d '' 'ebpf.plugin' '%s'\n",
  921. type,
  922. id,
  923. title,
  924. units,
  925. (family)?family:"",
  926. (context)?context:"",
  927. (charttype)?charttype:"",
  928. order,
  929. update_every,
  930. module);
  931. }
  932. /**
  933. * Write chart cmd on standard output
  934. *
  935. * @param type chart type
  936. * @param id chart id
  937. * @param title chart title
  938. * @param units units label
  939. * @param family group name used to attach the chart on dashboard
  940. * @param charttype chart type
  941. * @param context chart context
  942. * @param order chart order
  943. * @param update_every value to overwrite the update frequency set by the server.
  944. */
  945. void ebpf_write_chart_obsolete(char *type, char *id, char *title, char *units, char *family,
  946. char *charttype, char *context, int order, int update_every)
  947. {
  948. printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d 'obsolete'\n",
  949. type,
  950. id,
  951. title,
  952. units,
  953. (family)?family:"",
  954. (context)?context:"",
  955. (charttype)?charttype:"",
  956. order,
  957. update_every);
  958. }
  959. /**
  960. * Write the dimension command on standard output
  961. *
  962. * @param name the dimension name
  963. * @param id the dimension id
  964. * @param algo the dimension algorithm
  965. */
  966. void ebpf_write_global_dimension(char *name, char *id, char *algorithm)
  967. {
  968. printf("DIMENSION %s %s %s 1 1\n", name, id, algorithm);
  969. }
  970. /**
  971. * Call ebpf_write_global_dimension to create the dimensions for a specific chart
  972. *
  973. * @param ptr a pointer to a structure of the type netdata_publish_syscall_t
  974. * @param end the number of dimensions for the structure ptr
  975. */
  976. void ebpf_create_global_dimension(void *ptr, int end)
  977. {
  978. netdata_publish_syscall_t *move = ptr;
  979. int i = 0;
  980. while (move && i < end) {
  981. ebpf_write_global_dimension(move->name, move->dimension, move->algorithm);
  982. move = move->next;
  983. i++;
  984. }
  985. }
  986. /**
  987. * Call write_chart_cmd to create the charts
  988. *
  989. * @param type chart type
  990. * @param id chart id
  991. * @param title chart title
  992. * @param units axis label
  993. * @param family group name used to attach the chart on dashboard
  994. * @param context chart context
  995. * @param charttype chart type
  996. * @param order order number of the specified chart
  997. * @param ncd a pointer to a function called to create dimensions
  998. * @param move a pointer for a structure that has the dimensions
  999. * @param end number of dimensions for the chart created
  1000. * @param update_every update interval used with chart.
  1001. * @param module chart module name, this is the eBPF thread.
  1002. */
  1003. void ebpf_create_chart(char *type,
  1004. char *id,
  1005. char *title,
  1006. char *units,
  1007. char *family,
  1008. char *context,
  1009. char *charttype,
  1010. int order,
  1011. void (*ncd)(void *, int),
  1012. void *move,
  1013. int end,
  1014. int update_every,
  1015. char *module)
  1016. {
  1017. ebpf_write_chart_cmd(type, id, title, units, family, charttype, context, order, update_every, module);
  1018. if (ncd) {
  1019. ncd(move, end);
  1020. }
  1021. }
  1022. /**
  1023. * Create charts on apps submenu
  1024. *
  1025. * @param id the chart id
  1026. * @param title the value displayed on vertical axis.
  1027. * @param units the value displayed on vertical axis.
  1028. * @param family Submenu that the chart will be attached on dashboard.
  1029. * @param charttype chart type
  1030. * @param order the chart order
  1031. * @param algorithm the algorithm used by dimension
  1032. * @param root structure used to create the dimensions.
  1033. * @param update_every update interval used by plugin
  1034. * @param module chart module name, this is the eBPF thread.
  1035. */
  1036. void ebpf_create_charts_on_apps(char *id, char *title, char *units, char *family, char *charttype, int order,
  1037. char *algorithm, struct ebpf_target *root, int update_every, char *module)
  1038. {
  1039. struct ebpf_target *w;
  1040. ebpf_write_chart_cmd(NETDATA_APPS_FAMILY, id, title, units, family, charttype, NULL, order,
  1041. update_every, module);
  1042. for (w = root; w; w = w->next) {
  1043. if (unlikely(w->exposed))
  1044. fprintf(stdout, "DIMENSION %s '' %s 1 1\n", w->name, algorithm);
  1045. }
  1046. }
  1047. /**
  1048. * Call the necessary functions to create a name.
  1049. *
  1050. * @param family family name
  1051. * @param name chart name
  1052. * @param hist0 histogram values
  1053. * @param dimensions dimension values.
  1054. * @param end number of bins that will be sent to Netdata.
  1055. *
  1056. * @return It returns a variable that maps the charts that did not have zero values.
  1057. */
  1058. void write_histogram_chart(char *family, char *name, const netdata_idx_t *hist, char **dimensions, uint32_t end)
  1059. {
  1060. write_begin_chart(family, name);
  1061. uint32_t i;
  1062. for (i = 0; i < end; i++) {
  1063. write_chart_dimension(dimensions[i], (long long) hist[i]);
  1064. }
  1065. write_end_chart();
  1066. fflush(stdout);
  1067. }
  1068. /**
  1069. * ARAL Charts
  1070. *
  1071. * Add chart to monitor ARAL usage
  1072. * Caller must call this function with mutex locked.
  1073. *
  1074. * @param name the name used to create aral
  1075. * @param em a pointer to the structure with the default values.
  1076. */
  1077. void ebpf_statistic_create_aral_chart(char *name, ebpf_module_t *em)
  1078. {
  1079. static int priority = 140100;
  1080. char *mem = { NETDATA_EBPF_STAT_DIMENSION_MEMORY };
  1081. char *aral = { NETDATA_EBPF_STAT_DIMENSION_ARAL };
  1082. snprintfz(em->memory_usage, NETDATA_EBPF_CHART_MEM_LENGTH -1, "aral_%s_size", name);
  1083. snprintfz(em->memory_allocations, NETDATA_EBPF_CHART_MEM_LENGTH -1, "aral_%s_alloc", name);
  1084. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  1085. em->memory_usage,
  1086. "Bytes allocated for ARAL.",
  1087. "bytes",
  1088. NETDATA_EBPF_FAMILY,
  1089. NETDATA_EBPF_CHART_TYPE_STACKED,
  1090. "netdata.ebpf_aral_stat_size",
  1091. priority++,
  1092. em->update_every,
  1093. NETDATA_EBPF_MODULE_NAME_PROCESS);
  1094. ebpf_write_global_dimension(mem,
  1095. mem,
  1096. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  1097. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  1098. em->memory_allocations,
  1099. "Calls to allocate memory.",
  1100. "calls",
  1101. NETDATA_EBPF_FAMILY,
  1102. NETDATA_EBPF_CHART_TYPE_STACKED,
  1103. "netdata.ebpf_aral_stat_alloc",
  1104. priority++,
  1105. em->update_every,
  1106. NETDATA_EBPF_MODULE_NAME_PROCESS);
  1107. ebpf_write_global_dimension(aral,
  1108. aral,
  1109. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  1110. }
  1111. /**
  1112. * Send data from aral chart
  1113. *
  1114. * Send data for eBPF plugin
  1115. *
  1116. * @param memory a pointer to the allocated address
  1117. * @param em a pointer to the structure with the default values.
  1118. */
  1119. void ebpf_send_data_aral_chart(ARAL *memory, ebpf_module_t *em)
  1120. {
  1121. char *mem = { NETDATA_EBPF_STAT_DIMENSION_MEMORY };
  1122. char *aral = { NETDATA_EBPF_STAT_DIMENSION_ARAL };
  1123. struct aral_statistics *stats = aral_statistics(memory);
  1124. write_begin_chart(NETDATA_MONITORING_FAMILY, em->memory_usage);
  1125. write_chart_dimension(mem, (long long)stats->structures.allocated_bytes);
  1126. write_end_chart();
  1127. write_begin_chart(NETDATA_MONITORING_FAMILY, em->memory_allocations);
  1128. write_chart_dimension(aral, (long long)stats->structures.allocations);
  1129. write_end_chart();
  1130. }
  1131. /*****************************************************************
  1132. *
  1133. * FUNCTIONS TO DEFINE OPTIONS
  1134. *
  1135. *****************************************************************/
  1136. /**
  1137. * Define labels used to generate charts
  1138. *
  1139. * @param is structure with information about number of calls made for a function.
  1140. * @param pio structure used to generate charts.
  1141. * @param dim a pointer for the dimensions name
  1142. * @param name a pointer for the tensor with the name of the functions.
  1143. * @param algorithm a vector with the algorithms used to make the charts
  1144. * @param end the number of elements in the previous 4 arguments.
  1145. */
  1146. void ebpf_global_labels(netdata_syscall_stat_t *is, netdata_publish_syscall_t *pio, char **dim,
  1147. char **name, int *algorithm, int end)
  1148. {
  1149. int i;
  1150. netdata_syscall_stat_t *prev = NULL;
  1151. netdata_publish_syscall_t *publish_prev = NULL;
  1152. for (i = 0; i < end; i++) {
  1153. if (prev) {
  1154. prev->next = &is[i];
  1155. }
  1156. prev = &is[i];
  1157. pio[i].dimension = dim[i];
  1158. pio[i].name = name[i];
  1159. pio[i].algorithm = ebpf_algorithms[algorithm[i]];
  1160. if (publish_prev) {
  1161. publish_prev->next = &pio[i];
  1162. }
  1163. publish_prev = &pio[i];
  1164. }
  1165. }
  1166. /**
  1167. * Define thread mode for all ebpf program.
  1168. *
  1169. * @param lmode the mode that will be used for them.
  1170. */
  1171. static inline void ebpf_set_thread_mode(netdata_run_mode_t lmode)
  1172. {
  1173. int i;
  1174. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1175. ebpf_modules[i].mode = lmode;
  1176. }
  1177. }
  1178. /**
  1179. * Enable specific charts selected by user.
  1180. *
  1181. * @param em the structure that will be changed
  1182. * @param disable_apps the status about the apps charts.
  1183. * @param disable_cgroup the status about the cgroups charts.
  1184. */
  1185. static inline void ebpf_enable_specific_chart(struct ebpf_module *em, int disable_apps, int disable_cgroup)
  1186. {
  1187. em->enabled = CONFIG_BOOLEAN_YES;
  1188. // oomkill stores data inside apps submenu, so it always need to have apps_enabled for plugin to create
  1189. // its chart, without this comparison eBPF.plugin will try to store invalid data when apps is disabled.
  1190. if (!disable_apps || !strcmp(em->thread_name, "oomkill")) {
  1191. em->apps_charts = NETDATA_EBPF_APPS_FLAG_YES;
  1192. }
  1193. if (!disable_cgroup) {
  1194. em->cgroup_charts = CONFIG_BOOLEAN_YES;
  1195. }
  1196. em->global_charts = CONFIG_BOOLEAN_YES;
  1197. }
  1198. /**
  1199. * Enable all charts
  1200. *
  1201. * @param apps what is the current status of apps
  1202. * @param cgroups what is the current status of cgroups
  1203. */
  1204. static inline void ebpf_enable_all_charts(int apps, int cgroups)
  1205. {
  1206. int i;
  1207. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1208. ebpf_enable_specific_chart(&ebpf_modules[i], apps, cgroups);
  1209. }
  1210. }
  1211. /**
  1212. * Disable all Global charts
  1213. *
  1214. * Disable charts
  1215. */
  1216. static inline void disable_all_global_charts()
  1217. {
  1218. int i;
  1219. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1220. ebpf_modules[i].enabled = 0;
  1221. ebpf_modules[i].global_charts = 0;
  1222. }
  1223. }
  1224. /**
  1225. * Enable the specified chart group
  1226. *
  1227. * @param idx the index of ebpf_modules that I am enabling
  1228. * @param disable_apps should I keep apps charts?
  1229. */
  1230. static inline void ebpf_enable_chart(int idx, int disable_apps, int disable_cgroup)
  1231. {
  1232. int i;
  1233. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1234. if (i == idx) {
  1235. ebpf_enable_specific_chart(&ebpf_modules[i], disable_apps, disable_cgroup);
  1236. break;
  1237. }
  1238. }
  1239. }
  1240. /**
  1241. * Disable APPs
  1242. *
  1243. * Disable charts for apps loading only global charts.
  1244. */
  1245. static inline void ebpf_disable_apps()
  1246. {
  1247. int i;
  1248. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1249. ebpf_modules[i].apps_charts = NETDATA_EBPF_APPS_FLAG_NO;
  1250. }
  1251. }
  1252. /**
  1253. * Disable Cgroups
  1254. *
  1255. * Disable charts for apps loading only global charts.
  1256. */
  1257. static inline void ebpf_disable_cgroups()
  1258. {
  1259. int i;
  1260. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1261. ebpf_modules[i].cgroup_charts = 0;
  1262. }
  1263. }
  1264. /**
  1265. * Update Disabled Plugins
  1266. *
  1267. * This function calls ebpf_update_stats to update statistics for collector.
  1268. *
  1269. * @param em a pointer to `struct ebpf_module`
  1270. */
  1271. void ebpf_update_disabled_plugin_stats(ebpf_module_t *em)
  1272. {
  1273. pthread_mutex_lock(&lock);
  1274. ebpf_update_stats(&plugin_statistics, em);
  1275. pthread_mutex_unlock(&lock);
  1276. }
  1277. /**
  1278. * Print help on standard error for user knows how to use the collector.
  1279. */
  1280. void ebpf_print_help()
  1281. {
  1282. const time_t t = time(NULL);
  1283. struct tm ct;
  1284. struct tm *test = localtime_r(&t, &ct);
  1285. int year;
  1286. if (test)
  1287. year = ct.tm_year;
  1288. else
  1289. year = 0;
  1290. fprintf(stderr,
  1291. "\n"
  1292. " Netdata ebpf.plugin %s\n"
  1293. " Copyright (C) 2016-%d Costa Tsaousis <costa@tsaousis.gr>\n"
  1294. " Released under GNU General Public License v3 or later.\n"
  1295. " All rights reserved.\n"
  1296. "\n"
  1297. " This eBPF.plugin is a data collector plugin for netdata.\n"
  1298. "\n"
  1299. " This plugin only accepts long options with one or two dashes. The available command line options are:\n"
  1300. "\n"
  1301. " SECONDS Set the data collection frequency.\n"
  1302. "\n"
  1303. " [-]-help Show this help.\n"
  1304. "\n"
  1305. " [-]-version Show software version.\n"
  1306. "\n"
  1307. " [-]-global Disable charts per application and cgroup.\n"
  1308. "\n"
  1309. " [-]-all Enable all chart groups (global, apps, and cgroup), unless -g is also given.\n"
  1310. "\n"
  1311. " [-]-cachestat Enable charts related to process run time.\n"
  1312. "\n"
  1313. " [-]-dcstat Enable charts related to directory cache.\n"
  1314. "\n"
  1315. " [-]-disk Enable charts related to disk monitoring.\n"
  1316. "\n"
  1317. " [-]-filesystem Enable chart related to filesystem run time.\n"
  1318. "\n"
  1319. " [-]-hardirq Enable chart related to hard IRQ latency.\n"
  1320. "\n"
  1321. " [-]-mdflush Enable charts related to multi-device flush.\n"
  1322. "\n"
  1323. " [-]-mount Enable charts related to mount monitoring.\n"
  1324. "\n"
  1325. " [-]-net Enable network viewer charts.\n"
  1326. "\n"
  1327. " [-]-oomkill Enable chart related to OOM kill tracking.\n"
  1328. "\n"
  1329. " [-]-process Enable charts related to process run time.\n"
  1330. "\n"
  1331. " [-]-return Run the collector in return mode.\n"
  1332. "\n"
  1333. " [-]-shm Enable chart related to shared memory tracking.\n"
  1334. "\n"
  1335. " [-]-softirq Enable chart related to soft IRQ latency.\n"
  1336. "\n"
  1337. " [-]-sync Enable chart related to sync run time.\n"
  1338. "\n"
  1339. " [-]-swap Enable chart related to swap run time.\n"
  1340. "\n"
  1341. " [-]-vfs Enable chart related to vfs run time.\n"
  1342. "\n"
  1343. " [-]-legacy Load legacy eBPF programs.\n"
  1344. "\n"
  1345. " [-]-core Use CO-RE when available(Working in progress).\n"
  1346. "\n",
  1347. VERSION,
  1348. (year >= 116) ? year + 1900 : 2020);
  1349. }
  1350. /*****************************************************************
  1351. *
  1352. * TRACEPOINT MANAGEMENT FUNCTIONS
  1353. *
  1354. *****************************************************************/
  1355. /**
  1356. * Enable a tracepoint.
  1357. *
  1358. * @return 0 on success, -1 on error.
  1359. */
  1360. int ebpf_enable_tracepoint(ebpf_tracepoint_t *tp)
  1361. {
  1362. int test = ebpf_is_tracepoint_enabled(tp->class, tp->event);
  1363. // err?
  1364. if (test == -1) {
  1365. return -1;
  1366. }
  1367. // disabled?
  1368. else if (test == 0) {
  1369. // enable it then.
  1370. if (ebpf_enable_tracing_values(tp->class, tp->event)) {
  1371. return -1;
  1372. }
  1373. }
  1374. // enabled now or already was.
  1375. tp->enabled = true;
  1376. return 0;
  1377. }
  1378. /**
  1379. * Disable a tracepoint if it's enabled.
  1380. *
  1381. * @return 0 on success, -1 on error.
  1382. */
  1383. int ebpf_disable_tracepoint(ebpf_tracepoint_t *tp)
  1384. {
  1385. int test = ebpf_is_tracepoint_enabled(tp->class, tp->event);
  1386. // err?
  1387. if (test == -1) {
  1388. return -1;
  1389. }
  1390. // enabled?
  1391. else if (test == 1) {
  1392. // disable it then.
  1393. if (ebpf_disable_tracing_values(tp->class, tp->event)) {
  1394. return -1;
  1395. }
  1396. }
  1397. // disable now or already was.
  1398. tp->enabled = false;
  1399. return 0;
  1400. }
  1401. /**
  1402. * Enable multiple tracepoints on a list of tracepoints which end when the
  1403. * class is NULL.
  1404. *
  1405. * @return the number of successful enables.
  1406. */
  1407. uint32_t ebpf_enable_tracepoints(ebpf_tracepoint_t *tps)
  1408. {
  1409. uint32_t cnt = 0;
  1410. for (int i = 0; tps[i].class != NULL; i++) {
  1411. if (ebpf_enable_tracepoint(&tps[i]) == -1) {
  1412. netdata_log_error("Failed to enable tracepoint %s:%s", tps[i].class, tps[i].event);
  1413. }
  1414. else {
  1415. cnt += 1;
  1416. }
  1417. }
  1418. return cnt;
  1419. }
  1420. /*****************************************************************
  1421. *
  1422. * AUXILIARY FUNCTIONS USED DURING INITIALIZATION
  1423. *
  1424. *****************************************************************/
  1425. /**
  1426. * Read Local Ports
  1427. *
  1428. * Parse /proc/net/{tcp,udp} and get the ports Linux is listening.
  1429. *
  1430. * @param filename the proc file to parse.
  1431. * @param proto is the magic number associated to the protocol file we are reading.
  1432. */
  1433. static void read_local_ports(char *filename, uint8_t proto)
  1434. {
  1435. procfile *ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  1436. if (!ff)
  1437. return;
  1438. ff = procfile_readall(ff);
  1439. if (!ff)
  1440. return;
  1441. size_t lines = procfile_lines(ff), l;
  1442. netdata_passive_connection_t values = {.counter = 0, .tgid = 0, .pid = 0};
  1443. for(l = 0; l < lines ;l++) {
  1444. size_t words = procfile_linewords(ff, l);
  1445. // This is header or end of file
  1446. if (unlikely(words < 14))
  1447. continue;
  1448. // https://elixir.bootlin.com/linux/v5.7.8/source/include/net/tcp_states.h
  1449. // 0A = TCP_LISTEN
  1450. if (strcmp("0A", procfile_lineword(ff, l, 5)))
  1451. continue;
  1452. // Read local port
  1453. uint16_t port = (uint16_t)strtol(procfile_lineword(ff, l, 2), NULL, 16);
  1454. update_listen_table(htons(port), proto, &values);
  1455. }
  1456. procfile_close(ff);
  1457. }
  1458. /**
  1459. * Read Local addresseses
  1460. *
  1461. * Read the local address from the interfaces.
  1462. */
  1463. static void read_local_addresses()
  1464. {
  1465. struct ifaddrs *ifaddr, *ifa;
  1466. if (getifaddrs(&ifaddr) == -1) {
  1467. netdata_log_error("Cannot get the local IP addresses, it is no possible to do separation between inbound and outbound connections");
  1468. return;
  1469. }
  1470. char *notext = { "No text representation" };
  1471. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  1472. if (ifa->ifa_addr == NULL)
  1473. continue;
  1474. if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6))
  1475. continue;
  1476. ebpf_network_viewer_ip_list_t *w = callocz(1, sizeof(ebpf_network_viewer_ip_list_t));
  1477. int family = ifa->ifa_addr->sa_family;
  1478. w->ver = (uint8_t) family;
  1479. char text[INET6_ADDRSTRLEN];
  1480. if (family == AF_INET) {
  1481. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  1482. w->first.addr32[0] = in->sin_addr.s_addr;
  1483. w->last.addr32[0] = in->sin_addr.s_addr;
  1484. if (inet_ntop(AF_INET, w->first.addr8, text, INET_ADDRSTRLEN)) {
  1485. w->value = strdupz(text);
  1486. w->hash = simple_hash(text);
  1487. } else {
  1488. w->value = strdupz(notext);
  1489. w->hash = simple_hash(notext);
  1490. }
  1491. } else {
  1492. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  1493. memcpy(w->first.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  1494. memcpy(w->last.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  1495. if (inet_ntop(AF_INET6, w->first.addr8, text, INET_ADDRSTRLEN)) {
  1496. w->value = strdupz(text);
  1497. w->hash = simple_hash(text);
  1498. } else {
  1499. w->value = strdupz(notext);
  1500. w->hash = simple_hash(notext);
  1501. }
  1502. }
  1503. ebpf_fill_ip_list((family == AF_INET)?&network_viewer_opt.ipv4_local_ip:&network_viewer_opt.ipv6_local_ip,
  1504. w,
  1505. "selector");
  1506. }
  1507. freeifaddrs(ifaddr);
  1508. }
  1509. /**
  1510. * Start Pthread Variable
  1511. *
  1512. * This function starts all pthread variables.
  1513. */
  1514. void ebpf_start_pthread_variables()
  1515. {
  1516. pthread_mutex_init(&lock, NULL);
  1517. pthread_mutex_init(&ebpf_exit_cleanup, NULL);
  1518. pthread_mutex_init(&collect_data_mutex, NULL);
  1519. pthread_mutex_init(&mutex_cgroup_shm, NULL);
  1520. }
  1521. /**
  1522. * Am I collecting PIDs?
  1523. *
  1524. * Test if eBPF plugin needs to collect PID information.
  1525. *
  1526. * @return It returns 1 if at least one thread needs to collect the data, or zero otherwise.
  1527. */
  1528. static inline uint32_t ebpf_am_i_collect_pids()
  1529. {
  1530. uint32_t ret = 0;
  1531. int i;
  1532. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1533. ret |= ebpf_modules[i].cgroup_charts | (ebpf_modules[i].apps_charts & NETDATA_EBPF_APPS_FLAG_YES);
  1534. }
  1535. return ret;
  1536. }
  1537. /**
  1538. * Allocate the vectors used for all threads.
  1539. */
  1540. static void ebpf_allocate_common_vectors()
  1541. {
  1542. if (unlikely(!ebpf_am_i_collect_pids())) {
  1543. return;
  1544. }
  1545. ebpf_all_pids = callocz((size_t)pid_max, sizeof(struct ebpf_pid_stat *));
  1546. ebpf_aral_init();
  1547. }
  1548. /**
  1549. * Define how to load the ebpf programs
  1550. *
  1551. * @param ptr the option given by users
  1552. */
  1553. static inline void how_to_load(char *ptr)
  1554. {
  1555. if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_RETURN))
  1556. ebpf_set_thread_mode(MODE_RETURN);
  1557. else if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_DEFAULT))
  1558. ebpf_set_thread_mode(MODE_ENTRY);
  1559. else
  1560. netdata_log_error("the option %s for \"ebpf load mode\" is not a valid option.", ptr);
  1561. }
  1562. /**
  1563. * Update interval
  1564. *
  1565. * Update default interval with value from user
  1566. *
  1567. * @param update_every value to overwrite the update frequency set by the server.
  1568. */
  1569. static void ebpf_update_interval(int update_every)
  1570. {
  1571. int i;
  1572. int value = (int) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_UPDATE_EVERY,
  1573. update_every);
  1574. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1575. ebpf_modules[i].update_every = value;
  1576. }
  1577. }
  1578. /**
  1579. * Update PID table size
  1580. *
  1581. * Update default size with value from user
  1582. */
  1583. static void ebpf_update_table_size()
  1584. {
  1585. int i;
  1586. uint32_t value = (uint32_t) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION,
  1587. EBPF_CFG_PID_SIZE, ND_EBPF_DEFAULT_PID_SIZE);
  1588. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1589. ebpf_modules[i].pid_map_size = value;
  1590. }
  1591. }
  1592. /**
  1593. * Set Load mode
  1594. *
  1595. * @param origin specify the configuration file loaded
  1596. */
  1597. static inline void ebpf_set_load_mode(netdata_ebpf_load_mode_t load, netdata_ebpf_load_mode_t origin)
  1598. {
  1599. int i;
  1600. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1601. ebpf_modules[i].load &= ~NETDATA_EBPF_LOAD_METHODS;
  1602. ebpf_modules[i].load |= load | origin ;
  1603. }
  1604. }
  1605. /**
  1606. * Update mode
  1607. *
  1608. * @param str value read from configuration file.
  1609. * @param origin specify the configuration file loaded
  1610. */
  1611. static inline void epbf_update_load_mode(char *str, netdata_ebpf_load_mode_t origin)
  1612. {
  1613. netdata_ebpf_load_mode_t load = epbf_convert_string_to_load_mode(str);
  1614. ebpf_set_load_mode(load, origin);
  1615. }
  1616. /**
  1617. * Update Map per core
  1618. *
  1619. * Define the map type used with some hash tables.
  1620. */
  1621. static void ebpf_update_map_per_core()
  1622. {
  1623. int i;
  1624. int value = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION,
  1625. EBPF_CFG_MAPS_PER_CORE, CONFIG_BOOLEAN_YES);
  1626. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1627. ebpf_modules[i].maps_per_core = value;
  1628. }
  1629. }
  1630. /**
  1631. * Read collector values
  1632. *
  1633. * @param disable_apps variable to store information related to apps.
  1634. * @param disable_cgroups variable to store information related to cgroups.
  1635. * @param update_every value to overwrite the update frequency set by the server.
  1636. * @param origin specify the configuration file loaded
  1637. */
  1638. static void read_collector_values(int *disable_apps, int *disable_cgroups,
  1639. int update_every, netdata_ebpf_load_mode_t origin)
  1640. {
  1641. // Read global section
  1642. char *value;
  1643. if (appconfig_exists(&collector_config, EBPF_GLOBAL_SECTION, "load")) // Backward compatibility
  1644. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, "load",
  1645. EBPF_CFG_LOAD_MODE_DEFAULT);
  1646. else
  1647. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE,
  1648. EBPF_CFG_LOAD_MODE_DEFAULT);
  1649. how_to_load(value);
  1650. btf_path = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_PROGRAM_PATH,
  1651. EBPF_DEFAULT_BTF_PATH);
  1652. #ifdef LIBBPF_MAJOR_VERSION
  1653. default_btf = ebpf_load_btf_file(btf_path, EBPF_DEFAULT_BTF_FILE);
  1654. #endif
  1655. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_TYPE_FORMAT, EBPF_CFG_DEFAULT_PROGRAM);
  1656. epbf_update_load_mode(value, origin);
  1657. ebpf_update_interval(update_every);
  1658. ebpf_update_table_size();
  1659. // This is kept to keep compatibility
  1660. uint32_t enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, "disable apps",
  1661. CONFIG_BOOLEAN_NO);
  1662. if (!enabled) {
  1663. // Apps is a positive sentence, so we need to invert the values to disable apps.
  1664. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
  1665. CONFIG_BOOLEAN_YES);
  1666. enabled = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  1667. }
  1668. *disable_apps = (int)enabled;
  1669. // Cgroup is a positive sentence, so we need to invert the values to disable apps.
  1670. // We are using the same pattern for cgroup and apps
  1671. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_CGROUP, CONFIG_BOOLEAN_NO);
  1672. *disable_cgroups = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  1673. ebpf_update_map_per_core();
  1674. // Read ebpf programs section
  1675. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  1676. ebpf_modules[EBPF_MODULE_PROCESS_IDX].config_name, CONFIG_BOOLEAN_YES);
  1677. int started = 0;
  1678. if (enabled) {
  1679. ebpf_enable_chart(EBPF_MODULE_PROCESS_IDX, *disable_apps, *disable_cgroups);
  1680. started++;
  1681. }
  1682. // This is kept to keep compatibility
  1683. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network viewer",
  1684. CONFIG_BOOLEAN_NO);
  1685. if (!enabled)
  1686. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  1687. ebpf_modules[EBPF_MODULE_SOCKET_IDX].config_name,
  1688. CONFIG_BOOLEAN_NO);
  1689. if (enabled) {
  1690. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, *disable_apps, *disable_cgroups);
  1691. started++;
  1692. }
  1693. // This is kept to keep compatibility
  1694. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connection monitoring",
  1695. CONFIG_BOOLEAN_NO);
  1696. if (!enabled)
  1697. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connections",
  1698. CONFIG_BOOLEAN_NO);
  1699. network_viewer_opt.enabled = enabled;
  1700. if (enabled) {
  1701. if (!ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled)
  1702. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, *disable_apps, *disable_cgroups);
  1703. // Read network viewer section if network viewer is enabled
  1704. // This is kept here to keep backward compatibility
  1705. parse_network_viewer_section(&collector_config);
  1706. parse_service_name_section(&collector_config);
  1707. started++;
  1708. }
  1709. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "cachestat",
  1710. CONFIG_BOOLEAN_NO);
  1711. if (enabled) {
  1712. ebpf_enable_chart(EBPF_MODULE_CACHESTAT_IDX, *disable_apps, *disable_cgroups);
  1713. started++;
  1714. }
  1715. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "sync",
  1716. CONFIG_BOOLEAN_YES);
  1717. if (enabled) {
  1718. ebpf_enable_chart(EBPF_MODULE_SYNC_IDX, *disable_apps, *disable_cgroups);
  1719. started++;
  1720. }
  1721. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "dcstat",
  1722. CONFIG_BOOLEAN_NO);
  1723. if (enabled) {
  1724. ebpf_enable_chart(EBPF_MODULE_DCSTAT_IDX, *disable_apps, *disable_cgroups);
  1725. started++;
  1726. }
  1727. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "swap",
  1728. CONFIG_BOOLEAN_NO);
  1729. if (enabled) {
  1730. ebpf_enable_chart(EBPF_MODULE_SWAP_IDX, *disable_apps, *disable_cgroups);
  1731. started++;
  1732. }
  1733. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "vfs",
  1734. CONFIG_BOOLEAN_NO);
  1735. if (enabled) {
  1736. ebpf_enable_chart(EBPF_MODULE_VFS_IDX, *disable_apps, *disable_cgroups);
  1737. started++;
  1738. }
  1739. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "filesystem",
  1740. CONFIG_BOOLEAN_NO);
  1741. if (enabled) {
  1742. ebpf_enable_chart(EBPF_MODULE_FILESYSTEM_IDX, *disable_apps, *disable_cgroups);
  1743. started++;
  1744. }
  1745. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "disk",
  1746. CONFIG_BOOLEAN_NO);
  1747. if (enabled) {
  1748. ebpf_enable_chart(EBPF_MODULE_DISK_IDX, *disable_apps, *disable_cgroups);
  1749. started++;
  1750. }
  1751. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "mount",
  1752. CONFIG_BOOLEAN_YES);
  1753. if (enabled) {
  1754. ebpf_enable_chart(EBPF_MODULE_MOUNT_IDX, *disable_apps, *disable_cgroups);
  1755. started++;
  1756. }
  1757. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "fd",
  1758. CONFIG_BOOLEAN_YES);
  1759. if (enabled) {
  1760. ebpf_enable_chart(EBPF_MODULE_FD_IDX, *disable_apps, *disable_cgroups);
  1761. started++;
  1762. }
  1763. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "hardirq",
  1764. CONFIG_BOOLEAN_YES);
  1765. if (enabled) {
  1766. ebpf_enable_chart(EBPF_MODULE_HARDIRQ_IDX, *disable_apps, *disable_cgroups);
  1767. started++;
  1768. }
  1769. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "softirq",
  1770. CONFIG_BOOLEAN_YES);
  1771. if (enabled) {
  1772. ebpf_enable_chart(EBPF_MODULE_SOFTIRQ_IDX, *disable_apps, *disable_cgroups);
  1773. started++;
  1774. }
  1775. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "oomkill",
  1776. CONFIG_BOOLEAN_YES);
  1777. if (enabled) {
  1778. ebpf_enable_chart(EBPF_MODULE_OOMKILL_IDX, *disable_apps, *disable_cgroups);
  1779. started++;
  1780. }
  1781. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "shm",
  1782. CONFIG_BOOLEAN_YES);
  1783. if (enabled) {
  1784. ebpf_enable_chart(EBPF_MODULE_SHM_IDX, *disable_apps, *disable_cgroups);
  1785. started++;
  1786. }
  1787. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "mdflush",
  1788. CONFIG_BOOLEAN_NO);
  1789. if (enabled) {
  1790. ebpf_enable_chart(EBPF_MODULE_MDFLUSH_IDX, *disable_apps, *disable_cgroups);
  1791. started++;
  1792. }
  1793. if (!started){
  1794. ebpf_enable_all_charts(*disable_apps, *disable_cgroups);
  1795. // Read network viewer section
  1796. // This is kept here to keep backward compatibility
  1797. if (network_viewer_opt.enabled) {
  1798. parse_network_viewer_section(&collector_config);
  1799. parse_service_name_section(&collector_config);
  1800. }
  1801. }
  1802. }
  1803. /**
  1804. * Load collector config
  1805. *
  1806. * @param path the path where the file ebpf.conf is stored.
  1807. * @param disable_apps variable to store the information about apps plugin status.
  1808. * @param disable_cgroups variable to store the information about cgroups plugin status.
  1809. * @param update_every value to overwrite the update frequency set by the server.
  1810. *
  1811. * @return 0 on success and -1 otherwise.
  1812. */
  1813. static int load_collector_config(char *path, int *disable_apps, int *disable_cgroups, int update_every)
  1814. {
  1815. char lpath[4096];
  1816. netdata_ebpf_load_mode_t origin;
  1817. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_CONFIG_FILE);
  1818. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  1819. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_OLD_CONFIG_FILE);
  1820. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  1821. return -1;
  1822. }
  1823. origin = EBPF_LOADED_FROM_STOCK;
  1824. } else
  1825. origin = EBPF_LOADED_FROM_USER;
  1826. read_collector_values(disable_apps, disable_cgroups, update_every, origin);
  1827. return 0;
  1828. }
  1829. /**
  1830. * Set global variables reading environment variables
  1831. */
  1832. void set_global_variables()
  1833. {
  1834. // Get environment variables
  1835. ebpf_plugin_dir = getenv("NETDATA_PLUGINS_DIR");
  1836. if (!ebpf_plugin_dir)
  1837. ebpf_plugin_dir = PLUGINS_DIR;
  1838. ebpf_user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  1839. if (!ebpf_user_config_dir)
  1840. ebpf_user_config_dir = CONFIG_DIR;
  1841. ebpf_stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  1842. if (!ebpf_stock_config_dir)
  1843. ebpf_stock_config_dir = LIBCONFIG_DIR;
  1844. ebpf_configured_log_dir = getenv("NETDATA_LOG_DIR");
  1845. if (!ebpf_configured_log_dir)
  1846. ebpf_configured_log_dir = LOG_DIR;
  1847. ebpf_nprocs = (int)sysconf(_SC_NPROCESSORS_ONLN);
  1848. if (ebpf_nprocs < 0) {
  1849. ebpf_nprocs = NETDATA_MAX_PROCESSOR;
  1850. netdata_log_error("Cannot identify number of process, using default value %d", ebpf_nprocs);
  1851. }
  1852. isrh = get_redhat_release();
  1853. pid_max = get_system_pid_max();
  1854. running_on_kernel = ebpf_get_kernel_version();
  1855. }
  1856. /**
  1857. * Load collector config
  1858. */
  1859. static inline void ebpf_load_thread_config()
  1860. {
  1861. int i;
  1862. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1863. ebpf_update_module(&ebpf_modules[i], default_btf, running_on_kernel, isrh);
  1864. }
  1865. }
  1866. /**
  1867. * Check Conditions
  1868. *
  1869. * This function checks kernel that plugin is running and permissions.
  1870. *
  1871. * @return It returns 0 on success and -1 otherwise
  1872. */
  1873. int ebpf_check_conditions()
  1874. {
  1875. if (!has_condition_to_run(running_on_kernel)) {
  1876. netdata_log_error("The current collector cannot run on this kernel.");
  1877. return -1;
  1878. }
  1879. if (!am_i_running_as_root()) {
  1880. netdata_log_error(
  1881. "ebpf.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities..",
  1882. (unsigned int)getuid(), (unsigned int)geteuid());
  1883. return -1;
  1884. }
  1885. return 0;
  1886. }
  1887. /**
  1888. * Adjust memory
  1889. *
  1890. * Adjust memory values to load eBPF programs.
  1891. *
  1892. * @return It returns 0 on success and -1 otherwise
  1893. */
  1894. int ebpf_adjust_memory_limit()
  1895. {
  1896. struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
  1897. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  1898. netdata_log_error("Setrlimit(RLIMIT_MEMLOCK)");
  1899. return -1;
  1900. }
  1901. return 0;
  1902. }
  1903. /**
  1904. * Parse arguments given from user.
  1905. *
  1906. * @param argc the number of arguments
  1907. * @param argv the pointer to the arguments
  1908. */
  1909. static void ebpf_parse_args(int argc, char **argv)
  1910. {
  1911. int disable_apps = 0;
  1912. int disable_cgroups = 1;
  1913. int freq = 0;
  1914. int option_index = 0;
  1915. uint64_t select_threads = 0;
  1916. static struct option long_options[] = {
  1917. {"process", no_argument, 0, 0 },
  1918. {"net", no_argument, 0, 0 },
  1919. {"cachestat", no_argument, 0, 0 },
  1920. {"sync", no_argument, 0, 0 },
  1921. {"dcstat", no_argument, 0, 0 },
  1922. {"swap", no_argument, 0, 0 },
  1923. {"vfs", no_argument, 0, 0 },
  1924. {"filesystem", no_argument, 0, 0 },
  1925. {"disk", no_argument, 0, 0 },
  1926. {"mount", no_argument, 0, 0 },
  1927. {"filedescriptor", no_argument, 0, 0 },
  1928. {"hardirq", no_argument, 0, 0 },
  1929. {"softirq", no_argument, 0, 0 },
  1930. {"oomkill", no_argument, 0, 0 },
  1931. {"shm", no_argument, 0, 0 },
  1932. {"mdflush", no_argument, 0, 0 },
  1933. /* INSERT NEW THREADS BEFORE THIS COMMENT TO KEEP COMPATIBILITY WITH enum ebpf_module_indexes */
  1934. {"all", no_argument, 0, 0 },
  1935. {"version", no_argument, 0, 0 },
  1936. {"help", no_argument, 0, 0 },
  1937. {"global", no_argument, 0, 0 },
  1938. {"return", no_argument, 0, 0 },
  1939. {"legacy", no_argument, 0, 0 },
  1940. {"core", no_argument, 0, 0 },
  1941. {"unittest", no_argument, 0, 0 },
  1942. {0, 0, 0, 0}
  1943. };
  1944. memset(&network_viewer_opt, 0, sizeof(network_viewer_opt));
  1945. network_viewer_opt.max_dim = NETDATA_NV_CAP_VALUE;
  1946. if (argc > 1) {
  1947. int n = (int)str2l(argv[1]);
  1948. if (n > 0) {
  1949. freq = n;
  1950. }
  1951. }
  1952. if (!freq)
  1953. freq = EBPF_DEFAULT_UPDATE_EVERY;
  1954. if (load_collector_config(ebpf_user_config_dir, &disable_apps, &disable_cgroups, freq)) {
  1955. netdata_log_info(
  1956. "Does not have a configuration file inside `%s/ebpf.d.conf. It will try to load stock file.",
  1957. ebpf_user_config_dir);
  1958. if (load_collector_config(ebpf_stock_config_dir, &disable_apps, &disable_cgroups, freq)) {
  1959. netdata_log_info("Does not have a stock file. It is starting with default options.");
  1960. }
  1961. }
  1962. ebpf_load_thread_config();
  1963. while (1) {
  1964. int c = getopt_long_only(argc, argv, "", long_options, &option_index);
  1965. if (c == -1)
  1966. break;
  1967. switch (option_index) {
  1968. case EBPF_MODULE_PROCESS_IDX: {
  1969. select_threads |= 1<<EBPF_MODULE_PROCESS_IDX;
  1970. #ifdef NETDATA_INTERNAL_CHECKS
  1971. netdata_log_info("EBPF enabling \"PROCESS\" charts, because it was started with the option \"[-]-process\".");
  1972. #endif
  1973. break;
  1974. }
  1975. case EBPF_MODULE_SOCKET_IDX: {
  1976. select_threads |= 1<<EBPF_MODULE_SOCKET_IDX;
  1977. #ifdef NETDATA_INTERNAL_CHECKS
  1978. netdata_log_info("EBPF enabling \"NET\" charts, because it was started with the option \"[-]-net\".");
  1979. #endif
  1980. break;
  1981. }
  1982. case EBPF_MODULE_CACHESTAT_IDX: {
  1983. select_threads |= 1<<EBPF_MODULE_CACHESTAT_IDX;
  1984. #ifdef NETDATA_INTERNAL_CHECKS
  1985. netdata_log_info("EBPF enabling \"CACHESTAT\" charts, because it was started with the option \"[-]-cachestat\".");
  1986. #endif
  1987. break;
  1988. }
  1989. case EBPF_MODULE_SYNC_IDX: {
  1990. select_threads |= 1<<EBPF_MODULE_SYNC_IDX;
  1991. #ifdef NETDATA_INTERNAL_CHECKS
  1992. netdata_log_info("EBPF enabling \"SYNC\" chart, because it was started with the option \"[-]-sync\".");
  1993. #endif
  1994. break;
  1995. }
  1996. case EBPF_MODULE_DCSTAT_IDX: {
  1997. select_threads |= 1<<EBPF_MODULE_DCSTAT_IDX;
  1998. #ifdef NETDATA_INTERNAL_CHECKS
  1999. netdata_log_info("EBPF enabling \"DCSTAT\" charts, because it was started with the option \"[-]-dcstat\".");
  2000. #endif
  2001. break;
  2002. }
  2003. case EBPF_MODULE_SWAP_IDX: {
  2004. select_threads |= 1<<EBPF_MODULE_SWAP_IDX;
  2005. #ifdef NETDATA_INTERNAL_CHECKS
  2006. netdata_log_info("EBPF enabling \"SWAP\" chart, because it was started with the option \"[-]-swap\".");
  2007. #endif
  2008. break;
  2009. }
  2010. case EBPF_MODULE_VFS_IDX: {
  2011. select_threads |= 1<<EBPF_MODULE_VFS_IDX;
  2012. #ifdef NETDATA_INTERNAL_CHECKS
  2013. netdata_log_info("EBPF enabling \"VFS\" chart, because it was started with the option \"[-]-vfs\".");
  2014. #endif
  2015. break;
  2016. }
  2017. case EBPF_MODULE_FILESYSTEM_IDX: {
  2018. select_threads |= 1<<EBPF_MODULE_FILESYSTEM_IDX;
  2019. #ifdef NETDATA_INTERNAL_CHECKS
  2020. netdata_log_info("EBPF enabling \"FILESYSTEM\" chart, because it was started with the option \"[-]-filesystem\".");
  2021. #endif
  2022. break;
  2023. }
  2024. case EBPF_MODULE_DISK_IDX: {
  2025. select_threads |= 1<<EBPF_MODULE_DISK_IDX;
  2026. #ifdef NETDATA_INTERNAL_CHECKS
  2027. netdata_log_info("EBPF enabling \"DISK\" chart, because it was started with the option \"[-]-disk\".");
  2028. #endif
  2029. break;
  2030. }
  2031. case EBPF_MODULE_MOUNT_IDX: {
  2032. select_threads |= 1<<EBPF_MODULE_MOUNT_IDX;
  2033. #ifdef NETDATA_INTERNAL_CHECKS
  2034. netdata_log_info("EBPF enabling \"MOUNT\" chart, because it was started with the option \"[-]-mount\".");
  2035. #endif
  2036. break;
  2037. }
  2038. case EBPF_MODULE_FD_IDX: {
  2039. select_threads |= 1<<EBPF_MODULE_FD_IDX;
  2040. #ifdef NETDATA_INTERNAL_CHECKS
  2041. netdata_log_info("EBPF enabling \"FILEDESCRIPTOR\" chart, because it was started with the option \"[-]-filedescriptor\".");
  2042. #endif
  2043. break;
  2044. }
  2045. case EBPF_MODULE_HARDIRQ_IDX: {
  2046. select_threads |= 1<<EBPF_MODULE_HARDIRQ_IDX;
  2047. #ifdef NETDATA_INTERNAL_CHECKS
  2048. netdata_log_info("EBPF enabling \"HARDIRQ\" chart, because it was started with the option \"[-]-hardirq\".");
  2049. #endif
  2050. break;
  2051. }
  2052. case EBPF_MODULE_SOFTIRQ_IDX: {
  2053. select_threads |= 1<<EBPF_MODULE_SOFTIRQ_IDX;
  2054. #ifdef NETDATA_INTERNAL_CHECKS
  2055. netdata_log_info("EBPF enabling \"SOFTIRQ\" chart, because it was started with the option \"[-]-softirq\".");
  2056. #endif
  2057. break;
  2058. }
  2059. case EBPF_MODULE_OOMKILL_IDX: {
  2060. select_threads |= 1<<EBPF_MODULE_OOMKILL_IDX;
  2061. #ifdef NETDATA_INTERNAL_CHECKS
  2062. netdata_log_info("EBPF enabling \"OOMKILL\" chart, because it was started with the option \"[-]-oomkill\".");
  2063. #endif
  2064. break;
  2065. }
  2066. case EBPF_MODULE_SHM_IDX: {
  2067. select_threads |= 1<<EBPF_MODULE_SHM_IDX;
  2068. #ifdef NETDATA_INTERNAL_CHECKS
  2069. netdata_log_info("EBPF enabling \"SHM\" chart, because it was started with the option \"[-]-shm\".");
  2070. #endif
  2071. break;
  2072. }
  2073. case EBPF_MODULE_MDFLUSH_IDX: {
  2074. select_threads |= 1<<EBPF_MODULE_MDFLUSH_IDX;
  2075. #ifdef NETDATA_INTERNAL_CHECKS
  2076. netdata_log_info("EBPF enabling \"MDFLUSH\" chart, because it was started with the option \"[-]-mdflush\".");
  2077. #endif
  2078. break;
  2079. }
  2080. case EBPF_OPTION_ALL_CHARTS: {
  2081. disable_apps = 0;
  2082. disable_cgroups = 0;
  2083. #ifdef NETDATA_INTERNAL_CHECKS
  2084. netdata_log_info("EBPF running with all chart groups, because it was started with the option \"[-]-all\".");
  2085. #endif
  2086. break;
  2087. }
  2088. case EBPF_OPTION_VERSION: {
  2089. printf("ebpf.plugin %s\n", VERSION);
  2090. exit(0);
  2091. }
  2092. case EBPF_OPTION_HELP: {
  2093. ebpf_print_help();
  2094. exit(0);
  2095. }
  2096. case EBPF_OPTION_GLOBAL_CHART: {
  2097. disable_apps = 1;
  2098. disable_cgroups = 1;
  2099. #ifdef NETDATA_INTERNAL_CHECKS
  2100. netdata_log_info("EBPF running with global chart group, because it was started with the option \"[-]-global\".");
  2101. #endif
  2102. break;
  2103. }
  2104. case EBPF_OPTION_RETURN_MODE: {
  2105. ebpf_set_thread_mode(MODE_RETURN);
  2106. #ifdef NETDATA_INTERNAL_CHECKS
  2107. netdata_log_info("EBPF running in \"RETURN\" mode, because it was started with the option \"[-]-return\".");
  2108. #endif
  2109. break;
  2110. }
  2111. case EBPF_OPTION_LEGACY: {
  2112. ebpf_set_load_mode(EBPF_LOAD_LEGACY, EBPF_LOADED_FROM_USER);
  2113. #ifdef NETDATA_INTERNAL_CHECKS
  2114. netdata_log_info("EBPF running with \"LEGACY\" code, because it was started with the option \"[-]-legacy\".");
  2115. #endif
  2116. break;
  2117. }
  2118. case EBPF_OPTION_CORE: {
  2119. ebpf_set_load_mode(EBPF_LOAD_CORE, EBPF_LOADED_FROM_USER);
  2120. #ifdef NETDATA_INTERNAL_CHECKS
  2121. netdata_log_info("EBPF running with \"CO-RE\" code, because it was started with the option \"[-]-core\".");
  2122. #endif
  2123. break;
  2124. }
  2125. case EBPF_OPTION_UNITTEST: {
  2126. // if we cannot run until the end, we will cancel the unittest
  2127. int exit_code = ECANCELED;
  2128. if (ebpf_check_conditions())
  2129. goto unittest;
  2130. if (ebpf_adjust_memory_limit())
  2131. goto unittest;
  2132. // Load binary in entry mode
  2133. ebpf_ut_initialize_structure(MODE_ENTRY);
  2134. if (ebpf_ut_load_real_binary())
  2135. goto unittest;
  2136. ebpf_ut_cleanup_memory();
  2137. // Do not load a binary in entry mode
  2138. ebpf_ut_initialize_structure(MODE_ENTRY);
  2139. if (ebpf_ut_load_fake_binary())
  2140. goto unittest;
  2141. ebpf_ut_cleanup_memory();
  2142. exit_code = 0;
  2143. unittest:
  2144. exit(exit_code);
  2145. }
  2146. default: {
  2147. break;
  2148. }
  2149. }
  2150. }
  2151. if (disable_apps || disable_cgroups) {
  2152. if (disable_apps)
  2153. ebpf_disable_apps();
  2154. if (disable_cgroups)
  2155. ebpf_disable_cgroups();
  2156. }
  2157. if (select_threads) {
  2158. disable_all_global_charts();
  2159. uint64_t idx;
  2160. for (idx = 0; idx < EBPF_OPTION_ALL_CHARTS; idx++) {
  2161. if (select_threads & 1<<idx)
  2162. ebpf_enable_specific_chart(&ebpf_modules[idx], disable_apps, disable_cgroups);
  2163. }
  2164. }
  2165. // Load apps_groups.conf
  2166. if (ebpf_read_apps_groups_conf(
  2167. &apps_groups_default_target, &apps_groups_root_target, ebpf_user_config_dir, "groups")) {
  2168. netdata_log_info("Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'",
  2169. ebpf_user_config_dir, ebpf_stock_config_dir);
  2170. if (ebpf_read_apps_groups_conf(
  2171. &apps_groups_default_target, &apps_groups_root_target, ebpf_stock_config_dir, "groups")) {
  2172. netdata_log_error("Cannot read process groups '%s/apps_groups.conf'. There are no internal defaults. Failing.",
  2173. ebpf_stock_config_dir);
  2174. ebpf_exit();
  2175. }
  2176. } else
  2177. netdata_log_info("Loaded config file '%s/apps_groups.conf'", ebpf_user_config_dir);
  2178. }
  2179. /*****************************************************************
  2180. *
  2181. * COLLECTOR ENTRY POINT
  2182. *
  2183. *****************************************************************/
  2184. /**
  2185. * Update PID file
  2186. *
  2187. * Update the content of PID file
  2188. *
  2189. * @param filename is the full name of the file.
  2190. * @param pid that identifies the process
  2191. */
  2192. static void ebpf_update_pid_file(char *filename, pid_t pid)
  2193. {
  2194. FILE *fp = fopen(filename, "w");
  2195. if (!fp)
  2196. return;
  2197. fprintf(fp, "%d", pid);
  2198. fclose(fp);
  2199. }
  2200. /**
  2201. * Get Process Name
  2202. *
  2203. * Get process name from /proc/PID/status
  2204. *
  2205. * @param pid that identifies the process
  2206. */
  2207. static char *ebpf_get_process_name(pid_t pid)
  2208. {
  2209. char *name = NULL;
  2210. char filename[FILENAME_MAX + 1];
  2211. snprintfz(filename, FILENAME_MAX, "/proc/%d/status", pid);
  2212. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  2213. if(unlikely(!ff)) {
  2214. netdata_log_error("Cannot open %s", filename);
  2215. return name;
  2216. }
  2217. ff = procfile_readall(ff);
  2218. if(unlikely(!ff))
  2219. return name;
  2220. unsigned long i, lines = procfile_lines(ff);
  2221. for(i = 0; i < lines ; i++) {
  2222. char *cmp = procfile_lineword(ff, i, 0);
  2223. if (!strcmp(cmp, "Name:")) {
  2224. name = strdupz(procfile_lineword(ff, i, 1));
  2225. break;
  2226. }
  2227. }
  2228. procfile_close(ff);
  2229. return name;
  2230. }
  2231. /**
  2232. * Read Previous PID
  2233. *
  2234. * @param filename is the full name of the file.
  2235. *
  2236. * @return It returns the PID used during previous execution on success or 0 otherwise
  2237. */
  2238. static pid_t ebpf_read_previous_pid(char *filename)
  2239. {
  2240. FILE *fp = fopen(filename, "r");
  2241. if (!fp)
  2242. return 0;
  2243. char buffer[64];
  2244. size_t length = fread(buffer, sizeof(*buffer), 63, fp);
  2245. pid_t old_pid = 0;
  2246. if (length) {
  2247. if (length > 63)
  2248. length = 63;
  2249. buffer[length] = '\0';
  2250. old_pid = (pid_t) str2uint32_t(buffer, NULL);
  2251. }
  2252. fclose(fp);
  2253. return old_pid;
  2254. }
  2255. /**
  2256. * Kill previous process
  2257. *
  2258. * Kill previous process whether it was not closed.
  2259. *
  2260. * @param filename is the full name of the file.
  2261. * @param pid that identifies the process
  2262. */
  2263. static void ebpf_kill_previous_process(char *filename, pid_t pid)
  2264. {
  2265. pid_t old_pid = ebpf_read_previous_pid(filename);
  2266. if (!old_pid)
  2267. return;
  2268. // Process is not running
  2269. char *prev_name = ebpf_get_process_name(old_pid);
  2270. if (!prev_name)
  2271. return;
  2272. char *current_name = ebpf_get_process_name(pid);
  2273. if (!strcmp(prev_name, current_name))
  2274. kill(old_pid, SIGKILL);
  2275. freez(prev_name);
  2276. freez(current_name);
  2277. // wait few microseconds before start new plugin
  2278. sleep_usec(USEC_PER_MS * 300);
  2279. }
  2280. /**
  2281. * PID file
  2282. *
  2283. * Write the filename for PID inside the given vector.
  2284. *
  2285. * @param filename vector where we will store the name.
  2286. * @param length number of bytes available in filename vector
  2287. */
  2288. void ebpf_pid_file(char *filename, size_t length)
  2289. {
  2290. snprintfz(filename, length, "%s%s/ebpf.d/ebpf.pid", netdata_configured_host_prefix, ebpf_plugin_dir);
  2291. }
  2292. /**
  2293. * Manage PID
  2294. *
  2295. * This function kills another instance of eBPF whether it is necessary and update the file content.
  2296. *
  2297. * @param pid that identifies the process
  2298. */
  2299. static void ebpf_manage_pid(pid_t pid)
  2300. {
  2301. char filename[FILENAME_MAX + 1];
  2302. ebpf_pid_file(filename, FILENAME_MAX);
  2303. ebpf_kill_previous_process(filename, pid);
  2304. ebpf_update_pid_file(filename, pid);
  2305. }
  2306. /**
  2307. * Set start routine
  2308. *
  2309. * Set static routine before threads to be created.
  2310. */
  2311. static void ebpf_set_static_routine()
  2312. {
  2313. int i;
  2314. for (i = 0; ebpf_modules[i].thread_name; i++) {
  2315. ebpf_threads[i].start_routine = ebpf_modules[i].start_routine;
  2316. }
  2317. }
  2318. /**
  2319. * Entry point
  2320. *
  2321. * @param argc the number of arguments
  2322. * @param argv the pointer to the arguments
  2323. *
  2324. * @return it returns 0 on success and another integer otherwise
  2325. */
  2326. int main(int argc, char **argv)
  2327. {
  2328. stderror = stderr;
  2329. clocks_init();
  2330. main_thread_id = gettid();
  2331. set_global_variables();
  2332. ebpf_parse_args(argc, argv);
  2333. ebpf_manage_pid(getpid());
  2334. if (ebpf_check_conditions())
  2335. return 2;
  2336. // set name
  2337. program_name = "ebpf.plugin";
  2338. // disable syslog
  2339. error_log_syslog = 0;
  2340. // set errors flood protection to 100 logs per hour
  2341. error_log_errors_per_period = 100;
  2342. error_log_throttle_period = 3600;
  2343. if (ebpf_adjust_memory_limit())
  2344. return 3;
  2345. signal(SIGINT, ebpf_stop_threads);
  2346. signal(SIGQUIT, ebpf_stop_threads);
  2347. signal(SIGTERM, ebpf_stop_threads);
  2348. signal(SIGPIPE, ebpf_stop_threads);
  2349. ebpf_start_pthread_variables();
  2350. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  2351. if(verify_netdata_host_prefix() == -1) ebpf_exit(6);
  2352. ebpf_allocate_common_vectors();
  2353. #ifdef LIBBPF_MAJOR_VERSION
  2354. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  2355. #endif
  2356. read_local_addresses();
  2357. read_local_ports("/proc/net/tcp", IPPROTO_TCP);
  2358. read_local_ports("/proc/net/tcp6", IPPROTO_TCP);
  2359. read_local_ports("/proc/net/udp", IPPROTO_UDP);
  2360. read_local_ports("/proc/net/udp6", IPPROTO_UDP);
  2361. ebpf_set_static_routine();
  2362. cgroup_integration_thread.thread = mallocz(sizeof(netdata_thread_t));
  2363. cgroup_integration_thread.start_routine = ebpf_cgroup_integration;
  2364. netdata_thread_create(cgroup_integration_thread.thread, cgroup_integration_thread.name,
  2365. NETDATA_THREAD_OPTION_DEFAULT, ebpf_cgroup_integration, NULL);
  2366. int i;
  2367. for (i = 0; ebpf_threads[i].name != NULL; i++) {
  2368. struct netdata_static_thread *st = &ebpf_threads[i];
  2369. ebpf_module_t *em = &ebpf_modules[i];
  2370. em->thread = st;
  2371. // We always initialize process, because it is responsible to take care of apps integration
  2372. if (em->enabled || !i) {
  2373. st->thread = mallocz(sizeof(netdata_thread_t));
  2374. em->thread_id = i;
  2375. em->enabled = NETDATA_THREAD_EBPF_RUNNING;
  2376. netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_DEFAULT, st->start_routine, em);
  2377. } else {
  2378. em->enabled = NETDATA_THREAD_EBPF_NOT_RUNNING;
  2379. }
  2380. }
  2381. usec_t step = USEC_PER_SEC;
  2382. heartbeat_t hb;
  2383. heartbeat_init(&hb);
  2384. int update_apps_every = (int) EBPF_CFG_UPDATE_APPS_EVERY_DEFAULT;
  2385. int update_apps_list = update_apps_every - 1;
  2386. int process_maps_per_core = ebpf_modules[EBPF_MODULE_PROCESS_IDX].maps_per_core;
  2387. //Plugin will be killed when it receives a signal
  2388. while (!ebpf_exit_plugin) {
  2389. (void)heartbeat_next(&hb, step);
  2390. pthread_mutex_lock(&ebpf_exit_cleanup);
  2391. if (process_pid_fd != -1) {
  2392. pthread_mutex_lock(&collect_data_mutex);
  2393. if (++update_apps_list == update_apps_every) {
  2394. update_apps_list = 0;
  2395. cleanup_exited_pids();
  2396. collect_data_for_all_processes(process_pid_fd, process_maps_per_core);
  2397. pthread_mutex_lock(&lock);
  2398. ebpf_create_apps_charts(apps_groups_root_target);
  2399. pthread_mutex_unlock(&lock);
  2400. }
  2401. pthread_mutex_unlock(&collect_data_mutex);
  2402. }
  2403. pthread_mutex_unlock(&ebpf_exit_cleanup);
  2404. }
  2405. ebpf_stop_threads(0);
  2406. return 0;
  2407. }