xref: /freebsd/usr.bin/systat/tcp.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 #if 0
35 #ifndef lint
36 /* From: */
37 static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
39 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp";
40 #endif /* not lint */
41 #endif
42 
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/tcp.h>
55 #include <netinet/tcp_seq.h>
56 #include <netinet/tcp_fsm.h>
57 #include <netinet/tcp_timer.h>
58 #include <netinet/tcp_var.h>
59 
60 #include <stdlib.h>
61 #include <string.h>
62 #include <paths.h>
63 
64 #include "systat.h"
65 #include "extern.h"
66 #include "mode.h"
67 
68 static struct tcpstat curstat, initstat, oldstat;
69 
70 /*-
71 --0         1         2         3         4         5         6         7
72 --0123456789012345678901234567890123456789012345678901234567890123456789012345
73 00             TCP Connections                       TCP Packets
74 01999999999999 connections initiated    999999999999 total packets sent
75 02999999999999 connections accepted     999999999999 - data
76 03999999999999 connections established  999999999999 - data (retransmit by dupack)
77 04999999999999 connections dropped      999999999999 - data (retransmit by sack)
78 05999999999999 - in embryonic state     999999999999 - ack-only
79 06999999999999 - on retransmit timeout  999999999999 - window probes
80 07999999999999 - by keepalive           999999999999 - window updates
81 08999999999999 - from listen queue      999999999999 - urgent data only
82 09                                      999999999999 - control
83 10                                      999999999999 - resends by PMTU discovery
84 11             TCP Timers               999999999999 total packets received
85 12999999999999 potential rtt updates    999999999999 - in sequence
86 13999999999999 - successful             999999999999 - completely duplicate
87 14999999999999 delayed acks sent        999999999999 - with some duplicate data
88 15999999999999 retransmit timeouts      999999999999 - out-of-order
89 16999999999999 persist timeouts         999999999999 - duplicate acks
90 17999999999999 keepalive probes         999999999999 - acks
91 18999999999999 - timeouts               999999999999 - window probes
92 19                                      999999999999 - window updates
93 20                                      999999999999 - bad checksum
94 --0123456789012345678901234567890123456789012345678901234567890123456789012345
95 --0         1         2         3         4         5         6         7
96 */
97 
98 WINDOW *
99 opentcp(void)
100 {
101 	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
102 }
103 
104 void
105 closetcp(w)
106 	WINDOW *w;
107 {
108 	if (w == NULL)
109 		return;
110 	wclear(w);
111 	wrefresh(w);
112 	delwin(w);
113 }
114 
115 void
116 labeltcp(void)
117 {
118 	wmove(wnd, 0, 0); wclrtoeol(wnd);
119 #define L(row, str) mvwprintw(wnd, row, 13, str)
120 #define R(row, str) mvwprintw(wnd, row, 51, str);
121 	L(0, "TCP Connections");		R(0, "TCP Packets");
122 	L(1, "connections initiated");		R(1, "total packets sent");
123 	L(2, "connections accepted");		R(2, "- data");
124 	L(3, "connections established");	R(3, "- data (retransmit by dupack)");
125 	L(4, "connections dropped");		R(4, "- data (retransmit by sack)");
126 	L(5, "- in embryonic state");		R(5, "- ack-only");
127 	L(6, "- on retransmit timeout");	R(6, "- window probes");
128 	L(7, "- by keepalive");			R(7, "- window updates");
129 	L(8, "- from listen queue");		R(8, "- urgent data only");
130 						R(9, "- control");
131 						R(10, "- resends by PMTU discovery");
132 	L(11, "TCP Timers");			R(11, "total packets received");
133 	L(12, "potential rtt updates");		R(12, "- in sequence");
134 	L(13, "- successful");			R(13, "- completely duplicate");
135 	L(14, "delayed acks sent");		R(14, "- with some duplicate data");
136 	L(15, "retransmit timeouts");		R(15, "- out-of-order");
137 	L(16, "persist timeouts");		R(16, "- duplicate acks");
138 	L(17, "keepalive probes");		R(17, "- acks");
139 	L(18, "- timeouts");			R(18, "- window probes");
140 						R(19, "- window updates");
141 						R(20, "- bad checksum");
142 #undef L
143 #undef R
144 }
145 
146 static void
147 domode(struct tcpstat *ret)
148 {
149 	const struct tcpstat *sub;
150 	int divisor = 1;
151 
152 	switch(currentmode) {
153 	case display_RATE:
154 		sub = &oldstat;
155 		divisor = naptime;
156 		break;
157 	case display_DELTA:
158 		sub = &oldstat;
159 		break;
160 	case display_SINCE:
161 		sub = &initstat;
162 		break;
163 	default:
164 		*ret = curstat;
165 		return;
166 	}
167 #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
168 	DO(tcps_connattempt);
169 	DO(tcps_accepts);
170 	DO(tcps_connects);
171 	DO(tcps_drops);
172 	DO(tcps_conndrops);
173 	DO(tcps_closed);
174 	DO(tcps_segstimed);
175 	DO(tcps_rttupdated);
176 	DO(tcps_delack);
177 	DO(tcps_timeoutdrop);
178 	DO(tcps_rexmttimeo);
179 	DO(tcps_persisttimeo);
180 	DO(tcps_keeptimeo);
181 	DO(tcps_keepprobe);
182 	DO(tcps_keepdrops);
183 
184 	DO(tcps_sndtotal);
185 	DO(tcps_sndpack);
186 	DO(tcps_sndbyte);
187 	DO(tcps_sndrexmitpack);
188 	DO(tcps_sack_rexmits);
189 	DO(tcps_sndacks);
190 	DO(tcps_sndprobe);
191 	DO(tcps_sndurg);
192 	DO(tcps_sndwinup);
193 	DO(tcps_sndctrl);
194 
195 	DO(tcps_rcvtotal);
196 	DO(tcps_rcvpack);
197 	DO(tcps_rcvbyte);
198 	DO(tcps_rcvbadsum);
199 	DO(tcps_rcvbadoff);
200 	DO(tcps_rcvshort);
201 	DO(tcps_rcvduppack);
202 	DO(tcps_rcvdupbyte);
203 	DO(tcps_rcvpartduppack);
204 	DO(tcps_rcvpartdupbyte);
205 	DO(tcps_rcvoopack);
206 	DO(tcps_rcvoobyte);
207 	DO(tcps_rcvpackafterwin);
208 	DO(tcps_rcvbyteafterwin);
209 	DO(tcps_rcvafterclose);
210 	DO(tcps_rcvwinprobe);
211 	DO(tcps_rcvdupack);
212 	DO(tcps_rcvacktoomuch);
213 	DO(tcps_rcvackpack);
214 	DO(tcps_rcvackbyte);
215 	DO(tcps_rcvwinupd);
216 	DO(tcps_pawsdrop);
217 	DO(tcps_predack);
218 	DO(tcps_preddat);
219 	DO(tcps_pcbcachemiss);
220 	DO(tcps_cachedrtt);
221 	DO(tcps_cachedrttvar);
222 	DO(tcps_cachedssthresh);
223 	DO(tcps_usedrtt);
224 	DO(tcps_usedrttvar);
225 	DO(tcps_usedssthresh);
226 	DO(tcps_persistdrop);
227 	DO(tcps_badsyn);
228 	DO(tcps_mturesent);
229 	DO(tcps_listendrop);
230 #undef DO
231 }
232 
233 void
234 showtcp(void)
235 {
236 	struct tcpstat stats;
237 
238 	memset(&stats, 0, sizeof stats);
239 	domode(&stats);
240 
241 #define DO(stat, row, col) \
242 	mvwprintw(wnd, row, col, "%12lu", stats.stat)
243 #define	L(row, stat) DO(stat, row, 0)
244 #define	R(row, stat) DO(stat, row, 38)
245 	L(1, tcps_connattempt);		R(1, tcps_sndtotal);
246 	L(2, tcps_accepts);		R(2, tcps_sndpack);
247 	L(3, tcps_connects);		R(3, tcps_sndrexmitpack);
248 	L(4, tcps_drops);		R(4, tcps_sack_rexmits);
249 	L(5, tcps_conndrops);		R(5, tcps_sndacks);
250 	L(6, tcps_timeoutdrop);		R(6, tcps_sndprobe);
251 	L(7, tcps_keepdrops);		R(7, tcps_sndwinup);
252 	L(8, tcps_listendrop);		R(8, tcps_sndurg);
253 					R(9, tcps_sndctrl);
254 					R(10, tcps_mturesent);
255 					R(11, tcps_rcvtotal);
256 	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
257 	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
258 	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
259 	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
260 	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
261 	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
262 	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
263 					R(19, tcps_rcvwinupd);
264 					R(20, tcps_rcvbadsum);
265 #undef DO
266 #undef L
267 #undef R
268 }
269 
270 int
271 inittcp(void)
272 {
273 	size_t len;
274 	int name[4];
275 
276 	name[0] = CTL_NET;
277 	name[1] = PF_INET;
278 	name[2] = IPPROTO_TCP;
279 	name[3] = TCPCTL_STATS;
280 
281 	len = 0;
282 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
283 		error("sysctl getting tcpstat size failed");
284 		return 0;
285 	}
286 	if (len > sizeof curstat) {
287 		error("tcpstat structure has grown--recompile systat!");
288 		return 0;
289 	}
290 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
291 		error("sysctl getting tcpstat failed");
292 		return 0;
293 	}
294 	oldstat = initstat;
295 	return 1;
296 }
297 
298 void
299 resettcp(void)
300 {
301 	size_t len;
302 	int name[4];
303 
304 	name[0] = CTL_NET;
305 	name[1] = PF_INET;
306 	name[2] = IPPROTO_TCP;
307 	name[3] = TCPCTL_STATS;
308 
309 	len = sizeof initstat;
310 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
311 		error("sysctl getting tcpstat failed");
312 	}
313 	oldstat = initstat;
314 }
315 
316 void
317 fetchtcp(void)
318 {
319 	int name[4];
320 	size_t len;
321 
322 	oldstat = curstat;
323 	name[0] = CTL_NET;
324 	name[1] = PF_INET;
325 	name[2] = IPPROTO_TCP;
326 	name[3] = TCPCTL_STATS;
327 	len = sizeof curstat;
328 
329 	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
330 		return;
331 }
332 
333