Statistics
| Branch: | Revision:

root / migration.c @ ef4b7eee

History | View | Annotate | Download (12 kB)

1
/*
2
 * QEMU live migration
3
 *
4
 * Copyright IBM, Corp. 2008
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10
 * the COPYING file in the top-level directory.
11
 *
12
 */
13

    
14
#include "qemu-common.h"
15
#include "migration.h"
16
#include "monitor.h"
17
#include "buffered_file.h"
18
#include "sysemu.h"
19
#include "block.h"
20
#include "qemu_socket.h"
21
#include "block-migration.h"
22
#include "qemu-objects.h"
23

    
24
//#define DEBUG_MIGRATION
25

    
26
#ifdef DEBUG_MIGRATION
27
#define DPRINTF(fmt, ...) \
28
    do { printf("migration: " fmt, ## __VA_ARGS__); } while (0)
29
#else
30
#define DPRINTF(fmt, ...) \
31
    do { } while (0)
32
#endif
33

    
34
/* Migration speed throttling */
35
static uint32_t max_throttle = (32 << 20);
36

    
37
static MigrationState *current_migration;
38

    
39
void qemu_start_incoming_migration(const char *uri)
40
{
41
    const char *p;
42

    
43
    if (strstart(uri, "tcp:", &p))
44
        tcp_start_incoming_migration(p);
45
#if !defined(WIN32)
46
    else if (strstart(uri, "exec:", &p))
47
        exec_start_incoming_migration(p);
48
    else if (strstart(uri, "unix:", &p))
49
        unix_start_incoming_migration(p);
50
    else if (strstart(uri, "fd:", &p))
51
        fd_start_incoming_migration(p);
52
#endif
53
    else
54
        fprintf(stderr, "unknown migration protocol: %s\n", uri);
55
}
56

    
57
void do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
58
{
59
    MigrationState *s = NULL;
60
    const char *p;
61
    int detach = qdict_get_int(qdict, "detach");
62
    const char *uri = qdict_get_str(qdict, "uri");
63

    
64
    if (current_migration &&
65
        current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
66
        monitor_printf(mon, "migration already in progress\n");
67
        return;
68
    }
69

    
70
    if (strstart(uri, "tcp:", &p))
71
        s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
72
                                         (int)qdict_get_int(qdict, "blk"), 
73
                                         (int)qdict_get_int(qdict, "inc"));
74
#if !defined(WIN32)
75
    else if (strstart(uri, "exec:", &p))
76
        s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
77
                                          (int)qdict_get_int(qdict, "blk"), 
78
                                          (int)qdict_get_int(qdict, "inc"));
79
    else if (strstart(uri, "unix:", &p))
80
        s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
81
                                          (int)qdict_get_int(qdict, "blk"), 
82
                                          (int)qdict_get_int(qdict, "inc"));
83
    else if (strstart(uri, "fd:", &p))
84
        s = fd_start_outgoing_migration(mon, p, max_throttle, detach, 
85
                                        (int)qdict_get_int(qdict, "blk"), 
86
                                        (int)qdict_get_int(qdict, "inc"));
87
#endif
88
    else
89
        monitor_printf(mon, "unknown migration protocol: %s\n", uri);
90

    
91
    if (s == NULL)
92
        monitor_printf(mon, "migration failed\n");
93
    else {
94
        if (current_migration)
95
            current_migration->release(current_migration);
96

    
97
        current_migration = s;
98
    }
99
}
100

    
101
int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
102
{
103
    MigrationState *s = current_migration;
104

    
105
    if (s)
106
        s->cancel(s);
107

    
108
    return 0;
109
}
110

    
111
int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
112
{
113
    double d;
114
    FdMigrationState *s;
115

    
116
    d = qdict_get_double(qdict, "value");
117
    d = MAX(0, MIN(UINT32_MAX, d));
118
    max_throttle = d;
119

    
120
    s = migrate_to_fms(current_migration);
121
    if (s && s->file) {
122
        qemu_file_set_rate_limit(s->file, max_throttle);
123
    }
124

    
125
    return 0;
126
}
127

    
128
/* amount of nanoseconds we are willing to wait for migration to be down.
129
 * the choice of nanoseconds is because it is the maximum resolution that
130
 * get_clock() can achieve. It is an internal measure. All user-visible
131
 * units must be in seconds */
132
static uint64_t max_downtime = 30000000;
133

    
134
uint64_t migrate_max_downtime(void)
135
{
136
    return max_downtime;
137
}
138

    
139
int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
140
                            QObject **ret_data)
