Blame SOURCES/kvm-QIOChannel-Add-flags-on-io_writev-and-introduce-io_f.patch

719b13
From dc840cee933cfc1790b7624c88052f6deb43101d Mon Sep 17 00:00:00 2001
719b13
From: Leonardo Bras <leobras@redhat.com>
719b13
Date: Wed, 18 May 2022 02:52:24 -0300
719b13
Subject: [PATCH 17/34] QIOChannel: Add flags on io_writev and introduce
719b13
 io_flush callback
719b13
MIME-Version: 1.0
719b13
Content-Type: text/plain; charset=UTF-8
719b13
Content-Transfer-Encoding: 8bit
719b13
719b13
RH-Author: Leonardo Brás <leobras@redhat.com>
719b13
RH-MergeRequest: 185: MSG_ZEROCOPY + Multifd @ rhel8.6
719b13
RH-Commit: [17/34] 8ebb6301a83816937d7b87709cf906e1a9c16b01
719b13
RH-Bugzilla: 2117252
719b13
RH-Acked-by: quintela1 <quintela@redhat.com>
719b13
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
719b13
RH-Acked-by: Peter Xu <peterx@redhat.com>
719b13
719b13
Add flags to io_writev and introduce io_flush as optional callback to
719b13
QIOChannelClass, allowing the implementation of zero copy writes by
719b13
subclasses.
719b13
719b13
How to use them:
719b13
- Write data using qio_channel_writev*(...,QIO_CHANNEL_WRITE_FLAG_ZERO_COPY),
719b13
- Wait write completion with qio_channel_flush().
719b13
719b13
Notes:
719b13
As some zero copy write implementations work asynchronously, it's
719b13
recommended to keep the write buffer untouched until the return of
719b13
qio_channel_flush(), to avoid the risk of sending an updated buffer
719b13
instead of the buffer state during write.
719b13
719b13
As io_flush callback is optional, if a subclass does not implement it, then:
719b13
- io_flush will return 0 without changing anything.
719b13
719b13
Also, some functions like qio_channel_writev_full_all() were adapted to
719b13
receive a flag parameter. That allows shared code between zero copy and
719b13
non-zero copy writev, and also an easier implementation on new flags.
719b13
719b13
Signed-off-by: Leonardo Bras <leobras@redhat.com>
719b13
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
719b13
Reviewed-by: Peter Xu <peterx@redhat.com>
719b13
Reviewed-by: Juan Quintela <quintela@redhat.com>
719b13
Message-Id: <20220513062836.965425-3-leobras@redhat.com>
719b13
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
719b13
(cherry picked from commit b88651cb4d4fa416fdbb6afaf5b26ec8c035eaad)
719b13
Signed-off-by: Leonardo Bras <leobras@redhat.com>
719b13
---
719b13
 chardev/char-io.c                   |  2 +-
719b13
 hw/remote/mpqemu-link.c             |  2 +-
719b13
 include/io/channel.h                | 38 +++++++++++++++++++++-
719b13
 io/channel-buffer.c                 |  1 +
719b13
 io/channel-command.c                |  1 +
719b13
 io/channel-file.c                   |  1 +
719b13
 io/channel-socket.c                 |  2 ++
719b13
 io/channel-tls.c                    |  1 +
719b13
 io/channel-websock.c                |  1 +
719b13
 io/channel.c                        | 49 +++++++++++++++++++++++------
719b13
 migration/rdma.c                    |  1 +
719b13
 scsi/pr-manager-helper.c            |  2 +-
719b13
 tests/unit/test-io-channel-socket.c |  1 +
719b13
 13 files changed, 88 insertions(+), 14 deletions(-)
