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