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