Skip to contentSkip to conversation

QIOChannel async_write & async_flush + MSG_ZEROCOPY + multifd

PATCH In review Leonardo Bras 3 participants 23 messages Sep 22, 2021 – Sep 30, 2021

lore
Share & tools
lore
#0 lore raw

v3 cover QIOChannel async_write & async_flush + MSG_ZEROCOPY + multifd

This patch series intends to enable MSG_ZEROCOPY in QIOChannel, and make use of it for multifd migration performance improvement.

Patch #1 creates new callbacks for QIOChannel, allowing the implementation of asynchronous writing.

Patch #2 implements async_write and async_flush on QIOChannelSocket,

Patch #3 Makes use of async_write + async_flush to enable MSG_ZEROCOPY for migration using multifd nocomp.

Results:
So far, the resource usage of __sys_sendmsg() reduced 15 times, and the overall migration took 13-18% less time, based in synthetic workload.

The objective is to reduce migration time in hosts with heavy cpu usage.

---
Changes since v2:
- Patch #1: One more fallback
- Patch #2: Fall back to sync if fails to lock buffer memory in MSG_ZEROCOPY send.

Changes since v1:
- Reimplemented the patchset using async_write + async_flush approach.
- Implemented a flush to be able to tell whenever all data was written.

Leonardo Bras (3):
QIOChannel: Add io_async_writev & io_async_flush callbacks
QIOChannelSocket: Implement io_async_write & io_async_flush
multifd: Send using asynchronous write on nocomp to send RAM pages.

 include/io/channel-socket.h |   2 +
 include/io/channel.h        |  94 ++++++++++++++++---
 io/channel-socket.c         | 176 ++++++++++++++++++++++++++++++++++--
 io/channel.c                |  66 +++++++++++---
 migration/multifd.c         |   3 +-
 5 files changed, 300 insertions(+), 41 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH v3 1/3 QIOChannel: Add io_async_writev & io_async_flush callbacks

Adds io_async_writev and io_async_flush as optional callback to QIOChannelClass, allowing the implementation of asynchronous writes by subclasses.

How to use them:
- Write data using qio_channel_async_writev(),
- Wait write completion with qio_channel_async_flush().

Notes:
Some asynchronous implementations may benefit from zerocopy mechanisms, so it's recommended to keep the write buffer untouched until the return of qio_channel_async_flush().

As the new callbacks are optional, if a subclass does not implement them there will be a fallback to the mandatory synchronous implementation:
- io_async_writev will fallback to io_writev,
- io_async_flush will return without changing anything.
This makes simpler for the user to make use of the asynchronous implementation.

Also, some functions like qio_channel_writev_full_all() were adapted to offer an async version, and make better use of the new callbacks.

---
 include/io/channel.h | 93 +++++++++++++++++++++++++++++++++++++-------
 io/channel.c         | 66 ++++++++++++++++++++++++-------
 2 files changed, 129 insertions(+), 30 deletions(-)
include/io/channel.h +78 -15
index 88988979f8..74f2e3ae8a 100644
@@ -136,6 +136,14 @@ struct QIOChannelClass {
                                   IOHandler *io_read,
                                   IOHandler *io_write,
                                   void *opaque);
+    ssize_t (*io_async_writev)(QIOChannel *ioc,
+                               const struct iovec *iov,
+                               size_t niov,
+                               int *fds,
+                               size_t nfds,
+                               Error **errp);
+   void (*io_async_flush)(QIOChannel *ioc,
+                          Error **errp);
 };
 
 /* General I/O handling functions */
@@ -255,12 +263,17 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
  * and the channel is non-blocking
  */
-ssize_t qio_channel_writev_full(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds,
-                                size_t nfds,
-                                Error **errp);
+ssize_t __qio_channel_writev_full(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  int *fds,
+                                  size_t nfds,
+                                  bool async,
+                                  Error **errp);
+#define qio_channel_writev_full(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full(ioc, iov, niov, fds, nfds, false, errp)
+#define qio_channel_async_writev_full(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full(ioc, iov, niov, fds, nfds, true, errp)
 
 /**
  * qio_channel_readv_all_eof:
@@ -339,10 +352,15 @@ int qio_channel_readv_all(QIOChannel *ioc,
  *
  * Returns: 0 if all bytes were written, or -1 on error
  */
-int qio_channel_writev_all(QIOChannel *ioc,
-                           const struct iovec *iov,
-                           size_t niov,
-                           Error **erp);
+int __qio_channel_writev_all(QIOChannel *ioc,
+                             const struct iovec *iov,
+                             size_t niov,
+                             bool async,
+                             Error **erp);
+#define qio_channel_writev_all(ioc, iov, niov, erp) \
+    __qio_channel_writev_all(ioc, iov, niov, false, erp)
+#define qio_channel_async_writev_all(ioc, iov, niov, erp) \
+    __qio_channel_writev_all(ioc, iov, niov, true, erp)
 
 /**
  * qio_channel_readv:
@@ -849,10 +867,55 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
  * Returns: 0 if all bytes were written, or -1 on error
  */
 
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp);
+int __qio_channel_writev_full_all(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  int *fds, size_t nfds,
+                                  bool async, Error **errp);
+#define qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, false, errp)
+#define qio_channel_async_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, true, errp)
+
+/**
+ * qio_channel_async_writev:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @fds: an array of file handles to send
+ * @nfds: number of file handles in @fds
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Behaves like qio_channel_writev_full, but will send
+ * data asynchronously, this meaning this function
+ * may return before the data is actually sent.
+ *
+ * If at some point it's necessary wait for all data to be
+ * sent, use qio_channel_async_flush().
+ *
+ * If not implemented, falls back to the default writev
+ */
+
+ssize_t qio_channel_async_writev(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int *fds,
+                                 size_t nfds,
+                                 Error **errp);
+
+/**
+ * qio_channel_async_flush:
+ * @ioc: the channel object
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Will lock until every packet queued with qio_channel_async_writev()
+ * is sent.
+ *
+ * If not implemented, returns without changing anything.
+ */
+
+void qio_channel_async_flush(QIOChannel *ioc,
+                             Error **errp);
+
 
 #endif /* QIO_CHANNEL_H */
io/channel.c +51 -15

102 lines not on this page · raw message

Signature · 1 line
2.33.0
#2 lore raw

Re: [PATCH v3 1/3] QIOChannel: Add io_async_writev & io_async_flush callbacks

Quoted 47 lines · Leonardo Bras

Adds io_async_writev and io_async_flush as optional callback to QIOChannelClass, allowing the implementation of asynchronous writes by subclasses.

How to use them:
- Write data using qio_channel_async_writev(),
- Wait write completion with qio_channel_async_flush().

Notes:
Some asynchronous implementations may benefit from zerocopy mechanisms, so it's recommended to keep the write buffer untouched until the return of qio_channel_async_flush().

As the new callbacks are optional, if a subclass does not implement them there will be a fallback to the mandatory synchronous implementation:
- io_async_writev will fallback to io_writev,
- io_async_flush will return without changing anything.
This makes simpler for the user to make use of the asynchronous implementation.

Also, some functions like qio_channel_writev_full_all() were adapted to offer an async version, and make better use of the new callbacks.

---
 include/io/channel.h | 93 +++++++++++++++++++++++++++++++++++++-------
 io/channel.c         | 66 ++++++++++++++++++++++++-------
 2 files changed, 129 insertions(+), 30 deletions(-)
include/io/channel.h +8 -0
index 88988979f8..74f2e3ae8a 100644
@@ -136,6 +136,14 @@ struct QIOChannelClass {
                                   IOHandler *io_read,
                                   IOHandler *io_write,
                                   void *opaque);
+    ssize_t (*io_async_writev)(QIOChannel *ioc,
+                               const struct iovec *iov,
+                               size_t niov,
+                               int *fds,
+                               size_t nfds,
+                               Error **errp);
+   void (*io_async_flush)(QIOChannel *ioc,
+                          Error **errp);
 };
 
 /* General I/O handling functions */

