summaryrefslogtreecommitdiffstats
path: root/usr.bin/systat/tcp.c
blob: bd9e2f9c05c680fd03d7dbb8a93e693d629def0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*-
 * Copyright (c) 1980, 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>

__FBSDID("$FreeBSD$");

/* From:
	"@(#)mbufs.c	8.1 (Berkeley) 6/6/93"
	"Id: mbufs.c,v 1.5 1997/02/24 20:59:03 wollman Exp"
*/

#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysctl.h>

#include <net/route.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/tcp_seq.h>
#include <netinet/tcp_fsm.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>

#include <stdlib.h>
#include <string.h>
#include <paths.h>

#include "systat.h"
#include "extern.h"
#include "mode.h"

static struct tcpstat curstat, initstat, oldstat;

/*-
--0         1         2         3         4         5         6         7
--0123456789012345678901234567890123456789012345678901234567890123456789012345
01          TCP Connections                    TCP Packets
02999999999 connections initiated    999999999 total packets sent
03999999999 connections accepted     999999999 - data
04999999999 connections established  999999999 - data (retransmit)
05999999999 connections dropped      999999999 - ack-only
06999999999 - in embryonic state     999999999 - window probes
07999999999 - on retransmit timeout  999999999 - window updates
08999999999 - by keepalive           999999999 - urgent data only
09999999999 - from listen queue      999999999 - control
10                                   999999999 - resends by PMTU discovery
11          TCP Timers               999999999 total packets received
12999999999 potential rtt updates    999999999 - in sequence
13999999999 - successful             999999999 - completely duplicate
14999999999 delayed acks sent        999999999 - with some duplicate data
15999999999 retransmit timeouts      999999999 - out-of-order
16999999999 persist timeouts         999999999 - duplicate acks
17999999999 keepalive probes         999999999 - acks
18999999999 - timeouts               999999999 - window probes
19                                   999999999 - window updates
20                                   999999999 - bad checksum
--0123456789012345678901234567890123456789012345678901234567890123456789012345
--0         1         2         3         4         5         6         7
*/

WINDOW *
opentcp(void)
{

	return (stdscr);
}

void
closetcp(w)
	WINDOW *w;
{
	if (w == NULL)
		return;
	wclear(w);
	wrefresh(w);
}

void
labeltcp(void)
{
	wmove(wnd, 0, 0); wclrtoeol(wnd);
#define L(row, str) mvwprintw(wnd, row, 10, str)
#define R(row, str) mvwprintw(wnd, row, 45, str);
	L(1, "TCP Connections");		R(1, "TCP Packets");
	L(2, "connections initiated");		R(2, "total packets sent");
	L(3, "connections accepted");		R(3, "- data");
	L(4, "connections established");	R(4, "- data (retransmit)");
	L(5, "connections dropped");		R(5, "- ack-only");
	L(6, "- in embryonic state");		R(6, "- window probes");
	L(7, "- on retransmit timeout");	R(7, "- window updates");
	L(8, "- by keepalive");			R(8, "- urgent data only");
	L(9, "- from listen queue");		R(9, "- control");
	R(10, "- resends by PMTU discovery");
	L(11, "TCP Timers");		R(11, "total packets received");
	L(12, "potential rtt updates");		R(12, "- in sequence");
	L(13, "- successful");		R(13, "- completely duplicate");
	L(14, "delayed acks sent");	R(14, "- with some duplicate data");
	L(15, "retransmit timeouts");	R(15, "- out-of-order");
	L(16, "persist timeouts");	R(16, "- duplicate acks");
	L(17, "keepalive probes");	R(17, "- acks");
	L(18, "- timeouts");		R(18, "- window probes");
	R(19, "- window updates");
	R(20, "- bad checksum");
#undef L
#undef R
}

