tcp.c revision 87715
1/*-
2 * Copyright (c) 1980, 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35
36__FBSDID("$FreeBSD: head/usr.bin/systat/tcp.c 87715 2001-12-12 00:13:37Z markm $");
37
38/* From:
39	"@(#)mbufs.c	8.1 (Berkeley) 6/6/93"
40	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
41*/
42
43#include <sys/param.h>
44#include <sys/types.h>
45#include <sys/socket.h>
46#include <sys/sysctl.h>
47
48#include <net/route.h>
49
50#include <netinet/in.h>
51#include <netinet/in_systm.h>
52#include <netinet/ip.h>
53#include <netinet/tcp.h>
54#include <netinet/tcp_seq.h>
55#include <netinet/tcp_fsm.h>
56#include <netinet/tcp_timer.h>
57#include <netinet/tcp_var.h>
58
59#include <stdlib.h>
60#include <string.h>
61#include <paths.h>
62
63#include "systat.h"
64#include "extern.h"
65#include "mode.h"
66
67static struct tcpstat curstat, initstat, oldstat;
68
69/*-
70--0         1         2         3         4         5         6         7
71--0123456789012345678901234567890123456789012345678901234567890123456789012345
7201          TCP Connections                    TCP Packets
7302999999999 connections initiated    999999999 total packets sent
7403999999999 connections accepted     999999999 - data
7504999999999 connections established  999999999 - data (retransmit)
7605999999999 connections dropped      999999999 - ack-only
7706999999999 - in embryonic state     999999999 - window probes
7807999999999 - on retransmit timeout  999999999 - window updates
7908999999999 - by keepalive           999999999 - urgent data only
8009999999999 - from listen queue      999999999 - control
8110                                   999999999 - resends by PMTU discovery
8211          TCP Timers               999999999 total packets received
8312999999999 potential rtt updates    999999999 - in sequence
8413999999999 - successful             999999999 - completely duplicate
8514999999999 delayed acks sent        999999999 - with some duplicate data
8615999999999 retransmit timeouts      999999999 - out-of-order
8716999999999 persist timeouts         999999999 - duplicate acks
8817999999999 keepalive probes         999999999 - acks
8918999999999 - timeouts               999999999 - window probes
9019                                   999999999 - window updates
9120                                   999999999 - bad checksum
92--0123456789012345678901234567890123456789012345678901234567890123456789012345
93--0         1         2         3         4         5         6         7
94*/
95
96WINDOW *
97opentcp(void)
98{
99
100	return (stdscr);
101}
102
103void
104closetcp(w)
105	WINDOW *w;
106{
107	if (w == NULL)
108		return;
109	wclear(w);
110	wrefresh(w);
111}
112
113void
114labeltcp(void)
115{
116	wmove(wnd, 0, 0); wclrtoeol(wnd);
117#define L(row, str) mvwprintw(wnd, row, 10, str)
118#define R(row, str) mvwprintw(wnd, row, 45, str);
119	L(1, "TCP Connections");		R(1, "TCP Packets");
120	L(2, "connections initiated");		R(2, "total packets sent");
121	L(3, "connections accepted");		R(3, "- data");
122	L(4, "connections established");	R(4, "- data (retransmit)");
123	L(5, "connections dropped");		R(5, "- ack-only");
124	L(6, "- in embryonic state");		R(6, "- window probes");
125	L(7, "- on retransmit timeout");	R(7, "- window updates");
126	L(8, "- by keepalive");			R(8, "- urgent data only");
127	L(9, "- from listen queue");		R(9, "- control");
128	R(10, "- resends by PMTU discovery");
129	L(11, "TCP Timers");		R(11, "total packets received");
130	L(12, "potential rtt updates");		R(12, "- in sequence");
131	L(13, "- successful");		R(13, "- completely duplicate");
132	L(14, "delayed acks sent");	R(14, "- with some duplicate data");
133	L(15, "retransmit timeouts");	R(15, "- out-of-order");
134	L(16, "persist timeouts");	R(16, "- duplicate acks");
135	L(17, "keepalive probes");	R(17, "- acks");
136	L(18, "- timeouts");		R(18, "- window probes");
137	R(19, "- window updates");
138	R(20, "- bad checksum");
139#undef L
140#undef R
141}
142
143static void
144domode(struct tcpstat *ret)
145{
146	const struct tcpstat *sub;
147	int divisor = 1;
148
149	switch(currentmode) {
150	case display_RATE:
151		sub = &oldstat;
152		divisor = naptime;
153		break;
154	case display_DELTA:
155		sub = &oldstat;
156		break;
157	case display_SINCE:
158		sub = &initstat;
159		break;
160	default:
161		*ret = curstat;
162		return;
163	}
164#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
165	DO(tcps_connattempt);
166	DO(tcps_accepts);
167	DO(tcps_connects);
168	DO(tcps_drops);
169	DO(tcps_conndrops);
170	DO(tcps_closed);
171	DO(tcps_segstimed);
172	DO(tcps_rttupdated);
173	DO(tcps_delack);
174	DO(tcps_timeoutdrop);
175	DO(tcps_rexmttimeo);
176	DO(tcps_persisttimeo);
177	DO(tcps_keeptimeo);
178	DO(tcps_keepprobe);
179	DO(tcps_keepdrops);
180
181	DO(tcps_sndtotal);
182	DO(tcps_sndpack);
183	DO(tcps_sndbyte);
184	DO(tcps_sndrexmitpack);
185	DO(tcps_sndrexmitbyte);
186	DO(tcps_sndacks);
187	DO(tcps_sndprobe);
188	DO(tcps_sndurg);
189	DO(tcps_sndwinup);
190	DO(tcps_sndctrl);
191
192	DO(tcps_rcvtotal);
193	DO(tcps_rcvpack);
194	DO(tcps_rcvbyte);
195	DO(tcps_rcvbadsum);
196	DO(tcps_rcvbadoff);
197	DO(tcps_rcvshort);
198	DO(tcps_rcvduppack);
199	DO(tcps_rcvdupbyte);
200	DO(tcps_rcvpartduppack);
201	DO(tcps_rcvpartdupbyte);
202	DO(tcps_rcvoopack);
203	DO(tcps_rcvoobyte);
204	DO(tcps_rcvpackafterwin);
205	DO(tcps_rcvbyteafterwin);
206	DO(tcps_rcvafterclose);
207	DO(tcps_rcvwinprobe);
208	DO(tcps_rcvdupack);
209	DO(tcps_rcvacktoomuch);
210	DO(tcps_rcvackpack);
211	DO(tcps_rcvackbyte);
212	DO(tcps_rcvwinupd);
213	DO(tcps_pawsdrop);
214	DO(tcps_predack);
215	DO(tcps_preddat);
216	DO(tcps_pcbcachemiss);
217	DO(tcps_cachedrtt);
218	DO(tcps_cachedrttvar);
219	DO(tcps_cachedssthresh);
220	DO(tcps_usedrtt);
221	DO(tcps_usedrttvar);
222	DO(tcps_usedssthresh);
223	DO(tcps_persistdrop);
224	DO(tcps_badsyn);
225	DO(tcps_mturesent);
226	DO(tcps_listendrop);
227#undef DO
228}
229
230void
231showtcp(void)
232{
233	struct tcpstat stats;
234
235	memset(&stats, 0, sizeof stats);
236	domode(&stats);
237
238#define DO(stat, row, col) \
239	mvwprintw(wnd, row, col, "%9lu", stats.stat)
240#define	L(row, stat) DO(stat, row, 0)
241#define	R(row, stat) DO(stat, row, 35)
242	L(2, tcps_connattempt);		R(2, tcps_sndtotal);
243	L(3, tcps_accepts);		R(3, tcps_sndpack);
244	L(4, tcps_connects);		R(4, tcps_sndrexmitpack);
245	L(5, tcps_drops);		R(5, tcps_sndacks);
246	L(6, tcps_conndrops);		R(6, tcps_sndprobe);
247	L(7, tcps_timeoutdrop);		R(7, tcps_sndwinup);
248	L(8, tcps_keepdrops);		R(8, tcps_sndurg);
249	L(9, tcps_listendrop);		R(9, tcps_sndctrl);
250	R(10, tcps_mturesent);
251	R(11, tcps_rcvtotal);
252	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
253	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
254	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
255	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
256	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
257	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
258	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
259	R(19, tcps_rcvwinupd);
260	R(20, tcps_rcvbadsum);
261#undef DO
262#undef L
263#undef R
264}
265
266int
267inittcp(void)
268{
269	size_t len;
270	int name[4];
271
272	name[0] = CTL_NET;
273	name[1] = PF_INET;
274	name[2] = IPPROTO_TCP;
275	name[3] = TCPCTL_STATS;
276
277	len = 0;
278	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
279		error("sysctl getting tcpstat size failed");
280		return 0;
281	}
282	if (len > sizeof curstat) {
283		error("tcpstat structure has grown--recompile systat!");
284		return 0;
285	}
286	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
287		error("sysctl getting tcpstat failed");
288		return 0;
289	}
290	oldstat = initstat;
291	return 1;
292}
293
294void
295resettcp(void)
296{
297	size_t len;
298	int name[4];
299
300	name[0] = CTL_NET;
301	name[1] = PF_INET;
302	name[2] = IPPROTO_TCP;
303	name[3] = TCPCTL_STATS;
304
305	len = sizeof initstat;
306	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
307		error("sysctl getting tcpstat failed");
308	}
309	oldstat = initstat;
310}
311
312void
313fetchtcp(void)
314{
315	int name[4];
316	size_t len;
317
318	oldstat = curstat;
319	name[0] = CTL_NET;
320	name[1] = PF_INET;
321	name[2] = IPPROTO_TCP;
322	name[3] = TCPCTL_STATS;
323	len = sizeof curstat;
324
325	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
326		return;
327}
328
329