Instead of converting between 802.11 frequencies and channels umpteen
[obnox/wireshark/wip.git] / tap-iostat.c
1 /* tap-iostat.c
2  * iostat   2002 Ronnie Sahlberg
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include "epan/epan_dissect.h"
37 #include "epan/packet_info.h"
38 #include <epan/tap.h>
39 #include <epan/stat_cmd_args.h>
40 #include "register.h"
41
42
43 typedef struct _io_stat_t {
44         gint32 interval;        /* unit is ms */
45         guint32 num_items;
46         struct _io_stat_item_t *items;
47         const char **filters;
48 } io_stat_t;    
49
50 #define CALC_TYPE_BYTES 0
51 #define CALC_TYPE_COUNT 1
52 #define CALC_TYPE_SUM   2
53 #define CALC_TYPE_MIN   3
54 #define CALC_TYPE_MAX   4
55 #define CALC_TYPE_AVG   5
56
57 typedef struct _io_stat_item_t {
58         io_stat_t *parent;
59         struct _io_stat_item_t *next;
60         struct _io_stat_item_t *prev;
61         gint32 time;            /* unit is ms since start of capture */
62         int calc_type;
63         int hf_index;
64         guint32 frames;
65         guint32 num;
66         guint32 counter;
67 } io_stat_item_t;
68
69
70 static int
71 iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *dummy _U_)
72 {
73         io_stat_item_t *mit = arg;
74         io_stat_item_t *it;
75         gint32 current_time;
76         GPtrArray *gp;
77         guint i;
78
79         current_time=(gint32) ((pinfo->fd->rel_ts.secs*1000)+(pinfo->fd->rel_ts.nsecs/1000000));
80
81         /* the prev item before the main one is always the last interval we saw packets for */
82         it=mit->prev;
83
84         /* XXX for the time being, just ignore all frames that are in the past.
85            should be fixed in the future but hopefully it is uncommon */
86         if(current_time<it->time){
87                 return FALSE;
88         }
89
90         /* we have moved into a new interval, we need to create a new struct */
91         if(current_time>=(it->time+mit->parent->interval)){
92                 it->next=g_malloc(sizeof(io_stat_item_t));
93                 it->next->prev=it;
94                 it->next->next=NULL;
95                 it=it->next;
96                 mit->prev=it;
97
98                 it->time=(current_time / mit->parent->interval) * mit->parent->interval;
99                 it->frames=0;
100                 it->counter=0;
101                 it->num=0;
102                 it->calc_type=it->prev->calc_type;
103                 it->hf_index=it->prev->hf_index;
104         }
105
106         /* it will now give us the current structure to use to store the data in */
107         it->frames++;
108
109         switch(it->calc_type){
110         case CALC_TYPE_BYTES:
111                 it->counter+=pinfo->fd->pkt_len;
112                 break;
113         case CALC_TYPE_COUNT:
114                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
115                 if(gp){
116                         it->counter+=gp->len;
117                 }
118                 break;
119         case CALC_TYPE_SUM:
120                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
121                 if(gp){
122                         for(i=0;i<gp->len;i++){
123                                 switch(proto_registrar_get_ftype(it->hf_index)){
124                                 case FT_UINT8:
125                                 case FT_UINT16:
126                                 case FT_UINT24:
127                                 case FT_UINT32:
128                                         it->counter+=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
129                                         break;
130                                 case FT_INT8:
131                                 case FT_INT16:
132                                 case FT_INT24:
133                                 case FT_INT32:
134                                         it->counter+=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
135                                         break;
136                                 }
137                         }
138                 }
139                 break;
140         case CALC_TYPE_MIN:
141                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
142                 if(gp){
143                         int type;
144                         guint32 val;
145                         nstime_t *new_time;
146
147                         type=proto_registrar_get_ftype(it->hf_index);
148                         for(i=0;i<gp->len;i++){
149                                 switch(type){
150                                 case FT_UINT8:
151                                 case FT_UINT16:
152                                 case FT_UINT24:
153                                 case FT_UINT32:
154                                         val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
155                                         if((it->frames==1)&&(i==0)){
156                                                 it->counter=val;
157                                         } else if(val<it->counter){
158                                                 it->counter=val;
159                                         }                               
160                                         break;
161                                 case FT_INT8:
162                                 case FT_INT16:
163                                 case FT_INT24:
164                                 case FT_INT32:
165                                         val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
166                                         if((it->frames==1)&&(i==0)){
167                                                 it->counter=val;
168                                         } else if((gint32)val<(gint32)(it->counter)){
169                                                 it->counter=val;
170                                         }
171                                         break;
172                                 case FT_RELATIVE_TIME:
173                                         new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
174                                         val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
175                                         if((it->frames==1)&&(i==0)){
176                                                 it->counter=val;
177                                         } else if(val<it->counter){
178                                                 it->counter=val;
179                                         }                               
180                                         break;
181                                 }
182                         }
183                 }
184                 break;
185         case CALC_TYPE_MAX:
186                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
187                 if(gp){
188                         int type;
189                         guint32 val;
190                         nstime_t *new_time;
191
192                         type=proto_registrar_get_ftype(it->hf_index);
193                         for(i=0;i<gp->len;i++){
194                                 switch(type){
195                                 case FT_UINT8:
196                                 case FT_UINT16:
197                                 case FT_UINT24:
198                                 case FT_UINT32:
199                                         val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
200                                         if((it->frames==1)&&(i==0)){
201                                                 it->counter=val;
202                                         } else if(val>it->counter){
203                                                 it->counter=val;
204                                         }                               
205                                         break;
206                                 case FT_INT8:
207                                 case FT_INT16:
208                                 case FT_INT24:
209                                 case FT_INT32:
210                                         val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
211                                         if((it->frames==1)&&(i==0)){
212                                                 it->counter=val;
213                                         } else if((gint32)val>(gint32)(it->counter)){
214                                                 it->counter=val;
215                                         }                               
216                                         break;
217                                 case FT_RELATIVE_TIME:
218                                         new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
219                                         val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
220                                         if((it->frames==1)&&(i==0)){
221                                                 it->counter=val;
222                                         } else if(val>it->counter){
223                                                 it->counter=val;
224                                         }                               
225                                         break;
226                                 }
227                         }
228                 }
229                 break;
230         case CALC_TYPE_AVG:
231                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
232                 if(gp){
233                         int type;
234                         guint32 val;
235                         nstime_t *new_time;
236
237                         type=proto_registrar_get_ftype(it->hf_index);
238                         for(i=0;i<gp->len;i++){
239                                 it->num++;
240                                 switch(type){
241                                 case FT_UINT8:
242                                 case FT_UINT16:
243                                 case FT_UINT24:
244                                 case FT_UINT32:
245                                         val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
246                                         it->counter+=val;
247                                         break;
248                                 case FT_INT8:
249                                 case FT_INT16:
250                                 case FT_INT24:
251                                 case FT_INT32:
252                                         val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
253                                         it->counter+=val;
254                                         break;
255                                 case FT_RELATIVE_TIME:
256                                         new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
257                                         val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
258                                         it->counter+=val;
259                                         break;
260                                 }
261                         }
262                 }
263                 break;
264         }
265
266         return TRUE;
267 }
268
269 static void
270 iostat_draw(void *arg)
271 {
272         io_stat_item_t *mit = arg;
273         io_stat_t *iot;
274         io_stat_item_t **items;
275         guint32 *frames;
276         guint32 *counters;
277         guint32 *num;
278         guint32 i,more_items;
279         gint t;
280
281         iot=mit->parent;
282
283         printf("\n");
284         printf("===================================================================\n");
285         printf("IO Statistics\n");
286         printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
287         for(i=0;i<iot->num_items;i++){
288                 printf("Column #%d: %s\n",i,iot->filters[i]?iot->filters[i]:"");
289         }
290         printf("                ");
291         for(i=0;i<iot->num_items;i++){
292                 printf("|   Column #%-2d   ",i);
293         }
294         printf("\n");
295         printf("Time            ");
296         for(i=0;i<iot->num_items;i++){
297                 switch(iot->items[i].calc_type){
298                 case CALC_TYPE_BYTES:
299                         printf("|frames|  bytes  ");
300                         break;
301                 case CALC_TYPE_COUNT:
302                         printf("|          COUNT ");
303                         break;
304                 case CALC_TYPE_SUM:
305                         printf("|            SUM ");
306                         break;
307                 case CALC_TYPE_MIN:
308                         printf("|            MIN ");
309                         break;
310                 case CALC_TYPE_MAX:
311                         printf("|            MAX ");
312                         break;
313                 case CALC_TYPE_AVG:
314                         printf("|            AVG ");
315                         break;
316                 }
317         }
318         printf("\n");
319
320         items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
321         frames=g_malloc(sizeof(guint32)*iot->num_items);
322         counters=g_malloc(sizeof(guint32)*iot->num_items);
323         num=g_malloc(sizeof(guint32)*iot->num_items);
324         /* preset all items at the first interval */
325         for(i=0;i<iot->num_items;i++){
326                 items[i]=&iot->items[i];
327         }
328
329         /* loop the items until we run out of them all */
330         t=0;
331         do {
332                 more_items=0;
333                 for(i=0;i<iot->num_items;i++){
334                         frames[i]=0;
335                         counters[i]=0;
336                         num[i]=0;
337                 }
338                 for(i=0;i<iot->num_items;i++){
339                         if(items[i] && (t>=(items[i]->time+iot->interval))){
340                                 items[i]=items[i]->next;
341                         }
342
343                         if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
344                                 frames[i]=items[i]->frames;
345                                 counters[i]=items[i]->counter;
346                                 num[i]=items[i]->num;
347                         }
348
349                         if(items[i]){
350                                 more_items=1;
351                         }
352                 }
353
354                 if(more_items){
355                         printf("%03d.%03d-%03d.%03d  ",
356                                 t/1000,t%1000,
357                                 (t+iot->interval)/1000,(t+iot->interval)%1000);
358                         for(i=0;i<iot->num_items;i++){
359                                 switch(iot->items[i].calc_type){
360                                 case CALC_TYPE_BYTES:
361                                         printf("%6d %9d ",frames[i],counters[i]);
362                                         break;
363                                 case CALC_TYPE_COUNT:
364                                         printf("        %8d ", counters[i]);
365                                         break;
366                                 case CALC_TYPE_SUM:
367                                         printf("        %8d ", counters[i]);
368                                         break;
369                                 case CALC_TYPE_MIN:
370                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
371                                         case FT_UINT8:
372                                         case FT_UINT16:
373                                         case FT_UINT24:
374                                         case FT_UINT32:
375                                                 printf("        %8u ", counters[i]);
376                                                 break;
377                                         case FT_INT8:
378                                         case FT_INT16:
379                                         case FT_INT24:
380                                         case FT_INT32:
381                                                 printf("        %8d ", counters[i]);
382                                                 break;
383                                         case FT_RELATIVE_TIME:
384                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
385                                                 break;
386                                         }
387                                         break;
388                                 case CALC_TYPE_MAX:
389                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
390                                         case FT_UINT8:
391                                         case FT_UINT16:
392                                         case FT_UINT24:
393                                         case FT_UINT32:
394                                                 printf("        %8u ", counters[i]);
395                                                 break;
396                                         case FT_INT8:
397                                         case FT_INT16:
398                                         case FT_INT24:
399                                         case FT_INT32:
400                                                 printf("        %8d ", counters[i]);
401                                                 break;
402                                         case FT_RELATIVE_TIME:
403                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
404                                                 break;
405                                         }
406                                         break;
407                                 case CALC_TYPE_AVG:
408                                         if(num[i]==0){
409                                                 num[i]=1;
410                                         }
411                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
412                                         case FT_UINT8:
413                                         case FT_UINT16:
414                                         case FT_UINT24:
415                                         case FT_UINT32:
416                                                 printf("        %8u ", counters[i]/num[i]);
417                                                 break;
418                                         case FT_INT8:
419                                         case FT_INT16:
420                                         case FT_INT24:
421                                         case FT_INT32:
422                                                 printf("        %8d ", counters[i]/num[i]);
423                                                 break;
424                                         case FT_RELATIVE_TIME:
425                                                 counters[i]/=num[i];
426                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
427                                                 break;
428                                         }
429                                         break;
430
431                                 }
432                         }
433                         printf("\n");
434                 }
435
436                 t+=iot->interval;
437         } while(more_items);
438
439         printf("===================================================================\n");
440
441         g_free(items);
442         g_free(frames);
443         g_free(counters);
444         g_free(num);
445 }
446
447
448 typedef struct {
449         const char *func_name;
450         int calc_type;
451 } calc_type_ent_t;
452
453 static calc_type_ent_t calc_type_table[] = {
454         { "COUNT", CALC_TYPE_COUNT },
455         { "SUM", CALC_TYPE_SUM },
456         { "MIN", CALC_TYPE_MIN },
457         { "MAX", CALC_TYPE_MAX },
458         { "AVG", CALC_TYPE_AVG },
459         { NULL, 0 }
460 };
461
462 static void
463 register_io_tap(io_stat_t *io, int i, const char *filter)
464 {
465         GString *error_string;
466         const char *flt;
467         int j;
468         size_t namelen;
469         const char *p, *parenp;
470         char *field;
471         header_field_info *hfi;
472
473         io->items[i].prev=&io->items[i];
474         io->items[i].next=NULL;
475         io->items[i].parent=io;
476         io->items[i].time=0;
477         io->items[i].calc_type=CALC_TYPE_BYTES;
478         io->items[i].frames=0;
479         io->items[i].counter=0;
480         io->items[i].num=0;
481         io->filters[i]=filter;
482         flt=filter;
483
484         if(!filter){
485                 filter="";
486         }
487         field=NULL;
488         hfi=NULL;
489         for(j=0; calc_type_table[j].func_name; j++){
490                 namelen=strlen(calc_type_table[j].func_name);
491                 if(strncmp(filter, calc_type_table[j].func_name, namelen) == 0
492                     && *(filter+namelen)=='('){
493                         io->items[i].calc_type=calc_type_table[j].calc_type;
494
495                         p=filter+namelen+1;
496                         parenp=strchr(p, ')');
497                         if(!parenp){
498                                 fprintf(stderr, "tshark: Closing parenthesis missing from calculated expression.\n");
499                                 exit(10);
500                         }
501                         /* bail out if there was no field specified */
502                         if(parenp==p){
503                                 fprintf(stderr, "tshark: You didn't specify a field name for %s(*).\n",
504                                     calc_type_table[j].func_name);
505                                 exit(10);
506                         }
507                         field=malloc(parenp-p+1);
508                         if(!field){
509                                 fprintf(stderr, "tshark: Out of memory.\n");
510                                 exit(10);
511                         }
512                         memcpy(field, p, parenp-p);
513                         field[parenp-p] = '\0';
514                         flt=parenp + 1;
515
516                         hfi=proto_registrar_get_byname(field);
517                         if(!hfi){
518                                 fprintf(stderr, "tshark: There is no field named '%s'.\n",
519                                     field);
520                                 free(field);
521                                 exit(10);
522                         }
523         
524                         io->items[i].hf_index=hfi->id;
525                         break;
526                 }
527         }
528         if(io->items[i].calc_type!=CALC_TYPE_BYTES){
529                 /* check that the type is compatible */
530                 switch(hfi->type){
531                 case FT_UINT8:
532                 case FT_UINT16:
533                 case FT_UINT24:
534                 case FT_UINT32:
535                 case FT_INT8:
536                 case FT_INT16:
537                 case FT_INT24:
538                 case FT_INT32:
539                         /* these types support all calculations */
540                         break;
541                 case FT_RELATIVE_TIME:
542                         /* this type only supports SUM, COUNT, MAX, MIN, AVG */
543                         switch(io->items[i].calc_type){
544                         case CALC_TYPE_SUM:
545                         case CALC_TYPE_COUNT:
546                         case CALC_TYPE_MAX:
547                         case CALC_TYPE_MIN:
548                         case CALC_TYPE_AVG:
549                                 break;
550                         default:
551                                 fprintf(stderr,
552                                     "tshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
553                                     field,
554                                     calc_type_table[j].func_name);
555                                 exit(10);
556                         }
557                         break;
558                 case FT_UINT64:
559                 case FT_INT64:
560                         /*
561                          * XXX - support this if gint64/guint64 are
562                          * available?
563                          */
564                         if(io->items[i].calc_type!=CALC_TYPE_COUNT){
565                                 fprintf(stderr,
566                                     "tshark: %s is a 64-bit integer, so %s(*) calculations are not supported on it.",
567                                     field,
568                                     calc_type_table[j].func_name);
569                                 exit(10);
570                         }
571                         break;
572                 default:
573                         /*
574                          * XXX - support all operations on floating-point
575                          * numbers?
576                          */
577                         if(io->items[i].calc_type!=CALC_TYPE_COUNT){
578                                 fprintf(stderr,
579                                     "tshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
580                                     field,
581                                     calc_type_table[j].func_name);
582                                 exit(10);
583                         }
584                         break;
585                 }
586                 free(field);
587         }
588
589 /*
590 CALC_TYPE_SUM   2
591 CALC_TYPE_MIN   3
592 CALC_TYPE_MAX   4
593 CALC_TYPE_AVG   5
594 */
595
596         error_string=register_tap_listener("frame", &io->items[i], flt, NULL, iostat_packet, i?NULL:iostat_draw);
597         if(error_string){
598                 g_free(io->items);
599                 g_free(io);
600                 fprintf(stderr, "tshark: Couldn't register io,stat tap: %s\n",
601                     error_string->str);
602                 g_string_free(error_string, TRUE);
603                 exit(1);
604         }
605 }
606
607 void
608 iostat_init(const char *optarg, void* userdata _U_)
609 {
610         float interval_float;
611         gint32 interval; 
612         int pos=0;
613         io_stat_t *io;
614         const char *filter=NULL;
615
616         if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
617                 if(pos){
618                         filter=optarg+pos;
619                 } else {
620                         filter=NULL;
621                 }
622         } else {
623                 fprintf(stderr, "tshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
624                 exit(1);
625         }
626
627
628         /* make interval be number of ms */
629         interval=(gint32)(interval_float*1000.0+0.9);   
630         if(interval<1){
631                 fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds.\n");
632                 exit(10);
633         }
634         
635         io=g_malloc(sizeof(io_stat_t));
636         io->interval=interval;
637         if((!filter)||(filter[0]==0)){
638                 io->num_items=1;
639                 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
640                 io->filters=g_malloc(sizeof(char *)*io->num_items);
641
642                 register_io_tap(io, 0, NULL);
643         } else {
644                 const char *str,*pos;
645                 char *tmp;
646                 int i;
647                 /* find how many ',' separated filters we have */
648                 str=filter;
649                 io->num_items=1;
650                 while((str=strchr(str,','))){
651                         io->num_items++;
652                         str++;
653                 }
654
655                 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
656                 io->filters=g_malloc(sizeof(char *)*io->num_items);
657
658                 /* for each filter, register a tap listener */          
659                 i=0;
660                 str=filter;
661                 do{
662                         pos=strchr(str,',');
663                         if(pos==str){
664                                 register_io_tap(io, i, NULL);
665                         } else if(pos==NULL) {
666                                 tmp=g_malloc(strlen(str)+1);
667                                 strcpy(tmp,str);
668                                 register_io_tap(io, i, tmp);
669                         } else {
670                                 tmp=g_malloc((pos-str)+1);
671                                 strncpy(tmp,str,(pos-str));
672                                 tmp[pos-str]=0;
673                                 register_io_tap(io, i, tmp);
674                         }
675                         str=pos+1;
676                         i++;                    
677                 } while(pos);
678         }                       
679 }
680
681 void
682 register_tap_listener_iostat(void)
683 {
684         register_stat_cmd_arg("io,stat,", iostat_init, NULL);
685 }