include/io/channel.h ssize_t qio_channel_readv_full(QIOChannel *ioc, · line 266

Quoted 10 lines
include/io/channel.h +1 -6
@@ -255,12 +263,17 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
  * and the channel is non-blocking
  */
-ssize_t qio_channel_writev_full(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds,
-                                size_t nfds,
-                                Error **errp);
+ssize_t __qio_channel_writev_full(QIOChannel *ioc,

Using "__" is undesirable as that namespace is reserved.

include/io/channel.h ssize_t qio_channel_readv_full(QIOChannel *ioc, · line 276

Quoted 10 lines
include/io/channel.h +10 -0
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  int *fds,
+                                  size_t nfds,
+                                  bool async,
+                                  Error **errp);
+#define qio_channel_writev_full(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full(ioc, iov, niov, fds, nfds, false, errp)
+#define qio_channel_async_writev_full(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full(ioc, iov, niov, fds, nfds, true, errp)

The API docs only cover the first function, not the second.

Quoted 51 lines
include/io/channel.h +9 -4
 /**
  * qio_channel_readv_all_eof:
@@ -339,10 +352,15 @@ int qio_channel_readv_all(QIOChannel *ioc,
  *
  * Returns: 0 if all bytes were written, or -1 on error
  */
-int qio_channel_writev_all(QIOChannel *ioc,
-                           const struct iovec *iov,
-                           size_t niov,
-                           Error **erp);
+int __qio_channel_writev_all(QIOChannel *ioc,
+                             const struct iovec *iov,
+                             size_t niov,
+                             bool async,
+                             Error **erp);
+#define qio_channel_writev_all(ioc, iov, niov, erp) \
+    __qio_channel_writev_all(ioc, iov, niov, false, erp)
+#define qio_channel_async_writev_all(ioc, iov, niov, erp) \
+    __qio_channel_writev_all(ioc, iov, niov, true, erp)
 
 
 

/**
* qio_channel_readv:

include/io/channel.h +18 -5
@@ -849,10 +867,55 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
  * Returns: 0 if all bytes were written, or -1 on error
  */
 
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp);
+int __qio_channel_writev_full_all(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  int *fds, size_t nfds,
+                                  bool async, Error **errp);
+#define qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, false, errp)
+#define qio_channel_async_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, true, errp)
+
+/**
+ * qio_channel_async_writev:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @fds: an array of file handles to send
+ * @nfds: number of file handles in @fds
+ * @errp: pointer to a NULL-initialized error object

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 897

Quoted 10 lines
include/io/channel.h +10 -0
+ *
+ * Behaves like qio_channel_writev_full, but will send
+ * data asynchronously, this meaning this function
+ * may return before the data is actually sent.
+ *
+ * If at some point it's necessary wait for all data to be
+ * sent, use qio_channel_async_flush().
+ *
+ * If not implemented, falls back to the default writev
+ */

I'm not convinced by the fallback here. If you're layering I/O channels this is not going to result in desirable behaviour.

eg if QIOChannelTLS doesn't implement async, then when you call async_writev, it'lll invoke sync writev on the QIOChannelTLS, which will in turn invoke the sync writev on QIOChannelSocket, despite the latter having async writev support. I think this is very misleading behaviour

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 904

Quoted 6 lines
include/io/channel.h +6 -0
+ssize_t qio_channel_async_writev(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int *fds,
+                                 size_t nfds,
+                                 Error **errp);

This is missing any flags. We need something like

QIO_CHANNEL_WRITE_FLAG_ZEROCOPY

passed in an 'unsigned int flags' parameter. This in turn makes me question whether we should have the common helpers at all, as the api is going to be different for sync vs async.

The QIOChannelFeature enum probably ought to be extended with QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY with support for probing whether that's supported or not.

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 911

Quoted 7 lines
include/io/channel.h +7 -0
+
+/**
+ * qio_channel_async_flush:
+ * @ioc: the channel object
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Will lock until every packet queued with qio_channel_async_writev()

s/lock/block/ I presume.

Quoted 124 lines
include/io/channel.h +8 -0
+ * is sent.
+ *
+ * If not implemented, returns without changing anything.
+ */
+
+void qio_channel_async_flush(QIOChannel *ioc,
+                             Error **errp);
+
 
 #endif /* QIO_CHANNEL_H */
io/channel.c +51 -15
index e8b019dc36..c4819b922f 100644
@@ -67,12 +67,13 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
 }
 
 
-ssize_t qio_channel_writev_full(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds,
-                                size_t nfds,
-                                Error **errp)
+ssize_t __qio_channel_writev_full(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  int *fds,
+                                  size_t nfds,
+                                  bool async,
+                                  Error **errp)
 {
     QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
 
@@ -83,6 +84,10 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc,
         return -1;
     }
 
+    if (async && klass->io_async_writev) {
+        return klass->io_async_writev(ioc, iov, niov, fds, nfds, errp);
+    }
+
     return klass->io_writev(ioc, iov, niov, fds, nfds, errp);
 }
 
@@ -212,19 +217,20 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
     return ret;
 }
 
-int qio_channel_writev_all(QIOChannel *ioc,
-                           const struct iovec *iov,
-                           size_t niov,
-                           Error **errp)
+int __qio_channel_writev_all(QIOChannel *ioc,
+                             const struct iovec *iov,
+                             size_t niov,
+                             bool async,
+                             Error **errp)
 {
-    return qio_channel_writev_full_all(ioc, iov, niov, NULL, 0, errp);
+    return __qio_channel_writev_full_all(ioc, iov, niov, NULL, 0, async, errp);
 }
 
