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
[] = "/tmp/p_local_disk.XXXXXX";
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
= {
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 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
);
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
);
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)
169 unlink(p_local_disk
);
172 unlink(s_local_disk
);
173 unlink(s_active_disk
);
174 unlink(s_hidden_disk
);
177 static BlockBackend
*start_primary(void)
182 Error
*local_err
= NULL
;
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"
189 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
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
);
198 g_assert(!local_err
);
200 monitor_add_blk(blk
, P_ID
, &local_err
);
201 g_assert(!local_err
);
208 static void teardown_primary(void)
214 blk
= blk_by_name(P_ID
);
217 ctx
= blk_get_aio_context(blk
);
218 aio_context_acquire(ctx
);
219 monitor_remove_blk(blk
);
221 aio_context_release(ctx
);
224 static void test_primary_read(void)
228 blk
= start_primary();
230 /* read from 0 to IMG_SIZE */
231 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
236 static void test_primary_write(void)
240 blk
= start_primary();
242 /* write from 0 to IMG_SIZE */
243 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
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);
267 static void test_primary_stop(void)
269 Error
*local_err
= NULL
;
270 bool failover
= true;
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
);
283 static void test_primary_do_checkpoint(void)
285 Error
*local_err
= NULL
;
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
);
298 static void test_primary_get_error_all(void)
300 Error
*local_err
= NULL
;
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
);
313 static BlockBackend
*start_secondary(void)
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,"
325 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
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
);
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);
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
352 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
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
);
361 monitor_add_blk(blk
, S_ID
, &local_err
);
362 g_assert(!local_err
);
369 static void teardown_secondary(void)
371 /* only need to destroy two BBs */
375 /* remove S_LOCAL_DISK_ID */
376 blk
= blk_by_name(S_LOCAL_DISK_ID
);
379 ctx
= blk_get_aio_context(blk
);
380 aio_context_acquire(ctx
);
381 monitor_remove_blk(blk
);
383 aio_context_release(ctx
);
386 blk
= blk_by_name(S_ID
);
389 ctx
= blk_get_aio_context(blk
);
390 aio_context_acquire(ctx
);
391 monitor_remove_blk(blk
);
393 aio_context_release(ctx
);
396 static void test_secondary_read(void)
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)
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);
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_continuous_replication(void)
495 BlockBackend
*top_blk
, *local_blk
;
496 Error
*local_err
= NULL
;
498 top_blk
= start_secondary();
499 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
500 g_assert(!local_err
);
502 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
503 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
504 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
506 /* replication will backup s_local_disk to s_hidden_disk */
507 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
508 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
510 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
511 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
513 /* do failover (active commit) */
514 replication_stop_all(true, &local_err
);
515 g_assert(!local_err
);
517 /* it should ignore all requests from now on */
519 /* start after failover */
520 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
521 g_assert(!local_err
);
524 replication_do_checkpoint_all(&local_err
);
525 g_assert(!local_err
);
528 replication_stop_all(true, &local_err
);
529 g_assert(!local_err
);
531 /* read from s_local_disk (0, IMG_SIZE / 2) */
532 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
533 0, IMG_SIZE
/ 2, false);
536 /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
537 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
538 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
540 teardown_secondary();
543 static void test_secondary_do_checkpoint(void)
545 BlockBackend
*top_blk
, *local_blk
;
546 Error
*local_err
= NULL
;
547 bool failover
= true;
549 top_blk
= start_secondary();
550 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
551 g_assert(!local_err
);
553 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
554 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
555 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2,
556 IMG_SIZE
/ 2, false);
558 /* replication will backup s_local_disk to s_hidden_disk */
559 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
560 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
562 replication_do_checkpoint_all(&local_err
);
563 g_assert(!local_err
);
565 /* after checkpoint, read pattern 0x22 from s_local_disk */
566 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
567 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
570 replication_stop_all(failover
, &local_err
);
571 g_assert(!local_err
);
573 teardown_secondary();
576 static void test_secondary_get_error_all(void)
578 Error
*local_err
= NULL
;
579 bool failover
= true;
582 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
583 g_assert(!local_err
);
585 replication_get_error_all(&local_err
);
586 g_assert(!local_err
);
589 replication_stop_all(failover
, &local_err
);
590 g_assert(!local_err
);
592 teardown_secondary();
595 static void sigabrt_handler(int signo
)
600 static void setup_sigabrt_handler(void)
602 struct sigaction sigact
;
604 sigact
= (struct sigaction
) {
605 .sa_handler
= sigabrt_handler
,
606 .sa_flags
= SA_RESETHAND
,
608 sigemptyset(&sigact
.sa_mask
);
609 sigaction(SIGABRT
, &sigact
, NULL
);
612 int main(int argc
, char **argv
)
615 qemu_init_main_loop(&error_fatal
);
618 g_test_init(&argc
, &argv
, NULL
);
619 setup_sigabrt_handler();
624 g_test_add_func("/replication/primary/read", test_primary_read
);
625 g_test_add_func("/replication/primary/write", test_primary_write
);
626 g_test_add_func("/replication/primary/start", test_primary_start
);
627 g_test_add_func("/replication/primary/stop", test_primary_stop
);
628 g_test_add_func("/replication/primary/do_checkpoint",
629 test_primary_do_checkpoint
);
630 g_test_add_func("/replication/primary/get_error_all",
631 test_primary_get_error_all
);
634 g_test_add_func("/replication/secondary/read", test_secondary_read
);
635 g_test_add_func("/replication/secondary/write", test_secondary_write
);
636 g_test_add_func("/replication/secondary/start", test_secondary_start
);
637 g_test_add_func("/replication/secondary/stop", test_secondary_stop
);
638 g_test_add_func("/replication/secondary/continuous_replication",
639 test_secondary_continuous_replication
);
640 g_test_add_func("/replication/secondary/do_checkpoint",
641 test_secondary_do_checkpoint
);
642 g_test_add_func("/replication/secondary/get_error_all",
643 test_secondary_get_error_all
);