846 Commits

Author SHA1 Message Date
Brad Smith
9ab0fa8793 Fix linking of libiscsi to libpthread
libiscsi utilizes a bunch of POSIX threads functions but does
not link against libpthread as it should nor the pkg-config file
properly listing libpthread as a static link time dependency.
2026-03-22 18:57:44 -04:00
Brad Smith
b2b05b6fbe Add thread id retrieval for FreeBSD/DragonFly, OpenBSD and NetBSD 2026-03-22 00:12:47 -04:00
Duncan Overbruck
0873c22d55 use size_t printf format for iovector->offset 2026-02-04 20:07:24 +01:00
Peter Lieven
0ab21f4643 fix: do only query unit serial for non-removable direct access devices
other device types might not implement evpd page 0x80 or change their serial.

Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-09-22 16:01:46 +00:00
Peter Lieven
ea141f8475 fix: only try to read unit serial if lun != -1
iscsi-ls --show-luns connects to lun -1 for which iscsi inquiry fails on some storage.

Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-09-22 14:38:38 +00:00
Peter Lieven
247133c9e2 feat: allow targetname redirect
this patch adds an non-RFC extenstion to the redirect login response which allows
a target to temporarily redirect not only to a different target address but also to
a different targetname.

This is needed to allow scenarious in active/active storage clusters where each
node had its own targetname, but maps the same volumes behind equal LUN ids.

For this non-RFC behaviour the environment variable LIBISCSI_ALLLOW_TARGETNAME_REDIRECT
has to be set.

Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-09-11 19:51:00 +00:00
Peter Lieven
2b2529d196 fix: check scsi_datain_unmarshall for NULL
Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-09-11 19:50:45 +00:00
Peter Lieven
8c4e2ad46b feat: read and validate unit serial number after login
this patch adds a read of VPD page 0x80 (unit serial number) after successful login.

The serial is then validated on secutive reconnects to avoid the accidental mismatch
of LUN ids if some kind of remapping appears between loss of connection and a later
reconnect.

An additional url parameter force_usn is added to enforce the usn right from the beginning.
If not set via url or the new iscsi_set_unit_serial_number function the usn is learned
at the first successful login.

Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-09-11 15:15:42 +00:00
sahlberg
46631b9083 New version 1.20.3
When queueing the first PDU to an idle socket, immediately try to write
it to the socket instead of waiting for the event-loop to
timeout/notice there is work to do.

Various other small bugfixes.

Signed-off-by: sahlberg <sahlberg@r10-0-1.rocky10.sahlberg>
2025-07-15 19:27:12 +10:00
sahlberg
a8c3920ab8 Write the PDU directly to the socket if the socket is idle.
Signed-off-by: sahlberg <sahlberg@r10-0-1.rocky10.sahlberg>
2025-07-15 19:26:46 +10:00
Anatoliy Glagolev
89713775dd fall-through in maintenancein unmarshal switch
Fixing a potential fall-through in switch in data unmarshal function
for maintenance-in commands. The fall-through never happens for valid
commands, but the build tools may complain about it.
2025-07-08 16:11:26 -06:00
Anatoliy Glagolev
19d05ab7a7 RTPG support
Implementing support of the Report Target Port Groups command.

Tested on Ubuntu against Pure Storage Flash Array
using designated unit tests and new iscsi-rtpg utility

