Commit Graph

2159 Commits

Author SHA1 Message Date
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
zhenwei pi 5233a80de4 Add /utils/iscsi-pr into .gitignore
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-05-31 06:49:59 -07:00
zhenwei pi 2b9d5bdbad iscsi-perf: Add -h/--help
Add -h/--help to show usage without "Unrecognized option".

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2023-05-31 06:49:59 -07: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
Ronnie Sahlberg 2f30d3acee Merge pull request #389 from likunyur/likunyu
aros_compat: Add check after malloc allocation
2022-12-14 20:15:19 +10:00
Li kunyu c0fdc4655a connect: Add check after malloc allocation 2022-12-14 14:47:05 +08:00
Li kunyu 583211a52f aros_compat: Add check after malloc allocation 2022-12-14 11:33:19 +08:00
Bart Van Assche be9e2d70cb ci/build.sh: Enable parallel compilation 2022-11-12 18:16:17 -08:00
Bart Van Assche c23545c604 ci: Fix the Appveyor Linux and Windows builds 2022-11-12 18:13:26 -08:00
Bart Van Assche 3fe06e8563 ci: Add ci/install.bat
Move the MinGW package installation commands from ci/build.sh into
ci/install.sh.
2022-11-12 18:12:45 -08:00
Ronnie Sahlberg 505b2a0ab4 Merge pull request #387 from lgtm-migrator/codeql
Add CodeQL workflow for GitHub code scanning
2022-11-09 07:59:43 +10:00
LGTM Migrator 2229f42246 Add CodeQL workflow for GitHub code scanning 2022-11-08 13:08:20 +00:00
Li kunyu 6b520bee52 test-tool: remove a redundant semicolon
Signed-off-by: Li kunyu <kunyu@nfschina.com>
2022-10-08 20:34:36 -07: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
sallyjunjun 045c2387e7 fix iscsi-ls parameter parse
If invalid option is input with iscsi-ls,such as "iscsi-ls -a iscsi://", the command just stuck here and do not print useful information for the user to correct.
Fix this problem with getopt_long.
2022-06-07 19:19:39 -07:00
sallyjunjun b087a09a0b iscsi-swp: handle setting of debug_level correctly
According to the man page and help info, --debug=integer can specify the
debug_level, while it would report following error:

iscsi-swp --debug=1
iscsi-swp: option '--debug' doesn't allow an argument
2022-05-31 15:12:49 -07:00
sallyjunjun 8592dc5883 Update iscsi-dd.c
add check after malloc to avoid referencing an illegal pointer
2022-05-31 15:12:49 -07:00
Ronnie Sahlberg babb0c3d51 Merge pull request #375 from wenchao-hao/master
iscsi-inq: handle setting of debug_level correctly
2022-04-07 17:23:43 +10:00
Wenchao Hao 5e63853230 iscsi-inq: handle setting of debug_level correctly
According to the man page and help info, --debug=integer can specify the
debug_level, while it would report following error:

iscsi-inq --debug=2
iscsi-inq: option '--debug' doesn't allow an argument

It's because the iscsi-inq code did not handle this parameters
correctly. So here we just correct it.

Signed-off-by: Wenchao Hao <haowenchao@huawei.com>
2022-04-06 10:28:23 +08: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
zhenwei pi fb9c3f93ed iscsi-pr: add persistent reservation tool
iscsi-pr uses libiscsi to connect target and issues PR command.
In most cases, iscsi-pr use the same parameters as sg_persist(from sg3-utils).

For example, use iscsi-pr to dump keys by command:
  ~# ./utils/iscsi-pr --read-keys iscsi://192.168.122.44/iqn.2003-01.org.linux-iscsi.bytedance.x8664:sn.a6b7bff3d509/0 -i iqn.2005-03.org.open-iscsi:12345678
  PR generation=0x20, 19 registered reservation keys follow:
    0xabcd1234
    0xabcd
    0xabcd
    0xabc

A few command is not implemented in this patch, add them if necessary
in future.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-02-20 21:17:40 -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
John Levon 314aa26576 add libiscsi.syms to .gitignore
This file is generated; add it to .gitignore.

Signed-off-by: John Levon <john.levon@nutanix.com>
2022-01-17 13:14:38 -08:00
Li Feng 97d2f681d7 iscsi-support: fix memory leak
If doesn't support the report_supported_opcodes, the task will not be
freed in REPORT_SUPPORTED_OPCODES.

Change-Id: I1e251eec518721fb35e51013621aa61865d4b46b
Signed-off-by: Li Feng <fengli@smartx.com>
2021-09-30 19:54:08 -07:00
Bart Van Assche d592e2e01e Merge pull request #364 from tianrenz2/fix-dist-make
Add some files into the dist tarball necessary for rpm generation
2021-09-12 15:06:42 -07:00
Tianren Zhang 2b8905b4f9 add README.md to dist tarball
README is required in rpm spec, but not included into the dist
tarball
2021-09-12 03:44:23 -04:00
Tianren Zhang e31fe70fb5 add libiscsi.syms.in to dist tarball
libiscsi.syms.in is required for 'make libiscsi.syms'
2021-09-12 03:30:51 -04:00
Bart Van Assche b781b21d50 test-tool, xcopy: Fix target descriptor handling
Fail the XCOPY tests if no appropriate target descriptor is available
instead of causing memory corruption or truncating the descriptor.
2021-09-05 20:00:02 -07: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 5cbbc95c20 configure: Remove -Wno-strict-aliasing
Using -Wno-strict-aliasing is almost always wrong. If any code would be
introduced in the future that violates the strict aliasing rules, please
use -fno-strict-aliasing instead.
2021-09-05 20:00:02 -07:00
Bart Van Assche 3f9735b3a4 slist: Clean up the slist.h header file
Fix indentation, align backslashes, surround multiline macros with
do { } while (0) and remove the unused ISCSI_LIST_LENGTH() macro.
2021-09-05 20:00:02 -07:00
Li Feng 6b93325b39 slist: Make this header file compatible with C++
C++ requires explicit conversions from a void to a non-void pointer.

Signed-off-by: Li Feng <fengli@smartx.com>
[ bvanassche: edited commit message, removed casts and changed the declaration
  type ]
2021-09-05 20:00:02 -07:00
Bart Van Assche c412ae0e1d scsi-lowlevel.h: Include <assert.h>
Checking whether static_assert() is defined must happen after <assert.h> has
been included. Hence include <assert.h>.
2021-09-05 20:00:02 -07:00
Simon Rowe 35dcf89599 examples/iscsi-dd: use stderr for all error text
Signed-off-by: Simon Rowe <simon.rowe@nutanix.com>
2021-09-05 20:00:02 -07:00