141
{
142
    double d;
143

    
144
    d = qdict_get_double(qdict, "value") * 1e9;
145
    d = MAX(0, MIN(UINT64_MAX, d));
146
    max_downtime = (uint64_t)d;
147

    
148
    return 0;
149
}
150

    
151
static void migrate_print_status(Monitor *mon, const char *name,
152
                                 const QDict *status_dict)
153
{
154
    QDict *qdict;
155

    
156
    qdict = qobject_to_qdict(qdict_get(status_dict, name));
157

    
158
    monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
159
                        qdict_get_int(qdict, "transferred") >> 10);
160
    monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
161
                        qdict_get_int(qdict, "remaining") >> 10);
162
    monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
163
                        qdict_get_int(qdict, "total") >> 10);
164
}
165

    
166
void do_info_migrate_print(Monitor *mon, const QObject *data)
167
{
168
    QDict *qdict;
169

    
170
    qdict = qobject_to_qdict(data);
171

    
172
    monitor_printf(mon, "Migration status: %s\n",
173
                   qdict_get_str(qdict, "status"));
174

    
175
    if (qdict_haskey(qdict, "ram")) {
176
        migrate_print_status(mon, "ram", qdict);
177
    }
178

    
179
    if (qdict_haskey(qdict, "disk")) {
180
        migrate_print_status(mon, "disk", qdict);
181
    }
182
}
183

    
184
static void migrate_put_status(QDict *qdict, const char *name,
185
                               uint64_t trans, uint64_t rem, uint64_t total)
186
{
187
    QObject *obj;
188

    
189
    obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
190
                               "'remaining': %" PRId64 ", "
191
                               "'total': %" PRId64 " }", trans, rem, total);
192
    qdict_put_obj(qdict, name, obj);
193
}
194

    
195
/**
196
 * do_info_migrate(): Migration status
197
 *
198
 * Return a QDict. If migration is active there will be another
199
 * QDict with RAM migration status and if block migration is active
200
 * another one with block migration status.
201
 *
202
 * The main QDict contains the following:
203
 *
204
 * - "status": migration status
205
 * - "ram": only present if "status" is "active", it is a QDict with the
206
 *   following RAM information (in bytes):
207
 *          - "transferred": amount transferred
208
 *          - "remaining": amount remaining
209
 *          - "total": total
210
 * - "disk": only present if "status" is "active" and it is a block migration,
211
 *   it is a QDict with the following disk information (in bytes):
212
 *          - "transferred": amount transferred
213
 *          - "remaining": amount remaining
214
 *          - "total": total
215
 *
216
 * Examples:
217
 *
218
 * 1. Migration is "completed":
219
 *
220
 * { "status": "completed" }
221
 *
222
 * 2. Migration is "active" and it is not a block migration:
223
 *
224
 * { "status": "active",
225
 *            "ram": { "transferred": 123, "remaining": 123, "total": 246 } }
226
 *
227
 * 3. Migration is "active" and it is a block migration:
228
 *
229
 * { "status": "active",
230
 *   "ram": { "total": 1057024, "remaining": 1053304, "transferred": 3720 },
231
 *   "disk": { "total": 20971520, "remaining": 20880384, "transferred": 91136 }}
232
 */
233
void do_info_migrate(Monitor *mon, QObject **ret_data)
234
{
235
    QDict *qdict;
236
    MigrationState *s = current_migration;
237

    
238
    if (s) {
239
        switch (s->get_status(s)) {
240
        case MIG_STATE_ACTIVE:
241
            qdict = qdict_new();
242
            qdict_put(qdict, "status", qstring_from_str("active"));
243

    
244
            migrate_put_status(qdict, "ram", ram_bytes_transferred(),
245
                               ram_bytes_remaining(), ram_bytes_total());
246

    
247
            if (blk_mig_active()) {
248
                migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
249
                                   blk_mig_bytes_remaining(),
250
                                   blk_mig_bytes_total());
251
            }
252

    
253
            *ret_data = QOBJECT(qdict);
254
            break;
255
        case MIG_STATE_COMPLETED:
256
            *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
257
            break;
258
        case MIG_STATE_ERROR:
259
            *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
260
            break;
261
        case MIG_STATE_CANCELLED:
262
            *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
263
            break;
264
        }
265
    }
