ppc: Deassert the external interrupt pin in KVM on reset
[qemu/ar7.git] / tests / test-replication.c
blob4747d8a6beab7b3e2d4b9011c7e6d9c3372a77fd
1 /*
2 * Block replication tests
4 * Copyright (c) 2016 FUJITSU LIMITED
5 * Author: Changlong Xie <xiecl.fnst@cn.fujitsu.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or
8 * later. See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qapi/qmp/qdict.h"
15 #include "qemu/option.h"
16 #include "qemu/main-loop.h"
17 #include "replication.h"
18 #include "block/block_int.h"
19 #include "block/qdict.h"
20 #include "sysemu/block-backend.h"
22 #define IMG_SIZE (64 * 1024 * 1024)
24 /* primary */
25 #define P_ID "primary-id"
26 static char p_local_disk[] = "/tmp/p_local_disk.XXXXXX";
28 /* secondary */
29 #define S_ID "secondary-id"
30 #define S_LOCAL_DISK_ID "secondary-local-disk-id"
31 static char s_local_disk[] = "/tmp/s_local_disk.XXXXXX";
32 static char s_active_disk[] = "/tmp/s_active_disk.XXXXXX";
33 static char s_hidden_disk[] = "/tmp/s_hidden_disk.XXXXXX";
35 /* FIXME: steal from blockdev.c */
36 QemuOptsList qemu_drive_opts = {
37 .name = "drive",
38 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
39 .desc = {
40 { /* end of list */ }
44 #define NOT_DONE 0x7fffffff
46 static void blk_rw_done(void *opaque, int ret)
48 *(int *)opaque = ret;
51 static void test_blk_read(BlockBackend *blk, long pattern,
52 int64_t pattern_offset, int64_t pattern_count,
53 int64_t offset, int64_t count,
54 bool expect_failed)
56 void *pattern_buf = NULL;
57 QEMUIOVector qiov;
58 void *cmp_buf = NULL;
59 int async_ret = NOT_DONE;
61 if (pattern) {
62 cmp_buf = g_malloc(pattern_count);
63 memset(cmp_buf, pattern, pattern_count);
66 pattern_buf = g_malloc(count);
67 if (pattern) {
68 memset(pattern_buf, pattern, count);
69 } else {
70 memset(pattern_buf, 0x00, count);
73 qemu_iovec_init(&qiov, 1);
74 qemu_iovec_add(&qiov, pattern_buf, count);
76 blk_aio_preadv(blk, offset, &qiov, 0, blk_rw_done, &async_ret);
77 while (async_ret == NOT_DONE) {
78 main_loop_wait(false);
81 if (expect_failed) {
82 g_assert(async_ret != 0);
83 } else {
84 g_assert(async_ret == 0);
85 if (pattern) {
86 g_assert(memcmp(pattern_buf + pattern_offset,
87 cmp_buf, pattern_count) <= 0);
91 g_free(pattern_buf);
92 g_free(cmp_buf);
93 qemu_iovec_destroy(&qiov);
96 static void test_blk_write(BlockBackend *blk, long pattern, int64_t offset,
97 int64_t count, bool expect_failed)
99 void *pattern_buf = NULL;
100 QEMUIOVector qiov;
101 int async_ret = NOT_DONE;
103 pattern_buf = g_malloc(count);
104 if (pattern) {
105 memset(pattern_buf, pattern, count);
106 } else {
107 memset(pattern_buf, 0x00, count);
110 qemu_iovec_init(&qiov, 1);
111 qemu_iovec_add(&qiov, pattern_buf, count);
113 blk_aio_pwritev(blk, offset, &qiov, 0, blk_rw_done, &async_ret);
114 while (async_ret == NOT_DONE) {
115 main_loop_wait(false);
118 if (expect_failed) {
119 g_assert(async_ret != 0);
120 } else {
121 g_assert(async_ret == 0);
124 g_free(pattern_buf);
125 qemu_iovec_destroy(&qiov);
129 * Create a uniquely-named empty temporary file.
131 static void make_temp(char *template)
133 int fd;
135 fd = mkstemp(template);
136 g_assert(fd >= 0);
137 close(fd);
140 static void prepare_imgs(void)
142 Error *local_err = NULL;
144 make_temp(p_local_disk);
145 make_temp(s_local_disk);
146 make_temp(s_active_disk);
147 make_temp(s_hidden_disk);
149 /* Primary */
150 bdrv_img_create(p_local_disk, "qcow2", NULL, NULL, NULL, IMG_SIZE,
151 BDRV_O_RDWR, true, &local_err);
152 g_assert(!local_err);
154 /* Secondary */
155 bdrv_img_create(s_local_disk, "qcow2", NULL, NULL, NULL, IMG_SIZE,
156 BDRV_O_RDWR, true, &local_err);
157 g_assert(!local_err);
158 bdrv_img_create(s_active_disk, "qcow2", NULL, NULL, NULL, IMG_SIZE,
159 BDRV_O_RDWR, true, &local_err);
160 g_assert(!local_err);
161 bdrv_img_create(s_hidden_disk, "qcow2", NULL, NULL, NULL, IMG_SIZE,
162 BDRV_O_RDWR, true, &local_err);
163 g_assert(!local_err);
166 static void cleanup_imgs(void)
168 /* Primary */
169 unlink(p_local_disk);
171 /* Secondary */
172 unlink(s_local_disk);
173 unlink(s_active_disk);
174 unlink(s_hidden_disk);
177 static BlockBackend *start_primary(void)
179 BlockBackend *blk;
180 QemuOpts *opts;
181 QDict *qdict;
182 Error *local_err = NULL;
183 char *cmdline;
185 cmdline = g_strdup_printf("driver=replication,mode=primary,node-name=xxx,"
186 "file.driver=qcow2,file.file.filename=%s,"
187 "file.file.locking=off"
188 , p_local_disk);
189 opts = qemu_opts_parse_noisily(&qemu_drive_opts, cmdline, false);
190 g_free(cmdline);
192 qdict = qemu_opts_to_qdict(opts, NULL);
193 qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
194 qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
196 blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &local_err);
197 g_assert(blk);
198 g_assert(!local_err);
200 monitor_add_blk(blk, P_ID, &local_err);
201 g_assert(!local_err);
203 qemu_opts_del(opts);
205 return blk;
208 static void teardown_primary(void)
210 BlockBackend *blk;
211 AioContext *ctx;
213 /* remove P_ID */
214 blk = blk_by_name(P_ID);
215 assert(blk);
217 ctx = blk_get_aio_context(blk);
218 aio_context_acquire(ctx);
219 monitor_remove_blk(blk);
220 blk_unref(blk);
221 aio_context_release(ctx);
224 static void test_primary_read(void)
226 BlockBackend *blk;
228 blk = start_primary();
230 /* read from 0 to IMG_SIZE */
231 test_blk_read(blk, 0, 0, IMG_SIZE, 0, IMG_SIZE, true);
233 teardown_primary();
236 static void test_primary_write(void)
238 BlockBackend *blk;
240 blk = start_primary();
242 /* write from 0 to IMG_SIZE */
243 test_blk_write(blk, 0, 0, IMG_SIZE, true);
245 teardown_primary();
248 static void test_primary_start(void)
250 BlockBackend *blk = NULL;
251 Error *local_err = NULL;
253 blk = start_primary();
255 replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
256 g_assert(!local_err);
258 /* read from 0 to IMG_SIZE */
259 test_blk_read(blk, 0, 0, IMG_SIZE, 0, IMG_SIZE, true);
261 /* write 0x22 from 0 to IMG_SIZE */
262 test_blk_write(blk, 0x22, 0, IMG_SIZE, false);
264 teardown_primary();
267 static void test_primary_stop(void)
269 Error *local_err = NULL;
270 bool failover = true;
272 start_primary();
274 replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
275 g_assert(!local_err);
277 replication_stop_all(failover, &local_err);
278 g_assert(!local_err);
280 teardown_primary();
283 static void test_primary_do_checkpoint(void)
285 Error *local_err = NULL;
287 start_primary();
289 replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
290 g_assert(!local_err);
292 replication_do_checkpoint_all(&local_err);
293 g_assert(!local_err);
295 teardown_primary();
298 static void test_primary_get_error_all(void)
300 Error *local_err = NULL;
302 start_primary();
304 replication_start_all(REPLICATION_MODE_PRIMARY, &local_err);
305 g_assert(!local_err);
307 replication_get_error_all(&local_err);
308 g_assert(!local_err);
310 teardown_primary();
313 static BlockBackend *start_secondary(void)
315 QemuOpts *opts;
316 QDict *qdict;
317 BlockBackend *blk;
318 char *cmdline;
319 Error *local_err = NULL;
321 /* add s_local_disk and forge S_LOCAL_DISK_ID */
322 cmdline = g_strdup_printf("file.filename=%s,driver=qcow2,"
323 "file.locking=off",
324 s_local_disk);
325 opts = qemu_opts_parse_noisily(&qemu_drive_opts, cmdline, false);
326 g_free(cmdline);
328 qdict = qemu_opts_to_qdict(opts, NULL);
329 qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
330 qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
332 blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &local_err);
333 assert(blk);
334 monitor_add_blk(blk, S_LOCAL_DISK_ID, &local_err);
335 g_assert(!local_err);
337 /* format s_local_disk with pattern "0x11" */
338 test_blk_write(blk, 0x11, 0, IMG_SIZE, false);
340 qemu_opts_del(opts);
342 /* add S_(ACTIVE/HIDDEN)_DISK and forge S_ID */
343 cmdline = g_strdup_printf("driver=replication,mode=secondary,top-id=%s,"
344 "file.driver=qcow2,file.file.filename=%s,"
345 "file.file.locking=off,"
346 "file.backing.driver=qcow2,"
347 "file.backing.file.filename=%s,"
348 "file.backing.file.locking=off,"
349 "file.backing.backing=%s"
350 , S_ID, s_active_disk, s_hidden_disk
351 , S_LOCAL_DISK_ID);
352 opts = qemu_opts_parse_noisily(&qemu_drive_opts, cmdline, false);
353 g_free(cmdline);
355 qdict = qemu_opts_to_qdict(opts, NULL);
356 qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
357 qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
359 blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &local_err);
360 assert(blk);
361 monitor_add_blk(blk, S_ID, &local_err);
362 g_assert(!local_err);
364 qemu_opts_del(opts);
366 return blk;
369 static void teardown_secondary(void)
371 /* only need to destroy two BBs */
372 BlockBackend *blk;
373 AioContext *ctx;
375 /* remove S_LOCAL_DISK_ID */
376 blk = blk_by_name(S_LOCAL_DISK_ID);
377 assert(blk);
379 ctx = blk_get_aio_context(blk);
380 aio_context_acquire(ctx);
381 monitor_remove_blk(blk);
382 blk_unref(blk);
383 aio_context_release(ctx);
385 /* remove S_ID */
386 blk = blk_by_name(S_ID);
387 assert(blk);
389 ctx = blk_get_aio_context(blk);
390 aio_context_acquire(ctx);
391 monitor_remove_blk(blk);
392 blk_unref(blk);
393 aio_context_release(ctx);
396 static void test_secondary_read(void)
398 BlockBackend *blk;
400 blk = start_secondary();
402 /* read from 0 to IMG_SIZE */
403 test_blk_read(blk, 0, 0, IMG_SIZE, 0, IMG_SIZE, true);
405 teardown_secondary();
408 static void test_secondary_write(void)
410 BlockBackend *blk;
412 blk = start_secondary();
414 /* write from 0 to IMG_SIZE */
415 test_blk_write(blk, 0, 0, IMG_SIZE, true);
417 teardown_secondary();
420 static void test_secondary_start(void)
422 BlockBackend *top_blk, *local_blk;
423 Error *local_err = NULL;
424 bool failover = true;
426 top_blk = start_secondary();
427 replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
428 g_assert(!local_err);
430 /* read from s_local_disk (0, IMG_SIZE) */
431 test_blk_read(top_blk, 0x11, 0, IMG_SIZE, 0, IMG_SIZE, false);
433 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
434 local_blk = blk_by_name(S_LOCAL_DISK_ID);
435 test_blk_write(local_blk, 0x22, IMG_SIZE / 2, IMG_SIZE / 2, false);
437 /* replication will backup s_local_disk to s_hidden_disk */
438 test_blk_read(top_blk, 0x11, IMG_SIZE / 2,
439 IMG_SIZE / 2, 0, IMG_SIZE, false);
441 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
442 test_blk_write(top_blk, 0x33, 0, IMG_SIZE / 2, false);
444 /* read from s_active_disk (0, IMG_SIZE/2) */
445 test_blk_read(top_blk, 0x33, 0, IMG_SIZE / 2,
446 0, IMG_SIZE / 2, false);
448 /* unblock top_bs */
449 replication_stop_all(failover, &local_err);
450 g_assert(!local_err);
452 teardown_secondary();
456 static void test_secondary_stop(void)
458 BlockBackend *top_blk, *local_blk;
459 Error *local_err = NULL;
460 bool failover = true;
462 top_blk = start_secondary();
463 replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
464 g_assert(!local_err);
466 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
467 local_blk = blk_by_name(S_LOCAL_DISK_ID);
468 test_blk_write(local_blk, 0x22, IMG_SIZE / 2, IMG_SIZE / 2, false);
470 /* replication will backup s_local_disk to s_hidden_disk */
471 test_blk_read(top_blk, 0x11, IMG_SIZE / 2,
472 IMG_SIZE / 2, 0, IMG_SIZE, false);
474 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
475 test_blk_write(top_blk, 0x33, 0, IMG_SIZE / 2, false);
477 /* do active commit */
478 replication_stop_all(failover, &local_err);
479 g_assert(!local_err);
481 /* read from s_local_disk (0, IMG_SIZE / 2) */
482 test_blk_read(top_blk, 0x33, 0, IMG_SIZE / 2,
483 0, IMG_SIZE / 2, false);
486 /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
487 test_blk_read(top_blk, 0x22, IMG_SIZE / 2,
488 IMG_SIZE / 2, 0, IMG_SIZE, false);
490 teardown_secondary();
493 static void test_secondary_do_checkpoint(void)
495 BlockBackend *top_blk, *local_blk;
496 Error *local_err = NULL;
497 bool failover = true;
499 top_blk = start_secondary();
500 replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
501 g_assert(!local_err);
503 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
504 local_blk = blk_by_name(S_LOCAL_DISK_ID);
505 test_blk_write(local_blk, 0x22, IMG_SIZE / 2,
506 IMG_SIZE / 2, false);
508 /* replication will backup s_local_disk to s_hidden_disk */
509 test_blk_read(top_blk, 0x11, IMG_SIZE / 2,
510 IMG_SIZE / 2, 0, IMG_SIZE, false);
512 replication_do_checkpoint_all(&local_err);
513 g_assert(!local_err);
515 /* after checkpoint, read pattern 0x22 from s_local_disk */
516 test_blk_read(top_blk, 0x22, IMG_SIZE / 2,
517 IMG_SIZE / 2, 0, IMG_SIZE, false);
519 /* unblock top_bs */
520 replication_stop_all(failover, &local_err);
521 g_assert(!local_err);
523 teardown_secondary();
526 static void test_secondary_get_error_all(void)
528 Error *local_err = NULL;
529 bool failover = true;
531 start_secondary();
532 replication_start_all(REPLICATION_MODE_SECONDARY, &local_err);
533 g_assert(!local_err);
535 replication_get_error_all(&local_err);
536 g_assert(!local_err);
538 /* unblock top_bs */
539 replication_stop_all(failover, &local_err);
540 g_assert(!local_err);
542 teardown_secondary();
545 static void sigabrt_handler(int signo)
547 cleanup_imgs();
550 static void setup_sigabrt_handler(void)
552 struct sigaction sigact;
554 sigact = (struct sigaction) {
555 .sa_handler = sigabrt_handler,
556 .sa_flags = SA_RESETHAND,
558 sigemptyset(&sigact.sa_mask);
559 sigaction(SIGABRT, &sigact, NULL);
562 int main(int argc, char **argv)
564 int ret;
565 qemu_init_main_loop(&error_fatal);
566 bdrv_init();
568 g_test_init(&argc, &argv, NULL);
569 setup_sigabrt_handler();
571 prepare_imgs();
573 /* Primary */
574 g_test_add_func("/replication/primary/read", test_primary_read);
575 g_test_add_func("/replication/primary/write", test_primary_write);
576 g_test_add_func("/replication/primary/start", test_primary_start);
577 g_test_add_func("/replication/primary/stop", test_primary_stop);
578 g_test_add_func("/replication/primary/do_checkpoint",
579 test_primary_do_checkpoint);
580 g_test_add_func("/replication/primary/get_error_all",
581 test_primary_get_error_all);
583 /* Secondary */
584 g_test_add_func("/replication/secondary/read", test_secondary_read);
585 g_test_add_func("/replication/secondary/write", test_secondary_write);
586 g_test_add_func("/replication/secondary/start", test_secondary_start);
587 g_test_add_func("/replication/secondary/stop", test_secondary_stop);
588 g_test_add_func("/replication/secondary/do_checkpoint",
589 test_secondary_do_checkpoint);
590 g_test_add_func("/replication/secondary/get_error_all",
591 test_secondary_get_error_all);
593 ret = g_test_run();
595 cleanup_imgs();
597 return ret;