Blame SOURCES/kvm-QIOChannelSocket-Implement-io_writev-zero-copy-flag-.patch

1be5c7
From 5fd7af93a06adaddbae719aabbaf912159f4fb28 Mon Sep 17 00:00:00 2001
1be5c7
From: Leonardo Bras <leobras@redhat.com>
1be5c7
Date: Wed, 18 May 2022 02:52:25 -0300
1be5c7
Subject: [PATCH 18/37] QIOChannelSocket: Implement io_writev zero copy flag &
1be5c7
 io_flush for CONFIG_LINUX
1be5c7
MIME-Version: 1.0
1be5c7
Content-Type: text/plain; charset=UTF-8
1be5c7
Content-Transfer-Encoding: 8bit
1be5c7
1be5c7
RH-Author: Leonardo Brás <leobras@redhat.com>
1be5c7
RH-MergeRequest: 191: MSG_ZEROCOPY + Multifd @ rhel8.7
1be5c7
RH-Commit: [18/26] 6f65c8c879a5df57213b541d58285b65178f8547
1be5c7
RH-Bugzilla: 2072049
1be5c7
RH-Acked-by: Peter Xu <peterx@redhat.com>
1be5c7
RH-Acked-by: Daniel P. Berrangé <berrange@redhat.com>
1be5c7
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1be5c7
1be5c7
For CONFIG_LINUX, implement the new zero copy flag and the optional callback
1be5c7
io_flush on QIOChannelSocket, but enables it only when MSG_ZEROCOPY
1be5c7
feature is available in the host kernel, which is checked on
1be5c7
qio_channel_socket_connect_sync()
1be5c7
1be5c7
qio_channel_socket_flush() was implemented by counting how many times
1be5c7
sendmsg(...,MSG_ZEROCOPY) was successfully called, and then reading the
1be5c7
socket's error queue, in order to find how many of them finished sending.
1be5c7
Flush will loop until those counters are the same, or until some error occurs.
1be5c7
1be5c7
Notes on using writev() with QIO_CHANNEL_WRITE_FLAG_ZERO_COPY:
1be5c7
1: Buffer
1be5c7
- As MSG_ZEROCOPY tells the kernel to use the same user buffer to avoid copying,
1be5c7
some caution is necessary to avoid overwriting any buffer before it's sent.
1be5c7
If something like this happen, a newer version of the buffer may be sent instead.
1be5c7
- If this is a problem, it's recommended to call qio_channel_flush() before freeing
1be5c7
or re-using the buffer.
1be5c7
1be5c7
2: Locked memory
1be5c7
- When using MSG_ZERCOCOPY, the buffer memory will be locked after queued, and
1be5c7
unlocked after it's sent.
1be5c7
- Depending on the size of each buffer, and how often it's sent, it may require
1be5c7
a larger amount of locked memory than usually available to non-root user.
1be5c7
- If the required amount of locked memory is not available, writev_zero_copy
1be5c7
will return an error, which can abort an operation like migration,
1be5c7
- Because of this, when an user code wants to add zero copy as a feature, it
1be5c7
requires a mechanism to disable it, so it can still be accessible to less
1be5c7
privileged users.
1be5c7
1be5c7
Signed-off-by: Leonardo Bras <leobras@redhat.com>
1be5c7
Reviewed-by: Peter Xu <peterx@redhat.com>
1be5c7
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
1be5c7
Reviewed-by: Juan Quintela <quintela@redhat.com>
1be5c7
Message-Id: <20220513062836.965425-4-leobras@redhat.com>
1be5c7
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
1be5c7
(cherry picked from commit 2bc58ffc2926a4efdd03edfb5909861fefc68c3d)
1be5c7
Signed-off-by: Leonardo Bras <leobras@redhat.com>
1be5c7
---
1be5c7
 include/io/channel-socket.h |   2 +
1be5c7
 io/channel-socket.c         | 116 ++++++++++++++++++++++++++++++++++--
1be5c7
 2 files changed, 114 insertions(+), 4 deletions(-)
1be5c7
1be5c7
diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h
1be5c7
index e747e63514..513c428fe4 100644
1be5c7
--- a/include/io/channel-socket.h
1be5c7
+++ b/include/io/channel-socket.h
1be5c7
@@ -47,6 +47,8 @@ struct QIOChannelSocket {
1be5c7
     socklen_t localAddrLen;
1be5c7
     struct sockaddr_storage remoteAddr;
1be5c7
     socklen_t remoteAddrLen;
1be5c7
+    ssize_t zero_copy_queued;
1be5c7
+    ssize_t zero_copy_sent;
1be5c7
 };
