ebpf.c 86 KB

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