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 "$FreeBSD$"; 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 errx(1, "%s", clnt_spcreateerror(NULL)); 128 129 130 /* 131 * For some strange reason, RPC 4.0 sets the default timeout, 132 * thus timeouts specified in clnt_call() are always ignored. 133 * 134 * The following (undocumented) hack resets the internal state 135 * of the client handle. 136 */ 137 clnt_control(cl, CLSET_TIMEOUT, (caddr_t)&NO_DEFAULT); 138 139 140 /* Clear server statistics */ 141 if (clnt_call(cl, SPRAYPROC_CLEAR, xdr_void, NULL, xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) 142 errx(1, "%s", clnt_sperror(cl, NULL)); 143 144 145 /* Spray server with packets */ 146 printf ("sending %d packets of lnth %d to %s ...", count, length, *argv); 147 fflush (stdout); 148 149 for (i = 0; i < count; i++) { 150 clnt_call(cl, SPRAYPROC_SPRAY, xdr_sprayarr, &host_array, xdr_void, NULL, ONE_WAY); 151 152 if (delay) { 153 usleep(delay); 154 } 155 } 156 157 158 /* Collect statistics from server */ 159 if (clnt_call(cl, SPRAYPROC_GET, xdr_void, NULL, xdr_spraycumul, &host_stats, TIMEOUT) != RPC_SUCCESS) 160 errx(1, "%s", clnt_sperror(cl, NULL)); 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 void 190 print_xferstats(packets, packetlen, xfertime) 191 int packets; 192 int packetlen; 193 double xfertime; 194 { 195 int datalen; 196 double pps; /* packets per second */ 197 double bps; /* bytes per second */ 198 199 datalen = packets * packetlen; 200 pps = packets / xfertime; 201 bps = datalen / xfertime; 202 203 printf("\t%.0f packets/sec, ", pps); 204 205 if (bps >= 1024) 206 printf ("%.1fK ", bps / 1024); 207 else 208 printf ("%.0f ", bps); 209 210 printf("bytes/sec\n"); 211 } 212 213 214 static void 215 usage () 216 { 217 fprintf(stderr, 218 "usage: spray [-c count] [-l length] [-d delay] host\n"); 219 exit(1); 220 } 221