aboutsummaryrefslogtreecommitdiffstats
path: root/ui/cli/tap-scsistat.c
blob: b0dae2960d752648fa9be790360bb952ca533b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* tap-scsistat.c	2010 Chris Costa and Cal Turney
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>

#include <string.h>
#include <epan/packet_info.h>
#include <epan/epan.h>
#include <epan/stat_cmd_args.h>
#include <epan/tap.h>
#include <epan/conversation.h>
#include <epan/dissectors/packet-scsi.h>
#include <epan/dissectors/packet-fc.h>
#include <epan/dissectors/packet-scsi-sbc.h>
#include <epan/dissectors/packet-scsi-ssc.h>
#include <epan/dissectors/packet-scsi-smc.h>
#include <epan/dissectors/packet-scsi-osd.h>

static guint8 scsi_program=0;

/* used to keep track of statistics for a specific procedure */
typedef struct _scsi_procedure_t {
	const char *proc;
	int num;
	nstime_t min;
	nstime_t max;
	nstime_t tot;
} scsi_procedure_t;

/* used to keep track of the statistics for an entire program interface */
typedef struct _scsistat_t {
	guint8 cmdset;
	char *filter;
	const value_string *cdbnames;
	const char *prog;
#define MAX_PROCEDURES 256
	scsi_procedure_t *procedures;
} scsistat_t;

#define NANOSECS_PER_SEC 1000000000

static void
scsistat_reset(void *prs)
{
	scsistat_t *rs=prs;
	guint32 i;

	for(i=0; i < MAX_PROCEDURES; i++) {
		rs->procedures[i].num=0;
		rs->procedures[i].min.secs=0;
		rs->procedures[i].min.nsecs=0;
		rs->procedures[i].max.secs=0;
		rs->procedures[i].max.nsecs=0;
		rs->procedures[i].tot.secs=0;
		rs->procedures[i].tot.nsecs=0;
	}
}

static int
scsistat_packet(void *prs, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
{
	scsistat_t *rs = prs;
	const scsi_task_data_t *ri = pri;
	nstime_t delta;
	scsi_procedure_t *rp;

	/* we are only interested in response packets */
	if(ri->type!=SCSI_PDU_TYPE_RSP) {
		return 0;
	}
	/* we are only interested in a specific commandset */
	if( (!ri->itl) || ((ri->itl->cmdset&SCSI_CMDSET_MASK)!=rs->cmdset) ) {
		return 0;
	}
	/* check that the opcode looks sane */
	if( (!ri->itlq) || (ri->itlq->scsi_opcode > 255) ) {
		return 0;
	}

	rp=&(rs->procedures[ri->itlq->scsi_opcode]);

	/* calculate time delta between request and reply */
	nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->itlq->fc_time);

	if(rp->num==0) {
		rp->max.secs=delta.secs;
		rp->max.nsecs=delta.nsecs;
	}
	if(rp->num==0) {
		rp->min.secs= delta.secs;
		rp->min.nsecs=delta.nsecs;
	}
	if( (delta.secs  < rp->min.secs)
	||( (delta.secs == rp->min.secs)
	  &&(delta.nsecs < rp->min.nsecs) ) ) {
		rp->min.secs = delta.secs;
		rp->min.nsecs= delta.nsecs;
	}
	if( (delta.secs  > rp->max.secs)
	||( (delta.secs == rp->max.secs)
	  &&(delta.nsecs > rp->max.nsecs) ) ) {
		rp->max.secs = delta.secs;
		rp->max.nsecs= delta.nsecs;
	}
	rp->tot.secs  += delta.secs;
	rp->tot.nsecs += delta.nsecs;
	if(rp->tot.nsecs > NANOSECS_PER_SEC) {
		rp->tot.nsecs -= NANOSECS_PER_SEC;
		rp->tot.secs++;
	}
	rp->num++;
	return 1;
}

