xref: /freebsd/share/dtrace/tcpdebug (revision 38c63bdc46252d4d8cd313dff4183ec4546d26d9)
15d06879aSGeorge V. Neville-Neil#!/usr/sbin/dtrace -s
25d06879aSGeorge V. Neville-Neil/*
35d06879aSGeorge V. Neville-Neil * Copyright (c) 2015 George V. Neville-Neil
45d06879aSGeorge V. Neville-Neil * All rights reserved.
55d06879aSGeorge V. Neville-Neil *
65d06879aSGeorge V. Neville-Neil * Redistribution and use in source and binary forms, with or without
75d06879aSGeorge V. Neville-Neil * modification, are permitted provided that the following conditions
85d06879aSGeorge V. Neville-Neil * are met:
95d06879aSGeorge V. Neville-Neil * 1. Redistributions of source code must retain the above copyright
105d06879aSGeorge V. Neville-Neil *    notice, this list of conditions and the following disclaimer.
115d06879aSGeorge V. Neville-Neil * 2. Redistributions in binary form must reproduce the above copyright
125d06879aSGeorge V. Neville-Neil *    notice, this list of conditions and the following disclaimer in the
135d06879aSGeorge V. Neville-Neil *    documentation and/or other materials provided with the distribution.
145d06879aSGeorge V. Neville-Neil *
155d06879aSGeorge V. Neville-Neil * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
165d06879aSGeorge V. Neville-Neil * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
175d06879aSGeorge V. Neville-Neil * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
185d06879aSGeorge V. Neville-Neil * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
195d06879aSGeorge V. Neville-Neil * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
205d06879aSGeorge V. Neville-Neil * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
215d06879aSGeorge V. Neville-Neil * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
225d06879aSGeorge V. Neville-Neil * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
235d06879aSGeorge V. Neville-Neil * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
245d06879aSGeorge V. Neville-Neil * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
255d06879aSGeorge V. Neville-Neil * SUCH DAMAGE.
265d06879aSGeorge V. Neville-Neil *
275d06879aSGeorge V. Neville-Neil * The tcpdebug D script uses the tcp:kernel::debug tracepoints
285d06879aSGeorge V. Neville-Neil * to replicate the action of turning on TCPDEBUG in a kernel configuration.
295d06879aSGeorge V. Neville-Neil *
305d06879aSGeorge V. Neville-Neil * A TCP debug statement shows a connection's
315d06879aSGeorge V. Neville-Neil *
325d06879aSGeorge V. Neville-Neil * direction:	input, output, user, drop
335d06879aSGeorge V. Neville-Neil * state:	CLOSED,	LISTEN,	SYN_SENT, SYN_RCVD, ESTABLISHED,
345d06879aSGeorge V. Neville-Neil * 		CLOSE_WAIT, FIN_WAIT_1, CLOSING, LAST_ACK, FIN_WAIT_2,TIME_WAIT
355d06879aSGeorge V. Neville-Neil * sequence:	sequence space
365d06879aSGeorge V. Neville-Neil *
375d06879aSGeorge V. Neville-Neil * congestion:	rcv_nxt, rcv_wnd, rcv_up, snd_una, snd_nxt, snx_max,
385d06879aSGeorge V. Neville-Neil *		snd_wl1, snd_wl2, snd_wnd
395d06879aSGeorge V. Neville-Neil *
405d06879aSGeorge V. Neville-Neil * NOTE: Only sockets with SO_DEBUG set will be shown.
415d06879aSGeorge V. Neville-Neil *
425d06879aSGeorge V. Neville-Neil * Usage: tcpdebug
435d06879aSGeorge V. Neville-Neil */
445d06879aSGeorge V. Neville-Neil
455d06879aSGeorge V. Neville-Neil#pragma D option quiet
465d06879aSGeorge V. Neville-Neiltcp:kernel::debug-input
475d06879aSGeorge V. Neville-Neil/args[0]->tcps_debug/
485d06879aSGeorge V. Neville-Neil{
495d06879aSGeorge V. Neville-Neil	seq = args[1]->tcp_seq;
505d06879aSGeorge V. Neville-Neil	ack = args[1]->tcp_ack;
515d06879aSGeorge V. Neville-Neil	len = args[2]->ip_plength - sizeof(struct tcphdr);
525d06879aSGeorge V. Neville-Neil	flags = args[1]->tcp_flags;
535d06879aSGeorge V. Neville-Neil
545d06879aSGeorge V. Neville-Neil	printf("%p %s: input [%xu..%xu]", arg0,
555d06879aSGeorge V. Neville-Neil	       tcp_state_string[args[0]->tcps_state], seq, seq + len);
565d06879aSGeorge V. Neville-Neil
575d06879aSGeorge V. Neville-Neil	printf("@%x, urp=%x", ack, args[1]->tcp_urgent);
585d06879aSGeorge V. Neville-Neil
595d06879aSGeorge V. Neville-Neil	printf("%s", flags != 0 ? "<" : "");
605d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_SYN ? "SYN," :"");
615d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_ACK ? "ACK," :"");
625d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_FIN ? "FIN," :"");
635d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_RST ? "RST," :"");
645d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_PUSH ? "PUSH," :"");
655d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_URG ? "URG," :"");
665d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_ECE ? "ECE," :"");
67*38c63bdcSRichard Scheffenegger	printf("%s", flags & TH_CWR ? "CWR," :"");
68*38c63bdcSRichard Scheffenegger	printf("%s", flags & TH_AE ? "AE" :"");
695d06879aSGeorge V. Neville-Neil	printf("%s", flags != 0 ? ">" : "");
705d06879aSGeorge V. Neville-Neil
715d06879aSGeorge V. Neville-Neil	printf("\n");
725d06879aSGeorge V. Neville-Neil	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
735d06879aSGeorge V. Neville-Neil	       args[0]->tcps_rnxt, args[0]->tcps_rwnd, args[0]->tcps_rup,
745d06879aSGeorge V. Neville-Neil	       args[0]->tcps_suna, args[0]->tcps_snxt, args[0]->tcps_smax);
755d06879aSGeorge V. Neville-Neil	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
765d06879aSGeorge V. Neville-Neil	       args[0]->tcps_swl1, args[0]->tcps_swl2, args[0]->tcps_swnd);
775d06879aSGeorge V. Neville-Neil
785d06879aSGeorge V. Neville-Neil}
795d06879aSGeorge V. Neville-Neil
805d06879aSGeorge V. Neville-Neiltcp:kernel::debug-output
815d06879aSGeorge V. Neville-Neil/args[0]->tcps_debug/
825d06879aSGeorge V. Neville-Neil{
835d06879aSGeorge V. Neville-Neil	seq = args[1]->tcp_seq;
845d06879aSGeorge V. Neville-Neil	ack = args[1]->tcp_ack;
85342af4d5SGeorge V. Neville-Neil	len = args[2]->ip_plength - sizeof(struct tcphdr);
86342af4d5SGeorge V. Neville-Neil	flags = args[1]->tcp_flags;
875d06879aSGeorge V. Neville-Neil
885d06879aSGeorge V. Neville-Neil	printf("%p %s: output [%x..%x]", arg0,
895d06879aSGeorge V. Neville-Neil	       tcp_state_string[args[0]->tcps_state], seq, seq + len);
905d06879aSGeorge V. Neville-Neil
915d06879aSGeorge V. Neville-Neil	printf("@%x, urp=%x", ack, args[1]->tcp_urgent);
925d06879aSGeorge V. Neville-Neil
935d06879aSGeorge V. Neville-Neil	printf("%s", flags != 0 ? "<" : "");
945d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_SYN ? "SYN," :"");
955d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_ACK ? "ACK," :"");
965d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_FIN ? "FIN," :"");
975d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_RST ? "RST," :"");
985d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_PUSH ? "PUSH," :"");
995d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_URG ? "URG," :"");
1005d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_ECE ? "ECE," :"");
101*38c63bdcSRichard Scheffenegger	printf("%s", flags & TH_CWR ? "CWR," :"");
102*38c63bdcSRichard Scheffenegger	printf("%s", flags & TH_AE ? "AE" :"");
1035d06879aSGeorge V. Neville-Neil	printf("%s", flags != 0 ? ">" : "");
1045d06879aSGeorge V. Neville-Neil
1055d06879aSGeorge V. Neville-Neil	printf("\n");
1065d06879aSGeorge V. Neville-Neil	printf("\trcv_(nxt,wnd,up) (%u,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
1075d06879aSGeorge V. Neville-Neil	       args[0]->tcps_rnxt, args[0]->tcps_rwnd, args[0]->tcps_rup,
1085d06879aSGeorge V. Neville-Neil	       args[0]->tcps_suna, args[0]->tcps_snxt, args[0]->tcps_smax);
1095d06879aSGeorge V. Neville-Neil	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
1105d06879aSGeorge V. Neville-Neil	       args[0]->tcps_swl1, args[0]->tcps_swl2, args[0]->tcps_swnd);
1115d06879aSGeorge V. Neville-Neil
1125d06879aSGeorge V. Neville-Neil}
1135d06879aSGeorge V. Neville-Neil
1145d06879aSGeorge V. Neville-Neiltcp:kernel::debug-drop
1155d06879aSGeorge V. Neville-Neil/args[0]->tcps_debug/
1165d06879aSGeorge V. Neville-Neil{
1175d06879aSGeorge V. Neville-Neil	printf("%p %s: output [x..x] @%x, urp=%x\n", arg0,
1185d06879aSGeorge V. Neville-Neil	       tcp_state_string[args[0]->tcps_state],
1195d06879aSGeorge V. Neville-Neil	       args[1]->tcp_ack,
1205d06879aSGeorge V. Neville-Neil	       args[1]->tcp_urgent);
1215d06879aSGeorge V. Neville-Neil
1225d06879aSGeorge V. Neville-Neil	seq = args[1]->tcp_seq;
1235d06879aSGeorge V. Neville-Neil	ack = args[1]->tcp_ack;
124342af4d5SGeorge V. Neville-Neil	len = args[2]->ip_plength - sizeof(struct tcphdr);
125342af4d5SGeorge V. Neville-Neil	flags = args[1]->tcp_flags;
1265d06879aSGeorge V. Neville-Neil
1275d06879aSGeorge V. Neville-Neil	printf("%p %s: drop [%x..%x]", arg0,
1285d06879aSGeorge V. Neville-Neil	       tcp_state_string[args[0]->tcps_state], seq, seq + len);
1295d06879aSGeorge V. Neville-Neil
1305d06879aSGeorge V. Neville-Neil	printf("@%x, urp=%x", ack, args[1]->tcp_urgent);
1315d06879aSGeorge V. Neville-Neil
1325d06879aSGeorge V. Neville-Neil	printf("%s", flags != 0 ? "<" : "");
1335d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_SYN ? "SYN," :"");
1345d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_ACK ? "ACK," :"");
1355d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_FIN ? "FIN," :"");
1365d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_RST ? "RST," :"");
1375d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_PUSH ? "PUSH," :"");
1385d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_URG ? "URG," :"");
1395d06879aSGeorge V. Neville-Neil	printf("%s", flags & TH_ECE ? "ECE," :"");
140*38c63bdcSRichard Scheffenegger	printf("%s", flags & TH_CWR ? "CWR," :"");
141*38c63bdcSRichard Scheffenegger	printf("%s", flags & TH_AE ? "AE" :"");
1425d06879aSGeorge V. Neville-Neil	printf("%s", flags != 0 ? ">" : "");
1435d06879aSGeorge V. Neville-Neil
1445d06879aSGeorge V. Neville-Neil	printf("\n");
1455d06879aSGeorge V. Neville-Neil	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
1465d06879aSGeorge V. Neville-Neil	       args[0]->tcps_rnxt, args[0]->tcps_rwnd, args[0]->tcps_rup,
1475d06879aSGeorge V. Neville-Neil	       args[0]->tcps_suna, args[0]->tcps_snxt, args[0]->tcps_smax);
1485d06879aSGeorge V. Neville-Neil	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
1495d06879aSGeorge V. Neville-Neil	       args[0]->tcps_swl1, args[0]->tcps_swl2, args[0]->tcps_swnd);
1505d06879aSGeorge V. Neville-Neil
1515d06879aSGeorge V. Neville-Neil}
1525d06879aSGeorge V. Neville-Neil
1535d06879aSGeorge V. Neville-Neiltcp:kernel::debug-user
1545d06879aSGeorge V. Neville-Neil/args[0]->tcps_debug/
1555d06879aSGeorge V. Neville-Neil{
1565d06879aSGeorge V. Neville-Neil	printf("%p %s: user ", arg0,
1575d06879aSGeorge V. Neville-Neil	       tcp_state_string[args[0]->tcps_state]);
1585d06879aSGeorge V. Neville-Neil
1595d06879aSGeorge V. Neville-Neil	printf("%s", prureq_string[arg1]);
1605d06879aSGeorge V. Neville-Neil	printf("\n");
1615d06879aSGeorge V. Neville-Neil	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
1625d06879aSGeorge V. Neville-Neil	       args[0]->tcps_rnxt, args[0]->tcps_rwnd, args[0]->tcps_rup,
1635d06879aSGeorge V. Neville-Neil	       args[0]->tcps_suna, args[0]->tcps_snxt, args[0]->tcps_smax);
1645d06879aSGeorge V. Neville-Neil	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
1655d06879aSGeorge V. Neville-Neil	       args[0]->tcps_swl1, args[0]->tcps_swl2, args[0]->tcps_swnd);
1665d06879aSGeorge V. Neville-Neil
1675d06879aSGeorge V. Neville-Neil}
1685d06879aSGeorge V. Neville-Neil
169