Brendan Gregg | 12c234f | 2020-09-16 14:34:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
| 4 | # tcpsynbl Show TCP SYN backlog. |
| 5 | # For Linux, uses BCC, eBPF. Embedded C. |
| 6 | # |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 7 | # USAGE: tcpsynbl [-4 | -6] [-h] |
| 8 | # |
Brendan Gregg | 12c234f | 2020-09-16 14:34:29 -0700 | [diff] [blame] | 9 | # Copyright (c) 2019 Brendan Gregg. |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License"). |
| 11 | # This was originally created for the BPF Performance Tools book |
| 12 | # published by Addison Wesley. ISBN-13: 9780136554820 |
| 13 | # When copying or porting, include this comment. |
| 14 | # |
| 15 | # 03-Jul-2019 Brendan Gregg Ported from bpftrace to BCC. |
| 16 | |
| 17 | from __future__ import print_function |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 18 | import argparse |
Brendan Gregg | 12c234f | 2020-09-16 14:34:29 -0700 | [diff] [blame] | 19 | from bcc import BPF |
| 20 | from time import sleep |
| 21 | |
| 22 | # load BPF program |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 23 | bpf_text = """ |
Brendan Gregg | 12c234f | 2020-09-16 14:34:29 -0700 | [diff] [blame] | 24 | #include <net/sock.h> |
| 25 | |
| 26 | typedef struct backlog_key { |
| 27 | u32 backlog; |
| 28 | u64 slot; |
| 29 | } backlog_key_t; |
| 30 | |
| 31 | BPF_HISTOGRAM(dist, backlog_key_t); |
| 32 | |
| 33 | int do_entry(struct pt_regs *ctx) { |
| 34 | struct sock *sk = (struct sock *)PT_REGS_PARM1(ctx); |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 35 | |
Brendan Gregg | 12c234f | 2020-09-16 14:34:29 -0700 | [diff] [blame] | 36 | backlog_key_t key = {}; |
| 37 | key.backlog = sk->sk_max_ack_backlog; |
| 38 | key.slot = bpf_log2l(sk->sk_ack_backlog); |
zcy | 80242fb | 2021-07-02 00:12:32 +0800 | [diff] [blame] | 39 | dist.atomic_increment(key); |
Brendan Gregg | 12c234f | 2020-09-16 14:34:29 -0700 | [diff] [blame] | 40 | |
| 41 | return 0; |
| 42 | }; |
Hariharan Ananthakrishnan | 04893e3 | 2021-08-12 05:55:21 -0700 | [diff] [blame] | 43 | """ |
| 44 | examples = """examples: |
| 45 | ./tcpsynbl # trace syn backlog |
| 46 | ./tcpsynbl -4 # trace IPv4 family only |
| 47 | ./tcpsynbl -6 # trace IPv6 family only |
| 48 | """ |
| 49 | parser = argparse.ArgumentParser( |
| 50 | description="Show TCP SYN backlog.", |
| 51 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 52 | epilog=examples) |
| 53 | group = parser.add_mutually_exclusive_group() |
| 54 | group.add_argument("-4", "--ipv4", action="store_true", |
| 55 | help="trace IPv4 family only") |
| 56 | group.add_argument("-6", "--ipv6", action="store_true", |
| 57 | help="trace IPv6 family only") |
| 58 | args = parser.parse_args() |
| 59 | |
| 60 | b = BPF(text=bpf_text) |
| 61 | |
| 62 | if args.ipv4: |
| 63 | b.attach_kprobe(event="tcp_v4_syn_recv_sock", fn_name="do_entry") |
| 64 | elif args.ipv6: |
| 65 | b.attach_kprobe(event="tcp_v6_syn_recv_sock", fn_name="do_entry") |
| 66 | else: |
| 67 | b.attach_kprobe(event="tcp_v4_syn_recv_sock", fn_name="do_entry") |
| 68 | b.attach_kprobe(event="tcp_v6_syn_recv_sock", fn_name="do_entry") |
Brendan Gregg | 12c234f | 2020-09-16 14:34:29 -0700 | [diff] [blame] | 69 | |
| 70 | print("Tracing SYN backlog size. Ctrl-C to end."); |
| 71 | |
| 72 | try: |
| 73 | sleep(99999999) |
| 74 | except KeyboardInterrupt: |
| 75 | print() |
| 76 | |
| 77 | dist = b.get_table("dist") |
| 78 | dist.print_log2_hist("backlog", "backlog_max") |