Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
Brendan Gregg | 73b5401 | 2017-12-18 20:37:11 -0800 | [diff] [blame] | 4 | # tcpretrans Trace or count TCP retransmits and TLPs. |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 5 | # For Linux, uses BCC, eBPF. Embedded C. |
| 6 | # |
Brendan Gregg | 73b5401 | 2017-12-18 20:37:11 -0800 | [diff] [blame] | 7 | # USAGE: tcpretrans [-c] [-h] [-l] |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 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. |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 16 | # 03-Nov-2017 Matthias Tafelmeier Extended this. |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 17 | |
| 18 | from __future__ import print_function |
| 19 | from bcc import BPF |
| 20 | import argparse |
| 21 | from time import strftime |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 22 | from socket import inet_ntop, AF_INET, AF_INET6 |
| 23 | from struct import pack |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 24 | import ctypes as ct |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 25 | from time import sleep |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 26 | |
| 27 | # arguments |
| 28 | examples = """examples: |
| 29 | ./tcpretrans # trace TCP retransmits |
| 30 | ./tcpretrans -l # include TLP attempts |
| 31 | """ |
| 32 | parser = argparse.ArgumentParser( |
| 33 | description="Trace TCP retransmits", |
| 34 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 35 | epilog=examples) |
| 36 | parser.add_argument("-l", "--lossprobe", action="store_true", |
| 37 | help="include tail loss probe attempts") |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 38 | parser.add_argument("-c", "--count", action="store_true", |
| 39 | help="count occurred retransmits per flow") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 40 | parser.add_argument("--ebpf", action="store_true", |
| 41 | help=argparse.SUPPRESS) |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 42 | args = parser.parse_args() |
Brendan Gregg | 73b5401 | 2017-12-18 20:37:11 -0800 | [diff] [blame] | 43 | debug = 0 |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 44 | |
| 45 | # define BPF program |
| 46 | bpf_text = """ |
| 47 | #include <uapi/linux/ptrace.h> |
| 48 | #include <net/sock.h> |
| 49 | #include <bcc/proto.h> |
| 50 | |
| 51 | #define RETRANSMIT 1 |
| 52 | #define TLP 2 |
| 53 | |
| 54 | // separate data structs for ipv4 and ipv6 |
| 55 | struct ipv4_data_t { |
| 56 | // XXX: switch some to u32's when supported |
| 57 | u64 pid; |
| 58 | u64 ip; |
| 59 | u64 saddr; |
| 60 | u64 daddr; |
| 61 | u64 lport; |
| 62 | u64 dport; |
| 63 | u64 state; |
| 64 | u64 type; |
| 65 | }; |
| 66 | BPF_PERF_OUTPUT(ipv4_events); |
| 67 | |
| 68 | struct ipv6_data_t { |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 69 | u64 pid; |
| 70 | u64 ip; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 71 | unsigned __int128 saddr; |
| 72 | unsigned __int128 daddr; |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 73 | u64 lport; |
| 74 | u64 dport; |
| 75 | u64 state; |
| 76 | u64 type; |
| 77 | }; |
| 78 | BPF_PERF_OUTPUT(ipv6_events); |
| 79 | |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 80 | // separate flow keys per address family |
| 81 | struct ipv4_flow_key_t { |
| 82 | u32 saddr; |
| 83 | u32 daddr; |
| 84 | u16 lport; |
| 85 | u16 dport; |
| 86 | }; |
| 87 | BPF_HASH(ipv4_count, struct ipv4_flow_key_t); |
| 88 | |
| 89 | struct ipv6_flow_key_t { |
| 90 | unsigned __int128 saddr; |
| 91 | unsigned __int128 daddr; |
| 92 | u16 lport; |
| 93 | u16 dport; |
| 94 | }; |
| 95 | BPF_HASH(ipv6_count, struct ipv6_flow_key_t); |
| 96 | |
Paul Chaignon | 25212ee | 2017-08-06 11:15:11 +0200 | [diff] [blame] | 97 | static int trace_event(struct pt_regs *ctx, struct sock *skp, int type) |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 98 | { |
Paul Chaignon | 25212ee | 2017-08-06 11:15:11 +0200 | [diff] [blame] | 99 | if (skp == NULL) |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 100 | return 0; |
| 101 | u32 pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 102 | |
| 103 | // pull in details |
Paul Chaignon | 25212ee | 2017-08-06 11:15:11 +0200 | [diff] [blame] | 104 | u16 family = skp->__sk_common.skc_family; |
| 105 | u16 lport = skp->__sk_common.skc_num; |
| 106 | u16 dport = skp->__sk_common.skc_dport; |
| 107 | char state = skp->__sk_common.skc_state; |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 108 | |
| 109 | if (family == AF_INET) { |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 110 | IPV4_INIT |
| 111 | IPV4_CORE |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 112 | } else if (family == AF_INET6) { |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 113 | IPV6_INIT |
| 114 | IPV6_CORE |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 115 | } |
| 116 | // else drop |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int trace_retransmit(struct pt_regs *ctx, struct sock *sk) |
| 122 | { |
| 123 | trace_event(ctx, sk, RETRANSMIT); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int trace_tlp(struct pt_regs *ctx, struct sock *sk) |
| 128 | { |
| 129 | trace_event(ctx, sk, TLP); |
| 130 | return 0; |
| 131 | } |
| 132 | """ |
| 133 | |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 134 | struct_init = { 'ipv4': |
| 135 | { 'count' : |
| 136 | """ |
| 137 | struct ipv4_flow_key_t flow_key = {}; |
| 138 | flow_key.saddr = skp->__sk_common.skc_rcv_saddr; |
| 139 | flow_key.daddr = skp->__sk_common.skc_daddr; |
| 140 | // lport is host order |
| 141 | flow_key.lport = lport; |
| 142 | flow_key.dport = ntohs(dport);""", |
| 143 | 'trace' : |
| 144 | """ |
| 145 | struct ipv4_data_t data4 = {.pid = pid, .ip = 4, .type = type}; |
| 146 | data4.saddr = skp->__sk_common.skc_rcv_saddr; |
| 147 | data4.daddr = skp->__sk_common.skc_daddr; |
| 148 | // lport is host order |
| 149 | data4.lport = lport; |
| 150 | data4.dport = ntohs(dport); |
| 151 | data4.state = state; """ |
| 152 | }, |
| 153 | 'ipv6': |
| 154 | { 'count' : |
| 155 | """ |
| 156 | struct ipv6_flow_key_t flow_key = {}; |
| 157 | bpf_probe_read(&flow_key.saddr, sizeof(flow_key.saddr), |
| 158 | skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
| 159 | bpf_probe_read(&flow_key.daddr, sizeof(flow_key.daddr), |
| 160 | skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
| 161 | // lport is host order |
| 162 | flow_key.lport = lport; |
| 163 | flow_key.dport = ntohs(dport);""", |
| 164 | 'trace' : """ |
| 165 | struct ipv6_data_t data6 = {.pid = pid, .ip = 6, .type = type}; |
| 166 | bpf_probe_read(&data6.saddr, sizeof(data6.saddr), |
| 167 | skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
| 168 | bpf_probe_read(&data6.daddr, sizeof(data6.daddr), |
| 169 | skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
| 170 | // lport is host order |
| 171 | data6.lport = lport; |
| 172 | data6.dport = ntohs(dport); |
| 173 | data6.state = state;""" |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | count_core_base = """ |
| 178 | u64 zero = 0, *val; |
| 179 | val = COUNT_STRUCT.lookup_or_init(&flow_key, &zero); |
| 180 | (*val)++; |
| 181 | """ |
| 182 | |
| 183 | if args.count: |
| 184 | bpf_text = bpf_text.replace("IPV4_INIT", struct_init['ipv4']['count']) |
| 185 | bpf_text = bpf_text.replace("IPV6_INIT", struct_init['ipv6']['count']) |
| 186 | bpf_text = bpf_text.replace("IPV4_CORE", count_core_base.replace("COUNT_STRUCT", 'ipv4_count')) |
| 187 | bpf_text = bpf_text.replace("IPV6_CORE", count_core_base.replace("COUNT_STRUCT", 'ipv6_count')) |
| 188 | else: |
| 189 | bpf_text = bpf_text.replace("IPV4_INIT", struct_init['ipv4']['trace']) |
| 190 | bpf_text = bpf_text.replace("IPV6_INIT", struct_init['ipv6']['trace']) |
| 191 | bpf_text = bpf_text.replace("IPV4_CORE", "ipv4_events.perf_submit(ctx, &data4, sizeof(data4));") |
| 192 | bpf_text = bpf_text.replace("IPV6_CORE", "ipv6_events.perf_submit(ctx, &data6, sizeof(data6));") |
| 193 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 194 | if debug or args.ebpf: |
Brendan Gregg | 73b5401 | 2017-12-18 20:37:11 -0800 | [diff] [blame] | 195 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 196 | if args.ebpf: |
| 197 | exit() |
Brendan Gregg | 73b5401 | 2017-12-18 20:37:11 -0800 | [diff] [blame] | 198 | |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 199 | # event data |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 200 | class Data_ipv4(ct.Structure): |
| 201 | _fields_ = [ |
| 202 | ("pid", ct.c_ulonglong), |
| 203 | ("ip", ct.c_ulonglong), |
| 204 | ("saddr", ct.c_ulonglong), |
| 205 | ("daddr", ct.c_ulonglong), |
| 206 | ("lport", ct.c_ulonglong), |
| 207 | ("dport", ct.c_ulonglong), |
| 208 | ("state", ct.c_ulonglong), |
| 209 | ("type", ct.c_ulonglong) |
| 210 | ] |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 211 | |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 212 | class Data_ipv6(ct.Structure): |
| 213 | _fields_ = [ |
| 214 | ("pid", ct.c_ulonglong), |
| 215 | ("ip", ct.c_ulonglong), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 216 | ("saddr", (ct.c_ulonglong * 2)), |
| 217 | ("daddr", (ct.c_ulonglong * 2)), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 218 | ("lport", ct.c_ulonglong), |
| 219 | ("dport", ct.c_ulonglong), |
| 220 | ("state", ct.c_ulonglong), |
| 221 | ("type", ct.c_ulonglong) |
| 222 | ] |
| 223 | |
| 224 | # from bpf_text: |
| 225 | type = {} |
| 226 | type[1] = 'R' |
| 227 | type[2] = 'L' |
| 228 | |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 229 | # from include/net/tcp_states.h: |
| 230 | tcpstate = {} |
| 231 | tcpstate[1] = 'ESTABLISHED' |
| 232 | tcpstate[2] = 'SYN_SENT' |
| 233 | tcpstate[3] = 'SYN_RECV' |
| 234 | tcpstate[4] = 'FIN_WAIT1' |
| 235 | tcpstate[5] = 'FIN_WAIT2' |
| 236 | tcpstate[6] = 'TIME_WAIT' |
| 237 | tcpstate[7] = 'CLOSE' |
| 238 | tcpstate[8] = 'CLOSE_WAIT' |
| 239 | tcpstate[9] = 'LAST_ACK' |
| 240 | tcpstate[10] = 'LISTEN' |
| 241 | tcpstate[11] = 'CLOSING' |
| 242 | tcpstate[12] = 'NEW_SYN_RECV' |
| 243 | |
| 244 | # process event |
| 245 | def print_ipv4_event(cpu, data, size): |
| 246 | event = ct.cast(data, ct.POINTER(Data_ipv4)).contents |
| 247 | print("%-8s %-6d %-2d %-20s %1s> %-20s %s" % ( |
| 248 | strftime("%H:%M:%S"), event.pid, event.ip, |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 249 | "%s:%d" % (inet_ntop(AF_INET, pack('I', event.saddr)), event.lport), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 250 | type[event.type], |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 251 | "%s:%s" % (inet_ntop(AF_INET, pack('I', event.daddr)), event.dport), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 252 | tcpstate[event.state])) |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 253 | |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 254 | def print_ipv6_event(cpu, data, size): |
| 255 | event = ct.cast(data, ct.POINTER(Data_ipv6)).contents |
Ivan Babrou | c862e31 | 2016-06-23 18:11:25 +0100 | [diff] [blame] | 256 | print("%-8s %-6d %-2d %-20s %1s> %-20s %s" % ( |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 257 | strftime("%H:%M:%S"), event.pid, event.ip, |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 258 | "%s:%d" % (inet_ntop(AF_INET6, event.saddr), event.lport), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 259 | type[event.type], |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 260 | "%s:%d" % (inet_ntop(AF_INET6, event.daddr), event.dport), |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 261 | tcpstate[event.state])) |
| 262 | |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 263 | def depict_cnt(counts_tab, l3prot='ipv4'): |
| 264 | for k, v in sorted(counts_tab.items(), key=lambda counts: counts[1].value): |
| 265 | depict_key = "" |
| 266 | ep_fmt = "[%s]#%d" |
| 267 | if l3prot == 'ipv4': |
| 268 | depict_key = "%-20s <-> %-20s" % (ep_fmt % (inet_ntop(AF_INET, pack('I', k.saddr)), k.lport), |
| 269 | ep_fmt % (inet_ntop(AF_INET, pack('I', k.daddr)), k.dport)) |
| 270 | else: |
| 271 | depict_key = "%-20s <-> %-20s" % (ep_fmt % (inet_ntop(AF_INET6, k.saddr), k.lport), |
| 272 | ep_fmt % (inet_ntop(AF_INET6, k.daddr), k.dport)) |
| 273 | |
| 274 | print ("%s %10d" % (depict_key, v.value)) |
| 275 | |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 276 | # initialize BPF |
| 277 | b = BPF(text=bpf_text) |
| 278 | b.attach_kprobe(event="tcp_retransmit_skb", fn_name="trace_retransmit") |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 279 | if args.lossprobe: |
| 280 | b.attach_kprobe(event="tcp_send_loss_probe", fn_name="trace_tlp") |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 281 | |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 282 | print("Tracing retransmits ... Hit Ctrl-C to end") |
| 283 | if args.count: |
| 284 | try: |
| 285 | while 1: |
| 286 | sleep(99999999) |
| 287 | except BaseException: |
| 288 | pass |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 289 | |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 290 | # header |
| 291 | print("\n%-25s %-25s %-10s" % ( |
| 292 | "LADDR:LPORT", "RADDR:RPORT", "RETRANSMITS")) |
| 293 | depict_cnt(b.get_table("ipv4_count")) |
| 294 | depict_cnt(b.get_table("ipv6_count"), l3prot='ipv6') |
Brendan Gregg | 553f2aa | 2016-02-14 18:15:24 -0800 | [diff] [blame] | 295 | # read events |
Matthias Tafelmeier | 1e9467f | 2017-12-13 18:50:22 +0100 | [diff] [blame] | 296 | else: |
| 297 | # header |
| 298 | print("%-8s %-6s %-2s %-20s %1s> %-20s %-4s" % ("TIME", "PID", "IP", |
| 299 | "LADDR:LPORT", "T", "RADDR:RPORT", "STATE")) |
| 300 | b["ipv4_events"].open_perf_buffer(print_ipv4_event) |
| 301 | b["ipv6_events"].open_perf_buffer(print_ipv6_event) |
| 302 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 303 | b.perf_buffer_poll() |