blob: c24eb960772a130bac50f11f473e097df6f0cd4b [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#
7# Copyright (c) 2019 Brendan Gregg.
8# Licensed under the Apache License, Version 2.0 (the "License").
9# This was originally created for the BPF Performance Tools book
10# published by Addison Wesley. ISBN-13: 9780136554820
11# When copying or porting, include this comment.
12#
13# 03-Jul-2019 Brendan Gregg Ported from bpftrace to BCC.
14
15from __future__ import print_function
16from bcc import BPF
17from time import sleep
18
19# load BPF program
20b = BPF(text="""
21#include <net/sock.h>
22
23typedef struct backlog_key {
24 u32 backlog;
25 u64 slot;
26} backlog_key_t;
27
28BPF_HISTOGRAM(dist, backlog_key_t);
29
30int do_entry(struct pt_regs *ctx) {
31 struct sock *sk = (struct sock *)PT_REGS_PARM1(ctx);
32
33 backlog_key_t key = {};
34 key.backlog = sk->sk_max_ack_backlog;
35 key.slot = bpf_log2l(sk->sk_ack_backlog);
36 dist.increment(key);
37
38 return 0;
39};
40""")
41b.attach_kprobe(event="tcp_v4_syn_recv_sock", fn_name="do_entry")
42b.attach_kprobe(event="tcp_v6_syn_recv_sock", fn_name="do_entry")
43
44print("Tracing SYN backlog size. Ctrl-C to end.");
45
46try:
47 sleep(99999999)
48except KeyboardInterrupt:
49 print()
50
51dist = b.get_table("dist")
52dist.print_log2_hist("backlog", "backlog_max")