2014-06-10 12:03:23 +02:00
|
|
|
/*
|
|
|
|
* QTest testcase for the vhost-user
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Virtual Open Systems Sarl.
|
|
|
|
*
|
|
|
|
* 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 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2014-06-19 17:07:59 +02:00
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
#include "libqtest.h"
|
2016-10-22 11:52:55 +02:00
|
|
|
#include "qapi/error.h"
|
2016-12-12 18:22:24 +01:00
|
|
|
#include "qemu/config-file.h"
|
2014-06-10 12:03:23 +02:00
|
|
|
#include "qemu/option.h"
|
2015-10-09 17:17:39 +02:00
|
|
|
#include "qemu/range.h"
|
2016-06-22 19:11:19 +02:00
|
|
|
#include "qemu/sockets.h"
|
2017-01-26 15:26:44 +01:00
|
|
|
#include "chardev/char-fe.h"
|
2014-06-10 12:03:23 +02:00
|
|
|
#include "sysemu/sysemu.h"
|
2016-09-02 20:59:43 +02:00
|
|
|
#include "libqos/libqos.h"
|
|
|
|
#include "libqos/pci-pc.h"
|
|
|
|
#include "libqos/virtio-pci.h"
|
2016-10-22 11:52:52 +02:00
|
|
|
#include "qapi/error.h"
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2016-09-09 13:34:44 +02:00
|
|
|
#include "libqos/malloc-pc.h"
|
|
|
|
#include "hw/virtio/virtio-net.h"
|
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
#include <linux/vhost.h>
|
2016-09-02 20:59:43 +02:00
|
|
|
#include <linux/virtio_ids.h>
|
|
|
|
#include <linux/virtio_net.h>
|
2014-06-10 12:03:23 +02:00
|
|
|
#include <sys/vfs.h>
|
|
|
|
|
2017-09-05 20:06:02 +02:00
|
|
|
#define VHOST_USER_NET_TESTS_WORKING 0 /* broken as of 2.10.0 */
|
|
|
|
|
vhost-user-test: Fix 'make check' broken on glib < 2.26
After commit 89b516d8, some logics is turbid and
breaks 'make check' as below errors:
tests/vhost-user-test.c: In function '_cond_wait_until':
tests/vhost-user-test.c:154: error: 'G_TIME_SPAN_SECOND' undeclared (first use in this function)
tests/vhost-user-test.c:154: error: (Each undeclared identifier is reported only once
tests/vhost-user-test.c:154: error: for each function it appears in.)
tests/vhost-user-test.c: In function 'read_guest_mem':
tests/vhost-user-test.c:192: warning: implicit declaration of function 'g_get_monotonic_time'
tests/vhost-user-test.c:192: warning: nested extern declaration of 'g_get_monotonic_time'
tests/vhost-user-test.c:192: error: 'G_TIME_SPAN_SECOND' undeclared (first use in this function)
make: *** [tests/vhost-user-test.o] Error 1
First, vhost-usr-test.c rely on glib-compat.h because
of using G_TIME_SPAN_SECOND [glib < 2.26] and g_get_monotonic_time(),
but vhost-usr-test.c defined QEMU_GLIB_COMPAT_H, which make
glib-compat.h will not be included.
Second, if we remove QEMU_GLIB_COMPAT_H definability in
vhost-usr-test.c, then we will get below warnings:
tests/vhost-user-test.c: In function 'read_guest_mem':
tests/vhost-user-test.c:190: warning: passing argument 1 of 'g_mutex_lock' from incompatible pointer type
tests/vhost-user-test.c:234: warning: passing argument 1 of 'g_mutex_unlock' from incompatible pointer type
That's because glib-compat.h redefine the g_mutex_lock/unlock
function. Those functions' arguments is CompatGMutex/CompatGCond,
but vhost-user-test.c is using GMutex/GCond, which cause the type
is not consistent.
We can rerealize those functions of vhost-user-test.c,
which need a lots of patches. Let's simply address it, and
leave this file alone.
Signed-off-by: Gonglei <arei.gonglei@huawei.com>
Message-id: 1415149259-6188-1-git-send-email-arei.gonglei@huawei.com
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
2014-11-05 02:00:59 +01:00
|
|
|
/* GLIB version compatibility flags */
|
|
|
|
#if !GLIB_CHECK_VERSION(2, 26, 0)
|
|
|
|
#define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if GLIB_CHECK_VERSION(2, 28, 0)
|
|
|
|
#define HAVE_MONOTONIC_TIME
|
|
|
|
#endif
|
|
|
|
|
2015-10-09 17:17:38 +02:00
|
|
|
#define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
|
2014-06-10 12:03:23 +02:00
|
|
|
"mem-path=%s,share=on -numa node,memdev=mem"
|
2016-06-06 18:45:08 +02:00
|
|
|
#define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
|
2015-10-09 17:17:38 +02:00
|
|
|
#define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
|
2016-09-02 20:59:43 +02:00
|
|
|
#define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2016-09-02 20:59:43 +02:00
|
|
|
#define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
|
vhost-user-test: use correct ROM to speed up and avoid spurious failures
The mechanism to get the option ROM for virtio-net does not block the
PCI ROM from being loaded. Therefore, in vhost-user-test there are
two entries in the boot menu for the virtio-net card: one as an
embedded option ROM, one from the ROM BAR.
The embedded option ROM in vhost-user-test is the non-EFI-enabled,
while the ROM BAR has an EFI-enabled ROM. The two are compiled with
slightly different parameters, where only the old BIOS-only one doesn't
have a timeout for the "Press Ctrl-B" banner. When using a new
machine type, therefore, the vhost-user-test has to wait for the
EFI-enabled ROM's banner to go away. There are several ways to fix
this:
1) fix the ROMs to have the same configuration
2) add ",romfile=" to the -device line
3) remove --option-rom and add the ROM file name to the -device line
4) use an old machine type
This patch chooses 3. In addition, the file name was wrong because
qtest runs QEMU relative to the top build directory, not to the
x86_64-softmmu/ subdirectory, which is fixed too.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
2016-02-04 16:00:52 +01:00
|
|
|
QEMU_CMD_NETDEV QEMU_CMD_NET
|
2014-06-10 12:03:23 +02:00
|
|
|
|
|
|
|
#define HUGETLBFS_MAGIC 0x958458f6
|
|
|
|
|
|
|
|
/*********** FROM hw/virtio/vhost-user.c *************************************/
|
|
|
|
|
|
|
|
#define VHOST_MEMORY_MAX_NREGIONS 8
|
|
|
|
|
2015-09-24 18:22:01 +02:00
|
|
|
#define VHOST_USER_F_PROTOCOL_FEATURES 30
|
2016-09-09 13:34:44 +02:00
|
|
|
#define VHOST_USER_PROTOCOL_F_MQ 0
|
2015-10-09 17:17:39 +02:00
|
|
|
#define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
|
|
|
|
|
|
|
|
#define VHOST_LOG_PAGE 0x1000
|
2015-09-24 18:22:01 +02:00
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
typedef enum VhostUserRequest {
|
|
|
|
VHOST_USER_NONE = 0,
|
|
|
|
VHOST_USER_GET_FEATURES = 1,
|
|
|
|
VHOST_USER_SET_FEATURES = 2,
|
|
|
|
VHOST_USER_SET_OWNER = 3,
|
2015-11-11 14:24:37 +01:00
|
|
|
VHOST_USER_RESET_OWNER = 4,
|
2014-06-10 12:03:23 +02:00
|
|
|
VHOST_USER_SET_MEM_TABLE = 5,
|
|
|
|
VHOST_USER_SET_LOG_BASE = 6,
|
|
|
|
VHOST_USER_SET_LOG_FD = 7,
|
|
|
|
VHOST_USER_SET_VRING_NUM = 8,
|
|
|
|
VHOST_USER_SET_VRING_ADDR = 9,
|
|
|
|
VHOST_USER_SET_VRING_BASE = 10,
|
|
|
|
VHOST_USER_GET_VRING_BASE = 11,
|
|
|
|
VHOST_USER_SET_VRING_KICK = 12,
|
|
|
|
VHOST_USER_SET_VRING_CALL = 13,
|
|
|
|
VHOST_USER_SET_VRING_ERR = 14,
|
2015-09-24 18:22:01 +02:00
|
|
|
VHOST_USER_GET_PROTOCOL_FEATURES = 15,
|
|
|
|
VHOST_USER_SET_PROTOCOL_FEATURES = 16,
|
2016-09-09 13:34:44 +02:00
|
|
|
VHOST_USER_GET_QUEUE_NUM = 17,
|
2015-11-16 12:33:36 +01:00
|
|
|
VHOST_USER_SET_VRING_ENABLE = 18,
|
2014-06-10 12:03:23 +02:00
|
|
|
VHOST_USER_MAX
|
|
|
|
} VhostUserRequest;
|
|
|
|
|
|
|
|
typedef struct VhostUserMemoryRegion {
|
|
|
|
uint64_t guest_phys_addr;
|
|
|
|
uint64_t memory_size;
|
|
|
|
uint64_t userspace_addr;
|
2014-07-12 03:43:19 +02:00
|
|
|
uint64_t mmap_offset;
|
2014-06-10 12:03:23 +02:00
|
|
|
} VhostUserMemoryRegion;
|
|
|
|
|
|
|
|
typedef struct VhostUserMemory {
|
|
|
|
uint32_t nregions;
|
|
|
|
uint32_t padding;
|
|
|
|
VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
|
|
|
|
} VhostUserMemory;
|
|
|
|
|
2015-11-11 15:26:02 +01:00
|
|
|
typedef struct VhostUserLog {
|
|
|
|
uint64_t mmap_size;
|
|
|
|
uint64_t mmap_offset;
|
|
|
|
} VhostUserLog;
|
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
typedef struct VhostUserMsg {
|
|
|
|
VhostUserRequest request;
|
|
|
|
|
|
|
|
#define VHOST_USER_VERSION_MASK (0x3)
|
|
|
|
#define VHOST_USER_REPLY_MASK (0x1<<2)
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t size; /* the following payload size */
|
|
|
|
union {
|
2015-11-11 15:26:02 +01:00
|
|
|
#define VHOST_USER_VRING_IDX_MASK (0xff)
|
|
|
|
#define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
|
2014-06-10 12:03:23 +02:00
|
|
|
uint64_t u64;
|
|
|
|
struct vhost_vring_state state;
|
|
|
|
struct vhost_vring_addr addr;
|
|
|
|
VhostUserMemory memory;
|
2015-11-11 15:26:02 +01:00
|
|
|
VhostUserLog log;
|
2015-10-22 21:28:37 +02:00
|
|
|
} payload;
|
2014-06-10 12:03:23 +02:00
|
|
|
} QEMU_PACKED VhostUserMsg;
|
|
|
|
|
|
|
|
static VhostUserMsg m __attribute__ ((unused));
|
|
|
|
#define VHOST_USER_HDR_SIZE (sizeof(m.request) \
|
|
|
|
+ sizeof(m.flags) \
|
|
|
|
+ sizeof(m.size))
|
|
|
|
|
|
|
|
#define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
|
|
|
|
|
|
|
|
/* The version of the protocol we support */
|
|
|
|
#define VHOST_USER_VERSION (0x1)
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2016-09-09 13:34:45 +02:00
|
|
|
enum {
|
|
|
|
TEST_FLAGS_OK,
|
|
|
|
TEST_FLAGS_DISCONNECT,
|
|
|
|
TEST_FLAGS_BAD,
|
|
|
|
TEST_FLAGS_END,
|
|
|
|
};
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
typedef struct TestServer {
|
2017-02-03 13:06:12 +01:00
|
|
|
QPCIBus *bus;
|
2015-10-09 17:17:37 +02:00
|
|
|
gchar *socket_path;
|
2015-11-27 15:41:19 +01:00
|
|
|
gchar *mig_path;
|
2015-10-09 17:17:37 +02:00
|
|
|
gchar *chr_name;
|
2016-10-22 11:52:52 +02:00
|
|
|
CharBackend chr;
|
2015-10-09 17:17:37 +02:00
|
|
|
int fds_num;
|
|
|
|
int fds[VHOST_MEMORY_MAX_NREGIONS];
|
|
|
|
VhostUserMemory memory;
|
2016-06-28 18:32:42 +02:00
|
|
|
CompatGMutex data_mutex;
|
|
|
|
CompatGCond data_cond;
|
2015-10-09 17:17:39 +02:00
|
|
|
int log_fd;
|
2015-11-26 14:14:02 +01:00
|
|
|
uint64_t rings;
|
2016-09-09 13:34:43 +02:00
|
|
|
bool test_fail;
|
2016-09-09 13:34:45 +02:00
|
|
|
int test_flags;
|
2016-09-09 13:34:44 +02:00
|
|
|
int queues;
|
2015-10-09 17:17:37 +02:00
|
|
|
} TestServer;
|
2014-06-19 17:07:59 +02:00
|
|
|
|
2015-10-09 17:17:38 +02:00
|
|
|
static const char *tmpfs;
|
|
|
|
static const char *root;
|
|
|
|
|
2016-09-02 20:59:43 +02:00
|
|
|
static void init_virtio_dev(TestServer *s)
|
|
|
|
{
|
|
|
|
QVirtioPCIDevice *dev;
|
|
|
|
uint32_t features;
|
|
|
|
|
2017-02-03 13:06:12 +01:00
|
|
|
s->bus = qpci_init_pc(NULL);
|
|
|
|
g_assert_nonnull(s->bus);
|
2016-09-02 20:59:43 +02:00
|
|
|
|
2017-02-03 13:06:12 +01:00
|
|
|
dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
|
2016-09-02 20:59:43 +02:00
|
|
|
g_assert_nonnull(dev);
|
|
|
|
|
|
|
|
qvirtio_pci_device_enable(dev);
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_reset(&dev->vdev);
|
|
|
|
qvirtio_set_acknowledge(&dev->vdev);
|
|
|
|
qvirtio_set_driver(&dev->vdev);
|
2016-09-02 20:59:43 +02:00
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
features = qvirtio_get_features(&dev->vdev);
|
2016-09-02 20:59:43 +02:00
|
|
|
features = features & VIRTIO_NET_F_MAC;
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_set_features(&dev->vdev, features);
|
2016-09-02 20:59:43 +02:00
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_set_driver_ok(&dev->vdev);
|
2017-02-03 13:06:12 +01:00
|
|
|
qvirtio_pci_device_free(dev);
|
2016-09-02 20:59:43 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
static void wait_for_fds(TestServer *s)
|
2014-06-10 12:03:23 +02:00
|
|
|
{
|
|
|
|
gint64 end_time;
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
g_mutex_lock(&s->data_mutex);
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2015-09-29 14:12:03 +02:00
|
|
|
end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
|
2015-10-09 17:17:37 +02:00
|
|
|
while (!s->fds_num) {
|
|
|
|
if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
|
2014-06-10 12:03:23 +02:00
|
|
|
/* timeout has passed */
|
2015-10-09 17:17:37 +02:00
|
|
|
g_assert(s->fds_num);
|
2014-06-10 12:03:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for sanity */
|
2015-10-09 17:17:37 +02:00
|
|
|
g_assert_cmpint(s->fds_num, >, 0);
|
|
|
|
g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
g_mutex_unlock(&s->data_mutex);
|
2015-10-09 17:17:35 +02:00
|
|
|
}
|
|
|
|
|
2015-12-02 21:20:33 +01:00
|
|
|
static void read_guest_mem(const void *data)
|
2015-10-09 17:17:35 +02:00
|
|
|
{
|
2015-12-02 21:20:33 +01:00
|
|
|
TestServer *s = (void *)data;
|
2015-10-09 17:17:35 +02:00
|
|
|
uint32_t *guest_mem;
|
|
|
|
int i, j;
|
|
|
|
size_t size;
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
wait_for_fds(s);
|
2015-10-09 17:17:35 +02:00
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
g_mutex_lock(&s->data_mutex);
|
2015-10-09 17:17:35 +02:00
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
/* iterate all regions */
|
2015-10-09 17:17:37 +02:00
|
|
|
for (i = 0; i < s->fds_num; i++) {
|
2014-06-10 12:03:23 +02:00
|
|
|
|
|
|
|
/* We'll check only the region statring at 0x0*/
|
2015-10-09 17:17:37 +02:00
|
|
|
if (s->memory.regions[i].guest_phys_addr != 0x0) {
|
2014-06-10 12:03:23 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
size = s->memory.regions[i].memory_size +
|
|
|
|
s->memory.regions[i].mmap_offset;
|
2014-07-12 03:43:19 +02:00
|
|
|
|
|
|
|
guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
|
2015-10-09 17:17:37 +02:00
|
|
|
MAP_SHARED, s->fds[i], 0);
|
2014-07-12 03:43:19 +02:00
|
|
|
|
|
|
|
g_assert(guest_mem != MAP_FAILED);
|
2015-10-09 17:17:37 +02:00
|
|
|
guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
|
2014-06-10 12:03:23 +02:00
|
|
|
|
|
|
|
for (j = 0; j < 256; j++) {
|
2015-10-09 17:17:37 +02:00
|
|
|
uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
|
2014-06-10 12:03:23 +02:00
|
|
|
uint32_t b = guest_mem[j];
|
|
|
|
|
|
|
|
g_assert_cmpint(a, ==, b);
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
munmap(guest_mem, s->memory.regions[i].memory_size);
|
2014-06-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
g_mutex_unlock(&s->data_mutex);
|
2014-06-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *thread_function(void *data)
|
|
|
|
{
|
2015-11-27 15:41:18 +01:00
|
|
|
GMainLoop *loop = data;
|
2014-06-10 12:03:23 +02:00
|
|
|
g_main_loop_run(loop);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chr_can_read(void *opaque)
|
|
|
|
{
|
|
|
|
return VHOST_USER_HDR_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void chr_read(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
2015-10-09 17:17:37 +02:00
|
|
|
TestServer *s = opaque;
|
2016-10-22 11:52:52 +02:00
|
|
|
CharBackend *chr = &s->chr;
|
2014-06-10 12:03:23 +02:00
|
|
|
VhostUserMsg msg;
|
|
|
|
uint8_t *p = (uint8_t *) &msg;
|
|
|
|
int fd;
|
|
|
|
|
2016-09-09 13:34:43 +02:00
|
|
|
if (s->test_fail) {
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_disconnect(chr);
|
2016-09-09 13:34:43 +02:00
|
|
|
/* now switch to non-failure */
|
|
|
|
s->test_fail = false;
|
|
|
|
}
|
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
if (size != VHOST_USER_HDR_SIZE) {
|
|
|
|
g_test_message("Wrong message size received %d\n", size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
g_mutex_lock(&s->data_mutex);
|
2014-06-10 12:03:23 +02:00
|
|
|
memcpy(p, buf, VHOST_USER_HDR_SIZE);
|
|
|
|
|
|
|
|
if (msg.size) {
|
|
|
|
p += VHOST_USER_HDR_SIZE;
|
2016-10-22 11:52:55 +02:00
|
|
|
size = qemu_chr_fe_read_all(chr, p, msg.size);
|
2016-06-06 18:45:08 +02:00
|
|
|
if (size != msg.size) {
|
|
|
|
g_test_message("Wrong message size received %d != %d\n",
|
|
|
|
size, msg.size);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg.request) {
|
|
|
|
case VHOST_USER_GET_FEATURES:
|
2015-09-24 18:22:01 +02:00
|
|
|
/* send back features to qemu */
|
|
|
|
msg.flags |= VHOST_USER_REPLY_MASK;
|
2015-10-22 21:28:37 +02:00
|
|
|
msg.size = sizeof(m.payload.u64);
|
|
|
|
msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
|
2015-10-09 17:17:39 +02:00
|
|
|
0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
|
2016-09-09 13:34:44 +02:00
|
|
|
if (s->queues > 1) {
|
|
|
|
msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
|
|
|
|
}
|
2016-09-09 13:34:45 +02:00
|
|
|
if (s->test_flags >= TEST_FLAGS_BAD) {
|
|
|
|
msg.payload.u64 = 0;
|
|
|
|
s->test_flags = TEST_FLAGS_END;
|
|
|
|
}
|
2015-09-24 18:22:01 +02:00
|
|
|
p = (uint8_t *) &msg;
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
|
2015-09-24 18:22:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_FEATURES:
|
2015-10-22 21:28:37 +02:00
|
|
|
g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
|
2015-09-24 18:22:01 +02:00
|
|
|
!=, 0ULL);
|
2016-09-09 13:34:45 +02:00
|
|
|
if (s->test_flags == TEST_FLAGS_DISCONNECT) {
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_disconnect(chr);
|
2016-09-09 13:34:45 +02:00
|
|
|
s->test_flags = TEST_FLAGS_BAD;
|
|
|
|
}
|
2015-09-24 18:22:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_GET_PROTOCOL_FEATURES:
|
2014-06-10 12:03:23 +02:00
|
|
|
/* send back features to qemu */
|
|
|
|
msg.flags |= VHOST_USER_REPLY_MASK;
|
2015-10-22 21:28:37 +02:00
|
|
|
msg.size = sizeof(m.payload.u64);
|
|
|
|
msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
|
2016-09-09 13:34:44 +02:00
|
|
|
if (s->queues > 1) {
|
|
|
|
msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
|
|
|
|
}
|
2014-06-10 12:03:23 +02:00
|
|
|
p = (uint8_t *) &msg;
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
|
2014-06-10 12:03:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_GET_VRING_BASE:
|
|
|
|
/* send back vring base to qemu */
|
|
|
|
msg.flags |= VHOST_USER_REPLY_MASK;
|
2015-10-22 21:28:37 +02:00
|
|
|
msg.size = sizeof(m.payload.state);
|
|
|
|
msg.payload.state.num = 0;
|
2014-06-10 12:03:23 +02:00
|
|
|
p = (uint8_t *) &msg;
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
|
2015-11-26 14:14:02 +01:00
|
|
|
|
2016-09-09 13:34:44 +02:00
|
|
|
assert(msg.payload.state.index < s->queues * 2);
|
2015-11-26 14:14:02 +01:00
|
|
|
s->rings &= ~(0x1ULL << msg.payload.state.index);
|
2014-06-10 12:03:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_MEM_TABLE:
|
|
|
|
/* received the mem table */
|
2015-10-22 21:28:37 +02:00
|
|
|
memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
|
2016-10-22 11:52:55 +02:00
|
|
|
s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
|
2016-10-22 11:52:52 +02:00
|
|
|
G_N_ELEMENTS(s->fds));
|
2014-06-10 12:03:23 +02:00
|
|
|
|
|
|
|
/* signal the test that it can continue */
|
2015-10-09 17:17:37 +02:00
|
|
|
g_cond_signal(&s->data_cond);
|
2014-06-10 12:03:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case VHOST_USER_SET_VRING_KICK:
|
|
|
|
case VHOST_USER_SET_VRING_CALL:
|
|
|
|
/* consume the fd */
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_get_msgfds(chr, &fd, 1);
|
2014-06-10 12:03:23 +02:00
|
|
|
/*
|
|
|
|
* This is a non-blocking eventfd.
|
|
|
|
* The receive function forces it to be blocking,
|
|
|
|
* so revert it back to non-blocking.
|
|
|
|
*/
|
|
|
|
qemu_set_nonblock(fd);
|
|
|
|
break;
|
2015-10-09 17:17:39 +02:00
|
|
|
|
|
|
|
case VHOST_USER_SET_LOG_BASE:
|
|
|
|
if (s->log_fd != -1) {
|
|
|
|
close(s->log_fd);
|
|
|
|
s->log_fd = -1;
|
|
|
|
}
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
|
2015-10-09 17:17:39 +02:00
|
|
|
msg.flags |= VHOST_USER_REPLY_MASK;
|
|
|
|
msg.size = 0;
|
|
|
|
p = (uint8_t *) &msg;
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
|
2015-10-09 17:17:39 +02:00
|
|
|
|
|
|
|
g_cond_signal(&s->data_cond);
|
|
|
|
break;
|
|
|
|
|
2015-11-26 14:14:02 +01:00
|
|
|
case VHOST_USER_SET_VRING_BASE:
|
2016-09-09 13:34:44 +02:00
|
|
|
assert(msg.payload.state.index < s->queues * 2);
|
2015-11-26 14:14:02 +01:00
|
|
|
s->rings |= 0x1ULL << msg.payload.state.index;
|
2015-10-09 17:17:40 +02:00
|
|
|
break;
|
|
|
|
|
2016-09-09 13:34:44 +02:00
|
|
|
case VHOST_USER_GET_QUEUE_NUM:
|
|
|
|
msg.flags |= VHOST_USER_REPLY_MASK;
|
|
|
|
msg.size = sizeof(m.payload.u64);
|
|
|
|
msg.payload.u64 = s->queues;
|
|
|
|
p = (uint8_t *) &msg;
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
|
2016-09-09 13:34:44 +02:00
|
|
|
break;
|
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-10-09 17:17:37 +02:00
|
|
|
|
|
|
|
g_mutex_unlock(&s->data_mutex);
|
2014-06-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 17:01:21 +02:00
|
|
|
static const char *init_hugepagefs(const char *path)
|
2014-06-10 12:03:23 +02:00
|
|
|
{
|
|
|
|
struct statfs fs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (access(path, R_OK | W_OK | X_OK)) {
|
|
|
|
g_test_message("access on path (%s): %s\n", path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = statfs(path, &fs);
|
|
|
|
} while (ret != 0 && errno == EINTR);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fs.f_type != HUGETLBFS_MAGIC) {
|
|
|
|
g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:38 +02:00
|
|
|
static TestServer *test_server_new(const gchar *name)
|
2015-10-09 17:17:37 +02:00
|
|
|
{
|
|
|
|
TestServer *server = g_new0(TestServer, 1);
|
|
|
|
|
|
|
|
server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
|
2015-11-27 15:41:19 +01:00
|
|
|
server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
|
2015-10-09 17:17:37 +02:00
|
|
|
server->chr_name = g_strdup_printf("chr-%s", name);
|
|
|
|
|
|
|
|
g_mutex_init(&server->data_mutex);
|
|
|
|
g_cond_init(&server->data_cond);
|
|
|
|
|
2015-10-09 17:17:39 +02:00
|
|
|
server->log_fd = -1;
|
2016-09-09 13:34:44 +02:00
|
|
|
server->queues = 1;
|
2015-10-09 17:17:39 +02:00
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
2016-09-09 13:34:45 +02:00
|
|
|
static void chr_event(void *opaque, int event)
|
|
|
|
{
|
|
|
|
TestServer *s = opaque;
|
|
|
|
|
|
|
|
if (s->test_flags == TEST_FLAGS_END &&
|
|
|
|
event == CHR_EVENT_CLOSED) {
|
|
|
|
s->test_flags = TEST_FLAGS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:45:08 +02:00
|
|
|
static void test_server_create_chr(TestServer *server, const gchar *opt)
|
|
|
|
{
|
|
|
|
gchar *chr_path;
|
2016-12-07 14:20:22 +01:00
|
|
|
Chardev *chr;
|
2016-10-22 11:52:55 +02:00
|
|
|
|
2016-06-06 18:45:08 +02:00
|
|
|
chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
|
2016-10-22 11:52:52 +02:00
|
|
|
chr = qemu_chr_new(server->chr_name, chr_path);
|
2016-06-06 18:45:08 +02:00
|
|
|
g_free(chr_path);
|
|
|
|
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_init(&server->chr, chr, &error_abort);
|
|
|
|
qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
|
2017-07-06 14:08:49 +02:00
|
|
|
chr_event, NULL, server, NULL, true);
|
2016-06-06 18:45:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_server_listen(TestServer *server)
|
|
|
|
{
|
|
|
|
test_server_create_chr(server, ",server,nowait");
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GET_QEMU_CMD(s) \
|
|
|
|
g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
|
|
|
|
(s)->socket_path, "", (s)->chr_name)
|
|
|
|
|
|
|
|
#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
|
|
|
|
g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
|
|
|
|
(s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
|
2015-10-09 17:17:37 +02:00
|
|
|
|
2015-11-27 15:41:18 +01:00
|
|
|
static gboolean _test_server_free(TestServer *server)
|
2015-10-09 17:17:37 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-01-26 21:49:13 +01:00
|
|
|
qemu_chr_fe_deinit(&server->chr, true);
|
2015-10-09 17:17:37 +02:00
|
|
|
|
|
|
|
for (i = 0; i < server->fds_num; i++) {
|
|
|
|
close(server->fds[i]);
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:39 +02:00
|
|
|
if (server->log_fd != -1) {
|
|
|
|
close(server->log_fd);
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
unlink(server->socket_path);
|
|
|
|
g_free(server->socket_path);
|
|
|
|
|
2015-11-27 15:41:19 +01:00
|
|
|
unlink(server->mig_path);
|
|
|
|
g_free(server->mig_path);
|
|
|
|
|
2015-10-09 17:17:39 +02:00
|
|
|
g_free(server->chr_name);
|
2017-02-03 13:06:12 +01:00
|
|
|
qpci_free_pc(server->bus);
|
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
g_free(server);
|
2015-11-27 15:41:18 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_server_free(TestServer *server)
|
|
|
|
{
|
|
|
|
g_idle_add((GSourceFunc)_test_server_free, server);
|
2015-10-09 17:17:37 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:39 +02:00
|
|
|
static void wait_for_log_fd(TestServer *s)
|
|
|
|
{
|
|
|
|
gint64 end_time;
|
|
|
|
|
|
|
|
g_mutex_lock(&s->data_mutex);
|
|
|
|
end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
|
|
|
|
while (s->log_fd == -1) {
|
|
|
|
if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
|
|
|
|
/* timeout has passed */
|
|
|
|
g_assert(s->log_fd != -1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_unlock(&s->data_mutex);
|
|
|
|
}
|
|
|
|
|
2016-01-22 16:09:21 +01:00
|
|
|
static void write_guest_mem(TestServer *s, uint32_t seed)
|
2015-10-09 17:17:39 +02:00
|
|
|
{
|
|
|
|
uint32_t *guest_mem;
|
|
|
|
int i, j;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
wait_for_fds(s);
|
|
|
|
|
|
|
|
/* iterate all regions */
|
|
|
|
for (i = 0; i < s->fds_num; i++) {
|
|
|
|
|
|
|
|
/* We'll write only the region statring at 0x0 */
|
|
|
|
if (s->memory.regions[i].guest_phys_addr != 0x0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
|
|
|
|
|
|
|
|
size = s->memory.regions[i].memory_size +
|
|
|
|
s->memory.regions[i].mmap_offset;
|
|
|
|
|
|
|
|
guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, s->fds[i], 0);
|
|
|
|
|
|
|
|
g_assert(guest_mem != MAP_FAILED);
|
|
|
|
guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
|
|
|
|
|
|
|
|
for (j = 0; j < 256; j++) {
|
|
|
|
guest_mem[j] = seed + j;
|
|
|
|
}
|
|
|
|
|
|
|
|
munmap(guest_mem, s->memory.regions[i].memory_size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint64 get_log_size(TestServer *s)
|
|
|
|
{
|
|
|
|
guint64 log_size = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < s->memory.nregions; ++i) {
|
|
|
|
VhostUserMemoryRegion *reg = &s->memory.regions[i];
|
|
|
|
guint64 last = range_get_last(reg->guest_phys_addr,
|
|
|
|
reg->memory_size);
|
|
|
|
log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return log_size;
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:40 +02:00
|
|
|
typedef struct TestMigrateSource {
|
|
|
|
GSource source;
|
|
|
|
TestServer *src;
|
|
|
|
TestServer *dest;
|
|
|
|
} TestMigrateSource;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
test_migrate_source_check(GSource *source)
|
|
|
|
{
|
|
|
|
TestMigrateSource *t = (TestMigrateSource *)source;
|
2015-11-26 14:14:02 +01:00
|
|
|
gboolean overlap = t->src->rings && t->dest->rings;
|
2015-10-09 17:17:40 +02:00
|
|
|
|
|
|
|
g_assert(!overlap);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:44:49 +01:00
|
|
|
#if !GLIB_CHECK_VERSION(2,36,0)
|
|
|
|
/* this callback is unnecessary with glib >2.36, the default
|
|
|
|
* prepare for the source does the same */
|
|
|
|
static gboolean
|
|
|
|
test_migrate_source_prepare(GSource *source, gint *timeout)
|
|
|
|
{
|
|
|
|
*timeout = -1;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-09 17:17:40 +02:00
|
|
|
GSourceFuncs test_migrate_source_funcs = {
|
2015-11-30 17:44:49 +01:00
|
|
|
#if !GLIB_CHECK_VERSION(2,36,0)
|
|
|
|
.prepare = test_migrate_source_prepare,
|
|
|
|
#endif
|
|
|
|
.check = test_migrate_source_check,
|
2015-10-09 17:17:40 +02:00
|
|
|
};
|
|
|
|
|
2015-10-09 17:17:39 +02:00
|
|
|
static void test_migrate(void)
|
|
|
|
{
|
|
|
|
TestServer *s = test_server_new("src");
|
|
|
|
TestServer *dest = test_server_new("dest");
|
2015-11-27 15:41:19 +01:00
|
|
|
char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
|
2015-10-09 17:17:39 +02:00
|
|
|
QTestState *global = global_qtest, *from, *to;
|
2015-10-09 17:17:40 +02:00
|
|
|
GSource *source;
|
2015-10-09 17:17:39 +02:00
|
|
|
gchar *cmd;
|
|
|
|
QDict *rsp;
|
|
|
|
guint8 *log;
|
|
|
|
guint64 size;
|
|
|
|
|
2016-06-06 18:45:08 +02:00
|
|
|
test_server_listen(s);
|
|
|
|
test_server_listen(dest);
|
|
|
|
|
|
|
|
cmd = GET_QEMU_CMDE(s, 2, "", "");
|
2015-10-09 17:17:39 +02:00
|
|
|
from = qtest_start(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
|
2016-09-02 20:59:43 +02:00
|
|
|
init_virtio_dev(s);
|
2015-10-09 17:17:39 +02:00
|
|
|
wait_for_fds(s);
|
|
|
|
size = get_log_size(s);
|
|
|
|
g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
|
|
|
|
|
2016-06-06 18:45:08 +02:00
|
|
|
cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
|
2015-10-09 17:17:39 +02:00
|
|
|
to = qtest_init(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
|
2015-10-09 17:17:40 +02:00
|
|
|
source = g_source_new(&test_migrate_source_funcs,
|
|
|
|
sizeof(TestMigrateSource));
|
|
|
|
((TestMigrateSource *)source)->src = s;
|
|
|
|
((TestMigrateSource *)source)->dest = dest;
|
|
|
|
g_source_attach(source, NULL);
|
|
|
|
|
2015-10-09 17:17:39 +02:00
|
|
|
/* slow down migration to have time to fiddle with log */
|
|
|
|
/* TODO: qtest could learn to break on some places */
|
|
|
|
rsp = qmp("{ 'execute': 'migrate_set_speed',"
|
|
|
|
"'arguments': { 'value': 10 } }");
|
|
|
|
g_assert(qdict_haskey(rsp, "return"));
|
|
|
|
QDECREF(rsp);
|
|
|
|
|
|
|
|
cmd = g_strdup_printf("{ 'execute': 'migrate',"
|
|
|
|
"'arguments': { 'uri': '%s' } }",
|
|
|
|
uri);
|
|
|
|
rsp = qmp(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
g_assert(qdict_haskey(rsp, "return"));
|
|
|
|
QDECREF(rsp);
|
|
|
|
|
|
|
|
wait_for_log_fd(s);
|
|
|
|
|
|
|
|
log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
|
|
|
|
g_assert(log != MAP_FAILED);
|
|
|
|
|
|
|
|
/* modify first page */
|
|
|
|
write_guest_mem(s, 0x42);
|
|
|
|
log[0] = 1;
|
|
|
|
munmap(log, size);
|
|
|
|
|
|
|
|
/* speed things up */
|
|
|
|
rsp = qmp("{ 'execute': 'migrate_set_speed',"
|
|
|
|
"'arguments': { 'value': 0 } }");
|
|
|
|
g_assert(qdict_haskey(rsp, "return"));
|
|
|
|
QDECREF(rsp);
|
|
|
|
|
|
|
|
qmp_eventwait("STOP");
|
|
|
|
|
|
|
|
global_qtest = to;
|
|
|
|
qmp_eventwait("RESUME");
|
|
|
|
|
|
|
|
read_guest_mem(dest);
|
|
|
|
|
2015-10-09 17:17:40 +02:00
|
|
|
g_source_destroy(source);
|
|
|
|
g_source_unref(source);
|
|
|
|
|
2015-10-09 17:17:39 +02:00
|
|
|
qtest_quit(to);
|
|
|
|
test_server_free(dest);
|
|
|
|
qtest_quit(from);
|
|
|
|
test_server_free(s);
|
2015-11-27 15:41:19 +01:00
|
|
|
g_free(uri);
|
2015-10-09 17:17:39 +02:00
|
|
|
|
|
|
|
global_qtest = global;
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:45:08 +02:00
|
|
|
static void wait_for_rings_started(TestServer *s, size_t count)
|
|
|
|
{
|
|
|
|
gint64 end_time;
|
|
|
|
|
|
|
|
g_mutex_lock(&s->data_mutex);
|
|
|
|
end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
|
|
|
|
while (ctpop64(s->rings) != count) {
|
|
|
|
if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
|
|
|
|
/* timeout has passed */
|
|
|
|
g_assert_cmpint(ctpop64(s->rings), ==, count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_mutex_unlock(&s->data_mutex);
|
|
|
|
}
|
|
|
|
|
2017-09-05 20:06:02 +02:00
|
|
|
#if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
|
|
|
|
static inline void test_server_connect(TestServer *server)
|
|
|
|
{
|
|
|
|
test_server_create_chr(server, ",reconnect=1");
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:45:08 +02:00
|
|
|
static gboolean
|
|
|
|
reconnect_cb(gpointer user_data)
|
|
|
|
{
|
|
|
|
TestServer *s = user_data;
|
|
|
|
|
2016-10-22 11:52:55 +02:00
|
|
|
qemu_chr_fe_disconnect(&s->chr);
|
2016-06-06 18:45:08 +02:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
connect_thread(gpointer data)
|
|
|
|
{
|
|
|
|
TestServer *s = data;
|
|
|
|
|
|
|
|
/* wait for qemu to start before first try, to avoid extra warnings */
|
|
|
|
g_usleep(G_USEC_PER_SEC);
|
|
|
|
test_server_connect(s);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_reconnect_subprocess(void)
|
|
|
|
{
|
|
|
|
TestServer *s = test_server_new("reconnect");
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
g_thread_new("connect", connect_thread, s);
|
|
|
|
cmd = GET_QEMU_CMDE(s, 2, ",server", "");
|
|
|
|
qtest_start(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
|
2016-09-02 20:59:43 +02:00
|
|
|
init_virtio_dev(s);
|
2016-06-06 18:45:08 +02:00
|
|
|
wait_for_fds(s);
|
|
|
|
wait_for_rings_started(s, 2);
|
|
|
|
|
|
|
|
/* reconnect */
|
|
|
|
s->fds_num = 0;
|
|
|
|
s->rings = 0;
|
|
|
|
g_idle_add(reconnect_cb, s);
|
|
|
|
wait_for_fds(s);
|
|
|
|
wait_for_rings_started(s, 2);
|
|
|
|
|
|
|
|
qtest_end();
|
|
|
|
test_server_free(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_reconnect(void)
|
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
|
|
|
|
qtest_get_arch());
|
|
|
|
g_test_trap_subprocess(path, 0, 0);
|
|
|
|
g_test_trap_assert_passed();
|
2016-07-26 23:15:21 +02:00
|
|
|
g_free(path);
|
2016-06-06 18:45:08 +02:00
|
|
|
}
|
2016-09-09 13:34:43 +02:00
|
|
|
|
|
|
|
static void test_connect_fail_subprocess(void)
|
|
|
|
{
|
|
|
|
TestServer *s = test_server_new("connect-fail");
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
s->test_fail = true;
|
|
|
|
g_thread_new("connect", connect_thread, s);
|
|
|
|
cmd = GET_QEMU_CMDE(s, 2, ",server", "");
|
|
|
|
qtest_start(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
|
|
|
|
init_virtio_dev(s);
|
|
|
|
wait_for_fds(s);
|
|
|
|
wait_for_rings_started(s, 2);
|
|
|
|
|
|
|
|
qtest_end();
|
|
|
|
test_server_free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_connect_fail(void)
|
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
|
|
|
|
qtest_get_arch());
|
|
|
|
g_test_trap_subprocess(path, 0, 0);
|
|
|
|
g_test_trap_assert_passed();
|
|
|
|
g_free(path);
|
|
|
|
}
|
|
|
|
|
2016-09-09 13:34:45 +02:00
|
|
|
static void test_flags_mismatch_subprocess(void)
|
|
|
|
{
|
|
|
|
TestServer *s = test_server_new("flags-mismatch");
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
s->test_flags = TEST_FLAGS_DISCONNECT;
|
|
|
|
g_thread_new("connect", connect_thread, s);
|
|
|
|
cmd = GET_QEMU_CMDE(s, 2, ",server", "");
|
|
|
|
qtest_start(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
|
|
|
|
init_virtio_dev(s);
|
|
|
|
wait_for_fds(s);
|
|
|
|
wait_for_rings_started(s, 2);
|
|
|
|
|
|
|
|
qtest_end();
|
|
|
|
test_server_free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_flags_mismatch(void)
|
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
|
|
|
|
qtest_get_arch());
|
|
|
|
g_test_trap_subprocess(path, 0, 0);
|
|
|
|
g_test_trap_assert_passed();
|
|
|
|
g_free(path);
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:45:08 +02:00
|
|
|
#endif
|
|
|
|
|
2016-09-09 13:34:44 +02:00
|
|
|
static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
|
|
|
|
{
|
|
|
|
QVirtioPCIDevice *dev;
|
|
|
|
|
|
|
|
dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
|
|
|
|
g_assert(dev != NULL);
|
|
|
|
g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
|
|
|
|
|
|
|
|
qvirtio_pci_device_enable(dev);
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_reset(&dev->vdev);
|
|
|
|
qvirtio_set_acknowledge(&dev->vdev);
|
|
|
|
qvirtio_set_driver(&dev->vdev);
|
2016-09-09 13:34:44 +02:00
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
static void driver_init(QVirtioDevice *dev)
|
2016-09-09 13:34:44 +02:00
|
|
|
{
|
|
|
|
uint32_t features;
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
features = qvirtio_get_features(dev);
|
2016-09-09 13:34:44 +02:00
|
|
|
features = features & ~(QVIRTIO_F_BAD_FEATURE |
|
|
|
|
(1u << VIRTIO_RING_F_INDIRECT_DESC) |
|
|
|
|
(1u << VIRTIO_RING_F_EVENT_IDX));
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_set_features(dev, features);
|
2016-09-09 13:34:44 +02:00
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_set_driver_ok(dev);
|
2016-09-09 13:34:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PCI_SLOT 0x04
|
|
|
|
|
|
|
|
static void test_multiqueue(void)
|
|
|
|
{
|
|
|
|
const int queues = 2;
|
|
|
|
TestServer *s = test_server_new("mq");
|
|
|
|
QVirtioPCIDevice *dev;
|
|
|
|
QPCIBus *bus;
|
|
|
|
QVirtQueuePCI *vq[queues * 2];
|
|
|
|
QGuestAllocator *alloc;
|
|
|
|
char *cmd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
s->queues = queues;
|
|
|
|
test_server_listen(s);
|
|
|
|
|
|
|
|
cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
|
|
|
|
"-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
|
|
|
|
512, 512, root, s->chr_name,
|
|
|
|
s->socket_path, "", s->chr_name,
|
|
|
|
queues, queues * 2 + 2);
|
|
|
|
qtest_start(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
|
2016-09-29 12:32:45 +02:00
|
|
|
bus = qpci_init_pc(NULL);
|
2016-09-09 13:34:44 +02:00
|
|
|
dev = virtio_net_pci_init(bus, PCI_SLOT);
|
|
|
|
|
|
|
|
alloc = pc_alloc_init();
|
|
|
|
for (i = 0; i < queues * 2; i++) {
|
2016-10-17 12:30:21 +02:00
|
|
|
vq[i] = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, alloc, i);
|
2016-09-09 13:34:44 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
driver_init(&dev->vdev);
|
2016-09-09 13:34:44 +02:00
|
|
|
wait_for_rings_started(s, queues * 2);
|
|
|
|
|
|
|
|
/* End test */
|
|
|
|
for (i = 0; i < queues * 2; i++) {
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtqueue_cleanup(dev->vdev.bus, &vq[i]->vq, alloc);
|
2016-09-09 13:34:44 +02:00
|
|
|
}
|
|
|
|
pc_alloc_uninit(alloc);
|
|
|
|
qvirtio_pci_device_disable(dev);
|
|
|
|
g_free(dev->pdev);
|
|
|
|
g_free(dev);
|
|
|
|
qpci_free_pc(bus);
|
|
|
|
qtest_end();
|
|
|
|
|
|
|
|
test_server_free(s);
|
|
|
|
}
|
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
QTestState *s = NULL;
|
2015-10-09 17:17:37 +02:00
|
|
|
TestServer *server = NULL;
|
2015-09-30 17:01:21 +02:00
|
|
|
const char *hugefs;
|
2015-10-09 17:17:37 +02:00
|
|
|
char *qemu_cmd = NULL;
|
2014-06-10 12:03:23 +02:00
|
|
|
int ret;
|
2015-09-30 17:01:21 +02:00
|
|
|
char template[] = "/tmp/vhost-test-XXXXXX";
|
2015-11-27 15:41:18 +01:00
|
|
|
GMainLoop *loop;
|
|
|
|
GThread *thread;
|
2014-06-10 12:03:23 +02:00
|
|
|
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
|
|
|
module_call_init(MODULE_INIT_QOM);
|
2015-10-09 17:17:37 +02:00
|
|
|
qemu_add_opts(&qemu_chardev_opts);
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2015-09-30 17:01:21 +02:00
|
|
|
tmpfs = mkdtemp(template);
|
|
|
|
if (!tmpfs) {
|
2015-10-09 17:17:37 +02:00
|
|
|
g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
|
2015-09-30 17:01:21 +02:00
|
|
|
}
|
|
|
|
g_assert(tmpfs);
|
|
|
|
|
|
|
|
hugefs = getenv("QTEST_HUGETLBFS_PATH");
|
|
|
|
if (hugefs) {
|
|
|
|
root = init_hugepagefs(hugefs);
|
|
|
|
g_assert(root);
|
|
|
|
} else {
|
|
|
|
root = tmpfs;
|
2014-06-10 12:03:23 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:17:38 +02:00
|
|
|
server = test_server_new("test");
|
2016-06-06 18:45:08 +02:00
|
|
|
test_server_listen(server);
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2015-11-27 15:41:18 +01:00
|
|
|
loop = g_main_loop_new(NULL, FALSE);
|
2014-06-10 12:03:23 +02:00
|
|
|
/* run the main loop thread so the chardev may operate */
|
2015-11-27 15:41:18 +01:00
|
|
|
thread = g_thread_new(NULL, thread_function, loop);
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2015-10-09 17:17:38 +02:00
|
|
|
qemu_cmd = GET_QEMU_CMD(server);
|
2015-10-09 17:17:37 +02:00
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
s = qtest_start(qemu_cmd);
|
|
|
|
g_free(qemu_cmd);
|
2016-09-02 20:59:43 +02:00
|
|
|
init_virtio_dev(server);
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2015-10-09 17:17:37 +02:00
|
|
|
qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
|
2015-10-09 17:17:39 +02:00
|
|
|
qtest_add_func("/vhost-user/migrate", test_migrate);
|
2016-09-09 13:34:44 +02:00
|
|
|
qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
|
2017-09-05 20:06:02 +02:00
|
|
|
|
|
|
|
#if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
|
2016-06-06 18:45:08 +02:00
|
|
|
qtest_add_func("/vhost-user/reconnect/subprocess",
|
|
|
|
test_reconnect_subprocess);
|
|
|
|
qtest_add_func("/vhost-user/reconnect", test_reconnect);
|
2016-09-09 13:34:43 +02:00
|
|
|
qtest_add_func("/vhost-user/connect-fail/subprocess",
|
|
|
|
test_connect_fail_subprocess);
|
|
|
|
qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
|
2016-09-09 13:34:45 +02:00
|
|
|
qtest_add_func("/vhost-user/flags-mismatch/subprocess",
|
|
|
|
test_flags_mismatch_subprocess);
|
|
|
|
qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
|
2016-06-06 18:45:08 +02:00
|
|
|
#endif
|
2014-06-10 12:03:23 +02:00
|
|
|
|
|
|
|
ret = g_test_run();
|
|
|
|
|
|
|
|
if (s) {
|
|
|
|
qtest_quit(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2015-10-09 17:17:37 +02:00
|
|
|
test_server_free(server);
|
2014-06-10 12:03:23 +02:00
|
|
|
|
2015-11-27 15:41:18 +01:00
|
|
|
/* finish the helper thread and dispatch pending sources */
|
|
|
|
g_main_loop_quit(loop);
|
|
|
|
g_thread_join(thread);
|
|
|
|
while (g_main_context_pending(NULL)) {
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
|
|
|
}
|
|
|
|
g_main_loop_unref(loop);
|
|
|
|
|
2015-09-30 17:01:21 +02:00
|
|
|
ret = rmdir(tmpfs);
|
|
|
|
if (ret != 0) {
|
|
|
|
g_test_message("unable to rmdir: path (%s): %s\n",
|
|
|
|
tmpfs, strerror(errno));
|
|
|
|
}
|
|
|
|
g_assert_cmpint(ret, ==, 0);
|
|
|
|
|
2014-06-10 12:03:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|