ebpf.c 47 KB

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