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