Commit Graph

812 Commits

Author SHA1 Message Date
Ronnie Sahlberg
37bc6fcd81 TCP: immediately trigger the service thread to write PDU
IF qe queue a new PDU to an empty outqueue then the mt service thread
will still be stuck in poll() until it timesout or the socket becomes
readable.

Fix this by sending SIGUSR1 to the service thread when we queue a PDU
to an empty queue. This will break out of poll() and we can immediately
go and write to the socket.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
f63ed0b76e Remove two global variables used to set the initial seed
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
5b391ad188 Pass iscsi to iscsi_init_sync_state
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
09ec037e34 Make the mutex symbols always available
so we do not need to wrap them inside an ifdef.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
3c48aea225 Add initial multithreading support and example
This is the basic support for doing i/o in a separate worker thread.
It is still not threads safe but a start.
Now we need to protect all variables such as outqueue, waitpdu
and friends.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
cb44ad4e26 Add multithreading helpers
Add an abstraction for mutexts and threads
that handles both pthread api and native win32 api

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-03-07 08:46:47 +10:00
Ronnie Sahlberg
b8d9fbf5e3 Add CHAP SHA3-256 support
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-01-04 14:20:28 +10:00
Ronnie Sahlberg
34e83f7dbd Add support for CHAP with SHA-256
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-01-04 09:09:02 +10:00
Ronnie Sahlberg
95a0d98cfd Add support for CHAP using SHA1
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-01-04 00:59:02 +10:00
Ronnie Sahlberg
a92b41318c Don't use inet_ntoa, it is deprecated
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-01-03 15:48:51 +10:00
Ronnie Sahlberg
9637e1371e Merge pull request #434 from raywang-dev/fix-stack-corruption
fix: scan PDUs before exiting event_loop due to connect timedout
2024-12-07 01:30:47 -05:00
hongleiwang
5cb77051ce fix: scan PDUs before exiting event_loop due to connect timedout
In iSCSI synchronous operations, a struct iscsi_sync_state variable
(state) is allocated on the stack, and its address is assigned to
pdu->scsi_cbdata.private_data. This address is eventually used in
the PDU callback function.

However, if a reconnection occurs during a synchronous operation
(e.g., read or write), but the connect function fails (iscsi->fd
will be set to -1), the event_loop times out and exits. At this point,
unprocessed PDUs remain. If the PDU callback function is triggered
after the timeout (e.g., during iscsi_destroy_context), it may
access the pdu->scsi_cbdata.private_data address, which no longer
points to the original stack allocation. Writing to this invalid
address in the callback corrupts the current stack structure,
leading to process crash.

This patch addresses the issue by scanning PDUs before exiting the
event_loop due connect timedout, ensuring the unprocessed PDUs are
properly handled to prevent stack corruption and crash.

Signed-off-by: raywang <honglei.wang@smartx.com>
2024-12-04 17:55:49 +08:00
Tianren Zhang
458030219d lib: finish event_loop on invalid fd
When iscsi->fd gets invalid, there is not point to
keep stuck in the event loop, instead could give an
accurate error about the invalid fd.

Signed-off-by: Tianren Zhang <tianren@smartx.com>
2024-11-27 04:53:02 +00:00
Tianren Zhang
97ba4c34e2 lib: reserve the fd on reconnect
On reconnect case, the iscsi_tcp_connect tries to reuse
the fd number of old_iscsi. However, this fd could have been
already closed in previous iscsi_tcp_disconnect if
iscsi->fd == iscsi->old_iscsi->fd and the fd number
might have been allocated to some other caller, in this
case the fd reuse in iscsi_tcp_connect is not safe anymore.

Solve this by not closing the fd if iscsi and old_iscsi
share the same fd on reconnect to "really" reserve this
fd number.

Signed-off-by: Tianren Zhang <tianren@smartx.com>
2024-11-22 06:16:59 +00:00
IriKa Qiu
057fa61f00 Fix free pdu mismatch with alloc
The pdu alloced by iscsi->drv->new_pdu, by free with iscsi_free direct
when fail in iscsi_allocate_pdu.

