tcp.c revision 87715
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
3487715Smarkm#include <sys/cdefs.h>
3587715Smarkm
3687715Smarkm__FBSDID("$FreeBSD: head/usr.bin/systat/tcp.c 87715 2001-12-12 00:13:37Z markm $");
3787715Smarkm
3829881Swollman/* From:
3987715Smarkm	"@(#)mbufs.c	8.1 (Berkeley) 6/6/93"
4087715Smarkm	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
4129881Swollman*/
4229881Swollman
4329881Swollman#include <sys/param.h>
4429881Swollman#include <sys/types.h>
4529881Swollman#include <sys/socket.h>
4629881Swollman#include <sys/sysctl.h>
4729881Swollman
4886784Sjlemon#include <net/route.h>
4986784Sjlemon
5029881Swollman#include <netinet/in.h>
5129881Swollman#include <netinet/in_systm.h>
5229881Swollman#include <netinet/ip.h>
5329881Swollman#include <netinet/tcp.h>
5429881Swollman#include <netinet/tcp_seq.h>
5529881Swollman#include <netinet/tcp_fsm.h>
5629881Swollman#include <netinet/tcp_timer.h>
5729881Swollman#include <netinet/tcp_var.h>
5829881Swollman
5929881Swollman#include <stdlib.h>
6029881Swollman#include <string.h>
6129881Swollman#include <paths.h>
6287715Smarkm
6329881Swollman#include "systat.h"
6429881Swollman#include "extern.h"
6529881Swollman#include "mode.h"
6629881Swollman
6729881Swollmanstatic struct tcpstat curstat, initstat, oldstat;
6829881Swollman
6929881Swollman/*-
7029881Swollman--0         1         2         3         4         5         6         7
7129881Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
7229881Swollman01          TCP Connections                    TCP Packets
7329881Swollman02999999999 connections initiated    999999999 total packets sent
7429881Swollman03999999999 connections accepted     999999999 - data
7529881Swollman04999999999 connections established  999999999 - data (retransmit)
7629881Swollman05999999999 connections dropped      999999999 - ack-only
7729881Swollman06999999999 - in embryonic state     999999999 - window probes
7829881Swollman07999999999 - on retransmit timeout  999999999 - window updates
7929881Swollman08999999999 - by keepalive           999999999 - urgent data only
8029881Swollman09999999999 - from listen queue      999999999 - control
8129881Swollman10                                   999999999 - resends by PMTU discovery
8229881Swollman11          TCP Timers               999999999 total packets received
8329881Swollman12999999999 potential rtt updates    999999999 - in sequence
8429881Swollman13999999999 - successful             999999999 - completely duplicate
8529881Swollman14999999999 delayed acks sent        999999999 - with some duplicate data
8629881Swollman15999999999 retransmit timeouts      999999999 - out-of-order
8729881Swollman16999999999 persist timeouts         999999999 - duplicate acks
8829881Swollman17999999999 keepalive probes         999999999 - acks
8929881Swollman18999999999 - timeouts               999999999 - window probes
9029881Swollman19                                   999999999 - window updates
9177514Sru20                                   999999999 - bad checksum
9229881Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
9329881Swollman--0         1         2         3         4         5         6         7
9429881Swollman*/
9529881Swollman
9629881SwollmanWINDOW *
9729881Swollmanopentcp(void)
9829881Swollman{
9977515Sru
10077515Sru	return (stdscr);
10129881Swollman}
10229881Swollman
10329881Swollmanvoid
10429881Swollmanclosetcp(w)
10529881Swollman	WINDOW *w;
10629881Swollman{
10729881Swollman	if (w == NULL)
10829881Swollman		return;
10929881Swollman	wclear(w);
11029881Swollman	wrefresh(w);
11129881Swollman}
11229881Swollman
11329881Swollmanvoid
11429881Swollmanlabeltcp(void)
11529881Swollman{
11629881Swollman	wmove(wnd, 0, 0); wclrtoeol(wnd);
11729881Swollman#define L(row, str) mvwprintw(wnd, row, 10, str)
11829881Swollman#define R(row, str) mvwprintw(wnd, row, 45, str);
11929881Swollman	L(1, "TCP Connections");		R(1, "TCP Packets");
12029881Swollman	L(2, "connections initiated");		R(2, "total packets sent");
12129881Swollman	L(3, "connections accepted");		R(3, "- data");
12229881Swollman	L(4, "connections established");	R(4, "- data (retransmit)");
12329881Swollman	L(5, "connections dropped");		R(5, "- ack-only");
12429881Swollman	L(6, "- in embryonic state");		R(6, "- window probes");
12529881Swollman	L(7, "- on retransmit timeout");	R(7, "- window updates");
12629881Swollman	L(8, "- by keepalive");			R(8, "- urgent data only");
12729881Swollman	L(9, "- from listen queue");		R(9, "- control");
12829881Swollman	R(10, "- resends by PMTU discovery");
12929881Swollman	L(11, "TCP Timers");		R(11, "total packets received");
13029881Swollman	L(12, "potential rtt updates");		R(12, "- in sequence");
13129881Swollman	L(13, "- successful");		R(13, "- completely duplicate");
13229881Swollman	L(14, "delayed acks sent");	R(14, "- with some duplicate data");
13329881Swollman	L(15, "retransmit timeouts");	R(15, "- out-of-order");
13429881Swollman	L(16, "persist timeouts");	R(16, "- duplicate acks");
13529881Swollman	L(17, "keepalive probes");	R(17, "- acks");
13629881Swollman	L(18, "- timeouts");		R(18, "- window probes");
13729881Swollman	R(19, "- window updates");
13877514Sru	R(20, "- bad checksum");
13929881Swollman#undef L
14029881Swollman#undef R
14129881Swollman}
14229881Swollman
14329881Swollmanstatic void
14429881Swollmandomode(struct tcpstat *ret)
14529881Swollman{
14629881Swollman	const struct tcpstat *sub;
14740060Sobrien	int divisor = 1;
14829881Swollman
14929881Swollman	switch(currentmode) {
15029881Swollman	case display_RATE:
15129881Swollman		sub = &oldstat;
15229881Swollman		divisor = naptime;
15329881Swollman		break;
15429881Swollman	case display_DELTA:
15529881Swollman		sub = &oldstat;
15629881Swollman		break;
15729881Swollman	case display_SINCE:
15829881Swollman		sub = &initstat;
15929881Swollman		break;
16029881Swollman	default:
16129881Swollman		*ret = curstat;
16229881Swollman		return;
16329881Swollman	}
16429881Swollman#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
16529881Swollman	DO(tcps_connattempt);
16629881Swollman	DO(tcps_accepts);
16729881Swollman	DO(tcps_connects);
16829881Swollman	DO(tcps_drops);
16929881Swollman	DO(tcps_conndrops);
17029881Swollman	DO(tcps_closed);
17129881Swollman	DO(tcps_segstimed);
17229881Swollman	DO(tcps_rttupdated);
17329881Swollman	DO(tcps_delack);
17429881Swollman	DO(tcps_timeoutdrop);
17529881Swollman	DO(tcps_rexmttimeo);
17629881Swollman	DO(tcps_persisttimeo);
17729881Swollman	DO(tcps_keeptimeo);
17829881Swollman	DO(tcps_keepprobe);
17929881Swollman	DO(tcps_keepdrops);
18029881Swollman
18129881Swollman	DO(tcps_sndtotal);
18229881Swollman	DO(tcps_sndpack);
18329881Swollman	DO(tcps_sndbyte);
18429881Swollman	DO(tcps_sndrexmitpack);
18529881Swollman	DO(tcps_sndrexmitbyte);
18629881Swollman	DO(tcps_sndacks);
18729881Swollman	DO(tcps_sndprobe);
18829881Swollman	DO(tcps_sndurg);
18929881Swollman	DO(tcps_sndwinup);
19029881Swollman	DO(tcps_sndctrl);
19129881Swollman
19229881Swollman	DO(tcps_rcvtotal);
19329881Swollman	DO(tcps_rcvpack);
19429881Swollman	DO(tcps_rcvbyte);
19529881Swollman	DO(tcps_rcvbadsum);
19629881Swollman	DO(tcps_rcvbadoff);
19729881Swollman	DO(tcps_rcvshort);
19829881Swollman	DO(tcps_rcvduppack);
19929881Swollman	DO(tcps_rcvdupbyte);
20029881Swollman	DO(tcps_rcvpartduppack);
20129881Swollman	DO(tcps_rcvpartdupbyte);
20229881Swollman	DO(tcps_rcvoopack);
20329881Swollman	DO(tcps_rcvoobyte);
20429881Swollman	DO(tcps_rcvpackafterwin);
20529881Swollman	DO(tcps_rcvbyteafterwin);
20629881Swollman	DO(tcps_rcvafterclose);
20729881Swollman	DO(tcps_rcvwinprobe);
20829881Swollman	DO(tcps_rcvdupack);
20929881Swollman	DO(tcps_rcvacktoomuch);
21029881Swollman	DO(tcps_rcvackpack);
21129881Swollman	DO(tcps_rcvackbyte);
21229881Swollman	DO(tcps_rcvwinupd);
21329881Swollman	DO(tcps_pawsdrop);
21429881Swollman	DO(tcps_predack);
21529881Swollman	DO(tcps_preddat);
21629881Swollman	DO(tcps_pcbcachemiss);
21729881Swollman	DO(tcps_cachedrtt);
21829881Swollman	DO(tcps_cachedrttvar);
21929881Swollman	DO(tcps_cachedssthresh);
22029881Swollman	DO(tcps_usedrtt);
22129881Swollman	DO(tcps_usedrttvar);
22229881Swollman	DO(tcps_usedssthresh);
22329881Swollman	DO(tcps_persistdrop);
22429881Swollman	DO(tcps_badsyn);
22529881Swollman	DO(tcps_mturesent);
22629881Swollman	DO(tcps_listendrop);
22729881Swollman#undef DO
22829881Swollman}
22929881Swollman
23029881Swollmanvoid
23129881Swollmanshowtcp(void)
23229881Swollman{
23329881Swollman	struct tcpstat stats;
23429881Swollman
23529881Swollman	memset(&stats, 0, sizeof stats);
23629881Swollman	domode(&stats);
23729881Swollman
23829881Swollman#define DO(stat, row, col) \
23929881Swollman	mvwprintw(wnd, row, col, "%9lu", stats.stat)
24029881Swollman#define	L(row, stat) DO(stat, row, 0)
24129881Swollman#define	R(row, stat) DO(stat, row, 35)
24229881Swollman	L(2, tcps_connattempt);		R(2, tcps_sndtotal);
24329881Swollman	L(3, tcps_accepts);		R(3, tcps_sndpack);
24429881Swollman	L(4, tcps_connects);		R(4, tcps_sndrexmitpack);
24529881Swollman	L(5, tcps_drops);		R(5, tcps_sndacks);
24629881Swollman	L(6, tcps_conndrops);		R(6, tcps_sndprobe);
24729881Swollman	L(7, tcps_timeoutdrop);		R(7, tcps_sndwinup);
24829881Swollman	L(8, tcps_keepdrops);		R(8, tcps_sndurg);
24929881Swollman	L(9, tcps_listendrop);		R(9, tcps_sndctrl);
25029881Swollman	R(10, tcps_mturesent);
25129881Swollman	R(11, tcps_rcvtotal);
25229881Swollman	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
25329881Swollman	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
25429881Swollman	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
25529881Swollman	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
25629881Swollman	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
25729881Swollman	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
25829881Swollman	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
25929881Swollman	R(19, tcps_rcvwinupd);
26077514Sru	R(20, tcps_rcvbadsum);
26129881Swollman#undef DO
26229881Swollman#undef L
26329881Swollman#undef R
26429881Swollman}
26529881Swollman
26629881Swollmanint
26729881Swollmaninittcp(void)
26829881Swollman{
26929881Swollman	size_t len;
27029881Swollman	int name[4];
27129881Swollman
27229881Swollman	name[0] = CTL_NET;
27329881Swollman	name[1] = PF_INET;
27429881Swollman	name[2] = IPPROTO_TCP;
27529881Swollman	name[3] = TCPCTL_STATS;
27629881Swollman
27729881Swollman	len = 0;
27829881Swollman	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
27929881Swollman		error("sysctl getting tcpstat size failed");
28029881Swollman		return 0;
28129881Swollman	}
28229881Swollman	if (len > sizeof curstat) {
28329881Swollman		error("tcpstat structure has grown--recompile systat!");
28429881Swollman		return 0;
28529881Swollman	}
28629881Swollman	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
28729881Swollman		error("sysctl getting tcpstat failed");
28829881Swollman		return 0;
28929881Swollman	}
29029881Swollman	oldstat = initstat;
29129881Swollman	return 1;
29229881Swollman}
29329881Swollman
29429881Swollmanvoid
29529881Swollmanresettcp(void)
29629881Swollman{
29729881Swollman	size_t len;
29829881Swollman	int name[4];
29929881Swollman
30029881Swollman	name[0] = CTL_NET;
30129881Swollman	name[1] = PF_INET;
30229881Swollman	name[2] = IPPROTO_TCP;
30329881Swollman	name[3] = TCPCTL_STATS;
30429881Swollman
30529881Swollman	len = sizeof initstat;
30629881Swollman	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
30729881Swollman		error("sysctl getting tcpstat failed");
30829881Swollman	}
30929881Swollman	oldstat = initstat;
31029881Swollman}
31129881Swollman
31229881Swollmanvoid
31329881Swollmanfetchtcp(void)
31429881Swollman{
31529881Swollman	int name[4];
31629881Swollman	size_t len;
31729881Swollman
31829881Swollman	oldstat = curstat;
31929881Swollman	name[0] = CTL_NET;
32029881Swollman	name[1] = PF_INET;
32129881Swollman	name[2] = IPPROTO_TCP;
32229881Swollman	name[3] = TCPCTL_STATS;
32329881Swollman	len = sizeof curstat;
32429881Swollman
32529881Swollman	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
32629881Swollman		return;
32729881Swollman}
32829881Swollman
329