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.
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)
25 #define P_ID "primary-id"
26 static char *p_local_disk
;
29 #define S_ID "secondary-id"
30 #define S_LOCAL_DISK_ID "secondary-local-disk-id"
31 static char *s_local_disk
;
32 static char *s_active_disk
;
33 static char *s_hidden_disk
;
35 /* FIXME: steal from blockdev.c */
36 QemuOptsList qemu_drive_opts
= {
38 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
44 #define NOT_DONE 0x7fffffff
46 static void blk_rw_done(void *opaque
, int 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
,
56 void *pattern_buf
= NULL
;
59 int async_ret
= NOT_DONE
;
62 cmp_buf
= g_malloc(pattern_count
);
63 memset(cmp_buf
, pattern
, pattern_count
);
66 pattern_buf
= g_malloc(count
);
68 memset(pattern_buf
, pattern
, count
);
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);
82 g_assert(async_ret
!= 0);
84 g_assert(async_ret
== 0);
86 g_assert(memcmp(pattern_buf
+ pattern_offset
,
87 cmp_buf
, pattern_count
) <= 0);
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
;
101 int async_ret
= NOT_DONE
;
103 pattern_buf
= g_malloc(count
);
105 memset(pattern_buf
, pattern
, count
);
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);
119 g_assert(async_ret
!= 0);
121 g_assert(async_ret
== 0);
125 qemu_iovec_destroy(&qiov
);
129 * Create a uniquely-named empty temporary file.
131 static void make_temp(char *template)
135 fd
= mkstemp(template);
140 static void prepare_imgs(void)
142 make_temp(p_local_disk
);
143 make_temp(s_local_disk
);
144 make_temp(s_active_disk
);
145 make_temp(s_hidden_disk
);
148 bdrv_img_create(p_local_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
149 BDRV_O_RDWR
, true, &error_abort
);
152 bdrv_img_create(s_local_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
153 BDRV_O_RDWR
, true, &error_abort
);
154 bdrv_img_create(s_active_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
155 BDRV_O_RDWR
, true, &error_abort
);
156 bdrv_img_create(s_hidden_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
157 BDRV_O_RDWR
, true, &error_abort
);
160 static void cleanup_imgs(void)
163 unlink(p_local_disk
);
166 unlink(s_local_disk
);
167 unlink(s_active_disk
);
168 unlink(s_hidden_disk
);
171 static BlockBackend
*start_primary(void)
178 cmdline
= g_strdup_printf("driver=replication,mode=primary,node-name=xxx,"
179 "file.driver=qcow2,file.file.filename=%s,"
180 "file.file.locking=off"
182 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
185 qdict
= qemu_opts_to_qdict(opts
, NULL
);
186 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
187 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
189 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &error_abort
);
192 monitor_add_blk(blk
, P_ID
, &error_abort
);
199 static void teardown_primary(void)
205 blk
= blk_by_name(P_ID
);
208 ctx
= blk_get_aio_context(blk
);
209 aio_context_acquire(ctx
);
210 monitor_remove_blk(blk
);
212 aio_context_release(ctx
);
215 static void test_primary_read(void)
219 blk
= start_primary();
221 /* read from 0 to IMG_SIZE */
222 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
227 static void test_primary_write(void)
231 blk
= start_primary();
233 /* write from 0 to IMG_SIZE */
234 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
239 static void test_primary_start(void)
241 BlockBackend
*blk
= NULL
;
243 blk
= start_primary();
245 replication_start_all(REPLICATION_MODE_PRIMARY
, &error_abort
);
247 /* read from 0 to IMG_SIZE */
248 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
250 /* write 0x22 from 0 to IMG_SIZE */
251 test_blk_write(blk
, 0x22, 0, IMG_SIZE
, false);
256 static void test_primary_stop(void)
258 bool failover
= true;
262 replication_start_all(REPLICATION_MODE_PRIMARY
, &error_abort
);
264 replication_stop_all(failover
, &error_abort
);
269 static void test_primary_do_checkpoint(void)
273 replication_start_all(REPLICATION_MODE_PRIMARY
, &error_abort
);
275 replication_do_checkpoint_all(&error_abort
);
280 static void test_primary_get_error_all(void)
284 replication_start_all(REPLICATION_MODE_PRIMARY
, &error_abort
);
286 replication_get_error_all(&error_abort
);
291 static BlockBackend
*start_secondary(void)
298 /* add s_local_disk and forge S_LOCAL_DISK_ID */
299 cmdline
= g_strdup_printf("file.filename=%s,driver=qcow2,"
302 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
305 qdict
= qemu_opts_to_qdict(opts
, NULL
);
306 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
307 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
309 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &error_abort
);
311 monitor_add_blk(blk
, S_LOCAL_DISK_ID
, &error_abort
);
313 /* format s_local_disk with pattern "0x11" */
314 test_blk_write(blk
, 0x11, 0, IMG_SIZE
, false);
318 /* add S_(ACTIVE/HIDDEN)_DISK and forge S_ID */
319 cmdline
= g_strdup_printf("driver=replication,mode=secondary,top-id=%s,"
320 "file.driver=qcow2,file.file.filename=%s,"
321 "file.file.locking=off,"
322 "file.backing.driver=qcow2,"
323 "file.backing.file.filename=%s,"
324 "file.backing.file.locking=off,"
325 "file.backing.backing=%s"
326 , S_ID
, s_active_disk
, s_hidden_disk
328 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
331 qdict
= qemu_opts_to_qdict(opts
, NULL
);
332 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
333 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
335 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &error_abort
);
337 monitor_add_blk(blk
, S_ID
, &error_abort
);
344 static void teardown_secondary(void)
346 /* only need to destroy two BBs */
350 /* remove S_LOCAL_DISK_ID */
351 blk
= blk_by_name(S_LOCAL_DISK_ID
);
354 ctx
= blk_get_aio_context(blk
);
355 aio_context_acquire(ctx
);
356 monitor_remove_blk(blk
);
358 aio_context_release(ctx
);
361 blk
= blk_by_name(S_ID
);
364 ctx
= blk_get_aio_context(blk
);
365 aio_context_acquire(ctx
);
366 monitor_remove_blk(blk
);
368 aio_context_release(ctx
);
371 static void test_secondary_read(void)
375 blk
= start_secondary();
377 /* read from 0 to IMG_SIZE */
378 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
380 teardown_secondary();
383 static void test_secondary_write(void)
387 blk
= start_secondary();
389 /* write from 0 to IMG_SIZE */
390 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
392 teardown_secondary();
396 static void test_secondary_start(void)
398 BlockBackend
*top_blk
, *local_blk
;
399 bool failover
= true;
401 top_blk
= start_secondary();
402 replication_start_all(REPLICATION_MODE_SECONDARY
, &error_abort
);
404 /* read from s_local_disk (0, IMG_SIZE) */
405 test_blk_read(top_blk
, 0x11, 0, IMG_SIZE
, 0, IMG_SIZE
, false);
407 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
408 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
409 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
411 /* replication will backup s_local_disk to s_hidden_disk */
412 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
413 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
415 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
416 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
418 /* read from s_active_disk (0, IMG_SIZE/2) */
419 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
420 0, IMG_SIZE
/ 2, false);
423 replication_stop_all(failover
, &error_abort
);
425 teardown_secondary();
429 static void test_secondary_stop(void)
431 BlockBackend
*top_blk
, *local_blk
;
432 bool failover
= true;
434 top_blk
= start_secondary();
435 replication_start_all(REPLICATION_MODE_SECONDARY
, &error_abort
);
437 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
438 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
439 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
441 /* replication will backup s_local_disk to s_hidden_disk */
442 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
443 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
445 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
446 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
448 /* do active commit */
449 replication_stop_all(failover
, &error_abort
);
451 /* read from s_local_disk (0, IMG_SIZE / 2) */
452 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
453 0, IMG_SIZE
/ 2, false);
456 /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
457 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
458 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
460 teardown_secondary();
463 static void test_secondary_continuous_replication(void)
465 BlockBackend
*top_blk
, *local_blk
;
467 top_blk
= start_secondary();
468 replication_start_all(REPLICATION_MODE_SECONDARY
, &error_abort
);
470 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
471 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
472 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
474 /* replication will backup s_local_disk to s_hidden_disk */
475 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
476 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
478 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
479 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
481 /* do failover (active commit) */
482 replication_stop_all(true, &error_abort
);
484 /* it should ignore all requests from now on */
486 /* start after failover */
487 replication_start_all(REPLICATION_MODE_PRIMARY
, &error_abort
);
490 replication_do_checkpoint_all(&error_abort
);
493 replication_stop_all(true, &error_abort
);
495 /* read from s_local_disk (0, IMG_SIZE / 2) */
496 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
497 0, IMG_SIZE
/ 2, false);
500 /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
501 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
502 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
504 teardown_secondary();
507 static void test_secondary_do_checkpoint(void)
509 BlockBackend
*top_blk
, *local_blk
;
510 bool failover
= true;
512 top_blk
= start_secondary();
513 replication_start_all(REPLICATION_MODE_SECONDARY
, &error_abort
);
515 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
516 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
517 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2,
518 IMG_SIZE
/ 2, false);
520 /* replication will backup s_local_disk to s_hidden_disk */
521 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
522 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
524 replication_do_checkpoint_all(&error_abort
);
526 /* after checkpoint, read pattern 0x22 from s_local_disk */
527 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
528 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
531 replication_stop_all(failover
, &error_abort
);
533 teardown_secondary();
536 static void test_secondary_get_error_all(void)
538 bool failover
= true;
541 replication_start_all(REPLICATION_MODE_SECONDARY
, &error_abort
);
543 replication_get_error_all(&error_abort
);
546 replication_stop_all(failover
, &error_abort
);
548 teardown_secondary();
552 static void sigabrt_handler(int signo
)
557 static void setup_sigabrt_handler(void)
560 signal(SIGABRT
, sigabrt_handler
);
562 struct sigaction sigact
;
564 sigact
= (struct sigaction
) {
565 .sa_handler
= sigabrt_handler
,
566 .sa_flags
= SA_RESETHAND
,
568 sigemptyset(&sigact
.sa_mask
);
569 sigaction(SIGABRT
, &sigact
, NULL
);
573 int main(int argc
, char **argv
)
576 const char *tmpdir
= g_get_tmp_dir();
577 p_local_disk
= g_strdup_printf("%s/p_local_disk.XXXXXX", tmpdir
);
578 s_local_disk
= g_strdup_printf("%s/s_local_disk.XXXXXX", tmpdir
);
579 s_active_disk
= g_strdup_printf("%s/s_active_disk.XXXXXX", tmpdir
);
580 s_hidden_disk
= g_strdup_printf("%s/s_hidden_disk.XXXXXX", tmpdir
);
581 qemu_init_main_loop(&error_fatal
);
584 g_test_init(&argc
, &argv
, NULL
);
585 setup_sigabrt_handler();
590 g_test_add_func("/replication/primary/read", test_primary_read
);
591 g_test_add_func("/replication/primary/write", test_primary_write
);
592 g_test_add_func("/replication/primary/start", test_primary_start
);
593 g_test_add_func("/replication/primary/stop", test_primary_stop
);
594 g_test_add_func("/replication/primary/do_checkpoint",
595 test_primary_do_checkpoint
);
596 g_test_add_func("/replication/primary/get_error_all",
597 test_primary_get_error_all
);
600 g_test_add_func("/replication/secondary/read", test_secondary_read
);
601 g_test_add_func("/replication/secondary/write", test_secondary_write
);
603 g_test_add_func("/replication/secondary/start", test_secondary_start
);
604 g_test_add_func("/replication/secondary/stop", test_secondary_stop
);
605 g_test_add_func("/replication/secondary/continuous_replication",
606 test_secondary_continuous_replication
);
607 g_test_add_func("/replication/secondary/do_checkpoint",
608 test_secondary_do_checkpoint
);
609 g_test_add_func("/replication/secondary/get_error_all",
610 test_secondary_get_error_all
);
617 g_free(p_local_disk
);
618 g_free(s_local_disk
);
619 g_free(s_active_disk
);
620 g_free(s_hidden_disk
);