mvadkert / rpms / qemu

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