1be5c7
 
1be5c7
 
1be5c7
diff --git a/io/channel-socket.c b/io/channel-socket.c
1be5c7
index bfbd64787e..38a46ba213 100644
1be5c7
--- a/io/channel-socket.c
1be5c7
+++ b/io/channel-socket.c
1be5c7
@@ -26,6 +26,14 @@
1be5c7
 #include "io/channel-watch.h"
1be5c7
 #include "trace.h"
1be5c7
 #include "qapi/clone-visitor.h"
1be5c7
+#ifdef CONFIG_LINUX
1be5c7
+#include <linux/errqueue.h>
1be5c7
+#include <sys/socket.h>
1be5c7
+
1be5c7
+#if (defined(MSG_ZEROCOPY) && defined(SO_ZEROCOPY))
1be5c7
+#define QEMU_MSG_ZEROCOPY
1be5c7
+#endif
1be5c7
+#endif
1be5c7
 
1be5c7
 #define SOCKET_MAX_FDS 16
1be5c7
 
1be5c7
@@ -55,6 +63,8 @@ qio_channel_socket_new(void)
1be5c7
 
1be5c7
     sioc = QIO_CHANNEL_SOCKET(object_new(TYPE_QIO_CHANNEL_SOCKET));
1be5c7
     sioc->fd = -1;
1be5c7
+    sioc->zero_copy_queued = 0;
1be5c7
+    sioc->zero_copy_sent = 0;
1be5c7
 
1be5c7
     ioc = QIO_CHANNEL(sioc);
1be5c7
     qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
1be5c7
@@ -154,6 +164,16 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
1be5c7
         return -1;
1be5c7
     }
1be5c7
 
1be5c7
+#ifdef QEMU_MSG_ZEROCOPY
1be5c7
+    int ret, v = 1;
1be5c7
+    ret = setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &v, sizeof(v));
1be5c7
+    if (ret == 0) {
1be5c7
+        /* Zero copy available on host */
1be5c7
+        qio_channel_set_feature(QIO_CHANNEL(ioc),
1be5c7
+                                QIO_CHANNEL_FEATURE_WRITE_ZERO_COPY);
1be5c7
+    }
1be5c7
+#endif
1be5c7
+
1be5c7
     return 0;
1be5c7
 }
1be5c7
 
1be5c7
@@ -534,6 +554,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
1be5c7
     char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)];
1be5c7
     size_t fdsize = sizeof(int) * nfds;
1be5c7
     struct cmsghdr *cmsg;
1be5c7
+    int sflags = 0;
1be5c7
 
1be5c7
     memset(control, 0, CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS));
1be5c7
 
1be5c7
@@ -558,15 +579,31 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
1be5c7
         memcpy(CMSG_DATA(cmsg), fds, fdsize);
1be5c7
     }
1be5c7
 
1be5c7
+#ifdef QEMU_MSG_ZEROCOPY
1be5c7
+    if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) {
1be5c7
+        sflags = MSG_ZEROCOPY;
1be5c7
+    }
1be5c7
+#endif
1be5c7
+
1be5c7
  retry:
