2246 Commits

Author SHA1 Message Date
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
Ronnie Sahlberg
9f7f2c9a8b Merge pull request #419 from bmeagherix/add_data_digest
Add support for Data Digest
2024-05-30 07:46:29 +10:00
Brian Meagher
882bcad53a Add support for Data Digest 2024-05-04 19:34:06 -07:00
Ronnie Sahlberg
ec5d33da63 Merge pull request #416 from folkertvanheusden/timestamp-in-logging
A possible 'fix' for issue #415
2024-04-10 03:38:07 +10:00
Ronnie Sahlberg
90e1916ee0 Merge pull request #418 from bonzini/without-libgcrypt
allow choosing between libgcrypt, gnutls or embedded MD5
2024-04-10 03:36:55 +10: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
Paolo Bonzini
380c16a82b configure: add --with-libgcrypt option
libgcrypt is a relatively large dependency that is used only for
the sake of computing MD5 in the CHAP authentication protocol.

Allow distributions to disable it forcibly and rely on the
embedded MD5 implementation.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2024-04-09 16:01:42 +02:00
folkert van heusden
5fc2dcb88f A possible 'fix' for https://github.com/sahlberg/libiscsi/issues/415
This patch adds a timestamp before each logged line. That could help
correlating a logging to a network-trace. Because of offsets in time
between the tracer and the test and the DUT, this does not always help.
2024-02-14 00:44:26 +01:00
Ronnie Sahlberg
fef688aa99 Merge pull request #414 from mjt0k/remove-mentions-of-ld_iscsi
COPYING: stop mentioning ld_scsi
2024-02-06 09:51:38 +10:00
Michael Tokarev
6e0206c135 spelling fixes: eventhough, shoudl, asyncronous, maxium, implicity
These changes are carried in debian for a long time, some since 2016.
The last one (implicity) is new in 1.20.0.

Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2024-02-05 10:22:23 -08:00
Michael Tokarev
0be2be2c31 COPYING: stop mentioning ld_scsi
Fixes: e6bcdf5fdb "drop the LD_PRELOAD tool"
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2024-02-05 21:19:00 +03: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
Ronnie Sahlberg
bfce73325d Merge pull request #411 from folkertvanheusden/task-status
print task-status in hex
2024-01-31 12:57:00 +10:00
folkert van heusden
a51eafa90d Previously, 'task->status' was printed as decimal. In iscsi.h, the
values that are put into it are defined as hexadecimal. This is a bit
confusing (grepping through the code for 251658241 did not result in
anything while the hex variant f000001 resulted in SCSI_STATUS_ERROR).
2024-01-30 19:47:28 +01:00
folkert van heusden
abedc1848c Fix for https://github.com/sahlberg/libiscsi/issues/409
'ms->pages' was not checked for being NULL. This can happen when a
target does not return any pages.
2024-01-30 06:39:08 -08:00
zeroway
9ec12158b0 Manually set task to null after free to avoid double free issue 2024-01-19 17:41:54 +08:00
Ronnie Sahlberg
0fac8df48c Merge pull request #406 from pizhenwei/improve-dump-header
Improve dump header
2023-12-01 19:49:13 +10: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
Ronnie Sahlberg
fdd00ee801 Merge pull request #404 from pizhenwei/drain-dataout-pdu-on-timeout
Drain DATAOUT PDU on timeout
2023-11-22 14:43:22 +10: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
Ronnie Sahlberg
03e9ddc0a6 Merge pull request #403 from pizhenwei/fix-socket-error-log
Log necessary message on internal reconnect
2023-11-14 02:50:59 -05: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
Ronnie Sahlberg
08f79ebf1d Merge pull request #402 from pizhenwei/debug-info-on-timeout
Debug info on timeout
2023-11-03 14:36:51 +10: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
Ronnie Sahlberg
c4a180fc4e Merge pull request #401 from pizhenwei/doc-iscsi-md5sum
doc: Add iscsi-md5sum
2023-10-25 13:53:59 +10:00
zhenwei pi
17fc7159b6 doc: Add iscsi-md5sum
Add document for utils/iscsi-md5sum.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-10-25 11:31:55 +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
Ronnie Sahlberg
d0cc81347f Merge pull request #399 from anatoliy-glagolev/master
fixup! checking if task is in outqueue
2023-09-21 09:25:02 +10:00
Anatoliy Glagolev
de05f8ecb4 fixup! checking if task is in outqueue 2023-09-20 17:03:31 -06:00
Ronnie Sahlberg
528a3861e7 Merge pull request #398 from anatoliy-glagolev/master
checking if task is in outqueue
2023-09-21 05:56:17 +10: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
Ronnie Sahlberg
a2f9ee5d78 Merge pull request #397 from pizhenwei/iscsi-discard-fix
iscsi-discard: adapt block limits
2023-08-14 12:57:23 +10:00
zhenwei pi
2794079fef iscsi-discard: adapt block limits
The blocks may exceed the block limits offered by the target,
query unmap/write zero max blocks by inquiry
SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS command, than use a loop to do the
job.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-08-08 15:07:23 +08:00
Ronnie Sahlberg
7577ec589c Merge pull request #396 from pizhenwei/iscsi-md5sum
Iscsi md5sum
2023-08-04 19:55:11 +10:00
zhenwei pi
29e46fe290 gitignore: Add utils/iscsi-md5sum
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-07-31 15:39:53 +08:00
zhenwei pi
45b274aec8 iscsi-md5sum: Add new tool
The new tool 'iscsi-md5sum' is used to calculate MD5 value of an iSCSI
target. This help users to verify data at range [LBA, Length).

For example, double-write on a RAID1 of 2 iSCSI targets, a daemon
process runs iscsi-md5sum to check data periodically.

Originally, we have to use several steps to achieve:
1, use iscsiadm to login.(root privilege required)
2, use dd(dd if=/dev/sdX of=/TMPPATH bs=4k count=LENGTH skip=LBA)
   to dump data. (root privilege required, additional disk space required)
3, use md5sum to calculate MD5 value
4, remove data.

Instead, a single command iscsi-md5sum without root privilege is enough.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-07-31 15:29:11 +08:00
Ronnie Sahlberg
9e9a3e8198 Merge pull request #395 from pizhenwei/iscsi-discard
Iscsi discard
2023-07-22 17:06:00 +10:00
zhenwei pi
ae6909fd31 gitignore: add utils/iscsi-discard
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-07-21 18:19:51 +08:00
zhenwei pi
d631b08c19 iscsi-discard: Add new tool
The new tool 'iscsi-discard' is used to excute unmap or write zeros
on an ISCSI target. The parameters look like the command 'blkdiscard'
(from util-linux).

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-07-21 18:15:45 +08:00
zhenwei pi
6e2c677553 utils: use strtol instead of atoi
HEX format is friendly to iSCSI utils, for example:
./iscsi-inq -e 0x1 -c 0xb1 iscsi://...

atoi supports decimal only, this example does not work.
Use strtol(nptr, NULL, 0) to auto-detect format, then this works fine.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-07-08 07:44:16 -07:00
Ronnie Sahlberg
9ca47316f4 Merge pull request #393 from pizhenwei/on-fd-dup
Add iscsi_set_fd_dup_cb
2023-06-06 16:11:30 +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