ebpf_sync.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_sync.h"
  4. static char *sync_counter_dimension_name[NETDATA_SYNC_IDX_END] = { "sync", "syncfs", "msync", "fsync", "fdatasync",
  5. "sync_file_range" };
  6. static netdata_syscall_stat_t sync_counter_aggregated_data[NETDATA_SYNC_IDX_END];
  7. static netdata_publish_syscall_t sync_counter_publish_aggregated[NETDATA_SYNC_IDX_END];
  8. static netdata_idx_t sync_hash_values[NETDATA_SYNC_IDX_END];
  9. static ebpf_local_maps_t sync_maps[] = {{.name = "tbl_sync", .internal_input = NETDATA_SYNC_END,
  10. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  11. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  12. {.name = "tbl_syncfs", .internal_input = NETDATA_SYNC_END,
  13. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  14. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  15. {.name = "tbl_msync", .internal_input = NETDATA_SYNC_END,
  16. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  17. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  18. {.name = "tbl_fsync", .internal_input = NETDATA_SYNC_END,
  19. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  20. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  21. {.name = "tbl_fdatasync", .internal_input = NETDATA_SYNC_END,
  22. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  23. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  24. {.name = "tbl_syncfr", .internal_input = NETDATA_SYNC_END,
  25. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  26. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  27. {.name = NULL, .internal_input = 0, .user_input = 0,
  28. .type = NETDATA_EBPF_MAP_CONTROLLER,
  29. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED}};
  30. struct config sync_config = { .first_section = NULL,
  31. .last_section = NULL,
  32. .mutex = NETDATA_MUTEX_INITIALIZER,
  33. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  34. .rwlock = AVL_LOCK_INITIALIZER } };
  35. netdata_ebpf_targets_t sync_targets[] = { {.name = NETDATA_SYSCALLS_SYNC, .mode = EBPF_LOAD_TRAMPOLINE},
  36. {.name = NETDATA_SYSCALLS_SYNCFS, .mode = EBPF_LOAD_TRAMPOLINE},
  37. {.name = NETDATA_SYSCALLS_MSYNC, .mode = EBPF_LOAD_TRAMPOLINE},
  38. {.name = NETDATA_SYSCALLS_FSYNC, .mode = EBPF_LOAD_TRAMPOLINE},
  39. {.name = NETDATA_SYSCALLS_FDATASYNC, .mode = EBPF_LOAD_TRAMPOLINE},
  40. {.name = NETDATA_SYSCALLS_SYNC_FILE_RANGE, .mode = EBPF_LOAD_TRAMPOLINE},
  41. {.name = NULL, .mode = EBPF_LOAD_TRAMPOLINE}};
  42. #ifdef LIBBPF_MAJOR_VERSION
  43. /*****************************************************************
  44. *
  45. * BTF FUNCTIONS
  46. *
  47. *****************************************************************/
  48. /**
  49. * Disable probe
  50. *
  51. * Disable kprobe to use another method.
  52. *
  53. * @param obj is the main structure for bpf objects.
  54. */
  55. static inline void ebpf_sync_disable_probe(struct sync_bpf *obj)
  56. {
  57. bpf_program__set_autoload(obj->progs.netdata_sync_kprobe, false);
  58. }
  59. /**
  60. * Disable trampoline
  61. *
  62. * Disable trampoline to use another method.
  63. *
  64. * @param obj is the main structure for bpf objects.
  65. */
  66. static inline void ebpf_sync_disable_trampoline(struct sync_bpf *obj)
  67. {
  68. bpf_program__set_autoload(obj->progs.netdata_sync_fentry, false);
  69. }
  70. /**
  71. * Disable tracepoint
  72. *
  73. * Disable tracepoints according information given.
  74. *
  75. * @param obj object loaded
  76. * @param idx Which syscall will not be disabled
  77. */
  78. void ebpf_sync_disable_tracepoints(struct sync_bpf *obj, sync_syscalls_index_t idx)
  79. {
  80. if (idx != NETDATA_SYNC_SYNC_IDX)
  81. bpf_program__set_autoload(obj->progs.netdata_sync_entry, false);
  82. if (idx != NETDATA_SYNC_SYNCFS_IDX)
  83. bpf_program__set_autoload(obj->progs.netdata_syncfs_entry, false);
  84. if (idx != NETDATA_SYNC_MSYNC_IDX)
  85. bpf_program__set_autoload(obj->progs.netdata_msync_entry, false);
  86. if (idx != NETDATA_SYNC_FSYNC_IDX)
  87. bpf_program__set_autoload(obj->progs.netdata_fsync_entry, false);
  88. if (idx != NETDATA_SYNC_FDATASYNC_IDX)
  89. bpf_program__set_autoload(obj->progs.netdata_fdatasync_entry, false);
  90. if (idx != NETDATA_SYNC_SYNC_FILE_RANGE_IDX)
  91. bpf_program__set_autoload(obj->progs.netdata_sync_file_range_entry, false);
  92. }
  93. /**
  94. * Set hash tables
  95. *
  96. * Set the values for maps according the value given by kernel.
  97. *
  98. * @param obj is the main structure for bpf objects.
  99. * @param idx the index for the main structure
  100. */
  101. static void ebpf_sync_set_hash_tables(struct sync_bpf *obj, sync_syscalls_index_t idx)
  102. {
  103. sync_maps[idx].map_fd = bpf_map__fd(obj->maps.tbl_sync);
  104. }
  105. /**
  106. * Load and attach
  107. *
  108. * Load and attach the eBPF code in kernel.
  109. *
  110. * @param obj is the main structure for bpf objects.
  111. * @param em the structure with configuration
  112. * @param target the syscall that we are attaching a tracer.
  113. * @param idx the index for the main structure
  114. *
  115. * @return it returns 0 on success and -1 otherwise
  116. */
  117. static inline int ebpf_sync_load_and_attach(struct sync_bpf *obj, ebpf_module_t *em, char *target,
  118. sync_syscalls_index_t idx)
  119. {
  120. netdata_ebpf_targets_t *synct = em->targets;
  121. netdata_ebpf_program_loaded_t test = synct[NETDATA_SYNC_SYNC_IDX].mode;
  122. if (test == EBPF_LOAD_TRAMPOLINE) {
  123. ebpf_sync_disable_probe(obj);
  124. ebpf_sync_disable_tracepoints(obj, NETDATA_SYNC_IDX_END);
  125. bpf_program__set_attach_target(obj->progs.netdata_sync_fentry, 0,
  126. target);
  127. } else if (test == EBPF_LOAD_PROBE ||
  128. test == EBPF_LOAD_RETPROBE) {
  129. ebpf_sync_disable_tracepoints(obj, NETDATA_SYNC_IDX_END);
  130. ebpf_sync_disable_trampoline(obj);
  131. } else {
  132. ebpf_sync_disable_probe(obj);
  133. ebpf_sync_disable_trampoline(obj);
  134. ebpf_sync_disable_tracepoints(obj, idx);
  135. }
  136. int ret = sync_bpf__load(obj);
  137. if (!ret) {
  138. if (test != EBPF_LOAD_PROBE && test != EBPF_LOAD_RETPROBE) {
  139. ret = sync_bpf__attach(obj);
  140. } else {
  141. obj->links.netdata_sync_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_sync_kprobe,
  142. false, target);
  143. ret = (int)libbpf_get_error(obj->links.netdata_sync_kprobe);
  144. }
  145. if (!ret)
  146. ebpf_sync_set_hash_tables(obj, idx);
  147. }
  148. return ret;
  149. }
  150. #endif
  151. /*****************************************************************
  152. *
  153. * CLEANUP THREAD
  154. *
  155. *****************************************************************/
  156. #ifdef LIBBPF_MAJOR_VERSION
  157. /**
  158. * Cleanup Objects
  159. *
  160. * Cleanup loaded objects when thread was initialized.
  161. */
  162. void ebpf_sync_cleanup_objects()
  163. {
  164. int i;
  165. for (i = 0; local_syscalls[i].syscall; i++) {
  166. ebpf_sync_syscalls_t *w = &local_syscalls[i];
  167. if (w->sync_obj)
  168. sync_bpf__destroy(w->sync_obj);
  169. }
  170. }
  171. #endif
  172. /**
  173. * Sync Free
  174. *
  175. * Cleanup variables after child threads to stop
  176. *
  177. * @param ptr thread data.
  178. */
  179. static void ebpf_sync_free(ebpf_module_t *em)
  180. {
  181. #ifdef LIBBPF_MAJOR_VERSION
  182. ebpf_sync_cleanup_objects();
  183. #endif
  184. pthread_mutex_lock(&ebpf_exit_cleanup);
  185. em->enabled = NETDATA_THREAD_EBPF_STOPPED;
  186. pthread_mutex_unlock(&ebpf_exit_cleanup);
  187. }
  188. /**
  189. * Exit
  190. *
  191. * Clean up the main thread.
  192. *
  193. * @param ptr thread data.
  194. */
  195. static void ebpf_sync_exit(void *ptr)
  196. {
  197. ebpf_module_t *em = (ebpf_module_t *)ptr;
  198. ebpf_sync_free(em);
  199. }
  200. /*****************************************************************
  201. *
  202. * INITIALIZE THREAD
  203. *
  204. *****************************************************************/
  205. /**
  206. * Load Legacy
  207. *
  208. * Load legacy code.
  209. *
  210. * @param w is the sync output structure with pointers to objects loaded.
  211. * @param em is structure with configuration
  212. *
  213. * @return 0 on success and -1 otherwise.
  214. */
  215. static int ebpf_sync_load_legacy(ebpf_sync_syscalls_t *w, ebpf_module_t *em)
  216. {
  217. em->thread_name = w->syscall;
  218. if (!w->probe_links) {
  219. w->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &w->objects);
  220. if (!w->probe_links) {
  221. return -1;
  222. }
  223. }
  224. return 0;
  225. }
  226. /*
  227. * Initialize Syscalls
  228. *
  229. * Load the eBPF programs to monitor syscalls
  230. *
  231. * @return 0 on success and -1 otherwise.
  232. */
  233. static int ebpf_sync_initialize_syscall(ebpf_module_t *em)
  234. {
  235. int i;
  236. const char *saved_name = em->thread_name;
  237. int errors = 0;
  238. for (i = 0; local_syscalls[i].syscall; i++) {
  239. ebpf_sync_syscalls_t *w = &local_syscalls[i];
  240. if (w->enabled) {
  241. if (em->load & EBPF_LOAD_LEGACY) {
  242. if (ebpf_sync_load_legacy(w, em))
  243. errors++;
  244. em->thread_name = saved_name;
  245. }
  246. #ifdef LIBBPF_MAJOR_VERSION
  247. else {
  248. char syscall[NETDATA_EBPF_MAX_SYSCALL_LENGTH];
  249. ebpf_select_host_prefix(syscall, NETDATA_EBPF_MAX_SYSCALL_LENGTH, w->syscall, running_on_kernel);
  250. w->sync_obj = sync_bpf__open();
  251. if (!w->sync_obj) {
  252. errors++;
  253. } else {
  254. if (ebpf_is_function_inside_btf(default_btf, syscall)) {
  255. if (ebpf_sync_load_and_attach(w->sync_obj, em, syscall, i)) {
  256. errors++;
  257. }
  258. } else {
  259. if (ebpf_sync_load_legacy(w, em))
  260. errors++;
  261. }
  262. em->thread_name = saved_name;
  263. }
  264. }
  265. #endif
  266. }
  267. }
  268. em->thread_name = saved_name;
  269. memset(sync_counter_aggregated_data, 0 , NETDATA_SYNC_IDX_END * sizeof(netdata_syscall_stat_t));
  270. memset(sync_counter_publish_aggregated, 0 , NETDATA_SYNC_IDX_END * sizeof(netdata_publish_syscall_t));
  271. memset(sync_hash_values, 0 , NETDATA_SYNC_IDX_END * sizeof(netdata_idx_t));
  272. return (errors) ? -1 : 0;
  273. }
  274. /*****************************************************************
  275. *
  276. * DATA THREAD
  277. *
  278. *****************************************************************/
  279. /**
  280. * Read global table
  281. *
  282. * Read the table with number of calls for all functions
  283. */
  284. static void ebpf_sync_read_global_table()
  285. {
  286. netdata_idx_t stored;
  287. uint32_t idx = NETDATA_SYNC_CALL;
  288. int i;
  289. for (i = 0; local_syscalls[i].syscall; i++) {
  290. if (local_syscalls[i].enabled) {
  291. int fd = sync_maps[i].map_fd;
  292. if (!bpf_map_lookup_elem(fd, &idx, &stored)) {
  293. sync_hash_values[i] = stored;
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * Create Sync charts
  300. *
  301. * Create charts and dimensions according user input.
  302. *
  303. * @param id chart id
  304. * @param idx the first index with data.
  305. * @param end the last index with data.
  306. */
  307. static void ebpf_send_sync_chart(char *id,
  308. int idx,
  309. int end)
  310. {
  311. write_begin_chart(NETDATA_EBPF_MEMORY_GROUP, id);
  312. netdata_publish_syscall_t *move = &sync_counter_publish_aggregated[idx];
  313. while (move && idx <= end) {
  314. if (local_syscalls[idx].enabled)
  315. write_chart_dimension(move->name, sync_hash_values[idx]);
  316. move = move->next;
  317. idx++;
  318. }
  319. write_end_chart();
  320. }
  321. /**
  322. * Send data
  323. *
  324. * Send global charts to Netdata
  325. */
  326. static void sync_send_data()
  327. {
  328. if (local_syscalls[NETDATA_SYNC_FSYNC_IDX].enabled || local_syscalls[NETDATA_SYNC_FDATASYNC_IDX].enabled) {
  329. ebpf_send_sync_chart(NETDATA_EBPF_FILE_SYNC_CHART, NETDATA_SYNC_FSYNC_IDX, NETDATA_SYNC_FDATASYNC_IDX);
  330. }
  331. if (local_syscalls[NETDATA_SYNC_MSYNC_IDX].enabled)
  332. ebpf_one_dimension_write_charts(NETDATA_EBPF_MEMORY_GROUP, NETDATA_EBPF_MSYNC_CHART,
  333. sync_counter_publish_aggregated[NETDATA_SYNC_MSYNC_IDX].dimension,
  334. sync_hash_values[NETDATA_SYNC_MSYNC_IDX]);
  335. if (local_syscalls[NETDATA_SYNC_SYNC_IDX].enabled || local_syscalls[NETDATA_SYNC_SYNCFS_IDX].enabled) {
  336. ebpf_send_sync_chart(NETDATA_EBPF_SYNC_CHART, NETDATA_SYNC_SYNC_IDX, NETDATA_SYNC_SYNCFS_IDX);
  337. }
  338. if (local_syscalls[NETDATA_SYNC_SYNC_FILE_RANGE_IDX].enabled)
  339. ebpf_one_dimension_write_charts(NETDATA_EBPF_MEMORY_GROUP, NETDATA_EBPF_FILE_SEGMENT_CHART,
  340. sync_counter_publish_aggregated[NETDATA_SYNC_SYNC_FILE_RANGE_IDX].dimension,
  341. sync_hash_values[NETDATA_SYNC_SYNC_FILE_RANGE_IDX]);
  342. }
  343. /**
  344. * Main loop for this collector.
  345. */
  346. static void sync_collector(ebpf_module_t *em)
  347. {
  348. heartbeat_t hb;
  349. heartbeat_init(&hb);
  350. int update_every = em->update_every;
  351. int counter = update_every - 1;
  352. while (!ebpf_exit_plugin) {
  353. (void)heartbeat_next(&hb, USEC_PER_SEC);
  354. if (ebpf_exit_plugin || ++counter != update_every)
  355. continue;
  356. counter = 0;
  357. ebpf_sync_read_global_table();
  358. pthread_mutex_lock(&lock);
  359. sync_send_data();
  360. pthread_mutex_unlock(&lock);
  361. }
  362. }
  363. /*****************************************************************
  364. *
  365. * MAIN THREAD
  366. *
  367. *****************************************************************/
  368. /**
  369. * Create Sync charts
  370. *
  371. * Create charts and dimensions according user input.
  372. *
  373. * @param id chart id
  374. * @param title chart title
  375. * @param order order number of the specified chart
  376. * @param idx the first index with data.
  377. * @param end the last index with data.
  378. * @param update_every value to overwrite the update frequency set by the server.
  379. */
  380. static void ebpf_create_sync_chart(char *id,
  381. char *title,
  382. int order,
  383. int idx,
  384. int end,
  385. int update_every)
  386. {
  387. ebpf_write_chart_cmd(NETDATA_EBPF_MEMORY_GROUP, id, title, EBPF_COMMON_DIMENSION_CALL,
  388. NETDATA_EBPF_SYNC_SUBMENU, NETDATA_EBPF_CHART_TYPE_LINE, NULL, order,
  389. update_every,
  390. NETDATA_EBPF_MODULE_NAME_SYNC);
  391. netdata_publish_syscall_t *move = &sync_counter_publish_aggregated[idx];
  392. while (move && idx <= end) {
  393. if (local_syscalls[idx].enabled)
  394. ebpf_write_global_dimension(move->name, move->dimension, move->algorithm);
  395. move = move->next;
  396. idx++;
  397. }
  398. }
  399. /**
  400. * Create global charts
  401. *
  402. * Call ebpf_create_chart to create the charts for the collector.
  403. *
  404. * @param update_every value to overwrite the update frequency set by the server.
  405. */
  406. static void ebpf_create_sync_charts(int update_every)
  407. {
  408. if (local_syscalls[NETDATA_SYNC_FSYNC_IDX].enabled || local_syscalls[NETDATA_SYNC_FDATASYNC_IDX].enabled)
  409. ebpf_create_sync_chart(NETDATA_EBPF_FILE_SYNC_CHART,
  410. "Monitor calls for <code>fsync(2)</code> and <code>fdatasync(2)</code>.", 21300,
  411. NETDATA_SYNC_FSYNC_IDX, NETDATA_SYNC_FDATASYNC_IDX, update_every);
  412. if (local_syscalls[NETDATA_SYNC_MSYNC_IDX].enabled)
  413. ebpf_create_sync_chart(NETDATA_EBPF_MSYNC_CHART,
  414. "Monitor calls for <code>msync(2)</code>.", 21301,
  415. NETDATA_SYNC_MSYNC_IDX, NETDATA_SYNC_MSYNC_IDX, update_every);
  416. if (local_syscalls[NETDATA_SYNC_SYNC_IDX].enabled || local_syscalls[NETDATA_SYNC_SYNCFS_IDX].enabled)
  417. ebpf_create_sync_chart(NETDATA_EBPF_SYNC_CHART,
  418. "Monitor calls for <code>sync(2)</code> and <code>syncfs(2)</code>.", 21302,
  419. NETDATA_SYNC_SYNC_IDX, NETDATA_SYNC_SYNCFS_IDX, update_every);
  420. if (local_syscalls[NETDATA_SYNC_SYNC_FILE_RANGE_IDX].enabled)
  421. ebpf_create_sync_chart(NETDATA_EBPF_FILE_SEGMENT_CHART,
  422. "Monitor calls for <code>sync_file_range(2)</code>.", 21303,
  423. NETDATA_SYNC_SYNC_FILE_RANGE_IDX, NETDATA_SYNC_SYNC_FILE_RANGE_IDX, update_every);
  424. }
  425. /**
  426. * Parse Syscalls
  427. *
  428. * Parse syscall options available inside ebpf.d/sync.conf
  429. */
  430. static void ebpf_sync_parse_syscalls()
  431. {
  432. int i;
  433. for (i = 0; local_syscalls[i].syscall; i++) {
  434. local_syscalls[i].enabled = appconfig_get_boolean(&sync_config, NETDATA_SYNC_CONFIG_NAME,
  435. local_syscalls[i].syscall, CONFIG_BOOLEAN_YES);
  436. }
  437. }
  438. /**
  439. * Sync thread
  440. *
  441. * Thread used to make sync thread
  442. *
  443. * @param ptr a pointer to `struct ebpf_module`
  444. *
  445. * @return It always return NULL
  446. */
  447. void *ebpf_sync_thread(void *ptr)
  448. {
  449. netdata_thread_cleanup_push(ebpf_sync_exit, ptr);
  450. ebpf_module_t *em = (ebpf_module_t *)ptr;
  451. em->maps = sync_maps;
  452. ebpf_sync_parse_syscalls();
  453. #ifdef LIBBPF_MAJOR_VERSION
  454. ebpf_adjust_thread_load(em, default_btf);
  455. #endif
  456. if (ebpf_sync_initialize_syscall(em)) {
  457. goto endsync;
  458. }
  459. int algorithms[NETDATA_SYNC_IDX_END] = { NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX,
  460. NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX,
  461. NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX };
  462. ebpf_global_labels(sync_counter_aggregated_data, sync_counter_publish_aggregated,
  463. sync_counter_dimension_name, sync_counter_dimension_name,
  464. algorithms, NETDATA_SYNC_IDX_END);
  465. pthread_mutex_lock(&lock);
  466. ebpf_create_sync_charts(em->update_every);
  467. ebpf_update_stats(&plugin_statistics, em);
  468. pthread_mutex_unlock(&lock);
  469. sync_collector(em);
  470. endsync:
  471. ebpf_update_disabled_plugin_stats(em);
  472. netdata_thread_cleanup_pop(1);
  473. return NULL;
  474. }