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