ebpf.c 66 KB

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