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