Signed-off-by: IriKa Qiu <qiujie.jq@gmail.com>
2024-11-10 03:19:37 +00:00
IriKa Qiu
2934d643ca Fix pdu indata of iser alloc and free mismatch
The pdu indata alloc by iscsi_malloc with a undetermined size, but free
by iscsi_sfree. The iscsi_sfree can only be used to free memory which
size is equal to iscsi->smalloc_size.

Signed-off-by: IriKa Qiu <qiujie.jq@gmail.com>
2024-11-10 03:10:51 +00:00
hongleiwang
29e626c0f4 feat: add iscsi_reset_next_reconnect interface
When an iSCSI connection enters the reconnection phase, the backoff
time (next_reconnect) increases with reconnection retry_cnt. However,
if the client detects that the target has recovered before reaching
next_reconnect, calling iscsi_reconnect/iscsi_force_reconnect has no
any effect, making fast reconnection impossible.

This patch introduces an interface to reset next_reconnect, so that
the client can reset the backoff time upon detecting target recovery
and achieve faster reconnection.

Resolves: https://github.com/sahlberg/libiscsi/issues/428

Signed-off-by: raywang <honglei.wang@smartx.com>
2024-10-30 18:29:33 +08:00
Ronnie Sahlberg
b25ee4f0c4 fix use after free in recent commit
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2024-05-30 07:49:48 +10:00
Ronnie Sahlberg
2227e7bda2 Merge pull request #400 from anatoliy-glagolev/master
lun_reset cancelling lun tasks only
2024-05-30 07:48:24 +10:00
Ronnie Sahlberg
dd94a21f64 Merge pull request #408 from 54shady/fix-task-free
Manually set task to null after free to avoid double free issue
2024-05-30 07:47:24 +10:00
Brian Meagher
882bcad53a Add support for Data Digest 2024-05-04 19:34:06 -07:00
Paolo Bonzini
35fec3ea0e login: add support for gnutls
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2024-04-09 16:06:35 +02:00
Paolo Bonzini
e07472a337 login: do not try to "emulate" the libgcrypt API
Implement a more generic wrapper API for message digests, so
that it is easier to also include gnutls as an option.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2024-04-09 16:01:43 +02:00
Ronnie Sahlberg
d960e6253c New version 1.20.0
Mostly various updates to the test tool

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2024-02-05 16:59:18 +10:00
zeroway
9ec12158b0 Manually set task to null after free to avoid double free issue 2024-01-19 17:41:54 +08:00
zhenwei pi
384191f57f Improve iSCSI PDU header dump
Dump iSCSI opcode firstly, then dump SCSI opcode for SCSI request.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-12-01 16:42:12 +08:00
zhenwei pi
a6a664e8cb Abstract value_string as common utility
Originally, we use this in scsi-lowlevel.c only, this works as static
function. It also could be used to dump ISCSI opcode, so move it into
common utils.h/utils.c.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-12-01 10:13:20 +08:00
zhenwei pi
addb11d64c Drain DATAOUT PDU on timeout
Describe the reason again:
A WRITE16 command[w] handles R2T, and queues DATAOUT PDU m,x,y,z:

          outqueue->DATAOUT[x]->DATAOUT[y]->DATAOUT[z]...
  outqueue_current->DATAOUT[m]
         waitqueue->WRITE16[w]...

1, Once x, y, z gets released in initiator side, the target still expects the remaining
DATAOUT PDUs.
2, Once command w timeout and callback to uplayer, uplayers usually releases memory of
   iscsi task(include memory referenced by iovec.iov_base). DATAOUT[m] would access
   invalid memory iovce.iov_base.

So invoke WRITEx command callback until draining DATAOUT PDUs.

