Skip to content

Commit ad22c30

Browse files
elena-ufimtsevastefanhaRH
authored andcommitted
multi-process: define MPQemuMsg format and transmission functions
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. Define transmission functions used by proxy and by remote. Signed-off-by: Jagannathan Raman <[email protected]> Signed-off-by: John G Johnson <[email protected]> Signed-off-by: Elena Ufimtseva <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Message-id: 56ca8bcf95195b2b195b08f6b9565b6d7410bce5.1611938319.git.jag.raman@oracle.com [Replace struct iovec send[2] = {0} with {} to make clang happy as suggested by Peter Maydell <[email protected]>. --Stefan] Signed-off-by: Stefan Hajnoczi <[email protected]>
1 parent bebab91 commit ad22c30

File tree

9 files changed

+289
-0
lines changed

9 files changed

+289
-0
lines changed

MAINTAINERS

+2
Original file line numberDiff line numberDiff line change
@@ -3211,6 +3211,8 @@ F: hw/pci-host/remote.c
32113211
F: include/hw/pci-host/remote.h
32123212
F: hw/remote/machine.c
32133213
F: include/hw/remote/machine.h
3214+
F: hw/remote/mpqemu-link.c
3215+
F: include/hw/remote/mpqemu-link.h
32143216

32153217
Build and test automation
32163218
-------------------------

hw/remote/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
remote_ss = ss.source_set()
22

33
remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('machine.c'))
4+
remote_ss.add(when: 'CONFIG_MULTIPROCESS', if_true: files('mpqemu-link.c'))
45

56
softmmu_ss.add_all(when: 'CONFIG_MULTIPROCESS', if_true: remote_ss)

