Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -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 | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # tcpaccept Trace TCP accept()s. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 6 | # |
| 7 | # USAGE: tcpaccept [-h] [-t] [-p PID] |
| 8 | # |
| 9 | # This uses dynamic tracing of the kernel inet_csk_accept() socket function |
| 10 | # (from tcp_prot.accept), and will need to be modified to match kernel changes. |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 11 | # |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 12 | # Copyright (c) 2015 Brendan Gregg. |
| 13 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 14 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 15 | # 13-Oct-2015 Brendan Gregg Created this. |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 16 | # 14-Feb-2016 " " Switch to bpf_perf_output. |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 17 | |
| 18 | from __future__ import print_function |
| 19 | from bcc import BPF |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 20 | from socket import inet_ntop, AF_INET, AF_INET6 |
| 21 | from struct import pack |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 22 | import argparse |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 23 | import ctypes as ct |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 24 | |
| 25 | # arguments |
| 26 | examples = """examples: |
Brendan Gregg | 000a4e6 | 2015-10-13 15:41:46 -0700 | [diff] [blame] | 27 | ./tcpaccept # trace all TCP accept()s |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 28 | ./tcpaccept -t # include timestamps |
| 29 | ./tcpaccept -p 181 # only trace PID 181 |
| 30 | """ |
| 31 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 32 | description="Trace TCP accepts", |
| 33 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 34 | epilog=examples) |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 35 | parser.add_argument("-t", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 36 | help="include timestamp on output") |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 37 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 38 | help="trace this PID only") |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 39 | args = parser.parse_args() |
| 40 | debug = 0 |
| 41 | |
| 42 | # define BPF program |
| 43 | bpf_text = """ |
| 44 | #include <uapi/linux/ptrace.h> |
| 45 | #include <net/sock.h> |
| 46 | #include <bcc/proto.h> |
| 47 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 48 | // separate data structs for ipv4 and ipv6 |
| 49 | struct ipv4_data_t { |
| 50 | // XXX: switch some to u32's when supported |
| 51 | u64 ts_us; |
| 52 | u64 pid; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 53 | u64 saddr; |
| 54 | u64 daddr; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 55 | u64 ip; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 56 | u64 lport; |
| 57 | char task[TASK_COMM_LEN]; |
| 58 | }; |
| 59 | BPF_PERF_OUTPUT(ipv4_events); |
| 60 | |
| 61 | struct ipv6_data_t { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 62 | u64 ts_us; |
| 63 | u64 pid; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 64 | unsigned __int128 saddr; |
| 65 | unsigned __int128 daddr; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 66 | u64 ip; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 67 | u64 lport; |
| 68 | char task[TASK_COMM_LEN]; |
| 69 | }; |
| 70 | BPF_PERF_OUTPUT(ipv6_events); |
| 71 | |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 72 | int kretprobe__inet_csk_accept(struct pt_regs *ctx) |
| 73 | { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 74 | struct sock *newsk = (struct sock *)PT_REGS_RC(ctx); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 75 | u32 pid = bpf_get_current_pid_tgid(); |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 76 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 77 | if (newsk == NULL) |
| 78 | return 0; |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 79 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 80 | // check this is TCP |
| 81 | u8 protocol = 0; |
| 82 | // workaround for reading the sk_protocol bitfield: |
| 83 | bpf_probe_read(&protocol, 1, (void *)((long)&newsk->sk_wmem_queued) - 3); |
| 84 | if (protocol != IPPROTO_TCP) |
| 85 | return 0; |
Brendan Gregg | 10e1b14 | 2015-10-13 16:35:25 -0700 | [diff] [blame] | 86 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 87 | // pull in details |
| 88 | u16 family = 0, lport = 0; |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 89 | bpf_probe_read(&family, sizeof(family), &newsk->__sk_common.skc_family); |
| 90 | bpf_probe_read(&lport, sizeof(lport), &newsk->__sk_common.skc_num); |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 91 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 92 | if (family == AF_INET) { |
| 93 | struct ipv4_data_t data4 = {.pid = pid, .ip = 4}; |
| 94 | data4.ts_us = bpf_ktime_get_ns() / 1000; |
| 95 | bpf_probe_read(&data4.saddr, sizeof(u32), |
| 96 | &newsk->__sk_common.skc_rcv_saddr); |
| 97 | bpf_probe_read(&data4.daddr, sizeof(u32), |
| 98 | &newsk->__sk_common.skc_daddr); |
| 99 | data4.lport = lport; |
| 100 | bpf_get_current_comm(&data4.task, sizeof(data4.task)); |
| 101 | ipv4_events.perf_submit(ctx, &data4, sizeof(data4)); |
| 102 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 103 | } else if (family == AF_INET6) { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 104 | struct ipv6_data_t data6 = {.pid = pid, .ip = 6}; |
| 105 | data6.ts_us = bpf_ktime_get_ns() / 1000; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 106 | bpf_probe_read(&data6.saddr, sizeof(data6.saddr), |
| 107 | &newsk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
| 108 | bpf_probe_read(&data6.daddr, sizeof(data6.daddr), |
| 109 | &newsk->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 110 | data6.lport = lport; |
| 111 | bpf_get_current_comm(&data6.task, sizeof(data6.task)); |
| 112 | ipv6_events.perf_submit(ctx, &data6, sizeof(data6)); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 113 | } |
| 114 | // else drop |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 115 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 116 | return 0; |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 117 | } |
| 118 | """ |
| 119 | |
| 120 | # code substitutions |
| 121 | if args.pid: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 122 | bpf_text = bpf_text.replace('FILTER', |
| 123 | 'if (pid != %s) { return 0; }' % args.pid) |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 124 | else: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 125 | bpf_text = bpf_text.replace('FILTER', '') |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 126 | if debug: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 127 | print(bpf_text) |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 128 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 129 | # event data |
| 130 | TASK_COMM_LEN = 16 # linux/sched.h |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 131 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 132 | class Data_ipv4(ct.Structure): |
| 133 | _fields_ = [ |
| 134 | ("ts_us", ct.c_ulonglong), |
| 135 | ("pid", ct.c_ulonglong), |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 136 | ("saddr", ct.c_ulonglong), |
| 137 | ("daddr", ct.c_ulonglong), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 138 | ("ip", ct.c_ulonglong), |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 139 | ("lport", ct.c_ulonglong), |
| 140 | ("task", ct.c_char * TASK_COMM_LEN) |
| 141 | ] |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 142 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 143 | class Data_ipv6(ct.Structure): |
| 144 | _fields_ = [ |
| 145 | ("ts_us", ct.c_ulonglong), |
| 146 | ("pid", ct.c_ulonglong), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 147 | ("saddr", (ct.c_ulonglong * 2)), |
| 148 | ("daddr", (ct.c_ulonglong * 2)), |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 149 | ("ip", ct.c_ulonglong), |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 150 | ("lport", ct.c_ulonglong), |
| 151 | ("task", ct.c_char * TASK_COMM_LEN) |
| 152 | ] |
| 153 | |
| 154 | # process event |
| 155 | def print_ipv4_event(cpu, data, size): |
| 156 | event = ct.cast(data, ct.POINTER(Data_ipv4)).contents |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 157 | global start_ts |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 158 | if args.timestamp: |
| 159 | if start_ts == 0: |
| 160 | start_ts = event.ts_us |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 161 | print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="") |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 162 | print("%-6d %-12.12s %-2d %-16s %-16s %-4d" % (event.pid, |
| 163 | event.task.decode(), event.ip, |
| 164 | inet_ntop(AF_INET, pack("I", event.daddr)), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 165 | inet_ntop(AF_INET, pack("I", event.saddr)), event.lport)) |
| 166 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 167 | def print_ipv6_event(cpu, data, size): |
| 168 | event = ct.cast(data, ct.POINTER(Data_ipv6)).contents |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 169 | global start_ts |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 170 | if args.timestamp: |
| 171 | if start_ts == 0: |
| 172 | start_ts = event.ts_us |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 173 | print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="") |
Rafael F | 78948e4 | 2017-03-26 14:54:25 +0200 | [diff] [blame] | 174 | print("%-6d %-12.12s %-2d %-16s %-16s %-4d" % (event.pid, |
| 175 | event.task.decode(), event.ip, inet_ntop(AF_INET6, event.daddr), |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 176 | inet_ntop(AF_INET6, event.saddr), event.lport)) |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 177 | |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 178 | # initialize BPF |
| 179 | b = BPF(text=bpf_text) |
| 180 | |
| 181 | # header |
| 182 | if args.timestamp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 183 | print("%-9s" % ("TIME(s)"), end="") |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 184 | print("%-6s %-12s %-2s %-16s %-16s %-4s" % ("PID", "COMM", "IP", "RADDR", |
| 185 | "LADDR", "LPORT")) |
| 186 | |
| 187 | start_ts = 0 |
| 188 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 189 | # read events |
| 190 | b["ipv4_events"].open_perf_buffer(print_ipv4_event) |
| 191 | b["ipv6_events"].open_perf_buffer(print_ipv6_event) |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 192 | while 1: |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 193 | b.kprobe_poll() |