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