Commit Graph

137 Commits

Author SHA1 Message Date
Sitsofe Wheeler
6ea30f9fb2 socket: Simplify SOL_TCP check
Rather than checking for the names of OSes that don't implement it
just check whether the define is available directly.
2016-01-30 08:12:28 +00:00
Sitsofe Wheeler
4714e12e8f socket: Use alternatives if MSG_NOSIGNAL is unavailable
Only Linux has MSG_NOSIGNAL as a socket flag (see
http://stackoverflow.com/questions/108183/how-to-prevent-sigpipes-or-handle-them-properly
) which makes compilation fail on other OSes. The BSDs and OS X have
SO_NOSIGPIPE which implements the same thing but others like
Solaris don't even have that (see
http://stackoverflow.com/questions/2205455/detecting-broken-pipe-in-solaris-send-call
)...

Work around this by checking using MSG_NOSIGNAL or SO_NOSIGPIPE if
available otherwise don't set anything at all so we at least continue
to compile.
2016-01-30 07:50:50 +00:00
Ronnie Sahlberg
70391b6449 Typo
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-12-15 19:09:08 -08:00
Ronnie Sahlberg
74fc637eeb Use MSG_NOSIGNAL with send()
If the server closes the TCP connection before we call send() in
iscsi_write_to_socket() we will get SIGPIPE unless we pass this
argument to send.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-12-06 12:12:49 -08:00
Ronnie Sahlberg
0630aa5ef0 Timeouts: Fix bugs in scsi task timeout code
Add a test to verify the pdu timeout handling.
Fix numerous bugs in the timeout handling. It was really broken.

Add test for non-SCSI task PDUs too and verify they works.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-05-24 14:34:06 -07:00
Peter Lieven
724f44c31b socket: change the order of POLLIN and POLLOUT in iscsi_service
the original idea of writing to the socket before reading was to put data
on the wire as early as possible and avoid potential latency increase due
to long taking callbacks invoked in iscsi_read_from_socket.

Benchmarking with libiscsi userland tools have shown that changing the
order increases throughput by about 5%.
The reason is that during POLLIN we might receive an updated MaxCmdSN
which might us allow to send new data out during POLLOUT.

For Qemu the change doesn't matter since POLLIN and POLLOUT are processed
independently.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-05-05 13:57:10 +02:00
Peter Lieven
1f6d901324 socket: fix disconnected message for pending logouts
the check was not quite correct. iscsi->pending_reconnect can be true but
we are still connected (and thus can disconnect). This is the case when
we receive an async logout request.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-04-20 14:26:11 +02:00
Ronnie Sahlberg
ec4eb766c7 reconnect: improve error message is reconnect is disabled
if reconnect is disabled then a failure in iscsi_service_reconnect_if_loggedin
can leak back to the application by iscsi_service returning an error
but the error string is not being updated.
Thus the applucation will print the last prevous error message that was set.

Change iscsi_service_reconnect_if_loggedin to print a less confusing
error message.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-04-16 08:12:49 -07:00
Peter Lieven
812d55d343 socket: do not show misleading disconnect message if we are not actually logged in
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-04-16 11:58:28 +02:00
Peter Lieven
913b2ab708 reconnect: avoid deadlock if socker error is received during reconnect
If we are reconnecting and the connection is already established we
end up looping forever if there is a socket error before we finish
the login process. Fix this by invoking the reconnect callback if
there is an error and no new reconnect is scheduled.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-04-13 12:11:03 +02:00
Peter Lieven
a8b33cefac drop iscsi->is_reconnecting
iscsi->is_reconnecting != 0 is equal to iscsi->old_iscsi != NULL.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-04-13 10:13:36 +02:00
Peter Lieven
a3e5c4d3c5 socket: allow iscsi_which_events to return 0
this is meant as a hint that there can be no event at the moment
and in this special case iscsi_service will return immediately.
We use this at the moment in the case of a wait for a reconnect
in which case we would enter a busy loop waiting for that
reconnect. The 0 means do not poll for the client application.
Do not poll, just wait a little (a few hundred ms) and call iscsi_which_events
again. The wait should be timer driven or at least non busy looping,
of course.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-04-02 08:34:52 +02:00
Peter Lieven
b7cb8fbf5a iscsi_service: dont return if a reconnect is pending but not yet reconnecting
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-04-02 08:31:53 +02:00
Peter Lieven
b152d26eb9 connect: make the reconnect async
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-31 16:21:00 +02:00
Peter Lieven
153e6ba07c pdu: set pdu->cmdsn when the pdu structure is updated
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-30 12:32:03 +02:00
Peter Lieven
37f908bb8a socket: set expstatsn when we put the PDU on the wire
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-30 08:44:50 +02:00
Peter Lieven
47e2b8918e socket: Data-Out PDUs may carry a CmdSN < ExpCmdSN
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-27 22:21:51 +01:00
Peter Lieven
cb6667a5dc drop ISCSI_PDU_URGENT_DELIVERY
its not needed anymore

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-26 17:00:20 +01:00
Peter Lieven
2a57cea180 socket: queue immediate PDUs at front of outqueue
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-26 16:51:18 +01:00
Peter Lieven
b76c8a703e socket: honour ISCSI_PDU_IMMEDIATE in iscsi_which_events
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-26 16:45:03 +01:00
Peter Lieven
33c301335b socket: check if expcmdsn is not greater than pdus cmdsn
this is just another sanity check.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-26 12:27:48 +01:00
Peter Lieven
21cb9a9014 debug: add NOP Out and stalled outqueue debug messages
Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-26 12:18:29 +01:00
Peter Lieven
4ce740f737 socket: fix bounds check in iscsi_iovector_readv_writev
if we received too little iovectors niov++ may also
make iov2 point to uninitialized memory.