719b13
719b13
diff --git a/chardev/char-io.c b/chardev/char-io.c
719b13
index 8ced184160..4451128cba 100644
719b13
--- a/chardev/char-io.c
719b13
+++ b/chardev/char-io.c
719b13
@@ -122,7 +122,7 @@ int io_channel_send_full(QIOChannel *ioc,
719b13
 
719b13
         ret = qio_channel_writev_full(
719b13
             ioc, &iov, 1,
719b13
-            fds, nfds, NULL);
719b13
+            fds, nfds, 0, NULL);
719b13
         if (ret == QIO_CHANNEL_ERR_BLOCK) {
719b13
             if (offset) {
719b13
                 return offset;
719b13
diff --git a/hw/remote/mpqemu-link.c b/hw/remote/mpqemu-link.c
719b13
index 7e841820e5..e8f556bd27 100644
719b13
--- a/hw/remote/mpqemu-link.c
719b13
+++ b/hw/remote/mpqemu-link.c
719b13
@@ -69,7 +69,7 @@ bool mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc, Error **errp)
719b13
     }
719b13
 
719b13
     if (!qio_channel_writev_full_all(ioc, send, G_N_ELEMENTS(send),
719b13
-                                    fds, nfds, errp)) {
719b13
+                                    fds, nfds, 0, errp)) {
719b13
         ret = true;
719b13
     } else {
719b13
         trace_mpqemu_send_io_error(msg->cmd, msg->size, nfds);
719b13
diff --git a/include/io/channel.h b/include/io/channel.h
719b13
index 88988979f8..c680ee7480 100644
719b13
--- a/include/io/channel.h
719b13
+++ b/include/io/channel.h
719b13
@@ -32,12 +32,15 @@ OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass,
719b13
 
719b13
 #define QIO_CHANNEL_ERR_BLOCK -2
719b13
 
719b13
+#define QIO_CHANNEL_WRITE_FLAG_ZERO_COPY 0x1
719b13
+
719b13
 typedef enum QIOChannelFeature QIOChannelFeature;
719b13
 
719b13
 enum QIOChannelFeature {
719b13
     QIO_CHANNEL_FEATURE_FD_PASS,
719b13
     QIO_CHANNEL_FEATURE_SHUTDOWN,
719b13
     QIO_CHANNEL_FEATURE_LISTEN,
719b13
+    QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY,
719b13
 };
719b13
 
719b13
 
719b13
@@ -104,6 +107,7 @@ struct QIOChannelClass {
719b13
                          size_t niov,
719b13
                          int *fds,
719b13
                          size_t nfds,
719b13
+                         int flags,
719b13
                          Error **errp);
719b13
     ssize_t (*io_readv)(QIOChannel *ioc,
719b13
                         const struct iovec *iov,
719b13
@@ -136,6 +140,8 @@ struct QIOChannelClass {
719b13
                                   IOHandler *io_read,
719b13
                                   IOHandler *io_write,
719b13
                                   void *opaque);
719b13
+    int (*io_flush)(QIOChannel *ioc,
719b13
+                    Error **errp);
719b13
 };
719b13
 
719b13
 /* General I/O handling functions */
719b13
@@ -228,6 +234,7 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
719b13
  * @niov: the length of the @iov array
719b13
  * @fds: an array of file handles to send
719b13
  * @nfds: number of file handles in @fds
719b13
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
719b13
  * @errp: pointer to a NULL-initialized error object
719b13
  *
719b13
  * Write data to the IO channel, reading it from the
719b13
@@ -260,6 +267,7 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc,
719b13
                                 size_t niov,
719b13
                                 int *fds,
719b13
                                 size_t nfds,
719b13
+                                int flags,
719b13
                                 Error **errp);
719b13
 
719b13
 /**
719b13
@@ -837,6 +845,7 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
719b13
  * @niov: the length of the @iov array
719b13
  * @fds: an array of file handles to send
719b13
  * @nfds: number of file handles in @fds
719b13
+ * @flags: write flags (QIO_CHANNEL_WRITE_FLAG_*)
719b13
  * @errp: pointer to a NULL-initialized error object
719b13
  *
719b13
  *
719b13
@@ -846,6 +855,14 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
719b13
  * to be written, yielding from the current coroutine
719b13
  * if required.
719b13
  *
719b13
+ * If QIO_CHANNEL_WRITE_FLAG_ZERO_COPY is passed in flags,
719b13
+ * instead of waiting for all requested data to be written,
719b13
+ * this function will wait until it's all queued for writing.
719b13
+ * In this case, if the buffer gets changed between queueing and
719b13
+ * sending, the updated buffer will be sent. If this is not a
719b13
+ * desired behavior, it's suggested to call qio_channel_flush()
719b13
+ * before reusing the buffer.
719b13
+ *
719b13
  * Returns: 0 if all bytes were written, or -1 on error
719b13
  */