266
}
267

    
268
/* shared migration helpers */
269

    
270
void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
271
{
272
    s->mon = mon;
273
    if (monitor_suspend(mon) == 0) {
274
        DPRINTF("suspending monitor\n");
275
    } else {
276
        monitor_printf(mon, "terminal does not allow synchronous "
277
                       "migration, continuing detached\n");
278
    }
279
}
280

    
281
void migrate_fd_error(FdMigrationState *s)
282
{
283
    DPRINTF("setting error state\n");
284
    s->state = MIG_STATE_ERROR;
285
    migrate_fd_cleanup(s);
286
}
287

    
288
void migrate_fd_cleanup(FdMigrationState *s)
289
{
290
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
291

    
292
    if (s->file) {
293
        DPRINTF("closing file\n");
294
        qemu_fclose(s->file);
295
        s->file = NULL;
296
    }
297

    
298
    if (s->fd != -1)
299
        close(s->fd);
300

    
301
    /* Don't resume monitor until we've flushed all of the buffers */
302
    if (s->mon) {
303
        monitor_resume(s->mon);
304
    }
305

    
306
    s->fd = -1;
307
}
308

    
309
void migrate_fd_put_notify(void *opaque)
310
{
311
    FdMigrationState *s = opaque;
312

    
313
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
314
    qemu_file_put_notify(s->file);
315
}
316

    
317
ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
318
{
319
    FdMigrationState *s = opaque;
320
    ssize_t ret;
321

    
322
    do {
323
        ret = s->write(s, data, size);
324
    } while (ret == -1 && ((s->get_error(s)) == EINTR));
325

    
326
    if (ret == -1)
327
        ret = -(s->get_error(s));
328

    
329
    if (ret == -EAGAIN)
330
        qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
331

    
332
    return ret;
333
}
334

    
335
void migrate_fd_connect(FdMigrationState *s)
336
{
337
    int ret;
338

    
339
    s->file = qemu_fopen_ops_buffered(s,
340
                                      s->bandwidth_limit,
341
                                      migrate_fd_put_buffer,
342
                                      migrate_fd_put_ready,
343
                                      migrate_fd_wait_for_unfreeze,
344
                                      migrate_fd_close);
345

    
346
    DPRINTF("beginning savevm\n");
347
    ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
348
                                  s->mig_state.shared);
349
    if (ret < 0) {
350
        DPRINTF("failed, %d\n", ret);
351
        migrate_fd_error(s);
352
        return;
353
    }
354
    
355
    migrate_fd_put_ready(s);
356
}
357

    
358
void migrate_fd_put_ready(void *opaque)
359
{
360
    FdMigrationState *s = opaque;
361

    
362
    if (s->state != MIG_STATE_ACTIVE) {
363
        DPRINTF("put_ready returning because of non-active state\n");
364
        return;
365
    }
366

    
367
    DPRINTF("iterate\n");
368
    if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
369
        int state;
370
        int old_vm_running = vm_running;
371

    
372
        DPRINTF("done iterating\n");
373
        vm_stop(0);
374

    
375
        qemu_aio_flush();
376
        bdrv_flush_all();
377
        if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
378
            if (old_vm_running) {
379
                vm_start();
380
            }
381
            state = MIG_STATE_ERROR;
382
        } else {
383
            state = MIG_STATE_COMPLETED;
384
        }
385
        migrate_fd_cleanup(s);
386
        s->state = state;
387
    }
388
}
389

    
390
int migrate_fd_get_status(MigrationState *mig_state)
391
{
392
    FdMigrationState *s = migrate_to_fms(mig_state);
393
    return s->state;
394
}
395

    
396
void migrate_fd_cancel(MigrationState *mig_state)
397
{
398
    FdMigrationState *s = migrate_to_fms(mig_state);
399

    
400
    if (s->state != MIG_STATE_ACTIVE)
401
        return;
402

    
403
    DPRINTF("cancelling migration\n");
404

    
405
    s->state = MIG_STATE_CANCELLED;
406
    qemu_savevm_state_cancel(s->mon, s->file);
407

    
408
    migrate_fd_cleanup(s);
409
}
410

    
411
void migrate_fd_release(MigrationState *mig_state)
412
{
413
    FdMigrationState *s = migrate_to_fms(mig_state);
414

    
415
    DPRINTF("releasing state\n");
416
   
417
    if (s->state == MIG_STATE_ACTIVE) {
418
        s->state = MIG_STATE_CANCELLED;
419
        migrate_fd_cleanup(s);
420
    }
421
    free(s);
422
}
423

    
424
void migrate_fd_wait_for_unfreeze(void *opaque)
425
{
426
    FdMigrationState *s = opaque;
427
    int ret;
428

    
429
    DPRINTF("wait for unfreeze\n");
430
    if (s->state != MIG_STATE_ACTIVE)
431
        return;
432

    
433
    do {
434
        fd_set wfds;
435

    
436
        FD_ZERO(&wfds);
437
        FD_SET(s->fd, &wfds);
438

    
439
        ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
440
    } while (ret == -1 && (s->get_error(s)) == EINTR);
441
}
442

    
443
int migrate_fd_close(void *opaque)
444
{
445
    FdMigrationState *s = opaque;
446

    
447
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
448
    return s->close(s);
449
}