blob: 0e91063675984f84103c1de062fa8d3e5d27d60a [file] [log] [blame]
Brendan Gregg052f89c2015-10-13 15:35:58 -07001#!/usr/bin/python
2#
3# tcpaccept Trace TCP accept()s.
4# For Linux, uses BCC, eBPF. Embedded C.
5#
6# USAGE: tcpaccept [-h] [-t] [-p PID]
7#
8# This uses dynamic tracing of the kernel inet_csk_accept() socket function
9# (from tcp_prot.accept), and will need to be modified to match kernel changes.
10# This also traces DCCP traffic; check for future versions where this should
11# be filtered (should be done via "sk_protocol != IPPROTO_TCP").
12#
13# IPv4 addresses are printed as dotted quads. For IPv6 addresses, the last four
14# bytes are printed after "..."; check for future versions with better IPv6
15# support.
16#
17# Copyright (c) 2015 Brendan Gregg.
18# Licensed under the Apache License, Version 2.0 (the "License")
19#
20# 13-Oct-2015 Brendan Gregg Created this.
21
22from __future__ import print_function
23from bcc import BPF
24import argparse
25
26# arguments
27examples = """examples:
Brendan Gregg000a4e62015-10-13 15:41:46 -070028 ./tcpaccept # trace all TCP accept()s
Brendan Gregg052f89c2015-10-13 15:35:58 -070029 ./tcpaccept -t # include timestamps
30 ./tcpaccept -p 181 # only trace PID 181
31"""
32parser = argparse.ArgumentParser(
33 description="Trace TCP accepts",
34 formatter_class=argparse.RawDescriptionHelpFormatter,
35 epilog=examples)
36parser.add_argument("-t", "--timestamp", action="store_true",
37 help="include timestamp on output")
38parser.add_argument("-p", "--pid",
39 help="trace this PID only")
40args = parser.parse_args()
41debug = 0
42
43# define BPF program
44bpf_text = """
45#include <uapi/linux/ptrace.h>
46#include <net/sock.h>
47#include <bcc/proto.h>
48
49int kretprobe__inet_csk_accept(struct pt_regs *ctx)
50{
51 struct sock *newsk = (struct sock *)ctx->ax;
52 u32 pid = bpf_get_current_pid_tgid();
53
54 if (newsk == NULL)
55 return 0;
56
57 // pull in details
58 u16 family = 0, lport = 0;
59 u32 saddr = 0, daddr = 0;
60 bpf_probe_read(&family, sizeof(family), &newsk->__sk_common.skc_family);
61 bpf_probe_read(&lport, sizeof(lport), &newsk->__sk_common.skc_num);
62 if (family == AF_INET) {
63 bpf_probe_read(&saddr, sizeof(saddr),
64 &newsk->__sk_common.skc_rcv_saddr);
65 bpf_probe_read(&daddr, sizeof(daddr),
66 &newsk->__sk_common.skc_daddr);
67
68 // output
69 bpf_trace_printk("4 %x %x %d\\n", daddr, saddr, lport);
70 } else if (family == AF_INET6) {
71 // just grab the last 4 bytes for now
72 bpf_probe_read(&saddr, sizeof(saddr),
73 &newsk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32[3]);
74 bpf_probe_read(&daddr, sizeof(daddr),
75 &newsk->__sk_common.skc_v6_daddr.in6_u.u6_addr32[3]);
76
77 // output and flip byte order of addresses
78 bpf_trace_printk("6 %x %x %d\\n", bpf_ntohl(daddr),
79 bpf_ntohl(saddr), lport);
80 }
81 // else drop
82
83 return 0;
84}
85"""
86
87# code substitutions
88if args.pid:
89 bpf_text = bpf_text.replace('FILTER',
90 'if (pid != %s) { return 0; }' % args.pid)
91else:
92 bpf_text = bpf_text.replace('FILTER', '')
93if debug:
94 print(bpf_text)
95
96# initialize BPF
97b = BPF(text=bpf_text)
98
99# header
100if args.timestamp:
101 print("%-9s" % ("TIME(s)"), end="")
102print("%-6s %-12s %-2s %-16s %-16s %-4s" % ("PID", "COMM", "IP", "RADDR",
103 "LADDR", "LPORT"))
104
105start_ts = 0
106
107def inet_ntoa(addr):
108 dq = ''
109 for i in range(0, 4):
110 dq = dq + str(addr & 0xff)
111 if (i != 3):
112 dq = dq + '.'
113 addr = addr >> 8
114 return dq
115
116# format output
117while 1:
118 (task, pid, cpu, flags, ts, msg) = b.trace_fields()
119 (ip_s, raddr_hs, laddr_hs, lport_s) = msg.split(" ")
120
121 if args.timestamp:
122 if start_ts == 0:
123 start_ts = ts
124 print("%-9.3f" % (ts - start_ts), end="")
125 print("%-6d %-12.12s %-2s %-16s %-16s %-4s" % (pid, task, ip_s,
126 inet_ntoa(int(raddr_hs, 16)) if ip_s == "4" else "..." + raddr_hs,
127 inet_ntoa(int(laddr_hs, 16)) if ip_s == "4" else "..." + laddr_hs,
128 lport_s))