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 | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # tcpaccept Trace TCP accept()s. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 6 | # |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 7 | # USAGE: tcpaccept [-h] [-T] [-t] [-p PID] [-P PORTS] |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 8 | # |
| 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 Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 11 | # |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 12 | # Copyright (c) 2015 Brendan Gregg. |
| 13 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 14 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 15 | # 13-Oct-2015 Brendan Gregg Created this. |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 16 | # 14-Feb-2016 " " Switch to bpf_perf_output. |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 17 | |
| 18 | from __future__ import print_function |
| 19 | from bcc import BPF |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 20 | from socket import inet_ntop, AF_INET, AF_INET6 |
| 21 | from struct import pack |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 22 | import argparse |
japroc | aed9b1e | 2019-01-04 20:21:46 +0300 | [diff] [blame] | 23 | from bcc.utils import printb |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 24 | from time import strftime |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 25 | |
| 26 | # arguments |
| 27 | examples = """examples: |
Brendan Gregg | 000a4e6 | 2015-10-13 15:41:46 -0700 | [diff] [blame] | 28 | ./tcpaccept # trace all TCP accept()s |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 29 | ./tcpaccept -t # include timestamps |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 30 | ./tcpaccept -P 80,81 # only trace port 80 and 81 |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 31 | ./tcpaccept -p 181 # only trace PID 181 |
| 32 | """ |
| 33 | parser = argparse.ArgumentParser( |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 34 | description="Trace TCP accepts", |
| 35 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 36 | epilog=examples) |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 37 | parser.add_argument("-T", "--time", action="store_true", |
| 38 | help="include time column on output (HH:MM:SS)") |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 39 | parser.add_argument("-t", "--timestamp", action="store_true", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 40 | help="include timestamp on output") |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 41 | parser.add_argument("-p", "--pid", |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 42 | help="trace this PID only") |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 43 | parser.add_argument("-P", "--port", |
| 44 | help="comma-separated list of local ports to trace") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 45 | parser.add_argument("--ebpf", action="store_true", |
| 46 | help=argparse.SUPPRESS) |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 47 | args = parser.parse_args() |
| 48 | debug = 0 |
| 49 | |
| 50 | # define BPF program |
| 51 | bpf_text = """ |
| 52 | #include <uapi/linux/ptrace.h> |
| 53 | #include <net/sock.h> |
| 54 | #include <bcc/proto.h> |
| 55 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 56 | // separate data structs for ipv4 and ipv6 |
| 57 | struct ipv4_data_t { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 58 | u64 ts_us; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 59 | u32 pid; |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 60 | u32 saddr; |
| 61 | u32 daddr; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 62 | u64 ip; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 63 | u16 lport; |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 64 | u16 dport; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 65 | char task[TASK_COMM_LEN]; |
| 66 | }; |
| 67 | BPF_PERF_OUTPUT(ipv4_events); |
| 68 | |
| 69 | struct ipv6_data_t { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 70 | u64 ts_us; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 71 | u32 pid; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 72 | unsigned __int128 saddr; |
| 73 | unsigned __int128 daddr; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 74 | u64 ip; |
Joe Yin | 36ce112 | 2018-08-17 06:04:00 +0800 | [diff] [blame] | 75 | u16 lport; |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 76 | u16 dport; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 77 | char task[TASK_COMM_LEN]; |
| 78 | }; |
| 79 | BPF_PERF_OUTPUT(ipv6_events); |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 80 | """ |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 81 | |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 82 | # |
| 83 | # The following is the code for older kernels(Linux pre-4.16). |
| 84 | # It uses kprobes to instrument inet_csk_accept(). On Linux 4.16 and |
| 85 | # later, the sock:inet_sock_set_state tracepoint should be used instead, as |
| 86 | # is done by the code that follows this. |
| 87 | # |
| 88 | bpf_text_kprobe = """ |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 89 | int kretprobe__inet_csk_accept(struct pt_regs *ctx) |
| 90 | { |
Naveen N. Rao | 4afa96a | 2016-05-03 14:54:21 +0530 | [diff] [blame] | 91 | struct sock *newsk = (struct sock *)PT_REGS_RC(ctx); |
Brendan Gregg | 8cd3efd | 2019-04-07 12:32:53 -0700 | [diff] [blame^] | 92 | u32 pid = bpf_get_current_pid_tgid() >> 32; |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 93 | |
detailyang | 54044d5 | 2019-01-10 00:43:39 +0800 | [diff] [blame] | 94 | ##FILTER_PID## |
| 95 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 96 | if (newsk == NULL) |
| 97 | return 0; |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 98 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 99 | // check this is TCP |
| 100 | u8 protocol = 0; |
| 101 | // workaround for reading the sk_protocol bitfield: |
Joe Yin | 116bb40 | 2018-06-18 23:34:52 +0800 | [diff] [blame] | 102 | |
| 103 | // Following comments add by Joe Yin: |
| 104 | // Unfortunately,it can not work since Linux 4.10, |
| 105 | // because the sk_wmem_queued is not following the bitfield of sk_protocol. |
| 106 | // And the following member is sk_gso_max_segs. |
| 107 | // So, we can use this: |
| 108 | // bpf_probe_read(&protocol, 1, (void *)((u64)&newsk->sk_gso_max_segs) - 3); |
| 109 | // In order to diff the pre-4.10 and 4.10+ ,introduce the variables gso_max_segs_offset,sk_lingertime, |
| 110 | // sk_lingertime is closed to the gso_max_segs_offset,and |
| 111 | // the offset between the two members is 4 |
| 112 | |
| 113 | int gso_max_segs_offset = offsetof(struct sock, sk_gso_max_segs); |
| 114 | int sk_lingertime_offset = offsetof(struct sock, sk_lingertime); |
| 115 | |
| 116 | if (sk_lingertime_offset - gso_max_segs_offset == 4) |
| 117 | // 4.10+ with little endian |
| 118 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
Paul Chaignon | 8d78edd | 2018-06-29 07:47:44 +0200 | [diff] [blame] | 119 | protocol = *(u8 *)((u64)&newsk->sk_gso_max_segs - 3); |
Joe Yin | 116bb40 | 2018-06-18 23:34:52 +0800 | [diff] [blame] | 120 | else |
| 121 | // pre-4.10 with little endian |
Paul Chaignon | 8d78edd | 2018-06-29 07:47:44 +0200 | [diff] [blame] | 122 | protocol = *(u8 *)((u64)&newsk->sk_wmem_queued - 3); |
Joe Yin | 116bb40 | 2018-06-18 23:34:52 +0800 | [diff] [blame] | 123 | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 124 | // 4.10+ with big endian |
Paul Chaignon | 8d78edd | 2018-06-29 07:47:44 +0200 | [diff] [blame] | 125 | protocol = *(u8 *)((u64)&newsk->sk_gso_max_segs - 1); |
Joe Yin | 116bb40 | 2018-06-18 23:34:52 +0800 | [diff] [blame] | 126 | else |
| 127 | // pre-4.10 with big endian |
Paul Chaignon | 8d78edd | 2018-06-29 07:47:44 +0200 | [diff] [blame] | 128 | protocol = *(u8 *)((u64)&newsk->sk_wmem_queued - 1); |
Joe Yin | 116bb40 | 2018-06-18 23:34:52 +0800 | [diff] [blame] | 129 | #else |
| 130 | # error "Fix your compiler's __BYTE_ORDER__?!" |
| 131 | #endif |
| 132 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 133 | if (protocol != IPPROTO_TCP) |
| 134 | return 0; |
Brendan Gregg | 10e1b14 | 2015-10-13 16:35:25 -0700 | [diff] [blame] | 135 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 136 | // pull in details |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 137 | u16 family = 0, lport = 0, dport; |
Paul Chaignon | a9f96c0 | 2018-06-15 00:27:08 +0200 | [diff] [blame] | 138 | family = newsk->__sk_common.skc_family; |
| 139 | lport = newsk->__sk_common.skc_num; |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 140 | dport = newsk->__sk_common.skc_dport; |
| 141 | dport = ntohs(dport); |
| 142 | |
| 143 | ##FILTER_PORT## |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 144 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 145 | if (family == AF_INET) { |
| 146 | struct ipv4_data_t data4 = {.pid = pid, .ip = 4}; |
| 147 | data4.ts_us = bpf_ktime_get_ns() / 1000; |
Paul Chaignon | a9f96c0 | 2018-06-15 00:27:08 +0200 | [diff] [blame] | 148 | data4.saddr = newsk->__sk_common.skc_rcv_saddr; |
| 149 | data4.daddr = newsk->__sk_common.skc_daddr; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 150 | data4.lport = lport; |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 151 | data4.dport = dport; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 152 | bpf_get_current_comm(&data4.task, sizeof(data4.task)); |
| 153 | ipv4_events.perf_submit(ctx, &data4, sizeof(data4)); |
| 154 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 155 | } else if (family == AF_INET6) { |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 156 | struct ipv6_data_t data6 = {.pid = pid, .ip = 6}; |
| 157 | data6.ts_us = bpf_ktime_get_ns() / 1000; |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 158 | bpf_probe_read(&data6.saddr, sizeof(data6.saddr), |
| 159 | &newsk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32); |
| 160 | bpf_probe_read(&data6.daddr, sizeof(data6.daddr), |
| 161 | &newsk->__sk_common.skc_v6_daddr.in6_u.u6_addr32); |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 162 | data6.lport = lport; |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 163 | data6.dport = dport; |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 164 | bpf_get_current_comm(&data6.task, sizeof(data6.task)); |
| 165 | ipv6_events.perf_submit(ctx, &data6, sizeof(data6)); |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 166 | } |
| 167 | // else drop |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 168 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 169 | return 0; |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 170 | } |
| 171 | """ |
| 172 | |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 173 | bpf_text_tracepoint = """ |
| 174 | TRACEPOINT_PROBE(sock, inet_sock_set_state) |
| 175 | { |
| 176 | if (args->protocol != IPPROTO_TCP) |
| 177 | return 0; |
Xiaozhou Liu | 2ebdf16 | 2019-02-27 05:20:12 +0800 | [diff] [blame] | 178 | if (args->oldstate != TCP_SYN_RECV || args->newstate != TCP_ESTABLISHED) |
| 179 | return 0; |
Brendan Gregg | 8cd3efd | 2019-04-07 12:32:53 -0700 | [diff] [blame^] | 180 | u32 pid = bpf_get_current_pid_tgid() >> 32; |
detailyang | 54044d5 | 2019-01-10 00:43:39 +0800 | [diff] [blame] | 181 | |
| 182 | ##FILTER_PID## |
| 183 | |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 184 | // pull in details |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 185 | u16 family = 0, lport = 0, dport; |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 186 | family = args->family; |
| 187 | lport = args->sport; |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 188 | dport = args->dport; |
| 189 | |
| 190 | ##FILTER_PORT## |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 191 | |
| 192 | if (family == AF_INET) { |
| 193 | struct ipv4_data_t data4 = {.pid = pid, .ip = 4}; |
| 194 | data4.ts_us = bpf_ktime_get_ns() / 1000; |
| 195 | __builtin_memcpy(&data4.saddr, args->saddr, sizeof(data4.saddr)); |
| 196 | __builtin_memcpy(&data4.daddr, args->daddr, sizeof(data4.daddr)); |
| 197 | data4.lport = lport; |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 198 | data4.dport = dport; |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 199 | bpf_get_current_comm(&data4.task, sizeof(data4.task)); |
| 200 | ipv4_events.perf_submit(args, &data4, sizeof(data4)); |
| 201 | } else if (family == AF_INET6) { |
| 202 | struct ipv6_data_t data6 = {.pid = pid, .ip = 6}; |
| 203 | data6.ts_us = bpf_ktime_get_ns() / 1000; |
| 204 | __builtin_memcpy(&data6.saddr, args->saddr, sizeof(data6.saddr)); |
| 205 | __builtin_memcpy(&data6.daddr, args->daddr, sizeof(data6.daddr)); |
| 206 | data6.lport = lport; |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 207 | data6.dport = dport; |
Joe Yin | 365eade | 2018-06-21 13:41:03 +0800 | [diff] [blame] | 208 | bpf_get_current_comm(&data6.task, sizeof(data6.task)); |
| 209 | ipv6_events.perf_submit(args, &data6, sizeof(data6)); |
| 210 | } |
| 211 | // else drop |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | """ |
| 216 | |
| 217 | if (BPF.tracepoint_exists("sock", "inet_sock_set_state")): |
| 218 | bpf_text += bpf_text_tracepoint |
| 219 | else: |
| 220 | bpf_text += bpf_text_kprobe |
| 221 | |
| 222 | |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 223 | # code substitutions |
| 224 | if args.pid: |
detailyang | 54044d5 | 2019-01-10 00:43:39 +0800 | [diff] [blame] | 225 | bpf_text = bpf_text.replace('##FILTER_PID##', |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 226 | 'if (pid != %s) { return 0; }' % args.pid) |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 227 | else: |
detailyang | 54044d5 | 2019-01-10 00:43:39 +0800 | [diff] [blame] | 228 | bpf_text = bpf_text.replace('##FILTER_PID##', '') |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 229 | if args.port: |
| 230 | lports = [int(lport) for lport in args.port.split(',')] |
| 231 | lports_if = ' && '.join(['lport != %d' % lport for lport in lports]) |
| 232 | bpf_text = bpf_text.replace('##FILTER_PORT##', |
| 233 | 'if (%s) { return 0; }' % lports_if) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 234 | if debug or args.ebpf: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 235 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 236 | if args.ebpf: |
| 237 | exit() |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 238 | |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 239 | bpf_text = bpf_text.replace('##FILTER_PORT##', '') |
| 240 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 241 | # process event |
| 242 | def print_ipv4_event(cpu, data, size): |
Xiaozhou Liu | 51d62d3 | 2019-02-15 13:03:05 +0800 | [diff] [blame] | 243 | event = b["ipv4_events"].event(data) |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 244 | global start_ts |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 245 | if args.time: |
| 246 | print("%-9s" % strftime("%H:%M:%S"), end="") |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 247 | if args.timestamp: |
| 248 | if start_ts == 0: |
| 249 | start_ts = event.ts_us |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 250 | print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="") |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 251 | printb(b"%-7d %-12.12s %-2d %-16s %-5d %-16s %-5d" % (event.pid, |
Yonghong Song | ebe1951 | 2019-01-10 14:54:16 -0800 | [diff] [blame] | 252 | event.task, event.ip, |
| 253 | inet_ntop(AF_INET, pack("I", event.daddr)).encode(), |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 254 | event.dport, |
Yonghong Song | ebe1951 | 2019-01-10 14:54:16 -0800 | [diff] [blame] | 255 | inet_ntop(AF_INET, pack("I", event.saddr)).encode(), |
| 256 | event.lport)) |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 257 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 258 | def print_ipv6_event(cpu, data, size): |
Xiaozhou Liu | 51d62d3 | 2019-02-15 13:03:05 +0800 | [diff] [blame] | 259 | event = b["ipv6_events"].event(data) |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 260 | global start_ts |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 261 | if args.time: |
| 262 | print("%-9s" % strftime("%H:%M:%S"), end="") |
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 |
Mark Drayton | 11de298 | 2016-06-26 21:14:44 +0100 | [diff] [blame] | 266 | print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="") |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 267 | printb(b"%-7d %-12.12s %-2d %-16s %-5d %-16s %-5d" % (event.pid, |
Yonghong Song | ebe1951 | 2019-01-10 14:54:16 -0800 | [diff] [blame] | 268 | event.task, event.ip, |
| 269 | inet_ntop(AF_INET6, event.daddr).encode(), |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 270 | event.dport, |
Yonghong Song | ebe1951 | 2019-01-10 14:54:16 -0800 | [diff] [blame] | 271 | inet_ntop(AF_INET6, event.saddr).encode(), |
jeromemarchand | b96ebcd | 2018-10-10 01:58:15 +0200 | [diff] [blame] | 272 | event.lport)) |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 273 | |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 274 | # initialize BPF |
| 275 | b = BPF(text=bpf_text) |
| 276 | |
| 277 | # header |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 278 | if args.time: |
| 279 | print("%-9s" % ("TIME"), end="") |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 280 | if args.timestamp: |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 281 | print("%-9s" % ("TIME(s)"), end="") |
Xiaozhou Liu | 701bd73 | 2019-03-08 13:46:28 +0800 | [diff] [blame] | 282 | print("%-7s %-12s %-2s %-16s %-5s %-16s %-5s" % ("PID", "COMM", "IP", "RADDR", |
| 283 | "RPORT", "LADDR", "LPORT")) |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 284 | |
| 285 | start_ts = 0 |
| 286 | |
Brendan Gregg | 2482552 | 2016-02-14 16:32:29 -0800 | [diff] [blame] | 287 | # read events |
| 288 | b["ipv4_events"].open_perf_buffer(print_ipv4_event) |
| 289 | b["ipv6_events"].open_perf_buffer(print_ipv6_event) |
Brendan Gregg | 052f89c | 2015-10-13 15:35:58 -0700 | [diff] [blame] | 290 | while 1: |
Jerome Marchand | 5167127 | 2018-12-19 01:57:24 +0100 | [diff] [blame] | 291 | try: |
| 292 | b.perf_buffer_poll() |
| 293 | except KeyboardInterrupt: |
| 294 | exit() |