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