-int qio_channel_writev_full_all(QIOChannel *ioc,
+int __qio_channel_writev_full_all(QIOChannel *ioc,
                                 const struct iovec *iov,
                                 size_t niov,
                                 int *fds, size_t nfds,
-                                Error **errp)
+                                bool async, Error **errp)
 {
     int ret = -1;
     struct iovec *local_iov = g_new(struct iovec, niov);
@@ -237,8 +243,8 @@ int qio_channel_writev_full_all(QIOChannel *ioc,
 
     while (nlocal_iov > 0) {
         ssize_t len;
-        len = qio_channel_writev_full(ioc, local_iov, nlocal_iov, fds, nfds,
-                                      errp);
+        len = __qio_channel_writev_full(ioc, local_iov, nlocal_iov, fds, nfds,
+                                        async, errp);
         if (len == QIO_CHANNEL_ERR_BLOCK) {
             if (qemu_in_coroutine()) {
                 qio_channel_yield(ioc, G_IO_OUT);
@@ -474,6 +480,36 @@ off_t qio_channel_io_seek(QIOChannel *ioc,
 }
 
 
+ssize_t qio_channel_async_writev(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int *fds,
+                                 size_t nfds,
+                                 Error **errp)
+{
+     QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_async_writev) {
+        return klass->io_writev(ioc, iov, niov, fds, nfds, errp);
+    }
+
+     return klass->io_async_writev(ioc, iov, niov, fds, nfds, errp);
+}
+
+
+void qio_channel_async_flush(QIOChannel *ioc,
+                             Error **errp)
+{
+     QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_async_flush) {
+        return;
+    }
+
+     klass->io_async_flush(ioc, errp);
+}
+
+
 static void qio_channel_restart_read(void *opaque)
 {
     QIOChannel *ioc = opaque;
Signature · 1 line
2.33.0

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

Re: [PATCH v3 1/3] QIOChannel: Add io_async_writev & io_async_flush callbacks

Quoted 11 lines · Daniel P. Berrangé

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 904

Quoted 6 lines
include/io/channel.h +6 -0
+ssize_t qio_channel_async_writev(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int *fds,
+                                 size_t nfds,
+                                 Error **errp);

This is missing any flags. We need something like

QIO_CHANNEL_WRITE_FLAG_ZEROCOPY

Quoted 8 lines

passed in an 'unsigned int flags' parameter. This in turn makes me question whether we should have the common helpers at all, as the api is going to be different for sync vs async.

The QIOChannelFeature enum probably ought to be extended with QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY with support for probing whether that's supported or not.

I'm also wondering whether we could just drop the fds/nfds as per my knowledge SCM_RIGHT is the only user, at the meantime I don't see why an async interface would pass in any fd anyways.. Thanks,

Signature · 1 line
Peter Xu
#4 lore raw

Re: [PATCH v3 1/3] QIOChannel: Add io_async_writev & io_async_flush callbacks

Hello Peter, thanks for reviewing!

Quoted 18 lines · Peter Xu
Quoted 16 lines · Daniel P. Berrangé

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 904

Quoted 6 lines
include/io/channel.h +6 -0
+ssize_t qio_channel_async_writev(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int *fds,
+                                 size_t nfds,
+                                 Error **errp);

This is missing any flags. We need something like

QIO_CHANNEL_WRITE_FLAG_ZEROCOPY

passed in an 'unsigned int flags' parameter. This in turn makes me question whether we should have the common helpers at all, as the api is going to be different for sync vs async.

Quoted 7 lines
Quoted 3 lines

The QIOChannelFeature enum probably ought to be extended with QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY with support for probing whether that's supported or not.

I'm also wondering whether we could just drop the fds/nfds as per my knowledge SCM_RIGHT is the only user, at the meantime I don't see why an async interface would pass in any fd anyways.. Thanks,

FWIW, I think it's a great idea.
Daniel, what do you think?

Quoted 4 lines

--
Peter Xu

Best regards,
Leonardo

#5 lore raw

Re: [PATCH v3 1/3] QIOChannel: Add io_async_writev & io_async_flush callbacks

Quoted 25 lines · Leonardo Bras Soares Passos

Hello Peter, thanks for reviewing!

Quoted 22 lines · Peter Xu
Quoted 19 lines · Daniel P. Berrangé

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 904

Quoted 6 lines
include/io/channel.h +6 -0
+ssize_t qio_channel_async_writev(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int *fds,
+                                 size_t nfds,
+                                 Error **errp);

This is missing any flags. We need something like

QIO_CHANNEL_WRITE_FLAG_ZEROCOPY

passed in an 'unsigned int flags' parameter. This in turn makes me question whether we should have the common helpers at all, as the api is going to be different for sync vs async.

The QIOChannelFeature enum probably ought to be extended with QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY with support for probing whether that's supported or not.

Quoted 6 lines
Quoted 3 lines

I'm also wondering whether we could just drop the fds/nfds as per my knowledge SCM_RIGHT is the only user, at the meantime I don't see why an async interface would pass in any fd anyways.. Thanks,

FWIW, I think it's a great idea.
Daniel, what do you think?

Yes, FD passing is not compatible with async operations, becuase it is too complex to deal with FD lifetime on failure to send IO

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#6 lore raw

Re: [PATCH v3 1/3] QIOChannel: Add io_async_writev & io_async_flush callbacks

In reply to Daniel P. Berrangé

Hello Daniel, thank you for reviewing!

Quoted 47 lines · Daniel P. Berrangé
Quoted 45 lines · Leonardo Bras

Adds io_async_writev and io_async_flush as optional callback to QIOChannelClass, allowing the implementation of asynchronous writes by subclasses.

How to use them:
- Write data using qio_channel_async_writev(),
- Wait write completion with qio_channel_async_flush().

Notes:
Some asynchronous implementations may benefit from zerocopy mechanisms, so it's recommended to keep the write buffer untouched until the return of qio_channel_async_flush().

As the new callbacks are optional, if a subclass does not implement them there will be a fallback to the mandatory synchronous implementation:
- io_async_writev will fallback to io_writev,
- io_async_flush will return without changing anything.
This makes simpler for the user to make use of the asynchronous implementation.

Also, some functions like qio_channel_writev_full_all() were adapted to offer an async version, and make better use of the new callbacks.

---
 include/io/channel.h | 93 +++++++++++++++++++++++++++++++++++++-------
 io/channel.c         | 66 ++++++++++++++++++++++++-------
 2 files changed, 129 insertions(+), 30 deletions(-)
include/io/channel.h +8 -0
index 88988979f8..74f2e3ae8a 100644
@@ -136,6 +136,14 @@ struct QIOChannelClass {
                                   IOHandler *io_read,
                                   IOHandler *io_write,
                                   void *opaque);
+    ssize_t (*io_async_writev)(QIOChannel *ioc,
+                               const struct iovec *iov,
+                               size_t niov,
+                               int *fds,
+                               size_t nfds,
+                               Error **errp);
+   void (*io_async_flush)(QIOChannel *ioc,
+                          Error **errp);
 };
 
Quoted 14 lines

include/io/channel.h ssize_t qio_channel_readv_full(QIOChannel *ioc, · line 266

Quoted 12 lines
include/io/channel.h +1 -6
 /* General I/O handling functions */
@@ -255,12 +263,17 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
  * or QIO_CHANNEL_ERR_BLOCK if no data is can be sent
  * and the channel is non-blocking
  */
-ssize_t qio_channel_writev_full(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds,
-                                size_t nfds,
-                                Error **errp);
+ssize_t __qio_channel_writev_full(QIOChannel *ioc,

Using "__" is undesirable as that namespace is reserved.

Thank you for the tip!
I will make sure to remember avoiding this in the future.

Quoted 13 lines

include/io/channel.h ssize_t qio_channel_readv_full(QIOChannel *ioc, · line 276

Quoted 11 lines
include/io/channel.h +10 -0
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  int *fds,
+                                  size_t nfds,
+                                  bool async,
+                                  Error **errp);
+#define qio_channel_writev_full(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full(ioc, iov, niov, fds, nfds, false, errp)
+#define qio_channel_async_writev_full(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full(ioc, iov, niov, fds, nfds, true, errp)

The API docs only cover the first function, not the second.

You are right.
If this ends up being the final implementation, I will make sure to provide correct docs.

Quoted 68 lines
Quoted 53 lines
include/io/channel.h +9 -4
 /**
  * qio_channel_readv_all_eof:
@@ -339,10 +352,15 @@ int qio_channel_readv_all(QIOChannel *ioc,
  *
  * Returns: 0 if all bytes were written, or -1 on error
  */
-int qio_channel_writev_all(QIOChannel *ioc,
-                           const struct iovec *iov,
-                           size_t niov,
-                           Error **erp);
+int __qio_channel_writev_all(QIOChannel *ioc,
+                             const struct iovec *iov,
+                             size_t niov,
+                             bool async,
+                             Error **erp);
+#define qio_channel_writev_all(ioc, iov, niov, erp) \
+    __qio_channel_writev_all(ioc, iov, niov, false, erp)
+#define qio_channel_async_writev_all(ioc, iov, niov, erp) \
+    __qio_channel_writev_all(ioc, iov, niov, true, erp)
 
 
 

/**
* qio_channel_readv:

include/io/channel.h +18 -5
@@ -849,10 +867,55 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
  * Returns: 0 if all bytes were written, or -1 on error
  */
 
-int qio_channel_writev_full_all(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds, size_t nfds,
-                                Error **errp);
+int __qio_channel_writev_full_all(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  int *fds, size_t nfds,
+                                  bool async, Error **errp);
+#define qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, false, errp)
+#define qio_channel_async_writev_full_all(ioc, iov, niov, fds, nfds, errp) \
+    __qio_channel_writev_full_all(ioc, iov, niov, fds, nfds, true, errp)
+
+/**
+ * qio_channel_async_writev:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @fds: an array of file handles to send
+ * @nfds: number of file handles in @fds
+ * @errp: pointer to a NULL-initialized error object

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 897

Quoted 10 lines
include/io/channel.h +10 -0
+ *
+ * Behaves like qio_channel_writev_full, but will send
+ * data asynchronously, this meaning this function
+ * may return before the data is actually sent.
+ *
+ * If at some point it's necessary wait for all data to be
+ * sent, use qio_channel_async_flush().
+ *
+ * If not implemented, falls back to the default writev
+ */

I'm not convinced by the fallback here. If you're layering I/O channels this is not going to result in desirable behaviour.

Quoted 6 lines

eg if QIOChannelTLS doesn't implement async, then when you call async_writev, it'lll invoke sync writev on the QIOChannelTLS, which will in turn invoke the sync writev on QIOChannelSocket, despite the latter having async writev support. I think this is very misleading behaviour

Yeah, it's a good point.
Failing when async is not supported seems a better approach.

Quoted 12 lines

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 904

Quoted 7 lines
include/io/channel.h +6 -0
+ssize_t qio_channel_async_writev(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int *fds,
+                                 size_t nfds,
+                                 Error **errp);

This is missing any flags. We need something like

QIO_CHANNEL_WRITE_FLAG_ZEROCOPY

Quoted 8 lines

passed in an 'unsigned int flags' parameter. This in turn makes me question whether we should have the common helpers at all, as the api is going to be different for sync vs async.

The QIOChannelFeature enum probably ought to be extended with QIO_CHANNEL_FEATURE_WRITE_ZEROCOPY with support for probing whether that's supported or not.

Yeah, that makes sense to me.

Quoted 10 lines

include/io/channel.h int qio_channel_readv_full_all(QIOChannel *ioc, · line 911

Quoted 8 lines
include/io/channel.h +7 -0
+
+/**
+ * qio_channel_async_flush:
+ * @ioc: the channel object
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Will lock until every packet queued with qio_channel_async_writev()

s/lock/block/ I presume.

correct.

Quoted 133 lines
Quoted 123 lines
include/io/channel.h +8 -0
+ * is sent.
+ *
+ * If not implemented, returns without changing anything.
+ */
+
+void qio_channel_async_flush(QIOChannel *ioc,
+                             Error **errp);
+
 
 #endif /* QIO_CHANNEL_H */
io/channel.c +51 -16
index e8b019dc36..c4819b922f 100644
@@ -67,12 +67,13 @@ ssize_t qio_channel_readv_full(QIOChannel *ioc,
 }
 
 
-ssize_t qio_channel_writev_full(QIOChannel *ioc,
-                                const struct iovec *iov,
-                                size_t niov,
-                                int *fds,
-                                size_t nfds,
-                                Error **errp)
+ssize_t __qio_channel_writev_full(QIOChannel *ioc,
+                                  const struct iovec *iov,
+                                  size_t niov,
+                                  int *fds,
+                                  size_t nfds,
+                                  bool async,
+                                  Error **errp)
 {
     QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
 
@@ -83,6 +84,10 @@ ssize_t qio_channel_writev_full(QIOChannel *ioc,
         return -1;
     }
 
+    if (async && klass->io_async_writev) {
+        return klass->io_async_writev(ioc, iov, niov, fds, nfds, errp);
+    }
+
     return klass->io_writev(ioc, iov, niov, fds, nfds, errp);
 }
 
@@ -212,19 +217,20 @@ int qio_channel_readv_full_all(QIOChannel *ioc,
     return ret;
 }
 
-int qio_channel_writev_all(QIOChannel *ioc,
-                           const struct iovec *iov,
-                           size_t niov,
-                           Error **errp)
+int __qio_channel_writev_all(QIOChannel *ioc,
+                             const struct iovec *iov,
+                             size_t niov,
+                             bool async,
+                             Error **errp)
 {
-    return qio_channel_writev_full_all(ioc, iov, niov, NULL, 0, errp);
+    return __qio_channel_writev_full_all(ioc, iov, niov, NULL, 0, async, errp);
 }
 
-int qio_channel_writev_full_all(QIOChannel *ioc,
+int __qio_channel_writev_full_all(QIOChannel *ioc,
                                 const struct iovec *iov,
                                 size_t niov,
                                 int *fds, size_t nfds,
-                                Error **errp)
+                                bool async, Error **errp)
 {
     int ret = -1;
     struct iovec *local_iov = g_new(struct iovec, niov);
@@ -237,8 +243,8 @@ int qio_channel_writev_full_all(QIOChannel *ioc,
 
     while (nlocal_iov > 0) {
         ssize_t len;
-        len = qio_channel_writev_full(ioc, local_iov, nlocal_iov, fds, nfds,
-                                      errp);
+        len = __qio_channel_writev_full(ioc, local_iov, nlocal_iov, fds, nfds,
+                                        async, errp);
         if (len == QIO_CHANNEL_ERR_BLOCK) {
             if (qemu_in_coroutine()) {
                 qio_channel_yield(ioc, G_IO_OUT);
@@ -474,6 +480,36 @@ off_t qio_channel_io_seek(QIOChannel *ioc,
 }
 
 
+ssize_t qio_channel_async_writev(QIOChannel *ioc,
+                                 const struct iovec *iov,
+                                 size_t niov,
+                                 int *fds,
+                                 size_t nfds,
+                                 Error **errp)
+{
+     QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_async_writev) {
+        return klass->io_writev(ioc, iov, niov, fds, nfds, errp);
+    }
+
+     return klass->io_async_writev(ioc, iov, niov, fds, nfds, errp);
+}
+
+
+void qio_channel_async_flush(QIOChannel *ioc,
+                             Error **errp)
+{
+     QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+
+    if (!klass->io_async_flush) {
+        return;
+    }
+
+     klass->io_async_flush(ioc, errp);
+}
+
+
 static void qio_channel_restart_read(void *opaque)
 {
     QIOChannel *ioc = opaque;
--
Quoted 2 lines

2.33.0

Regards,
Daniel
--

|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#7 lore raw

PATCH v3 2/3 QIOChannelSocket: Implement io_async_write & io_async_flush

In reply to Leonardo Bras

Implement the new optional callbacks io_async_write and io_async_flush on QIOChannelSocket, but enables it only when MSG_ZEROCOPY feature is available in the host kernel, and TCP sockets are used.

qio_channel_socket_writev() contents were moved to a helper function __qio_channel_socket_writev() which accepts an extra 'flag' argument. This helper function is used to implement qio_channel_socket_writev(), with flags = 0, keeping it's behavior unchanged, and qio_channel_socket_async_writev() with flags = MSG_ZEROCOPY.

qio_channel_socket_async_flush() was implemented by reading the socket's error queue, which will have information on MSG_ZEROCOPY send completion. There is no need to worry with re-sending packets in case any error happens, as MSG_ZEROCOPY only works with TCP and it will re-tranmsmit if any error ocurs.

Notes on using async_write():
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying, some caution is necessary to avoid overwriting any buffer before it's sent. If something like this happen, a newer version of the buffer may be sent instead.
- If this is a problem, it's recommended to use async_flush() before freeing or re-using the buffer.
- When using MSG_ZERCOCOPY, the buffer memory will be locked, so it may require a larger amount than usually available to non-root user.
- If the required amount of locked memory is not available, it falls-back to buffer copying behavior, and synchronous sending.

---
 include/io/channel-socket.h |   2 +
 include/io/channel.h        |   1 +
 io/channel-socket.c         | 176 ++++++++++++++++++++++++++++++++++--
 3 files changed, 169 insertions(+), 10 deletions(-)
include/io/channel-socket.h +2 -0
index e747e63514..4d1be0637a 100644
@@ -47,6 +47,8 @@ struct QIOChannelSocket {
     socklen_t localAddrLen;
     struct sockaddr_storage remoteAddr;
     socklen_t remoteAddrLen;
+    ssize_t async_queued;
+    ssize_t async_sent;
 };
 
 
include/io/channel.h +1 -0
index 74f2e3ae8a..611bb2ea26 100644
@@ -31,6 +31,7 @@ OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass,
 
 
 #define QIO_CHANNEL_ERR_BLOCK -2
+#define QIO_CHANNEL_ERR_NOBUFS -3
 
 typedef enum QIOChannelFeature QIOChannelFeature;
 
io/channel-socket.c +166 -10

225 lines not on this page · raw message

Signature · 1 line
2.33.0
#8 lore raw

Re: [PATCH v3 2/3] QIOChannelSocket: Implement io_async_write & io_async_flush

Quoted 109 lines · Leonardo Bras

Implement the new optional callbacks io_async_write and io_async_flush on QIOChannelSocket, but enables it only when MSG_ZEROCOPY feature is available in the host kernel, and TCP sockets are used.

qio_channel_socket_writev() contents were moved to a helper function __qio_channel_socket_writev() which accepts an extra 'flag' argument. This helper function is used to implement qio_channel_socket_writev(), with flags = 0, keeping it's behavior unchanged, and qio_channel_socket_async_writev() with flags = MSG_ZEROCOPY.

qio_channel_socket_async_flush() was implemented by reading the socket's error queue, which will have information on MSG_ZEROCOPY send completion. There is no need to worry with re-sending packets in case any error happens, as MSG_ZEROCOPY only works with TCP and it will re-tranmsmit if any error ocurs.

Notes on using async_write():
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying, some caution is necessary to avoid overwriting any buffer before it's sent. If something like this happen, a newer version of the buffer may be sent instead.
- If this is a problem, it's recommended to use async_flush() before freeing or re-using the buffer.
- When using MSG_ZERCOCOPY, the buffer memory will be locked, so it may require a larger amount than usually available to non-root user.
- If the required amount of locked memory is not available, it falls-back to buffer copying behavior, and synchronous sending.

---
 include/io/channel-socket.h |   2 +
 include/io/channel.h        |   1 +
 io/channel-socket.c         | 176 ++++++++++++++++++++++++++++++++++--
 3 files changed, 169 insertions(+), 10 deletions(-)
include/io/channel-socket.h +2 -0
index e747e63514..4d1be0637a 100644
@@ -47,6 +47,8 @@ struct QIOChannelSocket {
     socklen_t localAddrLen;
     struct sockaddr_storage remoteAddr;
     socklen_t remoteAddrLen;
+    ssize_t async_queued;
+    ssize_t async_sent;
 };
 
 
include/io/channel.h +1 -0
index 74f2e3ae8a..611bb2ea26 100644
@@ -31,6 +31,7 @@ OBJECT_DECLARE_TYPE(QIOChannel, QIOChannelClass,
 
 
 #define QIO_CHANNEL_ERR_BLOCK -2
+#define QIO_CHANNEL_ERR_NOBUFS -3
 
 typedef enum QIOChannelFeature QIOChannelFeature;
 
io/channel-socket.c +19 -0
index 606ec97cf7..c67832d0bb 100644
@@ -26,9 +26,23 @@
 #include "io/channel-watch.h"
 #include "trace.h"
 #include "qapi/clone-visitor.h"
+#ifdef CONFIG_LINUX
+#include <linux/errqueue.h>
+#include <poll.h>
+#endif
 
 #define SOCKET_MAX_FDS 16
 
+static ssize_t qio_channel_socket_async_writev(QIOChannel *ioc,
+                                               const struct iovec *iov,
+                                               size_t niov,
+                                               int *fds,
+                                               size_t nfds,
+                                               Error **errp);
+
+static void qio_channel_socket_async_flush(QIOChannel *ioc,
+                                           Error **errp);
+
 SocketAddress *
 qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
                                      Error **errp)
@@ -55,6 +69,8 @@ qio_channel_socket_new(void)
 
     sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
     sioc->fd = -1;
+    sioc->async_queued = 0;
+    sioc->async_sent = 0;
 
     ioc = QIO_CHANNEL(sioc);
     qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
@@ -140,6 +156,7 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
                                     Error **errp)
 {
     int fd;
+    int ret, v = 1;
 
     trace_qio_channel_socket_connect_sync(ioc, addr);
     fd = socket_connect(addr, errp);
@@ -154,6 +171,19 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
         return -1;
     }
 
+#ifdef CONFIG_LINUX
+    if (addr->type != SOCKET_ADDRESS_TYPE_INET) {

io/channel-socket.c int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, · line 185

Quoted 10 lines
io/channel-socket.c +10 -0
+        return 0;
+    }
+
+    ret = qemu_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
+    if (ret >= 0) {
+        QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+        klass->io_async_writev = qio_channel_socket_async_writev;
+        klass->io_async_flush = qio_channel_socket_async_flush;
+    }
+#endif

This is not write - the async APIs should not be tied 1:1 to ZEROCOPY usage - we should have them take a flag to request ZEROCOPY behaviour.

Quoted 74 lines
io/channel-socket.c +40 -10
+
     return 0;
 }
 
@@ -520,12 +550,13 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
     return ret;
 }
 
-static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
-                                         const struct iovec *iov,
-                                         size_t niov,
-                                         int *fds,
-                                         size_t nfds,
-                                         Error **errp)
+static ssize_t __qio_channel_socket_writev(QIOChannel *ioc,
+                                           const struct iovec *iov,
+                                           size_t niov,
+                                           int *fds,
+                                           size_t nfds,
+                                           int flags,
+                                           Error **errp)
 {
     QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
     ssize_t ret;
@@ -558,20 +589,145 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
     }
 
  retry:
-    ret = sendmsg(sioc->fd, &msg, 0);
+    ret = sendmsg(sioc->fd, &msg, flags);
     if (ret <= 0) {
-        if (errno == EAGAIN) {
+        switch (errno) {
+        case EAGAIN:
             return QIO_CHANNEL_ERR_BLOCK;
-        }
-        if (errno == EINTR) {
+        case EINTR:
             goto retry;
+        case ENOBUFS:
+            return QIO_CHANNEL_ERR_NOBUFS;
         }
+
         error_setg_errno(errp, errno,
                          "Unable to write to socket");
         return -1;
     }
     return ret;
 }
+
+static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
+                                         const struct iovec *iov,
+                                         size_t niov,
+                                         int *fds,
+                                         size_t nfds,
+                                         Error **errp)
+{
+    return __qio_channel_socket_writev(ioc, iov, niov, fds, nfds, 0, errp);
+}
+
+static ssize_t qio_channel_socket_async_writev(QIOChannel *ioc,
+                                               const struct iovec *iov,
+                                               size_t niov,
+                                               int *fds,
+                                               size_t nfds,
+                                               Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    ssize_t ret;
+
+    sioc->async_queued++;
+
+    ret = __qio_channel_socket_writev(ioc, iov, niov, fds, nfds, MSG_ZEROCOPY,
+                                       errp);

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 643

Quoted 10 lines
io/channel-socket.c +10 -0
+    if (ret == QIO_CHANNEL_ERR_NOBUFS) {
+        /*
+         * Not enough locked memory available to the process.
+         * Fallback to default sync callback.
+         */
+
+        if (errp && *errp) {
+            warn_reportf_err(*errp,
+                             "Process can't lock enough memory for using MSG_ZEROCOPY,"
+                             "falling back to non-zerocopy");

This is not nice as it hides what is likely mis-configuration error. If someone asked for zerocopy, we should honour that or report an error back.

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 648

Quoted 5 lines
io/channel-socket.c +5 -0
+        }
+
+        QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+        klass->io_async_writev = NULL;
+        klass->io_async_flush = NULL;

Clearing the flush callback is wrong. We might have pending async writes that haven't been processed that, and the lack of buffers may be a transient problem just caused by a backlog of writes.

Quoted 8 lines
io/channel-socket.c +8 -0
+
+        /* Re-send current buffer */
+        ret = qio_channel_socket_writev(ioc, iov, niov, fds, nfds, errp);
+    }
+
+    return ret;
+}
+

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 666

Quoted 10 lines
io/channel-socket.c +10 -0
+
+static void qio_channel_socket_async_flush(QIOChannel *ioc,
+                                           Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    struct msghdr msg = {};
+    struct pollfd pfd;
+    struct sock_extended_err *serr;
+    struct cmsghdr *cm;
+    char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];

Err sizeof(int) * SOCKET_MAX_FDS doesn't look right. This
buffer needs to hold 'struct sock_extended_err' instances, not 'int', and SOCKET_MAX_FDS is an unrelated limit.

Quoted 9 lines
io/channel-socket.c +9 -0
+    int ret;
+
+    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
+    msg.msg_control = control;
+    msg.msg_controllen = sizeof(control);
+
+    while (sioc->async_sent < sioc->async_queued) {
+        ret = recvmsg(sioc->fd, &msg, MSG_ERRQUEUE);
+        if (ret < 0) {

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 685

Quoted 10 lines
io/channel-socket.c +10 -0
+            if (errno == EAGAIN) {
+                /* Nothing on errqueue, wait */
+                pfd.fd = sioc->fd;
+                pfd.events = 0;
+                ret = poll(&pfd, 1, 250);
+                if (ret == 0) {
+                    /*
+                     * Timeout : After 250ms without receiving any zerocopy
+                     * notification, consider all data as sent.
+                     */

This feels very dubious indeed. If some caller needs a guarantee that the data was successfully sent, merely waiting 250ms is not going to be reliable enough.

A regular non-async + non-zerocopy right will wait as long as is needed unless SO_SNDTIMEO has been set on the socket.

At the very least the timeout ought to be a parameter passed in, and the return value should indicate whether it timed out, or report how many pending writes still aren't processed, so the caller can decide whether to call flush again.

Quoted 38 lines
io/channel-socket.c +38 -0
+                    break;
+                } else if (ret < 0 ||
+                           (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))) {
+                    error_setg_errno(errp, errno,
+                                     "Poll error");
+                    break;
+                } else {
+                    continue;
+                }
+            }
+            if (errno == EINTR) {
+                continue;
+            }
+
+            error_setg_errno(errp, errno,
+                             "Unable to read errqueue");
+            break;
+        }
+
+        cm = CMSG_FIRSTHDR(&msg);
+        if (cm->cmsg_level != SOL_IP &&
+            cm->cmsg_type != IP_RECVERR) {
+            error_setg_errno(errp, EPROTOTYPE,
+                             "Wrong cmsg in errqueue");
+            break;
+        }
+
+        serr = (void *) CMSG_DATA(cm);
+        if (serr->ee_errno != SO_EE_ORIGIN_NONE) {
+            error_setg_errno(errp, serr->ee_errno,
+                             "Error on socket");
+            break;
+        }
+        if (serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
+            error_setg_errno(errp, serr->ee_origin,
+                             "Error not from zerocopy");
+            break;
+        }

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 733

Quoted 10 lines
io/channel-socket.c +7 -0
+
+        /* No errors, count sent ids*/
+        sioc->async_sent += serr->ee_data - serr->ee_info + 1;
+    }
+}
+
+
 #else /* WIN32 */
 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
                                         const struct iovec *iov,

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#9 lore raw

Re: [PATCH v3 2/3] QIOChannelSocket: Implement io_async_write & io_async_flush

Hello Daniel,

On Fri, Sep 24, 2021 at 2:38 PM Daniel P. Berrangé <[email protected]> wrote:
[...]

Quoted 4 lines
Quoted 4 lines
io/channel-socket.c +0 -0
@@ -154,6 +171,19 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
         return -1;
     }
 
Quoted 15 lines

io/channel-socket.c int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, · line 185

Quoted 12 lines
io/channel-socket.c +12 -0
+#ifdef CONFIG_LINUX
+    if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
+        return 0;
+    }
+
+    ret = qemu_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
+    if (ret >= 0) {
+        QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+        klass->io_async_writev = qio_channel_socket_async_writev;
+        klass->io_async_flush = qio_channel_socket_async_flush;
+    }
+#endif

This is not write - the async APIs should not be tied 1:1 to ZEROCOPY usage - we should have them take a flag to request ZEROCOPY behaviour.

I agree, but I am not aware of how to do asynchronous send in a socket without MSG_ZEROCOPY.

I mean, I know of the non-blocking send, but I am not sure how it checks if everything was sent (i.e. the flush part). Would it also be using the ERRQUEUE for that?

What would you suggest?

Quoted 81 lines
Quoted 81 lines
io/channel-socket.c +46 -10
+
     return 0;
 }
 
@@ -520,12 +550,13 @@ static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
     return ret;
 }
 
