xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/syncstat.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  * Display synchronous serial line statistics
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <sys/ioctl.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <sys/stream.h>
400Sstevel@tonic-gate #include <sys/stropts.h>
410Sstevel@tonic-gate #include <fcntl.h>
420Sstevel@tonic-gate #include <sys/ser_sync.h>
430Sstevel@tonic-gate #include <libdlpi.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static struct scc_mode sm;
460Sstevel@tonic-gate static struct sl_stats st;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static void usage(void);
490Sstevel@tonic-gate static void sample(int count, int period);
500Sstevel@tonic-gate 
51*3628Sss150715 static char sername[DLPI_LINKNAME_MAX];
520Sstevel@tonic-gate static int fd;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate int
main(int argc,char ** argv)550Sstevel@tonic-gate main(int argc, char **argv)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	char *cp;
58*3628Sss150715 	char serdevice[DLPI_LINKNAME_MAX];
590Sstevel@tonic-gate 	int do_clear = 0;
600Sstevel@tonic-gate 	int period = 0;
610Sstevel@tonic-gate 	int isize, osize;
620Sstevel@tonic-gate 	int count;
63*3628Sss150715 	int retval;
640Sstevel@tonic-gate 	struct strioctl sioc;
65*3628Sss150715 	uint_t ppa;
66*3628Sss150715 	dlpi_handle_t dh;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (argc == 1) {
690Sstevel@tonic-gate 		usage();
700Sstevel@tonic-gate 		exit(1);
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 	argc--;				/* skip the command name */
730Sstevel@tonic-gate 	argv++;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * The following loop processes command line arguments.
770Sstevel@tonic-gate 	 * If the argument begins with a '-', it is trated as an option.
780Sstevel@tonic-gate 	 * The only option currently implemented is "-c" (clears statistics).
790Sstevel@tonic-gate 	 * If the argument begins with a numeral, it is treated as an interval.
800Sstevel@tonic-gate 	 * Intervals must be positive integers greater than zero.
810Sstevel@tonic-gate 	 * Any argument that survives this is treated as a device name to be
820Sstevel@tonic-gate 	 * found under /dev.
830Sstevel@tonic-gate 	 */
840Sstevel@tonic-gate 	while (argc > 0) {
850Sstevel@tonic-gate 		if (argv[0][0] == '-') {
860Sstevel@tonic-gate 			if (argc == 1) {
870Sstevel@tonic-gate 				usage();
880Sstevel@tonic-gate 				exit(1);
890Sstevel@tonic-gate 			}
900Sstevel@tonic-gate 			if (argv[0][1] != 'c') {
910Sstevel@tonic-gate 				usage();
920Sstevel@tonic-gate 				exit(1);
930Sstevel@tonic-gate 			}
940Sstevel@tonic-gate 			do_clear = 1;
950Sstevel@tonic-gate 		} else if ((argv[0][0] >= '0') && (argv[0][0] <= '9')) {
960Sstevel@tonic-gate 			period = atoi(*argv);
970Sstevel@tonic-gate 			if (period == 0) {
980Sstevel@tonic-gate 				(void) fprintf(stderr,
99*3628Sss150715 				    "syncstat: bad interval: %s\n", *argv);
1000Sstevel@tonic-gate 				exit(1);
1010Sstevel@tonic-gate 			}
1020Sstevel@tonic-gate 		} else {
103*3628Sss150715 			if (snprintf(sername, sizeof (sername), "%s",
104*3628Sss150715 			    *argv) >= sizeof (sername)) {
105*3628Sss150715 				(void) fprintf(stderr, "syncstat: invalid "
106*3628Sss150715 				    "device name (too long) %s\n", *argv);
1070Sstevel@tonic-gate 				    exit(1);
1080Sstevel@tonic-gate 			}
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate 		argc--;
1110Sstevel@tonic-gate 		argv++;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
114*3628Sss150715 	for (cp = sername; (*cp) && (!isdigit(*cp)); cp++) {}
1150Sstevel@tonic-gate 	if (*cp == '\0') {	/* hit the end without finding a number */
1160Sstevel@tonic-gate 		(void) fprintf(stderr,
117*3628Sss150715 		    "syncstat: %s missing minor device number\n", sername);
1180Sstevel@tonic-gate 		exit(1);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
121*3628Sss150715 	if ((retval = dlpi_open(sername, &dh, DLPI_SERIAL)) != DLPI_SUCCESS) {
122*3628Sss150715 		(void) fprintf(stderr, "syncstat: dlpi_open %s: %s\n", sername,
123*3628Sss150715 		    dlpi_strerror(retval));
1240Sstevel@tonic-gate 		exit(1);
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
127*3628Sss150715 	(void) dlpi_parselink(sername, serdevice, &ppa);
128*3628Sss150715 	(void) printf("syncstat: control device: %s, ppa=%u\n", serdevice, ppa);
1290Sstevel@tonic-gate 
130*3628Sss150715 	fd = dlpi_fd(dh);
1310Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
1320Sstevel@tonic-gate 	sioc.ic_timout = -1;
1330Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
1340Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
1350Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &sioc) < 0) {
1360Sstevel@tonic-gate 		perror("S_IOCGETMODE");
1370Sstevel@tonic-gate 		(void) fprintf(stderr,
138*3628Sss150715 		    "syncstat: can't get sync mode info for %s\n", sername);
1390Sstevel@tonic-gate 		exit(1);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	if (do_clear) {
1420Sstevel@tonic-gate 		sioc.ic_cmd = S_IOCCLRSTATS;
1430Sstevel@tonic-gate 		sioc.ic_timout = -1;
1440Sstevel@tonic-gate 		sioc.ic_len = sizeof (struct sl_stats);
1450Sstevel@tonic-gate 		sioc.ic_dp = (char *)&st;
1460Sstevel@tonic-gate 		if (ioctl(fd, I_STR, &sioc) < 0) {
1470Sstevel@tonic-gate 			perror("S_IOCCLRSTATS");
1480Sstevel@tonic-gate 			(void) fprintf(stderr,
149*3628Sss150715 			    "syncstat: can't clear stats for %s\n", sername);
1500Sstevel@tonic-gate 			exit(1);
1510Sstevel@tonic-gate 		}
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
1550Sstevel@tonic-gate 	sioc.ic_timout = -1;
1560Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
1570Sstevel@tonic-gate 	sioc.ic_dp = (char *)&st;
1580Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &sioc) < 0) {
1590Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
1600Sstevel@tonic-gate 		(void) fprintf(stderr, "syncstat: can't get stats for %s\n",
161*3628Sss150715 		    sername);
1620Sstevel@tonic-gate 		exit(1);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	if (period) {
1650Sstevel@tonic-gate 		if (sm.sm_baudrate == 0) {
1660Sstevel@tonic-gate 			(void) fprintf(stderr, "syncstat: baud rate not set\n");
1670Sstevel@tonic-gate 			exit(1);
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 		for (count = 0; ; count++) {
1700Sstevel@tonic-gate 			(void) fflush(stdout);
1710Sstevel@tonic-gate 			(void) sleep(period);
1720Sstevel@tonic-gate 			sample(count, period);
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 	isize = osize = 0;
1760Sstevel@tonic-gate 	if (st.opack)
1770Sstevel@tonic-gate 		osize = st.ochar / st.opack;
1780Sstevel@tonic-gate 	if (st.ipack)
1790Sstevel@tonic-gate 		isize = st.ichar / st.ipack;
180*3628Sss150715 	(void) printf("    speed   ipkts   opkts  undrun  ovrrun   abort     "
181*3628Sss150715 	    "crc   isize   osize\n");
182*3628Sss150715 	(void) printf(" %7d %7d %7d %7d %7d %7d %7d %7d %7d\n", sm.sm_baudrate,
183*3628Sss150715 	    st.ipack, st.opack, st.underrun, st.overrun, st.abort, st.crc,
184*3628Sss150715 	    isize, osize);
1850Sstevel@tonic-gate 	return (0);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate static void
sample(int count,int period)1890Sstevel@tonic-gate sample(int count, int period)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	struct sl_stats nst;
1920Sstevel@tonic-gate 	struct strioctl sioc;
1930Sstevel@tonic-gate 	int iutil, outil;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
1960Sstevel@tonic-gate 	sioc.ic_timout = -1;
1970Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
1980Sstevel@tonic-gate 	sioc.ic_dp = (char *)&nst;
1990Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &sioc) < 0) {
2000Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
2010Sstevel@tonic-gate 		(void) fprintf(stderr, "syncstat: can't get stats for %s\n",
202*3628Sss150715 		    sername);
2030Sstevel@tonic-gate 		exit(1);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	st.ipack = nst.ipack - st.ipack;
2070Sstevel@tonic-gate 	st.opack = nst.opack - st.opack;
2080Sstevel@tonic-gate 	st.ichar = nst.ichar - st.ichar;
2090Sstevel@tonic-gate 	st.ochar = nst.ochar - st.ochar;
2100Sstevel@tonic-gate 	st.crc = nst.crc - st.crc;
2110Sstevel@tonic-gate 	st.overrun = nst.overrun - st.overrun;
2120Sstevel@tonic-gate 	st.underrun = nst.underrun - st.underrun;
2130Sstevel@tonic-gate 	st.abort = nst.abort - st.abort;
2140Sstevel@tonic-gate 	iutil = 8 * st.ichar / period;
2150Sstevel@tonic-gate 	iutil = 100 * iutil / sm.sm_baudrate;
2160Sstevel@tonic-gate 	outil = 8 * st.ochar / period;
2170Sstevel@tonic-gate 	outil = 100 * outil / sm.sm_baudrate;
218*3628Sss150715 	if ((count % 20) == 0)
219*3628Sss150715 		(void) printf("    ipkts   opkts  undrun  ovrrun   abort     "
220*3628Sss150715 		    "crc   iutil   outil\n");
221*3628Sss150715 	(void) printf(" %7d %7d %7d %7d %7d %7d %6d%% %6d%%\n", st.ipack,
222*3628Sss150715 	    st.opack, st.underrun, st.overrun, st.abort, st.crc, iutil, outil);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	st = nst;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate static void
usage()2280Sstevel@tonic-gate usage()
2290Sstevel@tonic-gate {
230*3628Sss150715 	(void) fprintf(stderr, "Usage: syncstat [-c] device [period]\n");
2310Sstevel@tonic-gate }
232