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