Co-developed-by: Rui Zhang <zhangrui.1203@bytedance.com>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-11-21 16:59:40 +08:00
zhenwei pi
3593362721 Log necessary message on internal reconnect
Once error occurs on socker read/write, libiscsi tries to reconnect
silently. Add necessary log message for this case.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-11-14 15:39:33 +08:00
zhenwei pi
4c3ac5464a Dump opcode string in iscsi_dump_pdu_header()
Now we have more friendly opcode message once error occurs:
libiscsi:1 command timed out from waitqueue [...]
libiscsi:2 PDU header: 01 c1 ... e9 88[READ16] 00 00 00 ...
                                       ^(human readable opcode string)

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-11-03 12:18:30 +08:00
zhenwei pi
283e99b322 Introduce helper function scsi_opcode_str
Convert SCSI opcode into string for trouble shooting.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-11-03 12:17:38 +08:00
zhenwei pi
6e8497e53f Declare value-string maps as static
These value-string maps is read-only, it should be constructed once
during loading binary.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-11-03 11:22:38 +08:00
zhenwei pi
a90520da3c Distinguish command timeout
Distinguish command timeout from outqueue or waitqueue. For example,
A command sequence:  ...NOP,READ,WRITE...

NOP OUT command has no dependence on backend media, it is expected to
response soon. Once NOP timeout in libiscsi:
a, the command is already sent to target, the target side does *not*
   response.
b, the command is still pending in libiscsi.

Separate the two cases for trouble shooting.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-11-03 10:54:11 +08:00
Anatoliy Glagolev
35cf3a401c lun_reset cancelling lun tasks only
The existing implementation of iscsi_task_mgmt_lun_reset_async cancels
all tasks in ready-to-send and wait-for-completion queues.
If the ISCSI context has in-flight tasks for a different LUNs or
tasks that are not LUN-specific (such as NOPIN, NOPOUT), those tasks
are not supposed to be affected by the LUN reset.
Also, the tasks for the LUN being reset may have in-flight responses
not affected by a concurrent LUN reset; they have to be handled
accordingly.

This change cancels only the tasks for the LUN being reset if they are
in the ready-to-send queue ('outqueue'). The tasks in the wait-for-
completion queue should be cancelled on LUN reset completion.
For example:

    iscsi_task_mgmt_lun_reset_async(iscsi, lun, lun_reset_cb, ctxt);
    ....
....
void lun_reset_cb(struct iscsi_context * iscsi, int status,
                  void * command_data, void * private_data)
{
    // 'response' field per ISCSI spec rfc7143 section 11.6.1
    uint8_t iscsi_response = *(uint8_t *)command_data;
    if (iscsi_response == 0) {
        // The LUN has been reset. No further replies are expected
        // for in-flight tasks for that LUN. Explicitly cancelling
        // the tasks in wait-for-completion queue.
        for (.. scsi_task-s in flight ..) {
            iscsi_scsi_cancel_task(iscsi, task);
        }
    } ...
}
2023-09-26 15:03:30 -06:00
Anatoliy Glagolev
de05f8ecb4 fixup! checking if task is in outqueue 2023-09-20 17:03:31 -06:00
Anatoliy Glagolev
e4c4799f29 checking if task is in outqueue
Users need to check if a task is queued to send (not sent yet)
before invoking functions such as iscsi_task_mgmt_abort_task_async.
Otherwise, because task management requests are automatically
treated as "immediate", a request to abort a task is sent before
the task itself.

if (iscsi_scsi_is_task_in_outqueue(iscsi_, task_)) {
    iscsi_scsi_cancel_task(iscsi_, task_);
} else {
    iscsi_task_mgmt_abort_task_async(iscsi_, task_, AbortCb, context_);
}
2023-09-20 12:24:26 -06:00
Ronnie Sahlberg
75a46d2b2e Add add a timeout to the event_loop in sync.c
This timeout can be used to cancel async connect attempts to
a remote target.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2023-09-19 16:16:44 +10:00
zhenwei pi
7bf8091013 Add iscsi_set_fd_dup_cb
libiscsi is widely used by poll driven, include libiscsi itself,
QEMU. Using poll works fine after iscsi reconnect, but it does not work
in epoll, because the epoll event has been removed during duplicating
file descriptor in kernel.

Add a new function iscsi_set_fd_dup_cb to set callback, then uplayer
gets notified after duplicating. The following codes reproduce this
issue, and test this patch by compiling flags -DISCSI_FD_DUP_CB.

