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