1 /* $OpenBSD: tcpbench.c,v 1.65 2021/07/12 15:09:20 beck Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Damien Miller <djm@mindrot.org> 5 * Copyright (c) 2011 Christiano F. Haesbaert <haesbaert@haesbaert.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 #include <sys/time.h> 22 #include <sys/socket.h> 23 #include <sys/socketvar.h> 24 #include <sys/resource.h> 25 #include <sys/queue.h> 26 #include <sys/un.h> 27 28 #include <net/route.h> 29 30 #include <netinet/in.h> 31 #include <netinet/ip.h> 32 #include <netinet/tcp.h> 33 #include <netinet/tcp_timer.h> 34 #include <netinet/tcp_fsm.h> 35 #include <netinet/in_pcb.h> 36 #include <netinet/tcp_var.h> 37 38 #include <arpa/inet.h> 39 40 #include <unistd.h> 41 #include <limits.h> 42 #include <stdlib.h> 43 #include <stdio.h> 44 #include <string.h> 45 #include <errno.h> 46 #include <event.h> 47 #include <netdb.h> 48 #include <signal.h> 49 #include <err.h> 50 #include <fcntl.h> 51 #include <poll.h> 52 #include <paths.h> 53 #include <math.h> 54 55 #include <kvm.h> 56 #include <nlist.h> 57 58 #define DEFAULT_PORT "12345" 59 #define DEFAULT_STATS_INTERVAL 1000 /* ms */ 60 #define DEFAULT_BUF (256 * 1024) 61 #define DEFAULT_UDP_PKT (1500 - 28) /* TODO don't hardcode this */ 62 #define TCP_MODE !ptb->uflag 63 #define UDP_MODE ptb->uflag 64 #define MAX_FD 1024 65 66 /* Our tcpbench globals */ 67 struct { 68 int Dflag; /* Socket debug */ 69 int Sflag; /* Socket buffer size */ 70 u_int rflag; /* Report rate (ms) */ 71 int sflag; /* True if server */ 72 int Tflag; /* ToS if != -1 */ 73 int vflag; /* Verbose */ 74 int uflag; /* UDP mode */ 75 int Uflag; /* UNIX (AF_LOCAL) mode */ 76 int Rflag; /* randomize client write size */ 77 kvm_t *kvmh; /* Kvm handler */ 78 char **kvars; /* Kvm enabled vars */ 79 u_long ktcbtab; /* Ktcb */ 80 char *dummybuf; /* IO buffer */ 81 size_t dummybuf_len; /* IO buffer len */ 82 } tcpbench, *ptb; 83 84 struct tcpservsock { 85 struct event ev; 86 struct event evt; 87 int fd; 88 }; 89 90 /* stats for a single tcp connection, udp uses only one */ 91 struct statctx { 92 TAILQ_ENTRY(statctx) entry; 93 struct timeval t_start, t_last; 94 unsigned long long bytes; 95 int fd; 96 char *buf; 97 size_t buflen; 98 struct event ev; 99 /* TCP only */ 100 struct tcpservsock *tcp_ts; 101 u_long tcp_tcbaddr; 102 /* UDP only */ 103 u_long udp_slice_pkts; 104 }; 105 106 struct statctx *udp_sc; /* singleton */ 107 108 static void signal_handler(int, short, void *); 109 static void saddr_ntop(const struct sockaddr *, socklen_t, char *, size_t); 110 static void drop_gid(void); 111 static void set_slice_timer(int); 112 static void print_tcp_header(void); 113 static void kget(u_long, void *, size_t); 114 static u_long kfind_tcb(int); 115 static void kupdate_stats(u_long, struct inpcb *, struct tcpcb *, 116 struct socket *); 117 static void list_kvars(void); 118 static void check_kvar(const char *); 119 static char ** check_prepare_kvars(char *); 120 static void stats_prepare(struct statctx *); 121 static void summary_display(void); 122 static void tcp_stats_display(unsigned long long, long double, float, 123 struct statctx *, struct inpcb *, struct tcpcb *, struct socket *); 124 static void tcp_process_slice(int, short, void *); 125 static void tcp_server_handle_sc(int, short, void *); 126 static void tcp_server_accept(int, short, void *); 127 static void server_init(struct addrinfo *); 128 static void client_handle_sc(int, short, void *); 129 static void client_init(struct addrinfo *, int, struct addrinfo *); 130 static int clock_gettime_tv(clockid_t, struct timeval *); 131 static void udp_server_handle_sc(int, short, void *); 132 static void udp_process_slice(int, short, void *); 133 static int map_tos(char *, int *); 134 static void quit(int, short, void *); 135 static void wrapup(int); 136 137 /* 138 * We account the mainstats here, that is the stats 139 * for all connections, all variables starting with slice 140 * are used to account information for the timeslice 141 * between each output. Peak variables record the highest 142 * between all slices so far. 143 */ 144 static struct { 145 struct timeval t_first; /* first connect / packet */ 146 unsigned long long total_bytes; /* bytes since t_first */ 147 unsigned long long n_slices; /* slices since start */ 148 unsigned long long slice_bytes; /* bytes since slice reset */ 149 long double peak_mbps; /* peak mbps so far */ 150 long double floor_mbps; /* floor mbps so far */ 151 long double mean_mbps; /* mean mbps so far */ 152 long double nvariance_mbps; /* for online std dev */ 153 int nconns; /* connected clients */ 154 struct event timer; /* process timer */ 155 const char *host; /* remote server for display */ 156 } mainstats; 157 158 /* When adding variables, also add to tcp_stats_display() */ 159 static const char *allowed_kvars[] = { 160 "inpcb.inp_flags", 161 "sockb.so_rcv.sb_cc", 162 "sockb.so_rcv.sb_hiwat", 163 "sockb.so_rcv.sb_wat", 164 "sockb.so_snd.sb_cc", 165 "sockb.so_snd.sb_hiwat", 166 "sockb.so_snd.sb_wat", 167 "tcpcb.last_ack_sent", 168 "tcpcb.max_sndwnd", 169 "tcpcb.rcv_adv", 170 "tcpcb.rcv_nxt", 171 "tcpcb.rcv_scale", 172 "tcpcb.rcv_wnd", 173 "tcpcb.rfbuf_cnt", 174 "tcpcb.rfbuf_ts", 175 "tcpcb.snd_cwnd", 176 "tcpcb.snd_max", 177 "tcpcb.snd_nxt", 178 "tcpcb.snd_scale", 179 "tcpcb.snd_ssthresh", 180 "tcpcb.snd_una", 181 "tcpcb.snd_wl1", 182 "tcpcb.snd_wl2", 183 "tcpcb.snd_wnd", 184 "tcpcb.t_rcvtime", 185 "tcpcb.t_rtseq", 186 "tcpcb.t_rttmin", 187 "tcpcb.t_rtttime", 188 "tcpcb.t_rttvar", 189 "tcpcb.t_srtt", 190 "tcpcb.ts_recent", 191 "tcpcb.ts_recent_age", 192 NULL 193 }; 194 195 TAILQ_HEAD(, statctx) sc_queue; 196 197 static void __dead 198 usage(void) 199 { 200 fprintf(stderr, 201 "usage: tcpbench -l\n" 202 " tcpbench [-46DRUuv] [-B buf] [-b sourceaddr] [-k kvars] [-n connections]\n" 203 " [-p port] [-r interval] [-S space] [-T toskeyword]\n" 204 " [-t secs] [-V rtable] hostname\n" 205 " tcpbench -s [-46DUuv] [-B buf] [-k kvars] [-p port] [-r interval]\n" 206 " [-S space] [-T toskeyword] [-V rtable] [hostname]\n"); 207 exit(1); 208 } 209 210 static void 211 signal_handler(int sig, short event, void *bula) 212 { 213 /* 214 * signal handler rules don't apply, libevent decouples for us 215 */ 216 switch (sig) { 217 case SIGINFO: 218 printf("\n"); 219 wrapup(-1); 220 break; 221 case SIGINT: 222 printf("\n"); 223 wrapup(0); 224 break; /* NOTREACHED */ 225 case SIGTERM: 226 case SIGHUP: 227 warnx("Terminated by signal %d", sig); 228 wrapup(0); 229 break; /* NOTREACHED */ 230 default: 231 errx(1, "unexpected signal %d", sig); 232 break; /* NOTREACHED */ 233 } 234 } 235 236 static void 237 saddr_ntop(const struct sockaddr *addr, socklen_t alen, char *buf, size_t len) 238 { 239 char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; 240 int herr; 241 242 if (addr->sa_family == AF_UNIX) { 243 struct sockaddr_un *sun = (struct sockaddr_un *)addr; 244 snprintf(buf, len, "%s", sun->sun_path); 245 return; 246 } 247 if ((herr = getnameinfo(addr, alen, hbuf, sizeof(hbuf), 248 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV)) != 0) { 249 if (herr == EAI_SYSTEM) 250 err(1, "getnameinfo"); 251 else 252 errx(1, "getnameinfo: %s", gai_strerror(herr)); 253 } 254 snprintf(buf, len, "[%s]:%s", hbuf, pbuf); 255 } 256 257 static void 258 drop_gid(void) 259 { 260 gid_t gid; 261 262 gid = getgid(); 263 if (setresgid(gid, gid, gid) == -1) 264 err(1, "setresgid"); 265 } 266 267 static void 268 set_slice_timer(int on) 269 { 270 struct timeval tv; 271 272 if (ptb->rflag == 0) 273 return; 274 275 if (on) { 276 if (evtimer_pending(&mainstats.timer, NULL)) 277 return; 278 /* XXX Is there a better way to do this ? */ 279 tv.tv_sec = ptb->rflag / 1000; 280 tv.tv_usec = (ptb->rflag % 1000) * 1000; 281 282 evtimer_add(&mainstats.timer, &tv); 283 } else if (evtimer_pending(&mainstats.timer, NULL)) 284 evtimer_del(&mainstats.timer); 285 } 286 287 static int 288 clock_gettime_tv(clockid_t clock_id, struct timeval *tv) 289 { 290 struct timespec ts; 291 292 if (clock_gettime(clock_id, &ts) == -1) 293 return (-1); 294 295 TIMESPEC_TO_TIMEVAL(tv, &ts); 296 297 return (0); 298 } 299 300 static void 301 print_tcp_header(void) 302 { 303 char **kv; 304 305 if (ptb->rflag == 0) 306 return; 307 308 printf("%12s %14s %12s %8s ", "elapsed_ms", "bytes", "mbps", 309 "bwidth"); 310 for (kv = ptb->kvars; ptb->kvars != NULL && *kv != NULL; kv++) 311 printf("%s%s", kv != ptb->kvars ? "," : "", *kv); 312 printf("\n"); 313 } 314 315 static void 316 kget(u_long addr, void *buf, size_t size) 317 { 318 if (kvm_read(ptb->kvmh, addr, buf, size) != (ssize_t)size) 319 errx(1, "kvm_read: %s", kvm_geterr(ptb->kvmh)); 320 } 321 322 static u_long 323 kfind_tcb(int sock) 324 { 325 struct inpcbtable tcbtab; 326 struct inpcb *next, *prev; 327 struct inpcb inpcb, prevpcb; 328 struct tcpcb tcpcb; 329 330 struct sockaddr_storage me, them; 331 socklen_t melen, themlen; 332 struct sockaddr_in *in4; 333 struct sockaddr_in6 *in6; 334 char tmp1[64], tmp2[64]; 335 int nretry; 336 337 nretry = 10; 338 melen = themlen = sizeof(struct sockaddr_storage); 339 if (getsockname(sock, (struct sockaddr *)&me, &melen) == -1) 340 err(1, "getsockname"); 341 if (getpeername(sock, (struct sockaddr *)&them, &themlen) == -1) 342 err(1, "getpeername"); 343 if (me.ss_family != them.ss_family) 344 errx(1, "%s: me.ss_family != them.ss_family", __func__); 345 if (me.ss_family != AF_INET && me.ss_family != AF_INET6) 346 errx(1, "%s: unknown socket family", __func__); 347 if (ptb->vflag >= 2) { 348 saddr_ntop((struct sockaddr *)&me, me.ss_len, 349 tmp1, sizeof(tmp1)); 350 saddr_ntop((struct sockaddr *)&them, them.ss_len, 351 tmp2, sizeof(tmp2)); 352 fprintf(stderr, "Our socket local %s remote %s\n", tmp1, tmp2); 353 } 354 if (ptb->vflag >= 2) 355 fprintf(stderr, "Using PCB table at %lu\n", ptb->ktcbtab); 356 retry: 357 kget(ptb->ktcbtab, &tcbtab, sizeof(tcbtab)); 358 prev = NULL; 359 next = TAILQ_FIRST(&tcbtab.inpt_queue); 360 361 if (ptb->vflag >= 2) 362 fprintf(stderr, "PCB start at %p\n", next); 363 while (next != NULL) { 364 if (ptb->vflag >= 2) 365 fprintf(stderr, "Checking PCB %p\n", next); 366 kget((u_long)next, &inpcb, sizeof(inpcb)); 367 if (prev != NULL) { 368 kget((u_long)prev, &prevpcb, sizeof(prevpcb)); 369 if (TAILQ_NEXT(&prevpcb, inp_queue) != next) { 370 if (nretry--) { 371 warnx("PCB prev pointer insane"); 372 goto retry; 373 } else 374 errx(1, "PCB prev pointer insane," 375 " all attempts exhaused"); 376 } 377 } 378 prev = next; 379 next = TAILQ_NEXT(&inpcb, inp_queue); 380 381 if (me.ss_family == AF_INET) { 382 if ((inpcb.inp_flags & INP_IPV6) != 0) { 383 if (ptb->vflag >= 2) 384 fprintf(stderr, "Skip: INP_IPV6"); 385 continue; 386 } 387 if (ptb->vflag >= 2) { 388 inet_ntop(AF_INET, &inpcb.inp_laddr, 389 tmp1, sizeof(tmp1)); 390 inet_ntop(AF_INET, &inpcb.inp_faddr, 391 tmp2, sizeof(tmp2)); 392 fprintf(stderr, "PCB %p local: [%s]:%d " 393 "remote: [%s]:%d\n", prev, 394 tmp1, inpcb.inp_lport, 395 tmp2, inpcb.inp_fport); 396 } 397 in4 = (struct sockaddr_in *)&me; 398 if (memcmp(&in4->sin_addr, &inpcb.inp_laddr, 399 sizeof(struct in_addr)) != 0 || 400 in4->sin_port != inpcb.inp_lport) 401 continue; 402 in4 = (struct sockaddr_in *)&them; 403 if (memcmp(&in4->sin_addr, &inpcb.inp_faddr, 404 sizeof(struct in_addr)) != 0 || 405 in4->sin_port != inpcb.inp_fport) 406 continue; 407 } else { 408 if ((inpcb.inp_flags & INP_IPV6) == 0) 409 continue; 410 if (ptb->vflag >= 2) { 411 inet_ntop(AF_INET6, &inpcb.inp_laddr6, 412 tmp1, sizeof(tmp1)); 413 inet_ntop(AF_INET6, &inpcb.inp_faddr6, 414 tmp2, sizeof(tmp2)); 415 fprintf(stderr, "PCB %p local: [%s]:%d " 416 "remote: [%s]:%d\n", prev, 417 tmp1, inpcb.inp_lport, 418 tmp2, inpcb.inp_fport); 419 } 420 in6 = (struct sockaddr_in6 *)&me; 421 if (memcmp(&in6->sin6_addr, &inpcb.inp_laddr6, 422 sizeof(struct in6_addr)) != 0 || 423 in6->sin6_port != inpcb.inp_lport) 424 continue; 425 in6 = (struct sockaddr_in6 *)&them; 426 if (memcmp(&in6->sin6_addr, &inpcb.inp_faddr6, 427 sizeof(struct in6_addr)) != 0 || 428 in6->sin6_port != inpcb.inp_fport) 429 continue; 430 } 431 kget((u_long)inpcb.inp_ppcb, &tcpcb, sizeof(tcpcb)); 432 if (tcpcb.t_state != TCPS_ESTABLISHED) { 433 if (ptb->vflag >= 2) 434 fprintf(stderr, "Not established\n"); 435 continue; 436 } 437 if (ptb->vflag >= 2) 438 fprintf(stderr, "Found PCB at %p\n", prev); 439 return ((u_long)prev); 440 } 441 442 errx(1, "No matching PCB found"); 443 } 444 445 static void 446 kupdate_stats(u_long tcbaddr, struct inpcb *inpcb, 447 struct tcpcb *tcpcb, struct socket *sockb) 448 { 449 kget(tcbaddr, inpcb, sizeof(*inpcb)); 450 kget((u_long)inpcb->inp_ppcb, tcpcb, sizeof(*tcpcb)); 451 kget((u_long)inpcb->inp_socket, sockb, sizeof(*sockb)); 452 } 453 454 static void 455 check_kvar(const char *var) 456 { 457 u_int i; 458 459 for (i = 0; allowed_kvars[i] != NULL; i++) 460 if (strcmp(allowed_kvars[i], var) == 0) 461 return; 462 errx(1, "Unrecognised kvar: %s", var); 463 } 464 465 static void 466 list_kvars(void) 467 { 468 u_int i; 469 470 printf("Supported kernel variables:\n"); 471 for (i = 0; allowed_kvars[i] != NULL; i++) 472 printf("\t%s\n", allowed_kvars[i]); 473 } 474 475 static char ** 476 check_prepare_kvars(char *list) 477 { 478 char *item, **ret = NULL; 479 u_int n = 0; 480 481 while ((item = strsep(&list, ", \t\n")) != NULL) { 482 check_kvar(item); 483 if ((ret = reallocarray(ret, (++n + 1), sizeof(*ret))) == NULL) 484 err(1, "reallocarray(kvars)"); 485 if ((ret[n - 1] = strdup(item)) == NULL) 486 err(1, "strdup"); 487 ret[n] = NULL; 488 } 489 return (ret); 490 } 491 492 static void 493 stats_prepare(struct statctx *sc) 494 { 495 sc->buf = ptb->dummybuf; 496 sc->buflen = ptb->dummybuf_len; 497 498 if (ptb->kvars) 499 sc->tcp_tcbaddr = kfind_tcb(sc->fd); 500 if (clock_gettime_tv(CLOCK_MONOTONIC, &sc->t_start) == -1) 501 err(1, "clock_gettime_tv"); 502 sc->t_last = sc->t_start; 503 if (!timerisset(&mainstats.t_first)) 504 mainstats.t_first = sc->t_start; 505 } 506 507 static void 508 summary_display(void) 509 { 510 struct timeval t_cur, t_diff; 511 long double std_dev; 512 unsigned long long total_elapsed; 513 char *direction; 514 515 if (!ptb->sflag) { 516 direction = "sent"; 517 printf("--- %s tcpbench statistics ---\n", mainstats.host); 518 } else { 519 direction = "received"; 520 printf("--- tcpbench server statistics ---\n"); 521 } 522 523 std_dev = sqrtl(mainstats.nvariance_mbps / mainstats.n_slices); 524 525 if (clock_gettime_tv(CLOCK_MONOTONIC, &t_cur) == -1) 526 err(1, "clock_gettime_tv"); 527 timersub(&t_cur, &mainstats.t_first, &t_diff); 528 total_elapsed = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 529 530 printf("%llu bytes %s over %.3Lf seconds\n", 531 mainstats.total_bytes, direction, total_elapsed/1000.0L); 532 printf("bandwidth min/avg/max/std-dev = %.3Lf/%.3Lf/%.3Lf/%.3Lf Mbps\n", 533 mainstats.floor_mbps, mainstats.mean_mbps, mainstats.peak_mbps, 534 std_dev); 535 } 536 537 static void 538 tcp_stats_display(unsigned long long total_elapsed, long double mbps, 539 float bwperc, struct statctx *sc, struct inpcb *inpcb, 540 struct tcpcb *tcpcb, struct socket *sockb) 541 { 542 int j; 543 544 printf("%12llu %14llu %12.3Lf %7.2f%% ", total_elapsed, sc->bytes, 545 mbps, bwperc); 546 547 if (ptb->kvars != NULL) { 548 kupdate_stats(sc->tcp_tcbaddr, inpcb, tcpcb, 549 sockb); 550 551 for (j = 0; ptb->kvars[j] != NULL; j++) { 552 #define S(a) #a 553 #define P(b, v, f) \ 554 if (strcmp(ptb->kvars[j], S(b.v)) == 0) { \ 555 printf("%s"f, j > 0 ? "," : "", b->v); \ 556 continue; \ 557 } 558 P(inpcb, inp_flags, "0x%08x") 559 P(sockb, so_rcv.sb_cc, "%lu") 560 P(sockb, so_rcv.sb_hiwat, "%lu") 561 P(sockb, so_rcv.sb_wat, "%lu") 562 P(sockb, so_snd.sb_cc, "%lu") 563 P(sockb, so_snd.sb_hiwat, "%lu") 564 P(sockb, so_snd.sb_wat, "%lu") 565 P(tcpcb, last_ack_sent, "%u") 566 P(tcpcb, max_sndwnd, "%lu") 567 P(tcpcb, rcv_adv, "%u") 568 P(tcpcb, rcv_nxt, "%u") 569 P(tcpcb, rcv_scale, "%u") 570 P(tcpcb, rcv_wnd, "%lu") 571 P(tcpcb, rfbuf_cnt, "%u") 572 P(tcpcb, rfbuf_ts, "%u") 573 P(tcpcb, snd_cwnd, "%lu") 574 P(tcpcb, snd_max, "%u") 575 P(tcpcb, snd_nxt, "%u") 576 P(tcpcb, snd_scale, "%u") 577 P(tcpcb, snd_ssthresh, "%lu") 578 P(tcpcb, snd_una, "%u") 579 P(tcpcb, snd_wl1, "%u") 580 P(tcpcb, snd_wl2, "%u") 581 P(tcpcb, snd_wnd, "%lu") 582 P(tcpcb, t_rcvtime, "%u") 583 P(tcpcb, t_rtseq, "%u") 584 P(tcpcb, t_rttmin, "%hu") 585 P(tcpcb, t_rtttime, "%u") 586 P(tcpcb, t_rttvar, "%hu") 587 P(tcpcb, t_srtt, "%hu") 588 P(tcpcb, ts_recent, "%u") 589 P(tcpcb, ts_recent_age, "%u") 590 #undef S 591 #undef P 592 } 593 } 594 printf("\n"); 595 } 596 597 static void 598 tcp_process_slice(int fd, short event, void *bula) 599 { 600 unsigned long long total_elapsed, since_last; 601 long double mbps, old_mean_mbps, slice_mbps = 0; 602 float bwperc; 603 struct statctx *sc; 604 struct timeval t_cur, t_diff; 605 struct inpcb inpcb; 606 struct tcpcb tcpcb; 607 struct socket sockb; 608 609 if (TAILQ_EMPTY(&sc_queue)) 610 return; /* don't pollute stats */ 611 612 mainstats.n_slices++; 613 614 TAILQ_FOREACH(sc, &sc_queue, entry) { 615 if (clock_gettime_tv(CLOCK_MONOTONIC, &t_cur) == -1) 616 err(1, "clock_gettime_tv"); 617 if (ptb->kvars != NULL) /* process kernel stats */ 618 kupdate_stats(sc->tcp_tcbaddr, &inpcb, &tcpcb, 619 &sockb); 620 621 timersub(&t_cur, &sc->t_start, &t_diff); 622 total_elapsed = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 623 timersub(&t_cur, &sc->t_last, &t_diff); 624 since_last = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 625 if (since_last == 0) 626 continue; 627 bwperc = (sc->bytes * 100.0) / mainstats.slice_bytes; 628 mbps = (sc->bytes * 8) / (since_last * 1000.0); 629 slice_mbps += mbps; 630 631 tcp_stats_display(total_elapsed, mbps, bwperc, sc, 632 &inpcb, &tcpcb, &sockb); 633 634 sc->t_last = t_cur; 635 sc->bytes = 0; 636 } 637 638 /* process stats for this slice */ 639 if (slice_mbps > mainstats.peak_mbps) 640 mainstats.peak_mbps = slice_mbps; 641 if (slice_mbps < mainstats.floor_mbps) 642 mainstats.floor_mbps = slice_mbps; 643 old_mean_mbps = mainstats.mean_mbps; 644 mainstats.mean_mbps += (slice_mbps - mainstats.mean_mbps) / 645 mainstats.n_slices; 646 647 /* "Welford's method" for online variance 648 * see Knuth, TAoCP Volume 2, 3rd edn., p232 */ 649 mainstats.nvariance_mbps += (slice_mbps - old_mean_mbps) * 650 (slice_mbps - mainstats.mean_mbps); 651 652 printf("Conn: %3d Mbps: %12.3Lf Peak Mbps: %12.3Lf Avg Mbps: %12.3Lf\n", 653 mainstats.nconns, slice_mbps, mainstats.peak_mbps, 654 mainstats.nconns ? slice_mbps / mainstats.nconns : 0); 655 656 mainstats.slice_bytes = 0; 657 set_slice_timer(mainstats.nconns > 0); 658 } 659 660 static void 661 udp_process_slice(int fd, short event, void *bula) 662 { 663 unsigned long long total_elapsed, since_last, pps; 664 long double old_mean_mbps, slice_mbps; 665 struct timeval t_cur, t_diff; 666 667 mainstats.n_slices++; 668 669 if (clock_gettime_tv(CLOCK_MONOTONIC, &t_cur) == -1) 670 err(1, "clock_gettime_tv"); 671 672 timersub(&t_cur, &udp_sc->t_start, &t_diff); 673 total_elapsed = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 674 675 timersub(&t_cur, &udp_sc->t_last, &t_diff); 676 since_last = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000; 677 if (since_last == 0) 678 return; 679 680 slice_mbps = (udp_sc->bytes * 8) / (since_last * 1000.0); 681 pps = (udp_sc->udp_slice_pkts * 1000) / since_last; 682 683 if (slice_mbps > mainstats.peak_mbps) 684 mainstats.peak_mbps = slice_mbps; 685 if (slice_mbps < mainstats.floor_mbps) 686 mainstats.floor_mbps = slice_mbps; 687 old_mean_mbps = mainstats.mean_mbps; 688 mainstats.mean_mbps += (slice_mbps - mainstats.mean_mbps) / 689 mainstats.n_slices; 690 691 /* "Welford's method" for online variance 692 * see Knuth, TAoCP Volume 2, 3rd edn., p232 */ 693 mainstats.nvariance_mbps += (slice_mbps - old_mean_mbps) * 694 (slice_mbps - mainstats.mean_mbps); 695 696 printf("Elapsed: %11llu Mbps: %11.3Lf Peak Mbps: %11.3Lf %s PPS: %7llu\n", 697 total_elapsed, slice_mbps, mainstats.peak_mbps, 698 ptb->sflag ? "Rx" : "Tx", pps); 699 700 /* Clean up this slice time */ 701 udp_sc->t_last = t_cur; 702 udp_sc->bytes = 0; 703 udp_sc->udp_slice_pkts = 0; 704 705 mainstats.slice_bytes = 0; 706 set_slice_timer(1); 707 } 708 709 static void 710 udp_server_handle_sc(int fd, short event, void *bula) 711 { 712 static int first_read = 1; 713 ssize_t n; 714 715 n = read(fd, ptb->dummybuf, ptb->dummybuf_len); 716 if (n == 0) 717 return; 718 else if (n == -1) { 719 if (errno != EINTR && errno != EWOULDBLOCK) 720 warn("fd %d read error", fd); 721 return; 722 } 723 724 if (ptb->vflag >= 3) 725 fprintf(stderr, "read: %zd bytes\n", n); 726 if (first_read) { 727 first_read = 0; 728 stats_prepare(udp_sc); 729 set_slice_timer(1); 730 } 731 /* Account packet */ 732 udp_sc->udp_slice_pkts++; 733 udp_sc->bytes += n; 734 mainstats.slice_bytes += n; 735 mainstats.total_bytes += n; 736 } 737 738 static void 739 tcp_server_handle_sc(int fd, short event, void *v_sc) 740 { 741 struct statctx *sc = v_sc; 742 ssize_t n; 743 744 n = read(sc->fd, sc->buf, sc->buflen); 745 if (n == -1) { 746 if (errno != EINTR && errno != EWOULDBLOCK) 747 warn("fd %d read error", sc->fd); 748 return; 749 } else if (n == 0) { 750 if (ptb->vflag) 751 fprintf(stderr, "%8d closed by remote end\n", sc->fd); 752 753 TAILQ_REMOVE(&sc_queue, sc, entry); 754 755 event_del(&sc->ev); 756 close(sc->fd); 757 758 /* Some file descriptors are available again. */ 759 if (evtimer_pending(&sc->tcp_ts->evt, NULL)) { 760 evtimer_del(&sc->tcp_ts->evt); 761 event_add(&sc->tcp_ts->ev, NULL); 762 } 763 764 free(sc); 765 mainstats.nconns--; 766 return; 767 } 768 if (ptb->vflag >= 3) 769 fprintf(stderr, "read: %zd bytes\n", n); 770 sc->bytes += n; 771 mainstats.slice_bytes += n; 772 mainstats.total_bytes += n; 773 } 774 775 static void 776 tcp_server_accept(int fd, short event, void *arg) 777 { 778 struct tcpservsock *ts = arg; 779 int sock; 780 struct statctx *sc; 781 struct sockaddr_storage ss; 782 socklen_t sslen; 783 char tmp[128]; 784 785 sslen = sizeof(ss); 786 787 event_add(&ts->ev, NULL); 788 if (event & EV_TIMEOUT) 789 return; 790 if ((sock = accept4(fd, (struct sockaddr *)&ss, &sslen, SOCK_NONBLOCK)) 791 == -1) { 792 /* 793 * Pause accept if we are out of file descriptors, or 794 * libevent will haunt us here too. 795 */ 796 if (errno == ENFILE || errno == EMFILE) { 797 struct timeval evtpause = { 1, 0 }; 798 799 event_del(&ts->ev); 800 evtimer_add(&ts->evt, &evtpause); 801 } else if (errno != EWOULDBLOCK && errno != EINTR && 802 errno != ECONNABORTED) 803 warn("accept"); 804 return; 805 } 806 saddr_ntop((struct sockaddr *)&ss, sslen, 807 tmp, sizeof(tmp)); 808 if (ptb->Tflag != -1 && ss.ss_family == AF_INET) { 809 if (setsockopt(sock, IPPROTO_IP, IP_TOS, 810 &ptb->Tflag, sizeof(ptb->Tflag))) 811 err(1, "setsockopt IP_TOS"); 812 } 813 if (ptb->Tflag != -1 && ss.ss_family == AF_INET6) { 814 if (setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, 815 &ptb->Tflag, sizeof(ptb->Tflag))) 816 err(1, "setsockopt IPV6_TCLASS"); 817 } 818 /* Alloc client structure and register reading callback */ 819 if ((sc = calloc(1, sizeof(*sc))) == NULL) 820 err(1, "calloc"); 821 sc->tcp_ts = ts; 822 sc->fd = sock; 823 stats_prepare(sc); 824 825 event_set(&sc->ev, sc->fd, EV_READ | EV_PERSIST, 826 tcp_server_handle_sc, sc); 827 event_add(&sc->ev, NULL); 828 TAILQ_INSERT_TAIL(&sc_queue, sc, entry); 829 830 mainstats.nconns++; 831 if (mainstats.nconns == 1) 832 set_slice_timer(1); 833 if (ptb->vflag) 834 fprintf(stderr, "Accepted connection from %s, fd = %d\n", 835 tmp, sc->fd); 836 } 837 838 static void 839 server_init(struct addrinfo *aitop) 840 { 841 char tmp[128]; 842 int sock, on = 1; 843 struct addrinfo *ai; 844 struct event *ev; 845 struct tcpservsock *ts; 846 nfds_t lnfds; 847 848 lnfds = 0; 849 for (ai = aitop; ai != NULL; ai = ai->ai_next) { 850 saddr_ntop(ai->ai_addr, ai->ai_addrlen, tmp, sizeof(tmp)); 851 if (ptb->vflag) 852 fprintf(stderr, "Try to bind to %s\n", tmp); 853 if ((sock = socket(ai->ai_family, ai->ai_socktype, 854 ai->ai_protocol)) == -1) { 855 if (ai->ai_next == NULL) 856 err(1, "socket"); 857 if (ptb->vflag) 858 warn("socket"); 859 continue; 860 } 861 if (ptb->Dflag) { 862 if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, 863 &ptb->Dflag, sizeof(ptb->Dflag))) 864 err(1, "setsockopt SO_DEBUG"); 865 } 866 if (ptb->Tflag != -1 && ai->ai_family == AF_INET) { 867 if (setsockopt(sock, IPPROTO_IP, IP_TOS, 868 &ptb->Tflag, sizeof(ptb->Tflag))) 869 err(1, "setsockopt IP_TOS"); 870 } 871 if (ptb->Tflag != -1 && ai->ai_family == AF_INET6) { 872 if (setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, 873 &ptb->Tflag, sizeof(ptb->Tflag))) 874 err(1, "setsockopt IPV6_TCLASS"); 875 } 876 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 877 &on, sizeof(on)) == -1) 878 warn("reuse port"); 879 if (bind(sock, ai->ai_addr, ai->ai_addrlen) != 0) { 880 if (ai->ai_next == NULL) 881 err(1, "bind"); 882 if (ptb->vflag) 883 warn("bind"); 884 close(sock); 885 continue; 886 } 887 if (ptb->Sflag) { 888 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, 889 &ptb->Sflag, sizeof(ptb->Sflag)) == -1) 890 warn("set receive socket buffer size"); 891 } 892 if (TCP_MODE) { 893 if (listen(sock, 64) == -1) { 894 if (ai->ai_next == NULL) 895 err(1, "listen"); 896 if (ptb->vflag) 897 warn("listen"); 898 close(sock); 899 continue; 900 } 901 } 902 if (UDP_MODE) { 903 if ((ev = calloc(1, sizeof(*ev))) == NULL) 904 err(1, "calloc"); 905 event_set(ev, sock, EV_READ | EV_PERSIST, 906 udp_server_handle_sc, NULL); 907 event_add(ev, NULL); 908 } else { 909 if ((ts = calloc(1, sizeof(*ts))) == NULL) 910 err(1, "calloc"); 911 912 ts->fd = sock; 913 evtimer_set(&ts->evt, tcp_server_accept, ts); 914 event_set(&ts->ev, ts->fd, EV_READ, 915 tcp_server_accept, ts); 916 event_add(&ts->ev, NULL); 917 } 918 if (ptb->vflag >= 3) 919 fprintf(stderr, "bound to fd %d\n", sock); 920 lnfds++; 921 } 922 if (!ptb->Uflag) 923 freeaddrinfo(aitop); 924 if (lnfds == 0) 925 errx(1, "No working listen addresses found"); 926 } 927 928 static void 929 client_handle_sc(int fd, short event, void *v_sc) 930 { 931 struct statctx *sc = v_sc; 932 ssize_t n; 933 size_t blen = sc->buflen; 934 935 if (ptb->Rflag) 936 blen = arc4random_uniform(blen) + 1; 937 if ((n = write(sc->fd, sc->buf, blen)) == -1) { 938 if (errno == EINTR || errno == EWOULDBLOCK || 939 (UDP_MODE && errno == ENOBUFS)) 940 return; 941 warn("write"); 942 wrapup(1); 943 } 944 if (TCP_MODE && n == 0) { 945 fprintf(stderr, "Remote end closed connection"); 946 wrapup(1); 947 } 948 if (ptb->vflag >= 3) 949 fprintf(stderr, "write: %zd bytes\n", n); 950 sc->bytes += n; 951 mainstats.slice_bytes += n; 952 mainstats.total_bytes += n; 953 if (UDP_MODE) 954 sc->udp_slice_pkts++; 955 } 956 957 static void 958 client_init(struct addrinfo *aitop, int nconn, struct addrinfo *aib) 959 { 960 struct statctx *sc; 961 struct addrinfo *ai; 962 char tmp[128]; 963 int i, r, sock; 964 965 for (i = 0; i < nconn; i++) { 966 for (sock = -1, ai = aitop; ai != NULL; ai = ai->ai_next) { 967 saddr_ntop(ai->ai_addr, ai->ai_addrlen, tmp, 968 sizeof(tmp)); 969 if (ptb->vflag && i == 0) 970 fprintf(stderr, "Trying %s\n", tmp); 971 if ((sock = socket(ai->ai_family, ai->ai_socktype, 972 ai->ai_protocol)) == -1) { 973 if (ai->ai_next == NULL) 974 err(1, "socket"); 975 if (ptb->vflag) 976 warn("socket"); 977 continue; 978 } 979 if (ptb->Dflag) { 980 if (setsockopt(sock, SOL_SOCKET, SO_DEBUG, 981 &ptb->Dflag, sizeof(ptb->Dflag))) 982 err(1, "setsockopt SO_DEBUG"); 983 } 984 if (aib != NULL) { 985 saddr_ntop(aib->ai_addr, aib->ai_addrlen, 986 tmp, sizeof(tmp)); 987 if (ptb->vflag) 988 fprintf(stderr, 989 "Try to bind to %s\n", tmp); 990 if (bind(sock, (struct sockaddr *)aib->ai_addr, 991 aib->ai_addrlen) == -1) 992 err(1, "bind"); 993 } 994 if (ptb->Tflag != -1 && ai->ai_family == AF_INET) { 995 if (setsockopt(sock, IPPROTO_IP, IP_TOS, 996 &ptb->Tflag, sizeof(ptb->Tflag))) 997 err(1, "setsockopt IP_TOS"); 998 } 999 if (ptb->Tflag != -1 && ai->ai_family == AF_INET6) { 1000 if (setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, 1001 &ptb->Tflag, sizeof(ptb->Tflag))) 1002 err(1, "setsockopt IPV6_TCLASS"); 1003 } 1004 if (ptb->Sflag) { 1005 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, 1006 &ptb->Sflag, sizeof(ptb->Sflag)) == -1) 1007 warn("set send socket buffer size"); 1008 } 1009 if (connect(sock, ai->ai_addr, ai->ai_addrlen) != 0) { 1010 if (ai->ai_next == NULL) 1011 err(1, "connect"); 1012 if (ptb->vflag) 1013 warn("connect"); 1014 close(sock); 1015 sock = -1; 1016 continue; 1017 } 1018 break; 1019 } 1020 if (sock == -1) 1021 errx(1, "No host found"); 1022 if ((r = fcntl(sock, F_GETFL)) == -1) 1023 err(1, "fcntl(F_GETFL)"); 1024 r |= O_NONBLOCK; 1025 if (fcntl(sock, F_SETFL, r) == -1) 1026 err(1, "fcntl(F_SETFL, O_NONBLOCK)"); 1027 /* Alloc and prepare stats */ 1028 if (TCP_MODE) { 1029 if ((sc = calloc(1, sizeof(*sc))) == NULL) 1030 err(1, "calloc"); 1031 } else 1032 sc = udp_sc; 1033 1034 sc->fd = sock; 1035 stats_prepare(sc); 1036 1037 event_set(&sc->ev, sc->fd, EV_WRITE | EV_PERSIST, 1038 client_handle_sc, sc); 1039 event_add(&sc->ev, NULL); 1040 TAILQ_INSERT_TAIL(&sc_queue, sc, entry); 1041 1042 mainstats.nconns++; 1043 if (mainstats.nconns == 1) 1044 set_slice_timer(1); 1045 } 1046 if (!ptb->Uflag) 1047 freeaddrinfo(aitop); 1048 if (aib != NULL) 1049 freeaddrinfo(aib); 1050 1051 if (ptb->vflag && nconn > 1) 1052 fprintf(stderr, "%d connections established\n", 1053 mainstats.nconns); 1054 } 1055 1056 static int 1057 map_tos(char *s, int *val) 1058 { 1059 /* DiffServ Codepoints and other TOS mappings */ 1060 const struct toskeywords { 1061 const char *keyword; 1062 int val; 1063 } *t, toskeywords[] = { 1064 { "af11", IPTOS_DSCP_AF11 }, 1065 { "af12", IPTOS_DSCP_AF12 }, 1066 { "af13", IPTOS_DSCP_AF13 }, 1067 { "af21", IPTOS_DSCP_AF21 }, 1068 { "af22", IPTOS_DSCP_AF22 }, 1069 { "af23", IPTOS_DSCP_AF23 }, 1070 { "af31", IPTOS_DSCP_AF31 }, 1071 { "af32", IPTOS_DSCP_AF32 }, 1072 { "af33", IPTOS_DSCP_AF33 }, 1073 { "af41", IPTOS_DSCP_AF41 }, 1074 { "af42", IPTOS_DSCP_AF42 }, 1075 { "af43", IPTOS_DSCP_AF43 }, 1076 { "critical", IPTOS_PREC_CRITIC_ECP }, 1077 { "cs0", IPTOS_DSCP_CS0 }, 1078 { "cs1", IPTOS_DSCP_CS1 }, 1079 { "cs2", IPTOS_DSCP_CS2 }, 1080 { "cs3", IPTOS_DSCP_CS3 }, 1081 { "cs4", IPTOS_DSCP_CS4 }, 1082 { "cs5", IPTOS_DSCP_CS5 }, 1083 { "cs6", IPTOS_DSCP_CS6 }, 1084 { "cs7", IPTOS_DSCP_CS7 }, 1085 { "ef", IPTOS_DSCP_EF }, 1086 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL }, 1087 { "lowdelay", IPTOS_LOWDELAY }, 1088 { "netcontrol", IPTOS_PREC_NETCONTROL }, 1089 { "reliability", IPTOS_RELIABILITY }, 1090 { "throughput", IPTOS_THROUGHPUT }, 1091 { NULL, -1 }, 1092 }; 1093 1094 for (t = toskeywords; t->keyword != NULL; t++) { 1095 if (strcmp(s, t->keyword) == 0) { 1096 *val = t->val; 1097 return (1); 1098 } 1099 } 1100 1101 return (0); 1102 } 1103 1104 static void 1105 quit(int sig, short event, void *arg) 1106 { 1107 wrapup(0); 1108 } 1109 1110 static void 1111 wrapup(int err) 1112 { 1113 const int transfers = timerisset(&mainstats.t_first); 1114 const int stats = (mainstats.floor_mbps != INFINITY); 1115 1116 if (transfers) { 1117 if (!stats) { 1118 if (UDP_MODE) 1119 udp_process_slice(0, 0, NULL); 1120 else 1121 tcp_process_slice(0, 0, NULL); 1122 } 1123 1124 summary_display(); 1125 } 1126 1127 if (err != -1) 1128 exit(err); 1129 } 1130 1131 int 1132 main(int argc, char **argv) 1133 { 1134 struct timeval tv; 1135 unsigned int secs, rtable; 1136 1137 char kerr[_POSIX2_LINE_MAX], *tmp; 1138 struct addrinfo *aitop, *aib, hints; 1139 const char *errstr; 1140 struct rlimit rl; 1141 int ch, herr, nconn; 1142 int family = PF_UNSPEC; 1143 struct nlist nl[] = { { "_tcbtable" }, { "" } }; 1144 const char *host = NULL, *port = DEFAULT_PORT, *srcbind = NULL; 1145 struct event ev_sigint, ev_sigterm, ev_sighup, ev_siginfo, ev_progtimer; 1146 struct sockaddr_un sock_un; 1147 1148 /* Init world */ 1149 setvbuf(stdout, NULL, _IOLBF, 0); 1150 ptb = &tcpbench; 1151 ptb->dummybuf_len = 0; 1152 ptb->Dflag = 0; 1153 ptb->Sflag = ptb->sflag = ptb->vflag = ptb->Rflag = ptb->Uflag = 0; 1154 ptb->kvmh = NULL; 1155 ptb->kvars = NULL; 1156 ptb->rflag = DEFAULT_STATS_INTERVAL; 1157 ptb->Tflag = -1; 1158 nconn = 1; 1159 aib = NULL; 1160 secs = 0; 1161 1162 while ((ch = getopt(argc, argv, "46b:B:Dhlk:n:p:Rr:sS:t:T:uUvV:")) 1163 != -1) { 1164 switch (ch) { 1165 case '4': 1166 family = PF_INET; 1167 break; 1168 case '6': 1169 family = PF_INET6; 1170 break; 1171 case 'b': 1172 srcbind = optarg; 1173 break; 1174 case 'D': 1175 ptb->Dflag = 1; 1176 break; 1177 case 'l': 1178 list_kvars(); 1179 exit(0); 1180 case 'k': 1181 if ((tmp = strdup(optarg)) == NULL) 1182 err(1, "strdup"); 1183 ptb->kvars = check_prepare_kvars(tmp); 1184 free(tmp); 1185 break; 1186 case 'R': 1187 ptb->Rflag = 1; 1188 break; 1189 case 'r': 1190 ptb->rflag = strtonum(optarg, 0, 60 * 60 * 24 * 1000, 1191 &errstr); 1192 if (errstr != NULL) 1193 errx(1, "statistics interval is %s: %s", 1194 errstr, optarg); 1195 break; 1196 case 'p': 1197 port = optarg; 1198 break; 1199 case 's': 1200 ptb->sflag = 1; 1201 break; 1202 case 'S': 1203 ptb->Sflag = strtonum(optarg, 0, 1024*1024*1024, 1204 &errstr); 1205 if (errstr != NULL) 1206 errx(1, "socket buffer size is %s: %s", 1207 errstr, optarg); 1208 break; 1209 case 'B': 1210 ptb->dummybuf_len = strtonum(optarg, 0, 1024*1024*1024, 1211 &errstr); 1212 if (errstr != NULL) 1213 errx(1, "read/write buffer size is %s: %s", 1214 errstr, optarg); 1215 break; 1216 case 'v': 1217 ptb->vflag++; 1218 break; 1219 case 'V': 1220 rtable = (unsigned int)strtonum(optarg, 0, 1221 RT_TABLEID_MAX, &errstr); 1222 if (errstr) 1223 errx(1, "rtable value is %s: %s", 1224 errstr, optarg); 1225 if (setrtable(rtable) == -1) 1226 err(1, "setrtable"); 1227 break; 1228 case 'n': 1229 nconn = strtonum(optarg, 0, 65535, &errstr); 1230 if (errstr != NULL) 1231 errx(1, "number of connections is %s: %s", 1232 errstr, optarg); 1233 break; 1234 case 'u': 1235 ptb->uflag = 1; 1236 break; 1237 case 'U': 1238 ptb->Uflag = 1; 1239 break; 1240 case 'T': 1241 if (map_tos(optarg, &ptb->Tflag)) 1242 break; 1243 errstr = NULL; 1244 if (strlen(optarg) > 1 && optarg[0] == '0' && 1245 optarg[1] == 'x') 1246 ptb->Tflag = (int)strtol(optarg, NULL, 16); 1247 else 1248 ptb->Tflag = (int)strtonum(optarg, 0, 255, 1249 &errstr); 1250 if (ptb->Tflag == -1 || ptb->Tflag > 255 || errstr) 1251 errx(1, "illegal tos value %s", optarg); 1252 break; 1253 case 't': 1254 secs = strtonum(optarg, 1, UINT_MAX, &errstr); 1255 if (errstr != NULL) 1256 errx(1, "secs is %s: %s", 1257 errstr, optarg); 1258 break; 1259 case 'h': 1260 default: 1261 usage(); 1262 } 1263 } 1264 1265 if (pledge("stdio unveil rpath dns inet unix id proc", NULL) == -1) 1266 err(1, "pledge"); 1267 1268 argv += optind; 1269 argc -= optind; 1270 if ((argc != (ptb->sflag && !ptb->Uflag ? 0 : 1)) || 1271 (UDP_MODE && (ptb->kvars || nconn != 1))) 1272 usage(); 1273 1274 if (ptb->kvars) { 1275 if (unveil(_PATH_MEM, "r") == -1) 1276 err(1, "unveil %s", _PATH_MEM); 1277 if (unveil(_PATH_KMEM, "r") == -1) 1278 err(1, "unveil %s", _PATH_KMEM); 1279 if (unveil(_PATH_KSYMS, "r") == -1) 1280 err(1, "unveil %s", _PATH_KSYMS); 1281 1282 if ((ptb->kvmh = kvm_openfiles(NULL, NULL, NULL, 1283 O_RDONLY, kerr)) == NULL) 1284 errx(1, "kvm_open: %s", kerr); 1285 drop_gid(); 1286 if (kvm_nlist(ptb->kvmh, nl) < 0 || nl[0].n_type == 0) 1287 errx(1, "kvm: no namelist"); 1288 ptb->ktcbtab = nl[0].n_value; 1289 } else 1290 drop_gid(); 1291 1292 if (!ptb->sflag || ptb->Uflag) 1293 mainstats.host = host = argv[0]; 1294 1295 if (ptb->Uflag) 1296 if (unveil(host, "rwc") == -1) 1297 err(1, "unveil %s", host); 1298 1299 if (pledge("stdio id dns inet unix", NULL) == -1) 1300 err(1, "pledge"); 1301 1302 /* 1303 * Rationale, 1304 * If TCP, use a big buffer with big reads/writes. 1305 * If UDP, use a big buffer in server and a buffer the size of a 1306 * ethernet packet. 1307 */ 1308 if (!ptb->dummybuf_len) { 1309 if (ptb->sflag || TCP_MODE) 1310 ptb->dummybuf_len = DEFAULT_BUF; 1311 else 1312 ptb->dummybuf_len = DEFAULT_UDP_PKT; 1313 } 1314 1315 bzero(&hints, sizeof(hints)); 1316 hints.ai_family = family; 1317 if (UDP_MODE) { 1318 hints.ai_socktype = SOCK_DGRAM; 1319 hints.ai_protocol = IPPROTO_UDP; 1320 } else { 1321 hints.ai_socktype = SOCK_STREAM; 1322 hints.ai_protocol = IPPROTO_TCP; 1323 } 1324 if (ptb->Uflag) { 1325 hints.ai_family = AF_UNIX; 1326 hints.ai_protocol = 0; 1327 sock_un.sun_family = AF_UNIX; 1328 if (strlcpy(sock_un.sun_path, host, sizeof(sock_un.sun_path)) >= 1329 sizeof(sock_un.sun_path)) 1330 errx(1, "socket name '%s' too long", host); 1331 hints.ai_addr = (struct sockaddr *)&sock_un; 1332 hints.ai_addrlen = sizeof(sock_un); 1333 aitop = &hints; 1334 } else { 1335 if (ptb->sflag) 1336 hints.ai_flags = AI_PASSIVE; 1337 if (srcbind != NULL) { 1338 hints.ai_flags |= AI_NUMERICHOST; 1339 herr = getaddrinfo(srcbind, NULL, &hints, &aib); 1340 hints.ai_flags &= ~AI_NUMERICHOST; 1341 if (herr != 0) { 1342 if (herr == EAI_SYSTEM) 1343 err(1, "getaddrinfo"); 1344 else 1345 errx(1, "getaddrinfo: %s", 1346 gai_strerror(herr)); 1347 } 1348 } 1349 if ((herr = getaddrinfo(host, port, &hints, &aitop)) != 0) { 1350 if (herr == EAI_SYSTEM) 1351 err(1, "getaddrinfo"); 1352 else 1353 errx(1, "getaddrinfo: %s", gai_strerror(herr)); 1354 } 1355 } 1356 1357 if (pledge("stdio id inet unix", NULL) == -1) 1358 err(1, "pledge"); 1359 1360 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) 1361 err(1, "getrlimit"); 1362 if (rl.rlim_cur < MAX_FD) 1363 rl.rlim_cur = MAX_FD; 1364 if (setrlimit(RLIMIT_NOFILE, &rl)) 1365 err(1, "setrlimit"); 1366 if (getrlimit(RLIMIT_NOFILE, &rl) == -1) 1367 err(1, "getrlimit"); 1368 1369 if (pledge("stdio inet unix", NULL) == -1) 1370 err(1, "pledge"); 1371 1372 /* Init world */ 1373 TAILQ_INIT(&sc_queue); 1374 if ((ptb->dummybuf = malloc(ptb->dummybuf_len)) == NULL) 1375 err(1, "malloc"); 1376 arc4random_buf(ptb->dummybuf, ptb->dummybuf_len); 1377 1378 timerclear(&mainstats.t_first); 1379 mainstats.floor_mbps = INFINITY; 1380 1381 /* Setup libevent and signals */ 1382 event_init(); 1383 signal_set(&ev_sigterm, SIGTERM, signal_handler, NULL); 1384 signal_set(&ev_sighup, SIGHUP, signal_handler, NULL); 1385 signal_set(&ev_sigint, SIGINT, signal_handler, NULL); 1386 signal_set(&ev_siginfo, SIGINFO, signal_handler, NULL); 1387 signal_add(&ev_sigint, NULL); 1388 signal_add(&ev_sigterm, NULL); 1389 signal_add(&ev_sighup, NULL); 1390 signal_add(&ev_siginfo, NULL); 1391 signal(SIGPIPE, SIG_IGN); 1392 1393 if (UDP_MODE) { 1394 if ((udp_sc = calloc(1, sizeof(*udp_sc))) == NULL) 1395 err(1, "calloc"); 1396 udp_sc->fd = -1; 1397 evtimer_set(&mainstats.timer, udp_process_slice, NULL); 1398 } else { 1399 print_tcp_header(); 1400 evtimer_set(&mainstats.timer, tcp_process_slice, NULL); 1401 } 1402 1403 if (ptb->sflag) 1404 server_init(aitop); 1405 else { 1406 if (secs > 0) { 1407 timerclear(&tv); 1408 tv.tv_sec = secs + 1; 1409 evtimer_set(&ev_progtimer, quit, NULL); 1410 evtimer_add(&ev_progtimer, &tv); 1411 } 1412 client_init(aitop, nconn, aib); 1413 1414 if (pledge("stdio", NULL) == -1) 1415 err(1, "pledge"); 1416 } 1417 1418 /* libevent main loop*/ 1419 event_dispatch(); 1420 1421 return (0); 1422 } 1423