static void iscsi_epoll_event(struct iscsi_context *iscsi, int epollfd, bool new)
{
	static int epoll_event;
	struct epoll_event ev = { 0 };
	int pevent = iscsi_which_events(iscsi);
	int event = 0;

	if (pevent & POLLIN)
		event |= EPOLLIN;

	if (pevent & POLLOUT)
		event |= EPOLLOUT;

	ev.events = event;
	ev.data.fd = iscsi_get_fd(iscsi);
	if (new)
		epoll_ctl(epollfd, EPOLL_CTL_ADD, ev.data.fd, &ev);
	else if (epoll_event != event) {
		epoll_ctl(epollfd, EPOLL_CTL_MOD, ev.data.fd, &ev);
	}
	epoll_event = event;
}

static void iscsi_tsk_cb(struct iscsi_context *iscsi, int status, void *command_data, void *private_data)
{
	printf("iscsi_tsk_cb status %d\n", status);
	exit(0);
}

static void iscsi_fd_dup_cb(struct iscsi_context *iscsi, void *opaque)
{
	int epollfd = *(int *)opaque;

	iscsi_epoll_event(iscsi, epollfd, true);
}

static void iscsi_on_pollout(struct iscsi_context *iscsi, struct iscsi_url *iscsi_url, int epollfd)
{
	static struct scsi_task *tsk = NULL;

	iscsi_service(iscsi, POLLOUT);
	if (!tsk) {
		tsk = iscsi_readcapacity16_task(iscsi, iscsi_url->lun, iscsi_tsk_cb, NULL);
		assert(tsk);
	}
}

int main(int argc, char *argv[])
{
	struct iscsi_context *iscsi;
	struct iscsi_url *iscsi_url;
	struct epoll_event ev, revent;
	int epollfd;

	iscsi = iscsi_create_context("dummy");
	assert(iscsi);

	iscsi_url = iscsi_parse_full_url(iscsi, argv[1]);
	assert(iscsi_url);

	iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL);
	iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
	assert(!iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun));

	epollfd = epoll_create1(0);
	iscsi_epoll_event(iscsi, epollfd, true);

	iscsi_set_fd_dup_cb(iscsi, iscsi_fd_dup_cb, &epollfd);
	iscsi_reconnect(iscsi);

	while (epoll_wait(epollfd, &revent, 1, 100) >= 0) {
		if (revent.events & EPOLLIN)
			iscsi_service(iscsi, POLLIN);

		if (revent.events & EPOLLOUT)
			iscsi_on_pollout(iscsi, iscsi_url, epollfd);

		iscsi_epoll_event(iscsi, epollfd, false);
	}

	return 0;
}

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-06-06 13:29:52 +08:00
Li kunyu
22f7b26567 iser: Remove unnecessary 'return;'
Signed-off-by: Li kunyu <kunyu@nfschina.com>
2023-02-08 09:01:15 -08:00
Li kunyu
82846758e1 pdu: Remove temporary variables from functions
Signed-off-by: Li kunyu <kunyu@nfschina.com>
2023-01-07 13:26:54 -08:00
Li kunyu
c0fdc4655a connect: Add check after malloc allocation 2022-12-14 14:47:05 +08:00
Bart Van Assche
51df0d0512 lib/scsi-lowlevel: Make the REPORT LUNS unmarshalling code more flexible
Instead of rejecting REPORT LUNS responses if the data buffer size exceeds
the LUN list size, truncate the data buffer.

Fixes: https://github.com/sahlberg/libiscsi/issues/385
2022-09-09 15:02:44 -07:00
sallyjunjun
1017435ca9 Fix segmentation fault problem.
When execute iscsi_task_mgmt_lun_reset_async function,
pdus are already removed from waitpdu list. In iscsi_service
function, this will call iscsi_process_pdu and release
pdu from waitpdu again, which cause segmentation fault.

Whether waitpud list is NULL should be checked here to avoid
the problem.

Signed-off-by: geruijun <geruijun@huawei.com>
2022-06-14 20:49:09 -07:00
geruijun
8ef5d8243b Check return value of scsi_malloc in order to
avoid dereferencing NULL return value.

