Have the time field in the Graph Analyzis windos use the same time format as used...
[metze/wireshark/wip.git] / ui / gtk / scsi_stat.c
1 /* scsi_stat.c
2  * scsi_stat   2006 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 SRT (Server Response Time) statistics
26  * to Wireshark.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34
35 #include <gtk/gtk.h>
36
37 #include <epan/packet_info.h>
38 #include <epan/epan.h>
39 #include <epan/stat_cmd_args.h>
40 #include <epan/tap.h>
41 #include <epan/conversation.h>
42 #include <epan/dissectors/packet-scsi.h>
43 #include <epan/dissectors/packet-fc.h>
44 #include <epan/dissectors/packet-scsi-sbc.h>
45 #include <epan/dissectors/packet-scsi-ssc.h>
46 #include <epan/dissectors/packet-scsi-smc.h>
47 #include <epan/dissectors/packet-scsi-osd.h>
48
49 #include "../simple_dialog.h"
50 #include "../globals.h"
51 #include "../stat_menu.h"
52
53 #include "ui/gtk/gui_stat_menu.h"
54 #include "ui/gtk/gui_utils.h"
55 #include "ui/gtk/dlg_utils.h"
56 #include "ui/gtk/main.h"
57 #include "ui/gtk/service_response_time_table.h"
58 #include "ui/gtk/tap_param_dlg.h"
59 #include "ui/gtk/gtkglobals.h"
60
61 #include "ui/gtk/old-gtk-compat.h"
62
63 /* used to keep track of the statistics for an entire scsi command set */
64 typedef struct _scsistat_t {
65         GtkWidget *win;
66         srt_stat_table srt_table;
67         guint8 cmdset;
68         const value_string *cdbnames;
69         const char *prog;
70 } scsistat_t;
71
72 static guint8 scsi_program=0;
73
74 enum
75 {
76    SCSI_STAT_PROG_LABEL_SBC,
77    SCSI_STAT_PROG_LABEL_SSC,
78    SCSI_STAT_PROG_LABEL_MMC
79 };
80
81
82 static char *
83 scsistat_gen_title(scsistat_t *rs)
84 {
85         char *title;
86
87         title = g_strdup_printf("SCSI Service Response Time statistics for %s: %s",
88             rs->prog, cf_get_display_name(&cfile));
89         return title;
90 }
91
92 static void
93 scsistat_set_title(scsistat_t *rs)
94 {
95         char *title;
96
97         title = scsistat_gen_title(rs);
98         gtk_window_set_title(GTK_WINDOW(rs->win), title);
99         g_free(title);
100 }
101
102 static void
103 scsistat_reset(void *arg)
104 {
105         scsistat_t *rs = (scsistat_t *)arg;
106
107         reset_srt_table_data(&rs->srt_table);
108         scsistat_set_title(rs);
109 }
110
111
112 static int
113 scsistat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *arg2)
114 {
115         scsistat_t *rs = (scsistat_t *)arg;
116         const scsi_task_data_t *ri = (const scsi_task_data_t *)arg2;
117
118         /* we are only interested in response packets */
119         if(ri->type!=SCSI_PDU_TYPE_RSP){
120                 return 0;
121         }
122         /* we are only interested in a specific commandset */
123         if( (!ri->itl) || ((ri->itl->cmdset&SCSI_CMDSET_MASK)!=rs->cmdset) ){
124                 return 0;
125         }
126         /* check that the opcode looks sane */
127         if( (!ri->itlq) || (ri->itlq->scsi_opcode>255) ){
128                 return 0;
129         }
130
131         add_srt_table_data(&rs->srt_table, ri->itlq->scsi_opcode, &ri->itlq->fc_time, pinfo);
132
133         return 1;
134 }
135
136 static void
137 scsistat_draw(void *arg)
138 {
139         scsistat_t *rs = (scsistat_t *)arg;
140
141         draw_srt_table_data(&rs->srt_table);
142 }
143
144
145
146 /* since the gtk2 implementation of tap is multithreaded we must protect
147  * remove_tap_listener() from modifying the list while draw_tap_listener()
148  * is running.  the other protected block is in main.c
149  *
150  * there should not be any other critical regions in gtk2
151  */
152 static void
153 win_destroy_cb(GtkWindow *win _U_, gpointer data)
154 {
155         scsistat_t *rs=(scsistat_t *)data;
156
157         protect_thread_critical_region();
158         remove_tap_listener(rs);
159         unprotect_thread_critical_region();
160
161         free_srt_table_data(&rs->srt_table);
162         g_free(rs);
163 }
164
165
166 /* When called, this function will create a new instance of gtk2-scsistat.
167  */
168 static void
169 gtk_scsistat_init(const char *optarg, void* userdata _U_)
170 {
171         scsistat_t *rs;
172         guint32 i;
173         char *title_string;
174         char *filter_string;
175         GtkWidget *vbox;
176         GtkWidget *stat_label;
177         GtkWidget *filter_label;
178         GtkWidget *bbox;
179         GtkWidget *close_bt;
180         int program, pos;
181         const char *filter=NULL;
182         GString *error_string;
183         const char *hf_name=NULL;
184
185         pos=0;
186         if(sscanf(optarg,"scsi,srt,%d,%n",&program,&pos)==1){
187                 if(pos){
188                         filter=optarg+pos;
189                 } else {
190                         filter=NULL;
191                 }
192         } else {
193                 fprintf(stderr, "wireshark: invalid \"-z scsi,srt,<cmdset>[,<filter>]\" argument\n");
194                 exit(1);
195         }
196
197         scsi_program=program;
198         rs=(scsistat_t *)g_malloc(sizeof(scsistat_t));
199         rs->cmdset=program;
200         switch(program){
201         case SCSI_DEV_SBC:
202                 rs->prog="SBC (disk)";
203                 rs->cdbnames=scsi_sbc_vals;
204                 hf_name="scsi.sbc.opcode";
205                 break;
206         case SCSI_DEV_SSC:
207                 rs->prog="SSC (tape)";
208                 rs->cdbnames=scsi_ssc_vals;
209                 hf_name="scsi.ssc.opcode";
210                 break;
211         case SCSI_DEV_CDROM:
212                 rs->prog="MMC (cd/dvd)";
213                 rs->cdbnames=scsi_mmc_vals;
214                 hf_name="scsi.mmc.opcode";
215                 break;
216         case SCSI_DEV_SMC:
217                 rs->prog="SMC (tape robot)";
218                 rs->cdbnames=scsi_smc_vals;
219                 hf_name="scsi.smc.opcode";
220                 break;
221         case SCSI_DEV_OSD:
222                 rs->prog="OSD (object based)";
223                 rs->cdbnames=scsi_osd_vals;
224                 hf_name="scsi.osd.opcode";
225                 break;
226         }
227
228         rs->win = dlg_window_new("scsi-stat");  /* transient_for top_level */
229         gtk_window_set_destroy_with_parent (GTK_WINDOW(rs->win), TRUE);
230         gtk_window_set_default_size(GTK_WINDOW(rs->win), 550, 400);
231         scsistat_set_title(rs);
232
233         vbox=gtk_vbox_new(FALSE, 3);
234         gtk_container_add(GTK_CONTAINER(rs->win), vbox);
235         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
236
237         title_string = scsistat_gen_title(rs);
238         stat_label=gtk_label_new(title_string);
239         g_free(title_string);
240         gtk_box_pack_start(GTK_BOX(vbox), stat_label, FALSE, FALSE, 0);
241
242         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
243         filter_label=gtk_label_new(filter_string);
244         g_free(filter_string);
245         gtk_label_set_line_wrap(GTK_LABEL(filter_label), TRUE);
246         gtk_box_pack_start(GTK_BOX(vbox), filter_label, FALSE, FALSE, 0);
247
248         /* We must display TOP LEVEL Widget before calling init_srt_table() */
249         gtk_widget_show_all(rs->win);
250
251         init_srt_table(&rs->srt_table, 256, vbox, hf_name);
252
253         for(i=0;i<256;i++){
254                 init_srt_table_row(&rs->srt_table, i, val_to_str(i, rs->cdbnames, "Unknown-0x%02x"));
255         }
256
257
258         error_string=register_tap_listener("scsi", rs, filter, 0, scsistat_reset, scsistat_packet, scsistat_draw);
259         if(error_string){
260                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
261                 g_string_free(error_string, TRUE);
262                 free_srt_table_data(&rs->srt_table);
263                 g_free(rs);
264                 return;
265         }
266
267         /* Button row. */
268         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
269         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
270
271         close_bt = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
272         window_set_cancel_button(rs->win, close_bt, window_cancel_button_cb);
273
274         g_signal_connect(rs->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
275         g_signal_connect(rs->win, "destroy", G_CALLBACK(win_destroy_cb), rs);
276
277         gtk_widget_show_all(rs->win);
278         window_present(rs->win);
279
280         cf_retap_packets(&cfile);
281         gdk_window_raise(gtk_widget_get_window(rs->win));
282 }
283
284 static enum_val_t scsi_command_sets[] = {
285         { "sbc", "SBC (disk)", SCSI_DEV_SBC },
286         { "ssc", "SSC (tape)", SCSI_DEV_SSC },
287         { "mmc", "MMC (cd/dvd)", SCSI_DEV_CDROM },
288         { "smc", "SMC (tape robot)", SCSI_DEV_SMC },
289         { "osd", "OSD (object based)", SCSI_DEV_OSD },
290         { NULL, NULL, 0 }
291 };
292
293 static tap_param scsi_stat_params[] = {
294         { PARAM_ENUM, "Command set", scsi_command_sets },
295         { PARAM_FILTER, "Filter", NULL }
296 };
297
298 static tap_param_dlg scsi_stat_dlg = {
299         "SCSI SRT Statistics",
300         "scsi,srt",
301         gtk_scsistat_init,
302         -1,
303         G_N_ELEMENTS(scsi_stat_params),
304         scsi_stat_params
305 };
306
307 void
308 register_tap_listener_gtkscsistat(void)
309 {
310         register_dfilter_stat(&scsi_stat_dlg, "SCSI",
311             REGISTER_STAT_GROUP_RESPONSE_TIME);
312 }
313
314 void scsi_srt_cb(GtkAction *action, gpointer user_data _U_)
315 {
316         tap_param_dlg_cb(action, &scsi_stat_dlg);
317 }
318