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