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>
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>
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>
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>
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>
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>
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>
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>
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.
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>
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.
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>
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>
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>
This patch finally introduces a small allocation pool
which recycles all the small portions of memory that
are used for headers and pdu structures. This was
the initial idea behind wrapping all memory functions
in libiscsi.
The results of booting are test system up to the login
prompt are quite impressive:
BEFORE:
libiscsi:5 memory is clean at iscsi_destroy_context() after 10712 mallocs, 18 realloc(s) and 10712 free(s)
AFTER:
libiscsi:5 memory is clean at iscsi_destroy_context() after 41 mallocs, 18 realloc(s), 41 free(s) and 10584 reused small allocations
Signed-off-by: Peter Lieven <pl@kamp.de>