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