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