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