ebpf_socket.c 145 KB

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