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