ebpf.c 77 KB

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