2
0

migration-test.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /*
  2. * QTest testcase for migration
  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. #include "qemu/osdep.h"
  13. #include "libqtest.h"
  14. #include "qapi/qmp/qdict.h"
  15. #include "qapi/qmp/qjson.h"
  16. #include "qemu/module.h"
  17. #include "qemu/option.h"
  18. #include "qemu/range.h"
  19. #include "qemu/sockets.h"
  20. #include "chardev/char.h"
  21. #include "qapi/qapi-visit-sockets.h"
  22. #include "qapi/qobject-input-visitor.h"
  23. #include "qapi/qobject-output-visitor.h"
  24. #include "migration/migration-test.h"
  25. /* TODO actually test the results and get rid of this */
  26. #define qtest_qmp_discard_response(...) qobject_unref(qtest_qmp(__VA_ARGS__))
  27. unsigned start_address;
  28. unsigned end_address;
  29. bool got_stop;
  30. static bool uffd_feature_thread_id;
  31. #if defined(__linux__)
  32. #include <sys/syscall.h>
  33. #include <sys/vfs.h>
  34. #endif
  35. #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
  36. #include <sys/eventfd.h>
  37. #include <sys/ioctl.h>
  38. #include <linux/userfaultfd.h>
  39. static bool ufd_version_check(void)
  40. {
  41. struct uffdio_api api_struct;
  42. uint64_t ioctl_mask;
  43. int ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
  44. if (ufd == -1) {
  45. g_test_message("Skipping test: userfaultfd not available");
  46. return false;
  47. }
  48. api_struct.api = UFFD_API;
  49. api_struct.features = 0;
  50. if (ioctl(ufd, UFFDIO_API, &api_struct)) {
  51. g_test_message("Skipping test: UFFDIO_API failed");
  52. return false;
  53. }
  54. uffd_feature_thread_id = api_struct.features & UFFD_FEATURE_THREAD_ID;
  55. ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
  56. (__u64)1 << _UFFDIO_UNREGISTER;
  57. if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
  58. g_test_message("Skipping test: Missing userfault feature");
  59. return false;
  60. }
  61. return true;
  62. }
  63. #else
  64. static bool ufd_version_check(void)
  65. {
  66. g_test_message("Skipping test: Userfault not available (builtdtime)");
  67. return false;
  68. }
  69. #endif
  70. static const char *tmpfs;
  71. /* The boot file modifies memory area in [start_address, end_address)
  72. * repeatedly. It outputs a 'B' at a fixed rate while it's still running.
  73. */
  74. #include "tests/migration/i386/a-b-bootblock.h"
  75. #include "tests/migration/aarch64/a-b-kernel.h"
  76. #include "tests/migration/s390x/a-b-bios.h"
  77. static void init_bootfile(const char *bootpath, void *content, size_t len)
  78. {
  79. FILE *bootfile = fopen(bootpath, "wb");
  80. g_assert_cmpint(fwrite(content, len, 1, bootfile), ==, 1);
  81. fclose(bootfile);
  82. }
  83. /*
  84. * Wait for some output in the serial output file,
  85. * we get an 'A' followed by an endless string of 'B's
  86. * but on the destination we won't have the A.
  87. */
  88. static void wait_for_serial(const char *side)
  89. {
  90. char *serialpath = g_strdup_printf("%s/%s", tmpfs, side);
  91. FILE *serialfile = fopen(serialpath, "r");
  92. const char *arch = qtest_get_arch();
  93. int started = (strcmp(side, "src_serial") == 0 &&
  94. strcmp(arch, "ppc64") == 0) ? 0 : 1;
  95. g_free(serialpath);
  96. do {
  97. int readvalue = fgetc(serialfile);
  98. if (!started) {
  99. /* SLOF prints its banner before starting test,
  100. * to ignore it, mark the start of the test with '_',
  101. * ignore all characters until this marker
  102. */
  103. switch (readvalue) {
  104. case '_':
  105. started = 1;
  106. break;
  107. case EOF:
  108. fseek(serialfile, 0, SEEK_SET);
  109. usleep(1000);
  110. break;
  111. }
  112. continue;
  113. }
  114. switch (readvalue) {
  115. case 'A':
  116. /* Fine */
  117. break;
  118. case 'B':
  119. /* It's alive! */
  120. fclose(serialfile);
  121. return;
  122. case EOF:
  123. started = (strcmp(side, "src_serial") == 0 &&
  124. strcmp(arch, "ppc64") == 0) ? 0 : 1;
  125. fseek(serialfile, 0, SEEK_SET);
  126. usleep(1000);
  127. break;
  128. default:
  129. fprintf(stderr, "Unexpected %d on %s serial\n", readvalue, side);
  130. g_assert_not_reached();
  131. }
  132. } while (true);
  133. }
  134. static void stop_cb(void *opaque, const char *name, QDict *data)
  135. {
  136. if (!strcmp(name, "STOP")) {
  137. got_stop = true;
  138. }
  139. }
  140. /*
  141. * Events can get in the way of responses we are actually waiting for.
  142. */
  143. GCC_FMT_ATTR(3, 4)
  144. static QDict *wait_command_fd(QTestState *who, int fd, const char *command, ...)
  145. {
  146. va_list ap;
  147. va_start(ap, command);
  148. qtest_qmp_vsend_fds(who, &fd, 1, command, ap);
  149. va_end(ap);
  150. return qtest_qmp_receive_success(who, stop_cb, NULL);
  151. }
  152. /*
  153. * Events can get in the way of responses we are actually waiting for.
  154. */
  155. GCC_FMT_ATTR(2, 3)
  156. static QDict *wait_command(QTestState *who, const char *command, ...)
  157. {
  158. va_list ap;
  159. va_start(ap, command);
  160. qtest_qmp_vsend(who, command, ap);
  161. va_end(ap);
  162. return qtest_qmp_receive_success(who, stop_cb, NULL);
  163. }
  164. /*
  165. * Note: caller is responsible to free the returned object via
  166. * qobject_unref() after use
  167. */
  168. static QDict *migrate_query(QTestState *who)
  169. {
  170. return wait_command(who, "{ 'execute': 'query-migrate' }");
  171. }
  172. /*
  173. * Note: caller is responsible to free the returned object via
  174. * g_free() after use
  175. */
  176. static gchar *migrate_query_status(QTestState *who)
  177. {
  178. QDict *rsp_return = migrate_query(who);
  179. gchar *status = g_strdup(qdict_get_str(rsp_return, "status"));
  180. g_assert(status);
  181. qobject_unref(rsp_return);
  182. return status;
  183. }
  184. /*
  185. * It's tricky to use qemu's migration event capability with qtest,
  186. * events suddenly appearing confuse the qmp()/hmp() responses.
  187. */
  188. static int64_t read_ram_property_int(QTestState *who, const char *property)
  189. {
  190. QDict *rsp_return, *rsp_ram;
  191. int64_t result;
  192. rsp_return = migrate_query(who);
  193. if (!qdict_haskey(rsp_return, "ram")) {
  194. /* Still in setup */
  195. result = 0;
  196. } else {
  197. rsp_ram = qdict_get_qdict(rsp_return, "ram");
  198. result = qdict_get_try_int(rsp_ram, property, 0);
  199. }
  200. qobject_unref(rsp_return);
  201. return result;
  202. }
  203. static int64_t read_migrate_property_int(QTestState *who, const char *property)
  204. {
  205. QDict *rsp_return;
  206. int64_t result;
  207. rsp_return = migrate_query(who);
  208. result = qdict_get_try_int(rsp_return, property, 0);
  209. qobject_unref(rsp_return);
  210. return result;
  211. }
  212. static uint64_t get_migration_pass(QTestState *who)
  213. {
  214. return read_ram_property_int(who, "dirty-sync-count");
  215. }
  216. static void read_blocktime(QTestState *who)
  217. {
  218. QDict *rsp_return;
  219. rsp_return = migrate_query(who);
  220. g_assert(qdict_haskey(rsp_return, "postcopy-blocktime"));
  221. qobject_unref(rsp_return);
  222. }
  223. static bool check_migration_status(QTestState *who, const char *goal,
  224. const char **ungoals)
  225. {
  226. bool ready;
  227. char *current_status;
  228. const char **ungoal;
  229. current_status = migrate_query_status(who);
  230. ready = strcmp(current_status, goal) == 0;
  231. if (!ungoals) {
  232. g_assert_cmpstr(current_status, !=, "failed");
  233. /*
  234. * If looking for a state other than completed,
  235. * completion of migration would cause the test to
  236. * hang.
  237. */
  238. if (strcmp(goal, "completed") != 0) {
  239. g_assert_cmpstr(current_status, !=, "completed");
  240. }
  241. } else {
  242. for (ungoal = ungoals; *ungoal; ungoal++) {
  243. g_assert_cmpstr(current_status, !=, *ungoal);
  244. }
  245. }
  246. g_free(current_status);
  247. return ready;
  248. }
  249. static void wait_for_migration_status(QTestState *who,
  250. const char *goal,
  251. const char **ungoals)
  252. {
  253. while (!check_migration_status(who, goal, ungoals)) {
  254. usleep(1000);
  255. }
  256. }
  257. static void wait_for_migration_complete(QTestState *who)
  258. {
  259. wait_for_migration_status(who, "completed", NULL);
  260. }
  261. static void wait_for_migration_pass(QTestState *who)
  262. {
  263. uint64_t initial_pass = get_migration_pass(who);
  264. uint64_t pass;
  265. /* Wait for the 1st sync */
  266. while (!got_stop && !initial_pass) {
  267. usleep(1000);
  268. initial_pass = get_migration_pass(who);
  269. }
  270. do {
  271. usleep(1000);
  272. pass = get_migration_pass(who);
  273. } while (pass == initial_pass && !got_stop);
  274. }
  275. static void check_guests_ram(QTestState *who)
  276. {
  277. /* Our ASM test will have been incrementing one byte from each page from
  278. * start_address to < end_address in order. This gives us a constraint
  279. * that any page's byte should be equal or less than the previous pages
  280. * byte (mod 256); and they should all be equal except for one transition
  281. * at the point where we meet the incrementer. (We're running this with
  282. * the guest stopped).
  283. */
  284. unsigned address;
  285. uint8_t first_byte;
  286. uint8_t last_byte;
  287. bool hit_edge = false;
  288. int bad = 0;
  289. qtest_memread(who, start_address, &first_byte, 1);
  290. last_byte = first_byte;
  291. for (address = start_address + TEST_MEM_PAGE_SIZE; address < end_address;
  292. address += TEST_MEM_PAGE_SIZE)
  293. {
  294. uint8_t b;
  295. qtest_memread(who, address, &b, 1);
  296. if (b != last_byte) {
  297. if (((b + 1) % 256) == last_byte && !hit_edge) {
  298. /* This is OK, the guest stopped at the point of
  299. * incrementing the previous page but didn't get
  300. * to us yet.
  301. */
  302. hit_edge = true;
  303. last_byte = b;
  304. } else {
  305. bad++;
  306. if (bad <= 10) {
  307. fprintf(stderr, "Memory content inconsistency at %x"
  308. " first_byte = %x last_byte = %x current = %x"
  309. " hit_edge = %x\n",
  310. address, first_byte, last_byte, b, hit_edge);
  311. }
  312. }
  313. }
  314. }
  315. if (bad >= 10) {
  316. fprintf(stderr, "and in another %d pages", bad - 10);
  317. }
  318. g_assert(bad == 0);
  319. }
  320. static void cleanup(const char *filename)
  321. {
  322. char *path = g_strdup_printf("%s/%s", tmpfs, filename);
  323. unlink(path);
  324. g_free(path);
  325. }
  326. static char *get_shmem_opts(const char *mem_size, const char *shmem_path)
  327. {
  328. return g_strdup_printf("-object memory-backend-file,id=mem0,size=%s"
  329. ",mem-path=%s,share=on -numa node,memdev=mem0",
  330. mem_size, shmem_path);
  331. }
  332. static char *SocketAddress_to_str(SocketAddress *addr)
  333. {
  334. switch (addr->type) {
  335. case SOCKET_ADDRESS_TYPE_INET:
  336. return g_strdup_printf("tcp:%s:%s",
  337. addr->u.inet.host,
  338. addr->u.inet.port);
  339. case SOCKET_ADDRESS_TYPE_UNIX:
  340. return g_strdup_printf("unix:%s",
  341. addr->u.q_unix.path);
  342. case SOCKET_ADDRESS_TYPE_FD:
  343. return g_strdup_printf("fd:%s", addr->u.fd.str);
  344. case SOCKET_ADDRESS_TYPE_VSOCK:
  345. return g_strdup_printf("tcp:%s:%s",
  346. addr->u.vsock.cid,
  347. addr->u.vsock.port);
  348. default:
  349. return g_strdup("unknown address type");
  350. }
  351. }
  352. static char *migrate_get_socket_address(QTestState *who, const char *parameter)
  353. {
  354. QDict *rsp;
  355. char *result;
  356. Error *local_err = NULL;
  357. SocketAddressList *addrs;
  358. Visitor *iv = NULL;
  359. QObject *object;
  360. rsp = migrate_query(who);
  361. object = qdict_get(rsp, parameter);
  362. iv = qobject_input_visitor_new(object);
  363. visit_type_SocketAddressList(iv, NULL, &addrs, &local_err);
  364. visit_free(iv);
  365. /* we are only using a single address */
  366. result = SocketAddress_to_str(addrs->value);
  367. qapi_free_SocketAddressList(addrs);
  368. qobject_unref(rsp);
  369. return result;
  370. }
  371. static long long migrate_get_parameter_int(QTestState *who,
  372. const char *parameter)
  373. {
  374. QDict *rsp;
  375. long long result;
  376. rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
  377. result = qdict_get_int(rsp, parameter);
  378. qobject_unref(rsp);
  379. return result;
  380. }
  381. static void migrate_check_parameter_int(QTestState *who, const char *parameter,
  382. long long value)
  383. {
  384. long long result;
  385. result = migrate_get_parameter_int(who, parameter);
  386. g_assert_cmpint(result, ==, value);
  387. }
  388. static void migrate_set_parameter_int(QTestState *who, const char *parameter,
  389. long long value)
  390. {
  391. QDict *rsp;
  392. rsp = qtest_qmp(who,
  393. "{ 'execute': 'migrate-set-parameters',"
  394. "'arguments': { %s: %lld } }",
  395. parameter, value);
  396. g_assert(qdict_haskey(rsp, "return"));
  397. qobject_unref(rsp);
  398. migrate_check_parameter_int(who, parameter, value);
  399. }
  400. static void migrate_pause(QTestState *who)
  401. {
  402. QDict *rsp;
  403. rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
  404. qobject_unref(rsp);
  405. }
  406. static void migrate_continue(QTestState *who, const char *state)
  407. {
  408. QDict *rsp;
  409. rsp = wait_command(who,
  410. "{ 'execute': 'migrate-continue',"
  411. " 'arguments': { 'state': %s } }",
  412. state);
  413. qobject_unref(rsp);
  414. }
  415. static void migrate_recover(QTestState *who, const char *uri)
  416. {
  417. QDict *rsp;
  418. rsp = wait_command(who,
  419. "{ 'execute': 'migrate-recover', "
  420. " 'id': 'recover-cmd', "
  421. " 'arguments': { 'uri': %s } }",
  422. uri);
  423. qobject_unref(rsp);
  424. }
  425. static void migrate_set_capability(QTestState *who, const char *capability,
  426. bool value)
  427. {
  428. QDict *rsp;
  429. rsp = qtest_qmp(who,
  430. "{ 'execute': 'migrate-set-capabilities',"
  431. "'arguments': { "
  432. "'capabilities': [ { "
  433. "'capability': %s, 'state': %i } ] } }",
  434. capability, value);
  435. g_assert(qdict_haskey(rsp, "return"));
  436. qobject_unref(rsp);
  437. }
  438. /*
  439. * Send QMP command "migrate".
  440. * Arguments are built from @fmt... (formatted like
  441. * qobject_from_jsonf_nofail()) with "uri": @uri spliced in.
  442. */
  443. GCC_FMT_ATTR(3, 4)
  444. static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
  445. {
  446. va_list ap;
  447. QDict *args, *rsp;
  448. va_start(ap, fmt);
  449. args = qdict_from_vjsonf_nofail(fmt, ap);
  450. va_end(ap);
  451. g_assert(!qdict_haskey(args, "uri"));
  452. qdict_put_str(args, "uri", uri);
  453. rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p}", args);
  454. g_assert(qdict_haskey(rsp, "return"));
  455. qobject_unref(rsp);
  456. }
  457. static void migrate_postcopy_start(QTestState *from, QTestState *to)
  458. {
  459. QDict *rsp;
  460. rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
  461. qobject_unref(rsp);
  462. if (!got_stop) {
  463. qtest_qmp_eventwait(from, "STOP");
  464. }
  465. qtest_qmp_eventwait(to, "RESUME");
  466. }
  467. static int test_migrate_start(QTestState **from, QTestState **to,
  468. const char *uri, bool hide_stderr,
  469. bool use_shmem, const char *opts_src,
  470. const char *opts_dst)
  471. {
  472. gchar *cmd_src, *cmd_dst;
  473. char *bootpath = NULL;
  474. char *extra_opts = NULL;
  475. char *shmem_path = NULL;
  476. const char *arch = qtest_get_arch();
  477. const char *accel = "kvm:tcg";
  478. opts_src = opts_src ? opts_src : "";
  479. opts_dst = opts_dst ? opts_dst : "";
  480. if (use_shmem) {
  481. if (!g_file_test("/dev/shm", G_FILE_TEST_IS_DIR)) {
  482. g_test_skip("/dev/shm is not supported");
  483. return -1;
  484. }
  485. shmem_path = g_strdup_printf("/dev/shm/qemu-%d", getpid());
  486. }
  487. got_stop = false;
  488. bootpath = g_strdup_printf("%s/bootsect", tmpfs);
  489. if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
  490. /* the assembled x86 boot sector should be exactly one sector large */
  491. assert(sizeof(x86_bootsect) == 512);
  492. init_bootfile(bootpath, x86_bootsect, sizeof(x86_bootsect));
  493. extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
  494. cmd_src = g_strdup_printf("-machine accel=%s -m 150M"
  495. " -name source,debug-threads=on"
  496. " -serial file:%s/src_serial"
  497. " -drive file=%s,format=raw %s %s",
  498. accel, tmpfs, bootpath,
  499. extra_opts ? extra_opts : "", opts_src);
  500. cmd_dst = g_strdup_printf("-machine accel=%s -m 150M"
  501. " -name target,debug-threads=on"
  502. " -serial file:%s/dest_serial"
  503. " -drive file=%s,format=raw"
  504. " -incoming %s %s %s",
  505. accel, tmpfs, bootpath, uri,
  506. extra_opts ? extra_opts : "", opts_dst);
  507. start_address = X86_TEST_MEM_START;
  508. end_address = X86_TEST_MEM_END;
  509. } else if (g_str_equal(arch, "s390x")) {
  510. init_bootfile(bootpath, s390x_elf, sizeof(s390x_elf));
  511. extra_opts = use_shmem ? get_shmem_opts("128M", shmem_path) : NULL;
  512. cmd_src = g_strdup_printf("-machine accel=%s -m 128M"
  513. " -name source,debug-threads=on"
  514. " -serial file:%s/src_serial -bios %s %s %s",
  515. accel, tmpfs, bootpath,
  516. extra_opts ? extra_opts : "", opts_src);
  517. cmd_dst = g_strdup_printf("-machine accel=%s -m 128M"
  518. " -name target,debug-threads=on"
  519. " -serial file:%s/dest_serial -bios %s"
  520. " -incoming %s %s %s",
  521. accel, tmpfs, bootpath, uri,
  522. extra_opts ? extra_opts : "", opts_dst);
  523. start_address = S390_TEST_MEM_START;
  524. end_address = S390_TEST_MEM_END;
  525. } else if (strcmp(arch, "ppc64") == 0) {
  526. extra_opts = use_shmem ? get_shmem_opts("256M", shmem_path) : NULL;
  527. cmd_src = g_strdup_printf("-machine accel=%s,vsmt=8 -m 256M -nodefaults"
  528. " -name source,debug-threads=on"
  529. " -serial file:%s/src_serial"
  530. " -prom-env 'use-nvramrc?=true' -prom-env "
  531. "'nvramrc=hex .\" _\" begin %x %x "
  532. "do i c@ 1 + i c! 1000 +loop .\" B\" 0 "
  533. "until' %s %s", accel, tmpfs, end_address,
  534. start_address, extra_opts ? extra_opts : "",
  535. opts_src);
  536. cmd_dst = g_strdup_printf("-machine accel=%s,vsmt=8 -m 256M"
  537. " -name target,debug-threads=on"
  538. " -serial file:%s/dest_serial"
  539. " -incoming %s %s %s",
  540. accel, tmpfs, uri,
  541. extra_opts ? extra_opts : "", opts_dst);
  542. start_address = PPC_TEST_MEM_START;
  543. end_address = PPC_TEST_MEM_END;
  544. } else if (strcmp(arch, "aarch64") == 0) {
  545. init_bootfile(bootpath, aarch64_kernel, sizeof(aarch64_kernel));
  546. extra_opts = use_shmem ? get_shmem_opts("150M", shmem_path) : NULL;
  547. cmd_src = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
  548. "-name vmsource,debug-threads=on -cpu max "
  549. "-m 150M -serial file:%s/src_serial "
  550. "-kernel %s %s %s",
  551. accel, tmpfs, bootpath,
  552. extra_opts ? extra_opts : "", opts_src);
  553. cmd_dst = g_strdup_printf("-machine virt,accel=%s,gic-version=max "
  554. "-name vmdest,debug-threads=on -cpu max "
  555. "-m 150M -serial file:%s/dest_serial "
  556. "-kernel %s "
  557. "-incoming %s %s %s",
  558. accel, tmpfs, bootpath, uri,
  559. extra_opts ? extra_opts : "", opts_dst);
  560. start_address = ARM_TEST_MEM_START;
  561. end_address = ARM_TEST_MEM_END;
  562. g_assert(sizeof(aarch64_kernel) <= ARM_TEST_MAX_KERNEL_SIZE);
  563. } else {
  564. g_assert_not_reached();
  565. }
  566. g_free(bootpath);
  567. g_free(extra_opts);
  568. if (hide_stderr) {
  569. gchar *tmp;
  570. tmp = g_strdup_printf("%s 2>/dev/null", cmd_src);
  571. g_free(cmd_src);
  572. cmd_src = tmp;
  573. tmp = g_strdup_printf("%s 2>/dev/null", cmd_dst);
  574. g_free(cmd_dst);
  575. cmd_dst = tmp;
  576. }
  577. *from = qtest_init(cmd_src);
  578. g_free(cmd_src);
  579. *to = qtest_init(cmd_dst);
  580. g_free(cmd_dst);
  581. /*
  582. * Remove shmem file immediately to avoid memory leak in test failed case.
  583. * It's valid becase QEMU has already opened this file
  584. */
  585. if (use_shmem) {
  586. unlink(shmem_path);
  587. g_free(shmem_path);
  588. }
  589. return 0;
  590. }
  591. static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
  592. {
  593. unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
  594. qtest_quit(from);
  595. if (test_dest) {
  596. qtest_memread(to, start_address, &dest_byte_a, 1);
  597. /* Destination still running, wait for a byte to change */
  598. do {
  599. qtest_memread(to, start_address, &dest_byte_b, 1);
  600. usleep(1000 * 10);
  601. } while (dest_byte_a == dest_byte_b);
  602. qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
  603. /* With it stopped, check nothing changes */
  604. qtest_memread(to, start_address, &dest_byte_c, 1);
  605. usleep(1000 * 200);
  606. qtest_memread(to, start_address, &dest_byte_d, 1);
  607. g_assert_cmpint(dest_byte_c, ==, dest_byte_d);
  608. check_guests_ram(to);
  609. }
  610. qtest_quit(to);
  611. cleanup("bootsect");
  612. cleanup("migsocket");
  613. cleanup("src_serial");
  614. cleanup("dest_serial");
  615. }
  616. static void deprecated_set_downtime(QTestState *who, const double value)
  617. {
  618. QDict *rsp;
  619. rsp = qtest_qmp(who,
  620. "{ 'execute': 'migrate_set_downtime',"
  621. " 'arguments': { 'value': %f } }", value);
  622. g_assert(qdict_haskey(rsp, "return"));
  623. qobject_unref(rsp);
  624. migrate_check_parameter_int(who, "downtime-limit", value * 1000);
  625. }
  626. static void deprecated_set_speed(QTestState *who, long long value)
  627. {
  628. QDict *rsp;
  629. rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
  630. "'arguments': { 'value': %lld } }", value);
  631. g_assert(qdict_haskey(rsp, "return"));
  632. qobject_unref(rsp);
  633. migrate_check_parameter_int(who, "max-bandwidth", value);
  634. }
  635. static void deprecated_set_cache_size(QTestState *who, long long value)
  636. {
  637. QDict *rsp;
  638. rsp = qtest_qmp(who, "{ 'execute': 'migrate-set-cache-size',"
  639. "'arguments': { 'value': %lld } }", value);
  640. g_assert(qdict_haskey(rsp, "return"));
  641. qobject_unref(rsp);
  642. migrate_check_parameter_int(who, "xbzrle-cache-size", value);
  643. }
  644. static void test_deprecated(void)
  645. {
  646. QTestState *from;
  647. from = qtest_init("-machine none");
  648. deprecated_set_downtime(from, 0.12345);
  649. deprecated_set_speed(from, 12345);
  650. deprecated_set_cache_size(from, 4096);
  651. qtest_quit(from);
  652. }
  653. static int migrate_postcopy_prepare(QTestState **from_ptr,
  654. QTestState **to_ptr,
  655. bool hide_error)
  656. {
  657. char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
  658. QTestState *from, *to;
  659. if (test_migrate_start(&from, &to, uri, hide_error, false, NULL, NULL)) {
  660. return -1;
  661. }
  662. migrate_set_capability(from, "postcopy-ram", true);
  663. migrate_set_capability(to, "postcopy-ram", true);
  664. migrate_set_capability(to, "postcopy-blocktime", true);
  665. /* We want to pick a speed slow enough that the test completes
  666. * quickly, but that it doesn't complete precopy even on a slow
  667. * machine, so also set the downtime.
  668. */
  669. migrate_set_parameter_int(from, "max-bandwidth", 30000000);
  670. migrate_set_parameter_int(from, "downtime-limit", 1);
  671. /* Wait for the first serial output from the source */
  672. wait_for_serial("src_serial");
  673. migrate(from, uri, "{}");
  674. g_free(uri);
  675. wait_for_migration_pass(from);
  676. *from_ptr = from;
  677. *to_ptr = to;
  678. return 0;
  679. }
  680. static void migrate_postcopy_complete(QTestState *from, QTestState *to)
  681. {
  682. wait_for_migration_complete(from);
  683. /* Make sure we get at least one "B" on destination */
  684. wait_for_serial("dest_serial");
  685. if (uffd_feature_thread_id) {
  686. read_blocktime(to);
  687. }
  688. test_migrate_end(from, to, true);
  689. }
  690. static void test_postcopy(void)
  691. {
  692. QTestState *from, *to;
  693. if (migrate_postcopy_prepare(&from, &to, false)) {
  694. return;
  695. }
  696. migrate_postcopy_start(from, to);
  697. migrate_postcopy_complete(from, to);
  698. }
  699. static void test_postcopy_recovery(void)
  700. {
  701. QTestState *from, *to;
  702. char *uri;
  703. if (migrate_postcopy_prepare(&from, &to, true)) {
  704. return;
  705. }
  706. /* Turn postcopy speed down, 4K/s is slow enough on any machines */
  707. migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
  708. /* Now we start the postcopy */
  709. migrate_postcopy_start(from, to);
  710. /*
  711. * Wait until postcopy is really started; we can only run the
  712. * migrate-pause command during a postcopy
  713. */
  714. wait_for_migration_status(from, "postcopy-active", NULL);
  715. /*
  716. * Manually stop the postcopy migration. This emulates a network
  717. * failure with the migration socket
  718. */
  719. migrate_pause(from);
  720. /*
  721. * Wait for destination side to reach postcopy-paused state. The
  722. * migrate-recover command can only succeed if destination machine
  723. * is in the paused state
  724. */
  725. wait_for_migration_status(to, "postcopy-paused",
  726. (const char * []) { "failed", "active",
  727. "completed", NULL });
  728. /*
  729. * Create a new socket to emulate a new channel that is different
  730. * from the broken migration channel; tell the destination to
  731. * listen to the new port
  732. */
  733. uri = g_strdup_printf("unix:%s/migsocket-recover", tmpfs);
  734. migrate_recover(to, uri);
  735. /*
  736. * Try to rebuild the migration channel using the resume flag and
  737. * the newly created channel
  738. */
  739. wait_for_migration_status(from, "postcopy-paused",
  740. (const char * []) { "failed", "active",
  741. "completed", NULL });
  742. migrate(from, uri, "{'resume': true}");
  743. g_free(uri);
  744. /* Restore the postcopy bandwidth to unlimited */
  745. migrate_set_parameter_int(from, "max-postcopy-bandwidth", 0);
  746. migrate_postcopy_complete(from, to);
  747. }
  748. static void wait_for_migration_fail(QTestState *from, bool allow_active)
  749. {
  750. QDict *rsp_return;
  751. char *status;
  752. bool failed;
  753. do {
  754. status = migrate_query_status(from);
  755. bool result = !strcmp(status, "setup") || !strcmp(status, "failed") ||
  756. (allow_active && !strcmp(status, "active"));
  757. if (!result) {
  758. fprintf(stderr, "%s: unexpected status status=%s allow_active=%d\n",
  759. __func__, status, allow_active);
  760. }
  761. g_assert(result);
  762. failed = !strcmp(status, "failed");
  763. g_free(status);
  764. } while (!failed);
  765. /* Is the machine currently running? */
  766. rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
  767. g_assert(qdict_haskey(rsp_return, "running"));
  768. g_assert(qdict_get_bool(rsp_return, "running"));
  769. qobject_unref(rsp_return);
  770. }
  771. static void test_baddest(void)
  772. {
  773. QTestState *from, *to;
  774. if (test_migrate_start(&from, &to, "tcp:0:0", true, false, NULL, NULL)) {
  775. return;
  776. }
  777. migrate(from, "tcp:0:0", "{}");
  778. wait_for_migration_fail(from, false);
  779. test_migrate_end(from, to, false);
  780. }
  781. static void test_precopy_unix(void)
  782. {
  783. char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
  784. QTestState *from, *to;
  785. if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
  786. return;
  787. }
  788. /* We want to pick a speed slow enough that the test completes
  789. * quickly, but that it doesn't complete precopy even on a slow
  790. * machine, so also set the downtime.
  791. */
  792. /* 1 ms should make it not converge*/
  793. migrate_set_parameter_int(from, "downtime-limit", 1);
  794. /* 1GB/s */
  795. migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
  796. /* Wait for the first serial output from the source */
  797. wait_for_serial("src_serial");
  798. migrate(from, uri, "{}");
  799. wait_for_migration_pass(from);
  800. /* 300 ms should converge */
  801. migrate_set_parameter_int(from, "downtime-limit", 300);
  802. if (!got_stop) {
  803. qtest_qmp_eventwait(from, "STOP");
  804. }
  805. qtest_qmp_eventwait(to, "RESUME");
  806. wait_for_serial("dest_serial");
  807. wait_for_migration_complete(from);
  808. test_migrate_end(from, to, true);
  809. g_free(uri);
  810. }
  811. #if 0
  812. /* Currently upset on aarch64 TCG */
  813. static void test_ignore_shared(void)
  814. {
  815. char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
  816. QTestState *from, *to;
  817. if (test_migrate_start(&from, &to, uri, false, true, NULL, NULL)) {
  818. return;
  819. }
  820. migrate_set_capability(from, "x-ignore-shared", true);
  821. migrate_set_capability(to, "x-ignore-shared", true);
  822. /* Wait for the first serial output from the source */
  823. wait_for_serial("src_serial");
  824. migrate(from, uri, "{}");
  825. wait_for_migration_pass(from);
  826. if (!got_stop) {
  827. qtest_qmp_eventwait(from, "STOP");
  828. }
  829. qtest_qmp_eventwait(to, "RESUME");
  830. wait_for_serial("dest_serial");
  831. wait_for_migration_complete(from);
  832. /* Check whether shared RAM has been really skipped */
  833. g_assert_cmpint(read_ram_property_int(from, "transferred"), <, 1024 * 1024);
  834. test_migrate_end(from, to, true);
  835. g_free(uri);
  836. }
  837. #endif
  838. static void test_xbzrle(const char *uri)
  839. {
  840. QTestState *from, *to;
  841. if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
  842. return;
  843. }
  844. /*
  845. * We want to pick a speed slow enough that the test completes
  846. * quickly, but that it doesn't complete precopy even on a slow
  847. * machine, so also set the downtime.
  848. */
  849. /* 1 ms should make it not converge*/
  850. migrate_set_parameter_int(from, "downtime-limit", 1);
  851. /* 1GB/s */
  852. migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
  853. migrate_set_parameter_int(from, "xbzrle-cache-size", 33554432);
  854. migrate_set_capability(from, "xbzrle", "true");
  855. migrate_set_capability(to, "xbzrle", "true");
  856. /* Wait for the first serial output from the source */
  857. wait_for_serial("src_serial");
  858. migrate(from, uri, "{}");
  859. wait_for_migration_pass(from);
  860. /* 300ms should converge */
  861. migrate_set_parameter_int(from, "downtime-limit", 300);
  862. if (!got_stop) {
  863. qtest_qmp_eventwait(from, "STOP");
  864. }
  865. qtest_qmp_eventwait(to, "RESUME");
  866. wait_for_serial("dest_serial");
  867. wait_for_migration_complete(from);
  868. test_migrate_end(from, to, true);
  869. }
  870. static void test_xbzrle_unix(void)
  871. {
  872. char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
  873. test_xbzrle(uri);
  874. g_free(uri);
  875. }
  876. static void test_precopy_tcp(void)
  877. {
  878. char *uri;
  879. QTestState *from, *to;
  880. if (test_migrate_start(&from, &to, "tcp:127.0.0.1:0", false, false,
  881. NULL, NULL)) {
  882. return;
  883. }
  884. /*
  885. * We want to pick a speed slow enough that the test completes
  886. * quickly, but that it doesn't complete precopy even on a slow
  887. * machine, so also set the downtime.
  888. */
  889. /* 1 ms should make it not converge*/
  890. migrate_set_parameter_int(from, "downtime-limit", 1);
  891. /* 1GB/s */
  892. migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
  893. /* Wait for the first serial output from the source */
  894. wait_for_serial("src_serial");
  895. uri = migrate_get_socket_address(to, "socket-address");
  896. migrate(from, uri, "{}");
  897. wait_for_migration_pass(from);
  898. /* 300ms should converge */
  899. migrate_set_parameter_int(from, "downtime-limit", 300);
  900. if (!got_stop) {
  901. qtest_qmp_eventwait(from, "STOP");
  902. }
  903. qtest_qmp_eventwait(to, "RESUME");
  904. wait_for_serial("dest_serial");
  905. wait_for_migration_complete(from);
  906. test_migrate_end(from, to, true);
  907. g_free(uri);
  908. }
  909. static void test_migrate_fd_proto(void)
  910. {
  911. QTestState *from, *to;
  912. int ret;
  913. int pair[2];
  914. QDict *rsp;
  915. const char *error_desc;
  916. if (test_migrate_start(&from, &to, "defer", false, false, NULL, NULL)) {
  917. return;
  918. }
  919. /*
  920. * We want to pick a speed slow enough that the test completes
  921. * quickly, but that it doesn't complete precopy even on a slow
  922. * machine, so also set the downtime.
  923. */
  924. /* 1 ms should make it not converge */
  925. migrate_set_parameter_int(from, "downtime-limit", 1);
  926. /* 1GB/s */
  927. migrate_set_parameter_int(from, "max-bandwidth", 1000000000);
  928. /* Wait for the first serial output from the source */
  929. wait_for_serial("src_serial");
  930. /* Create two connected sockets for migration */
  931. ret = socketpair(PF_LOCAL, SOCK_STREAM, 0, pair);
  932. g_assert_cmpint(ret, ==, 0);
  933. /* Send the 1st socket to the target */
  934. rsp = wait_command_fd(to, pair[0],
  935. "{ 'execute': 'getfd',"
  936. " 'arguments': { 'fdname': 'fd-mig' }}");
  937. qobject_unref(rsp);
  938. close(pair[0]);
  939. /* Start incoming migration from the 1st socket */
  940. rsp = wait_command(to, "{ 'execute': 'migrate-incoming',"
  941. " 'arguments': { 'uri': 'fd:fd-mig' }}");
  942. qobject_unref(rsp);
  943. /* Send the 2nd socket to the target */
  944. rsp = wait_command_fd(from, pair[1],
  945. "{ 'execute': 'getfd',"
  946. " 'arguments': { 'fdname': 'fd-mig' }}");
  947. qobject_unref(rsp);
  948. close(pair[1]);
  949. /* Start migration to the 2nd socket*/
  950. migrate(from, "fd:fd-mig", "{}");
  951. wait_for_migration_pass(from);
  952. /* 300ms should converge */
  953. migrate_set_parameter_int(from, "downtime-limit", 300);
  954. if (!got_stop) {
  955. qtest_qmp_eventwait(from, "STOP");
  956. }
  957. qtest_qmp_eventwait(to, "RESUME");
  958. /* Test closing fds */
  959. /* We assume, that QEMU removes named fd from its list,
  960. * so this should fail */
  961. rsp = qtest_qmp(from, "{ 'execute': 'closefd',"
  962. " 'arguments': { 'fdname': 'fd-mig' }}");
  963. g_assert_true(qdict_haskey(rsp, "error"));
  964. error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
  965. g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
  966. qobject_unref(rsp);
  967. rsp = qtest_qmp(to, "{ 'execute': 'closefd',"
  968. " 'arguments': { 'fdname': 'fd-mig' }}");
  969. g_assert_true(qdict_haskey(rsp, "error"));
  970. error_desc = qdict_get_str(qdict_get_qdict(rsp, "error"), "desc");
  971. g_assert_cmpstr(error_desc, ==, "File descriptor named 'fd-mig' not found");
  972. qobject_unref(rsp);
  973. /* Complete migration */
  974. wait_for_serial("dest_serial");
  975. wait_for_migration_complete(from);
  976. test_migrate_end(from, to, true);
  977. }
  978. static void do_test_validate_uuid(const char *uuid_arg_src,
  979. const char *uuid_arg_dst,
  980. bool should_fail, bool hide_stderr)
  981. {
  982. char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
  983. QTestState *from, *to;
  984. if (test_migrate_start(&from, &to, uri, hide_stderr, false,
  985. uuid_arg_src, uuid_arg_dst)) {
  986. return;
  987. }
  988. /*
  989. * UUID validation is at the begin of migration. So, the main process of
  990. * migration is not interesting for us here. Thus, set huge downtime for
  991. * very fast migration.
  992. */
  993. migrate_set_parameter_int(from, "downtime-limit", 1000000);
  994. migrate_set_capability(from, "validate-uuid", true);
  995. /* Wait for the first serial output from the source */
  996. wait_for_serial("src_serial");
  997. migrate(from, uri, "{}");
  998. if (should_fail) {
  999. qtest_set_expected_status(to, 1);
  1000. wait_for_migration_fail(from, true);
  1001. } else {
  1002. wait_for_migration_complete(from);
  1003. }
  1004. test_migrate_end(from, to, false);
  1005. g_free(uri);
  1006. }
  1007. static void test_validate_uuid(void)
  1008. {
  1009. do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
  1010. "-uuid 11111111-1111-1111-1111-111111111111",
  1011. false, false);
  1012. }
  1013. static void test_validate_uuid_error(void)
  1014. {
  1015. do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111",
  1016. "-uuid 22222222-2222-2222-2222-222222222222",
  1017. true, true);
  1018. }
  1019. static void test_validate_uuid_src_not_set(void)
  1020. {
  1021. do_test_validate_uuid(NULL, "-uuid 11111111-1111-1111-1111-111111111111",
  1022. false, true);
  1023. }
  1024. static void test_validate_uuid_dst_not_set(void)
  1025. {
  1026. do_test_validate_uuid("-uuid 11111111-1111-1111-1111-111111111111", NULL,
  1027. false, true);
  1028. }
  1029. static void test_migrate_auto_converge(void)
  1030. {
  1031. char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
  1032. QTestState *from, *to;
  1033. int64_t remaining, percentage;
  1034. /*
  1035. * We want the test to be stable and as fast as possible.
  1036. * E.g., with 1Gb/s bandwith migration may pass without throttling,
  1037. * so we need to decrease a bandwidth.
  1038. */
  1039. const int64_t init_pct = 5, inc_pct = 50, max_pct = 95;
  1040. const int64_t max_bandwidth = 400000000; /* ~400Mb/s */
  1041. const int64_t downtime_limit = 250; /* 250ms */
  1042. /*
  1043. * We migrate through unix-socket (> 500Mb/s).
  1044. * Thus, expected migration speed ~= bandwidth limit (< 500Mb/s).
  1045. * So, we can predict expected_threshold
  1046. */
  1047. const int64_t expected_threshold = max_bandwidth * downtime_limit / 1000;
  1048. if (test_migrate_start(&from, &to, uri, false, false, NULL, NULL)) {
  1049. return;
  1050. }
  1051. migrate_set_capability(from, "auto-converge", true);
  1052. migrate_set_parameter_int(from, "cpu-throttle-initial", init_pct);
  1053. migrate_set_parameter_int(from, "cpu-throttle-increment", inc_pct);
  1054. migrate_set_parameter_int(from, "max-cpu-throttle", max_pct);
  1055. /*
  1056. * Set the initial parameters so that the migration could not converge
  1057. * without throttling.
  1058. */
  1059. migrate_set_parameter_int(from, "downtime-limit", 1);
  1060. migrate_set_parameter_int(from, "max-bandwidth", 100000000); /* ~100Mb/s */
  1061. /* To check remaining size after precopy */
  1062. migrate_set_capability(from, "pause-before-switchover", true);
  1063. /* Wait for the first serial output from the source */
  1064. wait_for_serial("src_serial");
  1065. migrate(from, uri, "{}");
  1066. /* Wait for throttling begins */
  1067. percentage = 0;
  1068. while (percentage == 0) {
  1069. percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
  1070. usleep(100);
  1071. g_assert_false(got_stop);
  1072. }
  1073. /* The first percentage of throttling should be equal to init_pct */
  1074. g_assert_cmpint(percentage, ==, init_pct);
  1075. /* Now, when we tested that throttling works, let it converge */
  1076. migrate_set_parameter_int(from, "downtime-limit", downtime_limit);
  1077. migrate_set_parameter_int(from, "max-bandwidth", max_bandwidth);
  1078. /*
  1079. * Wait for pre-switchover status to check last throttle percentage
  1080. * and remaining. These values will be zeroed later
  1081. */
  1082. wait_for_migration_status(from, "pre-switchover", NULL);
  1083. /* The final percentage of throttling shouldn't be greater than max_pct */
  1084. percentage = read_migrate_property_int(from, "cpu-throttle-percentage");
  1085. g_assert_cmpint(percentage, <=, max_pct);
  1086. remaining = read_ram_property_int(from, "remaining");
  1087. g_assert_cmpint(remaining, <, expected_threshold);
  1088. migrate_continue(from, "pre-switchover");
  1089. qtest_qmp_eventwait(to, "RESUME");
  1090. wait_for_serial("dest_serial");
  1091. wait_for_migration_complete(from);
  1092. g_free(uri);
  1093. test_migrate_end(from, to, true);
  1094. }
  1095. int main(int argc, char **argv)
  1096. {
  1097. char template[] = "/tmp/migration-test-XXXXXX";
  1098. int ret;
  1099. g_test_init(&argc, &argv, NULL);
  1100. if (!ufd_version_check()) {
  1101. return g_test_run();
  1102. }
  1103. /*
  1104. * On ppc64, the test only works with kvm-hv, but not with kvm-pr and TCG
  1105. * is touchy due to race conditions on dirty bits (especially on PPC for
  1106. * some reason)
  1107. */
  1108. if (g_str_equal(qtest_get_arch(), "ppc64") &&
  1109. access("/sys/module/kvm_hv", F_OK)) {
  1110. g_test_message("Skipping test: kvm_hv not available");
  1111. return g_test_run();
  1112. }
  1113. /*
  1114. * Similar to ppc64, s390x seems to be touchy with TCG, so disable it
  1115. * there until the problems are resolved
  1116. */
  1117. if (g_str_equal(qtest_get_arch(), "s390x")) {
  1118. #if defined(HOST_S390X)
  1119. if (access("/dev/kvm", R_OK | W_OK)) {
  1120. g_test_message("Skipping test: kvm not available");
  1121. return g_test_run();
  1122. }
  1123. #else
  1124. g_test_message("Skipping test: Need s390x host to work properly");
  1125. return g_test_run();
  1126. #endif
  1127. }
  1128. tmpfs = mkdtemp(template);
  1129. if (!tmpfs) {
  1130. g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
  1131. }
  1132. g_assert(tmpfs);
  1133. module_call_init(MODULE_INIT_QOM);
  1134. qtest_add_func("/migration/postcopy/unix", test_postcopy);
  1135. qtest_add_func("/migration/postcopy/recovery", test_postcopy_recovery);
  1136. qtest_add_func("/migration/deprecated", test_deprecated);
  1137. qtest_add_func("/migration/bad_dest", test_baddest);
  1138. qtest_add_func("/migration/precopy/unix", test_precopy_unix);
  1139. qtest_add_func("/migration/precopy/tcp", test_precopy_tcp);
  1140. /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
  1141. qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
  1142. qtest_add_func("/migration/fd_proto", test_migrate_fd_proto);
  1143. qtest_add_func("/migration/validate_uuid", test_validate_uuid);
  1144. qtest_add_func("/migration/validate_uuid_error", test_validate_uuid_error);
  1145. qtest_add_func("/migration/validate_uuid_src_not_set",
  1146. test_validate_uuid_src_not_set);
  1147. qtest_add_func("/migration/validate_uuid_dst_not_set",
  1148. test_validate_uuid_dst_not_set);
  1149. qtest_add_func("/migration/auto_converge", test_migrate_auto_converge);
  1150. ret = g_test_run();
  1151. g_assert_cmpint(ret, ==, 0);
  1152. ret = rmdir(tmpfs);
  1153. if (ret != 0) {
  1154. g_test_message("unable to rmdir: path (%s): %s",
  1155. tmpfs, strerror(errno));
  1156. }
  1157. return ret;
  1158. }