1 /*
2  * QTest migration helpers
3  *
4  * Copyright (c) 2016-2018 Red Hat, Inc. and/or its affiliates
5  *   based on the vhost-user-test.c that is:
6  *      Copyright (c) 2014 Virtual Open Systems Sarl.
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  *
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/ctype.h"
15 #include "qapi/qmp/qjson.h"
16 #include "qapi/qapi-visit-sockets.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qlist.h"
20 #include "qemu/cutils.h"
21 #include "qemu/memalign.h"
22 
23 #include "migration-helpers.h"
24 
25 /*
26  * Number of seconds we wait when looking for migration
27  * status changes, to avoid test suite hanging forever
28  * when things go wrong. Needs to be higher enough to
29  * avoid false positives on loaded hosts.
30  */
31 #define MIGRATION_STATUS_WAIT_TIMEOUT 120
32 
33 static char *SocketAddress_to_str(SocketAddress *addr)
34 {
35     switch (addr->type) {
36     case SOCKET_ADDRESS_TYPE_INET:
37         return g_strdup_printf("tcp:%s:%s",
38                                addr->u.inet.host,
39                                addr->u.inet.port);
40     case SOCKET_ADDRESS_TYPE_UNIX:
41         return g_strdup_printf("unix:%s",
42                                addr->u.q_unix.path);
43     case SOCKET_ADDRESS_TYPE_FD:
44         return g_strdup_printf("fd:%s", addr->u.fd.str);
45     case SOCKET_ADDRESS_TYPE_VSOCK:
46         return g_strdup_printf("vsock:%s:%s",
47                                addr->u.vsock.cid,
48                                addr->u.vsock.port);
49     default:
50         return g_strdup("unknown address type");
51     }
52 }
53 
54 static QDict *SocketAddress_to_qdict(SocketAddress *addr)
55 {
56     QDict *dict = qdict_new();
57 
58     switch (addr->type) {
59     case SOCKET_ADDRESS_TYPE_INET:
60         qdict_put_str(dict, "type", "inet");
61         qdict_put_str(dict, "host", addr->u.inet.host);
62         qdict_put_str(dict, "port", addr->u.inet.port);
63         break;
64     case SOCKET_ADDRESS_TYPE_UNIX:
65         qdict_put_str(dict, "type", "unix");
66         qdict_put_str(dict, "path", addr->u.q_unix.path);
67         break;
68     case SOCKET_ADDRESS_TYPE_FD:
69         qdict_put_str(dict, "type", "fd");
70         qdict_put_str(dict, "str", addr->u.fd.str);
71         break;
72     case SOCKET_ADDRESS_TYPE_VSOCK:
73         qdict_put_str(dict, "type", "vsock");
74         qdict_put_str(dict, "cid", addr->u.vsock.cid);
75         qdict_put_str(dict, "port", addr->u.vsock.port);
76         break;
77     default:
78         g_assert_not_reached();
79         break;
80     }
81 
82     return dict;
83 }
84 
85 static SocketAddress *migrate_get_socket_address(QTestState *who)
86 {
87     QDict *rsp;
88     SocketAddressList *addrs;
89     SocketAddress *addr;
90     Visitor *iv = NULL;
91     QObject *object;
92 
93     rsp = migrate_query(who);
94     object = qdict_get(rsp, "socket-address");
95 
96     iv = qobject_input_visitor_new(object);
97     visit_type_SocketAddressList(iv, NULL, &addrs, &error_abort);
98     addr = addrs->value;
99     visit_free(iv);
100 
101     qobject_unref(rsp);
102     return addr;
103 }
104 
105 static char *
106 migrate_get_connect_uri(QTestState *who)
107 {
108     SocketAddress *addrs;
109     char *connect_uri;
110 
111     addrs = migrate_get_socket_address(who);
112     connect_uri = SocketAddress_to_str(addrs);
113 
114     qapi_free_SocketAddress(addrs);
115     return connect_uri;
116 }
117 
118 static QDict *
119 migrate_get_connect_qdict(QTestState *who)
120 {
121     SocketAddress *addrs;
122     QDict *connect_qdict;
123 
124     addrs = migrate_get_socket_address(who);
125     connect_qdict = SocketAddress_to_qdict(addrs);
126 
127     qapi_free_SocketAddress(addrs);
128     return connect_qdict;
129 }
130 
131 static void migrate_set_ports(QTestState *to, QList *channel_list)
132 {
133     QDict *addr;
134     QListEntry *entry;
135     const char *addr_port = NULL;
136 
137     addr = migrate_get_connect_qdict(to);
138 
139     QLIST_FOREACH_ENTRY(channel_list, entry) {
140         QDict *channel = qobject_to(QDict, qlist_entry_obj(entry));
141         QDict *addrdict = qdict_get_qdict(channel, "addr");
142 
143         if (qdict_haskey(addrdict, "port") &&
144             qdict_haskey(addr, "port") &&
145             (strcmp(qdict_get_str(addrdict, "port"), "0") == 0)) {
146                 addr_port = qdict_get_str(addr, "port");
147                 qdict_put_str(addrdict, "port", g_strdup(addr_port));
148         }
149     }
150 
151     qobject_unref(addr);
152 }
153 
154 bool migrate_watch_for_events(QTestState *who, const char *name,
155                               QDict *event, void *opaque)
156 {
157     QTestMigrationState *state = opaque;
158 
159     if (g_str_equal(name, "STOP")) {
160         state->stop_seen = true;
161         return true;
162     } else if (g_str_equal(name, "SUSPEND")) {
163         state->suspend_seen = true;
164         return true;
165     } else if (g_str_equal(name, "RESUME")) {
166         state->resume_seen = true;
167         return true;
168     }
169 
170     return false;
171 }
172 
173 void migrate_qmp_fail(QTestState *who, const char *uri,
174                       const char *channels, const char *fmt, ...)
175 {
176     va_list ap;
177     QDict *args, *err;
178 
179     va_start(ap, fmt);
180     args = qdict_from_vjsonf_nofail(fmt, ap);
181     va_end(ap);
182 
183     g_assert(!qdict_haskey(args, "uri"));
184     if (uri) {
185         qdict_put_str(args, "uri", uri);
186     }
187 
188     g_assert(!qdict_haskey(args, "channels"));
189     if (channels) {
190         QObject *channels_obj = qobject_from_json(channels, &error_abort);
191         qdict_put_obj(args, "channels", channels_obj);
192     }
193 
194     err = qtest_qmp_assert_failure_ref(
195         who, "{ 'execute': 'migrate', 'arguments': %p}", args);
196 
197     g_assert(qdict_haskey(err, "desc"));
198 
199     qobject_unref(err);
200 }
201 
202 /*
203  * Send QMP command "migrate".
204  * Arguments are built from @fmt... (formatted like
205  * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
206  */
207 void migrate_qmp(QTestState *who, QTestState *to, const char *uri,
208                  const char *channels, const char *fmt, ...)
209 {
210     va_list ap;
211     QDict *args;
212     g_autofree char *connect_uri = NULL;
213 
214     va_start(ap, fmt);
215     args = qdict_from_vjsonf_nofail(fmt, ap);
216     va_end(ap);
217 
218     g_assert(!qdict_haskey(args, "uri"));
219     if (uri) {
220         qdict_put_str(args, "uri", uri);
221     } else if (!channels) {
222         connect_uri = migrate_get_connect_uri(to);
223         qdict_put_str(args, "uri", connect_uri);
224     }
225 
226     g_assert(!qdict_haskey(args, "channels"));
227     if (channels) {
228         QObject *channels_obj = qobject_from_json(channels, &error_abort);
229         QList *channel_list = qobject_to(QList, channels_obj);
230         migrate_set_ports(to, channel_list);
231         qdict_put_obj(args, "channels", channels_obj);
232     }
233 
234     qtest_qmp_assert_success(who,
235                              "{ 'execute': 'migrate', 'arguments': %p}", args);
236 }
237 
238 void migrate_set_capability(QTestState *who, const char *capability,
239                             bool value)
240 {
241     qtest_qmp_assert_success(who,
242                              "{ 'execute': 'migrate-set-capabilities',"
243                              "'arguments': { "
244                              "'capabilities': [ { "
245                              "'capability': %s, 'state': %i } ] } }",
246                              capability, value);
247 }
248 
249 void migrate_incoming_qmp(QTestState *to, const char *uri, const char *fmt, ...)
250 {
251     va_list ap;
252     QDict *args, *rsp, *data;
253 
254     va_start(ap, fmt);
255     args = qdict_from_vjsonf_nofail(fmt, ap);
256     va_end(ap);
257 
258     g_assert(!qdict_haskey(args, "uri"));
259     qdict_put_str(args, "uri", uri);
260 
261     migrate_set_capability(to, "events", true);
262 
263     rsp = qtest_qmp(to, "{ 'execute': 'migrate-incoming', 'arguments': %p}",
264                     args);
265 
266     if (!qdict_haskey(rsp, "return")) {
267         g_autoptr(GString) s = qobject_to_json_pretty(QOBJECT(rsp), true);
268         g_test_message("%s", s->str);
269     }
270 
271     g_assert(qdict_haskey(rsp, "return"));
272     qobject_unref(rsp);
273 
274     rsp = qtest_qmp_eventwait_ref(to, "MIGRATION");
275     g_assert(qdict_haskey(rsp, "data"));
276 
277     data = qdict_get_qdict(rsp, "data");
278     g_assert(qdict_haskey(data, "status"));
279     g_assert_cmpstr(qdict_get_str(data, "status"), ==, "setup");
280 
281     qobject_unref(rsp);
282 }
283 
284 /*
285  * Note: caller is responsible to free the returned object via
286  * qobject_unref() after use
287  */
288 QDict *migrate_query(QTestState *who)
289 {
290     return qtest_qmp_assert_success_ref(who, "{ 'execute': 'query-migrate' }");
291 }
292 
293 QDict *migrate_query_not_failed(QTestState *who)
294 {
295     const char *status;
296     QDict *rsp = migrate_query(who);
297     status = qdict_get_str(rsp, "status");
298     if (g_str_equal(status, "failed")) {
299         g_printerr("query-migrate shows failed migration: %s\n",
300                    qdict_get_str(rsp, "error-desc"));
301     }
302     g_assert(!g_str_equal(status, "failed"));
303     return rsp;
304 }
305 
306 /*
307  * Note: caller is responsible to free the returned object via
308  * g_free() after use
309  */
310 static gchar *migrate_query_status(QTestState *who)
311 {
312     QDict *rsp_return = migrate_query(who);
313     gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
314 
315     g_assert(status);
316     qobject_unref(rsp_return);
317 
318     return status;
319 }
320 
321 static bool check_migration_status(QTestState *who, const char *goal,
322                                    const char **ungoals)
323 {
324     bool ready;
325     char *current_status;
326     const char **ungoal;
327 
328     current_status = migrate_query_status(who);
329     ready = strcmp(current_status, goal) == 0;
330     if (!ungoals) {
331         g_assert_cmpstr(current_status, !=, "failed");
332         /*
333          * If looking for a state other than completed,
334          * completion of migration would cause the test to
335          * hang.
336          */
337         if (strcmp(goal, "completed") != 0) {
338             g_assert_cmpstr(current_status, !=, "completed");
339         }
340     } else {
341         for (ungoal = ungoals; *ungoal; ungoal++) {
342             g_assert_cmpstr(current_status, !=,  *ungoal);
343         }
344     }
345     g_free(current_status);
346     return ready;
347 }
348 
349 void wait_for_migration_status(QTestState *who,
350                                const char *goal, const char **ungoals)
351 {
352     g_test_timer_start();
353     while (!check_migration_status(who, goal, ungoals)) {
354         usleep(1000);
355 
356         g_assert(g_test_timer_elapsed() < MIGRATION_STATUS_WAIT_TIMEOUT);
357     }
358 }
359 
360 void wait_for_migration_complete(QTestState *who)
361 {
362     wait_for_migration_status(who, "completed", NULL);
363 }
364 
365 void wait_for_migration_fail(QTestState *from, bool allow_active)
366 {
367     g_test_timer_start();
368     QDict *rsp_return;
369     char *status;
370     bool failed;
371 
372     do {
373         status = migrate_query_status(from);
374         bool result = !strcmp(status, "setup") || !strcmp(status, "failed") ||
375             (allow_active && !strcmp(status, "active"));
376         if (!result) {
377             fprintf(stderr, "%s: unexpected status status=%s allow_active=%d\n",
378                     __func__, status, allow_active);
379         }
380         g_assert(result);
381         failed = !strcmp(status, "failed");
382         g_free(status);
383 
384         g_assert(g_test_timer_elapsed() < MIGRATION_STATUS_WAIT_TIMEOUT);
385     } while (!failed);
386 
387     /* Is the machine currently running? */
388     rsp_return = qtest_qmp_assert_success_ref(from,
389                                               "{ 'execute': 'query-status' }");
390     g_assert(qdict_haskey(rsp_return, "running"));
391     g_assert(qdict_get_bool(rsp_return, "running"));
392     qobject_unref(rsp_return);
393 }
394 
395 char *find_common_machine_version(const char *mtype, const char *var1,
396                                   const char *var2)
397 {
398     g_autofree char *type1 = qtest_resolve_machine_alias(var1, mtype);
399     g_autofree char *type2 = qtest_resolve_machine_alias(var2, mtype);
400 
401     g_assert(type1 && type2);
402 
403     if (g_str_equal(type1, type2)) {
404         /* either can be used */
405         return g_strdup(type1);
406     }
407 
408     if (qtest_has_machine_with_env(var2, type1)) {
409         return g_strdup(type1);
410     }
411 
412     if (qtest_has_machine_with_env(var1, type2)) {
413         return g_strdup(type2);
414     }
415 
416     g_test_message("No common machine version for machine type '%s' between "
417                    "binaries %s and %s", mtype, getenv(var1), getenv(var2));
418     g_assert_not_reached();
419 }
420 
421 char *resolve_machine_version(const char *alias, const char *var1,
422                               const char *var2)
423 {
424     const char *mname = g_getenv("QTEST_QEMU_MACHINE_TYPE");
425     g_autofree char *machine_name = NULL;
426 
427     if (mname) {
428         const char *dash = strrchr(mname, '-');
429         const char *dot = strrchr(mname, '.');
430 
431         machine_name = g_strdup(mname);
432 
433         if (dash && dot) {
434             assert(qtest_has_machine(machine_name));
435             return g_steal_pointer(&machine_name);
436         }
437         /* else: probably an alias, let it be resolved below */
438     } else {
439         /* use the hardcoded alias */
440         machine_name = g_strdup(alias);
441     }
442 
443     return find_common_machine_version(machine_name, var1, var2);
444 }
445 
446 typedef struct {
447     char *name;
448     void (*func)(void);
449 } MigrationTest;
450 
451 static void migration_test_destroy(gpointer data)
452 {
453     MigrationTest *test = (MigrationTest *)data;
454 
455     g_free(test->name);
456     g_free(test);
457 }
458 
459 static void migration_test_wrapper(const void *data)
460 {
461     MigrationTest *test = (MigrationTest *)data;
462 
463     g_test_message("Running /%s%s", qtest_get_arch(), test->name);
464     test->func();
465 }
466 
467 void migration_test_add(const char *path, void (*fn)(void))
468 {
469     MigrationTest *test = g_new0(MigrationTest, 1);
470 
471     test->func = fn;
472     test->name = g_strdup(path);
473 
474     qtest_add_data_func_full(path, test, migration_test_wrapper,
475                              migration_test_destroy);
476 }
477 
478 #ifdef O_DIRECT
479 /*
480  * Probe for O_DIRECT support on the filesystem. Since this is used
481  * for tests, be conservative, if anything fails, assume it's
482  * unsupported.
483  */
484 bool probe_o_direct_support(const char *tmpfs)
485 {
486     g_autofree char *filename = g_strdup_printf("%s/probe-o-direct", tmpfs);
487     int fd, flags = O_CREAT | O_RDWR | O_TRUNC | O_DIRECT;
488     void *buf;
489     ssize_t ret, len;
490     uint64_t offset;
491 
492     fd = open(filename, flags, 0660);
493     if (fd < 0) {
494         unlink(filename);
495         return false;
496     }
497 
498     /*
499      * Using 1MB alignment as conservative choice to satisfy any
500      * plausible architecture default page size, and/or filesystem
501      * alignment restrictions.
502      */
503     len = 0x100000;
504     offset = 0x100000;
505 
506     buf = qemu_try_memalign(len, len);
507     g_assert(buf);
508 
509     ret = pwrite(fd, buf, len, offset);
510     unlink(filename);
511     g_free(buf);
512 
513     if (ret < 0) {
514         return false;
515     }
516 
517     return true;
518 }
519 #endif
520