2009-11-02 14:40:58 +01:00
|
|
|
/*
|
|
|
|
* QEMU live block migration
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2009
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Liran Schour <lirans@il.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "block_int.h"
|
|
|
|
#include "hw/hw.h"
|
2009-11-30 18:21:20 +01:00
|
|
|
#include "qemu-queue.h"
|
2009-11-30 18:21:21 +01:00
|
|
|
#include "monitor.h"
|
2009-11-02 14:40:58 +01:00
|
|
|
#include "block-migration.h"
|
|
|
|
#include <assert.h>
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
#define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
|
2009-11-02 14:40:58 +01:00
|
|
|
|
|
|
|
#define BLK_MIG_FLAG_DEVICE_BLOCK 0x01
|
|
|
|
#define BLK_MIG_FLAG_EOS 0x02
|
2009-12-01 15:20:17 +01:00
|
|
|
#define BLK_MIG_FLAG_PROGRESS 0x04
|
2009-11-02 14:40:58 +01:00
|
|
|
|
|
|
|
#define MAX_IS_ALLOCATED_SEARCH 65536
|
|
|
|
#define MAX_BLOCKS_READ 10000
|
|
|
|
#define BLOCKS_READ_CHANGE 100
|
|
|
|
#define INITIAL_BLOCKS_READ 100
|
|
|
|
|
|
|
|
//#define DEBUG_BLK_MIGRATION
|
|
|
|
|
|
|
|
#ifdef DEBUG_BLK_MIGRATION
|
2010-02-07 00:03:50 +01:00
|
|
|
#define DPRINTF(fmt, ...) \
|
2009-11-02 14:40:58 +01:00
|
|
|
do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
|
|
|
|
#else
|
2010-02-07 00:03:50 +01:00
|
|
|
#define DPRINTF(fmt, ...) \
|
2009-11-02 14:40:58 +01:00
|
|
|
do { } while (0)
|
|
|
|
#endif
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
typedef struct BlkMigDevState {
|
|
|
|
BlockDriverState *bs;
|
|
|
|
int bulk_completed;
|
|
|
|
int shared_base;
|
|
|
|
int64_t cur_sector;
|
2009-11-30 18:21:21 +01:00
|
|
|
int64_t completed_sectors;
|
2009-11-30 18:21:19 +01:00
|
|
|
int64_t total_sectors;
|
|
|
|
int64_t dirty;
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_ENTRY(BlkMigDevState) entry;
|
2009-11-30 18:21:19 +01:00
|
|
|
} BlkMigDevState;
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
typedef struct BlkMigBlock {
|
|
|
|
uint8_t *buf;
|
|
|
|
BlkMigDevState *bmds;
|
|
|
|
int64_t sector;
|
|
|
|
struct iovec iov;
|
|
|
|
QEMUIOVector qiov;
|
|
|
|
BlockDriverAIOCB *aiocb;
|
|
|
|
int ret;
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_ENTRY(BlkMigBlock) entry;
|
2009-11-02 14:40:58 +01:00
|
|
|
} BlkMigBlock;
|
|
|
|
|
|
|
|
typedef struct BlkMigState {
|
|
|
|
int blk_enable;
|
|
|
|
int shared_base;
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
|
|
|
|
QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
|
2009-11-02 14:40:58 +01:00
|
|
|
int submitted;
|
|
|
|
int read_done;
|
|
|
|
int transferred;
|
2009-11-30 18:21:21 +01:00
|
|
|
int64_t total_sector_sum;
|
2009-12-01 15:20:17 +01:00
|
|
|
int prev_progress;
|
2009-11-02 14:40:58 +01:00
|
|
|
} BlkMigState;
|
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
static BlkMigState block_mig_state;
|
2009-11-02 14:40:58 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
static void blk_send(QEMUFile *f, BlkMigBlock * blk)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* sector number and flags */
|
|
|
|
qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
|
|
|
|
| BLK_MIG_FLAG_DEVICE_BLOCK);
|
|
|
|
|
|
|
|
/* device name */
|
|
|
|
len = strlen(blk->bmds->bs->device_name);
|
|
|
|
qemu_put_byte(f, len);
|
|
|
|
qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
|
|
|
|
|
|
|
|
qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
int blk_mig_active(void)
|
|
|
|
{
|
|
|
|
return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t blk_mig_bytes_transferred(void)
|
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
|
|
|
uint64_t sum = 0;
|
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
|
|
|
sum += bmds->completed_sectors;
|
|
|
|
}
|
|
|
|
return sum << BDRV_SECTOR_BITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t blk_mig_bytes_remaining(void)
|
|
|
|
{
|
|
|
|
return blk_mig_bytes_total() - blk_mig_bytes_transferred();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t blk_mig_bytes_total(void)
|
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
|
|
|
uint64_t sum = 0;
|
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
|
|
|
sum += bmds->total_sectors;
|
|
|
|
}
|
|
|
|
return sum << BDRV_SECTOR_BITS;
|
|
|
|
}
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
static void blk_mig_read_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
BlkMigBlock *blk = opaque;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
blk->ret = ret;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.submitted--;
|
|
|
|
block_mig_state.read_done++;
|
|
|
|
assert(block_mig_state.submitted >= 0);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
static int mig_save_device_bulk(Monitor *mon, QEMUFile *f,
|
|
|
|
BlkMigDevState *bmds, int is_async)
|
2009-11-30 18:21:19 +01:00
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
int64_t total_sectors = bmds->total_sectors;
|
|
|
|
int64_t cur_sector = bmds->cur_sector;
|
|
|
|
BlockDriverState *bs = bmds->bs;
|
2009-11-02 14:40:58 +01:00
|
|
|
BlkMigBlock *blk;
|
2009-11-30 18:21:21 +01:00
|
|
|
int nr_sectors;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
if (bmds->shared_base) {
|
2009-11-30 18:21:20 +01:00
|
|
|
while (cur_sector < total_sectors &&
|
2009-11-30 18:21:20 +01:00
|
|
|
!bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
|
|
|
|
&nr_sectors)) {
|
2009-11-02 14:40:58 +01:00
|
|
|
cur_sector += nr_sectors;
|
|
|
|
}
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
|
|
|
if (cur_sector >= total_sectors) {
|
2009-11-30 18:21:21 +01:00
|
|
|
bmds->cur_sector = bmds->completed_sectors = total_sectors;
|
2009-11-02 14:40:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
bmds->completed_sectors = cur_sector;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
/* we are going to transfer a full block even if it is not allocated */
|
|
|
|
nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
|
2009-11-02 14:40:58 +01:00
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
|
2009-11-30 18:21:20 +01:00
|
|
|
nr_sectors = total_sectors - cur_sector;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
blk = qemu_malloc(sizeof(BlkMigBlock));
|
|
|
|
blk->buf = qemu_malloc(BLOCK_SIZE);
|
|
|
|
blk->bmds = bmds;
|
|
|
|
blk->sector = cur_sector;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
if (is_async) {
|
2009-11-30 18:21:20 +01:00
|
|
|
blk->iov.iov_base = blk->buf;
|
|
|
|
blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
|
|
|
|
qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
|
|
|
|
nr_sectors, blk_mig_read_cb, blk);
|
|
|
|
if (!blk->aiocb) {
|
2009-11-30 18:21:21 +01:00
|
|
|
goto error;
|
2009-11-30 18:21:20 +01:00
|
|
|
}
|
|
|
|
block_mig_state.submitted++;
|
|
|
|
} else {
|
2009-11-30 18:21:21 +01:00
|
|
|
if (bdrv_read(bs, cur_sector, blk->buf, nr_sectors) < 0) {
|
2009-11-30 18:21:21 +01:00
|
|
|
goto error;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:21 +01:00
|
|
|
blk_send(f, blk);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
qemu_free(blk->buf);
|
|
|
|
qemu_free(blk);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
bdrv_reset_dirty(bs, cur_sector, nr_sectors);
|
|
|
|
bmds->cur_sector = cur_sector + nr_sectors;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
return (bmds->cur_sector >= total_sectors);
|
2009-11-30 18:21:21 +01:00
|
|
|
|
|
|
|
error:
|
2009-11-30 18:21:21 +01:00
|
|
|
monitor_printf(mon, "Error reading sector %" PRId64 "\n", cur_sector);
|
2009-11-30 18:21:21 +01:00
|
|
|
qemu_file_set_error(f);
|
|
|
|
qemu_free(blk->buf);
|
|
|
|
qemu_free(blk);
|
|
|
|
return 0;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void set_dirty_tracking(int enable)
|
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
2009-11-30 18:21:20 +01:00
|
|
|
|
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
2009-11-30 18:21:19 +01:00
|
|
|
bdrv_set_dirty_tracking(bmds->bs, enable);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
static void init_blk_migration(Monitor *mon, QEMUFile *f)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
BlkMigDevState *bmds;
|
2009-11-02 14:40:58 +01:00
|
|
|
BlockDriverState *bs;
|
2009-11-30 20:34:55 +01:00
|
|
|
int64_t sectors;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.submitted = 0;
|
|
|
|
block_mig_state.read_done = 0;
|
|
|
|
block_mig_state.transferred = 0;
|
2009-11-30 18:21:21 +01:00
|
|
|
block_mig_state.total_sector_sum = 0;
|
2009-12-01 15:20:17 +01:00
|
|
|
block_mig_state.prev_progress = -1;
|
2009-11-30 18:21:20 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
for (bs = bdrv_first; bs != NULL; bs = bs->next) {
|
2009-11-30 18:21:19 +01:00
|
|
|
if (bs->type == BDRV_TYPE_HD) {
|
2009-11-30 20:34:55 +01:00
|
|
|
sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
|
|
|
|
if (sectors == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
bmds = qemu_mallocz(sizeof(BlkMigDevState));
|
|
|
|
bmds->bs = bs;
|
|
|
|
bmds->bulk_completed = 0;
|
2009-11-30 20:34:55 +01:00
|
|
|
bmds->total_sectors = sectors;
|
2009-11-30 18:21:21 +01:00
|
|
|
bmds->completed_sectors = 0;
|
2009-11-30 18:21:20 +01:00
|
|
|
bmds->shared_base = block_mig_state.shared_base;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 20:34:55 +01:00
|
|
|
block_mig_state.total_sector_sum += sectors;
|
2009-11-30 18:21:21 +01:00
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
if (bmds->shared_base) {
|
2009-11-30 18:21:21 +01:00
|
|
|
monitor_printf(mon, "Start migration for %s with shared base "
|
|
|
|
"image\n",
|
|
|
|
bs->device_name);
|
2009-11-02 14:40:58 +01:00
|
|
|
} else {
|
2009-11-30 18:21:21 +01:00
|
|
|
monitor_printf(mon, "Start full migration for %s\n",
|
|
|
|
bs->device_name);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
}
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
static int blk_mig_save_bulked_block(Monitor *mon, QEMUFile *f, int is_async)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
2009-11-30 18:21:21 +01:00
|
|
|
int64_t completed_sector_sum = 0;
|
2009-11-02 14:40:58 +01:00
|
|
|
BlkMigDevState *bmds;
|
2009-12-01 15:20:17 +01:00
|
|
|
int progress;
|
2009-11-30 18:21:21 +01:00
|
|
|
int ret = 0;
|
2009-11-02 14:40:58 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
2009-11-30 18:21:19 +01:00
|
|
|
if (bmds->bulk_completed == 0) {
|
2009-11-30 18:21:21 +01:00
|
|
|
if (mig_save_device_bulk(mon, f, bmds, is_async) == 1) {
|
2009-11-30 18:21:20 +01:00
|
|
|
/* completed bulk section for this device */
|
|
|
|
bmds->bulk_completed = 1;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:21 +01:00
|
|
|
completed_sector_sum += bmds->completed_sectors;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
completed_sector_sum += bmds->completed_sectors;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-12-01 15:20:17 +01:00
|
|
|
progress = completed_sector_sum * 100 / block_mig_state.total_sector_sum;
|
|
|
|
if (progress != block_mig_state.prev_progress) {
|
|
|
|
block_mig_state.prev_progress = progress;
|
|
|
|
qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
|
|
|
|
| BLK_MIG_FLAG_PROGRESS);
|
|
|
|
monitor_printf(mon, "Completed %d %%\r", progress);
|
2009-11-30 18:21:21 +01:00
|
|
|
monitor_flush(mon);
|
2009-11-30 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_NUM_BLOCKS 4
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
static void blk_mig_save_dirty_blocks(Monitor *mon, QEMUFile *f)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
2009-11-30 18:21:21 +01:00
|
|
|
BlkMigBlock blk;
|
2009-11-02 14:40:58 +01:00
|
|
|
int64_t sector;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
blk.buf = qemu_malloc(BLOCK_SIZE);
|
2009-11-30 18:21:20 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
2009-11-30 18:21:19 +01:00
|
|
|
for (sector = 0; sector < bmds->cur_sector;) {
|
|
|
|
if (bdrv_get_dirty(bmds->bs, sector)) {
|
2009-11-30 18:21:21 +01:00
|
|
|
if (bdrv_read(bmds->bs, sector, blk.buf,
|
2009-11-30 18:21:19 +01:00
|
|
|
BDRV_SECTORS_PER_DIRTY_CHUNK) < 0) {
|
2009-11-30 18:21:21 +01:00
|
|
|
monitor_printf(mon, "Error reading sector %" PRId64 "\n",
|
|
|
|
sector);
|
2009-11-30 18:21:21 +01:00
|
|
|
qemu_file_set_error(f);
|
|
|
|
qemu_free(blk.buf);
|
|
|
|
return;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:21 +01:00
|
|
|
blk.bmds = bmds;
|
|
|
|
blk.sector = sector;
|
|
|
|
blk_send(f, &blk);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
|
|
|
bdrv_reset_dirty(bmds->bs, sector,
|
2009-11-30 18:21:19 +01:00
|
|
|
BDRV_SECTORS_PER_DIRTY_CHUNK);
|
2009-11-30 18:21:19 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
}
|
2009-11-30 18:21:20 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
qemu_free(blk.buf);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void flush_blks(QEMUFile* f)
|
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
BlkMigBlock *blk;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2010-02-07 00:03:50 +01:00
|
|
|
DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
|
2009-11-30 18:21:20 +01:00
|
|
|
__FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
|
|
|
|
block_mig_state.transferred);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
|
|
|
|
if (qemu_file_rate_limit(f)) {
|
|
|
|
break;
|
|
|
|
}
|
2009-11-30 18:21:21 +01:00
|
|
|
if (blk->ret < 0) {
|
|
|
|
qemu_file_set_error(f);
|
|
|
|
break;
|
|
|
|
}
|
2009-11-30 18:21:21 +01:00
|
|
|
blk_send(f, blk);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
|
2009-11-02 14:40:58 +01:00
|
|
|
qemu_free(blk->buf);
|
|
|
|
qemu_free(blk);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.read_done--;
|
|
|
|
block_mig_state.transferred++;
|
|
|
|
assert(block_mig_state.read_done >= 0);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
2010-02-07 00:03:50 +01:00
|
|
|
DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.submitted, block_mig_state.read_done,
|
|
|
|
block_mig_state.transferred);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int is_stage2_completed(void)
|
|
|
|
{
|
|
|
|
BlkMigDevState *bmds;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
if (block_mig_state.submitted > 0) {
|
2009-11-02 14:40:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
2009-11-30 18:21:19 +01:00
|
|
|
if (bmds->bulk_completed == 0) {
|
2009-11-02 14:40:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
static void blk_mig_cleanup(Monitor *mon)
|
2009-11-30 18:21:21 +01:00
|
|
|
{
|
2009-11-30 18:21:21 +01:00
|
|
|
BlkMigDevState *bmds;
|
|
|
|
BlkMigBlock *blk;
|
2009-11-30 18:21:21 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
|
2009-11-30 18:21:21 +01:00
|
|
|
qemu_free(bmds);
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
|
|
|
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
|
2009-11-30 18:21:21 +01:00
|
|
|
qemu_free(blk->buf);
|
|
|
|
qemu_free(blk);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_dirty_tracking(0);
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
monitor_printf(mon, "\n");
|
2009-11-30 18:21:21 +01:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
|
2009-11-02 14:40:58 +01:00
|
|
|
{
|
2010-02-07 00:03:50 +01:00
|
|
|
DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
|
2009-11-30 18:21:20 +01:00
|
|
|
stage, block_mig_state.submitted, block_mig_state.transferred);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
if (stage < 0) {
|
2009-11-30 18:21:21 +01:00
|
|
|
blk_mig_cleanup(mon);
|
2009-11-30 18:21:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
if (block_mig_state.blk_enable != 1) {
|
2009-11-02 14:40:58 +01:00
|
|
|
/* no need to migrate storage */
|
2009-11-30 18:21:19 +01:00
|
|
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
2009-11-02 14:40:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
|
|
|
if (stage == 1) {
|
2009-11-30 18:21:21 +01:00
|
|
|
init_blk_migration(mon, f);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
/* start track dirty blocks */
|
|
|
|
set_dirty_tracking(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_blks(f);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
if (qemu_file_has_error(f)) {
|
2009-11-30 18:21:21 +01:00
|
|
|
blk_mig_cleanup(mon);
|
2009-11-30 18:21:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
/* control the rate of transfer */
|
2009-11-30 18:21:20 +01:00
|
|
|
while ((block_mig_state.submitted +
|
|
|
|
block_mig_state.read_done) * BLOCK_SIZE <
|
2009-11-30 18:21:19 +01:00
|
|
|
qemu_file_get_rate_limit(f)) {
|
2009-11-30 18:21:21 +01:00
|
|
|
if (blk_mig_save_bulked_block(mon, f, 1) == 0) {
|
2009-11-30 18:21:19 +01:00
|
|
|
/* no more bulk blocks for now */
|
2009-11-02 14:40:58 +01:00
|
|
|
break;
|
2009-11-30 18:21:19 +01:00
|
|
|
}
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
flush_blks(f);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
if (qemu_file_has_error(f)) {
|
2009-11-30 18:21:21 +01:00
|
|
|
blk_mig_cleanup(mon);
|
2009-11-30 18:21:21 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
if (stage == 3) {
|
2009-11-30 18:21:21 +01:00
|
|
|
while (blk_mig_save_bulked_block(mon, f, 0) != 0) {
|
2009-11-30 18:21:19 +01:00
|
|
|
/* empty */
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
blk_mig_save_dirty_blocks(mon, f);
|
|
|
|
blk_mig_cleanup(mon);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-12-01 15:20:17 +01:00
|
|
|
/* report completion */
|
|
|
|
qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
if (qemu_file_has_error(f)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-30 18:21:21 +01:00
|
|
|
monitor_printf(mon, "Block migration completed\n");
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
|
|
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
return ((stage == 2) && is_stage2_completed());
|
|
|
|
}
|
|
|
|
|
|
|
|
static int block_load(QEMUFile *f, void *opaque, int version_id)
|
|
|
|
{
|
2009-12-01 15:20:17 +01:00
|
|
|
static int banner_printed;
|
2009-11-02 14:40:58 +01:00
|
|
|
int len, flags;
|
|
|
|
char device_name[256];
|
|
|
|
int64_t addr;
|
|
|
|
BlockDriverState *bs;
|
|
|
|
uint8_t *buf;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
do {
|
|
|
|
addr = qemu_get_be64(f);
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
flags = addr & ~BDRV_SECTOR_MASK;
|
|
|
|
addr >>= BDRV_SECTOR_BITS;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
|
|
|
if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
|
2009-11-02 14:40:58 +01:00
|
|
|
/* get device name */
|
|
|
|
len = qemu_get_byte(f);
|
|
|
|
qemu_get_buffer(f, (uint8_t *)device_name, len);
|
|
|
|
device_name[len] = '\0';
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
bs = bdrv_find(device_name);
|
2009-11-30 18:21:21 +01:00
|
|
|
if (!bs) {
|
|
|
|
fprintf(stderr, "Error unknown block device %s\n",
|
|
|
|
device_name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-30 18:21:20 +01:00
|
|
|
buf = qemu_malloc(BLOCK_SIZE);
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
qemu_get_buffer(f, buf, BLOCK_SIZE);
|
2009-11-30 18:21:21 +01:00
|
|
|
bdrv_write(bs, addr, buf, BDRV_SECTORS_PER_DIRTY_CHUNK);
|
2009-11-30 18:21:20 +01:00
|
|
|
|
|
|
|
qemu_free(buf);
|
2009-12-01 15:20:17 +01:00
|
|
|
} else if (flags & BLK_MIG_FLAG_PROGRESS) {
|
|
|
|
if (!banner_printed) {
|
|
|
|
printf("Receiving block device images\n");
|
|
|
|
banner_printed = 1;
|
|
|
|
}
|
|
|
|
printf("Completed %d %%%c", (int)addr,
|
|
|
|
(addr == 100) ? '\n' : '\r');
|
|
|
|
fflush(stdout);
|
2009-11-30 18:21:19 +01:00
|
|
|
} else if (!(flags & BLK_MIG_FLAG_EOS)) {
|
2009-11-30 18:21:21 +01:00
|
|
|
fprintf(stderr, "Unknown flags\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (qemu_file_has_error(f)) {
|
|
|
|
return -EIO;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
2009-11-30 18:21:19 +01:00
|
|
|
} while (!(flags & BLK_MIG_FLAG_EOS));
|
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void block_set_params(int blk_enable, int shared_base, void *opaque)
|
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.blk_enable = blk_enable;
|
|
|
|
block_mig_state.shared_base = shared_base;
|
2009-11-30 18:21:19 +01:00
|
|
|
|
2009-11-02 14:40:58 +01:00
|
|
|
/* shared base means that blk_enable = 1 */
|
2009-11-30 18:21:20 +01:00
|
|
|
block_mig_state.blk_enable |= shared_base;
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void blk_mig_init(void)
|
2009-11-30 18:21:19 +01:00
|
|
|
{
|
2009-11-30 18:21:20 +01:00
|
|
|
QSIMPLEQ_INIT(&block_mig_state.bmds_list);
|
|
|
|
QSIMPLEQ_INIT(&block_mig_state.blk_list);
|
|
|
|
|
2009-11-30 18:21:19 +01:00
|
|
|
register_savevm_live("block", 0, 1, block_set_params, block_save_live,
|
2009-11-30 18:21:20 +01:00
|
|
|
NULL, block_load, &block_mig_state);
|
2009-11-02 14:40:58 +01:00
|
|
|
}
|