cnwctl.c revision 1.2 1 1.2 itojun /* $NetBSD: cnwctl.c,v 1.2 1999/11/29 12:55:08 itojun 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.2 itojun printf("%qu rx\n", cnwis.stats.nws_rx);
167 1.2 itojun printf("%qu rxoverflow\n", cnwis.stats.nws_rxoverflow);
168 1.2 itojun printf("%qu rxoverrun\n", cnwis.stats.nws_rxoverrun);
169 1.2 itojun printf("%qu rxcrcerror\n", cnwis.stats.nws_rxcrcerror);
170 1.2 itojun printf("%qu rxframe\n", cnwis.stats.nws_rxframe);
171 1.2 itojun printf("%qu rxerrors\n", cnwis.stats.nws_rxerrors);
172 1.2 itojun printf("%qu rxavail\n", cnwis.stats.nws_rxavail);
173 1.2 itojun
174 1.2 itojun printf("%qu tx\n", cnwis.stats.nws_tx);
175 1.2 itojun printf("%qu txokay\n", cnwis.stats.nws_txokay);
176 1.2 itojun printf("%qu txabort\n", cnwis.stats.nws_txabort);
177 1.2 itojun printf("%qu txlostcd\n", cnwis.stats.nws_txlostcd);
178 1.2 itojun printf("%qu txerrors\n", cnwis.stats.nws_txerrors);
179 1.2 itojun printf("%qu txretries\n", cnwis.stats.nws_txretries[0]);
180 1.1 itojun for (i = 1; i < 16; ++i)
181 1.1 itojun if (cnwis.stats.nws_txretries[i])
182 1.2 itojun printf("%10s %qu %dx retries\n", "",
183 1.1 itojun cnwis.stats.nws_txretries[i], i);
184 1.1 itojun }
185 1.1 itojun
186 1.1 itojun if (rate == 0 && Sflag) {
187 1.1 itojun printf(" 0x%02x link integrity field\n",
188 1.1 itojun cnws.data[0x4e]);
189 1.1 itojun printf(" 0x%02x connection quality\n",
190 1.1 itojun cnws.data[0x54]);
191 1.1 itojun printf(" 0x%02x spu\n",
192 1.1 itojun cnws.data[0x55]);
193 1.1 itojun printf(" 0x%02x link quality\n",
194 1.1 itojun cnws.data[0x56]);
195 1.1 itojun printf(" 0x%02x hhc\n",
196 1.1 itojun cnws.data[0x58]);
197 1.1 itojun printf(" 0x%02x mhs\n",
198 1.1 itojun cnws.data[0x6b]);
199 1.1 itojun printf(" %04x %04x revision\n",
200 1.1 itojun *(u_short *)&cnws.data[0x66], *(u_short *)&cnws.data[0x68]);
201 1.1 itojun printf(" %c%c id\n",
202 1.1 itojun cnws.data[0x6e], cnws.data[0x6f]);
203 1.1 itojun }
204 1.1 itojun
205 1.1 itojun if (rate == 0)
206 1.1 itojun exit (0);
207 1.1 itojun
208 1.1 itojun if (ioctl(0, TIOCGWINSZ, &ts) < 0)
209 1.1 itojun ts.ts_lines = 24;
210 1.1 itojun c = 0;
211 1.1 itojun
212 1.1 itojun if (Sflag) for (;;) {
213 1.1 itojun if (c-- == 0) {
214 1.1 itojun printf("lif cq spu lq hhc mhs\n");
215 1.1 itojun c = ts.ts_lines - 3;
216 1.1 itojun }
217 1.1 itojun printf(" %02x %02x %02x %02x %02x %02x\n",
218 1.1 itojun cnws.data[0x4e],
219 1.1 itojun cnws.data[0x54],
220 1.1 itojun cnws.data[0x55],
221 1.1 itojun cnws.data[0x56],
222 1.1 itojun cnws.data[0x58],
223 1.1 itojun cnws.data[0x6b]);
224 1.1 itojun fflush(stdout);
225 1.1 itojun if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
226 1.1 itojun err(1, "SIOCGCNWSTATUS");
227 1.1 itojun sleep (rate);
228 1.1 itojun }
229 1.1 itojun
230 1.1 itojun for (;;) {
231 1.1 itojun if (c-- == 0) {
232 1.1 itojun printf("%10s %10s %10s %10s %10s %10s\n",
233 1.1 itojun "tx-request", "tx-okay", "tx-error", "tx-retry",
234 1.1 itojun "rx", "rx-error");
235 1.1 itojun c = ts.ts_lines - 3;
236 1.1 itojun memset(&onwis, 0, sizeof(onwis));
237 1.1 itojun }
238 1.2 itojun printf("%10qu ", cnwis.stats.nws_tx - onwis.stats.nws_tx);
239 1.2 itojun printf("%10qu ", cnwis.stats.nws_txokay - onwis.stats.nws_txokay);
240 1.2 itojun printf("%10qu ", cnwis.stats.nws_txerrors - onwis.stats.nws_txerrors);
241 1.2 itojun printf("%10qu ", cnwis.stats.nws_txretries[0] - onwis.stats.nws_txretries[0]);
242 1.2 itojun printf("%10qu ", cnwis.stats.nws_rx - onwis.stats.nws_rx);
243 1.2 itojun printf("%10qu\n", cnwis.stats.nws_rxerrors - onwis.stats.nws_rxerrors);
244 1.1 itojun fflush(stdout);
245 1.1 itojun sleep (rate);
246 1.1 itojun onwis = cnwis;
247 1.1 itojun
248 1.1 itojun if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
249 1.1 itojun err(1, "SIOCGCNWSTATS");
250 1.1 itojun cnwis.stats.nws_txretries[0] = 0;
251 1.1 itojun for (i = 1; i < 16; ++i)
252 1.1 itojun cnwis.stats.nws_txretries[0] +=
253 1.1 itojun cnwis.stats.nws_txretries[i] * i;
254 1.1 itojun }
255 1.1 itojun }
256