ebpf.c 88 KB

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