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