tcp.c revision 170780
129881Swollman/*-
229881Swollman * Copyright (c) 1980, 1992, 1993
329881Swollman *	The Regents of the University of California.  All rights reserved.
429881Swollman *
529881Swollman * Redistribution and use in source and binary forms, with or without
629881Swollman * modification, are permitted provided that the following conditions
729881Swollman * are met:
829881Swollman * 1. Redistributions of source code must retain the above copyright
929881Swollman *    notice, this list of conditions and the following disclaimer.
1029881Swollman * 2. Redistributions in binary form must reproduce the above copyright
1129881Swollman *    notice, this list of conditions and the following disclaimer in the
1229881Swollman *    documentation and/or other materials provided with the distribution.
1329881Swollman * 3. All advertising materials mentioning features or use of this software
1429881Swollman *    must display the following acknowledgement:
1529881Swollman *	This product includes software developed by the University of
1629881Swollman *	California, Berkeley and its contributors.
1729881Swollman * 4. Neither the name of the University nor the names of its contributors
1829881Swollman *    may be used to endorse or promote products derived from this software
1929881Swollman *    without specific prior written permission.
2029881Swollman *
2129881Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2229881Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2329881Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2429881Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2529881Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2629881Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2729881Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2829881Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2929881Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3029881Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3129881Swollman * SUCH DAMAGE.
3229881Swollman */
3329881Swollman
34126229Sbde#if 0
35126229Sbde#ifndef lint
36126229Sbde/* From: */
37126229Sbdestatic char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
38126229Sbdestatic const char rcsid[] =
39126229Sbde	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp";
40126229Sbde#endif /* not lint */
41126229Sbde#endif
42126229Sbde
4387715Smarkm#include <sys/cdefs.h>
4487715Smarkm__FBSDID("$FreeBSD: head/usr.bin/systat/tcp.c 170780 2007-06-15 17:55:44Z jhb $");
4587715Smarkm
4629881Swollman#include <sys/param.h>
4729881Swollman#include <sys/types.h>
4829881Swollman#include <sys/socket.h>
4929881Swollman#include <sys/sysctl.h>
5029881Swollman
5129881Swollman#include <netinet/in.h>
5229881Swollman#include <netinet/in_systm.h>
5329881Swollman#include <netinet/ip.h>
5429881Swollman#include <netinet/tcp.h>
5529881Swollman#include <netinet/tcp_seq.h>
5629881Swollman#include <netinet/tcp_fsm.h>
5729881Swollman#include <netinet/tcp_timer.h>
5829881Swollman#include <netinet/tcp_var.h>
5929881Swollman
6029881Swollman#include <stdlib.h>
6129881Swollman#include <string.h>
6229881Swollman#include <paths.h>
6387715Smarkm
6429881Swollman#include "systat.h"
6529881Swollman#include "extern.h"
6629881Swollman#include "mode.h"
6729881Swollman
6829881Swollmanstatic struct tcpstat curstat, initstat, oldstat;
6929881Swollman
7029881Swollman/*-
7129881Swollman--0         1         2         3         4         5         6         7
7229881Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
73158160Sbde00          TCP Connections                    TCP Packets
74158160Sbde01999999999 connections initiated    999999999 total packets sent
75158160Sbde02999999999 connections accepted     999999999 - data
76170780Sjhb03999999999 connections established  999999999 - data (retransmit by dupack)
77170780Sjhb04999999999 connections dropped      999999999 - data (retransmit by sack)
78170780Sjhb05999999999 - in embryonic state     999999999 - ack-only
79170780Sjhb06999999999 - on retransmit timeout  999999999 - window probes
80170780Sjhb07999999999 - by keepalive           999999999 - window updates
81170780Sjhb08999999999 - from listen queue      999999999 - urgent data only
82170780Sjhb09                                   999999999 - control
83170780Sjhb10                                   999999999 - resends by PMTU discovery
84170780Sjhb11          TCP Timers               999999999 total packets received
85170780Sjhb12999999999 potential rtt updates    999999999 - in sequence
86170780Sjhb13999999999 - successful             999999999 - completely duplicate
87170780Sjhb14999999999 delayed acks sent        999999999 - with some duplicate data
88170780Sjhb15999999999 retransmit timeouts      999999999 - out-of-order
89170780Sjhb16999999999 persist timeouts         999999999 - duplicate acks
90170780Sjhb17999999999 keepalive probes         999999999 - acks
91170780Sjhb18999999999 - timeouts               999999999 - window probes
92170780Sjhb19                                   999999999 - window updates
93170780Sjhb20                                   999999999 - bad checksum
9429881Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
9529881Swollman--0         1         2         3         4         5         6         7
9629881Swollman*/
9729881Swollman
9829881SwollmanWINDOW *
9929881Swollmanopentcp(void)
10029881Swollman{
101158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
10229881Swollman}
10329881Swollman
10429881Swollmanvoid
10529881Swollmanclosetcp(w)
10629881Swollman	WINDOW *w;
10729881Swollman{
10829881Swollman	if (w == NULL)
10929881Swollman		return;
11029881Swollman	wclear(w);
11129881Swollman	wrefresh(w);
112158160Sbde	delwin(w);
11329881Swollman}
11429881Swollman
11529881Swollmanvoid
11629881Swollmanlabeltcp(void)
11729881Swollman{
11829881Swollman	wmove(wnd, 0, 0); wclrtoeol(wnd);
11929881Swollman#define L(row, str) mvwprintw(wnd, row, 10, str)
12029881Swollman#define R(row, str) mvwprintw(wnd, row, 45, str);
121158160Sbde	L(0, "TCP Connections");		R(0, "TCP Packets");
122158160Sbde	L(1, "connections initiated");		R(1, "total packets sent");
123158160Sbde	L(2, "connections accepted");		R(2, "- data");
124170780Sjhb	L(3, "connections established");	R(3, "- data (retransmit by dupack)");
125170780Sjhb	L(4, "connections dropped");		R(4, "- data (retransmit by sack)");
126170780Sjhb	L(5, "- in embryonic state");		R(5, "- ack-only");
127170780Sjhb	L(6, "- on retransmit timeout");	R(6, "- window probes");
128170780Sjhb	L(7, "- by keepalive");			R(7, "- window updates");
129170780Sjhb	L(8, "- from listen queue");		R(8, "- urgent data only");
130170780Sjhb	R(9, "- control");
131170780Sjhb	R(10, "- resends by PMTU discovery");
132170780Sjhb	L(11, "TCP Timers");		R(11, "total packets received");
133170780Sjhb	L(12, "potential rtt updates");	R(12, "- in sequence");
134170780Sjhb	L(13, "- successful");		R(13, "- completely duplicate");
135170780Sjhb	L(14, "delayed acks sent");	R(14, "- with some duplicate data");
136170780Sjhb	L(15, "retransmit timeouts");	R(15, "- out-of-order");
137170780Sjhb	L(16, "persist timeouts");	R(16, "- duplicate acks");
138170780Sjhb	L(17, "keepalive probes");	R(17, "- acks");
139170780Sjhb	L(18, "- timeouts");		R(18, "- window probes");
140170780Sjhb	R(19, "- window updates");
141170780Sjhb	R(20, "- bad checksum");
14229881Swollman#undef L
14329881Swollman#undef R
14429881Swollman}
14529881Swollman
14629881Swollmanstatic void
14729881Swollmandomode(struct tcpstat *ret)
14829881Swollman{
14929881Swollman	const struct tcpstat *sub;
15040060Sobrien	int divisor = 1;
15129881Swollman
15229881Swollman	switch(currentmode) {
15329881Swollman	case display_RATE:
15429881Swollman		sub = &oldstat;
15529881Swollman		divisor = naptime;
15629881Swollman		break;
15729881Swollman	case display_DELTA:
15829881Swollman		sub = &oldstat;
15929881Swollman		break;
16029881Swollman	case display_SINCE:
16129881Swollman		sub = &initstat;
16229881Swollman		break;
16329881Swollman	default:
16429881Swollman		*ret = curstat;
16529881Swollman		return;
16629881Swollman	}
16729881Swollman#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
16829881Swollman	DO(tcps_connattempt);
16929881Swollman	DO(tcps_accepts);
17029881Swollman	DO(tcps_connects);
17129881Swollman	DO(tcps_drops);
17229881Swollman	DO(tcps_conndrops);
17329881Swollman	DO(tcps_closed);
17429881Swollman	DO(tcps_segstimed);
17529881Swollman	DO(tcps_rttupdated);
17629881Swollman	DO(tcps_delack);
17729881Swollman	DO(tcps_timeoutdrop);
17829881Swollman	DO(tcps_rexmttimeo);
17929881Swollman	DO(tcps_persisttimeo);
18029881Swollman	DO(tcps_keeptimeo);
18129881Swollman	DO(tcps_keepprobe);
18229881Swollman	DO(tcps_keepdrops);
18329881Swollman
18429881Swollman	DO(tcps_sndtotal);
18529881Swollman	DO(tcps_sndpack);
18629881Swollman	DO(tcps_sndbyte);
18729881Swollman	DO(tcps_sndrexmitpack);
188170780Sjhb	DO(tcps_sack_rexmits);
18929881Swollman	DO(tcps_sndacks);
19029881Swollman	DO(tcps_sndprobe);
19129881Swollman	DO(tcps_sndurg);
19229881Swollman	DO(tcps_sndwinup);
19329881Swollman	DO(tcps_sndctrl);
19429881Swollman
19529881Swollman	DO(tcps_rcvtotal);
19629881Swollman	DO(tcps_rcvpack);
19729881Swollman	DO(tcps_rcvbyte);
19829881Swollman	DO(tcps_rcvbadsum);
19929881Swollman	DO(tcps_rcvbadoff);
20029881Swollman	DO(tcps_rcvshort);
20129881Swollman	DO(tcps_rcvduppack);
20229881Swollman	DO(tcps_rcvdupbyte);
20329881Swollman	DO(tcps_rcvpartduppack);
20429881Swollman	DO(tcps_rcvpartdupbyte);
20529881Swollman	DO(tcps_rcvoopack);
20629881Swollman	DO(tcps_rcvoobyte);
20729881Swollman	DO(tcps_rcvpackafterwin);
20829881Swollman	DO(tcps_rcvbyteafterwin);
20929881Swollman	DO(tcps_rcvafterclose);
21029881Swollman	DO(tcps_rcvwinprobe);
21129881Swollman	DO(tcps_rcvdupack);
21229881Swollman	DO(tcps_rcvacktoomuch);
21329881Swollman	DO(tcps_rcvackpack);
21429881Swollman	DO(tcps_rcvackbyte);
21529881Swollman	DO(tcps_rcvwinupd);
21629881Swollman	DO(tcps_pawsdrop);
21729881Swollman	DO(tcps_predack);
21829881Swollman	DO(tcps_preddat);
21929881Swollman	DO(tcps_pcbcachemiss);
22029881Swollman	DO(tcps_cachedrtt);
22129881Swollman	DO(tcps_cachedrttvar);
22229881Swollman	DO(tcps_cachedssthresh);
22329881Swollman	DO(tcps_usedrtt);
22429881Swollman	DO(tcps_usedrttvar);
22529881Swollman	DO(tcps_usedssthresh);
22629881Swollman	DO(tcps_persistdrop);
22729881Swollman	DO(tcps_badsyn);
22829881Swollman	DO(tcps_mturesent);
22929881Swollman	DO(tcps_listendrop);
23029881Swollman#undef DO
23129881Swollman}
232158161Sbde
23329881Swollmanvoid
23429881Swollmanshowtcp(void)
23529881Swollman{
23629881Swollman	struct tcpstat stats;
23729881Swollman
23829881Swollman	memset(&stats, 0, sizeof stats);
23929881Swollman	domode(&stats);
24029881Swollman
24129881Swollman#define DO(stat, row, col) \
24229881Swollman	mvwprintw(wnd, row, col, "%9lu", stats.stat)
24329881Swollman#define	L(row, stat) DO(stat, row, 0)
24429881Swollman#define	R(row, stat) DO(stat, row, 35)
245158160Sbde	L(1, tcps_connattempt);		R(1, tcps_sndtotal);
246158160Sbde	L(2, tcps_accepts);		R(2, tcps_sndpack);
247158160Sbde	L(3, tcps_connects);		R(3, tcps_sndrexmitpack);
248170780Sjhb	L(4, tcps_drops);		R(4, tcps_sack_rexmits);
249170780Sjhb	L(5, tcps_conndrops);		R(5, tcps_sndacks);
250170780Sjhb	L(6, tcps_timeoutdrop);		R(6, tcps_sndprobe);
251170780Sjhb	L(7, tcps_keepdrops);		R(7, tcps_sndwinup);
252170780Sjhb	L(8, tcps_listendrop);		R(8, tcps_sndurg);
253170780Sjhb	R(9, tcps_sndctrl);
254170780Sjhb	R(10, tcps_mturesent);
255170780Sjhb	R(11, tcps_rcvtotal);
256170780Sjhb	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
257170780Sjhb	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
258170780Sjhb	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
259170780Sjhb	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
260170780Sjhb	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
261170780Sjhb	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
262170780Sjhb	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
263170780Sjhb	R(19, tcps_rcvwinupd);
264170780Sjhb	R(20, tcps_rcvbadsum);
26529881Swollman#undef DO
26629881Swollman#undef L
26729881Swollman#undef R
26829881Swollman}
26929881Swollman
27029881Swollmanint
27129881Swollmaninittcp(void)
27229881Swollman{
27329881Swollman	size_t len;
27429881Swollman	int name[4];
27529881Swollman
27629881Swollman	name[0] = CTL_NET;
27729881Swollman	name[1] = PF_INET;
27829881Swollman	name[2] = IPPROTO_TCP;
27929881Swollman	name[3] = TCPCTL_STATS;
28029881Swollman
28129881Swollman	len = 0;
28229881Swollman	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
28329881Swollman		error("sysctl getting tcpstat size failed");
28429881Swollman		return 0;
28529881Swollman	}
28629881Swollman	if (len > sizeof curstat) {
28729881Swollman		error("tcpstat structure has grown--recompile systat!");
28829881Swollman		return 0;
28929881Swollman	}
29029881Swollman	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
29129881Swollman		error("sysctl getting tcpstat failed");
29229881Swollman		return 0;
29329881Swollman	}
29429881Swollman	oldstat = initstat;
29529881Swollman	return 1;
29629881Swollman}
29729881Swollman
29829881Swollmanvoid
29929881Swollmanresettcp(void)
30029881Swollman{
30129881Swollman	size_t len;
30229881Swollman	int name[4];
30329881Swollman
30429881Swollman	name[0] = CTL_NET;
30529881Swollman	name[1] = PF_INET;
30629881Swollman	name[2] = IPPROTO_TCP;
30729881Swollman	name[3] = TCPCTL_STATS;
30829881Swollman
30929881Swollman	len = sizeof initstat;
31029881Swollman	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
31129881Swollman		error("sysctl getting tcpstat failed");
31229881Swollman	}
31329881Swollman	oldstat = initstat;
31429881Swollman}
31529881Swollman
31629881Swollmanvoid
31729881Swollmanfetchtcp(void)
31829881Swollman{
31929881Swollman	int name[4];
32029881Swollman	size_t len;
32129881Swollman
32229881Swollman	oldstat = curstat;
32329881Swollman	name[0] = CTL_NET;
32429881Swollman	name[1] = PF_INET;
32529881Swollman	name[2] = IPPROTO_TCP;
32629881Swollman	name[3] = TCPCTL_STATS;
32729881Swollman	len = sizeof curstat;
32829881Swollman
32929881Swollman	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
33029881Swollman		return;
33129881Swollman}
33229881Swollman
333