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