Debian Codegraph @

misc/wifistats_iterate.c

raw
1 /* wifistats_iterate: Reliable, fast monitoring of some wifi stats
2 * Author: Dave Taht
3 * Date: 13 Sept 2016
4 * Copyright (C) 2016 Michael David Taht
5 * Copyright (C) 2016 Toke Høiland-Jørgensen
6
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <assert.h>
29 #include <getopt.h>
30 #include <iconv.h>
31 #include <fcntl.h>
32 #include <math.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35 #include <sys/wait.h>
36 #include <sys/timerfd.h>
37 #include <sys/uio.h>
38
39 /*
40 openwrt's ash shell does not have usleep,
41 nor nanosecond time in the date command.
42 find is expensive
43
44 for i in \$(seq $count); do
45 date '+Time: %s.%N';
46 dir=\$(find /sys/kernel/debug/ieee80211 -name netdev:$interface);
47 for s in \$dir/stations/*; do
48 echo Station: \$(basename \$s);
49 [ -f \$s/airtime ] && echo Airtime: && cat \$s/airtime;
50 [ -f \$s/rc_stats_csv ] && echo RC stats: && cat \$s/rc_stats_csv;
51 done;
52 echo "---";
53 sleep $interval || exit 1;
54 done
55
56 So this C program (which is more accurate and lighter weight
57 than the shell script) needs to be used on things like lede,
58 when high resolution data is needed. In fact, everywhere.
59 */
60
61 #define BUFFERSIZE (1024*1024)
62 #define NSEC_PER_SEC (1000000000.0)
63
64 typedef struct {
65 int rc_stats;
66 int airtime;
67 char macaddr[6*3];
68 char airtime_file[256];
69 char rc_stats_file[256];
70 } station_stats;
71
72 struct arg {
73 int count;
74 struct timespec interval;
75 double finterval;
76 char *filename;
77 char *dev;
78 station_stats *stations;
79 char buffer;
80 };
81
82 typedef struct arg args;
83
84 static const struct option long_options[] = {
85 { "count" , required_argument , NULL , 'c' } ,
86 { "interval" , required_argument , NULL , 'I' } ,
87 { "interface", required_argument , NULL , 'i' } ,
88 { "help" , no_argument , NULL , 'h' } ,
89 { "buffer" , no_argument , NULL , 'b' } ,
90 };
91
92 void usage (char *err) {
93 if(err) fprintf(stderr,"%s\n",err);
94 printf("wifistats_iterate [options]\n");
95 printf(
96 "\t-h --help \n"
97 "\t-b --buffer [buffer up the output locally]\n"
98 "\t-c --count [number of iterations]\n"
99 "\t-I --interval [fractional number of seconds]\n"
100 "\t-i --interface [wifi interface]\n"
101 );
102 exit(-1);
103 }
104
105 // The way I originally did this was to open the fd and lseek to the beginning
106 // That doesn't work with rc_stats, so we need to open the file every time. Sigh.
107 // Or I had a bug elsewhere. ?
108 // If a station goes away, it's handled in stations_read
109
110 int stations_reset(station_stats *stations, int cnt) {
111 for(int i = 0; i < cnt; i++) {
112 if (stations[i].rc_stats)
113 close(stations[i].rc_stats);
114 if (stations[i].airtime)
115 close(stations[i].airtime);
116 stations[i].rc_stats = open(stations[i].rc_stats_file,O_RDONLY);
117 stations[i].airtime = open(stations[i].airtime_file,O_RDONLY);
118 }
119 return 0;
120 }
121
122 int stations_bsize(station_stats *stations, int pad) {
123 int size = 0;
124 int j = 0;
125 for(int i = 0; stations[i].rc_stats > 0; i++) {
126 if((j = lseek(stations[i].rc_stats,0,SEEK_END)) > 0) size+=j+pad;
127 if((j = lseek(stations[i].airtime, 0,SEEK_END)) > 0) size+=j+pad;
128 }
129 return size;
130 }
131
132 // NOTE: You must closedir the resulting pointer
133
134 DIR *dir_exists(char * dir) {
135 DIR* fd;
136 struct dirent* in;
137
138 if (NULL == (fd = opendir (dir)))
139 {
140 perror("Error : Failed to open stations directory");
141 return NULL;
142 }
143 return fd;
144 }
145
146 #define MAXPATHLEN 1024
147
148 int wifi_where(char * dev) {
149 char buf[1024];
150 DIR *fd;
151 for(int i = 0; i < 10; i++) {
152 sprintf(buf,"/sys/kernel/debug/ieee80211/phy%i/netdev:%s/stations",i,dev);
153 if((fd = dir_exists(buf)) != NULL) {
154 closedir(fd);
155 return i;
156 }
157 }
158 return -1;
159 }
160
161 int count_stations(char * dev) {
162 int cnt = 0;
163 DIR* fd;
164 struct dirent* in;
165 char dir[1024];
166 sprintf(dir,"/sys/kernel/debug/ieee80211/phy%i/netdev:%s/stations",
167 wifi_where(dev),dev);
168
169 if ((fd = dir_exists(dir)) == NULL) return -1;
170
171 while ((in = readdir(fd)))
172 {
173 if (!strcmp (in->d_name, "."))
174 continue;
175 if (!strcmp (in->d_name, ".."))
176 continue;
177 cnt++;
178 }
179 closedir(fd);
180 return cnt;
181 }
182
183 int stations_open(char * dev, station_stats *stations, int limit) {
184 int cnt = 0;
185 DIR* fd;
186 int f;
187 struct dirent* in;
188 char dir[MAXPATHLEN];
189 char airtime[MAXPATHLEN];
190 char rc_stats[MAXPATHLEN];
191
192 stations[cnt].rc_stats = stations[cnt].airtime = -1;
193 limit /= 2;
194 sprintf(dir,"/sys/kernel/debug/ieee80211/phy%i/netdev:%s/stations",
195 wifi_where(dev),dev);
196
197 if ((fd = dir_exists(dir)) == NULL) return -1;
198
199 while ((in = readdir(fd)))
200 {
201 if (!strcmp (in->d_name, "."))
202 continue;
203 if (!strcmp (in->d_name, ".."))
204 continue;
205
206 sprintf(stations[cnt].macaddr,"%s",in->d_name);
207 sprintf(stations[cnt].rc_stats_file,"%s/%s/%s",dir,in->d_name,"rc_stats_csv");
208 sprintf(stations[cnt].airtime_file,"%s/%s/%s",dir,in->d_name,"airtime");
209
210 if(++cnt > limit) {
211 perror("Error : Too many stations to process\n");
212 break;
213 }
214 }
215 stations[cnt].rc_stats = stations[cnt].airtime = -1;
216 closedir(fd);
217 return cnt;
218 }
219
220 int stations_close(station_stats *stations, int cnt) {
221 for (int i = 0; i < cnt ; i++) {
222 close(stations[i].rc_stats);
223 close(stations[i].airtime);
224 }
225 return(0);
226 }
227
228 int stations_read(station_stats *s, char * buf, int cnt) {
229 int i = 0;
230 int size = 0;
231
232 while(i < cnt) {
233 int t = 0;
234 size += sprintf(&buf[size],"Station: %s\n",s[i].macaddr);
235 if(s[i].airtime > 0) {
236 size += sprintf(&buf[size],"Airtime:\n");
237 if((t = read(s[i].airtime, &buf[size],8192)) > 0) size += t;
238 }
239 if(s[i].rc_stats > 0) {
240 size += sprintf(&buf[size],"RC stats:\n");
241 if((t = read(s[i].rc_stats,&buf[size],8192)) > 0) size += t;
242 }
243 i++;
244 }
245
246 return size;
247 }
248
249 static void defaults(args *a) {
250 a->filename = NULL;
251 a->dev = NULL;
252 a->stations = NULL;
253 a->finterval=.2;
254 a->count=10;
255 a->interval.tv_nsec = 0;
256 a->interval.tv_sec = 0;
257 a->buffer = 0;
258 }
259
260 #define QSTRING "c:I:f:i:hb"
261
262 int process_options(int argc, char **argv, args *o)
263 {
264 int option_index = 0;
265 int opt = 0;
266 optind = 1;
267
268 while(1)
269 {
270 opt = getopt_long(argc, argv,
271 QSTRING,
272 long_options, &option_index);
273 if(opt == -1) break;
274
275 switch (opt)
276 {
277 case 'c': o->count = strtoul(optarg,NULL,10); break;
278 case 'I': o->finterval = strtod(optarg,NULL); break;
279 case 'f': o->filename = optarg; break;
280 case 'i': o->dev = optarg; break;
281 case 'b': o->buffer = 1; break;
282 case '?':
283 case 'h': usage(NULL); break;
284 default: usage(NULL);
285 }
286 }
287 o->interval.tv_sec = floor(o->finterval);
288 o->interval.tv_nsec = (long long) ((o->finterval - o->interval.tv_sec) * NSEC_PER_SEC);
289 return 0;
290 }
291
292 static int result(int out, int size, int bufsize, char *buffer) {
293 struct timespec cur_time;
294 struct iovec iov[3];
295 int err = 0;
296 char mtime[40];
297 int added = 0;
298 clock_gettime(CLOCK_REALTIME, &cur_time);
299 added = sprintf(mtime,"Time: %ld.%09ld\n",
300 cur_time.tv_sec,cur_time.tv_nsec);
301
302 iov[0].iov_base = mtime;
303 iov[0].iov_len = added;
304
305 iov[1].iov_base = buffer;
306 iov[1].iov_len = size;
307
308 iov[2].iov_base = "---\n";
309 iov[2].iov_len = sizeof("---\n")-1;
310
311 if(bufsize - size > 40) {
312 if(( err = writev(out,iov,3) == -1)) {
313 perror("Write failed - out of disk?");
314 }
315 } else {
316 write(2,"Buffer Overrun\n",sizeof("Buffer Overrun\n"));
317 }
318 return err;
319 }
320
321 // Since this is linux only we can use timerfd for an isochronous clock
322
323 #define STABUF 8192 // hopefully big enough? (802.11ac?)
324
325 int run(args *a)
326 {
327 char tmpfile[] = "/tmp/wifistats_iterateXXXXXX";
328 int out = a->buffer ? mkstemp(tmpfile) : STDOUT_FILENO;
329 station_stats *stations = NULL;
330 char *buf;
331 int c;
332
333 if(a->buffer && !out) {
334 perror("Unable to create tmpfile");
335 exit(-1);
336 } else {
337 unlink(tmpfile); // make it disappear on close
338 }
339
340 // if (!a->filename)
341 // usage("Must specify filename");
342
343 if (!a->dev)
344 usage("Must specify wifi device");
345
346 if((c = count_stations(a->dev)) > 0) {
347 if((a->stations = malloc(2*c*sizeof(station_stats))) == NULL)
348 {
349 perror("Unable to allocate memory");
350 exit(-1);
351 }
352 if((buf = malloc(2*c*STABUF)) == NULL)
353 {
354 perror("Unable to allocate memory");
355 exit(-1);
356 }
357 } else {
358 usage("No stations found");
359 }
360
361 if((c = stations_open(a->dev,a->stations,512)) < 1) usage("No stations found");
362
363 struct itimerspec new_value = {0};
364
365 int timer = timerfd_create(CLOCK_REALTIME, 0);
366 new_value.it_interval = a->interval;
367 new_value.it_value = a->interval;
368
369 /* better method would be to poll for input (since writes from the
370 tool could block or return no output for some reason), timestamp the input,
371 and if the difference is less than half, skip this round.
372 this would absorb non-completing stuff */
373
374 char buffer[BUFFERSIZE];
375 int size = 0;
376 int ctr = 0;
377 stations = a->stations;
378
379 timerfd_settime(timer,0,&new_value,NULL); // relative timer
380
381 do {
382 int err;
383 long long fired;
384 if(read(timer,&fired,sizeof(fired))!=8) perror("reading timer");
385 ctr+=fired;
386
387 stations_reset(stations,c);
388 if((size = stations_read(stations,buffer,c)) > 0) {
389 err = result(out,size,BUFFERSIZE,buffer);
390 } else {
391 err = result(out,0,BUFFERSIZE,buffer);
392 perror("reading file");
393 }
394 if(err<0) break;
395 } while (ctr < a->count);
396
397 if(a->buffer) {
398 lseek(out, 0, SEEK_SET);
399 while((size = read(out, buffer, sizeof(buffer))) > 0)
400 write(STDOUT_FILENO,buffer,size);
401 }
402
403 close(out);
404 close(timer);
405 stations_close(stations,c);
406 free(buf);
407 free(stations);
408 a->stations = NULL;
409 return 0;
410 }
411
412 int main(int argc,char **argv) {
413 args a;
414 int status = 0;
415 defaults(&a);
416 process_options(argc, argv, &a);
417 run(&a);
418 return status;
419 }