Annotation of src/usr.bin/tcpbench/tcpbench.c, Revision 1.11
1.1 djm 1: /*
2: * Copyright (c) 2008 Damien Miller <djm@mindrot.org>
3: *
4: * Permission to use, copy, modify, and distribute this software for any
5: * purpose with or without fee is hereby granted, provided that the above
6: * copyright notice and this permission notice appear in all copies.
7: *
8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15: */
16:
17: #include <sys/types.h>
18: #include <sys/time.h>
19: #include <sys/socket.h>
20: #include <sys/socketvar.h>
1.11 ! claudio 21: #include <sys/resource.h>
1.1 djm 22:
23: #include <net/route.h>
24:
25: #include <netinet/in.h>
26: #include <netinet/in_systm.h>
27: #include <netinet/ip.h>
28: #include <netinet/tcp.h>
29: #include <netinet/tcp_timer.h>
30: #include <netinet/tcp_fsm.h>
31: #include <netinet/in_pcb.h>
32: #include <netinet/tcp_var.h>
33:
34: #include <arpa/inet.h>
35:
36: #include <unistd.h>
37: #include <limits.h>
38: #include <stdlib.h>
39: #include <stdio.h>
40: #include <string.h>
41: #include <errno.h>
42: #include <netdb.h>
43: #include <signal.h>
44: #include <err.h>
45: #include <fcntl.h>
46: #include <poll.h>
47:
48: #include <kvm.h>
49: #include <nlist.h>
50:
1.11 ! claudio 51: #define DEFAULT_PORT "12345"
! 52: #define DEFAULT_STATS_INTERVAL 1000 /* ms */
! 53: #define DEFAULT_BUF 256 * 1024
! 54: #define MAX_FD 1024
1.1 djm 55:
56: sig_atomic_t done = 0;
1.11 ! claudio 57: sig_atomic_t proc_slice = 0;
1.1 djm 58:
1.11 ! claudio 59: static u_int rdomain;
! 60: static char **kflag;
! 61: static size_t Bflag;
! 62: static int Sflag;
! 63: static int rflag;
! 64: static int sflag;
! 65: static int vflag;
1.9 claudio 66:
1.11 ! claudio 67: /* stats for a single connection */
1.1 djm 68: struct statctx {
1.11 ! claudio 69: struct timeval t_start, t_last;
1.1 djm 70: unsigned long long bytes;
71: u_long tcbaddr;
1.11 ! claudio 72: char **kvars;
1.1 djm 73: kvm_t *kh;
74: };
75:
1.11 ! claudio 76: /*
! 77: * We account the mainstats here, that is the stats
! 78: * for all connections, all variables starting with slice
! 79: * are used to account information for the timeslice
! 80: * between each output. Peak variables record the highest
! 81: * between all slices so far.
! 82: */
! 83: static struct {
! 84: unsigned long long slice_bytes; /* bytes for last slice */
! 85: struct timeval t_start; /* when we started counting */
! 86: long double peak_mbps; /* peak mbps so far */
! 87: int nconns; /* connected clients */
! 88: } mainstats;
! 89:
1.1 djm 90: /* When adding variables, also add to stats_display() */
91: static const char *allowed_kvars[] = {
92: "inpcb.inp_flags",
93: "sockb.so_rcv.sb_cc",
94: "sockb.so_rcv.sb_hiwat",
95: "sockb.so_snd.sb_cc",
96: "sockb.so_snd.sb_hiwat",
97: "tcpcb.snd_una",
98: "tcpcb.snd_nxt",
99: "tcpcb.snd_wl1",
100: "tcpcb.snd_wl2",
101: "tcpcb.snd_wnd",
102: "tcpcb.rcv_wnd",
103: "tcpcb.rcv_nxt",
104: "tcpcb.rcv_adv",
105: "tcpcb.snd_max",
106: "tcpcb.snd_cwnd",
107: "tcpcb.snd_ssthresh",
108: "tcpcb.t_rcvtime",
109: "tcpcb.t_rtttime",
110: "tcpcb.t_rtseq",
111: "tcpcb.t_srtt",
112: "tcpcb.t_rttvar",
113: "tcpcb.t_rttmin",
114: "tcpcb.max_sndwnd",
115: "tcpcb.snd_scale",
116: "tcpcb.rcv_scale",
117: "tcpcb.last_ack_sent",
118: NULL
119: };
120:
121: static void
122: exitsighand(int signo)
123: {
124: done = signo;
125: }
126:
127: static void
128: alarmhandler(int signo)
129: {
1.11 ! claudio 130: proc_slice = 1;
1.1 djm 131: signal(signo, alarmhandler);
132: }
133:
134: static void __dead
135: usage(void)
136: {
137: fprintf(stderr,
1.6 jmc 138: "usage: tcpbench -l\n"
139: " tcpbench [-v] [-B buf] [-k kvars] [-n connections]"
1.9 claudio 140: " [-p port] [-r rate] [-V rdomain]\n"
1.6 jmc 141: " [-S space] hostname\n"
142: " tcpbench -s [-v] [-B buf] [-k kvars] [-p port] [-r rate]"
1.9 claudio 143: " [-S space] [-V rdomain]\n");
1.1 djm 144: exit(1);
145: }
146:
147: static void
148: saddr_ntop(const struct sockaddr *addr, socklen_t alen, char *buf, size_t len)
149: {
150: char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
151: int herr;
152:
1.3 djm 153: if ((herr = getnameinfo(addr, alen, hbuf, sizeof(hbuf),
154: pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
1.1 djm 155: if (herr == EAI_SYSTEM)
156: err(1, "getnameinfo");
157: else
158: errx(1, "getnameinfo: %s", gai_strerror(herr));
159: }
160: snprintf(buf, len, "[%s]:%s", hbuf, pbuf);
161: }
162:
163: static void
1.11 ! claudio 164: set_timer(int toggle)
! 165: {
! 166: struct itimerval itv;
! 167:
! 168: if (rflag <= 0)
! 169: return;
! 170:
! 171: if (toggle) {
! 172: itv.it_interval.tv_sec = rflag / 1000;
! 173: itv.it_interval.tv_usec = (rflag % 1000) * 1000;
! 174: itv.it_value = itv.it_interval;
! 175: }
! 176: else
! 177: bzero(&itv, sizeof(itv));
! 178:
! 179: setitimer(ITIMER_REAL, &itv, NULL);
! 180: }
! 181:
! 182: static void
! 183: print_header(void)
! 184: {
! 185: char **kv;
! 186:
! 187: printf("%12s %14s %12s %8s ", "elapsed_ms", "bytes", "mbps",
! 188: "bwidth");
! 189:
! 190: for (kv = kflag; kflag != NULL && *kv != NULL; kv++)
! 191: printf("%s%s", kv != kflag ? "," : "", *kv);
! 192:
! 193: printf("\n");
! 194: }
! 195:
! 196: static void
1.1 djm 197: kget(kvm_t *kh, u_long addr, void *buf, int size)
198: {
199: if (kvm_read(kh, addr, buf, size) != size)
200: errx(1, "kvm_read: %s", kvm_geterr(kh));
201: }
202:
203: static u_long
1.11 ! claudio 204: kfind_tcb(kvm_t *kh, u_long ktcbtab, int sock)
1.1 djm 205: {
206: struct inpcbtable tcbtab;
207: struct inpcb *head, *next, *prev;
208: struct inpcb inpcb;
209: struct tcpcb tcpcb;
210:
211: struct sockaddr_storage me, them;
212: socklen_t melen, themlen;
213: struct sockaddr_in *in4;
214: struct sockaddr_in6 *in6;
215: char tmp1[64], tmp2[64];
1.11 ! claudio 216: int nretry;
1.1 djm 217:
1.11 ! claudio 218: nretry = 10;
1.1 djm 219: melen = themlen = sizeof(struct sockaddr_storage);
220: if (getsockname(sock, (struct sockaddr *)&me, &melen) == -1)
221: err(1, "getsockname");
222: if (getpeername(sock, (struct sockaddr *)&them, &themlen) == -1)
223: err(1, "getpeername");
224: if (me.ss_family != them.ss_family)
225: errx(1, "%s: me.ss_family != them.ss_family", __func__);
226: if (me.ss_family != AF_INET && me.ss_family != AF_INET6)
227: errx(1, "%s: unknown socket family", __func__);
228: if (vflag >= 2) {
229: saddr_ntop((struct sockaddr *)&me, me.ss_len,
230: tmp1, sizeof(tmp1));
231: saddr_ntop((struct sockaddr *)&them, them.ss_len,
232: tmp2, sizeof(tmp2));
233: fprintf(stderr, "Our socket local %s remote %s\n", tmp1, tmp2);
234: }
235: if (vflag >= 2)
236: fprintf(stderr, "Using PCB table at %lu\n", ktcbtab);
1.11 ! claudio 237: retry:
1.1 djm 238: kget(kh, ktcbtab, &tcbtab, sizeof(tcbtab));
239: prev = head = (struct inpcb *)&CIRCLEQ_FIRST(
240: &((struct inpcbtable *)ktcbtab)->inpt_queue);
241: next = CIRCLEQ_FIRST(&tcbtab.inpt_queue);
242:
243: if (vflag >= 2)
244: fprintf(stderr, "PCB head at %p\n", head);
245: while (next != head) {
246: if (vflag >= 2)
247: fprintf(stderr, "Checking PCB %p\n", next);
248: kget(kh, (u_long)next, &inpcb, sizeof(inpcb));
1.11 ! claudio 249: if (CIRCLEQ_PREV(&inpcb, inp_queue) != prev) {
! 250: if (nretry--) {
! 251: warnx("pcb prev pointer insane");
! 252: goto retry;
! 253: }
! 254: else
! 255: errx(1, "pcb prev pointer insane,"
! 256: " all attempts exausted");
! 257: }
1.1 djm 258: prev = next;
259: next = CIRCLEQ_NEXT(&inpcb, inp_queue);
260:
261: if (me.ss_family == AF_INET) {
262: if ((inpcb.inp_flags & INP_IPV6) != 0) {
263: if (vflag >= 2)
264: fprintf(stderr, "Skip: INP_IPV6");
265: continue;
266: }
267: if (vflag >= 2) {
268: inet_ntop(AF_INET, &inpcb.inp_laddr,
269: tmp1, sizeof(tmp1));
270: inet_ntop(AF_INET, &inpcb.inp_faddr,
271: tmp2, sizeof(tmp2));
272: fprintf(stderr, "PCB %p local: [%s]:%d "
273: "remote: [%s]:%d\n", prev,
274: tmp1, inpcb.inp_lport,
275: tmp2, inpcb.inp_fport);
276: }
277: in4 = (struct sockaddr_in *)&me;
278: if (memcmp(&in4->sin_addr, &inpcb.inp_laddr,
279: sizeof(struct in_addr)) != 0 ||
280: in4->sin_port != inpcb.inp_lport)
281: continue;
282: in4 = (struct sockaddr_in *)&them;
283: if (memcmp(&in4->sin_addr, &inpcb.inp_faddr,
284: sizeof(struct in_addr)) != 0 ||
285: in4->sin_port != inpcb.inp_fport)
286: continue;
287: } else {
288: if ((inpcb.inp_flags & INP_IPV6) == 0)
289: continue;
290: if (vflag >= 2) {
291: inet_ntop(AF_INET6, &inpcb.inp_laddr6,
292: tmp1, sizeof(tmp1));
293: inet_ntop(AF_INET6, &inpcb.inp_faddr6,
294: tmp2, sizeof(tmp2));
295: fprintf(stderr, "PCB %p local: [%s]:%d "
296: "remote: [%s]:%d\n", prev,
297: tmp1, inpcb.inp_lport,
298: tmp2, inpcb.inp_fport);
299: }
300: in6 = (struct sockaddr_in6 *)&me;
301: if (memcmp(&in6->sin6_addr, &inpcb.inp_laddr6,
302: sizeof(struct in6_addr)) != 0 ||
303: in6->sin6_port != inpcb.inp_lport)
304: continue;
305: in6 = (struct sockaddr_in6 *)&them;
306: if (memcmp(&in6->sin6_addr, &inpcb.inp_faddr6,
307: sizeof(struct in6_addr)) != 0 ||
308: in6->sin6_port != inpcb.inp_fport)
309: continue;
310: }
311: kget(kh, (u_long)inpcb.inp_ppcb, &tcpcb, sizeof(tcpcb));
312: if (tcpcb.t_state != TCPS_ESTABLISHED) {
313: if (vflag >= 2)
314: fprintf(stderr, "Not established\n");
315: continue;
316: }
317: if (vflag >= 2)
318: fprintf(stderr, "Found PCB at %p\n", prev);
319: return (u_long)prev;
320: }
321:
322: errx(1, "No matching PCB found");
323: }
324:
325: static void
326: kupdate_stats(kvm_t *kh, u_long tcbaddr,
327: struct inpcb *inpcb, struct tcpcb *tcpcb, struct socket *sockb)
328: {
329: kget(kh, tcbaddr, inpcb, sizeof(*inpcb));
330: kget(kh, (u_long)inpcb->inp_ppcb, tcpcb, sizeof(*tcpcb));
331: kget(kh, (u_long)inpcb->inp_socket, sockb, sizeof(*sockb));
332: }
333:
334: static void
335: check_kvar(const char *var)
336: {
337: size_t i;
338:
339: for (i = 0; allowed_kvars[i] != NULL; i++)
340: if (strcmp(allowed_kvars[i], var) == 0)
341: return;
342: errx(1, "Unrecognised kvar: %s", var);
343: }
344:
345: static void
346: list_kvars(void)
347: {
348: size_t i;
349:
350: fprintf(stderr, "Supported kernel variables:\n");
351: for (i = 0; allowed_kvars[i] != NULL; i++)
352: fprintf(stderr, "\t%s\n", allowed_kvars[i]);
353: }
354:
355: static char **
356: check_prepare_kvars(char *list)
357: {
358: char *item, **ret = NULL;
359: size_t n = 0;
360:
361: while ((item = strsep(&list, ", \t\n")) != NULL) {
362: check_kvar(item);
363: if ((ret = realloc(ret, sizeof(*ret) * (++n + 1))) == NULL)
364: errx(1, "realloc(kvars)");
365: if ((ret[n - 1] = strdup(item)) == NULL)
366: errx(1, "strdup");
367: ret[n] = NULL;
368: }
369: return ret;
370: }
371:
372: static void
1.11 ! claudio 373: stats_prepare(struct statctx *sc, int fd, kvm_t *kh, u_long ktcbtab)
1.1 djm 374: {
375: if (rflag <= 0)
376: return;
377: sc->kh = kh;
378: sc->kvars = kflag;
379: if (kflag)
1.11 ! claudio 380: sc->tcbaddr = kfind_tcb(kh, ktcbtab, fd);
! 381: if (gettimeofday(&sc->t_start, NULL) == -1)
! 382: err(1, "gettimeofday");
1.1 djm 383: sc->t_last = sc->t_start;
384: sc->bytes = 0;
385: }
386:
387: static void
388: stats_update(struct statctx *sc, ssize_t n)
389: {
390: sc->bytes += n;
1.11 ! claudio 391: mainstats.slice_bytes += n;
1.1 djm 392: }
393:
394: static void
1.11 ! claudio 395: stats_cleanslice(void)
1.1 djm 396: {
1.11 ! claudio 397: mainstats.slice_bytes = 0;
! 398: }
1.1 djm 399:
1.11 ! claudio 400: static void
! 401: stats_display(unsigned long long total_elapsed, long double mbps,
! 402: float bwperc, struct statctx *sc, struct inpcb *inpcb,
! 403: struct tcpcb *tcpcb, struct socket *sockb)
! 404: {
! 405: int j;
! 406:
! 407: printf("%12llu %14llu %12.3Lf %7.2f%% ", total_elapsed, sc->bytes,
! 408: mbps, bwperc);
! 409:
! 410: if (sc->kvars != NULL) {
! 411: kupdate_stats(sc->kh, sc->tcbaddr, inpcb, tcpcb,
! 412: sockb);
1.1 djm 413:
1.11 ! claudio 414: for (j = 0; sc->kvars[j] != NULL; j++) {
! 415: #define S(a) #a
! 416: #define P(b, v, f) \
! 417: if (strcmp(sc->kvars[j], S(b.v)) == 0) { \
! 418: printf("%s"f, j > 0 ? "," : "", b->v); \
! 419: continue; \
! 420: }
! 421: P(inpcb, inp_flags, "0x%08x")
! 422: P(sockb, so_rcv.sb_cc, "%lu")
! 423: P(sockb, so_rcv.sb_hiwat, "%lu")
! 424: P(sockb, so_snd.sb_cc, "%lu")
! 425: P(sockb, so_snd.sb_hiwat, "%lu")
! 426: P(tcpcb, snd_una, "%u")
! 427: P(tcpcb, snd_nxt, "%u")
! 428: P(tcpcb, snd_wl1, "%u")
! 429: P(tcpcb, snd_wl2, "%u")
! 430: P(tcpcb, snd_wnd, "%lu")
! 431: P(tcpcb, rcv_wnd, "%lu")
! 432: P(tcpcb, rcv_nxt, "%u")
! 433: P(tcpcb, rcv_adv, "%u")
! 434: P(tcpcb, snd_max, "%u")
! 435: P(tcpcb, snd_cwnd, "%lu")
! 436: P(tcpcb, snd_ssthresh, "%lu")
! 437: P(tcpcb, t_rcvtime, "%u")
! 438: P(tcpcb, t_rtttime, "%u")
! 439: P(tcpcb, t_rtseq, "%u")
! 440: P(tcpcb, t_srtt, "%hu")
! 441: P(tcpcb, t_rttvar, "%hu")
! 442: P(tcpcb, t_rttmin, "%hu")
! 443: P(tcpcb, max_sndwnd, "%lu")
! 444: P(tcpcb, snd_scale, "%u")
! 445: P(tcpcb, rcv_scale, "%u")
! 446: P(tcpcb, last_ack_sent, "%u")
! 447: #undef S
1.1 djm 448: #undef P
449: }
450: }
451: printf("\n");
452: }
453:
454: static void
1.11 ! claudio 455: mainstats_display(long double slice_mbps, long double avg_mbps)
1.1 djm 456: {
1.11 ! claudio 457: printf("Conn: %3d Mbps: %12.3Lf Peak Mbps: %12.3Lf Avg Mbps: %12.3Lf\n",
! 458: mainstats.nconns, slice_mbps, mainstats.peak_mbps, avg_mbps);
1.1 djm 459: }
460:
1.11 ! claudio 461: static void
! 462: process_slice(struct statctx *sc, size_t nsc)
1.1 djm 463: {
1.11 ! claudio 464: unsigned long long total_elapsed, since_last;
! 465: long double mbps, slice_mbps = 0;
! 466: float bwperc;
! 467: nfds_t i;
! 468: struct timeval t_cur, t_diff;
! 469: struct inpcb inpcb;
! 470: struct tcpcb tcpcb;
! 471: struct socket sockb;
! 472:
! 473: for (i = 0; i < nsc; i++, sc++) {
! 474: if (gettimeofday(&t_cur, NULL) == -1)
! 475: err(1, "gettimeofday");
! 476: if (sc->kvars != NULL) /* process kernel stats */
! 477: kupdate_stats(sc->kh, sc->tcbaddr, &inpcb, &tcpcb,
! 478: &sockb);
! 479: timersub(&t_cur, &sc->t_start, &t_diff);
! 480: total_elapsed = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000;
! 481: timersub(&t_cur, &sc->t_last, &t_diff);
! 482: since_last = t_diff.tv_sec * 1000 + t_diff.tv_usec / 1000;
! 483: bwperc = (sc->bytes * 100.0) / mainstats.slice_bytes;
! 484: mbps = (sc->bytes * 8) / (since_last * 1000.0);
! 485: slice_mbps += mbps;
! 486:
! 487: stats_display(total_elapsed, mbps, bwperc, sc,
! 488: &inpcb, &tcpcb, &sockb);
! 489:
! 490: sc->t_last = t_cur;
! 491: sc->bytes = 0;
1.1 djm 492:
1.11 ! claudio 493: }
1.1 djm 494:
1.11 ! claudio 495: /* process stats for this slice */
! 496: if (slice_mbps > mainstats.peak_mbps)
! 497: mainstats.peak_mbps = slice_mbps;
! 498: mainstats_display(slice_mbps, slice_mbps / mainstats.nconns);
! 499: }
1.1 djm 500:
1.11 ! claudio 501: static int
! 502: handle_connection(struct statctx *sc, int fd, char *buf, size_t buflen)
! 503: {
! 504: ssize_t n;
1.1 djm 505:
1.11 ! claudio 506: again:
! 507: n = read(fd, buf, buflen);
! 508: if (n == -1) {
! 509: if (errno == EINTR)
! 510: goto again;
! 511: else if (errno == EWOULDBLOCK)
! 512: return 0;
! 513: warn("fd %d read error", fd);
! 514:
! 515: return -1;
1.1 djm 516: }
1.11 ! claudio 517: else if (n == 0) {
! 518: if (vflag)
! 519: fprintf(stderr, "%8d closed by remote end\n", fd);
! 520: close(fd);
! 521: return -1;
! 522: }
! 523: if (vflag >= 3)
! 524: fprintf(stderr, "read: %zd bytes\n", n);
! 525:
! 526: stats_update(sc, n);
! 527: return 0;
1.1 djm 528: }
529:
1.11 ! claudio 530: static nfds_t
! 531: serverbind(struct pollfd *pfd, nfds_t max_nfds, struct addrinfo *aitop)
1.1 djm 532: {
533: char tmp[128];
1.11 ! claudio 534: int sock, on = 1;
1.1 djm 535: struct addrinfo *ai;
1.11 ! claudio 536: nfds_t lnfds;
1.1 djm 537:
1.11 ! claudio 538: lnfds = 0;
1.1 djm 539: for (ai = aitop; ai != NULL; ai = ai->ai_next) {
1.11 ! claudio 540: if (lnfds == max_nfds) {
! 541: fprintf(stderr,
! 542: "maximum number of listening fds reached\n");
! 543: break;
! 544: }
1.1 djm 545: saddr_ntop(ai->ai_addr, ai->ai_addrlen, tmp, sizeof(tmp));
546: if (vflag)
1.11 ! claudio 547: fprintf(stderr, "Try to listen on %s\n", tmp);
1.1 djm 548: if ((sock = socket(ai->ai_family, ai->ai_socktype,
549: ai->ai_protocol)) == -1) {
550: if (ai->ai_next == NULL)
551: err(1, "socket");
552: if (vflag)
553: warn("socket");
554: continue;
555: }
1.9 claudio 556: if (rdomain && ai->ai_family == AF_INET) {
557: if (setsockopt(sock, IPPROTO_IP, SO_RDOMAIN,
558: &rdomain, sizeof(rdomain)) == -1)
559: err(1, "setsockopt SO_RDOMAIN");
1.10 claudio 560: } else if (rdomain)
561: warnx("rdomain only supported on AF_INET");
1.1 djm 562: if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
563: &on, sizeof(on)) == -1)
564: warn("reuse port");
565: if (bind(sock, ai->ai_addr, ai->ai_addrlen) != 0) {
566: if (ai->ai_next == NULL)
567: err(1, "bind");
568: if (vflag)
569: warn("bind");
570: close(sock);
571: continue;
572: }
573: if (Sflag) {
574: if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
575: &Sflag, sizeof(Sflag)) == -1)
576: warn("set TCP receive buffer size");
577: }
578: if (listen(sock, 64) == -1) {
579: if (ai->ai_next == NULL)
580: err(1, "listen");
581: if (vflag)
582: warn("listen");
583: close(sock);
584: continue;
585: }
1.11 ! claudio 586: if (vflag >= 3)
! 587: fprintf(stderr, "listening on fd %d\n", sock);
! 588: lnfds++;
! 589: pfd[lnfds - 1].fd = sock;
! 590: pfd[lnfds - 1].events = POLLIN;
! 591:
1.1 djm 592: }
593: freeaddrinfo(aitop);
1.11 ! claudio 594: if (lnfds == 0)
1.1 djm 595: errx(1, "No working listen addresses found");
596:
1.11 ! claudio 597: return lnfds;
! 598: }
! 599:
! 600: static void
! 601: set_listening(struct pollfd *pfd, nfds_t lfds, int toggle) {
! 602: int i;
1.1 djm 603:
1.11 ! claudio 604: for (i = 0; i < (int)lfds; i++) {
! 605: if (toggle)
! 606: pfd[i].events = POLLIN;
! 607: else
! 608: pfd[i].events = 0;
! 609: }
! 610:
! 611: }
! 612: static void __dead
! 613: serverloop(kvm_t *kvmh, u_long ktcbtab, struct addrinfo *aitop)
! 614: {
! 615: socklen_t sslen;
! 616: struct pollfd *pfd;
! 617: char tmp[128], *buf;
! 618: struct statctx *psc;
! 619: struct sockaddr_storage ss;
! 620: nfds_t i, nfds, lfds;
! 621: size_t nalloc;
! 622: int r, sock, client_id;
! 623:
! 624: sslen = sizeof(ss);
! 625: nalloc = 128;
! 626: if ((pfd = calloc(sizeof(*pfd), nalloc)) == NULL)
! 627: err(1, "calloc");
! 628: if ((psc = calloc(sizeof(*psc), nalloc)) == NULL)
! 629: err(1, "calloc");
! 630: if ((buf = malloc(Bflag)) == NULL)
! 631: err(1, "malloc");
! 632: lfds = nfds = serverbind(pfd, nalloc - 1, aitop);
! 633: if (vflag >= 3)
! 634: fprintf(stderr, "listening on %d fds\n", lfds);
1.1 djm 635: if (setpgid(0, 0) == -1)
636: err(1, "setpgid");
1.11 ! claudio 637:
! 638: print_header();
! 639:
1.1 djm 640: client_id = 0;
1.11 ! claudio 641: while (!done) {
! 642: if (proc_slice) {
! 643: process_slice(psc + lfds, nfds - lfds);
! 644: stats_cleanslice();
! 645: proc_slice = 0;
! 646: }
! 647: if (vflag >= 3)
! 648: fprintf(stderr, "mainstats.nconns = %u\n",
! 649: mainstats.nconns);
1.1 djm 650: if ((r = poll(pfd, nfds, INFTIM)) == -1) {
651: if (errno == EINTR)
652: continue;
653: warn("poll");
654: break;
655: }
1.11 ! claudio 656:
1.1 djm 657: if (vflag >= 3)
658: fprintf(stderr, "poll: %d\n", r);
659: for (i = 0 ; r > 0 && i < nfds; i++) {
660: if ((pfd[i].revents & POLLIN) == 0)
661: continue;
1.11 ! claudio 662: if (pfd[i].fd == -1)
! 663: errx(1, "pfd insane");
! 664: r--;
1.1 djm 665: if (vflag >= 3)
1.11 ! claudio 666: fprintf(stderr, "fd %d active i = %d\n",
! 667: pfd[i].fd, i);
! 668: /* new connection */
! 669: if (i < lfds) {
! 670: if ((sock = accept(pfd[i].fd,
! 671: (struct sockaddr *)&ss,
! 672: &sslen)) == -1) {
! 673: if (errno == EINTR)
! 674: continue;
! 675: else if (errno == EMFILE ||
! 676: errno == ENFILE)
! 677: set_listening(pfd, lfds, 0);
! 678: warn("accept");
1.1 djm 679: continue;
1.11 ! claudio 680: }
! 681: if ((r = fcntl(sock, F_GETFL, 0)) == -1)
! 682: err(1, "fcntl(F_GETFL)");
! 683: r |= O_NONBLOCK;
! 684: if (fcntl(sock, F_SETFL, r) == -1)
! 685: err(1, "fcntl(F_SETFL, O_NONBLOCK)");
! 686: saddr_ntop((struct sockaddr *)&ss, sslen,
! 687: tmp, sizeof(tmp));
! 688: if (vflag)
! 689: fprintf(stderr,
! 690: "Accepted connection %d from "
! 691: "%s, fd = %d\n", client_id++, tmp,
! 692: sock);
! 693: /* alloc more space if we're full */
! 694: if (nfds == nalloc) {
! 695: nalloc *= 2;
! 696: if ((pfd = realloc(pfd,
! 697: sizeof(*pfd) * nalloc)) == NULL)
! 698: err(1, "realloc");
! 699: if ((psc = realloc(psc,
! 700: sizeof(*psc) * nalloc)) == NULL)
! 701: err(1, "realloc");
! 702: }
! 703: pfd[nfds].fd = sock;
! 704: pfd[nfds].events = POLLIN;
! 705: stats_prepare(&psc[nfds], sock, kvmh, ktcbtab);
! 706: nfds++;
! 707: if (!mainstats.nconns++)
! 708: set_timer(1);
! 709: continue;
1.1 djm 710: }
1.11 ! claudio 711: /* event in fd */
! 712: if (vflag >= 3)
! 713: fprintf(stderr,
! 714: "fd %d active", pfd[i].fd);
! 715: while (handle_connection(&psc[i], pfd[i].fd,
! 716: buf, Bflag) == -1) {
! 717: pfd[i] = pfd[nfds - 1];
! 718: pfd[nfds - 1].fd = -1;
! 719: psc[i] = psc[nfds - 1];
! 720: mainstats.nconns--;
! 721: nfds--;
! 722: /* stop display if no clients */
! 723: if (!mainstats.nconns) {
! 724: proc_slice = 1;
! 725: set_timer(0);
! 726: }
! 727: /* if we were full */
! 728: set_listening(pfd, lfds, 1);
! 729:
! 730: /* is there an event pending on the last fd? */
! 731: if (pfd[i].fd == -1 ||
! 732: (pfd[i].revents & POLLIN) == 0)
! 733: break;
1.1 djm 734: }
735: }
736: }
737: exit(1);
738: }
739:
740: static void __dead
1.11 ! claudio 741: clientloop(kvm_t *kvmh, u_long ktcbtab, const char *host, const char *port, int nconn)
1.1 djm 742: {
1.4 henning 743: struct addrinfo *aitop, *ai, hints;
1.11 ! claudio 744: struct statctx *psc;
1.4 henning 745: struct pollfd *pfd;
1.11 ! claudio 746: char tmp[128], *buf;
! 747: int i, r, herr, sock = -1;
! 748: u_int scnt = 0;
1.1 djm 749: ssize_t n;
750:
751: if ((buf = malloc(Bflag)) == NULL)
752: err(1, "malloc");
1.4 henning 753:
1.11 ! claudio 754: if ((pfd = calloc(nconn, sizeof(*pfd))) == NULL)
1.4 henning 755: err(1, "clientloop pfd calloc");
1.11 ! claudio 756: if ((psc = calloc(nconn, sizeof(*psc))) == NULL)
! 757: err(1, "clientloop psc calloc");
! 758:
1.4 henning 759: for (i = 0; i < nconn; i++) {
760: bzero(&hints, sizeof(hints));
761: hints.ai_socktype = SOCK_STREAM;
762: hints.ai_flags = 0;
763: if ((herr = getaddrinfo(host, port, &hints, &aitop)) != 0) {
764: if (herr == EAI_SYSTEM)
765: err(1, "getaddrinfo");
766: else
767: errx(1, "c getaddrinfo: %s", gai_strerror(herr));
768: }
769:
770: for (sock = -1, ai = aitop; ai != NULL; ai = ai->ai_next) {
771: saddr_ntop(ai->ai_addr, ai->ai_addrlen, tmp,
772: sizeof(tmp));
1.5 henning 773: if (vflag && scnt == 0)
1.4 henning 774: fprintf(stderr, "Trying %s\n", tmp);
775: if ((sock = socket(ai->ai_family, ai->ai_socktype,
776: ai->ai_protocol)) == -1) {
777: if (ai->ai_next == NULL)
778: err(1, "socket");
779: if (vflag)
780: warn("socket");
781: continue;
782: }
1.9 claudio 783: if (rdomain && ai->ai_family == AF_INET) {
784: if (setsockopt(sock, IPPROTO_IP, SO_RDOMAIN,
785: &rdomain, sizeof(rdomain)) == -1)
786: err(1, "setsockopt SO_RDOMAIN");
1.10 claudio 787: } else if (rdomain)
788: warnx("rdomain only supported on AF_INET");
1.4 henning 789: if (Sflag) {
790: if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
791: &Sflag, sizeof(Sflag)) == -1)
792: warn("set TCP send buffer size");
793: }
794: if (connect(sock, ai->ai_addr, ai->ai_addrlen) != 0) {
795: if (ai->ai_next == NULL)
796: err(1, "connect");
797: if (vflag)
798: warn("connect");
799: close(sock);
800: sock = -1;
801: continue;
802: }
803: break;
1.1 djm 804: }
1.4 henning 805: freeaddrinfo(aitop);
806: if (sock == -1)
807: errx(1, "No host found");
808:
809: if ((r = fcntl(sock, F_GETFL, 0)) == -1)
810: err(1, "fcntl(F_GETFL)");
811: r |= O_NONBLOCK;
812: if (fcntl(sock, F_SETFL, r) == -1)
813: err(1, "fcntl(F_SETFL, O_NONBLOCK)");
814:
815: pfd[i].fd = sock;
816: pfd[i].events = POLLOUT;
1.11 ! claudio 817: stats_prepare(psc + i, sock, kvmh, ktcbtab);
! 818: mainstats.nconns++;
1.5 henning 819: scnt++;
1.1 djm 820: }
821:
1.5 henning 822: if (vflag && scnt > 1)
823: fprintf(stderr, "%u connections established\n", scnt);
1.1 djm 824: arc4random_buf(buf, Bflag);
825:
1.11 ! claudio 826: print_header();
! 827: set_timer(1);
1.1 djm 828:
829: while (!done) {
1.11 ! claudio 830: if (proc_slice) {
! 831: process_slice(psc, scnt);
! 832: stats_cleanslice();
! 833: proc_slice = 0;
1.1 djm 834: }
1.4 henning 835: if (poll(pfd, nconn, INFTIM) == -1) {
1.1 djm 836: if (errno == EINTR)
837: continue;
838: err(1, "poll");
839: }
1.4 henning 840: for (i = 0; i < nconn; i++) {
1.7 henning 841: if (pfd[i].revents & POLLOUT) {
1.4 henning 842: if ((n = write(pfd[i].fd, buf, Bflag)) == -1) {
843: if (errno == EINTR || errno == EAGAIN)
844: continue;
845: err(1, "write");
846: }
1.7 henning 847: if (n == 0) {
848: warnx("Remote end closed connection");
849: done = -1;
850: break;
851: }
852: if (vflag >= 3)
853: fprintf(stderr, "write: %zd bytes\n",
854: n);
1.11 ! claudio 855: stats_update(psc + i, n);
1.4 henning 856: }
1.1 djm 857: }
858: }
1.11 ! claudio 859:
1.1 djm 860: if (done > 0)
861: warnx("Terminated by signal %d", done);
862:
863: free(buf);
864: close(sock);
865: exit(0);
866: }
867:
868: static void
869: drop_gid(void)
870: {
871: gid_t gid;
872:
873: gid = getgid();
874: if (setresgid(gid, gid, gid) == -1)
875: err(1, "setresgid");
876: }
877:
878: int
879: main(int argc, char **argv)
880: {
881: extern int optind;
882: extern char *optarg;
883:
884: char kerr[_POSIX2_LINE_MAX], *tmp;
1.11 ! claudio 885: struct addrinfo *aitop, hints;
1.1 djm 886: const char *errstr;
1.11 ! claudio 887: kvm_t *kvmh = NULL;
! 888: struct rlimit rl;
1.1 djm 889: int ch, herr;
890:
891: const char *host = NULL, *port = DEFAULT_PORT;
1.4 henning 892: int nconn = 1;
1.1 djm 893:
1.11 ! claudio 894: Bflag = DEFAULT_BUF;
! 895: Sflag = sflag = vflag = rdomain = 0;
! 896: kflag = NULL;
! 897: rflag = DEFAULT_STATS_INTERVAL;
! 898:
1.1 djm 899: struct nlist nl[] = { { "_tcbtable" }, { "" } };
900:
1.9 claudio 901: while ((ch = getopt(argc, argv, "B:hlk:n:p:r:sS:vV:")) != -1) {
1.1 djm 902: switch (ch) {
903: case 'l':
904: list_kvars();
905: exit(0);
906: case 'k':
907: if ((tmp = strdup(optarg)) == NULL)
908: errx(1, "strdup");
909: kflag = check_prepare_kvars(tmp);
910: free(tmp);
911: break;
912: case 'r':
913: rflag = strtonum(optarg, 0, 60 * 60 * 24 * 1000,
914: &errstr);
915: if (errstr != NULL)
916: errx(1, "statistics interval is %s: %s",
917: errstr, optarg);
918: break;
919: case 'p':
920: port = optarg;
921: break;
922: case 's':
923: sflag = 1;
924: break;
925: case 'S':
926: Sflag = strtonum(optarg, 0, 1024*1024*1024,
927: &errstr);
928: if (errstr != NULL)
929: errx(1, "receive space interval is %s: %s",
930: errstr, optarg);
931: break;
932: case 'B':
933: Bflag = strtonum(optarg, 0, 1024*1024*1024,
934: &errstr);
935: if (errstr != NULL)
936: errx(1, "read/write buffer size is %s: %s",
937: errstr, optarg);
938: break;
939: case 'v':
1.7 henning 940: vflag++;
1.9 claudio 941: break;
942: case 'V':
943: rdomain = (unsigned int)strtonum(optarg, 0,
944: RT_TABLEID_MAX, &errstr);
945: if (errstr)
946: errx(1, "rdomain value is %s: %s",
947: errstr, optarg);
1.1 djm 948: break;
1.4 henning 949: case 'n':
950: nconn = strtonum(optarg, 0, 65535, &errstr);
951: if (errstr != NULL)
952: errx(1, "number of connections is %s: %s",
953: errstr, optarg);
954: break;
1.1 djm 955: case 'h':
956: default:
957: usage();
958: }
959: }
960:
961: argv += optind;
962: argc -= optind;
963: if (argc != (sflag ? 0 : 1))
964: usage();
1.4 henning 965:
1.1 djm 966: if (!sflag)
967: host = argv[0];
968:
1.4 henning 969: if (sflag) {
970: bzero(&hints, sizeof(hints));
971: hints.ai_socktype = SOCK_STREAM;
972: hints.ai_flags = AI_PASSIVE;
973: if ((herr = getaddrinfo(host, port, &hints, &aitop)) != 0) {
974: if (herr == EAI_SYSTEM)
975: err(1, "getaddrinfo");
976: else
977: errx(1, "s getaddrinfo: %s", gai_strerror(herr));
978: }
1.1 djm 979: }
980:
981: if (kflag) {
982: if ((kvmh = kvm_openfiles(NULL, NULL, NULL,
983: O_RDONLY, kerr)) == NULL)
984: errx(1, "kvm_open: %s", kerr);
985: drop_gid();
986: if (kvm_nlist(kvmh, nl) < 0 || nl[0].n_type == 0)
987: errx(1, "kvm: no namelist");
988: } else
989: drop_gid();
990:
1.11 ! claudio 991: signal(SIGINT, exitsighand);
! 992: signal(SIGTERM, exitsighand);
! 993: signal(SIGHUP, exitsighand);
! 994: signal(SIGPIPE, SIG_IGN);
! 995: signal(SIGALRM, alarmhandler);
! 996:
! 997: if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
! 998: err(1, "getrlimit");
! 999: if (rl.rlim_cur < MAX_FD)
! 1000: rl.rlim_cur = MAX_FD;
! 1001: if (setrlimit(RLIMIT_NOFILE, &rl))
! 1002: err(1, "setrlimit");
! 1003: if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
! 1004: err(1, "getrlimit");
! 1005:
1.1 djm 1006: if (sflag)
1.11 ! claudio 1007: serverloop(kvmh, nl[0].n_value, aitop);
1.1 djm 1008: else
1.11 ! claudio 1009: clientloop(kvmh, nl[0].n_value, host, port, nconn);
1.1 djm 1010:
1011: return 0;
1012: }