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 "replication.h"
17 #include "block/block_int.h"
18 #include "sysemu/block-backend.h"
20 #define IMG_SIZE (64 * 1024 * 1024)
23 #define P_ID "primary-id"
24 static char p_local_disk
[] = "/tmp/p_local_disk.XXXXXX";
27 #define S_ID "secondary-id"
28 #define S_LOCAL_DISK_ID "secondary-local-disk-id"
29 static char s_local_disk
[] = "/tmp/s_local_disk.XXXXXX";
30 static char s_active_disk
[] = "/tmp/s_active_disk.XXXXXX";
31 static char s_hidden_disk
[] = "/tmp/s_hidden_disk.XXXXXX";
33 /* FIXME: steal from blockdev.c */
34 QemuOptsList qemu_drive_opts
= {
36 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
42 #define NOT_DONE 0x7fffffff
44 static void blk_rw_done(void *opaque
, int ret
)
49 static void test_blk_read(BlockBackend
*blk
, long pattern
,
50 int64_t pattern_offset
, int64_t pattern_count
,
51 int64_t offset
, int64_t count
,
54 void *pattern_buf
= NULL
;
57 int async_ret
= NOT_DONE
;
60 cmp_buf
= g_malloc(pattern_count
);
61 memset(cmp_buf
, pattern
, pattern_count
);
64 pattern_buf
= g_malloc(count
);
66 memset(pattern_buf
, pattern
, count
);
68 memset(pattern_buf
, 0x00, count
);
71 qemu_iovec_init(&qiov
, 1);
72 qemu_iovec_add(&qiov
, pattern_buf
, count
);
74 blk_aio_preadv(blk
, offset
, &qiov
, 0, blk_rw_done
, &async_ret
);
75 while (async_ret
== NOT_DONE
) {
76 main_loop_wait(false);
80 g_assert(async_ret
!= 0);
82 g_assert(async_ret
== 0);
84 g_assert(memcmp(pattern_buf
+ pattern_offset
,
85 cmp_buf
, pattern_count
) <= 0);
91 qemu_iovec_destroy(&qiov
);
94 static void test_blk_write(BlockBackend
*blk
, long pattern
, int64_t offset
,
95 int64_t count
, bool expect_failed
)
97 void *pattern_buf
= NULL
;
99 int async_ret
= NOT_DONE
;
101 pattern_buf
= g_malloc(count
);
103 memset(pattern_buf
, pattern
, count
);
105 memset(pattern_buf
, 0x00, count
);
108 qemu_iovec_init(&qiov
, 1);
109 qemu_iovec_add(&qiov
, pattern_buf
, count
);
111 blk_aio_pwritev(blk
, offset
, &qiov
, 0, blk_rw_done
, &async_ret
);
112 while (async_ret
== NOT_DONE
) {
113 main_loop_wait(false);
117 g_assert(async_ret
!= 0);
119 g_assert(async_ret
== 0);
123 qemu_iovec_destroy(&qiov
);
127 * Create a uniquely-named empty temporary file.
129 static void make_temp(char *template)
133 fd
= mkstemp(template);
138 static void prepare_imgs(void)
140 Error
*local_err
= NULL
;
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, &local_err
);
150 g_assert(!local_err
);
153 bdrv_img_create(s_local_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
154 BDRV_O_RDWR
, true, &local_err
);
155 g_assert(!local_err
);
156 bdrv_img_create(s_active_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
157 BDRV_O_RDWR
, true, &local_err
);
158 g_assert(!local_err
);
159 bdrv_img_create(s_hidden_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
160 BDRV_O_RDWR
, true, &local_err
);
161 g_assert(!local_err
);
164 static void cleanup_imgs(void)
167 unlink(p_local_disk
);
170 unlink(s_local_disk
);
171 unlink(s_active_disk
);
172 unlink(s_hidden_disk
);
175 static BlockBackend
*start_primary(void)
180 Error
*local_err
= NULL
;
183 cmdline
= g_strdup_printf("driver=replication,mode=primary,node-name=xxx,"
184 "file.driver=qcow2,file.file.filename=%s,"
185 "file.file.locking=off"
187 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
190 qdict
= qemu_opts_to_qdict(opts
, NULL
);
191 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
192 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
194 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
196 g_assert(!local_err
);
198 monitor_add_blk(blk
, P_ID
, &local_err
);
199 g_assert(!local_err
);
206 static void teardown_primary(void)
211 blk
= blk_by_name(P_ID
);
214 monitor_remove_blk(blk
);
218 static void test_primary_read(void)
222 blk
= start_primary();
224 /* read from 0 to IMG_SIZE */
225 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
230 static void test_primary_write(void)
234 blk
= start_primary();
236 /* write from 0 to IMG_SIZE */
237 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
242 static void test_primary_start(void)
244 BlockBackend
*blk
= NULL
;
245 Error
*local_err
= NULL
;
247 blk
= start_primary();
249 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
250 g_assert(!local_err
);
252 /* read from 0 to IMG_SIZE */
253 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
255 /* write 0x22 from 0 to IMG_SIZE */
256 test_blk_write(blk
, 0x22, 0, IMG_SIZE
, false);
261 static void test_primary_stop(void)
263 Error
*local_err
= NULL
;
264 bool failover
= true;
268 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
269 g_assert(!local_err
);
271 replication_stop_all(failover
, &local_err
);
272 g_assert(!local_err
);
277 static void test_primary_do_checkpoint(void)
279 Error
*local_err
= NULL
;
283 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
284 g_assert(!local_err
);
286 replication_do_checkpoint_all(&local_err
);
287 g_assert(!local_err
);
292 static void test_primary_get_error_all(void)
294 Error
*local_err
= NULL
;
298 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
299 g_assert(!local_err
);
301 replication_get_error_all(&local_err
);
302 g_assert(!local_err
);
307 static BlockBackend
*start_secondary(void)
313 Error
*local_err
= NULL
;
315 /* add s_local_disk and forge S_LOCAL_DISK_ID */
316 cmdline
= g_strdup_printf("file.filename=%s,driver=qcow2,"
319 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
322 qdict
= qemu_opts_to_qdict(opts
, NULL
);
323 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
324 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
326 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
328 monitor_add_blk(blk
, S_LOCAL_DISK_ID
, &local_err
);
329 g_assert(!local_err
);
331 /* format s_local_disk with pattern "0x11" */
332 test_blk_write(blk
, 0x11, 0, IMG_SIZE
, false);
336 /* add S_(ACTIVE/HIDDEN)_DISK and forge S_ID */
337 cmdline
= g_strdup_printf("driver=replication,mode=secondary,top-id=%s,"
338 "file.driver=qcow2,file.file.filename=%s,"
339 "file.file.locking=off,"
340 "file.backing.driver=qcow2,"
341 "file.backing.file.filename=%s,"
342 "file.backing.file.locking=off,"
343 "file.backing.backing=%s"
344 , S_ID
, s_active_disk
, s_hidden_disk
346 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
349 qdict
= qemu_opts_to_qdict(opts
, NULL
);
350 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
351 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
353 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
355 monitor_add_blk(blk
, S_ID
, &local_err
);
356 g_assert(!local_err
);
363 static void teardown_secondary(void)
365 /* only need to destroy two BBs */
368 /* remove S_LOCAL_DISK_ID */
369 blk
= blk_by_name(S_LOCAL_DISK_ID
);
372 monitor_remove_blk(blk
);
376 blk
= blk_by_name(S_ID
);
379 monitor_remove_blk(blk
);
383 static void test_secondary_read(void)
387 blk
= start_secondary();
389 /* read from 0 to IMG_SIZE */
390 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
392 teardown_secondary();
395 static void test_secondary_write(void)
399 blk
= start_secondary();
401 /* write from 0 to IMG_SIZE */
402 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
404 teardown_secondary();
407 static void test_secondary_start(void)
409 BlockBackend
*top_blk
, *local_blk
;
410 Error
*local_err
= NULL
;
411 bool failover
= true;
413 top_blk
= start_secondary();
414 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
415 g_assert(!local_err
);
417 /* read from s_local_disk (0, IMG_SIZE) */
418 test_blk_read(top_blk
, 0x11, 0, IMG_SIZE
, 0, IMG_SIZE
, false);
420 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
421 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
422 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
424 /* replication will backup s_local_disk to s_hidden_disk */
425 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
426 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
428 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
429 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
431 /* read from s_active_disk (0, IMG_SIZE/2) */
432 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
433 0, IMG_SIZE
/ 2, false);
436 replication_stop_all(failover
, &local_err
);
437 g_assert(!local_err
);
439 teardown_secondary();
443 static void test_secondary_stop(void)
445 BlockBackend
*top_blk
, *local_blk
;
446 Error
*local_err
= NULL
;
447 bool failover
= true;
449 top_blk
= start_secondary();
450 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
451 g_assert(!local_err
);
453 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
454 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
455 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
457 /* replication will backup s_local_disk to s_hidden_disk */
458 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
459 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
461 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
462 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
464 /* do active commit */
465 replication_stop_all(failover
, &local_err
);
466 g_assert(!local_err
);
468 /* read from s_local_disk (0, IMG_SIZE / 2) */
469 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
470 0, IMG_SIZE
/ 2, false);
473 /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
474 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
475 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
477 teardown_secondary();
480 static void test_secondary_do_checkpoint(void)
482 BlockBackend
*top_blk
, *local_blk
;
483 Error
*local_err
= NULL
;
484 bool failover
= true;
486 top_blk
= start_secondary();
487 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
488 g_assert(!local_err
);
490 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
491 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
492 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2,
493 IMG_SIZE
/ 2, false);
495 /* replication will backup s_local_disk to s_hidden_disk */
496 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
497 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
499 replication_do_checkpoint_all(&local_err
);
500 g_assert(!local_err
);
502 /* after checkpoint, read pattern 0x22 from s_local_disk */
503 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
504 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
507 replication_stop_all(failover
, &local_err
);
508 g_assert(!local_err
);
510 teardown_secondary();
513 static void test_secondary_get_error_all(void)
515 Error
*local_err
= NULL
;
516 bool failover
= true;
519 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
520 g_assert(!local_err
);
522 replication_get_error_all(&local_err
);
523 g_assert(!local_err
);
526 replication_stop_all(failover
, &local_err
);
527 g_assert(!local_err
);
529 teardown_secondary();
532 static void sigabrt_handler(int signo
)
537 static void setup_sigabrt_handler(void)
539 struct sigaction sigact
;
541 sigact
= (struct sigaction
) {
542 .sa_handler
= sigabrt_handler
,
543 .sa_flags
= SA_RESETHAND
,
545 sigemptyset(&sigact
.sa_mask
);
546 sigaction(SIGABRT
, &sigact
, NULL
);
549 int main(int argc
, char **argv
)
552 qemu_init_main_loop(&error_fatal
);
555 g_test_init(&argc
, &argv
, NULL
);
556 setup_sigabrt_handler();
561 g_test_add_func("/replication/primary/read", test_primary_read
);
562 g_test_add_func("/replication/primary/write", test_primary_write
);
563 g_test_add_func("/replication/primary/start", test_primary_start
);
564 g_test_add_func("/replication/primary/stop", test_primary_stop
);
565 g_test_add_func("/replication/primary/do_checkpoint",
566 test_primary_do_checkpoint
);
567 g_test_add_func("/replication/primary/get_error_all",
568 test_primary_get_error_all
);
571 g_test_add_func("/replication/secondary/read", test_secondary_read
);
572 g_test_add_func("/replication/secondary/write", test_secondary_write
);
573 g_test_add_func("/replication/secondary/start", test_secondary_start
);
574 g_test_add_func("/replication/secondary/stop", test_secondary_stop
);
575 g_test_add_func("/replication/secondary/do_checkpoint",
576 test_secondary_do_checkpoint
);
577 g_test_add_func("/replication/secondary/get_error_all",
578 test_secondary_get_error_all
);