ebpf.c 103 KB

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