Home | History | Annotate | Line # | Download | only in cnwctl
cnwctl.c revision 1.1
      1 /*
      2  * Copyright (c) 1997 Berkeley Software Design, Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that this notice is retained,
      7  * the conditions in the following notices are met, and terms applying
      8  * to contributors in the following notices also apply to Berkeley
      9  * Software Design, Inc.
     10  *
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by
     19  *	Berkeley Software Design, Inc.
     20  * 4. Neither the name of the Berkeley Software Design, Inc. nor the names
     21  *    of its contributors may be used to endorse or promote products derived
     22  *    from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *      PAO2 Id: cnwctl.c,v 1.1.1.1 1997/12/11 14:46:06 itojun Exp
     37  */
     38 
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/cdefs.h>
     42 #include <sys/errno.h>
     43 #include <sys/socket.h>
     44 #include <sys/ioctl.h>
     45 
     46 #include <net/if.h>
     47 
     48 #include <machine/if_cnwioctl.h>
     49 
     50 #include <err.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <time.h>
     55 #include <unistd.h>
     56 
     57 void
     58 main(int argc, char **argv)
     59 {
     60 	int c, domain, i, key, rate, sflag, Sflag, skt;
     61 	char *e, *interface;
     62 	struct ifreq ifr;
     63         struct cnwistats cnwis, onwis;
     64         struct cnwstatus cnws;
     65 	struct ttysize ts;
     66 
     67 	domain = -1;
     68 	key = -1;
     69 	Sflag = sflag = 0;
     70 	rate = 0;
     71 	interface = "cnw0";
     72 
     73 	while ((c = getopt(argc, argv, "d:i:k:sS")) != EOF)
     74 		switch (c) {
     75 		case 'd':
     76 			domain = strtol(optarg, &e, 0);
     77 			if (e == optarg || *e || domain & ~ 0x1ff)
     78 				errx(1, "%s: invalid domain", optarg);
     79 			break;
     80 		case 'i':
     81 			interface = optarg;
     82 			break;
     83 		case 'k':
     84 			key = strtol(optarg, &e, 0);
     85 			if (e == optarg || *e || key & ~ 0xffff)
     86 				errx(1, "%s: invalid scramble key", optarg);
     87 			break;
     88 		case 'S':
     89 			++Sflag;
     90 			break;
     91 		case 's':
     92 			++sflag;
     93 			break;
     94 		default: usage:
     95 			fprintf(stderr, "usage: cnwctl [-i interface] [-d domain] [-k key] [-sS [rate]]\n");
     96 			exit(1);
     97 		}
     98 
     99 	switch (argc - optind) {
    100 	case 0:
    101 		break;
    102 	case 1:
    103 		if (sflag == 0 && Sflag == 0)
    104 			goto usage;
    105 		if (sflag && Sflag)
    106 			errx(1, "only one of -s and -S may be specified with a rate");
    107 		rate = strtol(argv[optind], &e, 0);
    108 		if (e == optarg || *e || rate < 1)
    109 			errx(1, "%s: invalid rate", optarg);
    110 		break;
    111 	default:
    112 		goto usage;
    113 	}
    114 
    115         if ((skt = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    116                 err(1, "socket(AF_INET, SOCK_DGRAM)");
    117 
    118 	if (key >= 0) {
    119 		memset(&ifr, 0, sizeof(ifr));
    120 		strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    121 		ifr.ifr_key = key;
    122 		if (ioctl(skt, SIOCSCNWKEY, (caddr_t)&ifr) < 0)
    123 			err(1, "SIOCSCNWKEY");
    124 	}
    125 
    126 	if (domain >= 0) {
    127 		memset(&ifr, 0, sizeof(ifr));
    128 		strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    129 		ifr.ifr_domain = domain;
    130 		if (ioctl(skt, SIOCSCNWDOMAIN, (caddr_t)&ifr) < 0)
    131 			err(1, "SIOCSCNWDOMAIN");
    132 	}
    133 
    134 	if (sflag == 0 && Sflag == 0)
    135 		exit (0);
    136 
    137 	if (Sflag) {
    138         	memset(&cnws, 0, sizeof(cnws));
    139 		strncpy(cnws.ifr.ifr_name, interface,
    140 		    sizeof(cnws.ifr.ifr_name));
    141 		if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
    142 			err(1, "SIOCGCNWSTATUS");
    143 	}
    144 
    145 	if (sflag) {
    146 		memset(&cnwis, 0, sizeof(cnwis));
    147 		strncpy(cnwis.ifr.ifr_name, interface,
    148 		    sizeof(cnwis.ifr.ifr_name));
    149 		if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
    150 			err(1, "SIOCGCNWSTATS");
    151 		cnwis.stats.nws_txretries[0] = 0;
    152 
    153 		for (i = 1; i < 16; ++i)
    154 			cnwis.stats.nws_txretries[0] +=
    155 			    cnwis.stats.nws_txretries[i] * i;
    156 	}
    157 
    158 	if (rate == 0 && sflag) {
    159 		memset(&ifr, 0, sizeof(ifr));
    160 		strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    161 		if (ioctl(skt, SIOCGCNWDOMAIN, (caddr_t)&ifr) < 0)
    162 			err(1, "SIOCGCNWDOMAIN");
    163 		printf("%s:\n     0x%03x domain\n", interface, ifr.ifr_domain);
    164 			printf("%10d rx\n", cnwis.stats.nws_rx);
    165 		printf("%10d rxoverflow\n", cnwis.stats.nws_rxoverflow);
    166 		printf("%10d rxoverrun\n", cnwis.stats.nws_rxoverrun);
    167 		printf("%10d rxcrcerror\n", cnwis.stats.nws_rxcrcerror);
    168 		printf("%10d rxframe\n", cnwis.stats.nws_rxframe);
    169 		printf("%10d rxerrors\n", cnwis.stats.nws_rxerrors);
    170 		printf("%10d rxavail\n", cnwis.stats.nws_rxavail);
    171 
    172 		printf("%10d tx\n", cnwis.stats.nws_tx);
    173 		printf("%10d txokay\n", cnwis.stats.nws_txokay);
    174 		printf("%10d txabort\n", cnwis.stats.nws_txabort);
    175 		printf("%10d txlostcd\n", cnwis.stats.nws_txlostcd);
    176 		printf("%10d txerrors\n", cnwis.stats.nws_txerrors);
    177 		printf("%10d txretries\n", cnwis.stats.nws_txretries[0]);
    178 		for (i = 1; i < 16; ++i)
    179 			if (cnwis.stats.nws_txretries[i])
    180 				printf("%10s %10d %dx retries\n", "",
    181 				    cnwis.stats.nws_txretries[i], i);
    182 	}
    183 
    184 	if (rate == 0 && Sflag) {
    185 		printf("      0x%02x link integrity field\n",
    186 		    cnws.data[0x4e]);
    187 		printf("      0x%02x connection quality\n",
    188 		    cnws.data[0x54]);
    189 		printf("      0x%02x spu\n",
    190 		    cnws.data[0x55]);
    191 		printf("      0x%02x link quality\n",
    192 		    cnws.data[0x56]);
    193 		printf("      0x%02x hhc\n",
    194 		    cnws.data[0x58]);
    195 		printf("      0x%02x mhs\n",
    196 		    cnws.data[0x6b]);
    197 		printf(" %04x %04x revision\n",
    198 		    *(u_short *)&cnws.data[0x66], *(u_short *)&cnws.data[0x68]);
    199 		printf("        %c%c id\n",
    200 		    cnws.data[0x6e], cnws.data[0x6f]);
    201 	}
    202 
    203 	if (rate == 0)
    204 		exit (0);
    205 
    206 	if (ioctl(0, TIOCGWINSZ, &ts) < 0)
    207 		ts.ts_lines = 24;
    208 	c = 0;
    209 
    210 	if (Sflag) for (;;) {
    211 		if (c-- == 0) {
    212 			printf("lif  cq spu  lq hhc mhs\n");
    213 			c = ts.ts_lines - 3;
    214 		}
    215 		printf(" %02x  %02x  %02x  %02x  %02x  %02x\n",
    216                     cnws.data[0x4e],
    217                     cnws.data[0x54],
    218                     cnws.data[0x55],
    219                     cnws.data[0x56],
    220                     cnws.data[0x58],
    221 		    cnws.data[0x6b]);
    222 		fflush(stdout);
    223 		if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
    224 			err(1, "SIOCGCNWSTATUS");
    225 		sleep (rate);
    226 	}
    227 
    228 	for (;;) {
    229 		if (c-- == 0) {
    230 			printf("%10s %10s %10s %10s %10s %10s\n",
    231 			    "tx-request", "tx-okay", "tx-error", "tx-retry",
    232 			    "rx", "rx-error");
    233 			c = ts.ts_lines - 3;
    234 			memset(&onwis, 0, sizeof(onwis));
    235 		}
    236 		printf("%10d ", cnwis.stats.nws_tx - onwis.stats.nws_tx);
    237 		printf("%10d ", cnwis.stats.nws_txokay - onwis.stats.nws_txokay);
    238 		printf("%10d ", cnwis.stats.nws_txerrors - onwis.stats.nws_txerrors);
    239 		printf("%10d ", cnwis.stats.nws_txretries[0] - onwis.stats.nws_txretries[0]);
    240 		printf("%10d ", cnwis.stats.nws_rx - onwis.stats.nws_rx);
    241 		printf("%10d\n", cnwis.stats.nws_rxerrors - onwis.stats.nws_rxerrors);
    242 		fflush(stdout);
    243 		sleep (rate);
    244 		onwis = cnwis;
    245 
    246 		if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
    247 			err(1, "SIOCGCNWSTATS");
    248 		cnwis.stats.nws_txretries[0] = 0;
    249 		for (i = 1; i < 16; ++i)
    250 			cnwis.stats.nws_txretries[0] +=
    251 			    cnwis.stats.nws_txretries[i] * i;
    252 	}
    253 }
    254