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