ebpf_socket.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/resource.h>
  3. #include "ebpf.h"
  4. #include "ebpf_socket.h"
  5. /*****************************************************************
  6. *
  7. * GLOBAL VARIABLES
  8. *
  9. *****************************************************************/
  10. static char *socket_dimension_names[NETDATA_MAX_SOCKET_VECTOR] = { "sent", "received", "close", "sent",
  11. "received", "retransmitted" };
  12. static char *socket_id_names[NETDATA_MAX_SOCKET_VECTOR] = { "tcp_sendmsg", "tcp_cleanup_rbuf", "tcp_close",
  13. "udp_sendmsg", "udp_recvmsg", "tcp_retransmit_skb" };
  14. static netdata_idx_t *socket_hash_values = NULL;
  15. static netdata_syscall_stat_t *socket_aggregated_data = NULL;
  16. static netdata_publish_syscall_t *socket_publish_aggregated = NULL;
  17. static ebpf_data_t socket_data;
  18. ebpf_socket_publish_apps_t **socket_bandwidth_curr = NULL;
  19. ebpf_socket_publish_apps_t **socket_bandwidth_prev = NULL;
  20. static ebpf_bandwidth_t *bandwidth_vector = NULL;
  21. static int socket_apps_created = 0;
  22. pthread_mutex_t nv_mutex;
  23. int wait_to_plot = 0;
  24. int read_thread_closed = 1;
  25. netdata_vector_plot_t inbound_vectors = { .plot = NULL, .next = 0, .last = 0 };
  26. netdata_vector_plot_t outbound_vectors = { .plot = NULL, .next = 0, .last = 0 };
  27. netdata_socket_t *socket_values;
  28. ebpf_network_viewer_port_list_t *listen_ports = NULL;
  29. static int *map_fd = NULL;
  30. static struct bpf_object *objects = NULL;
  31. static struct bpf_link **probe_links = NULL;
  32. /*****************************************************************
  33. *
  34. * PROCESS DATA AND SEND TO NETDATA
  35. *
  36. *****************************************************************/
  37. /**
  38. * Update publish structure before to send data to Netdata.
  39. *
  40. * @param publish the first output structure with independent dimensions
  41. * @param tcp structure to store IO from tcp sockets
  42. * @param udp structure to store IO from udp sockets
  43. * @param input the structure with the input data.
  44. */
  45. static void ebpf_update_global_publish(
  46. netdata_publish_syscall_t *publish, netdata_publish_vfs_common_t *tcp, netdata_publish_vfs_common_t *udp,
  47. netdata_syscall_stat_t *input)
  48. {
  49. netdata_publish_syscall_t *move = publish;
  50. while (move) {
  51. if (input->call != move->pcall) {
  52. // This condition happens to avoid initial values with dimensions higher than normal values.
  53. if (move->pcall) {
  54. move->ncall = (input->call > move->pcall) ? input->call - move->pcall : move->pcall - input->call;
  55. move->nbyte = (input->bytes > move->pbyte) ? input->bytes - move->pbyte : move->pbyte - input->bytes;
  56. move->nerr = (input->ecall > move->nerr) ? input->ecall - move->perr : move->perr - input->ecall;
  57. } else {
  58. move->ncall = 0;
  59. move->nbyte = 0;
  60. move->nerr = 0;
  61. }
  62. move->pcall = input->call;
  63. move->pbyte = input->bytes;
  64. move->perr = input->ecall;
  65. } else {
  66. move->ncall = 0;
  67. move->nbyte = 0;
  68. move->nerr = 0;
  69. }
  70. input = input->next;
  71. move = move->next;
  72. }
  73. tcp->write = -((long)publish[0].nbyte);
  74. tcp->read = (long)publish[1].nbyte;
  75. udp->write = -((long)publish[3].nbyte);
  76. udp->read = (long)publish[4].nbyte;
  77. }
  78. /**
  79. * Update Network Viewer plot data
  80. *
  81. * @param plot the structure where the data will be stored
  82. * @param sock the last update from the socket
  83. */
  84. static inline void update_nv_plot_data(netdata_plot_values_t *plot, netdata_socket_t *sock)
  85. {
  86. if (sock->ct > plot->last_time) {
  87. plot->last_time = sock->ct;
  88. plot->plot_recv_packets = sock->recv_packets;
  89. plot->plot_sent_packets = sock->sent_packets;
  90. plot->plot_recv_bytes = sock->recv_bytes;
  91. plot->plot_sent_bytes = sock->sent_bytes;
  92. plot->plot_retransmit = sock->retransmit;
  93. }
  94. sock->recv_packets = 0;
  95. sock->sent_packets = 0;
  96. sock->recv_bytes = 0;
  97. sock->sent_bytes = 0;
  98. sock->retransmit = 0;
  99. }
  100. /**
  101. * Calculate Network Viewer Plot
  102. *
  103. * Do math with collected values before to plot data.
  104. */
  105. static inline void calculate_nv_plot()
  106. {
  107. uint32_t i;
  108. uint32_t end = inbound_vectors.next;
  109. for (i = 0; i < end; i++) {
  110. update_nv_plot_data(&inbound_vectors.plot[i].plot, &inbound_vectors.plot[i].sock);
  111. }
  112. inbound_vectors.max_plot = end;
  113. // The 'Other' dimension is always calculated for the chart to have at least one dimension
  114. update_nv_plot_data(&inbound_vectors.plot[inbound_vectors.last].plot,
  115. &inbound_vectors.plot[inbound_vectors.last].sock);
  116. end = outbound_vectors.next;
  117. for (i = 0; i < end; i++) {
  118. update_nv_plot_data(&outbound_vectors.plot[i].plot, &outbound_vectors.plot[i].sock);
  119. }
  120. outbound_vectors.max_plot = end;
  121. // The 'Other' dimension is always calculated for the chart to have at least one dimension
  122. update_nv_plot_data(&outbound_vectors.plot[outbound_vectors.last].plot,
  123. &outbound_vectors.plot[outbound_vectors.last].sock);
  124. }
  125. /**
  126. * Network viewer send bytes
  127. *
  128. * @param ptr the structure with values to plot
  129. * @param chart the chart name.
  130. */
  131. static inline void ebpf_socket_nv_send_bytes(netdata_vector_plot_t *ptr, char *chart)
  132. {
  133. uint32_t i;
  134. uint32_t end = ptr->last_plot;
  135. netdata_socket_plot_t *w = ptr->plot;
  136. collected_number value;
  137. write_begin_chart(NETDATA_EBPF_FAMILY, chart);
  138. for (i = 0; i < end; i++) {
  139. value = ((collected_number) w[i].plot.plot_sent_bytes);
  140. write_chart_dimension(w[i].dimension_sent, value);
  141. value = (collected_number) w[i].plot.plot_recv_bytes;
  142. write_chart_dimension(w[i].dimension_recv, value);
  143. }
  144. i = ptr->last;
  145. value = ((collected_number) w[i].plot.plot_sent_bytes);
  146. write_chart_dimension(w[i].dimension_sent, value);
  147. value = (collected_number) w[i].plot.plot_recv_bytes;
  148. write_chart_dimension(w[i].dimension_recv, value);
  149. write_end_chart();
  150. }
  151. /**
  152. * Network Viewer Send packets
  153. *
  154. * @param ptr the structure with values to plot
  155. * @param chart the chart name.
  156. */
  157. static inline void ebpf_socket_nv_send_packets(netdata_vector_plot_t *ptr, char *chart)
  158. {
  159. uint32_t i;
  160. uint32_t end = ptr->last_plot;
  161. netdata_socket_plot_t *w = ptr->plot;
  162. collected_number value;
  163. write_begin_chart(NETDATA_EBPF_FAMILY, chart);
  164. for (i = 0; i < end; i++) {
  165. value = ((collected_number)w[i].plot.plot_sent_packets);
  166. write_chart_dimension(w[i].dimension_sent, value);
  167. value = (collected_number) w[i].plot.plot_recv_packets;
  168. write_chart_dimension(w[i].dimension_recv, value);
  169. }
  170. i = ptr->last;
  171. value = ((collected_number)w[i].plot.plot_sent_packets);
  172. write_chart_dimension(w[i].dimension_sent, value);
  173. value = (collected_number)w[i].plot.plot_recv_packets;
  174. write_chart_dimension(w[i].dimension_recv, value);
  175. write_end_chart();
  176. }
  177. /**
  178. * Network Viewer Send Retransmit
  179. *
  180. * @param ptr the structure with values to plot
  181. * @param chart the chart name.
  182. */
  183. static inline void ebpf_socket_nv_send_retransmit(netdata_vector_plot_t *ptr, char *chart)
  184. {
  185. uint32_t i;
  186. uint32_t end = ptr->last_plot;
  187. netdata_socket_plot_t *w = ptr->plot;
  188. collected_number value;
  189. write_begin_chart(NETDATA_EBPF_FAMILY, chart);
  190. for (i = 0; i < end; i++) {
  191. value = (collected_number) w[i].plot.plot_retransmit;
  192. write_chart_dimension(w[i].dimension_retransmit, value);
  193. }
  194. i = ptr->last;
  195. value = (collected_number)w[i].plot.plot_retransmit;
  196. write_chart_dimension(w[i].dimension_retransmit, value);
  197. write_end_chart();
  198. }
  199. /**
  200. * Send network viewer data
  201. *
  202. * @param ptr the pointer to plot data
  203. */
  204. static void ebpf_socket_send_nv_data(netdata_vector_plot_t *ptr)
  205. {
  206. if (!ptr->flags)
  207. return;
  208. if (ptr == (netdata_vector_plot_t *)&outbound_vectors) {
  209. ebpf_socket_nv_send_bytes(ptr, NETDATA_NV_OUTBOUND_BYTES);
  210. fflush(stdout);
  211. ebpf_socket_nv_send_packets(ptr, NETDATA_NV_OUTBOUND_PACKETS);
  212. fflush(stdout);
  213. ebpf_socket_nv_send_retransmit(ptr, NETDATA_NV_OUTBOUND_RETRANSMIT);
  214. fflush(stdout);
  215. } else {
  216. ebpf_socket_nv_send_bytes(ptr, NETDATA_NV_INBOUND_BYTES);
  217. fflush(stdout);
  218. ebpf_socket_nv_send_packets(ptr, NETDATA_NV_INBOUND_PACKETS);
  219. fflush(stdout);
  220. }
  221. }
  222. /**
  223. * Update the publish strctures to create the dimenssions
  224. *
  225. * @param curr Last values read from memory.
  226. * @param prev Previous values read from memory.
  227. */
  228. static void ebpf_socket_update_apps_publish(ebpf_socket_publish_apps_t *curr, ebpf_socket_publish_apps_t *prev)
  229. {
  230. curr->publish_recv = curr->received - prev->received;
  231. curr->publish_sent = curr->sent - prev->sent;
  232. }
  233. /**
  234. * Send data to Netdata calling auxiliar functions.
  235. *
  236. * @param em the structure with thread information
  237. */
  238. static void ebpf_socket_send_data(ebpf_module_t *em)
  239. {
  240. netdata_publish_vfs_common_t common_tcp;
  241. netdata_publish_vfs_common_t common_udp;
  242. ebpf_update_global_publish(socket_publish_aggregated, &common_tcp, &common_udp, socket_aggregated_data);
  243. write_count_chart(
  244. NETDATA_TCP_FUNCTION_COUNT, NETDATA_EBPF_FAMILY, socket_publish_aggregated, 3);
  245. write_io_chart(
  246. NETDATA_TCP_FUNCTION_BYTES, NETDATA_EBPF_FAMILY, socket_id_names[0], socket_id_names[1], &common_tcp);
  247. if (em->mode < MODE_ENTRY) {
  248. write_err_chart(
  249. NETDATA_TCP_FUNCTION_ERROR, NETDATA_EBPF_FAMILY, socket_publish_aggregated, 2);
  250. }
  251. write_count_chart(
  252. NETDATA_TCP_RETRANSMIT, NETDATA_EBPF_FAMILY, &socket_publish_aggregated[NETDATA_RETRANSMIT_START], 1);
  253. write_count_chart(
  254. NETDATA_UDP_FUNCTION_COUNT, NETDATA_EBPF_FAMILY, &socket_publish_aggregated[NETDATA_UDP_START], 2);
  255. write_io_chart(
  256. NETDATA_UDP_FUNCTION_BYTES, NETDATA_EBPF_FAMILY, socket_id_names[3], socket_id_names[4], &common_udp);
  257. if (em->mode < MODE_ENTRY) {
  258. write_err_chart(
  259. NETDATA_UDP_FUNCTION_ERROR, NETDATA_EBPF_FAMILY, &socket_publish_aggregated[NETDATA_UDP_START], 2);
  260. }
  261. }
  262. /**
  263. * Sum values for pid
  264. *
  265. * @param root the structure with all available PIDs
  266. *
  267. * @param offset the address that we are reading
  268. *
  269. * @return it returns the sum of all PIDs
  270. */
  271. long long ebpf_socket_sum_values_for_pids(struct pid_on_target *root, size_t offset)
  272. {
  273. long long ret = 0;
  274. while (root) {
  275. int32_t pid = root->pid;
  276. ebpf_socket_publish_apps_t *w = socket_bandwidth_curr[pid];
  277. if (w) {
  278. ret += get_value_from_structure((char *)w, offset);
  279. }
  280. root = root->next;
  281. }
  282. return ret;
  283. }
  284. /**
  285. * Send data to Netdata calling auxiliar functions.
  286. *
  287. * @param em the structure with thread information
  288. * @param root the target list.
  289. */
  290. void ebpf_socket_send_apps_data(ebpf_module_t *em, struct target *root)
  291. {
  292. UNUSED(em);
  293. if (!socket_apps_created)
  294. return;
  295. struct target *w;
  296. collected_number value;
  297. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_SENT);
  298. for (w = root; w; w = w->next) {
  299. if (unlikely(w->exposed && w->processes)) {
  300. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t, publish_sent));
  301. write_chart_dimension(w->name, value);
  302. }
  303. }
  304. write_end_chart();
  305. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_RECV);
  306. for (w = root; w; w = w->next) {
  307. if (unlikely(w->exposed && w->processes)) {
  308. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t, publish_recv));
  309. write_chart_dimension(w->name, value);
  310. }
  311. }
  312. write_end_chart();
  313. }
  314. /*****************************************************************
  315. *
  316. * FUNCTIONS TO CREATE CHARTS
  317. *
  318. *****************************************************************/
  319. /**
  320. * Create global charts
  321. *
  322. * Call ebpf_create_chart to create the charts for the collector.
  323. *
  324. * @param em a pointer to the structure with the default values.
  325. */
  326. static void ebpf_create_global_charts(ebpf_module_t *em)
  327. {
  328. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  329. NETDATA_TCP_FUNCTION_COUNT,
  330. "Calls to internal functions",
  331. EBPF_COMMON_DIMENSION_CALL,
  332. NETDATA_SOCKET_GROUP,
  333. 21070,
  334. ebpf_create_global_dimension,
  335. socket_publish_aggregated,
  336. 3);
  337. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  338. NETDATA_TCP_FUNCTION_BYTES,
  339. "TCP bandwidth",
  340. EBPF_COMMON_DIMENSION_BYTESS,
  341. NETDATA_SOCKET_GROUP,
  342. 21071,
  343. ebpf_create_global_dimension,
  344. socket_publish_aggregated,
  345. 3);
  346. if (em->mode < MODE_ENTRY) {
  347. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  348. NETDATA_TCP_FUNCTION_ERROR,
  349. "TCP errors",
  350. EBPF_COMMON_DIMENSION_CALL,
  351. NETDATA_SOCKET_GROUP,
  352. 21072,
  353. ebpf_create_global_dimension,
  354. socket_publish_aggregated,
  355. 2);
  356. }
  357. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  358. NETDATA_TCP_RETRANSMIT,
  359. "Packages retransmitted",
  360. EBPF_COMMON_DIMENSION_CALL,
  361. NETDATA_SOCKET_GROUP,
  362. 21073,
  363. ebpf_create_global_dimension,
  364. &socket_publish_aggregated[NETDATA_RETRANSMIT_START],
  365. 1);
  366. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  367. NETDATA_UDP_FUNCTION_COUNT,
  368. "UDP calls",
  369. EBPF_COMMON_DIMENSION_CALL,
  370. NETDATA_SOCKET_GROUP,
  371. 21074,
  372. ebpf_create_global_dimension,
  373. &socket_publish_aggregated[NETDATA_UDP_START],
  374. 2);
  375. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  376. NETDATA_UDP_FUNCTION_BYTES,
  377. "UDP bandwidth",
  378. EBPF_COMMON_DIMENSION_BYTESS,
  379. NETDATA_SOCKET_GROUP,
  380. 21075,
  381. ebpf_create_global_dimension,
  382. &socket_publish_aggregated[NETDATA_UDP_START],
  383. 2);
  384. if (em->mode < MODE_ENTRY) {
  385. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  386. NETDATA_UDP_FUNCTION_ERROR,
  387. "UDP errors",
  388. EBPF_COMMON_DIMENSION_CALL,
  389. NETDATA_SOCKET_GROUP,
  390. 21076,
  391. ebpf_create_global_dimension,
  392. &socket_publish_aggregated[NETDATA_UDP_START],
  393. 2);
  394. }
  395. }
  396. /**
  397. * Create apps charts
  398. *
  399. * Call ebpf_create_chart to create the charts on apps submenu.
  400. *
  401. * @param em a pointer to the structure with the default values.
  402. */
  403. void ebpf_socket_create_apps_charts(ebpf_module_t *em, struct target *root)
  404. {
  405. UNUSED(em);
  406. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_SENT,
  407. "Bytes sent",
  408. EBPF_COMMON_DIMENSION_BYTESS,
  409. NETDATA_APPS_NET_GROUP,
  410. 20080,
  411. root);
  412. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_RECV,
  413. "bytes received",
  414. EBPF_COMMON_DIMENSION_BYTESS,
  415. NETDATA_APPS_NET_GROUP,
  416. 20081,
  417. root);
  418. socket_apps_created = 1;
  419. }
  420. /**
  421. * Create network viewer chart
  422. *
  423. * Create common charts.
  424. *
  425. * @param id the chart id
  426. * @param title the chart title
  427. * @param units the units label
  428. * @param family the group name used to attach the chart on dashaboard
  429. * @param order the chart order
  430. * @param ptr the plot structure with values.
  431. */
  432. static void ebpf_socket_create_nv_chart(char *id, char *title, char *units,
  433. char *family, int order, netdata_vector_plot_t *ptr)
  434. {
  435. ebpf_write_chart_cmd(NETDATA_EBPF_FAMILY,
  436. id,
  437. title,
  438. units,
  439. family,
  440. "stacked",
  441. order);
  442. uint32_t i;
  443. uint32_t end = ptr->last_plot;
  444. netdata_socket_plot_t *w = ptr->plot;
  445. for (i = 0; i < end; i++) {
  446. fprintf(stdout, "DIMENSION %s '' incremental -1 1\n", w[i].dimension_sent);
  447. fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[i].dimension_recv);
  448. }
  449. end = ptr->last;
  450. fprintf(stdout, "DIMENSION %s '' incremental -1 1\n", w[end].dimension_sent);
  451. fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[end].dimension_recv);
  452. }
  453. /**
  454. * Create network viewer retransmit
  455. *
  456. * Create a specific chart.
  457. *
  458. * @param id the chart id
  459. * @param title the chart title
  460. * @param units the units label
  461. * @param family the group name used to attach the chart on dashaboard
  462. * @param order the chart order
  463. * @param ptr the plot structure with values.
  464. */
  465. static void ebpf_socket_create_nv_retransmit(char *id, char *title, char *units,
  466. char *family, int order, netdata_vector_plot_t *ptr)
  467. {
  468. ebpf_write_chart_cmd(NETDATA_EBPF_FAMILY,
  469. id,
  470. title,
  471. units,
  472. family,
  473. "stacked",
  474. order);
  475. uint32_t i;
  476. uint32_t end = ptr->last_plot;
  477. netdata_socket_plot_t *w = ptr->plot;
  478. for (i = 0; i < end; i++) {
  479. fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[i].dimension_retransmit);
  480. }
  481. end = ptr->last;
  482. fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[end].dimension_retransmit);
  483. }
  484. /**
  485. * Create Network Viewer charts
  486. *
  487. * Recreate the charts when new sockets are created.
  488. *
  489. * @param ptr a pointer for inbound or outbound vectors.
  490. */
  491. static void ebpf_socket_create_nv_charts(netdata_vector_plot_t *ptr)
  492. {
  493. // We do not have new sockets, so we do not need move forward
  494. if (ptr->max_plot == ptr->last_plot)
  495. return;
  496. ptr->last_plot = ptr->max_plot;
  497. if (ptr == (netdata_vector_plot_t *)&outbound_vectors) {
  498. ebpf_socket_create_nv_chart(NETDATA_NV_OUTBOUND_BYTES,
  499. "Outbound connections (bytes).",
  500. EBPF_COMMON_DIMENSION_BYTESS,
  501. NETDATA_NETWORK_CONNECTIONS_GROUP,
  502. 21080,
  503. ptr);
  504. ebpf_socket_create_nv_chart(NETDATA_NV_OUTBOUND_PACKETS,
  505. "Outbound connections (packets)",
  506. EBPF_COMMON_DIMENSION_PACKETS,
  507. NETDATA_NETWORK_CONNECTIONS_GROUP,
  508. 21082,
  509. ptr);
  510. ebpf_socket_create_nv_retransmit(NETDATA_NV_OUTBOUND_RETRANSMIT,
  511. "Retransmitted packets",
  512. EBPF_COMMON_DIMENSION_CALL,
  513. NETDATA_NETWORK_CONNECTIONS_GROUP,
  514. 21083,
  515. ptr);
  516. } else {
  517. ebpf_socket_create_nv_chart(NETDATA_NV_INBOUND_BYTES,
  518. "Inbound connections (bytes)",
  519. EBPF_COMMON_DIMENSION_BYTESS,
  520. NETDATA_NETWORK_CONNECTIONS_GROUP,
  521. 21084,
  522. ptr);
  523. ebpf_socket_create_nv_chart(NETDATA_NV_INBOUND_PACKETS,
  524. "Inbound connections (packets)",
  525. EBPF_COMMON_DIMENSION_PACKETS,
  526. NETDATA_NETWORK_CONNECTIONS_GROUP,
  527. 21085,
  528. ptr);
  529. }
  530. ptr->flags |= NETWORK_VIEWER_CHARTS_CREATED;
  531. }
  532. /*****************************************************************
  533. *
  534. * READ INFORMATION FROM KERNEL RING
  535. *
  536. *****************************************************************/
  537. /**
  538. * Is specific ip inside the range
  539. *
  540. * Check if the ip is inside a IP range previously defined
  541. *
  542. * @param cmp the IP to compare
  543. * @param family the IP family
  544. *
  545. * @return It returns 1 if the IP is inside the range and 0 otherwise
  546. */
  547. static int is_specific_ip_inside_range(union netdata_ip_t *cmp, int family)
  548. {
  549. if (!network_viewer_opt.excluded_ips && !network_viewer_opt.included_ips)
  550. return 1;
  551. uint32_t ipv4_test = ntohl(cmp->addr32[0]);
  552. ebpf_network_viewer_ip_list_t *move = network_viewer_opt.excluded_ips;
  553. while (move) {
  554. if (family == AF_INET) {
  555. if (ntohl(move->first.addr32[0]) <= ipv4_test &&
  556. ipv4_test <= ntohl(move->last.addr32[0]) )
  557. return 0;
  558. } else {
  559. if (memcmp(move->first.addr8, cmp->addr8, sizeof(union netdata_ip_t)) <= 0 &&
  560. memcmp(move->last.addr8, cmp->addr8, sizeof(union netdata_ip_t)) >= 0) {
  561. return 0;
  562. }
  563. }
  564. move = move->next;
  565. }
  566. move = network_viewer_opt.included_ips;
  567. while (move) {
  568. if (family == AF_INET) {
  569. if (ntohl(move->first.addr32[0]) <= ipv4_test &&
  570. ntohl(move->last.addr32[0]) >= ipv4_test)
  571. return 1;
  572. } else {
  573. if (memcmp(move->first.addr8, cmp->addr8, sizeof(union netdata_ip_t)) <= 0 &&
  574. memcmp(move->last.addr8, cmp->addr8, sizeof(union netdata_ip_t)) >= 0) {
  575. return 1;
  576. }
  577. }
  578. move = move->next;
  579. }
  580. return 0;
  581. }
  582. /**
  583. * Is port inside range
  584. *
  585. * Verify if the cmp port is inside the range [first, last].
  586. * This function expects only the last parameter as big endian.
  587. *
  588. * @param cmp the value to compare
  589. *
  590. * @return It returns 1 when cmp is inside and 0 otherwise.
  591. */
  592. static int is_port_inside_range(uint16_t cmp)
  593. {
  594. // We do not have restrictions for ports.
  595. if (!network_viewer_opt.excluded_port && !network_viewer_opt.included_port)
  596. return 1;
  597. // Test if port is excluded
  598. ebpf_network_viewer_port_list_t *move = network_viewer_opt.excluded_port;
  599. cmp = htons(cmp);
  600. while (move) {
  601. if (move->cmp_first <= cmp && cmp <= move->cmp_last)
  602. return 0;
  603. move = move->next;
  604. }
  605. // Test if the port is inside allowed range
  606. move = network_viewer_opt.included_port;
  607. while (move) {
  608. if (move->cmp_first <= cmp && cmp <= move->cmp_last)
  609. return 1;
  610. move = move->next;
  611. }
  612. return 0;
  613. }
  614. /**
  615. * Hostname matches pattern
  616. *
  617. * @param cmp the value to compare
  618. *
  619. * @return It returns 1 when the value matches and zero otherwise.
  620. */
  621. int hostname_matches_pattern(char *cmp)
  622. {
  623. if (!network_viewer_opt.included_hostnames && !network_viewer_opt.excluded_hostnames)
  624. return 1;
  625. ebpf_network_viewer_hostname_list_t *move = network_viewer_opt.excluded_hostnames;
  626. while (move) {
  627. if (simple_pattern_matches(move->value_pattern, cmp))
  628. return 0;
  629. move = move->next;
  630. }
  631. move = network_viewer_opt.included_hostnames;
  632. while (move) {
  633. if (simple_pattern_matches(move->value_pattern, cmp))
  634. return 1;
  635. move = move->next;
  636. }
  637. return 0;
  638. }
  639. /**
  640. * Is socket allowed?
  641. *
  642. * Compare destination addresses and destination ports to define next steps
  643. *
  644. * @param key the socket read from kernel ring
  645. * @param family the family used to compare IPs (AF_INET and AF_INET6)
  646. *
  647. * @return It returns 1 if this socket is inside the ranges and 0 otherwise.
  648. */
  649. int is_socket_allowed(netdata_socket_idx_t *key, int family)
  650. {
  651. if (!is_port_inside_range(key->dport))
  652. return 0;
  653. return is_specific_ip_inside_range(&key->daddr, family);
  654. }
  655. /**
  656. * Compare sockets
  657. *
  658. * Compare destination address and destination port.
  659. * We do not compare source port, because it is random.
  660. * We also do not compare source address, because inbound and outbound connections are stored in separated AVL trees.
  661. *
  662. * @param a pointer to netdata_socket_plot
  663. * @param b pointer to netdata_socket_plot
  664. *
  665. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  666. */
  667. static int compare_sockets(void *a, void *b)
  668. {
  669. struct netdata_socket_plot *val1 = a;
  670. struct netdata_socket_plot *val2 = b;
  671. int cmp;
  672. // We do not need to compare val2 family, because data inside hash table is always from the same family
  673. if (val1->family == AF_INET) { //IPV4
  674. if (val1->flags & NETDATA_INBOUND_DIRECTION) {
  675. if (val1->index.sport == val2->index.sport)
  676. cmp = 0;
  677. else {
  678. cmp = (val1->index.sport > val2->index.sport)?1:-1;
  679. }
  680. } else {
  681. cmp = memcmp(&val1->index.dport, &val2->index.dport, sizeof(uint16_t));
  682. if (!cmp) {
  683. cmp = memcmp(&val1->index.daddr.addr32[0], &val2->index.daddr.addr32[0], sizeof(uint32_t));
  684. }
  685. }
  686. } else {
  687. if (val1->flags & NETDATA_INBOUND_DIRECTION) {
  688. if (val1->index.sport == val2->index.sport)
  689. cmp = 0;
  690. else {
  691. cmp = (val1->index.sport > val2->index.sport)?1:-1;
  692. }
  693. } else {
  694. cmp = memcmp(&val1->index.dport, &val2->index.dport, sizeof(uint16_t));
  695. if (!cmp) {
  696. cmp = memcmp(&val1->index.daddr.addr32, &val2->index.daddr.addr32, 4*sizeof(uint32_t));
  697. }
  698. }
  699. }
  700. return cmp;
  701. }
  702. /**
  703. * Build dimension name
  704. *
  705. * Fill dimension name vector with values given
  706. *
  707. * @param dimname the output vector
  708. * @param hostname the hostname for the socket.
  709. * @param service_name the service used to connect.
  710. * @param proto the protocol used in this connection
  711. * @param family is this IPV4(AF_INET) or IPV6(AF_INET6)
  712. *
  713. * @return it returns the size of the data copied on success and -1 otherwise.
  714. */
  715. static inline int build_outbound_dimension_name(char *dimname, char *hostname, char *service_name,
  716. char *proto, int family)
  717. {
  718. return snprintf(dimname, CONFIG_MAX_NAME - 7, (family == AF_INET)?"%s:%s:%s_":"%s:%s:[%s]_",
  719. service_name, proto,
  720. hostname);
  721. }
  722. /**
  723. * Fill inbound dimension name
  724. *
  725. * Mount the dimension name with the input given
  726. *
  727. * @param dimname the output vector
  728. * @param service_name the service used to connect.
  729. * @param proto the protocol used in this connection
  730. *
  731. * @return it returns the size of the data copied on success and -1 otherwise.
  732. */
  733. static inline int build_inbound_dimension_name(char *dimname, char *service_name, char *proto)
  734. {
  735. return snprintf(dimname, CONFIG_MAX_NAME - 7, "%s:%s_", service_name,
  736. proto);
  737. }
  738. /**
  739. * Fill Resolved Name
  740. *
  741. * Fill the resolved name structure with the value given.
  742. * The hostname is the largest value possible, if it is necessary to cut some value, it must be cut.
  743. *
  744. * @param ptr the output vector
  745. * @param hostname the hostname resolved or IP.
  746. * @param length the length for the hostname.
  747. * @param service_name the service name associated to the connection
  748. * @param is_outbound the is this an outbound connection
  749. */
  750. static inline void fill_resolved_name(netdata_socket_plot_t *ptr, char *hostname, size_t length,
  751. char *service_name, int is_outbound)
  752. {
  753. if (length < NETDATA_MAX_NETWORK_COMBINED_LENGTH)
  754. ptr->resolved_name = strdupz(hostname);
  755. else {
  756. length = NETDATA_MAX_NETWORK_COMBINED_LENGTH;
  757. ptr->resolved_name = mallocz( NETDATA_MAX_NETWORK_COMBINED_LENGTH + 1);
  758. memcpy(ptr->resolved_name, hostname, length);
  759. ptr->resolved_name[length] = '\0';
  760. }
  761. char dimname[CONFIG_MAX_NAME];
  762. int size;
  763. char *protocol;
  764. if (ptr->sock.protocol == IPPROTO_UDP) {
  765. protocol = "UDP";
  766. } else if (ptr->sock.protocol == IPPROTO_TCP) {
  767. protocol = "TCP";
  768. } else {
  769. protocol = "ALL";
  770. }
  771. if (is_outbound)
  772. size = build_outbound_dimension_name(dimname, hostname, service_name, protocol, ptr->family);
  773. else
  774. size = build_inbound_dimension_name(dimname,service_name, protocol);
  775. if (size > 0) {
  776. strcpy(&dimname[size], "sent");
  777. dimname[size + 4] = '\0';
  778. ptr->dimension_sent = strdupz(dimname);
  779. strcpy(&dimname[size], "recv");
  780. ptr->dimension_recv = strdupz(dimname);
  781. dimname[size - 1] = '\0';
  782. ptr->dimension_retransmit = strdupz(dimname);
  783. }
  784. }
  785. /**
  786. * Mount dimension names
  787. *
  788. * Fill the vector names after to resolve the addresses
  789. *
  790. * @param ptr a pointer to the structure where the values are stored.
  791. * @param is_outbound is a outbound ptr value?
  792. *
  793. * @return It returns 1 if the name is valid and 0 otherwise.
  794. */
  795. int fill_names(netdata_socket_plot_t *ptr, int is_outbound)
  796. {
  797. char hostname[NI_MAXHOST], service_name[NI_MAXSERV];
  798. if (ptr->resolved)
  799. return 1;
  800. int ret;
  801. static int resolve_name = -1;
  802. static int resolve_service = -1;
  803. if (resolve_name == -1)
  804. resolve_name = network_viewer_opt.hostname_resolution_enabled;
  805. if (resolve_service == -1)
  806. resolve_service = network_viewer_opt.service_resolution_enabled;
  807. netdata_socket_idx_t *idx = &ptr->index;
  808. char *errname = { "Not resolved" };
  809. // Resolve Name
  810. if (ptr->family == AF_INET) { //IPV4
  811. struct sockaddr_in myaddr;
  812. memset(&myaddr, 0 , sizeof(myaddr));
  813. myaddr.sin_family = ptr->family;
  814. if (is_outbound) {
  815. myaddr.sin_port = idx->dport;
  816. myaddr.sin_addr.s_addr = idx->daddr.addr32[0];
  817. } else {
  818. myaddr.sin_port = idx->sport;
  819. myaddr.sin_addr.s_addr = idx->saddr.addr32[0];
  820. }
  821. ret = (!resolve_name)?-1:getnameinfo((struct sockaddr *)&myaddr, sizeof(myaddr), hostname,
  822. sizeof(hostname), service_name, sizeof(service_name), NI_NAMEREQD);
  823. if (!ret && !resolve_service) {
  824. snprintf(service_name, sizeof(service_name), "%u", ntohs(myaddr.sin_port));
  825. }
  826. if (ret) {
  827. // I cannot resolve the name, I will use the IP
  828. if (!inet_ntop(AF_INET, &myaddr.sin_addr.s_addr, hostname, NI_MAXHOST)) {
  829. strncpy(hostname, errname, 13);
  830. }
  831. snprintf(service_name, sizeof(service_name), "%u", ntohs(myaddr.sin_port));
  832. ret = 1;
  833. }
  834. } else { // IPV6
  835. struct sockaddr_in6 myaddr6;
  836. memset(&myaddr6, 0 , sizeof(myaddr6));
  837. myaddr6.sin6_family = AF_INET6;
  838. if (is_outbound) {
  839. myaddr6.sin6_port = idx->dport;
  840. memcpy(myaddr6.sin6_addr.s6_addr, idx->daddr.addr8, sizeof(union netdata_ip_t));
  841. } else {
  842. myaddr6.sin6_port = idx->sport;
  843. memcpy(myaddr6.sin6_addr.s6_addr, idx->saddr.addr8, sizeof(union netdata_ip_t));
  844. }
  845. ret = (!resolve_name)?-1:getnameinfo((struct sockaddr *)&myaddr6, sizeof(myaddr6), hostname,
  846. sizeof(hostname), service_name, sizeof(service_name), NI_NAMEREQD);
  847. if (!ret && !resolve_service) {
  848. snprintf(service_name, sizeof(service_name), "%u", ntohs(myaddr6.sin6_port));
  849. }
  850. if (ret) {
  851. // I cannot resolve the name, I will use the IP
  852. if (!inet_ntop(AF_INET6, myaddr6.sin6_addr.s6_addr, hostname, NI_MAXHOST)) {
  853. strncpy(hostname, errname, 13);
  854. }
  855. snprintf(service_name, sizeof(service_name), "%u", ntohs(myaddr6.sin6_port));
  856. ret = 1;
  857. }
  858. }
  859. fill_resolved_name(ptr, hostname,
  860. strlen(hostname) + strlen(service_name)+ NETDATA_DOTS_PROTOCOL_COMBINED_LENGTH,
  861. service_name, is_outbound);
  862. if (resolve_name && !ret)
  863. ret = hostname_matches_pattern(hostname);
  864. ptr->resolved++;
  865. return ret;
  866. }
  867. /**
  868. * Fill last Network Viewer Dimension
  869. *
  870. * Fill the unique dimension that is always plotted.
  871. *
  872. * @param ptr the pointer for the last dimension
  873. * @param is_outbound is this an inbound structure?
  874. */
  875. static void fill_last_nv_dimension(netdata_socket_plot_t *ptr, int is_outbound)
  876. {
  877. char hostname[NI_MAXHOST], service_name[NI_MAXSERV];
  878. char *other = { "other" };
  879. // We are also copying the NULL bytes to avoid warnings in new compilers
  880. strncpy(hostname, other, 6);
  881. strncpy(service_name, other, 6);
  882. ptr->family = AF_INET;
  883. ptr->sock.protocol = 255;
  884. ptr->flags = (!is_outbound)?NETDATA_INBOUND_DIRECTION:NETDATA_OUTBOUND_DIRECTION;
  885. fill_resolved_name(ptr, hostname, 10 + NETDATA_DOTS_PROTOCOL_COMBINED_LENGTH, service_name, is_outbound);
  886. #ifdef NETDATA_INTERNAL_CHECKS
  887. info("Last %s dimension added: ID = %u, IP = OTHER, NAME = %s, DIM1 = %s, DIM2 = %s, DIM3 = %s",
  888. (is_outbound)?"outbound":"inbound", network_viewer_opt.max_dim - 1, ptr->resolved_name,
  889. ptr->dimension_recv, ptr->dimension_sent, ptr->dimension_retransmit);
  890. #endif
  891. }
  892. /**
  893. * Update Socket Data
  894. *
  895. * Update the socket information with last collected data
  896. *
  897. * @param sock
  898. * @param lvalues
  899. */
  900. static inline void update_socket_data(netdata_socket_t *sock, netdata_socket_t *lvalues)
  901. {
  902. sock->recv_packets += lvalues->recv_packets;
  903. sock->sent_packets += lvalues->sent_packets;
  904. sock->recv_bytes += lvalues->recv_bytes;
  905. sock->sent_bytes += lvalues->sent_bytes;
  906. sock->retransmit += lvalues->retransmit;
  907. if (lvalues->ct > sock->ct)
  908. sock->ct = lvalues->ct;
  909. }
  910. /**
  911. * Store socket inside avl
  912. *
  913. * Store the socket values inside the avl tree.
  914. *
  915. * @param out the structure with information used to plot charts.
  916. * @param lvalues Values read from socket ring.
  917. * @param lindex the index information, the real socket.
  918. * @param family the family associated to the socket
  919. * @param flags the connection flags
  920. */
  921. static void store_socket_inside_avl(netdata_vector_plot_t *out, netdata_socket_t *lvalues,
  922. netdata_socket_idx_t *lindex, int family, uint32_t flags)
  923. {
  924. netdata_socket_plot_t test, *ret ;
  925. memcpy(&test.index, lindex, sizeof(netdata_socket_idx_t));
  926. test.flags = flags;
  927. ret = (netdata_socket_plot_t *) avl_search_lock(&out->tree, (avl *)&test);
  928. if (ret) {
  929. if (lvalues->ct > ret->plot.last_time) {
  930. update_socket_data(&ret->sock, lvalues);
  931. }
  932. } else {
  933. uint32_t curr = out->next;
  934. uint32_t last = out->last;
  935. netdata_socket_plot_t *w = &out->plot[curr];
  936. int resolved;
  937. if (curr == last) {
  938. if (lvalues->ct > w->plot.last_time) {
  939. update_socket_data(&w->sock, lvalues);
  940. }
  941. return;
  942. } else {
  943. memcpy(&w->sock, lvalues, sizeof(netdata_socket_t));
  944. memcpy(&w->index, lindex, sizeof(netdata_socket_idx_t));
  945. w->family = family;
  946. resolved = fill_names(w, out != (netdata_vector_plot_t *)&inbound_vectors);
  947. }
  948. if (!resolved) {
  949. freez(w->resolved_name);
  950. freez(w->dimension_sent);
  951. freez(w->dimension_recv);
  952. freez(w->dimension_retransmit);
  953. memset(w, 0, sizeof(netdata_socket_plot_t));
  954. return;
  955. }
  956. w->flags = flags;
  957. netdata_socket_plot_t *check ;
  958. check = (netdata_socket_plot_t *) avl_insert_lock(&out->tree, (avl *)w);
  959. if (check != w)
  960. error("Internal error, cannot insert the AVL tree.");
  961. #ifdef NETDATA_INTERNAL_CHECKS
  962. char iptext[INET6_ADDRSTRLEN];
  963. if (inet_ntop(family, &w->index.daddr.addr8, iptext, sizeof(iptext)))
  964. info("New %s dimension added: ID = %u, IP = %s, NAME = %s, DIM1 = %s, DIM2 = %s, DIM3 = %s",
  965. (out == &inbound_vectors)?"inbound":"outbound", curr, iptext, w->resolved_name,
  966. w->dimension_recv, w->dimension_sent, w->dimension_retransmit);
  967. #endif
  968. curr++;
  969. if (curr > last)
  970. curr = last;
  971. out->next = curr;
  972. }
  973. }
  974. /**
  975. * Compare Vector to store
  976. *
  977. * Compare input values with local address to select table to store.
  978. *
  979. * @param direction store inbound and outbound direction.
  980. * @param cmp index read from hash table.
  981. * @param proto the protocol read.
  982. *
  983. * @return It returns the structure with address to compare.
  984. */
  985. netdata_vector_plot_t * select_vector_to_store(uint32_t *direction, netdata_socket_idx_t *cmp, uint8_t proto)
  986. {
  987. if (!listen_ports) {
  988. *direction = NETDATA_OUTBOUND_DIRECTION;
  989. return &outbound_vectors;
  990. }
  991. ebpf_network_viewer_port_list_t *move_ports = listen_ports;
  992. while (move_ports) {
  993. if (move_ports->protocol == proto && move_ports->first == cmp->sport) {
  994. *direction = NETDATA_INBOUND_DIRECTION;
  995. return &inbound_vectors;
  996. }
  997. move_ports = move_ports->next;
  998. }
  999. *direction = NETDATA_OUTBOUND_DIRECTION;
  1000. return &outbound_vectors;
  1001. }
  1002. /**
  1003. * Hash accumulator
  1004. *
  1005. * @param values the values used to calculate the data.
  1006. * @param key the key to store data.
  1007. * @param removesock check if this socket must be removed .
  1008. * @param family the connection family
  1009. * @param end the values size.
  1010. */
  1011. static void hash_accumulator(netdata_socket_t *values, netdata_socket_idx_t *key, int *removesock, int family, int end)
  1012. {
  1013. uint64_t bsent = 0, brecv = 0, psent = 0, precv = 0;
  1014. uint16_t retransmit = 0;
  1015. int i;
  1016. uint8_t protocol = values[0].protocol;
  1017. uint64_t ct = values[0].ct;
  1018. for (i = 1; i < end; i++) {
  1019. netdata_socket_t *w = &values[i];
  1020. precv += w->recv_packets;
  1021. psent += w->sent_packets;
  1022. brecv += w->recv_bytes;
  1023. bsent += w->sent_bytes;
  1024. retransmit += w->retransmit;
  1025. if (!protocol)
  1026. protocol = w->protocol;
  1027. if (w->ct > ct)
  1028. ct = w->ct;
  1029. *removesock += (int)w->removeme;
  1030. }
  1031. values[0].recv_packets += precv;
  1032. values[0].sent_packets += psent;
  1033. values[0].recv_bytes += brecv;
  1034. values[0].sent_bytes += bsent;
  1035. values[0].retransmit += retransmit;
  1036. values[0].removeme += (uint8_t)*removesock;
  1037. values[0].protocol = (!protocol)?IPPROTO_TCP:protocol;
  1038. values[0].ct = ct;
  1039. if (is_socket_allowed(key, family)) {
  1040. uint32_t dir;
  1041. netdata_vector_plot_t *table = select_vector_to_store(&dir, key, protocol);
  1042. store_socket_inside_avl(table, &values[0], key, family, dir);
  1043. }
  1044. }
  1045. /**
  1046. * Read socket hash table
  1047. *
  1048. * Read data from hash tables created on kernel ring.
  1049. *
  1050. * @param fd the hash table with data.
  1051. * @param family the family associated to the hash table
  1052. *
  1053. * @return it returns 0 on success and -1 otherwise.
  1054. */
  1055. static void read_socket_hash_table(int fd, int family, int network_connection)
  1056. {
  1057. if (wait_to_plot)
  1058. return;
  1059. netdata_socket_idx_t key = {};
  1060. netdata_socket_idx_t next_key;
  1061. netdata_socket_idx_t removeme;
  1062. int removesock = 0;
  1063. netdata_socket_t *values = socket_values;
  1064. size_t length = ebpf_nprocs*sizeof(netdata_socket_t);
  1065. int test, end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  1066. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  1067. // We need to reset the values when we are working on kernel 4.15 or newer, because kernel does not create
  1068. // values for specific processor unless it is used to store data. As result of this behavior one the next socket
  1069. // can have values from the previous one.
  1070. memset(values, 0, length);
  1071. test = bpf_map_lookup_elem(fd, &key, values);
  1072. if (test < 0) {
  1073. key = next_key;
  1074. continue;
  1075. }
  1076. if (removesock)
  1077. bpf_map_delete_elem(fd, &removeme);
  1078. if (network_connection) {
  1079. removesock = 0;
  1080. hash_accumulator(values, &key, &removesock, family, end);
  1081. }
  1082. if (removesock)
  1083. removeme = key;
  1084. key = next_key;
  1085. }
  1086. if (removesock)
  1087. bpf_map_delete_elem(fd, &removeme);
  1088. test = bpf_map_lookup_elem(fd, &next_key, values);
  1089. if (test < 0) {
  1090. return;
  1091. }
  1092. if (network_connection) {
  1093. removesock = 0;
  1094. hash_accumulator(values, &next_key, &removesock, family, end);
  1095. }
  1096. if (removesock)
  1097. bpf_map_delete_elem(fd, &next_key);
  1098. }
  1099. /**
  1100. * Update listen table
  1101. *
  1102. * Update link list when it is necessary.
  1103. *
  1104. * @param value the ports we are listen to.
  1105. * @param proto the protocol used with port connection.
  1106. */
  1107. void update_listen_table(uint16_t value, uint8_t proto)
  1108. {
  1109. ebpf_network_viewer_port_list_t *w;
  1110. if (likely(listen_ports)) {
  1111. ebpf_network_viewer_port_list_t *move = listen_ports, *store = listen_ports;
  1112. while (move) {
  1113. if (move->protocol == proto && move->first == value)
  1114. return;
  1115. store = move;
  1116. move = move->next;
  1117. }
  1118. w = callocz(1, sizeof(ebpf_network_viewer_port_list_t));
  1119. w->first = value;
  1120. w->protocol = proto;
  1121. store->next = w;
  1122. } else {
  1123. w = callocz(1, sizeof(ebpf_network_viewer_port_list_t));
  1124. w->first = value;
  1125. w->protocol = proto;
  1126. listen_ports = w;
  1127. }
  1128. #ifdef NETDATA_INTERNAL_CHECKS
  1129. info("The network viewer is monitoring inbound connections for port %u", ntohs(value));
  1130. #endif
  1131. }
  1132. /**
  1133. * Read listen table
  1134. *
  1135. * Read the table with all ports that we are listen on host.
  1136. */
  1137. static void read_listen_table()
  1138. {
  1139. uint16_t key = 0;
  1140. uint16_t next_key;
  1141. int fd = map_fd[NETDATA_SOCKET_LISTEN_TABLE];
  1142. uint8_t value;
  1143. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  1144. int test = bpf_map_lookup_elem(fd, &key, &value);
  1145. if (test < 0) {
  1146. key = next_key;
  1147. continue;
  1148. }
  1149. // The correct protocol must come from kernel
  1150. update_listen_table(htons(key), (key == 53)?IPPROTO_UDP:IPPROTO_TCP);
  1151. key = next_key;
  1152. }
  1153. if (next_key) {
  1154. // The correct protocol must come from kernel
  1155. update_listen_table(htons(next_key), (key == 53)?IPPROTO_UDP:IPPROTO_TCP);
  1156. }
  1157. }
  1158. /**
  1159. * Socket read hash
  1160. *
  1161. * This is the thread callback.
  1162. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  1163. *
  1164. * @param ptr It is a NULL value for this thread.
  1165. *
  1166. * @return It always returns NULL.
  1167. */
  1168. void *ebpf_socket_read_hash(void *ptr)
  1169. {
  1170. ebpf_module_t *em = (ebpf_module_t *)ptr;
  1171. read_thread_closed = 0;
  1172. heartbeat_t hb;
  1173. heartbeat_init(&hb);
  1174. usec_t step = NETDATA_SOCKET_READ_SLEEP_MS;
  1175. int fd_ipv4 = map_fd[NETDATA_SOCKET_IPV4_HASH_TABLE];
  1176. int fd_ipv6 = map_fd[NETDATA_SOCKET_IPV6_HASH_TABLE];
  1177. int network_connection = em->optional;
  1178. while (!close_ebpf_plugin) {
  1179. usec_t dt = heartbeat_next(&hb, step);
  1180. (void)dt;
  1181. pthread_mutex_lock(&nv_mutex);
  1182. read_listen_table();
  1183. read_socket_hash_table(fd_ipv4, AF_INET, network_connection);
  1184. read_socket_hash_table(fd_ipv6, AF_INET6, network_connection);
  1185. wait_to_plot = 1;
  1186. pthread_mutex_unlock(&nv_mutex);
  1187. }
  1188. read_thread_closed = 1;
  1189. return NULL;
  1190. }
  1191. /**
  1192. * Read the hash table and store data to allocated vectors.
  1193. */
  1194. static void read_hash_global_tables()
  1195. {
  1196. uint64_t idx;
  1197. netdata_idx_t res[NETDATA_SOCKET_COUNTER];
  1198. netdata_idx_t *val = socket_hash_values;
  1199. int fd = map_fd[NETDATA_SOCKET_GLOBAL_HASH_TABLE];
  1200. for (idx = 0; idx < NETDATA_SOCKET_COUNTER; idx++) {
  1201. if (!bpf_map_lookup_elem(fd, &idx, val)) {
  1202. uint64_t total = 0;
  1203. int i;
  1204. int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  1205. for (i = 0; i < end; i++)
  1206. total += val[i];
  1207. res[idx] = total;
  1208. } else {
  1209. res[idx] = 0;
  1210. }
  1211. }
  1212. socket_aggregated_data[0].call = res[NETDATA_KEY_CALLS_TCP_SENDMSG];
  1213. socket_aggregated_data[1].call = res[NETDATA_KEY_CALLS_TCP_CLEANUP_RBUF];
  1214. socket_aggregated_data[2].call = res[NETDATA_KEY_CALLS_TCP_CLOSE];
  1215. socket_aggregated_data[3].call = res[NETDATA_KEY_CALLS_UDP_RECVMSG];
  1216. socket_aggregated_data[4].call = res[NETDATA_KEY_CALLS_UDP_SENDMSG];
  1217. socket_aggregated_data[5].call = res[NETDATA_KEY_TCP_RETRANSMIT];
  1218. socket_aggregated_data[0].ecall = res[NETDATA_KEY_ERROR_TCP_SENDMSG];
  1219. socket_aggregated_data[1].ecall = res[NETDATA_KEY_ERROR_TCP_CLEANUP_RBUF];
  1220. socket_aggregated_data[3].ecall = res[NETDATA_KEY_ERROR_UDP_RECVMSG];
  1221. socket_aggregated_data[4].ecall = res[NETDATA_KEY_ERROR_UDP_SENDMSG];
  1222. socket_aggregated_data[0].bytes = res[NETDATA_KEY_BYTES_TCP_SENDMSG];
  1223. socket_aggregated_data[1].bytes = res[NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF];
  1224. socket_aggregated_data[3].bytes = res[NETDATA_KEY_BYTES_UDP_RECVMSG];
  1225. socket_aggregated_data[4].bytes = res[NETDATA_KEY_BYTES_UDP_SENDMSG];
  1226. }
  1227. /**
  1228. * Fill publish apps when necessary.
  1229. *
  1230. * @param current_pid the PID that I am updating
  1231. * @param eb the structure with data read from memory.
  1232. */
  1233. void ebpf_socket_fill_publish_apps(uint32_t current_pid, ebpf_bandwidth_t *eb)
  1234. {
  1235. ebpf_socket_publish_apps_t *curr = socket_bandwidth_curr[current_pid];
  1236. ebpf_socket_publish_apps_t *prev = socket_bandwidth_prev[current_pid];
  1237. if (!curr) {
  1238. ebpf_socket_publish_apps_t *ptr = callocz(2, sizeof(ebpf_socket_publish_apps_t));
  1239. curr = &ptr[0];
  1240. socket_bandwidth_curr[current_pid] = curr;
  1241. prev = &ptr[1];
  1242. socket_bandwidth_prev[current_pid] = prev;
  1243. } else {
  1244. memcpy(prev, curr, sizeof(ebpf_socket_publish_apps_t));
  1245. }
  1246. curr->sent = eb->sent;
  1247. curr->received = eb->received;
  1248. ebpf_socket_update_apps_publish(curr, prev);
  1249. }
  1250. /**
  1251. * Bandwidth accumulator.
  1252. *
  1253. * @param out the vector with the values to sum
  1254. */
  1255. void ebpf_socket_bandwidth_accumulator(ebpf_bandwidth_t *out)
  1256. {
  1257. int i, end = (running_on_kernel >= NETDATA_KERNEL_V4_15) ? ebpf_nprocs : 1;
  1258. ebpf_bandwidth_t *total = &out[0];
  1259. for (i = 1; i < end; i++) {
  1260. ebpf_bandwidth_t *move = &out[i];
  1261. total->sent += move->sent;
  1262. total->received += move->received;
  1263. }
  1264. }
  1265. /**
  1266. * Update the apps data reading information from the hash table
  1267. */
  1268. static void ebpf_socket_update_apps_data()
  1269. {
  1270. int fd = map_fd[NETDATA_SOCKET_APPS_HASH_TABLE];
  1271. ebpf_bandwidth_t *eb = bandwidth_vector;
  1272. uint32_t key;
  1273. struct pid_stat *pids = root_of_pids;
  1274. while (pids) {
  1275. key = pids->pid;
  1276. if (bpf_map_lookup_elem(fd, &key, eb)) {
  1277. pids = pids->next;
  1278. continue;
  1279. }
  1280. ebpf_socket_bandwidth_accumulator(eb);
  1281. ebpf_socket_fill_publish_apps(key, eb);
  1282. pids = pids->next;
  1283. }
  1284. }
  1285. /*****************************************************************
  1286. *
  1287. * FUNCTIONS WITH THE MAIN LOOP
  1288. *
  1289. *****************************************************************/
  1290. struct netdata_static_thread socket_threads = {"EBPF SOCKET READ",
  1291. NULL, NULL, 1, NULL,
  1292. NULL, ebpf_socket_read_hash };
  1293. /**
  1294. * Main loop for this collector.
  1295. *
  1296. * @param step the number of microseconds used with heart beat
  1297. * @param em the structure with thread information
  1298. */
  1299. static void socket_collector(usec_t step, ebpf_module_t *em)
  1300. {
  1301. UNUSED(em);
  1302. UNUSED(step);
  1303. heartbeat_t hb;
  1304. heartbeat_init(&hb);
  1305. socket_threads.thread = mallocz(sizeof(netdata_thread_t));
  1306. netdata_thread_create(socket_threads.thread, socket_threads.name,
  1307. NETDATA_THREAD_OPTION_JOINABLE, ebpf_socket_read_hash, em);
  1308. int socket_apps_enabled = ebpf_modules[EBPF_MODULE_SOCKET_IDX].apps_charts;
  1309. int socket_global_enabled = ebpf_modules[EBPF_MODULE_SOCKET_IDX].global_charts;
  1310. int network_connection = em->optional;
  1311. while (!close_ebpf_plugin) {
  1312. pthread_mutex_lock(&collect_data_mutex);
  1313. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  1314. if (socket_global_enabled)
  1315. read_hash_global_tables();
  1316. if (socket_apps_enabled)
  1317. ebpf_socket_update_apps_data();
  1318. calculate_nv_plot();
  1319. pthread_mutex_lock(&lock);
  1320. if (socket_global_enabled)
  1321. ebpf_socket_send_data(em);
  1322. if (socket_apps_enabled)
  1323. ebpf_socket_send_apps_data(em, apps_groups_root_target);
  1324. fflush(stdout);
  1325. if (network_connection) {
  1326. // We are calling fflush many times, because when we have a lot of dimensions
  1327. // we began to have not expected outputs and Netdata closed the plugin.
  1328. pthread_mutex_lock(&nv_mutex);
  1329. ebpf_socket_create_nv_charts(&inbound_vectors);
  1330. fflush(stdout);
  1331. ebpf_socket_send_nv_data(&inbound_vectors);
  1332. ebpf_socket_create_nv_charts(&outbound_vectors);
  1333. fflush(stdout);
  1334. ebpf_socket_send_nv_data(&outbound_vectors);
  1335. wait_to_plot = 0;
  1336. pthread_mutex_unlock(&nv_mutex);
  1337. }
  1338. pthread_mutex_unlock(&collect_data_mutex);
  1339. pthread_mutex_unlock(&lock);
  1340. }
  1341. }
  1342. /*****************************************************************
  1343. *
  1344. * FUNCTIONS TO CLOSE THE THREAD
  1345. *
  1346. *****************************************************************/
  1347. /**
  1348. * Clean internal socket plot
  1349. *
  1350. * Clean all structures allocated with strdupz.
  1351. *
  1352. * @param ptr the pointer with addresses to clean.
  1353. */
  1354. static inline void clean_internal_socket_plot(netdata_socket_plot_t *ptr)
  1355. {
  1356. freez(ptr->dimension_recv);
  1357. freez(ptr->dimension_sent);
  1358. freez(ptr->resolved_name);
  1359. freez(ptr->dimension_retransmit);
  1360. }
  1361. /**
  1362. * Clean socket plot
  1363. *
  1364. * Clean the allocated data for inbound and outbound vectors.
  1365. */
  1366. static void clean_allocated_socket_plot()
  1367. {
  1368. uint32_t i;
  1369. uint32_t end = inbound_vectors.last;
  1370. netdata_socket_plot_t *plot = inbound_vectors.plot;
  1371. for (i = 0; i < end; i++) {
  1372. clean_internal_socket_plot(&plot[i]);
  1373. }
  1374. clean_internal_socket_plot(&plot[inbound_vectors.last]);
  1375. end = outbound_vectors.last;
  1376. plot = outbound_vectors.plot;
  1377. for (i = 0; i < end; i++) {
  1378. clean_internal_socket_plot(&plot[i]);
  1379. }
  1380. clean_internal_socket_plot(&plot[outbound_vectors.last]);
  1381. }
  1382. /**
  1383. * Clean netowrk ports allocated during initializaion.
  1384. *
  1385. * @param ptr a pointer to the link list.
  1386. */
  1387. static void clean_network_ports(ebpf_network_viewer_port_list_t *ptr)
  1388. {
  1389. if (unlikely(!ptr))
  1390. return;
  1391. while (ptr) {
  1392. ebpf_network_viewer_port_list_t *next = ptr->next;
  1393. freez(ptr->value);
  1394. freez(ptr);
  1395. ptr = next;
  1396. }
  1397. }
  1398. /**
  1399. * Clean service names
  1400. *
  1401. * Clean the allocated link list that stores names.
  1402. *
  1403. * @param names the link list.
  1404. */
  1405. static void clean_service_names(ebpf_network_viewer_dim_name_t *names)
  1406. {
  1407. if (unlikely(!names))
  1408. return;
  1409. while (names) {
  1410. ebpf_network_viewer_dim_name_t *next = names->next;
  1411. freez(names->name);
  1412. freez(names);
  1413. names = next;
  1414. }
  1415. }
  1416. /**
  1417. * Clean hostnames
  1418. *
  1419. * @param hostnames the hostnames to clean
  1420. */
  1421. static void clean_hostnames(ebpf_network_viewer_hostname_list_t *hostnames)
  1422. {
  1423. if (unlikely(!hostnames))
  1424. return;
  1425. while (hostnames) {
  1426. ebpf_network_viewer_hostname_list_t *next = hostnames->next;
  1427. freez(hostnames->value);
  1428. simple_pattern_free(hostnames->value_pattern);
  1429. freez(hostnames);
  1430. hostnames = next;
  1431. }
  1432. }
  1433. void clean_thread_structures() {
  1434. struct pid_stat *pids = root_of_pids;
  1435. while (pids) {
  1436. freez(socket_bandwidth_curr[pids->pid]);
  1437. pids = pids->next;
  1438. }
  1439. }
  1440. /**
  1441. * Clean up the main thread.
  1442. *
  1443. * @param ptr thread data.
  1444. */
  1445. static void ebpf_socket_cleanup(void *ptr)
  1446. {
  1447. ebpf_module_t *em = (ebpf_module_t *)ptr;
  1448. if (!em->enabled)
  1449. return;
  1450. heartbeat_t hb;
  1451. heartbeat_init(&hb);
  1452. uint32_t tick = 200*USEC_PER_MS;
  1453. while (!read_thread_closed) {
  1454. usec_t dt = heartbeat_next(&hb, tick);
  1455. UNUSED(dt);
  1456. }
  1457. freez(socket_aggregated_data);
  1458. freez(socket_publish_aggregated);
  1459. freez(socket_hash_values);
  1460. clean_thread_structures();
  1461. freez(socket_bandwidth_curr);
  1462. freez(socket_bandwidth_prev);
  1463. freez(bandwidth_vector);
  1464. freez(socket_values);
  1465. clean_allocated_socket_plot();
  1466. freez(inbound_vectors.plot);
  1467. freez(outbound_vectors.plot);
  1468. clean_port_structure(&listen_ports);
  1469. ebpf_modules[EBPF_MODULE_SOCKET_IDX].enabled = 0;
  1470. clean_network_ports(network_viewer_opt.included_port);
  1471. clean_network_ports(network_viewer_opt.excluded_port);
  1472. clean_service_names(network_viewer_opt.names);
  1473. clean_hostnames(network_viewer_opt.included_hostnames);
  1474. clean_hostnames(network_viewer_opt.excluded_hostnames);
  1475. pthread_mutex_destroy(&nv_mutex);
  1476. freez(socket_data.map_fd);
  1477. freez(socket_threads.thread);
  1478. struct bpf_program *prog;
  1479. size_t i = 0 ;
  1480. bpf_object__for_each_program(prog, objects) {
  1481. bpf_link__destroy(probe_links[i]);
  1482. i++;
  1483. }
  1484. bpf_object__close(objects);
  1485. finalized_threads = 1;
  1486. }
  1487. /*****************************************************************
  1488. *
  1489. * FUNCTIONS TO START THREAD
  1490. *
  1491. *****************************************************************/
  1492. /**
  1493. * Allocate vectors used with this thread.
  1494. * We are not testing the return, because callocz does this and shutdown the software
  1495. * case it was not possible to allocate.
  1496. *
  1497. * @param length is the length for the vectors used inside the collector.
  1498. */
  1499. static void ebpf_socket_allocate_global_vectors(size_t length)
  1500. {
  1501. socket_aggregated_data = callocz(length, sizeof(netdata_syscall_stat_t));
  1502. socket_publish_aggregated = callocz(length, sizeof(netdata_publish_syscall_t));
  1503. socket_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  1504. socket_bandwidth_curr = callocz((size_t)pid_max, sizeof(ebpf_socket_publish_apps_t *));
  1505. socket_bandwidth_prev = callocz((size_t)pid_max, sizeof(ebpf_socket_publish_apps_t *));
  1506. bandwidth_vector = callocz((size_t)ebpf_nprocs, sizeof(ebpf_bandwidth_t));
  1507. socket_values = callocz((size_t)ebpf_nprocs, sizeof(netdata_socket_t));
  1508. inbound_vectors.plot = callocz(network_viewer_opt.max_dim, sizeof(netdata_socket_plot_t));
  1509. outbound_vectors.plot = callocz(network_viewer_opt.max_dim, sizeof(netdata_socket_plot_t));
  1510. }
  1511. void change_socket_event()
  1512. {
  1513. socket_probes[0].type = 'p';
  1514. socket_probes[4].type = 'p';
  1515. socket_probes[5].type = 'p';
  1516. socket_probes[7].name = NULL;
  1517. }
  1518. /**
  1519. * Set local function pointers, this function will never be compiled with static libraries
  1520. */
  1521. static void set_local_pointers(ebpf_module_t *em)
  1522. {
  1523. map_fd = socket_data.map_fd;
  1524. if (em->mode == MODE_ENTRY) {
  1525. change_socket_event();
  1526. }
  1527. }
  1528. /**
  1529. * Initialize Inbound and Outbound
  1530. *
  1531. * Initialize the common outbound and inbound sockets.
  1532. */
  1533. static void initialize_inbound_outbound()
  1534. {
  1535. inbound_vectors.last = network_viewer_opt.max_dim - 1;
  1536. outbound_vectors.last = inbound_vectors.last;
  1537. fill_last_nv_dimension(&inbound_vectors.plot[inbound_vectors.last], 0);
  1538. fill_last_nv_dimension(&outbound_vectors.plot[outbound_vectors.last], 1);
  1539. }
  1540. /*****************************************************************
  1541. *
  1542. * EBPF SOCKET THREAD
  1543. *
  1544. *****************************************************************/
  1545. /**
  1546. * Socket thread
  1547. *
  1548. * Thread used to generate socket charts.
  1549. *
  1550. * @param ptr a pointer to `struct ebpf_module`
  1551. *
  1552. * @return It always return NULL
  1553. */
  1554. void *ebpf_socket_thread(void *ptr)
  1555. {
  1556. netdata_thread_cleanup_push(ebpf_socket_cleanup, ptr);
  1557. avl_init_lock(&inbound_vectors.tree, compare_sockets);
  1558. avl_init_lock(&outbound_vectors.tree, compare_sockets);
  1559. ebpf_module_t *em = (ebpf_module_t *)ptr;
  1560. fill_ebpf_data(&socket_data);
  1561. if (!em->enabled)
  1562. goto endsocket;
  1563. if (pthread_mutex_init(&nv_mutex, NULL)) {
  1564. error("Cannot initialize local mutex");
  1565. goto endsocket;
  1566. }
  1567. pthread_mutex_lock(&lock);
  1568. ebpf_socket_allocate_global_vectors(NETDATA_MAX_SOCKET_VECTOR);
  1569. initialize_inbound_outbound();
  1570. if (ebpf_update_kernel(&socket_data)) {
  1571. pthread_mutex_unlock(&lock);
  1572. goto endsocket;
  1573. }
  1574. set_local_pointers(em);
  1575. probe_links = ebpf_load_program(ebpf_plugin_dir, em, kernel_string, &objects, socket_data.map_fd);
  1576. if (!probe_links) {
  1577. pthread_mutex_unlock(&lock);
  1578. goto endsocket;
  1579. }
  1580. ebpf_global_labels(
  1581. socket_aggregated_data, socket_publish_aggregated, socket_dimension_names, socket_id_names,
  1582. NETDATA_MAX_SOCKET_VECTOR);
  1583. ebpf_create_global_charts(em);
  1584. finalized_threads = 0;
  1585. pthread_mutex_unlock(&lock);
  1586. socket_collector((usec_t)(em->update_time * USEC_PER_SEC), em);
  1587. endsocket:
  1588. netdata_thread_cleanup_pop(1);
  1589. return NULL;
  1590. }