1/*-
2 * Copyright (c) 1980, 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32/* From: */
33static char sccsid[] = "@(#)mbufs.c	8.1 (Berkeley) 6/6/93";
34static const char rcsid[] =
35	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp";
36#endif /* not lint */
37#endif
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: stable/11/usr.bin/systat/tcp.c 368931 2021-01-05 20:02:55Z mr $");
41
42#include <sys/param.h>
43#include <sys/types.h>
44#include <sys/socket.h>
45#include <sys/sysctl.h>
46
47#include <netinet/in.h>
48#include <netinet/in_systm.h>
49#include <netinet/ip.h>
50#include <netinet/tcp.h>
51#include <netinet/tcp_seq.h>
52#include <netinet/tcp_fsm.h>
53#include <netinet/tcp_timer.h>
54#include <netinet/tcp_var.h>
55
56#include <inttypes.h>
57#include <stdlib.h>
58#include <string.h>
59#include <paths.h>
60
61#include "systat.h"
62#include "extern.h"
63#include "mode.h"
64
65static struct tcpstat curstat, initstat, oldstat;
66
67/*-
68--0         1         2         3         4         5         6         7
69--0123456789012345678901234567890123456789012345678901234567890123456789012345
7000             TCP Connections                       TCP Packets
7101999999999999 connections initiated    999999999999 total packets sent
7202999999999999 connections accepted     999999999999 - data
7303999999999999 connections established  999999999999 - data (retransmit by dupack)
7404999999999999 connections dropped      999999999999 - data (retransmit by sack)
7505999999999999 - in embryonic state     999999999999 - ack-only
7606999999999999 - on retransmit timeout  999999999999 - window probes
7707999999999999 - by keepalive           999999999999 - window updates
7808999999999999 - from listen queue      999999999999 - urgent data only
7909                                      999999999999 - control
8010                                      999999999999 - resends by PMTU discovery
8111             TCP Timers               999999999999 total packets received
8212999999999999 potential rtt updates    999999999999 - in sequence
8313999999999999 - successful             999999999999 - completely duplicate
8414999999999999 delayed acks sent        999999999999 - with some duplicate data
8515999999999999 retransmit timeouts      999999999999 - out-of-order
8616999999999999 persist timeouts         999999999999 - duplicate acks
8717999999999999 keepalive probes         999999999999 - acks
8818999999999999 - timeouts               999999999999 - window probes
8919                                      999999999999 - window updates
9020                                      999999999999 - bad checksum
91--0123456789012345678901234567890123456789012345678901234567890123456789012345
92--0         1         2         3         4         5         6         7
93*/
94
95WINDOW *
96opentcp(void)
97{
98	return (subwin(stdscr, LINES-3-1, 0, MAINWIN_ROW, 0));
99}
100
101void
102closetcp(WINDOW *w)
103{
104	if (w == NULL)
105		return;
106	wclear(w);
107	wrefresh(w);
108	delwin(w);
109}
110
111void
112labeltcp(void)
113{
114	wmove(wnd, 0, 0); wclrtoeol(wnd);
115#define L(row, str) mvwprintw(wnd, row, 13, str)
116#define R(row, str) mvwprintw(wnd, row, 51, str);
117	L(0, "TCP Connections");		R(0, "TCP Packets");
118	L(1, "connections initiated");		R(1, "total packets sent");
119	L(2, "connections accepted");		R(2, "- data");
120	L(3, "connections established");	R(3, "- data (retransmit by dupack)");
121	L(4, "connections dropped");		R(4, "- data (retransmit by sack)");
122	L(5, "- in embryonic state");		R(5, "- ack-only");
123	L(6, "- on retransmit timeout");	R(6, "- window probes");
124	L(7, "- by keepalive");			R(7, "- window updates");
125	L(8, "- from listen queue");		R(8, "- urgent data only");
126						R(9, "- control");
127						R(10, "- resends by PMTU discovery");
128	L(11, "TCP Timers");			R(11, "total packets received");
129	L(12, "potential rtt updates");		R(12, "- in sequence");
130	L(13, "- successful");			R(13, "- completely duplicate");
131	L(14, "delayed acks sent");		R(14, "- with some duplicate data");
132	L(15, "retransmit timeouts");		R(15, "- out-of-order");
133	L(16, "persist timeouts");		R(16, "- duplicate acks");
134	L(17, "keepalive probes");		R(17, "- acks");
135	L(18, "- timeouts");			R(18, "- window probes");
136						R(19, "- window updates");
137						R(20, "- bad checksum");
138#undef L
139#undef R
140}
141
142static void
143domode(struct tcpstat *ret)
144{
145	const struct tcpstat *sub;
146	int divisor = 1;
147
148	switch(currentmode) {
149	case display_RATE:
150		sub = &oldstat;
151		divisor = (delay > 1000000) ? delay / 1000000 : 1;
152		break;
153	case display_DELTA:
154		sub = &oldstat;
155		break;
156	case display_SINCE:
157		sub = &initstat;
158		break;
159	default:
160		*ret = curstat;
161		return;
162	}
163#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
164	DO(tcps_connattempt);
165	DO(tcps_accepts);
166	DO(tcps_connects);
167	DO(tcps_drops);
168	DO(tcps_conndrops);
169	DO(tcps_closed);
170	DO(tcps_segstimed);
171	DO(tcps_rttupdated);
172	DO(tcps_delack);
173	DO(tcps_timeoutdrop);
174	DO(tcps_rexmttimeo);
175	DO(tcps_persisttimeo);
176	DO(tcps_keeptimeo);
177	DO(tcps_keepprobe);
178	DO(tcps_keepdrops);
179
180	DO(tcps_sndtotal);
181	DO(tcps_sndpack);
182	DO(tcps_sndbyte);
183	DO(tcps_sndrexmitpack);
184	DO(tcps_sack_rexmits);
185	DO(tcps_sndacks);
186	DO(tcps_sndprobe);
187	DO(tcps_sndurg);
188	DO(tcps_sndwinup);
189	DO(tcps_sndctrl);
190
191	DO(tcps_rcvtotal);
192	DO(tcps_rcvpack);
193	DO(tcps_rcvbyte);
194	DO(tcps_rcvbadsum);
195	DO(tcps_rcvbadoff);
196	DO(tcps_rcvshort);
197	DO(tcps_rcvduppack);
198	DO(tcps_rcvdupbyte);
199	DO(tcps_rcvpartduppack);
200	DO(tcps_rcvpartdupbyte);
201	DO(tcps_rcvoopack);
202	DO(tcps_rcvoobyte);
203	DO(tcps_rcvpackafterwin);
204	DO(tcps_rcvbyteafterwin);
205	DO(tcps_rcvafterclose);
206	DO(tcps_rcvwinprobe);
207	DO(tcps_rcvdupack);
208	DO(tcps_rcvacktoomuch);
209	DO(tcps_rcvackpack);
210	DO(tcps_rcvackbyte);
211	DO(tcps_rcvwinupd);
212	DO(tcps_pawsdrop);
213	DO(tcps_predack);
214	DO(tcps_preddat);
215	DO(tcps_pcbcachemiss);
216	DO(tcps_cachedrtt);
217	DO(tcps_cachedrttvar);
218	DO(tcps_cachedssthresh);
219	DO(tcps_usedrtt);
220	DO(tcps_usedrttvar);
221	DO(tcps_usedssthresh);
222	DO(tcps_persistdrop);
223	DO(tcps_badsyn);
224	DO(tcps_mturesent);
225	DO(tcps_listendrop);
226#undef DO
227}
228
229void
230showtcp(void)
231{
232	struct tcpstat stats;
233
234	memset(&stats, 0, sizeof stats);
235	domode(&stats);
236
237#define DO(stat, row, col) \
238	mvwprintw(wnd, row, col, "%12"PRIu64, stats.stat)
239#define	L(row, stat) DO(stat, row, 0)
240#define	R(row, stat) DO(stat, row, 38)
241	L(1, tcps_connattempt);		R(1, tcps_sndtotal);
242	L(2, tcps_accepts);		R(2, tcps_sndpack);
243	L(3, tcps_connects);		R(3, tcps_sndrexmitpack);
244	L(4, tcps_drops);		R(4, tcps_sack_rexmits);
245	L(5, tcps_conndrops);		R(5, tcps_sndacks);
246	L(6, tcps_timeoutdrop);		R(6, tcps_sndprobe);
247	L(7, tcps_keepdrops);		R(7, tcps_sndwinup);
248	L(8, tcps_listendrop);		R(8, tcps_sndurg);
249					R(9, tcps_sndctrl);
250					R(10, tcps_mturesent);
251					R(11, tcps_rcvtotal);
252	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
253	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
254	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
255	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
256	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
257	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
258	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
259					R(19, tcps_rcvwinupd);
260					R(20, tcps_rcvbadsum);
261#undef DO
262#undef L
263#undef R
264}
265
266int
267inittcp(void)
268{
269	size_t len;
270	int name[4];
271
272	name[0] = CTL_NET;
273	name[1] = PF_INET;
274	name[2] = IPPROTO_TCP;
275	name[3] = TCPCTL_STATS;
276
277	len = 0;
278	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
279		error("sysctl getting tcpstat size failed");
280		return 0;
281	}
282	if (len > sizeof curstat) {
283		error("tcpstat structure has grown--recompile systat!");
284		return 0;
285	}
286	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
287		error("sysctl getting tcpstat failed");
288		return 0;
289	}
290	oldstat = initstat;
291	return 1;
292}
293
294void
295resettcp(void)
296{
297	size_t len;
298	int name[4];
299
300	name[0] = CTL_NET;
301	name[1] = PF_INET;
302	name[2] = IPPROTO_TCP;
303	name[3] = TCPCTL_STATS;
304
305	len = sizeof initstat;
306	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
307		error("sysctl getting tcpstat failed");
308	}
309	oldstat = initstat;
310}
311
312void
313fetchtcp(void)
314{
315	int name[4];
316	size_t len;
317
318	oldstat = curstat;
319	name[0] = CTL_NET;
320	name[1] = PF_INET;
321	name[2] = IPPROTO_TCP;
322	name[3] = TCPCTL_STATS;
323	len = sizeof curstat;
324
325	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
326		return;
327}
328