qemu/tests/libqos/libqos.c
<<
>>
Prefs
   1#include "qemu/osdep.h"
   2#include <sys/wait.h>
   3
   4#include "libqtest.h"
   5#include "libqos/libqos.h"
   6#include "libqos/pci.h"
   7#include "qapi/qmp/qdict.h"
   8
   9/*** Test Setup & Teardown ***/
  10
  11/**
  12 * Launch QEMU with the given command line,
  13 * and then set up interrupts and our guest malloc interface.
  14 * Never returns NULL:
  15 * Terminates the application in case an error is encountered.
  16 */
  17QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
  18{
  19    char *cmdline;
  20
  21    QOSState *qs = g_new0(QOSState, 1);
  22
  23    cmdline = g_strdup_vprintf(cmdline_fmt, ap);
  24    qs->qts = qtest_init(cmdline);
  25    qs->ops = ops;
  26    if (ops) {
  27        qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
  28        qs->pcibus = ops->qpci_init(qs->qts, qs->alloc);
  29    }
  30
  31    g_free(cmdline);
  32    return qs;
  33}
  34
  35/**
  36 * Launch QEMU with the given command line,
  37 * and then set up interrupts and our guest malloc interface.
  38 */
  39QOSState *qtest_boot(QOSOps *ops, const char *cmdline_fmt, ...)
  40{
  41    QOSState *qs;
  42    va_list ap;
  43
  44    va_start(ap, cmdline_fmt);
  45    qs = qtest_vboot(ops, cmdline_fmt, ap);
  46    va_end(ap);
  47
  48    return qs;
  49}
  50
  51/**
  52 * Tear down the QEMU instance.
  53 */
  54void qtest_common_shutdown(QOSState *qs)
  55{
  56    if (qs->ops) {
  57        if (qs->pcibus && qs->ops->qpci_free) {
  58            qs->ops->qpci_free(qs->pcibus);
  59            qs->pcibus = NULL;
  60        }
  61        if (qs->alloc && qs->ops->uninit_allocator) {
  62            qs->ops->uninit_allocator(qs->alloc);
  63            qs->alloc = NULL;
  64        }
  65    }
  66    qtest_quit(qs->qts);
  67    g_free(qs);
  68}
  69
  70void qtest_shutdown(QOSState *qs)
  71{
  72    if (qs->ops && qs->ops->shutdown) {
  73        qs->ops->shutdown(qs);
  74    } else {
  75        qtest_common_shutdown(qs);
  76    }
  77}
  78
  79void set_context(QOSState *s)
  80{
  81    global_qtest = s->qts;
  82}
  83
  84static QDict *qmp_execute(QTestState *qts, const char *command)
  85{
  86    return qtest_qmp(qts, "{ 'execute': %s }", command);
  87}
  88
  89void migrate(QOSState *from, QOSState *to, const char *uri)
  90{
  91    const char *st;
  92    QDict *rsp, *sub;
  93    bool running;
  94
  95    set_context(from);
  96
  97    /* Is the machine currently running? */
  98    rsp = qmp_execute(from->qts, "query-status");
  99    g_assert(qdict_haskey(rsp, "return"));
 100    sub = qdict_get_qdict(rsp, "return");
 101    g_assert(qdict_haskey(sub, "running"));
 102    running = qdict_get_bool(sub, "running");
 103    qobject_unref(rsp);
 104
 105    /* Issue the migrate command. */
 106    rsp = qtest_qmp(from->qts,
 107                    "{ 'execute': 'migrate', 'arguments': { 'uri': %s }}",
 108                    uri);
 109    g_assert(qdict_haskey(rsp, "return"));
 110    qobject_unref(rsp);
 111
 112    /* Wait for STOP event, but only if we were running: */
 113    if (running) {
 114        qtest_qmp_eventwait(from->qts, "STOP");
 115    }
 116
 117    /* If we were running, we can wait for an event. */
 118    if (running) {
 119        migrate_allocator(from->alloc, to->alloc);
 120        set_context(to);
 121        qtest_qmp_eventwait(to->qts, "RESUME");
 122        return;
 123    }
 124
 125    /* Otherwise, we need to wait: poll until migration is completed. */
 126    while (1) {
 127        rsp = qmp_execute(from->qts, "query-migrate");
 128        g_assert(qdict_haskey(rsp, "return"));
 129        sub = qdict_get_qdict(rsp, "return");
 130        g_assert(qdict_haskey(sub, "status"));
 131        st = qdict_get_str(sub, "status");
 132
 133        /* "setup", "active", "completed", "failed", "cancelled" */
 134        if (strcmp(st, "completed") == 0) {
 135            qobject_unref(rsp);
 136            break;
 137        }
 138
 139        if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0)) {
 140            qobject_unref(rsp);
 141            g_usleep(5000);
 142            continue;
 143        }
 144
 145        fprintf(stderr, "Migration did not complete, status: %s\n", st);
 146        g_assert_not_reached();
 147    }
 148
 149    migrate_allocator(from->alloc, to->alloc);
 150    set_context(to);
 151}
 152
 153bool have_qemu_img(void)
 154{
 155    char *rpath;
 156    const char *path = getenv("QTEST_QEMU_IMG");
 157    if (!path) {
 158        return false;
 159    }
 160
 161    rpath = realpath(path, NULL);
 162    if (!rpath) {
 163        return false;
 164    } else {
 165        free(rpath);
 166        return true;
 167    }
 168}
 169
 170void mkimg(const char *file, const char *fmt, unsigned size_mb)
 171{
 172    gchar *cli;
 173    bool ret;
 174    int rc;
 175    GError *err = NULL;
 176    char *qemu_img_path;
 177    gchar *out, *out2;
 178    char *qemu_img_abs_path;
 179
 180    qemu_img_path = getenv("QTEST_QEMU_IMG");
 181    g_assert(qemu_img_path);
 182    qemu_img_abs_path = realpath(qemu_img_path, NULL);
 183    g_assert(qemu_img_abs_path);
 184
 185    cli = g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path,
 186                          fmt, file, size_mb);
 187    ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
 188    if (err || !g_spawn_check_exit_status(rc, &err)) {
 189        fprintf(stderr, "%s\n", err->message);
 190        g_error_free(err);
 191    }
 192    g_assert(ret && !err);
 193
 194    g_free(out);
 195    g_free(out2);
 196    g_free(cli);
 197    free(qemu_img_abs_path);
 198}
 199
 200void mkqcow2(const char *file, unsigned size_mb)
 201{
 202    return mkimg(file, "qcow2", size_mb);
 203}
 204
 205void prepare_blkdebug_script(const char *debug_fn, const char *event)
 206{
 207    FILE *debug_file = fopen(debug_fn, "w");
 208    int ret;
 209
 210    fprintf(debug_file, "[inject-error]\n");
 211    fprintf(debug_file, "event = \"%s\"\n", event);
 212    fprintf(debug_file, "errno = \"5\"\n");
 213    fprintf(debug_file, "state = \"1\"\n");
 214    fprintf(debug_file, "immediately = \"off\"\n");
 215    fprintf(debug_file, "once = \"on\"\n");
 216
 217    fprintf(debug_file, "[set-state]\n");
 218    fprintf(debug_file, "event = \"%s\"\n", event);
 219    fprintf(debug_file, "new_state = \"2\"\n");
 220    fflush(debug_file);
 221    g_assert(!ferror(debug_file));
 222
 223    ret = fclose(debug_file);
 224    g_assert(ret == 0);
 225}
 226
 227void generate_pattern(void *buffer, size_t len, size_t cycle_len)
 228{
 229    int i, j;
 230    unsigned char *tx = (unsigned char *)buffer;
 231    unsigned char p;
 232    size_t *sx;
 233
 234    /* Write an indicative pattern that varies and is unique per-cycle */
 235    p = rand() % 256;
 236    for (i = 0; i < len; i++) {
 237        tx[i] = p++ % 256;
 238        if (i % cycle_len == 0) {
 239            p = rand() % 256;
 240        }
 241    }
 242
 243    /* force uniqueness by writing an id per-cycle */
 244    for (i = 0; i < len / cycle_len; i++) {
 245        j = i * cycle_len;
 246        if (j + sizeof(*sx) <= len) {
 247            sx = (size_t *)&tx[j];
 248            *sx = i;
 249        }
 250    }
 251}
 252