-static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
-                                         const struct iovec *iov,
-                                         size_t niov,
-                                         int *fds,
-                                         size_t nfds,
-                                         Error **errp)
+static ssize_t __qio_channel_socket_writev(QIOChannel *ioc,
+                                           const struct iovec *iov,
+                                           size_t niov,
+                                           int *fds,
+                                           size_t nfds,
+                                           int flags,
+                                           Error **errp)
 {
     QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
     ssize_t ret;
@@ -558,20 +589,145 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
     }
 
  retry:
-    ret = sendmsg(sioc->fd, &msg, 0);
+    ret = sendmsg(sioc->fd, &msg, flags);
     if (ret <= 0) {
-        if (errno == EAGAIN) {
+        switch (errno) {
+        case EAGAIN:
             return QIO_CHANNEL_ERR_BLOCK;
-        }
-        if (errno == EINTR) {
+        case EINTR:
             goto retry;
+        case ENOBUFS:
+            return QIO_CHANNEL_ERR_NOBUFS;
         }
+
         error_setg_errno(errp, errno,
                          "Unable to write to socket");
         return -1;
     }
     return ret;
 }
+
+static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
+                                         const struct iovec *iov,
+                                         size_t niov,
+                                         int *fds,
+                                         size_t nfds,
+                                         Error **errp)
+{
+    return __qio_channel_socket_writev(ioc, iov, niov, fds, nfds, 0, errp);
+}
+
+static ssize_t qio_channel_socket_async_writev(QIOChannel *ioc,
+                                               const struct iovec *iov,
+                                               size_t niov,
+                                               int *fds,
+                                               size_t nfds,
+                                               Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    ssize_t ret;
+
+    sioc->async_queued++;
+
+    ret = __qio_channel_socket_writev(ioc, iov, niov, fds, nfds, MSG_ZEROCOPY,
+                                       errp);
+    if (ret == QIO_CHANNEL_ERR_NOBUFS) {
+        /*
+         * Not enough locked memory available to the process.
+         * Fallback to default sync callback.
+         */
+
Quoted 8 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 643

Quoted 4 lines
io/channel-socket.c +4 -0
+        if (errp && *errp) {
+            warn_reportf_err(*errp,
+                             "Process can't lock enough memory for using MSG_ZEROCOPY,"
+                             "falling back to non-zerocopy");

This is not nice as it hides what is likely mis-configuration error. If someone asked for zerocopy, we should honour that or report an error back.

Yeah, that makes sense to me.
Thank you for pointing that out.

Quoted 10 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 648

Quoted 6 lines
io/channel-socket.c +5 -0
+        }
+
+        QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+        klass->io_async_writev = NULL;
+        klass->io_async_flush = NULL;

Clearing the flush callback is wrong. We might have pending async writes that haven't been processed that, and the lack of buffers may be a transient problem just caused by a backlog of writes.

I agree that it's wrong.
But I think it will be deprecated anyway if we implement ZEROCOPY as a feature instead of async, and avoid doing fallback to writev when async is not available.

Quoted 15 lines
Quoted 15 lines
io/channel-socket.c +14 -0
+
+        /* Re-send current buffer */
+        ret = qio_channel_socket_writev(ioc, iov, niov, fds, nfds, errp);
+    }
+
+    return ret;
+}
+
+
+static void qio_channel_socket_async_flush(QIOChannel *ioc,
+                                           Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    struct msghdr msg = {};
Quoted 8 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 666

Quoted 4 lines
io/channel-socket.c +4 -0
+    struct pollfd pfd;
+    struct sock_extended_err *serr;
+    struct cmsghdr *cm;
+    char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];

