Blame SOURCES/kvm-io-add-new-qio_channel_-readv-writev-read-write-_all.patch

4a2fec
From 1bafd8e2d09bfdf4bb2098b82537a9baa170aabc Mon Sep 17 00:00:00 2001
4a2fec
From: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
Date: Sat, 2 Dec 2017 12:19:37 +0100
4a2fec
Subject: [PATCH 11/36] io: add new qio_channel_{readv, writev, read,
4a2fec
 write}_all functions
4a2fec
4a2fec
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
Message-id: <20171202121953.13317-2-pbonzini@redhat.com>
4a2fec
Patchwork-id: 78074
4a2fec
O-Subject: [RHEL7.4 qemu-kvm-rhev PATCH 01/17] io: add new qio_channel_{readv, writev, read, write}_all functions
4a2fec
Bugzilla: 1464908
4a2fec
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
4a2fec
RH-Acked-by: John Snow <jsnow@redhat.com>
4a2fec
4a2fec
From: "Daniel P. Berrange" <berrange@redhat.com>
4a2fec
4a2fec
These functions wait until they are able to read / write the full
4a2fec
requested data buffer(s).
4a2fec
4a2fec
Reviewed-by: Eric Blake <eblake@redhat.com>
4a2fec
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
4a2fec
(cherry picked from commit d4622e55883211072621958d39ddaa73483d201e)
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 include/io/channel.h       |  90 +++++++++++++++++++++++++++++++++++++++
4a2fec
 io/channel.c               |  94 +++++++++++++++++++++++++++++++++++++++++
4a2fec
 tests/io-channel-helpers.c | 102 ++++-----------------------------------------
4a2fec
 3 files changed, 193 insertions(+), 93 deletions(-)
4a2fec
4a2fec
diff --git a/include/io/channel.h b/include/io/channel.h
4a2fec
index db9bb02..e11a62e 100644
4a2fec
--- a/include/io/channel.h
4a2fec
+++ b/include/io/channel.h
4a2fec
@@ -269,6 +269,58 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc,
4a2fec
                                 Error **errp);
4a2fec
 
