ebpf.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <dlfcn.h>
  6. #include <sys/utsname.h>
  7. #include "ebpf.h"
  8. #include "../libnetdata.h"
  9. char *ebpf_user_config_dir = CONFIG_DIR;
  10. char *ebpf_stock_config_dir = LIBCONFIG_DIR;
  11. /*
  12. static int clean_kprobe_event(FILE *out, char *filename, char *father_pid, netdata_ebpf_events_t *ptr)
  13. {
  14. int fd = open(filename, O_WRONLY | O_APPEND, 0);
  15. if (fd < 0) {
  16. if (out) {
  17. fprintf(out, "Cannot open %s : %s\n", filename, strerror(errno));
  18. }
  19. return 1;
  20. }
  21. char cmd[1024];
  22. int length = snprintf(cmd, 1023, "-:kprobes/%c_netdata_%s_%s", ptr->type, ptr->name, father_pid);
  23. int ret = 0;
  24. if (length > 0) {
  25. ssize_t written = write(fd, cmd, strlen(cmd));
  26. if (written < 0) {
  27. if (out) {
  28. fprintf(
  29. out, "Cannot remove the event (%d, %d) '%s' from %s : %s\n", getppid(), getpid(), cmd, filename,
  30. strerror((int)errno));
  31. }
  32. ret = 1;
  33. }
  34. }
  35. close(fd);
  36. return ret;
  37. }
  38. int clean_kprobe_events(FILE *out, int pid, netdata_ebpf_events_t *ptr)
  39. {
  40. char filename[FILENAME_MAX + 1];
  41. snprintf(filename, FILENAME_MAX, "%s%s", NETDATA_DEBUGFS, "kprobe_events");
  42. char removeme[16];
  43. snprintf(removeme, 15, "%d", pid);
  44. int i;
  45. for (i = 0; ptr[i].name; i++) {
  46. if (clean_kprobe_event(out, filename, removeme, &ptr[i])) {
  47. break;
  48. }
  49. }
  50. return 0;
  51. }
  52. */
  53. //----------------------------------------------------------------------------------------------------------------------
  54. /**
  55. * Get Kernel version
  56. *
  57. * Get the current kernel from /proc and returns an integer value representing it
  58. *
  59. * @return it returns a value representing the kernel version.
  60. */
  61. int ebpf_get_kernel_version()
  62. {
  63. char major[16], minor[16], patch[16];
  64. char ver[VERSION_STRING_LEN];
  65. char *version = ver;
  66. int fd = open("/proc/sys/kernel/osrelease", O_RDONLY);
  67. if (fd < 0)
  68. return -1;
  69. ssize_t len = read(fd, ver, sizeof(ver));
  70. if (len < 0) {
  71. close(fd);
  72. return -1;
  73. }
  74. close(fd);
  75. char *move = major;
  76. while (*version && *version != '.')
  77. *move++ = *version++;
  78. *move = '\0';
  79. version++;
  80. move = minor;
  81. while (*version && *version != '.')
  82. *move++ = *version++;
  83. *move = '\0';
  84. if (*version)
  85. version++;
  86. else
  87. return -1;
  88. move = patch;
  89. while (*version && *version != '\n' && *version != '-')
  90. *move++ = *version++;
  91. *move = '\0';
  92. // This new rule is fixing kernel version according the formula:
  93. // KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))
  94. // that was extracted from /usr/include/linux/version.h
  95. int ipatch = (int)str2l(patch);
  96. if (ipatch > 255)
  97. ipatch = 255;
  98. return ((int)(str2l(major) * 65536) + (int)(str2l(minor) * 256) + ipatch);
  99. }
  100. /**
  101. * Get RH release
  102. *
  103. * Read Red Hat release from /etc/redhat-release
  104. *
  105. * @return It returns RH release on success and -1 otherwise
  106. */
  107. int get_redhat_release()
  108. {
  109. char buffer[VERSION_STRING_LEN + 1];
  110. int major, minor;
  111. FILE *fp = fopen("/etc/redhat-release", "r");
  112. if (fp) {
  113. major = 0;
  114. minor = -1;
  115. size_t length = fread(buffer, sizeof(char), VERSION_STRING_LEN, fp);
  116. if (length > 4) {
  117. buffer[length] = '\0';
  118. char *end = strchr(buffer, '.');
  119. char *start;
  120. if (end) {
  121. *end = 0x0;
  122. if (end > buffer) {
  123. start = end - 1;
  124. major = strtol(start, NULL, 10);
  125. start = ++end;
  126. end++;
  127. if (end) {
  128. end = 0x00;
  129. minor = strtol(start, NULL, 10);
  130. } else {
  131. minor = -1;
  132. }
  133. }
  134. }
  135. }
  136. fclose(fp);
  137. return ((major * 256) + minor);
  138. } else {
  139. return -1;
  140. }
  141. }
  142. /**
  143. * Check if the kernel is in a list of rejected ones
  144. *
  145. * @return Returns 1 if the kernel is rejected, 0 otherwise.
  146. */
  147. static int kernel_is_rejected()
  148. {
  149. // Get kernel version from system
  150. char version_string[VERSION_STRING_LEN + 1];
  151. int version_string_len = 0;
  152. if (read_file("/proc/version_signature", version_string, VERSION_STRING_LEN)) {
  153. if (read_file("/proc/version", version_string, VERSION_STRING_LEN)) {
  154. struct utsname uname_buf;
  155. if (!uname(&uname_buf)) {
  156. netdata_log_info("Cannot check kernel version");
  157. return 0;
  158. }
  159. version_string_len =
  160. snprintfz(version_string, VERSION_STRING_LEN, "%s %s", uname_buf.release, uname_buf.version);
  161. }
  162. }
  163. if (!version_string_len)
  164. version_string_len = strlen(version_string);
  165. // Open a file with a list of rejected kernels
  166. char *config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  167. if (config_dir == NULL) {
  168. config_dir = CONFIG_DIR;
  169. }
  170. char filename[FILENAME_MAX + 1];
  171. snprintfz(filename, FILENAME_MAX, "%s/ebpf.d/%s", config_dir, EBPF_KERNEL_REJECT_LIST_FILE);
  172. FILE *kernel_reject_list = fopen(filename, "r");
  173. if (!kernel_reject_list) {
  174. // Keep this to have compatibility with old versions
  175. snprintfz(filename, FILENAME_MAX, "%s/%s", config_dir, EBPF_KERNEL_REJECT_LIST_FILE);
  176. kernel_reject_list = fopen(filename, "r");
  177. if (!kernel_reject_list) {
  178. config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  179. if (config_dir == NULL) {
  180. config_dir = LIBCONFIG_DIR;
  181. }
  182. snprintfz(filename, FILENAME_MAX, "%s/ebpf.d/%s", config_dir, EBPF_KERNEL_REJECT_LIST_FILE);
  183. kernel_reject_list = fopen(filename, "r");
  184. if (!kernel_reject_list)
  185. return 0;
  186. }
  187. }
  188. // Find if the kernel is in the reject list
  189. char *reject_string = NULL;
  190. size_t buf_len = 0;
  191. ssize_t reject_string_len;
  192. while ((reject_string_len = getline(&reject_string, &buf_len, kernel_reject_list) - 1) > 0) {
  193. if (version_string_len >= reject_string_len) {
  194. if (!strncmp(version_string, reject_string, reject_string_len)) {
  195. netdata_log_info("A buggy kernel is detected");
  196. fclose(kernel_reject_list);
  197. freez(reject_string);
  198. return 1;
  199. }
  200. }
  201. }
  202. fclose(kernel_reject_list);
  203. free(reject_string);
  204. return 0;
  205. }
  206. static int has_ebpf_kernel_version(int version)
  207. {
  208. if (kernel_is_rejected())
  209. return 0;
  210. // Kernel 4.11.0 or RH > 7.5
  211. return (version >= NETDATA_MINIMUM_EBPF_KERNEL || get_redhat_release() >= NETDATA_MINIMUM_RH_VERSION);
  212. }
  213. int has_condition_to_run(int version)
  214. {
  215. if (!has_ebpf_kernel_version(version))
  216. return 0;
  217. return 1;
  218. }
  219. //----------------------------------------------------------------------------------------------------------------------
  220. /**
  221. * Kernel Name
  222. *
  223. * Select kernel name used by eBPF programs
  224. *
  225. * Netdata delivers for users eBPF programs with specific suffixes that represent the kernels they were
  226. * compiled, when we load the eBPF program, the suffix must be the nereast possible of the kernel running.
  227. *
  228. * @param selector select the kernel version.
  229. *
  230. * @return It returns the string to load kernel.
  231. */
  232. static char *ebpf_select_kernel_name(uint32_t selector)
  233. {
  234. static char *kernel_names[] = { NETDATA_IDX_STR_V3_10, NETDATA_IDX_STR_V4_14, NETDATA_IDX_STR_V4_16,
  235. NETDATA_IDX_STR_V4_18, NETDATA_IDX_STR_V5_4, NETDATA_IDX_STR_V5_10,
  236. NETDATA_IDX_STR_V5_11, NETDATA_IDX_STR_V5_14, NETDATA_IDX_STR_V5_15,
  237. NETDATA_IDX_STR_V5_16
  238. };
  239. return kernel_names[selector];
  240. }
  241. /**
  242. * Select Max Index
  243. *
  244. * Select last index that will be tested on host.
  245. *
  246. * @param is_rhf is Red Hat fammily?
  247. * @param kver the kernel version
  248. *
  249. * @return it returns the index to access kernel string.
  250. */
  251. static int ebpf_select_max_index(int is_rhf, uint32_t kver)
  252. {
  253. if (is_rhf > 0) { // Is Red Hat family
  254. if (kver >= NETDATA_EBPF_KERNEL_5_14)
  255. return NETDATA_IDX_V5_14;
  256. else if (kver >= NETDATA_EBPF_KERNEL_5_4 && kver < NETDATA_EBPF_KERNEL_5_5) // For Oracle Linux
  257. return NETDATA_IDX_V5_4;
  258. else if (kver >= NETDATA_EBPF_KERNEL_4_11)
  259. return NETDATA_IDX_V4_18;
  260. } else { // Kernels from kernel.org
  261. if (kver >= NETDATA_EBPF_KERNEL_5_16)
  262. return NETDATA_IDX_V5_16;
  263. else if (kver >= NETDATA_EBPF_KERNEL_5_15)
  264. return NETDATA_IDX_V5_15;
  265. else if (kver >= NETDATA_EBPF_KERNEL_5_11)
  266. return NETDATA_IDX_V5_11;
  267. else if (kver >= NETDATA_EBPF_KERNEL_5_10)
  268. return NETDATA_IDX_V5_10;
  269. else if (kver >= NETDATA_EBPF_KERNEL_4_17)
  270. return NETDATA_IDX_V5_4;
  271. else if (kver >= NETDATA_EBPF_KERNEL_4_15)
  272. return NETDATA_IDX_V4_16;
  273. else if (kver >= NETDATA_EBPF_KERNEL_4_11)
  274. return NETDATA_IDX_V4_14;
  275. }
  276. return NETDATA_IDX_V3_10;
  277. }
  278. /**
  279. * Select Index
  280. *
  281. * Select index to load data.
  282. *
  283. * @param kernels is the variable with kernel versions.
  284. * @param is_rhf is Red Hat fammily?
  285. * param kver the kernel version
  286. */
  287. static uint32_t ebpf_select_index(uint32_t kernels, int is_rhf, uint32_t kver)
  288. {
  289. uint32_t start = ebpf_select_max_index(is_rhf, kver);
  290. uint32_t idx;
  291. if (is_rhf == -1)
  292. kernels &= ~NETDATA_V5_14;
  293. for (idx = start; idx; idx--) {
  294. if (kernels & 1 << idx)
  295. break;
  296. }
  297. return idx;
  298. }
  299. /**
  300. * Mount Name
  301. *
  302. * Mount name of eBPF program to be loaded.
  303. *
  304. * Netdata eBPF programs has the following format:
  305. *
  306. * Tnetdata_ebpf_N.V.o
  307. *
  308. * where:
  309. * T - Is the eBPF type. When starts with 'p', this means we are only adding probes,
  310. * and when they start with 'r' we are using retprobes.
  311. * N - The eBPF program name.
  312. * V - The kernel version in string format.
  313. *
  314. * @param out the vector where the name will be stored
  315. * @param len the size of the out vector.
  316. * @param path where the binaries are stored
  317. * @param kver the kernel version
  318. * @param name the eBPF program name.
  319. * @param is_return is return or entry ?
  320. */
  321. static void ebpf_mount_name(char *out, size_t len, char *path, uint32_t kver, const char *name,
  322. int is_return, int is_rhf)
  323. {
  324. char *version = ebpf_select_kernel_name(kver);
  325. snprintfz(out, len, "%s/ebpf.d/%cnetdata_ebpf_%s.%s%s.o",
  326. path,
  327. (is_return) ? 'r' : 'p',
  328. name,
  329. version,
  330. (is_rhf != -1) ? ".rhf" : "");
  331. }
  332. //----------------------------------------------------------------------------------------------------------------------
  333. /**
  334. * Statistics from targets
  335. *
  336. * Count the information from targets.
  337. *
  338. * @param report the output structure
  339. * @param targets vector with information about the eBPF plugin.
  340. * @param value factor used to update calculation
  341. */
  342. static void ebpf_stats_targets(ebpf_plugin_stats_t *report, netdata_ebpf_targets_t *targets, int value)
  343. {
  344. if (!targets) {
  345. report->probes = report->tracepoints = report->trampolines = 0;
  346. return;
  347. }
  348. int i = 0;
  349. while (targets[i].name) {
  350. switch (targets[i].mode) {
  351. case EBPF_LOAD_PROBE: {
  352. report->probes += value;
  353. break;
  354. }
  355. case EBPF_LOAD_RETPROBE: {
  356. report->retprobes += value;
  357. break;
  358. }
  359. case EBPF_LOAD_TRACEPOINT: {
  360. report->tracepoints += value;
  361. break;
  362. }
  363. case EBPF_LOAD_TRAMPOLINE: {
  364. report->trampolines += value;
  365. break;
  366. }
  367. }
  368. i++;
  369. }
  370. }
  371. /**
  372. * Update General stats
  373. *
  374. * Update eBPF plugin statistics that has relationship with the thread.
  375. *
  376. * This function must be called with mutex associated to charts is locked.
  377. *
  378. * @param report the output structure
  379. * @param em the structure with information about how the module/thread is working.
  380. */
  381. void ebpf_update_stats(ebpf_plugin_stats_t *report, ebpf_module_t *em)
  382. {
  383. int value;
  384. // It is not necessary to report more information.
  385. if (em->enabled > NETDATA_THREAD_EBPF_FUNCTION_RUNNING)
  386. value = -1;
  387. else
  388. value = 1;
  389. report->threads += value;
  390. report->running += value;
  391. // In theory the `else if` is useless, because when this function is called, the module should not stay in
  392. // EBPF_LOAD_PLAY_DICE. We have this additional condition to detect errors from developers.
  393. if (em->load & EBPF_LOAD_LEGACY)
  394. report->legacy += value;
  395. else if (em->load & EBPF_LOAD_CORE)
  396. report->core += value;
  397. if (em->maps_per_core)
  398. report->hash_percpu += value;
  399. else
  400. report->hash_unique += value;
  401. ebpf_stats_targets(report, em->targets, value);
  402. }
  403. /**
  404. * Update Kernel memory with memory
  405. *
  406. * This algorithm is an adaptation of https://elixir.bootlin.com/linux/v6.1.14/source/tools/bpf/bpftool/common.c#L402
  407. * to get 'memlock' data and update report.
  408. *
  409. * @param report the output structure
  410. * @param map pointer to a map.
  411. * @param action What action will be done with this map.
  412. */
  413. void ebpf_update_kernel_memory(ebpf_plugin_stats_t *report, ebpf_local_maps_t *map, ebpf_stats_action_t action)
  414. {
  415. char filename[FILENAME_MAX+1];
  416. snprintfz(filename, FILENAME_MAX, "/proc/self/fdinfo/%d", map->map_fd);
  417. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  418. if(unlikely(!ff)) {
  419. netdata_log_error("Cannot open %s", filename);
  420. return;
  421. }
  422. ff = procfile_readall(ff);
  423. if(unlikely(!ff))
  424. return;
  425. unsigned long j, lines = procfile_lines(ff);
  426. char *memlock = { "memlock" };
  427. for (j = 0; j < lines ; j++) {
  428. char *cmp = procfile_lineword(ff, j,0);
  429. if (!strncmp(memlock, cmp, 7)) {
  430. uint64_t memsize = (uint64_t) str2l(procfile_lineword(ff, j,1));
  431. switch (action) {
  432. case EBPF_ACTION_STAT_ADD: {
  433. report->memlock_kern += memsize;
  434. report->hash_tables += 1;
  435. #ifdef NETDATA_DEV_MODE
  436. netdata_log_info("Hash table %u: %s (FD = %d) is consuming %lu bytes totalizing %lu bytes",
  437. report->hash_tables, map->name, map->map_fd, memsize, report->memlock_kern);
  438. #endif
  439. break;
  440. }
  441. case EBPF_ACTION_STAT_REMOVE: {
  442. report->memlock_kern -= memsize;
  443. report->hash_tables -= 1;
  444. #ifdef NETDATA_DEV_MODE
  445. netdata_log_info("Hash table %s (FD = %d) was removed releasing %lu bytes, now we have %u tables loaded totalizing %lu bytes.",
  446. map->name, map->map_fd, memsize, report->hash_tables, report->memlock_kern);
  447. #endif
  448. break;
  449. }
  450. default: {
  451. break;
  452. }
  453. }
  454. break;
  455. }
  456. }
  457. procfile_close(ff);
  458. }
  459. /**
  460. * Update Kernel memory with memory
  461. *
  462. * This algorithm is an adaptation of https://elixir.bootlin.com/linux/v6.1.14/source/tools/bpf/bpftool/common.c#L402
  463. * to get 'memlock' data and update report.
  464. *
  465. * @param report the output structure
  466. * @param map pointer to a map. Last map must fish with name = NULL
  467. * @param action should plugin add or remove values from amount.
  468. */
  469. void ebpf_update_kernel_memory_with_vector(ebpf_plugin_stats_t *report,
  470. ebpf_local_maps_t *maps,
  471. ebpf_stats_action_t action)
  472. {
  473. if (!maps)
  474. return;
  475. ebpf_local_maps_t *map;
  476. int i = 0;
  477. for (map = &maps[i]; maps[i].name; i++, map = &maps[i]) {
  478. int fd = map->map_fd;
  479. if (fd == ND_EBPF_MAP_FD_NOT_INITIALIZED)
  480. continue;
  481. ebpf_update_kernel_memory(report, map, action);
  482. }
  483. }
  484. //----------------------------------------------------------------------------------------------------------------------
  485. void ebpf_update_pid_table(ebpf_local_maps_t *pid, ebpf_module_t *em)
  486. {
  487. pid->user_input = em->pid_map_size;
  488. }
  489. /**
  490. * Update map size
  491. *
  492. * Update map size with information read from configuration files.
  493. *
  494. * @param map the structure with file descriptor to update.
  495. * @param lmap the structure with information from configuration files.
  496. * @param em the structure with information about how the module/thread is working.
  497. * @param map_name the name of the file used to log.
  498. */
  499. void ebpf_update_map_size(struct bpf_map *map, ebpf_local_maps_t *lmap, ebpf_module_t *em, const char *map_name __maybe_unused)
  500. {
  501. uint32_t define_size = 0;
  502. uint32_t apps_type = NETDATA_EBPF_MAP_PID | NETDATA_EBPF_MAP_RESIZABLE;
  503. if (lmap->user_input && lmap->user_input != lmap->internal_input) {
  504. define_size = lmap->internal_input;
  505. #ifdef NETDATA_INTERNAL_CHECKS
  506. netdata_log_info("Changing map %s from size %u to %u ", map_name, lmap->internal_input, lmap->user_input);
  507. #endif
  508. } else if (((lmap->type & apps_type) == apps_type) && (!em->apps_charts) && (!em->cgroup_charts)) {
  509. lmap->user_input = ND_EBPF_DEFAULT_MIN_PID;
  510. } else if (((em->apps_charts) || (em->cgroup_charts)) && (em->apps_level != NETDATA_APPS_NOT_SET)) {
  511. switch (em->apps_level) {
  512. case NETDATA_APPS_LEVEL_ALL: {
  513. define_size = lmap->user_input;
  514. break;
  515. }
  516. case NETDATA_APPS_LEVEL_PARENT: {
  517. define_size = ND_EBPF_DEFAULT_PID_SIZE / 2;
  518. break;
  519. }
  520. case NETDATA_APPS_LEVEL_REAL_PARENT:
  521. default: {
  522. define_size = ND_EBPF_DEFAULT_PID_SIZE / 3;
  523. }
  524. }
  525. }
  526. if (!define_size)
  527. return;
  528. #ifdef LIBBPF_MAJOR_VERSION
  529. bpf_map__set_max_entries(map, define_size);
  530. #else
  531. bpf_map__resize(map, define_size);
  532. #endif
  533. }
  534. #ifdef LIBBPF_MAJOR_VERSION
  535. /**
  536. * Update map type
  537. *
  538. * Update map type with information given.
  539. *
  540. * @param map the map we want to modify
  541. * @param w a structure with user input
  542. */
  543. void ebpf_update_map_type(struct bpf_map *map, ebpf_local_maps_t *w)
  544. {
  545. if (bpf_map__set_type(map, w->map_type)) {
  546. netdata_log_error("Cannot modify map type for %s", w->name);
  547. }
  548. }
  549. /**
  550. * Define map type
  551. *
  552. * This PR defines the type used by hash tables according user input.
  553. *
  554. * @param maps the list of maps used with a hash table.
  555. * @param maps_per_core define if map type according user specification.
  556. * @param kver kernel version host is running.
  557. */
  558. void ebpf_define_map_type(ebpf_local_maps_t *maps, int maps_per_core, int kver)
  559. {
  560. if (!maps)
  561. return;
  562. // Before kernel 4.06 there was not percpu hash tables
  563. if (kver < NETDATA_EBPF_KERNEL_4_06)
  564. maps_per_core = CONFIG_BOOLEAN_NO;
  565. int i = 0;
  566. while (maps[i].name) {
  567. ebpf_local_maps_t *map = &maps[i];
  568. // maps_per_core is a boolean value in configuration files.
  569. if (maps_per_core) {
  570. if (map->map_type == BPF_MAP_TYPE_HASH)
  571. map->map_type = BPF_MAP_TYPE_PERCPU_HASH;
  572. else if (map->map_type == BPF_MAP_TYPE_ARRAY)
  573. map->map_type = BPF_MAP_TYPE_PERCPU_ARRAY;
  574. } else {
  575. if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH)
  576. map->map_type = BPF_MAP_TYPE_HASH;
  577. else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
  578. map->map_type = BPF_MAP_TYPE_ARRAY;
  579. }
  580. i++;
  581. }
  582. }
  583. #endif
  584. /**
  585. * Update Legacy map
  586. *
  587. * Update map for eBPF legacy code.
  588. *
  589. * @param program the structure with values read from binary.
  590. * @param em the structure with information about how the module/thread is working.
  591. */
  592. static void ebpf_update_legacy_map(struct bpf_object *program, ebpf_module_t *em)
  593. {
  594. struct bpf_map *map;
  595. ebpf_local_maps_t *maps = em->maps;
  596. if (!maps)
  597. return;
  598. bpf_map__for_each(map, program)
  599. {
  600. const char *map_name = bpf_map__name(map);
  601. int i = 0;
  602. while (maps[i].name) {
  603. ebpf_local_maps_t *w = &maps[i];
  604. if (!strcmp(w->name, map_name)) {
  605. // Modify size
  606. if (w->type & NETDATA_EBPF_MAP_RESIZABLE) {
  607. ebpf_update_map_size(map, w, em, map_name);
  608. }
  609. #ifdef LIBBPF_MAJOR_VERSION
  610. ebpf_update_map_type(map, w);
  611. #endif
  612. }
  613. i++;
  614. }
  615. }
  616. }
  617. size_t ebpf_count_programs(struct bpf_object *obj)
  618. {
  619. size_t tot = 0;
  620. struct bpf_program *prog;
  621. bpf_object__for_each_program(prog, obj)
  622. {
  623. tot++;
  624. }
  625. return tot;
  626. }
  627. static ebpf_specify_name_t *ebpf_find_names(ebpf_specify_name_t *names, const char *prog_name)
  628. {
  629. size_t i = 0;
  630. while (names[i].program_name) {
  631. if (!strcmp(prog_name, names[i].program_name))
  632. return &names[i];
  633. i++;
  634. }
  635. return NULL;
  636. }
  637. static struct bpf_link **ebpf_attach_programs(struct bpf_object *obj, size_t length, ebpf_specify_name_t *names)
  638. {
  639. struct bpf_link **links = callocz(length , sizeof(struct bpf_link *));
  640. size_t i = 0;
  641. struct bpf_program *prog;
  642. ebpf_specify_name_t *w;
  643. bpf_object__for_each_program(prog, obj)
  644. {
  645. if (names) {
  646. const char *name = bpf_program__name(prog);
  647. w = ebpf_find_names(names, name);
  648. } else
  649. w = NULL;
  650. if (w) {
  651. enum bpf_prog_type type = bpf_program__get_type(prog);
  652. if (type == BPF_PROG_TYPE_KPROBE)
  653. links[i] = bpf_program__attach_kprobe(prog, w->retprobe, w->optional);
  654. } else
  655. links[i] = bpf_program__attach(prog);
  656. if (libbpf_get_error(links[i])) {
  657. links[i] = NULL;
  658. }
  659. i++;
  660. }
  661. return links;
  662. }
  663. static void ebpf_update_maps(ebpf_module_t *em, struct bpf_object *obj)
  664. {
  665. if (!em->maps)
  666. return;
  667. ebpf_local_maps_t *maps = em->maps;
  668. struct bpf_map *map;
  669. bpf_map__for_each(map, obj)
  670. {
  671. int fd = bpf_map__fd(map);
  672. if (maps) {
  673. const char *map_name = bpf_map__name(map);
  674. int j = 0;
  675. while (maps[j].name) {
  676. ebpf_local_maps_t *w = &maps[j];
  677. if (w->map_fd == ND_EBPF_MAP_FD_NOT_INITIALIZED && !strcmp(map_name, w->name))
  678. w->map_fd = fd;
  679. j++;
  680. }
  681. }
  682. }
  683. }
  684. /**
  685. * Update Controller
  686. *
  687. * Update controller value with user input.
  688. *
  689. * @param fd the table file descriptor
  690. * @param em structure with information about eBPF program we will load.
  691. */
  692. void ebpf_update_controller(int fd, ebpf_module_t *em)
  693. {
  694. uint32_t values[NETDATA_CONTROLLER_END] = {
  695. (em->apps_charts & NETDATA_EBPF_APPS_FLAG_YES) | em->cgroup_charts,
  696. em->apps_level, 0, 0, 0, 0
  697. };
  698. uint32_t key;
  699. uint32_t end = NETDATA_CONTROLLER_PID_TABLE_ADD;
  700. for (key = NETDATA_CONTROLLER_APPS_ENABLED; key < end; key++) {
  701. int ret = bpf_map_update_elem(fd, &key, &values[key], BPF_ANY);
  702. if (ret)
  703. netdata_log_error("Add key(%u) for controller table failed.", key);
  704. }
  705. }
  706. /**
  707. * Update Legacy controller
  708. *
  709. * Update legacy controller table when eBPF program has it.
  710. *
  711. * @param em structure with information about eBPF program we will load.
  712. * @param obj bpf object with tables.
  713. */
  714. static void ebpf_update_legacy_controller(ebpf_module_t *em, struct bpf_object *obj)
  715. {
  716. ebpf_local_maps_t *maps = em->maps;
  717. if (!maps)
  718. return;
  719. struct bpf_map *map;
  720. bpf_map__for_each(map, obj)
  721. {
  722. size_t i = 0;
  723. while (maps[i].name) {
  724. ebpf_local_maps_t *w = &maps[i];
  725. if (w->map_fd != ND_EBPF_MAP_FD_NOT_INITIALIZED && (w->type & NETDATA_EBPF_MAP_CONTROLLER)) {
  726. w->type &= ~NETDATA_EBPF_MAP_CONTROLLER;
  727. w->type |= NETDATA_EBPF_MAP_CONTROLLER_UPDATED;
  728. ebpf_update_controller(w->map_fd, em);
  729. }
  730. i++;
  731. }
  732. }
  733. }
  734. /**
  735. * Load Program
  736. *
  737. * Load eBPF program into kernel
  738. *
  739. * @param plugins_dir directory where binary are stored
  740. * @param em structure with information about eBPF program we will load.
  741. * @param kver the kernel version according /usr/include/linux/version.h
  742. * @param is_rhf is a kernel from Red Hat Family?
  743. * @param obj structure where we will store object loaded.
  744. *
  745. * @return it returns a link for each target we associated an eBPF program.
  746. */
  747. struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, int kver, int is_rhf,
  748. struct bpf_object **obj)
  749. {
  750. char lpath[4096];
  751. uint32_t idx = ebpf_select_index(em->kernels, is_rhf, kver);
  752. ebpf_mount_name(lpath, 4095, plugins_dir, idx, em->info.thread_name, em->mode, is_rhf);
  753. // When this function is called ebpf.plugin is using legacy code, so we should reset the variable
  754. em->load &= ~ NETDATA_EBPF_LOAD_METHODS;
  755. em->load |= EBPF_LOAD_LEGACY;
  756. *obj = bpf_object__open_file(lpath, NULL);
  757. if (!*obj)
  758. return NULL;
  759. if (libbpf_get_error(obj)) {
  760. bpf_object__close(*obj);
  761. return NULL;
  762. }
  763. ebpf_update_legacy_map(*obj, em);
  764. if (bpf_object__load(*obj)) {
  765. netdata_log_error("ERROR: loading BPF object file failed %s\n", lpath);
  766. bpf_object__close(*obj);
  767. return NULL;
  768. }
  769. ebpf_update_maps(em, *obj);
  770. ebpf_update_legacy_controller(em, *obj);
  771. size_t count_programs = ebpf_count_programs(*obj);
  772. #ifdef NETDATA_INTERNAL_CHECKS
  773. netdata_log_info("eBPF program %s loaded with success!", lpath);
  774. #endif
  775. return ebpf_attach_programs(*obj, count_programs, em->names);
  776. }
  777. char *ebpf_find_symbol(char *search)
  778. {
  779. char filename[FILENAME_MAX + 1];
  780. char *ret = NULL;
  781. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, NETDATA_KALLSYMS);
  782. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  783. if(unlikely(!ff)) {
  784. netdata_log_error("Cannot open %s%s", netdata_configured_host_prefix, NETDATA_KALLSYMS);
  785. return ret;
  786. }
  787. ff = procfile_readall(ff);
  788. if(unlikely(!ff))
  789. return ret;
  790. unsigned long i, lines = procfile_lines(ff);
  791. size_t length = strlen(search);
  792. for(i = 0; i < lines ; i++) {
  793. char *cmp = procfile_lineword(ff, i,2);
  794. if (!strncmp(search, cmp, length)) {
  795. ret = strdupz(cmp);
  796. break;
  797. }
  798. }
  799. procfile_close(ff);
  800. return ret;
  801. }
  802. void ebpf_update_names(ebpf_specify_name_t *opt, ebpf_module_t *em)
  803. {
  804. int mode = em->mode;
  805. em->names = opt;
  806. size_t i = 0;
  807. while (opt[i].program_name) {
  808. opt[i].retprobe = (mode == MODE_RETURN);
  809. opt[i].optional = ebpf_find_symbol(opt[i].function_to_attach);
  810. i++;
  811. }
  812. }
  813. //----------------------------------------------------------------------------------------------------------------------
  814. void ebpf_mount_config_name(char *filename, size_t length, char *path, const char *config)
  815. {
  816. snprintf(filename, length, "%s/ebpf.d/%s", path, config);
  817. }
  818. int ebpf_load_config(struct config *config, char *filename)
  819. {
  820. return appconfig_load(config, filename, 0, NULL);
  821. }
  822. static netdata_run_mode_t ebpf_select_mode(char *mode)
  823. {
  824. if (!strcasecmp(mode,EBPF_CFG_LOAD_MODE_RETURN ))
  825. return MODE_RETURN;
  826. else if (!strcasecmp(mode, "dev"))
  827. return MODE_DEVMODE;
  828. return MODE_ENTRY;
  829. }
  830. static void ebpf_select_mode_string(char *output, size_t len, netdata_run_mode_t sel)
  831. {
  832. if (sel == MODE_RETURN)
  833. strncpyz(output, EBPF_CFG_LOAD_MODE_RETURN, len);
  834. else
  835. strncpyz(output, EBPF_CFG_LOAD_MODE_DEFAULT, len);
  836. }
  837. /**
  838. * Convert string to load mode
  839. *
  840. * Convert the string given as argument to value present in enum.
  841. *
  842. * @param str value read from configuration file.
  843. *
  844. * @return It returns the value to be used.
  845. */
  846. netdata_ebpf_load_mode_t epbf_convert_string_to_load_mode(char *str)
  847. {
  848. if (!strcasecmp(str, EBPF_CFG_CORE_PROGRAM))
  849. return EBPF_LOAD_CORE;
  850. else if (!strcasecmp(str, EBPF_CFG_LEGACY_PROGRAM))
  851. return EBPF_LOAD_LEGACY;
  852. return EBPF_LOAD_PLAY_DICE;
  853. }
  854. /**
  855. * Convert load mode to string
  856. *
  857. * @param mode value that will select the string
  858. *
  859. * @return It returns the string associated to mode.
  860. */
  861. static char *ebpf_convert_load_mode_to_string(netdata_ebpf_load_mode_t mode)
  862. {
  863. if (mode & EBPF_LOAD_CORE)
  864. return EBPF_CFG_CORE_PROGRAM;
  865. else if (mode & EBPF_LOAD_LEGACY)
  866. return EBPF_CFG_LEGACY_PROGRAM;
  867. return EBPF_CFG_DEFAULT_PROGRAM;
  868. }
  869. /**
  870. * Convert collect pid to string
  871. *
  872. * @param level value that will select the string
  873. *
  874. * @return It returns the string associated to level.
  875. */
  876. static char *ebpf_convert_collect_pid_to_string(netdata_apps_level_t level)
  877. {
  878. if (level == NETDATA_APPS_LEVEL_REAL_PARENT)
  879. return EBPF_CFG_PID_REAL_PARENT;
  880. else if (level == NETDATA_APPS_LEVEL_PARENT)
  881. return EBPF_CFG_PID_PARENT;
  882. else if (level == NETDATA_APPS_LEVEL_ALL)
  883. return EBPF_CFG_PID_ALL;
  884. return EBPF_CFG_PID_INTERNAL_USAGE;
  885. }
  886. /**
  887. * Convert string to apps level
  888. *
  889. * @param str the argument read from config files
  890. *
  891. * @return it returns the level associated to the string or default when it is a wrong value
  892. */
  893. netdata_apps_level_t ebpf_convert_string_to_apps_level(char *str)
  894. {
  895. if (!strcasecmp(str, EBPF_CFG_PID_REAL_PARENT))
  896. return NETDATA_APPS_LEVEL_REAL_PARENT;
  897. else if (!strcasecmp(str, EBPF_CFG_PID_PARENT))
  898. return NETDATA_APPS_LEVEL_PARENT;
  899. else if (!strcasecmp(str, EBPF_CFG_PID_ALL))
  900. return NETDATA_APPS_LEVEL_ALL;
  901. return NETDATA_APPS_NOT_SET;
  902. }
  903. /**
  904. * CO-RE type
  905. *
  906. * Select the preferential type of CO-RE
  907. *
  908. * @param str value read from configuration file.
  909. * @param lmode load mode used by collector.
  910. */
  911. netdata_ebpf_program_loaded_t ebpf_convert_core_type(char *str, netdata_run_mode_t lmode)
  912. {
  913. if (!strcasecmp(str, EBPF_CFG_ATTACH_TRACEPOINT))
  914. return EBPF_LOAD_TRACEPOINT;
  915. else if (!strcasecmp(str, EBPF_CFG_ATTACH_PROBE)) {
  916. return (lmode == MODE_ENTRY) ? EBPF_LOAD_PROBE : EBPF_LOAD_RETPROBE;
  917. }
  918. return EBPF_LOAD_TRAMPOLINE;
  919. }
  920. #ifdef LIBBPF_MAJOR_VERSION
  921. /**
  922. * Adjust Thread Load
  923. *
  924. * Adjust thread configuration according specified load.
  925. *
  926. * @param mod the main structure that will be adjusted.
  927. * @param file the btf file used with thread.
  928. */
  929. void ebpf_adjust_thread_load(ebpf_module_t *mod, struct btf *file)
  930. {
  931. if (!file) {
  932. mod->load &= ~EBPF_LOAD_CORE;
  933. mod->load |= EBPF_LOAD_LEGACY;
  934. } else if (mod->load == EBPF_LOAD_PLAY_DICE && file) {
  935. mod->load &= ~EBPF_LOAD_LEGACY;
  936. mod->load |= EBPF_LOAD_CORE;
  937. }
  938. }
  939. /**
  940. * Parse BTF file
  941. *
  942. * Parse a specific BTF file present on filesystem
  943. *
  944. * @param filename the file that will be parsed.
  945. *
  946. * @return It returns a pointer for the file on success and NULL otherwise.
  947. */
  948. struct btf *ebpf_parse_btf_file(const char *filename)
  949. {
  950. struct btf *bf = btf__parse(filename, NULL);
  951. if (libbpf_get_error(bf)) {
  952. fprintf(stderr, "Cannot parse btf file");
  953. btf__free(bf);
  954. return NULL;
  955. }
  956. return bf;
  957. }
  958. /**
  959. * Load default btf file
  960. *
  961. * Load the default BTF file on environment.
  962. *
  963. * @param path is the fullpath
  964. * @param filename is the file inside BTF path.
  965. */
  966. struct btf *ebpf_load_btf_file(char *path, char *filename)
  967. {
  968. char fullpath[PATH_MAX + 1];
  969. snprintfz(fullpath, PATH_MAX, "%s/%s", path, filename);
  970. struct btf *ret = ebpf_parse_btf_file(fullpath);
  971. if (!ret)
  972. netdata_log_info("Your environment does not have BTF file %s/%s. The plugin will work with 'legacy' code.",
  973. path, filename);
  974. return ret;
  975. }
  976. /**
  977. * Find BTF attach type
  978. *
  979. * Search type fr current btf file.
  980. *
  981. * @param file is the structure for the btf file already parsed.
  982. */
  983. static inline const struct btf_type *ebpf_find_btf_attach_type(struct btf *file)
  984. {
  985. int id = btf__find_by_name_kind(file, "bpf_attach_type", BTF_KIND_ENUM);
  986. if (id < 0) {
  987. fprintf(stderr, "Cannot find 'bpf_attach_type'");
  988. return NULL;
  989. }
  990. return btf__type_by_id(file, id);
  991. }
  992. /**
  993. * Is function inside BTF
  994. *
  995. * Look for a specific function inside the given BTF file.
  996. *
  997. * @param file is the structure for the btf file already parsed.
  998. * @param function is the function that we want to find.
  999. */
  1000. int ebpf_is_function_inside_btf(struct btf *file, char *function)
  1001. {
  1002. const struct btf_type *type = ebpf_find_btf_attach_type(file);
  1003. if (!type)
  1004. return -1;
  1005. const struct btf_enum *e = btf_enum(type);
  1006. int i, id;
  1007. for (id = -1, i = 0; i < btf_vlen(type); i++, e++) {
  1008. if (!strcmp(btf__name_by_offset(file, e->name_off), "BPF_TRACE_FENTRY")) {
  1009. id = btf__find_by_name_kind(file, function, BTF_KIND_FUNC);
  1010. break;
  1011. }
  1012. }
  1013. return (id > 0) ? 1 : 0;
  1014. }
  1015. #endif
  1016. /**
  1017. * Update target with configuration
  1018. *
  1019. * Update target load mode with value.
  1020. *
  1021. * @param em the module structure
  1022. * @param value value used to update.
  1023. */
  1024. static void ebpf_update_target_with_conf(ebpf_module_t *em, netdata_ebpf_program_loaded_t value)
  1025. {
  1026. netdata_ebpf_targets_t *targets = em->targets;
  1027. if (!targets) {
  1028. return;
  1029. }
  1030. int i = 0;
  1031. while (targets[i].name) {
  1032. targets[i].mode = value;
  1033. i++;
  1034. }
  1035. }
  1036. /**
  1037. * Select Load Mode
  1038. *
  1039. * Select the load mode according the given inputs.
  1040. *
  1041. * @param btf_file a pointer to the loaded btf file.
  1042. * @parma load current value.
  1043. * @param btf_file a pointer to the loaded btf file.
  1044. * @param is_rhf is Red Hat family?
  1045. *
  1046. * @return it returns the new load mode.
  1047. */
  1048. static netdata_ebpf_load_mode_t ebpf_select_load_mode(struct btf *btf_file, netdata_ebpf_load_mode_t load,
  1049. int kver, int is_rh)
  1050. {
  1051. #ifdef LIBBPF_MAJOR_VERSION
  1052. if ((load & EBPF_LOAD_CORE) || (load & EBPF_LOAD_PLAY_DICE)) {
  1053. // Quick fix for Oracle linux 8.x
  1054. load = (!btf_file || (is_rh && (kver >= NETDATA_EBPF_KERNEL_5_4 && kver < NETDATA_EBPF_KERNEL_5_5))) ?
  1055. EBPF_LOAD_LEGACY : EBPF_LOAD_CORE;
  1056. }
  1057. #else
  1058. load = EBPF_LOAD_LEGACY;
  1059. #endif
  1060. return load;
  1061. }
  1062. /**
  1063. * Update Module using config
  1064. *
  1065. * Update configuration for a specific thread.
  1066. *
  1067. * @param modules structure that will be updated
  1068. * @param origin specify the configuration file loaded
  1069. * @param btf_file a pointer to the loaded btf file.
  1070. * @param is_rhf is Red Hat family?
  1071. */
  1072. void ebpf_update_module_using_config(ebpf_module_t *modules, netdata_ebpf_load_mode_t origin, struct btf *btf_file,
  1073. int kver, int is_rh)
  1074. {
  1075. char default_value[EBPF_MAX_MODE_LENGTH + 1];
  1076. ebpf_select_mode_string(default_value, EBPF_MAX_MODE_LENGTH, modules->mode);
  1077. char *load_mode = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE, default_value);
  1078. modules->mode = ebpf_select_mode(load_mode);
  1079. modules->update_every = (int)appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION,
  1080. EBPF_CFG_UPDATE_EVERY, modules->update_every);
  1081. modules->apps_charts = appconfig_get_boolean(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
  1082. (int) (modules->apps_charts & NETDATA_EBPF_APPS_FLAG_YES));
  1083. modules->cgroup_charts = appconfig_get_boolean(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_CGROUP,
  1084. modules->cgroup_charts);
  1085. modules->pid_map_size = (uint32_t)appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_PID_SIZE,
  1086. modules->pid_map_size);
  1087. modules->lifetime = (uint32_t) appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION,
  1088. EBPF_CFG_LIFETIME, EBPF_DEFAULT_LIFETIME);
  1089. char *value = ebpf_convert_load_mode_to_string(modules->load & NETDATA_EBPF_LOAD_METHODS);
  1090. char *type_format = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_TYPE_FORMAT, value);
  1091. netdata_ebpf_load_mode_t load = epbf_convert_string_to_load_mode(type_format);
  1092. load = ebpf_select_load_mode(btf_file, load, kver, is_rh);
  1093. modules->load = origin | load;
  1094. char *core_attach = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_CORE_ATTACH, EBPF_CFG_ATTACH_TRAMPOLINE);
  1095. netdata_ebpf_program_loaded_t fill_lm = ebpf_convert_core_type(core_attach, modules->mode);
  1096. ebpf_update_target_with_conf(modules, fill_lm);
  1097. value = ebpf_convert_collect_pid_to_string(modules->apps_level);
  1098. char *collect_pid = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_COLLECT_PID, value);
  1099. modules->apps_level = ebpf_convert_string_to_apps_level(collect_pid);
  1100. modules->maps_per_core = appconfig_get_boolean(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_MAPS_PER_CORE,
  1101. modules->maps_per_core);
  1102. if (kver < NETDATA_EBPF_KERNEL_4_06)
  1103. modules->maps_per_core = CONFIG_BOOLEAN_NO;
  1104. #ifdef NETDATA_DEV_MODE
  1105. netdata_log_info("The thread %s was configured with: mode = %s; update every = %d; apps = %s; cgroup = %s; ebpf type format = %s; ebpf co-re tracing = %s; collect pid = %s; maps per core = %s, lifetime=%u",
  1106. modules->info.thread_name,
  1107. load_mode,
  1108. modules->update_every,
  1109. (modules->apps_charts)?"enabled":"disabled",
  1110. (modules->cgroup_charts)?"enabled":"disabled",
  1111. type_format,
  1112. core_attach,
  1113. collect_pid,
  1114. (modules->maps_per_core)?"enabled":"disabled",
  1115. modules->lifetime
  1116. );
  1117. #endif
  1118. }
  1119. /**
  1120. * Update module
  1121. *
  1122. * When this function is called, it will load the configuration file and after this
  1123. * it updates the global information of ebpf_module.
  1124. * If the module has specific configuration, this function will load it, but it will not
  1125. * update the variables.
  1126. *
  1127. * @param em the module structure
  1128. * @param btf_file a pointer to the loaded btf file.
  1129. * @param is_rhf is Red Hat family?
  1130. * @param kver the kernel version
  1131. */
  1132. void ebpf_update_module(ebpf_module_t *em, struct btf *btf_file, int kver, int is_rh)
  1133. {
  1134. char filename[FILENAME_MAX+1];
  1135. netdata_ebpf_load_mode_t origin;
  1136. ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_user_config_dir, em->config_file);
  1137. if (!ebpf_load_config(em->cfg, filename)) {
  1138. ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_stock_config_dir, em->config_file);
  1139. if (!ebpf_load_config(em->cfg, filename)) {
  1140. netdata_log_error("Cannot load the ebpf configuration file %s", em->config_file);
  1141. return;
  1142. }
  1143. // If user defined data globally, we will have here EBPF_LOADED_FROM_USER, we need to consider this, to avoid
  1144. // forcing users to configure thread by thread.
  1145. origin = (!(em->load & NETDATA_EBPF_LOAD_SOURCE)) ? EBPF_LOADED_FROM_STOCK : em->load & NETDATA_EBPF_LOAD_SOURCE;
  1146. } else
  1147. origin = EBPF_LOADED_FROM_USER;
  1148. ebpf_update_module_using_config(em, origin, btf_file, kver, is_rh);
  1149. }
  1150. /**
  1151. * Adjust Apps Cgroup
  1152. *
  1153. * Apps and cgroup has internal cleanup that needs attaching tracers to release_task, to avoid overload the function
  1154. * we will enable this integration by default, if and only if, we are running with trampolines.
  1155. *
  1156. * @param em a pointer to the main thread structure.
  1157. * @param mode is the mode used with different
  1158. */
  1159. void ebpf_adjust_apps_cgroup(ebpf_module_t *em, netdata_ebpf_program_loaded_t mode)
  1160. {
  1161. if ((em->load & EBPF_LOADED_FROM_STOCK) &&
  1162. (em->apps_charts || em->cgroup_charts) &&
  1163. mode != EBPF_LOAD_TRAMPOLINE) {
  1164. em->apps_charts = NETDATA_EBPF_APPS_FLAG_NO;
  1165. em->cgroup_charts = 0;
  1166. }
  1167. }
  1168. //----------------------------------------------------------------------------------------------------------------------
  1169. /**
  1170. * Load Address
  1171. *
  1172. * Helper used to get address from /proc/kallsym
  1173. *
  1174. * @param fa address structure
  1175. * @param fd file descriptor loaded inside kernel. If a negative value is given
  1176. * the function will load address and it won't update hash table.
  1177. */
  1178. void ebpf_load_addresses(ebpf_addresses_t *fa, int fd)
  1179. {
  1180. if (fa->addr)
  1181. return ;
  1182. procfile *ff = procfile_open("/proc/kallsyms", " \t:", PROCFILE_FLAG_DEFAULT);
  1183. if (!ff)
  1184. return;
  1185. ff = procfile_readall(ff);
  1186. if (!ff)
  1187. return;
  1188. fa->hash = simple_hash(fa->function);
  1189. size_t lines = procfile_lines(ff), l;
  1190. for(l = 0; l < lines ;l++) {
  1191. char *fcnt = procfile_lineword(ff, l, 2);
  1192. uint32_t hash = simple_hash(fcnt);
  1193. if (fa->hash == hash && !strcmp(fcnt, fa->function)) {
  1194. char *type = procfile_lineword(ff, l, 2);
  1195. fa->type = type[0];
  1196. if (fd > 0) {
  1197. char addr[128];
  1198. snprintf(addr, 127, "0x%s", procfile_lineword(ff, l, 0));
  1199. fa->addr = (unsigned long) strtoul(addr, NULL, 16);
  1200. uint32_t key = 0;
  1201. bpf_map_update_elem(fd, &key, &fa->addr, BPF_ANY);
  1202. } else
  1203. fa->addr = 1;
  1204. break;
  1205. }
  1206. }
  1207. procfile_close(ff);
  1208. }
  1209. //----------------------------------------------------------------------------------------------------------------------
  1210. /**
  1211. * Fill Algorithms
  1212. *
  1213. * Set one unique dimension for all vector position.
  1214. *
  1215. * @param algorithms the output vector
  1216. * @param length number of elements of algorithms vector
  1217. * @param algorithm algorithm used on charts.
  1218. */
  1219. void ebpf_fill_algorithms(int *algorithms, size_t length, int algorithm)
  1220. {
  1221. size_t i;
  1222. for (i = 0; i < length; i++) {
  1223. algorithms[i] = algorithm;
  1224. }
  1225. }
  1226. /**
  1227. * Fill Histogram dimension
  1228. *
  1229. * Fill the histogram dimension with the specified ranges
  1230. */
  1231. char **ebpf_fill_histogram_dimension(size_t maximum)
  1232. {
  1233. char *dimensions[] = { "us", "ms", "s"};
  1234. int previous_dim = 0, current_dim = 0;
  1235. uint32_t previous_level = 1000, current_level = 1000;
  1236. uint32_t previous_divisor = 1, current_divisor = 1;
  1237. uint32_t current = 1, previous = 0;
  1238. uint32_t selector;
  1239. char **out = callocz(maximum, sizeof(char *));
  1240. char range[128];
  1241. size_t end = maximum - 1;
  1242. for (selector = 0; selector < end; selector++) {
  1243. snprintf(range, 127, "%u%s->%u%s", previous/previous_divisor, dimensions[previous_dim],
  1244. current/current_divisor, dimensions[current_dim]);
  1245. out[selector] = strdupz(range);
  1246. previous = current;
  1247. current <<= 1;
  1248. if (previous_dim != 2 && previous > previous_level) {
  1249. previous_dim++;
  1250. previous_divisor *= 1000;
  1251. previous_level *= 1000;
  1252. }
  1253. if (current_dim != 2 && current > current_level) {
  1254. current_dim++;
  1255. current_divisor *= 1000;
  1256. current_level *= 1000;
  1257. }
  1258. }
  1259. snprintf(range, 127, "%u%s->+Inf", previous/previous_divisor, dimensions[previous_dim]);
  1260. out[selector] = strdupz(range);
  1261. return out;
  1262. }
  1263. /**
  1264. * Histogram dimension cleanup
  1265. *
  1266. * Cleanup dimensions allocated with function ebpf_fill_histogram_dimension
  1267. *
  1268. * @param ptr
  1269. * @param length
  1270. */
  1271. void ebpf_histogram_dimension_cleanup(char **ptr, size_t length)
  1272. {
  1273. size_t i;
  1274. for (i = 0; i < length; i++) {
  1275. freez(ptr[i]);
  1276. }
  1277. freez(ptr);
  1278. }
  1279. //----------------------------------------------------------------------------------------------------------------------
  1280. /**
  1281. * Open tracepoint path
  1282. *
  1283. * @param filename pointer to store the path
  1284. * @param length file length
  1285. * @param subsys is the name of your subsystem.
  1286. * @param eventname is the name of the event to trace.
  1287. * @param flags flags used with syscall open
  1288. *
  1289. * @return it returns a positive value on success and a negative otherwise.
  1290. */
  1291. static inline int ebpf_open_tracepoint_path(char *filename, size_t length, char *subsys, char *eventname, int flags)
  1292. {
  1293. snprintfz(filename, length, "%s/events/%s/%s/enable", NETDATA_DEBUGFS, subsys, eventname);
  1294. return open(filename, flags, 0);
  1295. }
  1296. /**
  1297. * Is tracepoint enabled
  1298. *
  1299. * Check whether the tracepoint is enabled.
  1300. *
  1301. * @param subsys is the name of your subsystem.
  1302. * @param eventname is the name of the event to trace.
  1303. *
  1304. * @return it returns 1 when it is enabled, 0 when it is disabled and -1 on error.
  1305. */
  1306. int ebpf_is_tracepoint_enabled(char *subsys, char *eventname)
  1307. {
  1308. char text[FILENAME_MAX + 1];
  1309. int fd = ebpf_open_tracepoint_path(text, FILENAME_MAX, subsys, eventname, O_RDONLY);
  1310. if (fd < 0) {
  1311. return -1;
  1312. }
  1313. ssize_t length = read(fd, text, 1);
  1314. if (length != 1) {
  1315. close(fd);
  1316. return -1;
  1317. }
  1318. close(fd);
  1319. return (text[0] == '1') ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
  1320. }
  1321. /**
  1322. * Change Tracing values
  1323. *
  1324. * Change value for specific tracepoint enabling or disabling it according value given.
  1325. *
  1326. * @param subsys is the name of your subsystem.
  1327. * @param eventname is the name of the event to trace.
  1328. * @param value a value to enable (1) or disable (0) a tracepoint.
  1329. *
  1330. * @return It returns 0 on success and -1 otherwise
  1331. */
  1332. static int ebpf_change_tracing_values(char *subsys, char *eventname, char *value)
  1333. {
  1334. if (strcmp("0", value) && strcmp("1", value)) {
  1335. netdata_log_error("Invalid value given to either enable or disable a tracepoint.");
  1336. return -1;
  1337. }
  1338. char filename[1024];
  1339. int fd = ebpf_open_tracepoint_path(filename, 1023, subsys, eventname, O_WRONLY);
  1340. if (fd < 0) {
  1341. return -1;
  1342. }
  1343. ssize_t written = write(fd, value, strlen(value));
  1344. if (written < 0) {
  1345. close(fd);
  1346. return -1;
  1347. }
  1348. close(fd);
  1349. return 0;
  1350. }
  1351. /**
  1352. * Enable tracing values
  1353. *
  1354. * Enable a tracepoint on a system
  1355. *
  1356. * @param subsys is the name of your subsystem.
  1357. * @param eventname is the name of the event to trace.
  1358. *
  1359. * @return It returns 0 on success and -1 otherwise
  1360. */
  1361. int ebpf_enable_tracing_values(char *subsys, char *eventname)
  1362. {
  1363. return ebpf_change_tracing_values(subsys, eventname, "1");
  1364. }
  1365. /**
  1366. * Disable tracing values
  1367. *
  1368. * Disable tracing points enabled by collector
  1369. *
  1370. * @param subsys is the name of your subsystem.
  1371. * @param eventname is the name of the event to trace.
  1372. *
  1373. * @return It returns 0 on success and -1 otherwise
  1374. */
  1375. int ebpf_disable_tracing_values(char *subsys, char *eventname)
  1376. {
  1377. return ebpf_change_tracing_values(subsys, eventname, "0");
  1378. }
  1379. /**
  1380. * Select PC prefix
  1381. *
  1382. * Identify the prefix to run on PC architecture.
  1383. *
  1384. * @return It returns 32 or 64 according to host arch.
  1385. */
  1386. static uint32_t ebpf_select_pc_prefix()
  1387. {
  1388. long counter = 1;
  1389. uint32_t i;
  1390. for (i = 0; i < 128; i++) {
  1391. counter <<= 1;
  1392. if (counter < 0)
  1393. break;
  1394. }
  1395. return counter;
  1396. }
  1397. /**
  1398. * Select Host Prefix
  1399. *
  1400. * Select prefix to syscall when host is running a kernel newer than 4.17.0
  1401. *
  1402. * @param output the vector to store data.
  1403. * @param length length of output vector.
  1404. * @param syscall the syscall that prefix will be attached;
  1405. * @param kver the current kernel version in format MAJOR*65536 + MINOR*256 + PATCH
  1406. */
  1407. void ebpf_select_host_prefix(char *output, size_t length, char *syscall, int kver)
  1408. {
  1409. if (kver < NETDATA_EBPF_KERNEL_4_17)
  1410. snprintfz(output, length, "sys_%s", syscall);
  1411. else {
  1412. uint32_t arch = ebpf_select_pc_prefix();
  1413. // Prefix selected according https://www.kernel.org/doc/html/latest/process/adding-syscalls.html
  1414. char *prefix = (arch == 32) ? "__ia32" : "__x64";
  1415. snprintfz(output, length, "%s_sys_%s", prefix, syscall);
  1416. }
  1417. }