1da668aa1SThomas Huth /*
2da668aa1SThomas Huth * Block replication tests
3da668aa1SThomas Huth *
4da668aa1SThomas Huth * Copyright (c) 2016 FUJITSU LIMITED
5da668aa1SThomas Huth * Author: Changlong Xie <xiecl.fnst@cn.fujitsu.com>
6da668aa1SThomas Huth *
7da668aa1SThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or
8da668aa1SThomas Huth * later. See the COPYING file in the top-level directory.
9da668aa1SThomas Huth */
10da668aa1SThomas Huth
11da668aa1SThomas Huth #include "qemu/osdep.h"
12da668aa1SThomas Huth
13da668aa1SThomas Huth #include "qapi/error.h"
14da668aa1SThomas Huth #include "qapi/qmp/qdict.h"
15da668aa1SThomas Huth #include "qemu/option.h"
16da668aa1SThomas Huth #include "qemu/main-loop.h"
17*b0262955SPaolo Bonzini #include "block/replication.h"
18da668aa1SThomas Huth #include "block/block_int.h"
19da668aa1SThomas Huth #include "block/qdict.h"
20da668aa1SThomas Huth #include "sysemu/block-backend.h"
21da668aa1SThomas Huth
22da668aa1SThomas Huth #define IMG_SIZE (64 * 1024 * 1024)
23da668aa1SThomas Huth
24da668aa1SThomas Huth /* primary */
25da668aa1SThomas Huth #define P_ID "primary-id"
26da668aa1SThomas Huth static char *p_local_disk;
27da668aa1SThomas Huth
28da668aa1SThomas Huth /* secondary */
29da668aa1SThomas Huth #define S_ID "secondary-id"
30da668aa1SThomas Huth #define S_LOCAL_DISK_ID "secondary-local-disk-id"
31da668aa1SThomas Huth static char *s_local_disk;
32da668aa1SThomas Huth static char *s_active_disk;
33da668aa1SThomas Huth static char *s_hidden_disk;
34da668aa1SThomas Huth
35da668aa1SThomas Huth /* FIXME: steal from blockdev.c */
36da668aa1SThomas Huth QemuOptsList qemu_drive_opts = {
37da668aa1SThomas Huth .name = "drive",
38da668aa1SThomas Huth .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
39da668aa1SThomas Huth .desc = {
40da668aa1SThomas Huth { /* end of list */ }
41da668aa1SThomas Huth },
42da668aa1SThomas Huth };
43da668aa1SThomas Huth
44da668aa1SThomas Huth #define NOT_DONE 0x7fffffff
45da668aa1SThomas Huth
blk_rw_done(void * opaque,int ret)46da668aa1SThomas Huth static void blk_rw_done(void *opaque, int ret)
47da668aa1SThomas Huth {
48da668aa1SThomas Huth *(int *)opaque = ret;
49da668aa1SThomas Huth }
50da668aa1SThomas Huth
test_blk_read(BlockBackend * blk,long pattern,int64_t pattern_offset,int64_t pattern_count,int64_t offset,int64_t count,bool expect_failed)51da668aa1SThomas Huth static void test_blk_read(BlockBackend *blk, long pattern,
52da668aa1SThomas Huth int64_t pattern_offset, int64_t pattern_count,
53da668aa1SThomas Huth int64_t offset, int64_t count,
54da668aa1SThomas Huth bool expect_failed)
55da668aa1SThomas Huth {
56da668aa1SThomas Huth void *pattern_buf = NULL;
57da668aa1SThomas Huth QEMUIOVector qiov;
58da668aa1SThomas Huth void *cmp_buf = NULL;
59da668aa1SThomas Huth int async_ret = NOT_DONE;
60da668aa1SThomas Huth
61da668aa1SThomas Huth if (pattern) {
62da668aa1SThomas Huth cmp_buf = g_malloc(pattern_count);
63da668aa1SThomas Huth memset(cmp_buf, pattern, pattern_count);
64da668aa1SThomas Huth }
65da668aa1SThomas Huth
66da668aa1SThomas Huth pattern_buf = g_malloc(count);
67da668aa1SThomas Huth if (pattern) {
68da668aa1SThomas Huth memset(pattern_buf, pattern, count);
69da668aa1SThomas Huth } else {
70da668aa1SThomas Huth memset(pattern_buf, 0x00, count);
71da668aa1SThomas Huth }
72da668aa1SThomas Huth
73da668aa1SThomas Huth qemu_iovec_init(&qiov, 1);
74da668aa1SThomas Huth qemu_iovec_add(&qiov, pattern_buf, count);
75da668aa1SThomas Huth
76da668aa1SThomas Huth blk_aio_preadv(blk, offset, &qiov, 0, blk_rw_done, &async_ret);
77da668aa1SThomas Huth while (async_ret == NOT_DONE) {
78da668aa1SThomas Huth main_loop_wait(false);
79da668aa1SThomas Huth }
80da668aa1SThomas Huth
81da668aa1SThomas Huth if (expect_failed) {
82da668aa1SThomas Huth g_assert(async_ret != 0);
83da668aa1SThomas Huth } else {
84da668aa1SThomas Huth g_assert(async_ret == 0);
85da668aa1SThomas Huth if (pattern) {
86da668aa1SThomas Huth g_assert(memcmp(pattern_buf + pattern_offset,
87da668aa1SThomas Huth cmp_buf, pattern_count) <= 0);
88da668aa1SThomas Huth }
89da668aa1SThomas Huth }
90da668aa1SThomas Huth
91da668aa1SThomas Huth g_free(pattern_buf);
92da668aa1SThomas Huth g_free(cmp_buf);
93da668aa1SThomas Huth qemu_iovec_destroy(&qiov);
94da668aa1SThomas Huth }
95da668aa1SThomas Huth
test_blk_write(BlockBackend * blk,long pattern,int64_t offset,int64_t count,bool expect_failed)96da668aa1SThomas Huth static void test_blk_write(BlockBackend *blk, long pattern, int64_t offset,
97da668aa1SThomas Huth int64_t count, bool expect_failed)
98da668aa1SThomas Huth {
99da668aa1SThomas Huth void *pattern_buf = NULL;
100da668aa1SThomas Huth QEMUIOVector qiov;
101da668aa1SThomas Huth int async_ret = NOT_DONE;
102da668aa1SThomas Huth
103da668aa1SThomas Huth pattern_buf = g_malloc(count);
104da668aa1SThomas Huth if (pattern) {
105da668aa1SThomas Huth memset(pattern_buf, pattern, count);
106da668aa1SThomas Huth } else {
107da668aa1SThomas Huth memset(pattern_buf, 0x00, count);
108da668aa1SThomas Huth }
109da668aa1SThomas Huth
110da668aa1SThomas Huth qemu_iovec_init(&qiov, 1);
111da668aa1SThomas Huth qemu_iovec_add(&qiov, pattern_buf, count);
112da668aa1SThomas Huth
113da668aa1SThomas Huth blk_aio_pwritev(blk, offset, &qiov, 0, blk_rw_done, &async_ret);
114da668aa1SThomas Huth while (async_ret == NOT_DONE) {
115da668aa1SThomas Huth main_loop_wait(false);
116da668aa1SThomas Huth }
117da668aa1SThomas Huth
118da668aa1SThomas Huth if (expect_failed) {
119da668aa1SThomas Huth g_assert(async_ret != 0);
120da668aa1SThomas Huth } else {
121da668aa1SThomas Huth g_assert(async_ret == 0);
122da668aa1SThomas Huth }
123da668aa1SThomas Huth
124da668aa1SThomas Huth g_free(pattern_buf);
125da668aa1SThomas Huth qemu_iovec_destroy(&qiov);
126da668aa1SThomas Huth }
127da668aa1SThomas Huth
128da668aa1SThomas Huth /*
129da668aa1SThomas Huth * Create a uniquely-named empty temporary file.
130da668aa1SThomas Huth */
make_temp(char * template)131da668aa1SThomas Huth static void make_temp(char *template)
132da668aa1SThomas Huth {
133da668aa1SThomas Huth int fd;
134da668aa1SThomas Huth
135da668aa1SThomas Huth fd = mkstemp(template);
136da668aa1SThomas Huth g_assert(fd >= 0);
137da668aa1SThomas Huth close(fd);
138da668aa1SThomas Huth }
139da668aa1SThomas Huth
prepare_imgs(void)140da668aa1SThomas Huth static void prepare_imgs(void)
141da668aa1SThomas Huth {
142da668aa1SThomas Huth make_temp(p_local_disk);
143da668aa1SThomas Huth make_temp(s_local_disk);
144da668aa1SThomas Huth make_temp(s_active_disk);
145da668aa1SThomas Huth make_temp(s_hidden_disk);
146da668aa1SThomas Huth
147da668aa1SThomas Huth /* Primary */
148da668aa1SThomas Huth bdrv_img_create(p_local_disk, "qcow2", NULL, NULL, NULL, IMG_SIZE,
149da668aa1SThomas Huth BDRV_O_RDWR, true, &error_abort);
150da668aa1SThomas Huth
151da668aa1SThomas Huth /* Secondary */
152da668aa1SThomas Huth bdrv_img_create(s_local_disk, "qcow2", NULL, NULL, NULL, IMG_SIZE,
153da668aa1SThomas Huth BDRV_O_RDWR, true, &error_abort);
154da668aa1SThomas Huth bdrv_img_create(s_active_disk, "qcow2", NULL, NULL, NULL, IMG_SIZE,
155da668aa1SThomas Huth BDRV_O_RDWR, true, &error_abort);
156da668aa1SThomas Huth bdrv_img_create(s_hidden_disk, "qcow2", NULL, NULL, NULL, IMG_SIZE,
157da668aa1SThomas Huth BDRV_O_RDWR, true, &error_abort);
158da668aa1SThomas Huth }
159da668aa1SThomas Huth
cleanup_imgs(void)160da668aa1SThomas Huth static void cleanup_imgs(void)
161da668aa1SThomas Huth {
162da668aa1SThomas Huth /* Primary */
163da668aa1SThomas Huth unlink(p_local_disk);
164da668aa1SThomas Huth
165da668aa1SThomas Huth /* Secondary */
166da668aa1SThomas Huth unlink(s_local_disk);
167da668aa1SThomas Huth unlink(s_active_disk);
168da668aa1SThomas Huth unlink(s_hidden_disk);
169da668aa1SThomas Huth }
170da668aa1SThomas Huth
start_primary(void)171da668aa1SThomas Huth static BlockBackend *start_primary(void)
172da668aa1SThomas Huth {
173da668aa1SThomas Huth BlockBackend *blk;
174da668aa1SThomas Huth QemuOpts *opts;
175da668aa1SThomas Huth QDict *qdict;
176da668aa1SThomas Huth char *cmdline;
177da668aa1SThomas Huth
178da668aa1SThomas Huth cmdline = g_strdup_printf("driver=replication,mode=primary,node-name=xxx,"
179da668aa1SThomas Huth "file.driver=qcow2,file.file.filename=%s,"
180da668aa1SThomas Huth "file.file.locking=off"
181da668aa1SThomas Huth , p_local_disk);
182da668aa1SThomas Huth opts = qemu_opts_parse_noisily(&qemu_drive_opts, cmdline, false);
183da668aa1SThomas Huth g_free(cmdline);
184da668aa1SThomas Huth
185da668aa1SThomas Huth qdict = qemu_opts_to_qdict(opts, NULL);
186da668aa1SThomas Huth qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
187da668aa1SThomas Huth qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
188da668aa1SThomas Huth
189da668aa1SThomas Huth blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &error_abort);
190da668aa1SThomas Huth g_assert(blk);
191da668aa1SThomas Huth
192da668aa1SThomas Huth monitor_add_blk(blk, P_ID, &error_abort);
193da668aa1SThomas Huth
194da668aa1SThomas Huth qemu_opts_del(opts);
195da668aa1SThomas Huth
196da668aa1SThomas Huth return blk;
197da668aa1SThomas Huth }
198da668aa1SThomas Huth
teardown_primary(void)199da668aa1SThomas Huth static void teardown_primary(void)
200da668aa1SThomas Huth {
201da668aa1SThomas Huth BlockBackend *blk;
202da668aa1SThomas Huth
203da668aa1SThomas Huth /* remove P_ID */
204da668aa1SThomas Huth blk = blk_by_name(P_ID);
205da668aa1SThomas Huth assert(blk);
206da668aa1SThomas Huth
207da668aa1SThomas Huth monitor_remove_blk(blk);
208da668aa1SThomas Huth blk_unref(blk);
209da668aa1SThomas Huth }
210da668aa1SThomas Huth
test_primary_read(void)211da668aa1SThomas Huth static void test_primary_read(void)
212da668aa1SThomas Huth {
213da668aa1SThomas Huth BlockBackend *blk;
214da668aa1SThomas Huth
215da668aa1SThomas Huth blk = start_primary();
216da668aa1SThomas Huth
217da668aa1SThomas Huth /* read from 0 to IMG_SIZE */
218da668aa1SThomas Huth test_blk_read(blk, 0, 0, IMG_SIZE, 0, IMG_SIZE, true);
219da668aa1SThomas Huth
220da668aa1SThomas Huth teardown_primary();
221da668aa1SThomas Huth }
222da668aa1SThomas Huth
test_primary_write(void)223da668aa1SThomas Huth static void test_primary_write(void)
224da668aa1SThomas Huth {
225da668aa1SThomas Huth BlockBackend *blk;
226da668aa1SThomas Huth
227da668aa1SThomas Huth blk = start_primary();
228da668aa1SThomas Huth
229da668aa1SThomas Huth /* write from 0 to IMG_SIZE */
230da668aa1SThomas Huth test_blk_write(blk, 0, 0, IMG_SIZE, true);
231da668aa1SThomas Huth
232da668aa1SThomas Huth teardown_primary();
233da668aa1SThomas Huth }
234da668aa1SThomas Huth
test_primary_start(void)235da668aa1SThomas Huth static void test_primary_start(void)
236da668aa1SThomas Huth {
237da668aa1SThomas Huth BlockBackend *blk = NULL;
238da668aa1SThomas Huth
239da668aa1SThomas Huth blk = start_primary();
240da668aa1SThomas Huth
241da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_PRIMARY, &error_abort);
242da668aa1SThomas Huth
243da668aa1SThomas Huth /* read from 0 to IMG_SIZE */
244da668aa1SThomas Huth test_blk_read(blk, 0, 0, IMG_SIZE, 0, IMG_SIZE, true);
245da668aa1SThomas Huth
246da668aa1SThomas Huth /* write 0x22 from 0 to IMG_SIZE */
247da668aa1SThomas Huth test_blk_write(blk, 0x22, 0, IMG_SIZE, false);
248da668aa1SThomas Huth
249da668aa1SThomas Huth teardown_primary();
250da668aa1SThomas Huth }
251da668aa1SThomas Huth
test_primary_stop(void)252da668aa1SThomas Huth static void test_primary_stop(void)
253da668aa1SThomas Huth {
254da668aa1SThomas Huth bool failover = true;
255da668aa1SThomas Huth
256da668aa1SThomas Huth start_primary();
257da668aa1SThomas Huth
258da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_PRIMARY, &error_abort);
259da668aa1SThomas Huth
260da668aa1SThomas Huth replication_stop_all(failover, &error_abort);
261da668aa1SThomas Huth
262da668aa1SThomas Huth teardown_primary();
263da668aa1SThomas Huth }
264da668aa1SThomas Huth
test_primary_do_checkpoint(void)265da668aa1SThomas Huth static void test_primary_do_checkpoint(void)
266da668aa1SThomas Huth {
267da668aa1SThomas Huth start_primary();
268da668aa1SThomas Huth
269da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_PRIMARY, &error_abort);
270da668aa1SThomas Huth
271da668aa1SThomas Huth replication_do_checkpoint_all(&error_abort);
272da668aa1SThomas Huth
273da668aa1SThomas Huth teardown_primary();
274da668aa1SThomas Huth }
275da668aa1SThomas Huth
test_primary_get_error_all(void)276da668aa1SThomas Huth static void test_primary_get_error_all(void)
277da668aa1SThomas Huth {
278da668aa1SThomas Huth start_primary();
279da668aa1SThomas Huth
280da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_PRIMARY, &error_abort);
281da668aa1SThomas Huth
282da668aa1SThomas Huth replication_get_error_all(&error_abort);
283da668aa1SThomas Huth
284da668aa1SThomas Huth teardown_primary();
285da668aa1SThomas Huth }
286da668aa1SThomas Huth
start_secondary(void)287da668aa1SThomas Huth static BlockBackend *start_secondary(void)
288da668aa1SThomas Huth {
289da668aa1SThomas Huth QemuOpts *opts;
290da668aa1SThomas Huth QDict *qdict;
291da668aa1SThomas Huth BlockBackend *blk;
292da668aa1SThomas Huth char *cmdline;
293da668aa1SThomas Huth
294da668aa1SThomas Huth /* add s_local_disk and forge S_LOCAL_DISK_ID */
295da668aa1SThomas Huth cmdline = g_strdup_printf("file.filename=%s,driver=qcow2,"
296da668aa1SThomas Huth "file.locking=off",
297da668aa1SThomas Huth s_local_disk);
298da668aa1SThomas Huth opts = qemu_opts_parse_noisily(&qemu_drive_opts, cmdline, false);
299da668aa1SThomas Huth g_free(cmdline);
300da668aa1SThomas Huth
301da668aa1SThomas Huth qdict = qemu_opts_to_qdict(opts, NULL);
302da668aa1SThomas Huth qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
303da668aa1SThomas Huth qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
304da668aa1SThomas Huth
305da668aa1SThomas Huth blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &error_abort);
306da668aa1SThomas Huth assert(blk);
307da668aa1SThomas Huth monitor_add_blk(blk, S_LOCAL_DISK_ID, &error_abort);
308da668aa1SThomas Huth
309da668aa1SThomas Huth /* format s_local_disk with pattern "0x11" */
310da668aa1SThomas Huth test_blk_write(blk, 0x11, 0, IMG_SIZE, false);
311da668aa1SThomas Huth
312da668aa1SThomas Huth qemu_opts_del(opts);
313da668aa1SThomas Huth
314da668aa1SThomas Huth /* add S_(ACTIVE/HIDDEN)_DISK and forge S_ID */
315da668aa1SThomas Huth cmdline = g_strdup_printf("driver=replication,mode=secondary,top-id=%s,"
316da668aa1SThomas Huth "file.driver=qcow2,file.file.filename=%s,"
317da668aa1SThomas Huth "file.file.locking=off,"
318da668aa1SThomas Huth "file.backing.driver=qcow2,"
319da668aa1SThomas Huth "file.backing.file.filename=%s,"
320da668aa1SThomas Huth "file.backing.file.locking=off,"
321da668aa1SThomas Huth "file.backing.backing=%s"
322da668aa1SThomas Huth , S_ID, s_active_disk, s_hidden_disk
323da668aa1SThomas Huth , S_LOCAL_DISK_ID);
324da668aa1SThomas Huth opts = qemu_opts_parse_noisily(&qemu_drive_opts, cmdline, false);
325da668aa1SThomas Huth g_free(cmdline);
326da668aa1SThomas Huth
327da668aa1SThomas Huth qdict = qemu_opts_to_qdict(opts, NULL);
328da668aa1SThomas Huth qdict_set_default_str(qdict, BDRV_OPT_CACHE_DIRECT, "off");
329da668aa1SThomas Huth qdict_set_default_str(qdict, BDRV_OPT_CACHE_NO_FLUSH, "off");
330da668aa1SThomas Huth
331da668aa1SThomas Huth blk = blk_new_open(NULL, NULL, qdict, BDRV_O_RDWR, &error_abort);
332da668aa1SThomas Huth assert(blk);
333da668aa1SThomas Huth monitor_add_blk(blk, S_ID, &error_abort);
334da668aa1SThomas Huth
335da668aa1SThomas Huth qemu_opts_del(opts);
336da668aa1SThomas Huth
337da668aa1SThomas Huth return blk;
338da668aa1SThomas Huth }
339da668aa1SThomas Huth
teardown_secondary(void)340da668aa1SThomas Huth static void teardown_secondary(void)
341da668aa1SThomas Huth {
342da668aa1SThomas Huth /* only need to destroy two BBs */
343da668aa1SThomas Huth BlockBackend *blk;
344da668aa1SThomas Huth
345da668aa1SThomas Huth /* remove S_LOCAL_DISK_ID */
346da668aa1SThomas Huth blk = blk_by_name(S_LOCAL_DISK_ID);
347da668aa1SThomas Huth assert(blk);
348da668aa1SThomas Huth
349da668aa1SThomas Huth monitor_remove_blk(blk);
350da668aa1SThomas Huth blk_unref(blk);
351da668aa1SThomas Huth
352da668aa1SThomas Huth /* remove S_ID */
353da668aa1SThomas Huth blk = blk_by_name(S_ID);
354da668aa1SThomas Huth assert(blk);
355da668aa1SThomas Huth
356da668aa1SThomas Huth monitor_remove_blk(blk);
357da668aa1SThomas Huth blk_unref(blk);
358da668aa1SThomas Huth }
359da668aa1SThomas Huth
test_secondary_read(void)360da668aa1SThomas Huth static void test_secondary_read(void)
361da668aa1SThomas Huth {
362da668aa1SThomas Huth BlockBackend *blk;
363da668aa1SThomas Huth
364da668aa1SThomas Huth blk = start_secondary();
365da668aa1SThomas Huth
366da668aa1SThomas Huth /* read from 0 to IMG_SIZE */
367da668aa1SThomas Huth test_blk_read(blk, 0, 0, IMG_SIZE, 0, IMG_SIZE, true);
368da668aa1SThomas Huth
369da668aa1SThomas Huth teardown_secondary();
370da668aa1SThomas Huth }
371da668aa1SThomas Huth
test_secondary_write(void)372da668aa1SThomas Huth static void test_secondary_write(void)
373da668aa1SThomas Huth {
374da668aa1SThomas Huth BlockBackend *blk;
375da668aa1SThomas Huth
376da668aa1SThomas Huth blk = start_secondary();
377da668aa1SThomas Huth
378da668aa1SThomas Huth /* write from 0 to IMG_SIZE */
379da668aa1SThomas Huth test_blk_write(blk, 0, 0, IMG_SIZE, true);
380da668aa1SThomas Huth
381da668aa1SThomas Huth teardown_secondary();
382da668aa1SThomas Huth }
383da668aa1SThomas Huth
384da668aa1SThomas Huth #ifndef _WIN32
test_secondary_start(void)385da668aa1SThomas Huth static void test_secondary_start(void)
386da668aa1SThomas Huth {
387da668aa1SThomas Huth BlockBackend *top_blk, *local_blk;
388da668aa1SThomas Huth bool failover = true;
389da668aa1SThomas Huth
390da668aa1SThomas Huth top_blk = start_secondary();
391da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_SECONDARY, &error_abort);
392da668aa1SThomas Huth
393da668aa1SThomas Huth /* read from s_local_disk (0, IMG_SIZE) */
394da668aa1SThomas Huth test_blk_read(top_blk, 0x11, 0, IMG_SIZE, 0, IMG_SIZE, false);
395da668aa1SThomas Huth
396da668aa1SThomas Huth /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
397da668aa1SThomas Huth local_blk = blk_by_name(S_LOCAL_DISK_ID);
398da668aa1SThomas Huth test_blk_write(local_blk, 0x22, IMG_SIZE / 2, IMG_SIZE / 2, false);
399da668aa1SThomas Huth
400da668aa1SThomas Huth /* replication will backup s_local_disk to s_hidden_disk */
401da668aa1SThomas Huth test_blk_read(top_blk, 0x11, IMG_SIZE / 2,
402da668aa1SThomas Huth IMG_SIZE / 2, 0, IMG_SIZE, false);
403da668aa1SThomas Huth
404da668aa1SThomas Huth /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
405da668aa1SThomas Huth test_blk_write(top_blk, 0x33, 0, IMG_SIZE / 2, false);
406da668aa1SThomas Huth
407da668aa1SThomas Huth /* read from s_active_disk (0, IMG_SIZE/2) */
408da668aa1SThomas Huth test_blk_read(top_blk, 0x33, 0, IMG_SIZE / 2,
409da668aa1SThomas Huth 0, IMG_SIZE / 2, false);
410da668aa1SThomas Huth
411da668aa1SThomas Huth /* unblock top_bs */
412da668aa1SThomas Huth replication_stop_all(failover, &error_abort);
413da668aa1SThomas Huth
414da668aa1SThomas Huth teardown_secondary();
415da668aa1SThomas Huth }
416da668aa1SThomas Huth
417da668aa1SThomas Huth
test_secondary_stop(void)418da668aa1SThomas Huth static void test_secondary_stop(void)
419da668aa1SThomas Huth {
420da668aa1SThomas Huth BlockBackend *top_blk, *local_blk;
421da668aa1SThomas Huth bool failover = true;
422da668aa1SThomas Huth
423da668aa1SThomas Huth top_blk = start_secondary();
424da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_SECONDARY, &error_abort);
425da668aa1SThomas Huth
426da668aa1SThomas Huth /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
427da668aa1SThomas Huth local_blk = blk_by_name(S_LOCAL_DISK_ID);
428da668aa1SThomas Huth test_blk_write(local_blk, 0x22, IMG_SIZE / 2, IMG_SIZE / 2, false);
429da668aa1SThomas Huth
430da668aa1SThomas Huth /* replication will backup s_local_disk to s_hidden_disk */
431da668aa1SThomas Huth test_blk_read(top_blk, 0x11, IMG_SIZE / 2,
432da668aa1SThomas Huth IMG_SIZE / 2, 0, IMG_SIZE, false);
433da668aa1SThomas Huth
434da668aa1SThomas Huth /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
435da668aa1SThomas Huth test_blk_write(top_blk, 0x33, 0, IMG_SIZE / 2, false);
436da668aa1SThomas Huth
437da668aa1SThomas Huth /* do active commit */
438da668aa1SThomas Huth replication_stop_all(failover, &error_abort);
439da668aa1SThomas Huth
440da668aa1SThomas Huth /* read from s_local_disk (0, IMG_SIZE / 2) */
441da668aa1SThomas Huth test_blk_read(top_blk, 0x33, 0, IMG_SIZE / 2,
442da668aa1SThomas Huth 0, IMG_SIZE / 2, false);
443da668aa1SThomas Huth
444da668aa1SThomas Huth
445da668aa1SThomas Huth /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
446da668aa1SThomas Huth test_blk_read(top_blk, 0x22, IMG_SIZE / 2,
447da668aa1SThomas Huth IMG_SIZE / 2, 0, IMG_SIZE, false);
448da668aa1SThomas Huth
449da668aa1SThomas Huth teardown_secondary();
450da668aa1SThomas Huth }
451da668aa1SThomas Huth
test_secondary_continuous_replication(void)452da668aa1SThomas Huth static void test_secondary_continuous_replication(void)
453da668aa1SThomas Huth {
454da668aa1SThomas Huth BlockBackend *top_blk, *local_blk;
455da668aa1SThomas Huth
456da668aa1SThomas Huth top_blk = start_secondary();
457da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_SECONDARY, &error_abort);
458da668aa1SThomas Huth
459da668aa1SThomas Huth /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
460da668aa1SThomas Huth local_blk = blk_by_name(S_LOCAL_DISK_ID);
461da668aa1SThomas Huth test_blk_write(local_blk, 0x22, IMG_SIZE / 2, IMG_SIZE / 2, false);
462da668aa1SThomas Huth
463da668aa1SThomas Huth /* replication will backup s_local_disk to s_hidden_disk */
464da668aa1SThomas Huth test_blk_read(top_blk, 0x11, IMG_SIZE / 2,
465da668aa1SThomas Huth IMG_SIZE / 2, 0, IMG_SIZE, false);
466da668aa1SThomas Huth
467da668aa1SThomas Huth /* write 0x33 to s_active_disk (0, IMG_SIZE / 2) */
468da668aa1SThomas Huth test_blk_write(top_blk, 0x33, 0, IMG_SIZE / 2, false);
469da668aa1SThomas Huth
470da668aa1SThomas Huth /* do failover (active commit) */
471da668aa1SThomas Huth replication_stop_all(true, &error_abort);
472da668aa1SThomas Huth
473da668aa1SThomas Huth /* it should ignore all requests from now on */
474da668aa1SThomas Huth
475da668aa1SThomas Huth /* start after failover */
476da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_PRIMARY, &error_abort);
477da668aa1SThomas Huth
478da668aa1SThomas Huth /* checkpoint */
479da668aa1SThomas Huth replication_do_checkpoint_all(&error_abort);
480da668aa1SThomas Huth
481da668aa1SThomas Huth /* stop */
482da668aa1SThomas Huth replication_stop_all(true, &error_abort);
483da668aa1SThomas Huth
484da668aa1SThomas Huth /* read from s_local_disk (0, IMG_SIZE / 2) */
485da668aa1SThomas Huth test_blk_read(top_blk, 0x33, 0, IMG_SIZE / 2,
486da668aa1SThomas Huth 0, IMG_SIZE / 2, false);
487da668aa1SThomas Huth
488da668aa1SThomas Huth
489da668aa1SThomas Huth /* read from s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
490da668aa1SThomas Huth test_blk_read(top_blk, 0x22, IMG_SIZE / 2,
491da668aa1SThomas Huth IMG_SIZE / 2, 0, IMG_SIZE, false);
492da668aa1SThomas Huth
493da668aa1SThomas Huth teardown_secondary();
494da668aa1SThomas Huth }
495da668aa1SThomas Huth
test_secondary_do_checkpoint(void)496da668aa1SThomas Huth static void test_secondary_do_checkpoint(void)
497da668aa1SThomas Huth {
498da668aa1SThomas Huth BlockBackend *top_blk, *local_blk;
499da668aa1SThomas Huth bool failover = true;
500da668aa1SThomas Huth
501da668aa1SThomas Huth top_blk = start_secondary();
502da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_SECONDARY, &error_abort);
503da668aa1SThomas Huth
504da668aa1SThomas Huth /* write 0x22 to s_local_disk (IMG_SIZE / 2, IMG_SIZE) */
505da668aa1SThomas Huth local_blk = blk_by_name(S_LOCAL_DISK_ID);
506da668aa1SThomas Huth test_blk_write(local_blk, 0x22, IMG_SIZE / 2,
507da668aa1SThomas Huth IMG_SIZE / 2, false);
508da668aa1SThomas Huth
509da668aa1SThomas Huth /* replication will backup s_local_disk to s_hidden_disk */
510da668aa1SThomas Huth test_blk_read(top_blk, 0x11, IMG_SIZE / 2,
511da668aa1SThomas Huth IMG_SIZE / 2, 0, IMG_SIZE, false);
512da668aa1SThomas Huth
513da668aa1SThomas Huth replication_do_checkpoint_all(&error_abort);
514da668aa1SThomas Huth
515da668aa1SThomas Huth /* after checkpoint, read pattern 0x22 from s_local_disk */
516da668aa1SThomas Huth test_blk_read(top_blk, 0x22, IMG_SIZE / 2,
517da668aa1SThomas Huth IMG_SIZE / 2, 0, IMG_SIZE, false);
518da668aa1SThomas Huth
519da668aa1SThomas Huth /* unblock top_bs */
520da668aa1SThomas Huth replication_stop_all(failover, &error_abort);
521da668aa1SThomas Huth
522da668aa1SThomas Huth teardown_secondary();
523da668aa1SThomas Huth }
524da668aa1SThomas Huth
test_secondary_get_error_all(void)525da668aa1SThomas Huth static void test_secondary_get_error_all(void)
526da668aa1SThomas Huth {
527da668aa1SThomas Huth bool failover = true;
528da668aa1SThomas Huth
529da668aa1SThomas Huth start_secondary();
530da668aa1SThomas Huth replication_start_all(REPLICATION_MODE_SECONDARY, &error_abort);
531da668aa1SThomas Huth
532da668aa1SThomas Huth replication_get_error_all(&error_abort);
533da668aa1SThomas Huth
534da668aa1SThomas Huth /* unblock top_bs */
535da668aa1SThomas Huth replication_stop_all(failover, &error_abort);
536da668aa1SThomas Huth
537da668aa1SThomas Huth teardown_secondary();
538da668aa1SThomas Huth }
539da668aa1SThomas Huth #endif
540da668aa1SThomas Huth
sigabrt_handler(int signo)541da668aa1SThomas Huth static void sigabrt_handler(int signo)
542da668aa1SThomas Huth {
543da668aa1SThomas Huth cleanup_imgs();
544da668aa1SThomas Huth }
545da668aa1SThomas Huth
setup_sigabrt_handler(void)546da668aa1SThomas Huth static void setup_sigabrt_handler(void)
547da668aa1SThomas Huth {
548da668aa1SThomas Huth #ifdef _WIN32
549da668aa1SThomas Huth signal(SIGABRT, sigabrt_handler);
550da668aa1SThomas Huth #else
551da668aa1SThomas Huth struct sigaction sigact;
552da668aa1SThomas Huth
553da668aa1SThomas Huth sigact = (struct sigaction) {
554da668aa1SThomas Huth .sa_handler = sigabrt_handler,
555da668aa1SThomas Huth .sa_flags = SA_RESETHAND,
556da668aa1SThomas Huth };
557da668aa1SThomas Huth sigemptyset(&sigact.sa_mask);
558da668aa1SThomas Huth sigaction(SIGABRT, &sigact, NULL);
559da668aa1SThomas Huth #endif
560da668aa1SThomas Huth }
561da668aa1SThomas Huth
main(int argc,char ** argv)562da668aa1SThomas Huth int main(int argc, char **argv)
563da668aa1SThomas Huth {
564da668aa1SThomas Huth int ret;
565da668aa1SThomas Huth const char *tmpdir = g_get_tmp_dir();
566da668aa1SThomas Huth p_local_disk = g_strdup_printf("%s/p_local_disk.XXXXXX", tmpdir);
567da668aa1SThomas Huth s_local_disk = g_strdup_printf("%s/s_local_disk.XXXXXX", tmpdir);
568da668aa1SThomas Huth s_active_disk = g_strdup_printf("%s/s_active_disk.XXXXXX", tmpdir);
569da668aa1SThomas Huth s_hidden_disk = g_strdup_printf("%s/s_hidden_disk.XXXXXX", tmpdir);
570da668aa1SThomas Huth qemu_init_main_loop(&error_fatal);
571da668aa1SThomas Huth bdrv_init();
572da668aa1SThomas Huth
573da668aa1SThomas Huth g_test_init(&argc, &argv, NULL);
574da668aa1SThomas Huth setup_sigabrt_handler();
575da668aa1SThomas Huth
576da668aa1SThomas Huth prepare_imgs();
577da668aa1SThomas Huth
578da668aa1SThomas Huth /* Primary */
579da668aa1SThomas Huth g_test_add_func("/replication/primary/read", test_primary_read);
580da668aa1SThomas Huth g_test_add_func("/replication/primary/write", test_primary_write);
581da668aa1SThomas Huth g_test_add_func("/replication/primary/start", test_primary_start);
582da668aa1SThomas Huth g_test_add_func("/replication/primary/stop", test_primary_stop);
583da668aa1SThomas Huth g_test_add_func("/replication/primary/do_checkpoint",
584da668aa1SThomas Huth test_primary_do_checkpoint);
585da668aa1SThomas Huth g_test_add_func("/replication/primary/get_error_all",
586da668aa1SThomas Huth test_primary_get_error_all);
587da668aa1SThomas Huth
588da668aa1SThomas Huth /* Secondary */
589da668aa1SThomas Huth g_test_add_func("/replication/secondary/read", test_secondary_read);
590da668aa1SThomas Huth g_test_add_func("/replication/secondary/write", test_secondary_write);
591da668aa1SThomas Huth #ifndef _WIN32
592da668aa1SThomas Huth g_test_add_func("/replication/secondary/start", test_secondary_start);
593da668aa1SThomas Huth g_test_add_func("/replication/secondary/stop", test_secondary_stop);
594da668aa1SThomas Huth g_test_add_func("/replication/secondary/continuous_replication",
595da668aa1SThomas Huth test_secondary_continuous_replication);
596da668aa1SThomas Huth g_test_add_func("/replication/secondary/do_checkpoint",
597da668aa1SThomas Huth test_secondary_do_checkpoint);
598da668aa1SThomas Huth g_test_add_func("/replication/secondary/get_error_all",
599da668aa1SThomas Huth test_secondary_get_error_all);
600da668aa1SThomas Huth #endif
601da668aa1SThomas Huth
602da668aa1SThomas Huth ret = g_test_run();
603da668aa1SThomas Huth
604da668aa1SThomas Huth cleanup_imgs();
605da668aa1SThomas Huth
606da668aa1SThomas Huth g_free(p_local_disk);
607da668aa1SThomas Huth g_free(s_local_disk);
608da668aa1SThomas Huth g_free(s_active_disk);
609da668aa1SThomas Huth g_free(s_hidden_disk);
610da668aa1SThomas Huth
611da668aa1SThomas Huth return ret;
612da668aa1SThomas Huth }
613