Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # tcpretrans Trace TCP retransmits and TLPs. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
| 6 | # |
| 7 | # USAGE: tcpretrans [-h] [-l] |
| 8 | # |
| 9 | # This uses dynamic tracing of kernel functions, and will need to be updated |
| 10 | # to match kernel changes. |
| 11 | # |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 12 | # Copyright 2016 Netflix, Inc. |
| 13 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 14 | # |
| 15 | # 14-Feb-2016 Brendan Gregg Created this. |
| 16 | |
| 17 | from __future__ import print_function |
| 18 | from bcc import BPF |
| 19 | import argparse |
| 20 | from time import strftime |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 21 | from socket import inet_ntop, AF_INET, AF_INET6 |
| 22 | from struct import pack |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 23 | import ctypes as ct |
| 24 | |
| 25 | # arguments |
| 26 | examples = """examples: |
| 27 | ./tcpretrans # trace TCP retransmits |
| 28 | ./tcpretrans -l # include TLP attempts |
| 29 | """ |
| 30 | parser = argparse.ArgumentParser( |
| 31 | description="Trace TCP retransmits", |
| 32 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 33 | epilog=examples) |
| 34 | parser.add_argument("-l", "--lossprobe", action="store_true", |
| 35 | help="include tail loss probe attempts") |
| 36 | args = parser.parse_args() |
| 37 | |
| 38 | # define BPF program |
| 39 | bpf_text = """ |
| 40 | #include <uapi/linux/ptrace.h> |
| 41 | #include <net/sock.h> |
| 42 | #include <bcc/proto.h> |
| 43 | |
| 44 | #define RETRANSMIT 1 |
| 45 | #define TLP 2 |
| 46 | |
| 47 | // separate data structs for ipv4 and ipv6 |
| 48 | struct ipv4_data_t { |
| 49 | // XXX: switch some to u32's when supported |
| 50 | u64 pid; |
| 51 | u64 ip; |
| 52 | u64 saddr; |
| 53 | u64 daddr; |
| 54 | u64 lport; |
| 55 | u64 dport; |
| 56 | u64 state; |
| 57 | u64 type; |
| 58 | }; |
| 59 | BPF_PERF_OUTPUT(ipv4_events); |
| 60 | |
| 61 | struct ipv6_data_t { |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 62 | u64 pid; |
| 63 | u64 ip; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 64 | unsigned __int128 saddr; |
| 65 | unsigned __int128 daddr; |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 66 | u64 lport; |
| 67 | u64 dport; |
| 68 | u64 state; |
| 69 | u64 type; |
| 70 | }; |
| 71 | BPF_PERF_OUTPUT(ipv6_events); |
| 72 | |
| 73 | static int trace_event(struct pt_regs *ctx, struct sock *sk, int type) |
| 74 | { |
| 75 | if (sk == NULL) |
| 76 | return 0; |
| 77 | u32 pid = bpf_get_current_pid_tgid(); |
| 78 | struct sock *skp = NULL; |
| 79 | bpf_probe_read(&skp, sizeof(skp), &sk); |
| 80 | |
| 81 | // pull in details |
| 82 | u16 family = 0, lport = 0, dport = 0; |
| 83 | char state = 0; |
| 84 | bpf_probe_read(&family, sizeof(family), &skp->__sk_common.skc_family); |
| 85 | bpf_probe_read(&lport, sizeof(lport), &skp->__sk_common.skc_num); |
| 86 | bpf_probe_read(&dport, sizeof(dport), &skp->__sk_common.skc_dport); |
| 87 | bpf_probe_read(&state, sizeof(state), (void *)&skp->__sk_common.skc_state); |
| 88 | |
| 89 | if (family == AF_INET) { |
| 90 | struct ipv4_data_t data4 = {.pid = pid, .ip = 4, .type = type}; |
| 91 | bpf_probe_read(&data4.saddr, sizeof(u32), |
| 92 | &skp->__sk_common.skc_rcv_saddr); |
| 93 | bpf_probe_read(&data4.daddr, sizeof(u32), |
| 94 | &skp->__sk_common.skc_daddr); |
Mark Drayton | 8ba998e | 2016-07-31 17:51:52 +0100 | [diff] [blame] | 95 | // lport is host order |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 96 | data4.lport = lport; |
Mark Drayton | 8ba998e | 2016-07-31 17:51:52 +0100 | [diff] [blame] | 97 | data4.dport = ntohs(dport); |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 98 | data4.state = state; |
| 99 | ipv4_events.perf_submit(ctx, &data4, sizeof(data4)); |
| 100 | |
| 101 | } else if (family == AF_INET6) { |
| 102 | struct ipv6_data_t data6 = {.pid = pid, .ip = 6, .type = type}; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 103 | bpf_probe_read(&data6.saddr, sizeof(data6.saddr), |
| 104 | &skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
| 105 | bpf_probe_read(&data6.daddr, sizeof(data6.daddr), |
| 106 | &skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
Mark Drayton | 8ba998e | 2016-07-31 17:51:52 +0100 | [diff] [blame] | 107 | // lport is host order |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 108 | data6.lport = lport; |
Mark Drayton | 8ba998e | 2016-07-31 17:51:52 +0100 | [diff] [blame] | 109 | data6.dport = ntohs(dport); |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 110 | data6.state = state; |
| 111 | ipv6_events.perf_submit(ctx, &data6, sizeof(data6)); |
| 112 | } |
| 113 | // else drop |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | int trace_retransmit(struct pt_regs *ctx, struct sock *sk) |
| 119 | { |
| 120 | trace_event(ctx, sk, RETRANSMIT); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int trace_tlp(struct pt_regs *ctx, struct sock *sk) |
| 125 | { |
| 126 | trace_event(ctx, sk, TLP); |
| 127 | return 0; |
| 128 | } |
| 129 | """ |
| 130 | |
| 131 | # event data |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 132 | class Data_ipv4(ct.Structure): |
| 133 | _fields_ = [ |
| 134 | ("pid", ct.c_ulonglong), |
| 135 | ("ip", ct.c_ulonglong), |
| 136 | ("saddr", ct.c_ulonglong), |
| 137 | ("daddr", ct.c_ulonglong), |
| 138 | ("lport", ct.c_ulonglong), |
| 139 | ("dport", ct.c_ulonglong), |
| 140 | ("state", ct.c_ulonglong), |
| 141 | ("type", ct.c_ulonglong) |
| 142 | ] |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 143 | |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 144 | class Data_ipv6(ct.Structure): |
| 145 | _fields_ = [ |
| 146 | ("pid", ct.c_ulonglong), |
| 147 | ("ip", ct.c_ulonglong), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 148 | ("saddr", (ct.c_ulonglong * 2)), |
| 149 | ("daddr", (ct.c_ulonglong * 2)), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 150 | ("lport", ct.c_ulonglong), |
| 151 | ("dport", ct.c_ulonglong), |
| 152 | ("state", ct.c_ulonglong), |
| 153 | ("type", ct.c_ulonglong) |
| 154 | ] |
| 155 | |
| 156 | # from bpf_text: |
| 157 | type = {} |
| 158 | type[1] = 'R' |
| 159 | type[2] = 'L' |
| 160 | |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 161 | # from include/net/tcp_states.h: |
| 162 | tcpstate = {} |
| 163 | tcpstate[1] = 'ESTABLISHED' |
| 164 | tcpstate[2] = 'SYN_SENT' |
| 165 | tcpstate[3] = 'SYN_RECV' |
| 166 | tcpstate[4] = 'FIN_WAIT1' |
| 167 | tcpstate[5] = 'FIN_WAIT2' |
| 168 | tcpstate[6] = 'TIME_WAIT' |
| 169 | tcpstate[7] = 'CLOSE' |
| 170 | tcpstate[8] = 'CLOSE_WAIT' |
| 171 | tcpstate[9] = 'LAST_ACK' |
| 172 | tcpstate[10] = 'LISTEN' |
| 173 | tcpstate[11] = 'CLOSING' |
| 174 | tcpstate[12] = 'NEW_SYN_RECV' |
| 175 | |
| 176 | # process event |
| 177 | def print_ipv4_event(cpu, data, size): |
| 178 | event = ct.cast(data, ct.POINTER(Data_ipv4)).contents |
| 179 | print("%-8s %-6d %-2d %-20s %1s> %-20s %s" % ( |
| 180 | strftime("%H:%M:%S"), event.pid, event.ip, |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 181 | "%s:%d" % (inet_ntop(AF_INET, pack('I', event.saddr)), event.lport), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 182 | type[event.type], |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 183 | "%s:%s" % (inet_ntop(AF_INET, pack('I', event.daddr)), event.dport), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 184 | tcpstate[event.state])) |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 185 | |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 186 | def print_ipv6_event(cpu, data, size): |
| 187 | event = ct.cast(data, ct.POINTER(Data_ipv6)).contents |
Ivan Babrou | c862e31 | 2016-06-23 18:11:25 +0100 | [diff] [blame] | 188 | print("%-8s %-6d %-2d %-20s %1s> %-20s %s" % ( |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 189 | strftime("%H:%M:%S"), event.pid, event.ip, |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 190 | "%s:%d" % (inet_ntop(AF_INET6, event.saddr), event.lport), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 191 | type[event.type], |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 192 | "%s:%d" % (inet_ntop(AF_INET6, event.daddr), event.dport), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 193 | tcpstate[event.state])) |
| 194 | |
| 195 | # initialize BPF |
| 196 | b = BPF(text=bpf_text) |
| 197 | b.attach_kprobe(event="tcp_retransmit_skb", fn_name="trace_retransmit") |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 198 | if args.lossprobe: |
| 199 | b.attach_kprobe(event="tcp_send_loss_probe", fn_name="trace_tlp") |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 200 | |
| 201 | # header |
| 202 | print("%-8s %-6s %-2s %-20s %1s> %-20s %-4s" % ("TIME", "PID", "IP", |
| 203 | "LADDR:LPORT", "T", "RADDR:RPORT", "STATE")) |
| 204 | |
| 205 | # read events |
| 206 | b["ipv4_events"].open_perf_buffer(print_ipv4_event) |
| 207 | b["ipv6_events"].open_perf_buffer(print_ipv6_event) |
| 208 | while 1: |
| 209 | b.kprobe_poll() |