ebpf_socket.c 145 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958
  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. // ARAL vectors used to speed up processing
  7. /*****************************************************************
  8. *
  9. * GLOBAL VARIABLES
  10. *
  11. *****************************************************************/
  12. static char *socket_dimension_names[NETDATA_MAX_SOCKET_VECTOR] = { "received", "sent", "close",
  13. "received", "sent", "retransmitted",
  14. "connected_V4", "connected_V6", "connected_tcp",
  15. "connected_udp"};
  16. static char *socket_id_names[NETDATA_MAX_SOCKET_VECTOR] = { "tcp_cleanup_rbuf", "tcp_sendmsg", "tcp_close",
  17. "udp_recvmsg", "udp_sendmsg", "tcp_retransmit_skb",
  18. "tcp_connect_v4", "tcp_connect_v6", "inet_csk_accept_tcp",
  19. "inet_csk_accept_udp" };
  20. static ebpf_local_maps_t socket_maps[] = {{.name = "tbl_bandwidth",
  21. .internal_input = NETDATA_COMPILED_CONNECTIONS_ALLOWED,
  22. .user_input = NETDATA_MAXIMUM_CONNECTIONS_ALLOWED,
  23. .type = NETDATA_EBPF_MAP_RESIZABLE | NETDATA_EBPF_MAP_PID,
  24. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  25. {.name = "tbl_global_sock",
  26. .internal_input = NETDATA_SOCKET_COUNTER,
  27. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  28. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  29. {.name = "tbl_lports",
  30. .internal_input = NETDATA_SOCKET_COUNTER,
  31. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  32. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  33. {.name = "tbl_conn_ipv4",
  34. .internal_input = NETDATA_COMPILED_CONNECTIONS_ALLOWED,
  35. .user_input = NETDATA_MAXIMUM_CONNECTIONS_ALLOWED,
  36. .type = NETDATA_EBPF_MAP_STATIC,
  37. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  38. {.name = "tbl_conn_ipv6",
  39. .internal_input = NETDATA_COMPILED_CONNECTIONS_ALLOWED,
  40. .user_input = NETDATA_MAXIMUM_CONNECTIONS_ALLOWED,
  41. .type = NETDATA_EBPF_MAP_STATIC,
  42. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  43. {.name = "tbl_nv_udp",
  44. .internal_input = NETDATA_COMPILED_UDP_CONNECTIONS_ALLOWED,
  45. .user_input = NETDATA_MAXIMUM_UDP_CONNECTIONS_ALLOWED,
  46. .type = NETDATA_EBPF_MAP_STATIC,
  47. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  48. {.name = "socket_ctrl", .internal_input = NETDATA_CONTROLLER_END,
  49. .user_input = 0,
  50. .type = NETDATA_EBPF_MAP_CONTROLLER,
  51. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  52. {.name = NULL, .internal_input = 0, .user_input = 0}};
  53. static netdata_idx_t *socket_hash_values = NULL;
  54. static netdata_syscall_stat_t socket_aggregated_data[NETDATA_MAX_SOCKET_VECTOR];
  55. static netdata_publish_syscall_t socket_publish_aggregated[NETDATA_MAX_SOCKET_VECTOR];
  56. static ebpf_bandwidth_t *bandwidth_vector = NULL;
  57. pthread_mutex_t nv_mutex;
  58. netdata_vector_plot_t inbound_vectors = { .plot = NULL, .next = 0, .last = 0 };
  59. netdata_vector_plot_t outbound_vectors = { .plot = NULL, .next = 0, .last = 0 };
  60. netdata_socket_t *socket_values;
  61. ebpf_network_viewer_port_list_t *listen_ports = NULL;
  62. struct config socket_config = { .first_section = NULL,
  63. .last_section = NULL,
  64. .mutex = NETDATA_MUTEX_INITIALIZER,
  65. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  66. .rwlock = AVL_LOCK_INITIALIZER } };
  67. netdata_ebpf_targets_t socket_targets[] = { {.name = "inet_csk_accept", .mode = EBPF_LOAD_TRAMPOLINE},
  68. {.name = "tcp_retransmit_skb", .mode = EBPF_LOAD_TRAMPOLINE},
  69. {.name = "tcp_cleanup_rbuf", .mode = EBPF_LOAD_TRAMPOLINE},
  70. {.name = "tcp_close", .mode = EBPF_LOAD_TRAMPOLINE},
  71. {.name = "udp_recvmsg", .mode = EBPF_LOAD_TRAMPOLINE},
  72. {.name = "tcp_sendmsg", .mode = EBPF_LOAD_TRAMPOLINE},
  73. {.name = "udp_sendmsg", .mode = EBPF_LOAD_TRAMPOLINE},
  74. {.name = "tcp_v4_connect", .mode = EBPF_LOAD_TRAMPOLINE},
  75. {.name = "tcp_v6_connect", .mode = EBPF_LOAD_TRAMPOLINE},
  76. {.name = NULL, .mode = EBPF_LOAD_TRAMPOLINE}};
  77. struct netdata_static_thread socket_threads = {
  78. .name = "EBPF SOCKET READ",
  79. .config_section = NULL,
  80. .config_name = NULL,
  81. .env_name = NULL,
  82. .enabled = 1,
  83. .thread = NULL,
  84. .init_routine = NULL,
  85. .start_routine = NULL
  86. };
  87. #ifdef LIBBPF_MAJOR_VERSION
  88. /**
  89. * Disable Probe
  90. *
  91. * Disable probes to use trampoline.
  92. *
  93. * @param obj is the main structure for bpf objects.
  94. */
  95. static void ebpf_socket_disable_probes(struct socket_bpf *obj)
  96. {
  97. bpf_program__set_autoload(obj->progs.netdata_inet_csk_accept_kretprobe, false);
  98. bpf_program__set_autoload(obj->progs.netdata_tcp_v4_connect_kretprobe, false);
  99. bpf_program__set_autoload(obj->progs.netdata_tcp_v6_connect_kretprobe, false);
  100. bpf_program__set_autoload(obj->progs.netdata_tcp_retransmit_skb_kprobe, false);
  101. bpf_program__set_autoload(obj->progs.netdata_tcp_cleanup_rbuf_kprobe, false);
  102. bpf_program__set_autoload(obj->progs.netdata_tcp_close_kprobe, false);
  103. bpf_program__set_autoload(obj->progs.netdata_udp_recvmsg_kprobe, false);
  104. bpf_program__set_autoload(obj->progs.netdata_udp_recvmsg_kretprobe, false);
  105. bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_kretprobe, false);
  106. bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_kprobe, false);
  107. bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_kretprobe, false);
  108. bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_kprobe, false);
  109. bpf_program__set_autoload(obj->progs.netdata_socket_release_task_kprobe, false);
  110. }
  111. /**
  112. * Disable Trampoline
  113. *
  114. * Disable trampoline to use probes.
  115. *
  116. * @param obj is the main structure for bpf objects.
  117. */
  118. static void ebpf_socket_disable_trampoline(struct socket_bpf *obj)
  119. {
  120. bpf_program__set_autoload(obj->progs.netdata_inet_csk_accept_fentry, false);
  121. bpf_program__set_autoload(obj->progs.netdata_tcp_v4_connect_fexit, false);
  122. bpf_program__set_autoload(obj->progs.netdata_tcp_v6_connect_fexit, false);
  123. bpf_program__set_autoload(obj->progs.netdata_tcp_retransmit_skb_fentry, false);
  124. bpf_program__set_autoload(obj->progs.netdata_tcp_cleanup_rbuf_fentry, false);
  125. bpf_program__set_autoload(obj->progs.netdata_tcp_close_fentry, false);
  126. bpf_program__set_autoload(obj->progs.netdata_udp_recvmsg_fentry, false);
  127. bpf_program__set_autoload(obj->progs.netdata_udp_recvmsg_fexit, false);
  128. bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_fentry, false);
  129. bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_fexit, false);
  130. bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_fentry, false);
  131. bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_fexit, false);
  132. bpf_program__set_autoload(obj->progs.netdata_socket_release_task_fentry, false);
  133. }
  134. /**
  135. * Set trampoline target.
  136. *
  137. * @param obj is the main structure for bpf objects.
  138. */
  139. static void ebpf_set_trampoline_target(struct socket_bpf *obj)
  140. {
  141. bpf_program__set_attach_target(obj->progs.netdata_inet_csk_accept_fentry, 0,
  142. socket_targets[NETDATA_FCNT_INET_CSK_ACCEPT].name);
  143. bpf_program__set_attach_target(obj->progs.netdata_tcp_v4_connect_fexit, 0,
  144. socket_targets[NETDATA_FCNT_TCP_V4_CONNECT].name);
  145. bpf_program__set_attach_target(obj->progs.netdata_tcp_v6_connect_fexit, 0,
  146. socket_targets[NETDATA_FCNT_TCP_V6_CONNECT].name);
  147. bpf_program__set_attach_target(obj->progs.netdata_tcp_retransmit_skb_fentry, 0,
  148. socket_targets[NETDATA_FCNT_TCP_RETRANSMIT].name);
  149. bpf_program__set_attach_target(obj->progs.netdata_tcp_cleanup_rbuf_fentry, 0,
  150. socket_targets[NETDATA_FCNT_CLEANUP_RBUF].name);
  151. bpf_program__set_attach_target(obj->progs.netdata_tcp_close_fentry, 0, socket_targets[NETDATA_FCNT_TCP_CLOSE].name);
  152. bpf_program__set_attach_target(obj->progs.netdata_udp_recvmsg_fentry, 0,
  153. socket_targets[NETDATA_FCNT_UDP_RECEVMSG].name);
  154. bpf_program__set_attach_target(obj->progs.netdata_udp_recvmsg_fexit, 0,
  155. socket_targets[NETDATA_FCNT_UDP_RECEVMSG].name);
  156. bpf_program__set_attach_target(obj->progs.netdata_tcp_sendmsg_fentry, 0,
  157. socket_targets[NETDATA_FCNT_TCP_SENDMSG].name);
  158. bpf_program__set_attach_target(obj->progs.netdata_tcp_sendmsg_fexit, 0,
  159. socket_targets[NETDATA_FCNT_TCP_SENDMSG].name);
  160. bpf_program__set_attach_target(obj->progs.netdata_udp_sendmsg_fentry, 0,
  161. socket_targets[NETDATA_FCNT_UDP_SENDMSG].name);
  162. bpf_program__set_attach_target(obj->progs.netdata_udp_sendmsg_fexit, 0,
  163. socket_targets[NETDATA_FCNT_UDP_SENDMSG].name);
  164. bpf_program__set_attach_target(obj->progs.netdata_socket_release_task_fentry, 0, EBPF_COMMON_FNCT_CLEAN_UP);
  165. }
  166. /**
  167. * Disable specific trampoline
  168. *
  169. * Disable specific trampoline to match user selection.
  170. *
  171. * @param obj is the main structure for bpf objects.
  172. * @param sel option selected by user.
  173. */
  174. static inline void ebpf_socket_disable_specific_trampoline(struct socket_bpf *obj, netdata_run_mode_t sel)
  175. {
  176. if (sel == MODE_RETURN) {
  177. bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_fentry, false);
  178. bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_fentry, false);
  179. } else {
  180. bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_fexit, false);
  181. bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_fexit, false);
  182. }
  183. }
  184. /**
  185. * Disable specific probe
  186. *
  187. * Disable specific probe to match user selection.
  188. *
  189. * @param obj is the main structure for bpf objects.
  190. * @param sel option selected by user.
  191. */
  192. static inline void ebpf_socket_disable_specific_probe(struct socket_bpf *obj, netdata_run_mode_t sel)
  193. {
  194. if (sel == MODE_RETURN) {
  195. bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_kprobe, false);
  196. bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_kprobe, false);
  197. } else {
  198. bpf_program__set_autoload(obj->progs.netdata_tcp_sendmsg_kretprobe, false);
  199. bpf_program__set_autoload(obj->progs.netdata_udp_sendmsg_kretprobe, false);
  200. }
  201. }
  202. /**
  203. * Attach probes
  204. *
  205. * Attach probes to targets.
  206. *
  207. * @param obj is the main structure for bpf objects.
  208. * @param sel option selected by user.
  209. */
  210. static int ebpf_socket_attach_probes(struct socket_bpf *obj, netdata_run_mode_t sel)
  211. {
  212. obj->links.netdata_inet_csk_accept_kretprobe = bpf_program__attach_kprobe(obj->progs.netdata_inet_csk_accept_kretprobe,
  213. true,
  214. socket_targets[NETDATA_FCNT_INET_CSK_ACCEPT].name);
  215. int ret = libbpf_get_error(obj->links.netdata_inet_csk_accept_kretprobe);
  216. if (ret)
  217. return -1;
  218. obj->links.netdata_tcp_v4_connect_kretprobe = bpf_program__attach_kprobe(obj->progs.netdata_tcp_v4_connect_kretprobe,
  219. true,
  220. socket_targets[NETDATA_FCNT_TCP_V4_CONNECT].name);
  221. ret = libbpf_get_error(obj->links.netdata_tcp_v4_connect_kretprobe);
  222. if (ret)
  223. return -1;
  224. obj->links.netdata_tcp_v6_connect_kretprobe = bpf_program__attach_kprobe(obj->progs.netdata_tcp_v6_connect_kretprobe,
  225. true,
  226. socket_targets[NETDATA_FCNT_TCP_V6_CONNECT].name);
  227. ret = libbpf_get_error(obj->links.netdata_tcp_v6_connect_kretprobe);
  228. if (ret)
  229. return -1;
  230. obj->links.netdata_tcp_retransmit_skb_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_tcp_retransmit_skb_kprobe,
  231. false,
  232. socket_targets[NETDATA_FCNT_TCP_RETRANSMIT].name);
  233. ret = libbpf_get_error(obj->links.netdata_tcp_retransmit_skb_kprobe);
  234. if (ret)
  235. return -1;
  236. obj->links.netdata_tcp_cleanup_rbuf_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_tcp_cleanup_rbuf_kprobe,
  237. false,
  238. socket_targets[NETDATA_FCNT_CLEANUP_RBUF].name);
  239. ret = libbpf_get_error(obj->links.netdata_tcp_cleanup_rbuf_kprobe);
  240. if (ret)
  241. return -1;
  242. obj->links.netdata_tcp_close_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_tcp_close_kprobe,
  243. false,
  244. socket_targets[NETDATA_FCNT_TCP_CLOSE].name);
  245. ret = libbpf_get_error(obj->links.netdata_tcp_close_kprobe);
  246. if (ret)
  247. return -1;
  248. obj->links.netdata_udp_recvmsg_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_udp_recvmsg_kprobe,
  249. false,
  250. socket_targets[NETDATA_FCNT_UDP_RECEVMSG].name);
  251. ret = libbpf_get_error(obj->links.netdata_udp_recvmsg_kprobe);
  252. if (ret)
  253. return -1;
  254. obj->links.netdata_udp_recvmsg_kretprobe = bpf_program__attach_kprobe(obj->progs.netdata_udp_recvmsg_kretprobe,
  255. true,
  256. socket_targets[NETDATA_FCNT_UDP_RECEVMSG].name);
  257. ret = libbpf_get_error(obj->links.netdata_udp_recvmsg_kretprobe);
  258. if (ret)
  259. return -1;
  260. if (sel == MODE_RETURN) {
  261. obj->links.netdata_tcp_sendmsg_kretprobe = bpf_program__attach_kprobe(obj->progs.netdata_tcp_sendmsg_kretprobe,
  262. true,
  263. socket_targets[NETDATA_FCNT_TCP_SENDMSG].name);
  264. ret = libbpf_get_error(obj->links.netdata_tcp_sendmsg_kretprobe);
  265. if (ret)
  266. return -1;
  267. obj->links.netdata_udp_sendmsg_kretprobe = bpf_program__attach_kprobe(obj->progs.netdata_udp_sendmsg_kretprobe,
  268. true,
  269. socket_targets[NETDATA_FCNT_UDP_SENDMSG].name);
  270. ret = libbpf_get_error(obj->links.netdata_udp_sendmsg_kretprobe);
  271. if (ret)
  272. return -1;
  273. } else {
  274. obj->links.netdata_tcp_sendmsg_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_tcp_sendmsg_kprobe,
  275. false,
  276. socket_targets[NETDATA_FCNT_TCP_SENDMSG].name);
  277. ret = libbpf_get_error(obj->links.netdata_tcp_sendmsg_kprobe);
  278. if (ret)
  279. return -1;
  280. obj->links.netdata_udp_sendmsg_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_udp_sendmsg_kprobe,
  281. false,
  282. socket_targets[NETDATA_FCNT_UDP_SENDMSG].name);
  283. ret = libbpf_get_error(obj->links.netdata_udp_sendmsg_kprobe);
  284. if (ret)
  285. return -1;
  286. }
  287. obj->links.netdata_socket_release_task_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_socket_release_task_kprobe,
  288. false, EBPF_COMMON_FNCT_CLEAN_UP);
  289. ret = libbpf_get_error(obj->links.netdata_socket_release_task_kprobe);
  290. if (ret)
  291. return -1;
  292. return 0;
  293. }
  294. /**
  295. * Set hash tables
  296. *
  297. * Set the values for maps according the value given by kernel.
  298. *
  299. * @param obj is the main structure for bpf objects.
  300. */
  301. static void ebpf_socket_set_hash_tables(struct socket_bpf *obj)
  302. {
  303. socket_maps[NETDATA_SOCKET_TABLE_BANDWIDTH].map_fd = bpf_map__fd(obj->maps.tbl_bandwidth);
  304. socket_maps[NETDATA_SOCKET_GLOBAL].map_fd = bpf_map__fd(obj->maps.tbl_global_sock);
  305. socket_maps[NETDATA_SOCKET_LPORTS].map_fd = bpf_map__fd(obj->maps.tbl_lports);
  306. socket_maps[NETDATA_SOCKET_TABLE_IPV4].map_fd = bpf_map__fd(obj->maps.tbl_conn_ipv4);
  307. socket_maps[NETDATA_SOCKET_TABLE_IPV6].map_fd = bpf_map__fd(obj->maps.tbl_conn_ipv6);
  308. socket_maps[NETDATA_SOCKET_TABLE_UDP].map_fd = bpf_map__fd(obj->maps.tbl_nv_udp);
  309. socket_maps[NETDATA_SOCKET_TABLE_CTRL].map_fd = bpf_map__fd(obj->maps.socket_ctrl);
  310. }
  311. /**
  312. * Adjust Map Size
  313. *
  314. * Resize maps according input from users.
  315. *
  316. * @param obj is the main structure for bpf objects.
  317. * @param em structure with configuration
  318. */
  319. static void ebpf_socket_adjust_map_size(struct socket_bpf *obj, ebpf_module_t *em)
  320. {
  321. ebpf_update_map_size(obj->maps.tbl_bandwidth, &socket_maps[NETDATA_SOCKET_TABLE_BANDWIDTH],
  322. em, bpf_map__name(obj->maps.tbl_bandwidth));
  323. ebpf_update_map_size(obj->maps.tbl_conn_ipv4, &socket_maps[NETDATA_SOCKET_TABLE_IPV4],
  324. em, bpf_map__name(obj->maps.tbl_conn_ipv4));
  325. ebpf_update_map_size(obj->maps.tbl_conn_ipv6, &socket_maps[NETDATA_SOCKET_TABLE_IPV6],
  326. em, bpf_map__name(obj->maps.tbl_conn_ipv6));
  327. ebpf_update_map_size(obj->maps.tbl_nv_udp, &socket_maps[NETDATA_SOCKET_TABLE_UDP],
  328. em, bpf_map__name(obj->maps.tbl_nv_udp));
  329. }
  330. /**
  331. * Load and attach
  332. *
  333. * Load and attach the eBPF code in kernel.
  334. *
  335. * @param obj is the main structure for bpf objects.
  336. * @param em structure with configuration
  337. *
  338. * @return it returns 0 on success and -1 otherwise
  339. */
  340. static inline int ebpf_socket_load_and_attach(struct socket_bpf *obj, ebpf_module_t *em)
  341. {
  342. netdata_ebpf_targets_t *mt = em->targets;
  343. netdata_ebpf_program_loaded_t test = mt[NETDATA_FCNT_INET_CSK_ACCEPT].mode;
  344. if (test == EBPF_LOAD_TRAMPOLINE) {
  345. ebpf_socket_disable_probes(obj);
  346. ebpf_set_trampoline_target(obj);
  347. ebpf_socket_disable_specific_trampoline(obj, em->mode);
  348. } else { // We are not using tracepoints for this thread.
  349. ebpf_socket_disable_trampoline(obj);
  350. ebpf_socket_disable_specific_probe(obj, em->mode);
  351. }
  352. int ret = socket_bpf__load(obj);
  353. if (ret) {
  354. fprintf(stderr, "failed to load BPF object: %d\n", ret);
  355. return ret;
  356. }
  357. ebpf_socket_adjust_map_size(obj, em);
  358. if (test == EBPF_LOAD_TRAMPOLINE) {
  359. ret = socket_bpf__attach(obj);
  360. } else {
  361. ret = ebpf_socket_attach_probes(obj, em->mode);
  362. }
  363. if (!ret) {
  364. ebpf_socket_set_hash_tables(obj);
  365. ebpf_update_controller(socket_maps[NETDATA_SOCKET_TABLE_CTRL].map_fd, em);
  366. }
  367. return ret;
  368. }
  369. #endif
  370. /*****************************************************************
  371. *
  372. * FUNCTIONS TO CLOSE THE THREAD
  373. *
  374. *****************************************************************/
  375. /**
  376. * Clean internal socket plot
  377. *
  378. * Clean all structures allocated with strdupz.
  379. *
  380. * @param ptr the pointer with addresses to clean.
  381. */
  382. static inline void clean_internal_socket_plot(netdata_socket_plot_t *ptr)
  383. {
  384. freez(ptr->dimension_recv);
  385. freez(ptr->dimension_sent);
  386. freez(ptr->resolved_name);
  387. freez(ptr->dimension_retransmit);
  388. }
  389. /**
  390. * Clean socket plot
  391. *
  392. * Clean the allocated data for inbound and outbound vectors.
  393. static void clean_allocated_socket_plot()
  394. {
  395. if (!network_viewer_opt.enabled)
  396. return;
  397. uint32_t i;
  398. uint32_t end = inbound_vectors.last;
  399. netdata_socket_plot_t *plot = inbound_vectors.plot;
  400. for (i = 0; i < end; i++) {
  401. clean_internal_socket_plot(&plot[i]);
  402. }
  403. clean_internal_socket_plot(&plot[inbound_vectors.last]);
  404. end = outbound_vectors.last;
  405. plot = outbound_vectors.plot;
  406. for (i = 0; i < end; i++) {
  407. clean_internal_socket_plot(&plot[i]);
  408. }
  409. clean_internal_socket_plot(&plot[outbound_vectors.last]);
  410. }
  411. */
  412. /**
  413. * Clean network ports allocated during initialization.
  414. *
  415. * @param ptr a pointer to the link list.
  416. static void clean_network_ports(ebpf_network_viewer_port_list_t *ptr)
  417. {
  418. if (unlikely(!ptr))
  419. return;
  420. while (ptr) {
  421. ebpf_network_viewer_port_list_t *next = ptr->next;
  422. freez(ptr->value);
  423. freez(ptr);
  424. ptr = next;
  425. }
  426. }
  427. */
  428. /**
  429. * Clean service names
  430. *
  431. * Clean the allocated link list that stores names.
  432. *
  433. * @param names the link list.
  434. static void clean_service_names(ebpf_network_viewer_dim_name_t *names)
  435. {
  436. if (unlikely(!names))
  437. return;
  438. while (names) {
  439. ebpf_network_viewer_dim_name_t *next = names->next;
  440. freez(names->name);
  441. freez(names);
  442. names = next;
  443. }
  444. }
  445. */
  446. /**
  447. * Clean hostnames
  448. *
  449. * @param hostnames the hostnames to clean
  450. static void clean_hostnames(ebpf_network_viewer_hostname_list_t *hostnames)
  451. {
  452. if (unlikely(!hostnames))
  453. return;
  454. while (hostnames) {
  455. ebpf_network_viewer_hostname_list_t *next = hostnames->next;
  456. freez(hostnames->value);
  457. simple_pattern_free(hostnames->value_pattern);
  458. freez(hostnames);
  459. hostnames = next;
  460. }
  461. }
  462. */
  463. /**
  464. * Clean port Structure
  465. *
  466. * Clean the allocated list.
  467. *
  468. * @param clean the list that will be cleaned
  469. */
  470. void clean_port_structure(ebpf_network_viewer_port_list_t **clean)
  471. {
  472. ebpf_network_viewer_port_list_t *move = *clean;
  473. while (move) {
  474. ebpf_network_viewer_port_list_t *next = move->next;
  475. freez(move->value);
  476. freez(move);
  477. move = next;
  478. }
  479. *clean = NULL;
  480. }
  481. /**
  482. * Clean IP structure
  483. *
  484. * Clean the allocated list.
  485. *
  486. * @param clean the list that will be cleaned
  487. */
  488. static void clean_ip_structure(ebpf_network_viewer_ip_list_t **clean)
  489. {
  490. ebpf_network_viewer_ip_list_t *move = *clean;
  491. while (move) {
  492. ebpf_network_viewer_ip_list_t *next = move->next;
  493. freez(move->value);
  494. freez(move);
  495. move = next;
  496. }
  497. *clean = NULL;
  498. }
  499. /**
  500. * Socket Free
  501. *
  502. * Cleanup variables after child threads to stop
  503. *
  504. * @param ptr thread data.
  505. */
  506. static void ebpf_socket_free(ebpf_module_t *em )
  507. {
  508. /* We can have thousands of sockets to clean, so we are transferring
  509. * for OS the responsibility while we do not use ARAL here
  510. freez(socket_hash_values);
  511. freez(bandwidth_vector);
  512. freez(socket_values);
  513. clean_allocated_socket_plot();
  514. freez(inbound_vectors.plot);
  515. freez(outbound_vectors.plot);
  516. clean_port_structure(&listen_ports);
  517. clean_network_ports(network_viewer_opt.included_port);
  518. clean_network_ports(network_viewer_opt.excluded_port);
  519. clean_service_names(network_viewer_opt.names);
  520. clean_hostnames(network_viewer_opt.included_hostnames);
  521. clean_hostnames(network_viewer_opt.excluded_hostnames);
  522. */
  523. pthread_mutex_destroy(&nv_mutex);
  524. pthread_mutex_lock(&ebpf_exit_cleanup);
  525. em->enabled = NETDATA_THREAD_EBPF_STOPPED;
  526. pthread_mutex_unlock(&ebpf_exit_cleanup);
  527. }
  528. /**
  529. * Socket exit
  530. *
  531. * Clean up the main thread.
  532. *
  533. * @param ptr thread data.
  534. */
  535. static void ebpf_socket_exit(void *ptr)
  536. {
  537. ebpf_module_t *em = (ebpf_module_t *)ptr;
  538. pthread_mutex_lock(&nv_mutex);
  539. if (socket_threads.thread)
  540. netdata_thread_cancel(*socket_threads.thread);
  541. pthread_mutex_unlock(&nv_mutex);
  542. ebpf_socket_free(em);
  543. }
  544. /**
  545. * Socket cleanup
  546. *
  547. * Clean up allocated addresses.
  548. *
  549. * @param ptr thread data.
  550. */
  551. void ebpf_socket_cleanup(void *ptr)
  552. {
  553. UNUSED(ptr);
  554. }
  555. /*****************************************************************
  556. *
  557. * PROCESS DATA AND SEND TO NETDATA
  558. *
  559. *****************************************************************/
  560. /**
  561. * Update publish structure before to send data to Netdata.
  562. *
  563. * @param publish the first output structure with independent dimensions
  564. * @param tcp structure to store IO from tcp sockets
  565. * @param udp structure to store IO from udp sockets
  566. * @param input the structure with the input data.
  567. */
  568. static void ebpf_update_global_publish(
  569. netdata_publish_syscall_t *publish, netdata_publish_vfs_common_t *tcp, netdata_publish_vfs_common_t *udp,
  570. netdata_syscall_stat_t *input)
  571. {
  572. netdata_publish_syscall_t *move = publish;
  573. while (move) {
  574. if (input->call != move->pcall) {
  575. // This condition happens to avoid initial values with dimensions higher than normal values.
  576. if (move->pcall) {
  577. move->ncall = (input->call > move->pcall) ? input->call - move->pcall : move->pcall - input->call;
  578. move->nbyte = (input->bytes > move->pbyte) ? input->bytes - move->pbyte : move->pbyte - input->bytes;
  579. move->nerr = (input->ecall > move->nerr) ? input->ecall - move->perr : move->perr - input->ecall;
  580. } else {
  581. move->ncall = 0;
  582. move->nbyte = 0;
  583. move->nerr = 0;
  584. }
  585. move->pcall = input->call;
  586. move->pbyte = input->bytes;
  587. move->perr = input->ecall;
  588. } else {
  589. move->ncall = 0;
  590. move->nbyte = 0;
  591. move->nerr = 0;
  592. }
  593. input = input->next;
  594. move = move->next;
  595. }
  596. tcp->write = -(long)publish[0].nbyte;
  597. tcp->read = (long)publish[1].nbyte;
  598. udp->write = -(long)publish[3].nbyte;
  599. udp->read = (long)publish[4].nbyte;
  600. }
  601. /**
  602. * Update Network Viewer plot data
  603. *
  604. * @param plot the structure where the data will be stored
  605. * @param sock the last update from the socket
  606. */
  607. static inline void update_nv_plot_data(netdata_plot_values_t *plot, netdata_socket_t *sock)
  608. {
  609. if (sock->ct != plot->last_time) {
  610. plot->last_time = sock->ct;
  611. plot->plot_recv_packets = sock->recv_packets;
  612. plot->plot_sent_packets = sock->sent_packets;
  613. plot->plot_recv_bytes = sock->recv_bytes;
  614. plot->plot_sent_bytes = sock->sent_bytes;
  615. plot->plot_retransmit = sock->retransmit;
  616. }
  617. sock->recv_packets = 0;
  618. sock->sent_packets = 0;
  619. sock->recv_bytes = 0;
  620. sock->sent_bytes = 0;
  621. sock->retransmit = 0;
  622. }
  623. /**
  624. * Calculate Network Viewer Plot
  625. *
  626. * Do math with collected values before to plot data.
  627. */
  628. static inline void calculate_nv_plot()
  629. {
  630. pthread_mutex_lock(&nv_mutex);
  631. uint32_t i;
  632. uint32_t end = inbound_vectors.next;
  633. for (i = 0; i < end; i++) {
  634. update_nv_plot_data(&inbound_vectors.plot[i].plot, &inbound_vectors.plot[i].sock);
  635. }
  636. inbound_vectors.max_plot = end;
  637. // The 'Other' dimension is always calculated for the chart to have at least one dimension
  638. update_nv_plot_data(&inbound_vectors.plot[inbound_vectors.last].plot,
  639. &inbound_vectors.plot[inbound_vectors.last].sock);
  640. end = outbound_vectors.next;
  641. for (i = 0; i < end; i++) {
  642. update_nv_plot_data(&outbound_vectors.plot[i].plot, &outbound_vectors.plot[i].sock);
  643. }
  644. outbound_vectors.max_plot = end;
  645. /*
  646. // The 'Other' dimension is always calculated for the chart to have at least one dimension
  647. update_nv_plot_data(&outbound_vectors.plot[outbound_vectors.last].plot,
  648. &outbound_vectors.plot[outbound_vectors.last].sock);
  649. */
  650. pthread_mutex_unlock(&nv_mutex);
  651. }
  652. /**
  653. * Network viewer send bytes
  654. *
  655. * @param ptr the structure with values to plot
  656. * @param chart the chart name.
  657. */
  658. static inline void ebpf_socket_nv_send_bytes(netdata_vector_plot_t *ptr, char *chart)
  659. {
  660. uint32_t i;
  661. uint32_t end = ptr->last_plot;
  662. netdata_socket_plot_t *w = ptr->plot;
  663. collected_number value;
  664. write_begin_chart(NETDATA_EBPF_FAMILY, chart);
  665. for (i = 0; i < end; i++) {
  666. value = ((collected_number) w[i].plot.plot_sent_bytes);
  667. write_chart_dimension(w[i].dimension_sent, value);
  668. value = (collected_number) w[i].plot.plot_recv_bytes;
  669. write_chart_dimension(w[i].dimension_recv, value);
  670. }
  671. i = ptr->last;
  672. value = ((collected_number) w[i].plot.plot_sent_bytes);
  673. write_chart_dimension(w[i].dimension_sent, value);
  674. value = (collected_number) w[i].plot.plot_recv_bytes;
  675. write_chart_dimension(w[i].dimension_recv, value);
  676. write_end_chart();
  677. }
  678. /**
  679. * Network Viewer Send packets
  680. *
  681. * @param ptr the structure with values to plot
  682. * @param chart the chart name.
  683. */
  684. static inline void ebpf_socket_nv_send_packets(netdata_vector_plot_t *ptr, char *chart)
  685. {
  686. uint32_t i;
  687. uint32_t end = ptr->last_plot;
  688. netdata_socket_plot_t *w = ptr->plot;
  689. collected_number value;
  690. write_begin_chart(NETDATA_EBPF_FAMILY, chart);
  691. for (i = 0; i < end; i++) {
  692. value = ((collected_number)w[i].plot.plot_sent_packets);
  693. write_chart_dimension(w[i].dimension_sent, value);
  694. value = (collected_number) w[i].plot.plot_recv_packets;
  695. write_chart_dimension(w[i].dimension_recv, value);
  696. }
  697. i = ptr->last;
  698. value = ((collected_number)w[i].plot.plot_sent_packets);
  699. write_chart_dimension(w[i].dimension_sent, value);
  700. value = (collected_number)w[i].plot.plot_recv_packets;
  701. write_chart_dimension(w[i].dimension_recv, value);
  702. write_end_chart();
  703. }
  704. /**
  705. * Network Viewer Send Retransmit
  706. *
  707. * @param ptr the structure with values to plot
  708. * @param chart the chart name.
  709. */
  710. static inline void ebpf_socket_nv_send_retransmit(netdata_vector_plot_t *ptr, char *chart)
  711. {
  712. uint32_t i;
  713. uint32_t end = ptr->last_plot;
  714. netdata_socket_plot_t *w = ptr->plot;
  715. collected_number value;
  716. write_begin_chart(NETDATA_EBPF_FAMILY, chart);
  717. for (i = 0; i < end; i++) {
  718. value = (collected_number) w[i].plot.plot_retransmit;
  719. write_chart_dimension(w[i].dimension_retransmit, value);
  720. }
  721. i = ptr->last;
  722. value = (collected_number)w[i].plot.plot_retransmit;
  723. write_chart_dimension(w[i].dimension_retransmit, value);
  724. write_end_chart();
  725. }
  726. /**
  727. * Send network viewer data
  728. *
  729. * @param ptr the pointer to plot data
  730. */
  731. static void ebpf_socket_send_nv_data(netdata_vector_plot_t *ptr)
  732. {
  733. if (!ptr->flags)
  734. return;
  735. if (ptr == (netdata_vector_plot_t *)&outbound_vectors) {
  736. ebpf_socket_nv_send_bytes(ptr, NETDATA_NV_OUTBOUND_BYTES);
  737. fflush(stdout);
  738. ebpf_socket_nv_send_packets(ptr, NETDATA_NV_OUTBOUND_PACKETS);
  739. fflush(stdout);
  740. ebpf_socket_nv_send_retransmit(ptr, NETDATA_NV_OUTBOUND_RETRANSMIT);
  741. fflush(stdout);
  742. } else {
  743. ebpf_socket_nv_send_bytes(ptr, NETDATA_NV_INBOUND_BYTES);
  744. fflush(stdout);
  745. ebpf_socket_nv_send_packets(ptr, NETDATA_NV_INBOUND_PACKETS);
  746. fflush(stdout);
  747. }
  748. }
  749. /**
  750. * Send Global Inbound connection
  751. *
  752. * Send number of connections read per protocol.
  753. */
  754. static void ebpf_socket_send_global_inbound_conn()
  755. {
  756. uint64_t udp_conn = 0;
  757. uint64_t tcp_conn = 0;
  758. ebpf_network_viewer_port_list_t *move = listen_ports;
  759. while (move) {
  760. if (move->protocol == IPPROTO_TCP)
  761. tcp_conn += move->connections;
  762. else
  763. udp_conn += move->connections;
  764. move = move->next;
  765. }
  766. write_begin_chart(NETDATA_EBPF_IP_FAMILY, NETDATA_INBOUND_CONNECTIONS);
  767. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_INCOMING_CONNECTION_TCP].name, (long long) tcp_conn);
  768. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_INCOMING_CONNECTION_UDP].name, (long long) udp_conn);
  769. write_end_chart();
  770. }
  771. /**
  772. * Send data to Netdata calling auxiliary functions.
  773. *
  774. * @param em the structure with thread information
  775. */
  776. static void ebpf_socket_send_data(ebpf_module_t *em)
  777. {
  778. netdata_publish_vfs_common_t common_tcp;
  779. netdata_publish_vfs_common_t common_udp;
  780. ebpf_update_global_publish(socket_publish_aggregated, &common_tcp, &common_udp, socket_aggregated_data);
  781. ebpf_socket_send_global_inbound_conn();
  782. write_count_chart(NETDATA_TCP_OUTBOUND_CONNECTIONS, NETDATA_EBPF_IP_FAMILY,
  783. &socket_publish_aggregated[NETDATA_IDX_TCP_CONNECTION_V4], 2);
  784. // We read bytes from function arguments, but bandwidth is given in bits,
  785. // so we need to multiply by 8 to convert for the final value.
  786. write_count_chart(NETDATA_TCP_FUNCTION_COUNT, NETDATA_EBPF_IP_FAMILY, socket_publish_aggregated, 3);
  787. write_io_chart(NETDATA_TCP_FUNCTION_BITS, NETDATA_EBPF_IP_FAMILY, socket_id_names[0],
  788. common_tcp.read * 8/BITS_IN_A_KILOBIT, socket_id_names[1],
  789. common_tcp.write * 8/BITS_IN_A_KILOBIT);
  790. if (em->mode < MODE_ENTRY) {
  791. write_err_chart(NETDATA_TCP_FUNCTION_ERROR, NETDATA_EBPF_IP_FAMILY, socket_publish_aggregated, 2);
  792. }
  793. write_count_chart(NETDATA_TCP_RETRANSMIT, NETDATA_EBPF_IP_FAMILY,
  794. &socket_publish_aggregated[NETDATA_IDX_TCP_RETRANSMIT],1);
  795. write_count_chart(NETDATA_UDP_FUNCTION_COUNT, NETDATA_EBPF_IP_FAMILY,
  796. &socket_publish_aggregated[NETDATA_IDX_UDP_RECVBUF],2);
  797. write_io_chart(NETDATA_UDP_FUNCTION_BITS, NETDATA_EBPF_IP_FAMILY,
  798. socket_id_names[3], (long long)common_udp.read * 8/BITS_IN_A_KILOBIT,
  799. socket_id_names[4], (long long)common_udp.write * 8/BITS_IN_A_KILOBIT);
  800. if (em->mode < MODE_ENTRY) {
  801. write_err_chart(NETDATA_UDP_FUNCTION_ERROR, NETDATA_EBPF_IP_FAMILY,
  802. &socket_publish_aggregated[NETDATA_UDP_START], 2);
  803. }
  804. }
  805. /**
  806. * Sum values for pid
  807. *
  808. * @param root the structure with all available PIDs
  809. *
  810. * @param offset the address that we are reading
  811. *
  812. * @return it returns the sum of all PIDs
  813. */
  814. long long ebpf_socket_sum_values_for_pids(struct ebpf_pid_on_target *root, size_t offset)
  815. {
  816. long long ret = 0;
  817. while (root) {
  818. int32_t pid = root->pid;
  819. ebpf_socket_publish_apps_t *w = socket_bandwidth_curr[pid];
  820. if (w) {
  821. ret += get_value_from_structure((char *)w, offset);
  822. }
  823. root = root->next;
  824. }
  825. return ret;
  826. }
  827. /**
  828. * Send data to Netdata calling auxiliary functions.
  829. *
  830. * @param em the structure with thread information
  831. * @param root the target list.
  832. */
  833. void ebpf_socket_send_apps_data(ebpf_module_t *em, struct ebpf_target *root)
  834. {
  835. UNUSED(em);
  836. struct ebpf_target *w;
  837. collected_number value;
  838. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_CONNECTION_TCP_V4);
  839. for (w = root; w; w = w->next) {
  840. if (unlikely(w->exposed && w->processes)) {
  841. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  842. call_tcp_v4_connection));
  843. write_chart_dimension(w->name, value);
  844. }
  845. }
  846. write_end_chart();
  847. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_CONNECTION_TCP_V6);
  848. for (w = root; w; w = w->next) {
  849. if (unlikely(w->exposed && w->processes)) {
  850. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  851. call_tcp_v6_connection));
  852. write_chart_dimension(w->name, value);
  853. }
  854. }
  855. write_end_chart();
  856. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_SENT);
  857. for (w = root; w; w = w->next) {
  858. if (unlikely(w->exposed && w->processes)) {
  859. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  860. bytes_sent));
  861. // We multiply by 0.008, because we read bytes, but we display bits
  862. write_chart_dimension(w->name, ((value)*8)/1000);
  863. }
  864. }
  865. write_end_chart();
  866. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_RECV);
  867. for (w = root; w; w = w->next) {
  868. if (unlikely(w->exposed && w->processes)) {
  869. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  870. bytes_received));
  871. // We multiply by 0.008, because we read bytes, but we display bits
  872. write_chart_dimension(w->name, ((value)*8)/1000);
  873. }
  874. }
  875. write_end_chart();
  876. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS);
  877. for (w = root; w; w = w->next) {
  878. if (unlikely(w->exposed && w->processes)) {
  879. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  880. call_tcp_sent));
  881. write_chart_dimension(w->name, value);
  882. }
  883. }
  884. write_end_chart();
  885. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS);
  886. for (w = root; w; w = w->next) {
  887. if (unlikely(w->exposed && w->processes)) {
  888. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  889. call_tcp_received));
  890. write_chart_dimension(w->name, value);
  891. }
  892. }
  893. write_end_chart();
  894. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT);
  895. for (w = root; w; w = w->next) {
  896. if (unlikely(w->exposed && w->processes)) {
  897. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  898. retransmit));
  899. write_chart_dimension(w->name, value);
  900. }
  901. }
  902. write_end_chart();
  903. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS);
  904. for (w = root; w; w = w->next) {
  905. if (unlikely(w->exposed && w->processes)) {
  906. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  907. call_udp_sent));
  908. write_chart_dimension(w->name, value);
  909. }
  910. }
  911. write_end_chart();
  912. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS);
  913. for (w = root; w; w = w->next) {
  914. if (unlikely(w->exposed && w->processes)) {
  915. value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
  916. call_udp_received));
  917. write_chart_dimension(w->name, value);
  918. }
  919. }
  920. write_end_chart();
  921. }
  922. /*****************************************************************
  923. *
  924. * FUNCTIONS TO CREATE CHARTS
  925. *
  926. *****************************************************************/
  927. /**
  928. * Create global charts
  929. *
  930. * Call ebpf_create_chart to create the charts for the collector.
  931. *
  932. * @param em a pointer to the structure with the default values.
  933. */
  934. static void ebpf_create_global_charts(ebpf_module_t *em)
  935. {
  936. int order = 21070;
  937. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY,
  938. NETDATA_INBOUND_CONNECTIONS,
  939. "Inbound connections.",
  940. EBPF_COMMON_DIMENSION_CONNECTIONS,
  941. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  942. NULL,
  943. NETDATA_EBPF_CHART_TYPE_LINE,
  944. order++,
  945. ebpf_create_global_dimension,
  946. &socket_publish_aggregated[NETDATA_IDX_INCOMING_CONNECTION_TCP],
  947. 2, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  948. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY,
  949. NETDATA_TCP_OUTBOUND_CONNECTIONS,
  950. "TCP outbound connections.",
  951. EBPF_COMMON_DIMENSION_CONNECTIONS,
  952. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  953. NULL,
  954. NETDATA_EBPF_CHART_TYPE_LINE,
  955. order++,
  956. ebpf_create_global_dimension,
  957. &socket_publish_aggregated[NETDATA_IDX_TCP_CONNECTION_V4],
  958. 2, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  959. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY,
  960. NETDATA_TCP_FUNCTION_COUNT,
  961. "Calls to internal functions",
  962. EBPF_COMMON_DIMENSION_CALL,
  963. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  964. NULL,
  965. NETDATA_EBPF_CHART_TYPE_LINE,
  966. order++,
  967. ebpf_create_global_dimension,
  968. socket_publish_aggregated,
  969. 3, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  970. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY, NETDATA_TCP_FUNCTION_BITS,
  971. "TCP bandwidth", EBPF_COMMON_DIMENSION_BITS,
  972. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  973. NULL,
  974. NETDATA_EBPF_CHART_TYPE_LINE,
  975. order++,
  976. ebpf_create_global_dimension,
  977. socket_publish_aggregated,
  978. 2, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  979. if (em->mode < MODE_ENTRY) {
  980. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY,
  981. NETDATA_TCP_FUNCTION_ERROR,
  982. "TCP errors",
  983. EBPF_COMMON_DIMENSION_CALL,
  984. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  985. NULL,
  986. NETDATA_EBPF_CHART_TYPE_LINE,
  987. order++,
  988. ebpf_create_global_dimension,
  989. socket_publish_aggregated,
  990. 2, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  991. }
  992. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY,
  993. NETDATA_TCP_RETRANSMIT,
  994. "Packages retransmitted",
  995. EBPF_COMMON_DIMENSION_CALL,
  996. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  997. NULL,
  998. NETDATA_EBPF_CHART_TYPE_LINE,
  999. order++,
  1000. ebpf_create_global_dimension,
  1001. &socket_publish_aggregated[NETDATA_IDX_TCP_RETRANSMIT],
  1002. 1, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1003. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY,
  1004. NETDATA_UDP_FUNCTION_COUNT,
  1005. "UDP calls",
  1006. EBPF_COMMON_DIMENSION_CALL,
  1007. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  1008. NULL,
  1009. NETDATA_EBPF_CHART_TYPE_LINE,
  1010. order++,
  1011. ebpf_create_global_dimension,
  1012. &socket_publish_aggregated[NETDATA_IDX_UDP_RECVBUF],
  1013. 2, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1014. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY, NETDATA_UDP_FUNCTION_BITS,
  1015. "UDP bandwidth", EBPF_COMMON_DIMENSION_BITS,
  1016. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  1017. NULL,
  1018. NETDATA_EBPF_CHART_TYPE_LINE,
  1019. order++,
  1020. ebpf_create_global_dimension,
  1021. &socket_publish_aggregated[NETDATA_IDX_UDP_RECVBUF],
  1022. 2, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1023. if (em->mode < MODE_ENTRY) {
  1024. ebpf_create_chart(NETDATA_EBPF_IP_FAMILY,
  1025. NETDATA_UDP_FUNCTION_ERROR,
  1026. "UDP errors",
  1027. EBPF_COMMON_DIMENSION_CALL,
  1028. NETDATA_SOCKET_KERNEL_FUNCTIONS,
  1029. NULL,
  1030. NETDATA_EBPF_CHART_TYPE_LINE,
  1031. order++,
  1032. ebpf_create_global_dimension,
  1033. &socket_publish_aggregated[NETDATA_IDX_UDP_RECVBUF],
  1034. 2, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1035. }
  1036. }
  1037. /**
  1038. * Create apps charts
  1039. *
  1040. * Call ebpf_create_chart to create the charts on apps submenu.
  1041. *
  1042. * @param em a pointer to the structure with the default values.
  1043. * @param ptr a pointer for targets
  1044. */
  1045. void ebpf_socket_create_apps_charts(struct ebpf_module *em, void *ptr)
  1046. {
  1047. struct ebpf_target *root = ptr;
  1048. int order = 20080;
  1049. ebpf_create_charts_on_apps(NETDATA_NET_APPS_CONNECTION_TCP_V4,
  1050. "Calls to tcp_v4_connection", EBPF_COMMON_DIMENSION_CONNECTIONS,
  1051. NETDATA_APPS_NET_GROUP,
  1052. NETDATA_EBPF_CHART_TYPE_STACKED,
  1053. order++,
  1054. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1055. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1056. ebpf_create_charts_on_apps(NETDATA_NET_APPS_CONNECTION_TCP_V6,
  1057. "Calls to tcp_v6_connection", EBPF_COMMON_DIMENSION_CONNECTIONS,
  1058. NETDATA_APPS_NET_GROUP,
  1059. NETDATA_EBPF_CHART_TYPE_STACKED,
  1060. order++,
  1061. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1062. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1063. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_SENT,
  1064. "Bytes sent", EBPF_COMMON_DIMENSION_BITS,
  1065. NETDATA_APPS_NET_GROUP,
  1066. NETDATA_EBPF_CHART_TYPE_STACKED,
  1067. order++,
  1068. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1069. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1070. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_RECV,
  1071. "bytes received", EBPF_COMMON_DIMENSION_BITS,
  1072. NETDATA_APPS_NET_GROUP,
  1073. NETDATA_EBPF_CHART_TYPE_STACKED,
  1074. order++,
  1075. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1076. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1077. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS,
  1078. "Calls for tcp_sendmsg",
  1079. EBPF_COMMON_DIMENSION_CALL,
  1080. NETDATA_APPS_NET_GROUP,
  1081. NETDATA_EBPF_CHART_TYPE_STACKED,
  1082. order++,
  1083. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1084. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1085. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS,
  1086. "Calls for tcp_cleanup_rbuf",
  1087. EBPF_COMMON_DIMENSION_CALL,
  1088. NETDATA_APPS_NET_GROUP,
  1089. NETDATA_EBPF_CHART_TYPE_STACKED,
  1090. order++,
  1091. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1092. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1093. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT,
  1094. "Calls for tcp_retransmit",
  1095. EBPF_COMMON_DIMENSION_CALL,
  1096. NETDATA_APPS_NET_GROUP,
  1097. NETDATA_EBPF_CHART_TYPE_STACKED,
  1098. order++,
  1099. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1100. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1101. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS,
  1102. "Calls for udp_sendmsg",
  1103. EBPF_COMMON_DIMENSION_CALL,
  1104. NETDATA_APPS_NET_GROUP,
  1105. NETDATA_EBPF_CHART_TYPE_STACKED,
  1106. order++,
  1107. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1108. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1109. ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS,
  1110. "Calls for udp_recvmsg",
  1111. EBPF_COMMON_DIMENSION_CALL,
  1112. NETDATA_APPS_NET_GROUP,
  1113. NETDATA_EBPF_CHART_TYPE_STACKED,
  1114. order++,
  1115. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  1116. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  1117. em->apps_charts |= NETDATA_EBPF_APPS_FLAG_CHART_CREATED;
  1118. }
  1119. /**
  1120. * Create network viewer chart
  1121. *
  1122. * Create common charts.
  1123. *
  1124. * @param id chart id
  1125. * @param title chart title
  1126. * @param units units label
  1127. * @param family group name used to attach the chart on dashboard
  1128. * @param order chart order
  1129. * @param update_every value to overwrite the update frequency set by the server.
  1130. * @param ptr plot structure with values.
  1131. */
  1132. static void ebpf_socket_create_nv_chart(char *id, char *title, char *units,
  1133. char *family, int order, int update_every, netdata_vector_plot_t *ptr)
  1134. {
  1135. ebpf_write_chart_cmd(NETDATA_EBPF_FAMILY,
  1136. id,
  1137. title,
  1138. units,
  1139. family,
  1140. NETDATA_EBPF_CHART_TYPE_STACKED,
  1141. NULL,
  1142. order,
  1143. update_every,
  1144. NETDATA_EBPF_MODULE_NAME_SOCKET);
  1145. uint32_t i;
  1146. uint32_t end = ptr->last_plot;
  1147. netdata_socket_plot_t *w = ptr->plot;
  1148. for (i = 0; i < end; i++) {
  1149. fprintf(stdout, "DIMENSION %s '' incremental -1 1\n", w[i].dimension_sent);
  1150. fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[i].dimension_recv);
  1151. }
  1152. end = ptr->last;
  1153. fprintf(stdout, "DIMENSION %s '' incremental -1 1\n", w[end].dimension_sent);
  1154. fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[end].dimension_recv);
  1155. }
  1156. /**
  1157. * Create network viewer retransmit
  1158. *
  1159. * Create a specific chart.
  1160. *
  1161. * @param id the chart id
  1162. * @param title the chart title
  1163. * @param units the units label
  1164. * @param family the group name used to attach the chart on dashboard
  1165. * @param order the chart order
  1166. * @param update_every value to overwrite the update frequency set by the server.
  1167. * @param ptr the plot structure with values.
  1168. */
  1169. static void ebpf_socket_create_nv_retransmit(char *id, char *title, char *units,
  1170. char *family, int order, int update_every, netdata_vector_plot_t *ptr)
  1171. {
  1172. ebpf_write_chart_cmd(NETDATA_EBPF_FAMILY,
  1173. id,
  1174. title,
  1175. units,
  1176. family,
  1177. NETDATA_EBPF_CHART_TYPE_STACKED,
  1178. NULL,
  1179. order,
  1180. update_every,
  1181. NETDATA_EBPF_MODULE_NAME_SOCKET);
  1182. uint32_t i;
  1183. uint32_t end = ptr->last_plot;
  1184. netdata_socket_plot_t *w = ptr->plot;
  1185. for (i = 0; i < end; i++) {
  1186. fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[i].dimension_retransmit);
  1187. }
  1188. end = ptr->last;
  1189. fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[end].dimension_retransmit);
  1190. }
  1191. /**
  1192. * Create Network Viewer charts
  1193. *
  1194. * Recreate the charts when new sockets are created.
  1195. *
  1196. * @param ptr a pointer for inbound or outbound vectors.
  1197. * @param update_every value to overwrite the update frequency set by the server.
  1198. */
  1199. static void ebpf_socket_create_nv_charts(netdata_vector_plot_t *ptr, int update_every)
  1200. {
  1201. // We do not have new sockets, so we do not need move forward
  1202. if (ptr->max_plot == ptr->last_plot)
  1203. return;
  1204. ptr->last_plot = ptr->max_plot;
  1205. if (ptr == (netdata_vector_plot_t *)&outbound_vectors) {
  1206. ebpf_socket_create_nv_chart(NETDATA_NV_OUTBOUND_BYTES,
  1207. "Outbound connections (bytes).", EBPF_COMMON_DIMENSION_BYTES,
  1208. NETDATA_NETWORK_CONNECTIONS_GROUP,
  1209. 21080,
  1210. update_every, ptr);
  1211. ebpf_socket_create_nv_chart(NETDATA_NV_OUTBOUND_PACKETS,
  1212. "Outbound connections (packets)",
  1213. EBPF_COMMON_DIMENSION_PACKETS,
  1214. NETDATA_NETWORK_CONNECTIONS_GROUP,
  1215. 21082,
  1216. update_every, ptr);
  1217. ebpf_socket_create_nv_retransmit(NETDATA_NV_OUTBOUND_RETRANSMIT,
  1218. "Retransmitted packets",
  1219. EBPF_COMMON_DIMENSION_CALL,
  1220. NETDATA_NETWORK_CONNECTIONS_GROUP,
  1221. 21083,
  1222. update_every, ptr);
  1223. } else {
  1224. ebpf_socket_create_nv_chart(NETDATA_NV_INBOUND_BYTES,
  1225. "Inbound connections (bytes)", EBPF_COMMON_DIMENSION_BYTES,
  1226. NETDATA_NETWORK_CONNECTIONS_GROUP,
  1227. 21084,
  1228. update_every, ptr);
  1229. ebpf_socket_create_nv_chart(NETDATA_NV_INBOUND_PACKETS,
  1230. "Inbound connections (packets)",
  1231. EBPF_COMMON_DIMENSION_PACKETS,
  1232. NETDATA_NETWORK_CONNECTIONS_GROUP,
  1233. 21085,
  1234. update_every, ptr);
  1235. }
  1236. ptr->flags |= NETWORK_VIEWER_CHARTS_CREATED;
  1237. }
  1238. /*****************************************************************
  1239. *
  1240. * READ INFORMATION FROM KERNEL RING
  1241. *
  1242. *****************************************************************/
  1243. /**
  1244. * Is specific ip inside the range
  1245. *
  1246. * Check if the ip is inside a IP range previously defined
  1247. *
  1248. * @param cmp the IP to compare
  1249. * @param family the IP family
  1250. *
  1251. * @return It returns 1 if the IP is inside the range and 0 otherwise
  1252. */
  1253. static int ebpf_is_specific_ip_inside_range(union netdata_ip_t *cmp, int family)
  1254. {
  1255. if (!network_viewer_opt.excluded_ips && !network_viewer_opt.included_ips)
  1256. return 1;
  1257. uint32_t ipv4_test = htonl(cmp->addr32[0]);
  1258. ebpf_network_viewer_ip_list_t *move = network_viewer_opt.excluded_ips;
  1259. while (move) {
  1260. if (family == AF_INET) {
  1261. if (move->first.addr32[0] <= ipv4_test &&
  1262. ipv4_test <= move->last.addr32[0])
  1263. return 0;
  1264. } else {
  1265. if (memcmp(move->first.addr8, cmp->addr8, sizeof(union netdata_ip_t)) <= 0 &&
  1266. memcmp(move->last.addr8, cmp->addr8, sizeof(union netdata_ip_t)) >= 0) {
  1267. return 0;
  1268. }
  1269. }
  1270. move = move->next;
  1271. }
  1272. move = network_viewer_opt.included_ips;
  1273. while (move) {
  1274. if (family == AF_INET && move->ver == AF_INET) {
  1275. if (move->first.addr32[0] <= ipv4_test &&
  1276. move->last.addr32[0] >= ipv4_test)
  1277. return 1;
  1278. } else {
  1279. if (move->ver == AF_INET6 &&
  1280. memcmp(move->first.addr8, cmp->addr8, sizeof(union netdata_ip_t)) <= 0 &&
  1281. memcmp(move->last.addr8, cmp->addr8, sizeof(union netdata_ip_t)) >= 0) {
  1282. return 1;
  1283. }
  1284. }
  1285. move = move->next;
  1286. }
  1287. return 0;
  1288. }
  1289. /**
  1290. * Is port inside range
  1291. *
  1292. * Verify if the cmp port is inside the range [first, last].
  1293. * This function expects only the last parameter as big endian.
  1294. *
  1295. * @param cmp the value to compare
  1296. *
  1297. * @return It returns 1 when cmp is inside and 0 otherwise.
  1298. */
  1299. static int is_port_inside_range(uint16_t cmp)
  1300. {
  1301. // We do not have restrictions for ports.
  1302. if (!network_viewer_opt.excluded_port && !network_viewer_opt.included_port)
  1303. return 1;
  1304. // Test if port is excluded
  1305. ebpf_network_viewer_port_list_t *move = network_viewer_opt.excluded_port;
  1306. cmp = htons(cmp);
  1307. while (move) {
  1308. if (move->cmp_first <= cmp && cmp <= move->cmp_last)
  1309. return 0;
  1310. move = move->next;
  1311. }
  1312. // Test if the port is inside allowed range
  1313. move = network_viewer_opt.included_port;
  1314. while (move) {
  1315. if (move->cmp_first <= cmp && cmp <= move->cmp_last)
  1316. return 1;
  1317. move = move->next;
  1318. }
  1319. return 0;
  1320. }
  1321. /**
  1322. * Hostname matches pattern
  1323. *
  1324. * @param cmp the value to compare
  1325. *
  1326. * @return It returns 1 when the value matches and zero otherwise.
  1327. */
  1328. int hostname_matches_pattern(char *cmp)
  1329. {
  1330. if (!network_viewer_opt.included_hostnames && !network_viewer_opt.excluded_hostnames)
  1331. return 1;
  1332. ebpf_network_viewer_hostname_list_t *move = network_viewer_opt.excluded_hostnames;
  1333. while (move) {
  1334. if (simple_pattern_matches(move->value_pattern, cmp))
  1335. return 0;
  1336. move = move->next;
  1337. }
  1338. move = network_viewer_opt.included_hostnames;
  1339. while (move) {
  1340. if (simple_pattern_matches(move->value_pattern, cmp))
  1341. return 1;
  1342. move = move->next;
  1343. }
  1344. return 0;
  1345. }
  1346. /**
  1347. * Is socket allowed?
  1348. *
  1349. * Compare destination addresses and destination ports to define next steps
  1350. *
  1351. * @param key the socket read from kernel ring
  1352. * @param family the family used to compare IPs (AF_INET and AF_INET6)
  1353. *
  1354. * @return It returns 1 if this socket is inside the ranges and 0 otherwise.
  1355. */
  1356. int is_socket_allowed(netdata_socket_idx_t *key, int family)
  1357. {
  1358. if (!is_port_inside_range(key->dport))
  1359. return 0;
  1360. return ebpf_is_specific_ip_inside_range(&key->daddr, family);
  1361. }
  1362. /**
  1363. * Compare sockets
  1364. *
  1365. * Compare destination address and destination port.
  1366. * We do not compare source port, because it is random.
  1367. * We also do not compare source address, because inbound and outbound connections are stored in separated AVL trees.
  1368. *
  1369. * @param a pointer to netdata_socket_plot
  1370. * @param b pointer to netdata_socket_plot
  1371. *
  1372. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  1373. */
  1374. static int ebpf_compare_sockets(void *a, void *b)
  1375. {
  1376. struct netdata_socket_plot *val1 = a;
  1377. struct netdata_socket_plot *val2 = b;
  1378. int cmp = 0;
  1379. // We do not need to compare val2 family, because data inside hash table is always from the same family
  1380. if (val1->family == AF_INET) { //IPV4
  1381. if (network_viewer_opt.included_port || network_viewer_opt.excluded_port)
  1382. cmp = memcmp(&val1->index.dport, &val2->index.dport, sizeof(uint16_t));
  1383. if (!cmp) {
  1384. cmp = memcmp(&val1->index.daddr.addr32[0], &val2->index.daddr.addr32[0], sizeof(uint32_t));
  1385. }
  1386. } else {
  1387. if (network_viewer_opt.included_port || network_viewer_opt.excluded_port)
  1388. cmp = memcmp(&val1->index.dport, &val2->index.dport, sizeof(uint16_t));
  1389. if (!cmp) {
  1390. cmp = memcmp(&val1->index.daddr.addr32, &val2->index.daddr.addr32, 4*sizeof(uint32_t));
  1391. }
  1392. }
  1393. return cmp;
  1394. }
  1395. /**
  1396. * Build dimension name
  1397. *
  1398. * Fill dimension name vector with values given
  1399. *
  1400. * @param dimname the output vector
  1401. * @param hostname the hostname for the socket.
  1402. * @param service_name the service used to connect.
  1403. * @param proto the protocol used in this connection
  1404. * @param family is this IPV4(AF_INET) or IPV6(AF_INET6)
  1405. *
  1406. * @return it returns the size of the data copied on success and -1 otherwise.
  1407. */
  1408. static inline int ebpf_build_outbound_dimension_name(char *dimname, char *hostname, char *service_name,
  1409. char *proto, int family)
  1410. {
  1411. if (network_viewer_opt.included_port || network_viewer_opt.excluded_port)
  1412. return snprintf(dimname, CONFIG_MAX_NAME - 7, (family == AF_INET)?"%s:%s:%s_":"%s:%s:[%s]_",
  1413. service_name, proto, hostname);
  1414. return snprintf(dimname, CONFIG_MAX_NAME - 7, (family == AF_INET)?"%s:%s_":"%s:[%s]_",
  1415. proto, hostname);
  1416. }
  1417. /**
  1418. * Fill inbound dimension name
  1419. *
  1420. * Mount the dimension name with the input given
  1421. *
  1422. * @param dimname the output vector
  1423. * @param service_name the service used to connect.
  1424. * @param proto the protocol used in this connection
  1425. *
  1426. * @return it returns the size of the data copied on success and -1 otherwise.
  1427. */
  1428. static inline int build_inbound_dimension_name(char *dimname, char *service_name, char *proto)
  1429. {
  1430. return snprintf(dimname, CONFIG_MAX_NAME - 7, "%s:%s_", service_name,
  1431. proto);
  1432. }
  1433. /**
  1434. * Fill Resolved Name
  1435. *
  1436. * Fill the resolved name structure with the value given.
  1437. * The hostname is the largest value possible, if it is necessary to cut some value, it must be cut.
  1438. *
  1439. * @param ptr the output vector
  1440. * @param hostname the hostname resolved or IP.
  1441. * @param length the length for the hostname.
  1442. * @param service_name the service name associated to the connection
  1443. * @param is_outbound the is this an outbound connection
  1444. */
  1445. static inline void fill_resolved_name(netdata_socket_plot_t *ptr, char *hostname, size_t length,
  1446. char *service_name, int is_outbound)
  1447. {
  1448. if (length < NETDATA_MAX_NETWORK_COMBINED_LENGTH)
  1449. ptr->resolved_name = strdupz(hostname);
  1450. else {
  1451. length = NETDATA_MAX_NETWORK_COMBINED_LENGTH;
  1452. ptr->resolved_name = mallocz( NETDATA_MAX_NETWORK_COMBINED_LENGTH + 1);
  1453. memcpy(ptr->resolved_name, hostname, length);
  1454. ptr->resolved_name[length] = '\0';
  1455. }
  1456. char dimname[CONFIG_MAX_NAME];
  1457. int size;
  1458. char *protocol;
  1459. if (ptr->sock.protocol == IPPROTO_UDP) {
  1460. protocol = "UDP";
  1461. } else if (ptr->sock.protocol == IPPROTO_TCP) {
  1462. protocol = "TCP";
  1463. } else {
  1464. protocol = "ALL";
  1465. }
  1466. if (is_outbound)
  1467. size = ebpf_build_outbound_dimension_name(dimname, hostname, service_name, protocol, ptr->family);
  1468. else
  1469. size = build_inbound_dimension_name(dimname,service_name, protocol);
  1470. if (size > 0) {
  1471. strcpy(&dimname[size], "sent");
  1472. dimname[size + 4] = '\0';
  1473. ptr->dimension_sent = strdupz(dimname);
  1474. strcpy(&dimname[size], "recv");
  1475. ptr->dimension_recv = strdupz(dimname);
  1476. dimname[size - 1] = '\0';
  1477. ptr->dimension_retransmit = strdupz(dimname);
  1478. }
  1479. }
  1480. /**
  1481. * Mount dimension names
  1482. *
  1483. * Fill the vector names after to resolve the addresses
  1484. *
  1485. * @param ptr a pointer to the structure where the values are stored.
  1486. * @param is_outbound is a outbound ptr value?
  1487. *
  1488. * @return It returns 1 if the name is valid and 0 otherwise.
  1489. */
  1490. int fill_names(netdata_socket_plot_t *ptr, int is_outbound)
  1491. {
  1492. char hostname[NI_MAXHOST], service_name[NI_MAXSERV];
  1493. if (ptr->resolved)
  1494. return 1;
  1495. int ret;
  1496. static int resolve_name = -1;
  1497. static int resolve_service = -1;
  1498. if (resolve_name == -1)
  1499. resolve_name = network_viewer_opt.hostname_resolution_enabled;
  1500. if (resolve_service == -1)
  1501. resolve_service = network_viewer_opt.service_resolution_enabled;
  1502. netdata_socket_idx_t *idx = &ptr->index;
  1503. char *errname = { "Not resolved" };
  1504. // Resolve Name
  1505. if (ptr->family == AF_INET) { //IPV4
  1506. struct sockaddr_in myaddr;
  1507. memset(&myaddr, 0 , sizeof(myaddr));
  1508. myaddr.sin_family = ptr->family;
  1509. if (is_outbound) {
  1510. myaddr.sin_port = idx->dport;
  1511. myaddr.sin_addr.s_addr = idx->daddr.addr32[0];
  1512. } else {
  1513. myaddr.sin_port = idx->sport;
  1514. myaddr.sin_addr.s_addr = idx->saddr.addr32[0];
  1515. }
  1516. ret = (!resolve_name)?-1:getnameinfo((struct sockaddr *)&myaddr, sizeof(myaddr), hostname,
  1517. sizeof(hostname), service_name, sizeof(service_name), NI_NAMEREQD);
  1518. if (!ret && !resolve_service) {
  1519. snprintf(service_name, sizeof(service_name), "%u", ntohs(myaddr.sin_port));
  1520. }
  1521. if (ret) {
  1522. // I cannot resolve the name, I will use the IP
  1523. if (!inet_ntop(AF_INET, &myaddr.sin_addr.s_addr, hostname, NI_MAXHOST)) {
  1524. strncpy(hostname, errname, 13);
  1525. }
  1526. snprintf(service_name, sizeof(service_name), "%u", ntohs(myaddr.sin_port));
  1527. ret = 1;
  1528. }
  1529. } else { // IPV6
  1530. struct sockaddr_in6 myaddr6;
  1531. memset(&myaddr6, 0 , sizeof(myaddr6));
  1532. myaddr6.sin6_family = AF_INET6;
  1533. if (is_outbound) {
  1534. myaddr6.sin6_port = idx->dport;
  1535. memcpy(myaddr6.sin6_addr.s6_addr, idx->daddr.addr8, sizeof(union netdata_ip_t));
  1536. } else {
  1537. myaddr6.sin6_port = idx->sport;
  1538. memcpy(myaddr6.sin6_addr.s6_addr, idx->saddr.addr8, sizeof(union netdata_ip_t));
  1539. }
  1540. ret = (!resolve_name)?-1:getnameinfo((struct sockaddr *)&myaddr6, sizeof(myaddr6), hostname,
  1541. sizeof(hostname), service_name, sizeof(service_name), NI_NAMEREQD);
  1542. if (!ret && !resolve_service) {
  1543. snprintf(service_name, sizeof(service_name), "%u", ntohs(myaddr6.sin6_port));
  1544. }
  1545. if (ret) {
  1546. // I cannot resolve the name, I will use the IP
  1547. if (!inet_ntop(AF_INET6, myaddr6.sin6_addr.s6_addr, hostname, NI_MAXHOST)) {
  1548. strncpy(hostname, errname, 13);
  1549. }
  1550. snprintf(service_name, sizeof(service_name), "%u", ntohs(myaddr6.sin6_port));
  1551. ret = 1;
  1552. }
  1553. }
  1554. fill_resolved_name(ptr, hostname,
  1555. strlen(hostname) + strlen(service_name)+ NETDATA_DOTS_PROTOCOL_COMBINED_LENGTH,
  1556. service_name, is_outbound);
  1557. if (resolve_name && !ret)
  1558. ret = hostname_matches_pattern(hostname);
  1559. ptr->resolved++;
  1560. return ret;
  1561. }
  1562. /**
  1563. * Fill last Network Viewer Dimension
  1564. *
  1565. * Fill the unique dimension that is always plotted.
  1566. *
  1567. * @param ptr the pointer for the last dimension
  1568. * @param is_outbound is this an inbound structure?
  1569. */
  1570. static void fill_last_nv_dimension(netdata_socket_plot_t *ptr, int is_outbound)
  1571. {
  1572. char hostname[NI_MAXHOST], service_name[NI_MAXSERV];
  1573. char *other = { "other" };
  1574. // We are also copying the NULL bytes to avoid warnings in new compilers
  1575. strncpy(hostname, other, 6);
  1576. strncpy(service_name, other, 6);
  1577. ptr->family = AF_INET;
  1578. ptr->sock.protocol = 255;
  1579. ptr->flags = (!is_outbound)?NETDATA_INBOUND_DIRECTION:NETDATA_OUTBOUND_DIRECTION;
  1580. fill_resolved_name(ptr, hostname, 10 + NETDATA_DOTS_PROTOCOL_COMBINED_LENGTH, service_name, is_outbound);
  1581. #ifdef NETDATA_INTERNAL_CHECKS
  1582. info("Last %s dimension added: ID = %u, IP = OTHER, NAME = %s, DIM1 = %s, DIM2 = %s, DIM3 = %s",
  1583. (is_outbound)?"outbound":"inbound", network_viewer_opt.max_dim - 1, ptr->resolved_name,
  1584. ptr->dimension_recv, ptr->dimension_sent, ptr->dimension_retransmit);
  1585. #endif
  1586. }
  1587. /**
  1588. * Update Socket Data
  1589. *
  1590. * Update the socket information with last collected data
  1591. *
  1592. * @param sock
  1593. * @param lvalues
  1594. */
  1595. static inline void update_socket_data(netdata_socket_t *sock, netdata_socket_t *lvalues)
  1596. {
  1597. sock->recv_packets = lvalues->recv_packets;
  1598. sock->sent_packets = lvalues->sent_packets;
  1599. sock->recv_bytes = lvalues->recv_bytes;
  1600. sock->sent_bytes = lvalues->sent_bytes;
  1601. sock->retransmit = lvalues->retransmit;
  1602. sock->ct = lvalues->ct;
  1603. }
  1604. /**
  1605. * Store socket inside avl
  1606. *
  1607. * Store the socket values inside the avl tree.
  1608. *
  1609. * @param out the structure with information used to plot charts.
  1610. * @param lvalues Values read from socket ring.
  1611. * @param lindex the index information, the real socket.
  1612. * @param family the family associated to the socket
  1613. * @param flags the connection flags
  1614. */
  1615. static void store_socket_inside_avl(netdata_vector_plot_t *out, netdata_socket_t *lvalues,
  1616. netdata_socket_idx_t *lindex, int family, uint32_t flags)
  1617. {
  1618. netdata_socket_plot_t test, *ret ;
  1619. memcpy(&test.index, lindex, sizeof(netdata_socket_idx_t));
  1620. test.flags = flags;
  1621. ret = (netdata_socket_plot_t *) avl_search_lock(&out->tree, (avl_t *)&test);
  1622. if (ret) {
  1623. if (lvalues->ct != ret->plot.last_time) {
  1624. update_socket_data(&ret->sock, lvalues);
  1625. }
  1626. } else {
  1627. uint32_t curr = out->next;
  1628. uint32_t last = out->last;
  1629. netdata_socket_plot_t *w = &out->plot[curr];
  1630. int resolved;
  1631. if (curr == last) {
  1632. if (lvalues->ct != w->plot.last_time) {
  1633. update_socket_data(&w->sock, lvalues);
  1634. }
  1635. return;
  1636. } else {
  1637. memcpy(&w->sock, lvalues, sizeof(netdata_socket_t));
  1638. memcpy(&w->index, lindex, sizeof(netdata_socket_idx_t));
  1639. w->family = family;
  1640. resolved = fill_names(w, out != (netdata_vector_plot_t *)&inbound_vectors);
  1641. }
  1642. if (!resolved) {
  1643. freez(w->resolved_name);
  1644. freez(w->dimension_sent);
  1645. freez(w->dimension_recv);
  1646. freez(w->dimension_retransmit);
  1647. memset(w, 0, sizeof(netdata_socket_plot_t));
  1648. return;
  1649. }
  1650. w->flags = flags;
  1651. netdata_socket_plot_t *check ;
  1652. check = (netdata_socket_plot_t *) avl_insert_lock(&out->tree, (avl_t *)w);
  1653. if (check != w)
  1654. error("Internal error, cannot insert the AVL tree.");
  1655. #ifdef NETDATA_INTERNAL_CHECKS
  1656. char iptext[INET6_ADDRSTRLEN];
  1657. if (inet_ntop(family, &w->index.daddr.addr8, iptext, sizeof(iptext)))
  1658. info("New %s dimension added: ID = %u, IP = %s, NAME = %s, DIM1 = %s, DIM2 = %s, DIM3 = %s",
  1659. (out == &inbound_vectors)?"inbound":"outbound", curr, iptext, w->resolved_name,
  1660. w->dimension_recv, w->dimension_sent, w->dimension_retransmit);
  1661. #endif
  1662. curr++;
  1663. if (curr > last)
  1664. curr = last;
  1665. out->next = curr;
  1666. }
  1667. }
  1668. /**
  1669. * Compare Vector to store
  1670. *
  1671. * Compare input values with local address to select table to store.
  1672. *
  1673. * @param direction store inbound and outbound direction.
  1674. * @param cmp index read from hash table.
  1675. * @param proto the protocol read.
  1676. *
  1677. * @return It returns the structure with address to compare.
  1678. */
  1679. netdata_vector_plot_t * select_vector_to_store(uint32_t *direction, netdata_socket_idx_t *cmp, uint8_t proto)
  1680. {
  1681. if (!listen_ports) {
  1682. *direction = NETDATA_OUTBOUND_DIRECTION;
  1683. return &outbound_vectors;
  1684. }
  1685. ebpf_network_viewer_port_list_t *move_ports = listen_ports;
  1686. while (move_ports) {
  1687. if (move_ports->protocol == proto && move_ports->first == cmp->sport) {
  1688. *direction = NETDATA_INBOUND_DIRECTION;
  1689. return &inbound_vectors;
  1690. }
  1691. move_ports = move_ports->next;
  1692. }
  1693. *direction = NETDATA_OUTBOUND_DIRECTION;
  1694. return &outbound_vectors;
  1695. }
  1696. /**
  1697. * Hash accumulator
  1698. *
  1699. * @param values the values used to calculate the data.
  1700. * @param key the key to store data.
  1701. * @param family the connection family
  1702. * @param end the values size.
  1703. */
  1704. static void hash_accumulator(netdata_socket_t *values, netdata_socket_idx_t *key, int family, int end)
  1705. {
  1706. if (!network_viewer_opt.enabled || !is_socket_allowed(key, family))
  1707. return;
  1708. uint64_t bsent = 0, brecv = 0, psent = 0, precv = 0;
  1709. uint16_t retransmit = 0;
  1710. int i;
  1711. uint8_t protocol = values[0].protocol;
  1712. uint64_t ct = values[0].ct;
  1713. for (i = 1; i < end; i++) {
  1714. netdata_socket_t *w = &values[i];
  1715. precv += w->recv_packets;
  1716. psent += w->sent_packets;
  1717. brecv += w->recv_bytes;
  1718. bsent += w->sent_bytes;
  1719. retransmit += w->retransmit;
  1720. if (!protocol)
  1721. protocol = w->protocol;
  1722. if (w->ct != ct)
  1723. ct = w->ct;
  1724. }
  1725. values[0].recv_packets += precv;
  1726. values[0].sent_packets += psent;
  1727. values[0].recv_bytes += brecv;
  1728. values[0].sent_bytes += bsent;
  1729. values[0].retransmit += retransmit;
  1730. values[0].protocol = (!protocol)?IPPROTO_TCP:protocol;
  1731. values[0].ct = ct;
  1732. uint32_t dir;
  1733. netdata_vector_plot_t *table = select_vector_to_store(&dir, key, protocol);
  1734. store_socket_inside_avl(table, &values[0], key, family, dir);
  1735. }
  1736. /**
  1737. * Read socket hash table
  1738. *
  1739. * Read data from hash tables created on kernel ring.
  1740. *
  1741. * @param fd the hash table with data.
  1742. * @param family the family associated to the hash table
  1743. *
  1744. * @return it returns 0 on success and -1 otherwise.
  1745. */
  1746. static void ebpf_read_socket_hash_table(int fd, int family)
  1747. {
  1748. netdata_socket_idx_t key = {};
  1749. netdata_socket_idx_t next_key = {};
  1750. netdata_socket_t *values = socket_values;
  1751. size_t length = ebpf_nprocs*sizeof(netdata_socket_t);
  1752. int test, end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  1753. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  1754. // We need to reset the values when we are working on kernel 4.15 or newer, because kernel does not create
  1755. // values for specific processor unless it is used to store data. As result of this behavior one the next socket
  1756. // can have values from the previous one.
  1757. memset(values, 0, length);
  1758. test = bpf_map_lookup_elem(fd, &key, values);
  1759. if (test < 0) {
  1760. key = next_key;
  1761. continue;
  1762. }
  1763. hash_accumulator(values, &key, family, end);
  1764. key = next_key;
  1765. }
  1766. }
  1767. /**
  1768. * Fill Network Viewer Port list
  1769. *
  1770. * Fill the structure with values read from /proc or hash table.
  1771. *
  1772. * @param out the structure where we will store data.
  1773. * @param value the ports we are listen to.
  1774. * @param proto the protocol used for this connection.
  1775. * @param in the structure with values read form different sources.
  1776. */
  1777. static inline void fill_nv_port_list(ebpf_network_viewer_port_list_t *out, uint16_t value, uint16_t proto,
  1778. netdata_passive_connection_t *in)
  1779. {
  1780. out->first = value;
  1781. out->protocol = proto;
  1782. out->pid = in->pid;
  1783. out->tgid = in->tgid;
  1784. out->connections = in->counter;
  1785. }
  1786. /**
  1787. * Update listen table
  1788. *
  1789. * Update link list when it is necessary.
  1790. *
  1791. * @param value the ports we are listen to.
  1792. * @param proto the protocol used with port connection.
  1793. * @param in the structure with values read form different sources.
  1794. */
  1795. void update_listen_table(uint16_t value, uint16_t proto, netdata_passive_connection_t *in)
  1796. {
  1797. ebpf_network_viewer_port_list_t *w;
  1798. if (likely(listen_ports)) {
  1799. ebpf_network_viewer_port_list_t *move = listen_ports, *store = listen_ports;
  1800. while (move) {
  1801. if (move->protocol == proto && move->first == value) {
  1802. move->pid = in->pid;
  1803. move->tgid = in->tgid;
  1804. move->connections = in->counter;
  1805. return;
  1806. }
  1807. store = move;
  1808. move = move->next;
  1809. }
  1810. w = callocz(1, sizeof(ebpf_network_viewer_port_list_t));
  1811. store->next = w;
  1812. } else {
  1813. w = callocz(1, sizeof(ebpf_network_viewer_port_list_t));
  1814. listen_ports = w;
  1815. }
  1816. fill_nv_port_list(w, value, proto, in);
  1817. #ifdef NETDATA_INTERNAL_CHECKS
  1818. info("The network viewer is monitoring inbound connections for port %u", ntohs(value));
  1819. #endif
  1820. }
  1821. /**
  1822. * Read listen table
  1823. *
  1824. * Read the table with all ports that we are listen on host.
  1825. */
  1826. static void read_listen_table()
  1827. {
  1828. netdata_passive_connection_idx_t key = {};
  1829. netdata_passive_connection_idx_t next_key = {};
  1830. int fd = socket_maps[NETDATA_SOCKET_LPORTS].map_fd;
  1831. netdata_passive_connection_t value = {};
  1832. while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
  1833. int test = bpf_map_lookup_elem(fd, &key, &value);
  1834. if (test < 0) {
  1835. key = next_key;
  1836. continue;
  1837. }
  1838. // The correct protocol must come from kernel
  1839. update_listen_table(key.port, key.protocol, &value);
  1840. key = next_key;
  1841. memset(&value, 0, sizeof(value));
  1842. }
  1843. if (next_key.port && value.pid) {
  1844. // The correct protocol must come from kernel
  1845. update_listen_table(next_key.port, next_key.protocol, &value);
  1846. }
  1847. }
  1848. /**
  1849. * Socket read hash
  1850. *
  1851. * This is the thread callback.
  1852. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  1853. *
  1854. * @param ptr It is a NULL value for this thread.
  1855. *
  1856. * @return It always returns NULL.
  1857. */
  1858. void *ebpf_socket_read_hash(void *ptr)
  1859. {
  1860. netdata_thread_cleanup_push(ebpf_socket_cleanup, ptr);
  1861. heartbeat_t hb;
  1862. heartbeat_init(&hb);
  1863. int fd_ipv4 = socket_maps[NETDATA_SOCKET_TABLE_IPV4].map_fd;
  1864. int fd_ipv6 = socket_maps[NETDATA_SOCKET_TABLE_IPV6].map_fd;
  1865. // This thread is cancelled from another thread
  1866. for (;;) {
  1867. (void)heartbeat_next(&hb, USEC_PER_SEC);
  1868. if (ebpf_exit_plugin)
  1869. break;
  1870. pthread_mutex_lock(&nv_mutex);
  1871. ebpf_read_socket_hash_table(fd_ipv4, AF_INET);
  1872. ebpf_read_socket_hash_table(fd_ipv6, AF_INET6);
  1873. pthread_mutex_unlock(&nv_mutex);
  1874. }
  1875. netdata_thread_cleanup_pop(1);
  1876. return NULL;
  1877. }
  1878. /**
  1879. * Read the hash table and store data to allocated vectors.
  1880. */
  1881. static void read_hash_global_tables()
  1882. {
  1883. uint64_t idx;
  1884. netdata_idx_t res[NETDATA_SOCKET_COUNTER];
  1885. netdata_idx_t *val = socket_hash_values;
  1886. int fd = socket_maps[NETDATA_SOCKET_GLOBAL].map_fd;
  1887. for (idx = 0; idx < NETDATA_SOCKET_COUNTER; idx++) {
  1888. if (!bpf_map_lookup_elem(fd, &idx, val)) {
  1889. uint64_t total = 0;
  1890. int i;
  1891. int end = ebpf_nprocs;
  1892. for (i = 0; i < end; i++)
  1893. total += val[i];
  1894. res[idx] = total;
  1895. } else {
  1896. res[idx] = 0;
  1897. }
  1898. }
  1899. socket_aggregated_data[NETDATA_IDX_TCP_SENDMSG].call = res[NETDATA_KEY_CALLS_TCP_SENDMSG];
  1900. socket_aggregated_data[NETDATA_IDX_TCP_CLEANUP_RBUF].call = res[NETDATA_KEY_CALLS_TCP_CLEANUP_RBUF];
  1901. socket_aggregated_data[NETDATA_IDX_TCP_CLOSE].call = res[NETDATA_KEY_CALLS_TCP_CLOSE];
  1902. socket_aggregated_data[NETDATA_IDX_UDP_RECVBUF].call = res[NETDATA_KEY_CALLS_UDP_RECVMSG];
  1903. socket_aggregated_data[NETDATA_IDX_UDP_SENDMSG].call = res[NETDATA_KEY_CALLS_UDP_SENDMSG];
  1904. socket_aggregated_data[NETDATA_IDX_TCP_RETRANSMIT].call = res[NETDATA_KEY_TCP_RETRANSMIT];
  1905. socket_aggregated_data[NETDATA_IDX_TCP_CONNECTION_V4].call = res[NETDATA_KEY_CALLS_TCP_CONNECT_IPV4];
  1906. socket_aggregated_data[NETDATA_IDX_TCP_CONNECTION_V6].call = res[NETDATA_KEY_CALLS_TCP_CONNECT_IPV6];
  1907. socket_aggregated_data[NETDATA_IDX_TCP_SENDMSG].ecall = res[NETDATA_KEY_ERROR_TCP_SENDMSG];
  1908. socket_aggregated_data[NETDATA_IDX_TCP_CLEANUP_RBUF].ecall = res[NETDATA_KEY_ERROR_TCP_CLEANUP_RBUF];
  1909. socket_aggregated_data[NETDATA_IDX_UDP_RECVBUF].ecall = res[NETDATA_KEY_ERROR_UDP_RECVMSG];
  1910. socket_aggregated_data[NETDATA_IDX_UDP_SENDMSG].ecall = res[NETDATA_KEY_ERROR_UDP_SENDMSG];
  1911. socket_aggregated_data[NETDATA_IDX_TCP_CONNECTION_V4].ecall = res[NETDATA_KEY_ERROR_TCP_CONNECT_IPV4];
  1912. socket_aggregated_data[NETDATA_IDX_TCP_CONNECTION_V6].ecall = res[NETDATA_KEY_ERROR_TCP_CONNECT_IPV6];
  1913. socket_aggregated_data[NETDATA_IDX_TCP_SENDMSG].bytes = res[NETDATA_KEY_BYTES_TCP_SENDMSG];
  1914. socket_aggregated_data[NETDATA_IDX_TCP_CLEANUP_RBUF].bytes = res[NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF];
  1915. socket_aggregated_data[NETDATA_IDX_UDP_RECVBUF].bytes = res[NETDATA_KEY_BYTES_UDP_RECVMSG];
  1916. socket_aggregated_data[NETDATA_IDX_UDP_SENDMSG].bytes = res[NETDATA_KEY_BYTES_UDP_SENDMSG];
  1917. }
  1918. /**
  1919. * Fill publish apps when necessary.
  1920. *
  1921. * @param current_pid the PID that I am updating
  1922. * @param eb the structure with data read from memory.
  1923. */
  1924. void ebpf_socket_fill_publish_apps(uint32_t current_pid, ebpf_bandwidth_t *eb)
  1925. {
  1926. ebpf_socket_publish_apps_t *curr = socket_bandwidth_curr[current_pid];
  1927. if (!curr) {
  1928. curr = ebpf_socket_stat_get();
  1929. socket_bandwidth_curr[current_pid] = curr;
  1930. }
  1931. curr->bytes_sent = eb->bytes_sent;
  1932. curr->bytes_received = eb->bytes_received;
  1933. curr->call_tcp_sent = eb->call_tcp_sent;
  1934. curr->call_tcp_received = eb->call_tcp_received;
  1935. curr->retransmit = eb->retransmit;
  1936. curr->call_udp_sent = eb->call_udp_sent;
  1937. curr->call_udp_received = eb->call_udp_received;
  1938. curr->call_close = eb->close;
  1939. curr->call_tcp_v4_connection = eb->tcp_v4_connection;
  1940. curr->call_tcp_v6_connection = eb->tcp_v6_connection;
  1941. }
  1942. /**
  1943. * Bandwidth accumulator.
  1944. *
  1945. * @param out the vector with the values to sum
  1946. */
  1947. void ebpf_socket_bandwidth_accumulator(ebpf_bandwidth_t *out)
  1948. {
  1949. int i, end = (running_on_kernel >= NETDATA_KERNEL_V4_15) ? ebpf_nprocs : 1;
  1950. ebpf_bandwidth_t *total = &out[0];
  1951. for (i = 1; i < end; i++) {
  1952. ebpf_bandwidth_t *move = &out[i];
  1953. total->bytes_sent += move->bytes_sent;
  1954. total->bytes_received += move->bytes_received;
  1955. total->call_tcp_sent += move->call_tcp_sent;
  1956. total->call_tcp_received += move->call_tcp_received;
  1957. total->retransmit += move->retransmit;
  1958. total->call_udp_sent += move->call_udp_sent;
  1959. total->call_udp_received += move->call_udp_received;
  1960. total->close += move->close;
  1961. total->tcp_v4_connection += move->tcp_v4_connection;
  1962. total->tcp_v6_connection += move->tcp_v6_connection;
  1963. }
  1964. }
  1965. /**
  1966. * Update the apps data reading information from the hash table
  1967. */
  1968. static void ebpf_socket_update_apps_data()
  1969. {
  1970. int fd = socket_maps[NETDATA_SOCKET_TABLE_BANDWIDTH].map_fd;
  1971. ebpf_bandwidth_t *eb = bandwidth_vector;
  1972. uint32_t key;
  1973. struct ebpf_pid_stat *pids = ebpf_root_of_pids;
  1974. while (pids) {
  1975. key = pids->pid;
  1976. if (bpf_map_lookup_elem(fd, &key, eb)) {
  1977. pids = pids->next;
  1978. continue;
  1979. }
  1980. ebpf_socket_bandwidth_accumulator(eb);
  1981. ebpf_socket_fill_publish_apps(key, eb);
  1982. pids = pids->next;
  1983. }
  1984. }
  1985. /**
  1986. * Update cgroup
  1987. *
  1988. * Update cgroup data based in
  1989. */
  1990. static void ebpf_update_socket_cgroup()
  1991. {
  1992. ebpf_cgroup_target_t *ect ;
  1993. ebpf_bandwidth_t *eb = bandwidth_vector;
  1994. int fd = socket_maps[NETDATA_SOCKET_TABLE_BANDWIDTH].map_fd;
  1995. pthread_mutex_lock(&mutex_cgroup_shm);
  1996. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  1997. struct pid_on_target2 *pids;
  1998. for (pids = ect->pids; pids; pids = pids->next) {
  1999. int pid = pids->pid;
  2000. ebpf_bandwidth_t *out = &pids->socket;
  2001. ebpf_socket_publish_apps_t *publish = &ect->publish_socket;
  2002. if (likely(socket_bandwidth_curr) && socket_bandwidth_curr[pid]) {
  2003. ebpf_socket_publish_apps_t *in = socket_bandwidth_curr[pid];
  2004. publish->bytes_sent = in->bytes_sent;
  2005. publish->bytes_received = in->bytes_received;
  2006. publish->call_tcp_sent = in->call_tcp_sent;
  2007. publish->call_tcp_received = in->call_tcp_received;
  2008. publish->retransmit = in->retransmit;
  2009. publish->call_udp_sent = in->call_udp_sent;
  2010. publish->call_udp_received = in->call_udp_received;
  2011. publish->call_close = in->call_close;
  2012. publish->call_tcp_v4_connection = in->call_tcp_v4_connection;
  2013. publish->call_tcp_v6_connection = in->call_tcp_v6_connection;
  2014. } else {
  2015. if (!bpf_map_lookup_elem(fd, &pid, eb)) {
  2016. ebpf_socket_bandwidth_accumulator(eb);
  2017. memcpy(out, eb, sizeof(ebpf_bandwidth_t));
  2018. publish->bytes_sent = out->bytes_sent;
  2019. publish->bytes_received = out->bytes_received;
  2020. publish->call_tcp_sent = out->call_tcp_sent;
  2021. publish->call_tcp_received = out->call_tcp_received;
  2022. publish->retransmit = out->retransmit;
  2023. publish->call_udp_sent = out->call_udp_sent;
  2024. publish->call_udp_received = out->call_udp_received;
  2025. publish->call_close = out->close;
  2026. publish->call_tcp_v4_connection = out->tcp_v4_connection;
  2027. publish->call_tcp_v6_connection = out->tcp_v6_connection;
  2028. }
  2029. }
  2030. }
  2031. }
  2032. pthread_mutex_unlock(&mutex_cgroup_shm);
  2033. }
  2034. /**
  2035. * Sum PIDs
  2036. *
  2037. * Sum values for all targets.
  2038. *
  2039. * @param fd structure used to store data
  2040. * @param pids input data
  2041. */
  2042. static void ebpf_socket_sum_cgroup_pids(ebpf_socket_publish_apps_t *socket, struct pid_on_target2 *pids)
  2043. {
  2044. ebpf_socket_publish_apps_t accumulator;
  2045. memset(&accumulator, 0, sizeof(accumulator));
  2046. while (pids) {
  2047. ebpf_bandwidth_t *w = &pids->socket;
  2048. accumulator.bytes_received += w->bytes_received;
  2049. accumulator.bytes_sent += w->bytes_sent;
  2050. accumulator.call_tcp_received += w->call_tcp_received;
  2051. accumulator.call_tcp_sent += w->call_tcp_sent;
  2052. accumulator.retransmit += w->retransmit;
  2053. accumulator.call_udp_received += w->call_udp_received;
  2054. accumulator.call_udp_sent += w->call_udp_sent;
  2055. accumulator.call_close += w->close;
  2056. accumulator.call_tcp_v4_connection += w->tcp_v4_connection;
  2057. accumulator.call_tcp_v6_connection += w->tcp_v6_connection;
  2058. pids = pids->next;
  2059. }
  2060. socket->bytes_sent = (accumulator.bytes_sent >= socket->bytes_sent) ? accumulator.bytes_sent : socket->bytes_sent;
  2061. socket->bytes_received = (accumulator.bytes_received >= socket->bytes_received) ? accumulator.bytes_received : socket->bytes_received;
  2062. socket->call_tcp_sent = (accumulator.call_tcp_sent >= socket->call_tcp_sent) ? accumulator.call_tcp_sent : socket->call_tcp_sent;
  2063. socket->call_tcp_received = (accumulator.call_tcp_received >= socket->call_tcp_received) ? accumulator.call_tcp_received : socket->call_tcp_received;
  2064. socket->retransmit = (accumulator.retransmit >= socket->retransmit) ? accumulator.retransmit : socket->retransmit;
  2065. socket->call_udp_sent = (accumulator.call_udp_sent >= socket->call_udp_sent) ? accumulator.call_udp_sent : socket->call_udp_sent;
  2066. socket->call_udp_received = (accumulator.call_udp_received >= socket->call_udp_received) ? accumulator.call_udp_received : socket->call_udp_received;
  2067. socket->call_close = (accumulator.call_close >= socket->call_close) ? accumulator.call_close : socket->call_close;
  2068. socket->call_tcp_v4_connection = (accumulator.call_tcp_v4_connection >= socket->call_tcp_v4_connection) ?
  2069. accumulator.call_tcp_v4_connection : socket->call_tcp_v4_connection;
  2070. socket->call_tcp_v6_connection = (accumulator.call_tcp_v6_connection >= socket->call_tcp_v6_connection) ?
  2071. accumulator.call_tcp_v6_connection : socket->call_tcp_v6_connection;
  2072. }
  2073. /**
  2074. * Create specific socket charts
  2075. *
  2076. * Create charts for cgroup/application.
  2077. *
  2078. * @param type the chart type.
  2079. * @param update_every value to overwrite the update frequency set by the server.
  2080. */
  2081. static void ebpf_create_specific_socket_charts(char *type, int update_every)
  2082. {
  2083. int order_basis = 5300;
  2084. ebpf_create_chart(type, NETDATA_NET_APPS_CONNECTION_TCP_V4,
  2085. "Calls to tcp_v4_connection",
  2086. EBPF_COMMON_DIMENSION_CONNECTIONS, NETDATA_CGROUP_NET_GROUP,
  2087. NETDATA_CGROUP_TCP_V4_CONN_CONTEXT,
  2088. NETDATA_EBPF_CHART_TYPE_LINE,
  2089. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2090. ebpf_create_global_dimension,
  2091. &socket_publish_aggregated[NETDATA_IDX_TCP_CONNECTION_V4], 1,
  2092. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2093. ebpf_create_chart(type, NETDATA_NET_APPS_CONNECTION_TCP_V6,
  2094. "Calls to tcp_v6_connection",
  2095. EBPF_COMMON_DIMENSION_CONNECTIONS, NETDATA_CGROUP_NET_GROUP,
  2096. NETDATA_CGROUP_TCP_V6_CONN_CONTEXT,
  2097. NETDATA_EBPF_CHART_TYPE_LINE,
  2098. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2099. ebpf_create_global_dimension,
  2100. &socket_publish_aggregated[NETDATA_IDX_TCP_CONNECTION_V6], 1,
  2101. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2102. ebpf_create_chart(type, NETDATA_NET_APPS_BANDWIDTH_RECV,
  2103. "Bytes received",
  2104. EBPF_COMMON_DIMENSION_CALL, NETDATA_CGROUP_NET_GROUP,
  2105. NETDATA_CGROUP_SOCKET_BYTES_RECV_CONTEXT,
  2106. NETDATA_EBPF_CHART_TYPE_LINE,
  2107. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2108. ebpf_create_global_dimension,
  2109. &socket_publish_aggregated[NETDATA_IDX_TCP_CLEANUP_RBUF], 1,
  2110. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2111. ebpf_create_chart(type, NETDATA_NET_APPS_BANDWIDTH_SENT,
  2112. "Bytes sent",
  2113. EBPF_COMMON_DIMENSION_CALL, NETDATA_CGROUP_NET_GROUP,
  2114. NETDATA_CGROUP_SOCKET_BYTES_SEND_CONTEXT,
  2115. NETDATA_EBPF_CHART_TYPE_LINE,
  2116. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2117. ebpf_create_global_dimension,
  2118. socket_publish_aggregated, 1,
  2119. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2120. ebpf_create_chart(type, NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS,
  2121. "Calls to tcp_cleanup_rbuf.",
  2122. EBPF_COMMON_DIMENSION_CALL, NETDATA_CGROUP_NET_GROUP,
  2123. NETDATA_CGROUP_SOCKET_TCP_RECV_CONTEXT,
  2124. NETDATA_EBPF_CHART_TYPE_LINE,
  2125. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2126. ebpf_create_global_dimension,
  2127. &socket_publish_aggregated[NETDATA_IDX_TCP_CLEANUP_RBUF], 1,
  2128. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2129. ebpf_create_chart(type, NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS,
  2130. "Calls to tcp_sendmsg.",
  2131. EBPF_COMMON_DIMENSION_CALL, NETDATA_CGROUP_NET_GROUP,
  2132. NETDATA_CGROUP_SOCKET_TCP_SEND_CONTEXT,
  2133. NETDATA_EBPF_CHART_TYPE_LINE,
  2134. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2135. ebpf_create_global_dimension,
  2136. socket_publish_aggregated, 1,
  2137. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2138. ebpf_create_chart(type, NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT,
  2139. "Calls to tcp_retransmit.",
  2140. EBPF_COMMON_DIMENSION_CALL, NETDATA_CGROUP_NET_GROUP,
  2141. NETDATA_CGROUP_SOCKET_TCP_RETRANSMIT_CONTEXT,
  2142. NETDATA_EBPF_CHART_TYPE_LINE,
  2143. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2144. ebpf_create_global_dimension,
  2145. &socket_publish_aggregated[NETDATA_IDX_TCP_RETRANSMIT], 1,
  2146. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2147. ebpf_create_chart(type, NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS,
  2148. "Calls to udp_sendmsg",
  2149. EBPF_COMMON_DIMENSION_CALL, NETDATA_CGROUP_NET_GROUP,
  2150. NETDATA_CGROUP_SOCKET_UDP_SEND_CONTEXT,
  2151. NETDATA_EBPF_CHART_TYPE_LINE,
  2152. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2153. ebpf_create_global_dimension,
  2154. &socket_publish_aggregated[NETDATA_IDX_UDP_SENDMSG], 1,
  2155. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2156. ebpf_create_chart(type, NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS,
  2157. "Calls to udp_recvmsg",
  2158. EBPF_COMMON_DIMENSION_CALL, NETDATA_CGROUP_NET_GROUP,
  2159. NETDATA_CGROUP_SOCKET_UDP_RECV_CONTEXT,
  2160. NETDATA_EBPF_CHART_TYPE_LINE,
  2161. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++,
  2162. ebpf_create_global_dimension,
  2163. &socket_publish_aggregated[NETDATA_IDX_UDP_RECVBUF], 1,
  2164. update_every, NETDATA_EBPF_MODULE_NAME_SOCKET);
  2165. }
  2166. /**
  2167. * Obsolete specific socket charts
  2168. *
  2169. * Obsolete charts for cgroup/application.
  2170. *
  2171. * @param type the chart type.
  2172. * @param update_every value to overwrite the update frequency set by the server.
  2173. */
  2174. static void ebpf_obsolete_specific_socket_charts(char *type, int update_every)
  2175. {
  2176. int order_basis = 5300;
  2177. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_CONNECTION_TCP_V4, "Calls to tcp_v4_connection",
  2178. EBPF_COMMON_DIMENSION_CONNECTIONS, NETDATA_APPS_NET_GROUP,
  2179. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_SERVICES_SOCKET_TCP_V4_CONN_CONTEXT,
  2180. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2181. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_CONNECTION_TCP_V6,"Calls to tcp_v6_connection",
  2182. EBPF_COMMON_DIMENSION_CONNECTIONS, NETDATA_APPS_NET_GROUP,
  2183. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_SERVICES_SOCKET_TCP_V6_CONN_CONTEXT,
  2184. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2185. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_BANDWIDTH_RECV, "Bytes received",
  2186. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_NET_GROUP,
  2187. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_SERVICES_SOCKET_BYTES_RECV_CONTEXT,
  2188. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2189. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_BANDWIDTH_SENT,"Bytes sent",
  2190. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_NET_GROUP,
  2191. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_SERVICES_SOCKET_BYTES_SEND_CONTEXT,
  2192. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2193. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS, "Calls to tcp_cleanup_rbuf.",
  2194. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_NET_GROUP,
  2195. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_SERVICES_SOCKET_TCP_RECV_CONTEXT,
  2196. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2197. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS, "Calls to tcp_sendmsg.",
  2198. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_NET_GROUP,
  2199. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_SERVICES_SOCKET_TCP_SEND_CONTEXT,
  2200. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2201. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT, "Calls to tcp_retransmit.",
  2202. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_NET_GROUP,
  2203. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_SERVICES_SOCKET_TCP_RETRANSMIT_CONTEXT,
  2204. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2205. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS, "Calls to udp_sendmsg",
  2206. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_NET_GROUP,
  2207. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_SERVICES_SOCKET_UDP_SEND_CONTEXT,
  2208. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2209. ebpf_write_chart_obsolete(type, NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS, "Calls to udp_recvmsg",
  2210. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_NET_GROUP, NETDATA_EBPF_CHART_TYPE_LINE,
  2211. NETDATA_SERVICES_SOCKET_UDP_RECV_CONTEXT,
  2212. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + order_basis++, update_every);
  2213. }
  2214. /*
  2215. * Send Specific Swap data
  2216. *
  2217. * Send data for specific cgroup/apps.
  2218. *
  2219. * @param type chart type
  2220. * @param values structure with values that will be sent to netdata
  2221. */
  2222. static void ebpf_send_specific_socket_data(char *type, ebpf_socket_publish_apps_t *values)
  2223. {
  2224. write_begin_chart(type, NETDATA_NET_APPS_CONNECTION_TCP_V4);
  2225. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_TCP_CONNECTION_V4].name,
  2226. (long long) values->call_tcp_v4_connection);
  2227. write_end_chart();
  2228. write_begin_chart(type, NETDATA_NET_APPS_CONNECTION_TCP_V6);
  2229. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_TCP_CONNECTION_V6].name,
  2230. (long long) values->call_tcp_v6_connection);
  2231. write_end_chart();
  2232. write_begin_chart(type, NETDATA_NET_APPS_BANDWIDTH_SENT);
  2233. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_TCP_SENDMSG].name,
  2234. (long long) values->bytes_sent);
  2235. write_end_chart();
  2236. write_begin_chart(type, NETDATA_NET_APPS_BANDWIDTH_RECV);
  2237. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_TCP_CLEANUP_RBUF].name,
  2238. (long long) values->bytes_received);
  2239. write_end_chart();
  2240. write_begin_chart(type, NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS);
  2241. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_TCP_SENDMSG].name,
  2242. (long long) values->call_tcp_sent);
  2243. write_end_chart();
  2244. write_begin_chart(type, NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS);
  2245. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_TCP_CLEANUP_RBUF].name,
  2246. (long long) values->call_tcp_received);
  2247. write_end_chart();
  2248. write_begin_chart(type, NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT);
  2249. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_TCP_RETRANSMIT].name,
  2250. (long long) values->retransmit);
  2251. write_end_chart();
  2252. write_begin_chart(type, NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS);
  2253. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_UDP_SENDMSG].name,
  2254. (long long) values->call_udp_sent);
  2255. write_end_chart();
  2256. write_begin_chart(type, NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS);
  2257. write_chart_dimension(socket_publish_aggregated[NETDATA_IDX_UDP_RECVBUF].name,
  2258. (long long) values->call_udp_received);
  2259. write_end_chart();
  2260. }
  2261. /**
  2262. * Create Systemd Socket Charts
  2263. *
  2264. * Create charts when systemd is enabled
  2265. *
  2266. * @param update_every value to overwrite the update frequency set by the server.
  2267. **/
  2268. static void ebpf_create_systemd_socket_charts(int update_every)
  2269. {
  2270. int order = 20080;
  2271. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_CONNECTION_TCP_V4,
  2272. "Calls to tcp_v4_connection", EBPF_COMMON_DIMENSION_CONNECTIONS,
  2273. NETDATA_APPS_NET_GROUP,
  2274. NETDATA_EBPF_CHART_TYPE_STACKED,
  2275. order++,
  2276. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2277. NETDATA_SERVICES_SOCKET_TCP_V4_CONN_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2278. update_every);
  2279. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_CONNECTION_TCP_V6,
  2280. "Calls to tcp_v6_connection", EBPF_COMMON_DIMENSION_CONNECTIONS,
  2281. NETDATA_APPS_NET_GROUP,
  2282. NETDATA_EBPF_CHART_TYPE_STACKED,
  2283. order++,
  2284. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2285. NETDATA_SERVICES_SOCKET_TCP_V6_CONN_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2286. update_every);
  2287. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_BANDWIDTH_RECV,
  2288. "Bytes received", EBPF_COMMON_DIMENSION_BITS,
  2289. NETDATA_APPS_NET_GROUP,
  2290. NETDATA_EBPF_CHART_TYPE_STACKED,
  2291. order++,
  2292. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2293. NETDATA_SERVICES_SOCKET_BYTES_RECV_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2294. update_every);
  2295. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_BANDWIDTH_SENT,
  2296. "Bytes sent", EBPF_COMMON_DIMENSION_BITS,
  2297. NETDATA_APPS_NET_GROUP,
  2298. NETDATA_EBPF_CHART_TYPE_STACKED,
  2299. order++,
  2300. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2301. NETDATA_SERVICES_SOCKET_BYTES_SEND_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2302. update_every);
  2303. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS,
  2304. "Calls to tcp_cleanup_rbuf.",
  2305. EBPF_COMMON_DIMENSION_CALL,
  2306. NETDATA_APPS_NET_GROUP,
  2307. NETDATA_EBPF_CHART_TYPE_STACKED,
  2308. order++,
  2309. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2310. NETDATA_SERVICES_SOCKET_TCP_RECV_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2311. update_every);
  2312. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS,
  2313. "Calls to tcp_sendmsg.",
  2314. EBPF_COMMON_DIMENSION_CALL,
  2315. NETDATA_APPS_NET_GROUP,
  2316. NETDATA_EBPF_CHART_TYPE_STACKED,
  2317. order++,
  2318. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2319. NETDATA_SERVICES_SOCKET_TCP_SEND_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2320. update_every);
  2321. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT,
  2322. "Calls to tcp_retransmit",
  2323. EBPF_COMMON_DIMENSION_CALL,
  2324. NETDATA_APPS_NET_GROUP,
  2325. NETDATA_EBPF_CHART_TYPE_STACKED,
  2326. order++,
  2327. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2328. NETDATA_SERVICES_SOCKET_TCP_RETRANSMIT_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2329. update_every);
  2330. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS,
  2331. "Calls to udp_sendmsg",
  2332. EBPF_COMMON_DIMENSION_CALL,
  2333. NETDATA_APPS_NET_GROUP,
  2334. NETDATA_EBPF_CHART_TYPE_STACKED,
  2335. order++,
  2336. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2337. NETDATA_SERVICES_SOCKET_UDP_SEND_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2338. update_every);
  2339. ebpf_create_charts_on_systemd(NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS,
  2340. "Calls to udp_recvmsg",
  2341. EBPF_COMMON_DIMENSION_CALL,
  2342. NETDATA_APPS_NET_GROUP,
  2343. NETDATA_EBPF_CHART_TYPE_STACKED,
  2344. order++,
  2345. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  2346. NETDATA_SERVICES_SOCKET_UDP_RECV_CONTEXT, NETDATA_EBPF_MODULE_NAME_SOCKET,
  2347. update_every);
  2348. }
  2349. /**
  2350. * Send Systemd charts
  2351. *
  2352. * Send collected data to Netdata.
  2353. */
  2354. static void ebpf_send_systemd_socket_charts()
  2355. {
  2356. ebpf_cgroup_target_t *ect;
  2357. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_CONNECTION_TCP_V4);
  2358. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2359. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2360. write_chart_dimension(ect->name, (long long)ect->publish_socket.call_tcp_v4_connection);
  2361. }
  2362. }
  2363. write_end_chart();
  2364. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_CONNECTION_TCP_V6);
  2365. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2366. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2367. write_chart_dimension(ect->name, (long long)ect->publish_socket.call_tcp_v6_connection);
  2368. }
  2369. }
  2370. write_end_chart();
  2371. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_BANDWIDTH_SENT);
  2372. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2373. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2374. write_chart_dimension(ect->name, (long long)ect->publish_socket.bytes_sent);
  2375. }
  2376. }
  2377. write_end_chart();
  2378. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_BANDWIDTH_RECV);
  2379. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2380. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2381. write_chart_dimension(ect->name, (long long)ect->publish_socket.bytes_received);
  2382. }
  2383. }
  2384. write_end_chart();
  2385. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS);
  2386. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2387. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2388. write_chart_dimension(ect->name, (long long)ect->publish_socket.call_tcp_sent);
  2389. }
  2390. }
  2391. write_end_chart();
  2392. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS);
  2393. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2394. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2395. write_chart_dimension(ect->name, (long long)ect->publish_socket.call_tcp_received);
  2396. }
  2397. }
  2398. write_end_chart();
  2399. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT);
  2400. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2401. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2402. write_chart_dimension(ect->name, (long long)ect->publish_socket.retransmit);
  2403. }
  2404. }
  2405. write_end_chart();
  2406. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS);
  2407. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2408. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2409. write_chart_dimension(ect->name, (long long)ect->publish_socket.call_udp_sent);
  2410. }
  2411. }
  2412. write_end_chart();
  2413. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS);
  2414. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2415. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  2416. write_chart_dimension(ect->name, (long long)ect->publish_socket.call_udp_received);
  2417. }
  2418. }
  2419. write_end_chart();
  2420. }
  2421. /**
  2422. * Update Cgroup algorithm
  2423. *
  2424. * Change algorithm from absolute to incremental
  2425. */
  2426. void ebpf_socket_update_cgroup_algorithm()
  2427. {
  2428. int i;
  2429. for (i = 0; i < NETDATA_MAX_SOCKET_VECTOR; i++) {
  2430. netdata_publish_syscall_t *ptr = &socket_publish_aggregated[i];
  2431. ptr->algorithm = ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX];
  2432. }
  2433. }
  2434. /**
  2435. * Send data to Netdata calling auxiliary functions.
  2436. *
  2437. * @param update_every value to overwrite the update frequency set by the server.
  2438. */
  2439. static void ebpf_socket_send_cgroup_data(int update_every)
  2440. {
  2441. if (!ebpf_cgroup_pids)
  2442. return;
  2443. pthread_mutex_lock(&mutex_cgroup_shm);
  2444. ebpf_cgroup_target_t *ect;
  2445. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2446. ebpf_socket_sum_cgroup_pids(&ect->publish_socket, ect->pids);
  2447. }
  2448. int has_systemd = shm_ebpf_cgroup.header->systemd_enabled;
  2449. if (has_systemd) {
  2450. if (send_cgroup_chart) {
  2451. ebpf_create_systemd_socket_charts(update_every);
  2452. }
  2453. ebpf_send_systemd_socket_charts();
  2454. }
  2455. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  2456. if (ect->systemd)
  2457. continue;
  2458. if (!(ect->flags & NETDATA_EBPF_CGROUP_HAS_SOCKET_CHART)) {
  2459. ebpf_create_specific_socket_charts(ect->name, update_every);
  2460. ect->flags |= NETDATA_EBPF_CGROUP_HAS_SOCKET_CHART;
  2461. }
  2462. if (ect->flags & NETDATA_EBPF_CGROUP_HAS_SOCKET_CHART && ect->updated) {
  2463. ebpf_send_specific_socket_data(ect->name, &ect->publish_socket);
  2464. } else {
  2465. ebpf_obsolete_specific_socket_charts(ect->name, update_every);
  2466. ect->flags &= ~NETDATA_EBPF_CGROUP_HAS_SOCKET_CHART;
  2467. }
  2468. }
  2469. pthread_mutex_unlock(&mutex_cgroup_shm);
  2470. }
  2471. /*****************************************************************
  2472. *
  2473. * FUNCTIONS WITH THE MAIN LOOP
  2474. *
  2475. *****************************************************************/
  2476. /**
  2477. * Main loop for this collector.
  2478. *
  2479. * @param em the structure with thread information
  2480. */
  2481. static void socket_collector(ebpf_module_t *em)
  2482. {
  2483. heartbeat_t hb;
  2484. heartbeat_init(&hb);
  2485. uint32_t network_connection = network_viewer_opt.enabled;
  2486. if (network_connection) {
  2487. socket_threads.thread = mallocz(sizeof(netdata_thread_t));
  2488. socket_threads.start_routine = ebpf_socket_read_hash;
  2489. netdata_thread_create(socket_threads.thread, socket_threads.name,
  2490. NETDATA_THREAD_OPTION_DEFAULT, ebpf_socket_read_hash, em);
  2491. }
  2492. int cgroups = em->cgroup_charts;
  2493. if (cgroups)
  2494. ebpf_socket_update_cgroup_algorithm();
  2495. int socket_global_enabled = em->global_charts;
  2496. int update_every = em->update_every;
  2497. int counter = update_every - 1;
  2498. while (!ebpf_exit_plugin) {
  2499. (void)heartbeat_next(&hb, USEC_PER_SEC);
  2500. if (ebpf_exit_plugin || ++counter != update_every)
  2501. continue;
  2502. counter = 0;
  2503. netdata_apps_integration_flags_t socket_apps_enabled = em->apps_charts;
  2504. if (socket_global_enabled) {
  2505. read_listen_table();
  2506. read_hash_global_tables();
  2507. }
  2508. pthread_mutex_lock(&collect_data_mutex);
  2509. if (socket_apps_enabled)
  2510. ebpf_socket_update_apps_data();
  2511. if (cgroups)
  2512. ebpf_update_socket_cgroup();
  2513. if (network_connection)
  2514. calculate_nv_plot();
  2515. pthread_mutex_lock(&lock);
  2516. if (socket_global_enabled)
  2517. ebpf_socket_send_data(em);
  2518. if (socket_apps_enabled & NETDATA_EBPF_APPS_FLAG_CHART_CREATED)
  2519. ebpf_socket_send_apps_data(em, apps_groups_root_target);
  2520. #ifdef NETDATA_DEV_MODE
  2521. if (ebpf_aral_socket_pid)
  2522. ebpf_send_data_aral_chart(ebpf_aral_socket_pid, em);
  2523. #endif
  2524. if (cgroups)
  2525. ebpf_socket_send_cgroup_data(update_every);
  2526. fflush(stdout);
  2527. if (network_connection) {
  2528. // We are calling fflush many times, because when we have a lot of dimensions
  2529. // we began to have not expected outputs and Netdata closed the plugin.
  2530. pthread_mutex_lock(&nv_mutex);
  2531. ebpf_socket_create_nv_charts(&inbound_vectors, update_every);
  2532. fflush(stdout);
  2533. ebpf_socket_send_nv_data(&inbound_vectors);
  2534. ebpf_socket_create_nv_charts(&outbound_vectors, update_every);
  2535. fflush(stdout);
  2536. ebpf_socket_send_nv_data(&outbound_vectors);
  2537. pthread_mutex_unlock(&nv_mutex);
  2538. }
  2539. pthread_mutex_unlock(&lock);
  2540. pthread_mutex_unlock(&collect_data_mutex);
  2541. }
  2542. }
  2543. /*****************************************************************
  2544. *
  2545. * FUNCTIONS TO START THREAD
  2546. *
  2547. *****************************************************************/
  2548. /**
  2549. * Allocate vectors used with this thread.
  2550. * We are not testing the return, because callocz does this and shutdown the software
  2551. * case it was not possible to allocate.
  2552. *
  2553. * @param apps is apps enabled?
  2554. */
  2555. static void ebpf_socket_allocate_global_vectors(int apps)
  2556. {
  2557. memset(socket_aggregated_data, 0 ,NETDATA_MAX_SOCKET_VECTOR * sizeof(netdata_syscall_stat_t));
  2558. memset(socket_publish_aggregated, 0 ,NETDATA_MAX_SOCKET_VECTOR * sizeof(netdata_publish_syscall_t));
  2559. socket_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  2560. if (apps) {
  2561. ebpf_socket_aral_init();
  2562. socket_bandwidth_curr = callocz((size_t)pid_max, sizeof(ebpf_socket_publish_apps_t *));
  2563. bandwidth_vector = callocz((size_t)ebpf_nprocs, sizeof(ebpf_bandwidth_t));
  2564. }
  2565. socket_values = callocz((size_t)ebpf_nprocs, sizeof(netdata_socket_t));
  2566. if (network_viewer_opt.enabled) {
  2567. inbound_vectors.plot = callocz(network_viewer_opt.max_dim, sizeof(netdata_socket_plot_t));
  2568. outbound_vectors.plot = callocz(network_viewer_opt.max_dim, sizeof(netdata_socket_plot_t));
  2569. }
  2570. }
  2571. /**
  2572. * Initialize Inbound and Outbound
  2573. *
  2574. * Initialize the common outbound and inbound sockets.
  2575. */
  2576. static void initialize_inbound_outbound()
  2577. {
  2578. inbound_vectors.last = network_viewer_opt.max_dim - 1;
  2579. outbound_vectors.last = inbound_vectors.last;
  2580. fill_last_nv_dimension(&inbound_vectors.plot[inbound_vectors.last], 0);
  2581. fill_last_nv_dimension(&outbound_vectors.plot[outbound_vectors.last], 1);
  2582. }
  2583. /*****************************************************************
  2584. *
  2585. * EBPF SOCKET THREAD
  2586. *
  2587. *****************************************************************/
  2588. /**
  2589. * Fill Port list
  2590. *
  2591. * @param out a pointer to the link list.
  2592. * @param in the structure that will be linked.
  2593. */
  2594. static inline void fill_port_list(ebpf_network_viewer_port_list_t **out, ebpf_network_viewer_port_list_t *in)
  2595. {
  2596. if (likely(*out)) {
  2597. ebpf_network_viewer_port_list_t *move = *out, *store = *out;
  2598. uint16_t first = ntohs(in->first);
  2599. uint16_t last = ntohs(in->last);
  2600. while (move) {
  2601. uint16_t cmp_first = ntohs(move->first);
  2602. uint16_t cmp_last = ntohs(move->last);
  2603. if (cmp_first <= first && first <= cmp_last &&
  2604. cmp_first <= last && last <= cmp_last ) {
  2605. info("The range/value (%u, %u) is inside the range/value (%u, %u) already inserted, it will be ignored.",
  2606. first, last, cmp_first, cmp_last);
  2607. freez(in->value);
  2608. freez(in);
  2609. return;
  2610. } else if (first <= cmp_first && cmp_first <= last &&
  2611. first <= cmp_last && cmp_last <= last) {
  2612. info("The range (%u, %u) is bigger than previous range (%u, %u) already inserted, the previous will be ignored.",
  2613. first, last, cmp_first, cmp_last);
  2614. freez(move->value);
  2615. move->value = in->value;
  2616. move->first = in->first;
  2617. move->last = in->last;
  2618. freez(in);
  2619. return;
  2620. }
  2621. store = move;
  2622. move = move->next;
  2623. }
  2624. store->next = in;
  2625. } else {
  2626. *out = in;
  2627. }
  2628. #ifdef NETDATA_INTERNAL_CHECKS
  2629. info("Adding values %s( %u, %u) to %s port list used on network viewer",
  2630. in->value, ntohs(in->first), ntohs(in->last),
  2631. (*out == network_viewer_opt.included_port)?"included":"excluded");
  2632. #endif
  2633. }
  2634. /**
  2635. * Parse Service List
  2636. *
  2637. * @param out a pointer to store the link list
  2638. * @param service the service used to create the structure that will be linked.
  2639. */
  2640. static void parse_service_list(void **out, char *service)
  2641. {
  2642. ebpf_network_viewer_port_list_t **list = (ebpf_network_viewer_port_list_t **)out;
  2643. struct servent *serv = getservbyname((const char *)service, "tcp");
  2644. if (!serv)
  2645. serv = getservbyname((const char *)service, "udp");
  2646. if (!serv) {
  2647. info("Cannot resolv the service '%s' with protocols TCP and UDP, it will be ignored", service);
  2648. return;
  2649. }
  2650. ebpf_network_viewer_port_list_t *w = callocz(1, sizeof(ebpf_network_viewer_port_list_t));
  2651. w->value = strdupz(service);
  2652. w->hash = simple_hash(service);
  2653. w->first = w->last = (uint16_t)serv->s_port;
  2654. fill_port_list(list, w);
  2655. }
  2656. /**
  2657. * Netmask
  2658. *
  2659. * Copied from iprange (https://github.com/firehol/iprange/blob/master/iprange.h)
  2660. *
  2661. * @param prefix create the netmask based in the CIDR value.
  2662. *
  2663. * @return
  2664. */
  2665. static inline in_addr_t netmask(int prefix) {
  2666. if (prefix == 0)
  2667. return (~((in_addr_t) - 1));
  2668. else
  2669. return (in_addr_t)(~((1 << (32 - prefix)) - 1));
  2670. }
  2671. /**
  2672. * Broadcast
  2673. *
  2674. * Copied from iprange (https://github.com/firehol/iprange/blob/master/iprange.h)
  2675. *
  2676. * @param addr is the ip address
  2677. * @param prefix is the CIDR value.
  2678. *
  2679. * @return It returns the last address of the range
  2680. */
  2681. static inline in_addr_t broadcast(in_addr_t addr, int prefix)
  2682. {
  2683. return (addr | ~netmask(prefix));
  2684. }
  2685. /**
  2686. * Network
  2687. *
  2688. * Copied from iprange (https://github.com/firehol/iprange/blob/master/iprange.h)
  2689. *
  2690. * @param addr is the ip address
  2691. * @param prefix is the CIDR value.
  2692. *
  2693. * @return It returns the first address of the range.
  2694. */
  2695. static inline in_addr_t ipv4_network(in_addr_t addr, int prefix)
  2696. {
  2697. return (addr & netmask(prefix));
  2698. }
  2699. /**
  2700. * IP to network long
  2701. *
  2702. * @param dst the vector to store the result
  2703. * @param ip the source ip given by our users.
  2704. * @param domain the ip domain (IPV4 or IPV6)
  2705. * @param source the original string
  2706. *
  2707. * @return it returns 0 on success and -1 otherwise.
  2708. */
  2709. static inline int ip2nl(uint8_t *dst, char *ip, int domain, char *source)
  2710. {
  2711. if (inet_pton(domain, ip, dst) <= 0) {
  2712. error("The address specified (%s) is invalid ", source);
  2713. return -1;
  2714. }
  2715. return 0;
  2716. }
  2717. /**
  2718. * Get IPV6 Last Address
  2719. *
  2720. * @param out the address to store the last address.
  2721. * @param in the address used to do the math.
  2722. * @param prefix number of bits used to calculate the address
  2723. */
  2724. static void get_ipv6_last_addr(union netdata_ip_t *out, union netdata_ip_t *in, uint64_t prefix)
  2725. {
  2726. uint64_t mask,tmp;
  2727. uint64_t ret[2];
  2728. memcpy(ret, in->addr32, sizeof(union netdata_ip_t));
  2729. if (prefix == 128) {
  2730. memcpy(out->addr32, in->addr32, sizeof(union netdata_ip_t));
  2731. return;
  2732. } else if (!prefix) {
  2733. ret[0] = ret[1] = 0xFFFFFFFFFFFFFFFF;
  2734. memcpy(out->addr32, ret, sizeof(union netdata_ip_t));
  2735. return;
  2736. } else if (prefix <= 64) {
  2737. ret[1] = 0xFFFFFFFFFFFFFFFFULL;
  2738. tmp = be64toh(ret[0]);
  2739. if (prefix > 0) {
  2740. mask = 0xFFFFFFFFFFFFFFFFULL << (64 - prefix);
  2741. tmp |= ~mask;
  2742. }
  2743. ret[0] = htobe64(tmp);
  2744. } else {
  2745. mask = 0xFFFFFFFFFFFFFFFFULL << (128 - prefix);
  2746. tmp = be64toh(ret[1]);
  2747. tmp |= ~mask;
  2748. ret[1] = htobe64(tmp);
  2749. }
  2750. memcpy(out->addr32, ret, sizeof(union netdata_ip_t));
  2751. }
  2752. /**
  2753. * Calculate ipv6 first address
  2754. *
  2755. * @param out the address to store the first address.
  2756. * @param in the address used to do the math.
  2757. * @param prefix number of bits used to calculate the address
  2758. */
  2759. static void get_ipv6_first_addr(union netdata_ip_t *out, union netdata_ip_t *in, uint64_t prefix)
  2760. {
  2761. uint64_t mask,tmp;
  2762. uint64_t ret[2];
  2763. memcpy(ret, in->addr32, sizeof(union netdata_ip_t));
  2764. if (prefix == 128) {
  2765. memcpy(out->addr32, in->addr32, sizeof(union netdata_ip_t));
  2766. return;
  2767. } else if (!prefix) {
  2768. ret[0] = ret[1] = 0;
  2769. memcpy(out->addr32, ret, sizeof(union netdata_ip_t));
  2770. return;
  2771. } else if (prefix <= 64) {
  2772. ret[1] = 0ULL;
  2773. tmp = be64toh(ret[0]);
  2774. if (prefix > 0) {
  2775. mask = 0xFFFFFFFFFFFFFFFFULL << (64 - prefix);
  2776. tmp &= mask;
  2777. }
  2778. ret[0] = htobe64(tmp);
  2779. } else {
  2780. mask = 0xFFFFFFFFFFFFFFFFULL << (128 - prefix);
  2781. tmp = be64toh(ret[1]);
  2782. tmp &= mask;
  2783. ret[1] = htobe64(tmp);
  2784. }
  2785. memcpy(out->addr32, ret, sizeof(union netdata_ip_t));
  2786. }
  2787. /**
  2788. * Is ip inside the range
  2789. *
  2790. * Check if the ip is inside a IP range
  2791. *
  2792. * @param rfirst the first ip address of the range
  2793. * @param rlast the last ip address of the range
  2794. * @param cmpfirst the first ip to compare
  2795. * @param cmplast the last ip to compare
  2796. * @param family the IP family
  2797. *
  2798. * @return It returns 1 if the IP is inside the range and 0 otherwise
  2799. */
  2800. static int ebpf_is_ip_inside_range(union netdata_ip_t *rfirst, union netdata_ip_t *rlast,
  2801. union netdata_ip_t *cmpfirst, union netdata_ip_t *cmplast, int family)
  2802. {
  2803. if (family == AF_INET) {
  2804. if ((rfirst->addr32[0] <= cmpfirst->addr32[0]) && (rlast->addr32[0] >= cmplast->addr32[0]))
  2805. return 1;
  2806. } else {
  2807. if (memcmp(rfirst->addr8, cmpfirst->addr8, sizeof(union netdata_ip_t)) <= 0 &&
  2808. memcmp(rlast->addr8, cmplast->addr8, sizeof(union netdata_ip_t)) >= 0) {
  2809. return 1;
  2810. }
  2811. }
  2812. return 0;
  2813. }
  2814. /**
  2815. * Fill IP list
  2816. *
  2817. * @param out a pointer to the link list.
  2818. * @param in the structure that will be linked.
  2819. * @param table the modified table.
  2820. */
  2821. void ebpf_fill_ip_list(ebpf_network_viewer_ip_list_t **out, ebpf_network_viewer_ip_list_t *in, char *table)
  2822. {
  2823. #ifndef NETDATA_INTERNAL_CHECKS
  2824. UNUSED(table);
  2825. #endif
  2826. if (in->ver == AF_INET) { // It is simpler to compare using host order
  2827. in->first.addr32[0] = ntohl(in->first.addr32[0]);
  2828. in->last.addr32[0] = ntohl(in->last.addr32[0]);
  2829. }
  2830. if (likely(*out)) {
  2831. ebpf_network_viewer_ip_list_t *move = *out, *store = *out;
  2832. while (move) {
  2833. if (in->ver == move->ver &&
  2834. ebpf_is_ip_inside_range(&move->first, &move->last, &in->first, &in->last, in->ver)) {
  2835. info("The range/value (%s) is inside the range/value (%s) already inserted, it will be ignored.",
  2836. in->value, move->value);
  2837. freez(in->value);
  2838. freez(in);
  2839. return;
  2840. }
  2841. store = move;
  2842. move = move->next;
  2843. }
  2844. store->next = in;
  2845. } else {
  2846. *out = in;
  2847. }
  2848. #ifdef NETDATA_INTERNAL_CHECKS
  2849. char first[256], last[512];
  2850. if (in->ver == AF_INET) {
  2851. info("Adding values %s: (%u - %u) to %s IP list \"%s\" used on network viewer",
  2852. in->value, in->first.addr32[0], in->last.addr32[0],
  2853. (*out == network_viewer_opt.included_ips)?"included":"excluded",
  2854. table);
  2855. } else {
  2856. if (inet_ntop(AF_INET6, in->first.addr8, first, INET6_ADDRSTRLEN) &&
  2857. inet_ntop(AF_INET6, in->last.addr8, last, INET6_ADDRSTRLEN))
  2858. info("Adding values %s - %s to %s IP list \"%s\" used on network viewer",
  2859. first, last,
  2860. (*out == network_viewer_opt.included_ips)?"included":"excluded",
  2861. table);
  2862. }
  2863. #endif
  2864. }
  2865. /**
  2866. * Parse IP List
  2867. *
  2868. * Parse IP list and link it.
  2869. *
  2870. * @param out a pointer to store the link list
  2871. * @param ip the value given as parameter
  2872. */
  2873. static void ebpf_parse_ip_list(void **out, char *ip)
  2874. {
  2875. ebpf_network_viewer_ip_list_t **list = (ebpf_network_viewer_ip_list_t **)out;
  2876. char *ipdup = strdupz(ip);
  2877. union netdata_ip_t first = { };
  2878. union netdata_ip_t last = { };
  2879. char *is_ipv6;
  2880. if (*ip == '*' && *(ip+1) == '\0') {
  2881. memset(first.addr8, 0, sizeof(first.addr8));
  2882. memset(last.addr8, 0xFF, sizeof(last.addr8));
  2883. is_ipv6 = ip;
  2884. clean_ip_structure(list);
  2885. goto storethisip;
  2886. }
  2887. char *end = ip;
  2888. // Move while I cannot find a separator
  2889. while (*end && *end != '/' && *end != '-') end++;
  2890. // We will use only the classic IPV6 for while, but we could consider the base 85 in a near future
  2891. // https://tools.ietf.org/html/rfc1924
  2892. is_ipv6 = strchr(ip, ':');
  2893. int select;
  2894. if (*end && !is_ipv6) { // IPV4 range
  2895. select = (*end == '/') ? 0 : 1;
  2896. *end++ = '\0';
  2897. if (*end == '!') {
  2898. info("The exclusion cannot be in the second part of the range %s, it will be ignored.", ipdup);
  2899. goto cleanipdup;
  2900. }
  2901. if (!select) { // CIDR
  2902. select = ip2nl(first.addr8, ip, AF_INET, ipdup);
  2903. if (select)
  2904. goto cleanipdup;
  2905. select = (int) str2i(end);
  2906. if (select < NETDATA_MINIMUM_IPV4_CIDR || select > NETDATA_MAXIMUM_IPV4_CIDR) {
  2907. info("The specified CIDR %s is not valid, the IP %s will be ignored.", end, ip);
  2908. goto cleanipdup;
  2909. }
  2910. last.addr32[0] = htonl(broadcast(ntohl(first.addr32[0]), select));
  2911. // This was added to remove
  2912. // https://app.codacy.com/manual/netdata/netdata/pullRequest?prid=5810941&bid=19021977
  2913. UNUSED(last.addr32[0]);
  2914. uint32_t ipv4_test = htonl(ipv4_network(ntohl(first.addr32[0]), select));
  2915. if (first.addr32[0] != ipv4_test) {
  2916. first.addr32[0] = ipv4_test;
  2917. struct in_addr ipv4_convert;
  2918. ipv4_convert.s_addr = ipv4_test;
  2919. char ipv4_msg[INET_ADDRSTRLEN];
  2920. if(inet_ntop(AF_INET, &ipv4_convert, ipv4_msg, INET_ADDRSTRLEN))
  2921. info("The network value of CIDR %s was updated for %s .", ipdup, ipv4_msg);
  2922. }
  2923. } else { // Range
  2924. select = ip2nl(first.addr8, ip, AF_INET, ipdup);
  2925. if (select)
  2926. goto cleanipdup;
  2927. select = ip2nl(last.addr8, end, AF_INET, ipdup);
  2928. if (select)
  2929. goto cleanipdup;
  2930. }
  2931. if (htonl(first.addr32[0]) > htonl(last.addr32[0])) {
  2932. info("The specified range %s is invalid, the second address is smallest than the first, it will be ignored.",
  2933. ipdup);
  2934. goto cleanipdup;
  2935. }
  2936. } else if (is_ipv6) { // IPV6
  2937. if (!*end) { // Unique
  2938. select = ip2nl(first.addr8, ip, AF_INET6, ipdup);
  2939. if (select)
  2940. goto cleanipdup;
  2941. memcpy(last.addr8, first.addr8, sizeof(first.addr8));
  2942. } else if (*end == '-') {
  2943. *end++ = 0x00;
  2944. if (*end == '!') {
  2945. info("The exclusion cannot be in the second part of the range %s, it will be ignored.", ipdup);
  2946. goto cleanipdup;
  2947. }
  2948. select = ip2nl(first.addr8, ip, AF_INET6, ipdup);
  2949. if (select)
  2950. goto cleanipdup;
  2951. select = ip2nl(last.addr8, end, AF_INET6, ipdup);
  2952. if (select)
  2953. goto cleanipdup;
  2954. } else { // CIDR
  2955. *end++ = 0x00;
  2956. if (*end == '!') {
  2957. info("The exclusion cannot be in the second part of the range %s, it will be ignored.", ipdup);
  2958. goto cleanipdup;
  2959. }
  2960. select = str2i(end);
  2961. if (select < 0 || select > 128) {
  2962. info("The CIDR %s is not valid, the address %s will be ignored.", end, ip);
  2963. goto cleanipdup;
  2964. }
  2965. uint64_t prefix = (uint64_t)select;
  2966. select = ip2nl(first.addr8, ip, AF_INET6, ipdup);
  2967. if (select)
  2968. goto cleanipdup;
  2969. get_ipv6_last_addr(&last, &first, prefix);
  2970. union netdata_ip_t ipv6_test;
  2971. get_ipv6_first_addr(&ipv6_test, &first, prefix);
  2972. if (memcmp(first.addr8, ipv6_test.addr8, sizeof(union netdata_ip_t)) != 0) {
  2973. memcpy(first.addr8, ipv6_test.addr8, sizeof(union netdata_ip_t));
  2974. struct in6_addr ipv6_convert;
  2975. memcpy(ipv6_convert.s6_addr, ipv6_test.addr8, sizeof(union netdata_ip_t));
  2976. char ipv6_msg[INET6_ADDRSTRLEN];
  2977. if(inet_ntop(AF_INET6, &ipv6_convert, ipv6_msg, INET6_ADDRSTRLEN))
  2978. info("The network value of CIDR %s was updated for %s .", ipdup, ipv6_msg);
  2979. }
  2980. }
  2981. if ((be64toh(*(uint64_t *)&first.addr32[2]) > be64toh(*(uint64_t *)&last.addr32[2]) &&
  2982. !memcmp(first.addr32, last.addr32, 2*sizeof(uint32_t))) ||
  2983. (be64toh(*(uint64_t *)&first.addr32) > be64toh(*(uint64_t *)&last.addr32)) ) {
  2984. info("The specified range %s is invalid, the second address is smallest than the first, it will be ignored.",
  2985. ipdup);
  2986. goto cleanipdup;
  2987. }
  2988. } else { // Unique ip
  2989. select = ip2nl(first.addr8, ip, AF_INET, ipdup);
  2990. if (select)
  2991. goto cleanipdup;
  2992. memcpy(last.addr8, first.addr8, sizeof(first.addr8));
  2993. }
  2994. ebpf_network_viewer_ip_list_t *store;
  2995. storethisip:
  2996. store = callocz(1, sizeof(ebpf_network_viewer_ip_list_t));
  2997. store->value = ipdup;
  2998. store->hash = simple_hash(ipdup);
  2999. store->ver = (uint8_t)(!is_ipv6)?AF_INET:AF_INET6;
  3000. memcpy(store->first.addr8, first.addr8, sizeof(first.addr8));
  3001. memcpy(store->last.addr8, last.addr8, sizeof(last.addr8));
  3002. ebpf_fill_ip_list(list, store, "socket");
  3003. return;
  3004. cleanipdup:
  3005. freez(ipdup);
  3006. }
  3007. /**
  3008. * Parse IP Range
  3009. *
  3010. * Parse the IP ranges given and create Network Viewer IP Structure
  3011. *
  3012. * @param ptr is a pointer with the text to parse.
  3013. */
  3014. static void ebpf_parse_ips(char *ptr)
  3015. {
  3016. // No value
  3017. if (unlikely(!ptr))
  3018. return;
  3019. while (likely(ptr)) {
  3020. // Move forward until next valid character
  3021. while (isspace(*ptr)) ptr++;
  3022. // No valid value found
  3023. if (unlikely(!*ptr))
  3024. return;
  3025. // Find space that ends the list
  3026. char *end = strchr(ptr, ' ');
  3027. if (end) {
  3028. *end++ = '\0';
  3029. }
  3030. int neg = 0;
  3031. if (*ptr == '!') {
  3032. neg++;
  3033. ptr++;
  3034. }
  3035. if (isascii(*ptr)) { // Parse port
  3036. ebpf_parse_ip_list((!neg)?(void **)&network_viewer_opt.included_ips:
  3037. (void **)&network_viewer_opt.excluded_ips,
  3038. ptr);
  3039. }
  3040. ptr = end;
  3041. }
  3042. }
  3043. /**
  3044. * Parse port list
  3045. *
  3046. * Parse an allocated port list with the range given
  3047. *
  3048. * @param out a pointer to store the link list
  3049. * @param range the informed range for the user.
  3050. */
  3051. static void parse_port_list(void **out, char *range)
  3052. {
  3053. int first, last;
  3054. ebpf_network_viewer_port_list_t **list = (ebpf_network_viewer_port_list_t **)out;
  3055. char *copied = strdupz(range);
  3056. if (*range == '*' && *(range+1) == '\0') {
  3057. first = 1;
  3058. last = 65535;
  3059. clean_port_structure(list);
  3060. goto fillenvpl;
  3061. }
  3062. char *end = range;
  3063. //Move while I cannot find a separator
  3064. while (*end && *end != ':' && *end != '-') end++;
  3065. //It has a range
  3066. if (likely(*end)) {
  3067. *end++ = '\0';
  3068. if (*end == '!') {
  3069. info("The exclusion cannot be in the second part of the range, the range %s will be ignored.", copied);
  3070. freez(copied);
  3071. return;
  3072. }
  3073. last = str2i((const char *)end);
  3074. } else {
  3075. last = 0;
  3076. }
  3077. first = str2i((const char *)range);
  3078. if (first < NETDATA_MINIMUM_PORT_VALUE || first > NETDATA_MAXIMUM_PORT_VALUE) {
  3079. info("The first port %d of the range \"%s\" is invalid and it will be ignored!", first, copied);
  3080. freez(copied);
  3081. return;
  3082. }
  3083. if (!last)
  3084. last = first;
  3085. if (last < NETDATA_MINIMUM_PORT_VALUE || last > NETDATA_MAXIMUM_PORT_VALUE) {
  3086. info("The second port %d of the range \"%s\" is invalid and the whole range will be ignored!", last, copied);
  3087. freez(copied);
  3088. return;
  3089. }
  3090. if (first > last) {
  3091. info("The specified order %s is wrong, the smallest value is always the first, it will be ignored!", copied);
  3092. freez(copied);
  3093. return;
  3094. }
  3095. ebpf_network_viewer_port_list_t *w;
  3096. fillenvpl:
  3097. w = callocz(1, sizeof(ebpf_network_viewer_port_list_t));
  3098. w->value = copied;
  3099. w->hash = simple_hash(copied);
  3100. w->first = (uint16_t)htons((uint16_t)first);
  3101. w->last = (uint16_t)htons((uint16_t)last);
  3102. w->cmp_first = (uint16_t)first;
  3103. w->cmp_last = (uint16_t)last;
  3104. fill_port_list(list, w);
  3105. }
  3106. /**
  3107. * Read max dimension.
  3108. *
  3109. * Netdata plot two dimensions per connection, so it is necessary to adjust the values.
  3110. *
  3111. * @param cfg the configuration structure
  3112. */
  3113. static void read_max_dimension(struct config *cfg)
  3114. {
  3115. int maxdim ;
  3116. maxdim = (int) appconfig_get_number(cfg,
  3117. EBPF_NETWORK_VIEWER_SECTION,
  3118. EBPF_MAXIMUM_DIMENSIONS,
  3119. NETDATA_NV_CAP_VALUE);
  3120. if (maxdim < 0) {
  3121. error("'maximum dimensions = %d' must be a positive number, Netdata will change for default value %ld.",
  3122. maxdim, NETDATA_NV_CAP_VALUE);
  3123. maxdim = NETDATA_NV_CAP_VALUE;
  3124. }
  3125. maxdim /= 2;
  3126. if (!maxdim) {
  3127. info("The number of dimensions is too small (%u), we are setting it to minimum 2", network_viewer_opt.max_dim);
  3128. network_viewer_opt.max_dim = 1;
  3129. return;
  3130. }
  3131. network_viewer_opt.max_dim = (uint32_t)maxdim;
  3132. }
  3133. /**
  3134. * Parse Port Range
  3135. *
  3136. * Parse the port ranges given and create Network Viewer Port Structure
  3137. *
  3138. * @param ptr is a pointer with the text to parse.
  3139. */
  3140. static void parse_ports(char *ptr)
  3141. {
  3142. // No value
  3143. if (unlikely(!ptr))
  3144. return;
  3145. while (likely(ptr)) {
  3146. // Move forward until next valid character
  3147. while (isspace(*ptr)) ptr++;
  3148. // No valid value found
  3149. if (unlikely(!*ptr))
  3150. return;
  3151. // Find space that ends the list
  3152. char *end = strchr(ptr, ' ');
  3153. if (end) {
  3154. *end++ = '\0';
  3155. }
  3156. int neg = 0;
  3157. if (*ptr == '!') {
  3158. neg++;
  3159. ptr++;
  3160. }
  3161. if (isdigit(*ptr)) { // Parse port
  3162. parse_port_list((!neg)?(void **)&network_viewer_opt.included_port:(void **)&network_viewer_opt.excluded_port,
  3163. ptr);
  3164. } else if (isalpha(*ptr)) { // Parse service
  3165. parse_service_list((!neg)?(void **)&network_viewer_opt.included_port:(void **)&network_viewer_opt.excluded_port,
  3166. ptr);
  3167. } else if (*ptr == '*') { // All
  3168. parse_port_list((!neg)?(void **)&network_viewer_opt.included_port:(void **)&network_viewer_opt.excluded_port,
  3169. ptr);
  3170. }
  3171. ptr = end;
  3172. }
  3173. }
  3174. /**
  3175. * Link hostname
  3176. *
  3177. * @param out is the output link list
  3178. * @param in the hostname to add to list.
  3179. */
  3180. static void link_hostname(ebpf_network_viewer_hostname_list_t **out, ebpf_network_viewer_hostname_list_t *in)
  3181. {
  3182. if (likely(*out)) {
  3183. ebpf_network_viewer_hostname_list_t *move = *out;
  3184. for (; move->next ; move = move->next ) {
  3185. if (move->hash == in->hash && !strcmp(move->value, in->value)) {
  3186. info("The hostname %s was already inserted, it will be ignored.", in->value);
  3187. freez(in->value);
  3188. simple_pattern_free(in->value_pattern);
  3189. freez(in);
  3190. return;
  3191. }
  3192. }
  3193. move->next = in;
  3194. } else {
  3195. *out = in;
  3196. }
  3197. #ifdef NETDATA_INTERNAL_CHECKS
  3198. info("Adding value %s to %s hostname list used on network viewer",
  3199. in->value,
  3200. (*out == network_viewer_opt.included_hostnames)?"included":"excluded");
  3201. #endif
  3202. }
  3203. /**
  3204. * Link Hostnames
  3205. *
  3206. * Parse the list of hostnames to create the link list.
  3207. * This is not associated with the IP, because simple patterns like *example* cannot be resolved to IP.
  3208. *
  3209. * @param out is the output link list
  3210. * @param parse is a pointer with the text to parser.
  3211. */
  3212. static void link_hostnames(char *parse)
  3213. {
  3214. // No value
  3215. if (unlikely(!parse))
  3216. return;
  3217. while (likely(parse)) {
  3218. // Find the first valid value
  3219. while (isspace(*parse)) parse++;
  3220. // No valid value found
  3221. if (unlikely(!*parse))
  3222. return;
  3223. // Find space that ends the list
  3224. char *end = strchr(parse, ' ');
  3225. if (end) {
  3226. *end++ = '\0';
  3227. }
  3228. int neg = 0;
  3229. if (*parse == '!') {
  3230. neg++;
  3231. parse++;
  3232. }
  3233. ebpf_network_viewer_hostname_list_t *hostname = callocz(1 , sizeof(ebpf_network_viewer_hostname_list_t));
  3234. hostname->value = strdupz(parse);
  3235. hostname->hash = simple_hash(parse);
  3236. hostname->value_pattern = simple_pattern_create(parse, NULL, SIMPLE_PATTERN_EXACT, true);
  3237. link_hostname((!neg)?&network_viewer_opt.included_hostnames:&network_viewer_opt.excluded_hostnames,
  3238. hostname);
  3239. parse = end;
  3240. }
  3241. }
  3242. /**
  3243. * Parse network viewer section
  3244. *
  3245. * @param cfg the configuration structure
  3246. */
  3247. void parse_network_viewer_section(struct config *cfg)
  3248. {
  3249. read_max_dimension(cfg);
  3250. network_viewer_opt.hostname_resolution_enabled = appconfig_get_boolean(cfg,
  3251. EBPF_NETWORK_VIEWER_SECTION,
  3252. EBPF_CONFIG_RESOLVE_HOSTNAME,
  3253. CONFIG_BOOLEAN_NO);
  3254. network_viewer_opt.service_resolution_enabled = appconfig_get_boolean(cfg,
  3255. EBPF_NETWORK_VIEWER_SECTION,
  3256. EBPF_CONFIG_RESOLVE_SERVICE,
  3257. CONFIG_BOOLEAN_NO);
  3258. char *value = appconfig_get(cfg, EBPF_NETWORK_VIEWER_SECTION, EBPF_CONFIG_PORTS, NULL);
  3259. parse_ports(value);
  3260. if (network_viewer_opt.hostname_resolution_enabled) {
  3261. value = appconfig_get(cfg, EBPF_NETWORK_VIEWER_SECTION, EBPF_CONFIG_HOSTNAMES, NULL);
  3262. link_hostnames(value);
  3263. } else {
  3264. info("Name resolution is disabled, collector will not parser \"hostnames\" list.");
  3265. }
  3266. value = appconfig_get(cfg, EBPF_NETWORK_VIEWER_SECTION,
  3267. "ips", "!127.0.0.1/8 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 fc00::/7 !::1/128");
  3268. ebpf_parse_ips(value);
  3269. }
  3270. /**
  3271. * Link dimension name
  3272. *
  3273. * Link user specified names inside a link list.
  3274. *
  3275. * @param port the port number associated to the dimension name.
  3276. * @param hash the calculated hash for the dimension name.
  3277. * @param name the dimension name.
  3278. */
  3279. static void link_dimension_name(char *port, uint32_t hash, char *value)
  3280. {
  3281. int test = str2i(port);
  3282. if (test < NETDATA_MINIMUM_PORT_VALUE || test > NETDATA_MAXIMUM_PORT_VALUE){
  3283. error("The dimension given (%s = %s) has an invalid value and it will be ignored.", port, value);
  3284. return;
  3285. }
  3286. ebpf_network_viewer_dim_name_t *w;
  3287. w = callocz(1, sizeof(ebpf_network_viewer_dim_name_t));
  3288. w->name = strdupz(value);
  3289. w->hash = hash;
  3290. w->port = (uint16_t) htons(test);
  3291. ebpf_network_viewer_dim_name_t *names = network_viewer_opt.names;
  3292. if (unlikely(!names)) {
  3293. network_viewer_opt.names = w;
  3294. } else {
  3295. for (; names->next; names = names->next) {
  3296. if (names->port == w->port) {
  3297. info("Duplicated definition for a service, the name %s will be ignored. ", names->name);
  3298. freez(names->name);
  3299. names->name = w->name;
  3300. names->hash = w->hash;
  3301. freez(w);
  3302. return;
  3303. }
  3304. }
  3305. names->next = w;
  3306. }
  3307. #ifdef NETDATA_INTERNAL_CHECKS
  3308. info("Adding values %s( %u) to dimension name list used on network viewer", w->name, htons(w->port));
  3309. #endif
  3310. }
  3311. /**
  3312. * Parse service Name section.
  3313. *
  3314. * This function gets the values that will be used to overwrite dimensions.
  3315. *
  3316. * @param cfg the configuration structure
  3317. */
  3318. void parse_service_name_section(struct config *cfg)
  3319. {
  3320. struct section *co = appconfig_get_section(cfg, EBPF_SERVICE_NAME_SECTION);
  3321. if (co) {
  3322. struct config_option *cv;
  3323. for (cv = co->values; cv ; cv = cv->next) {
  3324. link_dimension_name(cv->name, cv->hash, cv->value);
  3325. }
  3326. }
  3327. // Always associated the default port to Netdata
  3328. ebpf_network_viewer_dim_name_t *names = network_viewer_opt.names;
  3329. if (names) {
  3330. uint16_t default_port = htons(19999);
  3331. while (names) {
  3332. if (names->port == default_port)
  3333. return;
  3334. names = names->next;
  3335. }
  3336. }
  3337. char *port_string = getenv("NETDATA_LISTEN_PORT");
  3338. if (port_string) {
  3339. // if variable has an invalid value, we assume netdata is using 19999
  3340. int default_port = str2i(port_string);
  3341. if (default_port > 0 && default_port < 65536)
  3342. link_dimension_name(port_string, simple_hash(port_string), "Netdata");
  3343. }
  3344. }
  3345. void parse_table_size_options(struct config *cfg)
  3346. {
  3347. socket_maps[NETDATA_SOCKET_TABLE_BANDWIDTH].user_input = (uint32_t) appconfig_get_number(cfg,
  3348. EBPF_GLOBAL_SECTION,
  3349. EBPF_CONFIG_BANDWIDTH_SIZE, NETDATA_MAXIMUM_CONNECTIONS_ALLOWED);
  3350. socket_maps[NETDATA_SOCKET_TABLE_IPV4].user_input = (uint32_t) appconfig_get_number(cfg,
  3351. EBPF_GLOBAL_SECTION,
  3352. EBPF_CONFIG_IPV4_SIZE, NETDATA_MAXIMUM_CONNECTIONS_ALLOWED);
  3353. socket_maps[NETDATA_SOCKET_TABLE_IPV6].user_input = (uint32_t) appconfig_get_number(cfg,
  3354. EBPF_GLOBAL_SECTION,
  3355. EBPF_CONFIG_IPV6_SIZE, NETDATA_MAXIMUM_CONNECTIONS_ALLOWED);
  3356. socket_maps[NETDATA_SOCKET_TABLE_UDP].user_input = (uint32_t) appconfig_get_number(cfg,
  3357. EBPF_GLOBAL_SECTION,
  3358. EBPF_CONFIG_UDP_SIZE, NETDATA_MAXIMUM_UDP_CONNECTIONS_ALLOWED);
  3359. }
  3360. /*
  3361. * Load BPF
  3362. *
  3363. * Load BPF files.
  3364. *
  3365. * @param em the structure with configuration
  3366. */
  3367. static int ebpf_socket_load_bpf(ebpf_module_t *em)
  3368. {
  3369. int ret = 0;
  3370. if (em->load & EBPF_LOAD_LEGACY) {
  3371. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  3372. if (!em->probe_links) {
  3373. ret = -1;
  3374. }
  3375. }
  3376. #ifdef LIBBPF_MAJOR_VERSION
  3377. else {
  3378. socket_bpf_obj = socket_bpf__open();
  3379. if (!socket_bpf_obj)
  3380. ret = -1;
  3381. else
  3382. ret = ebpf_socket_load_and_attach(socket_bpf_obj, em);
  3383. }
  3384. #endif
  3385. if (ret) {
  3386. error("%s %s", EBPF_DEFAULT_ERROR_MSG, em->thread_name);
  3387. }
  3388. return ret;
  3389. }
  3390. /**
  3391. * Socket thread
  3392. *
  3393. * Thread used to generate socket charts.
  3394. *
  3395. * @param ptr a pointer to `struct ebpf_module`
  3396. *
  3397. * @return It always return NULL
  3398. */
  3399. void *ebpf_socket_thread(void *ptr)
  3400. {
  3401. netdata_thread_cleanup_push(ebpf_socket_exit, ptr);
  3402. ebpf_module_t *em = (ebpf_module_t *)ptr;
  3403. em->maps = socket_maps;
  3404. parse_table_size_options(&socket_config);
  3405. if (pthread_mutex_init(&nv_mutex, NULL)) {
  3406. error("Cannot initialize local mutex");
  3407. goto endsocket;
  3408. }
  3409. ebpf_socket_allocate_global_vectors(em->apps_charts);
  3410. if (network_viewer_opt.enabled) {
  3411. memset(&inbound_vectors.tree, 0, sizeof(avl_tree_lock));
  3412. memset(&outbound_vectors.tree, 0, sizeof(avl_tree_lock));
  3413. avl_init_lock(&inbound_vectors.tree, ebpf_compare_sockets);
  3414. avl_init_lock(&outbound_vectors.tree, ebpf_compare_sockets);
  3415. initialize_inbound_outbound();
  3416. }
  3417. if (running_on_kernel < NETDATA_EBPF_KERNEL_5_0)
  3418. em->mode = MODE_ENTRY;
  3419. #ifdef LIBBPF_MAJOR_VERSION
  3420. ebpf_adjust_thread_load(em, default_btf);
  3421. #endif
  3422. if (ebpf_socket_load_bpf(em)) {
  3423. pthread_mutex_unlock(&lock);
  3424. goto endsocket;
  3425. }
  3426. int algorithms[NETDATA_MAX_SOCKET_VECTOR] = {
  3427. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX,
  3428. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX,
  3429. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_INCREMENTAL_IDX,
  3430. NETDATA_EBPF_INCREMENTAL_IDX
  3431. };
  3432. ebpf_global_labels(
  3433. socket_aggregated_data, socket_publish_aggregated, socket_dimension_names, socket_id_names,
  3434. algorithms, NETDATA_MAX_SOCKET_VECTOR);
  3435. pthread_mutex_lock(&lock);
  3436. ebpf_create_global_charts(em);
  3437. ebpf_update_stats(&plugin_statistics, em);
  3438. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps);
  3439. #ifdef NETDATA_DEV_MODE
  3440. if (ebpf_aral_socket_pid)
  3441. ebpf_statistic_create_aral_chart(NETDATA_EBPF_SOCKET_ARAL_NAME, em);
  3442. #endif
  3443. pthread_mutex_unlock(&lock);
  3444. socket_collector(em);
  3445. endsocket:
  3446. ebpf_update_disabled_plugin_stats(em);
  3447. netdata_thread_cleanup_pop(1);
  3448. return NULL;
  3449. }