0fb2b2
From: "Daniel P. Berrange" <berrange@redhat.com>
0fb2b2
Date: Wed, 30 Aug 2017 14:53:59 +0100
59eb7a
Subject: [PATCH] io: add new qio_channel_{readv, writev, read, write}_all
59eb7a
 functions
0fb2b2
0fb2b2
These functions wait until they are able to read / write the full
0fb2b2
requested data buffer(s).
0fb2b2
0fb2b2
Reviewed-by: Eric Blake <eblake@redhat.com>
0fb2b2
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
0fb2b2
---
a056f3
 include/io/channel.h       |  90 ++++++++++++++++++++++++++++++++
a056f3
 io/channel.c               |  94 ++++++++++++++++++++++++++++++++++
a056f3
 tests/io-channel-helpers.c | 102 ++++---------------------------------
0fb2b2
 3 files changed, 193 insertions(+), 93 deletions(-)
0fb2b2
0fb2b2
diff --git a/include/io/channel.h b/include/io/channel.h
0fb2b2
index db9bb022a1..e11a62ea50 100644
0fb2b2
--- a/include/io/channel.h
0fb2b2
+++ b/include/io/channel.h
59eb7a
@@ -268,6 +268,58 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc,
59eb7a
                                 size_t nfds,
0fb2b2
                                 Error **errp);
0fb2b2
 
