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