ebpf_functions.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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 SELECT MODULE
  34. *****************************************************************/
  35. /**
  36. * Select Module
  37. *
  38. * @param thread_name name of the thread we are looking for.
  39. *
  40. * @return it returns a pointer for the module that has thread_name on success or NULL otherwise.
  41. ebpf_module_t *ebpf_functions_select_module(const char *thread_name) {
  42. int i;
  43. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  44. if (strcmp(ebpf_modules[i].info.thread_name, thread_name) == 0) {
  45. return &ebpf_modules[i];
  46. }
  47. }
  48. return NULL;
  49. }
  50. */
  51. /*****************************************************************
  52. * EBPF HELP FUNCTIONS
  53. *****************************************************************/
  54. /**
  55. * Thread Help
  56. *
  57. * Shows help with all options accepted by thread function.
  58. *
  59. * @param transaction the transaction id that Netdata sent for this function execution
  60. static void ebpf_function_thread_manipulation_help(const char *transaction) {
  61. BUFFER *wb = buffer_create(0, NULL);
  62. buffer_sprintf(wb, "%s",
  63. "ebpf.plugin / thread\n"
  64. "\n"
  65. "Function `thread` allows user to control eBPF threads.\n"
  66. "\n"
  67. "The following filters are supported:\n"
  68. "\n"
  69. " thread:NAME\n"
  70. " Shows information for the thread NAME. Names are listed inside `ebpf.d.conf`.\n"
  71. "\n"
  72. " enable:NAME:PERIOD\n"
  73. " Enable a specific thread named `NAME` to run a specific PERIOD in seconds. When PERIOD is not\n"
  74. " specified plugin will use the default 300 seconds\n"
  75. "\n"
  76. " disable:NAME\n"
  77. " Disable a sp.\n"
  78. "\n"
  79. "Filters can be combined. Each filter can be given only one time.\n"
  80. );
  81. pluginsd_function_result_to_stdout(transaction, HTTP_RESP_OK, "text/plain", now_realtime_sec() + 3600, wb);
  82. buffer_free(wb);
  83. }
  84. */
  85. /*****************************************************************
  86. * EBPF ERROR FUNCTIONS
  87. *****************************************************************/
  88. /**
  89. * Function error
  90. *
  91. * Show error when a wrong function is given
  92. *
  93. * @param transaction the transaction id that Netdata sent for this function execution
  94. * @param code the error code to show with the message.
  95. * @param msg the error message
  96. */
  97. static void ebpf_function_error(const char *transaction, int code, const char *msg) {
  98. pluginsd_function_json_error_to_stdout(transaction, code, msg);
  99. }
  100. /*****************************************************************
  101. * EBPF THREAD FUNCTION
  102. *****************************************************************/
  103. /**
  104. * Function: thread
  105. *
  106. * Enable a specific thread.
  107. *
  108. * @param transaction the transaction id that Netdata sent for this function execution
  109. * @param function function name and arguments given to thread.
  110. * @param line_buffer buffer used to parse args
  111. * @param line_max Number of arguments given
  112. * @param timeout The function timeout
  113. * @param em The structure with thread information
  114. static void ebpf_function_thread_manipulation(const char *transaction,
  115. char *function __maybe_unused,
  116. char *line_buffer __maybe_unused,
  117. int line_max __maybe_unused,
  118. int timeout __maybe_unused,
  119. ebpf_module_t *em)
  120. {
  121. char *words[PLUGINSD_MAX_WORDS] = { NULL };
  122. char message[512];
  123. uint32_t show_specific_thread = 0;
  124. size_t num_words = quoted_strings_splitter_pluginsd(function, words, PLUGINSD_MAX_WORDS);
  125. for(int i = 1; i < PLUGINSD_MAX_WORDS ;i++) {
  126. const char *keyword = get_word(words, num_words, i);
  127. if (!keyword)
  128. break;
  129. ebpf_module_t *lem;
  130. if(strncmp(keyword, EBPF_THREADS_ENABLE_CATEGORY, sizeof(EBPF_THREADS_ENABLE_CATEGORY) -1) == 0) {
  131. char thread_name[128];
  132. int period = -1;
  133. const char *name = &keyword[sizeof(EBPF_THREADS_ENABLE_CATEGORY) - 1];
  134. char *separator = strchr(name, ':');
  135. if (separator) {
  136. strncpyz(thread_name, name, separator - name);
  137. period = str2i(++separator);
  138. } else {
  139. strncpyz(thread_name, name, strlen(name));
  140. }
  141. lem = ebpf_functions_select_module(thread_name);
  142. if (!lem) {
  143. snprintfz(message, sizeof(message) - 1, "%s%s", EBPF_PLUGIN_THREAD_FUNCTION_ERROR_THREAD_NOT_FOUND, name);
  144. ebpf_function_error(transaction, HTTP_RESP_NOT_FOUND, message);
  145. return;
  146. }
  147. pthread_mutex_lock(&ebpf_exit_cleanup);
  148. if (lem->enabled > NETDATA_THREAD_EBPF_FUNCTION_RUNNING) {
  149. // Load configuration again
  150. ebpf_update_module(lem, default_btf, running_on_kernel, isrh);
  151. if (ebpf_function_start_thread(lem, period)) {
  152. ebpf_function_error(transaction,
  153. HTTP_RESP_INTERNAL_SERVER_ERROR,
  154. "Cannot start thread.");
  155. return;
  156. }
  157. } else {
  158. lem->running_time = 0;
  159. if (period > 0) // user is modifying period to run
  160. lem->lifetime = period;
  161. #ifdef NETDATA_INTERNAL_CHECKS
  162. netdata_log_info("Thread %s had lifetime updated for %d", thread_name, period);
  163. #endif
  164. }
  165. pthread_mutex_unlock(&ebpf_exit_cleanup);
  166. } else if(strncmp(keyword, EBPF_THREADS_DISABLE_CATEGORY, sizeof(EBPF_THREADS_DISABLE_CATEGORY) -1) == 0) {
  167. const char *name = &keyword[sizeof(EBPF_THREADS_DISABLE_CATEGORY) - 1];
  168. lem = ebpf_functions_select_module(name);
  169. if (!lem) {
  170. snprintfz(message, sizeof(message) - 1, "%s%s", EBPF_PLUGIN_THREAD_FUNCTION_ERROR_THREAD_NOT_FOUND, name);
  171. ebpf_function_error(transaction, HTTP_RESP_NOT_FOUND, message);
  172. return;
  173. }
  174. pthread_mutex_lock(&ebpf_exit_cleanup);
  175. if (lem->enabled < NETDATA_THREAD_EBPF_STOPPING && lem->thread->thread) {
  176. lem->lifetime = 0;
  177. lem->running_time = lem->update_every;
  178. netdata_thread_cancel(*lem->thread->thread);
  179. }
  180. pthread_mutex_unlock(&ebpf_exit_cleanup);
  181. } else if(strncmp(keyword, EBPF_THREADS_SELECT_THREAD, sizeof(EBPF_THREADS_SELECT_THREAD) -1) == 0) {
  182. const char *name = &keyword[sizeof(EBPF_THREADS_SELECT_THREAD) - 1];
  183. lem = ebpf_functions_select_module(name);
  184. if (!lem) {
  185. snprintfz(message, sizeof(message) - 1, "%s%s", EBPF_PLUGIN_THREAD_FUNCTION_ERROR_THREAD_NOT_FOUND, name);
  186. ebpf_function_error(transaction, HTTP_RESP_NOT_FOUND, message);
  187. return;
  188. }
  189. show_specific_thread |= 1<<lem->thread_id;
  190. } else if(strncmp(keyword, "help", 4) == 0) {
  191. ebpf_function_thread_manipulation_help(transaction);
  192. return;
  193. }
  194. }
  195. time_t expires = now_realtime_sec() + em->update_every;
  196. BUFFER *wb = buffer_create(PLUGINSD_LINE_MAX, NULL);
  197. buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS);
  198. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  199. buffer_json_member_add_string(wb, "type", "table");
  200. buffer_json_member_add_time_t(wb, "update_every", em->update_every);
  201. buffer_json_member_add_string(wb, "help", EBPF_PLUGIN_THREAD_FUNCTION_DESCRIPTION);
  202. // Collect data
  203. buffer_json_member_add_array(wb, "data");
  204. int i;
  205. for (i = 0; i < EBPF_MODULE_FUNCTION_IDX; i++) {
  206. if (show_specific_thread && !(show_specific_thread & 1<<i))
  207. continue;
  208. ebpf_module_t *wem = &ebpf_modules[i];
  209. buffer_json_add_array_item_array(wb);
  210. // IMPORTANT!
  211. // THE ORDER SHOULD BE THE SAME WITH THE FIELDS!
  212. // thread name
  213. buffer_json_add_array_item_string(wb, wem->info.thread_name);
  214. // description
  215. buffer_json_add_array_item_string(wb, wem->info.thread_description);
  216. // Either it is not running or received a disabled signal and it is stopping.
  217. if (wem->enabled > NETDATA_THREAD_EBPF_FUNCTION_RUNNING ||
  218. (!wem->lifetime && (int)wem->running_time == wem->update_every)) {
  219. // status
  220. buffer_json_add_array_item_string(wb, EBPF_THREAD_STATUS_STOPPED);
  221. // Time remaining
  222. buffer_json_add_array_item_uint64(wb, 0);
  223. // action
  224. buffer_json_add_array_item_string(wb, "NULL");
  225. } else {
  226. // status
  227. buffer_json_add_array_item_string(wb, EBPF_THREAD_STATUS_RUNNING);
  228. // Time remaining
  229. buffer_json_add_array_item_uint64(wb, (wem->lifetime) ? (wem->lifetime - wem->running_time) : 0);
  230. // action
  231. buffer_json_add_array_item_string(wb, "Enabled/Disabled");
  232. }
  233. buffer_json_array_close(wb);
  234. }
  235. buffer_json_array_close(wb); // data
  236. buffer_json_member_add_object(wb, "columns");
  237. {
  238. int fields_id = 0;
  239. // IMPORTANT!
  240. // THE ORDER SHOULD BE THE SAME WITH THE VALUES!
  241. buffer_rrdf_table_add_field(wb, fields_id++, "Thread", "Thread Name", RRDF_FIELD_TYPE_STRING,
  242. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  243. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  244. RRDF_FIELD_FILTER_MULTISELECT,
  245. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY | RRDF_FIELD_OPTS_UNIQUE_KEY, NULL);
  246. buffer_rrdf_table_add_field(wb, fields_id++, "Description", "Thread Desc", RRDF_FIELD_TYPE_STRING,
  247. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  248. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  249. RRDF_FIELD_FILTER_MULTISELECT,
  250. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  251. buffer_rrdf_table_add_field(wb, fields_id++, "Status", "Thread Status", RRDF_FIELD_TYPE_STRING,
  252. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  253. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  254. RRDF_FIELD_FILTER_MULTISELECT,
  255. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  256. buffer_rrdf_table_add_field(wb, fields_id++, "Time", "Time Remaining", RRDF_FIELD_TYPE_INTEGER,
  257. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL,
  258. NAN, RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  259. RRDF_FIELD_FILTER_MULTISELECT,
  260. RRDF_FIELD_OPTS_NONE, NULL);
  261. buffer_rrdf_table_add_field(wb, fields_id++, "Action", "Thread Action", RRDF_FIELD_TYPE_STRING,
  262. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  263. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  264. RRDF_FIELD_FILTER_MULTISELECT,
  265. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  266. }
  267. buffer_json_object_close(wb); // columns
  268. buffer_json_member_add_string(wb, "default_sort_column", "Thread");
  269. buffer_json_member_add_object(wb, "charts");
  270. {
  271. // Threads
  272. buffer_json_member_add_object(wb, "eBPFThreads");
  273. {
  274. buffer_json_member_add_string(wb, "name", "Threads");
  275. buffer_json_member_add_string(wb, "type", "line");
  276. buffer_json_member_add_array(wb, "columns");
  277. {
  278. buffer_json_add_array_item_string(wb, "Threads");
  279. }
  280. buffer_json_array_close(wb);
  281. }
  282. buffer_json_object_close(wb);
  283. // Life Time
  284. buffer_json_member_add_object(wb, "eBPFLifeTime");
  285. {
  286. buffer_json_member_add_string(wb, "name", "LifeTime");
  287. buffer_json_member_add_string(wb, "type", "line");
  288. buffer_json_member_add_array(wb, "columns");
  289. {
  290. buffer_json_add_array_item_string(wb, "Threads");
  291. buffer_json_add_array_item_string(wb, "Time");
  292. }
  293. buffer_json_array_close(wb);
  294. }
  295. buffer_json_object_close(wb);
  296. }
  297. buffer_json_object_close(wb); // charts
  298. // Do we use only on fields that can be groupped?
  299. buffer_json_member_add_object(wb, "group_by");
  300. {
  301. // group by Status
  302. buffer_json_member_add_object(wb, "Status");
  303. {
  304. buffer_json_member_add_string(wb, "name", "Thread status");
  305. buffer_json_member_add_array(wb, "columns");
  306. {
  307. buffer_json_add_array_item_string(wb, "Status");
  308. }
  309. buffer_json_array_close(wb);
  310. }
  311. buffer_json_object_close(wb);
  312. }
  313. buffer_json_object_close(wb); // group_by
  314. buffer_json_member_add_time_t(wb, "expires", expires);
  315. buffer_json_finalize(wb);
  316. // Lock necessary to avoid race condition
  317. pluginsd_function_result_to_stdout(transaction, HTTP_RESP_OK, "application/json", expires, wb);
  318. buffer_free(wb);
  319. }
  320. */
  321. /*****************************************************************
  322. * EBPF SOCKET FUNCTION
  323. *****************************************************************/
  324. /**
  325. * Thread Help
  326. *
  327. * Shows help with all options accepted by thread function.
  328. *
  329. * @param transaction the transaction id that Netdata sent for this function execution
  330. */
  331. static void ebpf_function_socket_help(const char *transaction) {
  332. pluginsd_function_result_begin_to_stdout(transaction, HTTP_RESP_OK, "text/plain", now_realtime_sec() + 3600);
  333. fprintf(stdout, "%s",
  334. "ebpf.plugin / socket\n"
  335. "\n"
  336. "Function `socket` display information for all open sockets during ebpf.plugin runtime.\n"
  337. "During thread runtime the plugin is always collecting data, but when an option is modified, the plugin\n"
  338. "resets completely the previous table and can show a clean data for the first request before to bring the\n"
  339. "modified request.\n"
  340. "\n"
  341. "The following filters are supported:\n"
  342. "\n"
  343. " family:FAMILY\n"
  344. " Shows information for the FAMILY specified. Option accepts IPV4, IPV6 and all, that is the default.\n"
  345. "\n"
  346. " period:PERIOD\n"
  347. " Enable socket to run a specific PERIOD in seconds. When PERIOD is not\n"
  348. " specified plugin will use the default 300 seconds\n"
  349. "\n"
  350. " resolve:BOOL\n"
  351. " Resolve service name, default value is YES.\n"
  352. "\n"
  353. " range:CIDR\n"
  354. " Show sockets that have only a specific destination. Default all addresses.\n"
  355. "\n"
  356. " port:range\n"
  357. " Show sockets that have only a specific destination.\n"
  358. "\n"
  359. " reset\n"
  360. " Send a reset to collector. When a collector receives this command, it uses everything defined in configuration file.\n"
  361. "\n"
  362. " interfaces\n"
  363. " When the collector receives this command, it read all available interfaces on host.\n"
  364. "\n"
  365. "Filters can be combined. Each filter can be given only one time. Default all ports\n"
  366. );
  367. pluginsd_function_result_end_to_stdout();
  368. fflush(stdout);
  369. }
  370. /**
  371. * Fill Fake socket
  372. *
  373. * Fill socket with an invalid request.
  374. *
  375. * @param fake_values is the structure where we are storing the value.
  376. */
  377. static inline void ebpf_socket_fill_fake_socket(netdata_socket_plus_t *fake_values)
  378. {
  379. snprintfz(fake_values->socket_string.src_ip, INET6_ADDRSTRLEN, "%s", "127.0.0.1");
  380. snprintfz(fake_values->socket_string.dst_ip, INET6_ADDRSTRLEN, "%s", "127.0.0.1");
  381. fake_values->pid = getpid();
  382. //fake_values->socket_string.src_port = 0;
  383. fake_values->socket_string.dst_port[0] = 0;
  384. snprintfz(fake_values->socket_string.dst_ip, NI_MAXSERV, "%s", "none");
  385. fake_values->data.family = AF_INET;
  386. fake_values->data.protocol = AF_UNSPEC;
  387. }
  388. /**
  389. * Fill function buffer
  390. *
  391. * Fill buffer with data to be shown on cloud.
  392. *
  393. * @param wb buffer where we store data.
  394. * @param values data read from hash table
  395. * @param name the process name
  396. */
  397. static void ebpf_fill_function_buffer(BUFFER *wb, netdata_socket_plus_t *values, char *name)
  398. {
  399. buffer_json_add_array_item_array(wb);
  400. // IMPORTANT!
  401. // THE ORDER SHOULD BE THE SAME WITH THE FIELDS!
  402. // PID
  403. buffer_json_add_array_item_uint64(wb, (uint64_t)values->pid);
  404. // NAME
  405. buffer_json_add_array_item_string(wb, (name) ? name : "not identified");
  406. // Origin
  407. buffer_json_add_array_item_string(wb, (values->data.external_origin) ? "incoming" : "outgoing");
  408. // Source IP
  409. buffer_json_add_array_item_string(wb, values->socket_string.src_ip);
  410. // SRC Port
  411. //buffer_json_add_array_item_uint64(wb, (uint64_t) values->socket_string.src_port);
  412. // Destination IP
  413. buffer_json_add_array_item_string(wb, values->socket_string.dst_ip);
  414. // DST Port
  415. buffer_json_add_array_item_string(wb, values->socket_string.dst_port);
  416. uint64_t connections;
  417. if (values->data.protocol == IPPROTO_TCP) {
  418. // Protocol
  419. buffer_json_add_array_item_string(wb, "TCP");
  420. // Bytes received
  421. buffer_json_add_array_item_uint64(wb, (uint64_t) values->data.tcp.tcp_bytes_received);
  422. // Bytes sent
  423. buffer_json_add_array_item_uint64(wb, (uint64_t) values->data.tcp.tcp_bytes_sent);
  424. // Connections
  425. connections = values->data.tcp.ipv4_connect + values->data.tcp.ipv6_connect;
  426. } else if (values->data.protocol == IPPROTO_UDP) {
  427. // Protocol
  428. buffer_json_add_array_item_string(wb, "UDP");
  429. // Bytes received
  430. buffer_json_add_array_item_uint64(wb, (uint64_t) values->data.udp.udp_bytes_received);
  431. // Bytes sent
  432. buffer_json_add_array_item_uint64(wb, (uint64_t) values->data.udp.udp_bytes_sent);
  433. // Connections
  434. connections = values->data.udp.call_udp_sent + values->data.udp.call_udp_received;
  435. } else {
  436. // Protocol
  437. buffer_json_add_array_item_string(wb, "UNSPEC");
  438. // Bytes received
  439. buffer_json_add_array_item_uint64(wb, 0);
  440. // Bytes sent
  441. buffer_json_add_array_item_uint64(wb, 0);
  442. connections = 1;
  443. }
  444. // Connections
  445. if (values->flags & NETDATA_SOCKET_FLAGS_ALREADY_OPEN) {
  446. connections++;
  447. } else if (!connections) {
  448. // If no connections, this means that we lost when connection was opened
  449. values->flags |= NETDATA_SOCKET_FLAGS_ALREADY_OPEN;
  450. connections++;
  451. }
  452. buffer_json_add_array_item_uint64(wb, connections);
  453. buffer_json_array_close(wb);
  454. }
  455. /**
  456. * Clean Judy array unsafe
  457. *
  458. * Clean all Judy Array allocated to show table when a function is called.
  459. * Before to call this function it is necessary to lock `ebpf_judy_pid.index.rw_spinlock`.
  460. **/
  461. static void ebpf_socket_clean_judy_array_unsafe()
  462. {
  463. if (!ebpf_judy_pid.index.JudyLArray)
  464. return;
  465. Pvoid_t *pid_value, *socket_value;
  466. Word_t local_pid = 0, local_socket = 0;
  467. bool first_pid = true, first_socket = true;
  468. while ((pid_value = JudyLFirstThenNext(ebpf_judy_pid.index.JudyLArray, &local_pid, &first_pid))) {
  469. netdata_ebpf_judy_pid_stats_t *pid_ptr = (netdata_ebpf_judy_pid_stats_t *)*pid_value;
  470. rw_spinlock_write_lock(&pid_ptr->socket_stats.rw_spinlock);
  471. if (pid_ptr->socket_stats.JudyLArray) {
  472. while ((socket_value = JudyLFirstThenNext(pid_ptr->socket_stats.JudyLArray, &local_socket, &first_socket))) {
  473. netdata_socket_plus_t *socket_clean = *socket_value;
  474. aral_freez(aral_socket_table, socket_clean);
  475. }
  476. JudyLFreeArray(&pid_ptr->socket_stats.JudyLArray, PJE0);
  477. pid_ptr->socket_stats.JudyLArray = NULL;
  478. }
  479. rw_spinlock_write_unlock(&pid_ptr->socket_stats.rw_spinlock);
  480. }
  481. }
  482. /**
  483. * Fill function buffer unsafe
  484. *
  485. * Fill the function buffer with socket information. Before to call this function it is necessary to lock
  486. * ebpf_judy_pid.index.rw_spinlock
  487. *
  488. * @param buf buffer used to store data to be shown by function.
  489. *
  490. * @return it returns 0 on success and -1 otherwise.
  491. */
  492. static void ebpf_socket_fill_function_buffer_unsafe(BUFFER *buf)
  493. {
  494. int counter = 0;
  495. Pvoid_t *pid_value, *socket_value;
  496. Word_t local_pid = 0;
  497. bool first_pid = true;
  498. while ((pid_value = JudyLFirstThenNext(ebpf_judy_pid.index.JudyLArray, &local_pid, &first_pid))) {
  499. netdata_ebpf_judy_pid_stats_t *pid_ptr = (netdata_ebpf_judy_pid_stats_t *)*pid_value;
  500. bool first_socket = true;
  501. Word_t local_timestamp = 0;
  502. rw_spinlock_read_lock(&pid_ptr->socket_stats.rw_spinlock);
  503. if (pid_ptr->socket_stats.JudyLArray) {
  504. while ((socket_value = JudyLFirstThenNext(pid_ptr->socket_stats.JudyLArray, &local_timestamp, &first_socket))) {
  505. netdata_socket_plus_t *values = (netdata_socket_plus_t *)*socket_value;
  506. ebpf_fill_function_buffer(buf, values, pid_ptr->cmdline);
  507. }
  508. counter++;
  509. }
  510. rw_spinlock_read_unlock(&pid_ptr->socket_stats.rw_spinlock);
  511. }
  512. if (!counter) {
  513. netdata_socket_plus_t fake_values = { };
  514. ebpf_socket_fill_fake_socket(&fake_values);
  515. ebpf_fill_function_buffer(buf, &fake_values, NULL);
  516. }
  517. }
  518. /**
  519. * Socket read hash
  520. *
  521. * This is the thread callback.
  522. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  523. *
  524. * @param buf the buffer to store data;
  525. * @param em the module main structure.
  526. *
  527. * @return It always returns NULL.
  528. */
  529. void ebpf_socket_read_open_connections(BUFFER *buf, struct ebpf_module *em)
  530. {
  531. // thread was not initialized or Array was reset
  532. rw_spinlock_read_lock(&ebpf_judy_pid.index.rw_spinlock);
  533. if (!em->maps || (em->maps[NETDATA_SOCKET_OPEN_SOCKET].map_fd == ND_EBPF_MAP_FD_NOT_INITIALIZED) ||
  534. !ebpf_judy_pid.index.JudyLArray){
  535. netdata_socket_plus_t fake_values = { };
  536. ebpf_socket_fill_fake_socket(&fake_values);
  537. ebpf_fill_function_buffer(buf, &fake_values, NULL);
  538. rw_spinlock_read_unlock(&ebpf_judy_pid.index.rw_spinlock);
  539. return;
  540. }
  541. rw_spinlock_read_lock(&network_viewer_opt.rw_spinlock);
  542. ebpf_socket_fill_function_buffer_unsafe(buf);
  543. rw_spinlock_read_unlock(&network_viewer_opt.rw_spinlock);
  544. rw_spinlock_read_unlock(&ebpf_judy_pid.index.rw_spinlock);
  545. }
  546. /**
  547. * Function: Socket
  548. *
  549. * Show information for sockets stored in hash tables.
  550. *
  551. * @param transaction the transaction id that Netdata sent for this function execution
  552. * @param function function name and arguments given to thread.
  553. * @param timeout The function timeout
  554. * @param cancelled Variable used to store function status.
  555. */
  556. static void ebpf_function_socket_manipulation(const char *transaction,
  557. char *function __maybe_unused,
  558. int timeout __maybe_unused,
  559. bool *cancelled __maybe_unused)
  560. {
  561. UNUSED(timeout);
  562. ebpf_module_t *em = &ebpf_modules[EBPF_MODULE_SOCKET_IDX];
  563. char *words[PLUGINSD_MAX_WORDS] = {NULL};
  564. size_t num_words = quoted_strings_splitter_pluginsd(function, words, PLUGINSD_MAX_WORDS);
  565. const char *name;
  566. int period = -1;
  567. rw_spinlock_write_lock(&ebpf_judy_pid.index.rw_spinlock);
  568. network_viewer_opt.enabled = CONFIG_BOOLEAN_YES;
  569. uint32_t previous;
  570. for (int i = 1; i < PLUGINSD_MAX_WORDS; i++) {
  571. const char *keyword = get_word(words, num_words, i);
  572. if (!keyword)
  573. break;
  574. if (strncmp(keyword, EBPF_FUNCTION_SOCKET_FAMILY, sizeof(EBPF_FUNCTION_SOCKET_FAMILY) - 1) == 0) {
  575. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_FAMILY) - 1];
  576. previous = network_viewer_opt.family;
  577. uint32_t family = AF_UNSPEC;
  578. if (!strcmp(name, "IPV4"))
  579. family = AF_INET;
  580. else if (!strcmp(name, "IPV6"))
  581. family = AF_INET6;
  582. if (family != previous) {
  583. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  584. network_viewer_opt.family = family;
  585. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  586. ebpf_socket_clean_judy_array_unsafe();
  587. }
  588. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_PERIOD, sizeof(EBPF_FUNCTION_SOCKET_PERIOD) - 1) == 0) {
  589. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_PERIOD) - 1];
  590. pthread_mutex_lock(&ebpf_exit_cleanup);
  591. period = str2i(name);
  592. if (period > 0) {
  593. em->lifetime = period;
  594. } else
  595. em->lifetime = EBPF_NON_FUNCTION_LIFE_TIME;
  596. #ifdef NETDATA_DEV_MODE
  597. collector_info("Lifetime modified for %u", em->lifetime);
  598. #endif
  599. pthread_mutex_unlock(&ebpf_exit_cleanup);
  600. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_RESOLVE, sizeof(EBPF_FUNCTION_SOCKET_RESOLVE) - 1) == 0) {
  601. previous = network_viewer_opt.service_resolution_enabled;
  602. uint32_t resolution;
  603. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_RESOLVE) - 1];
  604. resolution = (!strcasecmp(name, "YES")) ? CONFIG_BOOLEAN_YES : CONFIG_BOOLEAN_NO;
  605. if (previous != resolution) {
  606. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  607. network_viewer_opt.service_resolution_enabled = resolution;
  608. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  609. ebpf_socket_clean_judy_array_unsafe();
  610. }
  611. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_RANGE, sizeof(EBPF_FUNCTION_SOCKET_RANGE) - 1) == 0) {
  612. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_RANGE) - 1];
  613. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  614. ebpf_clean_ip_structure(&network_viewer_opt.included_ips);
  615. ebpf_clean_ip_structure(&network_viewer_opt.excluded_ips);
  616. ebpf_parse_ips_unsafe((char *)name);
  617. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  618. ebpf_socket_clean_judy_array_unsafe();
  619. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_PORT, sizeof(EBPF_FUNCTION_SOCKET_PORT) - 1) == 0) {
  620. name = &keyword[sizeof(EBPF_FUNCTION_SOCKET_PORT) - 1];
  621. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  622. ebpf_clean_port_structure(&network_viewer_opt.included_port);
  623. ebpf_clean_port_structure(&network_viewer_opt.excluded_port);
  624. ebpf_parse_ports((char *)name);
  625. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  626. ebpf_socket_clean_judy_array_unsafe();
  627. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_RESET, sizeof(EBPF_FUNCTION_SOCKET_RESET) - 1) == 0) {
  628. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  629. ebpf_clean_port_structure(&network_viewer_opt.included_port);
  630. ebpf_clean_port_structure(&network_viewer_opt.excluded_port);
  631. ebpf_clean_ip_structure(&network_viewer_opt.included_ips);
  632. ebpf_clean_ip_structure(&network_viewer_opt.excluded_ips);
  633. ebpf_clean_ip_structure(&network_viewer_opt.ipv4_local_ip);
  634. ebpf_clean_ip_structure(&network_viewer_opt.ipv6_local_ip);
  635. parse_network_viewer_section(&socket_config);
  636. ebpf_read_local_addresses_unsafe();
  637. network_viewer_opt.enabled = CONFIG_BOOLEAN_YES;
  638. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  639. } else if (strncmp(keyword, EBPF_FUNCTION_SOCKET_INTERFACES, sizeof(EBPF_FUNCTION_SOCKET_INTERFACES) - 1) == 0) {
  640. rw_spinlock_write_lock(&network_viewer_opt.rw_spinlock);
  641. ebpf_read_local_addresses_unsafe();
  642. rw_spinlock_write_unlock(&network_viewer_opt.rw_spinlock);
  643. } else if (strncmp(keyword, "help", 4) == 0) {
  644. ebpf_function_socket_help(transaction);
  645. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  646. return;
  647. }
  648. }
  649. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  650. pthread_mutex_lock(&ebpf_exit_cleanup);
  651. if (em->enabled > NETDATA_THREAD_EBPF_FUNCTION_RUNNING) {
  652. // Cleanup when we already had a thread running
  653. rw_spinlock_write_lock(&ebpf_judy_pid.index.rw_spinlock);
  654. ebpf_socket_clean_judy_array_unsafe();
  655. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  656. if (ebpf_function_start_thread(em, period)) {
  657. ebpf_function_error(transaction,
  658. HTTP_RESP_INTERNAL_SERVER_ERROR,
  659. "Cannot start thread.");
  660. pthread_mutex_unlock(&ebpf_exit_cleanup);
  661. return;
  662. }
  663. } else {
  664. if (period < 0 && em->lifetime < EBPF_NON_FUNCTION_LIFE_TIME) {
  665. em->lifetime = EBPF_NON_FUNCTION_LIFE_TIME;
  666. }
  667. }
  668. pthread_mutex_unlock(&ebpf_exit_cleanup);
  669. time_t expires = now_realtime_sec() + em->update_every;
  670. BUFFER *wb = buffer_create(PLUGINSD_LINE_MAX, NULL);
  671. buffer_json_initialize(wb, "\"", "\"", 0, true, false);
  672. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  673. buffer_json_member_add_string(wb, "type", "table");
  674. buffer_json_member_add_time_t(wb, "update_every", em->update_every);
  675. buffer_json_member_add_string(wb, "help", EBPF_PLUGIN_SOCKET_FUNCTION_DESCRIPTION);
  676. // Collect data
  677. buffer_json_member_add_array(wb, "data");
  678. ebpf_socket_read_open_connections(wb, em);
  679. buffer_json_array_close(wb); // data
  680. buffer_json_member_add_object(wb, "columns");
  681. {
  682. int fields_id = 0;
  683. // IMPORTANT!
  684. // THE ORDER SHOULD BE THE SAME WITH THE VALUES!
  685. buffer_rrdf_table_add_field(wb, fields_id++, "PID", "Process ID", RRDF_FIELD_TYPE_INTEGER,
  686. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  687. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  688. RRDF_FIELD_FILTER_MULTISELECT,
  689. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY,
  690. NULL);
  691. buffer_rrdf_table_add_field(wb, fields_id++, "Process Name", "Process Name", RRDF_FIELD_TYPE_STRING,
  692. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  693. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  694. RRDF_FIELD_FILTER_MULTISELECT,
  695. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  696. buffer_rrdf_table_add_field(wb, fields_id++, "Origin", "The connection origin.", RRDF_FIELD_TYPE_STRING,
  697. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  698. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  699. RRDF_FIELD_FILTER_MULTISELECT,
  700. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  701. buffer_rrdf_table_add_field(wb, fields_id++, "Request from", "Request from IP", RRDF_FIELD_TYPE_STRING,
  702. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  703. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  704. RRDF_FIELD_FILTER_MULTISELECT,
  705. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  706. /*
  707. buffer_rrdf_table_add_field(wb, fields_id++, "SRC PORT", "Source Port", RRDF_FIELD_TYPE_INTEGER,
  708. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  709. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  710. RRDF_FIELD_FILTER_MULTISELECT,
  711. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY,
  712. NULL);
  713. */
  714. buffer_rrdf_table_add_field(wb, fields_id++, "Destination IP", "Destination IP", RRDF_FIELD_TYPE_STRING,
  715. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  716. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  717. RRDF_FIELD_FILTER_MULTISELECT,
  718. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  719. buffer_rrdf_table_add_field(wb, fields_id++, "Destination Port", "Destination Port", RRDF_FIELD_TYPE_STRING,
  720. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  721. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  722. RRDF_FIELD_FILTER_MULTISELECT,
  723. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  724. buffer_rrdf_table_add_field(wb, fields_id++, "Protocol", "Communication protocol", RRDF_FIELD_TYPE_STRING,
  725. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NONE, 0, NULL, NAN,
  726. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  727. RRDF_FIELD_FILTER_MULTISELECT,
  728. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY, NULL);
  729. buffer_rrdf_table_add_field(wb, fields_id++, "Incoming Bandwidth", "Bytes received.", RRDF_FIELD_TYPE_INTEGER,
  730. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  731. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  732. RRDF_FIELD_FILTER_MULTISELECT,
  733. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY,
  734. NULL);
  735. buffer_rrdf_table_add_field(wb, fields_id++, "Outgoing Bandwidth", "Bytes sent.", RRDF_FIELD_TYPE_INTEGER,
  736. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  737. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  738. RRDF_FIELD_FILTER_MULTISELECT,
  739. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY,
  740. NULL);
  741. buffer_rrdf_table_add_field(wb, fields_id, "Connections", "Number of calls to tcp_vX_connections and udp_sendmsg, where X is the protocol version.", RRDF_FIELD_TYPE_INTEGER,
  742. RRDF_FIELD_VISUAL_VALUE, RRDF_FIELD_TRANSFORM_NUMBER, 0, NULL, NAN,
  743. RRDF_FIELD_SORT_ASCENDING, NULL, RRDF_FIELD_SUMMARY_COUNT,
  744. RRDF_FIELD_FILTER_MULTISELECT,
  745. RRDF_FIELD_OPTS_VISIBLE | RRDF_FIELD_OPTS_STICKY,
  746. NULL);
  747. }
  748. buffer_json_object_close(wb); // columns
  749. buffer_json_member_add_object(wb, "charts");
  750. {
  751. // OutBound Connections
  752. buffer_json_member_add_object(wb, "IPInboundConn");
  753. {
  754. buffer_json_member_add_string(wb, "name", "TCP Inbound Connection");
  755. buffer_json_member_add_string(wb, "type", "line");
  756. buffer_json_member_add_array(wb, "columns");
  757. {
  758. buffer_json_add_array_item_string(wb, "connected_tcp");
  759. buffer_json_add_array_item_string(wb, "connected_udp");
  760. }
  761. buffer_json_array_close(wb);
  762. }
  763. buffer_json_object_close(wb);
  764. // OutBound Connections
  765. buffer_json_member_add_object(wb, "IPTCPOutboundConn");
  766. {
  767. buffer_json_member_add_string(wb, "name", "TCP Outbound Connection");
  768. buffer_json_member_add_string(wb, "type", "line");
  769. buffer_json_member_add_array(wb, "columns");
  770. {
  771. buffer_json_add_array_item_string(wb, "connected_V4");
  772. buffer_json_add_array_item_string(wb, "connected_V6");
  773. }
  774. buffer_json_array_close(wb);
  775. }
  776. buffer_json_object_close(wb);
  777. // TCP Functions
  778. buffer_json_member_add_object(wb, "TCPFunctions");
  779. {
  780. buffer_json_member_add_string(wb, "name", "TCPFunctions");
  781. buffer_json_member_add_string(wb, "type", "line");
  782. buffer_json_member_add_array(wb, "columns");
  783. {
  784. buffer_json_add_array_item_string(wb, "received");
  785. buffer_json_add_array_item_string(wb, "sent");
  786. buffer_json_add_array_item_string(wb, "close");
  787. }
  788. buffer_json_array_close(wb);
  789. }
  790. buffer_json_object_close(wb);
  791. // TCP Bandwidth
  792. buffer_json_member_add_object(wb, "TCPBandwidth");
  793. {
  794. buffer_json_member_add_string(wb, "name", "TCPBandwidth");
  795. buffer_json_member_add_string(wb, "type", "line");
  796. buffer_json_member_add_array(wb, "columns");
  797. {
  798. buffer_json_add_array_item_string(wb, "received");
  799. buffer_json_add_array_item_string(wb, "sent");
  800. }
  801. buffer_json_array_close(wb);
  802. }
  803. buffer_json_object_close(wb);
  804. // UDP Functions
  805. buffer_json_member_add_object(wb, "UDPFunctions");
  806. {
  807. buffer_json_member_add_string(wb, "name", "UDPFunctions");
  808. buffer_json_member_add_string(wb, "type", "line");
  809. buffer_json_member_add_array(wb, "columns");
  810. {
  811. buffer_json_add_array_item_string(wb, "received");
  812. buffer_json_add_array_item_string(wb, "sent");
  813. }
  814. buffer_json_array_close(wb);
  815. }
  816. buffer_json_object_close(wb);
  817. // UDP Bandwidth
  818. buffer_json_member_add_object(wb, "UDPBandwidth");
  819. {
  820. buffer_json_member_add_string(wb, "name", "UDPBandwidth");
  821. buffer_json_member_add_string(wb, "type", "line");
  822. buffer_json_member_add_array(wb, "columns");
  823. {
  824. buffer_json_add_array_item_string(wb, "received");
  825. buffer_json_add_array_item_string(wb, "sent");
  826. }
  827. buffer_json_array_close(wb);
  828. }
  829. buffer_json_object_close(wb);
  830. }
  831. buffer_json_object_close(wb); // charts
  832. buffer_json_member_add_string(wb, "default_sort_column", "PID");
  833. // Do we use only on fields that can be groupped?
  834. buffer_json_member_add_object(wb, "group_by");
  835. {
  836. // group by PID
  837. buffer_json_member_add_object(wb, "PID");
  838. {
  839. buffer_json_member_add_string(wb, "name", "Process ID");
  840. buffer_json_member_add_array(wb, "columns");
  841. {
  842. buffer_json_add_array_item_string(wb, "PID");
  843. }
  844. buffer_json_array_close(wb);
  845. }
  846. buffer_json_object_close(wb);
  847. // group by Process Name
  848. buffer_json_member_add_object(wb, "Process Name");
  849. {
  850. buffer_json_member_add_string(wb, "name", "Process Name");
  851. buffer_json_member_add_array(wb, "columns");
  852. {
  853. buffer_json_add_array_item_string(wb, "Process Name");
  854. }
  855. buffer_json_array_close(wb);
  856. }
  857. buffer_json_object_close(wb);
  858. // group by Process Name
  859. buffer_json_member_add_object(wb, "Origin");
  860. {
  861. buffer_json_member_add_string(wb, "name", "Origin");
  862. buffer_json_member_add_array(wb, "columns");
  863. {
  864. buffer_json_add_array_item_string(wb, "Origin");
  865. }
  866. buffer_json_array_close(wb);
  867. }
  868. buffer_json_object_close(wb);
  869. // group by Request From IP
  870. buffer_json_member_add_object(wb, "Request from");
  871. {
  872. buffer_json_member_add_string(wb, "name", "Request from IP");
  873. buffer_json_member_add_array(wb, "columns");
  874. {
  875. buffer_json_add_array_item_string(wb, "Request from");
  876. }
  877. buffer_json_array_close(wb);
  878. }
  879. buffer_json_object_close(wb);
  880. // group by Destination IP
  881. buffer_json_member_add_object(wb, "Destination IP");
  882. {
  883. buffer_json_member_add_string(wb, "name", "Destination IP");
  884. buffer_json_member_add_array(wb, "columns");
  885. {
  886. buffer_json_add_array_item_string(wb, "Destination IP");
  887. }
  888. buffer_json_array_close(wb);
  889. }
  890. buffer_json_object_close(wb);
  891. // group by DST Port
  892. buffer_json_member_add_object(wb, "Destination Port");
  893. {
  894. buffer_json_member_add_string(wb, "name", "Destination Port");
  895. buffer_json_member_add_array(wb, "columns");
  896. {
  897. buffer_json_add_array_item_string(wb, "Destination Port");
  898. }
  899. buffer_json_array_close(wb);
  900. }
  901. buffer_json_object_close(wb);
  902. // group by Protocol
  903. buffer_json_member_add_object(wb, "Protocol");
  904. {
  905. buffer_json_member_add_string(wb, "name", "Protocol");
  906. buffer_json_member_add_array(wb, "columns");
  907. {
  908. buffer_json_add_array_item_string(wb, "Protocol");
  909. }
  910. buffer_json_array_close(wb);
  911. }
  912. buffer_json_object_close(wb);
  913. }
  914. buffer_json_object_close(wb); // group_by
  915. buffer_json_member_add_time_t(wb, "expires", expires);
  916. buffer_json_finalize(wb);
  917. // Lock necessary to avoid race condition
  918. pluginsd_function_result_begin_to_stdout(transaction, HTTP_RESP_OK, "application/json", expires);
  919. fwrite(buffer_tostring(wb), buffer_strlen(wb), 1, stdout);
  920. pluginsd_function_result_end_to_stdout();
  921. fflush(stdout);
  922. buffer_free(wb);
  923. }
  924. /*****************************************************************
  925. * EBPF FUNCTION THREAD
  926. *****************************************************************/
  927. /**
  928. * FUNCTION thread.
  929. *
  930. * @param ptr a `ebpf_module_t *`.
  931. *
  932. * @return always NULL.
  933. */
  934. void *ebpf_function_thread(void *ptr)
  935. {
  936. (void)ptr;
  937. struct functions_evloop_globals *wg = functions_evloop_init(1,
  938. "EBPF",
  939. &lock,
  940. &ebpf_plugin_exit);
  941. functions_evloop_add_function(wg,
  942. "ebpf_socket",
  943. ebpf_function_socket_manipulation,
  944. PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT);
  945. heartbeat_t hb;
  946. heartbeat_init(&hb);
  947. while(!ebpf_plugin_exit) {
  948. (void)heartbeat_next(&hb, USEC_PER_SEC);
  949. if (ebpf_plugin_exit) {
  950. break;
  951. }
  952. }
  953. return NULL;
  954. }