xref: /freebsd/usr.bin/systat/tcp.c (revision 380a989b3223d455375b4fae70fd0b9bdd43bafb)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 /* From:
36 static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
37 static const char rcsid[] =
38 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp";
39 */
40 static const char rcsid[] =
41 	"$Id: tcp.c,v 1.1 1997/09/27 00:44:55 wollman Exp $";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/tcp.h>
53 #include <netinet/tcp_seq.h>
54 #include <netinet/tcp_fsm.h>
55 #include <netinet/tcp_timer.h>
56 #include <netinet/tcp_var.h>
57 
58 #include <stdlib.h>
59 #include <string.h>
60 #include <paths.h>
61 #include "systat.h"
62 #include "extern.h"
63 #include "mode.h"
64 
65 static struct tcpstat curstat, initstat, oldstat;
66 
67 /*-
68 --0         1         2         3         4         5         6         7
69 --0123456789012345678901234567890123456789012345678901234567890123456789012345
70 01          TCP Connections                    TCP Packets
71 02999999999 connections initiated    999999999 total packets sent
72 03999999999 connections accepted     999999999 - data
73 04999999999 connections established  999999999 - data (retransmit)
74 05999999999 connections dropped      999999999 - ack-only
75 06999999999 - in embryonic state     999999999 - window probes
76 07999999999 - on retransmit timeout  999999999 - window updates
77 08999999999 - by keepalive           999999999 - urgent data only
78 09999999999 - from listen queue      999999999 - control
79 10                                   999999999 - resends by PMTU discovery
80 11          TCP Timers               999999999 total packets received
81 12999999999 potential rtt updates    999999999 - in sequence
82 13999999999 - successful             999999999 - completely duplicate
83 14999999999 delayed acks sent        999999999 - with some duplicate data
84 15999999999 retransmit timeouts      999999999 - out-of-order
85 16999999999 persist timeouts         999999999 - duplicate acks
86 17999999999 keepalive probes         999999999 - acks
87 18999999999 - timeouts               999999999 - window probes
88 19                                   999999999 - window updates
89 --0123456789012345678901234567890123456789012345678901234567890123456789012345
90 --0         1         2         3         4         5         6         7
91 */
92 
93 WINDOW *
94 opentcp(void)
95 {
96 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
97 }
98 
99 void
100 closetcp(w)
101 	WINDOW *w;
102 {
103 	if (w == NULL)
104 		return;
105 	wclear(w);
106 	wrefresh(w);
107 	delwin(w);
108 }
109 
110 void
111 labeltcp(void)
112 {
113 	wmove(wnd, 0, 0); wclrtoeol(wnd);
114 #define L(row, str) mvwprintw(wnd, row, 10, str)
115 #define R(row, str) mvwprintw(wnd, row, 45, str);
116 	L(1, "TCP Connections");		R(1, "TCP Packets");
117 	L(2, "connections initiated");		R(2, "total packets sent");
118 	L(3, "connections accepted");		R(3, "- data");
119 	L(4, "connections established");	R(4, "- data (retransmit)");
120 	L(5, "connections dropped");		R(5, "- ack-only");
121 	L(6, "- in embryonic state");		R(6, "- window probes");
122 	L(7, "- on retransmit timeout");	R(7, "- window updates");
123 	L(8, "- by keepalive");			R(8, "- urgent data only");
124 	L(9, "- from listen queue");		R(9, "- control");
125 	R(10, "- resends by PMTU discovery");
126 	L(11, "TCP Timers");		R(11, "total packets received");
127 	L(12, "potential rtt updates");		R(12, "- in sequence");
128 	L(13, "- successful");		R(13, "- completely duplicate");
129 	L(14, "delayed acks sent");	R(14, "- with some duplicate data");
130 	L(15, "retransmit timeouts");	R(15, "- out-of-order");
131 	L(16, "persist timeouts");	R(16, "- duplicate acks");
132 	L(17, "keepalive probes");	R(17, "- acks");
133 	L(18, "- timeouts");		R(18, "- window probes");
134 	R(19, "- window updates");
135 #undef L
136 #undef R
137 }
138 
139 static void
140 domode(struct tcpstat *ret)
141 {
142 	const struct tcpstat *sub;
143 	int divisor = 1;
144 
145 	switch(currentmode) {
146 	case display_RATE:
147 		sub = &oldstat;
148 		divisor = naptime;
149 		break;
150 	case display_DELTA:
151 		sub = &oldstat;
152 		break;
153 	case display_SINCE:
154 		sub = &initstat;
155 		break;
156 	default:
157 		*ret = curstat;
158 		return;
159 	}
160 #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
161 	DO(tcps_connattempt);
162 	DO(tcps_accepts);
163 	DO(tcps_connects);
164 	DO(tcps_drops);
165 	DO(tcps_conndrops);
166 	DO(tcps_closed);
167 	DO(tcps_segstimed);
168 	DO(tcps_rttupdated);
169 	DO(tcps_delack);
170 	DO(tcps_timeoutdrop);
171 	DO(tcps_rexmttimeo);
172 	DO(tcps_persisttimeo);
173 	DO(tcps_keeptimeo);
174 	DO(tcps_keepprobe);
175 	DO(tcps_keepdrops);
176 
177 	DO(tcps_sndtotal);
178 	DO(tcps_sndpack);
179 	DO(tcps_sndbyte);
180 	DO(tcps_sndrexmitpack);
181 	DO(tcps_sndrexmitbyte);
182 	DO(tcps_sndacks);
183 	DO(tcps_sndprobe);
184 	DO(tcps_sndurg);
185 	DO(tcps_sndwinup);
186 	DO(tcps_sndctrl);
187 
188 	DO(tcps_rcvtotal);
189 	DO(tcps_rcvpack);
190 	DO(tcps_rcvbyte);
191 	DO(tcps_rcvbadsum);
192 	DO(tcps_rcvbadoff);
193 	DO(tcps_rcvshort);
194 	DO(tcps_rcvduppack);
195 	DO(tcps_rcvdupbyte);
196 	DO(tcps_rcvpartduppack);
197 	DO(tcps_rcvpartdupbyte);
198 	DO(tcps_rcvoopack);
199 	DO(tcps_rcvoobyte);
200 	DO(tcps_rcvpackafterwin);
201 	DO(tcps_rcvbyteafterwin);
202 	DO(tcps_rcvafterclose);
203 	DO(tcps_rcvwinprobe);
204 	DO(tcps_rcvdupack);
205 	DO(tcps_rcvacktoomuch);
206 	DO(tcps_rcvackpack);
207 	DO(tcps_rcvackbyte);
208 	DO(tcps_rcvwinupd);
209 	DO(tcps_pawsdrop);
210 	DO(tcps_predack);
211 	DO(tcps_preddat);
212 	DO(tcps_pcbcachemiss);
213 	DO(tcps_cachedrtt);
214 	DO(tcps_cachedrttvar);
215 	DO(tcps_cachedssthresh);
216 	DO(tcps_usedrtt);
217 	DO(tcps_usedrttvar);
218 	DO(tcps_usedssthresh);
219 	DO(tcps_persistdrop);
220 	DO(tcps_badsyn);
221 	DO(tcps_mturesent);
222 	DO(tcps_listendrop);
223 #undef DO
224 }
225 
226 void
227 showtcp(void)
228 {
229 	struct tcpstat stats;
230 
231 	memset(&stats, 0, sizeof stats);
232 	domode(&stats);
233 
234 #define DO(stat, row, col) \
235 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
236 #define	L(row, stat) DO(stat, row, 0)
237 #define	R(row, stat) DO(stat, row, 35)
238 	L(2, tcps_connattempt);		R(2, tcps_sndtotal);
239 	L(3, tcps_accepts);		R(3, tcps_sndpack);
240 	L(4, tcps_connects);		R(4, tcps_sndrexmitpack);
241 	L(5, tcps_drops);		R(5, tcps_sndacks);
242 	L(6, tcps_conndrops);		R(6, tcps_sndprobe);
243 	L(7, tcps_timeoutdrop);		R(7, tcps_sndwinup);
244 	L(8, tcps_keepdrops);		R(8, tcps_sndurg);
245 	L(9, tcps_listendrop);		R(9, tcps_sndctrl);
246 	R(10, tcps_mturesent);
247 	R(11, tcps_rcvtotal);
248 	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
249 	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
250 	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
251 	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
252 	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
253 	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
254 	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
255 	R(19, tcps_rcvwinupd);
256 #undef DO
257 #undef L
258 #undef R
259 }
260 
261 int
262 inittcp(void)
263 {
264 	size_t len;
265 	int name[4];
266 
267 	name[0] = CTL_NET;
268 	name[1] = PF_INET;
269 	name[2] = IPPROTO_TCP;
270 	name[3] = TCPCTL_STATS;
271 
272 	len = 0;
273 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
274 		error("sysctl getting tcpstat size failed");
275 		return 0;
276 	}
277 	if (len > sizeof curstat) {
278 		error("tcpstat structure has grown--recompile systat!");
279 		return 0;
280 	}
281 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
282 		error("sysctl getting tcpstat failed");
283 		return 0;
284 	}
285 	oldstat = initstat;
286 	return 1;
287 }
288 
289 void
290 resettcp(void)
291 {
292 	size_t len;
293 	int name[4];
294 
295 	name[0] = CTL_NET;
296 	name[1] = PF_INET;
297 	name[2] = IPPROTO_TCP;
298 	name[3] = TCPCTL_STATS;
299 
300 	len = sizeof initstat;
301 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
302 		error("sysctl getting tcpstat failed");
303 	}
304 	oldstat = initstat;
305 }
306 
307 void
308 fetchtcp(void)
309 {
310 	int name[4];
311 	size_t len;
312 
313 	oldstat = curstat;
314 	name[0] = CTL_NET;
315 	name[1] = PF_INET;
316 	name[2] = IPPROTO_TCP;
317 	name[3] = TCPCTL_STATS;
318 	len = sizeof curstat;
319 
320 	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
321 		return;
322 }
323 
324