1 /* tap-scsistat.c 2010 Chris Costa and Cal Turney
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
35 #include <epan/packet_info.h>
36 #include <epan/epan.h>
37 #include <epan/stat_cmd_args.h>
39 #include <epan/conversation.h>
40 #include <epan/dissectors/packet-scsi.h>
41 #include <epan/dissectors/packet-fc.h>
42 #include <epan/dissectors/packet-scsi-sbc.h>
43 #include <epan/dissectors/packet-scsi-ssc.h>
44 #include <epan/dissectors/packet-scsi-smc.h>
45 #include <epan/dissectors/packet-scsi-osd.h>
47 static guint8 scsi_program=0;
49 /* used to keep track of statistics for a specific procedure */
50 typedef struct _scsi_procedure_t {
58 /* used to keep track of the statistics for an entire program interface */
59 typedef struct _scsistat_t {
62 const value_string *cdbnames;
64 #define MAX_PROCEDURES 256
65 scsi_procedure_t *procedures;
68 #define NANOSECS_PER_SEC 1000000000
71 scsistat_reset(void *prs)
76 for(i=0; i < MAX_PROCEDURES; i++) {
77 rs->procedures[i].num=0;
78 rs->procedures[i].min.secs=0;
79 rs->procedures[i].min.nsecs=0;
80 rs->procedures[i].max.secs=0;
81 rs->procedures[i].max.nsecs=0;
82 rs->procedures[i].tot.secs=0;
83 rs->procedures[i].tot.nsecs=0;
88 scsistat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
91 const scsi_task_data_t *ri = pri;
95 /* we are only interested in response packets */
96 if(ri->type!=SCSI_PDU_TYPE_RSP) {
99 /* we are only interested in a specific commandset */
100 if( (!ri->itl) || ((ri->itl->cmdset&SCSI_CMDSET_MASK)!=rs->cmdset) ) {
103 /* check that the opcode looks sane */
104 if( (!ri->itlq) || (ri->itlq->scsi_opcode > 255) ) {
108 rp=&(rs->procedures[ri->itlq->scsi_opcode]);
110 /* calculate time delta between request and reply */
111 nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->itlq->fc_time);
114 rp->max.secs=delta.secs;
115 rp->max.nsecs=delta.nsecs;
118 rp->min.secs= delta.secs;
119 rp->min.nsecs=delta.nsecs;
121 if( (delta.secs < rp->min.secs)
122 ||( (delta.secs == rp->min.secs)
123 &&(delta.nsecs < rp->min.nsecs) ) ) {
124 rp->min.secs = delta.secs;
125 rp->min.nsecs= delta.nsecs;
127 if( (delta.secs > rp->max.secs)
128 ||( (delta.secs == rp->max.secs)
129 &&(delta.nsecs > rp->max.nsecs) ) ) {
130 rp->max.secs = delta.secs;
131 rp->max.nsecs= delta.nsecs;
133 rp->tot.secs += delta.secs;
134 rp->tot.nsecs += delta.nsecs;
135 if(rp->tot.nsecs > NANOSECS_PER_SEC) {
136 rp->tot.nsecs -= NANOSECS_PER_SEC;
144 scsistat_draw(void *prs)
151 printf("===========================================================\n");
152 printf("SCSI %s SRT Statistics:\n", rs->prog);
153 printf("Filter: %s\n", rs->filter?rs->filter:"");
154 printf("Procedure Calls Min SRT Max SRT Avg SRT\n");
155 for(i=0; i < MAX_PROCEDURES; i++) {
156 if(rs->procedures[i].num==0) {
159 /* scale it to units of 1us.*/
160 td = ((guint64)(rs->procedures[i].tot.secs)) * NANOSECS_PER_SEC + rs->procedures[i].tot.nsecs;
161 td = ((td / rs->procedures[i].num) + 500) / 1000;
163 printf("%-19s %6d %3d.%06u %3d.%06u %3d.%06u \n",
164 rs->procedures[i].proc,
165 rs->procedures[i].num,
166 (int)(rs->procedures[i].min.secs),
167 (rs->procedures[i].min.nsecs+500)/1000,
168 (int)(rs->procedures[i].max.secs),
169 (rs->procedures[i].max.nsecs+500)/1000,
170 (int)(td/1000000), (int)(td%1000000)
173 printf("===========================================================\n");
177 scsistat_init(const char *optarg, void* userdata _U_)
182 const char *filter=NULL;
183 GString *error_string;
186 if(sscanf(optarg, "scsi,srt,%d,%n", &program, &pos)==1) {
193 fprintf(stderr, "tshark: invalid \"-z scsi,srt,<cmdset>[,<filter>]\" argument\n");
197 scsi_program=program;
198 rs=g_malloc(sizeof(scsistat_t));
200 rs->filter=g_strdup(filter);
208 rs->prog="SBC (disk)";
209 rs->cdbnames=scsi_sbc_vals;
212 rs->prog="SSC (tape)";
213 rs->cdbnames=scsi_ssc_vals;
216 rs->prog="MMC (cd/dvd)";
217 rs->cdbnames=scsi_mmc_vals;
220 rs->prog="SMC (tape robot)";
221 rs->cdbnames=scsi_smc_vals;
224 rs->prog="OSD (object based)";
225 rs->cdbnames=scsi_osd_vals;
228 /* Default to the SBC (disk), since this is what EMC SCSI seem to always be */
230 rs->prog="SBC (disk)";
231 rs->cdbnames=scsi_sbc_vals;
234 rs->procedures=g_malloc(sizeof(scsi_procedure_t)*MAX_PROCEDURES);
235 for(i=0; i < MAX_PROCEDURES; i++) {
236 rs->procedures[i].proc=val_to_str(i, rs->cdbnames, "Unknown-0x%02x");
237 rs->procedures[i].num=0;
238 rs->procedures[i].min.secs=0;
239 rs->procedures[i].min.nsecs=0;
240 rs->procedures[i].max.secs=0;
241 rs->procedures[i].max.nsecs=0;
242 rs->procedures[i].tot.secs=0;
243 rs->procedures[i].tot.nsecs=0;
245 error_string=register_tap_listener("scsi", rs, filter, 0, scsistat_reset, scsistat_packet, scsistat_draw);
247 /* error, we failed to attach to the tap. clean up */
248 g_free(rs->procedures);
252 fprintf(stderr, "tshark: Couldn't register scsi,srt tap: %s\n",
254 g_string_free(error_string, TRUE);
260 register_tap_listener_scsistat(void)
262 register_stat_cmd_arg("scsi,srt,", scsistat_init, NULL);