hw/remote/mpqemu-link.c

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Communication channel between QEMU and remote device process
3+
*
4+
* Copyright © 2018, 2021 Oracle and/or its affiliates.
5+
*
6+
* This work is licensed under the terms of the GNU GPL, version 2 or later.
7+
* See the COPYING file in the top-level directory.
8+
*
9+
*/
10+
11+
#include "qemu/osdep.h"
12+
#include "qemu-common.h"
13+
14+
#include "qemu/module.h"
15+
#include "hw/remote/mpqemu-link.h"
16+
#include "qapi/error.h"
17+
#include "qemu/iov.h"
18+
#include "qemu/error-report.h"
19+
#include "qemu/main-loop.h"
20+
#include "io/channel.h"
21+
#include "sysemu/iothread.h"
22+
#include "trace.h"
23+
24+
/*
25+
* Send message over the ioc QIOChannel.
26+
* This function is safe to call from:
27+
* - main loop in co-routine context. Will block the main loop if not in
28+
* co-routine context;
29+
* - vCPU thread with no co-routine context and if the channel is not part
30+
* of the main loop handling;
31+
* - IOThread within co-routine context, outside of co-routine context
32+
* will block IOThread;
33+
* Returns true if no errors were encountered, false otherwise.
34+
*/
35+
bool mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc, Error **errp)
36+
{
37+
ERRP_GUARD();
38+
bool iolock = qemu_mutex_iothread_locked();
39+
bool iothread = qemu_in_iothread();
40+
struct iovec send[2] = {};
41+
int *fds = NULL;
42+
size_t nfds = 0;
43+
bool ret = false;
44+
45+
send[0].iov_base = msg;
46+
send[0].iov_len = MPQEMU_MSG_HDR_SIZE;
47+
48+
send[1].iov_base = (void *)&msg->data;
49+
send[1].iov_len = msg->size;
50+
51+
if (msg->num_fds) {
52+
nfds = msg->num_fds;
53+
fds = msg->fds;
54+
}
55+
56+
/*
57+
* Dont use in IOThread out of co-routine context as
58+
* it will block IOThread.
59+
*/
60+
assert(qemu_in_coroutine() || !iothread);
61+
62+
/*
63+
* Skip unlocking/locking iothread lock when the IOThread is running
64+
* in co-routine context. Co-routine context is asserted above
65+
* for IOThread case.
66+
* Also skip lock handling while in a co-routine in the main context.
67+
*/
68+
if (iolock && !iothread && !qemu_in_coroutine()) {
69+
qemu_mutex_unlock_iothread();
70+
}
71+
72+
if (!qio_channel_writev_full_all(ioc, send, G_N_ELEMENTS(send),
73+
fds, nfds, errp)) {
74+
ret = true;
75+
} else {
76+
trace_mpqemu_send_io_error(msg->cmd, msg->size, nfds);
77+
}
78+
79+
if (iolock && !iothread && !qemu_in_coroutine()) {
80+
/* See above comment why skip locking here. */
81+
qemu_mutex_lock_iothread();
82+
}
83+
84+
return ret;
85+
}
86+
87+
/*
88+
* Read message from the ioc QIOChannel.
89+
* This function is safe to call from:
90+
* - From main loop in co-routine context. Will block the main loop if not in
91+
* co-routine context;
92+
* - From vCPU thread with no co-routine context and if the channel is not part
93+
* of the main loop handling;
94+
* - From IOThread within co-routine context, outside of co-routine context
95+
* will block IOThread;
96+
*/
97+
static ssize_t mpqemu_read(QIOChannel *ioc, void *buf, size_t len, int **fds,
98+
size_t *nfds, Error **errp)
99+
{
100+
ERRP_GUARD();
101+
struct iovec iov = { .iov_base = buf, .iov_len = len };
102+
bool iolock = qemu_mutex_iothread_locked();
103+
bool iothread = qemu_in_iothread();
104+
int ret = -1;
105+
106+
/*
107+
* Dont use in IOThread out of co-routine context as
108+
* it will block IOThread.
109+
*/
110+
assert(qemu_in_coroutine() || !iothread);
111+
112+
if (iolock && !iothread && !qemu_in_coroutine()) {
113+
qemu_mutex_unlock_iothread();
114+
}
115+
116+
ret = qio_channel_readv_full_all_eof(ioc, &iov, 1, fds, nfds, errp);
117+
118+
if (iolock && !iothread && !qemu_in_coroutine()) {
119+
qemu_mutex_lock_iothread();
120+
}
121+
122+
return (ret <= 0) ? ret : iov.iov_len;
123+
}
124+
125+
bool mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc, Error **errp)
126+
{
127+
ERRP_GUARD();
128+
g_autofree int *fds = NULL;
129+
size_t nfds = 0;
130+
ssize_t len;
131+
bool ret = false;
132+
133+
len = mpqemu_read(ioc, msg, MPQEMU_MSG_HDR_SIZE, &fds, &nfds, errp);
134+
if (len <= 0) {
135+
goto fail;
136+
} else if (len != MPQEMU_MSG_HDR_SIZE) {
137+
error_setg(errp, "Message header corrupted");
138+
goto fail;
139+
}
140+
141+
if (msg->size > sizeof(msg->data)) {
142+
error_setg(errp, "Invalid size for message");
143+
goto fail;
144+
}
145+
146+
if (!msg->size) {
147+
goto copy_fds;
148+
}
149+
150+
len = mpqemu_read(ioc, &msg->data, msg->size, NULL, NULL, errp);
151+
if (len <= 0) {
152+
goto fail;
153+
}
154+
if (len != msg->size) {
155+
error_setg(errp, "Unable to read full message");
156+
goto fail;
157+
}
158+
159+
copy_fds:
160+
msg->num_fds = nfds;
161+
if (nfds > G_N_ELEMENTS(msg->fds)) {
162+
error_setg(errp,
163+
"Overflow error: received %zu fds, more than max of %d fds",
164+
nfds, REMOTE_MAX_FDS);
165+
goto fail;
166+
}
167+
if (nfds) {
168+
memcpy(msg->fds, fds, nfds * sizeof(int));
169+
}
170+
171+
ret = true;
172+
173+
fail:
174+
if (*errp) {
175+
trace_mpqemu_recv_io_error(msg->cmd, msg->size, nfds);
176+
}
177+
while (*errp && nfds) {
178+
close(fds[nfds - 1]);
179+
nfds--;
180+
}
181+
182+
return ret;
183+
}
184+
185+
bool mpqemu_msg_valid(MPQemuMsg *msg)
186+
{
187+
if (msg->cmd >= MPQEMU_CMD_MAX && msg->cmd < 0) {
188+
return false;
189+
}
190+
191+
/* Verify FDs. */
192+
if (msg->num_fds >= REMOTE_MAX_FDS) {
193+
return false;
194+
}
195+
196+
if (msg->num_fds > 0) {
197+
for (int i = 0; i < msg->num_fds; i++) {
198+
if (fcntl(msg->fds[i], F_GETFL) == -1) {
199+
return false;
200+
}
201+
}
202+
}
203+
204+
return true;
205+
}

