ebpf.c 47 KB

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