forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpcong.py
executable file
·559 lines (509 loc) · 17.5 KB
/
tcpcong.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/usr/bin/env python
# @lint-avoid-python-3-compatibility-imports
#
# tcpcong Measure tcp congestion control status duration.
# For Linux, uses BCC, eBPF.
#
# USAGE: tcpcong [-h] [-T] [-L] [-R] [-m] [-d] [interval] [outputs]
#
# Copyright (c) Ping Gan.
#
# 27-Jan-2022 Ping Gan Created this.
from __future__ import print_function
from bcc import BPF
from time import sleep, strftime
from struct import pack
from socket import inet_ntop, AF_INET, AF_INET6
from struct import pack
import argparse
examples = """examples:
./tcpcong # show tcp congestion status duration
./tcpcong 1 10 # show 1 second summaries, 10 times
./tcpcong -L 3000-3006 1 # 1s summaries, local port 3000-3006
./tcpcong -R 5000-5005 1 # 1s summaries, remote port 5000-5005
./tcpcong -uT 1 # 1s summaries, microseconds, and timestamps
./tcpcong -d # show the duration as histograms
"""
parser = argparse.ArgumentParser(
description="Summarize tcp socket congestion control status duration",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=examples)
parser.add_argument("-L", "--localport",
help="trace local ports only")
parser.add_argument("-R", "--remoteport",
help="trace the dest ports only")
parser.add_argument("-T", "--timestamp", action="store_true",
help="include timestamp on output")
parser.add_argument("-d", "--dist", action="store_true",
help="show distributions as histograms")
parser.add_argument("-u", "--microseconds", action="store_true",
help="output in microseconds")
parser.add_argument("interval", nargs="?", default=99999999,
help="output interval, in seconds")
parser.add_argument("outputs", nargs="?", default=99999999,
help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args()
countdown = int(args.outputs)
debug = 0
start_rport = end_rport = -1
if args.remoteport:
rports = args.remoteport.split("-")
if (len(rports) != 2) and (len(rports) != 1):
print("unrecognized remote port range")
exit(1)
if len(rports) == 2:
start_rport = int(rports[0])
end_rport = int(rports[1])
else:
start_rport = int(rports[0])
end_rport = int(rports[0])
if start_rport > end_rport:
tmp = start_rport
start_rport = end_rport
end_rport = tmp
start_lport = end_lport = -1
if args.localport:
lports = args.localport.split("-")
if (len(lports) != 2) and (len(lports) != 1):
print("unrecognized local port range")
exit(1)
if len(lports) == 2:
start_lport = int(lports[0])
end_lport = int(lports[1])
else:
start_lport = int(lports[0])
end_lport = int(lports[0])
if start_lport > end_lport:
tmp = start_lport
start_lport = end_lport
end_lport = tmp
# define BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <bcc/proto.h>
#include <net/tcp.h>
#include <net/inet_connection_sock.h>
typedef struct ipv4_flow_key {
u32 saddr;
u32 daddr;
u16 lport;
u16 dport;
} ipv4_flow_key_t;
typedef struct ipv6_flow_key {
unsigned __int128 saddr;
unsigned __int128 daddr;
u16 lport;
u16 dport;
} ipv6_flow_key_t;
typedef struct process_key {
char comm[TASK_COMM_LEN];
u32 tid;
} process_key_t;
typedef struct ipv4_flow_val {
ipv4_flow_key_t ipv4_key;
u16 cong_state;
} ipv4_flow_val_t;
typedef struct ipv6_flow_val {
ipv6_flow_key_t ipv6_key;
u16 cong_state;
} ipv6_flow_val_t;
BPF_HASH(start_ipv4, process_key_t, ipv4_flow_val_t);
BPF_HASH(start_ipv6, process_key_t, ipv6_flow_val_t);
SOCK_STORE_DEF
typedef struct data_val {
DEF_TEXT
u64 last_ts;
u16 last_cong_stat;
} data_val_t;
typedef struct cong {
u8 cong_stat:5,
ca_inited:1,
ca_setsockopt:1,
ca_dstlocked:1;
} cong_status_t;
BPF_HASH(ipv4_stat, ipv4_flow_key_t, data_val_t);
BPF_HASH(ipv6_stat, ipv6_flow_key_t, data_val_t);
HIST_TABLE
static int entry_state_update_func(struct sock *sk)
{
u16 dport = 0, lport = 0;
u32 tid = bpf_get_current_pid_tgid();
process_key_t key = {0};
bpf_get_current_comm(&key.comm, sizeof(key.comm));
key.tid = tid;
u64 family = sk->__sk_common.skc_family;
struct inet_connection_sock *icsk = inet_csk(sk);
cong_status_t cong_status;
bpf_probe_read_kernel(&cong_status, sizeof(cong_status),
(void *)((long)&icsk->icsk_retransmits) - 1);
if (family == AF_INET) {
ipv4_flow_val_t ipv4_val = {0};
ipv4_val.ipv4_key.saddr = sk->__sk_common.skc_rcv_saddr;
ipv4_val.ipv4_key.daddr = sk->__sk_common.skc_daddr;
ipv4_val.ipv4_key.lport = sk->__sk_common.skc_num;
dport = sk->__sk_common.skc_dport;
dport = ntohs(dport);
lport = ipv4_val.ipv4_key.lport;
FILTER_LPORT
FILTER_DPORT
ipv4_val.ipv4_key.dport = dport;
ipv4_val.cong_state = cong_status.cong_stat + 1;
start_ipv4.update(&key, &ipv4_val);
} else if (family == AF_INET6) {
ipv6_flow_val_t ipv6_val = {0};
bpf_probe_read_kernel(&ipv6_val.ipv6_key.saddr,
sizeof(ipv6_val.ipv6_key.saddr),
&sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr32);
bpf_probe_read_kernel(&ipv6_val.ipv6_key.daddr,
sizeof(ipv6_val.ipv6_key.daddr),
&sk->__sk_common.skc_v6_daddr.in6_u.u6_addr32);
ipv6_val.ipv6_key.lport = sk->__sk_common.skc_num;
dport = sk->__sk_common.skc_dport;
dport = ntohs(dport);
lport = ipv6_val.ipv6_key.lport;
FILTER_LPORT
FILTER_DPORT
ipv6_val.ipv6_key.dport = dport;
ipv6_val.cong_state = cong_status.cong_stat + 1;
start_ipv6.update(&key, &ipv6_val);
}
SOCK_STORE_ADD
return 0;
}
static int ret_state_update_func(struct sock *sk)
{
u64 ts, ts1;
u16 family, last_cong_state;
u16 dport = 0, lport = 0;
u32 tid = bpf_get_current_pid_tgid();
process_key_t key = {0};
bpf_get_current_comm(&key.comm, sizeof(key.comm));
key.tid = tid;
struct inet_connection_sock *icsk = inet_csk(sk);
cong_status_t cong_status;
bpf_probe_read_kernel(&cong_status, sizeof(cong_status),
(void *)((long)&icsk->icsk_retransmits) - 1);
data_val_t *datap, data = {0};
STATE_KEY
bpf_probe_read_kernel(&family, sizeof(family),
&sk->__sk_common.skc_family);
if (family == AF_INET) {
ipv4_flow_val_t *val4 = start_ipv4.lookup(&key);
if (val4 == 0) {
SOCK_STORE_DEL
return 0; //missed
}
ipv4_flow_key_t keyv4 = {0};
bpf_probe_read_kernel(&keyv4, sizeof(ipv4_flow_key_t),
&(val4->ipv4_key));
dport = keyv4.dport;
lport = keyv4.lport;
FILTER_LPORT
FILTER_DPORT
datap = ipv4_stat.lookup(&keyv4);
if (datap == 0) {
data.last_ts = bpf_ktime_get_ns();
data.last_cong_stat = val4->cong_state;
ipv4_stat.update(&keyv4, &data);
} else {
last_cong_state = val4->cong_state;
if ((cong_status.cong_stat + 1) != last_cong_state) {
ts1 = bpf_ktime_get_ns();
ts = ts1 - datap->last_ts;
datap->last_ts = ts1;
datap->last_cong_stat = cong_status.cong_stat + 1;
ts /= 1000;
STORE
}
}
start_ipv4.delete(&key);
} else if (family == AF_INET6) {
ipv6_flow_val_t *val6 = start_ipv6.lookup(&key);
if (val6 == 0) {
SOCK_STORE_DEL
return 0; //missed
}
ipv6_flow_key_t keyv6 = {0};
bpf_probe_read_kernel(&keyv6, sizeof(ipv6_flow_key_t),
&(val6->ipv6_key));
dport = keyv6.dport;
lport = keyv6.lport;
FILTER_LPORT
FILTER_DPORT
datap = ipv6_stat.lookup(&keyv6);
if (datap == 0) {
data.last_ts = bpf_ktime_get_ns();
data.last_cong_stat = val6->cong_state;
ipv6_stat.update(&keyv6, &data);
} else {
last_cong_state = val6->cong_state;
if ((cong_status.cong_stat + 1) != last_cong_state) {
ts1 = bpf_ktime_get_ns();
ts = ts1 - datap->last_ts;
datap->last_ts = ts1;
datap->last_cong_stat = (cong_status.cong_stat + 1);
ts /= 1000;
STORE
}
}
start_ipv6.delete(&key);
}
SOCK_STORE_DEL
return 0;
}
"""
kprobe_program = """
int entry_func(struct pt_regs *ctx, struct sock *sk)
{
return entry_state_update_func(sk);
}
int ret_func(struct pt_regs *ctx)
{
u32 tid = bpf_get_current_pid_tgid();
process_key_t key = {0};
bpf_get_current_comm(&key.comm, sizeof(key.comm));
key.tid = tid;
struct sock **sockpp;
sockpp = sock_store.lookup(&key);
if (sockpp == 0) {
return 0; //miss the entry
}
struct sock *sk = *sockpp;
return ret_state_update_func(sk);
}
"""
kfunc_program = """
KFUNC_PROBE(tcp_fastretrans_alert, struct sock *sk)
{
return entry_state_update_func(sk);
}
KRETFUNC_PROBE(tcp_fastretrans_alert, struct sock *sk)
{
return ret_state_update_func(sk);
}
KFUNC_PROBE(tcp_enter_cwr, struct sock *sk)
{
return entry_state_update_func(sk);
}
KRETFUNC_PROBE(tcp_enter_cwr, struct sock *sk)
{
return ret_state_update_func(sk);
}
KFUNC_PROBE(tcp_enter_loss, struct sock *sk)
{
return entry_state_update_func(sk);
}
KRETFUNC_PROBE(tcp_enter_loss, struct sock *sk)
{
return ret_state_update_func(sk);
}
KFUNC_PROBE(tcp_enter_recovery, struct sock *sk)
{
return entry_state_update_func(sk);
}
KRETFUNC_PROBE(tcp_enter_recovery, struct sock *sk)
{
return ret_state_update_func(sk);
}
KFUNC_PROBE(tcp_process_tlp_ack, struct sock *sk)
{
return entry_state_update_func(sk);
}
KRETFUNC_PROBE(tcp_process_tlp_ack, struct sock *sk)
{
return ret_state_update_func(sk);
}
"""
# code replace
is_support_kfunc = BPF.support_kfunc()
if is_support_kfunc:
bpf_text += kfunc_program
bpf_text = bpf_text.replace('SOCK_STORE_DEF', '')
bpf_text = bpf_text.replace('SOCK_STORE_ADD', '')
bpf_text = bpf_text.replace('SOCK_STORE_DEL', '')
else:
bpf_text += kprobe_program
bpf_text = bpf_text.replace('SOCK_STORE_DEF',
'BPF_HASH(sock_store, process_key_t, struct sock *);')
bpf_text = bpf_text.replace('SOCK_STORE_ADD',
'sock_store.update(&key, &sk);')
bpf_text = bpf_text.replace('SOCK_STORE_DEL',
'sock_store.delete(&key);')
if args.localport:
bpf_text = bpf_text.replace('FILTER_LPORT',
'if (lport < %d || lport > %d) { return 0; }'
% (start_lport, end_lport))
else:
bpf_text = bpf_text.replace('FILTER_LPORT', '')
if args.remoteport:
bpf_text = bpf_text.replace('FILTER_DPORT',
'if (dport < %d || dport > %d) { return 0; }'
% (start_rport, end_rport))
else:
bpf_text = bpf_text.replace('FILTER_DPORT', '')
table_def_text = """
u64 open_dura;
u64 loss_dura;
u64 disorder_dura;
u64 recover_dura;
u64 cwr_dura;
u64 total_changes;
"""
store_text = """
datap->total_changes += 1;
if (last_cong_state == (TCP_CA_Open + 1)) {
datap->open_dura += ts;
} else if (last_cong_state == (TCP_CA_Disorder + 1)) {
datap->disorder_dura += ts;
} else if (last_cong_state == (TCP_CA_CWR + 1)) {
datap->cwr_dura += ts;
} else if (last_cong_state == (TCP_CA_Recovery + 1)) {
datap->recover_dura += ts;
} else if (last_cong_state == (TCP_CA_Loss + 1)) {
datap->loss_dura += ts;
}
"""
store_dist_text = """
if (last_cong_state == (TCP_CA_Open + 1)) {
key_s.state = TCP_CA_Open;
} else if (last_cong_state == (TCP_CA_Disorder + 1)) {
key_s.state = TCP_CA_Disorder;
} else if (last_cong_state == (TCP_CA_CWR + 1)) {
key_s.state = TCP_CA_CWR;
} else if (last_cong_state == (TCP_CA_Recovery + 1)) {
key_s.state = TCP_CA_Recovery;
} else if (last_cong_state == (TCP_CA_Loss + 1)) {
key_s.state = TCP_CA_Loss;
}
TIME_UNIT
key_s.slot = bpf_log2l(ts);
dist.atomic_increment(key_s);
"""
hist_table_text = """
typedef struct congest_state_key {
u32 state;
u64 slot;
}congest_state_key_t;
BPF_HISTOGRAM(dist, congest_state_key_t);
"""
if args.dist:
bpf_text = bpf_text.replace('DEF_TEXT', '')
bpf_text = bpf_text.replace('STORE', store_dist_text)
bpf_text = bpf_text.replace('STATE_KEY',
'congest_state_key_t key_s = {0};')
bpf_text = bpf_text.replace('HIST_TABLE', hist_table_text)
if args.microseconds:
bpf_text = bpf_text.replace('TIME_UNIT', '')
else:
bpf_text = bpf_text.replace('TIME_UNIT', 'ts /= 1000;')
else:
bpf_text = bpf_text.replace('DEF_TEXT', table_def_text)
bpf_text = bpf_text.replace('STORE', store_text)
bpf_text = bpf_text.replace('STATE_KEY', '')
bpf_text = bpf_text.replace('HIST_TABLE', '')
if debug or args.ebpf:
print(bpf_text)
if args.ebpf:
exit()
# load BPF program
b = BPF(text=bpf_text)
if not is_support_kfunc:
# all the tcp congestion control status update functions
# are called by below 5 functions.
b.attach_kprobe(event="tcp_fastretrans_alert", fn_name="entry_func")
b.attach_kretprobe(event="tcp_fastretrans_alert", fn_name="ret_func")
b.attach_kprobe(event="tcp_enter_cwr", fn_name="entry_func")
b.attach_kretprobe(event="tcp_enter_cwr", fn_name="ret_func")
b.attach_kprobe(event="tcp_process_tlp_ack", fn_name="entry_func")
b.attach_kretprobe(event="tcp_process_tlp_ack", fn_name="ret_func")
b.attach_kprobe(event="tcp_enter_loss", fn_name="entry_func")
b.attach_kretprobe(event="tcp_enter_loss", fn_name="ret_func")
b.attach_kprobe(event="tcp_enter_recovery", fn_name="entry_func")
b.attach_kretprobe(event="tcp_enter_recovery", fn_name="ret_func")
print("Tracing tcp congestion control status duration... Hit Ctrl-C to end.")
def cong_state_to_name(state):
# this need to match with kernel state
state_name = ["open", "disorder", "cwr", "recovery", "loss"]
return state_name[state]
# output
exiting = 0 if args.interval else 1
ipv6_stat = b.get_table("ipv6_stat")
ipv4_stat = b.get_table("ipv4_stat")
if args.dist:
dist = b.get_table("dist")
label = "ms"
if args.microseconds:
label = "us"
while (1):
try:
sleep(int(args.interval))
except KeyboardInterrupt:
exiting = 1
print()
if args.timestamp:
print("%-8s\n" % strftime("%H:%M:%S"), end="")
if args.dist:
if args.microseconds:
dist.print_log2_hist("usecs", "tcp_congest_state",
section_print_fn=cong_state_to_name)
else:
dist.print_log2_hist("msecs", "tcp_congest_state",
section_print_fn=cong_state_to_name)
dist.clear()
else:
if ipv4_stat:
print("%-21s% -21s %-7s %-6s %-7s %-7s %-6s %-5s" % ("LAddrPort",
"RAddrPort", "Open_" + label, "Dod_" + label,
"Rcov_" + label, "Cwr_" + label, "Los_" + label, "Chgs"))
laddr = ""
raddr = ""
for k, v in sorted(ipv4_stat.items(), key=lambda ipv4_stat: ipv4_stat[0].lport):
laddr = inet_ntop(AF_INET, pack("I", k.saddr))
raddr = inet_ntop(AF_INET, pack("I", k.daddr))
open_dura = v.open_dura
disorder_dura = v.disorder_dura
recover_dura = v.recover_dura
cwr_dura = v.cwr_dura
loss_dura = v.loss_dura
if not args.microseconds:
open_dura /= 1000
disorder_dura /= 1000
recover_dura /= 1000
cwr_dura /= 1000
loss_dura /= 1000
if v.total_changes != 0:
print("%-21s %-21s %-7d %-6d %-7d %-7d %-6d %-5d" % (laddr +
"/" + str(k.lport), raddr + "/" + str(k.dport), open_dura,
disorder_dura, recover_dura, cwr_dura, loss_dura,
v.total_changes))
if ipv6_stat:
print("%-32s %-32s %-7s %-6s %-7s %-7s %-6s %-5s" % ("LAddrPort6",
"RAddrPort6", "Open_" + label, "Dod_" + label, "Rcov_" + label,
"Cwr_" + label, "Los_" + label, "Chgs"))
for k, v in sorted(ipv6_stat.items(), key=lambda ipv6_stat: ipv6_stat[0].lport):
laddr = inet_ntop(AF_INET6, bytes(k.saddr))
raddr = inet_ntop(AF_INET6, bytes(k.daddr))
open_dura = v.open_dura
disorder_dura = v.disorder_dura
recover_dura = v.recover_dura
cwr_dura = v.cwr_dura
loss_dura = v.loss_dura
if not args.microseconds:
open_dura /= 1000
disorder_dura /= 1000
recover_dura /= 1000
cwr_dura /= 1000
loss_dura /= 1000
if v.total_changes != 0:
print("%-32s %-32s %-7d %-7d %-7d %-6d %-6d %-5d" % (laddr +
"/" + str(k.lport), raddr + "/" + str(k.dport), open_dura,
disorder_dura, recover_dura, cwr_dura, loss_dura,
v.total_changes))
ipv4_stat.clear()
ipv6_stat.clear()
countdown -= 1
if exiting or countdown == 0:
exit()