Signed-off-by: geruijun <geruijun@huawei.com>
2022-06-10 19:44:16 -07:00
Sergey Samoylenko
fbfa387d9a Retain the immediate data param during login processing
If a test sets the use_immediate_data parameter to ISCSI_IMMEDIATE_DATA_NO
for the iSCSI context, then the test expects that a data associated with
the Write command to be sent in a separate PDU.

But if for execute the command it is necessary to login on a target then
the use_immediate_data was previously set, will be rewrite during
the processing of the Login Response packet.

This happen during the iSCSI.iSCSIdatasn.iSCSIDataSnInvalid
(test_iscsi_datasn_invalid.c) test:

    --> iSCSI 114 SCSI: Write(10) LUN: 0x01 (LBA: 0x00000064, Len: 1)
    <-- iSCSI 114 Ready To Transfer
    --> iSCSI 578 SCSI: Data Out LUN: 0x01 (Write(10) Request Data)

    --> iSCSI 550 Login Command
Here we lose use_immediate_data value for iSCSI session.
    <-- iSCSI 426 Login Response (Success)
    --> iSCSI 114 SCSI: Test Unit Ready LUN: 0x01
    <-- iSCSI 114 SCSI: Response LUN: 0x01 (Test Unit Ready) (Good)
And this Write command includes payload into iSCSI PDU packet, but should not do it.
    --> iSCSI 578 SCSI: Write(10) LUN: 0x01 (LBA: 0x00000064, Len: 1)SCSI: Data Out LUN: 0x01 (Write(10) Request Data)
    <-- iSCSI 114 SCSI: Response LUN: 0x01 (Write(10)) (Good)

    --> iSCSI 114 SCSI: Write(10) LUN: 0x01 (LBA: 0x00000064, Len: 2)
    <-- iSCSI 114 Ready To Transfer
    --> iSCSI 578 SCSI: Data Out LUN: 0x01 (Write(10) Request Data)
    --> iSCSI 626 SCSI: Data Out LUN: 0x01 (Write(10) Request Data)

Signed-off-by: Sergey Samoylenko <s.samoylenko@yadro.com>
2022-03-21 06:58:00 -07:00
Raphael Norwitz
2674070fb8 iscsi-command: Fix leak in iscsi_send_data_out
In iscsi_send_data_out() a PDU is allocated, but there is no error
handling logic to free it if the PDU cannot be queued.
iscsi_allocate_pdu() may allocate memory for the PDU. This memory may be
leaked if iscsi_queue_pdu() fails since there is no call to free it.

Orignally there was a free() call but it was removed as a part of a
cleanup adding checks for NULL pdu callbacks.

Fixes: 423b82efa4 ("pdu: check callback for NULL everywhere")
Signed-off-by: Raphael Norwitz <raphael.norwitz@nutanix.com>
2022-03-03 12:58:08 -08:00
John Levon
30dd7c6429 add iscsi_force_reconnect()
If a connection attempt is hung, then iscsi_reconnect() won't do anything. This
makes sense if we'd just re-try to connect to the same target, but if (for
example) login redirect might point us to a different, healthy, target, it
should be possible to restart the full connection process on request.

Signed-off-by: John Levon <john.levon@nutanix.com>
2022-01-17 13:14:49 -08:00
Bart Van Assche
aa214feaf3 iser: Fix a compiler warning triggered by the container_of() definition
This patch fixes the following compiler warning:

iser.c:338:14: error: cast from 'uint8_t *' (aka 'unsigned char *') to 'struct iser_pdu *' increases required alignment from 1 to 8 [-Werror,-Wcast-align]
                iser_pdu = container_of(pdu, struct iser_pdu, iscsi_pdu);
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2021-09-05 20:00:02 -07:00
Bart Van Assche
696ac1e948 Merge pull request #353 from Ser01x/master
Before running the SCSI.ReceiveCopyResults.CopyStatus test we should check that it is supported
2021-07-26 20:34:19 -07:00
Ser01x
e8c15866fa Fixed several code style problems 2021-07-26 09:24:54 +00:00