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