xref: /illumos-gate/usr/src/cmd/rpcsvc/spray.c (revision 49e7ca4919cec3229f6fab9730bafc7cf24dab23)
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  *
22*49e7ca49Speteh  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
437c478bd9Sstevel@tonic-gate #include <rpcsvc/spray.h>
447c478bd9Sstevel@tonic-gate #include <sys/poll.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate enum clnt_stat sprayproc_spray_1nd(/*argp, clnt*/);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	DEFBYTES	100000	/* default numbers of bytes to send */
497c478bd9Sstevel@tonic-gate #define	MAXPACKETLEN	1514
507c478bd9Sstevel@tonic-gate #define	SPRAYOVERHEAD	86	/* size of rpc packet when size=0 */
517c478bd9Sstevel@tonic-gate 
52*49e7ca49Speteh static void slp(int usecs);
53*49e7ca49Speteh static void usage(void);
54*49e7ca49Speteh 
55*49e7ca49Speteh int
56*49e7ca49Speteh main(int argc, char *argv[])
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	int		c;
597c478bd9Sstevel@tonic-gate 	extern char	*optarg;
607c478bd9Sstevel@tonic-gate 	extern int	optind;
617c478bd9Sstevel@tonic-gate 	register CLIENT *clnt;
627c478bd9Sstevel@tonic-gate 	unsigned int	i;
637c478bd9Sstevel@tonic-gate 	int		delay = 0;
647c478bd9Sstevel@tonic-gate 	unsigned int	psec, bsec;
657c478bd9Sstevel@tonic-gate 	int		buf[SPRAYMAX/4];
667c478bd9Sstevel@tonic-gate 	char		msgbuf[256];
677c478bd9Sstevel@tonic-gate 	unsigned int	lnth, cnt;
687c478bd9Sstevel@tonic-gate 	sprayarr	arr;
697c478bd9Sstevel@tonic-gate 	spraycumul	cumul;
707c478bd9Sstevel@tonic-gate 	spraycumul	*co;
717c478bd9Sstevel@tonic-gate 	char		*host = NULL;
727c478bd9Sstevel@tonic-gate 	char		*type;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (argc < 2)
757c478bd9Sstevel@tonic-gate 		usage();
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	cnt = 0;
787c478bd9Sstevel@tonic-gate 	lnth = SPRAYOVERHEAD;
797c478bd9Sstevel@tonic-gate 	type = "netpath";
807c478bd9Sstevel@tonic-gate 	while (optind < argc) {
817c478bd9Sstevel@tonic-gate 		if (argv[optind][0] == '-') {
827c478bd9Sstevel@tonic-gate 			if ((c = getopt(argc, argv, "d:c:t:l:")) == EOF) {
837c478bd9Sstevel@tonic-gate 				break;
847c478bd9Sstevel@tonic-gate 			}
857c478bd9Sstevel@tonic-gate 			switch (c) {
867c478bd9Sstevel@tonic-gate 				case 'd':
877c478bd9Sstevel@tonic-gate 					delay = atoi(optarg);
887c478bd9Sstevel@tonic-gate 					break;
897c478bd9Sstevel@tonic-gate 				case 'c':
907c478bd9Sstevel@tonic-gate 					cnt = (unsigned int) atoi(optarg);
917c478bd9Sstevel@tonic-gate 					break;
927c478bd9Sstevel@tonic-gate 				case 't':
937c478bd9Sstevel@tonic-gate 					type = optarg;
947c478bd9Sstevel@tonic-gate 					break;
957c478bd9Sstevel@tonic-gate 				case 'l':
967c478bd9Sstevel@tonic-gate 					lnth = (unsigned int) atoi(optarg);
977c478bd9Sstevel@tonic-gate 					break;
987c478bd9Sstevel@tonic-gate 				default:
997c478bd9Sstevel@tonic-gate 					usage();
1007c478bd9Sstevel@tonic-gate 			}
1017c478bd9Sstevel@tonic-gate 		} else {
1027c478bd9Sstevel@tonic-gate 			host = argv[optind++];
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	if (host == NULL)
1067c478bd9Sstevel@tonic-gate 		usage();
1077c478bd9Sstevel@tonic-gate 	clnt = clnt_create(host, SPRAYPROG, SPRAYVERS, type);
1087c478bd9Sstevel@tonic-gate 	if (clnt == (CLIENT *)NULL) {
1097c478bd9Sstevel@tonic-gate 		sprintf(msgbuf, "spray: cannot clnt_create %s:%s", host, type);
1107c478bd9Sstevel@tonic-gate 		clnt_pcreateerror(msgbuf);
1117c478bd9Sstevel@tonic-gate 		exit(1);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	if (cnt == 0)
1147c478bd9Sstevel@tonic-gate 		cnt = DEFBYTES/lnth;
1157c478bd9Sstevel@tonic-gate 	if (lnth < SPRAYOVERHEAD)
1167c478bd9Sstevel@tonic-gate 		lnth = SPRAYOVERHEAD;
1177c478bd9Sstevel@tonic-gate 	else if (lnth >= SPRAYMAX)
1187c478bd9Sstevel@tonic-gate 		lnth = SPRAYMAX;
1197c478bd9Sstevel@tonic-gate 	if (lnth <= MAXPACKETLEN && lnth % 4 != 2)
1207c478bd9Sstevel@tonic-gate 		lnth = ((lnth + 5) / 4) * 4 - 2;
1217c478bd9Sstevel@tonic-gate 	arr.sprayarr_len = lnth - SPRAYOVERHEAD;
1227c478bd9Sstevel@tonic-gate 	arr.sprayarr_val = (char *)buf;
1237c478bd9Sstevel@tonic-gate 	printf("sending %u packets of length %u to %s ...", cnt, lnth, host);
1247c478bd9Sstevel@tonic-gate 	fflush(stdout);
1257c478bd9Sstevel@tonic-gate 	if (sprayproc_clear_1(NULL, clnt) == NULL) {
1267c478bd9Sstevel@tonic-gate 		clnt_perror(clnt, "SPRAYPROC_CLEAR ");
1277c478bd9Sstevel@tonic-gate 		exit(1);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
1307c478bd9Sstevel@tonic-gate 		sprayproc_spray_1nd(&arr, clnt);
1317c478bd9Sstevel@tonic-gate 		if (delay > 0)
1327c478bd9Sstevel@tonic-gate 			slp(delay);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	if ((co = sprayproc_get_1(NULL, clnt)) == NULL) {
1357c478bd9Sstevel@tonic-gate 		clnt_perror(clnt, "SPRAYPROC_GET ");
1367c478bd9Sstevel@tonic-gate 		exit(1);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	cumul = *co;
1397c478bd9Sstevel@tonic-gate 	if (cumul.counter < cnt)
1407c478bd9Sstevel@tonic-gate 		printf("\n\t%d packets (%.3f%%) dropped by %s\n",
1417c478bd9Sstevel@tonic-gate 			cnt - cumul.counter,
1427c478bd9Sstevel@tonic-gate 			100.0 * (cnt - cumul.counter)/cnt, host);
1437c478bd9Sstevel@tonic-gate 	else
1447c478bd9Sstevel@tonic-gate 		printf("\n\tno packets dropped by %s\n", host);
1457c478bd9Sstevel@tonic-gate 	psec = (1000000.0 * cumul.counter)
1467c478bd9Sstevel@tonic-gate 		/ (1000000.0 * cumul.clock.sec + cumul.clock.usec);
1477c478bd9Sstevel@tonic-gate 	bsec = (lnth * 1000000.0 * cumul.counter)/
1487c478bd9Sstevel@tonic-gate 		(1000000.0 * cumul.clock.sec + cumul.clock.usec);
1497c478bd9Sstevel@tonic-gate 	printf("\t%u packets/sec, %u bytes/sec\n", psec, bsec);
1507c478bd9Sstevel@tonic-gate 	exit(0);
1517c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * A special call, where the TIMEOUT is 0. So, every call times-out.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate static struct timeval TIMEOUT = { 0, 0 };
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate enum clnt_stat
1607c478bd9Sstevel@tonic-gate sprayproc_spray_1nd(argp, clnt)
1617c478bd9Sstevel@tonic-gate 	sprayarr *argp;
1627c478bd9Sstevel@tonic-gate 	CLIENT *clnt;
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	return (clnt_call(clnt, SPRAYPROC_SPRAY, xdr_sprayarr, (caddr_t)argp,
1657c478bd9Sstevel@tonic-gate 			xdr_void, NULL, TIMEOUT));
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* A cheap milliseconds sleep call */
169*49e7ca49Speteh static void
1707c478bd9Sstevel@tonic-gate slp(usecs)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	static struct pollfd pfds[1] = {
1737c478bd9Sstevel@tonic-gate 		0, POLLIN, 0
1747c478bd9Sstevel@tonic-gate 	};
1757c478bd9Sstevel@tonic-gate 	pfds[0].fd = fileno(stdout);
1767c478bd9Sstevel@tonic-gate 	poll(pfds, 1, usecs/1000);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
179*49e7ca49Speteh static void
1807c478bd9Sstevel@tonic-gate usage()
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	printf("spray host [-t nettype] [-l lnth] [-c cnt] [-d delay]\n");
1837c478bd9Sstevel@tonic-gate 	exit(1);
1847c478bd9Sstevel@tonic-gate }
185