59eb7a
+/**
0fb2b2
+ * qio_channel_readv_all:
0fb2b2
+ * @ioc: the channel object
0fb2b2
+ * @iov: the array of memory regions to read data into
0fb2b2
+ * @niov: the length of the @iov array
0fb2b2
+ * @errp: pointer to a NULL-initialized error object
0fb2b2
+ *
0fb2b2
+ * Read data from the IO channel, storing it in the
0fb2b2
+ * memory regions referenced by @iov. Each element
0fb2b2
+ * in the @iov will be fully populated with data
0fb2b2
+ * before the next one is used. The @niov parameter
0fb2b2
+ * specifies the total number of elements in @iov.
0fb2b2
+ *
0fb2b2
+ * The function will wait for all requested data
0fb2b2
+ * to be read, yielding from the current coroutine
0fb2b2
+ * if required.
0fb2b2
+ *
0fb2b2
+ * If end-of-file occurs before all requested data
0fb2b2
+ * has been read, an error will be reported.
0fb2b2
+ *
0fb2b2
+ * Returns: 0 if all bytes were read, or -1 on error
0fb2b2
+ */
0fb2b2
+int qio_channel_readv_all(QIOChannel *ioc,
0fb2b2
+                          const struct iovec *iov,
0fb2b2
+                          size_t niov,
0fb2b2
+                          Error **errp);
0fb2b2
+
0fb2b2
+
0fb2b2
+/**
0fb2b2
+ * qio_channel_writev_all:
0fb2b2
+ * @ioc: the channel object
0fb2b2
+ * @iov: the array of memory regions to write data from
0fb2b2
+ * @niov: the length of the @iov array
0fb2b2
+ * @errp: pointer to a NULL-initialized error object
0fb2b2
+ *
0fb2b2
+ * Write data to the IO channel, reading it from the
0fb2b2
+ * memory regions referenced by @iov. Each element
0fb2b2
+ * in the @iov will be fully sent, before the next
0fb2b2
+ * one is used. The @niov parameter specifies the
0fb2b2
+ * total number of elements in @iov.
0fb2b2
+ *
0fb2b2
+ * The function will wait for all requested data
0fb2b2
+ * to be written, yielding from the current coroutine
0fb2b2
+ * if required.
0fb2b2
+ *
0fb2b2
+ * Returns: 0 if all bytes were written, or -1 on error
0fb2b2
+ */
0fb2b2
+int qio_channel_writev_all(QIOChannel *ioc,
0fb2b2
+                           const struct iovec *iov,
0fb2b2
+                           size_t niov,
0fb2b2
+                           Error **erp);
0fb2b2
+
59eb7a
 /**
0fb2b2
  * qio_channel_readv:
0fb2b2
  * @ioc: the channel object
59eb7a
@@ -330,6 +382,44 @@ ssize_t qio_channel_write(QIOChannel *ioc,
59eb7a
                           size_t buflen,
0fb2b2
                           Error **errp);
0fb2b2
 
59eb7a
+/**
0fb2b2
+ * qio_channel_read_all:
0fb2b2
+ * @ioc: the channel object
0fb2b2
+ * @buf: the memory region to read data into
0fb2b2
+ * @buflen: the number of bytes to @buf
0fb2b2
+ * @errp: pointer to a NULL-initialized error object
0fb2b2
+ *
0fb2b2
+ * Reads @buflen bytes into @buf, possibly blocking or (if the
0fb2b2
+ * channel is non-blocking) yielding from the current coroutine
0fb2b2
+ * multiple times until the entire content is read. If end-of-file
0fb2b2
+ * occurs it will return an error rather than a short-read. Otherwise
0fb2b2
+ * behaves as qio_channel_read().
0fb2b2
+ *
0fb2b2
+ * Returns: 0 if all bytes were read, or -1 on error
0fb2b2
+ */
0fb2b2
+int qio_channel_read_all(QIOChannel *ioc,
0fb2b2
+                         char *buf,
0fb2b2
+                         size_t buflen,
0fb2b2
+                         Error **errp);
0fb2b2
+/**
0fb2b2
+ * qio_channel_write_all:
0fb2b2
+ * @ioc: the channel object
0fb2b2
+ * @buf: the memory region to write data into
0fb2b2
+ * @buflen: the number of bytes to @buf
0fb2b2
+ * @errp: pointer to a NULL-initialized error object
0fb2b2
+ *
0fb2b2
+ * Writes @buflen bytes from @buf, possibly blocking or (if the
0fb2b2
+ * channel is non-blocking) yielding from the current coroutine
0fb2b2
+ * multiple times until the entire content is written.  Otherwise
0fb2b2
+ * behaves as qio_channel_write().
0fb2b2
+ *
0fb2b2
+ * Returns: 0 if all bytes were written, or -1 on error
0fb2b2
+ */
0fb2b2
+int qio_channel_write_all(QIOChannel *ioc,
0fb2b2
+                          const char *buf,
0fb2b2
+                          size_t buflen,
0fb2b2
+                          Error **errp);
0fb2b2
+
59eb7a
 /**
0fb2b2
  * qio_channel_set_blocking:
0fb2b2
  * @ioc: the channel object
0fb2b2
diff --git a/io/channel.c b/io/channel.c
0fb2b2
index 1cfb8b33a2..5e8c2f0a91 100644
0fb2b2
--- a/io/channel.c
0fb2b2
+++ b/io/channel.c
0fb2b2
@@ -22,6 +22,7 @@
0fb2b2
 #include "io/channel.h"
0fb2b2
 #include "qapi/error.h"
0fb2b2
 #include "qemu/main-loop.h"
0fb2b2
+#include "qemu/iov.h"
0fb2b2
 
0fb2b2
 bool qio_channel_has_feature(QIOChannel *ioc,
0fb2b2
                              QIOChannelFeature feature)
0fb2b2
@@ -85,6 +86,79 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc,
0fb2b2
 }
0fb2b2
 
0fb2b2
 
0fb2b2
+
0fb2b2
+int qio_channel_readv_all(QIOChannel *ioc,
0fb2b2
+                          const struct iovec *iov,
0fb2b2
+                          size_t niov,
0fb2b2
+                          Error **errp)
0fb2b2
+{
0fb2b2
+    int ret = -1;
0fb2b2
+    struct iovec *local_iov = g_new(struct iovec, niov);
0fb2b2
+    struct iovec *local_iov_head = local_iov;
0fb2b2
+    unsigned int nlocal_iov = niov;
0fb2b2
+
0fb2b2
+    nlocal_iov = iov_copy(local_iov, nlocal_iov,
0fb2b2
+                          iov, niov,
0fb2b2
+                          0, iov_size(iov, niov));
0fb2b2
+
0fb2b2
+    while (nlocal_iov > 0) {
0fb2b2
+        ssize_t len;
0fb2b2
+        len = qio_channel_readv(ioc, local_iov, nlocal_iov, errp);
0fb2b2
+        if (len == QIO_CHANNEL_ERR_BLOCK) {
0fb2b2
+            qio_channel_wait(ioc, G_IO_IN);
0fb2b2
+            continue;
0fb2b2
+        } else if (len < 0) {
0fb2b2
+            goto cleanup;
0fb2b2
+        } else if (len == 0) {
0fb2b2
+            error_setg(errp,
0fb2b2
+                       "Unexpected end-of-file before all bytes were read");
0fb2b2
+            goto cleanup;
0fb2b2
+        }
0fb2b2
+
0fb2b2
+        iov_discard_front(&local_iov, &nlocal_iov, len);
0fb2b2
+    }
0fb2b2
+
0fb2b2
+    ret = 0;
0fb2b2
+
0fb2b2
+ cleanup:
0fb2b2
+    g_free(local_iov_head);
0fb2b2
+    return ret;
0fb2b2
+}
0fb2b2
+
0fb2b2
+int qio_channel_writev_all(QIOChannel *ioc,
0fb2b2
+                           const struct iovec *iov,
0fb2b2
+                           size_t niov,
0fb2b2
+                           Error **errp)
0fb2b2
+{
0fb2b2
+    int ret = -1;
0fb2b2
+    struct iovec *local_iov = g_new(struct iovec, niov);
0fb2b2
+    struct iovec *local_iov_head = local_iov;
0fb2b2
+    unsigned int nlocal_iov = niov;
0fb2b2
+
0fb2b2
+    nlocal_iov = iov_copy(local_iov, nlocal_iov,
0fb2b2
+                          iov, niov,
0fb2b2
+                          0, iov_size(iov, niov));
0fb2b2
+
0fb2b2
+    while (nlocal_iov > 0) {
0fb2b2
+        ssize_t len;
0fb2b2
+        len = qio_channel_writev(ioc, local_iov, nlocal_iov, errp);
0fb2b2
+        if (len == QIO_CHANNEL_ERR_BLOCK) {
0fb2b2
+            qio_channel_wait(ioc, G_IO_OUT);
0fb2b2
+            continue;
0fb2b2
+        }
0fb2b2
+        if (len < 0) {
0fb2b2
+            goto cleanup;
0fb2b2
+        }
0fb2b2
+
0fb2b2
+        iov_discard_front(&local_iov, &nlocal_iov, len);
0fb2b2
+    }
0fb2b2
+
0fb2b2
+    ret = 0;
0fb2b2
+ cleanup:
0fb2b2
+    g_free(local_iov_head);
0fb2b2
+    return ret;
0fb2b2
+}
0fb2b2
+
0fb2b2
 ssize_t qio_channel_readv(QIOChannel *ioc,
0fb2b2
                           const struct iovec *iov,
0fb2b2
                           size_t niov,
0fb2b2
@@ -123,6 +197,26 @@ ssize_t qio_channel_write(QIOChannel *ioc,
0fb2b2
 }
0fb2b2
 
0fb2b2
 
0fb2b2
+int qio_channel_read_all(QIOChannel *ioc,
0fb2b2
+                         char *buf,
0fb2b2
+                         size_t buflen,
0fb2b2
+                         Error **errp)
0fb2b2
+{
0fb2b2
+    struct iovec iov = { .iov_base = buf, .iov_len = buflen };
0fb2b2
+    return qio_channel_readv_all(ioc, &iov, 1, errp);
0fb2b2
+}
0fb2b2
+
0fb2b2
+
0fb2b2
+int qio_channel_write_all(QIOChannel *ioc,
0fb2b2
+                          const char *buf,
0fb2b2
+                          size_t buflen,
0fb2b2
+                          Error **errp)
0fb2b2
+{
0fb2b2
+    struct iovec iov = { .iov_base = (char *)buf, .iov_len = buflen };
0fb2b2
+    return qio_channel_writev_all(ioc, &iov, 1, errp);
0fb2b2
+}
0fb2b2
+
0fb2b2
+
0fb2b2
 int qio_channel_set_blocking(QIOChannel *ioc,
0fb2b2
                               bool enabled,
0fb2b2
                               Error **errp)
0fb2b2
diff --git a/tests/io-channel-helpers.c b/tests/io-channel-helpers.c
0fb2b2
index 05e5579cf8..5430e1389d 100644
0fb2b2
--- a/tests/io-channel-helpers.c
0fb2b2
+++ b/tests/io-channel-helpers.c
0fb2b2
@@ -21,6 +21,7 @@
0fb2b2
 #include "qemu/osdep.h"
0fb2b2
 #include "io-channel-helpers.h"
0fb2b2
 #include "qapi/error.h"
0fb2b2
+#include "qemu/iov.h"
0fb2b2
 
0fb2b2
 struct QIOChannelTest {
0fb2b2
     QIOChannel *src;
0fb2b2
@@ -37,77 +38,17 @@ struct QIOChannelTest {
0fb2b2
 };
0fb2b2
 
0fb2b2
 
0fb2b2
-static void test_skip_iovec(struct iovec **iov,
0fb2b2
-                            size_t *niov,
0fb2b2
-                            size_t skip,
0fb2b2
-                            struct iovec *old)
0fb2b2
-{
0fb2b2
-    size_t offset = 0;
0fb2b2
-    size_t i;
0fb2b2
-
0fb2b2
-    for (i = 0; i < *niov; i++) {
0fb2b2
-        if (skip < (*iov)[i].iov_len) {
0fb2b2
-            old->iov_len = (*iov)[i].iov_len;
0fb2b2
-            old->iov_base = (*iov)[i].iov_base;
0fb2b2
-
0fb2b2
-            (*iov)[i].iov_len -= skip;
0fb2b2
-            (*iov)[i].iov_base += skip;
0fb2b2
-            break;
0fb2b2
-        } else {
0fb2b2
-            skip -= (*iov)[i].iov_len;
0fb2b2
-
0fb2b2
-            if (i == 0 && old->iov_base) {
0fb2b2
-                (*iov)[i].iov_len = old->iov_len;
0fb2b2
-                (*iov)[i].iov_base = old->iov_base;
0fb2b2
-                old->iov_len = 0;
0fb2b2
-                old->iov_base = NULL;
0fb2b2
-            }
0fb2b2
-
0fb2b2
-            offset++;
0fb2b2
-        }
0fb2b2
-    }
0fb2b2
-
0fb2b2
-    *iov = *iov + offset;
0fb2b2
-    *niov -= offset;
0fb2b2
-}
0fb2b2
-
0fb2b2
-
0fb2b2
 /* This thread sends all data using iovecs */
