ISCSI: iscsi_process_read callback for when the iscsi socket becomes readable may be invoked by qemu after the fd-is-readable event has cleared.

Message ID
State New
Headers show

Commit Message

ronnie sahlberg May 11, 2012, 10:04 p.m.
Libiscsi treats a situation such as POLLIN was invoked and the socket is readable but ioctl(...FIONREAD...) returns that there are no bytes available to read as an error and that the socket is faulty or has been closed.
which may trigger a slow process of closing down the socket completely and trying to reconnect to recover.

Update the iscsi fd-is-readable callback  iscsi_process_read to check for this condition explicitely.
If are invoked and getsockopt tells us there is a real socket problem then we pass POLLIN onto libiscsi and let it try to handle the situation and/or recover.
If there is no error, but ioctl(...FIONREAD...) still indicates that there were no bytes to read, then we treat this as just a false invokation from the eventsystem and do nothing.
If there are bytes available to read, then we pass POLLIN into libiscsi and let it read and process the bytes.

ronnie sahlberg

Signed-off-by: Ronnie Sahlberg <>
 block/iscsi.c |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)


diff --git a/block/iscsi.c b/block/iscsi.c
index d37c4ee..694f135 100644
--- a/block/iscsi.c
+++ b/block/iscsi.c
@@ -25,6 +25,9 @@ 
 #include "config-host.h"
 #include <poll.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
 #include "qemu-common.h"
 #include "qemu-error.h"
 #include "block_int.h"
@@ -116,6 +119,35 @@  iscsi_process_read(void *arg)
     IscsiLun *iscsilun = arg;
     struct iscsi_context *iscsi = iscsilun->iscsi;
+    int socket_count = 0;
+    int err = 0;
+    socklen_t err_size = sizeof(err);
+    /* There are places where we might be invoked but the read-event
+       may not still be active.
+       Libiscsi treats POLLIN but socket having no bytes available to read
+       as a socket error.
+       So we have to check socket status and available bytes explicitely
+       before we invoke libiscsi.
+    */
+    if (getsockopt(iscsi_get_fd(iscsi), SOL_SOCKET, SO_ERROR, &err,
+                   &err_size) != 0 || err != 0) {
+        /* There is a socket error, call libicsi and let it try to handle
+           the error and maybe try reconnecting.
+         */
+        iscsi_service(iscsi, POLLIN);
+        iscsi_set_events(iscsilun);
+        return;
+    }
+    if (ioctl(iscsi_get_fd(iscsi), FIONREAD, &socket_count) == 0
+    && socket_count == 0) {
+        /* no bytes available to read from the socket, and there was no
+           error, just return without calling libiscsi
+         */
+        iscsi_set_events(iscsilun);
+        return;
+    }
     iscsi_service(iscsi, POLLIN);