ebpf_functions.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_functions.h"
  4. /*****************************************************************
  5. * EBPF FUNCTION COMMON
  6. *****************************************************************/
  7. /**
  8. * Function Start thread
  9. *
  10. * Start a specific thread after user request.
  11. *
  12. * @param em The structure with thread information
  13. * @param period
  14. * @return
  15. */
  16. static int ebpf_function_start_thread(ebpf_module_t *em, int period)
  17. {
  18. struct netdata_static_thread *st = em->thread;
  19. // another request for thread that already ran, cleanup and restart
  20. if (st->thread)
  21. freez(st->thread);
  22. if (period <= 0)
  23. period = EBPF_DEFAULT_LIFETIME;
  24. st->thread = mallocz(sizeof(netdata_thread_t));
  25. em->enabled = NETDATA_THREAD_EBPF_FUNCTION_RUNNING;
  26. em->lifetime = period;
  27. #ifdef NETDATA_INTERNAL_CHECKS
  28. netdata_log_info("Starting thread %s with lifetime = %d", em->info.thread_name, period);
  29. #endif
  30. return netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_DEFAULT, st->start_routine, em);
  31. }
  32. /*****************************************************************
  33. * EBPF ERROR FUNCTIONS
  34. *****************************************************************/
  35. /**
  36. * Function error
  37. *
  38. * Show error when a wrong function is given
  39. *
  40. * @param transaction the transaction id that Netdata sent for this function execution
  41. * @param code the error code to show with the message.
  42. * @param msg the error message
  43. */
  44. static inline void ebpf_function_error(const char *transaction, int code, const char *msg) {
  45. pluginsd_function_json_error_to_stdout(transaction, code, msg);
  46. }
  47. /**
  48. * Thread Help
  49. *
  50. * Shows help with all options accepted by thread function.
  51. *
  52. * @param transaction the transaction id that Netdata sent for this function execution
  53. */
  54. static inline void ebpf_function_help(const char *transaction, const char *message) {
  55. pluginsd_function_result_begin_to_stdout(transaction, HTTP_RESP_OK, "text/plain", now_realtime_sec() + 3600);
  56. fprintf(stdout, "%s", message);
  57. pluginsd_function_result_end_to_stdout();
  58. fflush(stdout);
  59. }
  60. /*****************************************************************
  61. * EBPF SOCKET FUNCTION
  62. *****************************************************************/
  63. /**
  64. * Fill Fake socket
  65. *
  66. * Fill socket with an invalid request.
  67. *
  68. * @param fake_values is the structure where we are storing the value.
  69. */
  70. static inline void ebpf_socket_fill_fake_socket(netdata_socket_plus_t *fake_values)
  71. {
  72. snprintfz(fake_values->socket_string.src_ip, INET6_ADDRSTRLEN, "%s", "127.0.0.1");
  73. snprintfz(fake_values->socket_string.dst_ip, INET6_ADDRSTRLEN, "%s", "127.0.0.1");
  74. fake_values->pid = getpid();
  75. //fake_values->socket_string.src_port = 0;
  76. fake_values->socket_string.dst_port[0] = 0;
  77. snprintfz(fake_values->socket_string.dst_ip, NI_MAXSERV, "%s", "none");
  78. fake_values->data.family = AF_INET;
  79. fake_values->data.protocol = AF_UNSPEC;
  80. }
  81. static NETDATA_DOUBLE bytes_to_mb(uint64_t bytes) {
  82. return (NETDATA_DOUBLE)bytes / (1024 * 1024);
  83. }
  84. /**
  85. * Fill function buffer
  86. *
  87. * Fill buffer with data to be shown on cloud.
  88. *
  89. * @param wb buffer where we store data.
  90. * @param values data read from hash table
  91. * @param name the process name
  92. */
  93. static void ebpf_fill_function_buffer(BUFFER *wb, netdata_socket_plus_t *values, char *name)
  94. {
  95. buffer_json_add_array_item_array(wb);
  96. // IMPORTANT!
  97. // THE ORDER SHOULD BE THE SAME WITH THE FIELDS!
  98. // PID
  99. buffer_json_add_array_item_uint64(wb, (uint64_t)values->pid);
  100. // NAME
  101. buffer_json_add_array_item_string(wb, (name) ? name : "unknown");
  102. // Origin
  103. buffer_json_add_array_item_string(wb, (values->data.external_origin) ? "in" : "out");
  104. // Source IP
  105. buffer_json_add_array_item_string(wb, values->socket_string.src_ip);
  106. // SRC Port
  107. //buffer_json_add_array_item_uint64(wb, (uint64_t) values->socket_string.src_port);
  108. // Destination IP
  109. buffer_json_add_array_item_string(wb, values->socket_string.dst_ip);
  110. // DST Port
  111. buffer_json_add_array_item_string(wb, values->socket_string.dst_port);
  112. uint64_t connections;
  113. if (values->data.protocol == IPPROTO_TCP) {
  114. buffer_json_add_array_item_string(wb, "TCP");
  115. buffer_json_add_array_item_double(wb, bytes_to_mb(values->data.tcp.tcp_bytes_received));
  116. buffer_json_add_array_item_double(wb, bytes_to_mb(values->data.tcp.tcp_bytes_sent));
  117. connections = values->data.tcp.ipv4_connect + values->data.tcp.ipv6_connect;
  118. } else if (values->data.protocol == IPPROTO_UDP) {
  119. buffer_json_add_array_item_string(wb, "UDP");
  120. buffer_json_add_array_item_double(wb, bytes_to_mb(values->data.udp.udp_bytes_received));
  121. buffer_json_add_array_item_double(wb, bytes_to_mb(values->data.udp.udp_bytes_sent));
  122. connections = values->data.udp.call_udp_sent + values->data.udp.call_udp_received;
  123. } else {
  124. buffer_json_add_array_item_string(wb, "UNSPEC");
  125. buffer_json_add_array_item_double(wb, 0);
  126. buffer_json_add_array_item_double(wb, 0);
  127. connections = 1;
  128. }
  129. // Connections
  130. if (values->flags & NETDATA_SOCKET_FLAGS_ALREADY_OPEN) {
  131. connections++;
  132. } else if (!connections) {
  133. // If no connections, this means that we lost when connection was opened
  134. values->flags |= NETDATA_SOCKET_FLAGS_ALREADY_OPEN;
  135. connections++;
  136. }
  137. buffer_json_add_array_item_uint64(wb, connections);
  138. buffer_json_array_close(wb);
  139. }
  140. /**
  141. * Clean Judy array unsafe
  142. *
  143. * Clean all Judy Array allocated to show table when a function is called.
  144. * Before to call this function it is necessary to lock `ebpf_judy_pid.index.rw_spinlock`.
  145. **/
  146. static void ebpf_socket_clean_judy_array_unsafe()
  147. {
  148. if (!ebpf_judy_pid.index.JudyLArray)
  149. return;
  150. Pvoid_t *pid_value, *socket_value;
  151. Word_t local_pid = 0, local_socket = 0;
  152. bool first_pid = true, first_socket = true;
  153. while ((pid_value = JudyLFirstThenNext(ebpf_judy_pid.index.JudyLArray, &local_pid, &first_pid))) {
  154. netdata_ebpf_judy_pid_stats_t *pid_ptr = (netdata_ebpf_judy_pid_stats_t *)*pid_value;
  155. rw_spinlock_write_lock(&pid_ptr->socket_stats.rw_spinlock);
  156. if (pid_ptr->socket_stats.JudyLArray) {
  157. while ((socket_value = JudyLFirstThenNext(pid_ptr->socket_stats.JudyLArray, &local_socket, &first_socket))) {
  158. netdata_socket_plus_t *socket_clean = *socket_value;
  159. aral_freez(aral_socket_table, socket_clean);
  160. }
  161. JudyLFreeArray(&pid_ptr->socket_stats.JudyLArray, PJE0);
  162. pid_ptr->socket_stats.JudyLArray = NULL;
  163. }
  164. rw_spinlock_write_unlock(&pid_ptr->socket_stats.rw_spinlock);
  165. }
  166. }
  167. /**
  168. * Fill function buffer unsafe
  169. *
  170. * Fill the function buffer with socket information. Before to call this function it is necessary to lock
  171. * ebpf_judy_pid.index.rw_spinlock
  172. *
  173. * @param buf buffer used to store data to be shown by function.
  174. *
  175. * @return it returns 0 on success and -1 otherwise.
  176. */
  177. static void ebpf_socket_fill_function_buffer_unsafe(BUFFER *buf)
  178. {
  179. int counter = 0;
  180. Pvoid_t *pid_value, *socket_value;
  181. Word_t local_pid = 0;
  182. bool first_pid = true;
  183. while ((pid_value = JudyLFirstThenNext(ebpf_judy_pid.index.JudyLArray, &local_pid, &first_pid))) {
  184. netdata_ebpf_judy_pid_stats_t *pid_ptr = (netdata_ebpf_judy_pid_stats_t *)*pid_value;
  185. bool first_socket = true;
  186. Word_t local_timestamp = 0;
  187. rw_spinlock_read_lock(&pid_ptr->socket_stats.rw_spinlock);
  188. if (pid_ptr->socket_stats.JudyLArray) {
  189. while ((socket_value = JudyLFirstThenNext(pid_ptr->socket_stats.JudyLArray, &local_timestamp, &first_socket))) {
  190. netdata_socket_plus_t *values = (netdata_socket_plus_t *)*socket_value;
  191. ebpf_fill_function_buffer(buf, values, pid_ptr->cmdline);
  192. }
  193. counter++;
  194. }
  195. rw_spinlock_read_unlock(&pid_ptr->socket_stats.rw_spinlock);
  196. }
  197. if (!counter) {
  198. netdata_socket_plus_t fake_values = { };
  199. ebpf_socket_fill_fake_socket(&fake_values);
  200. ebpf_fill_function_buffer(buf, &fake_values, NULL);
  201. }
  202. }
  203. /**
  204. * Socket read hash
  205. *
  206. * This is the thread callback.
  207. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  208. *
  209. * @param buf the buffer to store data;
  210. * @param em the module main structure.
  211. *
  212. * @return It always returns NULL.
  213. */
  214. void ebpf_socket_read_open_connections(BUFFER *buf, struct ebpf_module *em)
  215. {
  216. // thread was not initialized or Array was reset
  217. rw_spinlock_read_lock(&ebpf_judy_pid.index.rw_spinlock);
  218. if (!em->maps || (em->maps[NETDATA_SOCKET_OPEN_SOCKET].map_fd == ND_EBPF_MAP_FD_NOT_INITIALIZED) ||
  219. !ebpf_judy_pid.index.JudyLArray){
  220. netdata_socket_plus_t fake_values = { };
  221. ebpf_socket_fill_fake_socket(&fake_values);
  222. ebpf_fill_function_buffer(buf, &fake_values, NULL);
  223. rw_spinlock_read_unlock(&ebpf_judy_pid.index.rw_spinlock);
  224. return;
  225. }
  226. rw_spinlock_read_lock(&network_viewer_opt.rw_spinlock);
  227. ebpf_socket_fill_function_buffer_unsafe(buf);
  228. rw_spinlock_read_unlock(&network_viewer_opt.rw_spinlock);
  229. rw_spinlock_read_unlock(&ebpf_judy_pid.index.rw_spinlock);
  230. }
  231. /**
  232. * Function: Socket
  233. *
  234. * Show information for sockets stored in hash tables.
  235. *
  236. * @param transaction the transaction id that Netdata sent for this function execution
  237. * @param function function name and arguments given to thread.
  238. * @param timeout The function timeout
  239. * @param cancelled Variable used to store function status.
  240. */
  241. static void ebpf_function_socket_manipulation(const char *transaction,
  242. char *function __maybe_unused,
  243. usec_t *stop_monotonic_ut __maybe_unused,
  244. bool *cancelled __maybe_unused,
  245. BUFFER *payload __maybe_unused,
  246. const char *source __maybe_unused,
  247. void *data __maybe_unused)
  248. {
  249. ebpf_module_t *em = &ebpf_modules[EBPF_MODULE_SOCKET_IDX];
  250. char *words[PLUGINSD_MAX_WORDS] = {NULL};
  251. size_t num_words = quoted_strings_splitter_pluginsd(function, words, PLUGINSD_MAX_WORDS);
  252. const char *name;
  253. int period = -1;
  254. rw_spinlock_write_lock(&ebpf_judy_pid.index.rw_spinlock);
  255. network_viewer_opt.enabled = CONFIG_BOOLEAN_YES;
  256. uint32_t previous;
  257. static const char *socket_help = {
  258. "ebpf.plugin / socket\n"
  259. "\n"
  260. "Function `socket` display information for all open sockets during ebpf.plugin runtime.\n"
  261. "During thread runtime the plugin is always collecting data, but when an option is modified, the plugin\n"
  262. "resets completely the previous table and can show a clean data for the first request before to bring the\n"
  263. "modified request.\n"
  264. "\n"
  265. "The following filters are supported:\n"
  266. "\n"
  267. " family:FAMILY\n"
  268. " Shows information for the FAMILY specified. Option accepts IPV4, IPV6 and all, that is the default.\n"
  269. "\n"
  270. " period:PERIOD\n"
  271. " Enable socket to run a specific PERIOD in seconds. When PERIOD is not\n"
  272. " specified plugin will use the default 300 seconds\n"
  273. "\n"
  274. " resolve:BOOL\n"
  275. " Resolve service name, default value is YES.\n"
  276. "\n"
  277. " range:CIDR\n"
  278. " Show sockets that have only a specific destination. Default all addresses.\n"
  279. "\n"
  280. " port:range\n"
  281. " Show sockets that have only a specific destination.\n"
  282. "\n"
  283. " reset\n"
  284. " Send a reset to collector. When a collector receives this command, it uses everything defined in configuration file.\n"
  285. "\n"
  286. " interfaces\n"
  287. " When the collector receives this command, it read all available interfaces on host.\n"
  288. "\n"
  289. "Filters can be combined. Each filter can be given only one time. Default all ports\n"
  290. };
  291. for (int i = 1; i < PLUGINSD_MAX_WORDS; i++) {
  292. const char *keyword = get_word(words, num_words, i);
  293. if (!keyword)
  294. break;
  295. if (strncmp(keyword, EBPF_FUNCTION_SOCKET_FAMILY, sizeof(EBPF_FUNCTION_SOCKET_FAMILY) - 1) == 0) {
  296. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_FAMILY) - 1];
  297. previous = network_viewer_opt.family;
  298. uint32_t family = AF_UNSPEC;
  299. if (!strcmp(name, "IPV4"))
  300. family = AF_INET;
  301. else if (!strcmp(name, "IPV6"))
  302. family = AF_INET6;
  303. if (family != previous) {
  304. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  305. network_viewer_opt.family = family;
  306. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  307. ebpf_socket_clean_judy_array_unsafe();
  308. }
  309. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_PERIOD, sizeof(EBPF_FUNCTION_SOCKET_PERIOD) - 1) == 0) {
  310. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_PERIOD) - 1];
  311. pthread_mutex_lock(&ebpf_exit_cleanup);
  312. period = str2i(name);
  313. if (period > 0) {
  314. em->lifetime = period;
  315. } else
  316. em->lifetime = EBPF_NON_FUNCTION_LIFE_TIME;
  317. #ifdef NETDATA_DEV_MODE
  318. collector_info("Lifetime modified for %u", em->lifetime);
  319. #endif
  320. pthread_mutex_unlock(&ebpf_exit_cleanup);
  321. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_RESOLVE, sizeof(EBPF_FUNCTION_SOCKET_RESOLVE) - 1) == 0) {
  322. previous = network_viewer_opt.service_resolution_enabled;
  323. uint32_t resolution;
  324. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_RESOLVE) - 1];
  325. resolution = (!strcasecmp(name, "YES")) ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
  326. if (previous != resolution) {
  327. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  328. network_viewer_opt.service_resolution_enabled = resolution;
  329. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  330. ebpf_socket_clean_judy_array_unsafe();
  331. }
  332. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_RANGE, sizeof(EBPF_FUNCTION_SOCKET_RANGE) - 1) == 0) {
  333. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_RANGE) - 1];
  334. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  335. ebpf_clean_ip_structure(&network_viewer_opt.included_ips);
  336. ebpf_clean_ip_structure(&network_viewer_opt.excluded_ips);
  337. ebpf_parse_ips_unsafe((char *)name);
  338. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  339. ebpf_socket_clean_judy_array_unsafe();
  340. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_PORT, sizeof(EBPF_FUNCTION_SOCKET_PORT) - 1) == 0) {
  341. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_PORT) - 1];
  342. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  343. ebpf_clean_port_structure(&network_viewer_opt.included_port);
  344. ebpf_clean_port_structure(&network_viewer_opt.excluded_port);
  345. ebpf_parse_ports((char *)name);
  346. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  347. ebpf_socket_clean_judy_array_unsafe();
  348. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_RESET, sizeof(EBPF_FUNCTION_SOCKET_RESET) - 1) == 0) {
  349. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  350. ebpf_clean_port_structure(&network_viewer_opt.included_port);
  351. ebpf_clean_port_structure(&network_viewer_opt.excluded_port);
  352. ebpf_clean_ip_structure(&network_viewer_opt.included_ips);
  353. ebpf_clean_ip_structure(&network_viewer_opt.excluded_ips);
  354. ebpf_clean_ip_structure(&network_viewer_opt.ipv4_local_ip);
  355. ebpf_clean_ip_structure(&network_viewer_opt.ipv6_local_ip);
  356. parse_network_viewer_section(&socket_config);
  357. ebpf_read_local_addresses_unsafe();
  358. network_viewer_opt.enabled = CONFIG_BOOLEAN_YES;
  359. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  360. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_INTERFACES, sizeof(EBPF_FUNCTION_SOCKET_INTERFACES) - 1) == 0) {
  361. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  362. ebpf_read_local_addresses_unsafe();
  363. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  364. } else if (strncmp(keyword, "help", 4) == 0) {
  365. ebpf_function_help(transaction, socket_help);
  366. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  367. return;
  368. }
  369. }
  370. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  371. if (em->enabled > NETDATA_THREAD_EBPF_FUNCTION_RUNNING) {
  372. // Cleanup when we already had a thread running
  373. rw_spinlock_write_lock(&ebpf_judy_pid.index.rw_spinlock);
  374. ebpf_socket_clean_judy_array_unsafe();
  375. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  376. pthread_mutex_lock(&ebpf_exit_cleanup);
  377. if (ebpf_function_start_thread(em, period)) {
  378. ebpf_function_error(transaction,
  379. HTTP_RESP_INTERNAL_SERVER_ERROR,
  380. "Cannot start thread.");
  381. pthread_mutex_unlock(&ebpf_exit_cleanup);
  382. return;
  383. }
  384. } else {
  385. pthread_mutex_lock(&ebpf_exit_cleanup);
  386. if (period < 0)
  387. em->lifetime = (em->enabled != NETDATA_THREAD_EBPF_FUNCTION_RUNNING) ? EBPF_NON_FUNCTION_LIFE_TIME : EBPF_DEFAULT_LIFETIME;
  388. }
  389. pthread_mutex_unlock(&ebpf_exit_cleanup);
  390. BUFFER *wb = buffer_create(4096, NULL);
  391. buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS);
  392. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  393. buffer_json_member_add_string(wb, "type", "table");
  394. buffer_json_member_add_time_t(wb, "update_every", em->update_every);
  395. buffer_json_member_add_string(wb, "help", EBPF_PLUGIN_SOCKET_FUNCTION_DESCRIPTION);
  396. // Collect data
  397. buffer_json_member_add_array(wb, "data");
  398. ebpf_socket_read_open_connections(wb, em);
  399. buffer_json_array_close(wb); // data
  400. buffer_json_member_add_object(wb, "columns");
  401. {
  402. int fields_id = 0;
  403. // IMPORTANT!
  404. // THE ORDER SHOULD BE THE SAME WITH THE VALUES!
  405. buffer_rrdf_table_add_field(wb, fields_id++, "PID", "Process ID", RRDF_FIELD_TYPE_INTEGER,
  406. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  407. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  408. RRDF_FIELD_FILTER_MULTISELECT,
  409. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY,
  410. NULL);
  411. buffer_rrdf_table_add_field(wb, fields_id++, "Name", "Process Name", RRDF_FIELD_TYPE_STRING,
  412. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  413. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  414. RRDF_FIELD_FILTER_MULTISELECT,
  415. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY | RRDF_FIELD_OPTS_FULL_WIDTH,
  416. NULL);
  417. buffer_rrdf_table_add_field(wb, fields_id++, "Origin", "Connection Origin", RRDF_FIELD_TYPE_STRING,
  418. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  419. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  420. RRDF_FIELD_FILTER_MULTISELECT,
  421. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY,
  422. NULL);
  423. buffer_rrdf_table_add_field(wb, fields_id++, "Src", "Source IP Address", RRDF_FIELD_TYPE_STRING,
  424. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  425. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  426. RRDF_FIELD_FILTER_MULTISELECT,
  427. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY,
  428. NULL);
  429. /*
  430. buffer_rrdf_table_add_field(wb, fields_id++, "SrcPort", "Source Port", RRDF_FIELD_TYPE_INTEGER,
  431. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  432. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  433. RRDF_FIELD_FILTER_MULTISELECT,
  434. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY,
  435. NULL);
  436. */
  437. buffer_rrdf_table_add_field(wb, fields_id++, "Dst", "Destination IP Address", RRDF_FIELD_TYPE_STRING,
  438. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  439. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  440. RRDF_FIELD_FILTER_MULTISELECT,
  441. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY,
  442. NULL);
  443. buffer_rrdf_table_add_field(wb, fields_id++, "DstPort", "Destination Port", RRDF_FIELD_TYPE_STRING,
  444. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  445. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  446. RRDF_FIELD_FILTER_MULTISELECT,
  447. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY,
  448. NULL);
  449. buffer_rrdf_table_add_field(wb, fields_id++, "Protocol", "Transport Layer Protocol", RRDF_FIELD_TYPE_STRING,
  450. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  451. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  452. RRDF_FIELD_FILTER_MULTISELECT,
  453. RRDF_FIELD_OPTS_NONE | RRDF_FIELD_OPTS_UNIQUE_KEY | RRDF_FIELD_OPTS_STICKY,
  454. NULL);
  455. buffer_rrdf_table_add_field(wb, fields_id++, "Rcvd", "Traffic Received", RRDF_FIELD_TYPE_INTEGER,
  456. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 3, "MB", NAN,
  457. RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  458. RRDF_FIELD_FILTER_NONE,
  459. RRDF_FIELD_OPTS_VISIBLE,
  460. NULL);
  461. buffer_rrdf_table_add_field(wb, fields_id++, "Sent", "Traffic Sent", RRDF_FIELD_TYPE_INTEGER,
  462. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 3, "MB", NAN,
  463. RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  464. RRDF_FIELD_FILTER_NONE,
  465. RRDF_FIELD_OPTS_VISIBLE,
  466. NULL);
  467. buffer_rrdf_table_add_field(wb, fields_id, "Conns", "Connections", RRDF_FIELD_TYPE_INTEGER,
  468. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, "connections", NAN,
  469. RRDF_FIELD_SORT_DESCENDING, NULL, RRDF_FIELD_SUMMARY_SUM,
  470. RRDF_FIELD_FILTER_NONE,
  471. RRDF_FIELD_OPTS_VISIBLE,
  472. NULL);
  473. }
  474. buffer_json_object_close(wb); // columns
  475. buffer_json_member_add_string(wb, "default_sort_column", "Rcvd");
  476. buffer_json_member_add_object(wb, "charts");
  477. {
  478. buffer_json_member_add_object(wb, "Traffic");
  479. {
  480. buffer_json_member_add_string(wb, "name", "Traffic");
  481. buffer_json_member_add_string(wb, "type", "stacked-bar");
  482. buffer_json_member_add_array(wb, "columns");
  483. {
  484. buffer_json_add_array_item_string(wb, "Rcvd");
  485. buffer_json_add_array_item_string(wb, "Sent");
  486. }
  487. buffer_json_array_close(wb);
  488. }
  489. buffer_json_object_close(wb);
  490. buffer_json_member_add_object(wb, "Connections");
  491. {
  492. buffer_json_member_add_string(wb, "name", "Connections");
  493. buffer_json_member_add_string(wb, "type", "stacked-bar");
  494. buffer_json_member_add_array(wb, "columns");
  495. {
  496. buffer_json_add_array_item_string(wb, "Conns");
  497. }
  498. buffer_json_array_close(wb);
  499. }
  500. buffer_json_object_close(wb);
  501. }
  502. buffer_json_object_close(wb); // charts
  503. buffer_json_member_add_array(wb, "default_charts");
  504. {
  505. buffer_json_add_array_item_array(wb);
  506. buffer_json_add_array_item_string(wb, "Traffic");
  507. buffer_json_add_array_item_string(wb, "Name");
  508. buffer_json_array_close(wb);
  509. buffer_json_add_array_item_array(wb);
  510. buffer_json_add_array_item_string(wb, "Connections");
  511. buffer_json_add_array_item_string(wb, "Name");
  512. buffer_json_array_close(wb);
  513. }
  514. buffer_json_array_close(wb);
  515. buffer_json_member_add_object(wb, "group_by");
  516. {
  517. buffer_json_member_add_object(wb, "Name");
  518. {
  519. buffer_json_member_add_string(wb, "name", "Process Name");
  520. buffer_json_member_add_array(wb, "columns");
  521. {
  522. buffer_json_add_array_item_string(wb, "Name");
  523. }
  524. buffer_json_array_close(wb);
  525. }
  526. buffer_json_object_close(wb);
  527. buffer_json_member_add_object(wb, "Origin");
  528. {
  529. buffer_json_member_add_string(wb, "name", "Origin");
  530. buffer_json_member_add_array(wb, "columns");
  531. {
  532. buffer_json_add_array_item_string(wb, "Origin");
  533. }
  534. buffer_json_array_close(wb);
  535. }
  536. buffer_json_object_close(wb);
  537. buffer_json_member_add_object(wb, "Src");
  538. {
  539. buffer_json_member_add_string(wb, "name", "Source IP");
  540. buffer_json_member_add_array(wb, "columns");
  541. {
  542. buffer_json_add_array_item_string(wb, "Src");
  543. }
  544. buffer_json_array_close(wb);
  545. }
  546. buffer_json_object_close(wb);
  547. buffer_json_member_add_object(wb, "Dst");
  548. {
  549. buffer_json_member_add_string(wb, "name", "Destination IP");
  550. buffer_json_member_add_array(wb, "columns");
  551. {
  552. buffer_json_add_array_item_string(wb, "Dst");
  553. }
  554. buffer_json_array_close(wb);
  555. }
  556. buffer_json_object_close(wb);
  557. buffer_json_member_add_object(wb, "DstPort");
  558. {
  559. buffer_json_member_add_string(wb, "name", "Destination Port");
  560. buffer_json_member_add_array(wb, "columns");
  561. {
  562. buffer_json_add_array_item_string(wb, "DstPort");
  563. }
  564. buffer_json_array_close(wb);
  565. }
  566. buffer_json_object_close(wb);
  567. buffer_json_member_add_object(wb, "Protocol");
  568. {
  569. buffer_json_member_add_string(wb, "name", "Protocol");
  570. buffer_json_member_add_array(wb, "columns");
  571. {
  572. buffer_json_add_array_item_string(wb, "Protocol");
  573. }
  574. buffer_json_array_close(wb);
  575. }
  576. buffer_json_object_close(wb);
  577. }
  578. buffer_json_object_close(wb); // group_by
  579. time_t expires = now_realtime_sec() + em->update_every;
  580. buffer_json_member_add_time_t(wb, "expires", expires);
  581. buffer_json_finalize(wb);
  582. // Lock necessary to avoid race condition
  583. pluginsd_function_result_begin_to_stdout(transaction, HTTP_RESP_OK, "application/json", expires);
  584. fwrite(buffer_tostring(wb), buffer_strlen(wb), 1, stdout);
  585. pluginsd_function_result_end_to_stdout();
  586. fflush(stdout);
  587. buffer_free(wb);
  588. }
  589. /*****************************************************************
  590. * EBPF FUNCTION THREAD
  591. *****************************************************************/
  592. /**
  593. * FUNCTION thread.
  594. *
  595. * @param ptr a `ebpf_module_t *`.
  596. *
  597. * @return always NULL.
  598. */
  599. void *ebpf_function_thread(void *ptr)
  600. {
  601. (void)ptr;
  602. struct functions_evloop_globals *wg = functions_evloop_init(1,
  603. "EBPF",
  604. &lock,
  605. &ebpf_plugin_exit);
  606. functions_evloop_add_function(
  607. wg, EBPF_FUNCTION_SOCKET, ebpf_function_socket_manipulation, PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT, NULL);
  608. pthread_mutex_lock(&lock);
  609. int i;
  610. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  611. ebpf_module_t *em = &ebpf_modules[i];
  612. if (!em->functions.fnct_routine)
  613. continue;
  614. EBPF_PLUGIN_FUNCTIONS(em->functions.fcnt_name, em->functions.fcnt_desc, em->update_every);
  615. }
  616. pthread_mutex_unlock(&lock);
  617. heartbeat_t hb;
  618. heartbeat_init(&hb);
  619. while(!ebpf_plugin_exit) {
  620. (void)heartbeat_next(&hb, USEC_PER_SEC);
  621. if (ebpf_plugin_exit) {
  622. break;
  623. }
  624. }
  625. return NULL;
  626. }