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

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