ebpf_mount.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_mount.h"
  4. static ebpf_local_maps_t mount_maps[] = {{.name = "tbl_mount", .internal_input = NETDATA_MOUNT_END,
  5. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  6. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  7. {.name = NULL, .internal_input = 0, .user_input = 0,
  8. .type = NETDATA_EBPF_MAP_CONTROLLER,
  9. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED}};
  10. static char *mount_dimension_name[NETDATA_EBPF_MOUNT_SYSCALL] = { "mount", "umount" };
  11. static netdata_syscall_stat_t mount_aggregated_data[NETDATA_EBPF_MOUNT_SYSCALL];
  12. static netdata_publish_syscall_t mount_publish_aggregated[NETDATA_EBPF_MOUNT_SYSCALL];
  13. struct config mount_config = { .first_section = NULL, .last_section = NULL, .mutex = NETDATA_MUTEX_INITIALIZER,
  14. .index = {.avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  15. .rwlock = AVL_LOCK_INITIALIZER } };
  16. static netdata_idx_t *mount_values = NULL;
  17. static netdata_idx_t mount_hash_values[NETDATA_MOUNT_END];
  18. struct netdata_static_thread mount_thread = {"MOUNT KERNEL",
  19. NULL, NULL, 1, NULL,
  20. NULL, NULL};
  21. netdata_ebpf_targets_t mount_targets[] = { {.name = "mount", .mode = EBPF_LOAD_TRAMPOLINE},
  22. {.name = "umount", .mode = EBPF_LOAD_TRAMPOLINE},
  23. {.name = NULL, .mode = EBPF_LOAD_TRAMPOLINE}};
  24. static enum ebpf_threads_status ebpf_mount_exited = NETDATA_THREAD_EBPF_RUNNING;
  25. #ifdef LIBBPF_MAJOR_VERSION
  26. #include "includes/mount.skel.h" // BTF code
  27. static struct mount_bpf *bpf_obj = NULL;
  28. /*****************************************************************
  29. *
  30. * BTF FUNCTIONS
  31. *
  32. *****************************************************************/
  33. /*
  34. * Disable probe
  35. *
  36. * Disable all probes to use exclusively another method.
  37. *
  38. * @param obj is the main structure for bpf objects.
  39. */
  40. static inline void ebpf_mount_disable_probe(struct mount_bpf *obj)
  41. {
  42. bpf_program__set_autoload(obj->progs.netdata_mount_probe, false);
  43. bpf_program__set_autoload(obj->progs.netdata_umount_probe, false);
  44. bpf_program__set_autoload(obj->progs.netdata_mount_retprobe, false);
  45. bpf_program__set_autoload(obj->progs.netdata_umount_retprobe, false);
  46. }
  47. /*
  48. * Disable tracepoint
  49. *
  50. * Disable all tracepoints to use exclusively another method.
  51. *
  52. * @param obj is the main structure for bpf objects.
  53. */
  54. static inline void ebpf_mount_disable_tracepoint(struct mount_bpf *obj)
  55. {
  56. bpf_program__set_autoload(obj->progs.netdata_mount_exit, false);
  57. bpf_program__set_autoload(obj->progs.netdata_umount_exit, false);
  58. }
  59. /*
  60. * Disable trampoline
  61. *
  62. * Disable all trampoline to use exclusively another method.
  63. *
  64. * @param obj is the main structure for bpf objects.
  65. */
  66. static inline void ebpf_mount_disable_trampoline(struct mount_bpf *obj)
  67. {
  68. bpf_program__set_autoload(obj->progs.netdata_mount_fentry, false);
  69. bpf_program__set_autoload(obj->progs.netdata_umount_fentry, false);
  70. bpf_program__set_autoload(obj->progs.netdata_mount_fexit, false);
  71. bpf_program__set_autoload(obj->progs.netdata_umount_fexit, false);
  72. }
  73. /**
  74. * Set trampoline target
  75. *
  76. * Set the targets we will monitor.
  77. *
  78. * @param obj is the main structure for bpf objects.
  79. */
  80. static inline void netdata_set_trampoline_target(struct mount_bpf *obj)
  81. {
  82. char syscall[NETDATA_EBPF_MAX_SYSCALL_LENGTH + 1];
  83. ebpf_select_host_prefix(syscall, NETDATA_EBPF_MAX_SYSCALL_LENGTH,
  84. mount_targets[NETDATA_MOUNT_SYSCALL].name, running_on_kernel);
  85. bpf_program__set_attach_target(obj->progs.netdata_mount_fentry, 0,
  86. syscall);
  87. bpf_program__set_attach_target(obj->progs.netdata_mount_fexit, 0,
  88. syscall);
  89. ebpf_select_host_prefix(syscall, NETDATA_EBPF_MAX_SYSCALL_LENGTH,
  90. mount_targets[NETDATA_UMOUNT_SYSCALL].name, running_on_kernel);
  91. bpf_program__set_attach_target(obj->progs.netdata_umount_fentry, 0,
  92. syscall);
  93. bpf_program__set_attach_target(obj->progs.netdata_umount_fexit, 0,
  94. syscall);
  95. }
  96. /**
  97. * Mount Attach Probe
  98. *
  99. * Attach probes to target
  100. *
  101. * @param obj is the main structure for bpf objects.
  102. *
  103. * @return It returns 0 on success and -1 otherwise.
  104. */
  105. static int ebpf_mount_attach_probe(struct mount_bpf *obj)
  106. {
  107. char syscall[NETDATA_EBPF_MAX_SYSCALL_LENGTH + 1];
  108. ebpf_select_host_prefix(syscall, NETDATA_EBPF_MAX_SYSCALL_LENGTH,
  109. mount_targets[NETDATA_MOUNT_SYSCALL].name, running_on_kernel);
  110. obj->links.netdata_mount_probe = bpf_program__attach_kprobe(obj->progs.netdata_mount_probe,
  111. false, syscall);
  112. int ret = (int)libbpf_get_error(obj->links.netdata_mount_probe);
  113. if (ret)
  114. return -1;
  115. obj->links.netdata_mount_retprobe = bpf_program__attach_kprobe(obj->progs.netdata_mount_retprobe,
  116. true, syscall);
  117. ret = (int)libbpf_get_error(obj->links.netdata_mount_retprobe);
  118. if (ret)
  119. return -1;
  120. ebpf_select_host_prefix(syscall, NETDATA_EBPF_MAX_SYSCALL_LENGTH,
  121. mount_targets[NETDATA_UMOUNT_SYSCALL].name, running_on_kernel);
  122. obj->links.netdata_umount_probe = bpf_program__attach_kprobe(obj->progs.netdata_umount_probe,
  123. false, syscall);
  124. ret = (int)libbpf_get_error(obj->links.netdata_umount_probe);
  125. if (ret)
  126. return -1;
  127. obj->links.netdata_umount_retprobe = bpf_program__attach_kprobe(obj->progs.netdata_umount_retprobe,
  128. true, syscall);
  129. ret = (int)libbpf_get_error(obj->links.netdata_umount_retprobe);
  130. if (ret)
  131. return -1;
  132. return 0;
  133. }
  134. /**
  135. * Set hash tables
  136. *
  137. * Set the values for maps according the value given by kernel.
  138. *
  139. * @param obj is the main structure for bpf objects.
  140. */
  141. static void ebpf_mount_set_hash_tables(struct mount_bpf *obj)
  142. {
  143. mount_maps[NETDATA_KEY_MOUNT_TABLE].map_fd = bpf_map__fd(obj->maps.tbl_mount);
  144. }
  145. /**
  146. * Load and attach
  147. *
  148. * Load and attach the eBPF code in kernel.
  149. *
  150. * @param obj is the main structure for bpf objects.
  151. * @param em structure with configuration
  152. *
  153. * @return it returns 0 on succes and -1 otherwise
  154. */
  155. static inline int ebpf_mount_load_and_attach(struct mount_bpf *obj, ebpf_module_t *em)
  156. {
  157. netdata_ebpf_targets_t *mt = em->targets;
  158. netdata_ebpf_program_loaded_t test = mt[NETDATA_MOUNT_SYSCALL].mode;
  159. // We are testing only one, because all will have the same behavior
  160. if (test == EBPF_LOAD_TRAMPOLINE ) {
  161. ebpf_mount_disable_probe(obj);
  162. ebpf_mount_disable_tracepoint(obj);
  163. netdata_set_trampoline_target(obj);
  164. } else if (test == EBPF_LOAD_PROBE ||
  165. test == EBPF_LOAD_RETPROBE ) {
  166. ebpf_mount_disable_tracepoint(obj);
  167. ebpf_mount_disable_trampoline(obj);
  168. } else {
  169. ebpf_mount_disable_probe(obj);
  170. ebpf_mount_disable_trampoline(obj);
  171. }
  172. int ret = mount_bpf__load(obj);
  173. if (!ret) {
  174. if (test != EBPF_LOAD_PROBE && test != EBPF_LOAD_RETPROBE )
  175. ret = mount_bpf__attach(obj);
  176. else
  177. ret = ebpf_mount_attach_probe(obj);
  178. if (!ret)
  179. ebpf_mount_set_hash_tables(obj);
  180. }
  181. return ret;
  182. }
  183. #endif
  184. /*****************************************************************
  185. *
  186. * FUNCTIONS TO CLOSE THE THREAD
  187. *
  188. *****************************************************************/
  189. /**
  190. * Mount Exit
  191. *
  192. * Cancel child thread.
  193. *
  194. * @param ptr thread data.
  195. */
  196. static void ebpf_mount_exit(void *ptr)
  197. {
  198. ebpf_module_t *em = (ebpf_module_t *)ptr;
  199. if (!em->enabled) {
  200. em->enabled = NETDATA_MAIN_THREAD_EXITED;
  201. return;
  202. }
  203. ebpf_mount_exited = NETDATA_THREAD_EBPF_STOPPING;
  204. }
  205. /**
  206. * Mount cleanup
  207. *
  208. * Clean up allocated memory.
  209. *
  210. * @param ptr thread data.
  211. */
  212. static void ebpf_mount_cleanup(void *ptr)
  213. {
  214. ebpf_module_t *em = (ebpf_module_t *)ptr;
  215. if (ebpf_mount_exited != NETDATA_THREAD_EBPF_STOPPED)
  216. return;
  217. freez(mount_thread.thread);
  218. freez(mount_values);
  219. #ifdef LIBBPF_MAJOR_VERSION
  220. if (bpf_obj)
  221. mount_bpf__destroy(bpf_obj);
  222. #endif
  223. mount_thread.enabled = NETDATA_MAIN_THREAD_EXITED;
  224. em->enabled = NETDATA_MAIN_THREAD_EXITED;
  225. }
  226. /*****************************************************************
  227. *
  228. * MAIN LOOP
  229. *
  230. *****************************************************************/
  231. /**
  232. * Read global table
  233. *
  234. * Read the table with number of calls for all functions
  235. */
  236. static void read_global_table()
  237. {
  238. uint32_t idx;
  239. netdata_idx_t *val = mount_hash_values;
  240. netdata_idx_t *stored = mount_values;
  241. int fd = mount_maps[NETDATA_KEY_MOUNT_TABLE].map_fd;
  242. for (idx = NETDATA_KEY_MOUNT_CALL; idx < NETDATA_MOUNT_END; idx++) {
  243. if (!bpf_map_lookup_elem(fd, &idx, stored)) {
  244. int i;
  245. int end = ebpf_nprocs;
  246. netdata_idx_t total = 0;
  247. for (i = 0; i < end; i++)
  248. total += stored[i];
  249. val[idx] = total;
  250. }
  251. }
  252. }
  253. /**
  254. * Mount read hash
  255. *
  256. * This is the thread callback.
  257. * This thread is necessary, because we cannot freeze the whole plugin to read the data.
  258. *
  259. * @param ptr It is a NULL value for this thread.
  260. *
  261. * @return It always returns NULL.
  262. */
  263. void *ebpf_mount_read_hash(void *ptr)
  264. {
  265. netdata_thread_cleanup_push(ebpf_mount_cleanup, ptr);
  266. heartbeat_t hb;
  267. heartbeat_init(&hb);
  268. ebpf_module_t *em = (ebpf_module_t *)ptr;
  269. usec_t step = NETDATA_LATENCY_MOUNT_SLEEP_MS * em->update_every;
  270. //This will be cancelled by its parent
  271. while (ebpf_mount_exited == NETDATA_THREAD_EBPF_RUNNING) {
  272. usec_t dt = heartbeat_next(&hb, step);
  273. (void)dt;
  274. if (ebpf_mount_exited == NETDATA_THREAD_EBPF_STOPPING)
  275. break;
  276. read_global_table();
  277. }
  278. ebpf_mount_exited = NETDATA_THREAD_EBPF_STOPPED;
  279. netdata_thread_cleanup_pop(1);
  280. return NULL;
  281. }
  282. /**
  283. * Send data to Netdata calling auxiliary functions.
  284. */
  285. static void ebpf_mount_send_data()
  286. {
  287. int i, j;
  288. int end = NETDATA_EBPF_MOUNT_SYSCALL;
  289. for (i = NETDATA_KEY_MOUNT_CALL, j = NETDATA_KEY_MOUNT_ERROR; i < end; i++, j++) {
  290. mount_publish_aggregated[i].ncall = mount_hash_values[i];
  291. mount_publish_aggregated[i].nerr = mount_hash_values[j];
  292. }
  293. write_count_chart(NETDATA_EBPF_MOUNT_CALLS, NETDATA_EBPF_MOUNT_GLOBAL_FAMILY,
  294. mount_publish_aggregated, NETDATA_EBPF_MOUNT_SYSCALL);
  295. write_err_chart(NETDATA_EBPF_MOUNT_ERRORS, NETDATA_EBPF_MOUNT_GLOBAL_FAMILY,
  296. mount_publish_aggregated, NETDATA_EBPF_MOUNT_SYSCALL);
  297. }
  298. /**
  299. * Main loop for this collector.
  300. */
  301. static void mount_collector(ebpf_module_t *em)
  302. {
  303. mount_thread.thread = mallocz(sizeof(netdata_thread_t));
  304. mount_thread.start_routine = ebpf_mount_read_hash;
  305. memset(mount_hash_values, 0, sizeof(mount_hash_values));
  306. mount_values = callocz((size_t)ebpf_nprocs, sizeof(netdata_idx_t));
  307. netdata_thread_create(mount_thread.thread, mount_thread.name, NETDATA_THREAD_OPTION_DEFAULT,
  308. ebpf_mount_read_hash, em);
  309. heartbeat_t hb;
  310. heartbeat_init(&hb);
  311. usec_t step = em->update_every * USEC_PER_SEC;
  312. while (!ebpf_exit_plugin) {
  313. (void)heartbeat_next(&hb, step);
  314. if (ebpf_exit_plugin)
  315. break;
  316. pthread_mutex_lock(&lock);
  317. ebpf_mount_send_data();
  318. pthread_mutex_unlock(&lock);
  319. }
  320. }
  321. /*****************************************************************
  322. *
  323. * INITIALIZE THREAD
  324. *
  325. *****************************************************************/
  326. /**
  327. * Create mount charts
  328. *
  329. * Call ebpf_create_chart to create the charts for the collector.
  330. *
  331. * @param update_every value to overwrite the update frequency set by the server.
  332. */
  333. static void ebpf_create_mount_charts(int update_every)
  334. {
  335. ebpf_create_chart(NETDATA_EBPF_MOUNT_GLOBAL_FAMILY, NETDATA_EBPF_MOUNT_CALLS,
  336. "Calls to mount and umount syscalls",
  337. EBPF_COMMON_DIMENSION_CALL, NETDATA_EBPF_MOUNT_FAMILY,
  338. NULL,
  339. NETDATA_EBPF_CHART_TYPE_LINE,
  340. NETDATA_CHART_PRIO_EBPF_MOUNT_CHARTS,
  341. ebpf_create_global_dimension,
  342. mount_publish_aggregated, NETDATA_EBPF_MOUNT_SYSCALL,
  343. update_every, NETDATA_EBPF_MODULE_NAME_MOUNT);
  344. ebpf_create_chart(NETDATA_EBPF_MOUNT_GLOBAL_FAMILY, NETDATA_EBPF_MOUNT_ERRORS,
  345. "Errors to mount and umount file systems",
  346. EBPF_COMMON_DIMENSION_CALL, NETDATA_EBPF_MOUNT_FAMILY,
  347. NULL,
  348. NETDATA_EBPF_CHART_TYPE_LINE,
  349. NETDATA_CHART_PRIO_EBPF_MOUNT_CHARTS + 1,
  350. ebpf_create_global_dimension,
  351. mount_publish_aggregated, NETDATA_EBPF_MOUNT_SYSCALL,
  352. update_every, NETDATA_EBPF_MODULE_NAME_MOUNT);
  353. fflush(stdout);
  354. }
  355. /*****************************************************************
  356. *
  357. * MAIN THREAD
  358. *
  359. *****************************************************************/
  360. /*
  361. * Load BPF
  362. *
  363. * Load BPF files.
  364. *
  365. * @param em the structure with configuration
  366. */
  367. static int ebpf_mount_load_bpf(ebpf_module_t *em)
  368. {
  369. int ret = 0;
  370. if (em->load == EBPF_LOAD_LEGACY) {
  371. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  372. if (!em->probe_links) {
  373. em->enabled = CONFIG_BOOLEAN_NO;
  374. ret = -1;
  375. }
  376. }
  377. #ifdef LIBBPF_MAJOR_VERSION
  378. else {
  379. bpf_obj = mount_bpf__open();
  380. if (!bpf_obj)
  381. ret = -1;
  382. else
  383. ret = ebpf_mount_load_and_attach(bpf_obj, em);
  384. }
  385. #endif
  386. if (ret)
  387. error("%s %s", EBPF_DEFAULT_ERROR_MSG, em->thread_name);
  388. return ret;
  389. }
  390. /**
  391. * Mount thread
  392. *
  393. * Thread used to make mount thread
  394. *
  395. * @param ptr a pointer to `struct ebpf_module`
  396. *
  397. * @return It always returns NULL
  398. */
  399. void *ebpf_mount_thread(void *ptr)
  400. {
  401. netdata_thread_cleanup_push(ebpf_mount_exit, ptr);
  402. ebpf_module_t *em = (ebpf_module_t *)ptr;
  403. em->maps = mount_maps;
  404. if (!em->enabled)
  405. goto endmount;
  406. #ifdef LIBBPF_MAJOR_VERSION
  407. ebpf_adjust_thread_load(em, default_btf);
  408. #endif
  409. if (ebpf_mount_load_bpf(em)) {
  410. em->enabled = CONFIG_BOOLEAN_NO;
  411. goto endmount;
  412. }
  413. int algorithms[NETDATA_EBPF_MOUNT_SYSCALL] = { NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX };
  414. ebpf_global_labels(mount_aggregated_data, mount_publish_aggregated, mount_dimension_name, mount_dimension_name,
  415. algorithms, NETDATA_EBPF_MOUNT_SYSCALL);
  416. pthread_mutex_lock(&lock);
  417. ebpf_create_mount_charts(em->update_every);
  418. ebpf_update_stats(&plugin_statistics, em);
  419. pthread_mutex_unlock(&lock);
  420. mount_collector(em);
  421. endmount:
  422. if (!em->enabled)
  423. ebpf_update_disabled_plugin_stats(em);
  424. netdata_thread_cleanup_pop(1);
  425. return NULL;
  426. }