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