Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # tcpconnect Trace TCP connect()s. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 6 | # |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 7 | # USAGE: tcpconnect [-h] [-t] [-p PID] [-P PORT [PORT ...]] |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 8 | # |
| 9 | # All connection attempts are traced, even if they ultimately fail. |
| 10 | # |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 11 | # This uses dynamic tracing of kernel functions, and will need to be updated |
| 12 | # to match kernel changes. |
| 13 | # |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 14 | # Copyright (c) 2015 Brendan Gregg. |
| 15 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 16 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 17 | # 25-Sep-2015 Brendan Gregg Created this. |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 18 | # 14-Feb-2016 " " Switch to bpf_perf_output. |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 19 | |
| 20 | from __future__ import print_function |
| 21 | from bcc import BPF |
| 22 | import argparse |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 23 | from socket import inet_ntop, ntohs, AF_INET, AF_INET6 |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 24 | from struct import pack |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 25 | import ctypes as ct |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 26 | |
| 27 | # arguments |
| 28 | examples = """examples: |
| 29 | ./tcpconnect # trace all TCP connect()s |
| 30 | ./tcpconnect -t # include timestamps |
| 31 | ./tcpconnect -p 181 # only trace PID 181 |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 32 | ./tcpconnect -P 80 # only trace port 80 |
| 33 | ./tcpconnect -P 80,81 # only trace port 80 and 81 |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 34 | """ |
| 35 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 36 | description="Trace TCP connects", |
| 37 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 38 | epilog=examples) |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 39 | parser.add_argument("-t", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 40 | help="include timestamp on output") |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 41 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 42 | help="trace this PID only") |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 43 | parser.add_argument("-P", "--port", |
| 44 | help="comma-separated list of destination ports to trace.") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 45 | parser.add_argument("--ebpf", action="store_true", |
| 46 | help=argparse.SUPPRESS) |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 47 | args = parser.parse_args() |
| 48 | debug = 0 |
| 49 | |
| 50 | # define BPF program |
| 51 | bpf_text = """ |
| 52 | #include <uapi/linux/ptrace.h> |
| 53 | #include <net/sock.h> |
| 54 | #include <bcc/proto.h> |
| 55 | |
| 56 | BPF_HASH(currsock, u32, struct sock *); |
| 57 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 58 | // separate data structs for ipv4 and ipv6 |
| 59 | struct ipv4_data_t { |
| 60 | // XXX: switch some to u32's when supported |
| 61 | u64 ts_us; |
| 62 | u64 pid; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 63 | u64 saddr; |
| 64 | u64 daddr; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 65 | u64 ip; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 66 | u64 dport; |
| 67 | char task[TASK_COMM_LEN]; |
| 68 | }; |
| 69 | BPF_PERF_OUTPUT(ipv4_events); |
| 70 | |
| 71 | struct ipv6_data_t { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 72 | u64 ts_us; |
| 73 | u64 pid; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 74 | unsigned __int128 saddr; |
| 75 | unsigned __int128 daddr; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 76 | u64 ip; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 77 | u64 dport; |
| 78 | char task[TASK_COMM_LEN]; |
| 79 | }; |
| 80 | BPF_PERF_OUTPUT(ipv6_events); |
| 81 | |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 82 | int trace_connect_entry(struct pt_regs *ctx, struct sock *sk) |
| 83 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 84 | u32 pid = bpf_get_current_pid_tgid(); |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 85 | FILTER_PID |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 86 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 87 | // stash the sock ptr for lookup on return |
| 88 | currsock.update(&pid, &sk); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 89 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 90 | return 0; |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static int trace_connect_return(struct pt_regs *ctx, short ipver) |
| 94 | { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 95 | int ret = PT_REGS_RC(ctx); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 96 | u32 pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 97 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 98 | struct sock **skpp; |
| 99 | skpp = currsock.lookup(&pid); |
| 100 | if (skpp == 0) { |
| 101 | return 0; // missed entry |
| 102 | } |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 103 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 104 | if (ret != 0) { |
| 105 | // failed to send SYNC packet, may not have populated |
| 106 | // socket __sk_common.{skc_rcv_saddr, ...} |
| 107 | currsock.delete(&pid); |
| 108 | return 0; |
| 109 | } |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 110 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 111 | // pull in details |
| 112 | struct sock *skp = *skpp; |
Paul Chaignon | eae0acf | 2017-08-05 23:04:41 +0200 | [diff] [blame] | 113 | u16 dport = skp->__sk_common.skc_dport; |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 114 | |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 115 | FILTER_PORT |
| 116 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 117 | if (ipver == 4) { |
| 118 | struct ipv4_data_t data4 = {.pid = pid, .ip = ipver}; |
| 119 | data4.ts_us = bpf_ktime_get_ns() / 1000; |
Paul Chaignon | eae0acf | 2017-08-05 23:04:41 +0200 | [diff] [blame] | 120 | data4.saddr = skp->__sk_common.skc_rcv_saddr; |
| 121 | data4.daddr = skp->__sk_common.skc_daddr; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 122 | data4.dport = ntohs(dport); |
| 123 | bpf_get_current_comm(&data4.task, sizeof(data4.task)); |
| 124 | ipv4_events.perf_submit(ctx, &data4, sizeof(data4)); |
| 125 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 126 | } else /* 6 */ { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 127 | struct ipv6_data_t data6 = {.pid = pid, .ip = ipver}; |
| 128 | data6.ts_us = bpf_ktime_get_ns() / 1000; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 129 | bpf_probe_read(&data6.saddr, sizeof(data6.saddr), |
Paul Chaignon | eae0acf | 2017-08-05 23:04:41 +0200 | [diff] [blame] | 130 | skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 131 | bpf_probe_read(&data6.daddr, sizeof(data6.daddr), |
Paul Chaignon | eae0acf | 2017-08-05 23:04:41 +0200 | [diff] [blame] | 132 | skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 133 | data6.dport = ntohs(dport); |
| 134 | bpf_get_current_comm(&data6.task, sizeof(data6.task)); |
| 135 | ipv6_events.perf_submit(ctx, &data6, sizeof(data6)); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 136 | } |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 137 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 138 | currsock.delete(&pid); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 139 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 140 | return 0; |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | int trace_connect_v4_return(struct pt_regs *ctx) |
| 144 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 145 | return trace_connect_return(ctx, 4); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int trace_connect_v6_return(struct pt_regs *ctx) |
| 149 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 150 | return trace_connect_return(ctx, 6); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 151 | } |
| 152 | """ |
| 153 | |
| 154 | # code substitutions |
| 155 | if args.pid: |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 156 | bpf_text = bpf_text.replace('FILTER_PID', |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 157 | 'if (pid != %s) { return 0; }' % args.pid) |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 158 | if args.port: |
| 159 | dports = [int(dport) for dport in args.port.split(',')] |
| 160 | dports_if = ' && '.join(['dport != %d' % ntohs(dport) for dport in dports]) |
| 161 | bpf_text = bpf_text.replace('FILTER_PORT', |
| 162 | 'if (%s) { currsock.delete(&pid); return 0; }' % dports_if) |
| 163 | |
| 164 | bpf_text = bpf_text.replace('FILTER_PID', '') |
| 165 | bpf_text = bpf_text.replace('FILTER_PORT', '') |
| 166 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 167 | if debug or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 168 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 169 | if args.ebpf: |
| 170 | exit() |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 171 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 172 | # event data |
| 173 | TASK_COMM_LEN = 16 # linux/sched.h |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 174 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 175 | class Data_ipv4(ct.Structure): |
| 176 | _fields_ = [ |
| 177 | ("ts_us", ct.c_ulonglong), |
| 178 | ("pid", ct.c_ulonglong), |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 179 | ("saddr", ct.c_ulonglong), |
| 180 | ("daddr", ct.c_ulonglong), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 181 | ("ip", ct.c_ulonglong), |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 182 | ("dport", ct.c_ulonglong), |
| 183 | ("task", ct.c_char * TASK_COMM_LEN) |
| 184 | ] |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 185 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 186 | class Data_ipv6(ct.Structure): |
| 187 | _fields_ = [ |
| 188 | ("ts_us", ct.c_ulonglong), |
| 189 | ("pid", ct.c_ulonglong), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 190 | ("saddr", (ct.c_ulonglong * 2)), |
| 191 | ("daddr", (ct.c_ulonglong * 2)), |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 192 | ("ip", ct.c_ulonglong), |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 193 | ("dport", ct.c_ulonglong), |
| 194 | ("task", ct.c_char * TASK_COMM_LEN) |
| 195 | ] |
| 196 | |
| 197 | # process event |
| 198 | def print_ipv4_event(cpu, data, size): |
| 199 | event = ct.cast(data, ct.POINTER(Data_ipv4)).contents |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 200 | global start_ts |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 201 | if args.timestamp: |
| 202 | if start_ts == 0: |
| 203 | start_ts = event.ts_us |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 204 | print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="") |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 205 | print("%-6d %-12.12s %-2d %-16s %-16s %-4d" % (event.pid, |
| 206 | event.task.decode(), event.ip, |
| 207 | inet_ntop(AF_INET, pack("I", event.saddr)), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 208 | inet_ntop(AF_INET, pack("I", event.daddr)), event.dport)) |
Brendan Gregg | 9e0b087 | 2016-03-28 12:11:45 -0700 | [diff] [blame] | 209 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 210 | def print_ipv6_event(cpu, data, size): |
| 211 | event = ct.cast(data, ct.POINTER(Data_ipv6)).contents |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 212 | global start_ts |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 213 | if args.timestamp: |
| 214 | if start_ts == 0: |
| 215 | start_ts = event.ts_us |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 216 | print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="") |
Brendan Gregg | 9e0b087 | 2016-03-28 12:11:45 -0700 | [diff] [blame] | 217 | print("%-6d %-12.12s %-2d %-16s %-16s %-4d" % (event.pid, |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 218 | event.task.decode(), event.ip, inet_ntop(AF_INET6, event.saddr), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 219 | inet_ntop(AF_INET6, event.daddr), event.dport)) |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 220 | |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 221 | # initialize BPF |
| 222 | b = BPF(text=bpf_text) |
| 223 | b.attach_kprobe(event="tcp_v4_connect", fn_name="trace_connect_entry") |
| 224 | b.attach_kprobe(event="tcp_v6_connect", fn_name="trace_connect_entry") |
| 225 | b.attach_kretprobe(event="tcp_v4_connect", fn_name="trace_connect_v4_return") |
| 226 | b.attach_kretprobe(event="tcp_v6_connect", fn_name="trace_connect_v6_return") |
| 227 | |
| 228 | # header |
| 229 | if args.timestamp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 230 | print("%-9s" % ("TIME(s)"), end="") |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 231 | print("%-6s %-12s %-2s %-16s %-16s %-4s" % ("PID", "COMM", "IP", "SADDR", |
| 232 | "DADDR", "DPORT")) |
| 233 | |
| 234 | start_ts = 0 |
| 235 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 236 | # read events |
| 237 | b["ipv4_events"].open_perf_buffer(print_ipv4_event) |
| 238 | b["ipv6_events"].open_perf_buffer(print_ipv6_event) |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 239 | while 1: |
Teng Qin | dbf0029 | 2018-02-28 21:47:50 -0800 | [diff] [blame] | 240 | b.perf_buffer_poll() |