Home | History | Annotate | Line # | Download | only in spray
spray.c revision 1.5
      1 /*	$NetBSD: spray.c,v 1.5 1997/10/17 13:39:12 lukem 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.5 1997/10/17 13:39:12 lukem 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 int	main __P((int, char **));
     50 void	print_xferstats __P((int, int, double));
     51 void	usage __P((void));
     52 
     53 /* spray buffer */
     54 char spray_buffer[SPRAYMAX];
     55 
     56 /* RPC timeouts */
     57 struct timeval NO_DEFAULT = { -1, -1 };
     58 struct timeval ONE_WAY = { 0, 0 };
     59 struct timeval TIMEOUT = { 25, 0 };
     60 
     61 int
     62 main(argc, argv)
     63 	int argc;
     64 	char **argv;
     65 {
     66 	char *progname;
     67 	spraycumul	host_stats;
     68 	sprayarr	host_array;
     69 	CLIENT *cl;
     70 	int c;
     71 	int i;
     72 	int count = 0;
     73 	int delay = 0;
     74 	int length = 0;
     75 	double xmit_time;			/* time to receive data */
     76 
     77 	progname = *argv;
     78 	while ((c = getopt(argc, argv, "c:d:l:")) != -1) {
     79 		switch (c) {
     80 		case 'c':
     81 			count = atoi(optarg);
     82 			break;
     83 		case 'd':
     84 			delay = atoi(optarg);
     85 			break;
     86 		case 'l':
     87 			length = atoi(optarg);
     88 			break;
     89 		default:
     90 			usage();
     91 			/* NOTREACHED */
     92 		}
     93 	}
     94 	argc -= optind;
     95 	argv += optind;
     96 
     97 	if (argc != 1) {
     98 		usage();
     99 		/* NOTREACHED */
    100 	}
    101 
    102 
    103 	/* Correct packet length. */
    104 	if (length > SPRAYMAX) {
    105 		length = SPRAYMAX;
    106 	} else if (length < SPRAYOVERHEAD) {
    107 		length = SPRAYOVERHEAD;
    108 	} else {
    109 		/* The RPC portion of the packet is a multiple of 32 bits. */
    110 		length -= SPRAYOVERHEAD - 3;
    111 		length &= ~3;
    112 		length += SPRAYOVERHEAD;
    113 	}
    114 
    115 
    116 	/*
    117 	 * The default value of count is the number of packets required
    118 	 * to make the total stream size 100000 bytes.
    119 	 */
    120 	if (!count) {
    121 		count = 100000 / length;
    122 	}
    123 
    124 	/* Initialize spray argument */
    125 	host_array.sprayarr_len = length - SPRAYOVERHEAD;
    126 	host_array.sprayarr_val = spray_buffer;
    127 
    128 
    129 	/* create connection with server */
    130 	cl = clnt_create(*argv, SPRAYPROG, SPRAYVERS, "udp");
    131 	if (cl == NULL) {
    132 		clnt_pcreateerror(progname);
    133 		exit(1);
    134 	}
    135 
    136 
    137 	/*
    138 	 * For some strange reason, RPC 4.0 sets the default timeout,
    139 	 * thus timeouts specified in clnt_call() are always ignored.
    140 	 *
    141 	 * The following (undocumented) hack resets the internal state
    142 	 * of the client handle.
    143 	 */
    144 	clnt_control(cl, CLSET_TIMEOUT, (caddr_t)&NO_DEFAULT);
    145 
    146 
    147 	/* Clear server statistics */
    148 	if (clnt_call(cl, SPRAYPROC_CLEAR, xdr_void, NULL, xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
    149 		clnt_perror(cl, progname);
    150 		exit(1);
    151 	}
    152 
    153 
    154 	/* Spray server with packets */
    155 	printf ("sending %d packets of lnth %d to %s ...", count, length, *argv);
    156 	fflush (stdout);
    157 
    158 	for (i = 0; i < count; i++) {
    159 		clnt_call(cl, SPRAYPROC_SPRAY, xdr_sprayarr, &host_array, xdr_void, NULL, ONE_WAY);
    160 
    161 		if (delay) {
    162 			usleep(delay);
    163 		}
    164 	}
    165 
    166 
    167 	/* Collect statistics from server */
    168 	if (clnt_call(cl, SPRAYPROC_GET, xdr_void, NULL, xdr_spraycumul, &host_stats, TIMEOUT) != RPC_SUCCESS) {
    169 		clnt_perror(cl, progname);
    170 		exit(1);
    171 	}
    172 
    173 	xmit_time = host_stats.clock.sec +
    174 			(host_stats.clock.usec / 1000000.0);
    175 
    176 	printf ("\n\tin %.2f seconds elapsed time\n", xmit_time);
    177 
    178 
    179 	/* report dropped packets */
    180 	if (host_stats.counter != count) {
    181 		int packets_dropped = count - host_stats.counter;
    182 
    183 		printf("\t%d packets (%.2f%%) dropped\n",
    184 			packets_dropped,
    185 			100.0 * packets_dropped / count );
    186 	} else {
    187 		printf("\tno packets dropped\n");
    188 	}
    189 
    190 	printf("Sent:");
    191 	print_xferstats(count, length, xmit_time);
    192 
    193 	printf("Rcvd:");
    194 	print_xferstats(host_stats.counter, length, xmit_time);
    195 
    196 	exit (0);
    197 }
    198 
    199 
    200 void
    201 print_xferstats(packets, packetlen, xfertime)
    202 	int packets;
    203 	int packetlen;
    204 	double xfertime;
    205 {
    206 	int datalen;
    207 	double pps;		/* packets per second */
    208 	double bps;		/* bytes per second */
    209 
    210 	datalen = packets * packetlen;
    211 	pps = packets / xfertime;
    212 	bps = datalen / xfertime;
    213 
    214 	printf("\t%.0f packets/sec, ", pps);
    215 
    216 	if (bps >= 1024)
    217 		printf ("%.1fK ", bps / 1024);
    218 	else
    219 		printf ("%.0f ", bps);
    220 
    221 	printf("bytes/sec\n");
    222 }
    223 
    224 
    225 void
    226 usage ()
    227 {
    228 	fprintf(stderr, "usage: spray [-c count] [-l length] [-d delay] host\n");
    229 	exit(1);
    230 }
    231