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

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