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 "replication.h"
15 #include "block/block_int.h"
16 #include "sysemu/block-backend.h"
18 #define IMG_SIZE (64 * 1024 * 1024)
21 #define P_ID "primary-id"
22 static char p_local_disk
[] = "/tmp/p_local_disk.XXXXXX";
25 #define S_ID "secondary-id"
26 #define S_LOCAL_DISK_ID "secondary-local-disk-id"
27 static char s_local_disk
[] = "/tmp/s_local_disk.XXXXXX";
28 static char s_active_disk
[] = "/tmp/s_active_disk.XXXXXX";
29 static char s_hidden_disk
[] = "/tmp/s_hidden_disk.XXXXXX";
31 /* FIXME: steal from blockdev.c */
32 QemuOptsList qemu_drive_opts
= {
34 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
40 #define NOT_DONE 0x7fffffff
42 static void blk_rw_done(void *opaque
, int ret
)
47 static void test_blk_read(BlockBackend
*blk
, long pattern
,
48 int64_t pattern_offset
, int64_t pattern_count
,
49 int64_t offset
, int64_t count
,
52 void *pattern_buf
= NULL
;
55 int async_ret
= NOT_DONE
;
58 cmp_buf
= g_malloc(pattern_count
);
59 memset(cmp_buf
, pattern
, pattern_count
);
62 pattern_buf
= g_malloc(count
);
64 memset(pattern_buf
, pattern
, count
);
66 memset(pattern_buf
, 0x00, count
);
69 qemu_iovec_init(&qiov
, 1);
70 qemu_iovec_add(&qiov
, pattern_buf
, count
);
72 blk_aio_preadv(blk
, offset
, &qiov
, 0, blk_rw_done
, &async_ret
);
73 while (async_ret
== NOT_DONE
) {
74 main_loop_wait(false);
78 g_assert(async_ret
!= 0);
80 g_assert(async_ret
== 0);
82 g_assert(memcmp(pattern_buf
+ pattern_offset
,
83 cmp_buf
, pattern_count
) <= 0);
90 static void test_blk_write(BlockBackend
*blk
, long pattern
, int64_t offset
,
91 int64_t count
, bool expect_failed
)
93 void *pattern_buf
= NULL
;
95 int async_ret
= NOT_DONE
;
97 pattern_buf
= g_malloc(count
);
99 memset(pattern_buf
, pattern
, count
);
101 memset(pattern_buf
, 0x00, count
);
104 qemu_iovec_init(&qiov
, 1);
105 qemu_iovec_add(&qiov
, pattern_buf
, count
);
107 blk_aio_pwritev(blk
, offset
, &qiov
, 0, blk_rw_done
, &async_ret
);
108 while (async_ret
== NOT_DONE
) {
109 main_loop_wait(false);
113 g_assert(async_ret
!= 0);
115 g_assert(async_ret
== 0);
122 * Create a uniquely-named empty temporary file.
124 static void make_temp(char *template)
128 fd
= mkstemp(template);
133 static void prepare_imgs(void)
135 Error
*local_err
= NULL
;
137 make_temp(p_local_disk
);
138 make_temp(s_local_disk
);
139 make_temp(s_active_disk
);
140 make_temp(s_hidden_disk
);
143 bdrv_img_create(p_local_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
144 BDRV_O_RDWR
, &local_err
, true);
145 g_assert(!local_err
);
148 bdrv_img_create(s_local_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
149 BDRV_O_RDWR
, &local_err
, true);
150 g_assert(!local_err
);
151 bdrv_img_create(s_active_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
152 BDRV_O_RDWR
, &local_err
, true);
153 g_assert(!local_err
);
154 bdrv_img_create(s_hidden_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
155 BDRV_O_RDWR
, &local_err
, true);
156 g_assert(!local_err
);
159 static void cleanup_imgs(void)
162 unlink(p_local_disk
);
165 unlink(s_local_disk
);
166 unlink(s_active_disk
);
167 unlink(s_hidden_disk
);
170 static BlockBackend
*start_primary(void)
175 Error
*local_err
= NULL
;
178 cmdline
= g_strdup_printf("driver=replication,mode=primary,node-name=xxx,"
179 "file.driver=qcow2,file.file.filename=%s"
181 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
184 qdict
= qemu_opts_to_qdict(opts
, NULL
);
185 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
186 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
188 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
190 g_assert(!local_err
);
192 monitor_add_blk(blk
, P_ID
, &local_err
);
193 g_assert(!local_err
);
200 static void teardown_primary(void)
205 blk
= blk_by_name(P_ID
);
208 monitor_remove_blk(blk
);
212 static void test_primary_read(void)
216 blk
= start_primary();
218 /* read from 0 to IMG_SIZE */
219 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
224 static void test_primary_write(void)
228 blk
= start_primary();
230 /* write from 0 to IMG_SIZE */
231 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
236 static void test_primary_start(void)
238 BlockBackend
*blk
= NULL
;
239 Error
*local_err
= NULL
;
241 blk
= start_primary();
243 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
244 g_assert(!local_err
);
246 /* read from 0 to IMG_SIZE */
247 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
249 /* write 0x22 from 0 to IMG_SIZE */
250 test_blk_write(blk
, 0x22, 0, IMG_SIZE
, false);
255 static void test_primary_stop(void)
257 Error
*local_err
= NULL
;
258 bool failover
= true;
262 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
263 g_assert(!local_err
);
265 replication_stop_all(failover
, &local_err
);
266 g_assert(!local_err
);
271 static void test_primary_do_checkpoint(void)
273 Error
*local_err
= NULL
;
277 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
278 g_assert(!local_err
);
280 replication_do_checkpoint_all(&local_err
);
281 g_assert(!local_err
);
286 static void test_primary_get_error_all(void)
288 Error
*local_err
= NULL
;
292 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
293 g_assert(!local_err
);
295 replication_get_error_all(&local_err
);
296 g_assert(!local_err
);
301 static BlockBackend
*start_secondary(void)
307 Error
*local_err
= NULL
;
309 /* add s_local_disk and forge S_LOCAL_DISK_ID */
310 cmdline
= g_strdup_printf("file.filename=%s,driver=qcow2", s_local_disk
);
311 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
314 qdict
= qemu_opts_to_qdict(opts
, NULL
);
315 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
316 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
318 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
320 monitor_add_blk(blk
, S_LOCAL_DISK_ID
, &local_err
);
321 g_assert(!local_err
);
323 /* format s_local_disk with pattern "0x11" */
324 test_blk_write(blk
, 0x11, 0, IMG_SIZE
, false);
328 /* add S_(ACTIVE/HIDDEN)_DISK and forge S_ID */
329 cmdline
= g_strdup_printf("driver=replication,mode=secondary,top-id=%s,"
330 "file.driver=qcow2,file.file.filename=%s,"
331 "file.backing.driver=qcow2,"
332 "file.backing.file.filename=%s,"
333 "file.backing.backing=%s"
334 , S_ID
, s_active_disk
, s_hidden_disk
336 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
339 qdict
= qemu_opts_to_qdict(opts
, NULL
);
340 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
341 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
343 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
345 monitor_add_blk(blk
, S_ID
, &local_err
);
346 g_assert(!local_err
);
353 static void teardown_secondary(void)
355 /* only need to destroy two BBs */
358 /* remove S_LOCAL_DISK_ID */
359 blk
= blk_by_name(S_LOCAL_DISK_ID
);
362 monitor_remove_blk(blk
);
366 blk
= blk_by_name(S_ID
);
369 monitor_remove_blk(blk
);
373 static void test_secondary_read(void)
377 blk
= start_secondary();
379 /* read from 0 to IMG_SIZE */
380 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
382 teardown_secondary();
385 static void test_secondary_write(void)
389 blk
= start_secondary();
391 /* write from 0 to IMG_SIZE */
392 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
394 teardown_secondary();
397 static void test_secondary_start(void)
399 BlockBackend
*top_blk
, *local_blk
;
400 Error
*local_err
= NULL
;
401 bool failover
= true;
403 top_blk
= start_secondary();
404 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
405 g_assert(!local_err
);
407 /* read from s_local_disk (0, IMG_SIZE) */
408 test_blk_read(top_blk
, 0x11, 0, IMG_SIZE
, 0, IMG_SIZE
, false);
410 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
411 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
412 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
414 /* replication will backup s_local_disk to s_hidden_disk */
415 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
416 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
418 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
419 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
421 /* read from s_active_disk (0, IMG_SIZE/2) */
422 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
423 0, IMG_SIZE
/ 2, false);
426 replication_stop_all(failover
, &local_err
);
427 g_assert(!local_err
);
429 teardown_secondary();
433 static void test_secondary_stop(void)
435 BlockBackend
*top_blk
, *local_blk
;
436 Error
*local_err
= NULL
;
437 bool failover
= true;
439 top_blk
= start_secondary();
440 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
441 g_assert(!local_err
);
443 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
444 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
445 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
447 /* replication will backup s_local_disk to s_hidden_disk */
448 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
449 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
451 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
452 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
454 /* do active commit */
455 replication_stop_all(failover
, &local_err
);
456 g_assert(!local_err
);
458 /* read from s_local_disk (0, IMG_SIZE / 2) */
459 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
460 0, IMG_SIZE
/ 2, false);
463 /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
464 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
465 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
467 teardown_secondary();
470 static void test_secondary_do_checkpoint(void)
472 BlockBackend
*top_blk
, *local_blk
;
473 Error
*local_err
= NULL
;
474 bool failover
= true;
476 top_blk
= start_secondary();
477 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
478 g_assert(!local_err
);
480 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
481 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
482 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2,
483 IMG_SIZE
/ 2, false);
485 /* replication will backup s_local_disk to s_hidden_disk */
486 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
487 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
489 replication_do_checkpoint_all(&local_err
);
490 g_assert(!local_err
);
492 /* after checkpoint, read pattern 0x22 from s_local_disk */
493 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
494 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
497 replication_stop_all(failover
, &local_err
);
498 g_assert(!local_err
);
500 teardown_secondary();
503 static void test_secondary_get_error_all(void)
505 Error
*local_err
= NULL
;
506 bool failover
= true;
509 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
510 g_assert(!local_err
);
512 replication_get_error_all(&local_err
);
513 g_assert(!local_err
);
516 replication_stop_all(failover
, &local_err
);
517 g_assert(!local_err
);
519 teardown_secondary();
522 static void sigabrt_handler(int signo
)
527 static void setup_sigabrt_handler(void)
529 struct sigaction sigact
;
531 sigact
= (struct sigaction
) {
532 .sa_handler
= sigabrt_handler
,
533 .sa_flags
= SA_RESETHAND
,
535 sigemptyset(&sigact
.sa_mask
);
536 sigaction(SIGABRT
, &sigact
, NULL
);
539 int main(int argc
, char **argv
)
542 qemu_init_main_loop(&error_fatal
);
545 g_test_init(&argc
, &argv
, NULL
);
546 setup_sigabrt_handler();
551 g_test_add_func("/replication/primary/read", test_primary_read
);
552 g_test_add_func("/replication/primary/write", test_primary_write
);
553 g_test_add_func("/replication/primary/start", test_primary_start
);
554 g_test_add_func("/replication/primary/stop", test_primary_stop
);
555 g_test_add_func("/replication/primary/do_checkpoint",
556 test_primary_do_checkpoint
);
557 g_test_add_func("/replication/primary/get_error_all",
558 test_primary_get_error_all
);
561 g_test_add_func("/replication/secondary/read", test_secondary_read
);
562 g_test_add_func("/replication/secondary/write", test_secondary_write
);
563 g_test_add_func("/replication/secondary/start", test_secondary_start
);
564 g_test_add_func("/replication/secondary/stop", test_secondary_stop
);
565 g_test_add_func("/replication/secondary/do_checkpoint",
566 test_secondary_do_checkpoint
);
567 g_test_add_func("/replication/secondary/get_error_all",
568 test_secondary_get_error_all
);