hw/remote/trace-events

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# multi-process trace events
2+
3+
mpqemu_send_io_error(int cmd, int size, int nfds) "send command %d size %d, %d file descriptors to remote process"
4+
mpqemu_recv_io_error(int cmd, int size, int nfds) "failed to receive %d size %d, %d file descriptors to remote process"

hw/remote/trace.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "trace/trace-hw_remote.h"

include/hw/remote/mpqemu-link.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Communication channel between QEMU and remote device process
3+
*
4+
* Copyright © 2018, 2021 Oracle and/or its affiliates.
5+
*
6+
* This work is licensed under the terms of the GNU GPL, version 2 or later.
7+
* See the COPYING file in the top-level directory.
8+
*
9+
*/
10+
11+
#ifndef MPQEMU_LINK_H
12+
#define MPQEMU_LINK_H
13+
14+
#include "qom/object.h"
15+
#include "qemu/thread.h"
16+
#include "io/channel.h"
17+
18+
#define REMOTE_MAX_FDS 8
19+
20+
#define MPQEMU_MSG_HDR_SIZE offsetof(MPQemuMsg, data.u64)
21+
22+
/**
23+
* MPQemuCmd:
24+
*
25+
* MPQemuCmd enum type to specify the command to be executed on the remote
26+
* device.
27+
*
28+
* This uses a private protocol between QEMU and the remote process. vfio-user
29+
* protocol would supersede this in the future.
30+
*
31+
*/
32+
typedef enum {
33+
MPQEMU_CMD_MAX,
34+
} MPQemuCmd;
35+
36+
/**
37+
* MPQemuMsg:
38+
* @cmd: The remote command
39+
* @size: Size of the data to be shared
40+
* @data: Structured data
41+
* @fds: File descriptors to be shared with remote device
42+
*
43+
* MPQemuMsg Format of the message sent to the remote device from QEMU.
44+
*
45+
*/
46+
typedef struct {
47+
int cmd;
48+
size_t size;
49+
50+
union {
51+
uint64_t u64;
52+
} data;
53+
54+
int fds[REMOTE_MAX_FDS];
55+
int num_fds;
56+
} MPQemuMsg;
57+
58+
bool mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc, Error **errp);
59+
bool mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc, Error **errp);
60+
61+
bool mpqemu_msg_valid(MPQemuMsg *msg);
62+
63+
#endif

include/sysemu/iothread.h

+6
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ IOThread *iothread_create(const char *id, Error **errp);
5757
void iothread_stop(IOThread *iothread);
5858
void iothread_destroy(IOThread *iothread);
5959

60+
/*
61+
* Returns true if executing withing IOThread context,
62+
* false otherwise.
63+
*/
64+
bool qemu_in_iothread(void);
65+
6066
#endif /* IOTHREAD_H */

iothread.c

+6
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,9 @@ IOThread *iothread_by_id(const char *id)
369369
{
370370
return IOTHREAD(object_resolve_path_type(id, TYPE_IOTHREAD, NULL));
371371
}
372+
373+
bool qemu_in_iothread(void)
374+
{
375+
return qemu_get_current_aio_context() == qemu_get_aio_context() ?
376+
false : true;
377+
}

meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,7 @@ if have_system
18181818
'net',
18191819
'softmmu',
18201820
'ui',
1821+
'hw/remote',
18211822
]
18221823
endif
18231824
if have_system or have_user

0 commit comments

Comments
 (0)