Err sizeof(int) * SOCKET_MAX_FDS doesn't look right. This
buffer needs to hold 'struct sock_extended_err' instances, not 'int', and SOCKET_MAX_FDS is an unrelated limit.

That was a bad mistake,
I got it by reusing code from above functions, and it got past the review I did before sending the patch.
Sorry about that.

Quoted 16 lines
Quoted 16 lines
io/channel-socket.c +15 -0
+    int ret;
+
+    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
+    msg.msg_control = control;
+    msg.msg_controllen = sizeof(control);
+
+    while (sioc->async_sent < sioc->async_queued) {
+        ret = recvmsg(sioc->fd, &msg, MSG_ERRQUEUE);
+        if (ret < 0) {
+            if (errno == EAGAIN) {
+                /* Nothing on errqueue, wait */
+                pfd.fd = sioc->fd;
+                pfd.events = 0;
+                ret = poll(&pfd, 1, 250);
+                if (ret == 0) {
Quoted 8 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 685

Quoted 4 lines
io/channel-socket.c +4 -0
+                    /*
+                     * Timeout : After 250ms without receiving any zerocopy
+                     * notification, consider all data as sent.
+                     */

This feels very dubious indeed. If some caller needs a guarantee that the data was successfully sent, merely waiting 250ms is not going to be reliable enough.

That makes sense.
I added this part because at some point in debugging I got an infinite loop in this part
(I think it was somehow missing some notifications).

Quoted 3 lines

A regular non-async + non-zerocopy right will wait as long as is needed unless SO_SNDTIMEO has been set on the socket.

So It would be ok to let it loop here?
Maybe the timeout could be only enough to keep the cpu from getting stuck in here.

Quoted 5 lines

At the very least the timeout ought to be a parameter passed in, and the return value should indicate whether it timed out, or report how many pending writes still aren't processed, so the caller can decide whether to call flush again.

That is also makes sense to me.

Quoted 53 lines
Quoted 39 lines
io/channel-socket.c +38 -0
+                    break;
+                } else if (ret < 0 ||
+                           (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))) {
+                    error_setg_errno(errp, errno,
+                                     "Poll error");
+                    break;
+                } else {
+                    continue;
+                }
+            }
+            if (errno == EINTR) {
+                continue;
+            }
+
+            error_setg_errno(errp, errno,
+                             "Unable to read errqueue");
+            break;
+        }
+
+        cm = CMSG_FIRSTHDR(&msg);
+        if (cm->cmsg_level != SOL_IP &&
+            cm->cmsg_type != IP_RECVERR) {
+            error_setg_errno(errp, EPROTOTYPE,
+                             "Wrong cmsg in errqueue");
+            break;
+        }
+
+        serr = (void *) CMSG_DATA(cm);
+        if (serr->ee_errno != SO_EE_ORIGIN_NONE) {
+            error_setg_errno(errp, serr->ee_errno,
+                             "Error on socket");
+            break;
+        }
+        if (serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
+            error_setg_errno(errp, serr->ee_origin,
+                             "Error not from zerocopy");
+            break;
+        }

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 733

