xref: /dragonfly/usr.bin/systat/tcp.c (revision 0ca59c34)
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  * 3. 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  * @(#)mbufs.c	8.1 (Berkeley) 6/6/93
30  * $FreeBSD: src/usr.bin/systat/tcp.c,v 1.3 1999/08/28 01:06:06 peter Exp $
31  * $DragonFly: src/usr.bin/systat/tcp.c,v 1.5 2004/04/07 21:40:19 dillon Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/tcp.h>
43 #include <netinet/tcp_seq.h>
44 #include <netinet/tcp_fsm.h>
45 #include <netinet/tcp_timer.h>
46 #include <netinet/tcp_var.h>
47 
48 #include <stdlib.h>
49 #include <string.h>
50 #include <paths.h>
51 #include "systat.h"
52 #include "extern.h"
53 #include "mode.h"
54 
55 static struct tcp_stats curstat, initstat, oldstat;
56 
57 /*-
58 --0         1         2         3         4         5         6         7
59 --0123456789012345678901234567890123456789012345678901234567890123456789012345
60 01          TCP Connections                    TCP Packets
61 02999999999 connections initiated    999999999 total packets sent
62 03999999999 connections accepted     999999999 - data
63 04999999999 connections established  999999999 - data (retransmit)
64 05999999999 connections dropped      999999999 - ack-only
65 06999999999 - in embryonic state     999999999 - window probes
66 07999999999 - on retransmit timeout  999999999 - window updates
67 08999999999 - by keepalive           999999999 - urgent data only
68 09999999999 - from listen queue      999999999 - control
69 10                                   999999999 - resends by PMTU discovery
70 11          TCP Timers               999999999 total packets received
71 12999999999 potential rtt updates    999999999 - in sequence
72 13999999999 - successful             999999999 - completely duplicate
73 14999999999 delayed acks sent        999999999 - with some duplicate data
74 15999999999 retransmit timeouts      999999999 - out-of-order
75 16999999999 persist timeouts         999999999 - duplicate acks
76 17999999999 keepalive probes         999999999 - acks
77 18999999999 - timeouts               999999999 - window probes
78 19                                   999999999 - window updates
79 --0123456789012345678901234567890123456789012345678901234567890123456789012345
80 --0         1         2         3         4         5         6         7
81 */
82 
83 WINDOW *
84 opentcp(void)
85 {
86 	return (subwin(stdscr, LINES-5-1, 0, 5, 0));
87 }
88 
89 void
90 closetcp(WINDOW *w)
91 {
92 	if (w == NULL)
93 		return;
94 	wclear(w);
95 	wrefresh(w);
96 	delwin(w);
97 }
98 
99 void
100 labeltcp(void)
101 {
102 	wmove(wnd, 0, 0); wclrtoeol(wnd);
103 #define L(row, str) mvwprintw(wnd, row, 10, str)
104 #define R(row, str) mvwprintw(wnd, row, 45, str);
105 	L(1, "TCP Connections");		R(1, "TCP Packets");
106 	L(2, "connections initiated");		R(2, "total packets sent");
107 	L(3, "connections accepted");		R(3, "- data");
108 	L(4, "connections established");	R(4, "- data (retransmit)");
109 	L(5, "connections dropped");		R(5, "- ack-only");
110 	L(6, "- in embryonic state");		R(6, "- window probes");
111 	L(7, "- on retransmit timeout");	R(7, "- window updates");
112 	L(8, "- by keepalive");			R(8, "- urgent data only");
113 	L(9, "- from listen queue");		R(9, "- control");
114 	R(10, "- resends by PMTU discovery");
115 	L(11, "TCP Timers");		R(11, "total packets received");
116 	L(12, "potential rtt updates");		R(12, "- in sequence");
117 	L(13, "- successful");		R(13, "- completely duplicate");
118 	L(14, "delayed acks sent");	R(14, "- with some duplicate data");
119 	L(15, "retransmit timeouts");	R(15, "- out-of-order");
120 	L(16, "persist timeouts");	R(16, "- duplicate acks");
121 	L(17, "keepalive probes");	R(17, "- acks");
122 	L(18, "- timeouts");		R(18, "- window probes");
123 	R(19, "- window updates");
124 #undef L
125 #undef R
126 }
127 
128 static void
129 domode(struct tcp_stats *ret)
130 {
131 	const struct tcp_stats *sub;
132 	double divisor = 1.0;
133 
134 	switch(currentmode) {
135 	case display_RATE:
136 		sub = &oldstat;
137 		divisor = naptime;
138 		break;
139 	case display_DELTA:
140 		sub = &oldstat;
141 		break;
142 	case display_SINCE:
143 		sub = &initstat;
144 		break;
145 	default:
146 		*ret = curstat;
147 		return;
148 	}
149 #define DO(stat) ret->stat = (double)(curstat.stat - sub->stat) / divisor
150 	DO(tcps_connattempt);
151 	DO(tcps_accepts);
152 	DO(tcps_connects);
153 	DO(tcps_drops);
154 	DO(tcps_conndrops);
155 	DO(tcps_closed);
156 	DO(tcps_segstimed);
157 	DO(tcps_rttupdated);
158 	DO(tcps_delack);
159 	DO(tcps_timeoutdrop);
160 	DO(tcps_rexmttimeo);
161 	DO(tcps_persisttimeo);
162 	DO(tcps_keeptimeo);
163 	DO(tcps_keepprobe);
164 	DO(tcps_keepdrops);
165 
166 	DO(tcps_sndtotal);
167 	DO(tcps_sndpack);
168 	DO(tcps_sndbyte);
169 	DO(tcps_sndrexmitpack);
170 	DO(tcps_sndrexmitbyte);
171 	DO(tcps_sndacks);
172 	DO(tcps_sndprobe);
173 	DO(tcps_sndurg);
174 	DO(tcps_sndwinup);
175 	DO(tcps_sndctrl);
176 
177 	DO(tcps_rcvtotal);
178 	DO(tcps_rcvpack);
179 	DO(tcps_rcvbyte);
180 	DO(tcps_rcvbadsum);
181 	DO(tcps_rcvbadoff);
182 	DO(tcps_rcvshort);
183 	DO(tcps_rcvduppack);
184 	DO(tcps_rcvdupbyte);
185 	DO(tcps_rcvpartduppack);
186 	DO(tcps_rcvpartdupbyte);
187 	DO(tcps_rcvoopack);
188 	DO(tcps_rcvoobyte);
189 	DO(tcps_rcvpackafterwin);
190 	DO(tcps_rcvbyteafterwin);
191 	DO(tcps_rcvafterclose);
192 	DO(tcps_rcvwinprobe);
193 	DO(tcps_rcvdupack);
194 	DO(tcps_rcvacktoomuch);
195 	DO(tcps_rcvackpack);
196 	DO(tcps_rcvackbyte);
197 	DO(tcps_rcvwinupd);
198 	DO(tcps_pawsdrop);
199 	DO(tcps_predack);
200 	DO(tcps_preddat);
201 	DO(tcps_pcbcachemiss);
202 	DO(tcps_cachedrtt);
203 	DO(tcps_cachedrttvar);
204 	DO(tcps_cachedssthresh);
205 	DO(tcps_usedrtt);
206 	DO(tcps_usedrttvar);
207 	DO(tcps_usedssthresh);
208 	DO(tcps_persistdrop);
209 	DO(tcps_badsyn);
210 	DO(tcps_mturesent);
211 	DO(tcps_listendrop);
212 #undef DO
213 }
214 
215 void
216 showtcp(void)
217 {
218 	struct tcp_stats stats;
219 
220 	memset(&stats, 0, sizeof stats);
221 	domode(&stats);
222 
223 #define DO(stat, row, col) \
224 	mvwprintw(wnd, row, col, "%9lu", stats.stat)
225 #define	L(row, stat) DO(stat, row, 0)
226 #define	R(row, stat) DO(stat, row, 35)
227 	L(2, tcps_connattempt);		R(2, tcps_sndtotal);
228 	L(3, tcps_accepts);		R(3, tcps_sndpack);
229 	L(4, tcps_connects);		R(4, tcps_sndrexmitpack);
230 	L(5, tcps_drops);		R(5, tcps_sndacks);
231 	L(6, tcps_conndrops);		R(6, tcps_sndprobe);
232 	L(7, tcps_timeoutdrop);		R(7, tcps_sndwinup);
233 	L(8, tcps_keepdrops);		R(8, tcps_sndurg);
234 	L(9, tcps_listendrop);		R(9, tcps_sndctrl);
235 	R(10, tcps_mturesent);
236 	R(11, tcps_rcvtotal);
237 	L(12, tcps_segstimed);		R(12, tcps_rcvpack);
238 	L(13, tcps_rttupdated);		R(13, tcps_rcvduppack);
239 	L(14, tcps_delack);		R(14, tcps_rcvpartduppack);
240 	L(15, tcps_rexmttimeo);		R(15, tcps_rcvoopack);
241 	L(16, tcps_persisttimeo);	R(16, tcps_rcvdupack);
242 	L(17, tcps_keepprobe);		R(17, tcps_rcvackpack);
243 	L(18, tcps_keeptimeo);		R(18, tcps_rcvwinprobe);
244 	R(19, tcps_rcvwinupd);
245 #undef DO
246 #undef L
247 #undef R
248 }
249 
250 #define CPU_STATS_FUNC(proto,type)                            \
251 static void                                                   \
252 proto ##_stats_agg(type *ary, type *ttl, int cpucnt)          \
253 {                                                             \
254     int i, off, siz;                                          \
255     siz = sizeof(type);                                       \
256                                                               \
257     if (!ary && !ttl)                                         \
258         return;                                               \
259                                                               \
260     bzero(ttl, siz);                                          \
261     if (cpucnt == 1) {                                        \
262         *ttl = ary[0];                                        \
263     } else {                                                  \
264         for (i = 0; i < cpucnt; ++i) {                        \
265             for (off = 0; off < siz; off += sizeof(u_long)) { \
266                 *(u_long *)((char *)(*(&ttl)) + off) +=       \
267                 *(u_long *)((char *)&ary[i] + off);           \
268             }                                                 \
269         }                                                     \
270     }                                                         \
271 }
272 CPU_STATS_FUNC(tcp, struct tcp_stats);
273 
274 static int
275 fetch_tcpstats(struct tcp_stats *st)
276 {
277 	struct tcp_stats stattmp[SMP_MAXCPU];
278 	size_t len;
279 	int name[4], cpucnt;
280 
281 	name[0] = CTL_NET;
282 	name[1] = PF_INET;
283 	name[2] = IPPROTO_TCP;
284 	name[3] = TCPCTL_STATS;
285 
286 	len = sizeof(struct tcp_stats) * SMP_MAXCPU;
287 	if (sysctl(name, 4, stattmp, &len, NULL, 0) < 0) {
288 		error("sysctl getting tcp_stats failed");
289 		return -1;
290 	}
291 	cpucnt = len / sizeof(struct tcp_stats);
292 	tcp_stats_agg(stattmp, st, cpucnt);
293 
294 	return 0;
295 }
296 
297 int
298 inittcp(void)
299 {
300 	fetch_tcpstats(&initstat);
301 	oldstat = initstat;
302 	curstat = initstat;
303 	return 1;
304 }
305 
306 void
307 resettcp(void)
308 {
309 	fetch_tcpstats(&initstat);
310 	oldstat = initstat;
311 }
312 
313 void
314 fetchtcp(void)
315 {
316 	oldstat = curstat;
317 	fetch_tcpstats(&curstat);
318 }
319