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