xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/syncloop.c (revision 3628:98d9a6c27bd3)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3628Sss150715  * Common Development and Distribution License (the "License").
6*3628Sss150715  *  You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3628Sss150715  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Synchronous loop-back test program
300Sstevel@tonic-gate  * For installation verification of synchronous lines and facilities
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <sys/ioctl.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <sys/time.h>
380Sstevel@tonic-gate #include <sys/file.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <sys/stream.h>
450Sstevel@tonic-gate #include <sys/stropts.h>
460Sstevel@tonic-gate #include <sys/poll.h>
470Sstevel@tonic-gate #include <sys/ser_sync.h>
480Sstevel@tonic-gate #include <libdlpi.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static void Usage(void);
510Sstevel@tonic-gate static void quiet_period(void);
520Sstevel@tonic-gate static void first_packet();
530Sstevel@tonic-gate static void many_packets();
540Sstevel@tonic-gate static void printhex(char *cp, int len);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static unsigned int speed = 9600;
570Sstevel@tonic-gate static int reccount = 100;
580Sstevel@tonic-gate static int reclen = 100;
590Sstevel@tonic-gate static char loopstr[MAX_INPUT];
600Sstevel@tonic-gate static int looptype = 0;
610Sstevel@tonic-gate static int loopchange = 0;
620Sstevel@tonic-gate static int clockchange = 0;
630Sstevel@tonic-gate static int cfd, dfd;		/* control and data descriptors */
640Sstevel@tonic-gate static int data = -1;
650Sstevel@tonic-gate static int verbose = 0;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static char *yesno[] = {
680Sstevel@tonic-gate 	"no",
690Sstevel@tonic-gate 	"yes",
700Sstevel@tonic-gate 	"silent",
710Sstevel@tonic-gate 	0,
720Sstevel@tonic-gate };
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static char *txnames[] = {
750Sstevel@tonic-gate 	"txc",
760Sstevel@tonic-gate 	"rxc",
770Sstevel@tonic-gate 	"baud",
780Sstevel@tonic-gate 	"pll",
790Sstevel@tonic-gate 	"sysclk",
800Sstevel@tonic-gate 	"-txc",
810Sstevel@tonic-gate 	0,
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static char *rxnames[] = {
850Sstevel@tonic-gate 	"rxc",
860Sstevel@tonic-gate 	"txc",
870Sstevel@tonic-gate 	"baud",
880Sstevel@tonic-gate 	"pll",
890Sstevel@tonic-gate 	"sysclk",
900Sstevel@tonic-gate 	"-rxc",
910Sstevel@tonic-gate 	0,
920Sstevel@tonic-gate };
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #define	MAXPACKET	4096
950Sstevel@tonic-gate 
960Sstevel@tonic-gate int
main(int argc,char ** argv)970Sstevel@tonic-gate main(int argc, char **argv)
980Sstevel@tonic-gate {
99*3628Sss150715 	char *portname;
100*3628Sss150715 	char dnambuf[MAXPATHLEN], *cp;
101*3628Sss150715 	char device[DLPI_LINKNAME_MAX];
1020Sstevel@tonic-gate 	struct scc_mode sm;
1030Sstevel@tonic-gate 	struct strioctl sioc;
104*3628Sss150715 	uint_t ppa;
1050Sstevel@tonic-gate 	char *devstr = "/dev/";
1060Sstevel@tonic-gate 	int devstrlen;
107*3628Sss150715 	int retval;
108*3628Sss150715 	dlpi_handle_t dh;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	argc--;
1110Sstevel@tonic-gate 	argv++;
1120Sstevel@tonic-gate 	while (argc > 0 && argv[0][0] == '-')
1130Sstevel@tonic-gate 		switch (argv[0][1]) {
1140Sstevel@tonic-gate 		case 'c':	/* rec count */
1150Sstevel@tonic-gate 			if (argc < 2)
1160Sstevel@tonic-gate 				Usage();
1170Sstevel@tonic-gate 			reccount = atoi(argv[1]);
1180Sstevel@tonic-gate 			argc -= 2;
1190Sstevel@tonic-gate 			argv += 2;
1200Sstevel@tonic-gate 			break;
1210Sstevel@tonic-gate 		case 'd':
1220Sstevel@tonic-gate 			if (sscanf(argv[1], "%x", (uint_t *)&data) != 1)
1230Sstevel@tonic-gate 				Usage();
1240Sstevel@tonic-gate 			argc -= 2;
1250Sstevel@tonic-gate 			argv += 2;
1260Sstevel@tonic-gate 			break;
1270Sstevel@tonic-gate 		case 'l':	/* rec length */
1280Sstevel@tonic-gate 			if (argc < 2)
1290Sstevel@tonic-gate 				Usage();
1300Sstevel@tonic-gate 			reclen = atoi(argv[1]);
1310Sstevel@tonic-gate 			argc -= 2;
1320Sstevel@tonic-gate 			argv += 2;
1330Sstevel@tonic-gate 			break;
1340Sstevel@tonic-gate 		case 's':	/* line speed */
1350Sstevel@tonic-gate 			if (argc < 2)
1360Sstevel@tonic-gate 				Usage();
1370Sstevel@tonic-gate 			speed = atoi(argv[1]);
1380Sstevel@tonic-gate 			argc -= 2;
1390Sstevel@tonic-gate 			argv += 2;
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 		case 't':	/* test type */
1420Sstevel@tonic-gate 			if (argc < 2)
1430Sstevel@tonic-gate 				Usage();
1440Sstevel@tonic-gate 			looptype = atoi(argv[1]);
1450Sstevel@tonic-gate 			argc -= 2;
1460Sstevel@tonic-gate 			argv += 2;
1470Sstevel@tonic-gate 			break;
1480Sstevel@tonic-gate 		case 'v':
1490Sstevel@tonic-gate 			verbose = 1;
1500Sstevel@tonic-gate 			argc--;
1510Sstevel@tonic-gate 			argv++;
1520Sstevel@tonic-gate 			break;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 	if (argc != 1)
1550Sstevel@tonic-gate 		Usage();
1560Sstevel@tonic-gate 	portname = argv[0];
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	devstrlen = strlen(devstr);
1590Sstevel@tonic-gate 	if (strncmp(devstr, portname, devstrlen) != 0) {
1600Sstevel@tonic-gate 		if (snprintf(dnambuf, sizeof (dnambuf), "%s%s", devstr,
1610Sstevel@tonic-gate 		    portname) >= sizeof (dnambuf)) {
1620Sstevel@tonic-gate 			(void) fprintf(stderr,
1630Sstevel@tonic-gate 			    "syncloop: invalid device name (too long) %s\n",
1640Sstevel@tonic-gate 			    portname);
1650Sstevel@tonic-gate 			exit(1);
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	dfd = open(dnambuf, O_RDWR);
1700Sstevel@tonic-gate 	if (dfd < 0) {
1710Sstevel@tonic-gate 		(void) fprintf(stderr, "syncloop: cannot open %s\n", dnambuf);
1720Sstevel@tonic-gate 		perror(dnambuf);
1730Sstevel@tonic-gate 		exit(1);
1740Sstevel@tonic-gate 	}
175*3628Sss150715 
176*3628Sss150715 	cp = portname;
177*3628Sss150715 	while (*cp)			/* find the end of the name */
178*3628Sss150715 		cp++;
179*3628Sss150715 	cp--;
180*3628Sss150715 	if (!isdigit(*cp)) {
1810Sstevel@tonic-gate 		(void) fprintf(stderr,
182*3628Sss150715 		    "syncloop: %s missing minor device number\n", portname);
1830Sstevel@tonic-gate 		exit(1);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
186*3628Sss150715 	if (strlen(portname) >= DLPI_LINKNAME_MAX) {
187*3628Sss150715 		(void) fprintf(stderr,
188*3628Sss150715 		    "syncloop: invalid device name (too long) %s\n",
189*3628Sss150715 		    portname);
1900Sstevel@tonic-gate 		exit(1);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
193*3628Sss150715 	if ((retval = dlpi_open(portname, &dh, DLPI_SERIAL)) != DLPI_SUCCESS) {
194*3628Sss150715 		(void) fprintf(stderr, "syncloop: dlpi_open %s: %s\n", portname,
195*3628Sss150715 		    dlpi_strerror(retval));
196*3628Sss150715 		exit(1);
197*3628Sss150715 	}
198*3628Sss150715 
199*3628Sss150715 	(void) dlpi_parselink(portname, device, &ppa);
200*3628Sss150715 
2010Sstevel@tonic-gate 	if (reclen < 0 || reclen > MAXPACKET) {
2020Sstevel@tonic-gate 		(void) printf("invalid packet length: %d\n", reclen);
2030Sstevel@tonic-gate 		exit(1);
2040Sstevel@tonic-gate 	}
205*3628Sss150715 	(void) printf("[ Data device: %s | Control device: %s, ppa=%u ]\n",
206*3628Sss150715 		dnambuf, device, ppa);
207*3628Sss150715 
208*3628Sss150715 	cfd = dlpi_fd(dh);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
2110Sstevel@tonic-gate 	sioc.ic_timout = -1;
2120Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
2130Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
2140Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
2150Sstevel@tonic-gate 		perror("S_IOCGETMODE");
216*3628Sss150715 		(void) fprintf(stderr, "syncloop: can't get sync mode info "
217*3628Sss150715 		    "for %s\n", portname);
2180Sstevel@tonic-gate 		exit(1);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 	while (looptype < 1 || looptype > 4) {
2210Sstevel@tonic-gate 		(void) printf("Enter test type:\n");
2220Sstevel@tonic-gate 		(void) printf("1: Internal Test\n");
2230Sstevel@tonic-gate 		(void) printf(
2240Sstevel@tonic-gate "            (internal data loop, internal clocking)\n");
2250Sstevel@tonic-gate 		(void) printf("2: Test using loopback plugs\n");
2260Sstevel@tonic-gate 		(void) printf(
2270Sstevel@tonic-gate "            (external data loop, internal clocking)\n");
2280Sstevel@tonic-gate 		(void) printf("3: Test using local or remote modem loopback\n");
2290Sstevel@tonic-gate 		(void) printf(
2300Sstevel@tonic-gate "            (external data loop, external clocking)\n");
2310Sstevel@tonic-gate 		(void) printf("4: Other, previously set, special mode\n");
2320Sstevel@tonic-gate 		(void) printf("> "); (void) fflush(stdout);
2330Sstevel@tonic-gate 		(void) fgets(loopstr, sizeof (loopstr), stdin);
2340Sstevel@tonic-gate 		(void) sscanf(loopstr, "%d", &looptype);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	switch (looptype) {
2370Sstevel@tonic-gate 	case 1:
2380Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_BAUD) ||
2390Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_BAUD))
2400Sstevel@tonic-gate 			clockchange++;
2410Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_BAUD;
2420Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_BAUD;
2430Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) == 0)
2440Sstevel@tonic-gate 			loopchange++;
2450Sstevel@tonic-gate 		sm.sm_config |= CONN_LPBK;
2460Sstevel@tonic-gate 		break;
2470Sstevel@tonic-gate 	case 2:
2480Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_BAUD) ||
2490Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_RXC))
2500Sstevel@tonic-gate 			clockchange++;
2510Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_BAUD;
2520Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_RXC;
2530Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) != 0)
2540Sstevel@tonic-gate 			loopchange++;
2550Sstevel@tonic-gate 		sm.sm_config &= ~CONN_LPBK;
2560Sstevel@tonic-gate 		break;
2570Sstevel@tonic-gate 	case 3:
2580Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_TXC) ||
2590Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_RXC))
2600Sstevel@tonic-gate 			clockchange++;
2610Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_TXC;
2620Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_RXC;
2630Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) != 0)
2640Sstevel@tonic-gate 			loopchange++;
2650Sstevel@tonic-gate 		sm.sm_config &= ~CONN_LPBK;
2660Sstevel@tonic-gate 		break;
2670Sstevel@tonic-gate 	case 4:
2680Sstevel@tonic-gate 		goto no_params;
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	sm.sm_baudrate = speed;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCSETMODE;
2740Sstevel@tonic-gate 	sioc.ic_timout = -1;
2750Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
2760Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
2770Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
2780Sstevel@tonic-gate 		perror("S_IOCSETMODE");
2790Sstevel@tonic-gate 		(void) fprintf(stderr,
280*3628Sss150715 		    "syncloop: can't set sync mode info for %s\n", portname);
2810Sstevel@tonic-gate 		exit(1);
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate no_params:
2850Sstevel@tonic-gate 	/* report state */
2860Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
2870Sstevel@tonic-gate 	sioc.ic_timout = -1;
2880Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
2890Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
2900Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
2910Sstevel@tonic-gate 		perror("S_IOCGETMODE");
292*3628Sss150715 		(void) fprintf(stderr, "syncloop: can't get sync mode info "
293*3628Sss150715 			"for %s\n", portname);
2940Sstevel@tonic-gate 		exit(1);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 	(void) printf("speed=%d, loopback=%s, nrzi=%s, txc=%s, rxc=%s\n",
2970Sstevel@tonic-gate 		sm.sm_baudrate,
2980Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_LPBK) > 0)],
2990Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_NRZI) > 0)],
3000Sstevel@tonic-gate 		txnames[sm.sm_txclock],
3010Sstevel@tonic-gate 		rxnames[sm.sm_rxclock]);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	quiet_period();
3040Sstevel@tonic-gate 	first_packet();
3050Sstevel@tonic-gate 	many_packets();
3060Sstevel@tonic-gate 	return (0);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate static void
Usage()3100Sstevel@tonic-gate Usage()
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	(void) printf("Usage: syncloop [ options ] portname\n");
3130Sstevel@tonic-gate 	(void) printf("Options: -c packet_count\n");
3140Sstevel@tonic-gate 	(void) printf("         -l packet_length\n");
3150Sstevel@tonic-gate 	(void) printf("         -s line_speed\n");
3160Sstevel@tonic-gate 	(void) printf("         -t test_type\n");
3170Sstevel@tonic-gate 	(void) printf("         -d hex_data_byte\n");
3180Sstevel@tonic-gate 	exit(1);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate static int zero_time = 0;
3220Sstevel@tonic-gate static int short_time = 1000;
3230Sstevel@tonic-gate static int long_time = 4000;
3240Sstevel@tonic-gate static char bigbuf[4096];
3250Sstevel@tonic-gate static char packet[MAXPACKET];
3260Sstevel@tonic-gate static struct pollfd pfd;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate static void
quiet_period()3290Sstevel@tonic-gate quiet_period()
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	(void) printf("[ checking for quiet line ]\n");
3320Sstevel@tonic-gate 	pfd.fd = dfd;
3330Sstevel@tonic-gate 	pfd.events = POLLIN;
3340Sstevel@tonic-gate 	pfd.revents = 0;
3350Sstevel@tonic-gate 	while (poll(&pfd, 1, short_time) == 1) {
3360Sstevel@tonic-gate 		(void) read(dfd, bigbuf, sizeof (bigbuf));
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 	if (poll(&pfd, 1, long_time) == 1) {
3390Sstevel@tonic-gate 		(void) printf("packet received but none sent!\n");
3400Sstevel@tonic-gate 		(void) printf("quiesce other end before starting syncloop\n");
3410Sstevel@tonic-gate 		exit(1);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate static void
first_packet()3460Sstevel@tonic-gate first_packet()
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	int i, len;
3490Sstevel@tonic-gate 	int pollret;
3500Sstevel@tonic-gate 	struct strioctl sioc;
3510Sstevel@tonic-gate 	struct sl_stats start_stats, end_stats;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	for (i = 0; i < reclen; i++)
3540Sstevel@tonic-gate 		packet[i] = (data == -1) ? rand() : data;
3550Sstevel@tonic-gate 	(void) printf("[ Trying first packet ]\n");
3560Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
3570Sstevel@tonic-gate 	sioc.ic_timout = -1;
3580Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
3590Sstevel@tonic-gate 	sioc.ic_dp = (char *)&start_stats;
3600Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
3610Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
3620Sstevel@tonic-gate 		exit(1);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
3660Sstevel@tonic-gate 		if (write(dfd, packet, reclen) != reclen) {
3670Sstevel@tonic-gate 			(void) fprintf(stderr,
3680Sstevel@tonic-gate 				"packet write failed, errno %d\n",
3690Sstevel@tonic-gate 				errno);
3700Sstevel@tonic-gate 			exit(1);
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 		pfd.fd = dfd;
3730Sstevel@tonic-gate 		pfd.events = POLLIN;
3740Sstevel@tonic-gate 		pollret = poll(&pfd, 1, long_time);
3750Sstevel@tonic-gate 		if (pollret < 0) perror("poll");
3760Sstevel@tonic-gate 		if (pollret == 0)
3770Sstevel@tonic-gate 			(void) printf("poll: nothing to read.\n");
3780Sstevel@tonic-gate 		if (pollret == 1) {
3790Sstevel@tonic-gate 			len = read(dfd, bigbuf, reclen);
3800Sstevel@tonic-gate 			if (len == reclen && memcmp(packet, bigbuf, len) == 0)
3810Sstevel@tonic-gate 				return;	/* success */
3820Sstevel@tonic-gate 			else {
3830Sstevel@tonic-gate 				(void) printf("len %d should be %d\n",
3840Sstevel@tonic-gate 					len, reclen);
3850Sstevel@tonic-gate 				if (verbose) {
3860Sstevel@tonic-gate 					(void) printf("           ");
3870Sstevel@tonic-gate 					printhex(bigbuf, len);
3880Sstevel@tonic-gate 					(void) printf("\nshould be ");
3890Sstevel@tonic-gate 					printhex(packet, reclen);
3900Sstevel@tonic-gate 					(void) printf("\n");
3910Sstevel@tonic-gate 				}
3920Sstevel@tonic-gate 			}
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 	(void) printf("Loopback has TOTALLY FAILED - ");
3960Sstevel@tonic-gate 	(void) printf("no packets returned after 5 attempts\n");
3970Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
3980Sstevel@tonic-gate 	sioc.ic_timout = -1;
3990Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
4000Sstevel@tonic-gate 	sioc.ic_dp = (char *)&end_stats;
4010Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
4020Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
4030Sstevel@tonic-gate 		exit(1);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	if (start_stats.opack == end_stats.opack)
4060Sstevel@tonic-gate 		(void) printf(
4070Sstevel@tonic-gate 			"No packets transmitted - no transmit clock present\n");
4080Sstevel@tonic-gate 	exit(1);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate static void
many_packets()4120Sstevel@tonic-gate many_packets()
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	struct strioctl sioc;
4150Sstevel@tonic-gate 	struct sl_stats start_stats, end_stats;
4160Sstevel@tonic-gate 	struct timeval start_time, end_time;
4170Sstevel@tonic-gate 	int baddata = 0;
4180Sstevel@tonic-gate 	float secs, speed;
4190Sstevel@tonic-gate 	int i, len;
4200Sstevel@tonic-gate 	int incount = 0;
4210Sstevel@tonic-gate 	long prev_sec = -1;
4220Sstevel@tonic-gate 	int pollret;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	(void) printf("[ Trying many packets ]\n");
4250Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
4260Sstevel@tonic-gate 	sioc.ic_timout = -1;
4270Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
4280Sstevel@tonic-gate 	sioc.ic_dp = (char *)&start_stats;
4290Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
4300Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
4310Sstevel@tonic-gate 		exit(1);
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 	(void) gettimeofday(&start_time, 0);
4340Sstevel@tonic-gate 	end_time = start_time;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	i = 0;
4370Sstevel@tonic-gate 	while (i < reccount) {
4380Sstevel@tonic-gate 		if (end_time.tv_sec != prev_sec) {
4390Sstevel@tonic-gate 			prev_sec = end_time.tv_sec;
4400Sstevel@tonic-gate 			(void) printf("\r %d ", incount);
4410Sstevel@tonic-gate 			(void) fflush(stdout);
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 		pfd.fd = dfd;
4440Sstevel@tonic-gate 		pfd.events = POLLIN;
4450Sstevel@tonic-gate 		while (pollret = poll(&pfd, 1, zero_time)) {
4460Sstevel@tonic-gate 			if (pollret < 0)
4470Sstevel@tonic-gate 				perror("poll");
4480Sstevel@tonic-gate 			else {
4490Sstevel@tonic-gate 				(void) lseek(dfd, (long)0, 0);
4500Sstevel@tonic-gate 				len = read(dfd, bigbuf, reclen);
4510Sstevel@tonic-gate 				if (len != reclen ||
4520Sstevel@tonic-gate 				    memcmp(packet, bigbuf, len) != 0) {
4530Sstevel@tonic-gate 					(void) printf("len %d should be %d\n",
4540Sstevel@tonic-gate 						len, reclen);
4550Sstevel@tonic-gate 					if (verbose) {
4560Sstevel@tonic-gate 						(void) printf("           ");
4570Sstevel@tonic-gate 						printhex(bigbuf, len);
4580Sstevel@tonic-gate 						(void) printf("\nshould be ");
4590Sstevel@tonic-gate 						printhex(packet, reclen);
4600Sstevel@tonic-gate 						(void) printf("\n");
4610Sstevel@tonic-gate 					}
4620Sstevel@tonic-gate 					baddata++;
4630Sstevel@tonic-gate 				}
4640Sstevel@tonic-gate 				incount++;
4650Sstevel@tonic-gate 				(void) gettimeofday(&end_time, 0);
4660Sstevel@tonic-gate 			}
4670Sstevel@tonic-gate 		}
4680Sstevel@tonic-gate 		pfd.fd = dfd;
4690Sstevel@tonic-gate 		pfd.events = POLLIN|POLLOUT;
4700Sstevel@tonic-gate 		pollret = poll(&pfd, 1, long_time);
4710Sstevel@tonic-gate 		if (pollret < 0)
4720Sstevel@tonic-gate 			perror("poll");
4730Sstevel@tonic-gate 		if (pollret == 0)
4740Sstevel@tonic-gate 			(void) printf("poll: nothing to read or write.\n");
4750Sstevel@tonic-gate 		if (pollret == 1) {
4760Sstevel@tonic-gate 			if (pfd.revents & POLLOUT) {
4770Sstevel@tonic-gate 				(void) write(dfd, packet, reclen);
4780Sstevel@tonic-gate 				i++;
4790Sstevel@tonic-gate 			} else if (!(pfd.revents & POLLIN)) {
4800Sstevel@tonic-gate 				(void) printf("OUTPUT HAS LOCKED UP!!!\n");
4810Sstevel@tonic-gate 				break;
4820Sstevel@tonic-gate 			}
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 	pfd.fd = dfd;
4860Sstevel@tonic-gate 	pfd.events = POLLIN;
4870Sstevel@tonic-gate 	while ((incount < reccount) && (poll(&pfd, 1, long_time) == 1)) {
4880Sstevel@tonic-gate 		if (end_time.tv_sec != prev_sec) {
4890Sstevel@tonic-gate 			prev_sec = end_time.tv_sec;
4900Sstevel@tonic-gate 			(void) printf("\r %d ", incount);
4910Sstevel@tonic-gate 			(void) fflush(stdout);
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 		len = read(dfd, bigbuf, reclen);
4940Sstevel@tonic-gate 		if (len != reclen || memcmp(packet, bigbuf, len) != 0) {
4950Sstevel@tonic-gate 			(void) printf("len %d should be %d\n", len, reclen);
4960Sstevel@tonic-gate 			if (verbose) {
4970Sstevel@tonic-gate 				(void) printf("           ");
4980Sstevel@tonic-gate 				printhex(bigbuf, len);
4990Sstevel@tonic-gate 				(void) printf("\nshould be ");
5000Sstevel@tonic-gate 				printhex(packet, reclen);
5010Sstevel@tonic-gate 				(void) printf("\n");
5020Sstevel@tonic-gate 			}
5030Sstevel@tonic-gate 			baddata++;
5040Sstevel@tonic-gate 		}
5050Sstevel@tonic-gate 		incount++;
5060Sstevel@tonic-gate 		(void) gettimeofday(&end_time, 0);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 	(void) printf("\r %d \n", incount);
5090Sstevel@tonic-gate 	if (baddata)
5100Sstevel@tonic-gate 		(void) printf("%d packets with wrong data received!\n",
5110Sstevel@tonic-gate 			baddata);
5120Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
5130Sstevel@tonic-gate 	sioc.ic_timout = -1;
5140Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
5150Sstevel@tonic-gate 	sioc.ic_dp = (char *)&end_stats;
5160Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
5170Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
5180Sstevel@tonic-gate 		exit(1);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	end_stats.ipack -= start_stats.ipack;
5210Sstevel@tonic-gate 	end_stats.opack -= start_stats.opack;
5220Sstevel@tonic-gate 	end_stats.abort -= start_stats.abort;
5230Sstevel@tonic-gate 	end_stats.crc -= start_stats.crc;
5240Sstevel@tonic-gate 	end_stats.overrun -= start_stats.overrun;
5250Sstevel@tonic-gate 	end_stats.underrun -= start_stats.underrun;
5260Sstevel@tonic-gate 	end_stats.ierror -= start_stats.ierror;
5270Sstevel@tonic-gate 	end_stats.oerror -= start_stats.oerror;
5280Sstevel@tonic-gate 	if (reccount > end_stats.opack)
5290Sstevel@tonic-gate 		(void) printf("%d packets lost in outbound queueing\n",
5300Sstevel@tonic-gate 			reccount - end_stats.opack);
5310Sstevel@tonic-gate 	if (incount < end_stats.ipack && incount < reccount)
5320Sstevel@tonic-gate 		(void) printf("%d packets lost in inbound queueing\n",
5330Sstevel@tonic-gate 			end_stats.ipack - incount);
5340Sstevel@tonic-gate 	(void) printf("%d packets sent, %d received\n", reccount, incount);
5350Sstevel@tonic-gate 	(void) printf("CRC errors    Aborts   Overruns  Underruns         ");
5360Sstevel@tonic-gate 	(void) printf("   In <-Drops-> Out\n%9d  %9d  %9d  %9d  %12d  %12d\n",
5370Sstevel@tonic-gate 		end_stats.crc, end_stats.abort,
5380Sstevel@tonic-gate 		end_stats.overrun, end_stats.underrun,
5390Sstevel@tonic-gate 		end_stats.ierror, end_stats.oerror);
5400Sstevel@tonic-gate 	secs = (float)(end_time.tv_usec - start_time.tv_usec) / 1000000.0;
5410Sstevel@tonic-gate 	secs += (float)(end_time.tv_sec - start_time.tv_sec);
5420Sstevel@tonic-gate 	if (secs) {
5430Sstevel@tonic-gate 		speed = 8 * incount * (4 + reclen) / secs;
5440Sstevel@tonic-gate 		(void) printf("estimated line speed = %d bps\n", (int)speed);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate static void
printhex(char * cp,int len)5490Sstevel@tonic-gate printhex(char *cp, int len)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	char c, *hex = "0123456789ABCDEF";
5520Sstevel@tonic-gate 	int i;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
5550Sstevel@tonic-gate 		c = *cp++;
5560Sstevel@tonic-gate 		(void) putchar(hex[(c >> 4) & 0xF]);
5570Sstevel@tonic-gate 		(void) putchar(hex[c & 0xF]);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate }
560