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
5*c7e4935fSss  * Common Development and Distribution License (the "License").
6*c7e4935fSss  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*c7e4935fSss  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Display synchronous serial line statistics
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <sys/stream.h>
387c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
397c478bd9Sstevel@tonic-gate #include <fcntl.h>
407c478bd9Sstevel@tonic-gate #include <sys/ser_sync.h>
417c478bd9Sstevel@tonic-gate #include <libdlpi.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static struct scc_mode sm;
447c478bd9Sstevel@tonic-gate static struct sl_stats st;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static void usage(void);
477c478bd9Sstevel@tonic-gate static void sample(int count, int period);
487c478bd9Sstevel@tonic-gate 
49*c7e4935fSss static char sername[DLPI_LINKNAME_MAX];
507c478bd9Sstevel@tonic-gate static int fd;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)537c478bd9Sstevel@tonic-gate main(int argc, char **argv)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	char *cp;
56*c7e4935fSss 	char serdevice[DLPI_LINKNAME_MAX];
577c478bd9Sstevel@tonic-gate 	int do_clear = 0;
587c478bd9Sstevel@tonic-gate 	int period = 0;
597c478bd9Sstevel@tonic-gate 	int isize, osize;
607c478bd9Sstevel@tonic-gate 	int count;
61*c7e4935fSss 	int retval;
627c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
63*c7e4935fSss 	uint_t ppa;
64*c7e4935fSss 	dlpi_handle_t dh;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (argc == 1) {
677c478bd9Sstevel@tonic-gate 		usage();
687c478bd9Sstevel@tonic-gate 		exit(1);
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 	argc--;				/* skip the command name */
717c478bd9Sstevel@tonic-gate 	argv++;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * The following loop processes command line arguments.
757c478bd9Sstevel@tonic-gate 	 * If the argument begins with a '-', it is trated as an option.
767c478bd9Sstevel@tonic-gate 	 * The only option currently implemented is "-c" (clears statistics).
777c478bd9Sstevel@tonic-gate 	 * If the argument begins with a numeral, it is treated as an interval.
787c478bd9Sstevel@tonic-gate 	 * Intervals must be positive integers greater than zero.
797c478bd9Sstevel@tonic-gate 	 * Any argument that survives this is treated as a device name to be
807c478bd9Sstevel@tonic-gate 	 * found under /dev.
817c478bd9Sstevel@tonic-gate 	 */
827c478bd9Sstevel@tonic-gate 	while (argc > 0) {
837c478bd9Sstevel@tonic-gate 		if (argv[0][0] == '-') {
847c478bd9Sstevel@tonic-gate 			if (argc == 1) {
857c478bd9Sstevel@tonic-gate 				usage();
867c478bd9Sstevel@tonic-gate 				exit(1);
877c478bd9Sstevel@tonic-gate 			}
887c478bd9Sstevel@tonic-gate 			if (argv[0][1] != 'c') {
897c478bd9Sstevel@tonic-gate 				usage();
907c478bd9Sstevel@tonic-gate 				exit(1);
917c478bd9Sstevel@tonic-gate 			}
927c478bd9Sstevel@tonic-gate 			do_clear = 1;
937c478bd9Sstevel@tonic-gate 		} else if ((argv[0][0] >= '0') && (argv[0][0] <= '9')) {
947c478bd9Sstevel@tonic-gate 			period = atoi(*argv);
957c478bd9Sstevel@tonic-gate 			if (period == 0) {
967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
97*c7e4935fSss 				    "syncstat: bad interval: %s\n", *argv);
987c478bd9Sstevel@tonic-gate 				exit(1);
997c478bd9Sstevel@tonic-gate 			}
1007c478bd9Sstevel@tonic-gate 		} else {
101*c7e4935fSss 			if (snprintf(sername, sizeof (sername), "%s",
102*c7e4935fSss 			    *argv) >= sizeof (sername)) {
103*c7e4935fSss 				(void) fprintf(stderr, "syncstat: invalid "
104*c7e4935fSss 				    "device name (too long) %s\n", *argv);
1057c478bd9Sstevel@tonic-gate 				    exit(1);
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 		argc--;
1097c478bd9Sstevel@tonic-gate 		argv++;
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
112*c7e4935fSss 	for (cp = sername; (*cp) && (!isdigit(*cp)); cp++) {}
1137c478bd9Sstevel@tonic-gate 	if (*cp == '\0') {	/* hit the end without finding a number */
1147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
115*c7e4935fSss 		    "syncstat: %s missing minor device number\n", sername);
1167c478bd9Sstevel@tonic-gate 		exit(1);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
119*c7e4935fSss 	if ((retval = dlpi_open(sername, &dh, DLPI_SERIAL)) != DLPI_SUCCESS) {
120*c7e4935fSss 		(void) fprintf(stderr, "syncstat: dlpi_open %s: %s\n", sername,
121*c7e4935fSss 		    dlpi_strerror(retval));
1227c478bd9Sstevel@tonic-gate 		exit(1);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
125*c7e4935fSss 	(void) dlpi_parselink(sername, serdevice, &ppa);
126*c7e4935fSss 	(void) printf("syncstat: control device: %s, ppa=%u\n", serdevice, ppa);
1277c478bd9Sstevel@tonic-gate 
128*c7e4935fSss 	fd = dlpi_fd(dh);
1297c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
1307c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
1317c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
1327c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
1337c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &sioc) < 0) {
1347c478bd9Sstevel@tonic-gate 		perror("S_IOCGETMODE");
1357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
136*c7e4935fSss 		    "syncstat: can't get sync mode info for %s\n", sername);
1377c478bd9Sstevel@tonic-gate 		exit(1);
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	if (do_clear) {
1407c478bd9Sstevel@tonic-gate 		sioc.ic_cmd = S_IOCCLRSTATS;
1417c478bd9Sstevel@tonic-gate 		sioc.ic_timout = -1;
1427c478bd9Sstevel@tonic-gate 		sioc.ic_len = sizeof (struct sl_stats);
1437c478bd9Sstevel@tonic-gate 		sioc.ic_dp = (char *)&st;
1447c478bd9Sstevel@tonic-gate 		if (ioctl(fd, I_STR, &sioc) < 0) {
1457c478bd9Sstevel@tonic-gate 			perror("S_IOCCLRSTATS");
1467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
147*c7e4935fSss 			    "syncstat: can't clear stats for %s\n", sername);
1487c478bd9Sstevel@tonic-gate 			exit(1);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
1537c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
1547c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
1557c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&st;
1567c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &sioc) < 0) {
1577c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
1587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "syncstat: can't get stats for %s\n",
159*c7e4935fSss 		    sername);
1607c478bd9Sstevel@tonic-gate 		exit(1);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	if (period) {
1637c478bd9Sstevel@tonic-gate 		if (sm.sm_baudrate == 0) {
1647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "syncstat: baud rate not set\n");
1657c478bd9Sstevel@tonic-gate 			exit(1);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		for (count = 0; ; count++) {
1687c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
1697c478bd9Sstevel@tonic-gate 			(void) sleep(period);
1707c478bd9Sstevel@tonic-gate 			sample(count, period);
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	isize = osize = 0;
1747c478bd9Sstevel@tonic-gate 	if (st.opack)
1757c478bd9Sstevel@tonic-gate 		osize = st.ochar / st.opack;
1767c478bd9Sstevel@tonic-gate 	if (st.ipack)
1777c478bd9Sstevel@tonic-gate 		isize = st.ichar / st.ipack;
178*c7e4935fSss 	(void) printf("    speed   ipkts   opkts  undrun  ovrrun   abort     "
179*c7e4935fSss 	    "crc   isize   osize\n");
180*c7e4935fSss 	(void) printf(" %7d %7d %7d %7d %7d %7d %7d %7d %7d\n", sm.sm_baudrate,
181*c7e4935fSss 	    st.ipack, st.opack, st.underrun, st.overrun, st.abort, st.crc,
182*c7e4935fSss 	    isize, osize);
1837c478bd9Sstevel@tonic-gate 	return (0);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static void
sample(int count,int period)1877c478bd9Sstevel@tonic-gate sample(int count, int period)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	struct sl_stats nst;
1907c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
1917c478bd9Sstevel@tonic-gate 	int iutil, outil;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
1947c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
1957c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
1967c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&nst;
1977c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &sioc) < 0) {
1987c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
1997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "syncstat: can't get stats for %s\n",
200*c7e4935fSss 		    sername);
2017c478bd9Sstevel@tonic-gate 		exit(1);
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	st.ipack = nst.ipack - st.ipack;
2057c478bd9Sstevel@tonic-gate 	st.opack = nst.opack - st.opack;
2067c478bd9Sstevel@tonic-gate 	st.ichar = nst.ichar - st.ichar;
2077c478bd9Sstevel@tonic-gate 	st.ochar = nst.ochar - st.ochar;
2087c478bd9Sstevel@tonic-gate 	st.crc = nst.crc - st.crc;
2097c478bd9Sstevel@tonic-gate 	st.overrun = nst.overrun - st.overrun;
2107c478bd9Sstevel@tonic-gate 	st.underrun = nst.underrun - st.underrun;
2117c478bd9Sstevel@tonic-gate 	st.abort = nst.abort - st.abort;
2127c478bd9Sstevel@tonic-gate 	iutil = 8 * st.ichar / period;
2137c478bd9Sstevel@tonic-gate 	iutil = 100 * iutil / sm.sm_baudrate;
2147c478bd9Sstevel@tonic-gate 	outil = 8 * st.ochar / period;
2157c478bd9Sstevel@tonic-gate 	outil = 100 * outil / sm.sm_baudrate;
216*c7e4935fSss 	if ((count % 20) == 0)
217*c7e4935fSss 		(void) printf("    ipkts   opkts  undrun  ovrrun   abort     "
218*c7e4935fSss 		    "crc   iutil   outil\n");
219*c7e4935fSss 	(void) printf(" %7d %7d %7d %7d %7d %7d %6d%% %6d%%\n", st.ipack,
220*c7e4935fSss 	    st.opack, st.underrun, st.overrun, st.abort, st.crc, iutil, outil);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	st = nst;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static void
usage()2267c478bd9Sstevel@tonic-gate usage()
2277c478bd9Sstevel@tonic-gate {
228*c7e4935fSss 	(void) fprintf(stderr, "Usage: syncstat [-c] device [period]\n");
2297c478bd9Sstevel@tonic-gate }
230