| 1 | #include "qemu/osdep.h" |
| 2 | #include "../libqtest.h" |
| 3 | #include "libqos.h" |
| 4 | #include "pci.h" |
| 5 | #include "qobject/qdict.h" |
| 6 | |
| 7 | /*** Test Setup & Teardown ***/ |
| 8 | |
| 9 | /** |
| 10 | * Launch QEMU with the given command line, |
| 11 | * and then set up interrupts and our guest malloc interface. |
| 12 | * Never returns NULL: |
| 13 | * Terminates the application in case an error is encountered. |
| 14 | */ |
| 15 | QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap) |
| 16 | { |
| 17 | char *cmdline; |
| 18 | |
| 19 | QOSState *qs = g_new0(QOSState, 1); |
| 20 | |
| 21 | cmdline = g_strdup_vprintf(cmdline_fmt, ap); |
| 22 | qs->qts = qtest_init(cmdline); |
| 23 | qs->ops = ops; |
| 24 | if (ops) { |
| 25 | ops->alloc_init(&qs->alloc, qs->qts, ALLOC_NO_FLAGS); |
| 26 | qs->pcibus = ops->qpci_new(qs->qts, &qs->alloc); |
| 27 | } |
| 28 | |
| 29 | g_free(cmdline); |
| 30 | return qs; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Launch QEMU with the given command line, |
| 35 | * and then set up interrupts and our guest malloc interface. |
| 36 | */ |
| 37 | QOSState *qtest_boot(QOSOps *ops, const char *cmdline_fmt, ...) |
| 38 | { |
| 39 | QOSState *qs; |
| 40 | va_list ap; |
| 41 | |
| 42 | va_start(ap, cmdline_fmt); |
| 43 | qs = qtest_vboot(ops, cmdline_fmt, ap); |
| 44 | va_end(ap); |
| 45 | |
| 46 | return qs; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Tear down the QEMU instance. |
| 51 | */ |
| 52 | void qtest_common_shutdown(QOSState *qs) |
| 53 | { |
| 54 | if (qs->ops) { |
| 55 | if (qs->pcibus && qs->ops->qpci_free) { |
| 56 | qs->ops->qpci_free(qs->pcibus); |
| 57 | qs->pcibus = NULL; |
| 58 | } |
| 59 | } |
| 60 | alloc_destroy(&qs->alloc); |
| 61 | qtest_quit(qs->qts); |
| 62 | g_free(qs); |
| 63 | } |
| 64 | |
| 65 | void qtest_shutdown(QOSState *qs) |
| 66 | { |
| 67 | if (qs->ops && qs->ops->shutdown) { |
| 68 | qs->ops->shutdown(qs); |
| 69 | } else { |
| 70 | qtest_common_shutdown(qs); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static QDict *qmp_execute(QTestState *qts, const char *command) |
| 75 | { |
| 76 | return qtest_qmp(qts, "{ 'execute': %s }", command); |
| 77 | } |
| 78 | |
| 79 | void migrate(QOSState *from, QOSState *to, const char *uri) |
| 80 | { |
| 81 | const char *st; |
| 82 | QDict *rsp, *sub; |
| 83 | bool running; |
| 84 | |
| 85 | /* Is the machine currently running? */ |
| 86 | rsp = qmp_execute(from->qts, "query-status"); |
| 87 | g_assert(qdict_haskey(rsp, "return")); |
| 88 | sub = qdict_get_qdict(rsp, "return"); |
| 89 | g_assert(qdict_haskey(sub, "running")); |
| 90 | running = qdict_get_bool(sub, "running"); |
| 91 | qobject_unref(rsp); |
| 92 | |
| 93 | /* Issue the migrate command. */ |
| 94 | rsp = qtest_qmp(from->qts, |
| 95 | "{ 'execute': 'migrate', 'arguments': { 'uri': %s }}", |
| 96 | uri); |
| 97 | g_assert(qdict_haskey(rsp, "return")); |
| 98 | qobject_unref(rsp); |
| 99 | |
| 100 | /* Wait for STOP event, but only if we were running: */ |
| 101 | if (running) { |
| 102 | qtest_qmp_eventwait(from->qts, "STOP"); |
| 103 | } |
| 104 | |
| 105 | /* If we were running, we can wait for an event. */ |
| 106 | if (running) { |
| 107 | migrate_allocator(&from->alloc, &to->alloc); |
| 108 | qtest_qmp_eventwait(to->qts, "RESUME"); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | /* Otherwise, we need to wait: poll until migration is completed. */ |
| 113 | while (1) { |
| 114 | rsp = qmp_execute(from->qts, "query-migrate"); |
| 115 | g_assert(qdict_haskey(rsp, "return")); |
| 116 | sub = qdict_get_qdict(rsp, "return"); |
| 117 | g_assert(qdict_haskey(sub, "status")); |
| 118 | st = qdict_get_str(sub, "status"); |
| 119 | |
| 120 | /* "setup", "active", "device", "completed", "failed", "cancelled" */ |
| 121 | if (strcmp(st, "completed") == 0) { |
| 122 | qobject_unref(rsp); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | if ((strcmp(st, "setup") == 0) || (strcmp(st, "active") == 0) |
| 127 | || (strcmp(st, "device") == 0) |
| 128 | || (strcmp(st, "wait-unplug") == 0)) { |
| 129 | qobject_unref(rsp); |
| 130 | g_usleep(5000); |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | fprintf(stderr, "Migration did not complete, status: %s\n", st); |
| 135 | g_assert_not_reached(); |
| 136 | } |
| 137 | |
| 138 | migrate_allocator(&from->alloc, &to->alloc); |
| 139 | } |
| 140 | |
| 141 | void mkqcow2(const char *file, unsigned size_mb) |
| 142 | { |
| 143 | g_assert_true(mkimg(file, "qcow2", size_mb)); |
| 144 | } |
| 145 | |
| 146 | void prepare_blkdebug_script(const char *debug_fn, const char *event) |
| 147 | { |
| 148 | FILE *debug_file = fopen(debug_fn, "w"); |
| 149 | int ret; |
| 150 | |
| 151 | fprintf(debug_file, "[inject-error]\n"); |
| 152 | fprintf(debug_file, "event = \"%s\"\n", event); |
| 153 | fprintf(debug_file, "errno = \"5\"\n"); |
| 154 | fprintf(debug_file, "state = \"1\"\n"); |
| 155 | fprintf(debug_file, "immediately = \"off\"\n"); |
| 156 | fprintf(debug_file, "once = \"on\"\n"); |
| 157 | |
| 158 | fprintf(debug_file, "[set-state]\n"); |
| 159 | fprintf(debug_file, "event = \"%s\"\n", event); |
| 160 | fprintf(debug_file, "new_state = \"2\"\n"); |
| 161 | fflush(debug_file); |
| 162 | g_assert(!ferror(debug_file)); |
| 163 | |
| 164 | ret = fclose(debug_file); |
| 165 | g_assert(ret == 0); |
| 166 | } |
| 167 | |
| 168 | void generate_pattern(void *buffer, size_t len, size_t cycle_len) |
| 169 | { |
| 170 | int i, j; |
| 171 | unsigned char *tx = (unsigned char *)buffer; |
| 172 | unsigned char p; |
| 173 | size_t *sx; |
| 174 | |
| 175 | /* Write an indicative pattern that varies and is unique per-cycle */ |
| 176 | p = rand() % 256; |
| 177 | for (i = 0; i < len; i++) { |
| 178 | tx[i] = p++ % 256; |
| 179 | if (i % cycle_len == 0) { |
| 180 | p = rand() % 256; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* force uniqueness by writing an id per-cycle */ |
| 185 | for (i = 0; i < len / cycle_len; i++) { |
| 186 | j = i * cycle_len; |
| 187 | if (j + sizeof(*sx) <= len) { |
| 188 | sx = (size_t *)&tx[j]; |
| 189 | *sx = i; |
| 190 | } |
| 191 | } |
| 192 | } |