From 7e59b9bd2388846eb919faa42f2df3028e7b7658 Mon Sep 17 00:00:00 2001 From: wanghonghao Date: Sat, 9 May 2020 09:11:53 +0800 Subject: [PATCH] socket: fix rewrite cmdsn of immediate pdus Cmdsn of a data-out pdu struct is less than `expcmdsn` since it's from its cmd pdu. A data-out pdu doesn't carray a cmdsn on the wire actually, so it doesn't matter to itself, but if we rewrite the cmdsn of a immediate pdu with it, it will cause an error. Related error logs: libiscsi: iscsi_write_to_socket: outqueue[0]->cmdsn < expcmdsn (3648bab5 < 3648bab9) opcode 00 [iqn.2003-01.org.linux-iscsi.tgt0] libiscsi: reconnect initiated [iqn.2003-01.org.linux-iscsi.tgt0] libiscsi: connecting to portal 127.0.0.1 [iqn.2003-01.org.linux-iscsi.tgt0] libiscsi: connection established (127.0.0.1:62404 -> 127.0.0.1) [iqn.2003-01.org.linux-iscsi.tgt0] Signed-off-by: wanghonghao --- lib/socket.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/socket.c b/lib/socket.c index cd6c97a..f7c1b15 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -105,11 +105,19 @@ iscsi_add_to_outqueue(struct iscsi_context *iscsi, struct iscsi_pdu *pdu) /* queue pdus in ascending order of CmdSN. * ensure that pakets with the same CmdSN are kept in FIFO order. * immediate PDUs are queued in front of queue with the CmdSN - * of the first element in the outqueue. + * of the first cmd pdu in the outqueue. */ if (pdu->outdata.data[0] & ISCSI_PDU_IMMEDIATE) { - iscsi_pdu_set_cmdsn(pdu, current->cmdsn); + do { + if ((current->outdata.data[0] & 0x3f) != ISCSI_PDU_DATA_OUT) { + iscsi_pdu_set_cmdsn(pdu, current->cmdsn); + break; + } + current = current->next; + } while (current != NULL); + + current = iscsi->outqueue; } do {