ebpf.c 103 KB

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