ebpf_socket.c 149 KB

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