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*c7e4935fSss150715 * Common Development and Distribution License (the "License"). 6*c7e4935fSss150715 * 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*c7e4935fSss150715 * 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 #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Display synchronous serial line statistics 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <ctype.h> 347c478bd9Sstevel@tonic-gate #include <unistd.h> 357c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 367c478bd9Sstevel@tonic-gate #include <stdlib.h> 377c478bd9Sstevel@tonic-gate #include <stdio.h> 387c478bd9Sstevel@tonic-gate #include <string.h> 397c478bd9Sstevel@tonic-gate #include <sys/stream.h> 407c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 417c478bd9Sstevel@tonic-gate #include <fcntl.h> 427c478bd9Sstevel@tonic-gate #include <sys/ser_sync.h> 437c478bd9Sstevel@tonic-gate #include <libdlpi.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static struct scc_mode sm; 467c478bd9Sstevel@tonic-gate static struct sl_stats st; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate static void usage(void); 497c478bd9Sstevel@tonic-gate static void sample(int count, int period); 507c478bd9Sstevel@tonic-gate 51*c7e4935fSss150715 static char sername[DLPI_LINKNAME_MAX]; 527c478bd9Sstevel@tonic-gate static int fd; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate int 557c478bd9Sstevel@tonic-gate main(int argc, char **argv) 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate char *cp; 58*c7e4935fSss150715 char serdevice[DLPI_LINKNAME_MAX]; 597c478bd9Sstevel@tonic-gate int do_clear = 0; 607c478bd9Sstevel@tonic-gate int period = 0; 617c478bd9Sstevel@tonic-gate int isize, osize; 627c478bd9Sstevel@tonic-gate int count; 63*c7e4935fSss150715 int retval; 647c478bd9Sstevel@tonic-gate struct strioctl sioc; 65*c7e4935fSss150715 uint_t ppa; 66*c7e4935fSss150715 dlpi_handle_t dh; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate if (argc == 1) { 697c478bd9Sstevel@tonic-gate usage(); 707c478bd9Sstevel@tonic-gate exit(1); 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate argc--; /* skip the command name */ 737c478bd9Sstevel@tonic-gate argv++; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* 767c478bd9Sstevel@tonic-gate * The following loop processes command line arguments. 777c478bd9Sstevel@tonic-gate * If the argument begins with a '-', it is trated as an option. 787c478bd9Sstevel@tonic-gate * The only option currently implemented is "-c" (clears statistics). 797c478bd9Sstevel@tonic-gate * If the argument begins with a numeral, it is treated as an interval. 807c478bd9Sstevel@tonic-gate * Intervals must be positive integers greater than zero. 817c478bd9Sstevel@tonic-gate * Any argument that survives this is treated as a device name to be 827c478bd9Sstevel@tonic-gate * found under /dev. 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate while (argc > 0) { 857c478bd9Sstevel@tonic-gate if (argv[0][0] == '-') { 867c478bd9Sstevel@tonic-gate if (argc == 1) { 877c478bd9Sstevel@tonic-gate usage(); 887c478bd9Sstevel@tonic-gate exit(1); 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate if (argv[0][1] != 'c') { 917c478bd9Sstevel@tonic-gate usage(); 927c478bd9Sstevel@tonic-gate exit(1); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate do_clear = 1; 957c478bd9Sstevel@tonic-gate } else if ((argv[0][0] >= '0') && (argv[0][0] <= '9')) { 967c478bd9Sstevel@tonic-gate period = atoi(*argv); 977c478bd9Sstevel@tonic-gate if (period == 0) { 987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 997c478bd9Sstevel@tonic-gate "syncstat: bad interval: %s\n", *argv); 1007c478bd9Sstevel@tonic-gate exit(1); 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate } else { 103*c7e4935fSss150715 if (snprintf(sername, sizeof (sername), "%s", 104*c7e4935fSss150715 *argv) >= sizeof (sername)) { 105*c7e4935fSss150715 (void) fprintf(stderr, "syncstat: invalid " 106*c7e4935fSss150715 "device name (too long) %s\n", *argv); 1077c478bd9Sstevel@tonic-gate exit(1); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate argc--; 1117c478bd9Sstevel@tonic-gate argv++; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 114*c7e4935fSss150715 for (cp = sername; (*cp) && (!isdigit(*cp)); cp++) {} 1157c478bd9Sstevel@tonic-gate if (*cp == '\0') { /* hit the end without finding a number */ 1167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 117*c7e4935fSss150715 "syncstat: %s missing minor device number\n", sername); 1187c478bd9Sstevel@tonic-gate exit(1); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 121*c7e4935fSss150715 if ((retval = dlpi_open(sername, &dh, DLPI_SERIAL)) != DLPI_SUCCESS) { 122*c7e4935fSss150715 (void) fprintf(stderr, "syncstat: dlpi_open %s: %s\n", sername, 123*c7e4935fSss150715 dlpi_strerror(retval)); 1247c478bd9Sstevel@tonic-gate exit(1); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 127*c7e4935fSss150715 (void) dlpi_parselink(sername, serdevice, &ppa); 128*c7e4935fSss150715 (void) printf("syncstat: control device: %s, ppa=%u\n", serdevice, ppa); 1297c478bd9Sstevel@tonic-gate 130*c7e4935fSss150715 fd = dlpi_fd(dh); 1317c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETMODE; 1327c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 1337c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct scc_mode); 1347c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&sm; 1357c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 1367c478bd9Sstevel@tonic-gate perror("S_IOCGETMODE"); 1377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 138*c7e4935fSss150715 "syncstat: can't get sync mode info for %s\n", sername); 1397c478bd9Sstevel@tonic-gate exit(1); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate if (do_clear) { 1427c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCCLRSTATS; 1437c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 1447c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct sl_stats); 1457c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&st; 1467c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 1477c478bd9Sstevel@tonic-gate perror("S_IOCCLRSTATS"); 1487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 149*c7e4935fSss150715 "syncstat: can't clear stats for %s\n", sername); 1507c478bd9Sstevel@tonic-gate exit(1); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETSTATS; 1557c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 1567c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct sl_stats); 1577c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&st; 1587c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 1597c478bd9Sstevel@tonic-gate perror("S_IOCGETSTATS"); 1607c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "syncstat: can't get stats for %s\n", 161*c7e4935fSss150715 sername); 1627c478bd9Sstevel@tonic-gate exit(1); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate if (period) { 1657c478bd9Sstevel@tonic-gate if (sm.sm_baudrate == 0) { 1667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "syncstat: baud rate not set\n"); 1677c478bd9Sstevel@tonic-gate exit(1); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate for (count = 0; ; count++) { 1707c478bd9Sstevel@tonic-gate (void) fflush(stdout); 1717c478bd9Sstevel@tonic-gate (void) sleep(period); 1727c478bd9Sstevel@tonic-gate sample(count, period); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate isize = osize = 0; 1767c478bd9Sstevel@tonic-gate if (st.opack) 1777c478bd9Sstevel@tonic-gate osize = st.ochar / st.opack; 1787c478bd9Sstevel@tonic-gate if (st.ipack) 1797c478bd9Sstevel@tonic-gate isize = st.ichar / st.ipack; 180*c7e4935fSss150715 (void) printf(" speed ipkts opkts undrun ovrrun abort " 181*c7e4935fSss150715 "crc isize osize\n"); 182*c7e4935fSss150715 (void) printf(" %7d %7d %7d %7d %7d %7d %7d %7d %7d\n", sm.sm_baudrate, 183*c7e4935fSss150715 st.ipack, st.opack, st.underrun, st.overrun, st.abort, st.crc, 1847c478bd9Sstevel@tonic-gate isize, osize); 1857c478bd9Sstevel@tonic-gate return (0); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate static void 1897c478bd9Sstevel@tonic-gate sample(int count, int period) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate struct sl_stats nst; 1927c478bd9Sstevel@tonic-gate struct strioctl sioc; 1937c478bd9Sstevel@tonic-gate int iutil, outil; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETSTATS; 1967c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 1977c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct sl_stats); 1987c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&nst; 1997c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 2007c478bd9Sstevel@tonic-gate perror("S_IOCGETSTATS"); 2017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "syncstat: can't get stats for %s\n", 202*c7e4935fSss150715 sername); 2037c478bd9Sstevel@tonic-gate exit(1); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate st.ipack = nst.ipack - st.ipack; 2077c478bd9Sstevel@tonic-gate st.opack = nst.opack - st.opack; 2087c478bd9Sstevel@tonic-gate st.ichar = nst.ichar - st.ichar; 2097c478bd9Sstevel@tonic-gate st.ochar = nst.ochar - st.ochar; 2107c478bd9Sstevel@tonic-gate st.crc = nst.crc - st.crc; 2117c478bd9Sstevel@tonic-gate st.overrun = nst.overrun - st.overrun; 2127c478bd9Sstevel@tonic-gate st.underrun = nst.underrun - st.underrun; 2137c478bd9Sstevel@tonic-gate st.abort = nst.abort - st.abort; 2147c478bd9Sstevel@tonic-gate iutil = 8 * st.ichar / period; 2157c478bd9Sstevel@tonic-gate iutil = 100 * iutil / sm.sm_baudrate; 2167c478bd9Sstevel@tonic-gate outil = 8 * st.ochar / period; 2177c478bd9Sstevel@tonic-gate outil = 100 * outil / sm.sm_baudrate; 218*c7e4935fSss150715 if ((count % 20) == 0) 219*c7e4935fSss150715 (void) printf(" ipkts opkts undrun ovrrun abort " 220*c7e4935fSss150715 "crc iutil outil\n"); 221*c7e4935fSss150715 (void) printf(" %7d %7d %7d %7d %7d %7d %6d%% %6d%%\n", st.ipack, 222*c7e4935fSss150715 st.opack, st.underrun, st.overrun, st.abort, st.crc, iutil, outil); 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate st = nst; 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate static void 2287c478bd9Sstevel@tonic-gate usage() 2297c478bd9Sstevel@tonic-gate { 230*c7e4935fSss150715 (void) fprintf(stderr, "Usage: syncstat [-c] device [period]\n"); 2317c478bd9Sstevel@tonic-gate } 232