qemu/tests/ivshmem-test.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for ivshmem
   3 *
   4 * Copyright (c) 2014 SUSE LINUX Products GmbH
   5 * Copyright (c) 2015 Red Hat, Inc.
   6 *
   7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
   8 * See the COPYING file in the top-level directory.
   9 */
  10
  11#include "qemu/osdep.h"
  12#include <glib/gstdio.h>
  13#include "contrib/ivshmem-server/ivshmem-server.h"
  14#include "libqos/libqos-pc.h"
  15#include "libqos/libqos-spapr.h"
  16#include "libqtest.h"
  17#include "qemu-common.h"
  18
  19#define TMPSHMSIZE (1 << 20)
  20static char *tmpshm;
  21static void *tmpshmem;
  22static char *tmpdir;
  23static char *tmpserver;
  24
  25static void save_fn(QPCIDevice *dev, int devfn, void *data)
  26{
  27    QPCIDevice **pdev = (QPCIDevice **) data;
  28
  29    *pdev = dev;
  30}
  31
  32static QPCIDevice *get_device(QPCIBus *pcibus)
  33{
  34    QPCIDevice *dev;
  35
  36    dev = NULL;
  37    qpci_device_foreach(pcibus, 0x1af4, 0x1110, save_fn, &dev);
  38    g_assert(dev != NULL);
  39
  40    return dev;
  41}
  42
  43typedef struct _IVState {
  44    QOSState *qs;
  45    QPCIBar reg_bar, mem_bar;
  46    QPCIDevice *dev;
  47} IVState;
  48
  49enum Reg {
  50    INTRMASK = 0,
  51    INTRSTATUS = 4,
  52    IVPOSITION = 8,
  53    DOORBELL = 12,
  54};
  55
  56static const char* reg2str(enum Reg reg) {
  57    switch (reg) {
  58    case INTRMASK:
  59        return "IntrMask";
  60    case INTRSTATUS:
  61        return "IntrStatus";
  62    case IVPOSITION:
  63        return "IVPosition";
  64    case DOORBELL:
  65        return "DoorBell";
  66    default:
  67        return NULL;
  68    }
  69}
  70
  71static inline unsigned in_reg(IVState *s, enum Reg reg)
  72{
  73    const char *name = reg2str(reg);
  74    unsigned res;
  75
  76    res = qpci_io_readl(s->dev, s->reg_bar, reg);
  77    g_test_message("*%s -> %x", name, res);
  78
  79    return res;
  80}
  81
  82static inline void out_reg(IVState *s, enum Reg reg, unsigned v)
  83{
  84    const char *name = reg2str(reg);
  85
  86    g_test_message("%x -> *%s", v, name);
  87    qpci_io_writel(s->dev, s->reg_bar, reg, v);
  88}
  89
  90static inline void read_mem(IVState *s, uint64_t off, void *buf, size_t len)
  91{
  92    qpci_memread(s->dev, s->mem_bar, off, buf, len);
  93}
  94
  95static inline void write_mem(IVState *s, uint64_t off,
  96                             const void *buf, size_t len)
  97{
  98    qpci_memwrite(s->dev, s->mem_bar, off, buf, len);
  99}
 100
 101static void cleanup_vm(IVState *s)
 102{
 103    assert(!global_qtest);
 104    g_free(s->dev);
 105    qtest_shutdown(s->qs);
 106}
 107
 108static void setup_vm_cmd(IVState *s, const char *cmd, bool msix)
 109{
 110    uint64_t barsize;
 111    const char *arch = qtest_get_arch();
 112
 113    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
 114        s->qs = qtest_pc_boot(cmd);
 115    } else if (strcmp(arch, "ppc64") == 0) {
 116        s->qs = qtest_spapr_boot(cmd);
 117    } else {
 118        g_printerr("ivshmem-test tests are only available on x86 or ppc64\n");
 119        exit(EXIT_FAILURE);
 120    }
 121    s->dev = get_device(s->qs->pcibus);
 122
 123    s->reg_bar = qpci_iomap(s->dev, 0, &barsize);
 124    g_assert_cmpuint(barsize, ==, 256);
 125
 126    if (msix) {
 127        qpci_msix_enable(s->dev);
 128    }
 129
 130    s->mem_bar = qpci_iomap(s->dev, 2, &barsize);
 131    g_assert_cmpuint(barsize, ==, TMPSHMSIZE);
 132
 133    qpci_device_enable(s->dev);
 134}
 135
 136static void setup_vm(IVState *s)
 137{
 138    char *cmd = g_strdup_printf("-object memory-backend-file"
 139                                ",id=mb1,size=1M,share,mem-path=/dev/shm%s"
 140                                " -device ivshmem-plain,memdev=mb1", tmpshm);
 141
 142    setup_vm_cmd(s, cmd, false);
 143
 144    g_free(cmd);
 145}
 146
 147static void test_ivshmem_single(void)
 148{
 149    IVState state, *s;
 150    uint32_t data[1024];
 151    int i;
 152
 153    setup_vm(&state);
 154    s = &state;
 155
 156    /* initial state of readable registers */
 157    g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0);
 158    g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
 159    g_assert_cmpuint(in_reg(s, IVPOSITION), ==, 0);
 160
 161    /* trigger interrupt via registers */
 162    out_reg(s, INTRMASK, 0xffffffff);
 163    g_assert_cmpuint(in_reg(s, INTRMASK), ==, 0xffffffff);
 164    out_reg(s, INTRSTATUS, 1);
 165    /* check interrupt status */
 166    g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 1);
 167    /* reading clears */
 168    g_assert_cmpuint(in_reg(s, INTRSTATUS), ==, 0);
 169    /* TODO intercept actual interrupt (needs qtest work) */
 170
 171    /* invalid register access */
 172    out_reg(s, IVPOSITION, 1);
 173    in_reg(s, DOORBELL);
 174
 175    /* ring the (non-functional) doorbell */
 176    out_reg(s, DOORBELL, 8 << 16);
 177
 178    /* write shared memory */
 179    for (i = 0; i < G_N_ELEMENTS(data); i++) {
 180        data[i] = i;
 181    }
 182    write_mem(s, 0, data, sizeof(data));
 183
 184    /* verify write */
 185    for (i = 0; i < G_N_ELEMENTS(data); i++) {
 186        g_assert_cmpuint(((uint32_t *)tmpshmem)[i], ==, i);
 187    }
 188
 189    /* read it back and verify read */
 190    memset(data, 0, sizeof(data));
 191    read_mem(s, 0, data, sizeof(data));
 192    for (i = 0; i < G_N_ELEMENTS(data); i++) {
 193        g_assert_cmpuint(data[i], ==, i);
 194    }
 195
 196    cleanup_vm(s);
 197}
 198
 199static void test_ivshmem_pair(void)
 200{
 201    IVState state1, state2, *s1, *s2;
 202    char *data;
 203    int i;
 204
 205    setup_vm(&state1);
 206    s1 = &state1;
 207    setup_vm(&state2);
 208    s2 = &state2;
 209
 210    data = g_malloc0(TMPSHMSIZE);
 211
 212    /* host write, guest 1 & 2 read */
 213    memset(tmpshmem, 0x42, TMPSHMSIZE);
 214    read_mem(s1, 0, data, TMPSHMSIZE);
 215    for (i = 0; i < TMPSHMSIZE; i++) {
 216        g_assert_cmpuint(data[i], ==, 0x42);
 217    }
 218    read_mem(s2, 0, data, TMPSHMSIZE);
 219    for (i = 0; i < TMPSHMSIZE; i++) {
 220        g_assert_cmpuint(data[i], ==, 0x42);
 221    }
 222
 223    /* guest 1 write, guest 2 read */
 224    memset(data, 0x43, TMPSHMSIZE);
 225    write_mem(s1, 0, data, TMPSHMSIZE);
 226    memset(data, 0, TMPSHMSIZE);
 227    read_mem(s2, 0, data, TMPSHMSIZE);
 228    for (i = 0; i < TMPSHMSIZE; i++) {
 229        g_assert_cmpuint(data[i], ==, 0x43);
 230    }
 231
 232    /* guest 2 write, guest 1 read */
 233    memset(data, 0x44, TMPSHMSIZE);
 234    write_mem(s2, 0, data, TMPSHMSIZE);
 235    memset(data, 0, TMPSHMSIZE);
 236    read_mem(s1, 0, data, TMPSHMSIZE);
 237    for (i = 0; i < TMPSHMSIZE; i++) {
 238        g_assert_cmpuint(data[i], ==, 0x44);
 239    }
 240
 241    cleanup_vm(s1);
 242    cleanup_vm(s2);
 243    g_free(data);
 244}
 245
 246typedef struct ServerThread {
 247    GThread *thread;
 248    IvshmemServer *server;
 249    int pipe[2]; /* to handle quit */
 250} ServerThread;
 251
 252static void *server_thread(void *data)
 253{
 254    ServerThread *t = data;
 255    IvshmemServer *server = t->server;
 256
 257    while (true) {
 258        fd_set fds;
 259        int maxfd, ret;
 260
 261        FD_ZERO(&fds);
 262        FD_SET(t->pipe[0], &fds);
 263        maxfd = t->pipe[0] + 1;
 264
 265        ivshmem_server_get_fds(server, &fds, &maxfd);
 266
 267        ret = select(maxfd, &fds, NULL, NULL, NULL);
 268
 269        if (ret < 0) {
 270            if (errno == EINTR) {
 271                continue;
 272            }
 273
 274            g_critical("select error: %s\n", strerror(errno));
 275            break;
 276        }
 277        if (ret == 0) {
 278            continue;
 279        }
 280
 281        if (FD_ISSET(t->pipe[0], &fds)) {
 282            break;
 283        }
 284
 285        if (ivshmem_server_handle_fds(server, &fds, maxfd) < 0) {
 286            g_critical("ivshmem_server_handle_fds() failed\n");
 287            break;
 288        }
 289    }
 290
 291    return NULL;
 292}
 293
 294static void setup_vm_with_server(IVState *s, int nvectors)
 295{
 296    char *cmd;
 297
 298    cmd = g_strdup_printf("-chardev socket,id=chr0,path=%s "
 299                          "-device ivshmem-doorbell,chardev=chr0,vectors=%d",
 300                          tmpserver, nvectors);
 301
 302    setup_vm_cmd(s, cmd, true);
 303
 304    g_free(cmd);
 305}
 306
 307static void test_ivshmem_server(void)
 308{
 309    IVState state1, state2, *s1, *s2;
 310    ServerThread thread;
 311    IvshmemServer server;
 312    int ret, vm1, vm2;
 313    int nvectors = 2;
 314    guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
 315
 316    ret = ivshmem_server_init(&server, tmpserver, tmpshm, true,
 317                              TMPSHMSIZE, nvectors,
 318                              g_test_verbose());
 319    g_assert_cmpint(ret, ==, 0);
 320
 321    ret = ivshmem_server_start(&server);
 322    g_assert_cmpint(ret, ==, 0);
 323
 324    thread.server = &server;
 325    ret = pipe(thread.pipe);
 326    g_assert_cmpint(ret, ==, 0);
 327    thread.thread = g_thread_new("ivshmem-server", server_thread, &thread);
 328    g_assert(thread.thread != NULL);
 329
 330    setup_vm_with_server(&state1, nvectors);
 331    s1 = &state1;
 332    setup_vm_with_server(&state2, nvectors);
 333    s2 = &state2;
 334
 335    /* check got different VM ids */
 336    vm1 = in_reg(s1, IVPOSITION);
 337    vm2 = in_reg(s2, IVPOSITION);
 338    g_assert_cmpint(vm1, >=, 0);
 339    g_assert_cmpint(vm2, >=, 0);
 340    g_assert_cmpint(vm1, !=, vm2);
 341
 342    /* check number of MSI-X vectors */
 343    ret = qpci_msix_table_size(s1->dev);
 344    g_assert_cmpuint(ret, ==, nvectors);
 345
 346    /* TODO test behavior before MSI-X is enabled */
 347
 348    /* ping vm2 -> vm1 on vector 0 */
 349    ret = qpci_msix_pending(s1->dev, 0);
 350    g_assert_cmpuint(ret, ==, 0);
 351    out_reg(s2, DOORBELL, vm1 << 16);
 352    do {
 353        g_usleep(10000);
 354        ret = qpci_msix_pending(s1->dev, 0);
 355    } while (ret == 0 && g_get_monotonic_time() < end_time);
 356    g_assert_cmpuint(ret, !=, 0);
 357
 358    /* ping vm1 -> vm2 on vector 1 */
 359    ret = qpci_msix_pending(s2->dev, 1);
 360    g_assert_cmpuint(ret, ==, 0);
 361    out_reg(s1, DOORBELL, vm2 << 16 | 1);
 362    do {
 363        g_usleep(10000);
 364        ret = qpci_msix_pending(s2->dev, 1);
 365    } while (ret == 0 && g_get_monotonic_time() < end_time);
 366    g_assert_cmpuint(ret, !=, 0);
 367
 368    cleanup_vm(s2);
 369    cleanup_vm(s1);
 370
 371    if (qemu_write_full(thread.pipe[1], "q", 1) != 1) {
 372        g_error("qemu_write_full: %s", g_strerror(errno));
 373    }
 374
 375    g_thread_join(thread.thread);
 376
 377    ivshmem_server_close(&server);
 378    close(thread.pipe[1]);
 379    close(thread.pipe[0]);
 380}
 381
 382#define PCI_SLOT_HP             0x06
 383
 384static void test_ivshmem_hotplug(void)
 385{
 386    QTestState *qts;
 387    const char *arch = qtest_get_arch();
 388
 389    qts = qtest_init("-object memory-backend-ram,size=1M,id=mb1");
 390
 391    global_qtest = qts;  /* TODO: Get rid of global_qtest here */
 392    qtest_qmp_device_add("ivshmem-plain", "iv1",
 393                         "{'addr': %s, 'memdev': 'mb1'}",
 394                         stringify(PCI_SLOT_HP));
 395    if (strcmp(arch, "ppc64") != 0) {
 396        qpci_unplug_acpi_device_test(qts, "iv1", PCI_SLOT_HP);
 397    }
 398
 399    qtest_quit(qts);
 400    global_qtest = NULL;
 401}
 402
 403static void test_ivshmem_memdev(void)
 404{
 405    IVState state;
 406
 407    /* just for the sake of checking memory-backend property */
 408    setup_vm_cmd(&state, "-object memory-backend-ram,size=1M,id=mb1"
 409                 " -device ivshmem-plain,memdev=mb1", false);
 410
 411    cleanup_vm(&state);
 412}
 413
 414static void cleanup(void)
 415{
 416    if (tmpshmem) {
 417        munmap(tmpshmem, TMPSHMSIZE);
 418        tmpshmem = NULL;
 419    }
 420
 421    if (tmpshm) {
 422        shm_unlink(tmpshm);
 423        g_free(tmpshm);
 424        tmpshm = NULL;
 425    }
 426
 427    if (tmpserver) {
 428        g_unlink(tmpserver);
 429        g_free(tmpserver);
 430        tmpserver = NULL;
 431    }
 432
 433    if (tmpdir) {
 434        g_rmdir(tmpdir);
 435        tmpdir = NULL;
 436    }
 437}
 438
 439static void abrt_handler(void *data)
 440{
 441    cleanup();
 442}
 443
 444static gchar *mktempshm(int size, int *fd)
 445{
 446    while (true) {
 447        gchar *name;
 448
 449        name = g_strdup_printf("/qtest-%u-%u", getpid(), g_random_int());
 450        *fd = shm_open(name, O_CREAT|O_RDWR|O_EXCL,
 451                       S_IRWXU|S_IRWXG|S_IRWXO);
 452        if (*fd > 0) {
 453            g_assert(ftruncate(*fd, size) == 0);
 454            return name;
 455        }
 456
 457        g_free(name);
 458
 459        if (errno != EEXIST) {
 460            perror("shm_open");
 461            return NULL;
 462        }
 463    }
 464}
 465
 466int main(int argc, char **argv)
 467{
 468    int ret, fd;
 469    const char *arch = qtest_get_arch();
 470    gchar dir[] = "/tmp/ivshmem-test.XXXXXX";
 471
 472    g_test_init(&argc, &argv, NULL);
 473
 474    qtest_add_abrt_handler(abrt_handler, NULL);
 475    /* shm */
 476    tmpshm = mktempshm(TMPSHMSIZE, &fd);
 477    if (!tmpshm) {
 478        goto out;
 479    }
 480    tmpshmem = mmap(0, TMPSHMSIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
 481    g_assert(tmpshmem != MAP_FAILED);
 482    /* server */
 483    if (mkdtemp(dir) == NULL) {
 484        g_error("mkdtemp: %s", g_strerror(errno));
 485    }
 486    tmpdir = dir;
 487    tmpserver = g_strconcat(tmpdir, "/server", NULL);
 488
 489    qtest_add_func("/ivshmem/single", test_ivshmem_single);
 490    qtest_add_func("/ivshmem/hotplug", test_ivshmem_hotplug);
 491    qtest_add_func("/ivshmem/memdev", test_ivshmem_memdev);
 492    if (g_test_slow()) {
 493        qtest_add_func("/ivshmem/pair", test_ivshmem_pair);
 494        if (strcmp(arch, "ppc64") != 0) {
 495            qtest_add_func("/ivshmem/server", test_ivshmem_server);
 496        }
 497    }
 498
 499out:
 500    ret = g_test_run();
 501    cleanup();
 502    return ret;
 503}
 504