blob: fe36fb9bb384df990bae5f58dcf4d4db07271a55 [file] [log] [blame]
Brendan Gregg553f2aa2016-02-14 18:15:24 -08001#!/usr/bin/python
2# @lint-avoid-python-3-compatibility-imports
3#
Brendan Gregg73b54012017-12-18 20:37:11 -08004# tcpretrans Trace or count TCP retransmits and TLPs.
Brendan Gregg553f2aa2016-02-14 18:15:24 -08005# For Linux, uses BCC, eBPF. Embedded C.
6#
Brendan Gregg73b54012017-12-18 20:37:11 -08007# USAGE: tcpretrans [-c] [-h] [-l]
Brendan Gregg553f2aa2016-02-14 18:15:24 -08008#
9# This uses dynamic tracing of kernel functions, and will need to be updated
10# to match kernel changes.
11#
Brendan Gregg553f2aa2016-02-14 18:15:24 -080012# 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 Tafelmeier1e9467f2017-12-13 18:50:22 +010016# 03-Nov-2017 Matthias Tafelmeier Extended this.
Brendan Gregg553f2aa2016-02-14 18:15:24 -080017
18from __future__ import print_function
19from bcc import BPF
20import argparse
21from time import strftime
Mark Drayton11de2982016-06-26 21:14:44 +010022from socket import inet_ntop, AF_INET, AF_INET6
23from struct import pack
Brendan Gregg553f2aa2016-02-14 18:15:24 -080024import ctypes as ct
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +010025from time import sleep
Brendan Gregg553f2aa2016-02-14 18:15:24 -080026
27# arguments
28examples = """examples:
29 ./tcpretrans # trace TCP retransmits
30 ./tcpretrans -l # include TLP attempts
31"""
32parser = argparse.ArgumentParser(
33 description="Trace TCP retransmits",
34 formatter_class=argparse.RawDescriptionHelpFormatter,
35 epilog=examples)
36parser.add_argument("-l", "--lossprobe", action="store_true",
37 help="include tail loss probe attempts")
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +010038parser.add_argument("-c", "--count", action="store_true",
39 help="count occurred retransmits per flow")
Nathan Scottcf0792f2018-02-02 16:56:50 +110040parser.add_argument("--ebpf", action="store_true",
41 help=argparse.SUPPRESS)
Brendan Gregg553f2aa2016-02-14 18:15:24 -080042args = parser.parse_args()
Brendan Gregg73b54012017-12-18 20:37:11 -080043debug = 0
Brendan Gregg553f2aa2016-02-14 18:15:24 -080044
45# define BPF program
46bpf_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
55struct 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};
66BPF_PERF_OUTPUT(ipv4_events);
67
68struct ipv6_data_t {
Brendan Gregg553f2aa2016-02-14 18:15:24 -080069 u64 pid;
70 u64 ip;
Mark Drayton11de2982016-06-26 21:14:44 +010071 unsigned __int128 saddr;
72 unsigned __int128 daddr;
Brendan Gregg553f2aa2016-02-14 18:15:24 -080073 u64 lport;
74 u64 dport;
75 u64 state;
76 u64 type;
77};
78BPF_PERF_OUTPUT(ipv6_events);
79
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +010080// separate flow keys per address family
81struct ipv4_flow_key_t {
82 u32 saddr;
83 u32 daddr;
84 u16 lport;
85 u16 dport;
86};
87BPF_HASH(ipv4_count, struct ipv4_flow_key_t);
88
89struct ipv6_flow_key_t {
90 unsigned __int128 saddr;
91 unsigned __int128 daddr;
92 u16 lport;
93 u16 dport;
94};
95BPF_HASH(ipv6_count, struct ipv6_flow_key_t);
96
Paul Chaignon25212ee2017-08-06 11:15:11 +020097static int trace_event(struct pt_regs *ctx, struct sock *skp, int type)
Brendan Gregg553f2aa2016-02-14 18:15:24 -080098{
Paul Chaignon25212ee2017-08-06 11:15:11 +020099 if (skp == NULL)
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800100 return 0;
101 u32 pid = bpf_get_current_pid_tgid();
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800102
103 // pull in details
Paul Chaignon25212ee2017-08-06 11:15:11 +0200104 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 Gregg553f2aa2016-02-14 18:15:24 -0800108
109 if (family == AF_INET) {
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +0100110 IPV4_INIT
111 IPV4_CORE
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800112 } else if (family == AF_INET6) {
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +0100113 IPV6_INIT
114 IPV6_CORE
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800115 }
116 // else drop
117
118 return 0;
119}
120
121int trace_retransmit(struct pt_regs *ctx, struct sock *sk)
122{
123 trace_event(ctx, sk, RETRANSMIT);
124 return 0;
125}
126
127int trace_tlp(struct pt_regs *ctx, struct sock *sk)
128{
129 trace_event(ctx, sk, TLP);
130 return 0;
131}
132"""
133
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +0100134struct_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
177count_core_base = """
178 u64 zero = 0, *val;
179 val = COUNT_STRUCT.lookup_or_init(&flow_key, &zero);
180 (*val)++;
181"""
182
183if 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'))
188else:
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 Scottcf0792f2018-02-02 16:56:50 +1100194if debug or args.ebpf:
Brendan Gregg73b54012017-12-18 20:37:11 -0800195 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100196 if args.ebpf:
197 exit()
Brendan Gregg73b54012017-12-18 20:37:11 -0800198
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800199# event data
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800200class 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 Drayton11de2982016-06-26 21:14:44 +0100211
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800212class Data_ipv6(ct.Structure):
213 _fields_ = [
214 ("pid", ct.c_ulonglong),
215 ("ip", ct.c_ulonglong),
Mark Drayton11de2982016-06-26 21:14:44 +0100216 ("saddr", (ct.c_ulonglong * 2)),
217 ("daddr", (ct.c_ulonglong * 2)),
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800218 ("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:
225type = {}
226type[1] = 'R'
227type[2] = 'L'
228
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800229# from include/net/tcp_states.h:
230tcpstate = {}
231tcpstate[1] = 'ESTABLISHED'
232tcpstate[2] = 'SYN_SENT'
233tcpstate[3] = 'SYN_RECV'
234tcpstate[4] = 'FIN_WAIT1'
235tcpstate[5] = 'FIN_WAIT2'
236tcpstate[6] = 'TIME_WAIT'
237tcpstate[7] = 'CLOSE'
238tcpstate[8] = 'CLOSE_WAIT'
239tcpstate[9] = 'LAST_ACK'
240tcpstate[10] = 'LISTEN'
241tcpstate[11] = 'CLOSING'
242tcpstate[12] = 'NEW_SYN_RECV'
243
244# process event
245def 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 Drayton11de2982016-06-26 21:14:44 +0100249 "%s:%d" % (inet_ntop(AF_INET, pack('I', event.saddr)), event.lport),
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800250 type[event.type],
Mark Drayton11de2982016-06-26 21:14:44 +0100251 "%s:%s" % (inet_ntop(AF_INET, pack('I', event.daddr)), event.dport),
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800252 tcpstate[event.state]))
Mark Drayton11de2982016-06-26 21:14:44 +0100253
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800254def print_ipv6_event(cpu, data, size):
255 event = ct.cast(data, ct.POINTER(Data_ipv6)).contents
Ivan Babrouc862e312016-06-23 18:11:25 +0100256 print("%-8s %-6d %-2d %-20s %1s> %-20s %s" % (
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800257 strftime("%H:%M:%S"), event.pid, event.ip,
Mark Drayton11de2982016-06-26 21:14:44 +0100258 "%s:%d" % (inet_ntop(AF_INET6, event.saddr), event.lport),
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800259 type[event.type],
Mark Drayton11de2982016-06-26 21:14:44 +0100260 "%s:%d" % (inet_ntop(AF_INET6, event.daddr), event.dport),
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800261 tcpstate[event.state]))
262
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +0100263def 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 Gregg553f2aa2016-02-14 18:15:24 -0800276# initialize BPF
277b = BPF(text=bpf_text)
278b.attach_kprobe(event="tcp_retransmit_skb", fn_name="trace_retransmit")
Mark Drayton11de2982016-06-26 21:14:44 +0100279if args.lossprobe:
280 b.attach_kprobe(event="tcp_send_loss_probe", fn_name="trace_tlp")
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800281
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +0100282print("Tracing retransmits ... Hit Ctrl-C to end")
283if args.count:
284 try:
285 while 1:
286 sleep(99999999)
287 except BaseException:
288 pass
Brendan Gregg553f2aa2016-02-14 18:15:24 -0800289
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +0100290 # 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 Gregg553f2aa2016-02-14 18:15:24 -0800295# read events
Matthias Tafelmeier1e9467f2017-12-13 18:50:22 +0100296else:
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 Qindbf00292018-02-28 21:47:50 -0800303 b.perf_buffer_poll()