]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/mwl/mwldebug/mwldebug.c
MFV: zlib 1.3
[FreeBSD/FreeBSD.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
30 /*
31  * mwldebug [-i interface] flags
32  * (default interface is mwl0).
33  */
34
35 #include <sys/param.h>
36 #include <sys/file.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40
41 #include <ctype.h>
42 #include <err.h>
43 #include <getopt.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 const char *progname;
49
50 enum {
51         MWL_DEBUG_XMIT          = 0x00000001,   /* basic xmit operation */
52         MWL_DEBUG_XMIT_DESC     = 0x00000002,   /* xmit descriptors */
53         MWL_DEBUG_RECV          = 0x00000004,   /* basic recv operation */
54         MWL_DEBUG_RECV_DESC     = 0x00000008,   /* recv descriptors */
55         MWL_DEBUG_RESET         = 0x00000010,   /* reset processing */
56         MWL_DEBUG_BEACON        = 0x00000020,   /* beacon handling */
57         MWL_DEBUG_INTR          = 0x00000040,   /* ISR */
58         MWL_DEBUG_TX_PROC       = 0x00000080,   /* tx ISR proc */
59         MWL_DEBUG_RX_PROC       = 0x00000100,   /* rx ISR proc */
60         MWL_DEBUG_KEYCACHE      = 0x00000200,   /* key cache management */
61         MWL_DEBUG_STATE         = 0x00000400,   /* 802.11 state transitions */
62         MWL_DEBUG_NODE          = 0x00000800,   /* node management */
63         MWL_DEBUG_RECV_ALL      = 0x00001000,   /* trace all frames (beacons) */
64         MWL_DEBUG_TSO           = 0x00002000,   /* TSO processing */
65         MWL_DEBUG_AMPDU         = 0x00004000,   /* BA stream handling */
66         MWL_DEBUG_ANY           = 0xffffffff
67 };
68
69 static struct {
70         const char      *name;
71         u_int           bit;
72 } flags[] = {
73         { "xmit",       MWL_DEBUG_XMIT },
74         { "xmit_desc",  MWL_DEBUG_XMIT_DESC },
75         { "recv",       MWL_DEBUG_RECV },
76         { "recv_desc",  MWL_DEBUG_RECV_DESC },
77         { "reset",      MWL_DEBUG_RESET },
78         { "beacon",     MWL_DEBUG_BEACON },
79         { "intr",       MWL_DEBUG_INTR },
80         { "xmit_proc",  MWL_DEBUG_TX_PROC },
81         { "recv_proc",  MWL_DEBUG_RX_PROC },
82         { "keycache",   MWL_DEBUG_KEYCACHE },
83         { "state",      MWL_DEBUG_STATE },
84         { "node",       MWL_DEBUG_NODE },
85         { "recv_all",   MWL_DEBUG_RECV_ALL },
86         { "tso",        MWL_DEBUG_TSO },
87         { "ampdu",      MWL_DEBUG_AMPDU },
88         /* XXX these are a hack; there should be a separate sysctl knob */
89         { "hal",        0x02000000 },           /* cmd-completion processing */
90         { "hal2",       0x01000000 },           /* cmd submission processing */
91         { "halhang",    0x04000000 },           /* disable fw hang stuff */
92 };
93
94 static u_int
95 getflag(const char *name, int len)
96 {
97         int i;
98
99         for (i = 0; i < nitems(flags); i++)
100                 if (strncasecmp(flags[i].name, name, len) == 0)
101                         return flags[i].bit;
102         return 0;
103 }
104
105 #if 0
106 static const char *
107 getflagname(u_int flag)
108 {
109         int i;
110
111         for (i = 0; i < nitems(flags); i++)
112                 if (flags[i].bit == flag)
113                         return flags[i].name;
114         return "???";
115 }
116 #endif
117
118 static void
119 usage(void)
120 {
121         int i;
122
123         fprintf(stderr, "usage: %s [-i device] [flags]\n", progname);
124         fprintf(stderr, "where flags are:\n");
125         for (i = 0; i < nitems(flags); i++)
126                 printf("%s\n", flags[i].name);
127         exit(-1);
128 }
129
130 int
131 main(int argc, char *argv[])
132 {
133         const char *ifname = "mwl0";
134         const char *cp, *tp;
135         const char *sep;
136         int c, op, i;
137         u_int32_t debug, ndebug;
138         size_t debuglen;
139         char oid[256];
140
141         progname = argv[0];
142         if (argc > 1) {
143                 if (strcmp(argv[1], "-i") == 0) {
144                         if (argc < 2)
145                                 errx(1, "missing interface name for -i option");
146                         ifname = argv[2];
147                         if (strncmp(ifname, "mv", 2) != 0)
148                                 errx(2, "huh, this is for mv devices?");
149                         argc -= 2, argv += 2;
150                 } else if (strcmp(argv[1], "-?") == 0)
151                         usage();
152         }
153
154         snprintf(oid, sizeof(oid), "dev.mwl.%s.debug", ifname+3);
155         debuglen = sizeof(debug);
156         if (sysctlbyname(oid, &debug, &debuglen, NULL, 0) < 0)
157                 err(1, "sysctl-get(%s)", oid);
158         ndebug = debug;
159         for (; argc > 1; argc--, argv++) {
160                 cp = argv[1];
161                 do {
162                         u_int bit;
163
164                         if (*cp == '-') {
165                                 cp++;
166                                 op = -1;
167                         } else if (*cp == '+') {
168                                 cp++;
169                                 op = 1;
170                         } else
171                                 op = 0;
172                         for (tp = cp; *tp != '\0' && *tp != '+' && *tp != '-';)
173                                 tp++;
174                         bit = getflag(cp, tp-cp);
175                         if (op < 0)
176                                 ndebug &= ~bit;
177                         else if (op > 0)
178                                 ndebug |= bit;
179                         else {
180                                 if (bit == 0) {
181                                         c = *cp;
182                                         if (isdigit(c))
183                                                 bit = strtoul(cp, NULL, 0);
184                                         else
185                                                 errx(1, "unknown flag %.*s",
186                                                         (int)(tp-cp), cp);
187                                 }
188                                 ndebug = bit;
189                         }
190                 } while (*(cp = tp) != '\0');
191         }
192         if (debug != ndebug) {
193                 printf("%s: 0x%x => ", oid, debug);
194                 if (sysctlbyname(oid, NULL, NULL, &ndebug, sizeof(ndebug)) < 0)
195                         err(1, "sysctl-set(%s)", oid);
196                 printf("0x%x", ndebug);
197                 debug = ndebug;
198         } else
199                 printf("%s: 0x%x", oid, debug);
200         sep = "<";
201         for (i = 0; i < nitems(flags); i++)
202                 if (debug & flags[i].bit) {
203                         printf("%s%s", sep, flags[i].name);
204                         sep = ",";
205                 }
206         printf("%s\n", *sep != '<' ? ">" : "");
207         return 0;
208 }