ebpf.c 41 KB

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