From Francesco Fondelli:
[obnox/wireshark/wip.git] / tap-icmpv6stat.c
1 /* tap-icmpv6stat.c
2  * icmpv6stat   2011 Christopher Maynard
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 icmpv6 echo request/reply SRT statistics to tshark.
26  * It is only used by tshark and not wireshark
27  *
28  * It was based on tap-icmptat.c, which itself was based on tap-rpcstat.c and
29  * doc/README.tapping.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <stdio.h>
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #include <string.h>
43 #include "epan/packet_info.h"
44 #include <epan/tap.h>
45 #include <epan/stat_cmd_args.h>
46 #include <epan/dissectors/packet-icmp.h>
47 #include <math.h>
48
49 /* used to keep track of the ICMPv6 statistics */
50 typedef struct _icmpv6stat_t {
51     char *filter;
52     GSList *rt_list;
53     guint num_rqsts;
54     guint num_resps;
55     guint min_frame;
56     guint max_frame;
57     double min_msecs;
58     double max_msecs;
59     double tot_msecs;
60 } icmpv6stat_t;
61
62
63 /* This callback is never used by tshark but it is here for completeness.  When
64  * registering below, we could just have left this function as NULL.
65  *
66  * When used by wireshark, this function will be called whenever we would need
67  * to reset all state, such as when wireshark opens a new file, when it starts
68  * a new capture, when it rescans the packetlist after some prefs have changed,
69  * etc.
70  *
71  * So if your application has some state it needs to clean up in those
72  * situations, here is a good place to put that code.
73  */
74 static void
75 icmpv6stat_reset(void *tapdata)
76 {
77     icmpv6stat_t *icmpv6stat = tapdata;
78
79     g_slist_free(icmpv6stat->rt_list);
80     memset(icmpv6stat, 0, sizeof(icmpv6stat_t));
81     icmpv6stat->min_msecs = 1.0 * G_MAXUINT;
82 }
83
84
85 static gint compare_doubles(gconstpointer a, gconstpointer b)
86 {
87     double ad, bd;
88
89     ad = *(double *)a;
90     bd = *(double *)b;
91
92     if (ad < bd)
93         return -1;
94     if (ad > bd)
95         return 1;
96     return 0;
97 }
98
99
100 /* This callback is invoked whenever the tap system has seen a packet we might
101  * be interested in.  The function is to be used to only update internal state
102  * information in the *tapdata structure, and if there were state changes which
103  * requires the window to be redrawn, return 1 and (*draw) will be called
104  * sometime later.
105  *
106  * This function should be as lightweight as possible since it executes
107  * together with the normal wireshark dissectors.  Try to push as much
108  * processing as possible into (*draw) instead since that function executes
109  * asynchronously and does not affect the main thread's performance.
110  *
111  * If it is possible, try to do all "filtering" explicitly since you will get
112  * MUCH better performance than applying a similar display-filter in the
113  * register call.
114  *
115  * The third parameter is tap dependent.  Since we register this one to the
116  * "icmpv6" tap, the third parameter type is icmp_transaction_t.
117  *
118  * function returns :
119  *  0: no updates, no need to call (*draw) later
120  * !0: state has changed, call (*draw) sometime later
121  */
122 static int
123 icmpv6stat_packet(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *data)
124 {
125     icmpv6stat_t *icmpv6stat = tapdata;
126     const icmp_transaction_t *trans = data;
127     double *rt;
128
129     if (trans == NULL)
130         return 0;
131
132     if (trans->resp_frame) {
133         rt = g_malloc(sizeof(double));
134         if (rt == NULL)
135             return 0;
136         *rt = trans->resp_time;
137         icmpv6stat->rt_list = g_slist_insert_sorted(icmpv6stat->rt_list, rt, compare_doubles);
138         icmpv6stat->num_resps++;
139         if (icmpv6stat->min_msecs > trans->resp_time) {
140             icmpv6stat->min_frame = trans->resp_frame;
141             icmpv6stat->min_msecs = trans->resp_time;
142         }
143         if (icmpv6stat->max_msecs < trans->resp_time) {
144             icmpv6stat->max_frame = trans->resp_frame;
145             icmpv6stat->max_msecs = trans->resp_time;
146         }
147         icmpv6stat->tot_msecs += trans->resp_time;
148     } else if (trans->rqst_frame)
149         icmpv6stat->num_rqsts++;
150     else
151         return 0;
152
153     return 1;
154 }
155
156
157 /*
158  * Compute the mean, median and standard deviation.
159  */
160 static void compute_stats(icmpv6stat_t *icmpv6stat, double *mean, double *med, double *sdev)
161 {
162     GSList *slist = icmpv6stat->rt_list;
163     double diff;
164     double sq_diff_sum = 0.0;
165
166     if (icmpv6stat->num_resps == 0 || slist == NULL) {
167         *mean = 0.0;
168         *med = 0.0;
169         *sdev = 0.0;
170         return;
171     }
172
173     /* (arithmetic) mean */
174     *mean = icmpv6stat->tot_msecs / icmpv6stat->num_resps;
175
176     /* median: If we have an odd number of elements in our list, then the
177      * median is simply the middle element, otherwise the median is computed by
178      * averaging the 2 elements on either side of the mid-point. */
179     if (icmpv6stat->num_resps & 1)
180         *med = *(double *)g_slist_nth_data(slist, icmpv6stat->num_resps / 2);
181     else {
182         *med =
183             (*(double *)g_slist_nth_data(slist, (icmpv6stat->num_resps - 1) / 2) +
184             *(double *)g_slist_nth_data(slist, icmpv6stat->num_resps / 2)) / 2;
185     }
186
187     /* (sample) standard deviation */
188     for ( ; slist; slist = g_slist_next(slist)) {
189         diff = *(double *)slist->data - *mean;
190         sq_diff_sum += diff * diff;
191     }
192     if (icmpv6stat->num_resps > 1)
193         *sdev = sqrt(sq_diff_sum / (icmpv6stat->num_resps - 1));
194     else
195         *sdev = 0.0;
196 }
197
198
199 /* This callback is used when tshark wants us to draw/update our data to the
200  * output device.  Since this is tshark, the only output is stdout.
201  * TShark will only call this callback once, which is when tshark has finished
202  * reading all packets and exits.
203  * If used with wireshark this may be called any time, perhaps once every 3
204  * seconds or so.
205  * This function may even be called in parallel with (*reset) or (*draw), so
206  * make sure there are no races.  The data in the icmpv6stat_t can thus change
207  * beneath us.  Beware!
208  *
209  * How best to display the data?  For now, following other tap statistics
210  * output, but here are a few other alternatives we might choose from:
211  *
212  * -> Windows ping output:
213  *      Ping statistics for <IP>:
214  *          Packets: Sent = <S>, Received = <R>, Lost = <L> (<LP>% loss),
215  *      Approximate round trip times in milli-seconds:
216  *          Minimum = <m>ms, Maximum = <M>ms, Average = <A>ms
217  *
218  * -> Cygwin ping output:
219  *      ----<HOST> PING Statistics----
220  *      <S> packets transmitted, <R> packets received, <LP>% packet loss
221  *      round-trip (ms)  min/avg/max/med = <m>/<M>/<A>/<D>
222  *
223  * -> Linux ping output:
224  *      --- <HOST> ping statistics ---
225  *      <S> packets transmitted, <R> received, <LP>% packet loss, time <T>ms
226  *      rtt min/avg/max/mdev = <m>/<A>/<M>/<D> ms
227  */
228 static void
229 icmpv6stat_draw(void *tapdata)
230 {
231     icmpv6stat_t *icmpv6stat = tapdata;
232     unsigned int lost;
233     double mean, sdev, med;
234
235     printf("\n");
236     printf("==========================================================================\n");
237     printf("ICMPv6 Service Response Time (SRT) Statistics (all times in ms):\n");
238     printf("Filter: %s\n", icmpv6stat->filter ? icmpv6stat->filter : "<none>");
239     printf("\nRequests  Replies   Lost      %% Loss\n");
240
241     if (icmpv6stat->num_rqsts) {
242         lost =  icmpv6stat->num_rqsts - icmpv6stat->num_resps;
243         compute_stats(icmpv6stat, &mean, &med, &sdev);
244
245         printf("%-10u%-10u%-10u%5.1f%%\n\n",
246             icmpv6stat->num_rqsts, icmpv6stat->num_resps, lost,
247             100.0 * lost / icmpv6stat->num_rqsts);
248         printf("Minimum   Maximum   Mean      Median    SDeviation     Min Frame Max Frame\n");
249         printf("%-10.3f%-10.3f%-10.3f%-10.3f%-10.3f     %-10u%-10u\n",
250             icmpv6stat->min_msecs >= G_MAXUINT ? 0.0 : icmpv6stat->min_msecs,
251             icmpv6stat->max_msecs, mean, med, sdev,
252             icmpv6stat->min_frame, icmpv6stat->max_frame);
253     } else {
254         printf("0         0         0           0.0%%\n\n");
255         printf("Minimum   Maximum   Mean      Median    SDeviation     Min Frame Max Frame\n");
256         printf("0.000     0.000     0.000     0.000     0.000          0         0\n");
257     }
258     printf("==========================================================================\n");
259 }
260
261
262 /* When called, this function will create a new instance of icmpv6stat.
263  *
264  * This function is called from tshark when it parses the -z icmpv6, arguments
265  * and it creates a new instance to store statistics in and registers this new
266  * instance for the icmpv6 tap.
267  */
268 static void
269 icmpv6stat_init(const char *optarg, void* userdata _U_)
270 {
271     icmpv6stat_t *icmpv6stat;
272     const char *filter = NULL;
273     GString *error_string;
274
275     if (strstr(optarg, "icmpv6,srt,"))
276         filter = optarg + strlen("icmpv6,srt,");
277
278     icmpv6stat = g_try_malloc(sizeof(icmpv6stat_t));
279     if (icmpv6stat == NULL) {
280         fprintf(stderr, "tshark: g_try_malloc() fatal error.\n");
281         exit(1);
282     }
283     memset(icmpv6stat, 0, sizeof(icmpv6stat_t));
284     icmpv6stat->min_msecs = 1.0 * G_MAXUINT;
285
286     if (filter)
287         icmpv6stat->filter = g_strdup(filter);
288
289 /* It is possible to create a filter and attach it to the callbacks.  Then the
290  * callbacks would only be invoked if the filter matched.
291  *
292  * Evaluating filters is expensive and if we can avoid it and not use them,
293  * then we gain performance.
294  *
295  * In this case we do the filtering for protocol and version inside the
296  * callback itself but use whatever filter the user provided.
297  */
298
299     error_string = register_tap_listener("icmpv6", icmpv6stat, icmpv6stat->filter,
300         TL_REQUIRES_NOTHING, icmpv6stat_reset, icmpv6stat_packet, icmpv6stat_draw);
301     if (error_string) {
302         /* error, we failed to attach to the tap. clean up */
303         if (icmpv6stat->filter)
304             g_free(icmpv6stat->filter);
305         g_free(icmpv6stat);
306
307         fprintf(stderr, "tshark: Couldn't register icmpv6,srt tap: %s\n",
308             error_string->str);
309         g_string_free(error_string, TRUE);
310         exit(1);
311     }
312 }
313
314
315 void
316 register_tap_listener_icmpv6stat(void)
317 {
318     register_stat_cmd_arg("icmpv6,srt", icmpv6stat_init, NULL);
319 }
320