From: elena.ufimtseva@oracle.com
To: qemu-devel@nongnu.org
Cc: elena.ufimtseva@oracle.com, fam@euphon.net,
swapnil.ingle@nutanix.com, john.g.johnson@oracle.com,
kraxel@redhat.com, jag.raman@oracle.com, quintela@redhat.com,
mst@redhat.com, armbru@redhat.com, kanth.ghatraju@oracle.com,
felipe@nutanix.com, thuth@redhat.com, ehabkost@redhat.com,
konrad.wilk@oracle.com, dgilbert@redhat.com, stefanha@redhat.com,
thanos.makatos@nutanix.com, rth@twiddle.net, kwolf@redhat.com,
berrange@redhat.com, mreitz@redhat.com,
ross.lagerwall@citrix.com, marcandre.lureau@gmail.com,
pbonzini@redhat.com
Subject: [PATCH v9 06/20] multi-process: define MPQemuMsg format and transmission functions
Date: Thu, 27 Aug 2020 11:12:17 -0700 [thread overview]
Message-ID: <20200827181231.22778-7-elena.ufimtseva@oracle.com> (raw)
In-Reply-To: <20200827181231.22778-1-elena.ufimtseva@oracle.com>
From: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Defines MPQemuMsg, which is the message that is sent to the remote
process. This message is sent over QIOChannel and is used to
command the remote process to perform various tasks.
Also defined the helper functions to send and receive messages over the
QIOChannel
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
Signed-off-by: John G Johnson <john.g.johnson@oracle.com>
Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
---
MAINTAINERS | 2 +
include/io/mpqemu-link.h | 60 ++++++++++++++
io/meson.build | 2 +
io/mpqemu-link.c | 173 +++++++++++++++++++++++++++++++++++++++
4 files changed, 237 insertions(+)
create mode 100644 include/io/mpqemu-link.h
create mode 100644 io/mpqemu-link.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 38d19c83cd..1ca1f8ccff 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3045,6 +3045,8 @@ F: hw/pci-host/remote.c
F: include/hw/pci-host/remote.h
F: hw/i386/remote.c
F: include/hw/i386/remote.h
+F: io/mpqemu-link.c
+F: include/io/mpqemu-link.h
Build and test automation
-------------------------
diff --git a/include/io/mpqemu-link.h b/include/io/mpqemu-link.h
new file mode 100644
index 0000000000..c7de8648bc
--- /dev/null
+++ b/include/io/mpqemu-link.h
@@ -0,0 +1,60 @@
+/*
+ * Communication channel between QEMU and remote device process
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef MPQEMU_LINK_H
+#define MPQEMU_LINK_H
+
+#include "qom/object.h"
+#include "qemu/thread.h"
+#include "io/channel.h"
+
+#define REMOTE_MAX_FDS 8
+
+#define MPQEMU_MSG_HDR_SIZE offsetof(MPQemuMsg, data.u64)
+
+/**
+ * MPQemuCmd:
+ *
+ * MPQemuCmd enum type to specify the command to be executed on the remote
+ * device.
+ */
+typedef enum {
+ INIT = 0,
+ MAX = INT_MAX,
+} MPQemuCmd;
+
+/**
+ * MPQemuMsg:
+ * @cmd: The remote command
+ * @size: Size of the data to be shared
+ * @data: Structured data
+ * @fds: File descriptors to be shared with remote device
+ *
+ * MPQemuMsg Format of the message sent to the remote device from QEMU.
+ *
+ */
+typedef struct {
+ int cmd;
+ size_t size;
+
+ union {
+ uint64_t u64;
+ } data;
+
+ int fds[REMOTE_MAX_FDS];
+ int num_fds;
+} MPQemuMsg;
+
+void mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc, Error **errp);
+void mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc, Error **errp);
+
+bool mpqemu_msg_valid(MPQemuMsg *msg);
+
+#endif
diff --git a/io/meson.build b/io/meson.build
index 768c1b5ec3..3d40cd8867 100644
--- a/io/meson.build
+++ b/io/meson.build
@@ -15,6 +15,8 @@ io_ss.add(files(
'task.c',
))
+io_ss.add(when: 'CONFIG_MPQEMU', if_true: files('mpqemu-link.c'))
+
io_ss = io_ss.apply(config_host, strict: false)
libio = static_library('io', io_ss.sources() + genh,
dependencies: [io_ss.dependencies()],
diff --git a/io/mpqemu-link.c b/io/mpqemu-link.c
new file mode 100644
index 0000000000..d9be2bdeab
--- /dev/null
+++ b/io/mpqemu-link.c
@@ -0,0 +1,173 @@
+/*
+ * Communication channel between QEMU and remote device process
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+
+#include "qemu/module.h"
+#include "io/mpqemu-link.h"
+#include "qapi/error.h"
+#include "qemu/iov.h"
+#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+
+void mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc, Error **errp)
+{
+ bool iolock = qemu_mutex_iothread_locked();
+ Error *local_err = NULL;
+ struct iovec send[2];
+ int *fds = NULL;
+ size_t nfds = 0;
+
+ send[0].iov_base = msg;
+ send[0].iov_len = MPQEMU_MSG_HDR_SIZE;
+
+ send[1].iov_base = (void *)&msg->data;
+ send[1].iov_len = msg->size;
+
+ if (msg->num_fds) {
+ nfds = msg->num_fds;
+ fds = msg->fds;
+ }
+
+ if (iolock) {
+ qemu_mutex_unlock_iothread();
+ }
+
+ (void)qio_channel_writev_full_all(ioc, send, G_N_ELEMENTS(send), fds, nfds,
+ &local_err);
+
+ if (iolock) {
+ qemu_mutex_lock_iothread();
+ }
+
+ if (errp) {
+ error_propagate(errp, local_err);
+ } else if (local_err) {
+ error_report_err(local_err);
+ }
+
+ return;
+}
+
+static ssize_t mpqemu_read(QIOChannel *ioc, void *buf, size_t len, int **fds,
+ size_t *nfds, Error **errp)
+{
+ struct iovec iov = { .iov_base = buf, .iov_len = len };
+ bool iolock = qemu_mutex_iothread_locked();
+ struct iovec *iovp = &iov;
+ Error *local_err = NULL;
+ unsigned int niov = 1;
+ size_t *l_nfds = nfds;
+ int **l_fds = fds;
+ ssize_t bytes = 0;
+ size_t size;
+
+ iov.iov_base = buf;
+ iov.iov_len = len;
+ size = iov.iov_len;
+
+ while (size > 0) {
+ bytes = qio_channel_readv_full(ioc, iovp, niov, l_fds, l_nfds,
+ &local_err);
+
+ if (bytes == QIO_CHANNEL_ERR_BLOCK) {
+ if (iolock) {
+ qemu_mutex_unlock_iothread();
+ }
+
+ if (qemu_in_coroutine()) {
+ qio_channel_yield(ioc, G_IO_IN);
+ } else {
+ qio_channel_wait(ioc, G_IO_IN);
+ }
+
+ if (iolock) {
+ qemu_mutex_lock_iothread();
+ }
+ continue;
+ }
+
+ if (bytes <= 0) {
+ error_propagate(errp, local_err);
+ return -EIO;
+ }
+
+ l_fds = NULL;
+ l_nfds = NULL;
+
+ size -= bytes;
+
+ (void)iov_discard_front(&iovp, &niov, bytes);
+ }
+
+ return len - size;
+}
+
+void mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc, Error **errp)
+{
+ Error *local_err = NULL;
+ int *fds = NULL;
+ size_t nfds = 0;
+ ssize_t len;
+
+ len = mpqemu_read(ioc, (void *)msg, MPQEMU_MSG_HDR_SIZE, &fds, &nfds,
+ &local_err);
+ if (len < 0) {
+ goto fail;
+ } else if (len != MPQEMU_MSG_HDR_SIZE) {
+ error_setg(&local_err, "Message header corrupted");
+ goto fail;
+ }
+
+ if (msg->size > sizeof(msg->data)) {
+ error_setg(&local_err, "Invalid size for message");
+ goto fail;
+ }
+
+ if (mpqemu_read(ioc, (void *)&msg->data, msg->size, NULL, NULL,
+ &local_err) < 0) {
+ goto fail;
+ }
+
+ msg->num_fds = nfds;
+ if (nfds) {
+ memcpy(msg->fds, fds, nfds * sizeof(int));
+ }
+
+fail:
+ if (errp) {
+ error_propagate(errp, local_err);
+ } else if (local_err) {
+ error_report_err(local_err);
+ }
+}
+
+bool mpqemu_msg_valid(MPQemuMsg *msg)
+{
+ if (msg->cmd >= MAX && msg->cmd < 0) {
+ return false;
+ }
+
+ /* Verify FDs. */
+ if (msg->num_fds >= REMOTE_MAX_FDS) {
+ return false;
+ }
+
+ if (msg->num_fds > 0) {
+ for (int i = 0; i < msg->num_fds; i++) {
+ if (fcntl(msg->fds[i], F_GETFL) == -1) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
--
2.25.GIT
next prev parent reply other threads:[~2020-08-27 18:16 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-27 18:12 [PATCH v9 00/20] Initial support for multi-process Qemu elena.ufimtseva
2020-08-27 18:12 ` [PATCH v9 01/20] memory: alloc RAM from file at offset elena.ufimtseva
2020-08-27 18:12 ` [PATCH v9 02/20] multi-process: Add config option for multi-process QEMU elena.ufimtseva
2020-08-27 18:12 ` [PATCH v9 03/20] multi-process: setup PCI host bridge for remote device elena.ufimtseva
2020-09-14 15:46 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 04/20] multi-process: setup a machine object for remote device process elena.ufimtseva
2020-09-15 13:01 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 05/20] multi-process: add qio channel function to transmit elena.ufimtseva
2020-08-27 18:12 ` elena.ufimtseva [this message]
2020-09-23 13:47 ` [PATCH v9 06/20] multi-process: define MPQemuMsg format and transmission functions Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 07/20] multi-process: define transmission functions in remote elena.ufimtseva
2020-09-23 14:02 ` Stefan Hajnoczi
2020-09-24 17:18 ` Elena Ufimtseva
2020-08-27 18:12 ` [PATCH v9 08/20] multi-process: Initialize message handler in remote device elena.ufimtseva
2020-09-23 14:10 ` Stefan Hajnoczi
2020-09-24 17:20 ` Elena Ufimtseva
2020-08-27 18:12 ` [PATCH v9 09/20] multi-process: Associate fd of a PCIDevice with its object elena.ufimtseva
2020-09-23 14:17 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 10/20] multi-process: setup memory manager for remote device elena.ufimtseva
2020-09-23 15:03 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 11/20] multi-process: introduce proxy object elena.ufimtseva
2020-09-23 15:06 ` Stefan Hajnoczi
2020-09-23 15:10 ` Michael S. Tsirkin
2020-09-24 14:33 ` Jag Raman
2020-08-27 18:12 ` [PATCH v9 12/20] multi-process: add proxy communication functions elena.ufimtseva
2020-09-23 15:55 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 13/20] multi-process: Forward PCI config space acceses to the remote process elena.ufimtseva
2020-09-23 16:01 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 14/20] multi-process: PCI BAR read/write handling for proxy & remote endpoints elena.ufimtseva
2020-09-24 7:51 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 15/20] multi-process: Synchronize remote memory elena.ufimtseva
2020-09-24 8:27 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 16/20] multi-process: create IOHUB object to handle irq elena.ufimtseva
2020-09-24 8:29 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 17/20] multi-process: Retrieve PCI info from remote process elena.ufimtseva
2020-09-24 8:30 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 18/20] multi-process: perform device reset in the " elena.ufimtseva
2020-09-24 8:31 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 19/20] multi-process: add the concept description to docs/devel/qemu-multiprocess elena.ufimtseva
2020-09-24 8:32 ` Stefan Hajnoczi
2020-08-27 18:12 ` [PATCH v9 20/20] multi-process: add configure and usage information elena.ufimtseva
2020-09-24 8:32 ` Stefan Hajnoczi
2020-09-23 15:47 ` [PATCH v9 00/20] Initial support for multi-process Qemu Michael S. Tsirkin
2020-09-24 8:38 ` Stefan Hajnoczi
2020-09-24 14:33 ` Jag Raman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200827181231.22778-7-elena.ufimtseva@oracle.com \
--to=elena.ufimtseva@oracle.com \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=dgilbert@redhat.com \
--cc=ehabkost@redhat.com \
--cc=fam@euphon.net \
--cc=felipe@nutanix.com \
--cc=jag.raman@oracle.com \
--cc=john.g.johnson@oracle.com \
--cc=kanth.ghatraju@oracle.com \
--cc=konrad.wilk@oracle.com \
--cc=kraxel@redhat.com \
--cc=kwolf@redhat.com \
--cc=marcandre.lureau@gmail.com \
--cc=mreitz@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=ross.lagerwall@citrix.com \
--cc=rth@twiddle.net \
--cc=stefanha@redhat.com \
--cc=swapnil.ingle@nutanix.com \
--cc=thanos.makatos@nutanix.com \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).