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