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 * 4. Neither the name of the University nor the names of its contributors
1429881Swollman *    may be used to endorse or promote products derived from this software
1529881Swollman *    without specific prior written permission.
1629881Swollman *
1729881Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1829881Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1929881Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2029881Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2129881Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2229881Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2329881Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2429881Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2529881Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2629881Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2729881Swollman * SUCH DAMAGE.
2829881Swollman */
2929881Swollman
30126229Sbde#if 0
31126229Sbde#ifndef lint
32126229Sbde/* From: */
33126229Sbdestatic char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
34126229Sbdestatic const char rcsid[] =
35126229Sbde	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp";
36126229Sbde#endif /* not lint */
37126229Sbde#endif
38126229Sbde
3987715Smarkm#include <sys/cdefs.h>
4087715Smarkm__FBSDID("$FreeBSD$");
4187715Smarkm
4229881Swollman#include <sys/param.h>
4329881Swollman#include <sys/types.h>
4429881Swollman#include <sys/socket.h>
4529881Swollman#include <sys/sysctl.h>
4629881Swollman
4729881Swollman#include <netinet/in.h>
4829881Swollman#include <netinet/in_systm.h>
4929881Swollman#include <netinet/ip.h>
5029881Swollman#include <netinet/tcp.h>
5129881Swollman#include <netinet/tcp_seq.h>
5229881Swollman#include <netinet/tcp_fsm.h>
5329881Swollman#include <netinet/tcp_timer.h>
5429881Swollman#include <netinet/tcp_var.h>
5529881Swollman
56200462Sdelphij#include <stdlib.h>
5729881Swollman#include <string.h>
58200462Sdelphij#include <paths.h>
5987715Smarkm
6029881Swollman#include "systat.h"
6129881Swollman#include "extern.h"
6229881Swollman#include "mode.h"
6329881Swollman
6429881Swollmanstatic struct tcpstat curstat, initstat, oldstat;
6529881Swollman
6629881Swollman/*-
6729881Swollman--0         1         2         3         4         5         6         7
6829881Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
69170784Sjhb00             TCP Connections                       TCP Packets
70170784Sjhb01999999999999 connections initiated    999999999999 total packets sent
71170784Sjhb02999999999999 connections accepted     999999999999 - data
72170784Sjhb03999999999999 connections established  999999999999 - data (retransmit by dupack)
73170784Sjhb04999999999999 connections dropped      999999999999 - data (retransmit by sack)
74170784Sjhb05999999999999 - in embryonic state     999999999999 - ack-only
75170784Sjhb06999999999999 - on retransmit timeout  999999999999 - window probes
76170784Sjhb07999999999999 - by keepalive           999999999999 - window updates
77170784Sjhb08999999999999 - from listen queue      999999999999 - urgent data only
78170784Sjhb09                                      999999999999 - control
79170784Sjhb10                                      999999999999 - resends by PMTU discovery
80170784Sjhb11             TCP Timers               999999999999 total packets received
81170784Sjhb12999999999999 potential rtt updates    999999999999 - in sequence
82170784Sjhb13999999999999 - successful             999999999999 - completely duplicate
83170784Sjhb14999999999999 delayed acks sent        999999999999 - with some duplicate data
84170784Sjhb15999999999999 retransmit timeouts      999999999999 - out-of-order
85170784Sjhb16999999999999 persist timeouts         999999999999 - duplicate acks
86170784Sjhb17999999999999 keepalive probes         999999999999 - acks
87170784Sjhb18999999999999 - timeouts               999999999999 - window probes
88170784Sjhb19                                      999999999999 - window updates
89170784Sjhb20                                      999999999999 - bad checksum
9029881Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
9129881Swollman--0         1         2         3         4         5         6         7
9229881Swollman*/
9329881Swollman
9429881SwollmanWINDOW *
9529881Swollmanopentcp(void)
9629881Swollman{
97158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
9829881Swollman}
9929881Swollman
10029881Swollmanvoid
101175387Sdelphijclosetcp(WINDOW *w)
10229881Swollman{
10329881Swollman	if (w == NULL)
10429881Swollman		return;
10529881Swollman	wclear(w);
10629881Swollman	wrefresh(w);
107158160Sbde	delwin(w);
10829881Swollman}
10929881Swollman
11029881Swollmanvoid
11129881Swollmanlabeltcp(void)
11229881Swollman{
11329881Swollman	wmove(wnd, 0, 0); wclrtoeol(wnd);
114170784Sjhb#define L(row, str) mvwprintw(wnd, row, 13, str)
115170784Sjhb#define R(row, str) mvwprintw(wnd, row, 51, str);
116158160Sbde	L(0, "TCP Connections");		R(0, "TCP Packets");
117158160Sbde	L(1, "connections initiated");		R(1, "total packets sent");
118158160Sbde	L(2, "connections accepted");		R(2, "- data");
119170780Sjhb	L(3, "connections established");	R(3, "- data (retransmit by dupack)");
120170780Sjhb	L(4, "connections dropped");		R(4, "- data (retransmit by sack)");
121170780Sjhb	L(5, "- in embryonic state");		R(5, "- ack-only");
122170780Sjhb	L(6, "- on retransmit timeout");	R(6, "- window probes");
123170780Sjhb	L(7, "- by keepalive");			R(7, "- window updates");
124170780Sjhb	L(8, "- from listen queue");		R(8, "- urgent data only");
125170782Sjhb						R(9, "- control");
126170782Sjhb						R(10, "- resends by PMTU discovery");
127170782Sjhb	L(11, "TCP Timers");			R(11, "total packets received");
128170782Sjhb	L(12, "potential rtt updates");		R(12, "- in sequence");
129170782Sjhb	L(13, "- successful");			R(13, "- completely duplicate");
130170782Sjhb	L(14, "delayed acks sent");		R(14, "- with some duplicate data");
131170782Sjhb	L(15, "retransmit timeouts");		R(15, "- out-of-order");
132170782Sjhb	L(16, "persist timeouts");		R(16, "- duplicate acks");
133170782Sjhb	L(17, "keepalive probes");		R(17, "- acks");
134170782Sjhb	L(18, "- timeouts");			R(18, "- window probes");
135170782Sjhb						R(19, "- window updates");
136170782Sjhb						R(20, "- bad checksum");
13729881Swollman#undef L
13829881Swollman#undef R
13929881Swollman}
14029881Swollman
14129881Swollmanstatic void
14229881Swollmandomode(struct tcpstat *ret)
14329881Swollman{
14429881Swollman	const struct tcpstat *sub;
14540060Sobrien	int divisor = 1;
14629881Swollman
14729881Swollman	switch(currentmode) {
14829881Swollman	case display_RATE:
14929881Swollman		sub = &oldstat;
150240605Smelifaro		divisor = (delay > 1000000) ? delay / 1000000 : 1;
15129881Swollman		break;
15229881Swollman	case display_DELTA:
15329881Swollman		sub = &oldstat;
15429881Swollman		break;
15529881Swollman	case display_SINCE:
15629881Swollman		sub = &initstat;
15729881Swollman		break;
15829881Swollman	default:
15929881Swollman		*ret = curstat;
16029881Swollman		return;
16129881Swollman	}
16229881Swollman#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
16329881Swollman	DO(tcps_connattempt);
16429881Swollman	DO(tcps_accepts);
16529881Swollman	DO(tcps_connects);
16629881Swollman	DO(tcps_drops);
16729881Swollman	DO(tcps_conndrops);
16829881Swollman	DO(tcps_closed);
16929881Swollman	DO(tcps_segstimed);
17029881Swollman	DO(tcps_rttupdated);
17129881Swollman	DO(tcps_delack);
17229881Swollman	DO(tcps_timeoutdrop);
17329881Swollman	DO(tcps_rexmttimeo);
17429881Swollman	DO(tcps_persisttimeo);
17529881Swollman	DO(tcps_keeptimeo);
17629881Swollman	DO(tcps_keepprobe);
17729881Swollman	DO(tcps_keepdrops);
17829881Swollman
17929881Swollman	DO(tcps_sndtotal);
18029881Swollman	DO(tcps_sndpack);
18129881Swollman	DO(tcps_sndbyte);
18229881Swollman	DO(tcps_sndrexmitpack);
183170780Sjhb	DO(tcps_sack_rexmits);
18429881Swollman	DO(tcps_sndacks);
18529881Swollman	DO(tcps_sndprobe);
18629881Swollman	DO(tcps_sndurg);
18729881Swollman	DO(tcps_sndwinup);
18829881Swollman	DO(tcps_sndctrl);
18929881Swollman
19029881Swollman	DO(tcps_rcvtotal);
19129881Swollman	DO(tcps_rcvpack);
19229881Swollman	DO(tcps_rcvbyte);
19329881Swollman	DO(tcps_rcvbadsum);
19429881Swollman	DO(tcps_rcvbadoff);
19529881Swollman	DO(tcps_rcvshort);
19629881Swollman	DO(tcps_rcvduppack);
19729881Swollman	DO(tcps_rcvdupbyte);
19829881Swollman	DO(tcps_rcvpartduppack);
19929881Swollman	DO(tcps_rcvpartdupbyte);
20029881Swollman	DO(tcps_rcvoopack);
20129881Swollman	DO(tcps_rcvoobyte);
20229881Swollman	DO(tcps_rcvpackafterwin);
20329881Swollman	DO(tcps_rcvbyteafterwin);
20429881Swollman	DO(tcps_rcvafterclose);
20529881Swollman	DO(tcps_rcvwinprobe);
20629881Swollman	DO(tcps_rcvdupack);
20729881Swollman	DO(tcps_rcvacktoomuch);
20829881Swollman	DO(tcps_rcvackpack);
20929881Swollman	DO(tcps_rcvackbyte);
21029881Swollman	DO(tcps_rcvwinupd);
21129881Swollman	DO(tcps_pawsdrop);
21229881Swollman	DO(tcps_predack);
21329881Swollman	DO(tcps_preddat);
21429881Swollman	DO(tcps_pcbcachemiss);
21529881Swollman	DO(tcps_cachedrtt);
21629881Swollman	DO(tcps_cachedrttvar);
21729881Swollman	DO(tcps_cachedssthresh);
21829881Swollman	DO(tcps_usedrtt);
21929881Swollman	DO(tcps_usedrttvar);
22029881Swollman	DO(tcps_usedssthresh);
22129881Swollman	DO(tcps_persistdrop);
22229881Swollman	DO(tcps_badsyn);
22329881Swollman	DO(tcps_mturesent);
22429881Swollman	DO(tcps_listendrop);
22529881Swollman#undef DO
22629881Swollman}
227158161Sbde
22829881Swollmanvoid
22929881Swollmanshowtcp(void)
23029881Swollman{
23129881Swollman	struct tcpstat stats;
23229881Swollman
23329881Swollman	memset(&stats, 0, sizeof stats);
23429881Swollman	domode(&stats);
23529881Swollman
23629881Swollman#define DO(stat, row, col) \
237170784Sjhb	mvwprintw(wnd, row, col, "%12lu", stats.stat)
23829881Swollman#define	L(row, stat) DO(stat, row, 0)
239170784Sjhb#define	R(row, stat) DO(stat, row, 38)
240158160Sbde	L(1, tcps_connattempt);		R(1, tcps_sndtotal);
241158160Sbde	L(2, tcps_accepts);		R(2, tcps_sndpack);
242158160Sbde	L(3, tcps_connects);		R(3, tcps_sndrexmitpack);
243170780Sjhb	L(4, tcps_drops);		R(4, tcps_sack_rexmits);
244170780Sjhb	L(5, tcps_conndrops);		R(5, tcps_sndacks);
245170780Sjhb	L(6, tcps_timeoutdrop);		R(6, tcps_sndprobe);
246170780Sjhb	L(7, tcps_keepdrops);		R(7, tcps_sndwinup);
247170780Sjhb	L(8, tcps_listendrop);		R(8, tcps_sndurg);
248170782Sjhb					R(9, tcps_sndctrl);
249170782Sjhb					R(10, tcps_mturesent);
250170782Sjhb					R(11, tcps_rcvtotal);
251170780Sjhb	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
252170780Sjhb	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
253170780Sjhb	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
254170780Sjhb	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
255170780Sjhb	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
256170780Sjhb	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
257170780Sjhb	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
258170782Sjhb					R(19, tcps_rcvwinupd);
259170782Sjhb					R(20, tcps_rcvbadsum);
26029881Swollman#undef DO
26129881Swollman#undef L
26229881Swollman#undef R
26329881Swollman}
26429881Swollman
26529881Swollmanint
26629881Swollmaninittcp(void)
26729881Swollman{
26829881Swollman	size_t len;
26929881Swollman	int name[4];
27029881Swollman
27129881Swollman	name[0] = CTL_NET;
27229881Swollman	name[1] = PF_INET;
27329881Swollman	name[2] = IPPROTO_TCP;
27429881Swollman	name[3] = TCPCTL_STATS;
27529881Swollman
27629881Swollman	len = 0;
27729881Swollman	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
27829881Swollman		error("sysctl getting tcpstat size failed");
27929881Swollman		return 0;
28029881Swollman	}
28129881Swollman	if (len > sizeof curstat) {
28229881Swollman		error("tcpstat structure has grown--recompile systat!");
28329881Swollman		return 0;
28429881Swollman	}
28529881Swollman	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
28629881Swollman		error("sysctl getting tcpstat failed");
28729881Swollman		return 0;
28829881Swollman	}
28929881Swollman	oldstat = initstat;
29029881Swollman	return 1;
29129881Swollman}
29229881Swollman
29329881Swollmanvoid
29429881Swollmanresettcp(void)
29529881Swollman{
29629881Swollman	size_t len;
29729881Swollman	int name[4];
29829881Swollman
29929881Swollman	name[0] = CTL_NET;
30029881Swollman	name[1] = PF_INET;
30129881Swollman	name[2] = IPPROTO_TCP;
30229881Swollman	name[3] = TCPCTL_STATS;
30329881Swollman
30429881Swollman	len = sizeof initstat;
30529881Swollman	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
30629881Swollman		error("sysctl getting tcpstat failed");
30729881Swollman	}
30829881Swollman	oldstat = initstat;
30929881Swollman}
31029881Swollman
31129881Swollmanvoid
31229881Swollmanfetchtcp(void)
31329881Swollman{
31429881Swollman	int name[4];
31529881Swollman	size_t len;
31629881Swollman
31729881Swollman	oldstat = curstat;
31829881Swollman	name[0] = CTL_NET;
31929881Swollman	name[1] = PF_INET;
32029881Swollman	name[2] = IPPROTO_TCP;
32129881Swollman	name[3] = TCPCTL_STATS;
32229881Swollman	len = sizeof curstat;
32329881Swollman
32429881Swollman	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
32529881Swollman		return;
32629881Swollman}
327