Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / systat / tcp.c
blob7a237c42ce755bf4520851bcd0599ab7b76bf160
1 /* $NetBSD: tcp.c,v 1.14 2008/04/10 17:14:25 thorpej Exp $ */
3 /*
4 * Copyright (c) 1999, 2000 Andrew Doran <ad@NetBSD.org>
5 * All rights reserved.
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.
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.
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __RCSID("$NetBSD: tcp.c,v 1.14 2008/04/10 17:14:25 thorpej Exp $");
33 #endif /* not lint */
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
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>
46 #include <string.h>
48 #include "systat.h"
49 #include "extern.h"
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])
56 enum update {
57 UPDATE_TIME,
58 UPDATE_BOOT,
59 UPDATE_RUN,
62 static enum update update = UPDATE_TIME;
63 static uint64_t curstat[TCP_NSTATS];
64 static uint64_t newstat[TCP_NSTATS];
65 static uint64_t oldstat[TCP_NSTATS];
67 static struct nlist namelist[] = {
68 { .n_name = "_tcpstat" },
69 { .n_name = NULL }
72 WINDOW *
73 opentcp(void)
76 return (subwin(stdscr, -1, 0, 5, 0));
79 void
80 closetcp(WINDOW *w)
83 if (w != NULL) {
84 wclear(w);
85 wrefresh(w);
86 delwin(w);
90 void
91 labeltcp(void)
94 wmove(wnd, 0, 0); wclrtoeol(wnd);
96 LHD(0, "connections initiated");
97 LHD(1, "connections accepted");
98 LHD(2, "connections established");
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");
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");
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");
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");
134 void
135 showtcpsyn(void)
138 SHOW(0, 0, TCP_STAT_SC_ADDED);
139 SHOW(1, 0, TCP_STAT_SC_COMPLETED);
140 SHOW(2, 0, TCP_STAT_SC_TIMED_OUT);
141 SHOW(3, 0, TCP_STAT_SC_DUPESYN);
142 SHOW(4, 0, TCP_STAT_SC_COLLISIONS);
143 SHOW(5, 0, TCP_STAT_SC_RETRANSMITTED);
144 SHOW(6, 0, TCP_STAT_SC_ABORTED);
145 SHOW(7, 0, TCP_STAT_SC_OVERFLOWED);
146 SHOW(8, 0, TCP_STAT_SC_RESET);
147 SHOW(9, 0, TCP_STAT_SC_UNREACH);
148 SHOW(10, 0, TCP_STAT_SC_BUCKETOVERFLOW);
149 SHOW(11, 0, TCP_STAT_SC_DROPPED);
152 void
153 labeltcpsyn(void)
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)");
171 void
172 showtcp(void)
175 SHOW(0, 0, TCP_STAT_CONNATTEMPT);
176 SHOW(1, 0, TCP_STAT_ACCEPTS);
177 SHOW(2, 0, TCP_STAT_CONNECTS);
179 SHOW(4, 0, TCP_STAT_DROPS);
180 SHOW(5, 0, TCP_STAT_CONNDROPS);
181 SHOW(6, 0, TCP_STAT_TIMEOUTDROP);
182 SHOW(7, 0, TCP_STAT_KEEPDROPS);
183 SHOW(8, 0, TCP_STAT_PERSISTDROPS);
185 SHOW(10, 0, TCP_STAT_SEGSTIMED);
186 SHOW(11, 0, TCP_STAT_RTTUPDATED);
187 SHOW(12, 0, TCP_STAT_DELACK);
188 SHOW(13, 0, TCP_STAT_REXMTTIMEO);
189 SHOW(14, 0, TCP_STAT_PERSISTTIMEO);
190 SHOW(15, 0, TCP_STAT_KEEPPROBE);
191 SHOW(16, 0, TCP_STAT_KEEPTIMEO);
193 SHOW(0, 35, TCP_STAT_SNDTOTAL);
194 SHOW(1, 35, TCP_STAT_SNDPACK);
195 SHOW(2, 35, TCP_STAT_SNDREXMITPACK);
197 SHOW(3, 35, TCP_STAT_SNDACKS);
198 SHOW(4, 35, TCP_STAT_SNDPROBE);
199 SHOW(5, 35, TCP_STAT_SNDWINUP);
200 SHOW(6, 35, TCP_STAT_SNDURG);
201 SHOW(7, 35, TCP_STAT_SNDCTRL);
203 SHOW(9, 35, TCP_STAT_RCVTOTAL);
204 SHOW(10, 35, TCP_STAT_RCVPACK);
205 SHOW(11, 35, TCP_STAT_RCVDUPPACK);
206 SHOW(12, 35, TCP_STAT_RCVPARTDUPPACK);
207 SHOW(13, 35, TCP_STAT_RCVOOPACK);
208 SHOW(14, 35, TCP_STAT_RCVDUPACK);
209 SHOW(15, 35, TCP_STAT_RCVACKPACK);
210 SHOW(16, 35, TCP_STAT_RCVWINPROBE);
211 SHOW(17, 35, TCP_STAT_RCVWINUPD);
215 inittcp(void)
218 if (! use_sysctl) {
219 if (namelist[0].n_type == 0) {
220 if (kvm_nlist(kd, namelist)) {
221 nlisterr(namelist);
222 return(0);
224 if (namelist[0].n_type == 0) {
225 error("No namelist");
226 return(0);
230 return 1;
233 void
234 fetchtcp(void)
236 int i;
238 if (use_sysctl) {
239 size_t size = sizeof(newstat);
241 if (sysctlbyname("net.inet.tcp.stats", newstat, &size,
242 NULL, 0) == -1)
243 return;
244 } else {
245 KREAD((void *)namelist[0].n_value, newstat, sizeof(newstat));
248 for (i = 0; i < TCP_NSTATS; i++)
249 xADJINETCTR(curstat, oldstat, newstat, i);
251 if (update == UPDATE_TIME)
252 memcpy(oldstat, newstat, sizeof(oldstat));
255 void
256 tcp_boot(char *args)
259 memset(oldstat, 0, sizeof(oldstat));
260 update = UPDATE_BOOT;
263 void
264 tcp_run(char *args)
267 if (update != UPDATE_RUN) {
268 memcpy(oldstat, newstat, sizeof(oldstat));
269 update = UPDATE_RUN;
273 void
274 tcp_time(char *args)
277 if (update != UPDATE_TIME) {
278 memcpy(oldstat, newstat, sizeof(oldstat));
279 update = UPDATE_TIME;
283 void
284 tcp_zero(char *args)
287 if (update == UPDATE_RUN)
288 memcpy(oldstat, newstat, sizeof(oldstat));