blob: 2fda8a78e1be8aeb35a9eee0c9f28fbc06338f66 [file] [log] [blame]
Brendan Gregg052f89c2015-10-13 15:35:58 -07001#!/usr/bin/python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Gregg052f89c2015-10-13 15:35:58 -07003#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08004# tcpaccept Trace TCP accept()s.
5# For Linux, uses BCC, eBPF. Embedded C.
Brendan Gregg052f89c2015-10-13 15:35:58 -07006#
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 Gregg052f89c2015-10-13 15:35:58 -070011#
12# IPv4 addresses are printed as dotted quads. For IPv6 addresses, the last four
13# bytes are printed after "..."; check for future versions with better IPv6
14# support.
15#
16# Copyright (c) 2015 Brendan Gregg.
17# Licensed under the Apache License, Version 2.0 (the "License")
18#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080019# 13-Oct-2015 Brendan Gregg Created this.
Brendan Gregg052f89c2015-10-13 15:35:58 -070020
21from __future__ import print_function
22from bcc import BPF
23import argparse
24
25# arguments
26examples = """examples:
Brendan Gregg000a4e62015-10-13 15:41:46 -070027 ./tcpaccept # trace all TCP accept()s
Brendan Gregg052f89c2015-10-13 15:35:58 -070028 ./tcpaccept -t # include timestamps
29 ./tcpaccept -p 181 # only trace PID 181
30"""
31parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080032 description="Trace TCP accepts",
33 formatter_class=argparse.RawDescriptionHelpFormatter,
34 epilog=examples)
Brendan Gregg052f89c2015-10-13 15:35:58 -070035parser.add_argument("-t", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080036 help="include timestamp on output")
Brendan Gregg052f89c2015-10-13 15:35:58 -070037parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080038 help="trace this PID only")
Brendan Gregg052f89c2015-10-13 15:35:58 -070039args = parser.parse_args()
40debug = 0
41
42# define BPF program
43bpf_text = """
44#include <uapi/linux/ptrace.h>
45#include <net/sock.h>
46#include <bcc/proto.h>
47
48int kretprobe__inet_csk_accept(struct pt_regs *ctx)
49{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080050 struct sock *newsk = (struct sock *)ctx->ax;
51 u32 pid = bpf_get_current_pid_tgid();
Brendan Gregg052f89c2015-10-13 15:35:58 -070052
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080053 if (newsk == NULL)
54 return 0;
Brendan Gregg052f89c2015-10-13 15:35:58 -070055
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080056 // check this is TCP
57 u8 protocol = 0;
58 // workaround for reading the sk_protocol bitfield:
59 bpf_probe_read(&protocol, 1, (void *)((long)&newsk->sk_wmem_queued) - 3);
60 if (protocol != IPPROTO_TCP)
61 return 0;
Brendan Gregg10e1b142015-10-13 16:35:25 -070062
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080063 // pull in details
64 u16 family = 0, lport = 0;
65 u32 saddr = 0, daddr = 0;
66 bpf_probe_read(&family, sizeof(family), &newsk->__sk_common.skc_family);
67 bpf_probe_read(&lport, sizeof(lport), &newsk->__sk_common.skc_num);
68 if (family == AF_INET) {
69 bpf_probe_read(&saddr, sizeof(saddr),
70 &newsk->__sk_common.skc_rcv_saddr);
71 bpf_probe_read(&daddr, sizeof(daddr),
72 &newsk->__sk_common.skc_daddr);
Brendan Gregg052f89c2015-10-13 15:35:58 -070073
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080074 // output
75 bpf_trace_printk("4 %x %x %d\\n", daddr, saddr, lport);
76 } else if (family == AF_INET6) {
77 // just grab the last 4 bytes for now
78 bpf_probe_read(&saddr, sizeof(saddr),
79 &newsk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32[3]);
80 bpf_probe_read(&daddr, sizeof(daddr),
81 &newsk->__sk_common.skc_v6_daddr.in6_u.u6_addr32[3]);
Brendan Gregg052f89c2015-10-13 15:35:58 -070082
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080083 // output and flip byte order of addresses
84 bpf_trace_printk("6 %x %x %d\\n", bpf_ntohl(daddr),
85 bpf_ntohl(saddr), lport);
86 }
87 // else drop
Brendan Gregg052f89c2015-10-13 15:35:58 -070088
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080089 return 0;
Brendan Gregg052f89c2015-10-13 15:35:58 -070090}
91"""
92
93# code substitutions
94if args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080095 bpf_text = bpf_text.replace('FILTER',
96 'if (pid != %s) { return 0; }' % args.pid)
Brendan Gregg052f89c2015-10-13 15:35:58 -070097else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080098 bpf_text = bpf_text.replace('FILTER', '')
Brendan Gregg052f89c2015-10-13 15:35:58 -070099if debug:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800100 print(bpf_text)
Brendan Gregg052f89c2015-10-13 15:35:58 -0700101
102# initialize BPF
103b = BPF(text=bpf_text)
104
105# header
106if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800107 print("%-9s" % ("TIME(s)"), end="")
Brendan Gregg052f89c2015-10-13 15:35:58 -0700108print("%-6s %-12s %-2s %-16s %-16s %-4s" % ("PID", "COMM", "IP", "RADDR",
109 "LADDR", "LPORT"))
110
111start_ts = 0
112
113def inet_ntoa(addr):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800114 dq = ''
115 for i in range(0, 4):
116 dq = dq + str(addr & 0xff)
117 if (i != 3):
118 dq = dq + '.'
119 addr = addr >> 8
120 return dq
Brendan Gregg052f89c2015-10-13 15:35:58 -0700121
122# format output
123while 1:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800124 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
125 (ip_s, raddr_hs, laddr_hs, lport_s) = msg.split(" ")
Brendan Gregg052f89c2015-10-13 15:35:58 -0700126
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800127 if args.timestamp:
128 if start_ts == 0:
129 start_ts = ts
130 print("%-9.3f" % (ts - start_ts), end="")
131 print("%-6d %-12.12s %-2s %-16s %-16s %-4s" % (pid, task, ip_s,
132 inet_ntoa(int(raddr_hs, 16)) if ip_s == "4" else "..." + raddr_hs,
133 inet_ntoa(int(laddr_hs, 16)) if ip_s == "4" else "..." + laddr_hs,
134 lport_s))