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: stable/11/usr.bin/systat/tcp.c 368931 2021-01-05 20:02:55Z mr $");
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
56368931Smr#include <inttypes.h>
57200462Sdelphij#include <stdlib.h>
5829881Swollman#include <string.h>
59200462Sdelphij#include <paths.h>
6087715Smarkm
6129881Swollman#include "systat.h"
6229881Swollman#include "extern.h"
6329881Swollman#include "mode.h"
6429881Swollman
6529881Swollmanstatic struct tcpstat curstat, initstat, oldstat;
6629881Swollman
6729881Swollman/*-
6829881Swollman--0         1         2         3         4         5         6         7
6929881Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
70170784Sjhb00             TCP Connections                       TCP Packets
71170784Sjhb01999999999999 connections initiated    999999999999 total packets sent
72170784Sjhb02999999999999 connections accepted     999999999999 - data
73170784Sjhb03999999999999 connections established  999999999999 - data (retransmit by dupack)
74170784Sjhb04999999999999 connections dropped      999999999999 - data (retransmit by sack)
75170784Sjhb05999999999999 - in embryonic state     999999999999 - ack-only
76170784Sjhb06999999999999 - on retransmit timeout  999999999999 - window probes
77170784Sjhb07999999999999 - by keepalive           999999999999 - window updates
78170784Sjhb08999999999999 - from listen queue      999999999999 - urgent data only
79170784Sjhb09                                      999999999999 - control
80170784Sjhb10                                      999999999999 - resends by PMTU discovery
81170784Sjhb11             TCP Timers               999999999999 total packets received
82170784Sjhb12999999999999 potential rtt updates    999999999999 - in sequence
83170784Sjhb13999999999999 - successful             999999999999 - completely duplicate
84170784Sjhb14999999999999 delayed acks sent        999999999999 - with some duplicate data
85170784Sjhb15999999999999 retransmit timeouts      999999999999 - out-of-order
86170784Sjhb16999999999999 persist timeouts         999999999999 - duplicate acks
87170784Sjhb17999999999999 keepalive probes         999999999999 - acks
88170784Sjhb18999999999999 - timeouts               999999999999 - window probes
89170784Sjhb19                                      999999999999 - window updates
90170784Sjhb20                                      999999999999 - bad checksum
9129881Swollman--0123456789012345678901234567890123456789012345678901234567890123456789012345
9229881Swollman--0         1         2         3         4         5         6         7
9329881Swollman*/
9429881Swollman
9529881SwollmanWINDOW *
9629881Swollmanopentcp(void)
9729881Swollman{
98158160Sbde	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
9929881Swollman}
10029881Swollman
10129881Swollmanvoid
102175387Sdelphijclosetcp(WINDOW *w)
10329881Swollman{
10429881Swollman	if (w == NULL)
10529881Swollman		return;
10629881Swollman	wclear(w);
10729881Swollman	wrefresh(w);
108158160Sbde	delwin(w);
10929881Swollman}
11029881Swollman
11129881Swollmanvoid
11229881Swollmanlabeltcp(void)
11329881Swollman{
11429881Swollman	wmove(wnd, 0, 0); wclrtoeol(wnd);
115170784Sjhb#define L(row, str) mvwprintw(wnd, row, 13, str)
116170784Sjhb#define R(row, str) mvwprintw(wnd, row, 51, str);
117158160Sbde	L(0, "TCP Connections");		R(0, "TCP Packets");
118158160Sbde	L(1, "connections initiated");		R(1, "total packets sent");
119158160Sbde	L(2, "connections accepted");		R(2, "- data");
120170780Sjhb	L(3, "connections established");	R(3, "- data (retransmit by dupack)");
121170780Sjhb	L(4, "connections dropped");		R(4, "- data (retransmit by sack)");
122170780Sjhb	L(5, "- in embryonic state");		R(5, "- ack-only");
123170780Sjhb	L(6, "- on retransmit timeout");	R(6, "- window probes");
124170780Sjhb	L(7, "- by keepalive");			R(7, "- window updates");
125170780Sjhb	L(8, "- from listen queue");		R(8, "- urgent data only");
126170782Sjhb						R(9, "- control");
127170782Sjhb						R(10, "- resends by PMTU discovery");
128170782Sjhb	L(11, "TCP Timers");			R(11, "total packets received");
129170782Sjhb	L(12, "potential rtt updates");		R(12, "- in sequence");
130170782Sjhb	L(13, "- successful");			R(13, "- completely duplicate");
131170782Sjhb	L(14, "delayed acks sent");		R(14, "- with some duplicate data");
132170782Sjhb	L(15, "retransmit timeouts");		R(15, "- out-of-order");
133170782Sjhb	L(16, "persist timeouts");		R(16, "- duplicate acks");
134170782Sjhb	L(17, "keepalive probes");		R(17, "- acks");
135170782Sjhb	L(18, "- timeouts");			R(18, "- window probes");
136170782Sjhb						R(19, "- window updates");
137170782Sjhb						R(20, "- bad checksum");
13829881Swollman#undef L
13929881Swollman#undef R
14029881Swollman}
14129881Swollman
14229881Swollmanstatic void
14329881Swollmandomode(struct tcpstat *ret)
14429881Swollman{
14529881Swollman	const struct tcpstat *sub;
14640060Sobrien	int divisor = 1;
14729881Swollman
14829881Swollman	switch(currentmode) {
14929881Swollman	case display_RATE:
15029881Swollman		sub = &oldstat;
151240605Smelifaro		divisor = (delay > 1000000) ? delay / 1000000 : 1;
15229881Swollman		break;
15329881Swollman	case display_DELTA:
15429881Swollman		sub = &oldstat;
15529881Swollman		break;
15629881Swollman	case display_SINCE:
15729881Swollman		sub = &initstat;
15829881Swollman		break;
15929881Swollman	default:
16029881Swollman		*ret = curstat;
16129881Swollman		return;
16229881Swollman	}
16329881Swollman#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
16429881Swollman	DO(tcps_connattempt);
16529881Swollman	DO(tcps_accepts);
16629881Swollman	DO(tcps_connects);
16729881Swollman	DO(tcps_drops);
16829881Swollman	DO(tcps_conndrops);
16929881Swollman	DO(tcps_closed);
17029881Swollman	DO(tcps_segstimed);
17129881Swollman	DO(tcps_rttupdated);
17229881Swollman	DO(tcps_delack);
17329881Swollman	DO(tcps_timeoutdrop);
17429881Swollman	DO(tcps_rexmttimeo);
17529881Swollman	DO(tcps_persisttimeo);
17629881Swollman	DO(tcps_keeptimeo);
17729881Swollman	DO(tcps_keepprobe);
17829881Swollman	DO(tcps_keepdrops);
17929881Swollman
18029881Swollman	DO(tcps_sndtotal);
18129881Swollman	DO(tcps_sndpack);
18229881Swollman	DO(tcps_sndbyte);
18329881Swollman	DO(tcps_sndrexmitpack);
184170780Sjhb	DO(tcps_sack_rexmits);
18529881Swollman	DO(tcps_sndacks);
18629881Swollman	DO(tcps_sndprobe);
18729881Swollman	DO(tcps_sndurg);
18829881Swollman	DO(tcps_sndwinup);
18929881Swollman	DO(tcps_sndctrl);
19029881Swollman
19129881Swollman	DO(tcps_rcvtotal);
19229881Swollman	DO(tcps_rcvpack);
19329881Swollman	DO(tcps_rcvbyte);
19429881Swollman	DO(tcps_rcvbadsum);
19529881Swollman	DO(tcps_rcvbadoff);
19629881Swollman	DO(tcps_rcvshort);
19729881Swollman	DO(tcps_rcvduppack);
19829881Swollman	DO(tcps_rcvdupbyte);
19929881Swollman	DO(tcps_rcvpartduppack);
20029881Swollman	DO(tcps_rcvpartdupbyte);
20129881Swollman	DO(tcps_rcvoopack);
20229881Swollman	DO(tcps_rcvoobyte);
20329881Swollman	DO(tcps_rcvpackafterwin);
20429881Swollman	DO(tcps_rcvbyteafterwin);
20529881Swollman	DO(tcps_rcvafterclose);
20629881Swollman	DO(tcps_rcvwinprobe);
20729881Swollman	DO(tcps_rcvdupack);
20829881Swollman	DO(tcps_rcvacktoomuch);
20929881Swollman	DO(tcps_rcvackpack);
21029881Swollman	DO(tcps_rcvackbyte);
21129881Swollman	DO(tcps_rcvwinupd);
21229881Swollman	DO(tcps_pawsdrop);
21329881Swollman	DO(tcps_predack);
21429881Swollman	DO(tcps_preddat);
21529881Swollman	DO(tcps_pcbcachemiss);
21629881Swollman	DO(tcps_cachedrtt);
21729881Swollman	DO(tcps_cachedrttvar);
21829881Swollman	DO(tcps_cachedssthresh);
21929881Swollman	DO(tcps_usedrtt);
22029881Swollman	DO(tcps_usedrttvar);
22129881Swollman	DO(tcps_usedssthresh);
22229881Swollman	DO(tcps_persistdrop);
22329881Swollman	DO(tcps_badsyn);
22429881Swollman	DO(tcps_mturesent);
22529881Swollman	DO(tcps_listendrop);
22629881Swollman#undef DO
22729881Swollman}
228158161Sbde
22929881Swollmanvoid
23029881Swollmanshowtcp(void)
23129881Swollman{
23229881Swollman	struct tcpstat stats;
23329881Swollman
23429881Swollman	memset(&stats, 0, sizeof stats);
23529881Swollman	domode(&stats);
23629881Swollman
23729881Swollman#define DO(stat, row, col) \
238368931Smr	mvwprintw(wnd, row, col, "%12"PRIu64, stats.stat)
23929881Swollman#define	L(row, stat) DO(stat, row, 0)
240170784Sjhb#define	R(row, stat) DO(stat, row, 38)
241158160Sbde	L(1, tcps_connattempt);		R(1, tcps_sndtotal);
242158160Sbde	L(2, tcps_accepts);		R(2, tcps_sndpack);
243158160Sbde	L(3, tcps_connects);		R(3, tcps_sndrexmitpack);
244170780Sjhb	L(4, tcps_drops);		R(4, tcps_sack_rexmits);
245170780Sjhb	L(5, tcps_conndrops);		R(5, tcps_sndacks);
246170780Sjhb	L(6, tcps_timeoutdrop);		R(6, tcps_sndprobe);
247170780Sjhb	L(7, tcps_keepdrops);		R(7, tcps_sndwinup);
248170780Sjhb	L(8, tcps_listendrop);		R(8, tcps_sndurg);
249170782Sjhb					R(9, tcps_sndctrl);
250170782Sjhb					R(10, tcps_mturesent);
251170782Sjhb					R(11, tcps_rcvtotal);
252170780Sjhb	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
253170780Sjhb	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
254170780Sjhb	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
255170780Sjhb	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
256170780Sjhb	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
257170780Sjhb	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
258170780Sjhb	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
259170782Sjhb					R(19, tcps_rcvwinupd);
260170782Sjhb					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}
328