ebpf.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116
  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[] = { EBPF_CHART_ALGORITHM_ABSOLUTE, EBPF_CHART_ALGORITHM_INCREMENTAL};
  17. struct config collector_config = APPCONFIG_INITIALIZER;
  18. int running_on_kernel = 0;
  19. int ebpf_nprocs;
  20. int isrh = 0;
  21. int main_thread_id = 0;
  22. int process_pid_fd = -1;
  23. uint64_t collect_pids = 0;
  24. static size_t global_iterations_counter = 1;
  25. bool publish_internal_metrics = true;
  26. pthread_mutex_t lock;
  27. pthread_mutex_t ebpf_exit_cleanup;
  28. pthread_mutex_t collect_data_mutex;
  29. struct netdata_static_thread cgroup_integration_thread = {
  30. .name = "EBPF CGROUP INT",
  31. .config_section = NULL,
  32. .config_name = NULL,
  33. .env_name = NULL,
  34. .enabled = 1,
  35. .thread = NULL,
  36. .init_routine = NULL,
  37. .start_routine = NULL
  38. };
  39. ebpf_module_t ebpf_modules[] = {
  40. { .info = {.thread_name = "process",
  41. .config_name = "process",
  42. .thread_description = NETDATA_EBPF_MODULE_PROCESS_DESC},
  43. .functions = {.start_routine = ebpf_process_thread,
  44. .apps_routine = ebpf_process_create_apps_charts,
  45. .fnct_routine = NULL},
  46. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  47. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  48. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  49. .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &process_config,
  50. .config_file = NETDATA_PROCESS_CONFIG_FILE,
  51. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_10 |
  52. NETDATA_V5_14,
  53. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  54. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0 },
  55. { .info = {.thread_name = "socket",
  56. .config_name = "socket",
  57. .thread_description = NETDATA_EBPF_SOCKET_MODULE_DESC},
  58. .functions = {.start_routine = ebpf_socket_thread,
  59. .apps_routine = ebpf_socket_create_apps_charts,
  60. .fnct_routine = ebpf_socket_read_open_connections,
  61. .fcnt_name = EBPF_FUNCTION_SOCKET,
  62. .fcnt_desc = EBPF_PLUGIN_SOCKET_FUNCTION_DESCRIPTION,
  63. .fcnt_thread_chart_name = NULL,
  64. .fcnt_thread_lifetime_name = NULL},
  65. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  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. .maps = NULL,
  69. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &socket_config,
  70. .config_file = NETDATA_NETWORK_CONFIG_FILE,
  71. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  72. .load = EBPF_LOAD_LEGACY, .targets = socket_targets, .probe_links = NULL, .objects = NULL,
  73. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  74. { .info = {.thread_name = "cachestat", .config_name = "cachestat", .thread_description = NETDATA_EBPF_CACHESTAT_MODULE_DESC},
  75. .functions = {.start_routine = ebpf_cachestat_thread,
  76. .apps_routine = ebpf_cachestat_create_apps_charts,
  77. .fnct_routine = NULL},
  78. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  79. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  80. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  81. .maps = cachestat_maps, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &cachestat_config,
  82. .config_file = NETDATA_CACHESTAT_CONFIG_FILE,
  83. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18|
  84. NETDATA_V5_4 | NETDATA_V5_14 | NETDATA_V5_15 | NETDATA_V5_16,
  85. .load = EBPF_LOAD_LEGACY, .targets = cachestat_targets, .probe_links = NULL, .objects = NULL,
  86. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  87. { .info = {.thread_name = "sync",
  88. .config_name = "sync",
  89. .thread_description = NETDATA_EBPF_SYNC_MODULE_DESC},
  90. .functions = {.start_routine = ebpf_sync_thread,
  91. .apps_routine = NULL,
  92. .fnct_routine = NULL},
  93. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING, .maps = NULL,
  94. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  95. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  96. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &sync_config,
  97. .config_file = NETDATA_SYNC_CONFIG_FILE,
  98. // All syscalls have the same kernels
  99. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  100. .load = EBPF_LOAD_LEGACY, .targets = sync_targets, .probe_links = NULL, .objects = NULL,
  101. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  102. { .info = {.thread_name = "dc",
  103. .config_name = "dc",
  104. .thread_description = NETDATA_EBPF_DC_MODULE_DESC},
  105. .functions = {.start_routine = ebpf_dcstat_thread,
  106. .apps_routine = ebpf_dcstat_create_apps_charts,
  107. .fnct_routine = NULL},
  108. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  109. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  110. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  111. .maps = dcstat_maps,
  112. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &dcstat_config,
  113. .config_file = NETDATA_DIRECTORY_DCSTAT_CONFIG_FILE,
  114. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  115. .load = EBPF_LOAD_LEGACY, .targets = dc_targets, .probe_links = NULL, .objects = NULL,
  116. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  117. { .info = {.thread_name = "swap", .config_name = "swap", .thread_description = NETDATA_EBPF_SWAP_MODULE_DESC},
  118. .functions = {.start_routine = ebpf_swap_thread,
  119. .apps_routine = ebpf_swap_create_apps_charts,
  120. .fnct_routine = NULL},
  121. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  122. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  123. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  124. .maps = NULL,
  125. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &swap_config,
  126. .config_file = NETDATA_DIRECTORY_SWAP_CONFIG_FILE,
  127. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14 | NETDATA_V6_8,
  128. .load = EBPF_LOAD_LEGACY, .targets = swap_targets, .probe_links = NULL, .objects = NULL,
  129. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  130. { .info = {.thread_name = "vfs",
  131. .config_name = "vfs",
  132. .thread_description = NETDATA_EBPF_VFS_MODULE_DESC},
  133. .functions = {.start_routine = ebpf_vfs_thread,
  134. .apps_routine = ebpf_vfs_create_apps_charts,
  135. .fnct_routine = NULL},
  136. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  137. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  138. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  139. .maps = NULL,
  140. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &vfs_config,
  141. .config_file = NETDATA_DIRECTORY_VFS_CONFIG_FILE,
  142. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  143. .load = EBPF_LOAD_LEGACY, .targets = vfs_targets, .probe_links = NULL, .objects = NULL,
  144. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  145. { .info = {.thread_name = "filesystem", .config_name = "filesystem", .thread_description = NETDATA_EBPF_FS_MODULE_DESC},
  146. .functions = {.start_routine = ebpf_filesystem_thread,
  147. .apps_routine = NULL,
  148. .fnct_routine = NULL},
  149. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  150. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  151. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  152. .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fs_config,
  153. .config_file = NETDATA_FILESYSTEM_CONFIG_FILE,
  154. //We are setting kernels as zero, because we load eBPF programs according the kernel running.
  155. .kernels = 0, .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  156. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  157. { .info = {.thread_name = "disk",
  158. .config_name = "disk",
  159. .thread_description = NETDATA_EBPF_DISK_MODULE_DESC},
  160. .functions = {.start_routine = ebpf_disk_thread,
  161. .apps_routine = NULL,
  162. .fnct_routine = NULL},
  163. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  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. .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &disk_config,
  167. .config_file = NETDATA_DISK_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. { .info = {.thread_name = "mount",
  172. .config_name = "mount",
  173. .thread_description = NETDATA_EBPF_MOUNT_MODULE_DESC},
  174. .functions = {.start_routine = ebpf_mount_thread,
  175. .apps_routine = NULL,
  176. .fnct_routine = NULL},
  177. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  178. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  179. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  180. .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &mount_config,
  181. .config_file = NETDATA_MOUNT_CONFIG_FILE,
  182. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  183. .load = EBPF_LOAD_LEGACY, .targets = mount_targets, .probe_links = NULL, .objects = NULL,
  184. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  185. { .info = { .thread_name = "fd",
  186. .config_name = "fd",
  187. .thread_description = NETDATA_EBPF_FD_MODULE_DESC},
  188. .functions = {.start_routine = ebpf_fd_thread,
  189. .apps_routine = ebpf_fd_create_apps_charts,
  190. .fnct_routine = NULL},
  191. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  192. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  193. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  194. .maps = NULL,
  195. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fd_config,
  196. .config_file = NETDATA_FD_CONFIG_FILE,
  197. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_11 |
  198. NETDATA_V5_14,
  199. .load = EBPF_LOAD_LEGACY, .targets = fd_targets, .probe_links = NULL, .objects = NULL,
  200. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  201. { .info = { .thread_name = "hardirq",
  202. .config_name = "hardirq",
  203. .thread_description = NETDATA_EBPF_HARDIRQ_MODULE_DESC},
  204. .functions = {.start_routine = ebpf_hardirq_thread,
  205. .apps_routine = NULL,
  206. .fnct_routine = NULL},
  207. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  208. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  209. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  210. .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &hardirq_config,
  211. .config_file = NETDATA_HARDIRQ_CONFIG_FILE,
  212. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  213. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  214. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  215. { .info = { .thread_name = "softirq",
  216. .config_name = "softirq",
  217. .thread_description = NETDATA_EBPF_SOFTIRQ_MODULE_DESC},
  218. .functions = {.start_routine = ebpf_softirq_thread,
  219. .apps_routine = NULL,
  220. .fnct_routine = NULL },
  221. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  222. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  223. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  224. .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &softirq_config,
  225. .config_file = NETDATA_SOFTIRQ_CONFIG_FILE,
  226. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  227. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  228. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  229. { .info = {.thread_name = "oomkill",
  230. .config_name = "oomkill",
  231. .thread_description = NETDATA_EBPF_OOMKILL_MODULE_DESC},
  232. .functions = {.start_routine = ebpf_oomkill_thread,
  233. .apps_routine = ebpf_oomkill_create_apps_charts,
  234. .fnct_routine = NULL},.enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  235. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  236. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  237. .maps = NULL,
  238. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &oomkill_config,
  239. .config_file = NETDATA_OOMKILL_CONFIG_FILE,
  240. .kernels = NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  241. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  242. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  243. { .info = {.thread_name = "shm",
  244. .config_name = "shm",
  245. .thread_description = NETDATA_EBPF_SHM_MODULE_DESC},
  246. .functions = {.start_routine = ebpf_shm_thread,
  247. .apps_routine = ebpf_shm_create_apps_charts,
  248. .fnct_routine = NULL},
  249. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  250. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  251. .apps_level = NETDATA_APPS_LEVEL_REAL_PARENT, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  252. .maps = NULL,
  253. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &shm_config,
  254. .config_file = NETDATA_DIRECTORY_SHM_CONFIG_FILE,
  255. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  256. .load = EBPF_LOAD_LEGACY, .targets = shm_targets, .probe_links = NULL, .objects = NULL,
  257. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  258. { .info = { .thread_name = "mdflush",
  259. .config_name = "mdflush",
  260. .thread_description = NETDATA_EBPF_MD_MODULE_DESC},
  261. .functions = {.start_routine = ebpf_mdflush_thread,
  262. .apps_routine = NULL,
  263. .fnct_routine = NULL},
  264. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING,
  265. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  266. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  267. .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &mdflush_config,
  268. .config_file = NETDATA_DIRECTORY_MDFLUSH_CONFIG_FILE,
  269. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  270. .load = EBPF_LOAD_LEGACY, .targets = mdflush_targets, .probe_links = NULL, .objects = NULL,
  271. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  272. { .info = { .thread_name = "functions",
  273. .config_name = "functions",
  274. .thread_description = NETDATA_EBPF_FUNCTIONS_MODULE_DESC},
  275. .functions = {.start_routine = ebpf_function_thread,
  276. .apps_routine = NULL,
  277. .fnct_routine = NULL},
  278. .enabled = NETDATA_THREAD_EBPF_RUNNING,
  279. .update_every = EBPF_DEFAULT_UPDATE_EVERY, .global_charts = 1, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO,
  280. .apps_level = NETDATA_APPS_NOT_SET, .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0,
  281. .maps = NULL, .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = NULL,
  282. .config_file = NETDATA_DIRECTORY_FUNCTIONS_CONFIG_FILE,
  283. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_14,
  284. .load = EBPF_LOAD_LEGACY, .targets = NULL, .probe_links = NULL, .objects = NULL,
  285. .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES, .lifetime = EBPF_DEFAULT_LIFETIME, .running_time = 0},
  286. { .info = {.thread_name = NULL, .config_name = NULL},
  287. .functions = {.start_routine = NULL, .apps_routine = NULL, .fnct_routine = NULL},
  288. .enabled = NETDATA_THREAD_EBPF_NOT_RUNNING, .update_every = EBPF_DEFAULT_UPDATE_EVERY,
  289. .global_charts = 0, .apps_charts = NETDATA_EBPF_APPS_FLAG_NO, .apps_level = NETDATA_APPS_NOT_SET,
  290. .cgroup_charts = CONFIG_BOOLEAN_NO, .mode = MODE_ENTRY, .optional = 0, .maps = NULL,
  291. .pid_map_size = 0, .names = NULL, .cfg = NULL, .kernels = 0, .load = EBPF_LOAD_LEGACY,
  292. .targets = NULL, .probe_links = NULL, .objects = NULL, .thread = NULL, .maps_per_core = CONFIG_BOOLEAN_YES},
  293. };
  294. struct netdata_static_thread ebpf_threads[] = {
  295. {
  296. .name = "EBPF PROCESS",
  297. .config_section = NULL,
  298. .config_name = NULL,
  299. .env_name = NULL,
  300. .enabled = 1,
  301. .thread = NULL,
  302. .init_routine = NULL,
  303. .start_routine = NULL
  304. },
  305. {
  306. .name = "EBPF SOCKET",
  307. .config_section = NULL,
  308. .config_name = NULL,
  309. .env_name = NULL,
  310. .enabled = 1,
  311. .thread = NULL,
  312. .init_routine = NULL,
  313. .start_routine = NULL
  314. },
  315. {
  316. .name = "EBPF CACHESTAT",
  317. .config_section = NULL,
  318. .config_name = NULL,
  319. .env_name = NULL,
  320. .enabled = 1,
  321. .thread = NULL,
  322. .init_routine = NULL,
  323. .start_routine = NULL
  324. },
  325. {
  326. .name = "EBPF SYNC",
  327. .config_section = NULL,
  328. .config_name = NULL,
  329. .env_name = NULL,
  330. .enabled = 1,
  331. .thread = NULL,
  332. .init_routine = NULL,
  333. .start_routine = NULL
  334. },
  335. {
  336. .name = "EBPF DCSTAT",
  337. .config_section = NULL,
  338. .config_name = NULL,
  339. .env_name = NULL,
  340. .enabled = 1,
  341. .thread = NULL,
  342. .init_routine = NULL,
  343. .start_routine = NULL
  344. },
  345. {
  346. .name = "EBPF SWAP",
  347. .config_section = NULL,
  348. .config_name = NULL,
  349. .env_name = NULL,
  350. .enabled = 1,
  351. .thread = NULL,
  352. .init_routine = NULL,
  353. .start_routine = NULL
  354. },
  355. {
  356. .name = "EBPF VFS",
  357. .config_section = NULL,
  358. .config_name = NULL,
  359. .env_name = NULL,
  360. .enabled = 1,
  361. .thread = NULL,
  362. .init_routine = NULL,
  363. .start_routine = NULL
  364. },
  365. {
  366. .name = "EBPF FILESYSTEM",
  367. .config_section = NULL,
  368. .config_name = NULL,
  369. .env_name = NULL,
  370. .enabled = 1,
  371. .thread = NULL,
  372. .init_routine = NULL,
  373. .start_routine = NULL
  374. },
  375. {
  376. .name = "EBPF DISK",
  377. .config_section = NULL,
  378. .config_name = NULL,
  379. .env_name = NULL,
  380. .enabled = 1,
  381. .thread = NULL,
  382. .init_routine = NULL,
  383. .start_routine = NULL
  384. },
  385. {
  386. .name = "EBPF MOUNT",
  387. .config_section = NULL,
  388. .config_name = NULL,
  389. .env_name = NULL,
  390. .enabled = 1,
  391. .thread = NULL,
  392. .init_routine = NULL,
  393. .start_routine = NULL
  394. },
  395. {
  396. .name = "EBPF FD",
  397. .config_section = NULL,
  398. .config_name = NULL,
  399. .env_name = NULL,
  400. .enabled = 1,
  401. .thread = NULL,
  402. .init_routine = NULL,
  403. .start_routine = NULL
  404. },
  405. {
  406. .name = "EBPF HARDIRQ",
  407. .config_section = NULL,
  408. .config_name = NULL,
  409. .env_name = NULL,
  410. .enabled = 1,
  411. .thread = NULL,
  412. .init_routine = NULL,
  413. .start_routine = NULL
  414. },
  415. {
  416. .name = "EBPF SOFTIRQ",
  417. .config_section = NULL,
  418. .config_name = NULL,
  419. .env_name = NULL,
  420. .enabled = 1,
  421. .thread = NULL,
  422. .init_routine = NULL,
  423. .start_routine = NULL
  424. },
  425. {
  426. .name = "EBPF OOMKILL",
  427. .config_section = NULL,
  428. .config_name = NULL,
  429. .env_name = NULL,
  430. .enabled = 1,
  431. .thread = NULL,
  432. .init_routine = NULL,
  433. .start_routine = NULL
  434. },
  435. {
  436. .name = "EBPF SHM",
  437. .config_section = NULL,
  438. .config_name = NULL,
  439. .env_name = NULL,
  440. .enabled = 1,
  441. .thread = NULL,
  442. .init_routine = NULL,
  443. .start_routine = NULL
  444. },
  445. {
  446. .name = "EBPF MDFLUSH",
  447. .config_section = NULL,
  448. .config_name = NULL,
  449. .env_name = NULL,
  450. .enabled = 1,
  451. .thread = NULL,
  452. .init_routine = NULL,
  453. .start_routine = NULL
  454. },
  455. {
  456. .name = "EBPF FUNCTIONS",
  457. .config_section = NULL,
  458. .config_name = NULL,
  459. .env_name = NULL,
  460. #ifdef NETDATA_DEV_MODE
  461. .enabled = 1,
  462. #else
  463. .enabled = 0,
  464. #endif
  465. .thread = NULL,
  466. .init_routine = NULL,
  467. .start_routine = NULL
  468. },
  469. {
  470. .name = NULL,
  471. .config_section = NULL,
  472. .config_name = NULL,
  473. .env_name = NULL,
  474. .enabled = 0,
  475. .thread = NULL,
  476. .init_routine = NULL,
  477. .start_routine = NULL
  478. },
  479. };
  480. ebpf_filesystem_partitions_t localfs[] =
  481. {{.filesystem = "ext4",
  482. .optional_filesystem = NULL,
  483. .family = "ext4",
  484. .objects = NULL,
  485. .probe_links = NULL,
  486. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  487. .enabled = CONFIG_BOOLEAN_YES,
  488. .addresses = {.function = NULL, .addr = 0},
  489. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  490. .fs_maps = NULL,
  491. .fs_obj = NULL,
  492. .functions = { "ext4_file_read_iter",
  493. "ext4_file_write_iter",
  494. "ext4_file_open",
  495. "ext4_sync_file",
  496. NULL }},
  497. {.filesystem = "xfs",
  498. .optional_filesystem = NULL,
  499. .family = "xfs",
  500. .objects = NULL,
  501. .probe_links = NULL,
  502. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  503. .enabled = CONFIG_BOOLEAN_YES,
  504. .addresses = {.function = NULL, .addr = 0},
  505. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  506. .fs_maps = NULL,
  507. .fs_obj = NULL,
  508. .functions = { "xfs_file_read_iter",
  509. "xfs_file_write_iter",
  510. "xfs_file_open",
  511. "xfs_file_fsync",
  512. NULL }},
  513. {.filesystem = "nfs",
  514. .optional_filesystem = "nfs4",
  515. .family = "nfs",
  516. .objects = NULL,
  517. .probe_links = NULL,
  518. .flags = NETDATA_FILESYSTEM_ATTR_CHARTS,
  519. .enabled = CONFIG_BOOLEAN_YES,
  520. .addresses = {.function = NULL, .addr = 0},
  521. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  522. .fs_maps = NULL,
  523. .fs_obj = NULL,
  524. .functions = { "nfs_file_read",
  525. "nfs_file_write",
  526. "nfs_open",
  527. "nfs_getattr",
  528. NULL }}, // // "nfs4_file_open" - not present on all kernels
  529. {.filesystem = "zfs",
  530. .optional_filesystem = NULL,
  531. .family = "zfs",
  532. .objects = NULL,
  533. .probe_links = NULL,
  534. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  535. .enabled = CONFIG_BOOLEAN_YES,
  536. .addresses = {.function = NULL, .addr = 0},
  537. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4,
  538. .fs_maps = NULL,
  539. .fs_obj = NULL,
  540. .functions = { "zpl_iter_read",
  541. "zpl_iter_write",
  542. "zpl_open",
  543. "zpl_fsync",
  544. NULL }},
  545. {.filesystem = "btrfs",
  546. .optional_filesystem = NULL,
  547. .family = "btrfs",
  548. .objects = NULL,
  549. .probe_links = NULL,
  550. .flags = NETDATA_FILESYSTEM_FILL_ADDRESS_TABLE,
  551. .enabled = CONFIG_BOOLEAN_YES,
  552. .addresses = {.function = "btrfs_file_operations", .addr = 0},
  553. .kernels = NETDATA_V3_10 | NETDATA_V4_14 | NETDATA_V4_16 | NETDATA_V4_18 | NETDATA_V5_4 | NETDATA_V5_10,
  554. .fs_maps = NULL,
  555. .fs_obj = NULL,
  556. .functions = { "btrfs_file_read_iter",
  557. "btrfs_file_write_iter",
  558. "btrfs_file_open",
  559. "btrfs_sync_file",
  560. NULL }},
  561. {.filesystem = NULL,
  562. .optional_filesystem = NULL,
  563. .family = NULL,
  564. .objects = NULL,
  565. .probe_links = NULL,
  566. .flags = NETDATA_FILESYSTEM_FLAG_NO_PARTITION,
  567. .enabled = CONFIG_BOOLEAN_YES,
  568. .addresses = {.function = NULL, .addr = 0},
  569. .kernels = 0, .fs_maps = NULL, .fs_obj = NULL}};
  570. ebpf_sync_syscalls_t local_syscalls[] = {
  571. {.syscall = NETDATA_SYSCALLS_SYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  572. #ifdef LIBBPF_MAJOR_VERSION
  573. .sync_obj = NULL,
  574. #endif
  575. .sync_maps = NULL
  576. },
  577. {.syscall = NETDATA_SYSCALLS_SYNCFS, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  578. #ifdef LIBBPF_MAJOR_VERSION
  579. .sync_obj = NULL,
  580. #endif
  581. .sync_maps = NULL
  582. },
  583. {.syscall = NETDATA_SYSCALLS_MSYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  584. #ifdef LIBBPF_MAJOR_VERSION
  585. .sync_obj = NULL,
  586. #endif
  587. .sync_maps = NULL
  588. },
  589. {.syscall = NETDATA_SYSCALLS_FSYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  590. #ifdef LIBBPF_MAJOR_VERSION
  591. .sync_obj = NULL,
  592. #endif
  593. .sync_maps = NULL
  594. },
  595. {.syscall = NETDATA_SYSCALLS_FDATASYNC, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  596. #ifdef LIBBPF_MAJOR_VERSION
  597. .sync_obj = NULL,
  598. #endif
  599. .sync_maps = NULL
  600. },
  601. {.syscall = NETDATA_SYSCALLS_SYNC_FILE_RANGE, .enabled = CONFIG_BOOLEAN_YES, .objects = NULL, .probe_links = NULL,
  602. #ifdef LIBBPF_MAJOR_VERSION
  603. .sync_obj = NULL,
  604. #endif
  605. .sync_maps = NULL
  606. },
  607. {.syscall = NULL, .enabled = CONFIG_BOOLEAN_NO, .objects = NULL, .probe_links = NULL,
  608. #ifdef LIBBPF_MAJOR_VERSION
  609. .sync_obj = NULL,
  610. #endif
  611. .sync_maps = NULL
  612. }
  613. };
  614. // Link with cgroup.plugin
  615. netdata_ebpf_cgroup_shm_t shm_ebpf_cgroup = {NULL, NULL};
  616. int shm_fd_ebpf_cgroup = -1;
  617. sem_t *shm_sem_ebpf_cgroup = SEM_FAILED;
  618. pthread_mutex_t mutex_cgroup_shm;
  619. //Network viewer
  620. ebpf_network_viewer_options_t network_viewer_opt;
  621. // Statistic
  622. ebpf_plugin_stats_t plugin_statistics = {.core = 0, .legacy = 0, .running = 0, .threads = 0, .tracepoints = 0,
  623. .probes = 0, .retprobes = 0, .trampolines = 0, .memlock_kern = 0,
  624. .hash_tables = 0};
  625. netdata_ebpf_judy_pid_t ebpf_judy_pid = {.pid_table = NULL, .index = {.JudyLArray = NULL}};
  626. bool ebpf_plugin_exit = false;
  627. #ifdef LIBBPF_MAJOR_VERSION
  628. struct btf *default_btf = NULL;
  629. struct cachestat_bpf *cachestat_bpf_obj = NULL;
  630. struct dc_bpf *dc_bpf_obj = NULL;
  631. struct disk_bpf *disk_bpf_obj = NULL;
  632. struct fd_bpf *fd_bpf_obj = NULL;
  633. struct hardirq_bpf *hardirq_bpf_obj = NULL;
  634. struct mdflush_bpf *mdflush_bpf_obj = NULL;
  635. struct mount_bpf *mount_bpf_obj = NULL;
  636. struct shm_bpf *shm_bpf_obj = NULL;
  637. struct socket_bpf *socket_bpf_obj = NULL;
  638. struct swap_bpf *bpf_obj = NULL;
  639. struct vfs_bpf *vfs_bpf_obj = NULL;
  640. #else
  641. void *default_btf = NULL;
  642. #endif
  643. const char *btf_path = NULL;
  644. /*****************************************************************
  645. *
  646. * FUNCTIONS USED TO MANIPULATE JUDY ARRAY
  647. *
  648. *****************************************************************/
  649. /**
  650. * Hashtable insert unsafe
  651. *
  652. * Find or create a value associated to the index
  653. *
  654. * @return The lsocket = 0 when new item added to the array otherwise the existing item value is returned in *lsocket
  655. * we return a pointer to a pointer, so that the caller can put anything needed at the value of the index.
  656. * The pointer to pointer we return has to be used before any other operation that may change the index (insert/delete).
  657. *
  658. */
  659. void **ebpf_judy_insert_unsafe(PPvoid_t arr, Word_t key)
  660. {
  661. JError_t J_Error;
  662. Pvoid_t *idx = JudyLIns(arr, key, &J_Error);
  663. if (unlikely(idx == PJERR)) {
  664. netdata_log_error("Cannot add PID to JudyL, JU_ERRNO_* == %u, ID == %d",
  665. JU_ERRNO(&J_Error), JU_ERRID(&J_Error));
  666. }
  667. return idx;
  668. }
  669. /**
  670. * Get PID from judy
  671. *
  672. * Get a pointer for the `pid` from judy_array;
  673. *
  674. * @param judy_array a judy array where PID is the primary key
  675. * @param pid pid stored.
  676. */
  677. netdata_ebpf_judy_pid_stats_t *ebpf_get_pid_from_judy_unsafe(PPvoid_t judy_array, uint32_t pid)
  678. {
  679. netdata_ebpf_judy_pid_stats_t **pid_pptr =
  680. (netdata_ebpf_judy_pid_stats_t **)ebpf_judy_insert_unsafe(judy_array, pid);
  681. netdata_ebpf_judy_pid_stats_t *pid_ptr = *pid_pptr;
  682. if (likely(*pid_pptr == NULL)) {
  683. // a new PID added to the index
  684. *pid_pptr = aral_mallocz(ebpf_judy_pid.pid_table);
  685. pid_ptr = *pid_pptr;
  686. pid_ptr->cmdline = NULL;
  687. pid_ptr->socket_stats.JudyLArray = NULL;
  688. rw_spinlock_init(&pid_ptr->socket_stats.rw_spinlock);
  689. }
  690. return pid_ptr;
  691. }
  692. /*****************************************************************
  693. *
  694. * FUNCTIONS USED TO ALLOCATE APPS/CGROUP MEMORIES (ARAL)
  695. *
  696. *****************************************************************/
  697. /**
  698. * Allocate PID ARAL
  699. *
  700. * Allocate memory using ARAL functions to speed up processing.
  701. *
  702. * @param name the internal name used for allocated region.
  703. * @param size size of each element inside allocated space
  704. *
  705. * @return It returns the address on success and NULL otherwise.
  706. */
  707. ARAL *ebpf_allocate_pid_aral(char *name, size_t size)
  708. {
  709. static size_t max_elements = NETDATA_EBPF_ALLOC_MAX_PID;
  710. if (max_elements < NETDATA_EBPF_ALLOC_MIN_ELEMENTS) {
  711. netdata_log_error("Number of elements given is too small, adjusting it for %d", NETDATA_EBPF_ALLOC_MIN_ELEMENTS);
  712. max_elements = NETDATA_EBPF_ALLOC_MIN_ELEMENTS;
  713. }
  714. return aral_create(name, size,
  715. 0, max_elements,
  716. NULL, NULL, NULL, false, false);
  717. }
  718. /*****************************************************************
  719. *
  720. * FUNCTIONS USED TO CLEAN MEMORY AND OPERATE SYSTEM FILES
  721. *
  722. *****************************************************************/
  723. /**
  724. * Wait to avoid possible coredumps while process is closing.
  725. */
  726. static inline void ebpf_check_before2go()
  727. {
  728. int i = EBPF_OPTION_ALL_CHARTS;
  729. usec_t max = USEC_PER_SEC, step = 200000;
  730. while (i && max) {
  731. max -= step;
  732. sleep_usec(step);
  733. i = 0;
  734. int j;
  735. pthread_mutex_lock(&ebpf_exit_cleanup);
  736. for (j = 0; ebpf_modules[j].info.thread_name != NULL; j++) {
  737. if (ebpf_modules[j].enabled < NETDATA_THREAD_EBPF_STOPPING)
  738. i++;
  739. }
  740. pthread_mutex_unlock(&ebpf_exit_cleanup);
  741. }
  742. if (i) {
  743. netdata_log_error("eBPF cannot unload all threads on time, but it will go away");
  744. }
  745. }
  746. /**
  747. * Close the collector gracefully
  748. */
  749. static void ebpf_exit()
  750. {
  751. #ifdef LIBBPF_MAJOR_VERSION
  752. pthread_mutex_lock(&ebpf_exit_cleanup);
  753. if (default_btf) {
  754. btf__free(default_btf);
  755. default_btf = NULL;
  756. }
  757. pthread_mutex_unlock(&ebpf_exit_cleanup);
  758. #endif
  759. char filename[FILENAME_MAX + 1];
  760. ebpf_pid_file(filename, FILENAME_MAX);
  761. if (unlink(filename))
  762. netdata_log_error("Cannot remove PID file %s", filename);
  763. #ifdef NETDATA_INTERNAL_CHECKS
  764. netdata_log_error("Good bye world! I was PID %d", main_thread_id);
  765. #endif
  766. fprintf(stdout, "EXIT\n");
  767. fflush(stdout);
  768. ebpf_check_before2go();
  769. pthread_mutex_lock(&mutex_cgroup_shm);
  770. if (shm_ebpf_cgroup.header) {
  771. ebpf_unmap_cgroup_shared_memory();
  772. shm_unlink(NETDATA_SHARED_MEMORY_EBPF_CGROUP_NAME);
  773. }
  774. pthread_mutex_unlock(&mutex_cgroup_shm);
  775. exit(0);
  776. }
  777. /**
  778. * Unload loegacy code
  779. *
  780. * @param objects objects loaded from eBPF programs
  781. * @param probe_links links from loader
  782. */
  783. void ebpf_unload_legacy_code(struct bpf_object *objects, struct bpf_link **probe_links)
  784. {
  785. if (!probe_links || !objects)
  786. return;
  787. struct bpf_program *prog;
  788. size_t j = 0 ;
  789. bpf_object__for_each_program(prog, objects) {
  790. bpf_link__destroy(probe_links[j]);
  791. j++;
  792. }
  793. freez(probe_links);
  794. if (objects)
  795. bpf_object__close(objects);
  796. }
  797. /**
  798. * Unload Unique maps
  799. *
  800. * This function unload all BPF maps from threads using one unique BPF object.
  801. */
  802. static void ebpf_unload_unique_maps()
  803. {
  804. int i;
  805. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  806. // These threads are cleaned with other functions
  807. if (i != EBPF_MODULE_SOCKET_IDX)
  808. continue;
  809. if (ebpf_modules[i].enabled != NETDATA_THREAD_EBPF_STOPPED) {
  810. if (ebpf_modules[i].enabled != NETDATA_THREAD_EBPF_NOT_RUNNING)
  811. netdata_log_error("Cannot unload maps for thread %s, because it is not stopped.",
  812. ebpf_modules[i].info.thread_name);
  813. continue;
  814. }
  815. if (ebpf_modules[i].load == EBPF_LOAD_LEGACY) {
  816. ebpf_unload_legacy_code(ebpf_modules[i].objects, ebpf_modules[i].probe_links);
  817. continue;
  818. }
  819. #ifdef LIBBPF_MAJOR_VERSION
  820. if (socket_bpf_obj)
  821. socket_bpf__destroy(socket_bpf_obj);
  822. #endif
  823. }
  824. }
  825. /**
  826. * Unload filesystem maps
  827. *
  828. * This function unload all BPF maps from filesystem thread.
  829. */
  830. static void ebpf_unload_filesystems()
  831. {
  832. if (ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].enabled == NETDATA_THREAD_EBPF_NOT_RUNNING ||
  833. ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].enabled < NETDATA_THREAD_EBPF_STOPPING ||
  834. ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].load != EBPF_LOAD_LEGACY)
  835. return;
  836. int i;
  837. for (i = 0; localfs[i].filesystem != NULL; i++) {
  838. if (!localfs[i].objects)
  839. continue;
  840. ebpf_unload_legacy_code(localfs[i].objects, localfs[i].probe_links);
  841. }
  842. }
  843. /**
  844. * Unload sync maps
  845. *
  846. * This function unload all BPF maps from sync thread.
  847. */
  848. static void ebpf_unload_sync()
  849. {
  850. if (ebpf_modules[EBPF_MODULE_SYNC_IDX].enabled == NETDATA_THREAD_EBPF_NOT_RUNNING ||
  851. ebpf_modules[EBPF_MODULE_SYNC_IDX].enabled < NETDATA_THREAD_EBPF_STOPPING)
  852. return;
  853. int i;
  854. for (i = 0; local_syscalls[i].syscall != NULL; i++) {
  855. if (!local_syscalls[i].enabled)
  856. continue;
  857. #ifdef LIBBPF_MAJOR_VERSION
  858. if (local_syscalls[i].sync_obj) {
  859. sync_bpf__destroy(local_syscalls[i].sync_obj);
  860. continue;
  861. }
  862. #endif
  863. ebpf_unload_legacy_code(local_syscalls[i].objects, local_syscalls[i].probe_links);
  864. }
  865. }
  866. /**
  867. * Close the collector gracefully
  868. *
  869. * @param sig is the signal number used to close the collector
  870. */
  871. void ebpf_stop_threads(int sig)
  872. {
  873. UNUSED(sig);
  874. static int only_one = 0;
  875. // Child thread should be closed by itself.
  876. pthread_mutex_lock(&ebpf_exit_cleanup);
  877. if (main_thread_id != gettid_cached() || only_one) {
  878. pthread_mutex_unlock(&ebpf_exit_cleanup);
  879. return;
  880. }
  881. only_one = 1;
  882. int i;
  883. for (i = 0; ebpf_modules[i].info.thread_name != NULL; i++) {
  884. if (ebpf_modules[i].enabled < NETDATA_THREAD_EBPF_STOPPING) {
  885. nd_thread_signal_cancel(ebpf_modules[i].thread->thread);
  886. #ifdef NETDATA_DEV_MODE
  887. netdata_log_info("Sending cancel for thread %s", ebpf_modules[i].info.thread_name);
  888. #endif
  889. }
  890. }
  891. pthread_mutex_unlock(&ebpf_exit_cleanup);
  892. for (i = 0; ebpf_modules[i].info.thread_name != NULL; i++) {
  893. if (ebpf_threads[i].thread)
  894. nd_thread_join(ebpf_threads[i].thread);
  895. }
  896. ebpf_plugin_exit = true;
  897. pthread_mutex_lock(&mutex_cgroup_shm);
  898. nd_thread_signal_cancel(cgroup_integration_thread.thread);
  899. #ifdef NETDATA_DEV_MODE
  900. netdata_log_info("Sending cancel for thread %s", cgroup_integration_thread.name);
  901. #endif
  902. pthread_mutex_unlock(&mutex_cgroup_shm);
  903. ebpf_check_before2go();
  904. pthread_mutex_lock(&ebpf_exit_cleanup);
  905. ebpf_unload_unique_maps();
  906. ebpf_unload_filesystems();
  907. ebpf_unload_sync();
  908. pthread_mutex_unlock(&ebpf_exit_cleanup);
  909. ebpf_exit();
  910. }
  911. /*****************************************************************
  912. *
  913. * FUNCTIONS TO CREATE CHARTS
  914. *
  915. *****************************************************************/
  916. /**
  917. * Create apps for module
  918. *
  919. * Create apps chart that will be used with specific module
  920. *
  921. * @param em the module main structure.
  922. * @param root a pointer for the targets.
  923. */
  924. static inline void ebpf_create_apps_for_module(ebpf_module_t *em, struct ebpf_target *root) {
  925. if (em->enabled < NETDATA_THREAD_EBPF_STOPPING && em->apps_charts && em->functions.apps_routine)
  926. em->functions.apps_routine(em, root);
  927. }
  928. /**
  929. * Create apps charts
  930. *
  931. * Call ebpf_create_chart to create the charts on apps submenu.
  932. *
  933. * @param root a pointer for the targets.
  934. */
  935. static void ebpf_create_apps_charts(struct ebpf_target *root)
  936. {
  937. if (unlikely(!ebpf_pids))
  938. return;
  939. struct ebpf_target *w;
  940. int newly_added = 0;
  941. for (w = root; w; w = w->next) {
  942. if (w->target)
  943. continue;
  944. if (unlikely(w->processes && (debug_enabled || w->debug_enabled))) {
  945. struct ebpf_pid_on_target *pid_on_target;
  946. fprintf(
  947. stderr, "ebpf.plugin: target '%s' has aggregated %u process%s:", w->name, w->processes,
  948. (w->processes == 1) ? "" : "es");
  949. for (pid_on_target = w->root_pid; pid_on_target; pid_on_target = pid_on_target->next) {
  950. fprintf(stderr, " %d", pid_on_target->pid);
  951. }
  952. fputc('\n', stderr);
  953. }
  954. if (!w->exposed && w->processes) {
  955. newly_added++;
  956. w->exposed = 1;
  957. if (debug_enabled || w->debug_enabled)
  958. debug_log_int("%s just added - regenerating charts.", w->name);
  959. }
  960. }
  961. if (newly_added) {
  962. int i;
  963. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX ; i++) {
  964. if (!(collect_pids & (1<<i)))
  965. continue;
  966. ebpf_module_t *current = &ebpf_modules[i];
  967. ebpf_create_apps_for_module(current, root);
  968. }
  969. }
  970. }
  971. /**
  972. * Get a value from a structure.
  973. *
  974. * @param basis it is the first address of the structure
  975. * @param offset it is the offset of the data you want to access.
  976. * @return
  977. */
  978. collected_number get_value_from_structure(char *basis, size_t offset)
  979. {
  980. collected_number *value = (collected_number *)(basis + offset);
  981. collected_number ret = (collected_number)llabs(*value);
  982. // this reset is necessary to avoid keep a constant value while processing is not executing a task
  983. *value = 0;
  984. return ret;
  985. }
  986. /**
  987. * Write set command on standard output
  988. *
  989. * @param dim the dimension name
  990. * @param value the value for the dimension
  991. */
  992. void write_chart_dimension(char *dim, long long value)
  993. {
  994. printf("SET %s = %lld\n", dim, value);
  995. }
  996. /**
  997. * Call the necessary functions to create a chart.
  998. *
  999. * @param name the chart name
  1000. * @param family the chart family
  1001. * @param move the pointer with the values that will be published
  1002. * @param end the number of values that will be written on standard output
  1003. *
  1004. * @return It returns a variable that maps the charts that did not have zero values.
  1005. */
  1006. void write_count_chart(char *name, char *family, netdata_publish_syscall_t *move, uint32_t end)
  1007. {
  1008. ebpf_write_begin_chart(family, name, "");
  1009. uint32_t i = 0;
  1010. while (move && i < end) {
  1011. write_chart_dimension(move->name, move->ncall);
  1012. move = move->next;
  1013. i++;
  1014. }
  1015. ebpf_write_end_chart();
  1016. }
  1017. /**
  1018. * Call the necessary functions to create a chart.
  1019. *
  1020. * @param name the chart name
  1021. * @param family the chart family
  1022. * @param move the pointer with the values that will be published
  1023. * @param end the number of values that will be written on standard output
  1024. */
  1025. void write_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end)
  1026. {
  1027. ebpf_write_begin_chart(family, name, "");
  1028. int i = 0;
  1029. while (move && i < end) {
  1030. write_chart_dimension(move->name, move->nerr);
  1031. move = move->next;
  1032. i++;
  1033. }
  1034. ebpf_write_end_chart();
  1035. }
  1036. /**
  1037. * Write charts
  1038. *
  1039. * Write the current information to publish the charts.
  1040. *
  1041. * @param family chart family
  1042. * @param chart chart id
  1043. * @param dim dimension name
  1044. * @param v1 value.
  1045. */
  1046. void ebpf_one_dimension_write_charts(char *family, char *chart, char *dim, long long v1)
  1047. {
  1048. ebpf_write_begin_chart(family, chart, "");
  1049. write_chart_dimension(dim, v1);
  1050. ebpf_write_end_chart();
  1051. }
  1052. /**
  1053. * Call the necessary functions to create a chart.
  1054. *
  1055. * @param chart the chart name
  1056. * @param family the chart family
  1057. * @param dwrite the dimension name
  1058. * @param vwrite the value for previous dimension
  1059. * @param dread the dimension name
  1060. * @param vread the value for previous dimension
  1061. *
  1062. * @return It returns a variable that maps the charts that did not have zero values.
  1063. */
  1064. void write_io_chart(char *chart, char *family, char *dwrite, long long vwrite, char *dread, long long vread)
  1065. {
  1066. ebpf_write_begin_chart(family, chart, "");
  1067. write_chart_dimension(dwrite, vwrite);
  1068. write_chart_dimension(dread, vread);
  1069. ebpf_write_end_chart();
  1070. }
  1071. /**
  1072. * Write chart cmd on standard output
  1073. *
  1074. * @param type chart type
  1075. * @param id chart id (the apps group name).
  1076. * @param suffix suffix to differentiate charts
  1077. * @param title chart title
  1078. * @param units units label
  1079. * @param family group name used to attach the chart on dashboard
  1080. * @param charttype chart type
  1081. * @param context chart context
  1082. * @param order chart order
  1083. * @param update_every update interval used by plugin
  1084. * @param module chart module name, this is the eBPF thread.
  1085. */
  1086. void ebpf_write_chart_cmd(char *type, char *id, char *suffix, char *title, char *units, char *family,
  1087. char *charttype, char *context, int order, int update_every, char *module)
  1088. {
  1089. printf("CHART %s.%s%s '' '%s' '%s' '%s' '%s' '%s' %d %d '' 'ebpf.plugin' '%s'\n",
  1090. type,
  1091. id,
  1092. suffix,
  1093. title,
  1094. units,
  1095. (family)?family:"",
  1096. (context)?context:"",
  1097. (charttype)?charttype:"",
  1098. order,
  1099. update_every,
  1100. module);
  1101. }
  1102. /**
  1103. * Write chart cmd on standard output
  1104. *
  1105. * @param type chart type
  1106. * @param id chart id
  1107. * @param suffix add suffix to obsolete charts.
  1108. * @param title chart title
  1109. * @param units units label
  1110. * @param family group name used to attach the chart on dashboard
  1111. * @param charttype chart type
  1112. * @param context chart context
  1113. * @param order chart order
  1114. * @param update_every value to overwrite the update frequency set by the server.
  1115. */
  1116. void ebpf_write_chart_obsolete(char *type, char *id, char *suffix, char *title, char *units, char *family,
  1117. char *charttype, char *context, int order, int update_every)
  1118. {
  1119. printf("CHART %s.%s%s '' '%s' '%s' '%s' '%s' '%s' %d %d 'obsolete'\n",
  1120. type,
  1121. id,
  1122. suffix,
  1123. title,
  1124. units,
  1125. (family)?family:"",
  1126. (context)?context:"",
  1127. (charttype)?charttype:"",
  1128. order,
  1129. update_every);
  1130. }
  1131. /**
  1132. * Write the dimension command on standard output
  1133. *
  1134. * @param name the dimension name
  1135. * @param id the dimension id
  1136. * @param algo the dimension algorithm
  1137. */
  1138. void ebpf_write_global_dimension(char *name, char *id, char *algorithm)
  1139. {
  1140. printf("DIMENSION %s %s %s 1 1\n", name, id, algorithm);
  1141. }
  1142. /**
  1143. * Call ebpf_write_global_dimension to create the dimensions for a specific chart
  1144. *
  1145. * @param ptr a pointer to a structure of the type netdata_publish_syscall_t
  1146. * @param end the number of dimensions for the structure ptr
  1147. */
  1148. void ebpf_create_global_dimension(void *ptr, int end)
  1149. {
  1150. netdata_publish_syscall_t *move = ptr;
  1151. int i = 0;
  1152. while (move && i < end) {
  1153. ebpf_write_global_dimension(move->name, move->dimension, move->algorithm);
  1154. move = move->next;
  1155. i++;
  1156. }
  1157. }
  1158. /**
  1159. * Call write_chart_cmd to create the charts
  1160. *
  1161. * @param type chart type
  1162. * @param id chart id
  1163. * @param title chart title
  1164. * @param units axis label
  1165. * @param family group name used to attach the chart on dashboard
  1166. * @param context chart context
  1167. * @param charttype chart type
  1168. * @param order order number of the specified chart
  1169. * @param ncd a pointer to a function called to create dimensions
  1170. * @param move a pointer for a structure that has the dimensions
  1171. * @param end number of dimensions for the chart created
  1172. * @param update_every update interval used with chart.
  1173. * @param module chart module name, this is the eBPF thread.
  1174. */
  1175. void ebpf_create_chart(char *type,
  1176. char *id,
  1177. char *title,
  1178. char *units,
  1179. char *family,
  1180. char *context,
  1181. char *charttype,
  1182. int order,
  1183. void (*ncd)(void *, int),
  1184. void *move,
  1185. int end,
  1186. int update_every,
  1187. char *module)
  1188. {
  1189. ebpf_write_chart_cmd(type, id, "", title, units, family, charttype, context, order, update_every, module);
  1190. if (ncd) {
  1191. ncd(move, end);
  1192. }
  1193. }
  1194. /**
  1195. * Call the necessary functions to create a name.
  1196. *
  1197. * @param family family name
  1198. * @param name chart name
  1199. * @param hist0 histogram values
  1200. * @param dimensions dimension values.
  1201. * @param end number of bins that will be sent to Netdata.
  1202. *
  1203. * @return It returns a variable that maps the charts that did not have zero values.
  1204. */
  1205. void write_histogram_chart(char *family, char *name, const netdata_idx_t *hist, char **dimensions, uint32_t end)
  1206. {
  1207. ebpf_write_begin_chart(family, name, "");
  1208. uint32_t i;
  1209. for (i = 0; i < end; i++) {
  1210. write_chart_dimension(dimensions[i], (long long) hist[i]);
  1211. }
  1212. ebpf_write_end_chart();
  1213. fflush(stdout);
  1214. }
  1215. /**
  1216. * ARAL Charts
  1217. *
  1218. * Add chart to monitor ARAL usage
  1219. * Caller must call this function with mutex locked.
  1220. *
  1221. * @param name the name used to create aral
  1222. * @param em a pointer to the structure with the default values.
  1223. */
  1224. int ebpf_statistic_create_aral_chart(char *name, ebpf_module_t *em)
  1225. {
  1226. static int priority = NETATA_EBPF_ORDER_STAT_ARAL_BEGIN;
  1227. char *mem = { NETDATA_EBPF_STAT_DIMENSION_MEMORY };
  1228. char *aral = { NETDATA_EBPF_STAT_DIMENSION_ARAL };
  1229. snprintfz(em->memory_usage, NETDATA_EBPF_CHART_MEM_LENGTH -1, "aral_%s_size", name);
  1230. snprintfz(em->memory_allocations, NETDATA_EBPF_CHART_MEM_LENGTH -1, "aral_%s_alloc", name);
  1231. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  1232. em->memory_usage,
  1233. "",
  1234. "Bytes allocated for ARAL.",
  1235. "bytes",
  1236. NETDATA_EBPF_FAMILY,
  1237. NETDATA_EBPF_CHART_TYPE_STACKED,
  1238. "netdata.ebpf_aral_stat_size",
  1239. priority++,
  1240. em->update_every,
  1241. NETDATA_EBPF_MODULE_NAME_PROCESS);
  1242. ebpf_write_global_dimension(mem,
  1243. mem,
  1244. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  1245. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  1246. em->memory_allocations,
  1247. "",
  1248. "Calls to allocate memory.",
  1249. "calls",
  1250. NETDATA_EBPF_FAMILY,
  1251. NETDATA_EBPF_CHART_TYPE_STACKED,
  1252. "netdata.ebpf_aral_stat_alloc",
  1253. priority++,
  1254. em->update_every,
  1255. NETDATA_EBPF_MODULE_NAME_PROCESS);
  1256. ebpf_write_global_dimension(aral,
  1257. aral,
  1258. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  1259. return priority - 2;
  1260. }
  1261. /**
  1262. * ARAL Charts
  1263. *
  1264. * Add chart to monitor ARAL usage
  1265. * Caller must call this function with mutex locked.
  1266. *
  1267. * @param em a pointer to the structure with the default values.
  1268. * @param prio the initial priority used to disable charts.
  1269. */
  1270. void ebpf_statistic_obsolete_aral_chart(ebpf_module_t *em, int prio)
  1271. {
  1272. ebpf_write_chart_obsolete(NETDATA_MONITORING_FAMILY,
  1273. em->memory_allocations,
  1274. "",
  1275. "Calls to allocate memory.",
  1276. "calls",
  1277. NETDATA_EBPF_FAMILY,
  1278. NETDATA_EBPF_CHART_TYPE_STACKED,
  1279. "netdata.ebpf_aral_stat_alloc",
  1280. prio++,
  1281. em->update_every);
  1282. ebpf_write_chart_obsolete(NETDATA_MONITORING_FAMILY,
  1283. em->memory_allocations,
  1284. "",
  1285. "Calls to allocate memory.",
  1286. "calls",
  1287. NETDATA_EBPF_FAMILY,
  1288. NETDATA_EBPF_CHART_TYPE_STACKED,
  1289. "netdata.ebpf_aral_stat_alloc",
  1290. prio++,
  1291. em->update_every);
  1292. }
  1293. /**
  1294. * Send data from aral chart
  1295. *
  1296. * Send data for eBPF plugin
  1297. *
  1298. * @param memory a pointer to the allocated address
  1299. * @param em a pointer to the structure with the default values.
  1300. */
  1301. void ebpf_send_data_aral_chart(ARAL *memory, ebpf_module_t *em)
  1302. {
  1303. char *mem = { NETDATA_EBPF_STAT_DIMENSION_MEMORY };
  1304. char *aral = { NETDATA_EBPF_STAT_DIMENSION_ARAL };
  1305. struct aral_statistics *stats = aral_get_statistics(memory);
  1306. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, em->memory_usage, "");
  1307. write_chart_dimension(mem, (long long)stats->structures.allocated_bytes);
  1308. ebpf_write_end_chart();
  1309. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, em->memory_allocations, "");
  1310. write_chart_dimension(aral, (long long)stats->structures.allocations);
  1311. ebpf_write_end_chart();
  1312. }
  1313. /*****************************************************************
  1314. *
  1315. * FUNCTIONS TO READ GLOBAL HASH TABLES
  1316. *
  1317. *****************************************************************/
  1318. /**
  1319. * Read Global Table Stats
  1320. *
  1321. * Read data from specified table (map_fd) using array allocated inside thread(values) and storing
  1322. * them in stats vector starting from the first position.
  1323. *
  1324. * For PID tables is recommended to use a function to parse the specific data.
  1325. *
  1326. * @param stats vector used to store data
  1327. * @param values helper to read data from hash tables.
  1328. * @param map_fd table that has data
  1329. * @param maps_per_core Is necessary to read data from all cores?
  1330. * @param begin initial value to query hash table
  1331. * @param end last value that will not be used.
  1332. */
  1333. void ebpf_read_global_table_stats(netdata_idx_t *stats,
  1334. netdata_idx_t *values,
  1335. int map_fd,
  1336. int maps_per_core,
  1337. uint32_t begin,
  1338. uint32_t end)
  1339. {
  1340. uint32_t idx, order;
  1341. for (idx = begin, order = 0; idx < end; idx++, order++) {
  1342. if (!bpf_map_lookup_elem(map_fd, &idx, values)) {
  1343. int i;
  1344. int before = (maps_per_core) ? ebpf_nprocs: 1;
  1345. netdata_idx_t total = 0;
  1346. for (i = 0; i < before; i++)
  1347. total += values[i];
  1348. stats[order] = total;
  1349. }
  1350. }
  1351. }
  1352. /*****************************************************************
  1353. *
  1354. * FUNCTIONS USED WITH SOCKET
  1355. *
  1356. *****************************************************************/
  1357. /**
  1358. * Netmask
  1359. *
  1360. * Copied from iprange (https://github.com/firehol/iprange/blob/master/iprange.h)
  1361. *
  1362. * @param prefix create the netmask based in the CIDR value.
  1363. *
  1364. * @return
  1365. */
  1366. static inline in_addr_t ebpf_netmask(int prefix) {
  1367. if (prefix == 0)
  1368. return (~((in_addr_t) - 1));
  1369. else
  1370. return (in_addr_t)(~((1 << (32 - prefix)) - 1));
  1371. }
  1372. /**
  1373. * Broadcast
  1374. *
  1375. * Copied from iprange (https://github.com/firehol/iprange/blob/master/iprange.h)
  1376. *
  1377. * @param addr is the ip address
  1378. * @param prefix is the CIDR value.
  1379. *
  1380. * @return It returns the last address of the range
  1381. */
  1382. static inline in_addr_t ebpf_broadcast(in_addr_t addr, int prefix)
  1383. {
  1384. return (addr | ~ebpf_netmask(prefix));
  1385. }
  1386. /**
  1387. * Network
  1388. *
  1389. * Copied from iprange (https://github.com/firehol/iprange/blob/master/iprange.h)
  1390. *
  1391. * @param addr is the ip address
  1392. * @param prefix is the CIDR value.
  1393. *
  1394. * @return It returns the first address of the range.
  1395. */
  1396. static inline in_addr_t ebpf_ipv4_network(in_addr_t addr, int prefix)
  1397. {
  1398. return (addr & ebpf_netmask(prefix));
  1399. }
  1400. /**
  1401. * Calculate ipv6 first address
  1402. *
  1403. * @param out the address to store the first address.
  1404. * @param in the address used to do the math.
  1405. * @param prefix number of bits used to calculate the address
  1406. */
  1407. static void get_ipv6_first_addr(union netdata_ip_t *out, union netdata_ip_t *in, uint64_t prefix)
  1408. {
  1409. uint64_t mask,tmp;
  1410. uint64_t ret[2];
  1411. memcpy(ret, in->addr32, sizeof(union netdata_ip_t));
  1412. if (prefix == 128) {
  1413. memcpy(out->addr32, in->addr32, sizeof(union netdata_ip_t));
  1414. return;
  1415. } else if (!prefix) {
  1416. ret[0] = ret[1] = 0;
  1417. memcpy(out->addr32, ret, sizeof(union netdata_ip_t));
  1418. return;
  1419. } else if (prefix <= 64) {
  1420. ret[1] = 0ULL;
  1421. tmp = be64toh(ret[0]);
  1422. mask = 0xFFFFFFFFFFFFFFFFULL << (64 - prefix);
  1423. tmp &= mask;
  1424. ret[0] = htobe64(tmp);
  1425. } else {
  1426. mask = 0xFFFFFFFFFFFFFFFFULL << (128 - prefix);
  1427. tmp = be64toh(ret[1]);
  1428. tmp &= mask;
  1429. ret[1] = htobe64(tmp);
  1430. }
  1431. memcpy(out->addr32, ret, sizeof(union netdata_ip_t));
  1432. }
  1433. /**
  1434. * Get IPV6 Last Address
  1435. *
  1436. * @param out the address to store the last address.
  1437. * @param in the address used to do the math.
  1438. * @param prefix number of bits used to calculate the address
  1439. */
  1440. static void get_ipv6_last_addr(union netdata_ip_t *out, union netdata_ip_t *in, uint64_t prefix)
  1441. {
  1442. uint64_t mask,tmp;
  1443. uint64_t ret[2];
  1444. memcpy(ret, in->addr32, sizeof(union netdata_ip_t));
  1445. if (prefix == 128) {
  1446. memcpy(out->addr32, in->addr32, sizeof(union netdata_ip_t));
  1447. return;
  1448. } else if (!prefix) {
  1449. ret[0] = ret[1] = 0xFFFFFFFFFFFFFFFF;
  1450. memcpy(out->addr32, ret, sizeof(union netdata_ip_t));
  1451. return;
  1452. } else if (prefix <= 64) {
  1453. ret[1] = 0xFFFFFFFFFFFFFFFFULL;
  1454. tmp = be64toh(ret[0]);
  1455. mask = 0xFFFFFFFFFFFFFFFFULL << (64 - prefix);
  1456. tmp |= ~mask;
  1457. ret[0] = htobe64(tmp);
  1458. } else {
  1459. mask = 0xFFFFFFFFFFFFFFFFULL << (128 - prefix);
  1460. tmp = be64toh(ret[1]);
  1461. tmp |= ~mask;
  1462. ret[1] = htobe64(tmp);
  1463. }
  1464. memcpy(out->addr32, ret, sizeof(union netdata_ip_t));
  1465. }
  1466. /**
  1467. * IP to network long
  1468. *
  1469. * @param dst the vector to store the result
  1470. * @param ip the source ip given by our users.
  1471. * @param domain the ip domain (IPV4 or IPV6)
  1472. * @param source the original string
  1473. *
  1474. * @return it returns 0 on success and -1 otherwise.
  1475. */
  1476. static inline int ebpf_ip2nl(uint8_t *dst, const char *ip, int domain, char *source)
  1477. {
  1478. if (inet_pton(domain, ip, dst) <= 0) {
  1479. netdata_log_error("The address specified (%s) is invalid ", source);
  1480. return -1;
  1481. }
  1482. return 0;
  1483. }
  1484. /**
  1485. * Clean port Structure
  1486. *
  1487. * Clean the allocated list.
  1488. *
  1489. * @param clean the list that will be cleaned
  1490. */
  1491. void ebpf_clean_port_structure(ebpf_network_viewer_port_list_t **clean)
  1492. {
  1493. ebpf_network_viewer_port_list_t *move = *clean;
  1494. while (move) {
  1495. ebpf_network_viewer_port_list_t *next = move->next;
  1496. freez(move->value);
  1497. freez(move);
  1498. move = next;
  1499. }
  1500. *clean = NULL;
  1501. }
  1502. /**
  1503. * Clean IP structure
  1504. *
  1505. * Clean the allocated list.
  1506. *
  1507. * @param clean the list that will be cleaned
  1508. */
  1509. void ebpf_clean_ip_structure(ebpf_network_viewer_ip_list_t **clean)
  1510. {
  1511. ebpf_network_viewer_ip_list_t *move = *clean;
  1512. while (move) {
  1513. ebpf_network_viewer_ip_list_t *next = move->next;
  1514. freez(move->value);
  1515. freez(move);
  1516. move = next;
  1517. }
  1518. *clean = NULL;
  1519. }
  1520. /**
  1521. * Parse IP List
  1522. *
  1523. * Parse IP list and link it.
  1524. *
  1525. * @param out a pointer to store the link list
  1526. * @param ip the value given as parameter
  1527. */
  1528. static void ebpf_parse_ip_list_unsafe(void **out, const char *ip)
  1529. {
  1530. ebpf_network_viewer_ip_list_t **list = (ebpf_network_viewer_ip_list_t **)out;
  1531. char *ipdup = strdupz(ip);
  1532. union netdata_ip_t first = { };
  1533. union netdata_ip_t last = { };
  1534. const char *is_ipv6;
  1535. if (*ip == '*' && *(ip+1) == '\0') {
  1536. memset(first.addr8, 0, sizeof(first.addr8));
  1537. memset(last.addr8, 0xFF, sizeof(last.addr8));
  1538. is_ipv6 = ip;
  1539. ebpf_clean_ip_structure(list);
  1540. goto storethisip;
  1541. }
  1542. char *enddup = strdupz(ip);
  1543. char *end = enddup;
  1544. // Move while I cannot find a separator
  1545. while (*end && *end != '/' && *end != '-') end++;
  1546. // We will use only the classic IPV6 for while, but we could consider the base 85 in a near future
  1547. // https://tools.ietf.org/html/rfc1924
  1548. is_ipv6 = strchr(ip, ':');
  1549. int select;
  1550. if (*end && !is_ipv6) { // IPV4 range
  1551. select = (*end == '/') ? 0 : 1;
  1552. *end++ = '\0';
  1553. if (*end == '!') {
  1554. netdata_log_info("The exclusion cannot be in the second part of the range %s, it will be ignored.", ipdup);
  1555. goto cleanipdup;
  1556. }
  1557. if (!select) { // CIDR
  1558. select = ebpf_ip2nl(first.addr8, ip, AF_INET, ipdup);
  1559. if (select)
  1560. goto cleanipdup;
  1561. select = (int) str2i(end);
  1562. if (select < NETDATA_MINIMUM_IPV4_CIDR || select > NETDATA_MAXIMUM_IPV4_CIDR) {
  1563. netdata_log_info("The specified CIDR %s is not valid, the IP %s will be ignored.", end, ip);
  1564. goto cleanipdup;
  1565. }
  1566. last.addr32[0] = htonl(ebpf_broadcast(ntohl(first.addr32[0]), select));
  1567. // This was added to remove
  1568. // https://app.codacy.com/manual/netdata/netdata/pullRequest?prid=5810941&bid=19021977
  1569. UNUSED(last.addr32[0]);
  1570. uint32_t ipv4_test = htonl(ebpf_ipv4_network(ntohl(first.addr32[0]), select));
  1571. if (first.addr32[0] != ipv4_test) {
  1572. first.addr32[0] = ipv4_test;
  1573. struct in_addr ipv4_convert;
  1574. ipv4_convert.s_addr = ipv4_test;
  1575. char ipv4_msg[INET_ADDRSTRLEN];
  1576. if(inet_ntop(AF_INET, &ipv4_convert, ipv4_msg, INET_ADDRSTRLEN))
  1577. netdata_log_info("The network value of CIDR %s was updated for %s .", ipdup, ipv4_msg);
  1578. }
  1579. } else { // Range
  1580. select = ebpf_ip2nl(first.addr8, ip, AF_INET, ipdup);
  1581. if (select)
  1582. goto cleanipdup;
  1583. select = ebpf_ip2nl(last.addr8, end, AF_INET, ipdup);
  1584. if (select)
  1585. goto cleanipdup;
  1586. }
  1587. if (htonl(first.addr32[0]) > htonl(last.addr32[0])) {
  1588. netdata_log_info("The specified range %s is invalid, the second address is smallest than the first, it will be ignored.",
  1589. ipdup);
  1590. goto cleanipdup;
  1591. }
  1592. } else if (is_ipv6) { // IPV6
  1593. if (!*end) { // Unique
  1594. select = ebpf_ip2nl(first.addr8, ip, AF_INET6, ipdup);
  1595. if (select)
  1596. goto cleanipdup;
  1597. memcpy(last.addr8, first.addr8, sizeof(first.addr8));
  1598. } else if (*end == '-') {
  1599. *end++ = 0x00;
  1600. if (*end == '!') {
  1601. netdata_log_info("The exclusion cannot be in the second part of the range %s, it will be ignored.", ipdup);
  1602. goto cleanipdup;
  1603. }
  1604. select = ebpf_ip2nl(first.addr8, ip, AF_INET6, ipdup);
  1605. if (select)
  1606. goto cleanipdup;
  1607. select = ebpf_ip2nl(last.addr8, end, AF_INET6, ipdup);
  1608. if (select)
  1609. goto cleanipdup;
  1610. } else { // CIDR
  1611. *end++ = 0x00;
  1612. if (*end == '!') {
  1613. netdata_log_info("The exclusion cannot be in the second part of the range %s, it will be ignored.", ipdup);
  1614. goto cleanipdup;
  1615. }
  1616. select = str2i(end);
  1617. if (select < 0 || select > 128) {
  1618. netdata_log_info("The CIDR %s is not valid, the address %s will be ignored.", end, ip);
  1619. goto cleanipdup;
  1620. }
  1621. uint64_t prefix = (uint64_t)select;
  1622. select = ebpf_ip2nl(first.addr8, ip, AF_INET6, ipdup);
  1623. if (select)
  1624. goto cleanipdup;
  1625. get_ipv6_last_addr(&last, &first, prefix);
  1626. union netdata_ip_t ipv6_test;
  1627. get_ipv6_first_addr(&ipv6_test, &first, prefix);
  1628. if (memcmp(first.addr8, ipv6_test.addr8, sizeof(union netdata_ip_t)) != 0) {
  1629. memcpy(first.addr8, ipv6_test.addr8, sizeof(union netdata_ip_t));
  1630. struct in6_addr ipv6_convert;
  1631. memcpy(ipv6_convert.s6_addr, ipv6_test.addr8, sizeof(union netdata_ip_t));
  1632. char ipv6_msg[INET6_ADDRSTRLEN];
  1633. if(inet_ntop(AF_INET6, &ipv6_convert, ipv6_msg, INET6_ADDRSTRLEN))
  1634. netdata_log_info("The network value of CIDR %s was updated for %s .", ipdup, ipv6_msg);
  1635. }
  1636. }
  1637. if ((be64toh(*(uint64_t *)&first.addr64[1]) > be64toh(*(uint64_t *)&last.addr64[1]) &&
  1638. !memcmp(first.addr64, last.addr64, sizeof(uint64_t))) ||
  1639. (be64toh(*(uint64_t *)&first.addr64) > be64toh(*(uint64_t *)&last.addr64)) ) {
  1640. netdata_log_info("The specified range %s is invalid, the second address is smallest than the first, it will be ignored.",
  1641. ipdup);
  1642. goto cleanipdup;
  1643. }
  1644. } else { // Unique ip
  1645. select = ebpf_ip2nl(first.addr8, ip, AF_INET, ipdup);
  1646. if (select)
  1647. goto cleanipdup;
  1648. memcpy(last.addr8, first.addr8, sizeof(first.addr8));
  1649. }
  1650. ebpf_network_viewer_ip_list_t *store;
  1651. storethisip:
  1652. store = callocz(1, sizeof(ebpf_network_viewer_ip_list_t));
  1653. store->value = ipdup;
  1654. store->hash = simple_hash(ipdup);
  1655. store->ver = (uint8_t)(!is_ipv6)?AF_INET:AF_INET6;
  1656. memcpy(store->first.addr8, first.addr8, sizeof(first.addr8));
  1657. memcpy(store->last.addr8, last.addr8, sizeof(last.addr8));
  1658. ebpf_fill_ip_list_unsafe(list, store, "socket");
  1659. return;
  1660. cleanipdup:
  1661. freez(ipdup);
  1662. freez(enddup);
  1663. }
  1664. /**
  1665. * Parse IP Range
  1666. *
  1667. * Parse the IP ranges given and create Network Viewer IP Structure
  1668. *
  1669. * @param ptr is a pointer with the text to parse.
  1670. */
  1671. void ebpf_parse_ips_unsafe(const char *ptr)
  1672. {
  1673. // No value
  1674. if (unlikely(!ptr))
  1675. return;
  1676. while (likely(ptr)) {
  1677. // Move forward until next valid character
  1678. while (isspace(*ptr)) ptr++;
  1679. // No valid value found
  1680. if (unlikely(!*ptr))
  1681. return;
  1682. // Find space that ends the list
  1683. char *end = strchr(ptr, ' ');
  1684. if (end) {
  1685. *end++ = '\0';
  1686. }
  1687. int neg = 0;
  1688. if (*ptr == '!') {
  1689. neg++;
  1690. ptr++;
  1691. }
  1692. if (isascii(*ptr)) { // Parse port
  1693. ebpf_parse_ip_list_unsafe(
  1694. (!neg) ? (void **)&network_viewer_opt.included_ips : (void **)&network_viewer_opt.excluded_ips, ptr);
  1695. }
  1696. ptr = end;
  1697. }
  1698. }
  1699. /**
  1700. * Fill Port list
  1701. *
  1702. * @param out a pointer to the link list.
  1703. * @param in the structure that will be linked.
  1704. */
  1705. static inline void fill_port_list(ebpf_network_viewer_port_list_t **out, ebpf_network_viewer_port_list_t *in)
  1706. {
  1707. if (likely(*out)) {
  1708. ebpf_network_viewer_port_list_t *move = *out, *store = *out;
  1709. uint16_t first = ntohs(in->first);
  1710. uint16_t last = ntohs(in->last);
  1711. while (move) {
  1712. uint16_t cmp_first = ntohs(move->first);
  1713. uint16_t cmp_last = ntohs(move->last);
  1714. if (cmp_first <= first && first <= cmp_last &&
  1715. cmp_first <= last && last <= cmp_last ) {
  1716. netdata_log_info("The range/value (%u, %u) is inside the range/value (%u, %u) already inserted, it will be ignored.",
  1717. first, last, cmp_first, cmp_last);
  1718. freez(in->value);
  1719. freez(in);
  1720. return;
  1721. } else if (first <= cmp_first && cmp_first <= last &&
  1722. first <= cmp_last && cmp_last <= last) {
  1723. netdata_log_info("The range (%u, %u) is bigger than previous range (%u, %u) already inserted, the previous will be ignored.",
  1724. first, last, cmp_first, cmp_last);
  1725. freez(move->value);
  1726. move->value = in->value;
  1727. move->first = in->first;
  1728. move->last = in->last;
  1729. freez(in);
  1730. return;
  1731. }
  1732. store = move;
  1733. move = move->next;
  1734. }
  1735. store->next = in;
  1736. } else {
  1737. *out = in;
  1738. }
  1739. #ifdef NETDATA_INTERNAL_CHECKS
  1740. netdata_log_info("Adding values %s( %u, %u) to %s port list used on network viewer",
  1741. in->value, in->first, in->last,
  1742. (*out == network_viewer_opt.included_port)?"included":"excluded");
  1743. #endif
  1744. }
  1745. /**
  1746. * Parse Service List
  1747. *
  1748. * @param out a pointer to store the link list
  1749. * @param service the service used to create the structure that will be linked.
  1750. */
  1751. static void ebpf_parse_service_list(void **out, const char *service)
  1752. {
  1753. ebpf_network_viewer_port_list_t **list = (ebpf_network_viewer_port_list_t **)out;
  1754. struct servent *serv = getservbyname((const char *)service, "tcp");
  1755. if (!serv)
  1756. serv = getservbyname((const char *)service, "udp");
  1757. if (!serv) {
  1758. netdata_log_info("Cannot resolve the service '%s' with protocols TCP and UDP, it will be ignored", service);
  1759. return;
  1760. }
  1761. ebpf_network_viewer_port_list_t *w = callocz(1, sizeof(ebpf_network_viewer_port_list_t));
  1762. w->value = strdupz(service);
  1763. w->hash = simple_hash(service);
  1764. w->first = w->last = (uint16_t)serv->s_port;
  1765. fill_port_list(list, w);
  1766. }
  1767. /**
  1768. * Parse port list
  1769. *
  1770. * Parse an allocated port list with the range given
  1771. *
  1772. * @param out a pointer to store the link list
  1773. * @param range the informed range for the user.
  1774. */
  1775. static void ebpf_parse_port_list(void **out, const char *range_param) {
  1776. char range[strlen(range_param) + 1];
  1777. strncpyz(range, range_param, strlen(range_param));
  1778. int first, last;
  1779. ebpf_network_viewer_port_list_t **list = (ebpf_network_viewer_port_list_t **)out;
  1780. char *copied = strdupz(range);
  1781. if (*range == '*' && *(range+1) == '\0') {
  1782. first = 1;
  1783. last = 65535;
  1784. ebpf_clean_port_structure(list);
  1785. goto fillenvpl;
  1786. }
  1787. char *end = range;
  1788. //Move while I cannot find a separator
  1789. while (*end && *end != ':' && *end != '-') end++;
  1790. //It has a range
  1791. if (likely(*end)) {
  1792. *end++ = '\0';
  1793. if (*end == '!') {
  1794. netdata_log_info("The exclusion cannot be in the second part of the range, the range %s will be ignored.", copied);
  1795. freez(copied);
  1796. return;
  1797. }
  1798. last = str2i((const char *)end);
  1799. } else {
  1800. last = 0;
  1801. }
  1802. first = str2i((const char *)range);
  1803. if (first < NETDATA_MINIMUM_PORT_VALUE || first > NETDATA_MAXIMUM_PORT_VALUE) {
  1804. netdata_log_info("The first port %d of the range \"%s\" is invalid and it will be ignored!", first, copied);
  1805. freez(copied);
  1806. return;
  1807. }
  1808. if (!last)
  1809. last = first;
  1810. if (last < NETDATA_MINIMUM_PORT_VALUE || last > NETDATA_MAXIMUM_PORT_VALUE) {
  1811. netdata_log_info("The second port %d of the range \"%s\" is invalid and the whole range will be ignored!", last, copied);
  1812. freez(copied);
  1813. return;
  1814. }
  1815. if (first > last) {
  1816. netdata_log_info("The specified order %s is wrong, the smallest value is always the first, it will be ignored!", copied);
  1817. freez(copied);
  1818. return;
  1819. }
  1820. ebpf_network_viewer_port_list_t *w;
  1821. fillenvpl:
  1822. w = callocz(1, sizeof(ebpf_network_viewer_port_list_t));
  1823. w->value = copied;
  1824. w->hash = simple_hash(copied);
  1825. w->first = (uint16_t)first;
  1826. w->last = (uint16_t)last;
  1827. w->cmp_first = (uint16_t)first;
  1828. w->cmp_last = (uint16_t)last;
  1829. fill_port_list(list, w);
  1830. }
  1831. /**
  1832. * Parse Port Range
  1833. *
  1834. * Parse the port ranges given and create Network Viewer Port Structure
  1835. *
  1836. * @param ptr is a pointer with the text to parse.
  1837. */
  1838. void ebpf_parse_ports(const char *ptr)
  1839. {
  1840. // No value
  1841. if (unlikely(!ptr))
  1842. return;
  1843. while (likely(ptr)) {
  1844. // Move forward until next valid character
  1845. while (isspace(*ptr)) ptr++;
  1846. // No valid value found
  1847. if (unlikely(!*ptr))
  1848. return;
  1849. // Find space that ends the list
  1850. char *end = strchr(ptr, ' ');
  1851. if (end) {
  1852. *end++ = '\0';
  1853. }
  1854. int neg = 0;
  1855. if (*ptr == '!') {
  1856. neg++;
  1857. ptr++;
  1858. }
  1859. if (isdigit(*ptr)) { // Parse port
  1860. ebpf_parse_port_list(
  1861. (!neg) ? (void **)&network_viewer_opt.included_port : (void **)&network_viewer_opt.excluded_port, ptr);
  1862. } else if (isalpha(*ptr)) { // Parse service
  1863. ebpf_parse_service_list(
  1864. (!neg) ? (void **)&network_viewer_opt.included_port : (void **)&network_viewer_opt.excluded_port, ptr);
  1865. } else if (*ptr == '*') { // All
  1866. ebpf_parse_port_list(
  1867. (!neg) ? (void **)&network_viewer_opt.included_port : (void **)&network_viewer_opt.excluded_port, ptr);
  1868. }
  1869. ptr = end;
  1870. }
  1871. }
  1872. /*****************************************************************
  1873. *
  1874. * FUNCTIONS TO DEFINE OPTIONS
  1875. *
  1876. *****************************************************************/
  1877. /**
  1878. * Define labels used to generate charts
  1879. *
  1880. * @param is structure with information about number of calls made for a function.
  1881. * @param pio structure used to generate charts.
  1882. * @param dim a pointer for the dimensions name
  1883. * @param name a pointer for the tensor with the name of the functions.
  1884. * @param algorithm a vector with the algorithms used to make the charts
  1885. * @param end the number of elements in the previous 4 arguments.
  1886. */
  1887. void ebpf_global_labels(netdata_syscall_stat_t *is, netdata_publish_syscall_t *pio, char **dim,
  1888. char **name, int *algorithm, int end)
  1889. {
  1890. int i;
  1891. netdata_syscall_stat_t *prev = NULL;
  1892. netdata_publish_syscall_t *publish_prev = NULL;
  1893. for (i = 0; i < end; i++) {
  1894. if (prev) {
  1895. prev->next = &is[i];
  1896. }
  1897. prev = &is[i];
  1898. pio[i].dimension = dim[i];
  1899. pio[i].name = name[i];
  1900. pio[i].algorithm = ebpf_algorithms[algorithm[i]];
  1901. if (publish_prev) {
  1902. publish_prev->next = &pio[i];
  1903. }
  1904. publish_prev = &pio[i];
  1905. }
  1906. }
  1907. /**
  1908. * Define thread mode for all ebpf program.
  1909. *
  1910. * @param lmode the mode that will be used for them.
  1911. */
  1912. static inline void ebpf_set_thread_mode(netdata_run_mode_t lmode)
  1913. {
  1914. int i;
  1915. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  1916. ebpf_modules[i].mode = lmode;
  1917. }
  1918. }
  1919. /**
  1920. * Enable specific charts selected by user.
  1921. *
  1922. * @param em the structure that will be changed
  1923. * @param disable_cgroup the status about the cgroups charts.
  1924. */
  1925. static inline void ebpf_enable_specific_chart(struct ebpf_module *em, int disable_cgroup)
  1926. {
  1927. em->enabled = NETDATA_THREAD_EBPF_RUNNING;
  1928. if (!disable_cgroup) {
  1929. em->cgroup_charts = CONFIG_BOOLEAN_YES;
  1930. }
  1931. em->global_charts = CONFIG_BOOLEAN_YES;
  1932. }
  1933. /**
  1934. * Disable all Global charts
  1935. *
  1936. * Disable charts
  1937. */
  1938. static inline void disable_all_global_charts()
  1939. {
  1940. int i;
  1941. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  1942. ebpf_modules[i].enabled = NETDATA_THREAD_EBPF_NOT_RUNNING;
  1943. ebpf_modules[i].global_charts = 0;
  1944. }
  1945. }
  1946. /**
  1947. * Enable the specified chart group
  1948. *
  1949. * @param idx the index of ebpf_modules that I am enabling
  1950. */
  1951. static inline void ebpf_enable_chart(int idx, int disable_cgroup)
  1952. {
  1953. int i;
  1954. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  1955. if (i == idx) {
  1956. ebpf_enable_specific_chart(&ebpf_modules[i], disable_cgroup);
  1957. break;
  1958. }
  1959. }
  1960. }
  1961. /**
  1962. * Disable Cgroups
  1963. *
  1964. * Disable charts for apps loading only global charts.
  1965. */
  1966. static inline void ebpf_disable_cgroups()
  1967. {
  1968. int i;
  1969. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  1970. ebpf_modules[i].cgroup_charts = 0;
  1971. }
  1972. }
  1973. /**
  1974. * Update Disabled Plugins
  1975. *
  1976. * This function calls ebpf_update_stats to update statistics for collector.
  1977. *
  1978. * @param em a pointer to `struct ebpf_module`
  1979. */
  1980. void ebpf_update_disabled_plugin_stats(ebpf_module_t *em)
  1981. {
  1982. pthread_mutex_lock(&lock);
  1983. ebpf_update_stats(&plugin_statistics, em);
  1984. pthread_mutex_unlock(&lock);
  1985. }
  1986. /**
  1987. * Print help on standard error for user knows how to use the collector.
  1988. */
  1989. void ebpf_print_help()
  1990. {
  1991. const time_t t = time(NULL);
  1992. struct tm ct;
  1993. struct tm *test = localtime_r(&t, &ct);
  1994. int year;
  1995. if (test)
  1996. year = ct.tm_year;
  1997. else
  1998. year = 0;
  1999. fprintf(stderr,
  2000. "\n"
  2001. " Netdata ebpf.plugin %s\n"
  2002. " Copyright (C) 2016-%d Costa Tsaousis <costa@tsaousis.gr>\n"
  2003. " Released under GNU General Public License v3 or later.\n"
  2004. " All rights reserved.\n"
  2005. "\n"
  2006. " This eBPF.plugin is a data collector plugin for netdata.\n"
  2007. "\n"
  2008. " This plugin only accepts long options with one or two dashes. The available command line options are:\n"
  2009. "\n"
  2010. " SECONDS Set the data collection frequency.\n"
  2011. "\n"
  2012. " [-]-help Show this help.\n"
  2013. "\n"
  2014. " [-]-version Show software version.\n"
  2015. "\n"
  2016. " [-]-global Disable charts per application and cgroup.\n"
  2017. "\n"
  2018. " [-]-all Enable all chart groups (global, apps, and cgroup), unless -g is also given.\n"
  2019. "\n"
  2020. " [-]-cachestat Enable charts related to process run time.\n"
  2021. "\n"
  2022. " [-]-dcstat Enable charts related to directory cache.\n"
  2023. "\n"
  2024. " [-]-disk Enable charts related to disk monitoring.\n"
  2025. "\n"
  2026. " [-]-filesystem Enable chart related to filesystem run time.\n"
  2027. "\n"
  2028. " [-]-hardirq Enable chart related to hard IRQ latency.\n"
  2029. "\n"
  2030. " [-]-mdflush Enable charts related to multi-device flush.\n"
  2031. "\n"
  2032. " [-]-mount Enable charts related to mount monitoring.\n"
  2033. "\n"
  2034. " [-]-net Enable network viewer charts.\n"
  2035. "\n"
  2036. " [-]-oomkill Enable chart related to OOM kill tracking.\n"
  2037. "\n"
  2038. " [-]-process Enable charts related to process run time.\n"
  2039. "\n"
  2040. " [-]-return Run the collector in return mode.\n"
  2041. "\n"
  2042. " [-]-shm Enable chart related to shared memory tracking.\n"
  2043. "\n"
  2044. " [-]-softirq Enable chart related to soft IRQ latency.\n"
  2045. "\n"
  2046. " [-]-sync Enable chart related to sync run time.\n"
  2047. "\n"
  2048. " [-]-swap Enable chart related to swap run time.\n"
  2049. "\n"
  2050. " [-]-vfs Enable chart related to vfs run time.\n"
  2051. "\n"
  2052. " [-]-legacy Load legacy eBPF programs.\n"
  2053. "\n"
  2054. " [-]-core Use CO-RE when available(Working in progress).\n"
  2055. "\n",
  2056. NETDATA_VERSION,
  2057. (year >= 116) ? year + 1900 : 2020);
  2058. }
  2059. /*****************************************************************
  2060. *
  2061. * TRACEPOINT MANAGEMENT FUNCTIONS
  2062. *
  2063. *****************************************************************/
  2064. /**
  2065. * Enable a tracepoint.
  2066. *
  2067. * @return 0 on success, -1 on error.
  2068. */
  2069. int ebpf_enable_tracepoint(ebpf_tracepoint_t *tp)
  2070. {
  2071. int test = ebpf_is_tracepoint_enabled(tp->class, tp->event);
  2072. // err?
  2073. if (test == -1) {
  2074. return -1;
  2075. }
  2076. // disabled?
  2077. else if (test == 0) {
  2078. // enable it then.
  2079. if (ebpf_enable_tracing_values(tp->class, tp->event)) {
  2080. return -1;
  2081. }
  2082. }
  2083. // enabled now or already was.
  2084. tp->enabled = true;
  2085. return 0;
  2086. }
  2087. /**
  2088. * Disable a tracepoint if it's enabled.
  2089. *
  2090. * @return 0 on success, -1 on error.
  2091. */
  2092. int ebpf_disable_tracepoint(ebpf_tracepoint_t *tp)
  2093. {
  2094. int test = ebpf_is_tracepoint_enabled(tp->class, tp->event);
  2095. // err?
  2096. if (test == -1) {
  2097. return -1;
  2098. }
  2099. // enabled?
  2100. else if (test == 1) {
  2101. // disable it then.
  2102. if (ebpf_disable_tracing_values(tp->class, tp->event)) {
  2103. return -1;
  2104. }
  2105. }
  2106. // disable now or already was.
  2107. tp->enabled = false;
  2108. return 0;
  2109. }
  2110. /**
  2111. * Enable multiple tracepoints on a list of tracepoints which end when the
  2112. * class is NULL.
  2113. *
  2114. * @return the number of successful enables.
  2115. */
  2116. uint32_t ebpf_enable_tracepoints(ebpf_tracepoint_t *tps)
  2117. {
  2118. uint32_t cnt = 0;
  2119. for (int i = 0; tps[i].class != NULL; i++) {
  2120. if (ebpf_enable_tracepoint(&tps[i]) == -1) {
  2121. netdata_log_error("Failed to enable tracepoint %s:%s", tps[i].class, tps[i].event);
  2122. }
  2123. else {
  2124. cnt += 1;
  2125. }
  2126. }
  2127. return cnt;
  2128. }
  2129. /*****************************************************************
  2130. *
  2131. * AUXILIARY FUNCTIONS USED DURING INITIALIZATION
  2132. *
  2133. *****************************************************************/
  2134. /**
  2135. * Is ip inside the range
  2136. *
  2137. * Check if the ip is inside a IP range
  2138. *
  2139. * @param rfirst the first ip address of the range
  2140. * @param rlast the last ip address of the range
  2141. * @param cmpfirst the first ip to compare
  2142. * @param cmplast the last ip to compare
  2143. * @param family the IP family
  2144. *
  2145. * @return It returns 1 if the IP is inside the range and 0 otherwise
  2146. */
  2147. static int ebpf_is_ip_inside_range(union netdata_ip_t *rfirst, union netdata_ip_t *rlast,
  2148. union netdata_ip_t *cmpfirst, union netdata_ip_t *cmplast, int family)
  2149. {
  2150. if (family == AF_INET) {
  2151. if ((rfirst->addr32[0] <= cmpfirst->addr32[0]) && (rlast->addr32[0] >= cmplast->addr32[0]))
  2152. return 1;
  2153. } else {
  2154. if (memcmp(rfirst->addr8, cmpfirst->addr8, sizeof(union netdata_ip_t)) <= 0 &&
  2155. memcmp(rlast->addr8, cmplast->addr8, sizeof(union netdata_ip_t)) >= 0) {
  2156. return 1;
  2157. }
  2158. }
  2159. return 0;
  2160. }
  2161. /**
  2162. * Fill IP list
  2163. *
  2164. * @param out a pointer to the link list.
  2165. * @param in the structure that will be linked.
  2166. * @param table the modified table.
  2167. */
  2168. void ebpf_fill_ip_list_unsafe(ebpf_network_viewer_ip_list_t **out, ebpf_network_viewer_ip_list_t *in,
  2169. char *table __maybe_unused)
  2170. {
  2171. if (in->ver == AF_INET) { // It is simpler to compare using host order
  2172. in->first.addr32[0] = ntohl(in->first.addr32[0]);
  2173. in->last.addr32[0] = ntohl(in->last.addr32[0]);
  2174. }
  2175. if (likely(*out)) {
  2176. ebpf_network_viewer_ip_list_t *move = *out, *store = *out;
  2177. while (move) {
  2178. if (in->ver == move->ver &&
  2179. ebpf_is_ip_inside_range(&move->first, &move->last, &in->first, &in->last, in->ver)) {
  2180. #ifdef NETDATA_DEV_MODE
  2181. netdata_log_info("The range/value (%s) is inside the range/value (%s) already inserted, it will be ignored.",
  2182. in->value, move->value);
  2183. #endif
  2184. freez(in->value);
  2185. freez(in);
  2186. return;
  2187. }
  2188. store = move;
  2189. move = move->next;
  2190. }
  2191. store->next = in;
  2192. } else {
  2193. *out = in;
  2194. }
  2195. #ifdef NETDATA_DEV_MODE
  2196. char first[256], last[512];
  2197. if (in->ver == AF_INET) {
  2198. netdata_log_info("Adding values %s: (%u - %u) to %s IP list \"%s\" used on network viewer",
  2199. in->value, in->first.addr32[0], in->last.addr32[0],
  2200. (*out == network_viewer_opt.included_ips)?"included":"excluded",
  2201. table);
  2202. } else {
  2203. if (inet_ntop(AF_INET6, in->first.addr8, first, INET6_ADDRSTRLEN) &&
  2204. inet_ntop(AF_INET6, in->last.addr8, last, INET6_ADDRSTRLEN))
  2205. netdata_log_info("Adding values %s - %s to %s IP list \"%s\" used on network viewer",
  2206. first, last,
  2207. (*out == network_viewer_opt.included_ips)?"included":"excluded",
  2208. table);
  2209. }
  2210. #endif
  2211. }
  2212. /**
  2213. * Link hostname
  2214. *
  2215. * @param out is the output link list
  2216. * @param in the hostname to add to list.
  2217. */
  2218. static void ebpf_link_hostname(ebpf_network_viewer_hostname_list_t **out, ebpf_network_viewer_hostname_list_t *in)
  2219. {
  2220. if (likely(*out)) {
  2221. ebpf_network_viewer_hostname_list_t *move = *out;
  2222. for (; move->next ; move = move->next ) {
  2223. if (move->hash == in->hash && !strcmp(move->value, in->value)) {
  2224. netdata_log_info("The hostname %s was already inserted, it will be ignored.", in->value);
  2225. freez(in->value);
  2226. simple_pattern_free(in->value_pattern);
  2227. freez(in);
  2228. return;
  2229. }
  2230. }
  2231. move->next = in;
  2232. } else {
  2233. *out = in;
  2234. }
  2235. #ifdef NETDATA_INTERNAL_CHECKS
  2236. netdata_log_info("Adding value %s to %s hostname list used on network viewer",
  2237. in->value,
  2238. (*out == network_viewer_opt.included_hostnames)?"included":"excluded");
  2239. #endif
  2240. }
  2241. /**
  2242. * Link Hostnames
  2243. *
  2244. * Parse the list of hostnames to create the link list.
  2245. * This is not associated with the IP, because simple patterns like *example* cannot be resolved to IP.
  2246. *
  2247. * @param out is the output link list
  2248. * @param parse is a pointer with the text to parser.
  2249. */
  2250. static void ebpf_link_hostnames(const char *parse)
  2251. {
  2252. // No value
  2253. if (unlikely(!parse))
  2254. return;
  2255. while (likely(parse)) {
  2256. // Find the first valid value
  2257. while (isspace(*parse)) parse++;
  2258. // No valid value found
  2259. if (unlikely(!*parse))
  2260. return;
  2261. // Find space that ends the list
  2262. char *end = strchr(parse, ' ');
  2263. if (end) {
  2264. *end++ = '\0';
  2265. }
  2266. int neg = 0;
  2267. if (*parse == '!') {
  2268. neg++;
  2269. parse++;
  2270. }
  2271. ebpf_network_viewer_hostname_list_t *hostname = callocz(1 , sizeof(ebpf_network_viewer_hostname_list_t));
  2272. hostname->value = strdupz(parse);
  2273. hostname->hash = simple_hash(parse);
  2274. hostname->value_pattern = simple_pattern_create(parse, NULL, SIMPLE_PATTERN_EXACT, true);
  2275. ebpf_link_hostname((!neg) ? &network_viewer_opt.included_hostnames :
  2276. &network_viewer_opt.excluded_hostnames,
  2277. hostname);
  2278. parse = end;
  2279. }
  2280. }
  2281. /**
  2282. * Parse network viewer section
  2283. *
  2284. * @param cfg the configuration structure
  2285. */
  2286. void parse_network_viewer_section(struct config *cfg)
  2287. {
  2288. network_viewer_opt.hostname_resolution_enabled = appconfig_get_boolean(cfg,
  2289. EBPF_NETWORK_VIEWER_SECTION,
  2290. EBPF_CONFIG_RESOLVE_HOSTNAME,
  2291. CONFIG_BOOLEAN_NO);
  2292. network_viewer_opt.service_resolution_enabled = appconfig_get_boolean(cfg,
  2293. EBPF_NETWORK_VIEWER_SECTION,
  2294. EBPF_CONFIG_RESOLVE_SERVICE,
  2295. CONFIG_BOOLEAN_YES);
  2296. const char *value = appconfig_get(cfg, EBPF_NETWORK_VIEWER_SECTION, EBPF_CONFIG_PORTS, NULL);
  2297. ebpf_parse_ports(value);
  2298. if (network_viewer_opt.hostname_resolution_enabled) {
  2299. value = appconfig_get(cfg, EBPF_NETWORK_VIEWER_SECTION, EBPF_CONFIG_HOSTNAMES, NULL);
  2300. ebpf_link_hostnames(value);
  2301. } else {
  2302. netdata_log_info("Name resolution is disabled, collector will not parse \"hostnames\" list.");
  2303. }
  2304. value = appconfig_get(cfg,
  2305. EBPF_NETWORK_VIEWER_SECTION,
  2306. "ips",
  2307. NULL);
  2308. //"ips", "!127.0.0.1/8 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 fc00::/7 !::1/128");
  2309. ebpf_parse_ips_unsafe(value);
  2310. }
  2311. /**
  2312. * Read Local Ports
  2313. *
  2314. * Parse /proc/net/{tcp,udp} and get the ports Linux is listening.
  2315. *
  2316. * @param filename the proc file to parse.
  2317. * @param proto is the magic number associated to the protocol file we are reading.
  2318. */
  2319. static void read_local_ports(char *filename, uint8_t proto)
  2320. {
  2321. procfile *ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  2322. if (!ff)
  2323. return;
  2324. ff = procfile_readall(ff);
  2325. if (!ff)
  2326. return;
  2327. size_t lines = procfile_lines(ff), l;
  2328. netdata_passive_connection_t values = {.counter = 0, .tgid = 0, .pid = 0};
  2329. for(l = 0; l < lines ;l++) {
  2330. size_t words = procfile_linewords(ff, l);
  2331. // This is header or end of file
  2332. if (unlikely(words < 14))
  2333. continue;
  2334. // https://elixir.bootlin.com/linux/v5.7.8/source/include/net/tcp_states.h
  2335. // 0A = TCP_LISTEN
  2336. if (strcmp("0A", procfile_lineword(ff, l, 5)))
  2337. continue;
  2338. // Read local port
  2339. uint16_t port = (uint16_t)strtol(procfile_lineword(ff, l, 2), NULL, 16);
  2340. update_listen_table(htons(port), proto, &values);
  2341. }
  2342. procfile_close(ff);
  2343. }
  2344. /**
  2345. * Read Local addresseses
  2346. *
  2347. * Read the local address from the interfaces.
  2348. */
  2349. void ebpf_read_local_addresses_unsafe()
  2350. {
  2351. struct ifaddrs *ifaddr, *ifa;
  2352. if (getifaddrs(&ifaddr) == -1) {
  2353. netdata_log_error("Cannot get the local IP addresses, it is no possible to do separation between inbound and outbound connections");
  2354. return;
  2355. }
  2356. char *notext = { "No text representation" };
  2357. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  2358. if (ifa->ifa_addr == NULL)
  2359. continue;
  2360. if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6))
  2361. continue;
  2362. ebpf_network_viewer_ip_list_t *w = callocz(1, sizeof(ebpf_network_viewer_ip_list_t));
  2363. int family = ifa->ifa_addr->sa_family;
  2364. w->ver = (uint8_t) family;
  2365. char text[INET6_ADDRSTRLEN];
  2366. if (family == AF_INET) {
  2367. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  2368. w->first.addr32[0] = in->sin_addr.s_addr;
  2369. w->last.addr32[0] = in->sin_addr.s_addr;
  2370. if (inet_ntop(AF_INET, w->first.addr8, text, INET_ADDRSTRLEN)) {
  2371. w->value = strdupz(text);
  2372. w->hash = simple_hash(text);
  2373. } else {
  2374. w->value = strdupz(notext);
  2375. w->hash = simple_hash(notext);
  2376. }
  2377. } else {
  2378. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  2379. memcpy(w->first.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  2380. memcpy(w->last.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  2381. if (inet_ntop(AF_INET6, w->first.addr8, text, INET_ADDRSTRLEN)) {
  2382. w->value = strdupz(text);
  2383. w->hash = simple_hash(text);
  2384. } else {
  2385. w->value = strdupz(notext);
  2386. w->hash = simple_hash(notext);
  2387. }
  2388. }
  2389. ebpf_fill_ip_list_unsafe(
  2390. (family == AF_INET) ? &network_viewer_opt.ipv4_local_ip : &network_viewer_opt.ipv6_local_ip, w, "selector");
  2391. }
  2392. freeifaddrs(ifaddr);
  2393. }
  2394. /**
  2395. * Start Pthread Variable
  2396. *
  2397. * This function starts all pthread variables.
  2398. */
  2399. void ebpf_start_pthread_variables()
  2400. {
  2401. pthread_mutex_init(&lock, NULL);
  2402. pthread_mutex_init(&ebpf_exit_cleanup, NULL);
  2403. pthread_mutex_init(&collect_data_mutex, NULL);
  2404. pthread_mutex_init(&mutex_cgroup_shm, NULL);
  2405. rw_spinlock_init(&ebpf_judy_pid.index.rw_spinlock);
  2406. }
  2407. /**
  2408. * Allocate the vectors used for all threads.
  2409. */
  2410. static void ebpf_allocate_common_vectors()
  2411. {
  2412. ebpf_judy_pid.pid_table = ebpf_allocate_pid_aral(NETDATA_EBPF_PID_SOCKET_ARAL_TABLE_NAME,
  2413. sizeof(netdata_ebpf_judy_pid_stats_t));
  2414. ebpf_pids = callocz((size_t)pid_max, sizeof(ebpf_pid_data_t));
  2415. ebpf_aral_init();
  2416. }
  2417. /**
  2418. * Define how to load the ebpf programs
  2419. *
  2420. * @param ptr the option given by users
  2421. */
  2422. static inline void ebpf_how_to_load(const char *ptr)
  2423. {
  2424. if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_RETURN))
  2425. ebpf_set_thread_mode(MODE_RETURN);
  2426. else if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_DEFAULT))
  2427. ebpf_set_thread_mode(MODE_ENTRY);
  2428. else
  2429. netdata_log_error("the option %s for \"ebpf load mode\" is not a valid option.", ptr);
  2430. }
  2431. /**
  2432. * Define whether we should have charts for apps
  2433. *
  2434. * @param lmode the mode that will be used for them.
  2435. */
  2436. static inline void ebpf_set_apps_mode(netdata_apps_integration_flags_t value)
  2437. {
  2438. int i;
  2439. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  2440. ebpf_modules[i].apps_charts = value;
  2441. }
  2442. }
  2443. /**
  2444. * Update interval
  2445. *
  2446. * Update default interval with value from user
  2447. *
  2448. * @param update_every value to overwrite the update frequency set by the server.
  2449. */
  2450. static void ebpf_update_interval(int update_every)
  2451. {
  2452. int i;
  2453. int value = (int) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_UPDATE_EVERY,
  2454. update_every);
  2455. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  2456. ebpf_modules[i].update_every = value;
  2457. }
  2458. }
  2459. /**
  2460. * Update PID table size
  2461. *
  2462. * Update default size with value from user
  2463. */
  2464. static void ebpf_update_table_size()
  2465. {
  2466. int i;
  2467. uint32_t value = (uint32_t) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION,
  2468. EBPF_CFG_PID_SIZE, ND_EBPF_DEFAULT_PID_SIZE);
  2469. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  2470. ebpf_modules[i].pid_map_size = value;
  2471. }
  2472. }
  2473. /**
  2474. * Update lifetime
  2475. *
  2476. * Update the period of time that specific thread will run
  2477. */
  2478. static void ebpf_update_lifetime()
  2479. {
  2480. int i;
  2481. uint32_t value = (uint32_t) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION,
  2482. EBPF_CFG_LIFETIME, EBPF_DEFAULT_LIFETIME);
  2483. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  2484. ebpf_modules[i].lifetime = value;
  2485. }
  2486. }
  2487. /**
  2488. * Set Load mode
  2489. *
  2490. * @param origin specify the configuration file loaded
  2491. */
  2492. static inline void ebpf_set_load_mode(netdata_ebpf_load_mode_t load, netdata_ebpf_load_mode_t origin)
  2493. {
  2494. int i;
  2495. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  2496. ebpf_modules[i].load &= ~NETDATA_EBPF_LOAD_METHODS;
  2497. ebpf_modules[i].load |= load | origin ;
  2498. }
  2499. }
  2500. /**
  2501. * Update mode
  2502. *
  2503. * @param str value read from configuration file.
  2504. * @param origin specify the configuration file loaded
  2505. */
  2506. static inline void epbf_update_load_mode(const char *str, netdata_ebpf_load_mode_t origin)
  2507. {
  2508. netdata_ebpf_load_mode_t load = epbf_convert_string_to_load_mode(str);
  2509. ebpf_set_load_mode(load, origin);
  2510. }
  2511. /**
  2512. * Update Map per core
  2513. *
  2514. * Define the map type used with some hash tables.
  2515. */
  2516. static void ebpf_update_map_per_core()
  2517. {
  2518. int i;
  2519. int value = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION,
  2520. EBPF_CFG_MAPS_PER_CORE, CONFIG_BOOLEAN_YES);
  2521. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  2522. ebpf_modules[i].maps_per_core = value;
  2523. }
  2524. }
  2525. /**
  2526. * Read collector values
  2527. *
  2528. * @param disable_cgroups variable to store information related to cgroups.
  2529. * @param update_every value to overwrite the update frequency set by the server.
  2530. * @param origin specify the configuration file loaded
  2531. */
  2532. static void read_collector_values(int *disable_cgroups,
  2533. int update_every, netdata_ebpf_load_mode_t origin)
  2534. {
  2535. // Read global section
  2536. const char *value;
  2537. if (appconfig_exists(&collector_config, EBPF_GLOBAL_SECTION, "load")) // Backward compatibility
  2538. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, "load",
  2539. EBPF_CFG_LOAD_MODE_DEFAULT);
  2540. else
  2541. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE,
  2542. EBPF_CFG_LOAD_MODE_DEFAULT);
  2543. ebpf_how_to_load(value);
  2544. btf_path = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_PROGRAM_PATH,
  2545. EBPF_DEFAULT_BTF_PATH);
  2546. #ifdef LIBBPF_MAJOR_VERSION
  2547. default_btf = ebpf_load_btf_file(btf_path, EBPF_DEFAULT_BTF_FILE);
  2548. #endif
  2549. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_TYPE_FORMAT, EBPF_CFG_DEFAULT_PROGRAM);
  2550. epbf_update_load_mode(value, origin);
  2551. ebpf_update_interval(update_every);
  2552. ebpf_update_table_size();
  2553. ebpf_update_lifetime();
  2554. // This is kept to keep compatibility
  2555. uint32_t enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, "disable apps",
  2556. CONFIG_BOOLEAN_NO);
  2557. if (!enabled) {
  2558. // Apps is a positive sentence, so we need to invert the values to disable apps.
  2559. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
  2560. CONFIG_BOOLEAN_YES);
  2561. enabled = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  2562. }
  2563. ebpf_set_apps_mode(!enabled);
  2564. // Cgroup is a positive sentence, so we need to invert the values to disable apps.
  2565. // We are using the same pattern for cgroup and apps
  2566. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_CGROUP, CONFIG_BOOLEAN_NO);
  2567. *disable_cgroups = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  2568. ebpf_update_map_per_core();
  2569. // Read ebpf programs section
  2570. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  2571. ebpf_modules[EBPF_MODULE_PROCESS_IDX].info.config_name, CONFIG_BOOLEAN_YES);
  2572. if (enabled) {
  2573. ebpf_enable_chart(EBPF_MODULE_PROCESS_IDX, *disable_cgroups);
  2574. }
  2575. // This is kept to keep compatibility
  2576. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network viewer",
  2577. CONFIG_BOOLEAN_NO);
  2578. if (!enabled)
  2579. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  2580. ebpf_modules[EBPF_MODULE_SOCKET_IDX].info.config_name,
  2581. CONFIG_BOOLEAN_NO);
  2582. if (enabled) {
  2583. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, *disable_cgroups);
  2584. }
  2585. // This is kept to keep compatibility
  2586. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connection monitoring",
  2587. CONFIG_BOOLEAN_YES);
  2588. if (!enabled)
  2589. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connections",
  2590. CONFIG_BOOLEAN_YES);
  2591. network_viewer_opt.enabled = enabled;
  2592. if (enabled) {
  2593. if (!ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled)
  2594. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, *disable_cgroups);
  2595. // Read network viewer section if network viewer is enabled
  2596. // This is kept here to keep backward compatibility
  2597. parse_network_viewer_section(&collector_config);
  2598. ebpf_parse_service_name_section(&collector_config);
  2599. }
  2600. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "cachestat",
  2601. CONFIG_BOOLEAN_NO);
  2602. if (enabled) {
  2603. ebpf_enable_chart(EBPF_MODULE_CACHESTAT_IDX, *disable_cgroups);
  2604. }
  2605. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "sync",
  2606. CONFIG_BOOLEAN_YES);
  2607. if (enabled) {
  2608. ebpf_enable_chart(EBPF_MODULE_SYNC_IDX, *disable_cgroups);
  2609. }
  2610. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "dcstat",
  2611. CONFIG_BOOLEAN_NO);
  2612. if (enabled) {
  2613. ebpf_enable_chart(EBPF_MODULE_DCSTAT_IDX, *disable_cgroups);
  2614. }
  2615. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "swap",
  2616. CONFIG_BOOLEAN_NO);
  2617. if (enabled) {
  2618. ebpf_enable_chart(EBPF_MODULE_SWAP_IDX, *disable_cgroups);
  2619. }
  2620. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "vfs",
  2621. CONFIG_BOOLEAN_NO);
  2622. if (enabled) {
  2623. ebpf_enable_chart(EBPF_MODULE_VFS_IDX, *disable_cgroups);
  2624. }
  2625. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "filesystem",
  2626. CONFIG_BOOLEAN_NO);
  2627. if (enabled) {
  2628. ebpf_enable_chart(EBPF_MODULE_FILESYSTEM_IDX, *disable_cgroups);
  2629. }
  2630. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "disk",
  2631. CONFIG_BOOLEAN_NO);
  2632. if (enabled) {
  2633. ebpf_enable_chart(EBPF_MODULE_DISK_IDX, *disable_cgroups);
  2634. }
  2635. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "mount",
  2636. CONFIG_BOOLEAN_YES);
  2637. if (enabled) {
  2638. ebpf_enable_chart(EBPF_MODULE_MOUNT_IDX, *disable_cgroups);
  2639. }
  2640. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "fd",
  2641. CONFIG_BOOLEAN_YES);
  2642. if (enabled) {
  2643. ebpf_enable_chart(EBPF_MODULE_FD_IDX, *disable_cgroups);
  2644. }
  2645. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "hardirq",
  2646. CONFIG_BOOLEAN_YES);
  2647. if (enabled) {
  2648. ebpf_enable_chart(EBPF_MODULE_HARDIRQ_IDX, *disable_cgroups);
  2649. }
  2650. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "softirq",
  2651. CONFIG_BOOLEAN_YES);
  2652. if (enabled) {
  2653. ebpf_enable_chart(EBPF_MODULE_SOFTIRQ_IDX, *disable_cgroups);
  2654. }
  2655. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "oomkill",
  2656. CONFIG_BOOLEAN_YES);
  2657. if (enabled) {
  2658. ebpf_enable_chart(EBPF_MODULE_OOMKILL_IDX, *disable_cgroups);
  2659. }
  2660. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "shm",
  2661. CONFIG_BOOLEAN_YES);
  2662. if (enabled) {
  2663. ebpf_enable_chart(EBPF_MODULE_SHM_IDX, *disable_cgroups);
  2664. }
  2665. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "mdflush",
  2666. CONFIG_BOOLEAN_NO);
  2667. if (enabled) {
  2668. ebpf_enable_chart(EBPF_MODULE_MDFLUSH_IDX, *disable_cgroups);
  2669. }
  2670. }
  2671. /**
  2672. * Load collector config
  2673. *
  2674. * @param path the path where the file ebpf.conf is stored.
  2675. * @param disable_cgroups variable to store the information about cgroups plugin status.
  2676. * @param update_every value to overwrite the update frequency set by the server.
  2677. *
  2678. * @return 0 on success and -1 otherwise.
  2679. */
  2680. static int ebpf_load_collector_config(char *path, int *disable_cgroups, int update_every)
  2681. {
  2682. char lpath[4096];
  2683. netdata_ebpf_load_mode_t origin;
  2684. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_CONFIG_FILE);
  2685. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  2686. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_OLD_CONFIG_FILE);
  2687. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  2688. return -1;
  2689. }
  2690. origin = EBPF_LOADED_FROM_STOCK;
  2691. } else
  2692. origin = EBPF_LOADED_FROM_USER;
  2693. read_collector_values(disable_cgroups, update_every, origin);
  2694. return 0;
  2695. }
  2696. /**
  2697. * Set global variables reading environment variables
  2698. */
  2699. static void ebpf_set_global_variables()
  2700. {
  2701. // Get environment variables
  2702. ebpf_plugin_dir = getenv("NETDATA_PLUGINS_DIR");
  2703. if (!ebpf_plugin_dir)
  2704. ebpf_plugin_dir = PLUGINS_DIR;
  2705. ebpf_user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  2706. if (!ebpf_user_config_dir)
  2707. ebpf_user_config_dir = CONFIG_DIR;
  2708. ebpf_stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  2709. if (!ebpf_stock_config_dir)
  2710. ebpf_stock_config_dir = LIBCONFIG_DIR;
  2711. ebpf_configured_log_dir = getenv("NETDATA_LOG_DIR");
  2712. if (!ebpf_configured_log_dir)
  2713. ebpf_configured_log_dir = LOG_DIR;
  2714. ebpf_nprocs = (int)sysconf(_SC_NPROCESSORS_ONLN);
  2715. if (ebpf_nprocs < 0) {
  2716. ebpf_nprocs = NETDATA_MAX_PROCESSOR;
  2717. netdata_log_error("Cannot identify number of process, using default value %d", ebpf_nprocs);
  2718. }
  2719. isrh = get_redhat_release();
  2720. pid_max = os_get_system_pid_max();
  2721. running_on_kernel = ebpf_get_kernel_version();
  2722. memset(pids_fd, -1, sizeof(pids_fd));
  2723. }
  2724. /**
  2725. * Load collector config
  2726. */
  2727. static inline void ebpf_load_thread_config()
  2728. {
  2729. int i;
  2730. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  2731. ebpf_update_module(&ebpf_modules[i], default_btf, running_on_kernel, isrh);
  2732. }
  2733. }
  2734. /**
  2735. * Parse arguments given from user.
  2736. *
  2737. * @param argc the number of arguments
  2738. * @param argv the pointer to the arguments
  2739. */
  2740. static void ebpf_parse_args(int argc, char **argv)
  2741. {
  2742. int disable_cgroups = 1;
  2743. int freq = 0;
  2744. int option_index = 0;
  2745. uint64_t select_threads = 0;
  2746. static struct option long_options[] = {
  2747. {"process", no_argument, 0, 0 },
  2748. {"net", no_argument, 0, 0 },
  2749. {"cachestat", no_argument, 0, 0 },
  2750. {"sync", no_argument, 0, 0 },
  2751. {"dcstat", no_argument, 0, 0 },
  2752. {"swap", no_argument, 0, 0 },
  2753. {"vfs", no_argument, 0, 0 },
  2754. {"filesystem", no_argument, 0, 0 },
  2755. {"disk", no_argument, 0, 0 },
  2756. {"mount", no_argument, 0, 0 },
  2757. {"filedescriptor", no_argument, 0, 0 },
  2758. {"hardirq", no_argument, 0, 0 },
  2759. {"softirq", no_argument, 0, 0 },
  2760. {"oomkill", no_argument, 0, 0 },
  2761. {"shm", no_argument, 0, 0 },
  2762. {"mdflush", no_argument, 0, 0 },
  2763. /* INSERT NEW THREADS BEFORE THIS COMMENT TO KEEP COMPATIBILITY WITH enum ebpf_module_indexes */
  2764. {"all", no_argument, 0, 0 },
  2765. {"version", no_argument, 0, 0 },
  2766. {"help", no_argument, 0, 0 },
  2767. {"global", no_argument, 0, 0 },
  2768. {"return", no_argument, 0, 0 },
  2769. {"legacy", no_argument, 0, 0 },
  2770. {"core", no_argument, 0, 0 },
  2771. {"unittest", no_argument, 0, 0 },
  2772. {0, 0, 0, 0}
  2773. };
  2774. memset(&network_viewer_opt, 0, sizeof(network_viewer_opt));
  2775. rw_spinlock_init(&network_viewer_opt.rw_spinlock);
  2776. if (argc > 1) {
  2777. int n = (int)str2l(argv[1]);
  2778. if (n > 0) {
  2779. freq = n;
  2780. }
  2781. }
  2782. if (!freq)
  2783. freq = EBPF_DEFAULT_UPDATE_EVERY;
  2784. //rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  2785. if (ebpf_load_collector_config(ebpf_user_config_dir, &disable_cgroups, freq)) {
  2786. netdata_log_info(
  2787. "Does not have a configuration file inside `%s/ebpf.d.conf. It will try to load stock file.",
  2788. ebpf_user_config_dir);
  2789. if (ebpf_load_collector_config(ebpf_stock_config_dir, &disable_cgroups, freq)) {
  2790. netdata_log_info("Does not have a stock file. It is starting with default options.");
  2791. }
  2792. }
  2793. ebpf_load_thread_config();
  2794. //rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  2795. while (1) {
  2796. int c = getopt_long_only(argc, argv, "", long_options, &option_index);
  2797. if (c == -1)
  2798. break;
  2799. switch (option_index) {
  2800. case EBPF_MODULE_PROCESS_IDX: {
  2801. select_threads |= 1<<EBPF_MODULE_PROCESS_IDX;
  2802. #ifdef NETDATA_INTERNAL_CHECKS
  2803. netdata_log_info("EBPF enabling \"PROCESS\" charts, because it was started with the option \"[-]-process\".");
  2804. #endif
  2805. break;
  2806. }
  2807. case EBPF_MODULE_SOCKET_IDX: {
  2808. select_threads |= 1<<EBPF_MODULE_SOCKET_IDX;
  2809. #ifdef NETDATA_INTERNAL_CHECKS
  2810. netdata_log_info("EBPF enabling \"NET\" charts, because it was started with the option \"[-]-net\".");
  2811. #endif
  2812. break;
  2813. }
  2814. case EBPF_MODULE_CACHESTAT_IDX: {
  2815. select_threads |= 1<<EBPF_MODULE_CACHESTAT_IDX;
  2816. #ifdef NETDATA_INTERNAL_CHECKS
  2817. netdata_log_info("EBPF enabling \"CACHESTAT\" charts, because it was started with the option \"[-]-cachestat\".");
  2818. #endif
  2819. break;
  2820. }
  2821. case EBPF_MODULE_SYNC_IDX: {
  2822. select_threads |= 1<<EBPF_MODULE_SYNC_IDX;
  2823. #ifdef NETDATA_INTERNAL_CHECKS
  2824. netdata_log_info("EBPF enabling \"SYNC\" chart, because it was started with the option \"[-]-sync\".");
  2825. #endif
  2826. break;
  2827. }
  2828. case EBPF_MODULE_DCSTAT_IDX: {
  2829. select_threads |= 1<<EBPF_MODULE_DCSTAT_IDX;
  2830. #ifdef NETDATA_INTERNAL_CHECKS
  2831. netdata_log_info("EBPF enabling \"DCSTAT\" charts, because it was started with the option \"[-]-dcstat\".");
  2832. #endif
  2833. break;
  2834. }
  2835. case EBPF_MODULE_SWAP_IDX: {
  2836. select_threads |= 1<<EBPF_MODULE_SWAP_IDX;
  2837. #ifdef NETDATA_INTERNAL_CHECKS
  2838. netdata_log_info("EBPF enabling \"SWAP\" chart, because it was started with the option \"[-]-swap\".");
  2839. #endif
  2840. break;
  2841. }
  2842. case EBPF_MODULE_VFS_IDX: {
  2843. select_threads |= 1<<EBPF_MODULE_VFS_IDX;
  2844. #ifdef NETDATA_INTERNAL_CHECKS
  2845. netdata_log_info("EBPF enabling \"VFS\" chart, because it was started with the option \"[-]-vfs\".");
  2846. #endif
  2847. break;
  2848. }
  2849. case EBPF_MODULE_FILESYSTEM_IDX: {
  2850. select_threads |= 1<<EBPF_MODULE_FILESYSTEM_IDX;
  2851. #ifdef NETDATA_INTERNAL_CHECKS
  2852. netdata_log_info("EBPF enabling \"FILESYSTEM\" chart, because it was started with the option \"[-]-filesystem\".");
  2853. #endif
  2854. break;
  2855. }
  2856. case EBPF_MODULE_DISK_IDX: {
  2857. select_threads |= 1<<EBPF_MODULE_DISK_IDX;
  2858. #ifdef NETDATA_INTERNAL_CHECKS
  2859. netdata_log_info("EBPF enabling \"DISK\" chart, because it was started with the option \"[-]-disk\".");
  2860. #endif
  2861. break;
  2862. }
  2863. case EBPF_MODULE_MOUNT_IDX: {
  2864. select_threads |= 1<<EBPF_MODULE_MOUNT_IDX;
  2865. #ifdef NETDATA_INTERNAL_CHECKS
  2866. netdata_log_info("EBPF enabling \"MOUNT\" chart, because it was started with the option \"[-]-mount\".");
  2867. #endif
  2868. break;
  2869. }
  2870. case EBPF_MODULE_FD_IDX: {
  2871. select_threads |= 1<<EBPF_MODULE_FD_IDX;
  2872. #ifdef NETDATA_INTERNAL_CHECKS
  2873. netdata_log_info("EBPF enabling \"FILEDESCRIPTOR\" chart, because it was started with the option \"[-]-filedescriptor\".");
  2874. #endif
  2875. break;
  2876. }
  2877. case EBPF_MODULE_HARDIRQ_IDX: {
  2878. select_threads |= 1<<EBPF_MODULE_HARDIRQ_IDX;
  2879. #ifdef NETDATA_INTERNAL_CHECKS
  2880. netdata_log_info("EBPF enabling \"HARDIRQ\" chart, because it was started with the option \"[-]-hardirq\".");
  2881. #endif
  2882. break;
  2883. }
  2884. case EBPF_MODULE_SOFTIRQ_IDX: {
  2885. select_threads |= 1<<EBPF_MODULE_SOFTIRQ_IDX;
  2886. #ifdef NETDATA_INTERNAL_CHECKS
  2887. netdata_log_info("EBPF enabling \"SOFTIRQ\" chart, because it was started with the option \"[-]-softirq\".");
  2888. #endif
  2889. break;
  2890. }
  2891. case EBPF_MODULE_OOMKILL_IDX: {
  2892. select_threads |= 1<<EBPF_MODULE_OOMKILL_IDX;
  2893. #ifdef NETDATA_INTERNAL_CHECKS
  2894. netdata_log_info("EBPF enabling \"OOMKILL\" chart, because it was started with the option \"[-]-oomkill\".");
  2895. #endif
  2896. break;
  2897. }
  2898. case EBPF_MODULE_SHM_IDX: {
  2899. select_threads |= 1<<EBPF_MODULE_SHM_IDX;
  2900. #ifdef NETDATA_INTERNAL_CHECKS
  2901. netdata_log_info("EBPF enabling \"SHM\" chart, because it was started with the option \"[-]-shm\".");
  2902. #endif
  2903. break;
  2904. }
  2905. case EBPF_MODULE_MDFLUSH_IDX: {
  2906. select_threads |= 1<<EBPF_MODULE_MDFLUSH_IDX;
  2907. #ifdef NETDATA_INTERNAL_CHECKS
  2908. netdata_log_info("EBPF enabling \"MDFLUSH\" chart, because it was started with the option \"[-]-mdflush\".");
  2909. #endif
  2910. break;
  2911. }
  2912. case EBPF_OPTION_ALL_CHARTS: {
  2913. ebpf_set_apps_mode(NETDATA_EBPF_APPS_FLAG_YES);
  2914. disable_cgroups = 0;
  2915. #ifdef NETDATA_INTERNAL_CHECKS
  2916. netdata_log_info("EBPF running with all chart groups, because it was started with the option \"[-]-all\".");
  2917. #endif
  2918. break;
  2919. }
  2920. case EBPF_OPTION_VERSION: {
  2921. printf("ebpf.plugin %s\n", NETDATA_VERSION);
  2922. exit(0);
  2923. }
  2924. case EBPF_OPTION_HELP: {
  2925. ebpf_print_help();
  2926. exit(0);
  2927. }
  2928. case EBPF_OPTION_GLOBAL_CHART: {
  2929. disable_cgroups = 1;
  2930. #ifdef NETDATA_INTERNAL_CHECKS
  2931. netdata_log_info("EBPF running with global chart group, because it was started with the option \"[-]-global\".");
  2932. #endif
  2933. break;
  2934. }
  2935. case EBPF_OPTION_RETURN_MODE: {
  2936. ebpf_set_thread_mode(MODE_RETURN);
  2937. #ifdef NETDATA_INTERNAL_CHECKS
  2938. netdata_log_info("EBPF running in \"RETURN\" mode, because it was started with the option \"[-]-return\".");
  2939. #endif
  2940. break;
  2941. }
  2942. case EBPF_OPTION_LEGACY: {
  2943. ebpf_set_load_mode(EBPF_LOAD_LEGACY, EBPF_LOADED_FROM_USER);
  2944. #ifdef NETDATA_INTERNAL_CHECKS
  2945. netdata_log_info("EBPF running with \"LEGACY\" code, because it was started with the option \"[-]-legacy\".");
  2946. #endif
  2947. break;
  2948. }
  2949. case EBPF_OPTION_CORE: {
  2950. ebpf_set_load_mode(EBPF_LOAD_CORE, EBPF_LOADED_FROM_USER);
  2951. #ifdef NETDATA_INTERNAL_CHECKS
  2952. netdata_log_info("EBPF running with \"CO-RE\" code, because it was started with the option \"[-]-core\".");
  2953. #endif
  2954. break;
  2955. }
  2956. case EBPF_OPTION_UNITTEST: {
  2957. // if we cannot run until the end, we will cancel the unittest
  2958. int exit_code = ECANCELED;
  2959. if (ebpf_can_plugin_load_code(running_on_kernel, NETDATA_EBPF_PLUGIN_NAME))
  2960. goto unittest;
  2961. if (ebpf_adjust_memory_limit())
  2962. goto unittest;
  2963. // Load binary in entry mode
  2964. ebpf_ut_initialize_structure(MODE_ENTRY);
  2965. if (ebpf_ut_load_real_binary())
  2966. goto unittest;
  2967. ebpf_ut_cleanup_memory();
  2968. // Do not load a binary in entry mode
  2969. ebpf_ut_initialize_structure(MODE_ENTRY);
  2970. if (ebpf_ut_load_fake_binary())
  2971. goto unittest;
  2972. ebpf_ut_cleanup_memory();
  2973. exit_code = 0;
  2974. unittest:
  2975. exit(exit_code);
  2976. }
  2977. default: {
  2978. break;
  2979. }
  2980. }
  2981. }
  2982. if (disable_cgroups) {
  2983. ebpf_disable_cgroups();
  2984. }
  2985. if (select_threads) {
  2986. disable_all_global_charts();
  2987. uint64_t idx;
  2988. for (idx = 0; idx < EBPF_OPTION_ALL_CHARTS; idx++) {
  2989. if (select_threads & 1<<idx)
  2990. ebpf_enable_specific_chart(&ebpf_modules[idx], disable_cgroups);
  2991. }
  2992. }
  2993. // Load apps_groups.conf
  2994. if (ebpf_read_apps_groups_conf(
  2995. &apps_groups_default_target, &apps_groups_root_target, ebpf_user_config_dir, "groups")) {
  2996. netdata_log_info("Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'",
  2997. ebpf_user_config_dir, ebpf_stock_config_dir);
  2998. if (ebpf_read_apps_groups_conf(
  2999. &apps_groups_default_target, &apps_groups_root_target, ebpf_stock_config_dir, "groups")) {
  3000. netdata_log_error("Cannot read process groups '%s/apps_groups.conf'. There are no internal defaults. Failing.",
  3001. ebpf_stock_config_dir);
  3002. ebpf_exit();
  3003. }
  3004. } else
  3005. netdata_log_info("Loaded config file '%s/apps_groups.conf'", ebpf_user_config_dir);
  3006. }
  3007. /*****************************************************************
  3008. *
  3009. * Collector charts
  3010. *
  3011. *****************************************************************/
  3012. static char *load_event_stat[NETDATA_EBPF_LOAD_STAT_END] = {"legacy", "co-re"};
  3013. static char *memlock_stat = {"memory_locked"};
  3014. static char *hash_table_stat = {"hash_table"};
  3015. static char *hash_table_core[NETDATA_EBPF_LOAD_STAT_END] = {"per_core", "unique"};
  3016. /**
  3017. * Send Hash Table PID data
  3018. *
  3019. * Send all information associated with a specific pid table.
  3020. *
  3021. * @param chart chart id
  3022. * @param idx index position in hash_table_stats
  3023. */
  3024. static inline void ebpf_send_hash_table_pid_data(char *chart, uint32_t idx)
  3025. {
  3026. int i;
  3027. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, chart, "");
  3028. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  3029. ebpf_module_t *wem = &ebpf_modules[i];
  3030. if (wem->functions.apps_routine)
  3031. write_chart_dimension((char *)wem->info.thread_name,
  3032. (wem->enabled < NETDATA_THREAD_EBPF_STOPPING) ?
  3033. wem->hash_table_stats[idx]:
  3034. 0);
  3035. }
  3036. ebpf_write_end_chart();
  3037. }
  3038. /**
  3039. * Send Global Hash Table data
  3040. *
  3041. * Send all information associated with a specific pid table.
  3042. *
  3043. */
  3044. static inline void ebpf_send_global_hash_table_data()
  3045. {
  3046. int i;
  3047. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, NETDATA_EBPF_HASH_TABLES_GLOBAL_ELEMENTS, "");
  3048. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  3049. ebpf_module_t *wem = &ebpf_modules[i];
  3050. write_chart_dimension((char *)wem->info.thread_name,
  3051. (wem->enabled < NETDATA_THREAD_EBPF_STOPPING) ? NETDATA_CONTROLLER_END: 0);
  3052. }
  3053. ebpf_write_end_chart();
  3054. }
  3055. /**
  3056. * Send Statistic Data
  3057. *
  3058. * Send statistic information to netdata.
  3059. */
  3060. void ebpf_send_statistic_data()
  3061. {
  3062. if (!publish_internal_metrics)
  3063. return;
  3064. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, NETDATA_EBPF_THREADS, "");
  3065. int i;
  3066. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  3067. ebpf_module_t *wem = &ebpf_modules[i];
  3068. if (wem->functions.fnct_routine)
  3069. continue;
  3070. write_chart_dimension((char *)wem->info.thread_name, (wem->enabled < NETDATA_THREAD_EBPF_STOPPING) ? 1 : 0);
  3071. }
  3072. ebpf_write_end_chart();
  3073. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, "monitoring_pid", "");
  3074. write_chart_dimension("user", ebpf_all_pids_count);
  3075. write_chart_dimension("kernel", ebpf_hash_table_pids_count);
  3076. ebpf_write_end_chart();
  3077. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, NETDATA_EBPF_LIFE_TIME, "");
  3078. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX ; i++) {
  3079. ebpf_module_t *wem = &ebpf_modules[i];
  3080. // Threads like VFS is slow to load and this can create an invalid number, this is the motive
  3081. // we are also testing wem->lifetime value.
  3082. if (wem->functions.fnct_routine)
  3083. continue;
  3084. write_chart_dimension((char *)wem->info.thread_name,
  3085. (wem->lifetime && wem->enabled < NETDATA_THREAD_EBPF_STOPPING) ?
  3086. (long long) (wem->lifetime - wem->running_time):
  3087. 0) ;
  3088. }
  3089. ebpf_write_end_chart();
  3090. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, NETDATA_EBPF_LOAD_METHOD, "");
  3091. write_chart_dimension(load_event_stat[NETDATA_EBPF_LOAD_STAT_LEGACY], (long long)plugin_statistics.legacy);
  3092. write_chart_dimension(load_event_stat[NETDATA_EBPF_LOAD_STAT_CORE], (long long)plugin_statistics.core);
  3093. ebpf_write_end_chart();
  3094. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, NETDATA_EBPF_KERNEL_MEMORY, "");
  3095. write_chart_dimension(memlock_stat, (long long)plugin_statistics.memlock_kern);
  3096. ebpf_write_end_chart();
  3097. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, NETDATA_EBPF_HASH_TABLES_LOADED, "");
  3098. write_chart_dimension(hash_table_stat, (long long)plugin_statistics.hash_tables);
  3099. ebpf_write_end_chart();
  3100. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, NETDATA_EBPF_HASH_TABLES_PER_CORE, "");
  3101. write_chart_dimension(hash_table_core[NETDATA_EBPF_THREAD_PER_CORE], (long long)plugin_statistics.hash_percpu);
  3102. write_chart_dimension(hash_table_core[NETDATA_EBPF_THREAD_UNIQUE], (long long)plugin_statistics.hash_unique);
  3103. ebpf_write_end_chart();
  3104. ebpf_send_global_hash_table_data();
  3105. ebpf_send_hash_table_pid_data(NETDATA_EBPF_HASH_TABLES_INSERT_PID_ELEMENTS, NETDATA_EBPF_GLOBAL_TABLE_PID_TABLE_ADD);
  3106. ebpf_send_hash_table_pid_data(NETDATA_EBPF_HASH_TABLES_REMOVE_PID_ELEMENTS, NETDATA_EBPF_GLOBAL_TABLE_PID_TABLE_DEL);
  3107. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  3108. ebpf_module_t *wem = &ebpf_modules[i];
  3109. if (!wem->functions.fnct_routine)
  3110. continue;
  3111. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, (char *)wem->functions.fcnt_thread_chart_name, "");
  3112. write_chart_dimension((char *)wem->info.thread_name, (wem->enabled < NETDATA_THREAD_EBPF_STOPPING) ? 1 : 0);
  3113. ebpf_write_end_chart();
  3114. ebpf_write_begin_chart(NETDATA_MONITORING_FAMILY, (char *)wem->functions.fcnt_thread_lifetime_name, "");
  3115. write_chart_dimension((char *)wem->info.thread_name,
  3116. (wem->lifetime && wem->enabled < NETDATA_THREAD_EBPF_STOPPING) ?
  3117. (long long) (wem->lifetime - wem->running_time):
  3118. 0) ;
  3119. ebpf_write_end_chart();
  3120. }
  3121. }
  3122. /**
  3123. * Update Internal Metric variable
  3124. *
  3125. * By default eBPF.plugin sends internal metrics for netdata, but user can
  3126. * disable this.
  3127. *
  3128. * The function updates the variable used to send charts.
  3129. */
  3130. static void update_internal_metric_variable()
  3131. {
  3132. const char *s = getenv("NETDATA_INTERNALS_MONITORING");
  3133. if (s && *s && strcmp(s, "NO") == 0)
  3134. publish_internal_metrics = false;
  3135. }
  3136. /**
  3137. * Create PIDS Chart
  3138. *
  3139. * Write to standard output current values for PIDSs charts.
  3140. *
  3141. * @param order order to display chart
  3142. * @param update_every time used to update charts
  3143. */
  3144. static void ebpf_create_pids_chart(int order, int update_every)
  3145. {
  3146. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  3147. "monitoring_pid",
  3148. "",
  3149. "Total number of monitored PIDs",
  3150. "pids",
  3151. NETDATA_EBPF_FAMILY,
  3152. NETDATA_EBPF_CHART_TYPE_LINE,
  3153. "netdata.ebpf_pids",
  3154. order,
  3155. update_every,
  3156. "main");
  3157. ebpf_write_global_dimension("user",
  3158. "user",
  3159. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3160. ebpf_write_global_dimension("kernel",
  3161. "kernel",
  3162. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3163. }
  3164. /**
  3165. * Create Thread Chart
  3166. *
  3167. * Write to standard output current values for threads charts.
  3168. *
  3169. * @param name is the chart name
  3170. * @param title chart title.
  3171. * @param units chart units
  3172. * @param order is the chart order
  3173. * @param update_every time used to update charts
  3174. * @param module a module to create a specific chart.
  3175. */
  3176. static void ebpf_create_thread_chart(char *name,
  3177. char *title,
  3178. char *units,
  3179. int order,
  3180. int update_every,
  3181. ebpf_module_t *module)
  3182. {
  3183. // common call for specific and all charts.
  3184. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  3185. name,
  3186. "",
  3187. title,
  3188. units,
  3189. NETDATA_EBPF_FAMILY,
  3190. NETDATA_EBPF_CHART_TYPE_LINE,
  3191. NULL,
  3192. order,
  3193. update_every,
  3194. "main");
  3195. if (module) {
  3196. ebpf_write_global_dimension((char *)module->info.thread_name,
  3197. (char *)module->info.thread_name,
  3198. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3199. return;
  3200. }
  3201. int i;
  3202. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  3203. ebpf_module_t *em = &ebpf_modules[i];
  3204. if (em->functions.fnct_routine)
  3205. continue;
  3206. ebpf_write_global_dimension((char *)em->info.thread_name,
  3207. (char *)em->info.thread_name,
  3208. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3209. }
  3210. }
  3211. /**
  3212. * Create chart for Load Thread
  3213. *
  3214. * Write to standard output current values for load mode.
  3215. *
  3216. * @param update_every time used to update charts
  3217. */
  3218. static inline void ebpf_create_statistic_load_chart(int update_every)
  3219. {
  3220. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  3221. NETDATA_EBPF_LOAD_METHOD,
  3222. "",
  3223. "Load info.",
  3224. "methods",
  3225. NETDATA_EBPF_FAMILY,
  3226. NETDATA_EBPF_CHART_TYPE_LINE,
  3227. NULL,
  3228. NETDATA_EBPF_ORDER_STAT_LOAD_METHOD,
  3229. update_every,
  3230. NETDATA_EBPF_MODULE_NAME_PROCESS);
  3231. ebpf_write_global_dimension(load_event_stat[NETDATA_EBPF_LOAD_STAT_LEGACY],
  3232. load_event_stat[NETDATA_EBPF_LOAD_STAT_LEGACY],
  3233. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3234. ebpf_write_global_dimension(load_event_stat[NETDATA_EBPF_LOAD_STAT_CORE],
  3235. load_event_stat[NETDATA_EBPF_LOAD_STAT_CORE],
  3236. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3237. }
  3238. /**
  3239. * Create chart for Kernel Memory
  3240. *
  3241. * Write to standard output current values for allocated memory.
  3242. *
  3243. * @param update_every time used to update charts
  3244. */
  3245. static inline void ebpf_create_statistic_kernel_memory(int update_every)
  3246. {
  3247. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  3248. NETDATA_EBPF_KERNEL_MEMORY,
  3249. "",
  3250. "Memory allocated for hash tables.",
  3251. "bytes",
  3252. NETDATA_EBPF_FAMILY,
  3253. NETDATA_EBPF_CHART_TYPE_LINE,
  3254. NULL,
  3255. NETDATA_EBPF_ORDER_STAT_KERNEL_MEMORY,
  3256. update_every,
  3257. NETDATA_EBPF_MODULE_NAME_PROCESS);
  3258. ebpf_write_global_dimension(memlock_stat,
  3259. memlock_stat,
  3260. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3261. }
  3262. /**
  3263. * Create chart Hash Table
  3264. *
  3265. * Write to standard output number of hash tables used with this software.
  3266. *
  3267. * @param update_every time used to update charts
  3268. */
  3269. static inline void ebpf_create_statistic_hash_tables(int update_every)
  3270. {
  3271. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  3272. NETDATA_EBPF_HASH_TABLES_LOADED,
  3273. "",
  3274. "Number of hash tables loaded.",
  3275. "hash tables",
  3276. NETDATA_EBPF_FAMILY,
  3277. NETDATA_EBPF_CHART_TYPE_LINE,
  3278. NULL,
  3279. NETDATA_EBPF_ORDER_STAT_HASH_TABLES,
  3280. update_every,
  3281. NETDATA_EBPF_MODULE_NAME_PROCESS);
  3282. ebpf_write_global_dimension(hash_table_stat,
  3283. hash_table_stat,
  3284. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3285. }
  3286. /**
  3287. * Create chart for percpu stats
  3288. *
  3289. * Write to standard output current values for threads.
  3290. *
  3291. * @param update_every time used to update charts
  3292. */
  3293. static inline void ebpf_create_statistic_hash_per_core(int update_every)
  3294. {
  3295. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  3296. NETDATA_EBPF_HASH_TABLES_PER_CORE,
  3297. "",
  3298. "How threads are loading hash/array tables.",
  3299. "threads",
  3300. NETDATA_EBPF_FAMILY,
  3301. NETDATA_EBPF_CHART_TYPE_LINE,
  3302. NULL,
  3303. NETDATA_EBPF_ORDER_STAT_HASH_CORE,
  3304. update_every,
  3305. NETDATA_EBPF_MODULE_NAME_PROCESS);
  3306. ebpf_write_global_dimension(hash_table_core[NETDATA_EBPF_THREAD_PER_CORE],
  3307. hash_table_core[NETDATA_EBPF_THREAD_PER_CORE],
  3308. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3309. ebpf_write_global_dimension(hash_table_core[NETDATA_EBPF_THREAD_UNIQUE],
  3310. hash_table_core[NETDATA_EBPF_THREAD_UNIQUE],
  3311. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3312. }
  3313. /**
  3314. * Hash table global elements
  3315. *
  3316. * Write to standard output current values inside global tables.
  3317. *
  3318. * @param update_every time used to update charts
  3319. */
  3320. static void ebpf_create_statistic_hash_global_elements(int update_every)
  3321. {
  3322. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  3323. NETDATA_EBPF_HASH_TABLES_GLOBAL_ELEMENTS,
  3324. "",
  3325. "Controllers inside global table",
  3326. "rows",
  3327. NETDATA_EBPF_FAMILY,
  3328. NETDATA_EBPF_CHART_TYPE_LINE,
  3329. NULL,
  3330. NETDATA_EBPF_ORDER_STAT_HASH_GLOBAL_TABLE_TOTAL,
  3331. update_every,
  3332. NETDATA_EBPF_MODULE_NAME_PROCESS);
  3333. int i;
  3334. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  3335. ebpf_write_global_dimension((char *)ebpf_modules[i].info.thread_name,
  3336. (char *)ebpf_modules[i].info.thread_name,
  3337. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX]);
  3338. }
  3339. }
  3340. /**
  3341. * Hash table global elements
  3342. *
  3343. * Write to standard output current values inside global tables.
  3344. *
  3345. * @param update_every time used to update charts
  3346. * @param id chart id
  3347. * @param title chart title
  3348. * @param order ordder chart will be shown on dashboard.
  3349. */
  3350. static void ebpf_create_statistic_hash_pid_table(int update_every, char *id, char *title, int order)
  3351. {
  3352. ebpf_write_chart_cmd(NETDATA_MONITORING_FAMILY,
  3353. id,
  3354. "",
  3355. title,
  3356. "rows",
  3357. NETDATA_EBPF_FAMILY,
  3358. NETDATA_EBPF_CHART_TYPE_LINE,
  3359. NULL,
  3360. order,
  3361. update_every,
  3362. NETDATA_EBPF_MODULE_NAME_PROCESS);
  3363. int i;
  3364. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  3365. ebpf_module_t *wem = &ebpf_modules[i];
  3366. if (wem->functions.apps_routine)
  3367. ebpf_write_global_dimension((char *)wem->info.thread_name,
  3368. (char *)wem->info.thread_name,
  3369. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]);
  3370. }
  3371. }
  3372. /**
  3373. * Create Statistics Charts
  3374. *
  3375. * Create charts that will show statistics related to eBPF plugin.
  3376. *
  3377. * @param update_every time used to update charts
  3378. */
  3379. static void ebpf_create_statistic_charts(int update_every)
  3380. {
  3381. static char create_charts = 1;
  3382. update_internal_metric_variable();
  3383. if (!publish_internal_metrics)
  3384. return;
  3385. if (!create_charts)
  3386. return;
  3387. create_charts = 0;
  3388. ebpf_create_thread_chart(NETDATA_EBPF_THREADS,
  3389. "Threads running.",
  3390. "boolean",
  3391. NETDATA_EBPF_ORDER_STAT_THREADS,
  3392. update_every,
  3393. NULL);
  3394. ebpf_create_pids_chart(NETDATA_EBPF_ORDER_PIDS, update_every);
  3395. ebpf_create_thread_chart(NETDATA_EBPF_LIFE_TIME,
  3396. "Time remaining for thread.",
  3397. "seconds",
  3398. NETDATA_EBPF_ORDER_STAT_LIFE_TIME,
  3399. update_every,
  3400. NULL);
  3401. int i,j;
  3402. char name[256];
  3403. for (i = 0, j = NETDATA_EBPF_ORDER_FUNCTION_PER_THREAD; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  3404. ebpf_module_t *em = &ebpf_modules[i];
  3405. if (!em->functions.fnct_routine)
  3406. continue;
  3407. em->functions.order_thread_chart = j;
  3408. snprintfz(name, sizeof(name) - 1, "%s_%s", NETDATA_EBPF_THREADS, em->info.thread_name);
  3409. em->functions.fcnt_thread_chart_name = strdupz(name);
  3410. ebpf_create_thread_chart(name,
  3411. "Threads running.",
  3412. "boolean",
  3413. j++,
  3414. update_every,
  3415. em);
  3416. em->functions.order_thread_lifetime = j;
  3417. snprintfz(name, sizeof(name) - 1, "%s_%s", NETDATA_EBPF_LIFE_TIME, em->info.thread_name);
  3418. em->functions.fcnt_thread_lifetime_name = strdupz(name);
  3419. ebpf_create_thread_chart(name,
  3420. "Time remaining for thread.",
  3421. "seconds",
  3422. j++,
  3423. update_every,
  3424. em);
  3425. }
  3426. ebpf_create_statistic_load_chart(update_every);
  3427. ebpf_create_statistic_kernel_memory(update_every);
  3428. ebpf_create_statistic_hash_tables(update_every);
  3429. ebpf_create_statistic_hash_per_core(update_every);
  3430. ebpf_create_statistic_hash_global_elements(update_every);
  3431. ebpf_create_statistic_hash_pid_table(update_every,
  3432. NETDATA_EBPF_HASH_TABLES_INSERT_PID_ELEMENTS,
  3433. "Elements inserted into PID table",
  3434. NETDATA_EBPF_ORDER_STAT_HASH_PID_TABLE_ADDED);
  3435. ebpf_create_statistic_hash_pid_table(update_every,
  3436. NETDATA_EBPF_HASH_TABLES_REMOVE_PID_ELEMENTS,
  3437. "Elements removed from PID table",
  3438. NETDATA_EBPF_ORDER_STAT_HASH_PID_TABLE_REMOVED);
  3439. fflush(stdout);
  3440. }
  3441. /*****************************************************************
  3442. *
  3443. * COLLECTOR ENTRY POINT
  3444. *
  3445. *****************************************************************/
  3446. /**
  3447. * Update PID file
  3448. *
  3449. * Update the content of PID file
  3450. *
  3451. * @param filename is the full name of the file.
  3452. * @param pid that identifies the process
  3453. */
  3454. static void ebpf_update_pid_file(char *filename, pid_t pid)
  3455. {
  3456. FILE *fp = fopen(filename, "w");
  3457. if (!fp)
  3458. return;
  3459. fprintf(fp, "%d", pid);
  3460. fclose(fp);
  3461. }
  3462. /**
  3463. * Get Process Name
  3464. *
  3465. * Get process name from /proc/PID/status
  3466. *
  3467. * @param pid that identifies the process
  3468. */
  3469. static char *ebpf_get_process_name(pid_t pid)
  3470. {
  3471. char *name = NULL;
  3472. char filename[FILENAME_MAX + 1];
  3473. snprintfz(filename, FILENAME_MAX, "/proc/%d/status", pid);
  3474. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  3475. if(unlikely(!ff)) {
  3476. netdata_log_error("Cannot open %s", filename);
  3477. return name;
  3478. }
  3479. ff = procfile_readall(ff);
  3480. if(unlikely(!ff))
  3481. return name;
  3482. unsigned long i, lines = procfile_lines(ff);
  3483. for(i = 0; i < lines ; i++) {
  3484. char *cmp = procfile_lineword(ff, i, 0);
  3485. if (!strcmp(cmp, "Name:")) {
  3486. name = strdupz(procfile_lineword(ff, i, 1));
  3487. break;
  3488. }
  3489. }
  3490. procfile_close(ff);
  3491. return name;
  3492. }
  3493. /**
  3494. * Read Previous PID
  3495. *
  3496. * @param filename is the full name of the file.
  3497. *
  3498. * @return It returns the PID used during previous execution on success or 0 otherwise
  3499. */
  3500. static pid_t ebpf_read_previous_pid(char *filename)
  3501. {
  3502. FILE *fp = fopen(filename, "r");
  3503. if (!fp)
  3504. return 0;
  3505. char buffer[64];
  3506. size_t length = fread(buffer, sizeof(*buffer), 63, fp);
  3507. pid_t old_pid = 0;
  3508. if (length) {
  3509. if (length > 63)
  3510. length = 63;
  3511. buffer[length] = '\0';
  3512. old_pid = (pid_t) str2uint32_t(buffer, NULL);
  3513. }
  3514. fclose(fp);
  3515. return old_pid;
  3516. }
  3517. /**
  3518. * Kill previous process
  3519. *
  3520. * Kill previous process whether it was not closed.
  3521. *
  3522. * @param filename is the full name of the file.
  3523. * @param pid that identifies the process
  3524. */
  3525. static void ebpf_kill_previous_process(char *filename, pid_t pid)
  3526. {
  3527. pid_t old_pid = ebpf_read_previous_pid(filename);
  3528. if (!old_pid)
  3529. return;
  3530. // Process is not running
  3531. char *prev_name = ebpf_get_process_name(old_pid);
  3532. if (!prev_name)
  3533. return;
  3534. char *current_name = ebpf_get_process_name(pid);
  3535. if (!strcmp(prev_name, current_name))
  3536. kill(old_pid, SIGKILL);
  3537. freez(prev_name);
  3538. freez(current_name);
  3539. // wait few microseconds before start new plugin
  3540. sleep_usec(USEC_PER_MS * 300);
  3541. }
  3542. /**
  3543. * PID file
  3544. *
  3545. * Write the filename for PID inside the given vector.
  3546. *
  3547. * @param filename vector where we will store the name.
  3548. * @param length number of bytes available in filename vector
  3549. */
  3550. void ebpf_pid_file(char *filename, size_t length)
  3551. {
  3552. snprintfz(filename, length, "%s/var/run/ebpf.pid", netdata_configured_host_prefix);
  3553. }
  3554. /**
  3555. * Manage PID
  3556. *
  3557. * This function kills another instance of eBPF whether it is necessary and update the file content.
  3558. *
  3559. * @param pid that identifies the process
  3560. */
  3561. static void ebpf_manage_pid(pid_t pid)
  3562. {
  3563. char filename[FILENAME_MAX + 1];
  3564. ebpf_pid_file(filename, FILENAME_MAX);
  3565. ebpf_kill_previous_process(filename, pid);
  3566. ebpf_update_pid_file(filename, pid);
  3567. }
  3568. /**
  3569. * Set start routine
  3570. *
  3571. * Set static routine before threads to be created.
  3572. */
  3573. static void ebpf_set_static_routine()
  3574. {
  3575. int i;
  3576. for (i = 0; ebpf_modules[i].info.thread_name; i++) {
  3577. ebpf_threads[i].start_routine = ebpf_modules[i].functions.start_routine;
  3578. }
  3579. }
  3580. /**
  3581. * Entry point
  3582. *
  3583. * @param argc the number of arguments
  3584. * @param argv the pointer to the arguments
  3585. *
  3586. * @return it returns 0 on success and another integer otherwise
  3587. */
  3588. int main(int argc, char **argv)
  3589. {
  3590. nd_log_initialize_for_external_plugins(NETDATA_EBPF_PLUGIN_NAME);
  3591. ebpf_set_global_variables();
  3592. if (ebpf_can_plugin_load_code(running_on_kernel, NETDATA_EBPF_PLUGIN_NAME))
  3593. return 2;
  3594. if (ebpf_adjust_memory_limit())
  3595. return 3;
  3596. main_thread_id = gettid_cached();
  3597. ebpf_parse_args(argc, argv);
  3598. ebpf_manage_pid(getpid());
  3599. signal(SIGINT, ebpf_stop_threads);
  3600. signal(SIGQUIT, ebpf_stop_threads);
  3601. signal(SIGTERM, ebpf_stop_threads);
  3602. signal(SIGPIPE, ebpf_stop_threads);
  3603. ebpf_start_pthread_variables();
  3604. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  3605. if(verify_netdata_host_prefix(true) == -1) ebpf_exit(6);
  3606. ebpf_allocate_common_vectors();
  3607. #ifdef LIBBPF_MAJOR_VERSION
  3608. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  3609. #ifndef NETDATA_INTERNAL_CHECKS
  3610. libbpf_set_print(netdata_silent_libbpf_vfprintf);
  3611. #endif
  3612. #endif
  3613. ebpf_read_local_addresses_unsafe();
  3614. read_local_ports("/proc/net/tcp", IPPROTO_TCP);
  3615. read_local_ports("/proc/net/tcp6", IPPROTO_TCP);
  3616. read_local_ports("/proc/net/udp", IPPROTO_UDP);
  3617. read_local_ports("/proc/net/udp6", IPPROTO_UDP);
  3618. ebpf_set_static_routine();
  3619. cgroup_integration_thread.start_routine = ebpf_cgroup_integration;
  3620. cgroup_integration_thread.thread = nd_thread_create(
  3621. cgroup_integration_thread.name,
  3622. NETDATA_THREAD_OPTION_DEFAULT,
  3623. ebpf_cgroup_integration,
  3624. NULL);
  3625. uint32_t i;
  3626. for (i = 0; ebpf_threads[i].name != NULL; i++) {
  3627. struct netdata_static_thread *st = &ebpf_threads[i];
  3628. ebpf_module_t *em = &ebpf_modules[i];
  3629. em->thread = st;
  3630. em->thread_id = i;
  3631. if (em->enabled != NETDATA_THREAD_EBPF_NOT_RUNNING) {
  3632. em->enabled = NETDATA_THREAD_EBPF_RUNNING;
  3633. em->lifetime = EBPF_NON_FUNCTION_LIFE_TIME;
  3634. if (em->functions.apps_routine && (em->apps_charts || em->cgroup_charts)) {
  3635. collect_pids |= 1<<i;
  3636. }
  3637. st->thread = nd_thread_create(st->name, NETDATA_THREAD_OPTION_JOINABLE, st->start_routine, em);
  3638. } else {
  3639. em->lifetime = EBPF_DEFAULT_LIFETIME;
  3640. }
  3641. }
  3642. heartbeat_t hb;
  3643. heartbeat_init(&hb, USEC_PER_SEC);
  3644. int update_apps_every = (int) EBPF_CFG_UPDATE_APPS_EVERY_DEFAULT;
  3645. int update_apps_list = update_apps_every - 1;
  3646. int process_maps_per_core = ebpf_modules[EBPF_MODULE_PROCESS_IDX].maps_per_core;
  3647. //Plugin will be killed when it receives a signal
  3648. for ( ; !ebpf_plugin_stop(); global_iterations_counter++) {
  3649. (void)heartbeat_next(&hb);
  3650. if (global_iterations_counter % EBPF_DEFAULT_UPDATE_EVERY == 0) {
  3651. pthread_mutex_lock(&lock);
  3652. ebpf_create_statistic_charts(EBPF_DEFAULT_UPDATE_EVERY);
  3653. ebpf_send_statistic_data();
  3654. fflush(stdout);
  3655. pthread_mutex_unlock(&lock);
  3656. }
  3657. if (++update_apps_list == update_apps_every) {
  3658. update_apps_list = 0;
  3659. pthread_mutex_lock(&lock);
  3660. if (collect_pids) {
  3661. pthread_mutex_lock(&collect_data_mutex);
  3662. ebpf_parse_proc_files();
  3663. if (collect_pids & (1<<EBPF_MODULE_PROCESS_IDX)) {
  3664. collect_data_for_all_processes(process_pid_fd, process_maps_per_core);
  3665. }
  3666. ebpf_create_apps_charts(apps_groups_root_target);
  3667. pthread_mutex_unlock(&collect_data_mutex);
  3668. }
  3669. pthread_mutex_unlock(&lock);
  3670. }
  3671. }
  3672. ebpf_stop_threads(0);
  3673. return 0;
  3674. }