blob: dc85abe73b68373c26710eca8b8331f30e3302aa [file] [log] [blame]
Brendan Gregg12c234f2020-09-16 14:34:29 -07001#!/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 Ananthakrishnan04893e32021-08-12 05:55:21 -07007# USAGE: tcpsynbl [-4 | -6] [-h]
8#
Brendan Gregg12c234f2020-09-16 14:34:29 -07009# 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
17from __future__ import print_function
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -070018import argparse
Brendan Gregg12c234f2020-09-16 14:34:29 -070019from bcc import BPF
20from time import sleep
21
22# load BPF program
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -070023bpf_text = """
Brendan Gregg12c234f2020-09-16 14:34:29 -070024#include <net/sock.h>
25
26typedef struct backlog_key {
27 u32 backlog;
28 u64 slot;
29} backlog_key_t;
30
31BPF_HISTOGRAM(dist, backlog_key_t);
32
33int do_entry(struct pt_regs *ctx) {
34 struct sock *sk = (struct sock *)PT_REGS_PARM1(ctx);
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -070035
Brendan Gregg12c234f2020-09-16 14:34:29 -070036 backlog_key_t key = {};
37 key.backlog = sk->sk_max_ack_backlog;
38 key.slot = bpf_log2l(sk->sk_ack_backlog);
zcy80242fb2021-07-02 00:12:32 +080039 dist.atomic_increment(key);
Brendan Gregg12c234f2020-09-16 14:34:29 -070040
41 return 0;
42};
Hariharan Ananthakrishnan04893e32021-08-12 05:55:21 -070043"""
44examples = """examples:
45 ./tcpsynbl # trace syn backlog
46 ./tcpsynbl -4 # trace IPv4 family only
47 ./tcpsynbl -6 # trace IPv6 family only
48"""
49parser = argparse.ArgumentParser(
50 description="Show TCP SYN backlog.",
51 formatter_class=argparse.RawDescriptionHelpFormatter,
52 epilog=examples)
53group = parser.add_mutually_exclusive_group()
54group.add_argument("-4", "--ipv4", action="store_true",
55 help="trace IPv4 family only")
56group.add_argument("-6", "--ipv6", action="store_true",
57 help="trace IPv6 family only")
58args = parser.parse_args()
59
60b = BPF(text=bpf_text)
61
62if args.ipv4:
63 b.attach_kprobe(event="tcp_v4_syn_recv_sock", fn_name="do_entry")
64elif args.ipv6:
65 b.attach_kprobe(event="tcp_v6_syn_recv_sock", fn_name="do_entry")
66else:
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 Gregg12c234f2020-09-16 14:34:29 -070069
70print("Tracing SYN backlog size. Ctrl-C to end.");
71
72try:
73 sleep(99999999)
74except KeyboardInterrupt:
75 print()
76
77dist = b.get_table("dist")
78dist.print_log2_hist("backlog", "backlog_max")