blob: d3e44143d8b1334e9feae9e20bc23e92d4828e9a [file] [log] [blame]
Alexey Ivanovcc01a9c2019-01-16 09:50:46 -08001#!/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#
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -07007# USAGE: tcpaccept [-h] [-T] [-t] [-p PID] [-P PORTS] [-4 | -6]
Brendan Gregg052f89c2015-10-13 15:35:58 -07008#
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#
Brendan Gregg052f89c2015-10-13 15:35:58 -070012# Copyright (c) 2015 Brendan Gregg.
13# Licensed under the Apache License, Version 2.0 (the "License")
14#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080015# 13-Oct-2015 Brendan Gregg Created this.
Brendan Gregg24825522016-02-14 16:32:29 -080016# 14-Feb-2016 " " Switch to bpf_perf_output.
Brendan Gregg052f89c2015-10-13 15:35:58 -070017
18from __future__ import print_function
Alban Crequy32ab8582020-03-22 16:06:44 +010019from bcc.containers import filter_by_containers
Brendan Gregg052f89c2015-10-13 15:35:58 -070020from bcc import BPF
Mark Drayton11de2982016-06-26 21:14:44 +010021from socket import inet_ntop, AF_INET, AF_INET6
22from struct import pack
Brendan Gregg052f89c2015-10-13 15:35:58 -070023import argparse
japrocaed9b1e2019-01-04 20:21:46 +030024from bcc.utils import printb
Xiaozhou Liu701bd732019-03-08 13:46:28 +080025from time import strftime
Brendan Gregg052f89c2015-10-13 15:35:58 -070026
27# arguments
28examples = """examples:
Brendan Gregg000a4e62015-10-13 15:41:46 -070029 ./tcpaccept # trace all TCP accept()s
Brendan Gregg052f89c2015-10-13 15:35:58 -070030 ./tcpaccept -t # include timestamps
Xiaozhou Liu701bd732019-03-08 13:46:28 +080031 ./tcpaccept -P 80,81 # only trace port 80 and 81
Brendan Gregg052f89c2015-10-13 15:35:58 -070032 ./tcpaccept -p 181 # only trace PID 181
Alban Crequy32ab8582020-03-22 16:06:44 +010033 ./tcpaccept --cgroupmap mappath # only trace cgroups in this BPF map
34 ./tcpaccept --mntnsmap mappath # only trace mount namespaces in the map
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -070035 ./tcpaccept -4 # trace IPv4 family
36 ./tcpaccept -6 # trace IPv6 family
Brendan Gregg052f89c2015-10-13 15:35:58 -070037"""
38parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080039 description="Trace TCP accepts",
40 formatter_class=argparse.RawDescriptionHelpFormatter,
41 epilog=examples)
Xiaozhou Liu701bd732019-03-08 13:46:28 +080042parser.add_argument("-T", "--time", action="store_true",
43 help="include time column on output (HH:MM:SS)")
Brendan Gregg052f89c2015-10-13 15:35:58 -070044parser.add_argument("-t", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080045 help="include timestamp on output")
Brendan Gregg052f89c2015-10-13 15:35:58 -070046parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080047 help="trace this PID only")
Xiaozhou Liu701bd732019-03-08 13:46:28 +080048parser.add_argument("-P", "--port",
49 help="comma-separated list of local ports to trace")
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -070050group = parser.add_mutually_exclusive_group()
51group.add_argument("-4", "--ipv4", action="store_true",
52 help="trace IPv4 family only")
53group.add_argument("-6", "--ipv6", action="store_true",
54 help="trace IPv6 family only")
Alban Crequy1ce868f2020-02-19 17:07:41 +010055parser.add_argument("--cgroupmap",
56 help="trace cgroups in this BPF map only")
Alban Crequy32ab8582020-03-22 16:06:44 +010057parser.add_argument("--mntnsmap",
58 help="trace mount namespaces in this BPF map only")
Nathan Scottcf0792f2018-02-02 16:56:50 +110059parser.add_argument("--ebpf", action="store_true",
60 help=argparse.SUPPRESS)
Brendan Gregg052f89c2015-10-13 15:35:58 -070061args = parser.parse_args()
62debug = 0
63
64# define BPF program
65bpf_text = """
66#include <uapi/linux/ptrace.h>
67#include <net/sock.h>
68#include <bcc/proto.h>
69
Brendan Gregg24825522016-02-14 16:32:29 -080070// separate data structs for ipv4 and ipv6
71struct ipv4_data_t {
Brendan Gregg24825522016-02-14 16:32:29 -080072 u64 ts_us;
Joe Yin36ce1122018-08-17 06:04:00 +080073 u32 pid;
Joe Yin365eade2018-06-21 13:41:03 +080074 u32 saddr;
75 u32 daddr;
Mark Drayton11de2982016-06-26 21:14:44 +010076 u64 ip;
Joe Yin36ce1122018-08-17 06:04:00 +080077 u16 lport;
Xiaozhou Liu701bd732019-03-08 13:46:28 +080078 u16 dport;
Brendan Gregg24825522016-02-14 16:32:29 -080079 char task[TASK_COMM_LEN];
80};
81BPF_PERF_OUTPUT(ipv4_events);
82
83struct ipv6_data_t {
Brendan Gregg24825522016-02-14 16:32:29 -080084 u64 ts_us;
Joe Yin36ce1122018-08-17 06:04:00 +080085 u32 pid;
Mark Drayton11de2982016-06-26 21:14:44 +010086 unsigned __int128 saddr;
87 unsigned __int128 daddr;
Brendan Gregg24825522016-02-14 16:32:29 -080088 u64 ip;
Joe Yin36ce1122018-08-17 06:04:00 +080089 u16 lport;
Xiaozhou Liu701bd732019-03-08 13:46:28 +080090 u16 dport;
Brendan Gregg24825522016-02-14 16:32:29 -080091 char task[TASK_COMM_LEN];
92};
93BPF_PERF_OUTPUT(ipv6_events);
Joe Yin365eade2018-06-21 13:41:03 +080094"""
Brendan Gregg24825522016-02-14 16:32:29 -080095
Joe Yin365eade2018-06-21 13:41:03 +080096#
Xiaozhou Liufd737452019-04-10 23:35:16 +080097# The following code uses kprobes to instrument inet_csk_accept().
98# On Linux 4.16 and later, we could use sock:inet_sock_set_state
Michael Prokopc14d02a2020-01-09 02:29:18 +010099# tracepoint for efficiency, but it may output wrong PIDs. This is
Xiaozhou Liufd737452019-04-10 23:35:16 +0800100# because sock:inet_sock_set_state may run outside of process context.
101# Hence, we stick to kprobes until we find a proper solution.
Joe Yin365eade2018-06-21 13:41:03 +0800102#
103bpf_text_kprobe = """
Brendan Gregg052f89c2015-10-13 15:35:58 -0700104int kretprobe__inet_csk_accept(struct pt_regs *ctx)
105{
Alban Crequy32ab8582020-03-22 16:06:44 +0100106 if (container_should_be_filtered()) {
Alban Crequy1ce868f2020-02-19 17:07:41 +0100107 return 0;
108 }
Alban Crequy1ce868f2020-02-19 17:07:41 +0100109
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530110 struct sock *newsk = (struct sock *)PT_REGS_RC(ctx);
Brendan Gregg8cd3efd2019-04-07 12:32:53 -0700111 u32 pid = bpf_get_current_pid_tgid() >> 32;
Brendan Gregg052f89c2015-10-13 15:35:58 -0700112
detailyang54044d52019-01-10 00:43:39 +0800113 ##FILTER_PID##
114
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800115 if (newsk == NULL)
116 return 0;
Brendan Gregg052f89c2015-10-13 15:35:58 -0700117
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800118 // check this is TCP
119 u8 protocol = 0;
120 // workaround for reading the sk_protocol bitfield:
Alban Crequy32ab8582020-03-22 16:06:44 +0100121
Joe Yin116bb402018-06-18 23:34:52 +0800122 // Following comments add by Joe Yin:
123 // Unfortunately,it can not work since Linux 4.10,
124 // because the sk_wmem_queued is not following the bitfield of sk_protocol.
125 // And the following member is sk_gso_max_segs.
126 // So, we can use this:
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500127 // bpf_probe_read_kernel(&protocol, 1, (void *)((u64)&newsk->sk_gso_max_segs) - 3);
Alban Crequy32ab8582020-03-22 16:06:44 +0100128 // In order to diff the pre-4.10 and 4.10+ ,introduce the variables gso_max_segs_offset,sk_lingertime,
129 // sk_lingertime is closed to the gso_max_segs_offset,and
130 // the offset between the two members is 4
Joe Yin116bb402018-06-18 23:34:52 +0800131
132 int gso_max_segs_offset = offsetof(struct sock, sk_gso_max_segs);
133 int sk_lingertime_offset = offsetof(struct sock, sk_lingertime);
134
Alban Crequy32ab8582020-03-22 16:06:44 +0100135 if (sk_lingertime_offset - gso_max_segs_offset == 4)
Joe Yin116bb402018-06-18 23:34:52 +0800136 // 4.10+ with little endian
137#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Paul Chaignon8d78edd2018-06-29 07:47:44 +0200138 protocol = *(u8 *)((u64)&newsk->sk_gso_max_segs - 3);
Joe Yin116bb402018-06-18 23:34:52 +0800139 else
140 // pre-4.10 with little endian
Paul Chaignon8d78edd2018-06-29 07:47:44 +0200141 protocol = *(u8 *)((u64)&newsk->sk_wmem_queued - 3);
Joe Yin116bb402018-06-18 23:34:52 +0800142#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
143 // 4.10+ with big endian
Paul Chaignon8d78edd2018-06-29 07:47:44 +0200144 protocol = *(u8 *)((u64)&newsk->sk_gso_max_segs - 1);
Joe Yin116bb402018-06-18 23:34:52 +0800145 else
146 // pre-4.10 with big endian
Paul Chaignon8d78edd2018-06-29 07:47:44 +0200147 protocol = *(u8 *)((u64)&newsk->sk_wmem_queued - 1);
Joe Yin116bb402018-06-18 23:34:52 +0800148#else
149# error "Fix your compiler's __BYTE_ORDER__?!"
150#endif
151
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800152 if (protocol != IPPROTO_TCP)
153 return 0;
Brendan Gregg10e1b142015-10-13 16:35:25 -0700154
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800155 // pull in details
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800156 u16 family = 0, lport = 0, dport;
Paul Chaignona9f96c02018-06-15 00:27:08 +0200157 family = newsk->__sk_common.skc_family;
158 lport = newsk->__sk_common.skc_num;
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800159 dport = newsk->__sk_common.skc_dport;
160 dport = ntohs(dport);
161
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -0700162 ##FILTER_FAMILY##
163
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800164 ##FILTER_PORT##
Brendan Gregg052f89c2015-10-13 15:35:58 -0700165
Brendan Gregg24825522016-02-14 16:32:29 -0800166 if (family == AF_INET) {
167 struct ipv4_data_t data4 = {.pid = pid, .ip = 4};
168 data4.ts_us = bpf_ktime_get_ns() / 1000;
Paul Chaignona9f96c02018-06-15 00:27:08 +0200169 data4.saddr = newsk->__sk_common.skc_rcv_saddr;
170 data4.daddr = newsk->__sk_common.skc_daddr;
Brendan Gregg24825522016-02-14 16:32:29 -0800171 data4.lport = lport;
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800172 data4.dport = dport;
Brendan Gregg24825522016-02-14 16:32:29 -0800173 bpf_get_current_comm(&data4.task, sizeof(data4.task));
174 ipv4_events.perf_submit(ctx, &data4, sizeof(data4));
175
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800176 } else if (family == AF_INET6) {
Brendan Gregg24825522016-02-14 16:32:29 -0800177 struct ipv6_data_t data6 = {.pid = pid, .ip = 6};
178 data6.ts_us = bpf_ktime_get_ns() / 1000;
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500179 bpf_probe_read_kernel(&data6.saddr, sizeof(data6.saddr),
Mark Drayton11de2982016-06-26 21:14:44 +0100180 &newsk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
Sumanth Korikkar7f6066d2020-05-20 10:49:56 -0500181 bpf_probe_read_kernel(&data6.daddr, sizeof(data6.daddr),
Mark Drayton11de2982016-06-26 21:14:44 +0100182 &newsk->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
Brendan Gregg24825522016-02-14 16:32:29 -0800183 data6.lport = lport;
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800184 data6.dport = dport;
Brendan Gregg24825522016-02-14 16:32:29 -0800185 bpf_get_current_comm(&data6.task, sizeof(data6.task));
186 ipv6_events.perf_submit(ctx, &data6, sizeof(data6));
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800187 }
188 // else drop
Brendan Gregg052f89c2015-10-13 15:35:58 -0700189
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800190 return 0;
Brendan Gregg052f89c2015-10-13 15:35:58 -0700191}
192"""
193
Xiaozhou Liufd737452019-04-10 23:35:16 +0800194bpf_text += bpf_text_kprobe
Joe Yin365eade2018-06-21 13:41:03 +0800195
Brendan Gregg052f89c2015-10-13 15:35:58 -0700196# code substitutions
197if args.pid:
detailyang54044d52019-01-10 00:43:39 +0800198 bpf_text = bpf_text.replace('##FILTER_PID##',
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800199 'if (pid != %s) { return 0; }' % args.pid)
Brendan Gregg052f89c2015-10-13 15:35:58 -0700200else:
detailyang54044d52019-01-10 00:43:39 +0800201 bpf_text = bpf_text.replace('##FILTER_PID##', '')
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800202if args.port:
203 lports = [int(lport) for lport in args.port.split(',')]
204 lports_if = ' && '.join(['lport != %d' % lport for lport in lports])
205 bpf_text = bpf_text.replace('##FILTER_PORT##',
206 'if (%s) { return 0; }' % lports_if)
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -0700207if args.ipv4:
208 bpf_text = bpf_text.replace('##FILTER_FAMILY##',
209 'if (family != AF_INET) { return 0; }')
210elif args.ipv6:
211 bpf_text = bpf_text.replace('##FILTER_FAMILY##',
212 'if (family != AF_INET6) { return 0; }')
213
Alban Crequy32ab8582020-03-22 16:06:44 +0100214bpf_text = filter_by_containers(args) + bpf_text
Nathan Scottcf0792f2018-02-02 16:56:50 +1100215if debug or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800216 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100217 if args.ebpf:
218 exit()
Brendan Gregg052f89c2015-10-13 15:35:58 -0700219
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800220bpf_text = bpf_text.replace('##FILTER_PORT##', '')
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -0700221bpf_text = bpf_text.replace('##FILTER_FAMILY##', '')
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800222
Brendan Gregg24825522016-02-14 16:32:29 -0800223# process event
224def print_ipv4_event(cpu, data, size):
Xiaozhou Liu51d62d32019-02-15 13:03:05 +0800225 event = b["ipv4_events"].event(data)
Mark Drayton11de2982016-06-26 21:14:44 +0100226 global start_ts
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800227 if args.time:
Gary Lin6c793312019-04-18 15:17:56 +0800228 printb(b"%-9s" % strftime("%H:%M:%S").encode('ascii'), nl="")
Brendan Gregg24825522016-02-14 16:32:29 -0800229 if args.timestamp:
230 if start_ts == 0:
231 start_ts = event.ts_us
Gary Lin6c793312019-04-18 15:17:56 +0800232 printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="")
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800233 printb(b"%-7d %-12.12s %-2d %-16s %-5d %-16s %-5d" % (event.pid,
Yonghong Songebe19512019-01-10 14:54:16 -0800234 event.task, event.ip,
235 inet_ntop(AF_INET, pack("I", event.daddr)).encode(),
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800236 event.dport,
Yonghong Songebe19512019-01-10 14:54:16 -0800237 inet_ntop(AF_INET, pack("I", event.saddr)).encode(),
238 event.lport))
Mark Drayton11de2982016-06-26 21:14:44 +0100239
Brendan Gregg24825522016-02-14 16:32:29 -0800240def print_ipv6_event(cpu, data, size):
Xiaozhou Liu51d62d32019-02-15 13:03:05 +0800241 event = b["ipv6_events"].event(data)
Mark Drayton11de2982016-06-26 21:14:44 +0100242 global start_ts
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800243 if args.time:
Gary Lin6c793312019-04-18 15:17:56 +0800244 printb(b"%-9s" % strftime("%H:%M:%S").encode('ascii'), nl="")
Brendan Gregg24825522016-02-14 16:32:29 -0800245 if args.timestamp:
246 if start_ts == 0:
247 start_ts = event.ts_us
Gary Lin6c793312019-04-18 15:17:56 +0800248 printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="")
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800249 printb(b"%-7d %-12.12s %-2d %-16s %-5d %-16s %-5d" % (event.pid,
Yonghong Songebe19512019-01-10 14:54:16 -0800250 event.task, event.ip,
251 inet_ntop(AF_INET6, event.daddr).encode(),
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800252 event.dport,
Yonghong Songebe19512019-01-10 14:54:16 -0800253 inet_ntop(AF_INET6, event.saddr).encode(),
jeromemarchandb96ebcd2018-10-10 01:58:15 +0200254 event.lport))
Brendan Gregg24825522016-02-14 16:32:29 -0800255
Brendan Gregg052f89c2015-10-13 15:35:58 -0700256# initialize BPF
257b = BPF(text=bpf_text)
258
259# header
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800260if args.time:
261 print("%-9s" % ("TIME"), end="")
Brendan Gregg052f89c2015-10-13 15:35:58 -0700262if args.timestamp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800263 print("%-9s" % ("TIME(s)"), end="")
Xiaozhou Liu701bd732019-03-08 13:46:28 +0800264print("%-7s %-12s %-2s %-16s %-5s %-16s %-5s" % ("PID", "COMM", "IP", "RADDR",
265 "RPORT", "LADDR", "LPORT"))
Brendan Gregg052f89c2015-10-13 15:35:58 -0700266
267start_ts = 0
268
Brendan Gregg24825522016-02-14 16:32:29 -0800269# read events
270b["ipv4_events"].open_perf_buffer(print_ipv4_event)
271b["ipv6_events"].open_perf_buffer(print_ipv6_event)
Brendan Gregg052f89c2015-10-13 15:35:58 -0700272while 1:
Jerome Marchand51671272018-12-19 01:57:24 +0100273 try:
274 b.perf_buffer_poll()
275 except KeyboardInterrupt:
276 exit()