Home | History | Annotate | Line # | Download | only in spray
      1 /*	$NetBSD: spray.c,v 1.7 2011/08/30 20:45:31 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Winning Strategies, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Winning Strategies, Inc.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __RCSID("$NetBSD: spray.c,v 1.7 2011/08/30 20:45:31 joerg Exp $");
     36 #endif
     37 
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <unistd.h>
     41 
     42 #include <rpc/rpc.h>
     43 #include <rpcsvc/spray.h>
     44 
     45 #ifndef SPRAYOVERHEAD
     46 #define SPRAYOVERHEAD	86
     47 #endif
     48 
     49 static void	print_xferstats(int, int, double);
     50 __dead static void	usage(void);
     51 
     52 /* spray buffer */
     53 static char spray_buffer[SPRAYMAX];
     54 
     55 /* RPC timeouts */
     56 static struct timeval NO_DEFAULT = { -1, -1 };
     57 static struct timeval ONE_WAY = { 0, 0 };
     58 static struct timeval TIMEOUT = { 25, 0 };
     59 
     60 int
     61 main(int argc, char **argv)
     62 {
     63 	char *progname;
     64 	spraycumul	host_stats;
     65 	sprayarr	host_array;
     66 	CLIENT *cl;
     67 	int c;
     68 	int i;
     69 	int count = 0;
     70 	int delay = 0;
     71 	int length = 0;
     72 	double xmit_time;			/* time to receive data */
     73 
     74 	progname = *argv;
     75 	while ((c = getopt(argc, argv, "c:d:l:")) != -1) {
     76 		switch (c) {
     77 		case 'c':
     78 			count = atoi(optarg);
     79 			break;
     80 		case 'd':
     81 			delay = atoi(optarg);
     82 			break;
     83 		case 'l':
     84 			length = atoi(optarg);
     85 			break;
     86 		default:
     87 			usage();
     88 			/* NOTREACHED */
     89 		}
     90 	}
     91 	argc -= optind;
     92 	argv += optind;
     93 
     94 	if (argc != 1) {
     95 		usage();
     96 		/* NOTREACHED */
     97 	}
     98 
     99 
    100 	/* Correct packet length. */
    101 	if (length > SPRAYMAX) {
    102 		length = SPRAYMAX;
    103 	} else if (length < SPRAYOVERHEAD) {
    104 		length = SPRAYOVERHEAD;
    105 	} else {
    106 		/* The RPC portion of the packet is a multiple of 32 bits. */
    107 		length -= SPRAYOVERHEAD - 3;
    108 		length &= ~3;
    109 		length += SPRAYOVERHEAD;
    110 	}
    111 
    112 
    113 	/*
    114 	 * The default value of count is the number of packets required
    115 	 * to make the total stream size 100000 bytes.
    116 	 */
    117 	if (!count) {
    118 		count = 100000 / length;
    119 	}
    120 
    121 	/* Initialize spray argument */
    122 	host_array.sprayarr_len = length - SPRAYOVERHEAD;
    123 	host_array.sprayarr_val = spray_buffer;
    124 
    125 
    126 	/* create connection with server */
    127 	cl = clnt_create(*argv, SPRAYPROG, SPRAYVERS, "udp");
    128 	if (cl == NULL) {
    129 		clnt_pcreateerror(progname);
    130 		exit(1);
    131 	}
    132 
    133 
    134 	/*
    135 	 * For some strange reason, RPC 4.0 sets the default timeout,
    136 	 * thus timeouts specified in clnt_call() are always ignored.
    137 	 *
    138 	 * The following (undocumented) hack resets the internal state
    139 	 * of the client handle.
    140 	 */
    141 	clnt_control(cl, CLSET_TIMEOUT, (caddr_t)&NO_DEFAULT);
    142 
    143 
    144 	/* Clear server statistics */
    145 	if (clnt_call(cl, SPRAYPROC_CLEAR, xdr_void, NULL, xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
    146 		clnt_perror(cl, progname);
    147 		exit(1);
    148 	}
    149 
    150 
    151 	/* Spray server with packets */
    152 	printf ("sending %d packets of lnth %d to %s ...", count, length, *argv);
    153 	fflush (stdout);
    154 
    155 	for (i = 0; i < count; i++) {
    156 		clnt_call(cl, SPRAYPROC_SPRAY, xdr_sprayarr, &host_array, xdr_void, NULL, ONE_WAY);
    157 
    158 		if (delay) {
    159 			usleep(delay);
    160 		}
    161 	}
    162 
    163 
    164 	/* Collect statistics from server */
    165 	if (clnt_call(cl, SPRAYPROC_GET, xdr_void, NULL, xdr_spraycumul, &host_stats, TIMEOUT) != RPC_SUCCESS) {
    166 		clnt_perror(cl, progname);
    167 		exit(1);
    168 	}
    169 
    170 	xmit_time = host_stats.clock.sec +
    171 			(host_stats.clock.usec / 1000000.0);
    172 
    173 	printf ("\n\tin %.2f seconds elapsed time\n", xmit_time);
    174 
    175 
    176 	/* report dropped packets */
    177 	if (host_stats.counter != (unsigned)count) {
    178 		int packets_dropped = count - host_stats.counter;
    179 
    180 		printf("\t%d packets (%.2f%%) dropped\n",
    181 			packets_dropped,
    182 			100.0 * packets_dropped / count );
    183 	} else {
    184 		printf("\tno packets dropped\n");
    185 	}
    186 
    187 	printf("Sent:");
    188 	print_xferstats(count, length, xmit_time);
    189 
    190 	printf("Rcvd:");
    191 	print_xferstats(host_stats.counter, length, xmit_time);
    192 
    193 	exit (0);
    194 }
    195 
    196 
    197 static void
    198 print_xferstats(int packets, int packetlen, double xfertime)
    199 {
    200 	int datalen;
    201 	double pps;		/* packets per second */
    202 	double bps;		/* bytes per second */
    203 
    204 	datalen = packets * packetlen;
    205 	pps = packets / xfertime;
    206 	bps = datalen / xfertime;
    207 
    208 	printf("\t%.0f packets/sec, ", pps);
    209 
    210 	if (bps >= 1024)
    211 		printf ("%.1fK ", bps / 1024);
    212 	else
    213 		printf ("%.0f ", bps);
    214 
    215 	printf("bytes/sec\n");
    216 }
    217 
    218 static void
    219 usage(void)
    220 {
    221 	fprintf(stderr, "usage: spray [-c count] [-l length] [-d delay] host\n");
    222 	exit(1);
    223 }
    224