xref: /titanic_53/usr/src/cmd/cmd-inet/usr.sbin/syncloop.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Synchronous loop-back test program
31*7c478bd9Sstevel@tonic-gate  * For installation verification of synchronous lines and facilities
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <ctype.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
37*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate #include <unistd.h>
43*7c478bd9Sstevel@tonic-gate #include <string.h>
44*7c478bd9Sstevel@tonic-gate #include <errno.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/poll.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/ser_sync.h>
49*7c478bd9Sstevel@tonic-gate #include <libdlpi.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static void Usage(void);
52*7c478bd9Sstevel@tonic-gate static void quiet_period(void);
53*7c478bd9Sstevel@tonic-gate static void first_packet();
54*7c478bd9Sstevel@tonic-gate static void many_packets();
55*7c478bd9Sstevel@tonic-gate static void printhex(char *cp, int len);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate static char *portname = NULL;
58*7c478bd9Sstevel@tonic-gate static unsigned int speed = 9600;
59*7c478bd9Sstevel@tonic-gate static int reccount = 100;
60*7c478bd9Sstevel@tonic-gate static int reclen = 100;
61*7c478bd9Sstevel@tonic-gate static char loopstr[MAX_INPUT];
62*7c478bd9Sstevel@tonic-gate static int looptype = 0;
63*7c478bd9Sstevel@tonic-gate static int loopchange = 0;
64*7c478bd9Sstevel@tonic-gate static int clockchange = 0;
65*7c478bd9Sstevel@tonic-gate static int cfd, dfd;		/* control and data descriptors */
66*7c478bd9Sstevel@tonic-gate static int data = -1;
67*7c478bd9Sstevel@tonic-gate static int verbose = 0;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static char *yesno[] = {
70*7c478bd9Sstevel@tonic-gate 	"no",
71*7c478bd9Sstevel@tonic-gate 	"yes",
72*7c478bd9Sstevel@tonic-gate 	"silent",
73*7c478bd9Sstevel@tonic-gate 	0,
74*7c478bd9Sstevel@tonic-gate };
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static char *txnames[] = {
77*7c478bd9Sstevel@tonic-gate 	"txc",
78*7c478bd9Sstevel@tonic-gate 	"rxc",
79*7c478bd9Sstevel@tonic-gate 	"baud",
80*7c478bd9Sstevel@tonic-gate 	"pll",
81*7c478bd9Sstevel@tonic-gate 	"sysclk",
82*7c478bd9Sstevel@tonic-gate 	"-txc",
83*7c478bd9Sstevel@tonic-gate 	0,
84*7c478bd9Sstevel@tonic-gate };
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate static char *rxnames[] = {
87*7c478bd9Sstevel@tonic-gate 	"rxc",
88*7c478bd9Sstevel@tonic-gate 	"txc",
89*7c478bd9Sstevel@tonic-gate 	"baud",
90*7c478bd9Sstevel@tonic-gate 	"pll",
91*7c478bd9Sstevel@tonic-gate 	"sysclk",
92*7c478bd9Sstevel@tonic-gate 	"-rxc",
93*7c478bd9Sstevel@tonic-gate 	0,
94*7c478bd9Sstevel@tonic-gate };
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #define	MAXPACKET	4096
97*7c478bd9Sstevel@tonic-gate #define	MAXWAIT		15
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate int
100*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	char cnambuf[MAXPATHLEN], dnambuf[MAXPATHLEN], *cp, *cpp;
103*7c478bd9Sstevel@tonic-gate 	struct scc_mode sm;
104*7c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
105*7c478bd9Sstevel@tonic-gate 	ulong_t ppa;
106*7c478bd9Sstevel@tonic-gate 	char *devstr = "/dev/";
107*7c478bd9Sstevel@tonic-gate 	int devstrlen;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	argc--;
110*7c478bd9Sstevel@tonic-gate 	argv++;
111*7c478bd9Sstevel@tonic-gate 	while (argc > 0 && argv[0][0] == '-')
112*7c478bd9Sstevel@tonic-gate 		switch (argv[0][1]) {
113*7c478bd9Sstevel@tonic-gate 		case 'c':	/* rec count */
114*7c478bd9Sstevel@tonic-gate 			if (argc < 2)
115*7c478bd9Sstevel@tonic-gate 				Usage();
116*7c478bd9Sstevel@tonic-gate 			reccount = atoi(argv[1]);
117*7c478bd9Sstevel@tonic-gate 			argc -= 2;
118*7c478bd9Sstevel@tonic-gate 			argv += 2;
119*7c478bd9Sstevel@tonic-gate 			break;
120*7c478bd9Sstevel@tonic-gate 		case 'd':
121*7c478bd9Sstevel@tonic-gate 			if (sscanf(argv[1], "%x", (uint_t *)&data) != 1)
122*7c478bd9Sstevel@tonic-gate 				Usage();
123*7c478bd9Sstevel@tonic-gate 			argc -= 2;
124*7c478bd9Sstevel@tonic-gate 			argv += 2;
125*7c478bd9Sstevel@tonic-gate 			break;
126*7c478bd9Sstevel@tonic-gate 		case 'l':	/* rec length */
127*7c478bd9Sstevel@tonic-gate 			if (argc < 2)
128*7c478bd9Sstevel@tonic-gate 				Usage();
129*7c478bd9Sstevel@tonic-gate 			reclen = atoi(argv[1]);
130*7c478bd9Sstevel@tonic-gate 			argc -= 2;
131*7c478bd9Sstevel@tonic-gate 			argv += 2;
132*7c478bd9Sstevel@tonic-gate 			break;
133*7c478bd9Sstevel@tonic-gate 		case 's':	/* line speed */
134*7c478bd9Sstevel@tonic-gate 			if (argc < 2)
135*7c478bd9Sstevel@tonic-gate 				Usage();
136*7c478bd9Sstevel@tonic-gate 			speed = atoi(argv[1]);
137*7c478bd9Sstevel@tonic-gate 			argc -= 2;
138*7c478bd9Sstevel@tonic-gate 			argv += 2;
139*7c478bd9Sstevel@tonic-gate 			break;
140*7c478bd9Sstevel@tonic-gate 		case 't':	/* test type */
141*7c478bd9Sstevel@tonic-gate 			if (argc < 2)
142*7c478bd9Sstevel@tonic-gate 				Usage();
143*7c478bd9Sstevel@tonic-gate 			looptype = atoi(argv[1]);
144*7c478bd9Sstevel@tonic-gate 			argc -= 2;
145*7c478bd9Sstevel@tonic-gate 			argv += 2;
146*7c478bd9Sstevel@tonic-gate 			break;
147*7c478bd9Sstevel@tonic-gate 		case 'v':
148*7c478bd9Sstevel@tonic-gate 			verbose = 1;
149*7c478bd9Sstevel@tonic-gate 			argc--;
150*7c478bd9Sstevel@tonic-gate 			argv++;
151*7c478bd9Sstevel@tonic-gate 			break;
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 	if (argc != 1)
154*7c478bd9Sstevel@tonic-gate 		Usage();
155*7c478bd9Sstevel@tonic-gate 	portname = argv[0];
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	devstrlen = strlen(devstr);
158*7c478bd9Sstevel@tonic-gate 	if (strncmp(devstr, portname, devstrlen) != 0) {
159*7c478bd9Sstevel@tonic-gate 		if (snprintf(dnambuf, sizeof (dnambuf), "%s%s", devstr,
160*7c478bd9Sstevel@tonic-gate 		    portname) >= sizeof (dnambuf)) {
161*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
162*7c478bd9Sstevel@tonic-gate 			    "syncloop: invalid device name (too long) %s\n",
163*7c478bd9Sstevel@tonic-gate 			    portname);
164*7c478bd9Sstevel@tonic-gate 			exit(1);
165*7c478bd9Sstevel@tonic-gate 		}
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	dfd = open(dnambuf, O_RDWR);
169*7c478bd9Sstevel@tonic-gate 	if (dfd < 0) {
170*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "syncloop: cannot open %s\n", dnambuf);
171*7c478bd9Sstevel@tonic-gate 		perror(dnambuf);
172*7c478bd9Sstevel@tonic-gate 		exit(1);
173*7c478bd9Sstevel@tonic-gate 	}
174*7c478bd9Sstevel@tonic-gate 	for (cp = portname; (*cp) && (!isdigit(*cp)); cp++) {}
175*7c478bd9Sstevel@tonic-gate 	ppa = strtoul(cp, &cpp, 10);
176*7c478bd9Sstevel@tonic-gate 	if (cpp == cp) {
177*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
178*7c478bd9Sstevel@tonic-gate 			"syncloop: %s missing minor device number\n", portname);
179*7c478bd9Sstevel@tonic-gate 		exit(1);
180*7c478bd9Sstevel@tonic-gate 	}
181*7c478bd9Sstevel@tonic-gate 	*cp = '\0';	/* drop number, leaving name of clone device. */
182*7c478bd9Sstevel@tonic-gate 	/* the following won't fail since cnambuf and dnambuf are same size */
183*7c478bd9Sstevel@tonic-gate 	if (strncmp(devstr, portname, devstrlen) != 0) {
184*7c478bd9Sstevel@tonic-gate 		(void) snprintf(cnambuf, sizeof (cnambuf), "%s%s", devstr,
185*7c478bd9Sstevel@tonic-gate 		    portname);
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 	cfd = open(cnambuf, O_RDWR);
188*7c478bd9Sstevel@tonic-gate 	if (cfd < 0) {
189*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "syncloop: cannot open %s\n", cnambuf);
190*7c478bd9Sstevel@tonic-gate 		perror(cnambuf);
191*7c478bd9Sstevel@tonic-gate 		exit(1);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if (dlpi_attach(cfd, MAXWAIT, ppa) != 0) {
195*7c478bd9Sstevel@tonic-gate 		perror("syncloop: dlpi_attach");
196*7c478bd9Sstevel@tonic-gate 		exit(1);
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	if (reclen < 0 || reclen > MAXPACKET) {
200*7c478bd9Sstevel@tonic-gate 		(void) printf("invalid packet length: %d\n", reclen);
201*7c478bd9Sstevel@tonic-gate 		exit(1);
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 	(void) printf("[ Data device: %s | Control device: %s, ppa=%ld ]\n",
204*7c478bd9Sstevel@tonic-gate 		dnambuf, cnambuf, ppa);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
207*7c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
208*7c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
209*7c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
210*7c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
211*7c478bd9Sstevel@tonic-gate 		perror("S_IOCGETMODE");
212*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
213*7c478bd9Sstevel@tonic-gate 			"syncloop: can't get sync mode info for %s\n", cnambuf);
214*7c478bd9Sstevel@tonic-gate 		exit(1);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	while (looptype < 1 || looptype > 4) {
217*7c478bd9Sstevel@tonic-gate 		(void) printf("Enter test type:\n");
218*7c478bd9Sstevel@tonic-gate 		(void) printf("1: Internal Test\n");
219*7c478bd9Sstevel@tonic-gate 		(void) printf(
220*7c478bd9Sstevel@tonic-gate "            (internal data loop, internal clocking)\n");
221*7c478bd9Sstevel@tonic-gate 		(void) printf("2: Test using loopback plugs\n");
222*7c478bd9Sstevel@tonic-gate 		(void) printf(
223*7c478bd9Sstevel@tonic-gate "            (external data loop, internal clocking)\n");
224*7c478bd9Sstevel@tonic-gate 		(void) printf("3: Test using local or remote modem loopback\n");
225*7c478bd9Sstevel@tonic-gate 		(void) printf(
226*7c478bd9Sstevel@tonic-gate "            (external data loop, external clocking)\n");
227*7c478bd9Sstevel@tonic-gate 		(void) printf("4: Other, previously set, special mode\n");
228*7c478bd9Sstevel@tonic-gate 		(void) printf("> "); (void) fflush(stdout);
229*7c478bd9Sstevel@tonic-gate 		(void) fgets(loopstr, sizeof (loopstr), stdin);
230*7c478bd9Sstevel@tonic-gate 		(void) sscanf(loopstr, "%d", &looptype);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 	switch (looptype) {
233*7c478bd9Sstevel@tonic-gate 	case 1:
234*7c478bd9Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_BAUD) ||
235*7c478bd9Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_BAUD))
236*7c478bd9Sstevel@tonic-gate 			clockchange++;
237*7c478bd9Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_BAUD;
238*7c478bd9Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_BAUD;
239*7c478bd9Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) == 0)
240*7c478bd9Sstevel@tonic-gate 			loopchange++;
241*7c478bd9Sstevel@tonic-gate 		sm.sm_config |= CONN_LPBK;
242*7c478bd9Sstevel@tonic-gate 		break;
243*7c478bd9Sstevel@tonic-gate 	case 2:
244*7c478bd9Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_BAUD) ||
245*7c478bd9Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_RXC))
246*7c478bd9Sstevel@tonic-gate 			clockchange++;
247*7c478bd9Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_BAUD;
248*7c478bd9Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_RXC;
249*7c478bd9Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) != 0)
250*7c478bd9Sstevel@tonic-gate 			loopchange++;
251*7c478bd9Sstevel@tonic-gate 		sm.sm_config &= ~CONN_LPBK;
252*7c478bd9Sstevel@tonic-gate 		break;
253*7c478bd9Sstevel@tonic-gate 	case 3:
254*7c478bd9Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_TXC) ||
255*7c478bd9Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_RXC))
256*7c478bd9Sstevel@tonic-gate 			clockchange++;
257*7c478bd9Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_TXC;
258*7c478bd9Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_RXC;
259*7c478bd9Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) != 0)
260*7c478bd9Sstevel@tonic-gate 			loopchange++;
261*7c478bd9Sstevel@tonic-gate 		sm.sm_config &= ~CONN_LPBK;
262*7c478bd9Sstevel@tonic-gate 		break;
263*7c478bd9Sstevel@tonic-gate 	case 4:
264*7c478bd9Sstevel@tonic-gate 		goto no_params;
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	sm.sm_baudrate = speed;
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCSETMODE;
270*7c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
271*7c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
272*7c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
273*7c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
274*7c478bd9Sstevel@tonic-gate 		perror("S_IOCSETMODE");
275*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
276*7c478bd9Sstevel@tonic-gate 			"syncloop: can't set sync mode info for %s\n", cnambuf);
277*7c478bd9Sstevel@tonic-gate 		exit(1);
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate no_params:
281*7c478bd9Sstevel@tonic-gate 	/* report state */
282*7c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
283*7c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
284*7c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
285*7c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
286*7c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
287*7c478bd9Sstevel@tonic-gate 		perror("S_IOCGETMODE");
288*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
289*7c478bd9Sstevel@tonic-gate 			"syncloop: can't get sync mode info for %s\n", cnambuf);
290*7c478bd9Sstevel@tonic-gate 		exit(1);
291*7c478bd9Sstevel@tonic-gate 	}
292*7c478bd9Sstevel@tonic-gate 	(void) printf("speed=%d, loopback=%s, nrzi=%s, txc=%s, rxc=%s\n",
293*7c478bd9Sstevel@tonic-gate 		sm.sm_baudrate,
294*7c478bd9Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_LPBK) > 0)],
295*7c478bd9Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_NRZI) > 0)],
296*7c478bd9Sstevel@tonic-gate 		txnames[sm.sm_txclock],
297*7c478bd9Sstevel@tonic-gate 		rxnames[sm.sm_rxclock]);
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	quiet_period();
300*7c478bd9Sstevel@tonic-gate 	first_packet();
301*7c478bd9Sstevel@tonic-gate 	many_packets();
302*7c478bd9Sstevel@tonic-gate 	return (0);
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate static void
306*7c478bd9Sstevel@tonic-gate Usage()
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	(void) printf("Usage: syncloop [ options ] portname\n");
309*7c478bd9Sstevel@tonic-gate 	(void) printf("Options: -c packet_count\n");
310*7c478bd9Sstevel@tonic-gate 	(void) printf("         -l packet_length\n");
311*7c478bd9Sstevel@tonic-gate 	(void) printf("         -s line_speed\n");
312*7c478bd9Sstevel@tonic-gate 	(void) printf("         -t test_type\n");
313*7c478bd9Sstevel@tonic-gate 	(void) printf("         -d hex_data_byte\n");
314*7c478bd9Sstevel@tonic-gate 	exit(1);
315*7c478bd9Sstevel@tonic-gate }
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate static int zero_time = 0;
318*7c478bd9Sstevel@tonic-gate static int short_time = 1000;
319*7c478bd9Sstevel@tonic-gate static int long_time = 4000;
320*7c478bd9Sstevel@tonic-gate static char bigbuf[4096];
321*7c478bd9Sstevel@tonic-gate static char packet[MAXPACKET];
322*7c478bd9Sstevel@tonic-gate static struct pollfd pfd;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate static void
325*7c478bd9Sstevel@tonic-gate quiet_period()
326*7c478bd9Sstevel@tonic-gate {
327*7c478bd9Sstevel@tonic-gate 	(void) printf("[ checking for quiet line ]\n");
328*7c478bd9Sstevel@tonic-gate 	pfd.fd = dfd;
329*7c478bd9Sstevel@tonic-gate 	pfd.events = POLLIN;
330*7c478bd9Sstevel@tonic-gate 	pfd.revents = 0;
331*7c478bd9Sstevel@tonic-gate 	while (poll(&pfd, 1, short_time) == 1) {
332*7c478bd9Sstevel@tonic-gate 		(void) read(dfd, bigbuf, sizeof (bigbuf));
333*7c478bd9Sstevel@tonic-gate 	}
334*7c478bd9Sstevel@tonic-gate 	if (poll(&pfd, 1, long_time) == 1) {
335*7c478bd9Sstevel@tonic-gate 		(void) printf("packet received but none sent!\n");
336*7c478bd9Sstevel@tonic-gate 		(void) printf("quiesce other end before starting syncloop\n");
337*7c478bd9Sstevel@tonic-gate 		exit(1);
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate }
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate static void
342*7c478bd9Sstevel@tonic-gate first_packet()
343*7c478bd9Sstevel@tonic-gate {
344*7c478bd9Sstevel@tonic-gate 	int i, len;
345*7c478bd9Sstevel@tonic-gate 	int pollret;
346*7c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
347*7c478bd9Sstevel@tonic-gate 	struct sl_stats start_stats, end_stats;
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < reclen; i++)
350*7c478bd9Sstevel@tonic-gate 		packet[i] = (data == -1) ? rand() : data;
351*7c478bd9Sstevel@tonic-gate 	(void) printf("[ Trying first packet ]\n");
352*7c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
353*7c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
354*7c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
355*7c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&start_stats;
356*7c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
357*7c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
358*7c478bd9Sstevel@tonic-gate 		exit(1);
359*7c478bd9Sstevel@tonic-gate 	}
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
362*7c478bd9Sstevel@tonic-gate 		if (write(dfd, packet, reclen) != reclen) {
363*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
364*7c478bd9Sstevel@tonic-gate 				"packet write failed, errno %d\n",
365*7c478bd9Sstevel@tonic-gate 				errno);
366*7c478bd9Sstevel@tonic-gate 			exit(1);
367*7c478bd9Sstevel@tonic-gate 		}
368*7c478bd9Sstevel@tonic-gate 		pfd.fd = dfd;
369*7c478bd9Sstevel@tonic-gate 		pfd.events = POLLIN;
370*7c478bd9Sstevel@tonic-gate 		pollret = poll(&pfd, 1, long_time);
371*7c478bd9Sstevel@tonic-gate 		if (pollret < 0) perror("poll");
372*7c478bd9Sstevel@tonic-gate 		if (pollret == 0)
373*7c478bd9Sstevel@tonic-gate 			(void) printf("poll: nothing to read.\n");
374*7c478bd9Sstevel@tonic-gate 		if (pollret == 1) {
375*7c478bd9Sstevel@tonic-gate 			len = read(dfd, bigbuf, reclen);
376*7c478bd9Sstevel@tonic-gate 			if (len == reclen && memcmp(packet, bigbuf, len) == 0)
377*7c478bd9Sstevel@tonic-gate 				return;	/* success */
378*7c478bd9Sstevel@tonic-gate 			else {
379*7c478bd9Sstevel@tonic-gate 				(void) printf("len %d should be %d\n",
380*7c478bd9Sstevel@tonic-gate 					len, reclen);
381*7c478bd9Sstevel@tonic-gate 				if (verbose) {
382*7c478bd9Sstevel@tonic-gate 					(void) printf("           ");
383*7c478bd9Sstevel@tonic-gate 					printhex(bigbuf, len);
384*7c478bd9Sstevel@tonic-gate 					(void) printf("\nshould be ");
385*7c478bd9Sstevel@tonic-gate 					printhex(packet, reclen);
386*7c478bd9Sstevel@tonic-gate 					(void) printf("\n");
387*7c478bd9Sstevel@tonic-gate 				}
388*7c478bd9Sstevel@tonic-gate 			}
389*7c478bd9Sstevel@tonic-gate 		}
390*7c478bd9Sstevel@tonic-gate 	}
391*7c478bd9Sstevel@tonic-gate 	(void) printf("Loopback has TOTALLY FAILED - ");
392*7c478bd9Sstevel@tonic-gate 	(void) printf("no packets returned after 5 attempts\n");
393*7c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
394*7c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
395*7c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
396*7c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&end_stats;
397*7c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
398*7c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
399*7c478bd9Sstevel@tonic-gate 		exit(1);
400*7c478bd9Sstevel@tonic-gate 	}
401*7c478bd9Sstevel@tonic-gate 	if (start_stats.opack == end_stats.opack)
402*7c478bd9Sstevel@tonic-gate 		(void) printf(
403*7c478bd9Sstevel@tonic-gate 			"No packets transmitted - no transmit clock present\n");
404*7c478bd9Sstevel@tonic-gate 	exit(1);
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate static void
408*7c478bd9Sstevel@tonic-gate many_packets()
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
411*7c478bd9Sstevel@tonic-gate 	struct sl_stats start_stats, end_stats;
412*7c478bd9Sstevel@tonic-gate 	struct timeval start_time, end_time;
413*7c478bd9Sstevel@tonic-gate 	int baddata = 0;
414*7c478bd9Sstevel@tonic-gate 	float secs, speed;
415*7c478bd9Sstevel@tonic-gate 	int i, len;
416*7c478bd9Sstevel@tonic-gate 	int incount = 0;
417*7c478bd9Sstevel@tonic-gate 	long prev_sec = -1;
418*7c478bd9Sstevel@tonic-gate 	int pollret;
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	(void) printf("[ Trying many packets ]\n");
421*7c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
422*7c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
423*7c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
424*7c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&start_stats;
425*7c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
426*7c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
427*7c478bd9Sstevel@tonic-gate 		exit(1);
428*7c478bd9Sstevel@tonic-gate 	}
429*7c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&start_time, 0);
430*7c478bd9Sstevel@tonic-gate 	end_time = start_time;
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 	i = 0;
433*7c478bd9Sstevel@tonic-gate 	while (i < reccount) {
434*7c478bd9Sstevel@tonic-gate 		if (end_time.tv_sec != prev_sec) {
435*7c478bd9Sstevel@tonic-gate 			prev_sec = end_time.tv_sec;
436*7c478bd9Sstevel@tonic-gate 			(void) printf("\r %d ", incount);
437*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
438*7c478bd9Sstevel@tonic-gate 		}
439*7c478bd9Sstevel@tonic-gate 		pfd.fd = dfd;
440*7c478bd9Sstevel@tonic-gate 		pfd.events = POLLIN;
441*7c478bd9Sstevel@tonic-gate 		while (pollret = poll(&pfd, 1, zero_time)) {
442*7c478bd9Sstevel@tonic-gate 			if (pollret < 0)
443*7c478bd9Sstevel@tonic-gate 				perror("poll");
444*7c478bd9Sstevel@tonic-gate 			else {
445*7c478bd9Sstevel@tonic-gate 				(void) lseek(dfd, (long)0, 0);
446*7c478bd9Sstevel@tonic-gate 				len = read(dfd, bigbuf, reclen);
447*7c478bd9Sstevel@tonic-gate 				if (len != reclen ||
448*7c478bd9Sstevel@tonic-gate 				    memcmp(packet, bigbuf, len) != 0) {
449*7c478bd9Sstevel@tonic-gate 					(void) printf("len %d should be %d\n",
450*7c478bd9Sstevel@tonic-gate 						len, reclen);
451*7c478bd9Sstevel@tonic-gate 					if (verbose) {
452*7c478bd9Sstevel@tonic-gate 						(void) printf("           ");
453*7c478bd9Sstevel@tonic-gate 						printhex(bigbuf, len);
454*7c478bd9Sstevel@tonic-gate 						(void) printf("\nshould be ");
455*7c478bd9Sstevel@tonic-gate 						printhex(packet, reclen);
456*7c478bd9Sstevel@tonic-gate 						(void) printf("\n");
457*7c478bd9Sstevel@tonic-gate 					}
458*7c478bd9Sstevel@tonic-gate 					baddata++;
459*7c478bd9Sstevel@tonic-gate 				}
460*7c478bd9Sstevel@tonic-gate 				incount++;
461*7c478bd9Sstevel@tonic-gate 				(void) gettimeofday(&end_time, 0);
462*7c478bd9Sstevel@tonic-gate 			}
463*7c478bd9Sstevel@tonic-gate 		}
464*7c478bd9Sstevel@tonic-gate 		pfd.fd = dfd;
465*7c478bd9Sstevel@tonic-gate 		pfd.events = POLLIN|POLLOUT;
466*7c478bd9Sstevel@tonic-gate 		pollret = poll(&pfd, 1, long_time);
467*7c478bd9Sstevel@tonic-gate 		if (pollret < 0)
468*7c478bd9Sstevel@tonic-gate 			perror("poll");
469*7c478bd9Sstevel@tonic-gate 		if (pollret == 0)
470*7c478bd9Sstevel@tonic-gate 			(void) printf("poll: nothing to read or write.\n");
471*7c478bd9Sstevel@tonic-gate 		if (pollret == 1) {
472*7c478bd9Sstevel@tonic-gate 			if (pfd.revents & POLLOUT) {
473*7c478bd9Sstevel@tonic-gate 				(void) write(dfd, packet, reclen);
474*7c478bd9Sstevel@tonic-gate 				i++;
475*7c478bd9Sstevel@tonic-gate 			} else if (!(pfd.revents & POLLIN)) {
476*7c478bd9Sstevel@tonic-gate 				(void) printf("OUTPUT HAS LOCKED UP!!!\n");
477*7c478bd9Sstevel@tonic-gate 				break;
478*7c478bd9Sstevel@tonic-gate 			}
479*7c478bd9Sstevel@tonic-gate 		}
480*7c478bd9Sstevel@tonic-gate 	}
481*7c478bd9Sstevel@tonic-gate 	pfd.fd = dfd;
482*7c478bd9Sstevel@tonic-gate 	pfd.events = POLLIN;
483*7c478bd9Sstevel@tonic-gate 	while ((incount < reccount) && (poll(&pfd, 1, long_time) == 1)) {
484*7c478bd9Sstevel@tonic-gate 		if (end_time.tv_sec != prev_sec) {
485*7c478bd9Sstevel@tonic-gate 			prev_sec = end_time.tv_sec;
486*7c478bd9Sstevel@tonic-gate 			(void) printf("\r %d ", incount);
487*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
488*7c478bd9Sstevel@tonic-gate 		}
489*7c478bd9Sstevel@tonic-gate 		len = read(dfd, bigbuf, reclen);
490*7c478bd9Sstevel@tonic-gate 		if (len != reclen || memcmp(packet, bigbuf, len) != 0) {
491*7c478bd9Sstevel@tonic-gate 			(void) printf("len %d should be %d\n", len, reclen);
492*7c478bd9Sstevel@tonic-gate 			if (verbose) {
493*7c478bd9Sstevel@tonic-gate 				(void) printf("           ");
494*7c478bd9Sstevel@tonic-gate 				printhex(bigbuf, len);
495*7c478bd9Sstevel@tonic-gate 				(void) printf("\nshould be ");
496*7c478bd9Sstevel@tonic-gate 				printhex(packet, reclen);
497*7c478bd9Sstevel@tonic-gate 				(void) printf("\n");
498*7c478bd9Sstevel@tonic-gate 			}
499*7c478bd9Sstevel@tonic-gate 			baddata++;
500*7c478bd9Sstevel@tonic-gate 		}
501*7c478bd9Sstevel@tonic-gate 		incount++;
502*7c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&end_time, 0);
503*7c478bd9Sstevel@tonic-gate 	}
504*7c478bd9Sstevel@tonic-gate 	(void) printf("\r %d \n", incount);
505*7c478bd9Sstevel@tonic-gate 	if (baddata)
506*7c478bd9Sstevel@tonic-gate 		(void) printf("%d packets with wrong data received!\n",
507*7c478bd9Sstevel@tonic-gate 			baddata);
508*7c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
509*7c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
510*7c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
511*7c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&end_stats;
512*7c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
513*7c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
514*7c478bd9Sstevel@tonic-gate 		exit(1);
515*7c478bd9Sstevel@tonic-gate 	}
516*7c478bd9Sstevel@tonic-gate 	end_stats.ipack -= start_stats.ipack;
517*7c478bd9Sstevel@tonic-gate 	end_stats.opack -= start_stats.opack;
518*7c478bd9Sstevel@tonic-gate 	end_stats.abort -= start_stats.abort;
519*7c478bd9Sstevel@tonic-gate 	end_stats.crc -= start_stats.crc;
520*7c478bd9Sstevel@tonic-gate 	end_stats.overrun -= start_stats.overrun;
521*7c478bd9Sstevel@tonic-gate 	end_stats.underrun -= start_stats.underrun;
522*7c478bd9Sstevel@tonic-gate 	end_stats.ierror -= start_stats.ierror;
523*7c478bd9Sstevel@tonic-gate 	end_stats.oerror -= start_stats.oerror;
524*7c478bd9Sstevel@tonic-gate 	if (reccount > end_stats.opack)
525*7c478bd9Sstevel@tonic-gate 		(void) printf("%d packets lost in outbound queueing\n",
526*7c478bd9Sstevel@tonic-gate 			reccount - end_stats.opack);
527*7c478bd9Sstevel@tonic-gate 	if (incount < end_stats.ipack && incount < reccount)
528*7c478bd9Sstevel@tonic-gate 		(void) printf("%d packets lost in inbound queueing\n",
529*7c478bd9Sstevel@tonic-gate 			end_stats.ipack - incount);
530*7c478bd9Sstevel@tonic-gate 	(void) printf("%d packets sent, %d received\n", reccount, incount);
531*7c478bd9Sstevel@tonic-gate 	(void) printf("CRC errors    Aborts   Overruns  Underruns         ");
532*7c478bd9Sstevel@tonic-gate 	(void) printf("   In <-Drops-> Out\n%9d  %9d  %9d  %9d  %12d  %12d\n",
533*7c478bd9Sstevel@tonic-gate 		end_stats.crc, end_stats.abort,
534*7c478bd9Sstevel@tonic-gate 		end_stats.overrun, end_stats.underrun,
535*7c478bd9Sstevel@tonic-gate 		end_stats.ierror, end_stats.oerror);
536*7c478bd9Sstevel@tonic-gate 	secs = (float)(end_time.tv_usec - start_time.tv_usec) / 1000000.0;
537*7c478bd9Sstevel@tonic-gate 	secs += (float)(end_time.tv_sec - start_time.tv_sec);
538*7c478bd9Sstevel@tonic-gate 	if (secs) {
539*7c478bd9Sstevel@tonic-gate 		speed = 8 * incount * (4 + reclen) / secs;
540*7c478bd9Sstevel@tonic-gate 		(void) printf("estimated line speed = %d bps\n", (int)speed);
541*7c478bd9Sstevel@tonic-gate 	}
542*7c478bd9Sstevel@tonic-gate }
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate static void
545*7c478bd9Sstevel@tonic-gate printhex(char *cp, int len)
546*7c478bd9Sstevel@tonic-gate {
547*7c478bd9Sstevel@tonic-gate 	char c, *hex = "0123456789ABCDEF";
548*7c478bd9Sstevel@tonic-gate 	int i;
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
551*7c478bd9Sstevel@tonic-gate 		c = *cp++;
552*7c478bd9Sstevel@tonic-gate 		(void) putchar(hex[(c >> 4) & 0xF]);
553*7c478bd9Sstevel@tonic-gate 		(void) putchar(hex[c & 0xF]);
554*7c478bd9Sstevel@tonic-gate 	}
555*7c478bd9Sstevel@tonic-gate }
556