ebpf_socket.c 146 KB

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