0fb2b2
 static gpointer test_io_thread_writer(gpointer opaque)
0fb2b2
 {
0fb2b2
     QIOChannelTest *data = opaque;
0fb2b2
-    struct iovec *iov = data->inputv;
0fb2b2
-    size_t niov = data->niov;
0fb2b2
-    struct iovec old = { 0 };
0fb2b2
 
0fb2b2
     qio_channel_set_blocking(data->src, data->blocking, NULL);
0fb2b2
 
0fb2b2
-    while (niov) {
0fb2b2
-        ssize_t ret;
0fb2b2
-        ret = qio_channel_writev(data->src,
0fb2b2
-                                 iov,
0fb2b2
-                                 niov,
0fb2b2
-                                 &data->writeerr);
0fb2b2
-        if (ret == QIO_CHANNEL_ERR_BLOCK) {
0fb2b2
-            if (data->blocking) {
0fb2b2
-                error_setg(&data->writeerr,
0fb2b2
-                           "Unexpected I/O blocking");
0fb2b2
-                break;
0fb2b2
-            } else {
0fb2b2
-                qio_channel_wait(data->src,
0fb2b2
-                                 G_IO_OUT);
0fb2b2
-                continue;
0fb2b2
-            }
0fb2b2
-        } else if (ret < 0) {
0fb2b2
-            break;
0fb2b2
-        } else if (ret == 0) {
0fb2b2
-            error_setg(&data->writeerr,
0fb2b2
-                       "Unexpected zero length write");
0fb2b2
-            break;
0fb2b2
-        }
0fb2b2
-
0fb2b2
-        test_skip_iovec(&iov, &niov, ret, &old);
0fb2b2
-    }
0fb2b2
+    qio_channel_writev_all(data->src,
0fb2b2
+                           data->inputv,
0fb2b2
+                           data->niov,
0fb2b2
+                           &data->writeerr);
0fb2b2
 
0fb2b2
     return NULL;
0fb2b2
 }
