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 * Display synchronous serial line statistics 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate #include <ctype.h> 35*7c478bd9Sstevel@tonic-gate #include <unistd.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 37*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 38*7c478bd9Sstevel@tonic-gate #include <stdio.h> 39*7c478bd9Sstevel@tonic-gate #include <string.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/stream.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 42*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/ser_sync.h> 44*7c478bd9Sstevel@tonic-gate #include <libdlpi.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #define MAXWAIT 15 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate static struct scc_mode sm; 49*7c478bd9Sstevel@tonic-gate static struct sl_stats st; 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate static void usage(void); 52*7c478bd9Sstevel@tonic-gate static void sample(int count, int period); 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate /* 55*7c478bd9Sstevel@tonic-gate * errstr is MAXPATHLEN + 11; 11 = 10 for "syncstat: " + null 56*7c478bd9Sstevel@tonic-gate */ 57*7c478bd9Sstevel@tonic-gate static char errstr[11 + MAXPATHLEN] = "syncstat: /dev/"; 58*7c478bd9Sstevel@tonic-gate static char *ifdevice = errstr + 10; 59*7c478bd9Sstevel@tonic-gate static char *ifname = errstr + 15; 60*7c478bd9Sstevel@tonic-gate static int fd; 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate int 63*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 64*7c478bd9Sstevel@tonic-gate { 65*7c478bd9Sstevel@tonic-gate int len; 66*7c478bd9Sstevel@tonic-gate char *cp; 67*7c478bd9Sstevel@tonic-gate int do_clear = 0; 68*7c478bd9Sstevel@tonic-gate int period = 0; 69*7c478bd9Sstevel@tonic-gate int isize, osize; 70*7c478bd9Sstevel@tonic-gate int count; 71*7c478bd9Sstevel@tonic-gate struct strioctl sioc; 72*7c478bd9Sstevel@tonic-gate ulong_t ppa; 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate if (argc == 1) { 75*7c478bd9Sstevel@tonic-gate usage(); 76*7c478bd9Sstevel@tonic-gate exit(1); 77*7c478bd9Sstevel@tonic-gate } 78*7c478bd9Sstevel@tonic-gate argc--; /* skip the command name */ 79*7c478bd9Sstevel@tonic-gate argv++; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate /* 82*7c478bd9Sstevel@tonic-gate * The following loop processes command line arguments. 83*7c478bd9Sstevel@tonic-gate * If the argument begins with a '-', it is trated as an option. 84*7c478bd9Sstevel@tonic-gate * The only option currently implemented is "-c" (clears statistics). 85*7c478bd9Sstevel@tonic-gate * If the argument begins with a numeral, it is treated as an interval. 86*7c478bd9Sstevel@tonic-gate * Intervals must be positive integers greater than zero. 87*7c478bd9Sstevel@tonic-gate * Any argument that survives this is treated as a device name to be 88*7c478bd9Sstevel@tonic-gate * found under /dev. 89*7c478bd9Sstevel@tonic-gate */ 90*7c478bd9Sstevel@tonic-gate while (argc > 0) { 91*7c478bd9Sstevel@tonic-gate if (argv[0][0] == '-') { 92*7c478bd9Sstevel@tonic-gate if (argc == 1) { 93*7c478bd9Sstevel@tonic-gate usage(); 94*7c478bd9Sstevel@tonic-gate exit(1); 95*7c478bd9Sstevel@tonic-gate } 96*7c478bd9Sstevel@tonic-gate if (argv[0][1] != 'c') { 97*7c478bd9Sstevel@tonic-gate usage(); 98*7c478bd9Sstevel@tonic-gate exit(1); 99*7c478bd9Sstevel@tonic-gate } 100*7c478bd9Sstevel@tonic-gate do_clear = 1; 101*7c478bd9Sstevel@tonic-gate } else if ((argv[0][0] >= '0') && (argv[0][0] <= '9')) { 102*7c478bd9Sstevel@tonic-gate period = atoi(*argv); 103*7c478bd9Sstevel@tonic-gate if (period == 0) { 104*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 105*7c478bd9Sstevel@tonic-gate "syncstat: bad interval: %s\n", *argv); 106*7c478bd9Sstevel@tonic-gate exit(1); 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate } else { 109*7c478bd9Sstevel@tonic-gate len = sizeof (errstr) - strlen(errstr); 110*7c478bd9Sstevel@tonic-gate if (snprintf(ifname, len, "%s", *argv) >= len) { 111*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 112*7c478bd9Sstevel@tonic-gate "syncstat: invalid device name " 113*7c478bd9Sstevel@tonic-gate "(too long) %s\n", *argv); 114*7c478bd9Sstevel@tonic-gate exit(1); 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate } 117*7c478bd9Sstevel@tonic-gate argc--; 118*7c478bd9Sstevel@tonic-gate argv++; 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate for (cp = ifname; (*cp) && (!isdigit(*cp)); cp++) {} 122*7c478bd9Sstevel@tonic-gate if (*cp == '\0') { /* hit the end without finding a number */ 123*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 124*7c478bd9Sstevel@tonic-gate "syncstat: %s missing minor device number\n", ifname); 125*7c478bd9Sstevel@tonic-gate exit(1); 126*7c478bd9Sstevel@tonic-gate } 127*7c478bd9Sstevel@tonic-gate ppa = strtoul(cp, NULL, 10); 128*7c478bd9Sstevel@tonic-gate *cp = '\0'; /* drop number, leaving name of clone device. */ 129*7c478bd9Sstevel@tonic-gate fd = open(ifdevice, O_RDWR); 130*7c478bd9Sstevel@tonic-gate if (fd < 0) { 131*7c478bd9Sstevel@tonic-gate perror(errstr); 132*7c478bd9Sstevel@tonic-gate exit(1); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate if (dlpi_attach(fd, MAXWAIT, ppa) != 0) { 136*7c478bd9Sstevel@tonic-gate perror("syncstat: dlpi_attach"); 137*7c478bd9Sstevel@tonic-gate exit(1); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate (void) printf("syncstat: control device: %s, ppa=%ld\n", ifdevice, ppa); 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETMODE; 143*7c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 144*7c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct scc_mode); 145*7c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&sm; 146*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 147*7c478bd9Sstevel@tonic-gate perror("S_IOCGETMODE"); 148*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 149*7c478bd9Sstevel@tonic-gate "syncstat: can't get sync mode info for %s\n", 150*7c478bd9Sstevel@tonic-gate ifname); 151*7c478bd9Sstevel@tonic-gate exit(1); 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate if (do_clear) { 154*7c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCCLRSTATS; 155*7c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 156*7c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct sl_stats); 157*7c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&st; 158*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 159*7c478bd9Sstevel@tonic-gate perror("S_IOCCLRSTATS"); 160*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 161*7c478bd9Sstevel@tonic-gate "syncstat: can't clear stats for %s\n", 162*7c478bd9Sstevel@tonic-gate ifname); 163*7c478bd9Sstevel@tonic-gate exit(1); 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate } 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETSTATS; 168*7c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 169*7c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct sl_stats); 170*7c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&st; 171*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 172*7c478bd9Sstevel@tonic-gate perror("S_IOCGETSTATS"); 173*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "syncstat: can't get stats for %s\n", 174*7c478bd9Sstevel@tonic-gate ifname); 175*7c478bd9Sstevel@tonic-gate exit(1); 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate if (period) { 178*7c478bd9Sstevel@tonic-gate if (sm.sm_baudrate == 0) { 179*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "syncstat: baud rate not set\n"); 180*7c478bd9Sstevel@tonic-gate exit(1); 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate for (count = 0; ; count++) { 183*7c478bd9Sstevel@tonic-gate (void) fflush(stdout); 184*7c478bd9Sstevel@tonic-gate (void) sleep(period); 185*7c478bd9Sstevel@tonic-gate sample(count, period); 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate isize = osize = 0; 189*7c478bd9Sstevel@tonic-gate if (st.opack) 190*7c478bd9Sstevel@tonic-gate osize = st.ochar / st.opack; 191*7c478bd9Sstevel@tonic-gate if (st.ipack) 192*7c478bd9Sstevel@tonic-gate isize = st.ichar / st.ipack; 193*7c478bd9Sstevel@tonic-gate (void) printf( 194*7c478bd9Sstevel@tonic-gate " speed ipkts opkts undrun ovrrun abort crc isize osize\n"); 195*7c478bd9Sstevel@tonic-gate (void) printf(" %7d %7d %7d %7d %7d %7d %7d %7d %7d\n", 196*7c478bd9Sstevel@tonic-gate sm.sm_baudrate, 197*7c478bd9Sstevel@tonic-gate st.ipack, 198*7c478bd9Sstevel@tonic-gate st.opack, 199*7c478bd9Sstevel@tonic-gate st.underrun, 200*7c478bd9Sstevel@tonic-gate st.overrun, 201*7c478bd9Sstevel@tonic-gate st.abort, 202*7c478bd9Sstevel@tonic-gate st.crc, 203*7c478bd9Sstevel@tonic-gate isize, osize); 204*7c478bd9Sstevel@tonic-gate return (0); 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate static void 208*7c478bd9Sstevel@tonic-gate sample(int count, int period) 209*7c478bd9Sstevel@tonic-gate { 210*7c478bd9Sstevel@tonic-gate struct sl_stats nst; 211*7c478bd9Sstevel@tonic-gate struct strioctl sioc; 212*7c478bd9Sstevel@tonic-gate int iutil, outil; 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETSTATS; 215*7c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 216*7c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct sl_stats); 217*7c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&nst; 218*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 219*7c478bd9Sstevel@tonic-gate perror("S_IOCGETSTATS"); 220*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "syncstat: can't get stats for %s\n", 221*7c478bd9Sstevel@tonic-gate ifname); 222*7c478bd9Sstevel@tonic-gate exit(1); 223*7c478bd9Sstevel@tonic-gate } 224*7c478bd9Sstevel@tonic-gate 225*7c478bd9Sstevel@tonic-gate st.ipack = nst.ipack - st.ipack; 226*7c478bd9Sstevel@tonic-gate st.opack = nst.opack - st.opack; 227*7c478bd9Sstevel@tonic-gate st.ichar = nst.ichar - st.ichar; 228*7c478bd9Sstevel@tonic-gate st.ochar = nst.ochar - st.ochar; 229*7c478bd9Sstevel@tonic-gate st.crc = nst.crc - st.crc; 230*7c478bd9Sstevel@tonic-gate st.overrun = nst.overrun - st.overrun; 231*7c478bd9Sstevel@tonic-gate st.underrun = nst.underrun - st.underrun; 232*7c478bd9Sstevel@tonic-gate st.abort = nst.abort - st.abort; 233*7c478bd9Sstevel@tonic-gate iutil = 8 * st.ichar / period; 234*7c478bd9Sstevel@tonic-gate iutil = 100 * iutil / sm.sm_baudrate; 235*7c478bd9Sstevel@tonic-gate outil = 8 * st.ochar / period; 236*7c478bd9Sstevel@tonic-gate outil = 100 * outil / sm.sm_baudrate; 237*7c478bd9Sstevel@tonic-gate if ((count % 20) == 0) (void) printf( 238*7c478bd9Sstevel@tonic-gate " ipkts opkts undrun ovrrun abort crc iutil outil\n"); 239*7c478bd9Sstevel@tonic-gate (void) printf(" %7d %7d %7d %7d %7d %7d %6d%% %6d%%\n", 240*7c478bd9Sstevel@tonic-gate st.ipack, 241*7c478bd9Sstevel@tonic-gate st.opack, 242*7c478bd9Sstevel@tonic-gate st.underrun, 243*7c478bd9Sstevel@tonic-gate st.overrun, 244*7c478bd9Sstevel@tonic-gate st.abort, 245*7c478bd9Sstevel@tonic-gate st.crc, 246*7c478bd9Sstevel@tonic-gate iutil, outil); 247*7c478bd9Sstevel@tonic-gate 248*7c478bd9Sstevel@tonic-gate st = nst; 249*7c478bd9Sstevel@tonic-gate } 250*7c478bd9Sstevel@tonic-gate 251*7c478bd9Sstevel@tonic-gate static void 252*7c478bd9Sstevel@tonic-gate usage() 253*7c478bd9Sstevel@tonic-gate { 254*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", 255*7c478bd9Sstevel@tonic-gate "Usage: syncstat [-c] device [period]"); 256*7c478bd9Sstevel@tonic-gate } 257