-
Notifications
You must be signed in to change notification settings - Fork 46
/
render_bpf.c
2543 lines (2142 loc) · 73.5 KB
/
render_bpf.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
#include <uapi/linux/ptrace.h>
#ifndef KBUILD_MODNAME
#define KBUILD_MODNAME "ebpf_net"
#endif
/* include net/sock, ignore the enum-conversion warning */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wenum-conversion"
#pragma clang diagnostic ignored "-Wtautological-compare"
#include <net/sock.h>
#pragma clang diagnostic pop
#include <bcc/proto.h>
#include <linux/delayacct.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/sched.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/version.h>
#pragma passthrough on
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
#include <linux/sched/signal.h>
#endif
#pragma passthrough off
#include <net/flow.h>
#include <net/net_namespace.h>
#include <net/netfilter/nf_conntrack_tuple.h>
#include <net/netfilter/nf_nat.h>
#include <net/tcp.h>
// Configuration
#include "config.h"
#include "render_bpf.h"
// Perf events
BPF_PERF_OUTPUT(events);
#include "ebpf_net/agent_internal/bpf.h"
// Common utility functions
#include "tcp-processor/bpf_debug.h"
#include "tcp-processor/bpf_types.h"
static u64 abs_val(int val)
{
return val < 0 ? -val : val;
}
// using constants for placeholders for readability after code dump
#pragma passthrough on
#define REPORT_DEBUG_EVENTS REPORT_DEBUG_EVENTS_PLACEHOLDER
#pragma passthrough off
/* HELPERS FOR FILLERS */
struct pkts_if_t {
u32 packets_out;
u32 sacked_out;
u32 lost_out;
u32 retrans_out;
};
static inline u32 packets_in_flight_helper(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
struct pkts_if_t t = {};
bpf_probe_read(&t.packets_out, sizeof(u32), &tp->packets_out);
bpf_probe_read(&t.sacked_out, sizeof(u32), &tp->sacked_out);
bpf_probe_read(&t.lost_out, sizeof(u32), &tp->lost_out);
bpf_probe_read(&t.retrans_out, sizeof(u32), &tp->retrans_out);
return t.packets_out - (t.sacked_out + t.lost_out) + t.retrans_out;
}
/**
* Tracking open sockets
*/
struct tcp_open_socket_t {
u32 tgid;
u32 rcv_holes;
u64 last_output;
u64 bytes_received; /* last observed */
u32 rcv_delivered;
u32 padding;
#if TCP_STATS_ON_PARENT
struct sock *parent; /* parent listen socket if accepted, null otherwise */
#endif
};
struct udp_stats_t {
u64 last_output;
u32 laddr6[4];
u32 raddr6[4];
u16 lport;
u16 rport;
u8 addr_changed;
u32 bytes; /* bytes seen on socket since last submit */
u32 packets; /* packets seen since last submit */
u32 drops; /* drops total for socket as of last submit (receive side only) */
};
/**
* Tracking open sockets
*/
struct udp_open_socket_t {
u32 tgid;
struct udp_stats_t stats[2];
};
BPF_HASH(tgid_info_table, TGID, TGID, TABLE_SIZE__TGID_INFO);
BPF_HASH(dead_group_tasks, struct task_struct *, struct task_struct *, TABLE_SIZE__DEAD_GROUP_TASKS);
/* BPF_F_NO_PREALLOC was introduced in 6c9059817432, contained in
* v4.6-rc1~91^2~108^2~6 */
#pragma passthrough on
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
BPF_HASH(seen_inodes, u32, u32, TABLE_SIZE__SEEN_INODES);
#else /* #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) */
BPF_F_TABLE(
/*table_type*/ "hash",
/*key_type*/ u32,
/*value_type*/ u32,
/*name*/ seen_inodes,
/*max_entries*/ TABLE_SIZE__SEEN_INODES,
/*flags*/ BPF_F_NO_PREALLOC);
#endif
#pragma passthrough off
BPF_HASH(
seen_conntracks,
struct nf_conn *,
struct nf_conn *,
TABLE_SIZE__SEEN_CONNTRACKS); // Conntracks that we've reported to userspace
BPF_HASH(tcp_open_sockets, struct sock *, struct tcp_open_socket_t, TABLE_SIZE__TCP_OPEN_SOCKETS); /* information on live sks */
BPF_HASH(udp_open_sockets, struct sock *, struct udp_open_socket_t, TABLE_SIZE__UDP_OPEN_SOCKETS);
BPF_HASH(udp_get_port_hash, u64, struct sock *, TABLE_SIZE__UDP_GET_PORT_HASH);
BEGIN_DECLARE_SAVED_ARGS(cgroup_exit)
pid_t tgid;
END_DECLARE_SAVED_ARGS(cgroup_exit)
/*
* cgroups subsystem used for cgroup probing
* This is used by cgroup related probes to filter out cgroups that aren't in the memory hierarchy.
* See SUBSYS macro in /linux_kernel/kernel/cgroup/cgroup.c.
*/
#pragma passthrough on
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 79)
#define FLOW_CGROUP_SUBSYS mem_cgroup_subsys_id
#else
#define FLOW_CGROUP_SUBSYS memory_cgrp_id
#endif
#pragma passthrough off
/* forward declarations */
static void
perf_check_and_submit_dns(struct pt_regs *ctx, struct sock *sk, struct sk_buff *skb, u8 proto, u16 sport, u16 dport, int is_rx);
////////////////////////////////////////////////////////////////////////////////////
/* PROCESSES */
static int report_pid_exit(TIMESTAMP timestamp, struct pt_regs *ctx, struct task_struct *task)
{
perf_submit_agent_internal__pid_exit(ctx, timestamp, task->tgid, task->pid, task->exit_code);
return 1;
}
static int set_task_group_dead(struct pt_regs *ctx, struct task_struct *tsk)
{
int ret = dead_group_tasks.insert(&tsk, &tsk);
if (ret == -E2BIG || ret == -ENOMEM || ret == -EINVAL) {
#if DEBUG_OTHER_MAP_ERRORS
bpf_trace_printk("set_task_group_dead: set_task_group_dead table is full, dropping tsk tsk=%llx\n", tsk);
#endif
bpf_log(ctx, BPF_LOG_TABLE_FULL, BPF_TABLE_DEAD_GROUP_TASKS, 0, (u64)tsk);
return 0;
} else if (ret == -EEXIST) {
#if DEBUG_OTHER_MAP_ERRORS
bpf_trace_printk("set_task_group_dead: set_task_group_dead duplicate insert, dropping tsk tsk=%llx\n", tsk);
#endif
bpf_log(ctx, BPF_LOG_TABLE_DUPLICATE_INSERT, BPF_TABLE_DEAD_GROUP_TASKS, 0, (u64)tsk);
return 0;
} else if (ret != 0) {
#if DEBUG_OTHER_MAP_ERRORS
bpf_trace_printk("set_task_group_dead: unknown return code from map insert: ret=%d (tsk=%llx)\n", ret, tsk);
#endif
bpf_log(ctx, BPF_LOG_TABLE_BAD_INSERT, BPF_TABLE_DEAD_GROUP_TASKS, 0, abs_val(ret));
return 0;
}
return 1;
}
static int task_group_check_dead_and_remove(struct pt_regs *ctx, struct task_struct *tsk)
{
int ret = dead_group_tasks.delete(&tsk);
if (ret != 0) {
// wasn't in map
return 0;
}
return 1;
}
static int task_is_group_leader(struct pt_regs *ctx, struct task_struct *tsk)
{
int ret;
if (tsk == NULL) {
bpf_log(ctx, BPF_LOG_INVALID_POINTER, 0, 0, 0);
return 0;
}
struct task_struct *group_leader = NULL;
ret = bpf_probe_read(&group_leader, sizeof(group_leader), &tsk->group_leader);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
if (group_leader == NULL) {
bpf_log(ctx, BPF_LOG_INVALID_POINTER, 0, 0, 0);
return 0;
}
if (tsk == group_leader) {
return 1;
}
return 0;
}
static u64 get_task_cgroup(struct pt_regs *ctx, struct task_struct *tsk)
{
int ret;
if (tsk == NULL) {
bpf_log(ctx, BPF_LOG_INVALID_POINTER, 0, 0, 0);
return 0;
}
struct css_set *set = NULL;
ret = bpf_probe_read(&set, sizeof(set), &tsk->cgroups);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
if (set == NULL) {
bpf_log(ctx, BPF_LOG_INVALID_POINTER, 0, 0, 0);
return 0;
}
struct cgroup_subsys_state *css = NULL;
ret = bpf_probe_read(&css, sizeof(css), &set->subsys[FLOW_CGROUP_SUBSYS]);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
if (css == NULL) {
bpf_log(ctx, BPF_LOG_INVALID_POINTER, 0, 0, 0);
return 0;
}
struct cgroup *cgrp = NULL;
ret = bpf_probe_read(&cgrp, sizeof(cgrp), &css->cgroup);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
if (cgrp == NULL) {
bpf_log(ctx, BPF_LOG_INVALID_POINTER, 0, 0, 0);
return 0;
}
return (u64)cgrp;
}
static pid_t get_task_parent(struct pt_regs *ctx, struct task_struct *tsk)
{
int ret = 0;
struct task_struct *parent_tsk = NULL;
ret = bpf_probe_read(&parent_tsk, sizeof(parent_tsk), &tsk->parent);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return -1;
}
pid_t parent_tgid = 0;
ret = bpf_probe_read(&parent_tgid, sizeof(parent_tgid), &parent_tsk->tgid);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return -1;
}
return parent_tgid;
}
// insert_tgid_info:
// adds a task to the 'tgid_info' table
// returns 0 if the task was not inserted because it was a duplicate or table full
// returns 1 if a task is new and was inserted
static int insert_tgid_info(struct pt_regs *ctx, TGID tgid)
{
int ret = tgid_info_table.insert(&tgid, &tgid);
if (ret == -E2BIG || ret == -ENOMEM || ret == -EINVAL) {
#if DEBUG_OTHER_MAP_ERRORS
bpf_trace_printk("insert_tgid_info: tgid_info table is full, dropping task tgid=%u\n", tgid);
#endif
bpf_log(ctx, BPF_LOG_TABLE_FULL, BPF_TABLE_TGID_INFO, tgid, tgid);
return 0;
} else if (ret == -EEXIST) {
// if we've already seen it, then don't make a duplicate msg
// could arise if a new task shows up during the initial scan
// or this is a thread inside an existing group, so we're seeing the tgid again
return 0;
} else if (ret != 0) {
#if DEBUG_OTHER_MAP_ERRORS
bpf_trace_printk("insert_tgid_info: unknown return code from map insert: ret=%d (tgid=%u)\n", ret, tgid);
#endif
bpf_log(ctx, BPF_LOG_TABLE_BAD_INSERT, BPF_TABLE_TGID_INFO, tgid, abs_val(ret));
return 0;
}
return 1;
}
// remove_tgid_info:
// removes a task to the 'tgid_info' table
// returns 0 if the tgid was not found in the table or if there was an error
// returns 1 if a task was removed successfully
static int remove_tgid_info(struct pt_regs *ctx, TGID tgid)
{
int ret = tgid_info_table.delete(&tgid);
if (ret != 0) {
#if DEBUG_OTHER_MAP_ERRORS
bpf_trace_printk("remove_tgid_info: can't remove missing tgid=%u\n", tgid);
#endif
if (ret != -ENOENT) {
bpf_log(ctx, BPF_LOG_TABLE_BAD_REMOVE, BPF_TABLE_TGID_INFO, tgid, abs_val(ret));
}
return 0;
}
return 1;
}
// note is the tgid is dead or not
// used later by on_cgroup_exit handling
int on_taskstats_exit(struct pt_regs *ctx, struct task_struct *tsk, int group_dead)
{
if (group_dead) {
set_task_group_dead(ctx, tsk);
}
return 0;
}
// end
// this routine is called by 'do_exit' near the end of the destruction of a task
// but after all of the resources has been cleaned up, including file descriptor references
int on_cgroup_exit(struct pt_regs *ctx, struct task_struct *tsk)
{
int ret;
pid_t tgid = 0;
ret = bpf_probe_read(&tgid, sizeof(tgid), &(tsk->tgid));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
// only consider tasks that are the last task in the thread group
// since the process will not be terminating if there are more threads left
if (!task_group_check_dead_and_remove(ctx, tsk)) {
return 0;
}
GET_PID_TGID;
BEGIN_SAVE_ARGS(cgroup_exit)
SAVE_ARG(tgid)
END_SAVE_ARGS(cgroup_exit)
return 0;
}
int onret_cgroup_exit(struct pt_regs *ctx)
{
GET_PID_TGID;
GET_ARGS_MISSING_OK(cgroup_exit, args)
if (args == NULL) {
return 0;
}
pid_t tgid = args->tgid;
DELETE_ARGS(cgroup_exit);
// do some cleanup from our hashmap
// if we didn't record this tgid before, a log message will have
// fired, and won't send this along to the server
if (!remove_tgid_info(ctx, tgid)) {
return 0;
}
u64 now = get_timestamp();
char comm[16] = {};
bpf_get_current_comm(comm, sizeof(comm));
perf_submit_agent_internal__pid_close(ctx, now, tgid, comm);
return 0;
}
// set_task_comm: notice when command line is set for a process
int on_set_task_comm(struct pt_regs *ctx, struct task_struct *tsk, const char *buf)
{
int ret;
// only interested tasks considered the 'leader' of the group,
// effectively userland processes, not threads
if (!task_is_group_leader(ctx, tsk)) {
return 0;
}
u64 now = get_timestamp();
pid_t tgid = 0;
ret = bpf_probe_read(&tgid, sizeof(tgid), &tsk->tgid);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
perf_submit_agent_internal__pid_set_comm(ctx, now, tgid, (uint8_t *)buf);
return 0;
}
// start
int on_wake_up_new_task(struct pt_regs *ctx, struct task_struct *tsk)
{
int ret;
pid_t tgid = 0;
ret = bpf_probe_read(&tgid, sizeof(tgid), &tsk->tgid);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
// mark the thread group id as seen, and if we've already seen it, return
if (!insert_tgid_info(ctx, tgid)) {
return 0;
}
u64 now = get_timestamp();
u64 cgroup = get_task_cgroup(ctx, tsk);
pid_t parent_tgid = get_task_parent(ctx, tsk);
u8 comm[16] = {};
ret = bpf_probe_read(comm, sizeof(comm), tsk->comm);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
perf_submit_agent_internal__pid_info(ctx, now, tgid, comm, cgroup, parent_tgid);
return 0;
}
// existing
int onret_get_pid_task(struct pt_regs *ctx)
{
int ret;
struct task_struct *tsk = (struct task_struct *)PT_REGS_RC(ctx);
pid_t tgid = 0;
ret = bpf_probe_read(&tgid, sizeof(tgid), &tsk->tgid);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), (u64)tsk, 0);
return 0;
}
// mark the task as seen, and if we've already seen it, return
if (!insert_tgid_info(ctx, tgid)) {
return 0;
}
u64 now = get_timestamp();
u64 cgroup = get_task_cgroup(ctx, tsk);
pid_t parent_tgid = get_task_parent(ctx, tsk);
u8 comm[16] = {};
ret = bpf_probe_read(comm, sizeof(comm), tsk->comm);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return 0;
}
perf_submit_agent_internal__pid_info(ctx, now, tgid, comm, cgroup, parent_tgid);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////
/* TCP SOCKETS */
static inline u32 tcp_get_delivered(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
/* delivered accounting was introduced in ddf1af6fa00e77, i.e.,
* v4.6-rc1~91^2~316^2~3 */
#pragma passthrough on
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 6, 0)
u32 packets_out = 0;
u32 sacked_out = 0;
bpf_probe_read(&packets_out, sizeof(packets_out), &tp->packets_out);
bpf_probe_read(&sacked_out, sizeof(sacked_out), &tp->sacked_out);
return packets_out - sacked_out;
#else
u32 delivered = 0;
bpf_probe_read(&delivered, sizeof(delivered), &tp->delivered);
return delivered;
#endif
#pragma passthrough off
}
static inline void
report_rtt_estimator(struct pt_regs *ctx, struct sock *sk, struct tcp_open_socket_t *sk_info, u64 now, bool adjust)
{
int ret;
sk_info->last_output = now; // update the time in place
u32 rcv_rtt_us = 0;
#pragma passthrough on
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
bpf_probe_read(&rcv_rtt_us, sizeof(rcv_rtt_us), &tcp_sk(sk)->rcv_rtt_est.rtt);
#else /* #if LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0) */
bpf_probe_read(&rcv_rtt_us, sizeof(rcv_rtt_us), &tcp_sk(sk)->rcv_rtt_est.rtt_us);
#endif
#pragma passthrough off
// These values need to be taken from bpf_probe_read
u32 srtt = 0;
u32 snd_cwnd = 0;
u64 bytes_acked = 0;
u8 ca_state = 0;
u32 packets_retrans = 0;
u64 bytes_received = 0;
ret = bpf_probe_read(&srtt, sizeof(srtt), &(tcp_sk(sk)->srtt_us));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
ret = bpf_probe_read(&snd_cwnd, sizeof(snd_cwnd), &(tcp_sk(sk)->snd_cwnd));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
ret = bpf_probe_read(&bytes_acked, sizeof(bytes_acked), &(tcp_sk(sk)->bytes_acked));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
ret = bpf_probe_read(&ca_state, sizeof(ca_state), &(*(&(inet_csk(sk)->icsk_sync_mss) + 1)));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
ret = bpf_probe_read(&packets_retrans, sizeof(packets_retrans), &(tcp_sk(sk)->total_retrans));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
ret = bpf_probe_read(&bytes_received, sizeof(bytes_received), &(tcp_sk(sk)->bytes_received));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
// adjustment for bytes reported when the connection is closed
// zero out last two bits to account for this
if (adjust) {
bytes_acked &= ~3ull;
bytes_received &= ~3ull;
}
perf_submit_agent_internal__rtt_estimator(
ctx,
now,
srtt,
snd_cwnd,
bytes_acked,
ca_state,
(__u64)sk,
packets_in_flight_helper(sk),
tcp_get_delivered(sk),
packets_retrans,
sk_info->rcv_holes,
bytes_received,
sk_info->rcv_delivered,
rcv_rtt_us);
}
// add tcp_open_socket
//
// should be paired with a `perf_submit_agent_internal__new_sock_created`
// in most circumstances, however we don't do this in the case of already
// existing sockets.
//
// returns 1 if it added the socket, 0 if it already existed,
// and -1 if the table is full, broken, or out of memory
// this operation is atomic and not susceptible to race conditions, because
// map.insert is atomic
static int add_tcp_open_socket(struct pt_regs *ctx, struct sock *sk, u32 tgid, u64 now, struct sock *parent)
{
int ret;
#if TRACE_TCP_SOCKETS
bpf_trace_printk("add_tcp_open_socket: %llx (tgid=%u)\n", sk, tgid);
#endif
struct tcp_open_socket_t sk_info = {
.tgid = tgid,
.last_output = 0, // always do the first reporting
.rcv_holes = 0,
.rcv_delivered = 0,
#if TCP_STATS_ON_PARENT
.parent = parent
#endif
};
ret = bpf_probe_read(&sk_info.bytes_received, sizeof(sk_info.bytes_received), &tcp_sk(sk)->bytes_received);
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return -1;
}
ret = tcp_open_sockets.insert(&sk, &sk_info);
if (ret == -E2BIG || ret == -ENOMEM || ret == -EINVAL) {
#if DEBUG_TCP_SOCKET_ERRORS
bpf_trace_printk("add_tcp_open_socket: tcp_open_sockets table is full, dropping socket sk=%llx (tgid=%u)\n", sk, tgid);
#endif
bpf_log(ctx, BPF_LOG_TABLE_FULL, BPF_TABLE_TCP_OPEN_SOCKETS, tgid, (u64)sk);
return -1;
} else if (ret == -EEXIST) {
return 0;
} else if (ret != 0) {
#if DEBUG_TCP_SOCKET_ERRORS
bpf_trace_printk("add_tcp_open_socket: unknown return code from map insert: ret=%d sk=%llx (tgid=%u)\n", ret, sk, tgid);
#endif
bpf_log(ctx, BPF_LOG_TABLE_BAD_INSERT, BPF_TABLE_TCP_OPEN_SOCKETS, tgid, abs_val(ret));
return -1;
}
return 1;
}
static void remove_tcp_open_socket(struct pt_regs *ctx, struct sock *sk)
{
struct tcp_open_socket_t *sk_info_p;
sk_info_p = tcp_open_sockets.lookup(&sk);
if (!sk_info_p) {
return;
}
#if TRACE_TCP_SOCKETS
bpf_trace_printk("remove_tcp_open_socket: %llx\n", sk);
#endif
// The lookup was successful. Make a copy before deleting the entry, and only send related telemetry if the delete is
// successful.
struct tcp_open_socket_t sk_info = *sk_info_p;
// do some cleanup from our hashmap
int ret = tcp_open_sockets.delete(&sk);
if (ret != 0) {
// Another thread must have already deleted the entry for this sk.
return;
}
// always report last rtt estimator before close
// for short-lived connections, we won't see any data otherwise
u64 now = get_timestamp();
report_rtt_estimator(ctx, sk, &sk_info, now, true);
perf_submit_agent_internal__close_sock_info(ctx, now, (__u64)sk);
}
static inline void submit_set_state_ipv6(struct pt_regs *ctx, u64 now, int tx_rx, struct sock *sk)
{
struct sock *skp = NULL;
bpf_probe_read(&skp, sizeof(skp), &sk);
if (!skp) {
bpf_log(ctx, BPF_LOG_INVALID_POINTER, 0, 0, 0);
return;
}
u16 dport = 0;
u16 sport = 0;
uint8_t daddr[16] = {};
uint8_t saddr[16] = {};
// These values need to be taken from bpf_probe_read
bpf_probe_read(&dport, sizeof(dport), &(skp->sk_dport));
bpf_probe_read(&sport, sizeof(sport), &(skp->sk_num));
bpf_probe_read(daddr, sizeof(daddr), (uint8_t *)(sk->sk_v6_daddr.in6_u.u6_addr32));
bpf_probe_read(saddr, sizeof(saddr), (uint8_t *)(sk->sk_v6_rcv_saddr.in6_u.u6_addr32));
perf_submit_agent_internal__set_state_ipv6(ctx, now, daddr, saddr, ntohs(dport), sport, (__u64)sk, tx_rx);
}
// state - we want to get the 5-tuple as early as possible.
static inline void submit_set_state_ipv4(struct pt_regs *ctx, u64 now, int tx_rx, struct sock *sk)
{
struct sock *skp = NULL;
bpf_probe_read(&skp, sizeof(skp), &sk);
if (!skp) {
bpf_log(ctx, BPF_LOG_INVALID_POINTER, 0, 0, 0);
return;
}
u16 dport = 0;
u16 sport = 0;
// These values need to be taken from bpf_probe_read
bpf_probe_read(&dport, sizeof(dport), &(skp->sk_dport));
bpf_probe_read(&sport, sizeof(sport), &(skp->sk_num));
perf_submit_agent_internal__set_state_ipv4(ctx, now, sk->sk_daddr, sk->sk_rcv_saddr, ntohs(dport), sport, (__u64)sk, tx_rx);
}
static inline void submit_reset_tcp_counters(struct pt_regs *ctx, u64 now, u64 pid, struct sock *sk)
{
int ret;
u64 bytes_acked = 0;
u32 packets_retrans = 0;
u64 bytes_received = 0;
ret = bpf_probe_read(&bytes_acked, sizeof(bytes_acked), &(tcp_sk(sk)->bytes_acked));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
ret = bpf_probe_read(&packets_retrans, sizeof(packets_retrans), &(tcp_sk(sk)->total_retrans));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
ret = bpf_probe_read(&bytes_received, sizeof(bytes_received), &(tcp_sk(sk)->bytes_received));
if (ret != 0) {
bpf_log(ctx, BPF_LOG_BPF_CALL_FAILED, abs_val(ret), 0, 0);
return;
}
perf_submit_agent_internal__reset_tcp_counters(
ctx, now, (__u64)sk, bytes_acked, tcp_get_delivered(sk), packets_retrans, bytes_received, pid);
}
// common logic for handling existing sockets
static int ensure_tcp_existing(struct pt_regs *ctx, struct sock *sk, u32 pid)
{
if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6) {
return -1;
}
u64 now = get_timestamp();
int tx_rx = 0;
if (sk->sk_state == TCP_LISTEN)
tx_rx = 2;
int ret = add_tcp_open_socket(ctx, sk, pid, now, NULL);
if (ret == 1) {
submit_reset_tcp_counters(ctx, now, pid, sk);
if (sk->sk_family == AF_INET6) {
submit_set_state_ipv6(ctx, now, tx_rx, sk);
} else {
submit_set_state_ipv4(ctx, now, tx_rx, sk);
}
}
return ret;
}
//
// HACK: Add tcp sockets that should exist to the table but don't
//
#if TCP_EXISTING_HACK
static struct tcp_open_socket_t *tcp_existing_hack(struct pt_regs *ctx, struct sock *sk)
{
// Can only do this hack if we are in a userland process context
PID_TGID _pid_tgid = bpf_get_current_pid_tgid();
TGID _tgid = TGID_FROM_PID_TGID(_pid_tgid);
PID _pid = PID_FROM_PID_TGID(_pid_tgid);
if (_tgid == 0 || _pid == 0) {
return NULL;
}
// ensure the tcp socket exists
int ret = ensure_tcp_existing(ctx, sk, _tgid);
#if DEBUG_TCP_SOCKET_ERRORS
if (ret == 1) {
bpf_trace_printk(
"tcp_update_stats: add_tcp_open_socket of missing socket: %llx (tgid=%u, cpu=%u)\n",
sk,
_tgid,
bpf_get_smp_processor_id());
bpf_log(ctx, BPF_LOG_EXISTING_HACK, BPF_TABLE_TCP_OPEN_SOCKETS, (u64)sk, 0);
} else if (ret == 0) {
// already existing, okay
} else if (ret < 0) {
bpf_trace_printk("tcp_update_stats: add_tcp_open_socket failed: %llx (tgid=%u)\n", sk, _tgid);
}
#endif
struct tcp_open_socket_t *sk_info_p = tcp_open_sockets.lookup(&sk);
return sk_info_p;
}
#endif
// Ends a tcp socket lifetime and starts a new one
// Prep for Ticket #1166
static void restart_tcp_socket(struct pt_regs *ctx, TIMESTAMP now, struct sock *sk)
{
GET_PID_TGID;
// Connect to af_unspec starts a new span
remove_tcp_open_socket(ctx, sk);
// add an entry to our hash map
int ret = add_tcp_open_socket(ctx, sk, _tgid, now, NULL);
if (ret == 1) {
perf_submit_agent_internal__new_sock_created(ctx, now, _tgid, (__u64)sk);
} else if (ret == 0) {
#if DEBUG_TCP_SOCKET_ERRORS
bpf_trace_printk("tcp_init_sock: add_tcp_open_socket of existing socket: %llx (tgid=%u)\n", sk, _tgid);
#endif
bpf_log(ctx, BPF_LOG_TABLE_DUPLICATE_INSERT, BPF_TABLE_TCP_OPEN_SOCKETS, _tgid, (u64)sk);
} else {
#if DEBUG_TCP_SOCKET_ERRORS
bpf_trace_printk("tcp_init_sock: add_tcp_open_socket failed: %llx (tgid=%u)\n", sk, _tgid);
#endif
bpf_log(ctx, BPF_LOG_TABLE_BAD_INSERT, BPF_TABLE_TCP_OPEN_SOCKETS, _tgid, abs_val(ret));
}
}
// connectors
int on_tcp_connect(struct pt_regs *ctx, struct sock *sk)
{
struct tcp_open_socket_t *sk_info;
sk_info = tcp_open_sockets.lookup(&sk);
if (!sk_info) {
#if TCP_EXISTING_HACK
sk_info = tcp_existing_hack(ctx, sk);
if (sk_info == NULL) {
return 0;
}
#else
bpf_log(ctx, BPF_LOG_TABLE_MISSING_KEY, BPF_TABLE_TCP_OPEN_SOCKETS, (u64)sk, 0);
return 0;
#endif
}
u64 now = get_timestamp();
int tx_rx = 1;
u16 family = sk->sk_family;
if (family == AF_INET) {
submit_set_state_ipv4(ctx, now, tx_rx, sk);
} else if (family == AF_INET6) {
submit_set_state_ipv6(ctx, now, tx_rx, sk);
} else {
bpf_log(ctx, BPF_LOG_UNREACHABLE, 0, 0, 0);
}
return 0;
}
// listeners (acceptors)
// XXX: this function might fail so maybe we want to do some sort of kretprobe?
// I assume that if it fails, the user will handle things in a clever way
// and destroy the socket. But who knows?
int on_inet_csk_listen_start(struct pt_regs *ctx, struct sock *sk)
{
// filter out non-tcp connections
u16 family = sk->sk_family;
if (family != AF_INET && family != AF_INET6)
return 0;
struct tcp_open_socket_t *sk_info;
sk_info = tcp_open_sockets.lookup(&sk);
if (!sk_info) {
#if TCP_EXISTING_HACK
sk_info = tcp_existing_hack(ctx, sk);
if (sk_info == NULL) {
return 0;
}
#else
bpf_log(ctx, BPF_LOG_TABLE_MISSING_KEY, BPF_TABLE_TCP_OPEN_SOCKETS, (u64)sk, 0);
return 0;
#endif
}
u64 now = get_timestamp();
int tx_rx = 2; // this is the listener
if (family == AF_INET) {
submit_set_state_ipv4(ctx, now, tx_rx, sk);
} else if (family == AF_INET6) {
submit_set_state_ipv6(ctx, now, tx_rx, sk);
} else {
bpf_log(ctx, BPF_LOG_UNREACHABLE, 0, 0, 0);
}
return 0;
}
#if TCP_LIFETIME_HACK
//
// HACK: Remove open socket from table if it exists
//
static void tcp_lifetime_hack(struct pt_regs *ctx, struct sock *sk)
{
struct tcp_open_socket_t *sk_info;
sk_info = tcp_open_sockets.lookup(&sk);
if (sk_info) {
#if DEBUG_TCP_SOCKET_ERRORS
bpf_trace_printk("tcp_lifetime_hack: tcp_lifetime_hack, sk=%llx, cpu=%u\n", sk, bpf_get_smp_processor_id());
bpf_log(ctx, BPF_LOG_LIFETIME_HACK, BPF_TABLE_TCP_OPEN_SOCKETS, (u64)sk, 0);
#endif
#if REPORT_DEBUG_EVENTS
u64 now = get_timestamp();
perf_submit_agent_internal__report_debug_event(ctx, now, TCP_LIFETIME_HACK_CODE, (u64)sk, 0, 0, 0);
#endif // REPORT_DEBUG_EVENTS
remove_tcp_open_socket(ctx, sk);
}
}
#endif
// --- tcp_init_sock ----------------------------------------------------
// Where the start of TCP socket lifetimes is for IPv4 and IPv6
int on_tcp_init_sock(struct pt_regs *ctx, struct sock *sk)
{
GET_PID_TGID;
u64 now = get_timestamp();
#if TCP_LIFETIME_HACK
// remove the entry from our hash map if it's there
tcp_lifetime_hack(ctx, sk);
#endif
// add an entry to our hash map
int ret = add_tcp_open_socket(ctx, sk, _tgid, now, NULL);
if (ret == 1) {
perf_submit_agent_internal__new_sock_created(ctx, now, _tgid, (__u64)sk);
} else if (ret == 0) {
#if DEBUG_TCP_SOCKET_ERRORS
bpf_trace_printk("tcp_init_sock: add_tcp_open_socket of existing socket: %llx (tgid=%u)\n", sk, _tgid);
#endif
bpf_log(ctx, BPF_LOG_TABLE_DUPLICATE_INSERT, BPF_TABLE_TCP_OPEN_SOCKETS, _tgid, (u64)sk);
} else {
#if DEBUG_TCP_SOCKET_ERRORS
bpf_trace_printk("tcp_init_sock: add_tcp_open_socket failed: %llx (tgid=%u)\n", sk, _tgid);
#endif
bpf_log(ctx, BPF_LOG_TABLE_BAD_INSERT, BPF_TABLE_TCP_OPEN_SOCKETS, _tgid, abs_val(ret));
}
return 0;
}
// --- inet_csk_accept --------------------------------------------------
// Called when a listen socket accepts gets a connection
BEGIN_DECLARE_SAVED_ARGS(on_inet_csk_accept)
struct sock *sk;
int flags;
u32 _pad_0; // required alignment for bcc
int *err;
END_DECLARE_SAVED_ARGS(on_inet_csk_accept)
#pragma passthrough on // Let BCC process this #if
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
int on_inet_csk_accept(struct pt_regs *ctx, struct sock *sk, int flags, int *err, bool kern)