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