xref: /illumos-gate/usr/src/cmd/rpcsvc/spray.c (revision 201ceb75)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  *
2249e7ca49Speteh  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
267c478bd9Sstevel@tonic-gate /* All Rights Reserved */
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
297c478bd9Sstevel@tonic-gate  * The Regents of the University of California
307c478bd9Sstevel@tonic-gate  * All Rights Reserved
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
337c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
347c478bd9Sstevel@tonic-gate  * contributors.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <ctype.h>
407c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
417c478bd9Sstevel@tonic-gate #include <rpcsvc/spray.h>
427c478bd9Sstevel@tonic-gate #include <sys/poll.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate enum clnt_stat sprayproc_spray_1nd(/*argp, clnt*/);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	DEFBYTES	100000	/* default numbers of bytes to send */
477c478bd9Sstevel@tonic-gate #define	MAXPACKETLEN	1514
487c478bd9Sstevel@tonic-gate #define	SPRAYOVERHEAD	86	/* size of rpc packet when size=0 */
497c478bd9Sstevel@tonic-gate 
5049e7ca49Speteh static void slp(int usecs);
5149e7ca49Speteh static void usage(void);
5249e7ca49Speteh 
5349e7ca49Speteh int
main(int argc,char * argv[])5449e7ca49Speteh main(int argc, char *argv[])
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	int		c;
577c478bd9Sstevel@tonic-gate 	extern char	*optarg;
587c478bd9Sstevel@tonic-gate 	extern int	optind;
597c478bd9Sstevel@tonic-gate 	register CLIENT *clnt;
607c478bd9Sstevel@tonic-gate 	unsigned int	i;
617c478bd9Sstevel@tonic-gate 	int		delay = 0;
627c478bd9Sstevel@tonic-gate 	unsigned int	psec, bsec;
637c478bd9Sstevel@tonic-gate 	int		buf[SPRAYMAX/4];
647c478bd9Sstevel@tonic-gate 	char		msgbuf[256];
657c478bd9Sstevel@tonic-gate 	unsigned int	lnth, cnt;
667c478bd9Sstevel@tonic-gate 	sprayarr	arr;
677c478bd9Sstevel@tonic-gate 	spraycumul	cumul;
687c478bd9Sstevel@tonic-gate 	spraycumul	*co;
697c478bd9Sstevel@tonic-gate 	char		*host = NULL;
707c478bd9Sstevel@tonic-gate 	char		*type;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (argc < 2)
737c478bd9Sstevel@tonic-gate 		usage();
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	cnt = 0;
767c478bd9Sstevel@tonic-gate 	lnth = SPRAYOVERHEAD;
777c478bd9Sstevel@tonic-gate 	type = "netpath";
787c478bd9Sstevel@tonic-gate 	while (optind < argc) {
797c478bd9Sstevel@tonic-gate 		if (argv[optind][0] == '-') {
807c478bd9Sstevel@tonic-gate 			if ((c = getopt(argc, argv, "d:c:t:l:")) == EOF) {
817c478bd9Sstevel@tonic-gate 				break;
827c478bd9Sstevel@tonic-gate 			}
837c478bd9Sstevel@tonic-gate 			switch (c) {
847c478bd9Sstevel@tonic-gate 				case 'd':
857c478bd9Sstevel@tonic-gate 					delay = atoi(optarg);
867c478bd9Sstevel@tonic-gate 					break;
877c478bd9Sstevel@tonic-gate 				case 'c':
887c478bd9Sstevel@tonic-gate 					cnt = (unsigned int) atoi(optarg);
897c478bd9Sstevel@tonic-gate 					break;
907c478bd9Sstevel@tonic-gate 				case 't':
917c478bd9Sstevel@tonic-gate 					type = optarg;
927c478bd9Sstevel@tonic-gate 					break;
937c478bd9Sstevel@tonic-gate 				case 'l':
947c478bd9Sstevel@tonic-gate 					lnth = (unsigned int) atoi(optarg);
957c478bd9Sstevel@tonic-gate 					break;
967c478bd9Sstevel@tonic-gate 				default:
977c478bd9Sstevel@tonic-gate 					usage();
987c478bd9Sstevel@tonic-gate 			}
997c478bd9Sstevel@tonic-gate 		} else {
1007c478bd9Sstevel@tonic-gate 			host = argv[optind++];
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 	if (host == NULL)
1047c478bd9Sstevel@tonic-gate 		usage();
1057c478bd9Sstevel@tonic-gate 	clnt = clnt_create(host, SPRAYPROG, SPRAYVERS, type);
1067c478bd9Sstevel@tonic-gate 	if (clnt == (CLIENT *)NULL) {
1077c478bd9Sstevel@tonic-gate 		sprintf(msgbuf, "spray: cannot clnt_create %s:%s", host, type);
1087c478bd9Sstevel@tonic-gate 		clnt_pcreateerror(msgbuf);
1097c478bd9Sstevel@tonic-gate 		exit(1);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	if (cnt == 0)
1127c478bd9Sstevel@tonic-gate 		cnt = DEFBYTES/lnth;
1137c478bd9Sstevel@tonic-gate 	if (lnth < SPRAYOVERHEAD)
1147c478bd9Sstevel@tonic-gate 		lnth = SPRAYOVERHEAD;
1157c478bd9Sstevel@tonic-gate 	else if (lnth >= SPRAYMAX)
1167c478bd9Sstevel@tonic-gate 		lnth = SPRAYMAX;
1177c478bd9Sstevel@tonic-gate 	if (lnth <= MAXPACKETLEN && lnth % 4 != 2)
1187c478bd9Sstevel@tonic-gate 		lnth = ((lnth + 5) / 4) * 4 - 2;
1197c478bd9Sstevel@tonic-gate 	arr.sprayarr_len = lnth - SPRAYOVERHEAD;
12049e7ca49Speteh 	arr.sprayarr_val = (char *)buf;
1217c478bd9Sstevel@tonic-gate 	printf("sending %u packets of length %u to %s ...", cnt, lnth, host);
1227c478bd9Sstevel@tonic-gate 	fflush(stdout);
1237c478bd9Sstevel@tonic-gate 	if (sprayproc_clear_1(NULL, clnt) == NULL) {
1247c478bd9Sstevel@tonic-gate 		clnt_perror(clnt, "SPRAYPROC_CLEAR ");
1257c478bd9Sstevel@tonic-gate 		exit(1);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
1287c478bd9Sstevel@tonic-gate 		sprayproc_spray_1nd(&arr, clnt);
1297c478bd9Sstevel@tonic-gate 		if (delay > 0)
1307c478bd9Sstevel@tonic-gate 			slp(delay);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	if ((co = sprayproc_get_1(NULL, clnt)) == NULL) {
1337c478bd9Sstevel@tonic-gate 		clnt_perror(clnt, "SPRAYPROC_GET ");
1347c478bd9Sstevel@tonic-gate 		exit(1);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	cumul = *co;
1377c478bd9Sstevel@tonic-gate 	if (cumul.counter < cnt)
1387c478bd9Sstevel@tonic-gate 		printf("\n\t%d packets (%.3f%%) dropped by %s\n",
139*201ceb75SToomas Soome 		    cnt - cumul.counter,
140*201ceb75SToomas Soome 		    100.0 * (cnt - cumul.counter)/cnt, host);
1417c478bd9Sstevel@tonic-gate 	else
1427c478bd9Sstevel@tonic-gate 		printf("\n\tno packets dropped by %s\n", host);
143*201ceb75SToomas Soome 	psec = (1000000.0 * cumul.counter) /
144*201ceb75SToomas Soome 	    (1000000.0 * cumul.clock.sec + cumul.clock.usec);
145*201ceb75SToomas Soome 	bsec = (lnth * 1000000.0 * cumul.counter) /
146*201ceb75SToomas Soome 	    (1000000.0 * cumul.clock.sec + cumul.clock.usec);
1477c478bd9Sstevel@tonic-gate 	printf("\t%u packets/sec, %u bytes/sec\n", psec, bsec);
1487c478bd9Sstevel@tonic-gate 	exit(0);
1497c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * A special call, where the TIMEOUT is 0. So, every call times-out.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate static struct timeval TIMEOUT = { 0, 0 };
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate enum clnt_stat
sprayproc_spray_1nd(sprayarr * argp,CLIENT * clnt)158*201ceb75SToomas Soome sprayproc_spray_1nd(sprayarr *argp, CLIENT *clnt)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	return (clnt_call(clnt, SPRAYPROC_SPRAY, xdr_sprayarr, (caddr_t)argp,
161*201ceb75SToomas Soome 	    xdr_void, NULL, TIMEOUT));
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /* A cheap milliseconds sleep call */
16549e7ca49Speteh static void
slp(int usecs)166*201ceb75SToomas Soome slp(int usecs)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	static struct pollfd pfds[1] = {
1697c478bd9Sstevel@tonic-gate 		0, POLLIN, 0
1707c478bd9Sstevel@tonic-gate 	};
1717c478bd9Sstevel@tonic-gate 	pfds[0].fd = fileno(stdout);
1727c478bd9Sstevel@tonic-gate 	poll(pfds, 1, usecs/1000);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
17549e7ca49Speteh static void
usage()1767c478bd9Sstevel@tonic-gate usage()
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	printf("spray host [-t nettype] [-l lnth] [-c cnt] [-d delay]\n");
1797c478bd9Sstevel@tonic-gate 	exit(1);
1807c478bd9Sstevel@tonic-gate }
181