84a288e5701cdb1cbe9eee6fbaa3ee08fdb54e54
[obnox/wireshark/wip.git] / 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 "gtk/gui_stat_menu.h"
54 #include "gtk/gui_utils.h"
55 #include "gtk/dlg_utils.h"
56 #include "gtk/main.h"
57 #include "gtk/filter_dlg.h"
58 #include "gtk/stock_icons.h"
59 #include "gtk/service_response_time_table.h"
60 #include "gtk/gtkglobals.h"
61 #include "gtk/filter_autocomplete.h"
62
63
64 static GtkWidget *dlg=NULL;
65
66 /* used to keep track of the statistics for an entire scsi command set */
67 typedef struct _scsistat_t {
68         GtkWidget *win;
69         srt_stat_table srt_table;
70         guint8 cmdset;
71         const value_string *cdbnames;
72         const char *prog;
73 } scsistat_t;
74
75 static guint8 scsi_program=0;
76 static GtkWidget *filter_entry;
77
78 enum
79 {
80    SCSI_STAT_PROG_LABEL_SBC,
81    SCSI_STAT_PROG_LABEL_SSC,
82    SCSI_STAT_PROG_LABEL_MMC
83 };
84
85
86 static char *
87 scsistat_gen_title(scsistat_t *rs)
88 {
89         char *title;
90
91         title = g_strdup_printf("SCSI Service Response Time statistics for %s: %s",
92             rs->prog, cf_get_display_name(&cfile));
93         return title;
94 }
95
96 static void
97 scsistat_set_title(scsistat_t *rs)
98 {
99         char *title;
100
101         title = scsistat_gen_title(rs);
102         gtk_window_set_title(GTK_WINDOW(rs->win), title);
103         g_free(title);
104 }
105
106 static void
107 scsistat_reset(void *arg)
108 {
109         scsistat_t *rs = (scsistat_t *)arg;
110
111         reset_srt_table_data(&rs->srt_table);
112         scsistat_set_title(rs);
113 }
114
115
116 static void
117 dlg_destroy_cb(GtkWidget *w _U_, gpointer user_data _U_)
118 {
119         dlg=NULL;
120 }
121
122 static void
123 scsistat_program_select(GtkWidget *w, gpointer key _U_)
124 {
125         int i;
126
127         i = gtk_combo_box_get_active (GTK_COMBO_BOX(w));
128         switch(i){
129                 case SCSI_STAT_PROG_LABEL_SBC:
130                         scsi_program = SCSI_DEV_SBC;
131                         break;
132                 case SCSI_STAT_PROG_LABEL_SSC:
133                         scsi_program = SCSI_DEV_SSC;
134                         break;
135                 case SCSI_STAT_PROG_LABEL_MMC:
136                         scsi_program = SCSI_DEV_CDROM;
137                         break;
138                 default:
139                         scsi_program = SCSI_DEV_SBC;
140                         break;
141         }
142 }
143
144 static int
145 scsistat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *arg2)
146 {
147         scsistat_t *rs = (scsistat_t *)arg;
148         const scsi_task_data_t *ri = (const scsi_task_data_t *)arg2;
149
150         /* we are only interested in response packets */
151         if(ri->type!=SCSI_PDU_TYPE_RSP){
152                 return 0;
153         }
154         /* we are only interested in a specific commandset */
155         if( (!ri->itl) || ((ri->itl->cmdset&SCSI_CMDSET_MASK)!=rs->cmdset) ){
156                 return 0;
157         }
158         /* check that the opcode looks sane */
159         if( (!ri->itlq) || (ri->itlq->scsi_opcode>255) ){
160                 return 0;
161         }
162
163         add_srt_table_data(&rs->srt_table, ri->itlq->scsi_opcode, &ri->itlq->fc_time, pinfo);
164
165         return 1;
166 }
167
168 static void
169 scsistat_draw(void *arg)
170 {
171         scsistat_t *rs = (scsistat_t *)arg;
172
173         draw_srt_table_data(&rs->srt_table);
174 }
175
176
177
178 /* since the gtk2 implementation of tap is multithreaded we must protect
179  * remove_tap_listener() from modifying the list while draw_tap_listener()
180  * is running.  the other protected block is in main.c
181  *
182  * there should not be any other critical regions in gtk2
183  */
184 static void
185 win_destroy_cb(GtkWindow *win _U_, gpointer data)
186 {
187         scsistat_t *rs=(scsistat_t *)data;
188
189         protect_thread_critical_region();
190         remove_tap_listener(rs);
191         unprotect_thread_critical_region();
192
193         free_srt_table_data(&rs->srt_table);
194         g_free(rs);
195 }
196
197
198 /* When called, this function will create a new instance of gtk2-scsistat.
199  */
200 static void
201 gtk_scsistat_init(const char *optarg, void* userdata _U_)
202 {
203         scsistat_t *rs;
204         guint32 i;
205         char *title_string;
206         char *filter_string;
207         GtkWidget *vbox;
208         GtkWidget *stat_label;
209         GtkWidget *filter_label;
210         GtkWidget *bbox;
211         GtkWidget *close_bt;
212         int program, pos;
213         const char *filter=NULL;
214         GString *error_string;
215         const char *hf_name=NULL;
216
217         pos=0;
218         if(sscanf(optarg,"scsi,srt,%d,%n",&program,&pos)==1){
219                 if(pos){
220                         filter=optarg+pos;
221                 } else {
222                         filter=NULL;
223                 }
224         } else {
225                 fprintf(stderr, "wireshark: invalid \"-z scsi,srt,<cmdset>[,<filter>]\" argument\n");
226                 exit(1);
227         }
228
229         scsi_program=program;
230         rs=(scsistat_t *)g_malloc(sizeof(scsistat_t));
231         rs->cmdset=program;
232         switch(program){
233         case SCSI_DEV_SBC:
234                 rs->prog="SBC (disk)";
235                 rs->cdbnames=scsi_sbc_vals;
236                 hf_name="scsi.sbc.opcode";
237                 break;
238         case SCSI_DEV_SSC:
239                 rs->prog="SSC (tape)";
240                 rs->cdbnames=scsi_ssc_vals;
241                 hf_name="scsi.ssc.opcode";
242                 break;
243         case SCSI_DEV_CDROM:
244                 rs->prog="MMC (cd/dvd)";
245                 rs->cdbnames=scsi_mmc_vals;
246                 hf_name="scsi.mmc.opcode";
247                 break;
248         case SCSI_DEV_SMC:
249                 rs->prog="SMC (tape robot)";
250                 rs->cdbnames=scsi_smc_vals;
251                 hf_name="scsi.smc.opcode";
252                 break;
253         case SCSI_DEV_OSD:
254                 rs->prog="OSD (object based)";
255                 rs->cdbnames=scsi_osd_vals;
256                 hf_name="scsi.osd.opcode";
257                 break;
258         }
259
260         rs->win = dlg_window_new("scsi-stat");  /* transient_for top_level */
261         gtk_window_set_destroy_with_parent (GTK_WINDOW(rs->win), TRUE);
262         gtk_window_set_default_size(GTK_WINDOW(rs->win), 550, 400);
263         scsistat_set_title(rs);
264
265         vbox=gtk_vbox_new(FALSE, 3);
266         gtk_container_add(GTK_CONTAINER(rs->win), vbox);
267         gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
268
269         title_string = scsistat_gen_title(rs);
270         stat_label=gtk_label_new(title_string);
271         g_free(title_string);
272         gtk_box_pack_start(GTK_BOX(vbox), stat_label, FALSE, FALSE, 0);
273
274         filter_string = g_strdup_printf("Filter: %s", filter ? filter : "");
275         filter_label=gtk_label_new(filter_string);
276         g_free(filter_string);
277         gtk_label_set_line_wrap(GTK_LABEL(filter_label), TRUE);
278         gtk_box_pack_start(GTK_BOX(vbox), filter_label, FALSE, FALSE, 0);
279
280         /* We must display TOP LEVEL Widget before calling init_srt_table() */
281         gtk_widget_show_all(rs->win);
282
283         init_srt_table(&rs->srt_table, 256, vbox, hf_name);
284
285         for(i=0;i<256;i++){
286                 init_srt_table_row(&rs->srt_table, i, val_to_str(i, rs->cdbnames, "Unknown-0x%02x"));
287         }
288
289
290         error_string=register_tap_listener("scsi", rs, filter, 0, scsistat_reset, scsistat_packet, scsistat_draw);
291         if(error_string){
292                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", error_string->str);
293                 g_string_free(error_string, TRUE);
294                 free_srt_table_data(&rs->srt_table);
295                 g_free(rs);
296                 return;
297         }
298
299         /* Button row. */
300         bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
301         gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
302
303         close_bt = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
304         window_set_cancel_button(rs->win, close_bt, window_cancel_button_cb);
305
306         g_signal_connect(rs->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
307         g_signal_connect(rs->win, "destroy", G_CALLBACK(win_destroy_cb), rs);
308
309         gtk_widget_show_all(rs->win);
310         window_present(rs->win);
311
312         cf_retap_packets(&cfile);
313         gdk_window_raise(rs->win->window);
314 }
315
316
317
318 static void
319 scsistat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
320 {
321         GString *str;
322         const char *filter;
323
324         str = g_string_new("scsi,srt");
325         g_string_append_printf(str, ",%d", scsi_program);
326         filter=gtk_entry_get_text(GTK_ENTRY(filter_entry));
327         if(filter[0]!=0){
328                 g_string_append_printf(str, ",%s", filter);
329         }
330
331         gtk_scsistat_init(str->str,NULL);
332         g_string_free(str, TRUE);
333 }
334
335 static void
336 gtk_scsistat_cb(GtkWidget *w _U_, gpointer d _U_)
337 {
338         GtkWidget *prog_combo_box;
339         GtkWidget *dlg_box;
340         GtkWidget *prog_box, *prog_label;
341         GtkWidget *filter_box, *filter_bt;
342         GtkWidget *bbox, *start_button, *cancel_button;
343         const char *filter;
344         static construct_args_t args = {
345           "Service Response Time Statistics Filter",
346           TRUE,
347           FALSE,
348       FALSE
349         };
350
351         /* if the window is already open, bring it to front */
352         if(dlg){
353                 gdk_window_raise(dlg->window);
354                 return;
355         }
356
357         dlg=dlg_window_new("Wireshark: Compute SCSI SRT statistics");
358         gtk_window_set_default_size(GTK_WINDOW(dlg), 300, -1);
359
360         dlg_box=gtk_vbox_new(FALSE, 10);
361         gtk_container_set_border_width(GTK_CONTAINER(dlg_box), 10);
362         gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
363         gtk_widget_show(dlg_box);
364
365         /* Program box */
366         prog_box=gtk_hbox_new(FALSE, 10);
367
368         /* Program label */
369         gtk_container_set_border_width(GTK_CONTAINER(prog_box), 10);
370         prog_label=gtk_label_new("Commandset:");
371         gtk_box_pack_start(GTK_BOX(prog_box), prog_label, FALSE, FALSE, 0);
372         gtk_widget_show(prog_label);
373
374         /* Program menu */
375         prog_combo_box = gtk_combo_box_new_text ();
376
377         /* SBC */
378         gtk_combo_box_append_text (GTK_COMBO_BOX (prog_combo_box), "SBC (disk)");
379         /* SSC */
380         gtk_combo_box_append_text (GTK_COMBO_BOX (prog_combo_box), "SSC (tape)");
381         /* MMC */
382         gtk_combo_box_append_text (GTK_COMBO_BOX (prog_combo_box), "MMC (cd/dvd)");
383
384         gtk_box_pack_start(GTK_BOX(prog_box), prog_combo_box, TRUE, TRUE, 0);
385         gtk_combo_box_set_active(GTK_COMBO_BOX(prog_combo_box), SCSI_DEV_SBC);
386         g_signal_connect(prog_combo_box, "changed", G_CALLBACK(scsistat_program_select), NULL);
387         gtk_widget_show(prog_combo_box);
388
389         gtk_box_pack_start(GTK_BOX(dlg_box), prog_box, TRUE, TRUE, 0);
390         gtk_widget_show(prog_box);
391
392         /* Filter box */
393         filter_box=gtk_hbox_new(FALSE, 3);
394
395         /* Filter label */
396         filter_bt=gtk_button_new_from_stock(WIRESHARK_STOCK_DISPLAY_FILTER_ENTRY);
397         g_signal_connect(filter_bt, "clicked", G_CALLBACK(display_filter_construct_cb), &args);
398         gtk_box_pack_start(GTK_BOX(filter_box), filter_bt, FALSE, FALSE, 0);
399         gtk_widget_show(filter_bt);
400
401         /* Filter entry */
402         filter_entry=gtk_entry_new();
403         g_signal_connect(filter_entry, "changed", G_CALLBACK(filter_te_syntax_check_cb), NULL);
404         g_object_set_data(G_OBJECT(filter_box), E_FILT_AUTOCOMP_PTR_KEY, NULL);
405         g_signal_connect(filter_entry, "key-press-event", G_CALLBACK (filter_string_te_key_pressed_cb), NULL);
406         g_signal_connect(dlg, "key-press-event", G_CALLBACK (filter_parent_dlg_key_pressed_cb), NULL);
407
408         /* filter prefs dialog */
409         g_object_set_data(G_OBJECT(filter_bt), E_FILT_TE_PTR_KEY, filter_entry);
410         /* filter prefs dialog */
411
412         gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, TRUE, TRUE, 0);
413         filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
414         if(filter){
415                 gtk_entry_set_text(GTK_ENTRY(filter_entry), filter);
416         } else {
417                 colorize_filter_te_as_empty(filter_entry);
418         }
419         gtk_widget_show(filter_entry);
420
421         gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
422         gtk_widget_show(filter_box);
423
424         /* button box */
425         bbox = dlg_button_row_new(WIRESHARK_STOCK_CREATE_STAT, GTK_STOCK_CANCEL, NULL);
426         gtk_box_pack_start(GTK_BOX(dlg_box), bbox, FALSE, FALSE, 0);
427         gtk_widget_show(bbox);
428
429         start_button = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), WIRESHARK_STOCK_CREATE_STAT);
430         g_signal_connect_swapped(start_button, "clicked",
431                              G_CALLBACK(scsistat_start_button_clicked), NULL);
432
433         cancel_button = (GtkWidget *)g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CANCEL);
434         window_set_cancel_button(dlg, cancel_button, window_cancel_button_cb);
435
436         /* Give the initial focus to the "Filter" entry box. */
437         gtk_widget_grab_focus(filter_entry);
438
439         gtk_widget_grab_default(start_button );
440
441         g_signal_connect(dlg, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
442         g_signal_connect(dlg, "destroy", G_CALLBACK(dlg_destroy_cb), NULL);
443
444         gtk_widget_show_all(dlg);
445         window_present(dlg);
446 }
447
448
449 void
450 register_tap_listener_gtkscsistat(void)
451 {
452         register_stat_cmd_arg("scsi,srt,", gtk_scsistat_init, NULL);
453
454         register_stat_menu_item("SCSI...", REGISTER_STAT_GROUP_RESPONSE_TIME,
455             gtk_scsistat_cb, NULL, NULL, NULL);
456
457 }
458