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