ebpf_socket.c 148 KB

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