tcp.c revision 175387
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 175387 2008-01-16 19:27:43Z delphij $");
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
73170784Sjhb00             TCP Connections                       TCP Packets
74170784Sjhb01999999999999 connections initiated    999999999999 total packets sent
75170784Sjhb02999999999999 connections accepted     999999999999 - data
76170784Sjhb03999999999999 connections established  999999999999 - data (retransmit by dupack)
77170784Sjhb04999999999999 connections dropped      999999999999 - data (retransmit by sack)
78170784Sjhb05999999999999 - in embryonic state     999999999999 - ack-only
79170784Sjhb06999999999999 - on retransmit timeout  999999999999 - window probes
80170784Sjhb07999999999999 - by keepalive           999999999999 - window updates
81170784Sjhb08999999999999 - from listen queue      999999999999 - urgent data only
82170784Sjhb09                                      999999999999 - control
83170784Sjhb10                                      999999999999 - resends by PMTU discovery
84170784Sjhb11             TCP Timers               999999999999 total packets received
85170784Sjhb12999999999999 potential rtt updates    999999999999 - in sequence
86170784Sjhb13999999999999 - successful             999999999999 - completely duplicate
87170784Sjhb14999999999999 delayed acks sent        999999999999 - with some duplicate data
88170784Sjhb15999999999999 retransmit timeouts      999999999999 - out-of-order
89170784Sjhb16999999999999 persist timeouts         999999999999 - duplicate acks
90170784Sjhb17999999999999 keepalive probes         999999999999 - acks
91170784Sjhb18999999999999 - timeouts               999999999999 - window probes
92170784Sjhb19                                      999999999999 - window updates
93170784Sjhb20                                      999999999999 - 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
105175387Sdelphijclosetcp(WINDOW *w)
10629881Swollman{
10729881Swollman	if (w == NULL)
10829881Swollman		return;
10929881Swollman	wclear(w);
11029881Swollman	wrefresh(w);
111158160Sbde	delwin(w);
11229881Swollman}
11329881Swollman
11429881Swollmanvoid
11529881Swollmanlabeltcp(void)
11629881Swollman{
11729881Swollman	wmove(wnd, 0, 0); wclrtoeol(wnd);
118170784Sjhb#define L(row, str) mvwprintw(wnd, row, 13, str)
119170784Sjhb#define R(row, str) mvwprintw(wnd, row, 51, str);
120158160Sbde	L(0, "TCP Connections");		R(0, "TCP Packets");
121158160Sbde	L(1, "connections initiated");		R(1, "total packets sent");
122158160Sbde	L(2, "connections accepted");		R(2, "- data");
123170780Sjhb	L(3, "connections established");	R(3, "- data (retransmit by dupack)");
124170780Sjhb	L(4, "connections dropped");		R(4, "- data (retransmit by sack)");
125170780Sjhb	L(5, "- in embryonic state");		R(5, "- ack-only");
126170780Sjhb	L(6, "- on retransmit timeout");	R(6, "- window probes");
127170780Sjhb	L(7, "- by keepalive");			R(7, "- window updates");
128170780Sjhb	L(8, "- from listen queue");		R(8, "- urgent data only");
129170782Sjhb						R(9, "- control");
130170782Sjhb						R(10, "- resends by PMTU discovery");
131170782Sjhb	L(11, "TCP Timers");			R(11, "total packets received");
132170782Sjhb	L(12, "potential rtt updates");		R(12, "- in sequence");
133170782Sjhb	L(13, "- successful");			R(13, "- completely duplicate");
134170782Sjhb	L(14, "delayed acks sent");		R(14, "- with some duplicate data");
135170782Sjhb	L(15, "retransmit timeouts");		R(15, "- out-of-order");
136170782Sjhb	L(16, "persist timeouts");		R(16, "- duplicate acks");
137170782Sjhb	L(17, "keepalive probes");		R(17, "- acks");
138170782Sjhb	L(18, "- timeouts");			R(18, "- window probes");
139170782Sjhb						R(19, "- window updates");
140170782Sjhb						R(20, "- bad checksum");
14129881Swollman#undef L
14229881Swollman#undef R
14329881Swollman}
14429881Swollman
14529881Swollmanstatic void
14629881Swollmandomode(struct tcpstat *ret)
14729881Swollman{
14829881Swollman	const struct tcpstat *sub;
14940060Sobrien	int divisor = 1;
15029881Swollman
15129881Swollman	switch(currentmode) {
15229881Swollman	case display_RATE:
15329881Swollman		sub = &oldstat;
15429881Swollman		divisor = naptime;
15529881Swollman		break;
15629881Swollman	case display_DELTA:
15729881Swollman		sub = &oldstat;
15829881Swollman		break;
15929881Swollman	case display_SINCE:
16029881Swollman		sub = &initstat;
16129881Swollman		break;
16229881Swollman	default:
16329881Swollman		*ret = curstat;
16429881Swollman		return;
16529881Swollman	}
16629881Swollman#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
16729881Swollman	DO(tcps_connattempt);
16829881Swollman	DO(tcps_accepts);
16929881Swollman	DO(tcps_connects);
17029881Swollman	DO(tcps_drops);
17129881Swollman	DO(tcps_conndrops);
17229881Swollman	DO(tcps_closed);
17329881Swollman	DO(tcps_segstimed);
17429881Swollman	DO(tcps_rttupdated);
17529881Swollman	DO(tcps_delack);
17629881Swollman	DO(tcps_timeoutdrop);
17729881Swollman	DO(tcps_rexmttimeo);
17829881Swollman	DO(tcps_persisttimeo);
17929881Swollman	DO(tcps_keeptimeo);
18029881Swollman	DO(tcps_keepprobe);
18129881Swollman	DO(tcps_keepdrops);
18229881Swollman
18329881Swollman	DO(tcps_sndtotal);
18429881Swollman	DO(tcps_sndpack);
18529881Swollman	DO(tcps_sndbyte);
18629881Swollman	DO(tcps_sndrexmitpack);
187170780Sjhb	DO(tcps_sack_rexmits);
18829881Swollman	DO(tcps_sndacks);
18929881Swollman	DO(tcps_sndprobe);
19029881Swollman	DO(tcps_sndurg);
19129881Swollman	DO(tcps_sndwinup);
19229881Swollman	DO(tcps_sndctrl);
19329881Swollman
19429881Swollman	DO(tcps_rcvtotal);
19529881Swollman	DO(tcps_rcvpack);
19629881Swollman	DO(tcps_rcvbyte);
19729881Swollman	DO(tcps_rcvbadsum);
19829881Swollman	DO(tcps_rcvbadoff);
19929881Swollman	DO(tcps_rcvshort);
20029881Swollman	DO(tcps_rcvduppack);
20129881Swollman	DO(tcps_rcvdupbyte);
20229881Swollman	DO(tcps_rcvpartduppack);
20329881Swollman	DO(tcps_rcvpartdupbyte);
20429881Swollman	DO(tcps_rcvoopack);
20529881Swollman	DO(tcps_rcvoobyte);
20629881Swollman	DO(tcps_rcvpackafterwin);
20729881Swollman	DO(tcps_rcvbyteafterwin);
20829881Swollman	DO(tcps_rcvafterclose);
20929881Swollman	DO(tcps_rcvwinprobe);
21029881Swollman	DO(tcps_rcvdupack);
21129881Swollman	DO(tcps_rcvacktoomuch);
21229881Swollman	DO(tcps_rcvackpack);
21329881Swollman	DO(tcps_rcvackbyte);
21429881Swollman	DO(tcps_rcvwinupd);
21529881Swollman	DO(tcps_pawsdrop);
21629881Swollman	DO(tcps_predack);
21729881Swollman	DO(tcps_preddat);
21829881Swollman	DO(tcps_pcbcachemiss);
21929881Swollman	DO(tcps_cachedrtt);
22029881Swollman	DO(tcps_cachedrttvar);
22129881Swollman	DO(tcps_cachedssthresh);
22229881Swollman	DO(tcps_usedrtt);
22329881Swollman	DO(tcps_usedrttvar);
22429881Swollman	DO(tcps_usedssthresh);
22529881Swollman	DO(tcps_persistdrop);
22629881Swollman	DO(tcps_badsyn);
22729881Swollman	DO(tcps_mturesent);
22829881Swollman	DO(tcps_listendrop);
22929881Swollman#undef DO
23029881Swollman}
231158161Sbde
23229881Swollmanvoid
23329881Swollmanshowtcp(void)
23429881Swollman{
23529881Swollman	struct tcpstat stats;
23629881Swollman
23729881Swollman	memset(&stats, 0, sizeof stats);
23829881Swollman	domode(&stats);
23929881Swollman
24029881Swollman#define DO(stat, row, col) \
241170784Sjhb	mvwprintw(wnd, row, col, "%12lu", stats.stat)
24229881Swollman#define	L(row, stat) DO(stat, row, 0)
243170784Sjhb#define	R(row, stat) DO(stat, row, 38)
244158160Sbde	L(1, tcps_connattempt);		R(1, tcps_sndtotal);
245158160Sbde	L(2, tcps_accepts);		R(2, tcps_sndpack);
246158160Sbde	L(3, tcps_connects);		R(3, tcps_sndrexmitpack);
247170780Sjhb	L(4, tcps_drops);		R(4, tcps_sack_rexmits);
248170780Sjhb	L(5, tcps_conndrops);		R(5, tcps_sndacks);
249170780Sjhb	L(6, tcps_timeoutdrop);		R(6, tcps_sndprobe);
250170780Sjhb	L(7, tcps_keepdrops);		R(7, tcps_sndwinup);
251170780Sjhb	L(8, tcps_listendrop);		R(8, tcps_sndurg);
252170782Sjhb					R(9, tcps_sndctrl);
253170782Sjhb					R(10, tcps_mturesent);
254170782Sjhb					R(11, tcps_rcvtotal);
255170780Sjhb	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
256170780Sjhb	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
257170780Sjhb	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
258170780Sjhb	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
259170780Sjhb	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
260170780Sjhb	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
261170780Sjhb	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
262170782Sjhb					R(19, tcps_rcvwinupd);
263170782Sjhb					R(20, tcps_rcvbadsum);
26429881Swollman#undef DO
26529881Swollman#undef L
26629881Swollman#undef R
26729881Swollman}
26829881Swollman
26929881Swollmanint
27029881Swollmaninittcp(void)
27129881Swollman{
27229881Swollman	size_t len;
27329881Swollman	int name[4];
27429881Swollman
27529881Swollman	name[0] = CTL_NET;
27629881Swollman	name[1] = PF_INET;
27729881Swollman	name[2] = IPPROTO_TCP;
27829881Swollman	name[3] = TCPCTL_STATS;
27929881Swollman
28029881Swollman	len = 0;
28129881Swollman	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
28229881Swollman		error("sysctl getting tcpstat size failed");
28329881Swollman		return 0;
28429881Swollman	}
28529881Swollman	if (len > sizeof curstat) {
28629881Swollman		error("tcpstat structure has grown--recompile systat!");
28729881Swollman		return 0;
28829881Swollman	}
28929881Swollman	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
29029881Swollman		error("sysctl getting tcpstat failed");
29129881Swollman		return 0;
29229881Swollman	}
29329881Swollman	oldstat = initstat;
29429881Swollman	return 1;
29529881Swollman}
29629881Swollman
29729881Swollmanvoid
29829881Swollmanresettcp(void)
29929881Swollman{
30029881Swollman	size_t len;
30129881Swollman	int name[4];
30229881Swollman
30329881Swollman	name[0] = CTL_NET;
30429881Swollman	name[1] = PF_INET;
30529881Swollman	name[2] = IPPROTO_TCP;
30629881Swollman	name[3] = TCPCTL_STATS;
30729881Swollman
30829881Swollman	len = sizeof initstat;
30929881Swollman	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
31029881Swollman		error("sysctl getting tcpstat failed");
31129881Swollman	}
31229881Swollman	oldstat = initstat;
31329881Swollman}
31429881Swollman
31529881Swollmanvoid
31629881Swollmanfetchtcp(void)
31729881Swollman{
31829881Swollman	int name[4];
31929881Swollman	size_t len;
32029881Swollman
32129881Swollman	oldstat = curstat;
32229881Swollman	name[0] = CTL_NET;
32329881Swollman	name[1] = PF_INET;
32429881Swollman	name[2] = IPPROTO_TCP;
32529881Swollman	name[3] = TCPCTL_STATS;
32629881Swollman	len = sizeof curstat;
32729881Swollman
32829881Swollman	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
32929881Swollman		return;
33029881Swollman}
33129881Swollman
332