xref: /netbsd-src/usr.bin/systat/tcp.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: tcp.c,v 1.7 2000/07/05 11:03:23 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1999, 2000 Andrew Doran <ad@NetBSD.org>
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __RCSID("$NetBSD: tcp.c,v 1.7 2000/07/05 11:03:23 ad Exp $");
33 #endif /* not lint */
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39 
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/ip_var.h>
44 #include <netinet/tcp.h>
45 #include <netinet/tcp_seq.h>
46 #include <netinet/tcp_fsm.h>
47 #include <netinet/tcp_timer.h>
48 #include <netinet/tcp_var.h>
49 
50 #include <stdlib.h>
51 #include <string.h>
52 #include <paths.h>
53 #include <nlist.h>
54 #include <kvm.h>
55 
56 #include "systat.h"
57 #include "extern.h"
58 
59 #define LHD(row, str)		mvwprintw(wnd, row, 10, str)
60 #define RHD(row, str)		mvwprintw(wnd, row, 45, str)
61 #define SHOW(row, col, stat) \
62     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat.stat)
63 
64 enum update {
65 	UPDATE_TIME,
66 	UPDATE_BOOT,
67 	UPDATE_RUN,
68 };
69 
70 static enum update update = UPDATE_TIME;
71 static struct tcpstat curstat;
72 static struct tcpstat newstat;
73 static struct tcpstat oldstat;
74 
75 static struct nlist namelist[] = {
76 	{ "_tcpstat" },
77 	{ "" }
78 };
79 
80 WINDOW *
81 opentcp(void)
82 {
83 
84 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
85 }
86 
87 void
88 closetcp(WINDOW *w)
89 {
90 
91 	if (w != NULL) {
92 		wclear(w);
93 		wrefresh(w);
94 		delwin(w);
95 	}
96 }
97 
98 void
99 labeltcp(void)
100 {
101 
102 	wmove(wnd, 0, 0); wclrtoeol(wnd);
103 
104 	LHD(0,  "connections initiated");
105 	LHD(1,  "connections accepted");
106 	LHD(2,  "connections established");
107 
108 	LHD(4,  "connections dropped");
109 	LHD(5,  "  in embryonic state");
110 	LHD(6,  "  on retransmit timeout");
111 	LHD(7,  "  by keepalive");
112 	LHD(8,  "  by persist");
113 
114 	LHD(10, "potential rtt updates");
115 	LHD(11, "successful rtt updates");
116 	LHD(12, "delayed acks sent");
117 	LHD(13, "retransmit timeouts");
118 	LHD(14, "persist timeouts");
119 	LHD(15, "keepalive probes");
120 	LHD(16, "keepalive timeouts");
121 
122 	RHD(9,  "total TCP packets received");
123 	RHD(10, "  in sequence");
124 	RHD(11, "  completely duplicate");
125 	RHD(12, "  with some duplicate data");
126 	RHD(13, "  out of order");
127 	RHD(14, "  duplicate acks");
128 	RHD(15, "  acks");
129 	RHD(16, "  window probes");
130 	RHD(17, "  window updates");
131 
132 	RHD(0, "total TCP packets sent");
133 	RHD(1, "  data");
134 	RHD(2, "  data (retransmit)");
135 	RHD(3, "  ack-only");
136 	RHD(4, "  window probes");
137 	RHD(5, "  window updates");
138 	RHD(6, "  urgent data only");
139 	RHD(7, "  control");
140 }
141 
142 void
143 showtcpsyn(void)
144 {
145 
146 	SHOW(0, 0, tcps_sc_added);
147 	SHOW(1, 0, tcps_sc_completed);
148 	SHOW(2, 0, tcps_sc_timed_out);
149 	SHOW(3, 0, tcps_sc_dupesyn);
150 	SHOW(4, 0, tcps_sc_collisions);
151 	SHOW(5, 0, tcps_sc_retransmitted);
152 	SHOW(6, 0, tcps_sc_aborted);
153 	SHOW(7, 0, tcps_sc_overflowed);
154 	SHOW(8, 0, tcps_sc_reset);
155 	SHOW(9, 0, tcps_sc_unreach);
156 	SHOW(10, 0, tcps_sc_bucketoverflow);
157 	SHOW(11, 0, tcps_sc_dropped);
158 }
159 
160 void
161 labeltcpsyn(void)
162 {
163 
164 	wmove(wnd, 0, 0); wclrtoeol(wnd);
165 	LHD(0,  "entries added");
166 	LHD(1,  "connections completed");
167 	LHD(2,  "entries timed out");
168 	LHD(3,  "duplicate SYNs recieved");
169 	LHD(4,  "hash collisions");
170 	LHD(5,  "retransmissions");
171 	LHD(6,  "entries aborted (no memory)");
172 	LHD(7,  "dropped (overflow)");
173 	LHD(8,  "dropped (RST)");
174 	LHD(9,  "dropped (ICMP UNRCH)");
175 	LHD(10,  "dropped (bucket overflow)");
176 	LHD(11,  "dropped (unreachable/no memory)");
177 }
178 
179 void
180 showtcp(void)
181 {
182 
183 	SHOW(0, 0, tcps_connattempt);
184 	SHOW(1, 0, tcps_accepts);
185 	SHOW(2, 0, tcps_connects);
186 
187 	SHOW(4, 0, tcps_drops);
188 	SHOW(5, 0, tcps_conndrops);
189 	SHOW(6, 0, tcps_timeoutdrop);
190 	SHOW(7, 0, tcps_keepdrops);
191 	SHOW(8, 0, tcps_persistdrops);
192 
193 	SHOW(10, 0, tcps_segstimed);
194 	SHOW(11, 0, tcps_rttupdated);
195 	SHOW(12, 0, tcps_delack);
196 	SHOW(13, 0, tcps_rexmttimeo);
197 	SHOW(14, 0, tcps_persisttimeo);
198 	SHOW(15, 0, tcps_keepprobe);
199 	SHOW(16, 0, tcps_keeptimeo);
200 
201 	SHOW(0, 35, tcps_sndtotal);
202 	SHOW(1, 35, tcps_sndpack);
203 	SHOW(2, 35, tcps_sndrexmitpack);
204 
205 	SHOW(3, 35, tcps_sndacks);
206 	SHOW(4, 35, tcps_sndprobe);
207 	SHOW(5, 35, tcps_sndwinup);
208 	SHOW(6, 35, tcps_sndurg);
209 	SHOW(7, 35, tcps_sndctrl);
210 
211 	SHOW(9, 35, tcps_rcvtotal);
212 	SHOW(10, 35, tcps_rcvpack);
213 	SHOW(11, 35, tcps_rcvduppack);
214 	SHOW(12, 35, tcps_rcvpartduppack);
215 	SHOW(13, 35, tcps_rcvoopack);
216 	SHOW(14, 35, tcps_rcvdupack);
217 	SHOW(15, 35, tcps_rcvackpack);
218 	SHOW(16, 35, tcps_rcvwinprobe);
219 	SHOW(17, 35, tcps_rcvwinupd);
220 }
221 
222 int
223 inittcp(void)
224 {
225 
226 	if (namelist[0].n_type == 0) {
227 		if (kvm_nlist(kd, namelist)) {
228 			nlisterr(namelist);
229 			return(0);
230 		}
231 		if (namelist[0].n_type == 0) {
232 			error("No namelist");
233 			return(0);
234 		}
235 	}
236 	return 1;
237 }
238 
239 void
240 fetchtcp(void)
241 {
242 
243 	KREAD((void *)namelist[0].n_value, &newstat, sizeof(newstat));
244 
245 	ADJINETCTR(curstat, oldstat, newstat, tcps_connattempt);
246 	ADJINETCTR(curstat, oldstat, newstat, tcps_accepts);
247 	ADJINETCTR(curstat, oldstat, newstat, tcps_connects);
248 	ADJINETCTR(curstat, oldstat, newstat, tcps_drops);
249 	ADJINETCTR(curstat, oldstat, newstat, tcps_conndrops);
250 	ADJINETCTR(curstat, oldstat, newstat, tcps_timeoutdrop);
251 	ADJINETCTR(curstat, oldstat, newstat, tcps_keepdrops);
252 	ADJINETCTR(curstat, oldstat, newstat, tcps_persistdrops);
253 	ADJINETCTR(curstat, oldstat, newstat, tcps_segstimed);
254 	ADJINETCTR(curstat, oldstat, newstat, tcps_rttupdated);
255 	ADJINETCTR(curstat, oldstat, newstat, tcps_delack);
256 	ADJINETCTR(curstat, oldstat, newstat, tcps_rexmttimeo);
257 	ADJINETCTR(curstat, oldstat, newstat, tcps_persisttimeo);
258 	ADJINETCTR(curstat, oldstat, newstat, tcps_keepprobe);
259 	ADJINETCTR(curstat, oldstat, newstat, tcps_keeptimeo);
260 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndtotal);
261 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndpack);
262 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndrexmitpack);
263 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndacks);
264 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndprobe);
265 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndwinup);
266 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndurg);
267 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndctrl);
268 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvtotal);
269 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvpack);
270 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvduppack);
271 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvpartduppack);
272 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvoopack);
273 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvdupack);
274 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvackpack);
275 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvwinprobe);
276 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvwinupd);
277 
278 	if (update == UPDATE_TIME)
279 		memcpy(&oldstat, &newstat, sizeof(oldstat));
280 }
281 
282 void
283 tcp_boot(char *args)
284 {
285 
286 	memset(&oldstat, 0, sizeof(oldstat));
287 	update = UPDATE_BOOT;
288 }
289 
290 void
291 tcp_run(char *args)
292 {
293 
294 	if (update != UPDATE_RUN) {
295 		memcpy(&oldstat, &newstat, sizeof(oldstat));
296 		update = UPDATE_RUN;
297 	}
298 }
299 
300 void
301 tcp_time(char *args)
302 {
303 
304 	if (update != UPDATE_TIME) {
305 		memcpy(&oldstat, &newstat, sizeof(oldstat));
306 		update = UPDATE_TIME;
307 	}
308 }
309 
310 void
311 tcp_zero(char *args)
312 {
313 
314 	if (update == UPDATE_RUN)
315 		memcpy(&oldstat, &newstat, sizeof(oldstat));
316 }
317