ethereal->wireshark updates
[obnox/wireshark/wip.git] / tap-rpcstat.c
1 /* tap-rpcstat.c
2  * rpcstat   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 /* This module provides rpc call/reply RTT statistics to tethereal.
26  * It is only used by tethereal and not ethereal
27  *
28  * It serves as an example on how to use the tap api.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdio.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
39 #endif
40
41 #include <string.h>
42 #include "epan/packet_info.h"
43 #include <epan/tap.h>
44 #include <epan/stat_cmd_args.h>
45 #include <epan/dissectors/packet-rpc.h>
46 #include "register.h"
47
48 /* used to keep track of statistics for a specific procedure */
49 typedef struct _rpc_procedure_t {
50         const char *proc;
51         int num;
52         nstime_t min;
53         nstime_t max;
54         nstime_t tot;
55 } rpc_procedure_t;
56
57 /* used to keep track of the statistics for an entire program interface */
58 typedef struct _rpcstat_t {
59         const char *prog;
60         char *filter;
61         guint32 program;
62         guint32 version;
63         guint32 num_procedures;
64         rpc_procedure_t *procedures;
65 } rpcstat_t;
66
67
68
69 /* This callback is never used by tethereal but it is here for completeness.
70  * When registering below, we could just have left this function as NULL.
71  *
72  * When used by wireshark, this function will be called whenever we would need
73  * to reset all state. Such as when ethereal opens a new file, when it
74  * starts a new capture, when it rescans the packetlist after some prefs have
75  * changed etc.
76  * So if your aplication has some state it needs to clean up in those
77  * situations, here is a good place to put that code.
78  */
79 static void
80 rpcstat_reset(void *prs)
81 {
82         rpcstat_t *rs=prs;
83         guint32 i;
84
85         for(i=0;i<rs->num_procedures;i++){
86                 rs->procedures[i].num=0;        
87                 rs->procedures[i].min.secs=0;
88                 rs->procedures[i].min.nsecs=0;
89                 rs->procedures[i].max.secs=0;
90                 rs->procedures[i].max.nsecs=0;
91                 rs->procedures[i].tot.secs=0;
92                 rs->procedures[i].tot.nsecs=0;
93         }
94 }
95
96
97 /* This callback is invoked whenever the tap system has seen a packet
98  * we might be interested in.
99  * The function is to be used to only update internal state information
100  * in the *tapdata structure, and if there were state changes which requires
101  * the window to be redrawn, return 1 and (*draw) will be called sometime
102  * later.
103  *
104  * This function should be as lightweight as possible since it executes together
105  * with the normal ethereal dissectors. Try to push as much processing as
106  * possible into (*draw) instead since that function executes asynchronously
107  * and does not affect the main threads performance.
108  *
109  * If it is possible, try to do all "filtering" explicitely as we do below in 
110  * this example since you will get MUCH better performance than applying
111  * a similar display-filter in the register call.
112  *
113  * The third parameter is tap dependant. Since we register this one to the "rpc"
114  * tap the third parameters type is rpc_call_info_value.
115  *
116  *
117  * The filtering we do is just to check the rpc_call_info_value struct that
118  * we were called for the proper program and version. We didnt apply a filter
119  * when we registered so we will be called for ALL rpc packets and not just
120  * the ones we are collecting stats for.
121  * 
122  *
123  * function returns :
124  *  0: no updates, no need to call (*draw) later
125  * !0: state has changed, call (*draw) sometime later
126  */
127 static int
128 rpcstat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
129 {
130         rpcstat_t *rs=prs;
131         const rpc_call_info_value *ri=pri;
132         nstime_t delta;
133         rpc_procedure_t *rp;
134
135         if(ri->proc>=rs->num_procedures){
136                 /* dont handle this since its outside of known table */
137                 return 0;
138         }
139         /* we are only interested in reply packets */
140         if(ri->request){
141                 return 0;
142         }
143         /* we are only interested in certain program/versions */
144         if( (ri->prog!=rs->program) || (ri->vers!=rs->version) ){
145                 return 0;
146         }
147
148         rp=&(rs->procedures[ri->proc]);
149
150         /* calculate time delta between request and reply */
151         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
152
153         if(rp->num==0){
154                 rp->max.secs=delta.secs;
155                 rp->max.nsecs=delta.nsecs;
156         }
157
158         if(rp->num==0){
159                 rp->min.secs=delta.secs;
160                 rp->min.nsecs=delta.nsecs;
161         }
162
163         if( (delta.secs<rp->min.secs)
164         ||( (delta.secs==rp->min.secs)
165           &&(delta.nsecs<rp->min.nsecs) ) ){
166                 rp->min.secs=delta.secs;
167                 rp->min.nsecs=delta.nsecs;
168         }
169
170         if( (delta.secs>rp->max.secs)
171         ||( (delta.secs==rp->max.secs)
172           &&(delta.nsecs>rp->max.nsecs) ) ){
173                 rp->max.secs=delta.secs;
174                 rp->max.nsecs=delta.nsecs;
175         }
176         
177         rp->tot.secs += delta.secs;
178         rp->tot.nsecs += delta.nsecs;
179         if(rp->tot.nsecs>1000000000){
180                 rp->tot.nsecs-=1000000000;
181                 rp->tot.secs++;
182         }
183
184         rp->num++;
185
186         return 1;
187 }
188
189 /* This callback is used when tethereal wants us to draw/update our
190  * data to the output device. Since this is tethereal only output is
191  * stdout.
192  * Tethereal will only call this callback once, which is when tethereal has
193  * finished reading all packets and exists.
194  * If used with wireshark this may be called any time, perhaps once every 3 
195  * seconds or so.
196  * This function may even be called in parallell with (*reset) or (*draw)
197  * so make sure there are no races. The data in the rpcstat_t can thus change
198  * beneath us. Beware.
199  */
200 static void
201 rpcstat_draw(void *prs)
202 {
203         rpcstat_t *rs=prs;
204         guint32 i;
205 #ifdef G_HAVE_UINT64
206         guint64 td;
207 #else
208         guint32 td;
209 #endif
210         printf("\n");
211         printf("===================================================================\n");
212         printf("%s Version %d RTT Statistics:\n", rs->prog, rs->version);
213         printf("Filter: %s\n",rs->filter?rs->filter:"");
214         printf("Procedure        Calls   Min RTT   Max RTT   Avg RTT\n");
215         for(i=0;i<rs->num_procedures;i++){
216                 /* scale it to units of 10us.*/
217                 /* for long captures with a large tot time, this can overflow on 32bit */
218                 td=(int)rs->procedures[i].tot.secs;
219                 td=td*100000+(int)rs->procedures[i].tot.nsecs/10000;
220                 if(rs->procedures[i].num){
221                         td/=rs->procedures[i].num;
222                 } else {
223                         td=0;
224                 }
225
226                 printf("%-15s %6d %3d.%05d %3d.%05d %3d.%05d\n",
227                         rs->procedures[i].proc,
228                         rs->procedures[i].num,
229                         (int)rs->procedures[i].min.secs,rs->procedures[i].min.nsecs/10000,
230                         (int)rs->procedures[i].max.secs,rs->procedures[i].max.nsecs/10000,
231                         td/100000, td%100000
232                 );
233         }
234         printf("===================================================================\n");
235 }
236
237 static guint32 rpc_program=0;
238 static guint32 rpc_version=0;
239 static gint32 rpc_min_proc=-1;
240 static gint32 rpc_max_proc=-1;
241
242 static void *
243 rpcstat_find_procs(gpointer *key, gpointer *value _U_, gpointer *user_data _U_)
244 {
245         rpc_proc_info_key *k=(rpc_proc_info_key *)key;
246
247         if(k->prog!=rpc_program){
248                 return NULL;
249         }
250         if(k->vers!=rpc_version){
251                 return NULL;
252         }
253         if(rpc_min_proc==-1){
254                 rpc_min_proc=k->proc;
255                 rpc_max_proc=k->proc;
256         }
257         if((gint32)k->proc<rpc_min_proc){
258                 rpc_min_proc=k->proc;
259         }
260         if((gint32)k->proc>rpc_max_proc){
261                 rpc_max_proc=k->proc;
262         }
263
264         return NULL;
265 }
266
267
268 /* When called, this function will create a new instance of rpcstat.
269  * program and version are whick onc-rpc program/version we want to
270  * collect statistics for.
271  * This function is called from tethereal when it parses the -z rpc, arguments
272  * and it creates a new instance to store statistics in and registers this
273  * new instance for the rpc tap.
274  */
275 static void
276 rpcstat_init(const char *optarg, void* userdata _U_)
277 {
278         rpcstat_t *rs;
279         guint32 i;
280         int program, version;
281         int pos=0;
282         const char *filter=NULL;
283         GString *error_string;
284
285         if(sscanf(optarg,"rpc,rtt,%d,%d,%n",&program,&version,&pos)==2){
286                 if(pos){
287                         filter=optarg+pos;
288                 } else {
289                         filter=NULL;
290                 }
291         } else {
292                 fprintf(stderr, "tethereal: invalid \"-z rpc,rtt,<program>,<version>[,<filter>]\" argument\n");
293                 exit(1);
294         }
295
296         rs=g_malloc(sizeof(rpcstat_t));
297         rs->prog=rpc_prog_name(program);
298         rs->program=program;
299         rs->version=version;
300         if(filter){
301                 rs->filter=g_malloc(strlen(filter)+1);
302                 strcpy(rs->filter, filter);
303         } else {
304                 rs->filter=NULL;
305         }
306         rpc_program=program;
307         rpc_version=version;
308         rpc_min_proc=-1;
309         rpc_max_proc=-1;
310         g_hash_table_foreach(rpc_procs, (GHFunc)rpcstat_find_procs, NULL);
311         if(rpc_min_proc==-1){
312                 fprintf(stderr,"tethereal: Invalid -z rpc,rrt,%d,%d\n",rpc_program,rpc_version);
313                 fprintf(stderr,"   Program:%d version:%d isn't supported by tethereal.\n", rpc_program, rpc_version);
314                 exit(1);
315         }
316
317
318         rs->num_procedures=rpc_max_proc+1;
319         rs->procedures=g_malloc(sizeof(rpc_procedure_t)*(rs->num_procedures+1));
320         for(i=0;i<rs->num_procedures;i++){
321                 rs->procedures[i].proc=rpc_proc_name(program, version, i);
322                 rs->procedures[i].num=0;        
323                 rs->procedures[i].min.secs=0;
324                 rs->procedures[i].min.nsecs=0;
325                 rs->procedures[i].max.secs=0;
326                 rs->procedures[i].max.nsecs=0;
327                 rs->procedures[i].tot.secs=0;
328                 rs->procedures[i].tot.nsecs=0;
329         }
330
331 /* It is possible to create a filter and attach it to the callbacks. Then the
332  * callbacks would only be invoked if the filter matched.
333  * Evaluating filters is expensive and if we can avoid it and not use them
334  * we gain performance. 
335  * In this case we do the filtering for protocol and version inside the 
336  * callback itself but use whatever filter the user provided.
337  * (Perhaps the user only want the stats for nis+ traffic for certain objects?)
338  *
339  */
340
341         error_string=register_tap_listener("rpc", rs, filter, rpcstat_reset, rpcstat_packet, rpcstat_draw);
342         if(error_string){
343                 /* error, we failed to attach to the tap. clean up */
344                 g_free(rs->procedures);
345                 g_free(rs->filter);
346                 g_free(rs);
347
348                 fprintf(stderr, "tethereal: Couldn't register rpc,rtt tap: %s\n",
349                     error_string->str);
350                 g_string_free(error_string, TRUE);
351                 exit(1);
352         }
353 }
354
355
356 void
357 register_tap_listener_rpcstat(void)
358 {
359         register_stat_cmd_arg("rpc,rtt,", rpcstat_init,NULL);
360 }
361