ebpf_socket.c 118 KB

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