719b13
 
719b13
@@ -853,6 +870,25 @@ int qio_channel_writev_full_all(QIOChannel *ioc,
719b13
                                 const struct iovec *iov,
719b13
                                 size_t niov,
719b13
                                 int *fds, size_t nfds,
719b13
-                                Error **errp);
719b13
+                                int flags, Error **errp);
719b13
+
719b13
+/**
719b13
+ * qio_channel_flush:
719b13
+ * @ioc: the channel object
719b13
+ * @errp: pointer to a NULL-initialized error object
719b13
+ *
719b13
+ * Will block until every packet queued with
719b13
+ * qio_channel_writev_full() + QIO_CHANNEL_WRITE_FLAG_ZERO_COPY
719b13
+ * is sent, or return in case of any error.
719b13
+ *
719b13
+ * If not implemented, acts as a no-op, and returns 0.
719b13
+ *
719b13
+ * Returns -1 if any error is found,
719b13
+ *          1 if every send failed to use zero copy.
719b13
+ *          0 otherwise.
719b13
+ */
719b13
+
719b13
+int qio_channel_flush(QIOChannel *ioc,
719b13
+                      Error **errp);
719b13
 
719b13
 #endif /* QIO_CHANNEL_H */
719b13
diff --git a/io/channel-buffer.c b/io/channel-buffer.c
719b13
index baa4e2b089..bf52011be2 100644
719b13
--- a/io/channel-buffer.c
719b13
+++ b/io/channel-buffer.c
719b13
@@ -81,6 +81,7 @@ static ssize_t qio_channel_buffer_writev(QIOChannel *ioc,
719b13
                                          size_t niov,
719b13
                                          int *fds,
719b13
                                          size_t nfds,
719b13
+                                         int flags,
719b13
                                          Error **errp)
