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