1be5c7
-    ret = sendmsg(sioc->fd, &msg, 0);
1be5c7
+    ret = sendmsg(sioc->fd, &msg, sflags);
1be5c7
     if (ret <= 0) {
1be5c7
-        if (errno == EAGAIN) {
1be5c7
+        switch (errno) {
1be5c7
+        case EAGAIN:
1be5c7
             return QIO_CHANNEL_ERR_BLOCK;
1be5c7
-        }
1be5c7
-        if (errno == EINTR) {
1be5c7
+        case EINTR:
1be5c7
             goto retry;
1be5c7
+#ifdef QEMU_MSG_ZEROCOPY
1be5c7
+        case ENOBUFS:
1be5c7
+            if (sflags & MSG_ZEROCOPY) {
1be5c7
+                error_setg_errno(errp, errno,
1be5c7
+                                 "Process can't lock enough memory for using MSG_ZEROCOPY");
1be5c7
+                return -1;
1be5c7
+            }
1be5c7
+            break;
1be5c7
+#endif
1be5c7
         }
1be5c7
+
1be5c7
         error_setg_errno(errp, errno,
1be5c7
                          "Unable to write to socket");
1be5c7
         return -1;
1be5c7
@@ -660,6 +697,74 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc,
1be5c7
 }
1be5c7
 #endif /* WIN32 */
1be5c7
 
1be5c7
+
1be5c7
+#ifdef QEMU_MSG_ZEROCOPY
1be5c7
+static int qio_channel_socket_flush(QIOChannel *ioc,
1be5c7
+                                    Error **errp)
1be5c7
+{
1be5c7
+    QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(ioc);
1be5c7
+    struct msghdr msg = {};
1be5c7
+    struct sock_extended_err *serr;
1be5c7
+    struct cmsghdr *cm;
1be5c7
+    char control[CMSG_SPACE(sizeof(*serr))];
1be5c7
+    int received;
1be5c7
+    int ret = 1;
1be5c7
+
1be5c7
+    msg.msg_control = control;
1be5c7
+    msg.msg_controllen = sizeof(control);
1be5c7
+    memset(control, 0, sizeof(control));
1be5c7
+
1be5c7
+    while (sioc->zero_copy_sent < sioc->zero_copy_queued) {
1be5c7
+        received = recvmsg(sioc->fd, &msg, MSG_ERRQUEUE);
1be5c7
+        if (received < 0) {
1be5c7
+            switch (errno) {
1be5c7
+            case EAGAIN:
1be5c7
+                /* Nothing on errqueue, wait until something is available */
1be5c7
+                qio_channel_wait(ioc, G_IO_ERR);
1be5c7
+                continue;
1be5c7
+            case EINTR:
1be5c7
+                continue;
1be5c7
+            default:
1be5c7
+                error_setg_errno(errp, errno,
1be5c7
+                                 "Unable to read errqueue");
1be5c7
+                return -1;
1be5c7
+            }
1be5c7
+        }
1be5c7
+
1be5c7
+        cm = CMSG_FIRSTHDR(&msg;;
1be5c7
+        if (cm->cmsg_level != SOL_IP &&
1be5c7
+            cm->cmsg_type != IP_RECVERR) {
1be5c7
+            error_setg_errno(errp, EPROTOTYPE,
1be5c7
+                             "Wrong cmsg in errqueue");
1be5c7
+            return -1;
1be5c7
+        }
1be5c7
+
1be5c7
+        serr = (void *) CMSG_DATA(cm);
1be5c7
+        if (serr->ee_errno != SO_EE_ORIGIN_NONE) {
1be5c7
+            error_setg_errno(errp, serr->ee_errno,
1be5c7
+                             "Error on socket");
1be5c7
+            return -1;
1be5c7
+        }
1be5c7
+        if (serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
1be5c7
+            error_setg_errno(errp, serr->ee_origin,
1be5c7
+                             "Error not from zero copy");
1be5c7
+            return -1;
1be5c7
+        }
1be5c7
+
1be5c7
+        /* No errors, count successfully finished sendmsg()*/
1be5c7
+        sioc->zero_copy_sent += serr->ee_data - serr->ee_info + 1;
1be5c7
+
1be5c7
+        /* If any sendmsg() succeeded using zero copy, return 0 at the end */
1be5c7
+        if (serr->ee_code != SO_EE_CODE_ZEROCOPY_COPIED) {
1be5c7
+            ret = 0;
1be5c7
+        }
1be5c7
+    }
1be5c7
+
1be5c7
+    return ret;
1be5c7
+}
1be5c7
+
1be5c7
+#endif /* QEMU_MSG_ZEROCOPY */
1be5c7
+
1be5c7
 static int
1be5c7
 qio_channel_socket_set_blocking(QIOChannel *ioc,
1be5c7
                                 bool enabled,
1be5c7
@@ -789,6 +894,9 @@ static void qio_channel_socket_class_init(ObjectClass *klass,
1be5c7
     ioc_klass->io_set_delay = qio_channel_socket_set_delay;
1be5c7
     ioc_klass->io_create_watch = qio_channel_socket_create_watch;
1be5c7
     ioc_klass->io_set_aio_fd_handler = qio_channel_socket_set_aio_fd_handler;
1be5c7
+#ifdef QEMU_MSG_ZEROCOPY
1be5c7
+    ioc_klass->io_flush = qio_channel_socket_flush;
1be5c7
+#endif
1be5c7
 }
1be5c7
 
1be5c7
 static const TypeInfo qio_channel_socket_info = {
1be5c7
-- 
1be5c7
2.35.3
1be5c7