Quoted 10 lines
io/channel-socket.c +7 -0
+
+        /* No errors, count sent ids*/
+        sioc->async_sent += serr->ee_data - serr->ee_info + 1;
+    }
+}
+
+
 #else /* WIN32 */
 static ssize_t qio_channel_socket_readv(QIOChannel *ioc,
                                         const struct iovec *iov,

Regards,
Daniel
--

Quoted 4 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

Thanks Daniel,

Best regards,
Leonardo Bras

#10 lore raw

Re: [PATCH v3 2/3] QIOChannelSocket: Implement io_async_write & io_async_flush

Quoted 24 lines · Leonardo Bras Soares Passos

Hello Daniel,

On Fri, Sep 24, 2021 at 2:38 PM Daniel P. Berrangé <[email protected]> wrote:
[...]

Quoted 4 lines
Quoted 4 lines
io/channel-socket.c +0 -0
@@ -154,6 +171,19 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
         return -1;
     }
 
Quoted 15 lines

io/channel-socket.c int qio_channel_socket_connect_sync(QIOChannelSocket *ioc, · line 185

Quoted 12 lines
io/channel-socket.c +12 -0
+#ifdef CONFIG_LINUX
+    if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
+        return 0;
+    }
+
+    ret = qemu_setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
+    if (ret >= 0) {
+        QIOChannelClass *klass = QIO_CHANNEL_GET_CLASS(ioc);
+        klass->io_async_writev = qio_channel_socket_async_writev;
+        klass->io_async_flush = qio_channel_socket_async_flush;
+    }
+#endif

This is not write - the async APIs should not be tied 1:1 to ZEROCOPY usage - we should have them take a flag to request ZEROCOPY behaviour.

Quoted 8 lines

I agree, but I am not aware of how to do asynchronous send in a socket without MSG_ZEROCOPY.

I mean, I know of the non-blocking send, but I am not sure how it checks if everything was sent (i.e. the flush part). Would it also be using the ERRQUEUE for that?

What would you suggest?

Yeah, there isn't any really. I guess I'm anticipating a future that probably won't exist. Lets just call the callbacks 'io_write_zerocopy' and 'io_flush_zerocopy' and ignore the flag.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#11 lore raw

Re: [PATCH v3 2/3] QIOChannelSocket: Implement io_async_write & io_async_flush