static void
scsistat_draw(void *prs)
{
	scsistat_t *rs=prs;
	guint32 i;
	guint64 td;

	printf("\n");
	printf("===========================================================\n");
	printf("SCSI %s SRT Statistics:\n", rs->prog);
	printf("Filter: %s\n", rs->filter?rs->filter:"");
	printf("Procedure            Calls   Min SRT    Max SRT    Avg SRT\n");
	for(i=0; i < MAX_PROCEDURES; i++) {
		if(rs->procedures[i].num==0) {
			continue;
		}
		/* scale it to units of 1us.*/
		td = ((guint64)(rs->procedures[i].tot.secs)) * NANOSECS_PER_SEC + rs->procedures[i].tot.nsecs;
		td = ((td / rs->procedures[i].num) + 500) / 1000;

		printf("%-19s %6d %3d.%06u %3d.%06u %3d.%06u \n",
			rs->procedures[i].proc,
			rs->procedures[i].num,
			(int)(rs->procedures[i].min.secs),
			(rs->procedures[i].min.nsecs+500)/1000,
			(int)(rs->procedures[i].max.secs),
			(rs->procedures[i].max.nsecs+500)/1000,
			(int)(td/1000000), (int)(td%1000000)
		);
	}
	printf("===========================================================\n");
}

static void
scsistat_init(const char *optarg, void* userdata _U_)
{
	scsistat_t *rs;
	guint32 i;
	int program, pos;
	const char *filter=NULL;
	GString *error_string;

	pos=0;
	if(sscanf(optarg, "scsi,srt,%d,%n", &program, &pos)==1) {
		if(pos) {
			filter=optarg+pos;
		} else {
			filter=NULL;
		}
	} else {
		fprintf(stderr, "tshark: invalid \"-z scsi,srt,<cmdset>[,<filter>]\" argument\n");
		exit(1);
	}

	scsi_program=program;
	rs=g_malloc(sizeof(scsistat_t));
	if(filter) {
		rs->filter=g_strdup(filter);
	} else {
		rs->filter=NULL;
	}
	rs->cmdset=program;

	switch(program) {
		case SCSI_DEV_SBC:
			rs->prog="SBC (disk)";
			rs->cdbnames=scsi_sbc_vals;
			break;
		case SCSI_DEV_SSC:
			rs->prog="SSC (tape)";
			rs->cdbnames=scsi_ssc_vals;
			break;
		case SCSI_DEV_CDROM:
			rs->prog="MMC (cd/dvd)";
			rs->cdbnames=scsi_mmc_vals;
			break;
		case SCSI_DEV_SMC:
			rs->prog="SMC (tape robot)";
			rs->cdbnames=scsi_smc_vals;
			break;
		case SCSI_DEV_OSD:
			rs->prog="OSD (object based)";
			rs->cdbnames=scsi_osd_vals;
			break;
		default:
			/* Default to the SBC (disk), since this is what EMC SCSI seem to always be */
			rs->cmdset=0;
			rs->prog="SBC (disk)";
			rs->cdbnames=scsi_sbc_vals;
			break;
	}
	rs->procedures=g_malloc(sizeof(scsi_procedure_t)*MAX_PROCEDURES);
	for(i=0; i < MAX_PROCEDURES; i++) {
		rs->procedures[i].proc=val_to_str(i, rs->cdbnames, "Unknown-0x%02x");
		rs->procedures[i].num=0;
		rs->procedures[i].min.secs=0;
		rs->procedures[i].min.nsecs=0;
		rs->procedures[i].max.secs=0;
		rs->procedures[i].max.nsecs=0;
		rs->procedures[i].tot.secs=0;
		rs->procedures[i].tot.nsecs=0;
	}
	error_string=register_tap_listener("scsi", rs, filter, 0, scsistat_reset, scsistat_packet, scsistat_draw);
	if(error_string) {
		/* error, we failed to attach to the tap. clean up */
		g_free(rs->procedures);
		g_free(rs->filter);
		g_free(rs);

		fprintf(stderr, "tshark: Couldn't register scsi,srt tap: %s\n",
		        error_string->str);
		g_string_free(error_string, TRUE);
		exit(1);
	}
}

void
register_tap_listener_scsistat(void)
{
	register_stat_cmd_arg("scsi,srt,", scsistat_init, NULL);
}