From: claudio Date: Thu, 21 Jul 2016 10:11:11 +0000 (+0000) Subject: Add AF_UNIX support to tcpbench and also make it possible to randomize the X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=33ed67eefeb0702f621247bb857ca376edfa9f63;p=openbsd Add AF_UNIX support to tcpbench and also make it possible to randomize the write size in the client. pledge setup can be made tighter but that will be done in a second step. OK benno@, henning@, markus@ and some man page input by jmc@ --- diff --git a/usr.bin/tcpbench/tcpbench.1 b/usr.bin/tcpbench/tcpbench.1 index b1aa5612361..b0e11871751 100644 --- a/usr.bin/tcpbench/tcpbench.1 +++ b/usr.bin/tcpbench/tcpbench.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tcpbench.1,v 1.20 2014/08/19 03:28:53 dlg Exp $ +.\" $OpenBSD: tcpbench.1,v 1.21 2016/07/21 10:11:11 claudio Exp $ .\" .\" Copyright (c) 2008 Damien Miller .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: August 19 2014 $ +.Dd $Mdocdate: July 21 2016 $ .Dt TCPBENCH 1 .Os .Sh NAME @@ -24,7 +24,7 @@ .Nm .Fl l .Nm -.Op Fl 46uv +.Op Fl 46RUuv .Op Fl B Ar buf .Op Fl b Ar addr .Op Fl k Ar kvars @@ -39,7 +39,7 @@ .Nm .Bk -words .Fl s -.Op Fl 46uv +.Op Fl 46Uuv .Op Fl B Ar buf .Op Fl k Ar kvars .Op Fl p Ar port @@ -47,6 +47,7 @@ .Op Fl S Ar space .Op Fl T Ar toskeyword .Op Fl V Ar rtable +.Op Ar hostname .Ek .Sh DESCRIPTION .Nm @@ -105,6 +106,9 @@ Use the given number of TCP connections (default: 1). UDP is connectionless so this option isn't valid. .It Fl p Ar port Specify the port used for the test stream (default: 12345). +.It Fl R +In client mode the write buffer size is randomized up to the size specified via +.Fl B . .It Fl r Ar interval Specify the statistics interval reporting rate in milliseconds (default: 1000). .It Fl S Ar space @@ -138,6 +142,11 @@ or a number in either hex or decimal. Stop after .Ar secs seconds. +.It Fl U +Use AF_UNIX sockets instead of IPv4 or IPv6 sockets. +In client and server mode +.Ar hostname +is used as the path to the AF_UNIX socket. .It Fl u Use UDP instead of TCP; this must be specified on both the client and the server. diff --git a/usr.bin/tcpbench/tcpbench.c b/usr.bin/tcpbench/tcpbench.c index a1c058500a8..715618916b3 100644 --- a/usr.bin/tcpbench/tcpbench.c +++ b/usr.bin/tcpbench/tcpbench.c @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -66,6 +67,8 @@ struct { int Tflag; /* ToS if != -1 */ int vflag; /* Verbose */ int uflag; /* UDP mode */ + int Uflag; /* UNIX (AF_LOCAL) mode */ + int Rflag; /* randomize client write size */ kvm_t *kvmh; /* Kvm handler */ char **kvars; /* Kvm enabled vars */ u_long ktcbtab; /* Ktcb */ @@ -179,11 +182,11 @@ usage(void) { fprintf(stderr, "usage: tcpbench -l\n" - " tcpbench [-46uv] [-B buf] [-b addr] [-k kvars] [-n connections]\n" + " tcpbench [-46RUuv] [-B buf] [-b addr] [-k kvars] [-n connections]\n" " [-p port] [-r interval] [-S space] [-T toskeyword]\n" " [-t secs] [-V rtable] hostname\n" - " tcpbench -s [-46uv] [-B buf] [-k kvars] [-p port]\n" - " [-r interval] [-S space] [-T toskeyword] [-V rtable]\n"); + " tcpbench -s [-46Uuv] [-B buf] [-k kvars] [-p port] [-r interval]\n" + " [-S space] [-T toskeyword] [-V rtable] [hostname]\n"); exit(1); } @@ -212,6 +215,11 @@ saddr_ntop(const struct sockaddr *addr, socklen_t alen, char *buf, size_t len) char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV]; int herr; + if (addr->sa_family == AF_UNIX) { + struct sockaddr_un *sun = (struct sockaddr_un *)addr; + snprintf(buf, len, "%s", sun->sun_path); + return; + } if ((herr = getnameinfo(addr, alen, hbuf, sizeof(hbuf), pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV)) != 0) { if (herr == EAI_SYSTEM) @@ -805,7 +813,8 @@ server_init(struct addrinfo *aitop, struct statctx *udp_sc) fprintf(stderr, "bound to fd %d\n", sock); lnfds++; } - freeaddrinfo(aitop); + if (!ptb->Uflag) + freeaddrinfo(aitop); if (lnfds == 0) errx(1, "No working listen addresses found"); } @@ -815,8 +824,11 @@ client_handle_sc(int fd, short event, void *v_sc) { struct statctx *sc = v_sc; ssize_t n; + size_t blen = sc->buflen; - if ((n = write(sc->fd, sc->buf, sc->buflen)) == -1) { + if (ptb->Rflag) + blen = arc4random_uniform(blen) + 1; + if ((n = write(sc->fd, sc->buf, blen)) == -1) { if (errno == EINTR || errno == EWOULDBLOCK || (UDP_MODE && errno == ENOBUFS)) return; @@ -916,7 +928,8 @@ client_init(struct addrinfo *aitop, int nconn, struct statctx *udp_sc, if (mainstats.nconns == 1) set_slice_timer(1); } - freeaddrinfo(aitop); + if (!ptb->Uflag) + freeaddrinfo(aitop); if (aib != NULL) freeaddrinfo(aib); @@ -995,12 +1008,13 @@ main(int argc, char **argv) const char *host = NULL, *port = DEFAULT_PORT, *srcbind = NULL; struct event ev_sigint, ev_sigterm, ev_sighup, ev_progtimer; struct statctx *udp_sc = NULL; + struct sockaddr_un sock_un; /* Init world */ setvbuf(stdout, NULL, _IOLBF, 0); ptb = &tcpbench; ptb->dummybuf_len = 0; - ptb->Sflag = ptb->sflag = ptb->vflag = 0; + ptb->Sflag = ptb->sflag = ptb->vflag = ptb->Rflag = ptb->Uflag = 0; ptb->kvmh = NULL; ptb->kvars = NULL; ptb->rflag = DEFAULT_STATS_INTERVAL; @@ -1009,7 +1023,7 @@ main(int argc, char **argv) aib = NULL; secs = 0; - while ((ch = getopt(argc, argv, "46b:B:hlk:n:p:r:sS:t:T:uvV:")) != -1) { + while ((ch = getopt(argc, argv, "46b:B:hlk:n:p:Rr:sS:t:T:uUvV:")) != -1) { switch (ch) { case '4': family = PF_INET; @@ -1029,6 +1043,9 @@ main(int argc, char **argv) ptb->kvars = check_prepare_kvars(tmp); free(tmp); break; + case 'R': + ptb->Rflag = 1; + break; case 'r': ptb->rflag = strtonum(optarg, 0, 60 * 60 * 24 * 1000, &errstr); @@ -1077,6 +1094,9 @@ main(int argc, char **argv) case 'u': ptb->uflag = 1; break; + case 'U': + ptb->Uflag = 1; + break; case 'T': if (map_tos(optarg, &ptb->Tflag)) break; @@ -1102,12 +1122,12 @@ main(int argc, char **argv) } } - if (pledge("stdio rpath dns inet id proc", NULL) == -1) + if (pledge("stdio rpath dns inet unix id proc", NULL) == -1) err(1, "pledge"); argv += optind; argc -= optind; - if ((argc != (ptb->sflag ? 0 : 1)) || + if ((argc != (ptb->sflag && !ptb->Uflag ? 0 : 1)) || (UDP_MODE && (ptb->kvars || nconn != 1))) usage(); @@ -1122,10 +1142,10 @@ main(int argc, char **argv) } else drop_gid(); - if (pledge("stdio id dns inet", NULL) == -1) + if (pledge("stdio id dns inet unix", NULL) == -1) err(1, "pledge"); - if (!ptb->sflag) + if (!ptb->sflag || ptb->Uflag) host = argv[0]; /* * Rationale, @@ -1149,27 +1169,40 @@ main(int argc, char **argv) hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; } - if (ptb->sflag) - hints.ai_flags = AI_PASSIVE; - if (srcbind != NULL) { - hints.ai_flags |= AI_NUMERICHOST; - herr = getaddrinfo(srcbind, NULL, &hints, &aib); - hints.ai_flags &= ~AI_NUMERICHOST; - if (herr != 0) { + if (ptb->Uflag) { + hints.ai_family = AF_UNIX; + hints.ai_protocol = 0; + sock_un.sun_family = AF_UNIX; + if (strlcpy(sock_un.sun_path, host, sizeof(sock_un.sun_path)) >= + sizeof(sock_un.sun_path)) + errx(1, "socket name '%s' too long", host); + hints.ai_addr = (struct sockaddr *)&sock_un; + hints.ai_addrlen = sizeof(sock_un); + aitop = &hints; + } else { + if (ptb->sflag) + hints.ai_flags = AI_PASSIVE; + if (srcbind != NULL) { + hints.ai_flags |= AI_NUMERICHOST; + herr = getaddrinfo(srcbind, NULL, &hints, &aib); + hints.ai_flags &= ~AI_NUMERICHOST; + if (herr != 0) { + if (herr == EAI_SYSTEM) + err(1, "getaddrinfo"); + else + errx(1, "getaddrinfo: %s", + gai_strerror(herr)); + } + } + if ((herr = getaddrinfo(host, port, &hints, &aitop)) != 0) { if (herr == EAI_SYSTEM) err(1, "getaddrinfo"); else errx(1, "getaddrinfo: %s", gai_strerror(herr)); } } - if ((herr = getaddrinfo(host, port, &hints, &aitop)) != 0) { - if (herr == EAI_SYSTEM) - err(1, "getaddrinfo"); - else - errx(1, "getaddrinfo: %s", gai_strerror(herr)); - } - if (pledge("stdio id inet", NULL) == -1) + if (pledge("stdio id inet unix", NULL) == -1) err(1, "pledge"); if (getrlimit(RLIMIT_NOFILE, &rl) == -1) @@ -1181,7 +1214,7 @@ main(int argc, char **argv) if (getrlimit(RLIMIT_NOFILE, &rl) == -1) err(1, "getrlimit"); - if (pledge("stdio inet", NULL) == -1) + if (pledge("stdio inet unix", NULL) == -1) err(1, "pledge"); /* Init world */