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