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