ebpf.c 141 KB

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