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