Signed-off-by: Peter Lieven <pl@kamp.de>
2015-03-26 11:42:53 +01:00
Ronnie Sahlberg
2f94f16d02 iovectors: don't reset nalloc when resetting an iovector
Don't reset nalloc when resetting an iovector during reconnect.
Resetting offset/consumed should be sufficient.

Also, don't reset the iovectors when we detect an error condition in
iscsi_iovector_readv_writev. If there is a bug feeding an invalid
pos into this function, where pos suddently points before the amount
of data we have already read/written, then singlan this as an error
and return -EINVAL.

Previosly we did not reset the iovectors correctly when we re-queued
PDUs after a reconnect and thus relied on the iovectors being automatically
reset if/when we detected this type of error in readv_writev.
Now we do reset the iovectors properly so we do nt need to atuo reset them
here anymore and we can change this check to test for and abort the transfer
if an error is detected.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-03-22 08:20:29 -07:00
Ronnie Sahlberg
7dc0ea200d socket.c: allow re-using a context after disconnect
From Adam Endrodi <adam.endrodi@nsn.com>
Fix a bug and clear is_corked during socket disconnect so that we
can re-use the context for a new connection.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2015-01-09 15:13:58 -08:00
Mike Cui
2284a8763c set_tcp_* functions should be static since they are not used anywhere else.
set_tcp_sockopt in particular conflicts with libnfs's function of the same
name and prevents a a program from statically linking against both libnfs and
libiscsi.

Similar fix should also go into libnfs.
2015-01-05 09:24:01 -08:00
Bart Van Assche
161547d5b6 lib/socket.c: Fix a use-after-free
This issue was introduced via patch "pdu: introduce ISCSI_PDU_CORK_WHEN_SENT"
on June 13, 2014 (commit 99585b6996).

Valgrind reported this use-after-free as follows:

Invalid read of size 4
   at 0x5267606: iscsi_write_to_socket (socket.c:721)
   by 0x5267A72: iscsi_service (socket.c:823)
   by 0x526827C: event_loop (sync.c:67)
   by 0x52698A4: iscsi_compareandwrite_sync (sync.c:823)
   by 0x408111: compareandwrite (iscsi-support.c:1752)
   by 0x4139E2: test_compareandwrite_simple (test_compareandwrite_simple.c:88)
   by 0x503D260: ??? (in /usr/lib64/libcunit.so.1.0.1)
   by 0x503D578: ??? (in /usr/lib64/libcunit.so.1.0.1)
   by 0x503D8B5: CU_run_all_tests (in /usr/lib64/libcunit.so.1.0.1)
   by 0x4046C6: main (iscsi-test-cu.c:1241)
 Address 0x639f258 is 8 bytes inside a block of size 256 free'd
   at 0x4C291E7: free (vg_replace_malloc.c:473)
   by 0x525321B: iscsi_free (init.c:68)
   by 0x52532F0: iscsi_sfree (init.c:110)
   by 0x5257AD9: iscsi_free_pdu (pdu.c:179)
   by 0x5267601: iscsi_write_to_socket (socket.c:719)
   by 0x5267A72: iscsi_service (socket.c:823)
   by 0x526827C: event_loop (sync.c:67)
   by 0x52698A4: iscsi_compareandwrite_sync (sync.c:823)
   by 0x408111: compareandwrite (iscsi-support.c:1752)
   by 0x4139E2: test_compareandwrite_simple (test_compareandwrite_simple.c:88)
   by 0x503D260: ??? (in /usr/lib64/libcunit.so.1.0.1)
   by 0x503D578: ??? (in /usr/lib64/libcunit.so.1.0.1)

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Cc: Peter Lieven <pl@kamp.de>
2014-09-18 11:57:05 -07:00
Ronnie Sahlberg
f0ebbdc203 Merge pull request #128 from plieven/revert_iovector
socket: rename max_read parameter to count in iscsi_iovector_readv_write...
2014-07-14 06:24:05 -07:00
Peter Lieven
53e38dff7d socket: rename max_read parameter to count in iscsi_iovector_readv_writev
Signed-off-by: Peter Lieven <pl@kamp.de>
2014-07-14 15:05:19 +02:00
Ronnie Sahlberg
a0d09ca9de Merge pull request #127 from plieven/revert_iovector
Revert "Rewrite and simplify iscsi_iovector_readv_writev"
2014-07-14 05:56:12 -07:00
Peter Lieven
59e4a07129 Revert "Rewrite and simplify iscsi_iovector_readv_writev"
Since writing headers and payload in a single iov has never been
implementend and after thinking about it several times seems to
be very hairy I would like to revert this change since
the original implementation is in O(1) while the changed one
is in O(n). This results in a complexity of O(n^2) instead of
O(n) for the whole send operation.