4a2fec
 /**
4a2fec
+ * qio_channel_readv_all:
4a2fec
+ * @ioc: the channel object
4a2fec
+ * @iov: the array of memory regions to read data into
4a2fec
+ * @niov: the length of the @iov array
4a2fec
+ * @errp: pointer to a NULL-initialized error object
4a2fec
+ *
4a2fec
+ * Read data from the IO channel, storing it in the
4a2fec
+ * memory regions referenced by @iov. Each element
4a2fec
+ * in the @iov will be fully populated with data
4a2fec
+ * before the next one is used. The @niov parameter
4a2fec
+ * specifies the total number of elements in @iov.
4a2fec
+ *
4a2fec
+ * The function will wait for all requested data
4a2fec
+ * to be read, yielding from the current coroutine
4a2fec
+ * if required.
4a2fec
+ *
4a2fec
+ * If end-of-file occurs before all requested data
4a2fec
+ * has been read, an error will be reported.
4a2fec
+ *
4a2fec
+ * Returns: 0 if all bytes were read, or -1 on error
4a2fec
+ */
4a2fec
+int qio_channel_readv_all(QIOChannel *ioc,
4a2fec
+                          const struct iovec *iov,
4a2fec
+                          size_t niov,
4a2fec
+                          Error **errp);
4a2fec
+
4a2fec
+
4a2fec
+/**
4a2fec
+ * qio_channel_writev_all:
4a2fec
+ * @ioc: the channel object
4a2fec
+ * @iov: the array of memory regions to write data from
4a2fec
+ * @niov: the length of the @iov array
4a2fec
+ * @errp: pointer to a NULL-initialized error object
4a2fec
+ *
4a2fec
+ * Write data to the IO channel, reading it from the
4a2fec
+ * memory regions referenced by @iov. Each element
4a2fec
+ * in the @iov will be fully sent, before the next
4a2fec
+ * one is used. The @niov parameter specifies the
4a2fec
+ * total number of elements in @iov.
4a2fec
+ *
4a2fec
+ * The function will wait for all requested data
4a2fec
+ * to be written, yielding from the current coroutine
4a2fec
+ * if required.
4a2fec
+ *
4a2fec
+ * Returns: 0 if all bytes were written, or -1 on error
4a2fec
+ */
4a2fec
+int qio_channel_writev_all(QIOChannel *ioc,
4a2fec
+                           const struct iovec *iov,
4a2fec
+                           size_t niov,
4a2fec
+                           Error **erp);
4a2fec
+
4a2fec
+/**
4a2fec
  * qio_channel_readv:
4a2fec
  * @ioc: the channel object
4a2fec
  * @iov: the array of memory regions to read data into
4a2fec
@@ -331,6 +383,44 @@ ssize_t qio_channel_write(QIOChannel *ioc,
4a2fec
                           Error **errp);
4a2fec
 
4a2fec
 /**
4a2fec
+ * qio_channel_read_all:
4a2fec
+ * @ioc: the channel object
4a2fec
+ * @buf: the memory region to read data into
4a2fec
+ * @buflen: the number of bytes to @buf
4a2fec
+ * @errp: pointer to a NULL-initialized error object
4a2fec
+ *
4a2fec
+ * Reads @buflen bytes into @buf, possibly blocking or (if the
4a2fec
+ * channel is non-blocking) yielding from the current coroutine
4a2fec
+ * multiple times until the entire content is read. If end-of-file
4a2fec
+ * occurs it will return an error rather than a short-read. Otherwise
4a2fec
+ * behaves as qio_channel_read().
4a2fec
+ *
4a2fec
+ * Returns: 0 if all bytes were read, or -1 on error
4a2fec
+ */
4a2fec
+int qio_channel_read_all(QIOChannel *ioc,
4a2fec
+                         char *buf,
4a2fec
+                         size_t buflen,
4a2fec
+                         Error **errp);
4a2fec
+/**
4a2fec
+ * qio_channel_write_all:
4a2fec
+ * @ioc: the channel object
4a2fec
+ * @buf: the memory region to write data into
4a2fec
+ * @buflen: the number of bytes to @buf
4a2fec
+ * @errp: pointer to a NULL-initialized error object
4a2fec
+ *
4a2fec
+ * Writes @buflen bytes from @buf, possibly blocking or (if the
4a2fec
+ * channel is non-blocking) yielding from the current coroutine
4a2fec
+ * multiple times until the entire content is written.  Otherwise
4a2fec
+ * behaves as qio_channel_write().
4a2fec
+ *
4a2fec
+ * Returns: 0 if all bytes were written, or -1 on error
4a2fec
+ */
4a2fec
+int qio_channel_write_all(QIOChannel *ioc,
4a2fec
+                          const char *buf,
4a2fec
+                          size_t buflen,
4a2fec
+                          Error **errp);
4a2fec
+
4a2fec
+/**
4a2fec
  * qio_channel_set_blocking:
4a2fec
  * @ioc: the channel object
4a2fec
  * @enabled: the blocking flag state
4a2fec
diff --git a/io/channel.c b/io/channel.c
4a2fec
index 1cfb8b3..5e8c2f0 100644
4a2fec
--- a/io/channel.c
4a2fec
+++ b/io/channel.c
4a2fec
@@ -22,6 +22,7 @@
4a2fec
 #include "io/channel.h"
4a2fec
 #include "qapi/error.h"
4a2fec
 #include "qemu/main-loop.h"
4a2fec
+#include "qemu/iov.h"
4a2fec
 
4a2fec
 bool qio_channel_has_feature(QIOChannel *ioc,
4a2fec
                              QIOChannelFeature feature)
4a2fec
@@ -85,6 +86,79 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc,
4a2fec
 }
4a2fec
 
4a2fec
 
4a2fec
+
4a2fec
+int qio_channel_readv_all(QIOChannel *ioc,
4a2fec
+                          const struct iovec *iov,
4a2fec
+                          size_t niov,
4a2fec
+                          Error **errp)
4a2fec
+{
4a2fec
+    int ret = -1;
4a2fec
+    struct iovec *local_iov = g_new(struct iovec, niov);
4a2fec
+    struct iovec *local_iov_head = local_iov;
4a2fec
+    unsigned int nlocal_iov = niov;
4a2fec
+
4a2fec
+    nlocal_iov = iov_copy(local_iov, nlocal_iov,
4a2fec
+                          iov, niov,
4a2fec
+                          0, iov_size(iov, niov));
4a2fec
+
4a2fec
+    while (nlocal_iov > 0) {
4a2fec
+        ssize_t len;
4a2fec
+        len = qio_channel_readv(ioc, local_iov, nlocal_iov, errp);
4a2fec
+        if (len == QIO_CHANNEL_ERR_BLOCK) {
4a2fec
+            qio_channel_wait(ioc, G_IO_IN);
4a2fec
+            continue;
4a2fec
+        } else if (len < 0) {
4a2fec
+            goto cleanup;
4a2fec
+        } else if (len == 0) {
4a2fec
+            error_setg(errp,
4a2fec
+                       "Unexpected end-of-file before all bytes were read");
4a2fec
+            goto cleanup;
4a2fec
+        }
4a2fec
+
4a2fec
+        iov_discard_front(&local_iov, &nlocal_iov, len);
4a2fec
+    }
4a2fec
+
4a2fec
+    ret = 0;
4a2fec
+
4a2fec
+ cleanup:
4a2fec
+    g_free(local_iov_head);
4a2fec
+    return ret;
4a2fec
+}
4a2fec
+
4a2fec
+int qio_channel_writev_all(QIOChannel *ioc,
4a2fec
+                           const struct iovec *iov,
4a2fec
+                           size_t niov,
4a2fec
+                           Error **errp)
4a2fec
+{
4a2fec
+    int ret = -1;
4a2fec
+    struct iovec *local_iov = g_new(struct iovec, niov);
4a2fec
+    struct iovec *local_iov_head = local_iov;
4a2fec
+    unsigned int nlocal_iov = niov;
4a2fec
+
4a2fec
+    nlocal_iov = iov_copy(local_iov, nlocal_iov,
4a2fec
+                          iov, niov,
4a2fec
+                          0, iov_size(iov, niov));
4a2fec
+
4a2fec
+    while (nlocal_iov > 0) {
4a2fec
+        ssize_t len;
4a2fec
+        len = qio_channel_writev(ioc, local_iov, nlocal_iov, errp);
4a2fec
+        if (len == QIO_CHANNEL_ERR_BLOCK) {
4a2fec
+            qio_channel_wait(ioc, G_IO_OUT);
4a2fec
+            continue;
4a2fec
+        }
4a2fec
+        if (len < 0) {
4a2fec
+            goto cleanup;
4a2fec
+        }
4a2fec
+
4a2fec
+        iov_discard_front(&local_iov, &nlocal_iov, len);
4a2fec
+    }
4a2fec
+
4a2fec
+    ret = 0;
4a2fec
+ cleanup:
4a2fec
+    g_free(local_iov_head);
4a2fec
+    return ret;
4a2fec
+}
4a2fec
+
4a2fec
 ssize_t qio_channel_readv(QIOChannel *ioc,
4a2fec
                           const struct iovec *iov,
4a2fec
                           size_t niov,
4a2fec
@@ -123,6 +197,26 @@ ssize_t qio_channel_write(QIOChannel *ioc,
4a2fec
 }
4a2fec
 
4a2fec
 
4a2fec
+int qio_channel_read_all(QIOChannel *ioc,
4a2fec
+                         char *buf,
4a2fec
+                         size_t buflen,
4a2fec
+                         Error **errp)
4a2fec
+{
4a2fec
+    struct iovec iov = { .iov_base = buf, .iov_len = buflen };
4a2fec
+    return qio_channel_readv_all(ioc, &iov, 1, errp);
4a2fec
+}
4a2fec
+
4a2fec
+
4a2fec
+int qio_channel_write_all(QIOChannel *ioc,
4a2fec
+                          const char *buf,
4a2fec
+                          size_t buflen,
4a2fec
+                          Error **errp)
4a2fec
+{
4a2fec
+    struct iovec iov = { .iov_base = (char *)buf, .iov_len = buflen };
4a2fec
+    return qio_channel_writev_all(ioc, &iov, 1, errp);
4a2fec
+}
4a2fec
+
4a2fec
+
4a2fec
 int qio_channel_set_blocking(QIOChannel *ioc,
4a2fec
                               bool enabled,
4a2fec
                               Error **errp)
4a2fec
diff --git a/tests/io-channel-helpers.c b/tests/io-channel-helpers.c
4a2fec
index 05e5579..5430e13 100644
4a2fec
--- a/tests/io-channel-helpers.c
4a2fec
+++ b/tests/io-channel-helpers.c
4a2fec
@@ -21,6 +21,7 @@
4a2fec
 #include "qemu/osdep.h"
4a2fec
 #include "io-channel-helpers.h"
4a2fec
 #include "qapi/error.h"
4a2fec
+#include "qemu/iov.h"
4a2fec
 
4a2fec
 struct QIOChannelTest {
4a2fec
     QIOChannel *src;
4a2fec
@@ -37,77 +38,17 @@ struct QIOChannelTest {
4a2fec
 };
4a2fec
 
4a2fec
 
4a2fec
-static void test_skip_iovec(struct iovec **iov,
4a2fec
-                            size_t *niov,
4a2fec
-                            size_t skip,
4a2fec
-                            struct iovec *old)
4a2fec
-{
4a2fec
-    size_t offset = 0;
4a2fec
-    size_t i;
4a2fec
-
4a2fec
-    for (i = 0; i < *niov; i++) {
4a2fec
-        if (skip < (*iov)[i].iov_len) {
4a2fec
-            old->iov_len = (*iov)[i].iov_len;
4a2fec
-            old->iov_base = (*iov)[i].iov_base;
4a2fec
-
4a2fec
-            (*iov)[i].iov_len -= skip;
4a2fec
-            (*iov)[i].iov_base += skip;
4a2fec
-            break;
4a2fec
-        } else {
4a2fec
-            skip -= (*iov)[i].iov_len;
4a2fec
-
4a2fec
-            if (i == 0 && old->iov_base) {
4a2fec
-                (*iov)[i].iov_len = old->iov_len;
4a2fec
-                (*iov)[i].iov_base = old->iov_base;
4a2fec
-                old->iov_len = 0;
4a2fec
-                old->iov_base = NULL;
4a2fec
-            }
4a2fec
-
4a2fec
-            offset++;
4a2fec
-        }
4a2fec
-    }
4a2fec
-
4a2fec
-    *iov = *iov + offset;
4a2fec
-    *niov -= offset;
4a2fec
-}
4a2fec
-
4a2fec
-
4a2fec
 /* This thread sends all data using iovecs */
