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 "block/qdict.h"
19 #include "sysemu/block-backend.h"
21 #define IMG_SIZE (64 * 1024 * 1024)
24 #define P_ID "primary-id"
25 static char p_local_disk
[] = "/tmp/p_local_disk.XXXXXX";
28 #define S_ID "secondary-id"
29 #define S_LOCAL_DISK_ID "secondary-local-disk-id"
30 static char s_local_disk
[] = "/tmp/s_local_disk.XXXXXX";
31 static char s_active_disk
[] = "/tmp/s_active_disk.XXXXXX";
32 static char s_hidden_disk
[] = "/tmp/s_hidden_disk.XXXXXX";
34 /* FIXME: steal from blockdev.c */
35 QemuOptsList qemu_drive_opts
= {
37 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
43 #define NOT_DONE 0x7fffffff
45 static void blk_rw_done(void *opaque
, int ret
)
50 static void test_blk_read(BlockBackend
*blk
, long pattern
,
51 int64_t pattern_offset
, int64_t pattern_count
,
52 int64_t offset
, int64_t count
,
55 void *pattern_buf
= NULL
;
58 int async_ret
= NOT_DONE
;
61 cmp_buf
= g_malloc(pattern_count
);
62 memset(cmp_buf
, pattern
, pattern_count
);
65 pattern_buf
= g_malloc(count
);
67 memset(pattern_buf
, pattern
, count
);
69 memset(pattern_buf
, 0x00, count
);
72 qemu_iovec_init(&qiov
, 1);
73 qemu_iovec_add(&qiov
, pattern_buf
, count
);
75 blk_aio_preadv(blk
, offset
, &qiov
, 0, blk_rw_done
, &async_ret
);
76 while (async_ret
== NOT_DONE
) {
77 main_loop_wait(false);
81 g_assert(async_ret
!= 0);
83 g_assert(async_ret
== 0);
85 g_assert(memcmp(pattern_buf
+ pattern_offset
,
86 cmp_buf
, pattern_count
) <= 0);
92 qemu_iovec_destroy(&qiov
);
95 static void test_blk_write(BlockBackend
*blk
, long pattern
, int64_t offset
,
96 int64_t count
, bool expect_failed
)
98 void *pattern_buf
= NULL
;
100 int async_ret
= NOT_DONE
;
102 pattern_buf
= g_malloc(count
);
104 memset(pattern_buf
, pattern
, count
);
106 memset(pattern_buf
, 0x00, count
);
109 qemu_iovec_init(&qiov
, 1);
110 qemu_iovec_add(&qiov
, pattern_buf
, count
);
112 blk_aio_pwritev(blk
, offset
, &qiov
, 0, blk_rw_done
, &async_ret
);
113 while (async_ret
== NOT_DONE
) {
114 main_loop_wait(false);
118 g_assert(async_ret
!= 0);
120 g_assert(async_ret
== 0);
124 qemu_iovec_destroy(&qiov
);
128 * Create a uniquely-named empty temporary file.
130 static void make_temp(char *template)
134 fd
= mkstemp(template);
139 static void prepare_imgs(void)
141 Error
*local_err
= NULL
;
143 make_temp(p_local_disk
);
144 make_temp(s_local_disk
);
145 make_temp(s_active_disk
);
146 make_temp(s_hidden_disk
);
149 bdrv_img_create(p_local_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
150 BDRV_O_RDWR
, true, &local_err
);
151 g_assert(!local_err
);
154 bdrv_img_create(s_local_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
155 BDRV_O_RDWR
, true, &local_err
);
156 g_assert(!local_err
);
157 bdrv_img_create(s_active_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
158 BDRV_O_RDWR
, true, &local_err
);
159 g_assert(!local_err
);
160 bdrv_img_create(s_hidden_disk
, "qcow2", NULL
, NULL
, NULL
, IMG_SIZE
,
161 BDRV_O_RDWR
, true, &local_err
);
162 g_assert(!local_err
);
165 static void cleanup_imgs(void)
168 unlink(p_local_disk
);
171 unlink(s_local_disk
);
172 unlink(s_active_disk
);
173 unlink(s_hidden_disk
);
176 static BlockBackend
*start_primary(void)
181 Error
*local_err
= NULL
;
184 cmdline
= g_strdup_printf("driver=replication,mode=primary,node-name=xxx,"
185 "file.driver=qcow2,file.file.filename=%s,"
186 "file.file.locking=off"
188 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
191 qdict
= qemu_opts_to_qdict(opts
, NULL
);
192 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
193 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
195 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
197 g_assert(!local_err
);
199 monitor_add_blk(blk
, P_ID
, &local_err
);
200 g_assert(!local_err
);
207 static void teardown_primary(void)
213 blk
= blk_by_name(P_ID
);
216 ctx
= blk_get_aio_context(blk
);
217 aio_context_acquire(ctx
);
218 monitor_remove_blk(blk
);
220 aio_context_release(ctx
);
223 static void test_primary_read(void)
227 blk
= start_primary();
229 /* read from 0 to IMG_SIZE */
230 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
235 static void test_primary_write(void)
239 blk
= start_primary();
241 /* write from 0 to IMG_SIZE */
242 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
247 static void test_primary_start(void)
249 BlockBackend
*blk
= NULL
;
250 Error
*local_err
= NULL
;
252 blk
= start_primary();
254 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
255 g_assert(!local_err
);
257 /* read from 0 to IMG_SIZE */
258 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
260 /* write 0x22 from 0 to IMG_SIZE */
261 test_blk_write(blk
, 0x22, 0, IMG_SIZE
, false);
266 static void test_primary_stop(void)
268 Error
*local_err
= NULL
;
269 bool failover
= true;
273 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
274 g_assert(!local_err
);
276 replication_stop_all(failover
, &local_err
);
277 g_assert(!local_err
);
282 static void test_primary_do_checkpoint(void)
284 Error
*local_err
= NULL
;
288 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
289 g_assert(!local_err
);
291 replication_do_checkpoint_all(&local_err
);
292 g_assert(!local_err
);
297 static void test_primary_get_error_all(void)
299 Error
*local_err
= NULL
;
303 replication_start_all(REPLICATION_MODE_PRIMARY
, &local_err
);
304 g_assert(!local_err
);
306 replication_get_error_all(&local_err
);
307 g_assert(!local_err
);
312 static BlockBackend
*start_secondary(void)
318 Error
*local_err
= NULL
;
320 /* add s_local_disk and forge S_LOCAL_DISK_ID */
321 cmdline
= g_strdup_printf("file.filename=%s,driver=qcow2,"
324 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
327 qdict
= qemu_opts_to_qdict(opts
, NULL
);
328 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
329 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
331 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
333 monitor_add_blk(blk
, S_LOCAL_DISK_ID
, &local_err
);
334 g_assert(!local_err
);
336 /* format s_local_disk with pattern "0x11" */
337 test_blk_write(blk
, 0x11, 0, IMG_SIZE
, false);
341 /* add S_(ACTIVE/HIDDEN)_DISK and forge S_ID */
342 cmdline
= g_strdup_printf("driver=replication,mode=secondary,top-id=%s,"
343 "file.driver=qcow2,file.file.filename=%s,"
344 "file.file.locking=off,"
345 "file.backing.driver=qcow2,"
346 "file.backing.file.filename=%s,"
347 "file.backing.file.locking=off,"
348 "file.backing.backing=%s"
349 , S_ID
, s_active_disk
, s_hidden_disk
351 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, cmdline
, false);
354 qdict
= qemu_opts_to_qdict(opts
, NULL
);
355 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_DIRECT
, "off");
356 qdict_set_default_str(qdict
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
358 blk
= blk_new_open(NULL
, NULL
, qdict
, BDRV_O_RDWR
, &local_err
);
360 monitor_add_blk(blk
, S_ID
, &local_err
);
361 g_assert(!local_err
);
368 static void teardown_secondary(void)
370 /* only need to destroy two BBs */
374 /* remove S_LOCAL_DISK_ID */
375 blk
= blk_by_name(S_LOCAL_DISK_ID
);
378 ctx
= blk_get_aio_context(blk
);
379 aio_context_acquire(ctx
);
380 monitor_remove_blk(blk
);
382 aio_context_release(ctx
);
385 blk
= blk_by_name(S_ID
);
388 ctx
= blk_get_aio_context(blk
);
389 aio_context_acquire(ctx
);
390 monitor_remove_blk(blk
);
392 aio_context_release(ctx
);
395 static void test_secondary_read(void)
399 blk
= start_secondary();
401 /* read from 0 to IMG_SIZE */
402 test_blk_read(blk
, 0, 0, IMG_SIZE
, 0, IMG_SIZE
, true);
404 teardown_secondary();
407 static void test_secondary_write(void)
411 blk
= start_secondary();
413 /* write from 0 to IMG_SIZE */
414 test_blk_write(blk
, 0, 0, IMG_SIZE
, true);
416 teardown_secondary();
419 static void test_secondary_start(void)
421 BlockBackend
*top_blk
, *local_blk
;
422 Error
*local_err
= NULL
;
423 bool failover
= true;
425 top_blk
= start_secondary();
426 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
427 g_assert(!local_err
);
429 /* read from s_local_disk (0, IMG_SIZE) */
430 test_blk_read(top_blk
, 0x11, 0, IMG_SIZE
, 0, IMG_SIZE
, false);
432 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
433 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
434 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
436 /* replication will backup s_local_disk to s_hidden_disk */
437 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
438 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
440 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
441 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
443 /* read from s_active_disk (0, IMG_SIZE/2) */
444 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
445 0, IMG_SIZE
/ 2, false);
448 replication_stop_all(failover
, &local_err
);
449 g_assert(!local_err
);
451 teardown_secondary();
455 static void test_secondary_stop(void)
457 BlockBackend
*top_blk
, *local_blk
;
458 Error
*local_err
= NULL
;
459 bool failover
= true;
461 top_blk
= start_secondary();
462 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
463 g_assert(!local_err
);
465 /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
466 local_blk
= blk_by_name(S_LOCAL_DISK_ID
);
467 test_blk_write(local_blk
, 0x22, IMG_SIZE
/ 2, IMG_SIZE
/ 2, false);
469 /* replication will backup s_local_disk to s_hidden_disk */
470 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
471 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
473 /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
474 test_blk_write(top_blk
, 0x33, 0, IMG_SIZE
/ 2, false);
476 /* do active commit */
477 replication_stop_all(failover
, &local_err
);
478 g_assert(!local_err
);
480 /* read from s_local_disk (0, IMG_SIZE / 2) */
481 test_blk_read(top_blk
, 0x33, 0, IMG_SIZE
/ 2,
482 0, IMG_SIZE
/ 2, false);
485 /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
486 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
487 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
489 teardown_secondary();
492 static void test_secondary_do_checkpoint(void)
494 BlockBackend
*top_blk
, *local_blk
;
495 Error
*local_err
= NULL
;
496 bool failover
= true;
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,
505 IMG_SIZE
/ 2, false);
507 /* replication will backup s_local_disk to s_hidden_disk */
508 test_blk_read(top_blk
, 0x11, IMG_SIZE
/ 2,
509 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
511 replication_do_checkpoint_all(&local_err
);
512 g_assert(!local_err
);
514 /* after checkpoint, read pattern 0x22 from s_local_disk */
515 test_blk_read(top_blk
, 0x22, IMG_SIZE
/ 2,
516 IMG_SIZE
/ 2, 0, IMG_SIZE
, false);
519 replication_stop_all(failover
, &local_err
);
520 g_assert(!local_err
);
522 teardown_secondary();
525 static void test_secondary_get_error_all(void)
527 Error
*local_err
= NULL
;
528 bool failover
= true;
531 replication_start_all(REPLICATION_MODE_SECONDARY
, &local_err
);
532 g_assert(!local_err
);
534 replication_get_error_all(&local_err
);
535 g_assert(!local_err
);
538 replication_stop_all(failover
, &local_err
);
539 g_assert(!local_err
);
541 teardown_secondary();
544 static void sigabrt_handler(int signo
)
549 static void setup_sigabrt_handler(void)
551 struct sigaction sigact
;
553 sigact
= (struct sigaction
) {
554 .sa_handler
= sigabrt_handler
,
555 .sa_flags
= SA_RESETHAND
,
557 sigemptyset(&sigact
.sa_mask
);
558 sigaction(SIGABRT
, &sigact
, NULL
);
561 int main(int argc
, char **argv
)
564 qemu_init_main_loop(&error_fatal
);
567 g_test_init(&argc
, &argv
, NULL
);
568 setup_sigabrt_handler();
573 g_test_add_func("/replication/primary/read", test_primary_read
);
574 g_test_add_func("/replication/primary/write", test_primary_write
);
575 g_test_add_func("/replication/primary/start", test_primary_start
);
576 g_test_add_func("/replication/primary/stop", test_primary_stop
);
577 g_test_add_func("/replication/primary/do_checkpoint",
578 test_primary_do_checkpoint
);
579 g_test_add_func("/replication/primary/get_error_all",
580 test_primary_get_error_all
);
583 g_test_add_func("/replication/secondary/read", test_secondary_read
);
584 g_test_add_func("/replication/secondary/write", test_secondary_write
);
585 g_test_add_func("/replication/secondary/start", test_secondary_start
);
586 g_test_add_func("/replication/secondary/stop", test_secondary_stop
);
587 g_test_add_func("/replication/secondary/do_checkpoint",
588 test_secondary_do_checkpoint
);
589 g_test_add_func("/replication/secondary/get_error_all",
590 test_secondary_get_error_all
);