ebpf.c 109 KB

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