0fb2b2
@@ -117,38 +58,13 @@ static gpointer test_io_thread_writer(gpointer opaque)
0fb2b2
 static gpointer test_io_thread_reader(gpointer opaque)
0fb2b2
 {
0fb2b2
     QIOChannelTest *data = opaque;
0fb2b2
-    struct iovec *iov = data->outputv;
0fb2b2
-    size_t niov = data->niov;
0fb2b2
-    struct iovec old = { 0 };
0fb2b2
 
0fb2b2
     qio_channel_set_blocking(data->dst, data->blocking, NULL);
0fb2b2
 
0fb2b2
-    while (niov) {
0fb2b2
-        ssize_t ret;
0fb2b2
-
0fb2b2
-        ret = qio_channel_readv(data->dst,
0fb2b2
-                                iov,
0fb2b2
-                                niov,
0fb2b2
-                                &data->readerr);
0fb2b2
-
0fb2b2
-        if (ret == QIO_CHANNEL_ERR_BLOCK) {
0fb2b2
-            if (data->blocking) {
0fb2b2
-                error_setg(&data->readerr,
0fb2b2
-                           "Unexpected I/O blocking");
0fb2b2
-                break;
0fb2b2
-            } else {
0fb2b2
-                qio_channel_wait(data->dst,
0fb2b2
-                                 G_IO_IN);
0fb2b2
-                continue;
0fb2b2
-            }
0fb2b2
-        } else if (ret < 0) {
0fb2b2
-            break;
0fb2b2
-        } else if (ret == 0) {
0fb2b2
-            break;
0fb2b2
-        }
0fb2b2
-
0fb2b2
-        test_skip_iovec(&iov, &niov, ret, &old);
0fb2b2
-    }
0fb2b2
+    qio_channel_readv_all(data->dst,
0fb2b2
+                          data->outputv,
0fb2b2
+                          data->niov,
0fb2b2
+                          &data->readerr);
0fb2b2
 
0fb2b2
     return NULL;
0fb2b2
 }