2015-10-11 01:18:32 +03:00
|
|
|
/*
|
|
|
|
* QTest testcase for ivshmem
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 SUSE LINUX Products GmbH
|
2014-04-02 18:57:48 +04:00
|
|
|
* Copyright (c) 2015 Red Hat, Inc.
|
2015-10-11 01:18:32 +03:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2014-04-02 18:57:48 +04:00
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include "contrib/ivshmem-server/ivshmem-server.h"
|
2017-01-05 18:29:47 +03:00
|
|
|
#include "libqos/libqos-pc.h"
|
2017-01-05 18:29:48 +03:00
|
|
|
#include "libqos/libqos-spapr.h"
|
2020-08-04 21:00:40 +03:00
|
|
|
#include "libqos/libqtest.h"
|
2014-04-02 18:57:48 +04:00
|
|
|
#include "qemu-common.h"
|
2015-10-11 01:18:32 +03:00
|
|
|
|
2014-04-02 18:57:48 +04:00
|
|
|
#define TMPSHMSIZE (1 << 20)
|
|
|
|
static char *tmpshm;
|
|
|
|
static void *tmpshmem;
|
|
|
|
static char *tmpdir;
|
|
|
|
static char *tmpserver;
|
2015-10-11 01:18:32 +03:00
|
|
|
|
2014-04-02 18:57:48 +04:00
|
|
|
static void save_fn(QPCIDevice *dev, int devfn, void *data)
|
2015-10-11 01:18:32 +03:00
|
|
|
{
|
2014-04-02 18:57:48 +04:00
|
|
|
QPCIDevice **pdev = (QPCIDevice **) data;
|
|
|
|
|
|
|
|
*pdev = dev;
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:13:59 +03:00
|
|
|
static QPCIDevice *get_device(QPCIBus *pcibus)
|
2014-04-02 18:57:48 +04:00
|
|
|
{
|
|
|
|
QPCIDevice *dev;
|
|
|
|
|
2015-11-24 20:06:27 +03:00
|
|
|
dev = NULL;
|
2014-04-02 18:57:48 +04:00
|
|
|
qpci_device_foreach(pcibus, 0x1af4, 0x1110, save_fn, &dev);
|
|
|
|
g_assert(dev != NULL);
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _IVState {
|
2017-01-05 18:29:47 +03:00
|
|
|
QOSState *qs;
|
2016-10-24 07:52:06 +03:00
|
|
|
QPCIBar reg_bar, mem_bar;
|
2014-04-02 18:57:48 +04:00
|
|
|
QPCIDevice *dev;
|
|
|
|
} IVState;
|
|
|
|
|
|
|
|
enum Reg {
|
|
|
|
INTRMASK = 0,
|
|
|
|
INTRSTATUS = 4,
|
|
|
|
IVPOSITION = 8,
|
|
|
|
DOORBELL = 12,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* reg2str(enum Reg reg) {
|
|
|
|
switch (reg) {
|
|
|
|
case INTRMASK:
|
|
|
|
return "IntrMask";
|
|
|
|
case INTRSTATUS:
|
|
|
|
return "IntrStatus";
|
|
|
|
case IVPOSITION:
|
|
|
|
return "IVPosition";
|
|
|
|
case DOORBELL:
|
|
|
|
return "DoorBell";
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned in_reg(IVState *s, enum Reg reg)
|
|
|
|
{
|
|
|
|
const char *name = reg2str(reg);
|
|
|
|
unsigned res;
|
|
|
|
|
2016-10-24 07:52:06 +03:00
|
|
|
res = qpci_io_readl(s->dev, s->reg_bar, reg);
|
2019-03-01 18:10:18 +03:00
|
|
|
g_test_message("*%s -> %x", name, res);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void out_reg(IVState *s, enum Reg reg, unsigned v)
|
|
|
|
{
|
|
|
|
const char *name = reg2str(reg);
|
|
|
|
|
2019-03-01 18:10:18 +03:00
|
|
|
g_test_message("%x -> *%s", v, name);
|
2016-10-24 07:52:06 +03:00
|
|
|
qpci_io_writel(s->dev, s->reg_bar, reg, v);
|
2014-04-02 18:57:48 +04:00
|
|
|
}
|
|
|
|
|
2016-10-18 08:18:45 +03:00
|
|
|
static inline void read_mem(IVState *s, uint64_t off, void *buf, size_t len)
|
|
|
|
{
|
2016-10-24 07:52:06 +03:00
|
|
|
qpci_memread(s->dev, s->mem_bar, off, buf, len);
|
2016-10-18 08:18:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void write_mem(IVState *s, uint64_t off,
|
|
|
|
const void *buf, size_t len)
|
|
|
|
{
|
2016-10-24 07:52:06 +03:00
|
|
|
qpci_memwrite(s->dev, s->mem_bar, off, buf, len);
|
2016-10-18 08:18:45 +03:00
|
|
|
}
|
|
|
|
|
2015-12-18 17:13:59 +03:00
|
|
|
static void cleanup_vm(IVState *s)
|
|
|
|
{
|
|
|
|
g_free(s->dev);
|
2017-01-05 18:29:47 +03:00
|
|
|
qtest_shutdown(s->qs);
|
2015-12-18 17:13:59 +03:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:57:48 +04:00
|
|
|
static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
|
|
|
|
{
|
|
|
|
uint64_t barsize;
|
2017-01-05 18:29:47 +03:00
|
|
|
const char *arch = qtest_get_arch();
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2017-01-05 18:29:47 +03:00
|
|
|
if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
|
|
|
|
s->qs = qtest_pc_boot(cmd);
|
2017-01-05 18:29:48 +03:00
|
|
|
} else if (strcmp(arch, "ppc64") == 0) {
|
|
|
|
s->qs = qtest_spapr_boot(cmd);
|
2017-01-05 18:29:47 +03:00
|
|
|
} else {
|
2017-01-05 18:29:48 +03:00
|
|
|
g_printerr("ivshmem-test tests are only available on x86 or ppc64\n");
|
2017-01-05 18:29:47 +03:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
s->dev = get_device(s->qs->pcibus);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2016-10-24 07:52:06 +03:00
|
|
|
s->reg_bar = qpci_iomap(s->dev, 0, &barsize);
|
2016-03-15 21:34:21 +03:00
|
|
|
g_assert_cmpuint(barsize, ==, 256);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
|
|
|
if (msix) {
|
|
|
|
qpci_msix_enable(s->dev);
|
|
|
|
}
|
|
|
|
|
2016-10-24 07:52:06 +03:00
|
|
|
s->mem_bar = qpci_iomap(s->dev, 2, &barsize);
|
2016-03-15 21:34:21 +03:00
|
|
|
g_assert_cmpuint(barsize, ==, TMPSHMSIZE);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
|
|
|
qpci_device_enable(s->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_vm(IVState *s)
|
|
|
|
{
|
2016-03-15 21:34:51 +03:00
|
|
|
char *cmd = g_strdup_printf("-object memory-backend-file"
|
2020-11-03 15:37:46 +03:00
|
|
|
",id=mb1,size=1M,share=on,mem-path=/dev/shm%s"
|
2016-03-15 21:34:51 +03:00
|
|
|
" -device ivshmem-plain,memdev=mb1", tmpshm);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
|
|
|
setup_vm_cmd(s, cmd, false);
|
|
|
|
|
|
|
|
g_free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_ivshmem_single(void)
|
|
|
|
{
|
|
|
|
IVState state, *s;
|
|
|
|
uint32_t data[1024];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
setup_vm(&state);
|
|
|
|
s = &state;
|
|
|
|
|
2016-03-15 21:34:22 +03:00
|
|
|
/* initial state of readable registers */
|
|
|
|
g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0);
|
|
|
|
g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
|
|
|
|
g_assert_cmpuint(in_reg(s, IVPOSITION), ==, 0);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2016-03-15 21:34:22 +03:00
|
|
|
/* trigger interrupt via registers */
|
2014-04-02 18:57:48 +04:00
|
|
|
out_reg(s, INTRMASK, 0xffffffff);
|
|
|
|
g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0xffffffff);
|
|
|
|
out_reg(s, INTRSTATUS, 1);
|
2016-03-15 21:34:22 +03:00
|
|
|
/* check interrupt status */
|
2014-04-02 18:57:48 +04:00
|
|
|
g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 1);
|
2016-03-15 21:34:22 +03:00
|
|
|
/* reading clears */
|
|
|
|
g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
|
|
|
|
/* TODO intercept actual interrupt (needs qtest work) */
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2016-03-15 21:34:22 +03:00
|
|
|
/* invalid register access */
|
2014-04-02 18:57:48 +04:00
|
|
|
out_reg(s, IVPOSITION, 1);
|
2016-03-15 21:34:22 +03:00
|
|
|
in_reg(s, DOORBELL);
|
|
|
|
|
|
|
|
/* ring the (non-functional) doorbell */
|
2014-04-02 18:57:48 +04:00
|
|
|
out_reg(s, DOORBELL, 8 << 16);
|
|
|
|
|
2016-03-15 21:34:22 +03:00
|
|
|
/* write shared memory */
|
2014-04-02 18:57:48 +04:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(data); i++) {
|
|
|
|
data[i] = i;
|
|
|
|
}
|
2016-10-18 08:18:45 +03:00
|
|
|
write_mem(s, 0, data, sizeof(data));
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2016-03-15 21:34:22 +03:00
|
|
|
/* verify write */
|
2014-04-02 18:57:48 +04:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(data); i++) {
|
|
|
|
g_assert_cmpuint(((uint32_t *)tmpshmem)[i], ==, i);
|
|
|
|
}
|
|
|
|
|
2016-03-15 21:34:22 +03:00
|
|
|
/* read it back and verify read */
|
2014-04-02 18:57:48 +04:00
|
|
|
memset(data, 0, sizeof(data));
|
2016-10-18 08:18:45 +03:00
|
|
|
read_mem(s, 0, data, sizeof(data));
|
2014-04-02 18:57:48 +04:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(data); i++) {
|
|
|
|
g_assert_cmpuint(data[i], ==, i);
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:13:59 +03:00
|
|
|
cleanup_vm(s);
|
2014-04-02 18:57:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_ivshmem_pair(void)
|
|
|
|
{
|
|
|
|
IVState state1, state2, *s1, *s2;
|
|
|
|
char *data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
setup_vm(&state1);
|
|
|
|
s1 = &state1;
|
|
|
|
setup_vm(&state2);
|
|
|
|
s2 = &state2;
|
|
|
|
|
|
|
|
data = g_malloc0(TMPSHMSIZE);
|
|
|
|
|
|
|
|
/* host write, guest 1 & 2 read */
|
|
|
|
memset(tmpshmem, 0x42, TMPSHMSIZE);
|
2016-10-18 08:18:45 +03:00
|
|
|
read_mem(s1, 0, data, TMPSHMSIZE);
|
2014-04-02 18:57:48 +04:00
|
|
|
for (i = 0; i < TMPSHMSIZE; i++) {
|
|
|
|
g_assert_cmpuint(data[i], ==, 0x42);
|
|
|
|
}
|
2016-10-18 08:18:45 +03:00
|
|
|
read_mem(s2, 0, data, TMPSHMSIZE);
|
2014-04-02 18:57:48 +04:00
|
|
|
for (i = 0; i < TMPSHMSIZE; i++) {
|
|
|
|
g_assert_cmpuint(data[i], ==, 0x42);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* guest 1 write, guest 2 read */
|
|
|
|
memset(data, 0x43, TMPSHMSIZE);
|
2016-10-18 08:18:45 +03:00
|
|
|
write_mem(s1, 0, data, TMPSHMSIZE);
|
2014-04-02 18:57:48 +04:00
|
|
|
memset(data, 0, TMPSHMSIZE);
|
2016-10-18 08:18:45 +03:00
|
|
|
read_mem(s2, 0, data, TMPSHMSIZE);
|
2014-04-02 18:57:48 +04:00
|
|
|
for (i = 0; i < TMPSHMSIZE; i++) {
|
|
|
|
g_assert_cmpuint(data[i], ==, 0x43);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* guest 2 write, guest 1 read */
|
|
|
|
memset(data, 0x44, TMPSHMSIZE);
|
2016-10-18 08:18:45 +03:00
|
|
|
write_mem(s2, 0, data, TMPSHMSIZE);
|
2014-04-02 18:57:48 +04:00
|
|
|
memset(data, 0, TMPSHMSIZE);
|
2016-10-18 08:18:45 +03:00
|
|
|
read_mem(s1, 0, data, TMPSHMSIZE);
|
2014-04-02 18:57:48 +04:00
|
|
|
for (i = 0; i < TMPSHMSIZE; i++) {
|
|
|
|
g_assert_cmpuint(data[i], ==, 0x44);
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:13:59 +03:00
|
|
|
cleanup_vm(s1);
|
|
|
|
cleanup_vm(s2);
|
2014-04-02 18:57:48 +04:00
|
|
|
g_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct ServerThread {
|
|
|
|
GThread *thread;
|
|
|
|
IvshmemServer *server;
|
|
|
|
int pipe[2]; /* to handle quit */
|
|
|
|
} ServerThread;
|
|
|
|
|
|
|
|
static void *server_thread(void *data)
|
|
|
|
{
|
|
|
|
ServerThread *t = data;
|
|
|
|
IvshmemServer *server = t->server;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
fd_set fds;
|
|
|
|
int maxfd, ret;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(t->pipe[0], &fds);
|
|
|
|
maxfd = t->pipe[0] + 1;
|
|
|
|
|
|
|
|
ivshmem_server_get_fds(server, &fds, &maxfd);
|
|
|
|
|
|
|
|
ret = select(maxfd, &fds, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_critical("select error: %s\n", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(t->pipe[0], &fds)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
|
|
|
|
g_critical("ivshmem_server_handle_fds() failed\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:13:25 +03:00
|
|
|
static void setup_vm_with_server(IVState *s, int nvectors)
|
2014-04-02 18:57:48 +04:00
|
|
|
{
|
2018-12-19 16:13:25 +03:00
|
|
|
char *cmd;
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2019-02-11 21:24:31 +03:00
|
|
|
cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s "
|
2018-12-19 16:13:25 +03:00
|
|
|
"-device ivshmem-doorbell,chardev=chr0,vectors=%d",
|
|
|
|
tmpserver, nvectors);
|
|
|
|
|
|
|
|
setup_vm_cmd(s, cmd, true);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
|
|
|
g_free(cmd);
|
|
|
|
}
|
|
|
|
|
2018-12-19 16:13:25 +03:00
|
|
|
static void test_ivshmem_server(void)
|
2014-04-02 18:57:48 +04:00
|
|
|
{
|
|
|
|
IVState state1, state2, *s1, *s2;
|
|
|
|
ServerThread thread;
|
|
|
|
IvshmemServer server;
|
|
|
|
int ret, vm1, vm2;
|
|
|
|
int nvectors = 2;
|
|
|
|
guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
|
|
|
|
|
2016-03-15 21:34:18 +03:00
|
|
|
ret = ivshmem_server_init(&server, tmpserver, tmpshm, true,
|
2014-04-02 18:57:48 +04:00
|
|
|
TMPSHMSIZE, nvectors,
|
|
|
|
g_test_verbose());
|
|
|
|
g_assert_cmpint(ret, ==, 0);
|
|
|
|
|
|
|
|
ret = ivshmem_server_start(&server);
|
|
|
|
g_assert_cmpint(ret, ==, 0);
|
|
|
|
|
|
|
|
thread.server = &server;
|
|
|
|
ret = pipe(thread.pipe);
|
|
|
|
g_assert_cmpint(ret, ==, 0);
|
|
|
|
thread.thread = g_thread_new("ivshmem-server", server_thread, &thread);
|
|
|
|
g_assert(thread.thread != NULL);
|
|
|
|
|
2018-12-19 16:13:25 +03:00
|
|
|
setup_vm_with_server(&state1, nvectors);
|
ivshmem: Receive shared memory synchronously in realize()
When configured for interrupts (property "chardev" given), we receive
the shared memory from an ivshmem server. We do so asynchronously
after realize() completes, by setting up callbacks with
qemu_chr_add_handlers().
Keeping server I/O out of realize() that way avoids delays due to a
slow server. This is probably relevant only for hot plug.
However, this funny "no shared memory, yet" state of the device also
causes a raft of issues that are hard or impossible to work around:
* The guest is exposed to this state: when we enter and leave it its
shared memory contents is apruptly replaced, and device register
IVPosition changes.
This is a known issue. We document that guests should not access
the shared memory after device initialization until the IVPosition
register becomes non-negative.
For cold plug, the funny state is unlikely to be visible in
practice, because we normally receive the shared memory long before
the guest gets around to mess with the device.
For hot plug, the timing is tighter, but the relative slowness of
PCI device configuration has a good chance to hide the funny state.
In either case, guests complying with the documented procedure are
safe.
* Migration becomes racy.
If migration completes before the shared memory setup completes on
the source, shared memory contents is silently lost. Fortunately,
migration is rather unlikely to win this race.
If the shared memory's ramblock arrives at the destination before
shared memory setup completes, migration fails.
There is no known way for a management application to wait for
shared memory setup to complete.
All you can do is retry failed migration. You can improve your
chances by leaving more time between running the destination QEMU
and the migrate command.
To mitigate silent memory loss, you need to ensure the server
initializes shared memory exactly the same on source and
destination.
These issues are entirely undocumented so far.
I'd expect the server to be almost always fast enough to hide these
issues. But then rare catastrophic races are in a way the worst kind.
This is way more trouble than I'm willing to take from any device.
Kill the funny state by receiving shared memory synchronously in
realize(). If your hot plug hangs, go kill your ivshmem server.
For easier review, this commit only makes the receive synchronous, it
doesn't add the necessary error propagation. Without that, the funny
state persists. The next commit will do that, and kill it off for
real.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1458066895-20632-26-git-send-email-armbru@redhat.com>
2016-03-15 21:34:40 +03:00
|
|
|
s1 = &state1;
|
2018-12-19 16:13:25 +03:00
|
|
|
setup_vm_with_server(&state2, nvectors);
|
ivshmem: Receive shared memory synchronously in realize()
When configured for interrupts (property "chardev" given), we receive
the shared memory from an ivshmem server. We do so asynchronously
after realize() completes, by setting up callbacks with
qemu_chr_add_handlers().
Keeping server I/O out of realize() that way avoids delays due to a
slow server. This is probably relevant only for hot plug.
However, this funny "no shared memory, yet" state of the device also
causes a raft of issues that are hard or impossible to work around:
* The guest is exposed to this state: when we enter and leave it its
shared memory contents is apruptly replaced, and device register
IVPosition changes.
This is a known issue. We document that guests should not access
the shared memory after device initialization until the IVPosition
register becomes non-negative.
For cold plug, the funny state is unlikely to be visible in
practice, because we normally receive the shared memory long before
the guest gets around to mess with the device.
For hot plug, the timing is tighter, but the relative slowness of
PCI device configuration has a good chance to hide the funny state.
In either case, guests complying with the documented procedure are
safe.
* Migration becomes racy.
If migration completes before the shared memory setup completes on
the source, shared memory contents is silently lost. Fortunately,
migration is rather unlikely to win this race.
If the shared memory's ramblock arrives at the destination before
shared memory setup completes, migration fails.
There is no known way for a management application to wait for
shared memory setup to complete.
All you can do is retry failed migration. You can improve your
chances by leaving more time between running the destination QEMU
and the migrate command.
To mitigate silent memory loss, you need to ensure the server
initializes shared memory exactly the same on source and
destination.
These issues are entirely undocumented so far.
I'd expect the server to be almost always fast enough to hide these
issues. But then rare catastrophic races are in a way the worst kind.
This is way more trouble than I'm willing to take from any device.
Kill the funny state by receiving shared memory synchronously in
realize(). If your hot plug hangs, go kill your ivshmem server.
For easier review, this commit only makes the receive synchronous, it
doesn't add the necessary error propagation. Without that, the funny
state persists. The next commit will do that, and kill it off for
real.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1458066895-20632-26-git-send-email-armbru@redhat.com>
2016-03-15 21:34:40 +03:00
|
|
|
s2 = &state2;
|
2014-04-02 18:57:48 +04:00
|
|
|
|
|
|
|
/* check got different VM ids */
|
|
|
|
vm1 = in_reg(s1, IVPOSITION);
|
|
|
|
vm2 = in_reg(s2, IVPOSITION);
|
ivshmem: Receive shared memory synchronously in realize()
When configured for interrupts (property "chardev" given), we receive
the shared memory from an ivshmem server. We do so asynchronously
after realize() completes, by setting up callbacks with
qemu_chr_add_handlers().
Keeping server I/O out of realize() that way avoids delays due to a
slow server. This is probably relevant only for hot plug.
However, this funny "no shared memory, yet" state of the device also
causes a raft of issues that are hard or impossible to work around:
* The guest is exposed to this state: when we enter and leave it its
shared memory contents is apruptly replaced, and device register
IVPosition changes.
This is a known issue. We document that guests should not access
the shared memory after device initialization until the IVPosition
register becomes non-negative.
For cold plug, the funny state is unlikely to be visible in
practice, because we normally receive the shared memory long before
the guest gets around to mess with the device.
For hot plug, the timing is tighter, but the relative slowness of
PCI device configuration has a good chance to hide the funny state.
In either case, guests complying with the documented procedure are
safe.
* Migration becomes racy.
If migration completes before the shared memory setup completes on
the source, shared memory contents is silently lost. Fortunately,
migration is rather unlikely to win this race.
If the shared memory's ramblock arrives at the destination before
shared memory setup completes, migration fails.
There is no known way for a management application to wait for
shared memory setup to complete.
All you can do is retry failed migration. You can improve your
chances by leaving more time between running the destination QEMU
and the migrate command.
To mitigate silent memory loss, you need to ensure the server
initializes shared memory exactly the same on source and
destination.
These issues are entirely undocumented so far.
I'd expect the server to be almost always fast enough to hide these
issues. But then rare catastrophic races are in a way the worst kind.
This is way more trouble than I'm willing to take from any device.
Kill the funny state by receiving shared memory synchronously in
realize(). If your hot plug hangs, go kill your ivshmem server.
For easier review, this commit only makes the receive synchronous, it
doesn't add the necessary error propagation. Without that, the funny
state persists. The next commit will do that, and kill it off for
real.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1458066895-20632-26-git-send-email-armbru@redhat.com>
2016-03-15 21:34:40 +03:00
|
|
|
g_assert_cmpint(vm1, >=, 0);
|
|
|
|
g_assert_cmpint(vm2, >=, 0);
|
|
|
|
g_assert_cmpint(vm1, !=, vm2);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2016-03-15 21:34:24 +03:00
|
|
|
/* check number of MSI-X vectors */
|
2018-12-19 16:13:25 +03:00
|
|
|
ret = qpci_msix_table_size(s1->dev);
|
|
|
|
g_assert_cmpuint(ret, ==, nvectors);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2016-03-15 21:34:24 +03:00
|
|
|
/* TODO test behavior before MSI-X is enabled */
|
|
|
|
|
|
|
|
/* ping vm2 -> vm1 on vector 0 */
|
2018-12-19 16:13:25 +03:00
|
|
|
ret = qpci_msix_pending(s1->dev, 0);
|
|
|
|
g_assert_cmpuint(ret, ==, 0);
|
2014-04-02 18:57:48 +04:00
|
|
|
out_reg(s2, DOORBELL, vm1 << 16);
|
|
|
|
do {
|
|
|
|
g_usleep(10000);
|
2018-12-19 16:13:25 +03:00
|
|
|
ret = qpci_msix_pending(s1->dev, 0);
|
2014-04-02 18:57:48 +04:00
|
|
|
} while (ret == 0 && g_get_monotonic_time() < end_time);
|
|
|
|
g_assert_cmpuint(ret, !=, 0);
|
|
|
|
|
2016-03-15 21:34:24 +03:00
|
|
|
/* ping vm1 -> vm2 on vector 1 */
|
2018-12-19 16:13:25 +03:00
|
|
|
ret = qpci_msix_pending(s2->dev, 1);
|
|
|
|
g_assert_cmpuint(ret, ==, 0);
|
2016-03-15 21:34:24 +03:00
|
|
|
out_reg(s1, DOORBELL, vm2 << 16 | 1);
|
2014-04-02 18:57:48 +04:00
|
|
|
do {
|
|
|
|
g_usleep(10000);
|
2018-12-19 16:13:25 +03:00
|
|
|
ret = qpci_msix_pending(s2->dev, 1);
|
2014-04-02 18:57:48 +04:00
|
|
|
} while (ret == 0 && g_get_monotonic_time() < end_time);
|
|
|
|
g_assert_cmpuint(ret, !=, 0);
|
|
|
|
|
2015-12-18 17:13:59 +03:00
|
|
|
cleanup_vm(s2);
|
|
|
|
cleanup_vm(s1);
|
2014-04-02 18:57:48 +04:00
|
|
|
|
|
|
|
if (qemu_write_full(thread.pipe[1], "q", 1) != 1) {
|
|
|
|
g_error("qemu_write_full: %s", g_strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
g_thread_join(thread.thread);
|
|
|
|
|
|
|
|
ivshmem_server_close(&server);
|
|
|
|
close(thread.pipe[1]);
|
|
|
|
close(thread.pipe[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PCI_SLOT_HP 0x06
|
|
|
|
|
|
|
|
static void test_ivshmem_hotplug(void)
|
|
|
|
{
|
2019-04-09 15:25:57 +03:00
|
|
|
QTestState *qts;
|
2017-01-05 18:29:48 +03:00
|
|
|
const char *arch = qtest_get_arch();
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2019-04-09 15:25:57 +03:00
|
|
|
qts = qtest_init("-object memory-backend-ram,size=1M,id=mb1");
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2019-07-22 18:10:55 +03:00
|
|
|
qtest_qmp_device_add(qts, "ivshmem-plain", "iv1",
|
2018-12-19 16:13:25 +03:00
|
|
|
"{'addr': %s, 'memdev': 'mb1'}",
|
|
|
|
stringify(PCI_SLOT_HP));
|
2017-01-05 18:29:48 +03:00
|
|
|
if (strcmp(arch, "ppc64") != 0) {
|
2019-04-09 15:25:57 +03:00
|
|
|
qpci_unplug_acpi_device_test(qts, "iv1", PCI_SLOT_HP);
|
2017-01-05 18:29:48 +03:00
|
|
|
}
|
2014-04-02 18:57:48 +04:00
|
|
|
|
2019-04-09 15:25:57 +03:00
|
|
|
qtest_quit(qts);
|
2014-04-02 18:57:48 +04:00
|
|
|
}
|
|
|
|
|
2015-06-30 01:10:16 +03:00
|
|
|
static void test_ivshmem_memdev(void)
|
|
|
|
{
|
|
|
|
IVState state;
|
|
|
|
|
|
|
|
/* just for the sake of checking memory-backend property */
|
|
|
|
setup_vm_cmd(&state, "-object memory-backend-ram,size=1M,id=mb1"
|
2016-03-15 21:34:51 +03:00
|
|
|
" -device ivshmem-plain,memdev=mb1", false);
|
2015-06-30 01:10:16 +03:00
|
|
|
|
2015-12-18 17:13:59 +03:00
|
|
|
cleanup_vm(&state);
|
2015-06-30 01:10:16 +03:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:57:48 +04:00
|
|
|
static void cleanup(void)
|
|
|
|
{
|
|
|
|
if (tmpshmem) {
|
|
|
|
munmap(tmpshmem, TMPSHMSIZE);
|
|
|
|
tmpshmem = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmpshm) {
|
|
|
|
shm_unlink(tmpshm);
|
|
|
|
g_free(tmpshm);
|
|
|
|
tmpshm = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmpserver) {
|
|
|
|
g_unlink(tmpserver);
|
|
|
|
g_free(tmpserver);
|
|
|
|
tmpserver = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmpdir) {
|
|
|
|
g_rmdir(tmpdir);
|
|
|
|
tmpdir = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void abrt_handler(void *data)
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *mktempshm(int size, int *fd)
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
gchar *name;
|
|
|
|
|
2019-12-12 04:17:58 +03:00
|
|
|
name = g_strdup_printf("/qtest-%u-%u", getpid(), g_test_rand_int());
|
2014-04-02 18:57:48 +04:00
|
|
|
*fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL,
|
|
|
|
S_IRWXU|S_IRWXG|S_IRWXO);
|
|
|
|
if (*fd > 0) {
|
|
|
|
g_assert(ftruncate(*fd, size) == 0);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(name);
|
|
|
|
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
perror("shm_open");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 01:18:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ret, fd;
|
2017-01-05 18:29:48 +03:00
|
|
|
const char *arch = qtest_get_arch();
|
2014-04-02 18:57:48 +04:00
|
|
|
gchar dir[] = "/tmp/ivshmem-test.XXXXXX";
|
|
|
|
|
2015-10-11 01:18:32 +03:00
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
2014-04-02 18:57:48 +04:00
|
|
|
qtest_add_abrt_handler(abrt_handler, NULL);
|
|
|
|
/* shm */
|
|
|
|
tmpshm = mktempshm(TMPSHMSIZE, &fd);
|
|
|
|
if (!tmpshm) {
|
2018-11-29 20:45:30 +03:00
|
|
|
goto out;
|
2014-04-02 18:57:48 +04:00
|
|
|
}
|
|
|
|
tmpshmem = mmap(0, TMPSHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
g_assert(tmpshmem != MAP_FAILED);
|
|
|
|
/* server */
|
|
|
|
if (mkdtemp(dir) == NULL) {
|
|
|
|
g_error("mkdtemp: %s", g_strerror(errno));
|
|
|
|
}
|
|
|
|
tmpdir = dir;
|
|
|
|
tmpserver = g_strconcat(tmpdir, "/server", NULL);
|
2015-10-11 01:18:32 +03:00
|
|
|
|
2014-04-02 18:57:48 +04:00
|
|
|
qtest_add_func("/ivshmem/single", test_ivshmem_single);
|
|
|
|
qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug);
|
2015-06-30 01:10:16 +03:00
|
|
|
qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev);
|
2015-11-12 14:10:18 +03:00
|
|
|
if (g_test_slow()) {
|
|
|
|
qtest_add_func("/ivshmem/pair", test_ivshmem_pair);
|
2017-01-05 18:29:48 +03:00
|
|
|
if (strcmp(arch, "ppc64") != 0) {
|
2018-12-19 16:13:25 +03:00
|
|
|
qtest_add_func("/ivshmem/server", test_ivshmem_server);
|
2017-01-05 18:29:48 +03:00
|
|
|
}
|
2015-11-12 14:10:18 +03:00
|
|
|
}
|
2015-10-11 01:18:32 +03:00
|
|
|
|
2018-11-29 20:45:30 +03:00
|
|
|
out:
|
2015-10-11 01:18:32 +03:00
|
|
|
ret = g_test_run();
|
2014-04-02 18:57:48 +04:00
|
|
|
cleanup();
|
2015-10-11 01:18:32 +03:00
|
|
|
return ret;
|
|
|
|
}
|