719b13
 {
719b13
     QIOChannelBuffer *bioc = QIO_CHANNEL_BUFFER(ioc);
719b13
diff --git a/io/channel-command.c b/io/channel-command.c
719b13
index b2a9e27138..5ff1691bad 100644
719b13
--- a/io/channel-command.c
719b13
+++ b/io/channel-command.c
719b13
@@ -258,6 +258,7 @@ static ssize_t qio_channel_command_writev(QIOChannel *ioc,
719b13
                                           size_t niov,
719b13
                                           int *fds,
719b13
                                           size_t nfds,
719b13
+                                          int flags,
719b13
                                           Error **errp)
719b13
 {
719b13
     QIOChannelCommand *cioc = QIO_CHANNEL_COMMAND(ioc);
719b13
diff --git a/io/channel-file.c b/io/channel-file.c
719b13
index c4bf799a80..348a48545e 100644
719b13
--- a/io/channel-file.c
719b13
+++ b/io/channel-file.c
719b13
@@ -114,6 +114,7 @@ static ssize_t qio_channel_file_writev(QIOChannel *ioc,
719b13
                                        size_t niov,
719b13
                                        int *fds,
719b13
                                        size_t nfds,
719b13
+                                       int flags,
719b13
                                        Error **errp)
719b13
 {
719b13
     QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
719b13
diff --git a/io/channel-socket.c b/io/channel-socket.c
719b13
index 606ec97cf7..bfbd64787e 100644
719b13
--- a/io/channel-socket.c
719b13
+++ b/io/channel-socket.c
719b13
@@ -525,6 +525,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
719b13
                                          size_t niov,
719b13
                                          int *fds,
719b13
                                          size_t nfds,
719b13
+                                         int flags,
719b13
                                          Error **errp)
719b13
 {
719b13
     QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
719b13
@@ -620,6 +621,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
719b13
                                          size_t niov,
719b13
                                          int *fds,
719b13
                                          size_t nfds,
719b13
+                                         int flags,
719b13
                                          Error **errp)
719b13
 {
719b13
     QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
719b13
diff --git a/io/channel-tls.c b/io/channel-tls.c
719b13
index 2ae1b92fc0..4ce890a538 100644
719b13
--- a/io/channel-tls.c
719b13
+++ b/io/channel-tls.c
719b13
@@ -301,6 +301,7 @@ static ssize_t qio_channel_tls_writev(QIOChannel *ioc,
719b13
                                       size_t niov,
719b13
                                       int *fds,
719b13
                                       size_t nfds,
719b13
+                                      int flags,
719b13
                                       Error **errp)
719b13
 {
719b13
     QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
719b13
diff --git a/io/channel-websock.c b/io/channel-websock.c
719b13
index 70889bb54d..035dd6075b 100644
719b13
--- a/io/channel-websock.c
719b13
+++ b/io/channel-websock.c
719b13
@@ -1127,6 +1127,7 @@ static ssize_t qio_channel_websock_writev(QIOChannel *ioc,
719b13
                                           size_t niov,
719b13
                                           int *fds,
719b13
                                           size_t nfds,
719b13
+                                          int flags,
719b13
                                           Error **errp)
719b13
 {
719b13
     QIOChannelWebsock *wioc = QIO_CHANNEL_WEBSOCK(ioc);
719b13
diff --git a/io/channel.c b/io/channel.c
719b13
index e8b019dc36..0640941ac5 100644
719b13
--- a/io/channel.c
719b13
+++ b/io/channel.c
719b13
@@ -72,18 +72,32 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc,
719b13
                                 size_t niov,
719b13
                                 int *fds,
719b13
                                 size_t nfds,
719b13
+                                int flags,
719b13
                                 Error **errp)
719b13
 {
719b13
     QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
719b13
 
719b13
-    if ((fds || nfds) &&
719b13
-        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
719b13
+    if (fds || nfds) {
719b13
+        if (!qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
719b13
+            error_setg_errno(errp, EINVAL,
719b13
+                             "Channel does not support file descriptor passing");
719b13
+            return -1;
719b13
+        }
719b13
+        if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
719b13
+            error_setg_errno(errp, EINVAL,
719b13
+                             "Zero Copy does not support file descriptor passing");
719b13
+            return -1;
719b13
+        }
719b13
+    }
719b13
+
719b13
+    if ((flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) &&
719b13
+        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY)) {
719b13
         error_setg_errno(errp, EINVAL,
719b13
-                         "Channel does not support file descriptor passing");
719b13
+                         "Requested Zero Copy feature is not available");
719b13
         return -1;
719b13
     }
719b13
 
719b13
-    return klass->io_writev(ioc, iov, niov, fds, nfds, errp);
719b13
+    return klass->io_writev(ioc, iov, niov, fds, nfds, flags, errp);
719b13
 }
719b13
 
719b13
 
719b13
@@ -217,14 +231,14 @@ int qio_channel_writev_all(QIOChannel *ioc,
719b13
                            size_t niov,
719b13
                            Error **errp)
719b13
 {
719b13
-    return qio_channel_writev_full_all(ioc, iov, niov, NULL, 0, errp);
719b13
+    return qio_channel_writev_full_all(ioc, iov, niov, NULL, 0, 0, errp);
719b13
 }
719b13
 
719b13
 int qio_channel_writev_full_all(QIOChannel *ioc,
719b13
                                 const struct iovec *iov,
719b13
                                 size_t niov,
719b13
                                 int *fds, size_t nfds,
719b13
-                                Error **errp)
719b13
+                                int flags, Error **errp)
719b13
 {
719b13
     int ret = -1;
719b13
     struct iovec *local_iov = g_new(struct iovec, niov);
719b13
@@ -237,8 +251,10 @@ int qio_channel_writev_full_all(QIOChannel *ioc,
719b13
 
719b13
     while (nlocal_iov > 0) {
719b13
         ssize_t len;
719b13
-        len = qio_channel_writev_full(ioc, local_iov, nlocal_iov, fds, nfds,
719b13
-                                      errp);
719b13
+
719b13
+        len = qio_channel_writev_full(ioc, local_iov, nlocal_iov, fds,
719b13
+                                            nfds, flags, errp);
719b13
+
719b13
         if (len == QIO_CHANNEL_ERR_BLOCK) {
719b13
             if (qemu_in_coroutine()) {
719b13
                 qio_channel_yield(ioc, G_IO_OUT);
719b13
@@ -277,7 +293,7 @@ ssize_t qio_channel_writev(QIOChannel *ioc,
719b13
                            size_t niov,
719b13
                            Error **errp)
719b13
 {
719b13
-    return qio_channel_writev_full(ioc, iov, niov, NULL, 0, errp);
719b13
+    return qio_channel_writev_full(ioc, iov, niov, NULL, 0, 0, errp);
719b13
 }
719b13
 
719b13
 
719b13
@@ -297,7 +313,7 @@ ssize_t qio_channel_write(QIOChannel *ioc,
719b13
                           Error **errp)
719b13
 {
719b13
     struct iovec iov = { .iov_base = (char *)buf, .iov_len = buflen };
719b13
-    return qio_channel_writev_full(ioc, &iov, 1, NULL, 0, errp);
719b13
+    return qio_channel_writev_full(ioc, &iov, 1, NULL, 0, 0, errp);
719b13
 }
719b13
 
719b13
 
719b13
@@ -473,6 +489,19 @@ off_t qio_channel_io_seek(QIOChannel *ioc,
719b13
     return klass->io_seek(ioc, offset, whence, errp);
719b13
 }
719b13
 
719b13
+int qio_channel_flush(QIOChannel *ioc,
719b13
+                                Error **errp)
719b13
+{
719b13
+    QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
719b13
+
719b13
+    if (!klass->io_flush ||
719b13
+        !qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY)) {
719b13
+        return 0;
719b13
+    }
719b13
+
719b13
+    return klass->io_flush(ioc, errp);
719b13
+}
719b13
+
719b13
 
719b13
 static void qio_channel_restart_read(void *opaque)
719b13
 {
719b13
diff --git a/migration/rdma.c b/migration/rdma.c
719b13
index f5d3bbe7e9..54acd2000e 100644
719b13
--- a/migration/rdma.c
719b13
+++ b/migration/rdma.c
719b13
@@ -2833,6 +2833,7 @@ static ssize_t qio_channel_rdma_writev(QIOChannel *ioc,
719b13
                                        size_t niov,
719b13
                                        int *fds,
719b13
                                        size_t nfds,
719b13
+                                       int flags,
719b13
                                        Error **errp)
719b13
 {
719b13
     QIOChannelRDMA *rioc = QIO_CHANNEL_RDMA(ioc);
719b13
diff --git a/scsi/pr-manager-helper.c b/scsi/pr-manager-helper.c
719b13
index 451c7631b7..3be52a98d5 100644
719b13
--- a/scsi/pr-manager-helper.c
719b13
+++ b/scsi/pr-manager-helper.c
719b13
@@ -77,7 +77,7 @@ static int pr_manager_helper_write(PRManagerHelper *pr_mgr,
719b13
         iov.iov_base = (void *)buf;
719b13
         iov.iov_len = sz;
719b13
         n_written = qio_channel_writev_full(QIO_CHANNEL(pr_mgr->ioc), &iov, 1,
719b13
-                                            nfds ? &fd : NULL, nfds, errp);
719b13
+                                            nfds ? &fd : NULL, nfds, 0, errp);
719b13
 
719b13
         if (n_written <= 0) {
719b13
             assert(n_written != QIO_CHANNEL_ERR_BLOCK);
719b13
diff --git a/tests/unit/test-io-channel-socket.c b/tests/unit/test-io-channel-socket.c
719b13
index c49eec1f03..6713886d02 100644
719b13
--- a/tests/unit/test-io-channel-socket.c
719b13
+++ b/tests/unit/test-io-channel-socket.c
719b13
@@ -444,6 +444,7 @@ static void test_io_channel_unix_fd_pass(void)
719b13
                             G_N_ELEMENTS(iosend),
719b13
                             fdsend,
719b13
                             G_N_ELEMENTS(fdsend),
719b13
+                            0,
719b13
                             &error_abort);
719b13
 
719b13
     qio_channel_readv_full(dst,
719b13
-- 
719b13
2.35.3
719b13