cnwctl.c revision 1.5 1 1.5 sommerfe /* $NetBSD: cnwctl.c,v 1.5 1999/12/01 03:40:51 sommerfeld 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.1 itojun char *e, *interface;
64 1.1 itojun struct ifreq ifr;
65 1.1 itojun struct cnwistats cnwis, onwis;
66 1.1 itojun struct cnwstatus cnws;
67 1.1 itojun struct ttysize ts;
68 1.1 itojun
69 1.1 itojun domain = -1;
70 1.1 itojun key = -1;
71 1.1 itojun Sflag = sflag = 0;
72 1.1 itojun rate = 0;
73 1.1 itojun interface = "cnw0";
74 1.1 itojun
75 1.1 itojun while ((c = getopt(argc, argv, "d:i:k:sS")) != EOF)
76 1.1 itojun switch (c) {
77 1.1 itojun case 'd':
78 1.1 itojun domain = strtol(optarg, &e, 0);
79 1.1 itojun if (e == optarg || *e || domain & ~ 0x1ff)
80 1.1 itojun errx(1, "%s: invalid domain", optarg);
81 1.1 itojun break;
82 1.1 itojun case 'i':
83 1.1 itojun interface = optarg;
84 1.1 itojun break;
85 1.1 itojun case 'k':
86 1.1 itojun key = strtol(optarg, &e, 0);
87 1.1 itojun if (e == optarg || *e || key & ~ 0xffff)
88 1.1 itojun errx(1, "%s: invalid scramble key", optarg);
89 1.1 itojun break;
90 1.1 itojun case 'S':
91 1.1 itojun ++Sflag;
92 1.1 itojun break;
93 1.1 itojun case 's':
94 1.1 itojun ++sflag;
95 1.1 itojun break;
96 1.1 itojun default: usage:
97 1.1 itojun fprintf(stderr, "usage: cnwctl [-i interface] [-d domain] [-k key] [-sS [rate]]\n");
98 1.1 itojun exit(1);
99 1.1 itojun }
100 1.1 itojun
101 1.1 itojun switch (argc - optind) {
102 1.1 itojun case 0:
103 1.1 itojun break;
104 1.1 itojun case 1:
105 1.1 itojun if (sflag == 0 && Sflag == 0)
106 1.1 itojun goto usage;
107 1.1 itojun if (sflag && Sflag)
108 1.1 itojun errx(1, "only one of -s and -S may be specified with a rate");
109 1.1 itojun rate = strtol(argv[optind], &e, 0);
110 1.1 itojun if (e == optarg || *e || rate < 1)
111 1.1 itojun errx(1, "%s: invalid rate", optarg);
112 1.1 itojun break;
113 1.1 itojun default:
114 1.1 itojun goto usage;
115 1.1 itojun }
116 1.1 itojun
117 1.1 itojun if ((skt = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
118 1.1 itojun err(1, "socket(AF_INET, SOCK_DGRAM)");
119 1.1 itojun
120 1.1 itojun if (key >= 0) {
121 1.1 itojun memset(&ifr, 0, sizeof(ifr));
122 1.1 itojun strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
123 1.1 itojun ifr.ifr_key = key;
124 1.1 itojun if (ioctl(skt, SIOCSCNWKEY, (caddr_t)&ifr) < 0)
125 1.1 itojun err(1, "SIOCSCNWKEY");
126 1.1 itojun }
127 1.1 itojun
128 1.1 itojun if (domain >= 0) {
129 1.1 itojun memset(&ifr, 0, sizeof(ifr));
130 1.1 itojun strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
131 1.1 itojun ifr.ifr_domain = domain;
132 1.1 itojun if (ioctl(skt, SIOCSCNWDOMAIN, (caddr_t)&ifr) < 0)
133 1.1 itojun err(1, "SIOCSCNWDOMAIN");
134 1.1 itojun }
135 1.1 itojun
136 1.1 itojun if (sflag == 0 && Sflag == 0)
137 1.1 itojun exit (0);
138 1.1 itojun
139 1.1 itojun if (Sflag) {
140 1.1 itojun memset(&cnws, 0, sizeof(cnws));
141 1.1 itojun strncpy(cnws.ifr.ifr_name, interface,
142 1.1 itojun sizeof(cnws.ifr.ifr_name));
143 1.1 itojun if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
144 1.1 itojun err(1, "SIOCGCNWSTATUS");
145 1.1 itojun }
146 1.1 itojun
147 1.1 itojun if (sflag) {
148 1.1 itojun memset(&cnwis, 0, sizeof(cnwis));
149 1.1 itojun strncpy(cnwis.ifr.ifr_name, interface,
150 1.1 itojun sizeof(cnwis.ifr.ifr_name));
151 1.1 itojun if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
152 1.1 itojun err(1, "SIOCGCNWSTATS");
153 1.1 itojun cnwis.stats.nws_txretries[0] = 0;
154 1.1 itojun
155 1.1 itojun for (i = 1; i < 16; ++i)
156 1.1 itojun cnwis.stats.nws_txretries[0] +=
157 1.1 itojun cnwis.stats.nws_txretries[i] * i;
158 1.1 itojun }
159 1.1 itojun
160 1.1 itojun if (rate == 0 && sflag) {
161 1.1 itojun memset(&ifr, 0, sizeof(ifr));
162 1.1 itojun strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
163 1.1 itojun if (ioctl(skt, SIOCGCNWDOMAIN, (caddr_t)&ifr) < 0)
164 1.1 itojun err(1, "SIOCGCNWDOMAIN");
165 1.1 itojun printf("%s:\n 0x%03x domain\n", interface, ifr.ifr_domain);
166 1.5 sommerfe printf("%10llu rx\n", (unsigned long long)cnwis.stats.nws_rx);
167 1.4 sommerfe printf("%10llu rxoverflow\n",
168 1.5 sommerfe (unsigned long long)cnwis.stats.nws_rxoverflow);
169 1.4 sommerfe printf("%10llu rxoverrun\n",
170 1.5 sommerfe (unsigned long long)cnwis.stats.nws_rxoverrun);
171 1.4 sommerfe printf("%10llu rxcrcerror\n",
172 1.5 sommerfe (unsigned long long)cnwis.stats.nws_rxcrcerror);
173 1.4 sommerfe printf("%10llu rxframe\n",
174 1.5 sommerfe (unsigned long long)cnwis.stats.nws_rxframe);
175 1.4 sommerfe printf("%10llu rxerrors\n",
176 1.5 sommerfe (unsigned long long)cnwis.stats.nws_rxerrors);
177 1.5 sommerfe printf("%10llu rxavail\n",
178 1.5 sommerfe (unsigned long long)cnwis.stats.nws_rxavail);
179 1.5 sommerfe
180 1.5 sommerfe printf("%10llu tx\n", (unsigned long long)cnwis.stats.nws_tx);
181 1.5 sommerfe printf("%10llu txokay\n",
182 1.5 sommerfe (unsigned long long)cnwis.stats.nws_txokay);
183 1.5 sommerfe printf("%10llu txabort\n",
184 1.5 sommerfe (unsigned long long)cnwis.stats.nws_txabort);
185 1.4 sommerfe printf("%10llu txlostcd\n",
186 1.5 sommerfe (unsigned long long)cnwis.stats.nws_txlostcd);
187 1.4 sommerfe printf("%10llu txerrors\n",
188 1.5 sommerfe (unsigned long long)cnwis.stats.nws_txerrors);
189 1.4 sommerfe printf("%10llu txretries\n",
190 1.5 sommerfe (unsigned long long)cnwis.stats.nws_txretries[0]);
191 1.1 itojun for (i = 1; i < 16; ++i)
192 1.1 itojun if (cnwis.stats.nws_txretries[i])
193 1.4 sommerfe printf("%10s %10llu %dx retries\n", "",
194 1.5 sommerfe (unsigned long long)
195 1.5 sommerfe cnwis.stats.nws_txretries[i],
196 1.4 sommerfe i);
197 1.1 itojun }
198 1.1 itojun
199 1.1 itojun if (rate == 0 && Sflag) {
200 1.1 itojun printf(" 0x%02x link integrity field\n",
201 1.1 itojun cnws.data[0x4e]);
202 1.1 itojun printf(" 0x%02x connection quality\n",
203 1.1 itojun cnws.data[0x54]);
204 1.1 itojun printf(" 0x%02x spu\n",
205 1.1 itojun cnws.data[0x55]);
206 1.1 itojun printf(" 0x%02x link quality\n",
207 1.1 itojun cnws.data[0x56]);
208 1.1 itojun printf(" 0x%02x hhc\n",
209 1.1 itojun cnws.data[0x58]);
210 1.1 itojun printf(" 0x%02x mhs\n",
211 1.1 itojun cnws.data[0x6b]);
212 1.1 itojun printf(" %04x %04x revision\n",
213 1.1 itojun *(u_short *)&cnws.data[0x66], *(u_short *)&cnws.data[0x68]);
214 1.1 itojun printf(" %c%c id\n",
215 1.1 itojun cnws.data[0x6e], cnws.data[0x6f]);
216 1.1 itojun }
217 1.1 itojun
218 1.1 itojun if (rate == 0)
219 1.1 itojun exit (0);
220 1.1 itojun
221 1.1 itojun if (ioctl(0, TIOCGWINSZ, &ts) < 0)
222 1.1 itojun ts.ts_lines = 24;
223 1.1 itojun c = 0;
224 1.1 itojun
225 1.1 itojun if (Sflag) for (;;) {
226 1.1 itojun if (c-- == 0) {
227 1.1 itojun printf("lif cq spu lq hhc mhs\n");
228 1.1 itojun c = ts.ts_lines - 3;
229 1.1 itojun }
230 1.1 itojun printf(" %02x %02x %02x %02x %02x %02x\n",
231 1.1 itojun cnws.data[0x4e],
232 1.1 itojun cnws.data[0x54],
233 1.1 itojun cnws.data[0x55],
234 1.1 itojun cnws.data[0x56],
235 1.1 itojun cnws.data[0x58],
236 1.1 itojun cnws.data[0x6b]);
237 1.1 itojun fflush(stdout);
238 1.1 itojun if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
239 1.1 itojun err(1, "SIOCGCNWSTATUS");
240 1.1 itojun sleep (rate);
241 1.1 itojun }
242 1.1 itojun
243 1.1 itojun for (;;) {
244 1.1 itojun if (c-- == 0) {
245 1.1 itojun printf("%10s %10s %10s %10s %10s %10s\n",
246 1.1 itojun "tx-request", "tx-okay", "tx-error", "tx-retry",
247 1.1 itojun "rx", "rx-error");
248 1.1 itojun c = ts.ts_lines - 3;
249 1.1 itojun memset(&onwis, 0, sizeof(onwis));
250 1.1 itojun }
251 1.5 sommerfe printf("%10llu ", (unsigned long long)
252 1.5 sommerfe (cnwis.stats.nws_tx - onwis.stats.nws_tx));
253 1.5 sommerfe printf("%10llu ", (unsigned long long)
254 1.5 sommerfe (cnwis.stats.nws_txokay - onwis.stats.nws_txokay));
255 1.5 sommerfe printf("%10llu ", (unsigned long long)
256 1.5 sommerfe (cnwis.stats.nws_txerrors - onwis.stats.nws_txerrors));
257 1.5 sommerfe printf("%10llu ", (unsigned long long)
258 1.5 sommerfe (cnwis.stats.nws_txretries[0] -
259 1.5 sommerfe onwis.stats.nws_txretries[0]));
260 1.5 sommerfe printf("%10llu ", (unsigned long long)
261 1.5 sommerfe (cnwis.stats.nws_rx - onwis.stats.nws_rx));
262 1.5 sommerfe printf("%10llu\n", (unsigned long long)
263 1.5 sommerfe (cnwis.stats.nws_rxerrors - onwis.stats.nws_rxerrors));
264 1.1 itojun fflush(stdout);
265 1.1 itojun sleep (rate);
266 1.1 itojun onwis = cnwis;
267 1.1 itojun
268 1.1 itojun if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
269 1.1 itojun err(1, "SIOCGCNWSTATS");
270 1.1 itojun cnwis.stats.nws_txretries[0] = 0;
271 1.1 itojun for (i = 1; i < 16; ++i)
272 1.1 itojun cnwis.stats.nws_txretries[0] +=
273 1.1 itojun cnwis.stats.nws_txretries[i] * i;
274 1.1 itojun }
275 1.1 itojun }
276