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