./iscsi-rtpg  -i iqn.2005-03.org.open-iscsi:6feb2db21ea iscsi://192.168.1.12/iqn.2010-06.com.purestorage:flasharray.4e8d52d82e4b2c0f/1
RTPG retrieved 2 groups
Group 0x0000: preferred 0, format 0x00, ALUA state ACTIVE-OPTIMIZED,flags 0x8f, status code 0x02, port count 65
Ports: [ 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xa 0xb 0xc 0xd 0xe 0xf 0x10 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4a 0x4b 0x4c
0x4d 0x4e 0x4f 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5a 0x5b 0x5c 0x5d 0x5e 0x5f 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67
0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71]
Group 0x0001: preferred 0, format 0x00, ALUA state ACTIVE-OPTIMIZED,flags 0x8f, status code 0x02, port count 65
Ports: [ 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 0x1e 0x1f 0x20 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7a
0x7b 0x7c 0x7d 0x7e 0x7f 0x80 0x81 0x82 0x83 0x84 0x85 0x86 0x87 0x88 0x89 0x8a 0x8b 0x8c 0x8d 0x8e 0x8f 0x90 0x91 0x92 0x93 0x94 0x95
0x96 0x97 0x98 0x99 0x9a 0x9b 0x9c 0x9d 0x9e 0x9f 0xa0 0xa1 0xa2]
2025-07-03 17:20:52 -06:00
Ronnie Sahlberg
914eb5d578 Fix compiler warning/error from the previous commit
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-06-18 10:06:54 +10:00
lishiao
8ebfb20c55 Optimize the judgment conditions 2025-06-17 12:24:40 +08:00
lishiao
a81eacc0b1 iscsi: compute Data Digest for out_data segments 2025-06-17 11:57:38 +08:00
lishiao144
cbba36e150 fix ExpStatSN handling for Data-Out PDUs 2025-06-12 18:26:19 +08:00
lishiao144
484adf5017 Fix length check to exclude padding when comparing against initiator_max_recv_data_segment_length 2025-06-12 16:41:53 +08:00
Ronnie Sahlberg
2803276f60 Merge remote-tracking branch 'refs/remotes/origin/master' 2025-05-30 10:53:25 +10:00
lishiao144
de86745f14 Update login.c 2025-05-29 14:00:53 +08:00
lishiao144
a57f51708c Incorrect handling of CHAP_C encoding during CHAP_R computation 2025-05-29 11:51:50 +08:00
lishiao144
7254f954ff zero-initialize target_chap_c to avoid relying on stack memory 2025-05-28 15:59:27 +08:00
Ronnie Sahlberg
bd439878c0 New version 1.20.2
- Fix for Apple as their pthreads do not provide spinlock support.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-05-05 16:20:09 +10:00
Ronnie Sahlberg
8cd2381476 New version 1.20.1
Add sha1/sha256/sha3 authentication
Add multithreading support

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-05-03 10:02:42 +10:00
Ronnie Sahlberg
eb19863f77 Fix race between queueing the pdu and update the task data
After we have called iscsi_queue_pdu from iscsi_scsi_command_async
the pdu might have already completed if we are using multithreading
so we should not dereference pdu at that point.

Move the assignment of task->cmdsn and task->itt we need for
task management into iscsi_pdu_set_cmdsn instead.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 11:55:47 +10:00
Ronnie Sahlberg
d5e3bf6175 Merge branch 'tst' 2025-04-26 11:30:58 +10:00
Ronnie Sahlberg
edd7d9b843 Remove the small allocations
This is not compatible with multithreading and would require a complete
re-design.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
8047421868 Protect some variables in iscsi_context by the spinlock
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
91cc1e4197 Protect outqueue and waitpdu with a spinlock
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
4593746363 Add spinlock primitives
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
Ronnie Sahlberg
3fc5d2996b iscsi_queue_pdu() can never fail, make it void
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2025-04-26 08:56:16 +10:00
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
Peter Lieven
eb0853e36e fix: use correct maximum length for TargetName and InitiatorName
Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-03-26 13:28:23 +00:00
Peter Lieven
696c946a9b feat(socket): log resolved ip addresses if portal is a hostname
Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-03-25 22:06:21 +00:00
Peter Lieven
98f0f2f7f1 fix(socket): restore logging of local ip and port
commit a92b413 removed the logging of local ip and port.
For debugging puposes these information can be important.
Restore functionality by using the new thread safe replacement
for inet_ntoa called inet_ntop.

Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-03-25 22:06:19 +00:00
Peter Lieven
7f0a8597d6 feat(logging): include LUN into log messages
a process might have a connection to different LUNs on the same target.
To be able to distinguish the different connections add the LUN id to the target
to log messages.

Signed-off-by: Peter Lieven <pl@dlhnet.de>
2025-03-25 22:06:15 +00: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