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