In reply to Leonardo Bras
Quoted 19 lines · Leonardo Bras
io/channel-socket.c +19 -0
+static void qio_channel_socket_async_flush(QIOChannel *ioc,
+                                           Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    struct msghdr msg = {};
+    struct pollfd pfd;
+    struct sock_extended_err *serr;
+    struct cmsghdr *cm;
+    char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
+    int ret;
+
+    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
+    msg.msg_control = control;
+    msg.msg_controllen = sizeof(control);
+
+    while (sioc->async_sent < sioc->async_queued) {
+        ret = recvmsg(sioc->fd, &msg, MSG_ERRQUEUE);
+        if (ret < 0) {
+            if (errno == EAGAIN) {

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 686

Quoted 10 lines
io/channel-socket.c +10 -0
+                /* Nothing on errqueue, wait */
+                pfd.fd = sioc->fd;
+                pfd.events = 0;
+                ret = poll(&pfd, 1, 250);
+                if (ret == 0) {
+                    /*
+                     * Timeout : After 250ms without receiving any zerocopy
+                     * notification, consider all data as sent.
+                     */
+                    break;

After a timeout, we'll break the while loop and continue parsing an invalid msg [1]. Is that what we want?

Also, I don't think we can return the flush() even if timed out - iiuc we should keep polling until we have async_sent==async_queued. It depends on how we define flush(): if it's "when this function returns all data is sent", then we should keep polling, and afaict this is what we want here right now.

Quoted 9 lines
io/channel-socket.c +9 -0
+                } else if (ret < 0 ||
+                           (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))) {
+                    error_setg_errno(errp, errno,
+                                     "Poll error");
+                    break;
+                } else {
+                    continue;
+                }
+            }

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 705

Quoted 10 lines
io/channel-socket.c +10 -0
+            if (errno == EINTR) {
+                continue;
+            }
+
+            error_setg_errno(errp, errno,
+                             "Unable to read errqueue");
+            break;
+        }
+
+        cm = CMSG_FIRSTHDR(&msg);

[1]

Quoted 6 lines
io/channel-socket.c +6 -0
+        if (cm->cmsg_level != SOL_IP &&
+            cm->cmsg_type != IP_RECVERR) {
+            error_setg_errno(errp, EPROTOTYPE,
+                             "Wrong cmsg in errqueue");
+            break;
+        }
Signature · 1 line
Peter Xu
#12 lore raw

Re: [PATCH v3 2/3] QIOChannelSocket: Implement io_async_write & io_async_flush

Quoted 26 lines · Peter Xu
Quoted 24 lines · Leonardo Bras
io/channel-socket.c +24 -0
+static void qio_channel_socket_async_flush(QIOChannel *ioc,
+                                           Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    struct msghdr msg = {};
+    struct pollfd pfd;
+    struct sock_extended_err *serr;
+    struct cmsghdr *cm;
+    char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
+    int ret;
+
+    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
+    msg.msg_control = control;
+    msg.msg_controllen = sizeof(control);
+
+    while (sioc->async_sent < sioc->async_queued) {
+        ret = recvmsg(sioc->fd, &msg, MSG_ERRQUEUE);
+        if (ret < 0) {
+            if (errno == EAGAIN) {
+                /* Nothing on errqueue, wait */
+                pfd.fd = sioc->fd;
+                pfd.events = 0;
+                ret = poll(&pfd, 1, 250);
+                if (ret == 0) {
Quoted 8 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 686

Quoted 5 lines
io/channel-socket.c +5 -0
+                    /*
+                     * Timeout : After 250ms without receiving any zerocopy
+                     * notification, consider all data as sent.
+                     */
+                    break;

After a timeout, we'll break the while loop and continue parsing an invalid msg [1]. Is that what we want?

No, the point here was returning from flush if this (long) timeout happened, as in
"if asso long has passed, there must be no pending send", which I agree is quite bad,
but it was all I could think to avoid an infinite loop here if something goes wrong.

Quoted 5 lines

Also, I don't think we can return the flush() even if timed out - iiuc we should keep polling until we have async_sent==async_queued. It depends on how we define flush(): if it's "when this function returns all data is sent", then we should keep polling, and afaict this is what we want here right now.

Yeah, I agree.
That is the correct way to deal with this.

Quoted 30 lines
Quoted 10 lines
io/channel-socket.c +9 -0
+                } else if (ret < 0 ||
+                           (pfd.revents & (POLLERR | POLLHUP | POLLNVAL))) {
+                    error_setg_errno(errp, errno,
+                                     "Poll error");
+                    break;
+                } else {
+                    continue;
+                }
+            }

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 705

Quoted 10 lines
io/channel-socket.c +10 -0
+            if (errno == EINTR) {
+                continue;
+            }
+
+            error_setg_errno(errp, errno,
+                             "Unable to read errqueue");
+            break;
+        }
+
+        cm = CMSG_FIRSTHDR(&msg);

[1]

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 711

Quoted 6 lines
io/channel-socket.c +6 -0
+        if (cm->cmsg_level != SOL_IP &&
+            cm->cmsg_type != IP_RECVERR) {
+            error_setg_errno(errp, EPROTOTYPE,
+                             "Wrong cmsg in errqueue");
+            break;
+        }
Quoted 3 lines

--
Peter Xu

Best regards,
Leonardo

#13 lore raw

Re: [PATCH v3 2/3] QIOChannelSocket: Implement io_async_write & io_async_flush

Quoted 36 lines · Leonardo Bras Soares Passos
Quoted 26 lines · Peter Xu
Quoted 24 lines · Leonardo Bras
io/channel-socket.c +24 -0
+static void qio_channel_socket_async_flush(QIOChannel *ioc,
+                                           Error **errp)
+{
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
+    struct msghdr msg = {};
+    struct pollfd pfd;
+    struct sock_extended_err *serr;
+    struct cmsghdr *cm;
+    char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
+    int ret;
+
+    memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
+    msg.msg_control = control;
+    msg.msg_controllen = sizeof(control);
+
+    while (sioc->async_sent < sioc->async_queued) {
+        ret = recvmsg(sioc->fd, &msg, MSG_ERRQUEUE);
+        if (ret < 0) {
+            if (errno == EAGAIN) {
+                /* Nothing on errqueue, wait */
+                pfd.fd = sioc->fd;
+                pfd.events = 0;
+                ret = poll(&pfd, 1, 250);
+                if (ret == 0) {
Quoted 8 lines

io/channel-socket.c static ssize_t qio_channel_socket_writev(QIOChannel *ioc, · line 686

Quoted 5 lines
io/channel-socket.c +5 -0
+                    /*
+                     * Timeout : After 250ms without receiving any zerocopy
+                     * notification, consider all data as sent.
+                     */
+                    break;

After a timeout, we'll break the while loop and continue parsing an invalid msg [1]. Is that what we want?

Quoted 6 lines

No, the point here was returning from flush if this (long) timeout happened, as in
"if asso long has passed, there must be no pending send", which I agree is quite bad,
but it was all I could think to avoid an infinite loop here if something goes wrong.

IMHO it's the same when we write() to a socket but the buffer is always full, we'll simply block there until it has some space. I don't know what we can do here besides infinite loop on the timeout - we shouldn't eat the cpu all, but we should still wait?

Signature · 1 line
Peter Xu
#14 lore raw

PATCH v3 3/3 multifd: Send using asynchronous write on nocomp to send RAM pages.

In reply to Leonardo Bras

Change multifd nocomp version to use asynchronous write for RAM pages, and benefit of MSG_ZEROCOPY when it's available.

The asynchronous flush happens on cleanup only, before destroying the QIOChannel.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

---
 migration/multifd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
migration/multifd.c +2 -1
index 377da78f5b..d247207a0a 100644
@@ -105,7 +105,7 @@ static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
  */
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
 {
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
+    return qio_channel_async_writev_all(p->c, p->pages->iov, used, errp);
 }
 
 /**
@@ -546,6 +546,7 @@ void multifd_save_cleanup(void)
         MultiFDSendParams *p = &multifd_send_state->params[i];
         Error *local_err = NULL;
 
+        qio_channel_async_flush(p->c, NULL);
         socket_send_channel_destroy(p->c);
         p->c = NULL;
         qemu_mutex_destroy(&p->mutex);
Signature · 1 line
2.33.0
#15 lore raw

Re: [PATCH v3 3/3] multifd: Send using asynchronous write on nocomp to send RAM pages.

Quoted 20 lines · Leonardo Bras

Change multifd nocomp version to use asynchronous write for RAM pages, and benefit of MSG_ZEROCOPY when it's available.

The asynchronous flush happens on cleanup only, before destroying the QIOChannel.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

---
 migration/multifd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
migration/multifd.c +0 -0
index 377da78f5b..d247207a0a 100644

migration/multifd.c static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used, · line 109

Quoted 6 lines
migration/multifd.c +1 -1
@@ -105,7 +105,7 @@ static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
  */
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
 {
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
+    return qio_channel_async_writev_all(p->c, p->pages->iov, used, errp);
 }

This needs to be made conditional so zeroopy is only used if rquested by the mgmt app, and it isn't going to work in all cases (eg TLS) so silently enabling it is not good.

migration/multifd.c void multifd_save_cleanup(void) · line 552

Quoted 10 lines
migration/multifd.c +1 -0
 /**
@@ -546,6 +546,7 @@ void multifd_save_cleanup(void)
         MultiFDSendParams *p = &multifd_send_state->params[i];
         Error *local_err = NULL;
 
+        qio_channel_async_flush(p->c, NULL);
         socket_send_channel_destroy(p->c);
         p->c = NULL;
         qemu_mutex_destroy(&p->mutex);

This isn't reliable beucase qio_channel_async_flush will return early even if not everything is flushed.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#16 lore raw

Re: [PATCH v3 3/3] multifd: Send using asynchronous write on nocomp to send RAM pages.

Quoted 11 lines · Daniel P. Berrangé

migration/multifd.c void multifd_save_cleanup(void) · line 552

Quoted 8 lines
migration/multifd.c +1 -0
@@ -546,6 +546,7 @@ void multifd_save_cleanup(void)
         MultiFDSendParams *p = &multifd_send_state->params[i];
         Error *local_err = NULL;
 
+        qio_channel_async_flush(p->c, NULL);
         socket_send_channel_destroy(p->c);
         p->c = NULL;
         qemu_mutex_destroy(&p->mutex);

This isn't reliable beucase qio_channel_async_flush will return early even if not everything is flushed.

Right, though I think the problem is in patch 2 where we should make sure flush() will only return if all data sent.

And at the meantime we may also want to move it to before we send the devices states for both pre/post copy. multifd_save_cleanup() is called only until migration completed and we're cleaning stuffs, I'm afraid it's too late, so potentially the device states can arrive and dest vm running without the latest src VM memories.

Signature · 1 line
Peter Xu
#17 lore raw

Re: [PATCH v3 3/3] multifd: Send using asynchronous write on nocomp to send RAM pages.

Quoted 14 lines · Peter Xu
Quoted 11 lines · Daniel P. Berrangé

migration/multifd.c void multifd_save_cleanup(void) · line 552

Quoted 8 lines
migration/multifd.c +1 -0
@@ -546,6 +546,7 @@ void multifd_save_cleanup(void)
         MultiFDSendParams *p = &multifd_send_state->params[i];
         Error *local_err = NULL;
 
+        qio_channel_async_flush(p->c, NULL);
         socket_send_channel_destroy(p->c);
         p->c = NULL;
         qemu_mutex_destroy(&p->mutex);

This isn't reliable beucase qio_channel_async_flush will return early even if not everything is flushed.

Quoted 8 lines

Right, though I think the problem is in patch 2 where we should make sure flush() will only return if all data sent.

And at the meantime we may also want to move it to before we send the devices states for both pre/post copy. multifd_save_cleanup() is called only until migration completed and we're cleaning stuffs, I'm afraid it's too late, so potentially the device states can arrive and dest vm running without the latest src VM memories.

Thanks for that Peter!
I had some trouble sorting out when to flush, and I think I still missed this.

I will have thet improved for v3.

Quoted 4 lines

--
Peter Xu

Best regards,
Leonardo

#18 lore raw

Re: [PATCH v3 3/3] multifd: Send using asynchronous write on nocomp to send RAM pages.

In reply to Daniel P. Berrangé
Quoted 17 lines · Daniel P. Berrangé
Quoted 15 lines · Leonardo Bras

Change multifd nocomp version to use asynchronous write for RAM pages, and benefit of MSG_ZEROCOPY when it's available.

The asynchronous flush happens on cleanup only, before destroying the QIOChannel.

This will work fine on RAM migration because the RAM pages are not usually freed, and there is no problem on changing the pages content between async_send() and the actual sending of the buffer, because this change will dirty the page and cause it to be re-sent on a next iteration anyway.

---
 migration/multifd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 15 lines

migration/multifd.c static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used, · line 109

Quoted 11 lines
migration/multifd.c +1 -1
index 377da78f5b..d247207a0a 100644
@@ -105,7 +105,7 @@ static int nocomp_send_prepare(MultiFDSendParams *p, uint32_t used,
  */
 static int nocomp_send_write(MultiFDSendParams *p, uint32_t used, Error **errp)
 {
-    return qio_channel_writev_all(p->c, p->pages->iov, used, errp);
+    return qio_channel_async_writev_all(p->c, p->pages->iov, used, errp);
 }

This needs to be made conditional so zeroopy is only used if rquested by the mgmt app, and it isn't going to work in all cases (eg TLS) so silently enabling it is not good.

I agree, that seems a better approach.

Quoted 7 lines
Quoted 7 lines
migration/multifd.c +0 -0
 /**
@@ -546,6 +546,7 @@ void multifd_save_cleanup(void)
         MultiFDSendParams *p = &multifd_send_state->params[i];
         Error *local_err = NULL;
 
Quoted 7 lines

migration/multifd.c void multifd_save_cleanup(void) · line 552

Quoted 4 lines
migration/multifd.c +1 -0
+        qio_channel_async_flush(p->c, NULL);
         socket_send_channel_destroy(p->c);
         p->c = NULL;
         qemu_mutex_destroy(&p->mutex);

This isn't reliable beucase qio_channel_async_flush will return early even if not everything is flushed.

Yeah, I need to make sure qio_channel_async_flush will only return after everything is sent, or at least return the number of missing requests.

Quoted 8 lines

Regards,
Daniel
--

|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

Best regards,
Leonardo

#19 lore raw
In reply to Leonardo Bras
Quoted 8 lines · Leonardo Bras

This patch series intends to enable MSG_ZEROCOPY in QIOChannel, and make use of it for multifd migration performance improvement.

Patch #1 creates new callbacks for QIOChannel, allowing the implementation of asynchronous writing.

Patch #2 implements async_write and async_flush on QIOChannelSocket,

Quoted 8 lines

Patch #3 Makes use of async_write + async_flush to enable MSG_ZEROCOPY for migration using multifd nocomp.

Results:
So far, the resource usage of __sys_sendmsg() reduced 15 times, and the overall migration took 13-18% less time, based in synthetic workload.

The objective is to reduce migration time in hosts with heavy cpu usage.

My previous memory is that we'll add a capability bit for migration, so it'll not be enabled until user specified it. Plan to do it in the next version?

It'll still be okay if you want to separate the work of (1) qio channel zero-copy support on sockets, and (2) apply zero copy to migration, then that can be done in part 2. Your call. :)

Thanks,

Signature · 1 line
Peter Xu
#20 lore raw

Hello Peter,

Quoted 13 lines · Peter Xu
Quoted 11 lines · Leonardo Bras

This patch series intends to enable MSG_ZEROCOPY in QIOChannel, and make use of it for multifd migration performance improvement.

Patch #1 creates new callbacks for QIOChannel, allowing the implementation of asynchronous writing.

Patch #2 implements async_write and async_flush on QIOChannelSocket,

Patch #3 Makes use of async_write + async_flush to enable MSG_ZEROCOPY for migration using multifd nocomp.

Quoted 8 lines
Quoted 5 lines

Results:
So far, the resource usage of __sys_sendmsg() reduced 15 times, and the overall migration took 13-18% less time, based in synthetic workload.

The objective is to reduce migration time in hosts with heavy cpu usage.

My previous memory is that we'll add a capability bit for migration, so it'll not be enabled until user specified it. Plan to do it in the next version?

You mean like I did in pre-V1, enabling ZEROCOPY for multifd it in QMP? Or is this something else?

Quoted 4 lines

It'll still be okay if you want to separate the work of (1) qio channel zero-copy support on sockets, and (2) apply zero copy to migration, then that can be done in part 2. Your call. :)

Ok :)

Quoted 6 lines

Thanks,

--
Peter Xu

Thank you Peter,

Best regards,
Leonardo

#21 lore raw
Quoted 2 lines · Leonardo Bras Soares Passos

You mean like I did in pre-V1, enabling ZEROCOPY for multifd it in QMP? Or is this something else?

I mean any way to still be able to turn zerocopy off? E.g. when the user has no privilege on mlock. Thanks,

Signature · 1 line
Peter Xu
#22 lore raw
Quoted 11 lines · Peter Xu
Quoted 2 lines · Leonardo Bras Soares Passos

You mean like I did in pre-V1, enabling ZEROCOPY for multifd it in QMP? Or is this something else?

I mean any way to still be able to turn zerocopy off? E.g. when the user has no privilege on mlock. Thanks,

--
Peter Xu

Yeah, that makes sense in the new approach of failing when zerocopy is not possible.
I will make sure to do the in v3.

Best regards,
Leonardo