Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 099cf21

Browse files
committedDec 1, 2023
Improve iSCSI PDU header dump
Dump iSCSI opcode firstly, then dump SCSI opcode for SCSI request. Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
1 parent a6a664e commit 099cf21

File tree

1 file changed

+92
-12
lines changed

1 file changed

+92
-12
lines changed
 

‎lib/pdu.c

+92-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,98 @@ 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, "%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+
output_off += snprintf(&output[output_off], sizeof(output) - output_off, " %02x",
150+
data[data_off]);
151+
}
152+
153+
/* SCSI opcode: data[32] */
154+
scsi_opcode = data[data_off];
155+
scsi_opcode_string = scsi_opcode_str(scsi_opcode);
156+
output_off += snprintf(&output[output_off], sizeof(output) - output_off, " %02x[%s]",
157+
scsi_opcode, scsi_opcode_string);
158+
data_off++;
159+
160+
/* the rest SCSI PDU: data[33] - data[ISCSI_RAW_HEADER_SIZE - 1] */
161+
for ( ; data_off < ISCSI_RAW_HEADER_SIZE; data_off++) {
162+
output_off += snprintf(&output[output_off], sizeof(output) - output_off, " %02x",
163+
data[data_off]);
164+
}
165+
} else {
166+
/* the rest iSCSI PDU: data[1] - data[ISCSI_RAW_HEADER_SIZE - 1] */
167+
for ( ; data_off < ISCSI_RAW_HEADER_SIZE; data_off++) {
168+
output_off += snprintf(&output[output_off], sizeof(output) - output_off, " %02x",
169+
data[data_off]);
170+
}
171+
}
172+
173+
ISCSI_LOG(iscsi, 2, "PDU header: %s", output);
94174
}
95175

96176
struct iscsi_pdu*

0 commit comments

Comments
 (0)
Please sign in to comment.