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