]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/tools/mwl/mwldebug/mwldebug.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / tools / mwl / mwldebug / mwldebug.c
1 /*-
2  * Copyright (c) 2007 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31
32 /*
33  * mwldebug [-i interface] flags
34  * (default interface is mwl0).
35  */
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/file.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <ctype.h>
46 #include <getopt.h>
47 #include <string.h>
48 #include <err.h>
49
50 #define N(a)    (sizeof(a)/sizeof(a[0]))
51
52 const char *progname;
53
54 enum {
55         MWL_DEBUG_XMIT          = 0x00000001,   /* basic xmit operation */
56         MWL_DEBUG_XMIT_DESC     = 0x00000002,   /* xmit descriptors */
57         MWL_DEBUG_RECV          = 0x00000004,   /* basic recv operation */
58         MWL_DEBUG_RECV_DESC     = 0x00000008,   /* recv descriptors */
59         MWL_DEBUG_RESET         = 0x00000010,   /* reset processing */
60         MWL_DEBUG_BEACON        = 0x00000020,   /* beacon handling */
61         MWL_DEBUG_INTR          = 0x00000040,   /* ISR */
62         MWL_DEBUG_TX_PROC       = 0x00000080,   /* tx ISR proc */
63         MWL_DEBUG_RX_PROC       = 0x00000100,   /* rx ISR proc */
64         MWL_DEBUG_KEYCACHE      = 0x00000200,   /* key cache management */
65         MWL_DEBUG_STATE         = 0x00000400,   /* 802.11 state transitions */
66         MWL_DEBUG_NODE          = 0x00000800,   /* node management */
67         MWL_DEBUG_RECV_ALL      = 0x00001000,   /* trace all frames (beacons) */
68         MWL_DEBUG_TSO           = 0x00002000,   /* TSO processing */
69         MWL_DEBUG_AMPDU         = 0x00004000,   /* BA stream handling */
70         MWL_DEBUG_ANY           = 0xffffffff
71 };
72
73 static struct {
74         const char      *name;
75         u_int           bit;
76 } flags[] = {
77         { "xmit",       MWL_DEBUG_XMIT },
78         { "xmit_desc",  MWL_DEBUG_XMIT_DESC },
79         { "recv",       MWL_DEBUG_RECV },
80         { "recv_desc",  MWL_DEBUG_RECV_DESC },
81         { "reset",      MWL_DEBUG_RESET },
82         { "beacon",     MWL_DEBUG_BEACON },
83         { "intr",       MWL_DEBUG_INTR },
84         { "xmit_proc",  MWL_DEBUG_TX_PROC },
85         { "recv_proc",  MWL_DEBUG_RX_PROC },
86         { "keycache",   MWL_DEBUG_KEYCACHE },
87         { "state",      MWL_DEBUG_STATE },
88         { "node",       MWL_DEBUG_NODE },
89         { "recv_all",   MWL_DEBUG_RECV_ALL },
90         { "tso",        MWL_DEBUG_TSO },
91         { "ampdu",      MWL_DEBUG_AMPDU },
92         /* XXX these are a hack; there should be a separate sysctl knob */
93         { "hal",        0x02000000 },           /* cmd-completion processing */
94         { "hal2",       0x01000000 },           /* cmd submission processing */
95         { "halhang",    0x04000000 },           /* disable fw hang stuff */
96 };
97
98 static u_int
99 getflag(const char *name, int len)
100 {
101         int i;
102
103         for (i = 0; i < N(flags); i++)
104                 if (strncasecmp(flags[i].name, name, len) == 0)
105                         return flags[i].bit;
106         return 0;
107 }
108
109 #if 0
110 static const char *
111 getflagname(u_int flag)
112 {
113         int i;
114
115         for (i = 0; i < N(flags); i++)
116                 if (flags[i].bit == flag)
117                         return flags[i].name;
118         return "???";
119 }
120 #endif
121
122 static void
123 usage(void)
124 {
125         int i;
126
127         fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
128         fprintf(stderr, "where flags are:\n");
129         for (i = 0; i < N(flags); i++)
130                 printf("%s\n", flags[i].name);
131         exit(-1);
132 }
133
134 int
135 main(int argc, char *argv[])
136 {
137         const char *ifname = "mwl0";
138         const char *cp, *tp;
139         const char *sep;
140         int c, op, i;
141         u_int32_t debug, ndebug;
142         size_t debuglen;
143         char oid[256];
144
145         progname = argv[0];
146         if (argc > 1) {
147                 if (strcmp(argv[1], "-i") == 0) {
148                         if (argc < 2)
149                                 errx(1, "missing interface name for -i option");
150                         ifname = argv[2];
151                         if (strncmp(ifname, "mv", 2) != 0)
152                                 errx(2, "huh, this is for mv devices?");
153                         argc -= 2, argv += 2;
154                 } else if (strcmp(argv[1], "-?") == 0)
155                         usage();
156         }
157
158         snprintf(oid, sizeof(oid), "dev.mwl.%s.debug", ifname+3);
159         debuglen = sizeof(debug);
160         if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
161                 err(1, "sysctl-get(%s)", oid);
162         ndebug = debug;
163         for (; argc > 1; argc--, argv++) {
164                 cp = argv[1];
165                 do {
166                         u_int bit;
167
168                         if (*cp == '-') {
169                                 cp++;
170                                 op = -1;
171                         } else if (*cp == '+') {
172                                 cp++;
173                                 op = 1;
174                         } else
175                                 op = 0;
176                         for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
177                                 tp++;
178                         bit = getflag(cp, tp-cp);
179                         if (op < 0)
180                                 ndebug &= ~bit;
181                         else if (op > 0)
182                                 ndebug |= bit;
183                         else {
184                                 if (bit == 0) {
185                                         c = *cp;
186                                         if (isdigit(c))
187                                                 bit = strtoul(cp, NULL, 0);
188                                         else
189                                                 errx(1, "unknown flag %.*s",
190                                                         (int)(tp-cp), cp);
191                                 }
192                                 ndebug = bit;
193                         }
194                 } while (*(cp = tp) != '\0');
195         }
196         if (debug != ndebug) {
197                 printf("%s: 0x%x => ", oid, debug);
198                 if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
199                         err(1, "sysctl-set(%s)", oid);
200                 printf("0x%x", ndebug);
201                 debug = ndebug;
202         } else
203                 printf("%s: 0x%x", oid, debug);
204         sep = "<";
205         for (i = 0; i < N(flags); i++)
206                 if (debug & flags[i].bit) {
207                         printf("%s%s", sep, flags[i].name);
208                         sep = ",";
209                 }
210         printf("%s\n", *sep != '<' ? ">" : "");
211         return 0;
212 }