Skip to content

Commit 0091dd8

Browse files
committed
Improve iSCSI PDU header dump
Dump iSCSI opcode firstly, then dump SCSI opcode for SCSI request. Signed-off-by: zhenwei pi <[email protected]>
1 parent a6a664e commit 0091dd8

File tree

1 file changed

+98
-12
lines changed

1 file changed

+98
-12
lines changed

lib/pdu.c

+98-12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "iscsi-private.h"
4949
#include "scsi-lowlevel.h"
5050
#include "slist.h"
51+
#include "utils.h"
5152

5253
/* This adds 32-bit serial comparision as defined in RFC1982.
5354
* It returns 0 for equality, 1 if s1 is greater than s2 and
@@ -78,19 +79,104 @@ iscsi_itt_post_increment(struct iscsi_context *iscsi) {
7879
return old_itt;
7980
}
8081

82+
static const char *
83+
iscsi_opcode_str(int opcode)
84+
{
85+
static struct iscsi_value_string opcode_strings[] = {
86+
{ ISCSI_PDU_NOP_OUT,
87+
"NOP_OUT" },
88+
{ ISCSI_PDU_SCSI_REQUEST,
89+
"SCSI_REQUEST" },
90+
{ ISCSI_PDU_SCSI_TASK_MANAGEMENT_REQUEST,
91+
"SCSI_TASK_MANAGEMENT_REQUEST" },
92+
{ ISCSI_PDU_LOGIN_REQUEST,
93+
"LOGIN_REQUEST" },
94+
{ ISCSI_PDU_TEXT_REQUEST,
95+
"TEXT_REQUEST" },
96+
{ ISCSI_PDU_DATA_OUT,
97+
"DATA_OUT" },
98+
{ ISCSI_PDU_LOGOUT_REQUEST,
99+
"LOGOUT_REQUEST" },
100+
{ ISCSI_PDU_NOP_IN,
101+
"NOP_IN" },
102+
{ ISCSI_PDU_SCSI_RESPONSE,
103+
"SCSI_RESPONSE" },
104+
{ ISCSI_PDU_SCSI_TASK_MANAGEMENT_RESPONSE,
105+
"SCSI_TASK_MANAGEMENT_RESPONSE" },
106+
{ ISCSI_PDU_LOGIN_RESPONSE,
107+
"LOGIN_RESPONSE" },
108+
{ ISCSI_PDU_TEXT_RESPONSE,
109+
"TEXT_RESPONSE" },
110+
{ ISCSI_PDU_DATA_IN,
111+
"DATA_IN" },
112+
{ ISCSI_PDU_LOGOUT_RESPONSE,
113+
"LOGOUT_RESPONSE" },
114+
{ ISCSI_PDU_R2T,
115+
"R2T" },
116+
{ ISCSI_PDU_ASYNC_MSG,
117+
"ASYNC_MSG" },
118+
{ ISCSI_PDU_REJECT,
119+
"REJECT" },
120+
{ ISCSI_PDU_NO_PDU,
121+
"NO_PDU" },
122+
{0, NULL}
123+
};
124+
125+
return iscsi_value_string_find(opcode_strings, opcode, "UNKNOWN");
126+
}
127+
81128
void iscsi_dump_pdu_header(struct iscsi_context *iscsi, unsigned char *data) {
82-
char dump1[33*3+1]={0};
83-
char dump2[(ISCSI_RAW_HEADER_SIZE-33)*3+1]={0};
84-
const char *opcode;
85-
int i;
86-
for (i=0;i<33;i++) {
87-
snprintf(&dump1[i * 3], 4, " %02x", data[i]);
88-
}
89-
opcode = scsi_opcode_str(data[32]);
90-
for (;i<ISCSI_RAW_HEADER_SIZE;i++) {
91-
snprintf(&dump2[(i-33) * 3], 4, " %02x", data[i]);
92-
}
93-
ISCSI_LOG(iscsi, 2, "PDU header:%s[%s]%s", dump1, opcode, dump2);
129+
char output[1024] = { 0 };
130+
unsigned char iscsi_opcode;
131+
const char *iscsi_opcode_string;
132+
int output_off = 0;
133+
int data_off = 0;
134+
135+
136+
/* start to dump iSCSI opcode - data[0] */
137+
iscsi_opcode = data[data_off];
138+
iscsi_opcode_string = iscsi_opcode_str(iscsi_opcode);
139+
output_off += snprintf(&output[output_off], sizeof(output) - output_off - 1, "%02x[%s]",
140+
iscsi_opcode, iscsi_opcode_string);
141+
data_off++;
142+
143+
if (iscsi_opcode == ISCSI_PDU_SCSI_REQUEST) {
144+
unsigned char scsi_opcode;
145+
const char *scsi_opcode_string;
146+
147+
/* the rest iSCSI PDU: data[1] - data[31] */
148+
for ( ; data_off < 32; data_off++) {
149+
if (sizeof(output) - output_off - 1 > 0) {
150+
output_off += snprintf(&output[output_off], sizeof(output) - output_off - 1,
151+
" %02x", data[data_off]);
152+
}
153+
}
154+
155+
/* SCSI opcode: data[32] */
156+
scsi_opcode = data[data_off];
157+
scsi_opcode_string = scsi_opcode_str(scsi_opcode);
158+
if (sizeof(output) - output_off - 1 > 0) {
159+
output_off += snprintf(&output[output_off], sizeof(output) - output_off - 1,
160+
" %02x[%s]", scsi_opcode, scsi_opcode_string);
161+
data_off++;
162+
}
163+
164+
/* the rest SCSI PDU: data[33] - data[ISCSI_RAW_HEADER_SIZE - 1] */
165+
for ( ; data_off < ISCSI_RAW_HEADER_SIZE; data_off++) {
166+
if (sizeof(output) - output_off - 1 > 0) {
167+
output_off += snprintf(&output[output_off], sizeof(output) - output_off - 1,
168+
" %02x", data[data_off]);
169+
}
170+
}
171+
} else {
172+
/* the rest iSCSI PDU: data[1] - data[ISCSI_RAW_HEADER_SIZE - 1] */
173+
for ( ; data_off < ISCSI_RAW_HEADER_SIZE; data_off++) {
174+
output_off += snprintf(&output[output_off], sizeof(output) - output_off - 1, " %02x",
175+
data[data_off]);
176+
}
177+
}
178+
179+
ISCSI_LOG(iscsi, 2, "PDU header: %s", output);
94180
}
95181

96182
struct iscsi_pdu*

0 commit comments

Comments
 (0)