4a2fec
 static gpointer test_io_thread_writer(gpointer opaque)
4a2fec
 {
4a2fec
     QIOChannelTest *data = opaque;
4a2fec
-    struct iovec *iov = data->inputv;
4a2fec
-    size_t niov = data->niov;
4a2fec
-    struct iovec old = { 0 };
4a2fec
 
4a2fec
     qio_channel_set_blocking(data->src, data->blocking, NULL);
4a2fec
 
4a2fec
-    while (niov) {
4a2fec
-        ssize_t ret;
4a2fec
-        ret = qio_channel_writev(data->src,
4a2fec
-                                 iov,
4a2fec
-                                 niov,
4a2fec
-                                 &data->writeerr);
4a2fec
-        if (ret == QIO_CHANNEL_ERR_BLOCK) {
4a2fec
-            if (data->blocking) {
4a2fec
-                error_setg(&data->writeerr,
4a2fec
-                           "Unexpected I/O blocking");
4a2fec
-                break;
4a2fec
-            } else {
4a2fec
-                qio_channel_wait(data->src,
4a2fec
-                                 G_IO_OUT);
4a2fec
-                continue;
4a2fec
-            }
4a2fec
-        } else if (ret < 0) {
4a2fec
-            break;
4a2fec
-        } else if (ret == 0) {
4a2fec
-            error_setg(&data->writeerr,
4a2fec
-                       "Unexpected zero length write");
4a2fec
-            break;
4a2fec
-        }
4a2fec
-
4a2fec
-        test_skip_iovec(&iov, &niov, ret, &old;;
4a2fec
-    }
4a2fec
+    qio_channel_writev_all(data->src,
4a2fec
+                           data->inputv,
4a2fec
+                           data->niov,
4a2fec
+                           &data->writeerr);
4a2fec
 
4a2fec
     return NULL;
4a2fec
 }
4a2fec
@@ -117,38 +58,13 @@ static gpointer test_io_thread_writer(gpointer opaque)
4a2fec
 static gpointer test_io_thread_reader(gpointer opaque)
4a2fec
 {
4a2fec
     QIOChannelTest *data = opaque;
4a2fec
-    struct iovec *iov = data->outputv;
4a2fec
-    size_t niov = data->niov;
4a2fec
-    struct iovec old = { 0 };
4a2fec
 
4a2fec
     qio_channel_set_blocking(data->dst, data->blocking, NULL);
4a2fec
 
4a2fec
-    while (niov) {
4a2fec
-        ssize_t ret;
4a2fec
-
4a2fec
-        ret = qio_channel_readv(data->dst,
4a2fec
-                                iov,
4a2fec
-                                niov,
4a2fec
-                                &data->readerr);
4a2fec
-
4a2fec
-        if (ret == QIO_CHANNEL_ERR_BLOCK) {
4a2fec
-            if (data->blocking) {
4a2fec
-                error_setg(&data->readerr,
4a2fec
-                           "Unexpected I/O blocking");
4a2fec
-                break;
4a2fec
-            } else {
4a2fec
-                qio_channel_wait(data->dst,
4a2fec
-                                 G_IO_IN);
4a2fec
-                continue;
4a2fec
-            }
4a2fec
-        } else if (ret < 0) {
4a2fec
-            break;
4a2fec
-        } else if (ret == 0) {
4a2fec
-            break;
4a2fec
-        }
4a2fec
-
4a2fec
-        test_skip_iovec(&iov, &niov, ret, &old;;
4a2fec
-    }
4a2fec
+    qio_channel_readv_all(data->dst,
4a2fec
+                          data->outputv,
4a2fec
+                          data->niov,
4a2fec
+                          &data->readerr);
4a2fec
 
4a2fec
     return NULL;
4a2fec
 }
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec