1 /* $NetBSD: vtw.c,v 1.13 2022/09/01 10:10:20 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Coyote Point Systems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /* 32 * Copyright (c) 1983, 1988, 1993 33 * The Regents of the University of California. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 */ 59 60 #include <sys/cdefs.h> 61 #ifndef lint 62 #if 0 63 static char sccsid[] = "from: @(#)inet.c 8.4 (Berkeley) 4/20/94"; 64 #else 65 __RCSID("$NetBSD: vtw.c,v 1.13 2022/09/01 10:10:20 msaitoh Exp $"); 66 #endif 67 #endif /* not lint */ 68 69 #define _CALLOUT_PRIVATE /* for defs in sys/callout.h */ 70 71 #include <sys/param.h> 72 #include <sys/queue.h> 73 #include <sys/socket.h> 74 #include <sys/socketvar.h> 75 #include <sys/mbuf.h> 76 #include <sys/protosw.h> 77 #include <sys/sysctl.h> 78 79 #include <net/if_arp.h> 80 #include <net/route.h> 81 #include <netinet/in.h> 82 #include <netinet/in_systm.h> 83 #include <netinet/ip.h> 84 #include <netinet/in_pcb.h> 85 #include <netinet/ip_icmp.h> 86 87 #ifdef INET6 88 #include <netinet/ip6.h> 89 #endif 90 91 #include <netinet/icmp_var.h> 92 #include <netinet/igmp_var.h> 93 #include <netinet/ip_var.h> 94 #include <netinet/pim_var.h> 95 #include <netinet/tcp.h> 96 #include <netinet/tcp_seq.h> 97 #include <netinet/tcp_fsm.h> 98 #include <netinet/tcp_timer.h> 99 #include <netinet/tcp_var.h> 100 #include <netinet/tcp_debug.h> 101 #include <netinet/udp.h> 102 #include <netinet/ip_carp.h> 103 #include <netinet/udp_var.h> 104 #include <netinet/tcp_vtw.h> 105 106 #include <arpa/inet.h> 107 #include <kvm.h> 108 #include <netdb.h> 109 #include <stdio.h> 110 #include <string.h> 111 #include <unistd.h> 112 #include <stdlib.h> 113 #include <err.h> 114 #include "netstat.h" 115 #include "vtw.h" 116 #include "prog_ops.h" 117 118 static bool vtw_enabled(void); 119 static void snarf(const void *, void *, size_t); 120 static void *lookup(const char *); 121 static void process_vtw(const vtw_ctl_t *, void (*)(const vtw_t *)); 122 123 static bool 124 vtw_enabled(void) 125 { 126 127 if (use_sysctl) { 128 int enabled; 129 size_t size = sizeof(enabled); 130 131 if (prog_sysctlbyname("net.inet.tcp.vtw.enable", 132 &enabled, &size, NULL, 0) == -1) 133 return true; 134 return enabled ? true : false; 135 } else 136 return true; 137 } 138 139 static void 140 snarf(const void *addr, void *buf, size_t len) 141 { 142 size_t cc; 143 144 memset(buf, 0, len); 145 146 cc = kvm_read(get_kvmd(), (unsigned long) addr, buf, len); 147 148 if (cc != len) { 149 warnx("%s: short read at %p, len %zx cc %zx", __func__, addr, 150 len, cc); 151 } 152 } 153 154 static void * 155 lookup(const char *name) 156 { 157 kvm_t *k; 158 struct nlist nl[2]; 159 160 nl[0].n_name = name; 161 nl[0].n_value = 0; 162 nl[1].n_name = NULL; 163 164 if ((k = get_kvmd()) == NULL) { 165 if (Vflag) 166 errx(EXIT_FAILURE, "kvm not available"); 167 return NULL; 168 } 169 switch (kvm_nlist(k, &nl[0])) { 170 case -1: 171 err(EXIT_FAILURE, "kvm_nlist"); 172 break; 173 174 case 0: 175 return (void *)nl[0].n_value; 176 177 default: 178 if (Vflag) 179 errx(EXIT_FAILURE, "%s missing in symbol table", name); 180 break; 181 } 182 183 return NULL; 184 } 185 186 void 187 timebase(struct timeval *tv) 188 { 189 void *p; 190 struct bintime timebasebin; 191 192 if (!vtw_enabled()) { 193 memset(tv, 0, sizeof(*tv)); 194 return; 195 } 196 197 p = lookup("timebasebin"); 198 if (!p) 199 return; 200 snarf(p, &timebasebin, sizeof(timebasebin)); 201 bintime2timeval(&timebasebin, tv); 202 } 203 204 static void 205 process_vtw(const vtw_ctl_t * ctl, void (*print)(const vtw_t *)) 206 { 207 vtw_t *vp; 208 209 for (vp = ctl->base.v; vp && vp <= ctl->lim.v;) { 210 211 (*print)(vp); 212 213 if (ctl->is_v4) { 214 vtw_v4_t *v4 = (vtw_v4_t *)vp; 215 216 vp = &(++v4)->common; 217 } else if (ctl->is_v6) { 218 vtw_v6_t *v6 = (vtw_v6_t *)vp; 219 220 vp = &(++v6)->common; 221 } 222 } 223 } 224 225 void 226 show_vtw_stats(void) 227 { 228 vtw_stats_t stats; 229 void *p; 230 231 if (!Vflag) 232 return; 233 234 if (!vtw_enabled()) 235 return; 236 237 if ((p = lookup("vtw_stats")) == NULL) 238 return; 239 snarf(p, &stats, sizeof(stats)); 240 241 printf("\t\t%" PRIu64 " inserts\n", stats.ins); 242 printf("\t\t%" PRIu64 " deletes\n", stats.del); 243 printf("\t\t%" PRIu64 " assassinations\n", stats.kill); 244 printf("\tvestigial time-wait lookup_connect\n"); 245 printf("\t\t%" PRIu64 " look\n", stats.look[0]); 246 printf("\t\t%" PRIu64 " hit\n", stats.hit[0]); 247 printf("\t\t%" PRIu64 " miss\n", stats.miss[0]); 248 printf("\t\t%" PRIu64 " probe\n", stats.probe[0]); 249 printf("\t\t%" PRIu64 " losing\n", stats.losing[0]); 250 printf("\t\t%" PRIu64 " max_chain\n", stats.max_chain[0]); 251 printf("\t\t%" PRIu64 " max_probe\n", stats.max_probe[0]); 252 printf("\t\t%" PRIu64 " max_loss\n", stats.max_loss[0]); 253 printf("\tvestigial time-wait lookup_port\n"); 254 printf("\t\t%" PRIu64 " look\n", stats.look[1]); 255 printf("\t\t%" PRIu64 " hit\n", stats.hit[1]); 256 printf("\t\t%" PRIu64 " miss\n", stats.miss[1]); 257 printf("\t\t%" PRIu64 " probe\n", stats.probe[1]); 258 printf("\t\t%" PRIu64 " losing\n", stats.losing[1]); 259 printf("\t\t%" PRIu64 " max_chain\n", stats.max_chain[1]); 260 printf("\t\t%" PRIu64 " max_probe\n", stats.max_probe[1]); 261 printf("\t\t%" PRIu64 " max_loss\n", stats.max_loss[1]); 262 } 263 264 void 265 show_vtw_v4(void (*print)(const vtw_t *)) 266 { 267 fatp_t *base, *lim; 268 fatp_t **hash, **port; 269 size_t n; 270 fatp_ctl_t fat_tcpv4; 271 vtw_ctl_t vtw_tcpv4[VTW_NCLASS]; 272 int i; 273 int mem = 0; 274 void *p; 275 276 if (!vtw_enabled()) 277 return; 278 279 if ((p = lookup("fat_tcpv4")) == NULL) 280 return; 281 snarf(p, &fat_tcpv4, sizeof(fat_tcpv4)); 282 283 if ((p = lookup("vtw_tcpv4")) == NULL) 284 return; 285 snarf(p, &vtw_tcpv4[0], sizeof(vtw_tcpv4)); 286 287 mem += sizeof(fat_tcpv4); 288 mem += sizeof(vtw_tcpv4); 289 290 /* snarf/adjust vtw_ctl */ 291 for (i = 0; i < VTW_NCLASS; ++i) { 292 vtw_v4_t *kbase, *klim; 293 vtw_v4_t *ubase; 294 ptrdiff_t delta; 295 296 kbase = vtw_tcpv4[i].base.v4; 297 klim = vtw_tcpv4[i].lim.v4; 298 299 if (!kbase || !klim) 300 continue; 301 302 n = (klim - kbase + 1); 303 304 if (!i) { 305 ubase = NULL; 306 if (reallocarr(&ubase, n, sizeof(*kbase)) != 0) 307 err(EXIT_FAILURE, "reallocarr"); 308 snarf(kbase, ubase, n * sizeof(*ubase)); 309 310 mem += n * sizeof(*ubase); 311 } else 312 ubase = vtw_tcpv4[0].base.v4; 313 314 delta = ubase - kbase; 315 316 vtw_tcpv4[i].base.v4 += delta; 317 vtw_tcpv4[i].lim.v4 += delta; 318 vtw_tcpv4[i].alloc.v4 += delta; 319 vtw_tcpv4[i].fat = &fat_tcpv4; 320 321 if (vtw_tcpv4[i].oldest.v4) 322 vtw_tcpv4[i].oldest.v4 += delta; 323 } 324 325 /* snarf/adjust fat_ctl */ 326 327 base = fat_tcpv4.base; 328 lim = fat_tcpv4.lim; 329 330 if (!base || !lim) 331 goto end; 332 333 mem += (lim - base + 1) * sizeof(*base); 334 335 fat_tcpv4.base = NULL; 336 if (reallocarr(&fat_tcpv4.base, lim - base + 1, sizeof(*base)) != 0) 337 err(EXIT_FAILURE, "reallocarr"); 338 fat_tcpv4.lim = fat_tcpv4.base + (lim - base); 339 340 snarf(base, fat_tcpv4.base, sizeof(*base) * (lim - base + 1)); 341 342 fat_tcpv4.vtw = &vtw_tcpv4[0]; 343 fat_tcpv4.free = fat_tcpv4.base + (fat_tcpv4.free - base); 344 345 n = fat_tcpv4.mask + 1; 346 hash = fat_tcpv4.hash; 347 port = fat_tcpv4.port; 348 349 fat_tcpv4.hash = NULL; 350 if (reallocarr(&fat_tcpv4.hash, n, sizeof(*hash)) != 0) 351 err(EXIT_FAILURE, "reallocarr"); 352 353 fat_tcpv4.port = NULL; 354 if (reallocarr(&fat_tcpv4.port, n, sizeof(*port)) != 0) 355 err(EXIT_FAILURE, "reallocarr"); 356 357 snarf(hash, fat_tcpv4.hash, n * sizeof(*hash)); 358 snarf(port, fat_tcpv4.port, n * sizeof(*port)); 359 360 end: 361 process_vtw(&vtw_tcpv4[0], print); 362 363 #if 0 364 if (Vflag && vflag) { 365 printf("total memory for VTW in current config: " 366 "%d bytes %f MB\n", 367 mem, mem / (1024.0 * 1024)); 368 } 369 #endif 370 } 371 372 void 373 show_vtw_v6(void (*print)(const vtw_t *)) 374 { 375 fatp_t *base, *lim; 376 fatp_t **hash, **port; 377 size_t n; 378 fatp_ctl_t fat_tcpv6; 379 vtw_ctl_t vtw_tcpv6[VTW_NCLASS]; 380 int i; 381 int mem = 0; 382 void *p; 383 384 if (!vtw_enabled()) 385 return; 386 387 if ((p = lookup("fat_tcpv6")) == NULL) 388 return; 389 snarf(p, &fat_tcpv6, sizeof(fat_tcpv6)); 390 if ((p = lookup("vtw_tcpv6")) == NULL) 391 return; 392 snarf(p, &vtw_tcpv6[0], sizeof(vtw_tcpv6)); 393 394 mem += sizeof(fat_tcpv6); 395 mem += sizeof(vtw_tcpv6); 396 397 for (i = 0; i < VTW_NCLASS; ++i) { 398 vtw_v6_t *kbase, *klim; 399 vtw_v6_t *ubase; 400 ptrdiff_t delta; 401 402 kbase = vtw_tcpv6[i].base.v6; 403 klim = vtw_tcpv6[i].lim.v6; 404 405 if (!kbase || !klim) 406 continue; 407 408 n = (klim - kbase + 1); 409 410 if (!i) { 411 ubase = NULL; 412 if (reallocarr(&ubase, n, sizeof(*kbase)) != 0) 413 err(EXIT_FAILURE, "reallocarr"); 414 415 snarf(kbase, ubase, n * sizeof(*ubase)); 416 417 mem += n * sizeof(*ubase); 418 } else 419 ubase = vtw_tcpv6[0].base.v6; 420 421 delta = ubase - kbase; 422 423 vtw_tcpv6[i].base.v6 += delta; 424 vtw_tcpv6[i].lim.v6 += delta; 425 vtw_tcpv6[i].alloc.v6 += delta; 426 vtw_tcpv6[i].fat = &fat_tcpv6; 427 428 if (vtw_tcpv6[i].oldest.v6) 429 vtw_tcpv6[i].oldest.v6 += delta; 430 } 431 432 base = fat_tcpv6.base; 433 lim = fat_tcpv6.lim; 434 435 if (!base || !lim) 436 goto end; 437 438 mem += (lim - base + 1) * sizeof(*base); 439 440 fat_tcpv6.base = NULL; 441 if (reallocarr(&fat_tcpv6.base, lim - base + 1, sizeof(*base)) != 0) 442 err(EXIT_FAILURE, "reallocarr"); 443 444 fat_tcpv6.lim = fat_tcpv6.base + (lim - base); 445 446 snarf(base, fat_tcpv6.base, sizeof(*base) * (lim - base + 1)); 447 448 fat_tcpv6.vtw = &vtw_tcpv6[0]; 449 fat_tcpv6.free = fat_tcpv6.base + (fat_tcpv6.free - base); 450 451 n = fat_tcpv6.mask + 1; 452 hash = fat_tcpv6.hash; 453 port = fat_tcpv6.port; 454 455 fat_tcpv6.hash = NULL; 456 if (reallocarr(&fat_tcpv6.hash, n, sizeof(*hash)) != 0) 457 err(EXIT_FAILURE, "reallocarr"); 458 459 fat_tcpv6.port = NULL; 460 if (reallocarr(&fat_tcpv6.port, n, sizeof(*port)) != 0) 461 err(EXIT_FAILURE, "reallocarr"); 462 463 snarf(hash, fat_tcpv6.hash, n * sizeof(*hash)); 464 snarf(port, fat_tcpv6.port, n * sizeof(*port)); 465 466 end: 467 468 process_vtw(&vtw_tcpv6[0], print); 469 #if 0 470 if (Vflag && vflag) { 471 printf("total memory for VTW in current config: " 472 "%d bytes %f MB\n", 473 mem, mem / (1024.0 * 1024)); 474 } 475 #endif 476 } 477