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