ebpf.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/time.h>
  3. #include <sys/resource.h>
  4. #include <ifaddrs.h>
  5. #include "ebpf.h"
  6. #include "ebpf_socket.h"
  7. /*****************************************************************
  8. *
  9. * FUNCTIONS USED BY NETDATA
  10. *
  11. *****************************************************************/
  12. // callback required by eval()
  13. int health_variable_lookup(const char *variable, uint32_t hash, struct rrdcalc *rc, calculated_number *result)
  14. {
  15. UNUSED(variable);
  16. UNUSED(hash);
  17. UNUSED(rc);
  18. UNUSED(result);
  19. return 0;
  20. };
  21. void send_statistics(const char *action, const char *action_result, const char *action_data)
  22. {
  23. UNUSED(action);
  24. UNUSED(action_result);
  25. UNUSED(action_data);
  26. }
  27. // callbacks required by popen()
  28. void signals_block(void){};
  29. void signals_unblock(void){};
  30. void signals_reset(void){};
  31. // required by get_system_cpus()
  32. char *netdata_configured_host_prefix = "";
  33. // callback required by fatal()
  34. void netdata_cleanup_and_exit(int ret)
  35. {
  36. exit(ret);
  37. }
  38. // ----------------------------------------------------------------------
  39. /*****************************************************************
  40. *
  41. * GLOBAL VARIABLES
  42. *
  43. *****************************************************************/
  44. char *ebpf_plugin_dir = PLUGINS_DIR;
  45. static char *ebpf_configured_log_dir = LOG_DIR;
  46. char *ebpf_algorithms[] = {"absolute", "incremental"};
  47. int update_every = 1;
  48. static int thread_finished = 0;
  49. int close_ebpf_plugin = 0;
  50. struct config collector_config = { .first_section = NULL,
  51. .last_section = NULL,
  52. .mutex = NETDATA_MUTEX_INITIALIZER,
  53. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  54. .rwlock = AVL_LOCK_INITIALIZER } };
  55. int running_on_kernel = 0;
  56. char kernel_string[64];
  57. int ebpf_nprocs;
  58. static int isrh;
  59. uint32_t finalized_threads = 1;
  60. pthread_mutex_t lock;
  61. pthread_mutex_t collect_data_mutex;
  62. pthread_cond_t collect_data_cond_var;
  63. ebpf_module_t ebpf_modules[] = {
  64. { .thread_name = "process", .config_name = "process", .enabled = 0, .start_routine = ebpf_process_thread,
  65. .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
  66. .optional = 0, .apps_routine = ebpf_process_create_apps_charts, .maps = NULL,
  67. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &process_config,
  68. .config_file = NETDATA_PROCESS_CONFIG_FILE},
  69. { .thread_name = "socket", .config_name = "socket", .enabled = 0, .start_routine = ebpf_socket_thread,
  70. .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
  71. .optional = 0, .apps_routine = ebpf_socket_create_apps_charts, .maps = NULL,
  72. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &socket_config,
  73. .config_file = NETDATA_NETWORK_CONFIG_FILE},
  74. { .thread_name = "cachestat", .config_name = "cachestat", .enabled = 0, .start_routine = ebpf_cachestat_thread,
  75. .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
  76. .optional = 0, .apps_routine = ebpf_cachestat_create_apps_charts, .maps = NULL,
  77. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &cachestat_config,
  78. .config_file = NETDATA_CACHESTAT_CONFIG_FILE},
  79. { .thread_name = "sync", .config_name = "sync", .enabled = 0, .start_routine = ebpf_sync_thread,
  80. .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
  81. .optional = 0, .apps_routine = NULL, .maps = NULL,
  82. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &sync_config,
  83. .config_file = NETDATA_SYNC_CONFIG_FILE},
  84. { .thread_name = "dc", .config_name = "dc", .enabled = 0, .start_routine = ebpf_dcstat_thread,
  85. .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
  86. .optional = 0, .apps_routine = ebpf_dcstat_create_apps_charts, .maps = NULL,
  87. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &dcstat_config,
  88. .config_file = NETDATA_DIRECTORY_DCSTAT_CONFIG_FILE},
  89. { .thread_name = "swap", .config_name = "swap", .enabled = 0, .start_routine = ebpf_swap_thread,
  90. .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
  91. .optional = 0, .apps_routine = ebpf_swap_create_apps_charts, .maps = NULL,
  92. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &swap_config,
  93. .config_file = NETDATA_DIRECTORY_SWAP_CONFIG_FILE},
  94. { .thread_name = "vfs", .config_name = "swap", .enabled = 0, .start_routine = ebpf_vfs_thread,
  95. .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
  96. .optional = 0, .apps_routine = ebpf_vfs_create_apps_charts, .maps = NULL,
  97. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &vfs_config,
  98. .config_file = NETDATA_DIRECTORY_VFS_CONFIG_FILE },
  99. { .thread_name = "filesystem", .config_name = "filesystem", .enabled = 0, .start_routine = ebpf_filesystem_thread,
  100. .update_time = 1, .global_charts = 1, .apps_charts = 1, .mode = MODE_ENTRY,
  101. .optional = 0, .apps_routine = NULL, .maps = NULL,
  102. .pid_map_size = ND_EBPF_DEFAULT_PID_SIZE, .names = NULL, .cfg = &fs_config,
  103. .config_file = NETDATA_SYNC_CONFIG_FILE},
  104. { .thread_name = NULL, .enabled = 0, .start_routine = NULL, .update_time = 1,
  105. .global_charts = 0, .apps_charts = 1, .mode = MODE_ENTRY,
  106. .optional = 0, .apps_routine = NULL, .maps = NULL, .pid_map_size = 0, .names = NULL,
  107. .cfg = NULL, .config_name = NULL},
  108. };
  109. // Link with apps.plugin
  110. ebpf_process_stat_t *global_process_stat = NULL;
  111. //Network viewer
  112. ebpf_network_viewer_options_t network_viewer_opt;
  113. /*****************************************************************
  114. *
  115. * FUNCTIONS USED TO CLEAN MEMORY AND OPERATE SYSTEM FILES
  116. *
  117. *****************************************************************/
  118. /**
  119. * Clean Loaded Events
  120. *
  121. * This function cleans the events previous loaded on Linux.
  122. void clean_loaded_events()
  123. {
  124. int event_pid;
  125. for (event_pid = 0; ebpf_modules[event_pid].probes; event_pid++)
  126. clean_kprobe_events(NULL, (int)ebpf_modules[event_pid].thread_id, ebpf_modules[event_pid].probes);
  127. }
  128. */
  129. /**
  130. * Close the collector gracefully
  131. *
  132. * @param sig is the signal number used to close the collector
  133. */
  134. static void ebpf_exit(int sig)
  135. {
  136. close_ebpf_plugin = 1;
  137. // When both threads were not finished case I try to go in front this address, the collector will crash
  138. if (!thread_finished) {
  139. return;
  140. }
  141. if (ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled) {
  142. ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled = 0;
  143. clean_socket_apps_structures();
  144. freez(socket_bandwidth_curr);
  145. }
  146. if (ebpf_modules[EBPF_MODULE_CACHESTAT_IDX].enabled) {
  147. ebpf_modules[EBPF_MODULE_CACHESTAT_IDX].enabled = 0;
  148. clean_cachestat_pid_structures();
  149. freez(cachestat_pid);
  150. }
  151. if (ebpf_modules[EBPF_MODULE_DCSTAT_IDX].enabled) {
  152. ebpf_modules[EBPF_MODULE_DCSTAT_IDX].enabled = 0;
  153. clean_dcstat_pid_structures();
  154. freez(dcstat_pid);
  155. }
  156. if (ebpf_modules[EBPF_MODULE_SWAP_IDX].enabled) {
  157. ebpf_modules[EBPF_MODULE_SWAP_IDX].enabled = 0;
  158. clean_swap_pid_structures();
  159. freez(swap_pid);
  160. }
  161. if (ebpf_modules[EBPF_MODULE_VFS_IDX].enabled) {
  162. ebpf_modules[EBPF_MODULE_VFS_IDX].enabled = 0;
  163. clean_vfs_pid_structures();
  164. freez(vfs_pid);
  165. }
  166. /*
  167. int ret = fork();
  168. if (ret < 0) // error
  169. error("Cannot fork(), so I won't be able to clean %skprobe_events", NETDATA_DEBUGFS);
  170. else if (!ret) { // child
  171. int i;
  172. for (i = getdtablesize(); i >= 0; --i)
  173. close(i);
  174. int fd = open("/dev/null", O_RDWR, 0);
  175. if (fd != -1) {
  176. dup2(fd, STDIN_FILENO);
  177. dup2(fd, STDOUT_FILENO);
  178. dup2(fd, STDERR_FILENO);
  179. }
  180. if (fd > 2)
  181. close(fd);
  182. int sid = setsid();
  183. if (sid >= 0) {
  184. debug(D_EXIT, "Wait for father %d die", getpid());
  185. sleep_usec(200000); // Sleep 200 milliseconds to father dies.
  186. clean_loaded_events();
  187. } else {
  188. error("Cannot become session id leader, so I won't try to clean kprobe_events.\n");
  189. }
  190. } else { // parent
  191. exit(0);
  192. }
  193. */
  194. exit(sig);
  195. }
  196. /*****************************************************************
  197. *
  198. * FUNCTIONS TO CREATE CHARTS
  199. *
  200. *****************************************************************/
  201. /**
  202. * Get a value from a structure.
  203. *
  204. * @param basis it is the first address of the structure
  205. * @param offset it is the offset of the data you want to access.
  206. * @return
  207. */
  208. collected_number get_value_from_structure(char *basis, size_t offset)
  209. {
  210. collected_number *value = (collected_number *)(basis + offset);
  211. collected_number ret = (collected_number)llabs(*value);
  212. // this reset is necessary to avoid keep a constant value while processing is not executing a task
  213. *value = 0;
  214. return ret;
  215. }
  216. /**
  217. * Write begin command on standard output
  218. *
  219. * @param family the chart family name
  220. * @param name the chart name
  221. */
  222. void write_begin_chart(char *family, char *name)
  223. {
  224. printf("BEGIN %s.%s\n", family, name);
  225. }
  226. /**
  227. * Write END command on stdout.
  228. */
  229. inline void write_end_chart()
  230. {
  231. printf("END\n");
  232. }
  233. /**
  234. * Write set command on standard output
  235. *
  236. * @param dim the dimension name
  237. * @param value the value for the dimension
  238. */
  239. void write_chart_dimension(char *dim, long long value)
  240. {
  241. int ret = printf("SET %s = %lld\n", dim, value);
  242. UNUSED(ret);
  243. }
  244. /**
  245. * Call the necessary functions to create a chart.
  246. *
  247. * @param name the chart name
  248. * @param family the chart family
  249. * @param move the pointer with the values that will be published
  250. * @param end the number of values that will be written on standard output
  251. *
  252. * @return It returns a variable tha maps the charts that did not have zero values.
  253. */
  254. void write_count_chart(char *name, char *family, netdata_publish_syscall_t *move, uint32_t end)
  255. {
  256. write_begin_chart(family, name);
  257. uint32_t i = 0;
  258. while (move && i < end) {
  259. write_chart_dimension(move->name, move->ncall);
  260. move = move->next;
  261. i++;
  262. }
  263. write_end_chart();
  264. }
  265. /**
  266. * Call the necessary functions to create a chart.
  267. *
  268. * @param name the chart name
  269. * @param family the chart family
  270. * @param move the pointer with the values that will be published
  271. * @param end the number of values that will be written on standard output
  272. */
  273. void write_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end)
  274. {
  275. write_begin_chart(family, name);
  276. int i = 0;
  277. while (move && i < end) {
  278. write_chart_dimension(move->name, move->nerr);
  279. move = move->next;
  280. i++;
  281. }
  282. write_end_chart();
  283. }
  284. /**
  285. * Write charts
  286. *
  287. * Write the current information to publish the charts.
  288. *
  289. * @param family chart family
  290. * @param chart chart id
  291. * @param dim dimension name
  292. * @param v1 value.
  293. */
  294. void ebpf_one_dimension_write_charts(char *family, char *chart, char *dim, long long v1)
  295. {
  296. write_begin_chart(family, chart);
  297. write_chart_dimension(dim, v1);
  298. write_end_chart();
  299. }
  300. /**
  301. * Call the necessary functions to create a chart.
  302. *
  303. * @param chart the chart name
  304. * @param family the chart family
  305. * @param dwrite the dimension name
  306. * @param vwrite the value for previous dimension
  307. * @param dread the dimension name
  308. * @param vread the value for previous dimension
  309. *
  310. * @return It returns a variable tha maps the charts that did not have zero values.
  311. */
  312. void write_io_chart(char *chart, char *family, char *dwrite, long long vwrite, char *dread, long long vread)
  313. {
  314. write_begin_chart(family, chart);
  315. write_chart_dimension(dwrite, vwrite);
  316. write_chart_dimension(dread, vread);
  317. write_end_chart();
  318. }
  319. /**
  320. * Write chart cmd on standard output
  321. *
  322. * @param type chart type
  323. * @param id chart id
  324. * @param title chart title
  325. * @param units units label
  326. * @param family group name used to attach the chart on dashboard
  327. * @param charttype chart type
  328. * @param context chart context
  329. * @param order chart order
  330. */
  331. void ebpf_write_chart_cmd(char *type, char *id, char *title, char *units, char *family,
  332. char *charttype, char *context, int order)
  333. {
  334. printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d\n",
  335. type,
  336. id,
  337. title,
  338. units,
  339. (family)?family:"",
  340. (context)?context:"",
  341. (charttype)?charttype:"",
  342. order,
  343. update_every);
  344. }
  345. /**
  346. * Write chart cmd on standard output
  347. *
  348. * @param type chart type
  349. * @param id chart id
  350. * @param title chart title
  351. * @param units units label
  352. * @param family group name used to attach the chart on dashboard
  353. * @param charttype chart type
  354. * @param context chart context
  355. * @param order chart order
  356. */
  357. void ebpf_write_chart_obsolete(char *type, char *id, char *title, char *units, char *family,
  358. char *charttype, char *context, int order)
  359. {
  360. printf("CHART %s.%s '' '%s' '%s' '%s' '%s' '%s' %d %d 'obsolete'\n",
  361. type,
  362. id,
  363. title,
  364. units,
  365. (family)?family:"",
  366. (context)?context:"",
  367. (charttype)?charttype:"",
  368. order,
  369. update_every);
  370. }
  371. /**
  372. * Write the dimension command on standard output
  373. *
  374. * @param name the dimension name
  375. * @param id the dimension id
  376. * @param algo the dimension algorithm
  377. */
  378. void ebpf_write_global_dimension(char *name, char *id, char *algorithm)
  379. {
  380. printf("DIMENSION %s %s %s 1 1\n", name, id, algorithm);
  381. }
  382. /**
  383. * Call ebpf_write_global_dimension to create the dimensions for a specific chart
  384. *
  385. * @param ptr a pointer to a structure of the type netdata_publish_syscall_t
  386. * @param end the number of dimensions for the structure ptr
  387. */
  388. void ebpf_create_global_dimension(void *ptr, int end)
  389. {
  390. netdata_publish_syscall_t *move = ptr;
  391. int i = 0;
  392. while (move && i < end) {
  393. ebpf_write_global_dimension(move->name, move->dimension, move->algorithm);
  394. move = move->next;
  395. i++;
  396. }
  397. }
  398. /**
  399. * Call write_chart_cmd to create the charts
  400. *
  401. * @param type chart type
  402. * @param id chart id
  403. * @param title chart title
  404. * @param units axis label
  405. * @param family group name used to attach the chart on dashboard
  406. * @param context chart context
  407. * @param charttype chart type
  408. * @param order order number of the specified chart
  409. * @param ncd a pointer to a function called to create dimensions
  410. * @param move a pointer for a structure that has the dimensions
  411. * @param end number of dimensions for the chart created
  412. */
  413. void ebpf_create_chart(char *type,
  414. char *id,
  415. char *title,
  416. char *units,
  417. char *family,
  418. char *context,
  419. char *charttype,
  420. int order,
  421. void (*ncd)(void *, int),
  422. void *move,
  423. int end)
  424. {
  425. ebpf_write_chart_cmd(type, id, title, units, family, charttype, context, order);
  426. ncd(move, end);
  427. }
  428. /**
  429. * Create charts on apps submenu
  430. *
  431. * @param id the chart id
  432. * @param title the value displayed on vertical axis.
  433. * @param units the value displayed on vertical axis.
  434. * @param family Submenu that the chart will be attached on dashboard.
  435. * @param charttype chart type
  436. * @param order the chart order
  437. * @param algorithm the algorithm used by dimension
  438. * @param root structure used to create the dimensions.
  439. */
  440. void ebpf_create_charts_on_apps(char *id, char *title, char *units, char *family, char *charttype, int order,
  441. char *algorithm, struct target *root)
  442. {
  443. struct target *w;
  444. ebpf_write_chart_cmd(NETDATA_APPS_FAMILY, id, title, units, family, charttype, NULL, order);
  445. for (w = root; w; w = w->next) {
  446. if (unlikely(w->exposed))
  447. fprintf(stdout, "DIMENSION %s '' %s 1 1\n", w->name, algorithm);
  448. }
  449. }
  450. /*****************************************************************
  451. *
  452. * FUNCTIONS TO DEFINE OPTIONS
  453. *
  454. *****************************************************************/
  455. /**
  456. * Define labels used to generate charts
  457. *
  458. * @param is structure with information about number of calls made for a function.
  459. * @param pio structure used to generate charts.
  460. * @param dim a pointer for the dimensions name
  461. * @param name a pointer for the tensor with the name of the functions.
  462. * @param algorithm a vector with the algorithms used to make the charts
  463. * @param end the number of elements in the previous 4 arguments.
  464. */
  465. void ebpf_global_labels(netdata_syscall_stat_t *is, netdata_publish_syscall_t *pio, char **dim,
  466. char **name, int *algorithm, int end)
  467. {
  468. int i;
  469. netdata_syscall_stat_t *prev = NULL;
  470. netdata_publish_syscall_t *publish_prev = NULL;
  471. for (i = 0; i < end; i++) {
  472. if (prev) {
  473. prev->next = &is[i];
  474. }
  475. prev = &is[i];
  476. pio[i].dimension = dim[i];
  477. pio[i].name = name[i];
  478. pio[i].algorithm = strdupz(ebpf_algorithms[algorithm[i]]);
  479. if (publish_prev) {
  480. publish_prev->next = &pio[i];
  481. }
  482. publish_prev = &pio[i];
  483. }
  484. }
  485. /**
  486. * Define thread mode for all ebpf program.
  487. *
  488. * @param lmode the mode that will be used for them.
  489. */
  490. static inline void ebpf_set_thread_mode(netdata_run_mode_t lmode)
  491. {
  492. int i;
  493. for (i = 0; ebpf_modules[i].thread_name; i++) {
  494. ebpf_modules[i].mode = lmode;
  495. }
  496. }
  497. /**
  498. * Enable specific charts selected by user.
  499. *
  500. * @param em the structure that will be changed
  501. * @param enable the status about the apps charts.
  502. */
  503. static inline void ebpf_enable_specific_chart(struct ebpf_module *em, int enable)
  504. {
  505. em->enabled = 1;
  506. if (!enable) {
  507. em->apps_charts = 1;
  508. }
  509. em->global_charts = 1;
  510. }
  511. /**
  512. * Enable all charts
  513. *
  514. * @param apps what is the current status of apps
  515. */
  516. static inline void ebpf_enable_all_charts(int apps)
  517. {
  518. int i;
  519. for (i = 0; ebpf_modules[i].thread_name; i++) {
  520. ebpf_enable_specific_chart(&ebpf_modules[i], apps);
  521. }
  522. }
  523. /**
  524. * Enable the specified chart group
  525. *
  526. * @param idx the index of ebpf_modules that I am enabling
  527. * @param disable_apps should I keep apps charts?
  528. */
  529. static inline void ebpf_enable_chart(int idx, int disable_apps)
  530. {
  531. int i;
  532. for (i = 0; ebpf_modules[i].thread_name; i++) {
  533. if (i == idx) {
  534. ebpf_enable_specific_chart(&ebpf_modules[i], disable_apps);
  535. break;
  536. }
  537. }
  538. }
  539. /**
  540. * Disable APPs
  541. *
  542. * Disable charts for apps loading only global charts.
  543. */
  544. static inline void ebpf_disable_apps()
  545. {
  546. int i;
  547. for (i = 0; ebpf_modules[i].thread_name; i++) {
  548. ebpf_modules[i].apps_charts = 0;
  549. }
  550. }
  551. /**
  552. * Print help on standard error for user knows how to use the collector.
  553. */
  554. void ebpf_print_help()
  555. {
  556. const time_t t = time(NULL);
  557. struct tm ct;
  558. struct tm *test = localtime_r(&t, &ct);
  559. int year;
  560. if (test)
  561. year = ct.tm_year;
  562. else
  563. year = 0;
  564. fprintf(stderr,
  565. "\n"
  566. " Netdata ebpf.plugin %s\n"
  567. " Copyright (C) 2016-%d Costa Tsaousis <costa@tsaousis.gr>\n"
  568. " Released under GNU General Public License v3 or later.\n"
  569. " All rights reserved.\n"
  570. "\n"
  571. " This program is a data collector plugin for netdata.\n"
  572. "\n"
  573. " Available command line options:\n"
  574. "\n"
  575. " SECONDS Set the data collection frequency.\n"
  576. "\n"
  577. " --help or -h Show this help.\n"
  578. "\n"
  579. " --version or -v Show software version.\n"
  580. "\n"
  581. " --global or -g Disable charts per application.\n"
  582. "\n"
  583. " --all or -a Enable all chart groups (global and apps), unless -g is also given.\n"
  584. "\n"
  585. " --cachestat or -c Enable charts related to process run time.\n"
  586. "\n"
  587. " --dcstat or -d Enable charts related to directory cache.\n"
  588. "\n"
  589. " --filesystem or -i Enable chart related to filesystem run time.\n"
  590. "\n"
  591. " --net or -n Enable network viewer charts.\n"
  592. "\n"
  593. " --process or -p Enable charts related to process run time.\n"
  594. "\n"
  595. " --return or -r Run the collector in return mode.\n"
  596. "\n",
  597. " --sync or -s Enable chart related to sync run time.\n"
  598. "\n"
  599. " --swap or -w Enable chart related to swap run time.\n"
  600. "\n"
  601. " --vfs or -f Enable chart related to vfs run time.\n"
  602. "\n"
  603. VERSION,
  604. (year >= 116) ? year + 1900 : 2020);
  605. }
  606. /*****************************************************************
  607. *
  608. * AUXILIAR FUNCTIONS USED DURING INITIALIZATION
  609. *
  610. *****************************************************************/
  611. /**
  612. * Read Local Ports
  613. *
  614. * Parse /proc/net/{tcp,udp} and get the ports Linux is listening.
  615. *
  616. * @param filename the proc file to parse.
  617. * @param proto is the magic number associated to the protocol file we are reading.
  618. */
  619. static void read_local_ports(char *filename, uint8_t proto)
  620. {
  621. procfile *ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  622. if (!ff)
  623. return;
  624. ff = procfile_readall(ff);
  625. if (!ff)
  626. return;
  627. size_t lines = procfile_lines(ff), l;
  628. for(l = 0; l < lines ;l++) {
  629. size_t words = procfile_linewords(ff, l);
  630. // This is header or end of file
  631. if (unlikely(words < 14))
  632. continue;
  633. // https://elixir.bootlin.com/linux/v5.7.8/source/include/net/tcp_states.h
  634. // 0A = TCP_LISTEN
  635. if (strcmp("0A", procfile_lineword(ff, l, 5)))
  636. continue;
  637. // Read local port
  638. uint16_t port = (uint16_t)strtol(procfile_lineword(ff, l, 2), NULL, 16);
  639. update_listen_table(htons(port), proto);
  640. }
  641. procfile_close(ff);
  642. }
  643. /**
  644. * Read Local addresseses
  645. *
  646. * Read the local address from the interfaces.
  647. */
  648. static void read_local_addresses()
  649. {
  650. struct ifaddrs *ifaddr, *ifa;
  651. if (getifaddrs(&ifaddr) == -1) {
  652. error("Cannot get the local IP addresses, it is no possible to do separation between inbound and outbound connections");
  653. return;
  654. }
  655. char *notext = { "No text representation" };
  656. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  657. if (ifa->ifa_addr == NULL)
  658. continue;
  659. if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6))
  660. continue;
  661. ebpf_network_viewer_ip_list_t *w = callocz(1, sizeof(ebpf_network_viewer_ip_list_t));
  662. int family = ifa->ifa_addr->sa_family;
  663. w->ver = (uint8_t) family;
  664. char text[INET6_ADDRSTRLEN];
  665. if (family == AF_INET) {
  666. struct sockaddr_in *in = (struct sockaddr_in*) ifa->ifa_addr;
  667. w->first.addr32[0] = in->sin_addr.s_addr;
  668. w->last.addr32[0] = in->sin_addr.s_addr;
  669. if (inet_ntop(AF_INET, w->first.addr8, text, INET_ADDRSTRLEN)) {
  670. w->value = strdupz(text);
  671. w->hash = simple_hash(text);
  672. } else {
  673. w->value = strdupz(notext);
  674. w->hash = simple_hash(notext);
  675. }
  676. } else {
  677. struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa->ifa_addr;
  678. memcpy(w->first.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  679. memcpy(w->last.addr8, (void *)&in6->sin6_addr, sizeof(struct in6_addr));
  680. if (inet_ntop(AF_INET6, w->first.addr8, text, INET_ADDRSTRLEN)) {
  681. w->value = strdupz(text);
  682. w->hash = simple_hash(text);
  683. } else {
  684. w->value = strdupz(notext);
  685. w->hash = simple_hash(notext);
  686. }
  687. }
  688. fill_ip_list((family == AF_INET)?&network_viewer_opt.ipv4_local_ip:&network_viewer_opt.ipv6_local_ip,
  689. w,
  690. "selector");
  691. }
  692. freeifaddrs(ifaddr);
  693. }
  694. /**
  695. * Start Pthread Variable
  696. *
  697. * This function starts all pthread variables.
  698. *
  699. * @return It returns 0 on success and -1.
  700. */
  701. int ebpf_start_pthread_variables()
  702. {
  703. pthread_mutex_init(&lock, NULL);
  704. pthread_mutex_init(&collect_data_mutex, NULL);
  705. if (pthread_cond_init(&collect_data_cond_var, NULL)) {
  706. thread_finished++;
  707. error("Cannot start conditional variable to control Apps charts.");
  708. return -1;
  709. }
  710. return 0;
  711. }
  712. /**
  713. * Allocate the vectors used for all threads.
  714. */
  715. static void ebpf_allocate_common_vectors()
  716. {
  717. all_pids = callocz((size_t)pid_max, sizeof(struct pid_stat *));
  718. global_process_stat = callocz((size_t)ebpf_nprocs, sizeof(ebpf_process_stat_t));
  719. }
  720. /**
  721. * Fill the ebpf_data structure with default values
  722. *
  723. * @param ef the pointer to set default values
  724. */
  725. void fill_ebpf_data(ebpf_data_t *ef)
  726. {
  727. memset(ef, 0, sizeof(ebpf_data_t));
  728. ef->kernel_string = kernel_string;
  729. ef->running_on_kernel = running_on_kernel;
  730. ef->map_fd = callocz(EBPF_MAX_MAPS, sizeof(int));
  731. ef->isrh = isrh;
  732. }
  733. /**
  734. * Define how to load the ebpf programs
  735. *
  736. * @param ptr the option given by users
  737. */
  738. static inline void how_to_load(char *ptr)
  739. {
  740. if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_RETURN))
  741. ebpf_set_thread_mode(MODE_RETURN);
  742. else if (!strcasecmp(ptr, EBPF_CFG_LOAD_MODE_DEFAULT))
  743. ebpf_set_thread_mode(MODE_ENTRY);
  744. else
  745. error("the option %s for \"ebpf load mode\" is not a valid option.", ptr);
  746. }
  747. /**
  748. * Update interval
  749. *
  750. * Update default interval with value from user
  751. */
  752. static void ebpf_update_interval()
  753. {
  754. int i;
  755. int value = (int) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_UPDATE_EVERY, 1);
  756. for (i = 0; ebpf_modules[i].thread_name; i++) {
  757. ebpf_modules[i].update_time = value;
  758. }
  759. }
  760. /**
  761. * Update PID table size
  762. *
  763. * Update default size with value from user
  764. */
  765. static void ebpf_update_table_size()
  766. {
  767. int i;
  768. uint32_t value = (uint32_t) appconfig_get_number(&collector_config, EBPF_GLOBAL_SECTION,
  769. EBPF_CFG_PID_SIZE, ND_EBPF_DEFAULT_PID_SIZE);
  770. for (i = 0; ebpf_modules[i].thread_name; i++) {
  771. ebpf_modules[i].pid_map_size = value;
  772. }
  773. }
  774. /**
  775. * Read collector values
  776. *
  777. * @param disable_apps variable to store information related to apps.
  778. */
  779. static void read_collector_values(int *disable_apps)
  780. {
  781. // Read global section
  782. char *value;
  783. if (appconfig_exists(&collector_config, EBPF_GLOBAL_SECTION, "load")) // Backward compatibility
  784. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, "load",
  785. EBPF_CFG_LOAD_MODE_DEFAULT);
  786. else
  787. value = appconfig_get(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_LOAD_MODE,
  788. EBPF_CFG_LOAD_MODE_DEFAULT);
  789. how_to_load(value);
  790. ebpf_update_interval();
  791. ebpf_update_table_size();
  792. // This is kept to keep compatibility
  793. uint32_t enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, "disable apps",
  794. CONFIG_BOOLEAN_NO);
  795. if (!enabled) {
  796. // Apps is a positive sentence, so we need to invert the values to disable apps.
  797. enabled = appconfig_get_boolean(&collector_config, EBPF_GLOBAL_SECTION, EBPF_CFG_APPLICATION,
  798. CONFIG_BOOLEAN_YES);
  799. enabled = (enabled == CONFIG_BOOLEAN_NO)?CONFIG_BOOLEAN_YES:CONFIG_BOOLEAN_NO;
  800. }
  801. *disable_apps = (int)enabled;
  802. // Read ebpf programs section
  803. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  804. ebpf_modules[EBPF_MODULE_PROCESS_IDX].config_name, CONFIG_BOOLEAN_YES);
  805. int started = 0;
  806. if (enabled) {
  807. ebpf_enable_chart(EBPF_MODULE_PROCESS_IDX, *disable_apps);
  808. started++;
  809. }
  810. // This is kept to keep compatibility
  811. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network viewer",
  812. CONFIG_BOOLEAN_NO);
  813. if (!enabled)
  814. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION,
  815. ebpf_modules[EBPF_MODULE_SOCKET_IDX].config_name,
  816. CONFIG_BOOLEAN_NO);
  817. if (enabled) {
  818. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, *disable_apps);
  819. // Read network viewer section if network viewer is enabled
  820. // This is kept here to keep backward compatibility
  821. parse_network_viewer_section(&collector_config);
  822. parse_service_name_section(&collector_config);
  823. started++;
  824. }
  825. // This is kept to keep compatibility
  826. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connection monitoring",
  827. CONFIG_BOOLEAN_NO);
  828. if (!enabled)
  829. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "network connections",
  830. CONFIG_BOOLEAN_NO);
  831. ebpf_modules[EBPF_MODULE_SOCKET_IDX].optional = enabled;
  832. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "cachestat",
  833. CONFIG_BOOLEAN_NO);
  834. if (enabled) {
  835. ebpf_enable_chart(EBPF_MODULE_CACHESTAT_IDX, *disable_apps);
  836. started++;
  837. }
  838. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "sync",
  839. CONFIG_BOOLEAN_YES);
  840. if (enabled) {
  841. ebpf_enable_chart(EBPF_MODULE_SYNC_IDX, *disable_apps);
  842. started++;
  843. }
  844. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "dcstat",
  845. CONFIG_BOOLEAN_NO);
  846. if (enabled) {
  847. ebpf_enable_chart(EBPF_MODULE_DCSTAT_IDX, *disable_apps);
  848. started++;
  849. }
  850. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "swap",
  851. CONFIG_BOOLEAN_NO);
  852. if (enabled) {
  853. ebpf_enable_chart(EBPF_MODULE_SWAP_IDX, *disable_apps);
  854. started++;
  855. }
  856. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "vfs",
  857. CONFIG_BOOLEAN_NO);
  858. if (enabled) {
  859. ebpf_enable_chart(EBPF_MODULE_VFS_IDX, *disable_apps);
  860. started++;
  861. }
  862. enabled = appconfig_get_boolean(&collector_config, EBPF_PROGRAMS_SECTION, "filesystem",
  863. CONFIG_BOOLEAN_NO);
  864. if (enabled) {
  865. ebpf_enable_chart(EBPF_MODULE_FILESYSTEM_IDX, *disable_apps);
  866. started++;
  867. }
  868. if (!started){
  869. ebpf_enable_all_charts(*disable_apps);
  870. // Read network viewer section
  871. // This is kept here to keep backward compatibility
  872. parse_network_viewer_section(&collector_config);
  873. parse_service_name_section(&collector_config);
  874. }
  875. }
  876. /**
  877. * Load collector config
  878. *
  879. * @param path the path where the file ebpf.conf is stored.
  880. * @param disable_apps variable to store the information about apps plugin status.
  881. *
  882. * @return 0 on success and -1 otherwise.
  883. */
  884. static int load_collector_config(char *path, int *disable_apps)
  885. {
  886. char lpath[4096];
  887. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_CONFIG_FILE);
  888. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  889. snprintf(lpath, 4095, "%s/%s", path, NETDATA_EBPF_OLD_CONFIG_FILE);
  890. if (!appconfig_load(&collector_config, lpath, 0, NULL)) {
  891. return -1;
  892. }
  893. }
  894. read_collector_values(disable_apps);
  895. return 0;
  896. }
  897. /**
  898. * Set global variables reading environment variables
  899. */
  900. void set_global_variables()
  901. {
  902. // Get environment variables
  903. ebpf_plugin_dir = getenv("NETDATA_PLUGINS_DIR");
  904. if (!ebpf_plugin_dir)
  905. ebpf_plugin_dir = PLUGINS_DIR;
  906. ebpf_user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  907. if (!ebpf_user_config_dir)
  908. ebpf_user_config_dir = CONFIG_DIR;
  909. ebpf_stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  910. if (!ebpf_stock_config_dir)
  911. ebpf_stock_config_dir = LIBCONFIG_DIR;
  912. ebpf_configured_log_dir = getenv("NETDATA_LOG_DIR");
  913. if (!ebpf_configured_log_dir)
  914. ebpf_configured_log_dir = LOG_DIR;
  915. ebpf_nprocs = (int)sysconf(_SC_NPROCESSORS_ONLN);
  916. if (ebpf_nprocs > NETDATA_MAX_PROCESSOR) {
  917. ebpf_nprocs = NETDATA_MAX_PROCESSOR;
  918. }
  919. isrh = get_redhat_release();
  920. pid_max = get_system_pid_max();
  921. }
  922. /**
  923. * Parse arguments given from user.
  924. *
  925. * @param argc the number of arguments
  926. * @param argv the pointer to the arguments
  927. */
  928. static void parse_args(int argc, char **argv)
  929. {
  930. int enabled = 0;
  931. int disable_apps = 0;
  932. int freq = 0;
  933. int option_index = 0;
  934. static struct option long_options[] = {
  935. {"help", no_argument, 0, 'h' },
  936. {"version", no_argument, 0, 'v' },
  937. {"global", no_argument, 0, 'g' },
  938. {"all", no_argument, 0, 'a' },
  939. {"cachestat", no_argument, 0, 'c' },
  940. {"dcstat", no_argument, 0, 'd' },
  941. {"filesystem", no_argument, 0, 'i' },
  942. {"net", no_argument, 0, 'n' },
  943. {"process", no_argument, 0, 'p' },
  944. {"return", no_argument, 0, 'r' },
  945. {"sync", no_argument, 0, 's' },
  946. {"swap", no_argument, 0, 'w' },
  947. {"vfs", no_argument, 0, 'f' },
  948. {0, 0, 0, 0}
  949. };
  950. memset(&network_viewer_opt, 0, sizeof(network_viewer_opt));
  951. network_viewer_opt.max_dim = NETDATA_NV_CAP_VALUE;
  952. if (argc > 1) {
  953. int n = (int)str2l(argv[1]);
  954. if (n > 0) {
  955. freq = n;
  956. }
  957. }
  958. while (1) {
  959. int c = getopt_long(argc, argv, "hvgacdnprsw", long_options, &option_index);
  960. if (c == -1)
  961. break;
  962. switch (c) {
  963. case 'h': {
  964. ebpf_print_help();
  965. exit(0);
  966. }
  967. case 'v': {
  968. printf("ebpf.plugin %s\n", VERSION);
  969. exit(0);
  970. }
  971. case 'g': {
  972. disable_apps = 1;
  973. ebpf_disable_apps();
  974. #ifdef NETDATA_INTERNAL_CHECKS
  975. info(
  976. "EBPF running with global chart group, because it was started with the option \"--global\" or \"-g\".");
  977. #endif
  978. break;
  979. }
  980. case 'a': {
  981. ebpf_enable_all_charts(disable_apps);
  982. #ifdef NETDATA_INTERNAL_CHECKS
  983. info("EBPF running with all chart groups, because it was started with the option \"--all\" or \"-a\".");
  984. #endif
  985. break;
  986. }
  987. case 'c': {
  988. enabled = 1;
  989. ebpf_enable_chart(EBPF_MODULE_CACHESTAT_IDX, disable_apps);
  990. #ifdef NETDATA_INTERNAL_CHECKS
  991. info(
  992. "EBPF enabling \"CACHESTAT\" charts, because it was started with the option \"--cachestat\" or \"-c\".");
  993. #endif
  994. break;
  995. }
  996. case 'd': {
  997. enabled = 1;
  998. ebpf_enable_chart(EBPF_MODULE_DCSTAT_IDX, disable_apps);
  999. #ifdef NETDATA_INTERNAL_CHECKS
  1000. info(
  1001. "EBPF enabling \"DCSTAT\" charts, because it was started with the option \"--dcstat\" or \"-d\".");
  1002. #endif
  1003. break;
  1004. }
  1005. case 'i': {
  1006. enabled = 1;
  1007. ebpf_enable_chart(EBPF_MODULE_FILESYSTEM_IDX, disable_apps);
  1008. #ifdef NETDATA_INTERNAL_CHECKS
  1009. info("EBPF enabling \"filesystem\" chart, because it was started with the option \"--filesystem\" or \"-i\".");
  1010. #endif
  1011. break;
  1012. }
  1013. case 'n': {
  1014. enabled = 1;
  1015. ebpf_enable_chart(EBPF_MODULE_SOCKET_IDX, disable_apps);
  1016. #ifdef NETDATA_INTERNAL_CHECKS
  1017. info("EBPF enabling \"NET\" charts, because it was started with the option \"--net\" or \"-n\".");
  1018. #endif
  1019. break;
  1020. }
  1021. case 'p': {
  1022. enabled = 1;
  1023. ebpf_enable_chart(EBPF_MODULE_PROCESS_IDX, disable_apps);
  1024. #ifdef NETDATA_INTERNAL_CHECKS
  1025. info(
  1026. "EBPF enabling \"PROCESS\" charts, because it was started with the option \"--process\" or \"-p\".");
  1027. #endif
  1028. break;
  1029. }
  1030. case 'r': {
  1031. ebpf_set_thread_mode(MODE_RETURN);
  1032. #ifdef NETDATA_INTERNAL_CHECKS
  1033. info("EBPF running in \"return\" mode, because it was started with the option \"--return\" or \"-r\".");
  1034. #endif
  1035. break;
  1036. }
  1037. case 's': {
  1038. enabled = 1;
  1039. ebpf_enable_chart(EBPF_MODULE_SYNC_IDX, disable_apps);
  1040. #ifdef NETDATA_INTERNAL_CHECKS
  1041. info("EBPF enabling \"sync\" chart, because it was started with the option \"--sync\" or \"-s\".");
  1042. #endif
  1043. break;
  1044. }
  1045. case 'w': {
  1046. enabled = 1;
  1047. ebpf_enable_chart(EBPF_MODULE_SWAP_IDX, disable_apps);
  1048. #ifdef NETDATA_INTERNAL_CHECKS
  1049. info("EBPF enabling \"swap\" chart, because it was started with the option \"--swap\" or \"-w\".");
  1050. #endif
  1051. break;
  1052. }
  1053. case 'f': {
  1054. enabled = 1;
  1055. ebpf_enable_chart(EBPF_MODULE_VFS_IDX, disable_apps);
  1056. #ifdef NETDATA_INTERNAL_CHECKS
  1057. info("EBPF enabling \"vfs\" chart, because it was started with the option \"--vfs\" or \"-f\".");
  1058. #endif
  1059. break;
  1060. }
  1061. default: {
  1062. break;
  1063. }
  1064. }
  1065. }
  1066. if (freq > 0) {
  1067. update_every = freq;
  1068. }
  1069. if (load_collector_config(ebpf_user_config_dir, &disable_apps)) {
  1070. info(
  1071. "Does not have a configuration file inside `%s/ebpf.d.conf. It will try to load stock file.",
  1072. ebpf_user_config_dir);
  1073. if (load_collector_config(ebpf_stock_config_dir, &disable_apps)) {
  1074. info("Does not have a stock file. It is starting with default options.");
  1075. } else {
  1076. enabled = 1;
  1077. }
  1078. } else {
  1079. enabled = 1;
  1080. }
  1081. if (!enabled) {
  1082. ebpf_enable_all_charts(disable_apps);
  1083. #ifdef NETDATA_INTERNAL_CHECKS
  1084. info("EBPF running with all charts, because neither \"-n\" or \"-p\" was given.");
  1085. #endif
  1086. }
  1087. if (disable_apps)
  1088. return;
  1089. // Load apps_groups.conf
  1090. if (ebpf_read_apps_groups_conf(
  1091. &apps_groups_default_target, &apps_groups_root_target, ebpf_user_config_dir, "groups")) {
  1092. info(
  1093. "Cannot read process groups configuration file '%s/apps_groups.conf'. Will try '%s/apps_groups.conf'",
  1094. ebpf_user_config_dir, ebpf_stock_config_dir);
  1095. if (ebpf_read_apps_groups_conf(
  1096. &apps_groups_default_target, &apps_groups_root_target, ebpf_stock_config_dir, "groups")) {
  1097. error(
  1098. "Cannot read process groups '%s/apps_groups.conf'. There are no internal defaults. Failing.",
  1099. ebpf_stock_config_dir);
  1100. thread_finished++;
  1101. ebpf_exit(1);
  1102. }
  1103. } else
  1104. info("Loaded config file '%s/apps_groups.conf'", ebpf_user_config_dir);
  1105. }
  1106. /*****************************************************************
  1107. *
  1108. * COLLECTOR ENTRY POINT
  1109. *
  1110. *****************************************************************/
  1111. /**
  1112. * Load collector config
  1113. *
  1114. * @param lmode the mode that will be used for them.
  1115. */
  1116. static inline void ebpf_load_thread_config()
  1117. {
  1118. int i;
  1119. for (i = 0; ebpf_modules[i].thread_name; i++) {
  1120. ebpf_update_module(&ebpf_modules[i]);
  1121. }
  1122. }
  1123. /**
  1124. * Entry point
  1125. *
  1126. * @param argc the number of arguments
  1127. * @param argv the pointer to the arguments
  1128. *
  1129. * @return it returns 0 on success and another integer otherwise
  1130. */
  1131. int main(int argc, char **argv)
  1132. {
  1133. set_global_variables();
  1134. parse_args(argc, argv);
  1135. ebpf_load_thread_config();
  1136. running_on_kernel = get_kernel_version(kernel_string, 63);
  1137. if (!has_condition_to_run(running_on_kernel)) {
  1138. error("The current collector cannot run on this kernel.");
  1139. return 2;
  1140. }
  1141. if (!am_i_running_as_root()) {
  1142. error(
  1143. "ebpf.plugin should either run as root (now running with uid %u, euid %u) or have special capabilities..",
  1144. (unsigned int)getuid(), (unsigned int)geteuid());
  1145. return 3;
  1146. }
  1147. // set name
  1148. program_name = "ebpf.plugin";
  1149. // disable syslog
  1150. error_log_syslog = 0;
  1151. // set errors flood protection to 100 logs per hour
  1152. error_log_errors_per_period = 100;
  1153. error_log_throttle_period = 3600;
  1154. struct rlimit r = { RLIM_INFINITY, RLIM_INFINITY };
  1155. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  1156. error("Setrlimit(RLIMIT_MEMLOCK)");
  1157. return 4;
  1158. }
  1159. signal(SIGINT, ebpf_exit);
  1160. signal(SIGTERM, ebpf_exit);
  1161. signal(SIGPIPE, ebpf_exit);
  1162. if (ebpf_start_pthread_variables()) {
  1163. thread_finished++;
  1164. error("Cannot start mutex to control overall charts.");
  1165. ebpf_exit(5);
  1166. }
  1167. ebpf_allocate_common_vectors();
  1168. read_local_addresses();
  1169. read_local_ports("/proc/net/tcp", IPPROTO_TCP);
  1170. read_local_ports("/proc/net/tcp6", IPPROTO_TCP);
  1171. read_local_ports("/proc/net/udp", IPPROTO_UDP);
  1172. read_local_ports("/proc/net/udp6", IPPROTO_UDP);
  1173. struct netdata_static_thread ebpf_threads[] = {
  1174. {"EBPF PROCESS", NULL, NULL, 1,
  1175. NULL, NULL, ebpf_modules[EBPF_MODULE_PROCESS_IDX].start_routine},
  1176. {"EBPF SOCKET" , NULL, NULL, 1,
  1177. NULL, NULL, ebpf_modules[EBPF_MODULE_SOCKET_IDX].start_routine},
  1178. {"EBPF CACHESTAT" , NULL, NULL, 1,
  1179. NULL, NULL, ebpf_modules[EBPF_MODULE_CACHESTAT_IDX].start_routine},
  1180. {"EBPF SYNC" , NULL, NULL, 1,
  1181. NULL, NULL, ebpf_modules[EBPF_MODULE_SYNC_IDX].start_routine},
  1182. {"EBPF DCSTAT" , NULL, NULL, 1,
  1183. NULL, NULL, ebpf_modules[EBPF_MODULE_DCSTAT_IDX].start_routine},
  1184. {"EBPF SWAP" , NULL, NULL, 1,
  1185. NULL, NULL, ebpf_modules[EBPF_MODULE_SWAP_IDX].start_routine},
  1186. {"EBPF VFS" , NULL, NULL, 1,
  1187. NULL, NULL, ebpf_modules[EBPF_MODULE_VFS_IDX].start_routine},
  1188. {"EBPF FILESYSTEM" , NULL, NULL, 1,
  1189. NULL, NULL, ebpf_modules[EBPF_MODULE_FILESYSTEM_IDX].start_routine},
  1190. {NULL , NULL, NULL, 0,
  1191. NULL, NULL, NULL}
  1192. };
  1193. //clean_loaded_events();
  1194. int i;
  1195. for (i = 0; ebpf_threads[i].name != NULL; i++) {
  1196. struct netdata_static_thread *st = &ebpf_threads[i];
  1197. st->thread = mallocz(sizeof(netdata_thread_t));
  1198. ebpf_module_t *em = &ebpf_modules[i];
  1199. em->thread_id = i;
  1200. netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_JOINABLE, st->start_routine, em);
  1201. }
  1202. for (i = 0; ebpf_threads[i].name != NULL; i++) {
  1203. struct netdata_static_thread *st = &ebpf_threads[i];
  1204. netdata_thread_join(*st->thread, NULL);
  1205. }
  1206. thread_finished++;
  1207. ebpf_exit(0);
  1208. return 0;
  1209. }