tcp.c revision 158161
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#if 0
35#ifndef lint
36/* From: */
37static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
38static const char rcsid[] =
39	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp";
40#endif /* not lint */
41#endif
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/usr.bin/systat/tcp.c 158161 2006-04-30 04:47:23Z bde $");
45
46#include <sys/param.h>
47#include <sys/types.h>
48#include <sys/socket.h>
49#include <sys/sysctl.h>
50
51#include <netinet/in.h>
52#include <netinet/in_systm.h>
53#include <netinet/ip.h>
54#include <netinet/tcp.h>
55#include <netinet/tcp_seq.h>
56#include <netinet/tcp_fsm.h>
57#include <netinet/tcp_timer.h>
58#include <netinet/tcp_var.h>
59
60#include <stdlib.h>
61#include <string.h>
62#include <paths.h>
63
64#include "systat.h"
65#include "extern.h"
66#include "mode.h"
67
68static struct tcpstat curstat, initstat, oldstat;
69
70/*-
71--0         1         2         3         4         5         6         7
72--0123456789012345678901234567890123456789012345678901234567890123456789012345
7300          TCP Connections                    TCP Packets
7401999999999 connections initiated    999999999 total packets sent
7502999999999 connections accepted     999999999 - data
7603999999999 connections established  999999999 - data (retransmit)
7704999999999 connections dropped      999999999 - ack-only
7805999999999 - in embryonic state     999999999 - window probes
7906999999999 - on retransmit timeout  999999999 - window updates
8007999999999 - by keepalive           999999999 - urgent data only
8108999999999 - from listen queue      999999999 - control
8209                                   999999999 - resends by PMTU discovery
8310          TCP Timers               999999999 total packets received
8411999999999 potential rtt updates    999999999 - in sequence
8512999999999 - successful             999999999 - completely duplicate
8613999999999 delayed acks sent        999999999 - with some duplicate data
8714999999999 retransmit timeouts      999999999 - out-of-order
8815999999999 persist timeouts         999999999 - duplicate acks
8916999999999 keepalive probes         999999999 - acks
9017999999999 - timeouts               999999999 - window probes
9118                                   999999999 - window updates
9219                                   999999999 - bad checksum
93--0123456789012345678901234567890123456789012345678901234567890123456789012345
94--0         1         2         3         4         5         6         7
95*/
96
97WINDOW *
98opentcp(void)
99{
100	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
101}
102
103void
104closetcp(w)
105	WINDOW *w;
106{
107	if (w == NULL)
108		return;
109	wclear(w);
110	wrefresh(w);
111	delwin(w);
112}
113
114void
115labeltcp(void)
116{
117	wmove(wnd, 0, 0); wclrtoeol(wnd);
118#define L(row, str) mvwprintw(wnd, row, 10, str)
119#define R(row, str) mvwprintw(wnd, row, 45, str);
120	L(0, "TCP Connections");		R(0, "TCP Packets");
121	L(1, "connections initiated");		R(1, "total packets sent");
122	L(2, "connections accepted");		R(2, "- data");
123	L(3, "connections established");	R(3, "- data (retransmit)");
124	L(4, "connections dropped");		R(4, "- ack-only");
125	L(5, "- in embryonic state");		R(5, "- window probes");
126	L(6, "- on retransmit timeout");	R(6, "- window updates");
127	L(7, "- by keepalive");			R(7, "- urgent data only");
128	L(8, "- from listen queue");		R(8, "- control");
129	R(9, "- resends by PMTU discovery");
130	L(10, "TCP Timers");		R(10, "total packets received");
131	L(11, "potential rtt updates");	R(11, "- in sequence");
132	L(12, "- successful");		R(12, "- completely duplicate");
133	L(13, "delayed acks sent");	R(13, "- with some duplicate data");
134	L(14, "retransmit timeouts");	R(14, "- out-of-order");
135	L(15, "persist timeouts");	R(15, "- duplicate acks");
136	L(16, "keepalive probes");	R(16, "- acks");
137	L(17, "- timeouts");		R(17, "- window probes");
138	R(18, "- window updates");
139	R(19, "- bad checksum");
140#undef L
141#undef R
142}
143
144static void
145domode(struct tcpstat *ret)
146{
147	const struct tcpstat *sub;
148	int divisor = 1;
149
150	switch(currentmode) {
151	case display_RATE:
152		sub = &oldstat;
153		divisor = naptime;
154		break;
155	case display_DELTA:
156		sub = &oldstat;
157		break;
158	case display_SINCE:
159		sub = &initstat;
160		break;
161	default:
162		*ret = curstat;
163		return;
164	}
165#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
166	DO(tcps_connattempt);
167	DO(tcps_accepts);
168	DO(tcps_connects);
169	DO(tcps_drops);
170	DO(tcps_conndrops);
171	DO(tcps_closed);
172	DO(tcps_segstimed);
173	DO(tcps_rttupdated);
174	DO(tcps_delack);
175	DO(tcps_timeoutdrop);
176	DO(tcps_rexmttimeo);
177	DO(tcps_persisttimeo);
178	DO(tcps_keeptimeo);
179	DO(tcps_keepprobe);
180	DO(tcps_keepdrops);
181
182	DO(tcps_sndtotal);
183	DO(tcps_sndpack);
184	DO(tcps_sndbyte);
185	DO(tcps_sndrexmitpack);
186	DO(tcps_sndrexmitbyte);
187	DO(tcps_sndacks);
188	DO(tcps_sndprobe);
189	DO(tcps_sndurg);
190	DO(tcps_sndwinup);
191	DO(tcps_sndctrl);
192
193	DO(tcps_rcvtotal);
194	DO(tcps_rcvpack);
195	DO(tcps_rcvbyte);
196	DO(tcps_rcvbadsum);
197	DO(tcps_rcvbadoff);
198	DO(tcps_rcvshort);
199	DO(tcps_rcvduppack);
200	DO(tcps_rcvdupbyte);
201	DO(tcps_rcvpartduppack);
202	DO(tcps_rcvpartdupbyte);
203	DO(tcps_rcvoopack);
204	DO(tcps_rcvoobyte);
205	DO(tcps_rcvpackafterwin);
206	DO(tcps_rcvbyteafterwin);
207	DO(tcps_rcvafterclose);
208	DO(tcps_rcvwinprobe);
209	DO(tcps_rcvdupack);
210	DO(tcps_rcvacktoomuch);
211	DO(tcps_rcvackpack);
212	DO(tcps_rcvackbyte);
213	DO(tcps_rcvwinupd);
214	DO(tcps_pawsdrop);
215	DO(tcps_predack);
216	DO(tcps_preddat);
217	DO(tcps_pcbcachemiss);
218	DO(tcps_cachedrtt);
219	DO(tcps_cachedrttvar);
220	DO(tcps_cachedssthresh);
221	DO(tcps_usedrtt);
222	DO(tcps_usedrttvar);
223	DO(tcps_usedssthresh);
224	DO(tcps_persistdrop);
225	DO(tcps_badsyn);
226	DO(tcps_mturesent);
227	DO(tcps_listendrop);
228#undef DO
229}
230
231void
232showtcp(void)
233{
234	struct tcpstat stats;
235
236	memset(&stats, 0, sizeof stats);
237	domode(&stats);
238
239#define DO(stat, row, col) \
240	mvwprintw(wnd, row, col, "%9lu", stats.stat)
241#define	L(row, stat) DO(stat, row, 0)
242#define	R(row, stat) DO(stat, row, 35)
243	L(1, tcps_connattempt);		R(1, tcps_sndtotal);
244	L(2, tcps_accepts);		R(2, tcps_sndpack);
245	L(3, tcps_connects);		R(3, tcps_sndrexmitpack);
246	L(4, tcps_drops);		R(4, tcps_sndacks);
247	L(5, tcps_conndrops);		R(5, tcps_sndprobe);
248	L(6, tcps_timeoutdrop);		R(6, tcps_sndwinup);
249	L(7, tcps_keepdrops);		R(7, tcps_sndurg);
250	L(8, tcps_listendrop);		R(8, tcps_sndctrl);
251	R(9, tcps_mturesent);
252	R(10, tcps_rcvtotal);
253	L(11, tcps_segstimed);		R(11, tcps_rcvpack);
254	L(12, tcps_rttupdated);		R(12, tcps_rcvduppack);
255	L(13, tcps_delack);		R(13, tcps_rcvpartduppack);
256	L(14, tcps_rexmttimeo);		R(14, tcps_rcvoopack);
257	L(15, tcps_persisttimeo);	R(15, tcps_rcvdupack);
258	L(16, tcps_keepprobe);		R(16, tcps_rcvackpack);
259	L(17, tcps_keeptimeo);		R(17, tcps_rcvwinprobe);
260	R(18, tcps_rcvwinupd);
261	R(19, tcps_rcvbadsum);
262#undef DO
263#undef L
264#undef R
265}
266
267int
268inittcp(void)
269{
270	size_t len;
271	int name[4];
272
273	name[0] = CTL_NET;
274	name[1] = PF_INET;
275	name[2] = IPPROTO_TCP;
276	name[3] = TCPCTL_STATS;
277
278	len = 0;
279	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
280		error("sysctl getting tcpstat size failed");
281		return 0;
282	}
283	if (len > sizeof curstat) {
284		error("tcpstat structure has grown--recompile systat!");
285		return 0;
286	}
287	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
288		error("sysctl getting tcpstat failed");
289		return 0;
290	}
291	oldstat = initstat;
292	return 1;
293}
294
295void
296resettcp(void)
297{
298	size_t len;
299	int name[4];
300
301	name[0] = CTL_NET;
302	name[1] = PF_INET;
303	name[2] = IPPROTO_TCP;
304	name[3] = TCPCTL_STATS;
305
306	len = sizeof initstat;
307	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
308		error("sysctl getting tcpstat failed");
309	}
310	oldstat = initstat;
311}
312
313void
314fetchtcp(void)
315{
316	int name[4];
317	size_t len;
318
319	oldstat = curstat;
320	name[0] = CTL_NET;
321	name[1] = PF_INET;
322	name[2] = IPPROTO_TCP;
323	name[3] = TCPCTL_STATS;
324	len = sizeof curstat;
325
326	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
327		return;
328}
329
330