Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [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 | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # tcpconnect Trace TCP connect()s. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 6 | # |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 7 | # USAGE: tcpconnect [-h] [-c] [-t] [-p PID] [-P PORT [PORT ...]] |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 8 | # |
| 9 | # All connection attempts are traced, even if they ultimately fail. |
| 10 | # |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 11 | # This uses dynamic tracing of kernel functions, and will need to be updated |
| 12 | # to match kernel changes. |
| 13 | # |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 14 | # Copyright (c) 2015 Brendan Gregg. |
| 15 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 16 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 17 | # 25-Sep-2015 Brendan Gregg Created this. |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 18 | # 14-Feb-2016 " " Switch to bpf_perf_output. |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 19 | # 09-Jan-2019 Takuma Kume Support filtering by UID |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 20 | # 30-Jul-2019 Xiaozhou Liu Count connects. |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 21 | |
| 22 | from __future__ import print_function |
| 23 | from bcc import BPF |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 24 | from bcc.containers import filter_by_containers |
japroc | aed9b1e | 2019-01-04 20:21:46 +0300 | [diff] [blame] | 25 | from bcc.utils import printb |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 26 | import argparse |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 27 | from socket import inet_ntop, ntohs, AF_INET, AF_INET6 |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 28 | from struct import pack |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 29 | from time import sleep |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 30 | |
| 31 | # arguments |
| 32 | examples = """examples: |
| 33 | ./tcpconnect # trace all TCP connect()s |
| 34 | ./tcpconnect -t # include timestamps |
| 35 | ./tcpconnect -p 181 # only trace PID 181 |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 36 | ./tcpconnect -P 80 # only trace port 80 |
| 37 | ./tcpconnect -P 80,81 # only trace port 80 and 81 |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 38 | ./tcpconnect -U # include UID |
| 39 | ./tcpconnect -u 1000 # only trace UID 1000 |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 40 | ./tcpconnect -c # count connects per src ip and dest ip/port |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 41 | ./tcpconnect --cgroupmap mappath # only trace cgroups in this BPF map |
| 42 | ./tcpconnect --mntnsmap mappath # only trace mount namespaces in the map |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 43 | """ |
| 44 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 45 | description="Trace TCP connects", |
| 46 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 47 | epilog=examples) |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 48 | parser.add_argument("-t", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 49 | help="include timestamp on output") |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 50 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 51 | help="trace this PID only") |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 52 | parser.add_argument("-P", "--port", |
| 53 | help="comma-separated list of destination ports to trace.") |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 54 | parser.add_argument("-U", "--print-uid", action="store_true", |
| 55 | help="include UID on output") |
| 56 | parser.add_argument("-u", "--uid", |
| 57 | help="trace this UID only") |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 58 | parser.add_argument("-c", "--count", action="store_true", |
| 59 | help="count connects per src ip and dest ip/port") |
Alban Crequy | 1ce868f | 2020-02-19 17:07:41 +0100 | [diff] [blame] | 60 | parser.add_argument("--cgroupmap", |
| 61 | help="trace cgroups in this BPF map only") |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 62 | parser.add_argument("--mntnsmap", |
| 63 | help="trace mount namespaces in this BPF map only") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 64 | parser.add_argument("--ebpf", action="store_true", |
| 65 | help=argparse.SUPPRESS) |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 66 | args = parser.parse_args() |
| 67 | debug = 0 |
| 68 | |
| 69 | # define BPF program |
| 70 | bpf_text = """ |
| 71 | #include <uapi/linux/ptrace.h> |
| 72 | #include <net/sock.h> |
| 73 | #include <bcc/proto.h> |
| 74 | |
| 75 | BPF_HASH(currsock, u32, struct sock *); |
| 76 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 77 | // separate data structs for ipv4 and ipv6 |
| 78 | struct ipv4_data_t { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 79 | u64 ts_us; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 80 | u32 pid; |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 81 | u32 uid; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 82 | u32 saddr; |
| 83 | u32 daddr; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 84 | u64 ip; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 85 | u16 dport; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 86 | char task[TASK_COMM_LEN]; |
| 87 | }; |
| 88 | BPF_PERF_OUTPUT(ipv4_events); |
| 89 | |
| 90 | struct ipv6_data_t { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 91 | u64 ts_us; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 92 | u32 pid; |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 93 | u32 uid; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 94 | unsigned __int128 saddr; |
| 95 | unsigned __int128 daddr; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 96 | u64 ip; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 97 | u16 dport; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 98 | char task[TASK_COMM_LEN]; |
| 99 | }; |
| 100 | BPF_PERF_OUTPUT(ipv6_events); |
| 101 | |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 102 | // separate flow keys per address family |
| 103 | struct ipv4_flow_key_t { |
| 104 | u32 saddr; |
| 105 | u32 daddr; |
| 106 | u16 dport; |
| 107 | }; |
| 108 | BPF_HASH(ipv4_count, struct ipv4_flow_key_t); |
| 109 | |
| 110 | struct ipv6_flow_key_t { |
| 111 | unsigned __int128 saddr; |
| 112 | unsigned __int128 daddr; |
| 113 | u16 dport; |
| 114 | }; |
| 115 | BPF_HASH(ipv6_count, struct ipv6_flow_key_t); |
| 116 | |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 117 | int trace_connect_entry(struct pt_regs *ctx, struct sock *sk) |
| 118 | { |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 119 | if (container_should_be_filtered()) { |
| 120 | return 0; |
Alban Crequy | 1ce868f | 2020-02-19 17:07:41 +0100 | [diff] [blame] | 121 | } |
Alban Crequy | 1ce868f | 2020-02-19 17:07:41 +0100 | [diff] [blame] | 122 | |
Brendan Gregg | 8cd3efd | 2019-04-07 12:32:53 -0700 | [diff] [blame] | 123 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 124 | u32 pid = pid_tgid >> 32; |
| 125 | u32 tid = pid_tgid; |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 126 | FILTER_PID |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 127 | |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 128 | u32 uid = bpf_get_current_uid_gid(); |
| 129 | FILTER_UID |
| 130 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 131 | // stash the sock ptr for lookup on return |
Brendan Gregg | 8cd3efd | 2019-04-07 12:32:53 -0700 | [diff] [blame] | 132 | currsock.update(&tid, &sk); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 133 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 134 | return 0; |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | static int trace_connect_return(struct pt_regs *ctx, short ipver) |
| 138 | { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 139 | int ret = PT_REGS_RC(ctx); |
Brendan Gregg | 8cd3efd | 2019-04-07 12:32:53 -0700 | [diff] [blame] | 140 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 141 | u32 pid = pid_tgid >> 32; |
| 142 | u32 tid = pid_tgid; |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 143 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 144 | struct sock **skpp; |
Brendan Gregg | 8cd3efd | 2019-04-07 12:32:53 -0700 | [diff] [blame] | 145 | skpp = currsock.lookup(&tid); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 146 | if (skpp == 0) { |
| 147 | return 0; // missed entry |
| 148 | } |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 149 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 150 | if (ret != 0) { |
| 151 | // failed to send SYNC packet, may not have populated |
| 152 | // socket __sk_common.{skc_rcv_saddr, ...} |
Brendan Gregg | 8cd3efd | 2019-04-07 12:32:53 -0700 | [diff] [blame] | 153 | currsock.delete(&tid); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 154 | return 0; |
| 155 | } |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 156 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 157 | // pull in details |
| 158 | struct sock *skp = *skpp; |
Paul Chaignon | eae0acf | 2017-08-05 23:04:41 +0200 | [diff] [blame] | 159 | u16 dport = skp->__sk_common.skc_dport; |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 160 | |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 161 | FILTER_PORT |
| 162 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 163 | if (ipver == 4) { |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 164 | IPV4_CODE |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 165 | } else /* 6 */ { |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 166 | IPV6_CODE |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 167 | } |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 168 | |
Brendan Gregg | 8cd3efd | 2019-04-07 12:32:53 -0700 | [diff] [blame] | 169 | currsock.delete(&tid); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 170 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 171 | return 0; |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int trace_connect_v4_return(struct pt_regs *ctx) |
| 175 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 176 | return trace_connect_return(ctx, 4); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | int trace_connect_v6_return(struct pt_regs *ctx) |
| 180 | { |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 181 | return trace_connect_return(ctx, 6); |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 182 | } |
| 183 | """ |
| 184 | |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 185 | struct_init = { 'ipv4': |
| 186 | { 'count' : |
| 187 | """ |
| 188 | struct ipv4_flow_key_t flow_key = {}; |
| 189 | flow_key.saddr = skp->__sk_common.skc_rcv_saddr; |
| 190 | flow_key.daddr = skp->__sk_common.skc_daddr; |
| 191 | flow_key.dport = ntohs(dport); |
| 192 | ipv4_count.increment(flow_key);""", |
| 193 | 'trace' : |
| 194 | """ |
| 195 | struct ipv4_data_t data4 = {.pid = pid, .ip = ipver}; |
| 196 | data4.uid = bpf_get_current_uid_gid(); |
| 197 | data4.ts_us = bpf_ktime_get_ns() / 1000; |
| 198 | data4.saddr = skp->__sk_common.skc_rcv_saddr; |
| 199 | data4.daddr = skp->__sk_common.skc_daddr; |
| 200 | data4.dport = ntohs(dport); |
| 201 | bpf_get_current_comm(&data4.task, sizeof(data4.task)); |
| 202 | ipv4_events.perf_submit(ctx, &data4, sizeof(data4));""" |
| 203 | }, |
| 204 | 'ipv6': |
| 205 | { 'count' : |
| 206 | """ |
| 207 | struct ipv6_flow_key_t flow_key = {}; |
| 208 | bpf_probe_read(&flow_key.saddr, sizeof(flow_key.saddr), |
| 209 | skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
| 210 | bpf_probe_read(&flow_key.daddr, sizeof(flow_key.daddr), |
| 211 | skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
| 212 | flow_key.dport = ntohs(dport); |
| 213 | ipv6_count.increment(flow_key);""", |
| 214 | 'trace' : |
| 215 | """ |
| 216 | struct ipv6_data_t data6 = {.pid = pid, .ip = ipver}; |
| 217 | data6.uid = bpf_get_current_uid_gid(); |
| 218 | data6.ts_us = bpf_ktime_get_ns() / 1000; |
| 219 | bpf_probe_read(&data6.saddr, sizeof(data6.saddr), |
| 220 | skp->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
| 221 | bpf_probe_read(&data6.daddr, sizeof(data6.daddr), |
| 222 | skp->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
| 223 | data6.dport = ntohs(dport); |
| 224 | bpf_get_current_comm(&data6.task, sizeof(data6.task)); |
| 225 | ipv6_events.perf_submit(ctx, &data6, sizeof(data6));""" |
| 226 | } |
| 227 | } |
| 228 | |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 229 | # code substitutions |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 230 | if args.count: |
| 231 | bpf_text = bpf_text.replace("IPV4_CODE", struct_init['ipv4']['count']) |
| 232 | bpf_text = bpf_text.replace("IPV6_CODE", struct_init['ipv6']['count']) |
| 233 | else: |
| 234 | bpf_text = bpf_text.replace("IPV4_CODE", struct_init['ipv4']['trace']) |
| 235 | bpf_text = bpf_text.replace("IPV6_CODE", struct_init['ipv6']['trace']) |
| 236 | |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 237 | if args.pid: |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 238 | bpf_text = bpf_text.replace('FILTER_PID', |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 239 | 'if (pid != %s) { return 0; }' % args.pid) |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 240 | if args.port: |
| 241 | dports = [int(dport) for dport in args.port.split(',')] |
| 242 | dports_if = ' && '.join(['dport != %d' % ntohs(dport) for dport in dports]) |
| 243 | bpf_text = bpf_text.replace('FILTER_PORT', |
| 244 | 'if (%s) { currsock.delete(&pid); return 0; }' % dports_if) |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 245 | if args.uid: |
| 246 | bpf_text = bpf_text.replace('FILTER_UID', |
| 247 | 'if (uid != %s) { return 0; }' % args.uid) |
Alban Crequy | 32ab858 | 2020-03-22 16:06:44 +0100 | [diff] [blame] | 248 | bpf_text = filter_by_containers(args) + bpf_text |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 249 | |
| 250 | bpf_text = bpf_text.replace('FILTER_PID', '') |
| 251 | bpf_text = bpf_text.replace('FILTER_PORT', '') |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 252 | bpf_text = bpf_text.replace('FILTER_UID', '') |
chantra | 5293805 | 2016-09-10 09:44:50 -0700 | [diff] [blame] | 253 | |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 254 | if debug or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 255 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 256 | if args.ebpf: |
| 257 | exit() |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 258 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 259 | # process event |
| 260 | def print_ipv4_event(cpu, data, size): |
Xiaozhou Liu | 3156303 | 2019-02-14 14:33:58 +0800 | [diff] [blame] | 261 | event = b["ipv4_events"].event(data) |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 262 | global start_ts |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 263 | if args.timestamp: |
| 264 | if start_ts == 0: |
| 265 | start_ts = event.ts_us |
Gary Lin | 6c79331 | 2019-04-18 15:17:56 +0800 | [diff] [blame] | 266 | printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="") |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 267 | if args.print_uid: |
Gary Lin | 6c79331 | 2019-04-18 15:17:56 +0800 | [diff] [blame] | 268 | printb(b"%-6d" % event.uid, nl="") |
japroc | aed9b1e | 2019-01-04 20:21:46 +0300 | [diff] [blame] | 269 | printb(b"%-6d %-12.12s %-2d %-16s %-16s %-4d" % (event.pid, |
Yonghong Song | ebe1951 | 2019-01-10 14:54:16 -0800 | [diff] [blame] | 270 | event.task, event.ip, |
| 271 | inet_ntop(AF_INET, pack("I", event.saddr)).encode(), |
| 272 | inet_ntop(AF_INET, pack("I", event.daddr)).encode(), event.dport)) |
Brendan Gregg | 9e0b087 | 2016-03-28 12:11:45 -0700 | [diff] [blame] | 273 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 274 | def print_ipv6_event(cpu, data, size): |
Xiaozhou Liu | 3156303 | 2019-02-14 14:33:58 +0800 | [diff] [blame] | 275 | event = b["ipv6_events"].event(data) |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 276 | global start_ts |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 277 | if args.timestamp: |
| 278 | if start_ts == 0: |
| 279 | start_ts = event.ts_us |
Gary Lin | 6c79331 | 2019-04-18 15:17:56 +0800 | [diff] [blame] | 280 | printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="") |
Takuma Kume | b181a8e | 2019-01-10 05:49:59 +0900 | [diff] [blame] | 281 | if args.print_uid: |
Gary Lin | 6c79331 | 2019-04-18 15:17:56 +0800 | [diff] [blame] | 282 | printb(b"%-6d" % event.uid, nl="") |
japroc | aed9b1e | 2019-01-04 20:21:46 +0300 | [diff] [blame] | 283 | printb(b"%-6d %-12.12s %-2d %-16s %-16s %-4d" % (event.pid, |
Yonghong Song | ebe1951 | 2019-01-10 14:54:16 -0800 | [diff] [blame] | 284 | event.task, event.ip, |
| 285 | inet_ntop(AF_INET6, event.saddr).encode(), inet_ntop(AF_INET6, event.daddr).encode(), |
jeromemarchand | b96ebcd | 2018-10-10 01:58:15 +0200 | [diff] [blame] | 286 | event.dport)) |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 287 | |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 288 | def depict_cnt(counts_tab, l3prot='ipv4'): |
| 289 | for k, v in sorted(counts_tab.items(), key=lambda counts: counts[1].value, reverse=True): |
| 290 | depict_key = "" |
| 291 | if l3prot == 'ipv4': |
| 292 | depict_key = "%-25s %-25s %-20s" % ((inet_ntop(AF_INET, pack('I', k.saddr))), |
| 293 | inet_ntop(AF_INET, pack('I', k.daddr)), k.dport) |
| 294 | else: |
| 295 | depict_key = "%-25s %-25s %-20s" % ((inet_ntop(AF_INET6, k.saddr)), |
| 296 | inet_ntop(AF_INET6, k.daddr), k.dport) |
| 297 | |
| 298 | print ("%s %-10d" % (depict_key, v.value)) |
| 299 | |
Brendan Gregg | f06d3b4 | 2015-10-15 17:21:32 -0700 | [diff] [blame] | 300 | # initialize BPF |
| 301 | b = BPF(text=bpf_text) |
| 302 | b.attach_kprobe(event="tcp_v4_connect", fn_name="trace_connect_entry") |
| 303 | b.attach_kprobe(event="tcp_v6_connect", fn_name="trace_connect_entry") |
| 304 | b.attach_kretprobe(event="tcp_v4_connect", fn_name="trace_connect_v4_return") |
| 305 | b.attach_kretprobe(event="tcp_v6_connect", fn_name="trace_connect_v6_return") |
| 306 | |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 307 | print("Tracing connect ... Hit Ctrl-C to end") |
| 308 | if args.count: |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 309 | try: |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 310 | while 1: |
| 311 | sleep(99999999) |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 312 | except KeyboardInterrupt: |
Xiaozhou Liu | 9518a5b | 2019-08-02 01:13:53 +0800 | [diff] [blame] | 313 | pass |
| 314 | |
| 315 | # header |
| 316 | print("\n%-25s %-25s %-20s %-10s" % ( |
| 317 | "LADDR", "RADDR", "RPORT", "CONNECTS")) |
| 318 | depict_cnt(b["ipv4_count"]) |
| 319 | depict_cnt(b["ipv6_count"], l3prot='ipv6') |
| 320 | # read events |
| 321 | else: |
| 322 | # header |
| 323 | if args.timestamp: |
| 324 | print("%-9s" % ("TIME(s)"), end="") |
| 325 | if args.print_uid: |
| 326 | print("%-6s" % ("UID"), end="") |
| 327 | print("%-6s %-12s %-2s %-16s %-16s %-4s" % ("PID", "COMM", "IP", "SADDR", |
| 328 | "DADDR", "DPORT")) |
| 329 | |
| 330 | start_ts = 0 |
| 331 | |
| 332 | # read events |
| 333 | b["ipv4_events"].open_perf_buffer(print_ipv4_event) |
| 334 | b["ipv6_events"].open_perf_buffer(print_ipv6_event) |
| 335 | while 1: |
| 336 | try: |
| 337 | b.perf_buffer_poll() |
| 338 | except KeyboardInterrupt: |
| 339 | exit() |