forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebpf_socket.c
1938 lines (1672 loc) · 61.7 KB
/
ebpf_socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0-or-later
#include <sys/resource.h>
#include "ebpf.h"
#include "ebpf_socket.h"
/*****************************************************************
*
* GLOBAL VARIABLES
*
*****************************************************************/
static char *socket_dimension_names[NETDATA_MAX_SOCKET_VECTOR] = { "sent", "received", "close", "sent",
"received", "retransmitted" };
static char *socket_id_names[NETDATA_MAX_SOCKET_VECTOR] = { "tcp_sendmsg", "tcp_cleanup_rbuf", "tcp_close",
"udp_sendmsg", "udp_recvmsg", "tcp_retransmit_skb" };
static netdata_idx_t *socket_hash_values = NULL;
static netdata_syscall_stat_t *socket_aggregated_data = NULL;
static netdata_publish_syscall_t *socket_publish_aggregated = NULL;
static ebpf_data_t socket_data;
ebpf_socket_publish_apps_t **socket_bandwidth_curr = NULL;
ebpf_socket_publish_apps_t **socket_bandwidth_prev = NULL;
static ebpf_bandwidth_t *bandwidth_vector = NULL;
static int socket_apps_created = 0;
pthread_mutex_t nv_mutex;
int wait_to_plot = 0;
int read_thread_closed = 1;
netdata_vector_plot_t inbound_vectors = { .plot = NULL, .next = 0, .last = 0 };
netdata_vector_plot_t outbound_vectors = { .plot = NULL, .next = 0, .last = 0 };
netdata_socket_t *socket_values;
ebpf_network_viewer_port_list_t *listen_ports = NULL;
static int *map_fd = NULL;
static struct bpf_object *objects = NULL;
static struct bpf_link **probe_links = NULL;
/*****************************************************************
*
* PROCESS DATA AND SEND TO NETDATA
*
*****************************************************************/
/**
* Update publish structure before to send data to Netdata.
*
* @param publish the first output structure with independent dimensions
* @param tcp structure to store IO from tcp sockets
* @param udp structure to store IO from udp sockets
* @param input the structure with the input data.
*/
static void ebpf_update_global_publish(
netdata_publish_syscall_t *publish, netdata_publish_vfs_common_t *tcp, netdata_publish_vfs_common_t *udp,
netdata_syscall_stat_t *input)
{
netdata_publish_syscall_t *move = publish;
while (move) {
if (input->call != move->pcall) {
// This condition happens to avoid initial values with dimensions higher than normal values.
if (move->pcall) {
move->ncall = (input->call > move->pcall) ? input->call - move->pcall : move->pcall - input->call;
move->nbyte = (input->bytes > move->pbyte) ? input->bytes - move->pbyte : move->pbyte - input->bytes;
move->nerr = (input->ecall > move->nerr) ? input->ecall - move->perr : move->perr - input->ecall;
} else {
move->ncall = 0;
move->nbyte = 0;
move->nerr = 0;
}
move->pcall = input->call;
move->pbyte = input->bytes;
move->perr = input->ecall;
} else {
move->ncall = 0;
move->nbyte = 0;
move->nerr = 0;
}
input = input->next;
move = move->next;
}
tcp->write = -((long)publish[0].nbyte);
tcp->read = (long)publish[1].nbyte;
udp->write = -((long)publish[3].nbyte);
udp->read = (long)publish[4].nbyte;
}
/**
* Update Network Viewer plot data
*
* @param plot the structure where the data will be stored
* @param sock the last update from the socket
*/
static inline void update_nv_plot_data(netdata_plot_values_t *plot, netdata_socket_t *sock)
{
if (sock->ct > plot->last_time) {
plot->last_time = sock->ct;
plot->plot_recv_packets = sock->recv_packets;
plot->plot_sent_packets = sock->sent_packets;
plot->plot_recv_bytes = sock->recv_bytes;
plot->plot_sent_bytes = sock->sent_bytes;
plot->plot_retransmit = sock->retransmit;
}
sock->recv_packets = 0;
sock->sent_packets = 0;
sock->recv_bytes = 0;
sock->sent_bytes = 0;
sock->retransmit = 0;
}
/**
* Calculate Network Viewer Plot
*
* Do math with collected values before to plot data.
*/
static inline void calculate_nv_plot()
{
uint32_t i;
uint32_t end = inbound_vectors.next;
for (i = 0; i < end; i++) {
update_nv_plot_data(&inbound_vectors.plot[i].plot, &inbound_vectors.plot[i].sock);
}
inbound_vectors.max_plot = end;
// The 'Other' dimension is always calculated for the chart to have at least one dimension
update_nv_plot_data(&inbound_vectors.plot[inbound_vectors.last].plot,
&inbound_vectors.plot[inbound_vectors.last].sock);
end = outbound_vectors.next;
for (i = 0; i < end; i++) {
update_nv_plot_data(&outbound_vectors.plot[i].plot, &outbound_vectors.plot[i].sock);
}
outbound_vectors.max_plot = end;
// The 'Other' dimension is always calculated for the chart to have at least one dimension
update_nv_plot_data(&outbound_vectors.plot[outbound_vectors.last].plot,
&outbound_vectors.plot[outbound_vectors.last].sock);
}
/**
* Network viewer send bytes
*
* @param ptr the structure with values to plot
* @param chart the chart name.
*/
static inline void ebpf_socket_nv_send_bytes(netdata_vector_plot_t *ptr, char *chart)
{
uint32_t i;
uint32_t end = ptr->last_plot;
netdata_socket_plot_t *w = ptr->plot;
collected_number value;
write_begin_chart(NETDATA_EBPF_FAMILY, chart);
for (i = 0; i < end; i++) {
value = ((collected_number) w[i].plot.plot_sent_bytes);
write_chart_dimension(w[i].dimension_sent, value);
value = (collected_number) w[i].plot.plot_recv_bytes;
write_chart_dimension(w[i].dimension_recv, value);
}
i = ptr->last;
value = ((collected_number) w[i].plot.plot_sent_bytes);
write_chart_dimension(w[i].dimension_sent, value);
value = (collected_number) w[i].plot.plot_recv_bytes;
write_chart_dimension(w[i].dimension_recv, value);
write_end_chart();
}
/**
* Network Viewer Send packets
*
* @param ptr the structure with values to plot
* @param chart the chart name.
*/
static inline void ebpf_socket_nv_send_packets(netdata_vector_plot_t *ptr, char *chart)
{
uint32_t i;
uint32_t end = ptr->last_plot;
netdata_socket_plot_t *w = ptr->plot;
collected_number value;
write_begin_chart(NETDATA_EBPF_FAMILY, chart);
for (i = 0; i < end; i++) {
value = ((collected_number)w[i].plot.plot_sent_packets);
write_chart_dimension(w[i].dimension_sent, value);
value = (collected_number) w[i].plot.plot_recv_packets;
write_chart_dimension(w[i].dimension_recv, value);
}
i = ptr->last;
value = ((collected_number)w[i].plot.plot_sent_packets);
write_chart_dimension(w[i].dimension_sent, value);
value = (collected_number)w[i].plot.plot_recv_packets;
write_chart_dimension(w[i].dimension_recv, value);
write_end_chart();
}
/**
* Network Viewer Send Retransmit
*
* @param ptr the structure with values to plot
* @param chart the chart name.
*/
static inline void ebpf_socket_nv_send_retransmit(netdata_vector_plot_t *ptr, char *chart)
{
uint32_t i;
uint32_t end = ptr->last_plot;
netdata_socket_plot_t *w = ptr->plot;
collected_number value;
write_begin_chart(NETDATA_EBPF_FAMILY, chart);
for (i = 0; i < end; i++) {
value = (collected_number) w[i].plot.plot_retransmit;
write_chart_dimension(w[i].dimension_retransmit, value);
}
i = ptr->last;
value = (collected_number)w[i].plot.plot_retransmit;
write_chart_dimension(w[i].dimension_retransmit, value);
write_end_chart();
}
/**
* Send network viewer data
*
* @param ptr the pointer to plot data
*/
static void ebpf_socket_send_nv_data(netdata_vector_plot_t *ptr)
{
if (!ptr->flags)
return;
if (ptr == (netdata_vector_plot_t *)&outbound_vectors) {
ebpf_socket_nv_send_bytes(ptr, NETDATA_NV_OUTBOUND_BYTES);
fflush(stdout);
ebpf_socket_nv_send_packets(ptr, NETDATA_NV_OUTBOUND_PACKETS);
fflush(stdout);
ebpf_socket_nv_send_retransmit(ptr, NETDATA_NV_OUTBOUND_RETRANSMIT);
fflush(stdout);
} else {
ebpf_socket_nv_send_bytes(ptr, NETDATA_NV_INBOUND_BYTES);
fflush(stdout);
ebpf_socket_nv_send_packets(ptr, NETDATA_NV_INBOUND_PACKETS);
fflush(stdout);
}
}
/**
* Update the publish strctures to create the dimenssions
*
* @param curr Last values read from memory.
* @param prev Previous values read from memory.
*/
static void ebpf_socket_update_apps_publish(ebpf_socket_publish_apps_t *curr, ebpf_socket_publish_apps_t *prev)
{
curr->publish_received_bytes = curr->bytes_received - prev->bytes_received;
curr->publish_sent_bytes = curr->bytes_sent - prev->bytes_sent;
curr->publish_tcp_sent = curr->call_tcp_sent - prev->call_tcp_sent;
curr->publish_tcp_received = curr->call_tcp_received - prev->call_tcp_received;
curr->publish_retransmit = curr->retransmit - prev->retransmit;
curr->publish_udp_sent = curr->call_udp_sent - prev->call_udp_sent;
curr->publish_udp_received = curr->call_udp_received - prev->call_udp_received;
}
/**
* Send data to Netdata calling auxiliar functions.
*
* @param em the structure with thread information
*/
static void ebpf_socket_send_data(ebpf_module_t *em)
{
netdata_publish_vfs_common_t common_tcp;
netdata_publish_vfs_common_t common_udp;
ebpf_update_global_publish(socket_publish_aggregated, &common_tcp, &common_udp, socket_aggregated_data);
write_count_chart(
NETDATA_TCP_FUNCTION_COUNT, NETDATA_EBPF_FAMILY, socket_publish_aggregated, 3);
write_io_chart(
NETDATA_TCP_FUNCTION_BYTES, NETDATA_EBPF_FAMILY, socket_id_names[0], socket_id_names[1], &common_tcp);
if (em->mode < MODE_ENTRY) {
write_err_chart(
NETDATA_TCP_FUNCTION_ERROR, NETDATA_EBPF_FAMILY, socket_publish_aggregated, 2);
}
write_count_chart(
NETDATA_TCP_RETRANSMIT, NETDATA_EBPF_FAMILY, &socket_publish_aggregated[NETDATA_RETRANSMIT_START], 1);
write_count_chart(
NETDATA_UDP_FUNCTION_COUNT, NETDATA_EBPF_FAMILY, &socket_publish_aggregated[NETDATA_UDP_START], 2);
write_io_chart(
NETDATA_UDP_FUNCTION_BYTES, NETDATA_EBPF_FAMILY, socket_id_names[3], socket_id_names[4], &common_udp);
if (em->mode < MODE_ENTRY) {
write_err_chart(
NETDATA_UDP_FUNCTION_ERROR, NETDATA_EBPF_FAMILY, &socket_publish_aggregated[NETDATA_UDP_START], 2);
}
}
/**
* Sum values for pid
*
* @param root the structure with all available PIDs
*
* @param offset the address that we are reading
*
* @return it returns the sum of all PIDs
*/
long long ebpf_socket_sum_values_for_pids(struct pid_on_target *root, size_t offset)
{
long long ret = 0;
while (root) {
int32_t pid = root->pid;
ebpf_socket_publish_apps_t *w = socket_bandwidth_curr[pid];
if (w) {
ret += get_value_from_structure((char *)w, offset);
}
root = root->next;
}
return ret;
}
/**
* Send data to Netdata calling auxiliar functions.
*
* @param em the structure with thread information
* @param root the target list.
*/
void ebpf_socket_send_apps_data(ebpf_module_t *em, struct target *root)
{
UNUSED(em);
if (!socket_apps_created)
return;
struct target *w;
collected_number value;
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_SENT);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
publish_sent_bytes));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_RECV);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
publish_received_bytes));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
publish_tcp_sent));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
publish_tcp_received));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
publish_retransmit));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
publish_udp_sent));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS);
for (w = root; w; w = w->next) {
if (unlikely(w->exposed && w->processes)) {
value = ebpf_socket_sum_values_for_pids(w->root_pid, offsetof(ebpf_socket_publish_apps_t,
publish_udp_received));
write_chart_dimension(w->name, value);
}
}
write_end_chart();
}
/*****************************************************************
*
* FUNCTIONS TO CREATE CHARTS
*
*****************************************************************/
/**
* Create global charts
*
* Call ebpf_create_chart to create the charts for the collector.
*
* @param em a pointer to the structure with the default values.
*/
static void ebpf_create_global_charts(ebpf_module_t *em)
{
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_TCP_FUNCTION_COUNT,
"Calls to internal functions",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_SOCKET_GROUP,
21070,
ebpf_create_global_dimension,
socket_publish_aggregated,
3);
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_TCP_FUNCTION_BYTES,
"TCP bandwidth",
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_SOCKET_GROUP,
21071,
ebpf_create_global_dimension,
socket_publish_aggregated,
3);
if (em->mode < MODE_ENTRY) {
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_TCP_FUNCTION_ERROR,
"TCP errors",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_SOCKET_GROUP,
21072,
ebpf_create_global_dimension,
socket_publish_aggregated,
2);
}
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_TCP_RETRANSMIT,
"Packages retransmitted",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_SOCKET_GROUP,
21073,
ebpf_create_global_dimension,
&socket_publish_aggregated[NETDATA_RETRANSMIT_START],
1);
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_UDP_FUNCTION_COUNT,
"UDP calls",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_SOCKET_GROUP,
21074,
ebpf_create_global_dimension,
&socket_publish_aggregated[NETDATA_UDP_START],
2);
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_UDP_FUNCTION_BYTES,
"UDP bandwidth",
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_SOCKET_GROUP,
21075,
ebpf_create_global_dimension,
&socket_publish_aggregated[NETDATA_UDP_START],
2);
if (em->mode < MODE_ENTRY) {
ebpf_create_chart(NETDATA_EBPF_FAMILY,
NETDATA_UDP_FUNCTION_ERROR,
"UDP errors",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_SOCKET_GROUP,
21076,
ebpf_create_global_dimension,
&socket_publish_aggregated[NETDATA_UDP_START],
2);
}
}
/**
* Create apps charts
*
* Call ebpf_create_chart to create the charts on apps submenu.
*
* @param em a pointer to the structure with the default values.
*/
void ebpf_socket_create_apps_charts(ebpf_module_t *em, struct target *root)
{
UNUSED(em);
ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_SENT,
"Bytes sent",
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_APPS_NET_GROUP,
20080,
root);
ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_RECV,
"bytes received",
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_APPS_NET_GROUP,
20081,
root);
ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_TCP_SEND_CALLS,
"Calls for tcp_sendmsg",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_NET_GROUP,
20082,
root);
ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_TCP_RECV_CALLS,
"Calls for tcp_cleanup_rbuf",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_NET_GROUP,
20083,
root);
ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_TCP_RETRANSMIT,
"Calls for tcp_retransmit",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_NET_GROUP,
20084,
root);
ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_UDP_SEND_CALLS,
"Calls for udp_sendmsg",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_NET_GROUP,
20085,
root);
ebpf_create_charts_on_apps(NETDATA_NET_APPS_BANDWIDTH_UDP_RECV_CALLS,
"Calls for udp_recvmsg",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_APPS_NET_GROUP,
20086,
root);
socket_apps_created = 1;
}
/**
* Create network viewer chart
*
* Create common charts.
*
* @param id the chart id
* @param title the chart title
* @param units the units label
* @param family the group name used to attach the chart on dashaboard
* @param order the chart order
* @param ptr the plot structure with values.
*/
static void ebpf_socket_create_nv_chart(char *id, char *title, char *units,
char *family, int order, netdata_vector_plot_t *ptr)
{
ebpf_write_chart_cmd(NETDATA_EBPF_FAMILY,
id,
title,
units,
family,
"stacked",
order);
uint32_t i;
uint32_t end = ptr->last_plot;
netdata_socket_plot_t *w = ptr->plot;
for (i = 0; i < end; i++) {
fprintf(stdout, "DIMENSION %s '' incremental -1 1\n", w[i].dimension_sent);
fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[i].dimension_recv);
}
end = ptr->last;
fprintf(stdout, "DIMENSION %s '' incremental -1 1\n", w[end].dimension_sent);
fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[end].dimension_recv);
}
/**
* Create network viewer retransmit
*
* Create a specific chart.
*
* @param id the chart id
* @param title the chart title
* @param units the units label
* @param family the group name used to attach the chart on dashaboard
* @param order the chart order
* @param ptr the plot structure with values.
*/
static void ebpf_socket_create_nv_retransmit(char *id, char *title, char *units,
char *family, int order, netdata_vector_plot_t *ptr)
{
ebpf_write_chart_cmd(NETDATA_EBPF_FAMILY,
id,
title,
units,
family,
"stacked",
order);
uint32_t i;
uint32_t end = ptr->last_plot;
netdata_socket_plot_t *w = ptr->plot;
for (i = 0; i < end; i++) {
fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[i].dimension_retransmit);
}
end = ptr->last;
fprintf(stdout, "DIMENSION %s '' incremental 1 1\n", w[end].dimension_retransmit);
}
/**
* Create Network Viewer charts
*
* Recreate the charts when new sockets are created.
*
* @param ptr a pointer for inbound or outbound vectors.
*/
static void ebpf_socket_create_nv_charts(netdata_vector_plot_t *ptr)
{
// We do not have new sockets, so we do not need move forward
if (ptr->max_plot == ptr->last_plot)
return;
ptr->last_plot = ptr->max_plot;
if (ptr == (netdata_vector_plot_t *)&outbound_vectors) {
ebpf_socket_create_nv_chart(NETDATA_NV_OUTBOUND_BYTES,
"Outbound connections (bytes).",
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_NETWORK_CONNECTIONS_GROUP,
21080,
ptr);
ebpf_socket_create_nv_chart(NETDATA_NV_OUTBOUND_PACKETS,
"Outbound connections (packets)",
EBPF_COMMON_DIMENSION_PACKETS,
NETDATA_NETWORK_CONNECTIONS_GROUP,
21082,
ptr);
ebpf_socket_create_nv_retransmit(NETDATA_NV_OUTBOUND_RETRANSMIT,
"Retransmitted packets",
EBPF_COMMON_DIMENSION_CALL,
NETDATA_NETWORK_CONNECTIONS_GROUP,
21083,
ptr);
} else {
ebpf_socket_create_nv_chart(NETDATA_NV_INBOUND_BYTES,
"Inbound connections (bytes)",
EBPF_COMMON_DIMENSION_BYTESS,
NETDATA_NETWORK_CONNECTIONS_GROUP,
21084,
ptr);
ebpf_socket_create_nv_chart(NETDATA_NV_INBOUND_PACKETS,
"Inbound connections (packets)",
EBPF_COMMON_DIMENSION_PACKETS,
NETDATA_NETWORK_CONNECTIONS_GROUP,
21085,
ptr);
}
ptr->flags |= NETWORK_VIEWER_CHARTS_CREATED;
}
/*****************************************************************
*
* READ INFORMATION FROM KERNEL RING
*
*****************************************************************/
/**
* Is specific ip inside the range
*
* Check if the ip is inside a IP range previously defined
*
* @param cmp the IP to compare
* @param family the IP family
*
* @return It returns 1 if the IP is inside the range and 0 otherwise
*/
static int is_specific_ip_inside_range(union netdata_ip_t *cmp, int family)
{
if (!network_viewer_opt.excluded_ips && !network_viewer_opt.included_ips)
return 1;
uint32_t ipv4_test = ntohl(cmp->addr32[0]);
ebpf_network_viewer_ip_list_t *move = network_viewer_opt.excluded_ips;
while (move) {
if (family == AF_INET) {
if (ntohl(move->first.addr32[0]) <= ipv4_test &&
ipv4_test <= ntohl(move->last.addr32[0]) )
return 0;
} else {
if (memcmp(move->first.addr8, cmp->addr8, sizeof(union netdata_ip_t)) <= 0 &&
memcmp(move->last.addr8, cmp->addr8, sizeof(union netdata_ip_t)) >= 0) {
return 0;
}
}
move = move->next;
}
move = network_viewer_opt.included_ips;
while (move) {
if (family == AF_INET) {
if (ntohl(move->first.addr32[0]) <= ipv4_test &&
ntohl(move->last.addr32[0]) >= ipv4_test)
return 1;
} else {
if (memcmp(move->first.addr8, cmp->addr8, sizeof(union netdata_ip_t)) <= 0 &&
memcmp(move->last.addr8, cmp->addr8, sizeof(union netdata_ip_t)) >= 0) {
return 1;
}
}
move = move->next;
}
return 0;
}
/**
* Is port inside range
*
* Verify if the cmp port is inside the range [first, last].
* This function expects only the last parameter as big endian.
*
* @param cmp the value to compare
*
* @return It returns 1 when cmp is inside and 0 otherwise.
*/
static int is_port_inside_range(uint16_t cmp)
{
// We do not have restrictions for ports.
if (!network_viewer_opt.excluded_port && !network_viewer_opt.included_port)
return 1;
// Test if port is excluded
ebpf_network_viewer_port_list_t *move = network_viewer_opt.excluded_port;
cmp = htons(cmp);
while (move) {
if (move->cmp_first <= cmp && cmp <= move->cmp_last)
return 0;
move = move->next;
}
// Test if the port is inside allowed range
move = network_viewer_opt.included_port;
while (move) {
if (move->cmp_first <= cmp && cmp <= move->cmp_last)
return 1;
move = move->next;
}
return 0;
}
/**
* Hostname matches pattern
*
* @param cmp the value to compare
*
* @return It returns 1 when the value matches and zero otherwise.
*/
int hostname_matches_pattern(char *cmp)
{
if (!network_viewer_opt.included_hostnames && !network_viewer_opt.excluded_hostnames)
return 1;
ebpf_network_viewer_hostname_list_t *move = network_viewer_opt.excluded_hostnames;
while (move) {
if (simple_pattern_matches(move->value_pattern, cmp))
return 0;
move = move->next;
}
move = network_viewer_opt.included_hostnames;
while (move) {
if (simple_pattern_matches(move->value_pattern, cmp))
return 1;
move = move->next;
}
return 0;
}
/**
* Is socket allowed?
*
* Compare destination addresses and destination ports to define next steps
*
* @param key the socket read from kernel ring
* @param family the family used to compare IPs (AF_INET and AF_INET6)
*
* @return It returns 1 if this socket is inside the ranges and 0 otherwise.
*/
int is_socket_allowed(netdata_socket_idx_t *key, int family)
{
if (!is_port_inside_range(key->dport))
return 0;
return is_specific_ip_inside_range(&key->daddr, family);
}
/**
* Compare sockets
*
* Compare destination address and destination port.
* We do not compare source port, because it is random.
* We also do not compare source address, because inbound and outbound connections are stored in separated AVL trees.
*
* @param a pointer to netdata_socket_plot
* @param b pointer to netdata_socket_plot
*
* @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
*/
static int compare_sockets(void *a, void *b)
{
struct netdata_socket_plot *val1 = a;
struct netdata_socket_plot *val2 = b;
int cmp;
// We do not need to compare val2 family, because data inside hash table is always from the same family
if (val1->family == AF_INET) { //IPV4
if (val1->flags & NETDATA_INBOUND_DIRECTION) {
if (val1->index.sport == val2->index.sport)
cmp = 0;
else {
cmp = (val1->index.sport > val2->index.sport)?1:-1;
}
} else {
cmp = memcmp(&val1->index.dport, &val2->index.dport, sizeof(uint16_t));
if (!cmp) {
cmp = memcmp(&val1->index.daddr.addr32[0], &val2->index.daddr.addr32[0], sizeof(uint32_t));
}
}
} else {
if (val1->flags & NETDATA_INBOUND_DIRECTION) {
if (val1->index.sport == val2->index.sport)
cmp = 0;
else {
cmp = (val1->index.sport > val2->index.sport)?1:-1;
}
} else {
cmp = memcmp(&val1->index.dport, &val2->index.dport, sizeof(uint16_t));
if (!cmp) {
cmp = memcmp(&val1->index.daddr.addr32, &val2->index.daddr.addr32, 4*sizeof(uint32_t));
}
}
}
return cmp;
}
/**
* Build dimension name
*
* Fill dimension name vector with values given
*
* @param dimname the output vector
* @param hostname the hostname for the socket.
* @param service_name the service used to connect.
* @param proto the protocol used in this connection
* @param family is this IPV4(AF_INET) or IPV6(AF_INET6)
*
* @return it returns the size of the data copied on success and -1 otherwise.
*/
static inline int build_outbound_dimension_name(char *dimname, char *hostname, char *service_name,
char *proto, int family)
{
return snprintf(dimname, CONFIG_MAX_NAME - 7, (family == AF_INET)?"%s:%s:%s_":"%s:%s:[%s]_",
service_name, proto,
hostname);
}
/**
* Fill inbound dimension name
*
* Mount the dimension name with the input given
*
* @param dimname the output vector
* @param service_name the service used to connect.
* @param proto the protocol used in this connection
*
* @return it returns the size of the data copied on success and -1 otherwise.
*/
static inline int build_inbound_dimension_name(char *dimname, char *service_name, char *proto)
{
return snprintf(dimname, CONFIG_MAX_NAME - 7, "%s:%s_", service_name,
proto);
}
/**
* Fill Resolved Name
*
* Fill the resolved name structure with the value given.
* The hostname is the largest value possible, if it is necessary to cut some value, it must be cut.
*
* @param ptr the output vector
* @param hostname the hostname resolved or IP.
* @param length the length for the hostname.
* @param service_name the service name associated to the connection
* @param is_outbound the is this an outbound connection
*/
static inline void fill_resolved_name(netdata_socket_plot_t *ptr, char *hostname, size_t length,
char *service_name, int is_outbound)
{
if (length < NETDATA_MAX_NETWORK_COMBINED_LENGTH)
ptr->resolved_name = strdupz(hostname);
else {
length = NETDATA_MAX_NETWORK_COMBINED_LENGTH;
ptr->resolved_name = mallocz( NETDATA_MAX_NETWORK_COMBINED_LENGTH + 1);
memcpy(ptr->resolved_name, hostname, length);
ptr->resolved_name[length] = '\0';
}
char dimname[CONFIG_MAX_NAME];
int size;
char *protocol;
if (ptr->sock.protocol == IPPROTO_UDP) {
protocol = "UDP";
} else if (ptr->sock.protocol == IPPROTO_TCP) {
protocol = "TCP";
} else {
protocol = "ALL";
}
if (is_outbound)
size = build_outbound_dimension_name(dimname, hostname, service_name, protocol, ptr->family);
else
size = build_inbound_dimension_name(dimname,service_name, protocol);
if (size > 0) {
strcpy(&dimname[size], "sent");
dimname[size + 4] = '\0';
ptr->dimension_sent = strdupz(dimname);
strcpy(&dimname[size], "recv");
ptr->dimension_recv = strdupz(dimname);
dimname[size - 1] = '\0';
ptr->dimension_retransmit = strdupz(dimname);
}
}
/**
* Mount dimension names
*
* Fill the vector names after to resolve the addresses
*
* @param ptr a pointer to the structure where the values are stored.
* @param is_outbound is a outbound ptr value?
*
* @return It returns 1 if the name is valid and 0 otherwise.
*/
int fill_names(netdata_socket_plot_t *ptr, int is_outbound)
{
char hostname[NI_MAXHOST], service_name[NI_MAXSERV];
if (ptr->resolved)
return 1;
int ret;
static int resolve_name = -1;