xref: /netbsd-src/usr.bin/systat/tcp.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: tcp.c,v 1.11 2005/02/26 22:12:34 dsl 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.11 2005/02/26 22:12:34 dsl Exp $");
33 #endif /* not lint */
34 
35 #include <sys/param.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/ip_var.h>
42 #include <netinet/tcp.h>
43 #include <netinet/tcp_timer.h>
44 #include <netinet/tcp_var.h>
45 
46 #include <string.h>
47 
48 #include "systat.h"
49 #include "extern.h"
50 
51 #define LHD(row, str)		mvwprintw(wnd, row, 10, str)
52 #define RHD(row, str)		mvwprintw(wnd, row, 45, str)
53 #define SHOW(row, col, stat) \
54     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat.stat)
55 
56 enum update {
57 	UPDATE_TIME,
58 	UPDATE_BOOT,
59 	UPDATE_RUN,
60 };
61 
62 static enum update update = UPDATE_TIME;
63 static struct tcpstat curstat;
64 static struct tcpstat newstat;
65 static struct tcpstat oldstat;
66 
67 static struct nlist namelist[] = {
68 	{ "_tcpstat" },
69 	{ "" }
70 };
71 
72 WINDOW *
73 opentcp(void)
74 {
75 
76 	return (subwin(stdscr, -1, 0, 5, 0));
77 }
78 
79 void
80 closetcp(WINDOW *w)
81 {
82 
83 	if (w != NULL) {
84 		wclear(w);
85 		wrefresh(w);
86 		delwin(w);
87 	}
88 }
89 
90 void
91 labeltcp(void)
92 {
93 
94 	wmove(wnd, 0, 0); wclrtoeol(wnd);
95 
96 	LHD(0,  "connections initiated");
97 	LHD(1,  "connections accepted");
98 	LHD(2,  "connections established");
99 
100 	LHD(4,  "connections dropped");
101 	LHD(5,  "  in embryonic state");
102 	LHD(6,  "  on retransmit timeout");
103 	LHD(7,  "  by keepalive");
104 	LHD(8,  "  by persist");
105 
106 	LHD(10, "potential rtt updates");
107 	LHD(11, "successful rtt updates");
108 	LHD(12, "delayed acks sent");
109 	LHD(13, "retransmit timeouts");
110 	LHD(14, "persist timeouts");
111 	LHD(15, "keepalive probes");
112 	LHD(16, "keepalive timeouts");
113 
114 	RHD(9,  "total TCP packets received");
115 	RHD(10, "  in sequence");
116 	RHD(11, "  completely duplicate");
117 	RHD(12, "  with some duplicate data");
118 	RHD(13, "  out of order");
119 	RHD(14, "  duplicate acks");
120 	RHD(15, "  acks");
121 	RHD(16, "  window probes");
122 	RHD(17, "  window updates");
123 
124 	RHD(0, "total TCP packets sent");
125 	RHD(1, "  data");
126 	RHD(2, "  data (retransmit)");
127 	RHD(3, "  ack-only");
128 	RHD(4, "  window probes");
129 	RHD(5, "  window updates");
130 	RHD(6, "  urgent data only");
131 	RHD(7, "  control");
132 }
133 
134 void
135 showtcpsyn(void)
136 {
137 
138 	SHOW(0, 0, tcps_sc_added);
139 	SHOW(1, 0, tcps_sc_completed);
140 	SHOW(2, 0, tcps_sc_timed_out);
141 	SHOW(3, 0, tcps_sc_dupesyn);
142 	SHOW(4, 0, tcps_sc_collisions);
143 	SHOW(5, 0, tcps_sc_retransmitted);
144 	SHOW(6, 0, tcps_sc_aborted);
145 	SHOW(7, 0, tcps_sc_overflowed);
146 	SHOW(8, 0, tcps_sc_reset);
147 	SHOW(9, 0, tcps_sc_unreach);
148 	SHOW(10, 0, tcps_sc_bucketoverflow);
149 	SHOW(11, 0, tcps_sc_dropped);
150 }
151 
152 void
153 labeltcpsyn(void)
154 {
155 
156 	wmove(wnd, 0, 0); wclrtoeol(wnd);
157 	LHD(0,  "entries added");
158 	LHD(1,  "connections completed");
159 	LHD(2,  "entries timed out");
160 	LHD(3,  "duplicate SYNs received");
161 	LHD(4,  "hash collisions");
162 	LHD(5,  "retransmissions");
163 	LHD(6,  "entries aborted (no memory)");
164 	LHD(7,  "dropped (overflow)");
165 	LHD(8,  "dropped (RST)");
166 	LHD(9,  "dropped (ICMP UNRCH)");
167 	LHD(10,  "dropped (bucket overflow)");
168 	LHD(11,  "dropped (unreachable/no memory)");
169 }
170 
171 void
172 showtcp(void)
173 {
174 
175 	SHOW(0, 0, tcps_connattempt);
176 	SHOW(1, 0, tcps_accepts);
177 	SHOW(2, 0, tcps_connects);
178 
179 	SHOW(4, 0, tcps_drops);
180 	SHOW(5, 0, tcps_conndrops);
181 	SHOW(6, 0, tcps_timeoutdrop);
182 	SHOW(7, 0, tcps_keepdrops);
183 	SHOW(8, 0, tcps_persistdrops);
184 
185 	SHOW(10, 0, tcps_segstimed);
186 	SHOW(11, 0, tcps_rttupdated);
187 	SHOW(12, 0, tcps_delack);
188 	SHOW(13, 0, tcps_rexmttimeo);
189 	SHOW(14, 0, tcps_persisttimeo);
190 	SHOW(15, 0, tcps_keepprobe);
191 	SHOW(16, 0, tcps_keeptimeo);
192 
193 	SHOW(0, 35, tcps_sndtotal);
194 	SHOW(1, 35, tcps_sndpack);
195 	SHOW(2, 35, tcps_sndrexmitpack);
196 
197 	SHOW(3, 35, tcps_sndacks);
198 	SHOW(4, 35, tcps_sndprobe);
199 	SHOW(5, 35, tcps_sndwinup);
200 	SHOW(6, 35, tcps_sndurg);
201 	SHOW(7, 35, tcps_sndctrl);
202 
203 	SHOW(9, 35, tcps_rcvtotal);
204 	SHOW(10, 35, tcps_rcvpack);
205 	SHOW(11, 35, tcps_rcvduppack);
206 	SHOW(12, 35, tcps_rcvpartduppack);
207 	SHOW(13, 35, tcps_rcvoopack);
208 	SHOW(14, 35, tcps_rcvdupack);
209 	SHOW(15, 35, tcps_rcvackpack);
210 	SHOW(16, 35, tcps_rcvwinprobe);
211 	SHOW(17, 35, tcps_rcvwinupd);
212 }
213 
214 int
215 inittcp(void)
216 {
217 
218 	if (namelist[0].n_type == 0) {
219 		if (kvm_nlist(kd, namelist)) {
220 			nlisterr(namelist);
221 			return(0);
222 		}
223 		if (namelist[0].n_type == 0) {
224 			error("No namelist");
225 			return(0);
226 		}
227 	}
228 	return 1;
229 }
230 
231 void
232 fetchtcp(void)
233 {
234 
235 	KREAD((void *)namelist[0].n_value, &newstat, sizeof(newstat));
236 
237 	ADJINETCTR(curstat, oldstat, newstat, tcps_connattempt);
238 	ADJINETCTR(curstat, oldstat, newstat, tcps_accepts);
239 	ADJINETCTR(curstat, oldstat, newstat, tcps_connects);
240 	ADJINETCTR(curstat, oldstat, newstat, tcps_drops);
241 	ADJINETCTR(curstat, oldstat, newstat, tcps_conndrops);
242 	ADJINETCTR(curstat, oldstat, newstat, tcps_timeoutdrop);
243 	ADJINETCTR(curstat, oldstat, newstat, tcps_keepdrops);
244 	ADJINETCTR(curstat, oldstat, newstat, tcps_persistdrops);
245 	ADJINETCTR(curstat, oldstat, newstat, tcps_segstimed);
246 	ADJINETCTR(curstat, oldstat, newstat, tcps_rttupdated);
247 	ADJINETCTR(curstat, oldstat, newstat, tcps_delack);
248 	ADJINETCTR(curstat, oldstat, newstat, tcps_rexmttimeo);
249 	ADJINETCTR(curstat, oldstat, newstat, tcps_persisttimeo);
250 	ADJINETCTR(curstat, oldstat, newstat, tcps_keepprobe);
251 	ADJINETCTR(curstat, oldstat, newstat, tcps_keeptimeo);
252 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndtotal);
253 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndpack);
254 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndrexmitpack);
255 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndacks);
256 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndprobe);
257 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndwinup);
258 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndurg);
259 	ADJINETCTR(curstat, oldstat, newstat, tcps_sndctrl);
260 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvtotal);
261 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvpack);
262 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvduppack);
263 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvpartduppack);
264 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvoopack);
265 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvdupack);
266 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvackpack);
267 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvwinprobe);
268 	ADJINETCTR(curstat, oldstat, newstat, tcps_rcvwinupd);
269 
270 	if (update == UPDATE_TIME)
271 		memcpy(&oldstat, &newstat, sizeof(oldstat));
272 }
273 
274 void
275 tcp_boot(char *args)
276 {
277 
278 	memset(&oldstat, 0, sizeof(oldstat));
279 	update = UPDATE_BOOT;
280 }
281 
282 void
283 tcp_run(char *args)
284 {
285 
286 	if (update != UPDATE_RUN) {
287 		memcpy(&oldstat, &newstat, sizeof(oldstat));
288 		update = UPDATE_RUN;
289 	}
290 }
291 
292 void
293 tcp_time(char *args)
294 {
295 
296 	if (update != UPDATE_TIME) {
297 		memcpy(&oldstat, &newstat, sizeof(oldstat));
298 		update = UPDATE_TIME;
299 	}
300 }
301 
302 void
303 tcp_zero(char *args)
304 {
305 
306 	if (update == UPDATE_RUN)
307 		memcpy(&oldstat, &newstat, sizeof(oldstat));
308 }
309