qemu/tests/vhost-user-test.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for the vhost-user
   3 *
   4 * Copyright (c) 2014 Virtual Open Systems Sarl.
   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
  13#include "libqtest.h"
  14#include "qapi/error.h"
  15#include "qemu/config-file.h"
  16#include "qemu/option.h"
  17#include "qemu/range.h"
  18#include "qemu/sockets.h"
  19#include "chardev/char-fe.h"
  20#include "sysemu/sysemu.h"
  21#include "libqos/libqos.h"
  22#include "libqos/pci-pc.h"
  23#include "libqos/virtio-pci.h"
  24#include "qapi/error.h"
  25
  26#include "libqos/malloc-pc.h"
  27#include "hw/virtio/virtio-net.h"
  28
  29#include <linux/vhost.h>
  30#include <linux/virtio_ids.h>
  31#include <linux/virtio_net.h>
  32#include <sys/vfs.h>
  33
  34#define VHOST_USER_NET_TESTS_WORKING 0 /* broken as of 2.10.0 */
  35
  36/* GLIB version compatibility flags */
  37#if !GLIB_CHECK_VERSION(2, 26, 0)
  38#define G_TIME_SPAN_SECOND              (G_GINT64_CONSTANT(1000000))
  39#endif
  40
  41#if GLIB_CHECK_VERSION(2, 28, 0)
  42#define HAVE_MONOTONIC_TIME
  43#endif
  44
  45#define QEMU_CMD_MEM    " -m %d -object memory-backend-file,id=mem,size=%dM,"\
  46                        "mem-path=%s,share=on -numa node,memdev=mem"
  47#define QEMU_CMD_CHR    " -chardev socket,id=%s,path=%s%s"
  48#define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
  49#define QEMU_CMD_NET    " -device virtio-net-pci,netdev=net0"
  50
  51#define QEMU_CMD        QEMU_CMD_MEM QEMU_CMD_CHR \
  52                        QEMU_CMD_NETDEV QEMU_CMD_NET
  53
  54#define HUGETLBFS_MAGIC       0x958458f6
  55
  56/*********** FROM hw/virtio/vhost-user.c *************************************/
  57
  58#define VHOST_MEMORY_MAX_NREGIONS    8
  59
  60#define VHOST_USER_F_PROTOCOL_FEATURES 30
  61#define VHOST_USER_PROTOCOL_F_MQ 0
  62#define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
  63
  64#define VHOST_LOG_PAGE 0x1000
  65
  66typedef enum VhostUserRequest {
  67    VHOST_USER_NONE = 0,
  68    VHOST_USER_GET_FEATURES = 1,
  69    VHOST_USER_SET_FEATURES = 2,
  70    VHOST_USER_SET_OWNER = 3,
  71    VHOST_USER_RESET_OWNER = 4,
  72    VHOST_USER_SET_MEM_TABLE = 5,
  73    VHOST_USER_SET_LOG_BASE = 6,
  74    VHOST_USER_SET_LOG_FD = 7,
  75    VHOST_USER_SET_VRING_NUM = 8,
  76    VHOST_USER_SET_VRING_ADDR = 9,
  77    VHOST_USER_SET_VRING_BASE = 10,
  78    VHOST_USER_GET_VRING_BASE = 11,
  79    VHOST_USER_SET_VRING_KICK = 12,
  80    VHOST_USER_SET_VRING_CALL = 13,
  81    VHOST_USER_SET_VRING_ERR = 14,
  82    VHOST_USER_GET_PROTOCOL_FEATURES = 15,
  83    VHOST_USER_SET_PROTOCOL_FEATURES = 16,
  84    VHOST_USER_GET_QUEUE_NUM = 17,
  85    VHOST_USER_SET_VRING_ENABLE = 18,
  86    VHOST_USER_MAX
  87} VhostUserRequest;
  88
  89typedef struct VhostUserMemoryRegion {
  90    uint64_t guest_phys_addr;
  91    uint64_t memory_size;
  92    uint64_t userspace_addr;
  93    uint64_t mmap_offset;
  94} VhostUserMemoryRegion;
  95
  96typedef struct VhostUserMemory {
  97    uint32_t nregions;
  98    uint32_t padding;
  99    VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
 100} VhostUserMemory;
 101
 102typedef struct VhostUserLog {
 103    uint64_t mmap_size;
 104    uint64_t mmap_offset;
 105} VhostUserLog;
 106
 107typedef struct VhostUserMsg {
 108    VhostUserRequest request;
 109
 110#define VHOST_USER_VERSION_MASK     (0x3)
 111#define VHOST_USER_REPLY_MASK       (0x1<<2)
 112    uint32_t flags;
 113    uint32_t size; /* the following payload size */
 114    union {
 115#define VHOST_USER_VRING_IDX_MASK   (0xff)
 116#define VHOST_USER_VRING_NOFD_MASK  (0x1<<8)
 117        uint64_t u64;
 118        struct vhost_vring_state state;
 119        struct vhost_vring_addr addr;
 120        VhostUserMemory memory;
 121        VhostUserLog log;
 122    } payload;
 123} QEMU_PACKED VhostUserMsg;
 124
 125static VhostUserMsg m __attribute__ ((unused));
 126#define VHOST_USER_HDR_SIZE (sizeof(m.request) \
 127                            + sizeof(m.flags) \
 128                            + sizeof(m.size))
 129
 130#define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
 131
 132/* The version of the protocol we support */
 133#define VHOST_USER_VERSION    (0x1)
 134/*****************************************************************************/
 135
 136enum {
 137    TEST_FLAGS_OK,
 138    TEST_FLAGS_DISCONNECT,
 139    TEST_FLAGS_BAD,
 140    TEST_FLAGS_END,
 141};
 142
 143typedef struct TestServer {
 144    QPCIBus *bus;
 145    gchar *socket_path;
 146    gchar *mig_path;
 147    gchar *chr_name;
 148    CharBackend chr;
 149    int fds_num;
 150    int fds[VHOST_MEMORY_MAX_NREGIONS];
 151    VhostUserMemory memory;
 152    CompatGMutex data_mutex;
 153    CompatGCond data_cond;
 154    int log_fd;
 155    uint64_t rings;
 156    bool test_fail;
 157    int test_flags;
 158    int queues;
 159} TestServer;
 160
 161static const char *tmpfs;
 162static const char *root;
 163
 164static void init_virtio_dev(TestServer *s)
 165{
 166    QVirtioPCIDevice *dev;
 167    uint32_t features;
 168
 169    s->bus = qpci_init_pc(NULL);
 170    g_assert_nonnull(s->bus);
 171
 172    dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET);
 173    g_assert_nonnull(dev);
 174
 175    qvirtio_pci_device_enable(dev);
 176    qvirtio_reset(&dev->vdev);
 177    qvirtio_set_acknowledge(&dev->vdev);
 178    qvirtio_set_driver(&dev->vdev);
 179
 180    features = qvirtio_get_features(&dev->vdev);
 181    features = features & VIRTIO_NET_F_MAC;
 182    qvirtio_set_features(&dev->vdev, features);
 183
 184    qvirtio_set_driver_ok(&dev->vdev);
 185    qvirtio_pci_device_free(dev);
 186}
 187
 188static void wait_for_fds(TestServer *s)
 189{
 190    gint64 end_time;
 191
 192    g_mutex_lock(&s->data_mutex);
 193
 194    end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
 195    while (!s->fds_num) {
 196        if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
 197            /* timeout has passed */
 198            g_assert(s->fds_num);
 199            break;
 200        }
 201    }
 202
 203    /* check for sanity */
 204    g_assert_cmpint(s->fds_num, >, 0);
 205    g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
 206
 207    g_mutex_unlock(&s->data_mutex);
 208}
 209
 210static void read_guest_mem(const void *data)
 211{
 212    TestServer *s = (void *)data;
 213    uint32_t *guest_mem;
 214    int i, j;
 215    size_t size;
 216
 217    wait_for_fds(s);
 218
 219    g_mutex_lock(&s->data_mutex);
 220
 221    /* iterate all regions */
 222    for (i = 0; i < s->fds_num; i++) {
 223
 224        /* We'll check only the region statring at 0x0*/
 225        if (s->memory.regions[i].guest_phys_addr != 0x0) {
 226            continue;
 227        }
 228
 229        g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
 230
 231        size = s->memory.regions[i].memory_size +
 232            s->memory.regions[i].mmap_offset;
 233
 234        guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
 235                         MAP_SHARED, s->fds[i], 0);
 236
 237        g_assert(guest_mem != MAP_FAILED);
 238        guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
 239
 240        for (j = 0; j < 256; j++) {
 241            uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
 242            uint32_t b = guest_mem[j];
 243
 244            g_assert_cmpint(a, ==, b);
 245        }
 246
 247        munmap(guest_mem, s->memory.regions[i].memory_size);
 248    }
 249
 250    g_mutex_unlock(&s->data_mutex);
 251}
 252
 253static void *thread_function(void *data)
 254{
 255    GMainLoop *loop = data;
 256    g_main_loop_run(loop);
 257    return NULL;
 258}
 259
 260static int chr_can_read(void *opaque)
 261{
 262    return VHOST_USER_HDR_SIZE;
 263}
 264
 265static void chr_read(void *opaque, const uint8_t *buf, int size)
 266{
 267    TestServer *s = opaque;
 268    CharBackend *chr = &s->chr;
 269    VhostUserMsg msg;
 270    uint8_t *p = (uint8_t *) &msg;
 271    int fd;
 272
 273    if (s->test_fail) {
 274        qemu_chr_fe_disconnect(chr);
 275        /* now switch to non-failure */
 276        s->test_fail = false;
 277    }
 278
 279    if (size != VHOST_USER_HDR_SIZE) {
 280        g_test_message("Wrong message size received %d\n", size);
 281        return;
 282    }
 283
 284    g_mutex_lock(&s->data_mutex);
 285    memcpy(p, buf, VHOST_USER_HDR_SIZE);
 286
 287    if (msg.size) {
 288        p += VHOST_USER_HDR_SIZE;
 289        size = qemu_chr_fe_read_all(chr, p, msg.size);
 290        if (size != msg.size) {
 291            g_test_message("Wrong message size received %d != %d\n",
 292                           size, msg.size);
 293            return;
 294        }
 295    }
 296
 297    switch (msg.request) {
 298    case VHOST_USER_GET_FEATURES:
 299        /* send back features to qemu */
 300        msg.flags |= VHOST_USER_REPLY_MASK;
 301        msg.size = sizeof(m.payload.u64);
 302        msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
 303            0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
 304        if (s->queues > 1) {
 305            msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
 306        }
 307        if (s->test_flags >= TEST_FLAGS_BAD) {
 308            msg.payload.u64 = 0;
 309            s->test_flags = TEST_FLAGS_END;
 310        }
 311        p = (uint8_t *) &msg;
 312        qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
 313        break;
 314
 315    case VHOST_USER_SET_FEATURES:
 316        g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
 317                        !=, 0ULL);
 318        if (s->test_flags == TEST_FLAGS_DISCONNECT) {
 319            qemu_chr_fe_disconnect(chr);
 320            s->test_flags = TEST_FLAGS_BAD;
 321        }
 322        break;
 323
 324    case VHOST_USER_GET_PROTOCOL_FEATURES:
 325        /* send back features to qemu */
 326        msg.flags |= VHOST_USER_REPLY_MASK;
 327        msg.size = sizeof(m.payload.u64);
 328        msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
 329        if (s->queues > 1) {
 330            msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
 331        }
 332        p = (uint8_t *) &msg;
 333        qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
 334        break;
 335
 336    case VHOST_USER_GET_VRING_BASE:
 337        /* send back vring base to qemu */
 338        msg.flags |= VHOST_USER_REPLY_MASK;
 339        msg.size = sizeof(m.payload.state);
 340        msg.payload.state.num = 0;
 341        p = (uint8_t *) &msg;
 342        qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
 343
 344        assert(msg.payload.state.index < s->queues * 2);
 345        s->rings &= ~(0x1ULL << msg.payload.state.index);
 346        break;
 347
 348    case VHOST_USER_SET_MEM_TABLE:
 349        /* received the mem table */
 350        memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
 351        s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
 352                                            G_N_ELEMENTS(s->fds));
 353
 354        /* signal the test that it can continue */
 355        g_cond_signal(&s->data_cond);
 356        break;
 357
 358    case VHOST_USER_SET_VRING_KICK:
 359    case VHOST_USER_SET_VRING_CALL:
 360        /* consume the fd */
 361        qemu_chr_fe_get_msgfds(chr, &fd, 1);
 362        /*
 363         * This is a non-blocking eventfd.
 364         * The receive function forces it to be blocking,
 365         * so revert it back to non-blocking.
 366         */
 367        qemu_set_nonblock(fd);
 368        break;
 369
 370    case VHOST_USER_SET_LOG_BASE:
 371        if (s->log_fd != -1) {
 372            close(s->log_fd);
 373            s->log_fd = -1;
 374        }
 375        qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
 376        msg.flags |= VHOST_USER_REPLY_MASK;
 377        msg.size = 0;
 378        p = (uint8_t *) &msg;
 379        qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
 380
 381        g_cond_signal(&s->data_cond);
 382        break;
 383
 384    case VHOST_USER_SET_VRING_BASE:
 385        assert(msg.payload.state.index < s->queues * 2);
 386        s->rings |= 0x1ULL << msg.payload.state.index;
 387        break;
 388
 389    case VHOST_USER_GET_QUEUE_NUM:
 390        msg.flags |= VHOST_USER_REPLY_MASK;
 391        msg.size = sizeof(m.payload.u64);
 392        msg.payload.u64 = s->queues;
 393        p = (uint8_t *) &msg;
 394        qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
 395        break;
 396
 397    default:
 398        break;
 399    }
 400
 401    g_mutex_unlock(&s->data_mutex);
 402}
 403
 404static const char *init_hugepagefs(const char *path)
 405{
 406    struct statfs fs;
 407    int ret;
 408
 409    if (access(path, R_OK | W_OK | X_OK)) {
 410        g_test_message("access on path (%s): %s\n", path, strerror(errno));
 411        return NULL;
 412    }
 413
 414    do {
 415        ret = statfs(path, &fs);
 416    } while (ret != 0 && errno == EINTR);
 417
 418    if (ret != 0) {
 419        g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
 420        return NULL;
 421    }
 422
 423    if (fs.f_type != HUGETLBFS_MAGIC) {
 424        g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
 425        return NULL;
 426    }
 427
 428    return path;
 429}
 430
 431static TestServer *test_server_new(const gchar *name)
 432{
 433    TestServer *server = g_new0(TestServer, 1);
 434
 435    server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
 436    server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
 437    server->chr_name = g_strdup_printf("chr-%s", name);
 438
 439    g_mutex_init(&server->data_mutex);
 440    g_cond_init(&server->data_cond);
 441
 442    server->log_fd = -1;
 443    server->queues = 1;
 444
 445    return server;
 446}
 447
 448static void chr_event(void *opaque, int event)
 449{
 450    TestServer *s = opaque;
 451
 452    if (s->test_flags == TEST_FLAGS_END &&
 453        event == CHR_EVENT_CLOSED) {
 454        s->test_flags = TEST_FLAGS_OK;
 455    }
 456}
 457
 458static void test_server_create_chr(TestServer *server, const gchar *opt)
 459{
 460    gchar *chr_path;
 461    Chardev *chr;
 462
 463    chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
 464    chr = qemu_chr_new(server->chr_name, chr_path);
 465    g_free(chr_path);
 466
 467    qemu_chr_fe_init(&server->chr, chr, &error_abort);
 468    qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
 469                             chr_event, NULL, server, NULL, true);
 470}
 471
 472static void test_server_listen(TestServer *server)
 473{
 474    test_server_create_chr(server, ",server,nowait");
 475}
 476
 477#define GET_QEMU_CMD(s)                                         \
 478    g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name,  \
 479                    (s)->socket_path, "", (s)->chr_name)
 480
 481#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...)                     \
 482    g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
 483                    (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
 484
 485static gboolean _test_server_free(TestServer *server)
 486{
 487    int i;
 488
 489    qemu_chr_fe_deinit(&server->chr, true);
 490
 491    for (i = 0; i < server->fds_num; i++) {
 492        close(server->fds[i]);
 493    }
 494
 495    if (server->log_fd != -1) {
 496        close(server->log_fd);
 497    }
 498
 499    unlink(server->socket_path);
 500    g_free(server->socket_path);
 501
 502    unlink(server->mig_path);
 503    g_free(server->mig_path);
 504
 505    g_free(server->chr_name);
 506    qpci_free_pc(server->bus);
 507
 508    g_free(server);
 509
 510    return FALSE;
 511}
 512
 513static void test_server_free(TestServer *server)
 514{
 515    g_idle_add((GSourceFunc)_test_server_free, server);
 516}
 517
 518static void wait_for_log_fd(TestServer *s)
 519{
 520    gint64 end_time;
 521
 522    g_mutex_lock(&s->data_mutex);
 523    end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
 524    while (s->log_fd == -1) {
 525        if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
 526            /* timeout has passed */
 527            g_assert(s->log_fd != -1);
 528            break;
 529        }
 530    }
 531
 532    g_mutex_unlock(&s->data_mutex);
 533}
 534
 535static void write_guest_mem(TestServer *s, uint32_t seed)
 536{
 537    uint32_t *guest_mem;
 538    int i, j;
 539    size_t size;
 540
 541    wait_for_fds(s);
 542
 543    /* iterate all regions */
 544    for (i = 0; i < s->fds_num; i++) {
 545
 546        /* We'll write only the region statring at 0x0 */
 547        if (s->memory.regions[i].guest_phys_addr != 0x0) {
 548            continue;
 549        }
 550
 551        g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
 552
 553        size = s->memory.regions[i].memory_size +
 554            s->memory.regions[i].mmap_offset;
 555
 556        guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
 557                         MAP_SHARED, s->fds[i], 0);
 558
 559        g_assert(guest_mem != MAP_FAILED);
 560        guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
 561
 562        for (j = 0; j < 256; j++) {
 563            guest_mem[j] = seed + j;
 564        }
 565
 566        munmap(guest_mem, s->memory.regions[i].memory_size);
 567        break;
 568    }
 569}
 570
 571static guint64 get_log_size(TestServer *s)
 572{
 573    guint64 log_size = 0;
 574    int i;
 575
 576    for (i = 0; i < s->memory.nregions; ++i) {
 577        VhostUserMemoryRegion *reg = &s->memory.regions[i];
 578        guint64 last = range_get_last(reg->guest_phys_addr,
 579                                       reg->memory_size);
 580        log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
 581    }
 582
 583    return log_size;
 584}
 585
 586typedef struct TestMigrateSource {
 587    GSource source;
 588    TestServer *src;
 589    TestServer *dest;
 590} TestMigrateSource;
 591
 592static gboolean
 593test_migrate_source_check(GSource *source)
 594{
 595    TestMigrateSource *t = (TestMigrateSource *)source;
 596    gboolean overlap = t->src->rings && t->dest->rings;
 597
 598    g_assert(!overlap);
 599
 600    return FALSE;
 601}
 602
 603#if !GLIB_CHECK_VERSION(2,36,0)
 604/* this callback is unnecessary with glib >2.36, the default
 605 * prepare for the source does the same */
 606static gboolean
 607test_migrate_source_prepare(GSource *source, gint *timeout)
 608{
 609    *timeout = -1;
 610    return FALSE;
 611}
 612#endif
 613
 614GSourceFuncs test_migrate_source_funcs = {
 615#if !GLIB_CHECK_VERSION(2,36,0)
 616    .prepare = test_migrate_source_prepare,
 617#endif
 618    .check = test_migrate_source_check,
 619};
 620
 621static void test_migrate(void)
 622{
 623    TestServer *s = test_server_new("src");
 624    TestServer *dest = test_server_new("dest");
 625    char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
 626    QTestState *global = global_qtest, *from, *to;
 627    GSource *source;
 628    gchar *cmd;
 629    QDict *rsp;
 630    guint8 *log;
 631    guint64 size;
 632
 633    test_server_listen(s);
 634    test_server_listen(dest);
 635
 636    cmd = GET_QEMU_CMDE(s, 2, "", "");
 637    from = qtest_start(cmd);
 638    g_free(cmd);
 639
 640    init_virtio_dev(s);
 641    wait_for_fds(s);
 642    size = get_log_size(s);
 643    g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
 644
 645    cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
 646    to = qtest_init(cmd);
 647    g_free(cmd);
 648
 649    source = g_source_new(&test_migrate_source_funcs,
 650                          sizeof(TestMigrateSource));
 651    ((TestMigrateSource *)source)->src = s;
 652    ((TestMigrateSource *)source)->dest = dest;
 653    g_source_attach(source, NULL);
 654
 655    /* slow down migration to have time to fiddle with log */
 656    /* TODO: qtest could learn to break on some places */
 657    rsp = qmp("{ 'execute': 'migrate_set_speed',"
 658              "'arguments': { 'value': 10 } }");
 659    g_assert(qdict_haskey(rsp, "return"));
 660    QDECREF(rsp);
 661
 662    cmd = g_strdup_printf("{ 'execute': 'migrate',"
 663                          "'arguments': { 'uri': '%s' } }",
 664                          uri);
 665    rsp = qmp(cmd);
 666    g_free(cmd);
 667    g_assert(qdict_haskey(rsp, "return"));
 668    QDECREF(rsp);
 669
 670    wait_for_log_fd(s);
 671
 672    log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
 673    g_assert(log != MAP_FAILED);
 674
 675    /* modify first page */
 676    write_guest_mem(s, 0x42);
 677    log[0] = 1;
 678    munmap(log, size);
 679
 680    /* speed things up */
 681    rsp = qmp("{ 'execute': 'migrate_set_speed',"
 682              "'arguments': { 'value': 0 } }");
 683    g_assert(qdict_haskey(rsp, "return"));
 684    QDECREF(rsp);
 685
 686    qmp_eventwait("STOP");
 687
 688    global_qtest = to;
 689    qmp_eventwait("RESUME");
 690
 691    read_guest_mem(dest);
 692
 693    g_source_destroy(source);
 694    g_source_unref(source);
 695
 696    qtest_quit(to);
 697    test_server_free(dest);
 698    qtest_quit(from);
 699    test_server_free(s);
 700    g_free(uri);
 701
 702    global_qtest = global;
 703}
 704
 705static void wait_for_rings_started(TestServer *s, size_t count)
 706{
 707    gint64 end_time;
 708
 709    g_mutex_lock(&s->data_mutex);
 710    end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
 711    while (ctpop64(s->rings) != count) {
 712        if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
 713            /* timeout has passed */
 714            g_assert_cmpint(ctpop64(s->rings), ==, count);
 715            break;
 716        }
 717    }
 718
 719    g_mutex_unlock(&s->data_mutex);
 720}
 721
 722#if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
 723static inline void test_server_connect(TestServer *server)
 724{
 725    test_server_create_chr(server, ",reconnect=1");
 726}
 727
 728static gboolean
 729reconnect_cb(gpointer user_data)
 730{
 731    TestServer *s = user_data;
 732
 733    qemu_chr_fe_disconnect(&s->chr);
 734
 735    return FALSE;
 736}
 737
 738static gpointer
 739connect_thread(gpointer data)
 740{
 741    TestServer *s = data;
 742
 743    /* wait for qemu to start before first try, to avoid extra warnings */
 744    g_usleep(G_USEC_PER_SEC);
 745    test_server_connect(s);
 746
 747    return NULL;
 748}
 749
 750static void test_reconnect_subprocess(void)
 751{
 752    TestServer *s = test_server_new("reconnect");
 753    char *cmd;
 754
 755    g_thread_new("connect", connect_thread, s);
 756    cmd = GET_QEMU_CMDE(s, 2, ",server", "");
 757    qtest_start(cmd);
 758    g_free(cmd);
 759
 760    init_virtio_dev(s);
 761    wait_for_fds(s);
 762    wait_for_rings_started(s, 2);
 763
 764    /* reconnect */
 765    s->fds_num = 0;
 766    s->rings = 0;
 767    g_idle_add(reconnect_cb, s);
 768    wait_for_fds(s);
 769    wait_for_rings_started(s, 2);
 770
 771    qtest_end();
 772    test_server_free(s);
 773    return;
 774}
 775
 776static void test_reconnect(void)
 777{
 778    gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
 779                                  qtest_get_arch());
 780    g_test_trap_subprocess(path, 0, 0);
 781    g_test_trap_assert_passed();
 782    g_free(path);
 783}
 784
 785static void test_connect_fail_subprocess(void)
 786{
 787    TestServer *s = test_server_new("connect-fail");
 788    char *cmd;
 789
 790    s->test_fail = true;
 791    g_thread_new("connect", connect_thread, s);
 792    cmd = GET_QEMU_CMDE(s, 2, ",server", "");
 793    qtest_start(cmd);
 794    g_free(cmd);
 795
 796    init_virtio_dev(s);
 797    wait_for_fds(s);
 798    wait_for_rings_started(s, 2);
 799
 800    qtest_end();
 801    test_server_free(s);
 802}
 803
 804static void test_connect_fail(void)
 805{
 806    gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
 807                                  qtest_get_arch());
 808    g_test_trap_subprocess(path, 0, 0);
 809    g_test_trap_assert_passed();
 810    g_free(path);
 811}
 812
 813static void test_flags_mismatch_subprocess(void)
 814{
 815    TestServer *s = test_server_new("flags-mismatch");
 816    char *cmd;
 817
 818    s->test_flags = TEST_FLAGS_DISCONNECT;
 819    g_thread_new("connect", connect_thread, s);
 820    cmd = GET_QEMU_CMDE(s, 2, ",server", "");
 821    qtest_start(cmd);
 822    g_free(cmd);
 823
 824    init_virtio_dev(s);
 825    wait_for_fds(s);
 826    wait_for_rings_started(s, 2);
 827
 828    qtest_end();
 829    test_server_free(s);
 830}
 831
 832static void test_flags_mismatch(void)
 833{
 834    gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
 835                                  qtest_get_arch());
 836    g_test_trap_subprocess(path, 0, 0);
 837    g_test_trap_assert_passed();
 838    g_free(path);
 839}
 840
 841#endif
 842
 843static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
 844{
 845    QVirtioPCIDevice *dev;
 846
 847    dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
 848    g_assert(dev != NULL);
 849    g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
 850
 851    qvirtio_pci_device_enable(dev);
 852    qvirtio_reset(&dev->vdev);
 853    qvirtio_set_acknowledge(&dev->vdev);
 854    qvirtio_set_driver(&dev->vdev);
 855
 856    return dev;
 857}
 858
 859static void driver_init(QVirtioDevice *dev)
 860{
 861    uint32_t features;
 862
 863    features = qvirtio_get_features(dev);
 864    features = features & ~(QVIRTIO_F_BAD_FEATURE |
 865                            (1u << VIRTIO_RING_F_INDIRECT_DESC) |
 866                            (1u << VIRTIO_RING_F_EVENT_IDX));
 867    qvirtio_set_features(dev, features);
 868
 869    qvirtio_set_driver_ok(dev);
 870}
 871
 872#define PCI_SLOT                0x04
 873
 874static void test_multiqueue(void)
 875{
 876    const int queues = 2;
 877    TestServer *s = test_server_new("mq");
 878    QVirtioPCIDevice *dev;
 879    QPCIBus *bus;
 880    QVirtQueuePCI *vq[queues * 2];
 881    QGuestAllocator *alloc;
 882    char *cmd;
 883    int i;
 884
 885    s->queues = queues;
 886    test_server_listen(s);
 887
 888    cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
 889                          "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
 890                          512, 512, root, s->chr_name,
 891                          s->socket_path, "", s->chr_name,
 892                          queues, queues * 2 + 2);
 893    qtest_start(cmd);
 894    g_free(cmd);
 895
 896    bus = qpci_init_pc(NULL);
 897    dev = virtio_net_pci_init(bus, PCI_SLOT);
 898
 899    alloc = pc_alloc_init();
 900    for (i = 0; i < queues * 2; i++) {
 901        vq[i] = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, alloc, i);
 902    }
 903
 904    driver_init(&dev->vdev);
 905    wait_for_rings_started(s, queues * 2);
 906
 907    /* End test */
 908    for (i = 0; i < queues * 2; i++) {
 909        qvirtqueue_cleanup(dev->vdev.bus, &vq[i]->vq, alloc);
 910    }
 911    pc_alloc_uninit(alloc);
 912    qvirtio_pci_device_disable(dev);
 913    g_free(dev->pdev);
 914    g_free(dev);
 915    qpci_free_pc(bus);
 916    qtest_end();
 917
 918    test_server_free(s);
 919}
 920
 921int main(int argc, char **argv)
 922{
 923    QTestState *s = NULL;
 924    TestServer *server = NULL;
 925    const char *hugefs;
 926    char *qemu_cmd = NULL;
 927    int ret;
 928    char template[] = "/tmp/vhost-test-XXXXXX";
 929    GMainLoop *loop;
 930    GThread *thread;
 931
 932    g_test_init(&argc, &argv, NULL);
 933
 934    module_call_init(MODULE_INIT_QOM);
 935    qemu_add_opts(&qemu_chardev_opts);
 936
 937    tmpfs = mkdtemp(template);
 938    if (!tmpfs) {
 939        g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
 940    }
 941    g_assert(tmpfs);
 942
 943    hugefs = getenv("QTEST_HUGETLBFS_PATH");
 944    if (hugefs) {
 945        root = init_hugepagefs(hugefs);
 946        g_assert(root);
 947    } else {
 948        root = tmpfs;
 949    }
 950
 951    server = test_server_new("test");
 952    test_server_listen(server);
 953
 954    loop = g_main_loop_new(NULL, FALSE);
 955    /* run the main loop thread so the chardev may operate */
 956    thread = g_thread_new(NULL, thread_function, loop);
 957
 958    qemu_cmd = GET_QEMU_CMD(server);
 959
 960    s = qtest_start(qemu_cmd);
 961    g_free(qemu_cmd);
 962    init_virtio_dev(server);
 963
 964    qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
 965    qtest_add_func("/vhost-user/migrate", test_migrate);
 966    qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
 967
 968#if VHOST_USER_NET_TESTS_WORKING && defined(CONFIG_HAS_GLIB_SUBPROCESS_TESTS)
 969    qtest_add_func("/vhost-user/reconnect/subprocess",
 970                   test_reconnect_subprocess);
 971    qtest_add_func("/vhost-user/reconnect", test_reconnect);
 972    qtest_add_func("/vhost-user/connect-fail/subprocess",
 973                   test_connect_fail_subprocess);
 974    qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
 975    qtest_add_func("/vhost-user/flags-mismatch/subprocess",
 976                   test_flags_mismatch_subprocess);
 977    qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
 978#endif
 979
 980    ret = g_test_run();
 981
 982    if (s) {
 983        qtest_quit(s);
 984    }
 985
 986    /* cleanup */
 987    test_server_free(server);
 988
 989    /* finish the helper thread and dispatch pending sources */
 990    g_main_loop_quit(loop);
 991    g_thread_join(thread);
 992    while (g_main_context_pending(NULL)) {
 993        g_main_context_iteration (NULL, TRUE);
 994    }
 995    g_main_loop_unref(loop);
 996
 997    ret = rmdir(tmpfs);
 998    if (ret != 0) {
 999        g_test_message("unable to rmdir: path (%s): %s\n",
1000                       tmpfs, strerror(errno));
1001    }
1002    g_assert_cmpint(ret, ==, 0);
1003
1004    return ret;
1005}
1006