ebpf.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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. debug(D_EXIT, "Cleaning parent process events.");
  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. 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. 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. */
  341. static void ebpf_stats_targets(ebpf_plugin_stats_t *report, netdata_ebpf_targets_t *targets)
  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++;
  352. break;
  353. }
  354. case EBPF_LOAD_RETPROBE: {
  355. report->retprobes++;
  356. break;
  357. }
  358. case EBPF_LOAD_TRACEPOINT: {
  359. report->tracepoints++;
  360. break;
  361. }
  362. case EBPF_LOAD_TRAMPOLINE: {
  363. report->trampolines++;
  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. report->threads++;
  383. // It is not necessary to report more information.
  384. if (em->enabled != NETDATA_THREAD_EBPF_RUNNING)
  385. return;
  386. report->running++;
  387. // In theory the `else if` is useless, because when this function is called, the module should not stay in
  388. // EBPF_LOAD_PLAY_DICE. We have this additional condition to detect errors from developers.
  389. if (em->load & EBPF_LOAD_LEGACY)
  390. report->legacy++;
  391. else if (em->load & EBPF_LOAD_CORE)
  392. report->core++;
  393. ebpf_stats_targets(report, em->targets);
  394. }
  395. /**
  396. * Update Kernel memory with memory
  397. *
  398. * This algorithm is an adaptation of https://elixir.bootlin.com/linux/v6.1.14/source/tools/bpf/bpftool/common.c#L402
  399. * to get 'memlock' data and update report.
  400. *
  401. * @param report the output structure
  402. * @param map pointer to a map.
  403. * @param action What action will be done with this map.
  404. */
  405. void ebpf_update_kernel_memory(ebpf_plugin_stats_t *report, ebpf_local_maps_t *map, ebpf_stats_action_t action)
  406. {
  407. char filename[FILENAME_MAX+1];
  408. snprintfz(filename, FILENAME_MAX, "/proc/self/fdinfo/%d", map->map_fd);
  409. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  410. if(unlikely(!ff)) {
  411. error("Cannot open %s", filename);
  412. return;
  413. }
  414. ff = procfile_readall(ff);
  415. if(unlikely(!ff))
  416. return;
  417. unsigned long j, lines = procfile_lines(ff);
  418. char *memlock = { "memlock" };
  419. for (j = 0; j < lines ; j++) {
  420. char *cmp = procfile_lineword(ff, j,0);
  421. if (!strncmp(memlock, cmp, 7)) {
  422. uint64_t memsize = (uint64_t) str2l(procfile_lineword(ff, j,1));
  423. switch (action) {
  424. case EBPF_ACTION_STAT_ADD: {
  425. report->memlock_kern += memsize;
  426. report->hash_tables += 1;
  427. #ifdef NETDATA_DEV_MODE
  428. info("Hash table %u: %s (FD = %d) is consuming %lu bytes totalizing %lu bytes",
  429. report->hash_tables, map->name, map->map_fd, memsize, report->memlock_kern);
  430. #endif
  431. break;
  432. }
  433. case EBPF_ACTION_STAT_REMOVE: {
  434. report->memlock_kern -= memsize;
  435. report->hash_tables -= 1;
  436. #ifdef NETDATA_DEV_MODE
  437. info("Hash table %s (FD = %d) was removed releasing %lu bytes, now we have %u tables loaded totalizing %lu bytes.",
  438. map->name, map->map_fd, memsize, report->hash_tables, report->memlock_kern);
  439. #endif
  440. break;
  441. }
  442. default: {
  443. break;
  444. }
  445. }
  446. break;
  447. }
  448. }
  449. procfile_close(ff);
  450. }
  451. /**
  452. * Update Kernel memory with memory
  453. *
  454. * This algorithm is an adaptation of https://elixir.bootlin.com/linux/v6.1.14/source/tools/bpf/bpftool/common.c#L402
  455. * to get 'memlock' data and update report.
  456. *
  457. * @param report the output structure
  458. * @param map pointer to a map. Last map must fish with name = NULL
  459. */
  460. void ebpf_update_kernel_memory_with_vector(ebpf_plugin_stats_t *report, ebpf_local_maps_t *maps)
  461. {
  462. if (!maps)
  463. return;
  464. ebpf_local_maps_t *map;
  465. int i = 0;
  466. for (map = &maps[i]; maps[i].name; i++, map = &maps[i]) {
  467. int fd = map->map_fd;
  468. if (fd == ND_EBPF_MAP_FD_NOT_INITIALIZED)
  469. continue;
  470. ebpf_update_kernel_memory(report, map, EBPF_ACTION_STAT_ADD);
  471. }
  472. }
  473. //----------------------------------------------------------------------------------------------------------------------
  474. void ebpf_update_pid_table(ebpf_local_maps_t *pid, ebpf_module_t *em)
  475. {
  476. pid->user_input = em->pid_map_size;
  477. }
  478. /**
  479. * Update map size
  480. *
  481. * Update map size with information read from configuration files.
  482. *
  483. * @param map the structure with file descriptor to update.
  484. * @param lmap the structure with information from configuration files.
  485. * @param em the structure with information about how the module/thread is working.
  486. * @param map_name the name of the file used to log.
  487. */
  488. void ebpf_update_map_size(struct bpf_map *map, ebpf_local_maps_t *lmap, ebpf_module_t *em, const char *map_name __maybe_unused)
  489. {
  490. uint32_t define_size = 0;
  491. uint32_t apps_type = NETDATA_EBPF_MAP_PID | NETDATA_EBPF_MAP_RESIZABLE;
  492. if (lmap->user_input && lmap->user_input != lmap->internal_input) {
  493. define_size = lmap->internal_input;
  494. #ifdef NETDATA_INTERNAL_CHECKS
  495. info("Changing map %s from size %u to %u ", map_name, lmap->internal_input, lmap->user_input);
  496. #endif
  497. } else if (((lmap->type & apps_type) == apps_type) && (!em->apps_charts) && (!em->cgroup_charts)) {
  498. lmap->user_input = ND_EBPF_DEFAULT_MIN_PID;
  499. } else if (((em->apps_charts) || (em->cgroup_charts)) && (em->apps_level != NETDATA_APPS_NOT_SET)) {
  500. switch (em->apps_level) {
  501. case NETDATA_APPS_LEVEL_ALL: {
  502. define_size = lmap->user_input;
  503. break;
  504. }
  505. case NETDATA_APPS_LEVEL_PARENT: {
  506. define_size = ND_EBPF_DEFAULT_PID_SIZE / 2;
  507. break;
  508. }
  509. case NETDATA_APPS_LEVEL_REAL_PARENT:
  510. default: {
  511. define_size = ND_EBPF_DEFAULT_PID_SIZE / 3;
  512. }
  513. }
  514. }
  515. if (!define_size)
  516. return;
  517. #ifdef LIBBPF_MAJOR_VERSION
  518. bpf_map__set_max_entries(map, define_size);
  519. #else
  520. bpf_map__resize(map, define_size);
  521. #endif
  522. }
  523. /**
  524. * Update Legacy map sizes
  525. *
  526. * Update map size for eBPF legacy code.
  527. *
  528. * @param program the structure with values read from binary.
  529. * @param em the structure with information about how the module/thread is working.
  530. */
  531. static void ebpf_update_legacy_map_sizes(struct bpf_object *program, ebpf_module_t *em)
  532. {
  533. struct bpf_map *map;
  534. ebpf_local_maps_t *maps = em->maps;
  535. if (!maps)
  536. return;
  537. bpf_map__for_each(map, program)
  538. {
  539. const char *map_name = bpf_map__name(map);
  540. int i = 0; ;
  541. while (maps[i].name) {
  542. ebpf_local_maps_t *w = &maps[i];
  543. if (w->type & NETDATA_EBPF_MAP_RESIZABLE) {
  544. if (!strcmp(w->name, map_name)) {
  545. ebpf_update_map_size(map, w, em, map_name);
  546. }
  547. }
  548. i++;
  549. }
  550. }
  551. }
  552. size_t ebpf_count_programs(struct bpf_object *obj)
  553. {
  554. size_t tot = 0;
  555. struct bpf_program *prog;
  556. bpf_object__for_each_program(prog, obj)
  557. {
  558. tot++;
  559. }
  560. return tot;
  561. }
  562. static ebpf_specify_name_t *ebpf_find_names(ebpf_specify_name_t *names, const char *prog_name)
  563. {
  564. size_t i = 0;
  565. while (names[i].program_name) {
  566. if (!strcmp(prog_name, names[i].program_name))
  567. return &names[i];
  568. i++;
  569. }
  570. return NULL;
  571. }
  572. static struct bpf_link **ebpf_attach_programs(struct bpf_object *obj, size_t length, ebpf_specify_name_t *names)
  573. {
  574. struct bpf_link **links = callocz(length , sizeof(struct bpf_link *));
  575. size_t i = 0;
  576. struct bpf_program *prog;
  577. ebpf_specify_name_t *w;
  578. bpf_object__for_each_program(prog, obj)
  579. {
  580. if (names) {
  581. const char *name = bpf_program__name(prog);
  582. w = ebpf_find_names(names, name);
  583. } else
  584. w = NULL;
  585. if (w) {
  586. enum bpf_prog_type type = bpf_program__get_type(prog);
  587. if (type == BPF_PROG_TYPE_KPROBE)
  588. links[i] = bpf_program__attach_kprobe(prog, w->retprobe, w->optional);
  589. } else
  590. links[i] = bpf_program__attach(prog);
  591. if (libbpf_get_error(links[i])) {
  592. links[i] = NULL;
  593. }
  594. i++;
  595. }
  596. return links;
  597. }
  598. static void ebpf_update_maps(ebpf_module_t *em, struct bpf_object *obj)
  599. {
  600. if (!em->maps)
  601. return;
  602. ebpf_local_maps_t *maps = em->maps;
  603. struct bpf_map *map;
  604. bpf_map__for_each(map, obj)
  605. {
  606. int fd = bpf_map__fd(map);
  607. if (maps) {
  608. const char *map_name = bpf_map__name(map);
  609. int j = 0; ;
  610. while (maps[j].name) {
  611. ebpf_local_maps_t *w = &maps[j];
  612. if (w->map_fd == ND_EBPF_MAP_FD_NOT_INITIALIZED && !strcmp(map_name, w->name))
  613. w->map_fd = fd;
  614. j++;
  615. }
  616. }
  617. }
  618. }
  619. /**
  620. * Update Controller
  621. *
  622. * Update controller value with user input.
  623. *
  624. * @param fd the table file descriptor
  625. * @param em structure with information about eBPF program we will load.
  626. */
  627. void ebpf_update_controller(int fd, ebpf_module_t *em)
  628. {
  629. uint32_t values[NETDATA_CONTROLLER_END] = {
  630. (em->apps_charts & NETDATA_EBPF_APPS_FLAG_YES) | em->cgroup_charts,
  631. em->apps_level
  632. };
  633. uint32_t key;
  634. uint32_t end = (em->apps_level != NETDATA_APPS_NOT_SET) ? NETDATA_CONTROLLER_END : NETDATA_CONTROLLER_APPS_LEVEL;
  635. for (key = NETDATA_CONTROLLER_APPS_ENABLED; key < end; key++) {
  636. int ret = bpf_map_update_elem(fd, &key, &values[key], 0);
  637. if (ret)
  638. error("Add key(%u) for controller table failed.", key);
  639. }
  640. }
  641. /**
  642. * Update Legacy controller
  643. *
  644. * Update legacy controller table when eBPF program has it.
  645. *
  646. * @param em structure with information about eBPF program we will load.
  647. * @param obj bpf object with tables.
  648. */
  649. static void ebpf_update_legacy_controller(ebpf_module_t *em, struct bpf_object *obj)
  650. {
  651. ebpf_local_maps_t *maps = em->maps;
  652. if (!maps)
  653. return;
  654. struct bpf_map *map;
  655. bpf_map__for_each(map, obj)
  656. {
  657. size_t i = 0;
  658. while (maps[i].name) {
  659. ebpf_local_maps_t *w = &maps[i];
  660. if (w->map_fd != ND_EBPF_MAP_FD_NOT_INITIALIZED && (w->type & NETDATA_EBPF_MAP_CONTROLLER)) {
  661. w->type &= ~NETDATA_EBPF_MAP_CONTROLLER;
  662. w->type |= NETDATA_EBPF_MAP_CONTROLLER_UPDATED;
  663. ebpf_update_controller(w->map_fd, em);
  664. }
  665. i++;
  666. }
  667. }
  668. }
  669. /**
  670. * Load Program
  671. *
  672. * Load eBPF program into kernel
  673. *
  674. * @param plugins_dir directory where binary are stored
  675. * @param em structure with information about eBPF program we will load.
  676. * @param kver the kernel version according /usr/include/linux/version.h
  677. * @param is_rhf is a kernel from Red Hat Family?
  678. * @param obj structure where we will store object loaded.
  679. *
  680. * @return it returns a link for each target we associated an eBPF program.
  681. */
  682. struct bpf_link **ebpf_load_program(char *plugins_dir, ebpf_module_t *em, int kver, int is_rhf,
  683. struct bpf_object **obj)
  684. {
  685. char lpath[4096];
  686. uint32_t idx = ebpf_select_index(em->kernels, is_rhf, kver);
  687. ebpf_mount_name(lpath, 4095, plugins_dir, idx, em->thread_name, em->mode, is_rhf);
  688. // When this function is called ebpf.plugin is using legacy code, so we should reset the variable
  689. em->load &= ~ NETDATA_EBPF_LOAD_METHODS;
  690. em->load |= EBPF_LOAD_LEGACY;
  691. *obj = bpf_object__open_file(lpath, NULL);
  692. if (libbpf_get_error(obj)) {
  693. error("Cannot open BPF object %s", lpath);
  694. bpf_object__close(*obj);
  695. return NULL;
  696. }
  697. ebpf_update_legacy_map_sizes(*obj, em);
  698. if (bpf_object__load(*obj)) {
  699. error("ERROR: loading BPF object file failed %s\n", lpath);
  700. bpf_object__close(*obj);
  701. return NULL;
  702. }
  703. ebpf_update_maps(em, *obj);
  704. ebpf_update_legacy_controller(em, *obj);
  705. size_t count_programs = ebpf_count_programs(*obj);
  706. #ifdef NETDATA_INTERNAL_CHECKS
  707. info("eBPF program %s loaded with success!", lpath);
  708. #endif
  709. return ebpf_attach_programs(*obj, count_programs, em->names);
  710. }
  711. char *ebpf_find_symbol(char *search)
  712. {
  713. char filename[FILENAME_MAX + 1];
  714. char *ret = NULL;
  715. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, NETDATA_KALLSYMS);
  716. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  717. if(unlikely(!ff)) {
  718. error("Cannot open %s%s", netdata_configured_host_prefix, NETDATA_KALLSYMS);
  719. return ret;
  720. }
  721. ff = procfile_readall(ff);
  722. if(unlikely(!ff))
  723. return ret;
  724. unsigned long i, lines = procfile_lines(ff);
  725. size_t length = strlen(search);
  726. for(i = 0; i < lines ; i++) {
  727. char *cmp = procfile_lineword(ff, i,2);;
  728. if (!strncmp(search, cmp, length)) {
  729. ret = strdupz(cmp);
  730. break;
  731. }
  732. }
  733. procfile_close(ff);
  734. return ret;
  735. }
  736. void ebpf_update_names(ebpf_specify_name_t *opt, ebpf_module_t *em)
  737. {
  738. int mode = em->mode;
  739. em->names = opt;
  740. size_t i = 0;
  741. while (opt[i].program_name) {
  742. opt[i].retprobe = (mode == MODE_RETURN);
  743. opt[i].optional = ebpf_find_symbol(opt[i].function_to_attach);
  744. i++;
  745. }
  746. }
  747. //----------------------------------------------------------------------------------------------------------------------
  748. void ebpf_mount_config_name(char *filename, size_t length, char *path, const char *config)
  749. {
  750. snprintf(filename, length, "%s/ebpf.d/%s", path, config);
  751. }
  752. int ebpf_load_config(struct config *config, char *filename)
  753. {
  754. return appconfig_load(config, filename, 0, NULL);
  755. }
  756. static netdata_run_mode_t ebpf_select_mode(char *mode)
  757. {
  758. if (!strcasecmp(mode,EBPF_CFG_LOAD_MODE_RETURN ))
  759. return MODE_RETURN;
  760. else if (!strcasecmp(mode, "dev"))
  761. return MODE_DEVMODE;
  762. return MODE_ENTRY;
  763. }
  764. static void ebpf_select_mode_string(char *output, size_t len, netdata_run_mode_t sel)
  765. {
  766. if (sel == MODE_RETURN)
  767. strncpyz(output, EBPF_CFG_LOAD_MODE_RETURN, len);
  768. else
  769. strncpyz(output, EBPF_CFG_LOAD_MODE_DEFAULT, len);
  770. }
  771. /**
  772. * Convert string to load mode
  773. *
  774. * Convert the string given as argument to value present in enum.
  775. *
  776. * @param str value read from configuration file.
  777. *
  778. * @return It returns the value to be used.
  779. */
  780. netdata_ebpf_load_mode_t epbf_convert_string_to_load_mode(char *str)
  781. {
  782. if (!strcasecmp(str, EBPF_CFG_CORE_PROGRAM))
  783. return EBPF_LOAD_CORE;
  784. else if (!strcasecmp(str, EBPF_CFG_LEGACY_PROGRAM))
  785. return EBPF_LOAD_LEGACY;
  786. return EBPF_LOAD_PLAY_DICE;
  787. }
  788. /**
  789. * Convert load mode to string
  790. *
  791. * @param mode value that will select the string
  792. *
  793. * @return It returns the string associated to mode.
  794. */
  795. static char *ebpf_convert_load_mode_to_string(netdata_ebpf_load_mode_t mode)
  796. {
  797. if (mode & EBPF_LOAD_CORE)
  798. return EBPF_CFG_CORE_PROGRAM;
  799. else if (mode & EBPF_LOAD_LEGACY)
  800. return EBPF_CFG_LEGACY_PROGRAM;
  801. return EBPF_CFG_DEFAULT_PROGRAM;
  802. }
  803. /**
  804. * Convert collect pid to string
  805. *
  806. * @param level value that will select the string
  807. *
  808. * @return It returns the string associated to level.
  809. */
  810. static char *ebpf_convert_collect_pid_to_string(netdata_apps_level_t level)
  811. {
  812. if (level == NETDATA_APPS_LEVEL_REAL_PARENT)
  813. return EBPF_CFG_PID_REAL_PARENT;
  814. else if (level == NETDATA_APPS_LEVEL_PARENT)
  815. return EBPF_CFG_PID_PARENT;
  816. else if (level == NETDATA_APPS_LEVEL_ALL)
  817. return EBPF_CFG_PID_ALL;
  818. return EBPF_CFG_PID_INTERNAL_USAGE;
  819. }
  820. /**
  821. * Convert string to apps level
  822. *
  823. * @param str the argument read from config files
  824. *
  825. * @return it returns the level associated to the string or default when it is a wrong value
  826. */
  827. netdata_apps_level_t ebpf_convert_string_to_apps_level(char *str)
  828. {
  829. if (!strcasecmp(str, EBPF_CFG_PID_REAL_PARENT))
  830. return NETDATA_APPS_LEVEL_REAL_PARENT;
  831. else if (!strcasecmp(str, EBPF_CFG_PID_PARENT))
  832. return NETDATA_APPS_LEVEL_PARENT;
  833. else if (!strcasecmp(str, EBPF_CFG_PID_ALL))
  834. return NETDATA_APPS_LEVEL_ALL;
  835. return NETDATA_APPS_NOT_SET;
  836. }
  837. /**
  838. * CO-RE type
  839. *
  840. * Select the preferential type of CO-RE
  841. *
  842. * @param str value read from configuration file.
  843. * @param lmode load mode used by collector.
  844. */
  845. netdata_ebpf_program_loaded_t ebpf_convert_core_type(char *str, netdata_run_mode_t lmode)
  846. {
  847. if (!strcasecmp(str, EBPF_CFG_ATTACH_TRACEPOINT))
  848. return EBPF_LOAD_TRACEPOINT;
  849. else if (!strcasecmp(str, EBPF_CFG_ATTACH_PROBE)) {
  850. return (lmode == MODE_ENTRY) ? EBPF_LOAD_PROBE : EBPF_LOAD_RETPROBE;
  851. }
  852. return EBPF_LOAD_TRAMPOLINE;
  853. }
  854. #ifdef LIBBPF_MAJOR_VERSION
  855. /**
  856. * Adjust Thread Load
  857. *
  858. * Adjust thread configuration according specified load.
  859. *
  860. * @param mod the main structure that will be adjusted.
  861. * @param file the btf file used with thread.
  862. */
  863. void ebpf_adjust_thread_load(ebpf_module_t *mod, struct btf *file)
  864. {
  865. if (!file) {
  866. mod->load &= ~EBPF_LOAD_CORE;
  867. mod->load |= EBPF_LOAD_LEGACY;
  868. } else if (mod->load == EBPF_LOAD_PLAY_DICE && file) {
  869. mod->load &= ~EBPF_LOAD_LEGACY;
  870. mod->load |= EBPF_LOAD_CORE;
  871. }
  872. }
  873. /**
  874. * Parse BTF file
  875. *
  876. * Parse a specific BTF file present on filesystem
  877. *
  878. * @param filename the file that will be parsed.
  879. *
  880. * @return It returns a pointer for the file on success and NULL otherwise.
  881. */
  882. struct btf *ebpf_parse_btf_file(const char *filename)
  883. {
  884. struct btf *bf = btf__parse(filename, NULL);
  885. if (libbpf_get_error(bf)) {
  886. fprintf(stderr, "Cannot parse btf file");
  887. btf__free(bf);
  888. return NULL;
  889. }
  890. return bf;
  891. }
  892. /**
  893. * Load default btf file
  894. *
  895. * Load the default BTF file on environment.
  896. *
  897. * @param path is the fullpath
  898. * @param filename is the file inside BTF path.
  899. */
  900. struct btf *ebpf_load_btf_file(char *path, char *filename)
  901. {
  902. char fullpath[PATH_MAX + 1];
  903. snprintfz(fullpath, PATH_MAX, "%s/%s", path, filename);
  904. struct btf *ret = ebpf_parse_btf_file(fullpath);
  905. if (!ret)
  906. info("Your environment does not have BTF file %s/%s. The plugin will work with 'legacy' code.",
  907. path, filename);
  908. return ret;
  909. }
  910. /**
  911. * Find BTF attach type
  912. *
  913. * Search type fr current btf file.
  914. *
  915. * @param file is the structure for the btf file already parsed.
  916. */
  917. static inline const struct btf_type *ebpf_find_btf_attach_type(struct btf *file)
  918. {
  919. int id = btf__find_by_name_kind(file, "bpf_attach_type", BTF_KIND_ENUM);
  920. if (id < 0) {
  921. fprintf(stderr, "Cannot find 'bpf_attach_type'");
  922. return NULL;
  923. }
  924. return btf__type_by_id(file, id);
  925. }
  926. /**
  927. * Is function inside BTF
  928. *
  929. * Look for a specific function inside the given BTF file.
  930. *
  931. * @param file is the structure for the btf file already parsed.
  932. * @param function is the function that we want to find.
  933. */
  934. int ebpf_is_function_inside_btf(struct btf *file, char *function)
  935. {
  936. const struct btf_type *type = ebpf_find_btf_attach_type(file);
  937. if (!type)
  938. return -1;
  939. const struct btf_enum *e = btf_enum(type);
  940. int i, id;
  941. for (id = -1, i = 0; i < btf_vlen(type); i++, e++) {
  942. if (!strcmp(btf__name_by_offset(file, e->name_off), "BPF_TRACE_FENTRY")) {
  943. id = btf__find_by_name_kind(file, function, BTF_KIND_FUNC);
  944. break;
  945. }
  946. }
  947. return (id > 0) ? 1 : 0;
  948. }
  949. #endif
  950. /**
  951. * Update target with configuration
  952. *
  953. * Update target load mode with value.
  954. *
  955. * @param em the module structure
  956. * @param value value used to update.
  957. */
  958. static void ebpf_update_target_with_conf(ebpf_module_t *em, netdata_ebpf_program_loaded_t value)
  959. {
  960. netdata_ebpf_targets_t *targets = em->targets;
  961. if (!targets) {
  962. return;
  963. }
  964. int i = 0;
  965. while (targets[i].name) {
  966. targets[i].mode = value;
  967. i++;
  968. }
  969. }
  970. /**
  971. * Select Load Mode
  972. *
  973. * Select the load mode according the given inputs.
  974. *
  975. * @param btf_file a pointer to the loaded btf file.
  976. * @parma load current value.
  977. * @param btf_file a pointer to the loaded btf file.
  978. * @param is_rhf is Red Hat family?
  979. *
  980. * @return it returns the new load mode.
  981. */
  982. static netdata_ebpf_load_mode_t ebpf_select_load_mode(struct btf *btf_file, netdata_ebpf_load_mode_t load,
  983. int kver, int is_rh)
  984. {
  985. #ifdef LIBBPF_MAJOR_VERSION
  986. if ((load & EBPF_LOAD_CORE) || (load & EBPF_LOAD_PLAY_DICE)) {
  987. // Quick fix for Oracle linux 8.x
  988. load = (!btf_file || (is_rh && (kver >= NETDATA_EBPF_KERNEL_5_4 && kver < NETDATA_EBPF_KERNEL_5_5))) ?
  989. EBPF_LOAD_LEGACY : EBPF_LOAD_CORE;
  990. }
  991. #else
  992. load = EBPF_LOAD_LEGACY;
  993. #endif
  994. return load;
  995. }
  996. /**
  997. * Update Module using config
  998. *
  999. * Update configuration for a specific thread.
  1000. *
  1001. * @param modules structure that will be updated
  1002. * @param origin specify the configuration file loaded
  1003. * @param btf_file a pointer to the loaded btf file.
  1004. * @param is_rhf is Red Hat family?
  1005. */
  1006. void ebpf_update_module_using_config(ebpf_module_t *modules, netdata_ebpf_load_mode_t origin, struct btf *btf_file,
  1007. int kver, int is_rh)
  1008. {
  1009. char default_value[EBPF_MAX_MODE_LENGTH + 1];
  1010. ebpf_select_mode_string(default_value, EBPF_MAX_MODE_LENGTH, modules->mode);
  1011. char *value = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE, default_value);
  1012. modules->mode = ebpf_select_mode(value);
  1013. modules->update_every = (int)appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION,
  1014. EBPF_CFG_UPDATE_EVERY, modules->update_every);
  1015. modules->apps_charts = appconfig_get_boolean(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
  1016. (int) (modules->apps_charts & NETDATA_EBPF_APPS_FLAG_YES));
  1017. modules->cgroup_charts = appconfig_get_boolean(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_CGROUP,
  1018. modules->cgroup_charts);
  1019. modules->pid_map_size = (uint32_t)appconfig_get_number(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_PID_SIZE,
  1020. modules->pid_map_size);
  1021. value = ebpf_convert_load_mode_to_string(modules->load & NETDATA_EBPF_LOAD_METHODS);
  1022. value = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_TYPE_FORMAT, value);
  1023. netdata_ebpf_load_mode_t load = epbf_convert_string_to_load_mode(value);
  1024. load = ebpf_select_load_mode(btf_file, load, kver, is_rh);
  1025. modules->load = origin | load;
  1026. value = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_CORE_ATTACH, EBPF_CFG_ATTACH_TRAMPOLINE);
  1027. netdata_ebpf_program_loaded_t fill_lm = ebpf_convert_core_type(value, modules->mode);
  1028. ebpf_update_target_with_conf(modules, fill_lm);
  1029. value = ebpf_convert_collect_pid_to_string(modules->apps_level);
  1030. value = appconfig_get(modules->cfg, EBPF_GLOBAL_SECTION, EBPF_CFG_COLLECT_PID, value);
  1031. modules->apps_level = ebpf_convert_string_to_apps_level(value);
  1032. }
  1033. /**
  1034. * Update module
  1035. *
  1036. * When this function is called, it will load the configuration file and after this
  1037. * it updates the global information of ebpf_module.
  1038. * If the module has specific configuration, this function will load it, but it will not
  1039. * update the variables.
  1040. *
  1041. * @param em the module structure
  1042. * @param btf_file a pointer to the loaded btf file.
  1043. * @param is_rhf is Red Hat family?
  1044. * @param kver the kernel version
  1045. */
  1046. void ebpf_update_module(ebpf_module_t *em, struct btf *btf_file, int kver, int is_rh)
  1047. {
  1048. char filename[FILENAME_MAX+1];
  1049. netdata_ebpf_load_mode_t origin;
  1050. ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_user_config_dir, em->config_file);
  1051. if (!ebpf_load_config(em->cfg, filename)) {
  1052. ebpf_mount_config_name(filename, FILENAME_MAX, ebpf_stock_config_dir, em->config_file);
  1053. if (!ebpf_load_config(em->cfg, filename)) {
  1054. error("Cannot load the ebpf configuration file %s", em->config_file);
  1055. return;
  1056. }
  1057. // If user defined data globally, we will have here EBPF_LOADED_FROM_USER, we need to consider this, to avoid
  1058. // forcing users to configure thread by thread.
  1059. origin = (!(em->load & NETDATA_EBPF_LOAD_SOURCE)) ? EBPF_LOADED_FROM_STOCK : em->load & NETDATA_EBPF_LOAD_SOURCE;
  1060. } else
  1061. origin = EBPF_LOADED_FROM_USER;
  1062. ebpf_update_module_using_config(em, origin, btf_file, kver, is_rh);
  1063. }
  1064. /**
  1065. * Adjust Apps Cgroup
  1066. *
  1067. * Apps and cgroup has internal cleanup that needs attaching tracers to release_task, to avoid overload the function
  1068. * we will enable this integration by default, if and only if, we are running with trampolines.
  1069. *
  1070. * @param em a pointer to the main thread structure.
  1071. * @param mode is the mode used with different
  1072. */
  1073. void ebpf_adjust_apps_cgroup(ebpf_module_t *em, netdata_ebpf_program_loaded_t mode)
  1074. {
  1075. if ((em->load & EBPF_LOADED_FROM_STOCK) &&
  1076. (em->apps_charts || em->cgroup_charts) &&
  1077. mode != EBPF_LOAD_TRAMPOLINE) {
  1078. em->apps_charts = NETDATA_EBPF_APPS_FLAG_NO;
  1079. em->cgroup_charts = 0;
  1080. }
  1081. }
  1082. //----------------------------------------------------------------------------------------------------------------------
  1083. /**
  1084. * Load Address
  1085. *
  1086. * Helper used to get address from /proc/kallsym
  1087. *
  1088. * @param fa address structure
  1089. * @param fd file descriptor loaded inside kernel. If a negative value is given
  1090. * the function will load address and it won't update hash table.
  1091. */
  1092. void ebpf_load_addresses(ebpf_addresses_t *fa, int fd)
  1093. {
  1094. if (fa->addr)
  1095. return ;
  1096. procfile *ff = procfile_open("/proc/kallsyms", " \t:", PROCFILE_FLAG_DEFAULT);
  1097. if (!ff)
  1098. return;
  1099. ff = procfile_readall(ff);
  1100. if (!ff)
  1101. return;
  1102. fa->hash = simple_hash(fa->function);
  1103. size_t lines = procfile_lines(ff), l;
  1104. for(l = 0; l < lines ;l++) {
  1105. char *fcnt = procfile_lineword(ff, l, 2);
  1106. uint32_t hash = simple_hash(fcnt);
  1107. if (fa->hash == hash && !strcmp(fcnt, fa->function)) {
  1108. if (fd > 0) {
  1109. char addr[128];
  1110. snprintf(addr, 127, "0x%s", procfile_lineword(ff, l, 0));
  1111. fa->addr = (unsigned long) strtoul(addr, NULL, 16);
  1112. uint32_t key = 0;
  1113. bpf_map_update_elem(fd, &key, &fa->addr, BPF_ANY);
  1114. } else
  1115. fa->addr = 1;
  1116. break;
  1117. }
  1118. }
  1119. procfile_close(ff);
  1120. }
  1121. //----------------------------------------------------------------------------------------------------------------------
  1122. /**
  1123. * Fill Algorithms
  1124. *
  1125. * Set one unique dimension for all vector position.
  1126. *
  1127. * @param algorithms the output vector
  1128. * @param length number of elements of algorithms vector
  1129. * @param algorithm algorithm used on charts.
  1130. */
  1131. void ebpf_fill_algorithms(int *algorithms, size_t length, int algorithm)
  1132. {
  1133. size_t i;
  1134. for (i = 0; i < length; i++) {
  1135. algorithms[i] = algorithm;
  1136. }
  1137. }
  1138. /**
  1139. * Fill Histogram dimension
  1140. *
  1141. * Fill the histogram dimension with the specified ranges
  1142. */
  1143. char **ebpf_fill_histogram_dimension(size_t maximum)
  1144. {
  1145. char *dimensions[] = { "us", "ms", "s"};
  1146. int previous_dim = 0, current_dim = 0;
  1147. uint32_t previous_level = 1000, current_level = 1000;
  1148. uint32_t previous_divisor = 1, current_divisor = 1;
  1149. uint32_t current = 1, previous = 0;
  1150. uint32_t selector;
  1151. char **out = callocz(maximum, sizeof(char *));
  1152. char range[128];
  1153. size_t end = maximum - 1;
  1154. for (selector = 0; selector < end; selector++) {
  1155. snprintf(range, 127, "%u%s->%u%s", previous/previous_divisor, dimensions[previous_dim],
  1156. current/current_divisor, dimensions[current_dim]);
  1157. out[selector] = strdupz(range);
  1158. previous = current;
  1159. current <<= 1;
  1160. if (previous_dim != 2 && previous > previous_level) {
  1161. previous_dim++;
  1162. previous_divisor *= 1000;
  1163. previous_level *= 1000;
  1164. }
  1165. if (current_dim != 2 && current > current_level) {
  1166. current_dim++;
  1167. current_divisor *= 1000;
  1168. current_level *= 1000;
  1169. }
  1170. }
  1171. snprintf(range, 127, "%u%s->+Inf", previous/previous_divisor, dimensions[previous_dim]);
  1172. out[selector] = strdupz(range);
  1173. return out;
  1174. }
  1175. /**
  1176. * Histogram dimension cleanup
  1177. *
  1178. * Cleanup dimensions allocated with function ebpf_fill_histogram_dimension
  1179. *
  1180. * @param ptr
  1181. * @param length
  1182. */
  1183. void ebpf_histogram_dimension_cleanup(char **ptr, size_t length)
  1184. {
  1185. size_t i;
  1186. for (i = 0; i < length; i++) {
  1187. freez(ptr[i]);
  1188. }
  1189. freez(ptr);
  1190. }
  1191. //----------------------------------------------------------------------------------------------------------------------
  1192. /**
  1193. * Open tracepoint path
  1194. *
  1195. * @param filename pointer to store the path
  1196. * @param length file length
  1197. * @param subsys is the name of your subsystem.
  1198. * @param eventname is the name of the event to trace.
  1199. * @param flags flags used with syscall open
  1200. *
  1201. * @return it returns a positive value on success and a negative otherwise.
  1202. */
  1203. static inline int ebpf_open_tracepoint_path(char *filename, size_t length, char *subsys, char *eventname, int flags)
  1204. {
  1205. snprintfz(filename, length, "%s/events/%s/%s/enable", NETDATA_DEBUGFS, subsys, eventname);
  1206. return open(filename, flags, 0);
  1207. }
  1208. /**
  1209. * Is tracepoint enabled
  1210. *
  1211. * Check whether the tracepoint is enabled.
  1212. *
  1213. * @param subsys is the name of your subsystem.
  1214. * @param eventname is the name of the event to trace.
  1215. *
  1216. * @return it returns 1 when it is enabled, 0 when it is disabled and -1 on error.
  1217. */
  1218. int ebpf_is_tracepoint_enabled(char *subsys, char *eventname)
  1219. {
  1220. char text[FILENAME_MAX + 1];
  1221. int fd = ebpf_open_tracepoint_path(text, FILENAME_MAX, subsys, eventname, O_RDONLY);
  1222. if (fd < 0) {
  1223. return -1;
  1224. }
  1225. ssize_t length = read(fd, text, 1);
  1226. if (length != 1) {
  1227. close(fd);
  1228. return -1;
  1229. }
  1230. close(fd);
  1231. return (text[0] == '1') ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
  1232. }
  1233. /**
  1234. * Change Tracing values
  1235. *
  1236. * Change value for specific tracepoint enabling or disabling it according value given.
  1237. *
  1238. * @param subsys is the name of your subsystem.
  1239. * @param eventname is the name of the event to trace.
  1240. * @param value a value to enable (1) or disable (0) a tracepoint.
  1241. *
  1242. * @return It returns 0 on success and -1 otherwise
  1243. */
  1244. static int ebpf_change_tracing_values(char *subsys, char *eventname, char *value)
  1245. {
  1246. if (strcmp("0", value) && strcmp("1", value)) {
  1247. error("Invalid value given to either enable or disable a tracepoint.");
  1248. return -1;
  1249. }
  1250. char filename[1024];
  1251. int fd = ebpf_open_tracepoint_path(filename, 1023, subsys, eventname, O_WRONLY);
  1252. if (fd < 0) {
  1253. return -1;
  1254. }
  1255. ssize_t written = write(fd, value, strlen(value));
  1256. if (written < 0) {
  1257. close(fd);
  1258. return -1;
  1259. }
  1260. close(fd);
  1261. return 0;
  1262. }
  1263. /**
  1264. * Enable tracing values
  1265. *
  1266. * Enable a tracepoint on a system
  1267. *
  1268. * @param subsys is the name of your subsystem.
  1269. * @param eventname is the name of the event to trace.
  1270. *
  1271. * @return It returns 0 on success and -1 otherwise
  1272. */
  1273. int ebpf_enable_tracing_values(char *subsys, char *eventname)
  1274. {
  1275. return ebpf_change_tracing_values(subsys, eventname, "1");
  1276. }
  1277. /**
  1278. * Disable tracing values
  1279. *
  1280. * Disable tracing points enabled by collector
  1281. *
  1282. * @param subsys is the name of your subsystem.
  1283. * @param eventname is the name of the event to trace.
  1284. *
  1285. * @return It returns 0 on success and -1 otherwise
  1286. */
  1287. int ebpf_disable_tracing_values(char *subsys, char *eventname)
  1288. {
  1289. return ebpf_change_tracing_values(subsys, eventname, "0");
  1290. }
  1291. /**
  1292. * Select PC prefix
  1293. *
  1294. * Identify the prefix to run on PC architecture.
  1295. *
  1296. * @return It returns 32 or 64 according to host arch.
  1297. */
  1298. static uint32_t ebpf_select_pc_prefix()
  1299. {
  1300. long counter = 1;
  1301. uint32_t i;
  1302. for (i = 0; i < 128; i++) {
  1303. counter <<= 1;
  1304. if (counter < 0)
  1305. break;
  1306. }
  1307. return counter;
  1308. }
  1309. /**
  1310. * Select Host Prefix
  1311. *
  1312. * Select prefix to syscall when host is running a kernel newer than 4.17.0
  1313. *
  1314. * @param output the vector to store data.
  1315. * @param length length of output vector.
  1316. * @param syscall the syscall that prefix will be attached;
  1317. * @param kver the current kernel version in format MAJOR*65536 + MINOR*256 + PATCH
  1318. */
  1319. void ebpf_select_host_prefix(char *output, size_t length, char *syscall, int kver)
  1320. {
  1321. if (kver < NETDATA_EBPF_KERNEL_4_17)
  1322. snprintfz(output, length, "sys_%s", syscall);
  1323. else {
  1324. uint32_t arch = ebpf_select_pc_prefix();
  1325. // Prefix selected according https://www.kernel.org/doc/html/latest/process/adding-syscalls.html
  1326. char *prefix = (arch == 32) ? "__ia32" : "__x64";
  1327. snprintfz(output, length, "%s_sys_%s", prefix, syscall);
  1328. }
  1329. }