xref: /netbsd-src/usr.bin/systat/tcp.c (revision 95d875fb90b1458e4f1de6950286ddcd6644bc61)
1 /*	$NetBSD: tcp.c,v 1.2 1999/07/30 16:08:59 ad Exp $ */
2 
3 /*
4  * Copyright (c) 1999 Andy 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.2 1999/07/30 16:08:59 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 #include "systat.h"
56 #include "extern.h"
57 
58 #define LHD(row, str)		mvwprintw(wnd, row, 10, str)
59 #define RHD(row, str)		mvwprintw(wnd, row, 45, str)
60 #define SHOW(row, col, stat)	mvwprintw(wnd, row, col, "%9lu", curstat.stat)
61 
62 static struct tcpstat curstat, oldstat;
63 
64 static struct nlist namelist[] = {
65 	{ "_tcpstat" },
66 	{ "" }
67 };
68 
69 WINDOW *
70 opentcp(void)
71 {
72 
73 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
74 }
75 
76 void
77 closetcp(w)
78 	WINDOW *w;
79 {
80 
81 	if (w != NULL) {
82 		wclear(w);
83 		wrefresh(w);
84 		delwin(w);
85 	}
86 }
87 
88 void
89 labeltcp(void)
90 {
91 
92 	wmove(wnd, 0, 0); wclrtoeol(wnd);
93 
94 	LHD(0,  "connections initiated");
95 	LHD(1,  "connections accepted");
96 	LHD(2,  "connections established");
97 
98 	LHD(4,  "connections dropped");
99 	LHD(5,  "  in embryonic state");
100 	LHD(6,  "  on retransmit timeout");
101 	LHD(7,  "  by keepalive");
102 	LHD(8,  "  by persist");
103 
104 	LHD(10, "potential rtt updates");
105 	LHD(11, "successful rtt updates");
106 	LHD(12, "delayed acks sent");
107 	LHD(13, "retransmit timeouts");
108 	LHD(14, "persist timeouts");
109 	LHD(15, "keepalive probes");
110 	LHD(16, "keepalive timeouts");
111 
112 	RHD(9,  "total TCP packets received");
113 	RHD(10, "  in sequence");
114 	RHD(11, "  completely duplicate");
115 	RHD(12, "  with some duplicate data");
116 	RHD(13, "  out of order");
117 	RHD(14, "  duplicate acks");
118 	RHD(15, "  acks");
119 	RHD(16, "  window probes");
120 	RHD(17, "  window updates");
121 
122 	RHD(0, "total TCP packets sent");
123 	RHD(1, "  data");
124 	RHD(2, "  data (retransmit)");
125 	RHD(3, "  ack-only");
126 	RHD(4, "  window probes");
127 	RHD(5, "  window updates");
128 	RHD(6, "  urgent data only");
129 	RHD(7, "  control");
130 }
131 
132 void
133 showtcpsyn(void)
134 {
135 
136 	SHOW(0, 0, tcps_sc_added);
137 	SHOW(1, 0, tcps_sc_completed);
138 	SHOW(2, 0, tcps_sc_timed_out);
139 	SHOW(3, 0, tcps_sc_dupesyn);
140 	SHOW(4, 0, tcps_sc_collisions);
141 	SHOW(5, 0, tcps_sc_retransmitted);
142 	SHOW(6, 0, tcps_sc_aborted);
143 	SHOW(7, 0, tcps_sc_overflowed);
144 	SHOW(8, 0, tcps_sc_reset);
145 	SHOW(9, 0, tcps_sc_unreach);
146 	SHOW(10, 0, tcps_sc_bucketoverflow);
147 	SHOW(11, 0, tcps_sc_dropped);
148 }
149 
150 void
151 labeltcpsyn(void)
152 {
153 
154 	wmove(wnd, 0, 0); wclrtoeol(wnd);
155 	LHD(0,  "entries added");
156 	LHD(1,  "connections completed");
157 	LHD(2,  "entries timed out");
158 	LHD(3,  "duplicate SYNs recieved");
159 	LHD(4,  "hash collisions");
160 	LHD(5,  "retransmissions");
161 	LHD(6,  "entries aborted (no memory)");
162 	LHD(7,  "dropped (overflow)");
163 	LHD(8,  "dropped (RST)");
164 	LHD(9,  "dropped (ICMP UNRCH)");
165 	LHD(10,  "dropped (bucket overflow)");
166 	LHD(11,  "dropped (unreachable/no memory)");
167 }
168 
169 void
170 showtcp(void)
171 {
172 
173 	SHOW(0, 0, tcps_connattempt);
174 	SHOW(1, 0, tcps_accepts);
175 	SHOW(2, 0, tcps_connects);
176 
177 	SHOW(4, 0, tcps_drops);
178 	SHOW(5, 0, tcps_conndrops);
179 	SHOW(6, 0, tcps_timeoutdrop);
180 	SHOW(7, 0, tcps_keepdrops);
181 	SHOW(8, 0, tcps_persistdrops);
182 
183 	SHOW(10, 0, tcps_segstimed);
184 	SHOW(11, 0, tcps_rttupdated);
185 	SHOW(12, 0, tcps_delack);
186 	SHOW(13, 0, tcps_rexmttimeo);
187 	SHOW(14, 0, tcps_persisttimeo);
188 	SHOW(15, 0, tcps_keepprobe);
189 	SHOW(16, 0, tcps_keeptimeo);
190 
191 	SHOW(0, 35, tcps_sndtotal);
192 	SHOW(1, 35, tcps_sndpack);
193 	SHOW(2, 35, tcps_sndrexmitpack);
194 
195 	SHOW(3, 35, tcps_sndacks);
196 	SHOW(4, 35, tcps_sndprobe);
197 	SHOW(5, 35, tcps_sndwinup);
198 	SHOW(6, 35, tcps_sndurg);
199 	SHOW(7, 35, tcps_sndctrl);
200 
201 	SHOW(9, 35, tcps_rcvtotal);
202 	SHOW(10, 35, tcps_rcvpack);
203 	SHOW(11, 35, tcps_rcvduppack);
204 	SHOW(12, 35, tcps_rcvpartduppack);
205 	SHOW(13, 35, tcps_rcvoopack);
206 	SHOW(14, 35, tcps_rcvdupack);
207 	SHOW(15, 35, tcps_rcvackpack);
208 	SHOW(16, 35, tcps_rcvwinprobe);
209 	SHOW(17, 35, tcps_rcvwinupd);
210 }
211 
212 int
213 inittcp(void)
214 {
215 
216 	if (namelist[0].n_type == 0) {
217 		if (kvm_nlist(kd, namelist)) {
218 			nlisterr(namelist);
219 			return(0);
220 		}
221 		if (namelist[0].n_type == 0) {
222 			error("No namelist");
223 			return(0);
224 		}
225 	}
226 	return 1;
227 }
228 
229 void
230 fetchtcp(void)
231 {
232 
233 	oldstat = curstat;
234 	KREAD((void *)namelist[0].n_value, &curstat, sizeof(curstat));
235 }
236