56b881320875bb3a75dad96b80bc16a29cfed790
[obnox/wireshark/wip.git] / tap-icmpstat.c
1 /* tap-icmpstat.c
2  * icmpstat   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 icmp echo request/reply SRT statistics to tshark.
26  * It is only used by tshark and not wireshark
27  *
28  * It was based on tap-rpcstat.c and doc/README.tapping.
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-icmp.h>
46 #include <math.h>
47
48 /* used to keep track of the ICMP statistics */
49 typedef struct _icmpstat_t {
50     char *filter;
51     GSList *rt_list;
52     guint num_rqsts;
53     guint num_resps;
54     double min_msecs;
55     double max_msecs;
56     double tot_msecs;
57 } icmpstat_t;
58
59
60 /* This callback is never used by tshark but it is here for completeness. When
61  * registering below, we could just have left this function as NULL.
62  *
63  * When used by wireshark, this function will be called whenever we would need
64  * to reset all state, such as when wireshark opens a new file, when it starts
65  * a new capture, when it rescans the packetlist after some prefs have changed,
66  * etc.
67  *
68  * So if your application has some state it needs to clean up in those
69  * situations, here is a good place to put that code.
70  */
71 static void
72 icmpstat_reset(void *tapdata)
73 {
74     icmpstat_t *icmpstat = tapdata;
75
76     g_slist_free(icmpstat->rt_list);
77     icmpstat->rt_list = NULL;
78     icmpstat->num_rqsts = 0;
79     icmpstat->num_resps = 0;
80     icmpstat->min_msecs = 1.0 * G_MAXUINT;
81     icmpstat->max_msecs = 0.0;
82     icmpstat->tot_msecs = 0.0;
83 }
84
85
86 /* This callback is invoked whenever the tap system has seen a packet we might
87  * be interested in.  The function is to be used to only update internal state
88  * information in the *tapdata structure, and if there were state changes which
89  * requires the window to be redrawn, return 1 and (*draw) will be called
90  * sometime later.
91  *
92  * This function should be as lightweight as possible since it executes
93  * together with the normal wireshark dissectors.  Try to push as much
94  * processing as possible into (*draw) instead since that function executes
95  * asynchronously and does not affect the main thread's performance.
96  *
97  * If it is possible, try to do all "filtering" explicitly since you will get
98  * MUCH better performance than applying a similar display-filter in the
99  * register call.
100  *
101  * The third parameter is tap dependent.  Since we register this one to the
102  * "icmp" tap, the third parameter type is icmp_transaction_t.
103  *
104  * function returns :
105  *  0: no updates, no need to call (*draw) later
106  * !0: state has changed, call (*draw) sometime later
107  */
108 static int
109 icmpstat_packet(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *data)
110 {
111     icmpstat_t *icmpstat = tapdata;
112     const icmp_transaction_t *trans = data;
113     double *rt;
114
115     if (trans == NULL)
116         return 0;
117
118     if (trans->resp_frame) {
119         rt = g_malloc(sizeof(double));
120         if (rt == NULL)
121             return 0;
122         *rt = trans->resp_time;
123         icmpstat->rt_list = g_slist_prepend(icmpstat->rt_list, rt);
124         icmpstat->num_resps++;
125         if (icmpstat->min_msecs > trans->resp_time)
126             icmpstat->min_msecs = trans->resp_time;
127         if (icmpstat->max_msecs < trans->resp_time)
128             icmpstat->max_msecs = trans->resp_time;
129         icmpstat->tot_msecs += trans->resp_time;
130     } else if (trans->rqst_frame)
131         icmpstat->num_rqsts++;
132     else
133         return 0;
134
135     return 1;
136 }
137
138
139 static double compute_sdev(double average, guint num, GSList *slist)
140 {
141     double diff;
142     double sq_diff_sum;
143
144     if (num == 0)
145         return 0.0;
146
147     for ( sq_diff_sum = 0.0; slist; slist = g_slist_next(slist)) {
148         diff = *(double *)slist->data - average;
149         sq_diff_sum += diff * diff;
150     }
151
152     return sqrt(sq_diff_sum / num);
153 }
154
155
156 /* This callback is used when tshark wants us to draw/update our data to the
157  * output device.  Since this is tshark, the only output is stdout.
158  * TShark will only call this callback once, which is when tshark has finished
159  * reading all packets and exits.
160  * If used with wireshark this may be called any time, perhaps once every 3
161  * seconds or so.
162  * This function may even be called in parallel with (*reset) or (*draw), so
163  * make sure there are no races.  The data in the icmpstat_t can thus change
164  * beneath us.  Beware!
165  *
166  * How best to display the data?  For now, following other tap statistics
167  * output, but here are a few other alternatives we might choose from:
168  *
169  * -> Windows ping output:
170  *      Ping statistics for <IP>:
171  *          Packets: Sent = <S>, Received = <R>, Lost = <L> (<LP>% loss),
172  *      Approximate round trip times in milli-seconds:
173  *          Minimum = <m>ms, Maximum = <M>ms, Average = <A>ms
174  *
175  * -> Cygwin ping output:
176  *      ----<HOST> PING Statistics----
177  *      <S> packets transmitted, <R> packets received, <LP>% packet loss
178  *      round-trip (ms)  min/avg/max/med = <m>/<M>/<A>/<D>
179  *
180  * -> Linux ping output:
181  *      --- <HOST> ping statistics ---
182  *      <S> packets transmitted, <R> received, <LP>% packet loss, time <T>ms
183  *      rtt min/avg/max/mdev = <m>/<A>/<M>/<D> ms
184  */
185 static void
186 icmpstat_draw(void *tapdata)
187 {
188     icmpstat_t *icmpstat = tapdata;
189     unsigned int lost;
190     double average, sdev;
191
192     printf("\n");
193     printf("==========================================================================\n");
194     printf("ICMP SRT Statistics (all times in ms):\n");
195     printf("Filter: %s\n", icmpstat->filter ? icmpstat->filter : "");
196     printf("Requests  Replies   Lost      %% Loss  Min SRT   Max SRT   Avg SRT   SDEV\n");
197
198     if (icmpstat->num_rqsts) {
199         lost =  icmpstat->num_rqsts - icmpstat->num_resps;
200         average = icmpstat->tot_msecs / icmpstat->num_resps;
201         sdev = compute_sdev(average, icmpstat->num_resps, icmpstat->rt_list);
202         printf("%-10u%-10u%-10u%5.1f%%  %-10.3f%-10.3f%-10.3f%-10.3f\n",
203             icmpstat->num_rqsts, icmpstat->num_resps, lost,
204             100.0 * lost / icmpstat->num_rqsts,
205             icmpstat->min_msecs >= G_MAXUINT ? 0.0 : icmpstat->min_msecs,
206             icmpstat->max_msecs, average, sdev);
207     } else
208         printf("0         0         0           0.0%%  0.000     0.000     0.000     0.000\n");
209     printf("==========================================================================\n");
210 }
211
212
213 /* When called, this function will create a new instance of icmpstat.
214  *
215  * This function is called from tshark when it parses the -z icmp, arguments
216  * and it creates a new instance to store statistics in and registers this new
217  * instance for the icmp tap.
218  */
219 static void
220 icmpstat_init(const char *optarg, void* userdata _U_)
221 {
222     icmpstat_t *icmpstat;
223     const char *filter = NULL;
224     GString *error_string;
225
226     if (strstr(optarg, "icmp,srt,"))
227         filter = optarg + strlen("icmp,srt,");
228
229     icmpstat = g_malloc0(sizeof(icmpstat_t));
230     icmpstat->min_msecs = 1.0 * G_MAXUINT;
231     if (icmpstat == NULL) {
232         fprintf(stderr, "tshark: g_malloc() fatal error.\n");
233         exit(1);
234     }
235     if (filter)
236         icmpstat->filter = g_strdup(filter);
237
238 /* It is possible to create a filter and attach it to the callbacks.  Then the
239  * callbacks would only be invoked if the filter matched.
240  *
241  * Evaluating filters is expensive and if we can avoid it and not use them,
242  * then we gain performance.
243  *
244  * In this case we do the filtering for protocol and version inside the
245  * callback itself but use whatever filter the user provided.
246  */
247
248     error_string = register_tap_listener("icmp", icmpstat, icmpstat->filter,
249         TL_REQUIRES_NOTHING, icmpstat_reset, icmpstat_packet, icmpstat_draw);
250     if (error_string) {
251         /* error, we failed to attach to the tap. clean up */
252         if (icmpstat->filter)
253             g_free(icmpstat->filter);
254         g_free(icmpstat);
255
256         fprintf(stderr, "tshark: Couldn't register icmp,srt tap: %s\n",
257             error_string->str);
258         g_string_free(error_string, TRUE);
259         exit(1);
260     }
261 }
262
263
264 void
265 register_tap_listener_icmpstat(void)
266 {
267     register_stat_cmd_arg("icmp,srt", icmpstat_init, NULL);
268 }
269