xref: /freebsd/usr.bin/systat/tcp.c (revision d22889b82125f63cabcdcae17a4bad4039b43809)
1630fa006SGarrett Wollman /*-
2630fa006SGarrett Wollman  * Copyright (c) 1980, 1992, 1993
3630fa006SGarrett Wollman  *	The Regents of the University of California.  All rights reserved.
4630fa006SGarrett Wollman  *
5630fa006SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
6630fa006SGarrett Wollman  * modification, are permitted provided that the following conditions
7630fa006SGarrett Wollman  * are met:
8630fa006SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
9630fa006SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
10630fa006SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
11630fa006SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
12630fa006SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
13630fa006SGarrett Wollman  * 3. All advertising materials mentioning features or use of this software
14630fa006SGarrett Wollman  *    must display the following acknowledgement:
15630fa006SGarrett Wollman  *	This product includes software developed by the University of
16630fa006SGarrett Wollman  *	California, Berkeley and its contributors.
17630fa006SGarrett Wollman  * 4. Neither the name of the University nor the names of its contributors
18630fa006SGarrett Wollman  *    may be used to endorse or promote products derived from this software
19630fa006SGarrett Wollman  *    without specific prior written permission.
20630fa006SGarrett Wollman  *
21630fa006SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22630fa006SGarrett Wollman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23630fa006SGarrett Wollman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24630fa006SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25630fa006SGarrett Wollman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26630fa006SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27630fa006SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28630fa006SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29630fa006SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30630fa006SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31630fa006SGarrett Wollman  * SUCH DAMAGE.
32630fa006SGarrett Wollman  */
33630fa006SGarrett Wollman 
34630fa006SGarrett Wollman #ifndef lint
35630fa006SGarrett Wollman /* From:
36630fa006SGarrett Wollman static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
37630fa006SGarrett Wollman static const char rcsid[] =
38630fa006SGarrett Wollman 	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp";
39630fa006SGarrett Wollman */
40630fa006SGarrett Wollman static const char rcsid[] =
41d22889b8SDavid E. O'Brien 	"$Id: tcp.c,v 1.1 1997/09/27 00:44:55 wollman Exp $";
42630fa006SGarrett Wollman #endif /* not lint */
43630fa006SGarrett Wollman 
44630fa006SGarrett Wollman #include <sys/param.h>
45630fa006SGarrett Wollman #include <sys/types.h>
46630fa006SGarrett Wollman #include <sys/socket.h>
47630fa006SGarrett Wollman #include <sys/sysctl.h>
48630fa006SGarrett Wollman 
49630fa006SGarrett Wollman #include <netinet/in.h>
50630fa006SGarrett Wollman #include <netinet/in_systm.h>
51630fa006SGarrett Wollman #include <netinet/ip.h>
52630fa006SGarrett Wollman #include <netinet/tcp.h>
53630fa006SGarrett Wollman #include <netinet/tcp_seq.h>
54630fa006SGarrett Wollman #include <netinet/tcp_fsm.h>
55630fa006SGarrett Wollman #include <netinet/tcp_timer.h>
56630fa006SGarrett Wollman #include <netinet/tcp_var.h>
57630fa006SGarrett Wollman 
58630fa006SGarrett Wollman #include <stdlib.h>
59630fa006SGarrett Wollman #include <string.h>
60630fa006SGarrett Wollman #include <paths.h>
61630fa006SGarrett Wollman #include "systat.h"
62630fa006SGarrett Wollman #include "extern.h"
63630fa006SGarrett Wollman #include "mode.h"
64630fa006SGarrett Wollman 
65630fa006SGarrett Wollman static struct tcpstat curstat, initstat, oldstat;
66630fa006SGarrett Wollman 
67630fa006SGarrett Wollman /*-
68630fa006SGarrett Wollman --0         1         2         3         4         5         6         7
69630fa006SGarrett Wollman --0123456789012345678901234567890123456789012345678901234567890123456789012345
70630fa006SGarrett Wollman 01          TCP Connections                    TCP Packets
71630fa006SGarrett Wollman 02999999999 connections initiated    999999999 total packets sent
72630fa006SGarrett Wollman 03999999999 connections accepted     999999999 - data
73630fa006SGarrett Wollman 04999999999 connections established  999999999 - data (retransmit)
74630fa006SGarrett Wollman 05999999999 connections dropped      999999999 - ack-only
75630fa006SGarrett Wollman 06999999999 - in embryonic state     999999999 - window probes
76630fa006SGarrett Wollman 07999999999 - on retransmit timeout  999999999 - window updates
77630fa006SGarrett Wollman 08999999999 - by keepalive           999999999 - urgent data only
78630fa006SGarrett Wollman 09999999999 - from listen queue      999999999 - control
79630fa006SGarrett Wollman 10                                   999999999 - resends by PMTU discovery
80630fa006SGarrett Wollman 11          TCP Timers               999999999 total packets received
81630fa006SGarrett Wollman 12999999999 potential rtt updates    999999999 - in sequence
82630fa006SGarrett Wollman 13999999999 - successful             999999999 - completely duplicate
83630fa006SGarrett Wollman 14999999999 delayed acks sent        999999999 - with some duplicate data
84630fa006SGarrett Wollman 15999999999 retransmit timeouts      999999999 - out-of-order
85630fa006SGarrett Wollman 16999999999 persist timeouts         999999999 - duplicate acks
86630fa006SGarrett Wollman 17999999999 keepalive probes         999999999 - acks
87630fa006SGarrett Wollman 18999999999 - timeouts               999999999 - window probes
88630fa006SGarrett Wollman 19                                   999999999 - window updates
89630fa006SGarrett Wollman --0123456789012345678901234567890123456789012345678901234567890123456789012345
90630fa006SGarrett Wollman --0         1         2         3         4         5         6         7
91630fa006SGarrett Wollman */
92630fa006SGarrett Wollman 
93630fa006SGarrett Wollman WINDOW *
94630fa006SGarrett Wollman opentcp(void)
95630fa006SGarrett Wollman {
96630fa006SGarrett Wollman 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
97630fa006SGarrett Wollman }
98630fa006SGarrett Wollman 
99630fa006SGarrett Wollman void
100630fa006SGarrett Wollman closetcp(w)
101630fa006SGarrett Wollman 	WINDOW *w;
102630fa006SGarrett Wollman {
103630fa006SGarrett Wollman 	if (w == NULL)
104630fa006SGarrett Wollman 		return;
105630fa006SGarrett Wollman 	wclear(w);
106630fa006SGarrett Wollman 	wrefresh(w);
107630fa006SGarrett Wollman 	delwin(w);
108630fa006SGarrett Wollman }
109630fa006SGarrett Wollman 
110630fa006SGarrett Wollman void
111630fa006SGarrett Wollman labeltcp(void)
112630fa006SGarrett Wollman {
113630fa006SGarrett Wollman 	wmove(wnd, 0, 0); wclrtoeol(wnd);
114630fa006SGarrett Wollman #define L(row, str) mvwprintw(wnd, row, 10, str)
115630fa006SGarrett Wollman #define R(row, str) mvwprintw(wnd, row, 45, str);
116630fa006SGarrett Wollman 	L(1, "TCP Connections");		R(1, "TCP Packets");
117630fa006SGarrett Wollman 	L(2, "connections initiated");		R(2, "total packets sent");
118630fa006SGarrett Wollman 	L(3, "connections accepted");		R(3, "- data");
119630fa006SGarrett Wollman 	L(4, "connections established");	R(4, "- data (retransmit)");
120630fa006SGarrett Wollman 	L(5, "connections dropped");		R(5, "- ack-only");
121630fa006SGarrett Wollman 	L(6, "- in embryonic state");		R(6, "- window probes");
122630fa006SGarrett Wollman 	L(7, "- on retransmit timeout");	R(7, "- window updates");
123630fa006SGarrett Wollman 	L(8, "- by keepalive");			R(8, "- urgent data only");
124630fa006SGarrett Wollman 	L(9, "- from listen queue");		R(9, "- control");
125630fa006SGarrett Wollman 	R(10, "- resends by PMTU discovery");
126630fa006SGarrett Wollman 	L(11, "TCP Timers");		R(11, "total packets received");
127630fa006SGarrett Wollman 	L(12, "potential rtt updates");		R(12, "- in sequence");
128630fa006SGarrett Wollman 	L(13, "- successful");		R(13, "- completely duplicate");
129630fa006SGarrett Wollman 	L(14, "delayed acks sent");	R(14, "- with some duplicate data");
130630fa006SGarrett Wollman 	L(15, "retransmit timeouts");	R(15, "- out-of-order");
131630fa006SGarrett Wollman 	L(16, "persist timeouts");	R(16, "- duplicate acks");
132630fa006SGarrett Wollman 	L(17, "keepalive probes");	R(17, "- acks");
133630fa006SGarrett Wollman 	L(18, "- timeouts");		R(18, "- window probes");
134630fa006SGarrett Wollman 	R(19, "- window updates");
135630fa006SGarrett Wollman #undef L
136630fa006SGarrett Wollman #undef R
137630fa006SGarrett Wollman }
138630fa006SGarrett Wollman 
139630fa006SGarrett Wollman static void
140630fa006SGarrett Wollman domode(struct tcpstat *ret)
141630fa006SGarrett Wollman {
142630fa006SGarrett Wollman 	const struct tcpstat *sub;
143d22889b8SDavid E. O'Brien 	int divisor = 1;
144630fa006SGarrett Wollman 
145630fa006SGarrett Wollman 	switch(currentmode) {
146630fa006SGarrett Wollman 	case display_RATE:
147630fa006SGarrett Wollman 		sub = &oldstat;
148630fa006SGarrett Wollman 		divisor = naptime;
149630fa006SGarrett Wollman 		break;
150630fa006SGarrett Wollman 	case display_DELTA:
151630fa006SGarrett Wollman 		sub = &oldstat;
152630fa006SGarrett Wollman 		break;
153630fa006SGarrett Wollman 	case display_SINCE:
154630fa006SGarrett Wollman 		sub = &initstat;
155630fa006SGarrett Wollman 		break;
156630fa006SGarrett Wollman 	default:
157630fa006SGarrett Wollman 		*ret = curstat;
158630fa006SGarrett Wollman 		return;
159630fa006SGarrett Wollman 	}
160630fa006SGarrett Wollman #define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
161630fa006SGarrett Wollman 	DO(tcps_connattempt);
162630fa006SGarrett Wollman 	DO(tcps_accepts);
163630fa006SGarrett Wollman 	DO(tcps_connects);
164630fa006SGarrett Wollman 	DO(tcps_drops);
165630fa006SGarrett Wollman 	DO(tcps_conndrops);
166630fa006SGarrett Wollman 	DO(tcps_closed);
167630fa006SGarrett Wollman 	DO(tcps_segstimed);
168630fa006SGarrett Wollman 	DO(tcps_rttupdated);
169630fa006SGarrett Wollman 	DO(tcps_delack);
170630fa006SGarrett Wollman 	DO(tcps_timeoutdrop);
171630fa006SGarrett Wollman 	DO(tcps_rexmttimeo);
172630fa006SGarrett Wollman 	DO(tcps_persisttimeo);
173630fa006SGarrett Wollman 	DO(tcps_keeptimeo);
174630fa006SGarrett Wollman 	DO(tcps_keepprobe);
175630fa006SGarrett Wollman 	DO(tcps_keepdrops);
176630fa006SGarrett Wollman 
177630fa006SGarrett Wollman 	DO(tcps_sndtotal);
178630fa006SGarrett Wollman 	DO(tcps_sndpack);
179630fa006SGarrett Wollman 	DO(tcps_sndbyte);
180630fa006SGarrett Wollman 	DO(tcps_sndrexmitpack);
181630fa006SGarrett Wollman 	DO(tcps_sndrexmitbyte);
182630fa006SGarrett Wollman 	DO(tcps_sndacks);
183630fa006SGarrett Wollman 	DO(tcps_sndprobe);
184630fa006SGarrett Wollman 	DO(tcps_sndurg);
185630fa006SGarrett Wollman 	DO(tcps_sndwinup);
186630fa006SGarrett Wollman 	DO(tcps_sndctrl);
187630fa006SGarrett Wollman 
188630fa006SGarrett Wollman 	DO(tcps_rcvtotal);
189630fa006SGarrett Wollman 	DO(tcps_rcvpack);
190630fa006SGarrett Wollman 	DO(tcps_rcvbyte);
191630fa006SGarrett Wollman 	DO(tcps_rcvbadsum);
192630fa006SGarrett Wollman 	DO(tcps_rcvbadoff);
193630fa006SGarrett Wollman 	DO(tcps_rcvshort);
194630fa006SGarrett Wollman 	DO(tcps_rcvduppack);
195630fa006SGarrett Wollman 	DO(tcps_rcvdupbyte);
196630fa006SGarrett Wollman 	DO(tcps_rcvpartduppack);
197630fa006SGarrett Wollman 	DO(tcps_rcvpartdupbyte);
198630fa006SGarrett Wollman 	DO(tcps_rcvoopack);
199630fa006SGarrett Wollman 	DO(tcps_rcvoobyte);
200630fa006SGarrett Wollman 	DO(tcps_rcvpackafterwin);
201630fa006SGarrett Wollman 	DO(tcps_rcvbyteafterwin);
202630fa006SGarrett Wollman 	DO(tcps_rcvafterclose);
203630fa006SGarrett Wollman 	DO(tcps_rcvwinprobe);
204630fa006SGarrett Wollman 	DO(tcps_rcvdupack);
205630fa006SGarrett Wollman 	DO(tcps_rcvacktoomuch);
206630fa006SGarrett Wollman 	DO(tcps_rcvackpack);
207630fa006SGarrett Wollman 	DO(tcps_rcvackbyte);
208630fa006SGarrett Wollman 	DO(tcps_rcvwinupd);
209630fa006SGarrett Wollman 	DO(tcps_pawsdrop);
210630fa006SGarrett Wollman 	DO(tcps_predack);
211630fa006SGarrett Wollman 	DO(tcps_preddat);
212630fa006SGarrett Wollman 	DO(tcps_pcbcachemiss);
213630fa006SGarrett Wollman 	DO(tcps_cachedrtt);
214630fa006SGarrett Wollman 	DO(tcps_cachedrttvar);
215630fa006SGarrett Wollman 	DO(tcps_cachedssthresh);
216630fa006SGarrett Wollman 	DO(tcps_usedrtt);
217630fa006SGarrett Wollman 	DO(tcps_usedrttvar);
218630fa006SGarrett Wollman 	DO(tcps_usedssthresh);
219630fa006SGarrett Wollman 	DO(tcps_persistdrop);
220630fa006SGarrett Wollman 	DO(tcps_badsyn);
221630fa006SGarrett Wollman 	DO(tcps_mturesent);
222630fa006SGarrett Wollman 	DO(tcps_listendrop);
223630fa006SGarrett Wollman #undef DO
224630fa006SGarrett Wollman }
225630fa006SGarrett Wollman 
226630fa006SGarrett Wollman void
227630fa006SGarrett Wollman showtcp(void)
228630fa006SGarrett Wollman {
229630fa006SGarrett Wollman 	struct tcpstat stats;
230630fa006SGarrett Wollman 
231630fa006SGarrett Wollman 	memset(&stats, 0, sizeof stats);
232630fa006SGarrett Wollman 	domode(&stats);
233630fa006SGarrett Wollman 
234630fa006SGarrett Wollman #define DO(stat, row, col) \
235630fa006SGarrett Wollman 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
236630fa006SGarrett Wollman #define	L(row, stat) DO(stat, row, 0)
237630fa006SGarrett Wollman #define	R(row, stat) DO(stat, row, 35)
238630fa006SGarrett Wollman 	L(2, tcps_connattempt);		R(2, tcps_sndtotal);
239630fa006SGarrett Wollman 	L(3, tcps_accepts);		R(3, tcps_sndpack);
240630fa006SGarrett Wollman 	L(4, tcps_connects);		R(4, tcps_sndrexmitpack);
241630fa006SGarrett Wollman 	L(5, tcps_drops);		R(5, tcps_sndacks);
242630fa006SGarrett Wollman 	L(6, tcps_conndrops);		R(6, tcps_sndprobe);
243630fa006SGarrett Wollman 	L(7, tcps_timeoutdrop);		R(7, tcps_sndwinup);
244630fa006SGarrett Wollman 	L(8, tcps_keepdrops);		R(8, tcps_sndurg);
245630fa006SGarrett Wollman 	L(9, tcps_listendrop);		R(9, tcps_sndctrl);
246630fa006SGarrett Wollman 	R(10, tcps_mturesent);
247630fa006SGarrett Wollman 	R(11, tcps_rcvtotal);
248630fa006SGarrett Wollman 	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
249630fa006SGarrett Wollman 	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
250630fa006SGarrett Wollman 	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
251630fa006SGarrett Wollman 	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
252630fa006SGarrett Wollman 	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
253630fa006SGarrett Wollman 	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
254630fa006SGarrett Wollman 	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
255630fa006SGarrett Wollman 	R(19, tcps_rcvwinupd);
256630fa006SGarrett Wollman #undef DO
257630fa006SGarrett Wollman #undef L
258630fa006SGarrett Wollman #undef R
259630fa006SGarrett Wollman }
260630fa006SGarrett Wollman 
261630fa006SGarrett Wollman int
262630fa006SGarrett Wollman inittcp(void)
263630fa006SGarrett Wollman {
264630fa006SGarrett Wollman 	size_t len;
265630fa006SGarrett Wollman 	int name[4];
266630fa006SGarrett Wollman 
267630fa006SGarrett Wollman 	name[0] = CTL_NET;
268630fa006SGarrett Wollman 	name[1] = PF_INET;
269630fa006SGarrett Wollman 	name[2] = IPPROTO_TCP;
270630fa006SGarrett Wollman 	name[3] = TCPCTL_STATS;
271630fa006SGarrett Wollman 
272630fa006SGarrett Wollman 	len = 0;
273630fa006SGarrett Wollman 	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
274630fa006SGarrett Wollman 		error("sysctl getting tcpstat size failed");
275630fa006SGarrett Wollman 		return 0;
276630fa006SGarrett Wollman 	}
277630fa006SGarrett Wollman 	if (len > sizeof curstat) {
278630fa006SGarrett Wollman 		error("tcpstat structure has grown--recompile systat!");
279630fa006SGarrett Wollman 		return 0;
280630fa006SGarrett Wollman 	}
281630fa006SGarrett Wollman 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
282630fa006SGarrett Wollman 		error("sysctl getting tcpstat failed");
283630fa006SGarrett Wollman 		return 0;
284630fa006SGarrett Wollman 	}
285630fa006SGarrett Wollman 	oldstat = initstat;
286630fa006SGarrett Wollman 	return 1;
287630fa006SGarrett Wollman }
288630fa006SGarrett Wollman 
289630fa006SGarrett Wollman void
290630fa006SGarrett Wollman resettcp(void)
291630fa006SGarrett Wollman {
292630fa006SGarrett Wollman 	size_t len;
293630fa006SGarrett Wollman 	int name[4];
294630fa006SGarrett Wollman 
295630fa006SGarrett Wollman 	name[0] = CTL_NET;
296630fa006SGarrett Wollman 	name[1] = PF_INET;
297630fa006SGarrett Wollman 	name[2] = IPPROTO_TCP;
298630fa006SGarrett Wollman 	name[3] = TCPCTL_STATS;
299630fa006SGarrett Wollman 
300630fa006SGarrett Wollman 	len = sizeof initstat;
301630fa006SGarrett Wollman 	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
302630fa006SGarrett Wollman 		error("sysctl getting tcpstat failed");
303630fa006SGarrett Wollman 	}
304630fa006SGarrett Wollman 	oldstat = initstat;
305630fa006SGarrett Wollman }
306630fa006SGarrett Wollman 
307630fa006SGarrett Wollman void
308630fa006SGarrett Wollman fetchtcp(void)
309630fa006SGarrett Wollman {
310630fa006SGarrett Wollman 	int name[4];
311630fa006SGarrett Wollman 	size_t len;
312630fa006SGarrett Wollman 
313630fa006SGarrett Wollman 	oldstat = curstat;
314630fa006SGarrett Wollman 	name[0] = CTL_NET;
315630fa006SGarrett Wollman 	name[1] = PF_INET;
316630fa006SGarrett Wollman 	name[2] = IPPROTO_TCP;
317630fa006SGarrett Wollman 	name[3] = TCPCTL_STATS;
318630fa006SGarrett Wollman 	len = sizeof curstat;
319630fa006SGarrett Wollman 
320630fa006SGarrett Wollman 	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
321630fa006SGarrett Wollman 		return;
322630fa006SGarrett Wollman }
323630fa006SGarrett Wollman 
324