Claudio Jeker(cje...@diehard.n-r-g.com) on 2016.07.20 19:08:51 +0200:
> On Wed, Jul 20, 2016 at 04:09:48PM +0200, Claudio Jeker wrote:
> > For testing I want to abuse tcpbench to work over AF_UNIX sockets.
> > This diff does exactly that with minimal extras. Especially the unix
> > socket is not removed from the filesystem when closed. I don't want to
> > add pledge cpath to tcpbench just for that.
> > 
> 
> New version that documents the -R feature that I sneaked in (without
> realizing) and also fixes the documentation for -U a bit.
> I added -R some time ago to stress test different mbuf sizes. tcpbench is
> a test tool for me :)

ich habe es kompiliert und getestet.

ok, jawohl.

> 
> -- 
> :wq Claudio
> 
> Index: tcpbench.1
> ===================================================================
> RCS file: /cvs/src/usr.bin/tcpbench/tcpbench.1,v
> retrieving revision 1.20
> diff -u -p -r1.20 tcpbench.1
> --- tcpbench.1        19 Aug 2014 03:28:53 -0000      1.20
> +++ tcpbench.1        20 Jul 2016 16:11:44 -0000
> @@ -24,7 +24,7 @@
>  .Nm
>  .Fl l
>  .Nm
> -.Op Fl 46uv
> +.Op Fl 46UuRv
>  .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 
>  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.
> Index: tcpbench.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/tcpbench/tcpbench.c,v
> retrieving revision 1.50
> diff -u -p -r1.50 tcpbench.c
> --- tcpbench.c        6 Apr 2016 14:09:06 -0000       1.50
> +++ tcpbench.c        20 Jul 2016 16:05:15 -0000
> @@ -21,6 +21,7 @@
>  #include <sys/socketvar.h>
>  #include <sys/resource.h>
>  #include <sys/queue.h>
> +#include <sys/un.h>
>  
>  #include <net/route.h>
>  
> @@ -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 [-46UuRv] [-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, 
>       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, stru
>                       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, vo
>  {
>       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 
>               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 */
> 

-- 

Reply via email to