| 1 |
|
| 2 |
|
| 3 |
#include "common.h" |
| 4 |
|
| 5 |
#include "bpf_endian.h" |
| 6 |
#include "bpf_tracing.h" |
| 7 |
|
| 8 |
#define AF_INET 2 |
| 9 |
|
| 10 |
char __license[] SEC("license") = "Dual MIT/GPL"; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
struct sock_common { |
| 30 |
union { |
| 31 |
struct { |
| 32 |
|
| 33 |
__be32 skc_daddr; |
| 34 |
|
| 35 |
__be32 skc_rcv_saddr; |
| 36 |
}; |
| 37 |
}; |
| 38 |
union { |
| 39 |
struct { |
| 40 |
|
| 41 |
__be16 skc_dport; |
| 42 |
|
| 43 |
__u16 skc_num; |
| 44 |
}; |
| 45 |
}; |
| 46 |
|
| 47 |
short unsigned int skc_family; |
| 48 |
} __attribute__((preserve_access_index)); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
struct sock { |
| 56 |
struct sock_common __sk_common; |
| 57 |
} __attribute__((preserve_access_index)); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
struct tcp_sock { |
| 65 |
u32 srtt_us; |
| 66 |
} __attribute__((preserve_access_index)); |
| 67 |
|
| 68 |
struct { |
| 69 |
__uint(type, BPF_MAP_TYPE_RINGBUF); |
| 70 |
__uint(max_entries, 1 << 24); |
| 71 |
__type(value, struct event); |
| 72 |
} events SEC(".maps"); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
struct event { |
| 80 |
u16 sport; |
| 81 |
u16 dport; |
| 82 |
u32 saddr; |
| 83 |
u32 daddr; |
| 84 |
u32 srtt; |
| 85 |
}; |
| 86 |
|
| 87 |
SEC("fentry/tcp_close") |
| 88 |
int BPF_PROG(tcp_close, struct sock *sk) { |
| 89 |
if (sk->__sk_common.skc_family != AF_INET) { |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
struct tcp_sock *ts = bpf_skc_to_tcp_sock(sk); |
| 95 |
if (!ts) { |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
struct event *tcp_info; |
| 100 |
tcp_info = bpf_ringbuf_reserve(&events, sizeof(struct event), 0); |
| 101 |
if (!tcp_info) { |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
tcp_info->saddr = sk->__sk_common.skc_rcv_saddr; |
| 106 |
tcp_info->daddr = sk->__sk_common.skc_daddr; |
| 107 |
tcp_info->dport = bpf_ntohs(sk->__sk_common.skc_dport); |
| 108 |
tcp_info->sport = sk->__sk_common.skc_num; |
| 109 |
|
| 110 |
tcp_info->srtt = ts->srtt_us >> 3; |
| 111 |
tcp_info->srtt /= 1000; |
| 112 |
|
| 113 |
bpf_ringbuf_submit(tcp_info, 0); |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |