2 * Copyright (c) 1993 Winning Strategies, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
30 * $FreeBSD: src/usr.sbin/spray/spray.c,v 1.5.2.2 2001/07/30 10:23:00 dd Exp $
34 #include <rpcsvc/spray.h>
43 #define SPRAYOVERHEAD 86
46 static void usage(void);
47 static void print_xferstats(unsigned int, int, double);
48 static int getnum(const char *);
51 char spray_buffer[SPRAYMAX];
54 struct timeval NO_DEFAULT = { -1, -1 };
55 struct timeval ONE_WAY = { 0, 0 };
56 struct timeval TIMEOUT = { 25, 0 };
59 main(int argc, char *argv[])
61 spraycumul host_stats;
69 double xmit_time; /* time to receive data */
71 while ((c = getopt(argc, argv, "c:d:l:")) != -1) {
74 count = getnum(optarg);
77 delay = getnum(optarg);
80 length = getnum(optarg);
96 /* Correct packet length. */
97 if (length > SPRAYMAX) {
99 } else if (length < SPRAYOVERHEAD) {
100 length = SPRAYOVERHEAD;
102 /* The RPC portion of the packet is a multiple of 32 bits. */
103 length -= SPRAYOVERHEAD - 3;
105 length += SPRAYOVERHEAD;
110 * The default value of count is the number of packets required
111 * to make the total stream size 100000 bytes.
114 count = 100000 / length;
117 /* Initialize spray argument */
118 host_array.sprayarr_len = length - SPRAYOVERHEAD;
119 host_array.sprayarr_val = spray_buffer;
122 /* create connection with server */
123 if ((cl = clnt_create(*argv, SPRAYPROG, SPRAYVERS, "udp")) == NULL) {
124 clnt_pcreateerror(getprogname());
129 * For some strange reason, RPC 4.0 sets the default timeout,
130 * thus timeouts specified in clnt_call() are always ignored.
132 * The following (undocumented) hack resets the internal state
133 * of the client handle.
135 clnt_control(cl, CLSET_TIMEOUT, (caddr_t)&NO_DEFAULT);
138 /* Clear server statistics */
139 if (clnt_call(cl, SPRAYPROC_CLEAR, (xdrproc_t)xdr_void, NULL,
140 (xdrproc_t)xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
141 clnt_perror(cl, getprogname());
145 /* Spray server with packets */
146 printf ("sending %u packets of lnth %d to %s ...", count, length,
150 for (i = 0; i < count; i++) {
151 clnt_call(cl, SPRAYPROC_SPRAY, (xdrproc_t)xdr_sprayarr,
152 &host_array, (xdrproc_t)xdr_void, NULL, ONE_WAY);
160 /* Collect statistics from server */
161 if (clnt_call(cl, SPRAYPROC_GET, (xdrproc_t)xdr_void, NULL,
162 (xdrproc_t)xdr_spraycumul, &host_stats, TIMEOUT) != RPC_SUCCESS) {
163 clnt_perror(cl, getprogname());
167 xmit_time = host_stats.clock.sec +
168 (host_stats.clock.usec / 1000000.0);
170 printf ("\n\tin %.2f seconds elapsed time\n", xmit_time);
173 /* report dropped packets */
174 if (host_stats.counter != count) {
175 int packets_dropped = count - host_stats.counter;
177 printf("\t%d packets (%.2f%%) dropped\n",
179 100.0 * packets_dropped / count );
181 printf("\tno packets dropped\n");
185 print_xferstats(count, length, xmit_time);
188 print_xferstats(host_stats.counter, length, xmit_time);
196 print_xferstats(u_int packets, int packetlen, double xfertime)
199 double pps; /* packets per second */
200 double bps; /* bytes per second */
202 datalen = packets * packetlen;
203 pps = packets / xfertime;
204 bps = datalen / xfertime;
206 printf("\t%.0f packets/sec, ", pps);
209 printf ("%.1fK ", bps / 1024);
211 printf ("%.0f ", bps);
213 printf("bytes/sec\n");
217 getnum(const char *arg)
222 tmp = strtol(arg, &ep, 10);
223 if (*ep != '\0' || tmp < INT_MIN || tmp > INT_MAX)
224 errx(1, "invalid value: %s", arg);
233 "usage: spray [-c count] [-l length] [-d delay] host\n");