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