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