xref: /freebsd/share/dtrace/tcpdebug (revision acc1a9ef)
1#!/usr/sbin/dtrace -s
2/*
3 * Copyright (c) 2015 George V. Neville-Neil
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 *
29 * The tcpdebug D script uses the tcp:kernel::debug tracepoints
30 * to replicate the action of turning on TCPDEBUG in a kernel configuration.
31 *
32 * A TCP debug statement shows a connection's
33 *
34 * direction:	input, output, user, drop
35 * state:	CLOSED,	LISTEN,	SYN_SENT, SYN_RCVD, ESTABLISHED,
36 * 		CLOSE_WAIT, FIN_WAIT_1, CLOSING, LAST_ACK, FIN_WAIT_2,TIME_WAIT
37 * sequence:	sequence space
38 *
39 * congestion:	rcv_nxt, rcv_wnd, rcv_up, snd_una, snd_nxt, snx_max,
40 *		snd_wl1, snd_wl2, snd_wnd
41 *
42 * NOTE: Only sockets with SO_DEBUG set will be shown.
43 *
44 * Usage: tcpdebug
45 */
46
47#pragma D option quiet
48tcp:kernel::debug-input
49/args[0]->tcps_debug/
50{
51	seq = args[1]->tcp_seq;
52	ack = args[1]->tcp_ack;
53	len = args[2]->ip_plength - sizeof(struct tcphdr);
54	flags = args[1]->tcp_flags;
55
56	printf("%p %s: input [%xu..%xu]", arg0,
57	       tcp_state_string[args[0]->tcps_state], seq, seq + len);
58
59	printf("@%x, urp=%x", ack, args[1]->tcp_urgent);
60
61	printf("%s", flags != 0 ? "<" : "");
62	printf("%s", flags & TH_SYN ? "SYN," :"");
63	printf("%s", flags & TH_ACK ? "ACK," :"");
64	printf("%s", flags & TH_FIN ? "FIN," :"");
65	printf("%s", flags & TH_RST ? "RST," :"");
66	printf("%s", flags & TH_PUSH ? "PUSH," :"");
67	printf("%s", flags & TH_URG ? "URG," :"");
68	printf("%s", flags & TH_ECE ? "ECE," :"");
69	printf("%s", flags & TH_CWR ? "CWR" :"");
70	printf("%s", flags != 0 ? ">" : "");
71
72	printf("\n");
73	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
74	       args[0]->tcps_rnxt, args[0]->tcps_rwnd, args[0]->tcps_rup,
75	       args[0]->tcps_suna, args[0]->tcps_snxt, args[0]->tcps_smax);
76	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
77	       args[0]->tcps_swl1, args[0]->tcps_swl2, args[0]->tcps_swnd);
78
79}
80
81tcp:kernel::debug-output
82/args[0]->tcps_debug/
83{
84	seq = args[1]->tcp_seq;
85	ack = args[1]->tcp_ack;
86	len = args[2]->ip_plength - sizeof(struct tcphdr);
87	flags = args[1]->tcp_flags;
88
89	printf("%p %s: output [%x..%x]", arg0,
90	       tcp_state_string[args[0]->tcps_state], seq, seq + len);
91
92	printf("@%x, urp=%x", ack, args[1]->tcp_urgent);
93
94	printf("%s", flags != 0 ? "<" : "");
95	printf("%s", flags & TH_SYN ? "SYN," :"");
96	printf("%s", flags & TH_ACK ? "ACK," :"");
97	printf("%s", flags & TH_FIN ? "FIN," :"");
98	printf("%s", flags & TH_RST ? "RST," :"");
99	printf("%s", flags & TH_PUSH ? "PUSH," :"");
100	printf("%s", flags & TH_URG ? "URG," :"");
101	printf("%s", flags & TH_ECE ? "ECE," :"");
102	printf("%s", flags & TH_CWR ? "CWR" :"");
103	printf("%s", flags != 0 ? ">" : "");
104
105	printf("\n");
106	printf("\trcv_(nxt,wnd,up) (%u,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
107	       args[0]->tcps_rnxt, args[0]->tcps_rwnd, args[0]->tcps_rup,
108	       args[0]->tcps_suna, args[0]->tcps_snxt, args[0]->tcps_smax);
109	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
110	       args[0]->tcps_swl1, args[0]->tcps_swl2, args[0]->tcps_swnd);
111
112}
113
114tcp:kernel::debug-drop
115/args[0]->tcps_debug/
116{
117	printf("%p %s: output [x..x] @%x, urp=%x\n", arg0,
118	       tcp_state_string[args[0]->tcps_state],
119	       args[1]->tcp_ack,
120	       args[1]->tcp_urgent);
121
122	seq = args[1]->tcp_seq;
123	ack = args[1]->tcp_ack;
124	len = args[2]->ip_plength - sizeof(struct tcphdr);
125	flags = args[1]->tcp_flags;
126
127	printf("%p %s: drop [%x..%x]", arg0,
128	       tcp_state_string[args[0]->tcps_state], seq, seq + len);
129
130	printf("@%x, urp=%x", ack, args[1]->tcp_urgent);
131
132	printf("%s", flags != 0 ? "<" : "");
133	printf("%s", flags & TH_SYN ? "SYN," :"");
134	printf("%s", flags & TH_ACK ? "ACK," :"");
135	printf("%s", flags & TH_FIN ? "FIN," :"");
136	printf("%s", flags & TH_RST ? "RST," :"");
137	printf("%s", flags & TH_PUSH ? "PUSH," :"");
138	printf("%s", flags & TH_URG ? "URG," :"");
139	printf("%s", flags & TH_ECE ? "ECE," :"");
140	printf("%s", flags & TH_CWR ? "CWR" :"");
141	printf("%s", flags != 0 ? ">" : "");
142
143	printf("\n");
144	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
145	       args[0]->tcps_rnxt, args[0]->tcps_rwnd, args[0]->tcps_rup,
146	       args[0]->tcps_suna, args[0]->tcps_snxt, args[0]->tcps_smax);
147	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
148	       args[0]->tcps_swl1, args[0]->tcps_swl2, args[0]->tcps_swnd);
149
150}
151
152tcp:kernel::debug-user
153/args[0]->tcps_debug/
154{
155	printf("%p %s: user ", arg0,
156	       tcp_state_string[args[0]->tcps_state]);
157
158	printf("%s", prureq_string[arg1]);
159	printf("\n");
160	printf("\trcv_(nxt,wnd,up) (%x,%x,%x) snd_(una,nxt,max) (%x,%x,%x)\n",
161	       args[0]->tcps_rnxt, args[0]->tcps_rwnd, args[0]->tcps_rup,
162	       args[0]->tcps_suna, args[0]->tcps_snxt, args[0]->tcps_smax);
163	printf("\tsnd_(wl1,wl2,wnd) (%x,%x,%x)\n",
164	       args[0]->tcps_swl1, args[0]->tcps_swl2, args[0]->tcps_swnd);
165
166}
167
168