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