ebpf_socket.c 146 KB

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