ebpf_socket.c 145 KB

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