This reverts commit 06eab264f6.
2014-07-14 08:05:32 +02:00
Peter Lieven
4b3f4b5822 iscsi_which_events: do not set POLLOUT if context is corked
if the socket is corked we otherwise set POLLOUT and then
do not sent. Depending on the event loop implementation this
can result in a busy wait.

Signed-off-by: Peter Lieven <pl@kamp.de>
2014-07-14 08:02:24 +02:00
Peter Lieven
81921e3762 pdu: handle async target requests logout events
Signed-off-by: Peter Lieven <pl@kamp.de>
2014-06-17 14:52:27 +02:00
Peter Lieven
98bbeab664 pdu: introduce ISCSI_PDU_URGENT_DELIVERY
Signed-off-by: Peter Lieven <pl@kamp.de>
2014-06-17 14:50:41 +02:00
Peter Lieven
99585b6996 pdu: introduce ISCSI_PDU_CORK_WHEN_SENT
Signed-off-by: Peter Lieven <pl@kamp.de>
2014-06-17 14:42:08 +02:00
Peter Lieven
5d55d32adb socket: do not stop sending for immediate PDUs
Signed-off-by: Peter Lieven <pl@kamp.de>
2014-06-17 14:42:00 +02:00
Ronnie Sahlberg
717b95cb8a slist.h: rename SLIST_ to ISCSI_LIST_ to avoid clash on *BSD
Rename the macros for managing the linked lists from SLIST_* to ISCSI_LIST_*
to avoid a clash on *BSD which already have other macros SLIST_*

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
2014-05-07 06:44:19 -07:00
Michael Tokarev
120b308741 Spelling fixes: Dont => Don't
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
2013-12-16 18:19:24 -08:00
Ronnie Sahlberg
78e49f1427 Coding style update 2013-11-05 17:59:55 -08:00
Paolo Bonzini
bb0e59055a handle bad iscsi->fd in iscsi_service
Just do nothing if the file descriptor is invalid.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2013-11-05 14:31:16 +01:00
Edward Tomasz Napierala
dd1ca33094 Freebsd build fixes. 2013-10-13 15:35:00 +02:00
Ronnie Sahlberg
d7b7c7727a Fix win32 so it works again 2013-10-05 15:34:56 -07:00
Sitsofe Wheeler
2fda87160c Fix compilation on OS X
OS X doesn't implement SOL_TCP so workaround this the same way as on
FreeBSD/Solaris.
2013-09-09 19:38:00 +01:00
Paolo Bonzini
73b0d4777f Ignore padding when an iovector is supplied
The iSCSI protocol adds padding to a data packet if the data size is not
a multiple of four.  The iovector provided by QEMU does not include such
padding, and libiscsi then complains that there was a protocol error.
This patch fixes this by reading the padding in a separate "recv"
system call.  These packets anyway do not happen in the data path,
where the packet size is a multiple of 512.

This fixes QEMU's scsi-generic backend, which triggered the problem when
the target sent a 66-byte INQUIRY response.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2013-08-26 23:53:31 +02:00
Ronnie Sahlberg
dc981c93ef Cleanup: rename to payload_* the PDU variables used to track writing to socket 2013-08-04 17:34:15 -07:00
Ronnie Sahlberg
6cb88eb0ce Cleanup: rename pdu->written -> pdu->outdata_written 2013-08-04 17:27:29 -07:00
Ronnie Sahlberg
c5bd95aac9 Remove the use of TCP_CORK 2013-08-04 16:15:22 -07:00
Ronnie Sahlberg
06eab264f6 Rewrite and simplify iscsi_iovector_readv_writev 2013-08-04 14:34:57 -07:00
Ronnie Sahlberg
912b5dc495 Merge pull request #67 from plieven/small_allocations
MEMORY introduce a small allocation pool
2013-08-02 06:45:35 -07:00
Paolo Bonzini
73ce7f40c8 avoid casting struct sockaddr
On ARM, this produces a warning.  Use a union instead.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2013-08-02 15:06:16 +02:00