static void
domode(struct tcpstat *ret)
{
	const struct tcpstat *sub;
	int divisor = 1;

	switch(currentmode) {
	case display_RATE:
		sub = &oldstat;
		divisor = naptime;
		break;
	case display_DELTA:
		sub = &oldstat;
		break;
	case display_SINCE:
		sub = &initstat;
		break;
	default:
		*ret = curstat;
		return;
	}
#define DO(stat) ret->stat = (curstat.stat - sub->stat) / divisor
	DO(tcps_connattempt);
	DO(tcps_accepts);
	DO(tcps_connects);
	DO(tcps_drops);
	DO(tcps_conndrops);
	DO(tcps_closed);
	DO(tcps_segstimed);
	DO(tcps_rttupdated);
	DO(tcps_delack);
	DO(tcps_timeoutdrop);
	DO(tcps_rexmttimeo);
	DO(tcps_persisttimeo);
	DO(tcps_keeptimeo);
	DO(tcps_keepprobe);
	DO(tcps_keepdrops);

	DO(tcps_sndtotal);
	DO(tcps_sndpack);
	DO(tcps_sndbyte);
	DO(tcps_sndrexmitpack);
	DO(tcps_sndrexmitbyte);
	DO(tcps_sndacks);
	DO(tcps_sndprobe);
	DO(tcps_sndurg);
	DO(tcps_sndwinup);
	DO(tcps_sndctrl);

	DO(tcps_rcvtotal);
	DO(tcps_rcvpack);
	DO(tcps_rcvbyte);
	DO(tcps_rcvbadsum);
	DO(tcps_rcvbadoff);
	DO(tcps_rcvshort);
	DO(tcps_rcvduppack);
	DO(tcps_rcvdupbyte);
	DO(tcps_rcvpartduppack);
	DO(tcps_rcvpartdupbyte);
	DO(tcps_rcvoopack);
	DO(tcps_rcvoobyte);
	DO(tcps_rcvpackafterwin);
	DO(tcps_rcvbyteafterwin);
	DO(tcps_rcvafterclose);
	DO(tcps_rcvwinprobe);
	DO(tcps_rcvdupack);
	DO(tcps_rcvacktoomuch);
	DO(tcps_rcvackpack);
	DO(tcps_rcvackbyte);
	DO(tcps_rcvwinupd);
	DO(tcps_pawsdrop);
	DO(tcps_predack);
	DO(tcps_preddat);
	DO(tcps_pcbcachemiss);
	DO(tcps_cachedrtt);
	DO(tcps_cachedrttvar);
	DO(tcps_cachedssthresh);
	DO(tcps_usedrtt);
	DO(tcps_usedrttvar);
	DO(tcps_usedssthresh);
	DO(tcps_persistdrop);
	DO(tcps_badsyn);
	DO(tcps_mturesent);
	DO(tcps_listendrop);
#undef DO
}
	
void
showtcp(void)
{
	struct tcpstat stats;

	memset(&stats, 0, sizeof stats);
	domode(&stats);

#define DO(stat, row, col) \
	mvwprintw(wnd, row, col, "%9lu", stats.stat)
#define	L(row, stat) DO(stat, row, 0)
#define	R(row, stat) DO(stat, row, 35)
	L(2, tcps_connattempt);		R(2, tcps_sndtotal);
	L(3, tcps_accepts);		R(3, tcps_sndpack);
	L(4, tcps_connects);		R(4, tcps_sndrexmitpack);
	L(5, tcps_drops);		R(5, tcps_sndacks);
	L(6, tcps_conndrops);		R(6, tcps_sndprobe);
	L(7, tcps_timeoutdrop);		R(7, tcps_sndwinup);
	L(8, tcps_keepdrops);		R(8, tcps_sndurg);
	L(9, tcps_listendrop);		R(9, tcps_sndctrl);
	R(10, tcps_mturesent);
	R(11, tcps_rcvtotal);
	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
	R(19, tcps_rcvwinupd);
	R(20, tcps_rcvbadsum);
#undef DO
#undef L
#undef R
}

int
inittcp(void)
{
	size_t len;
	int name[4];

	name[0] = CTL_NET;
	name[1] = PF_INET;
	name[2] = IPPROTO_TCP;
	name[3] = TCPCTL_STATS;

	len = 0;
	if (sysctl(name, 4, 0, &len, 0, 0) < 0) {
		error("sysctl getting tcpstat size failed");
		return 0;
	}
	if (len > sizeof curstat) {
		error("tcpstat structure has grown--recompile systat!");
		return 0;
	}
	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
		error("sysctl getting tcpstat failed");
		return 0;
	}
	oldstat = initstat;
	return 1;
}

void
resettcp(void)
{
	size_t len;
	int name[4];

	name[0] = CTL_NET;
	name[1] = PF_INET;
	name[2] = IPPROTO_TCP;
	name[3] = TCPCTL_STATS;

	len = sizeof initstat;
	if (sysctl(name, 4, &initstat, &len, 0, 0) < 0) {
		error("sysctl getting tcpstat failed");
	}
	oldstat = initstat;
}

void
fetchtcp(void)
{
	int name[4];
	size_t len;

	oldstat = curstat;
	name[0] = CTL_NET;
	name[1] = PF_INET;
	name[2] = IPPROTO_TCP;
	name[3] = TCPCTL_STATS;
	len = sizeof curstat;

	if (sysctl(name, 4, &curstat, &len, 0, 0) < 0)
		return;
}

OpenPOWER on IntegriCloud