Statistics
| Branch: | Revision:

root / migration.c @ d523d5d6

History | View | Annotate | Download (11.3 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
int qemu_start_incoming_migration(const char *uri)
40
{
41
    const char *p;
42
    int ret;
43

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

    
61
void process_incoming_migration(QEMUFile *f)
62
{
63
    if (qemu_loadvm_state(f) < 0) {
64
        fprintf(stderr, "load of migration failed\n");
65
        exit(0);
66
    }
67
    qemu_announce_self();
68
    DPRINTF("successfully loaded vm state\n");
69

    
70
    incoming_expected = false;
71

    
72
    if (autostart)
73
        vm_start();
74
}
75

    
76
int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data)
77
{
78
    MigrationState *s = NULL;
79
    const char *p;
80
    int detach = qdict_get_try_bool(qdict, "detach", 0);
81
    int blk = qdict_get_try_bool(qdict, "blk", 0);
82
    int inc = qdict_get_try_bool(qdict, "inc", 0);
83
    const char *uri = qdict_get_str(qdict, "uri");
84

    
85
    if (current_migration &&
86
        current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) {
87
        monitor_printf(mon, "migration already in progress\n");
88
        return -1;
89
    }
90

    
91
    if (strstart(uri, "tcp:", &p)) {
92
        s = tcp_start_outgoing_migration(mon, p, max_throttle, detach,
93
                                         blk, inc);
94
#if !defined(WIN32)
95
    } else if (strstart(uri, "exec:", &p)) {
96
        s = exec_start_outgoing_migration(mon, p, max_throttle, detach,
97
                                          blk, inc);
98
    } else if (strstart(uri, "unix:", &p)) {
99
        s = unix_start_outgoing_migration(mon, p, max_throttle, detach,
100
                                          blk, inc);
101
    } else if (strstart(uri, "fd:", &p)) {
102
        s = fd_start_outgoing_migration(mon, p, max_throttle, detach, 
103
                                        blk, inc);
104
#endif
105
    } else {
106
        monitor_printf(mon, "unknown migration protocol: %s\n", uri);
107
        return -1;
108
    }
109

    
110
    if (s == NULL) {
111
        monitor_printf(mon, "migration failed\n");
112
        return -1;
113
    }
114

    
115
    if (current_migration) {
116
        current_migration->release(current_migration);
117
    }
118

    
119
    current_migration = s;
120
    return 0;
121
}
122

    
123
int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data)
124
{
125
    MigrationState *s = current_migration;
126

    
127
    if (s)
128
        s->cancel(s);
129

    
130
    return 0;
131
}
132

    
133
int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data)
134
{
135
    double d;
136
    FdMigrationState *s;
137

    
138
    d = qdict_get_double(qdict, "value");
139
    d = MAX(0, MIN(UINT32_MAX, d));
140
    max_throttle = d;
141

    
142
    s = migrate_to_fms(current_migration);
143
    if (s && s->file) {
144
        qemu_file_set_rate_limit(s->file, max_throttle);
145
    }
146

    
147
    return 0;
148
}
149

    
150
/* amount of nanoseconds we are willing to wait for migration to be down.
151
 * the choice of nanoseconds is because it is the maximum resolution that
152
 * get_clock() can achieve. It is an internal measure. All user-visible
153
 * units must be in seconds */
154
static uint64_t max_downtime = 30000000;
155

    
156
uint64_t migrate_max_downtime(void)
157
{
158
    return max_downtime;
159
}
160

    
161
int do_migrate_set_downtime(Monitor *mon, const QDict *qdict,
162
                            QObject **ret_data)
163
{
164
    double d;
165

    
166
    d = qdict_get_double(qdict, "value") * 1e9;
167
    d = MAX(0, MIN(UINT64_MAX, d));
168
    max_downtime = (uint64_t)d;
169

    
170
    return 0;
171
}
172

    
173
static void migrate_print_status(Monitor *mon, const char *name,
174
                                 const QDict *status_dict)
175
{
176
    QDict *qdict;
177

    
178
    qdict = qobject_to_qdict(qdict_get(status_dict, name));
179

    
180
    monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
181
                        qdict_get_int(qdict, "transferred") >> 10);
182
    monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
183
                        qdict_get_int(qdict, "remaining") >> 10);
184
    monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
185
                        qdict_get_int(qdict, "total") >> 10);
186
}
187

    
188
void do_info_migrate_print(Monitor *mon, const QObject *data)
189
{
190
    QDict *qdict;
191

    
192
    qdict = qobject_to_qdict(data);
193

    
194
    monitor_printf(mon, "Migration status: %s\n",
195
                   qdict_get_str(qdict, "status"));
196

    
197
    if (qdict_haskey(qdict, "ram")) {
198
        migrate_print_status(mon, "ram", qdict);
199
    }
200

    
201
    if (qdict_haskey(qdict, "disk")) {
202
        migrate_print_status(mon, "disk", qdict);
203
    }
204
}
205

    
206
static void migrate_put_status(QDict *qdict, const char *name,
207
                               uint64_t trans, uint64_t rem, uint64_t total)
208
{
209
    QObject *obj;
210

    
211
    obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
212
                               "'remaining': %" PRId64 ", "
213
                               "'total': %" PRId64 " }", trans, rem, total);
214
    qdict_put_obj(qdict, name, obj);
215
}
216

    
217
void do_info_migrate(Monitor *mon, QObject **ret_data)
218
{
219
    QDict *qdict;
220
    MigrationState *s = current_migration;
221

    
222
    if (s) {
223
        switch (s->get_status(s)) {
224
        case MIG_STATE_ACTIVE:
225
            qdict = qdict_new();
226
            qdict_put(qdict, "status", qstring_from_str("active"));
227

    
228
            migrate_put_status(qdict, "ram", ram_bytes_transferred(),
229
                               ram_bytes_remaining(), ram_bytes_total());
230

    
231
            if (blk_mig_active()) {
232
                migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
233
                                   blk_mig_bytes_remaining(),
234
                                   blk_mig_bytes_total());
235
            }
236

    
237
            *ret_data = QOBJECT(qdict);
238
            break;
239
        case MIG_STATE_COMPLETED:
240
            *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
241
            break;
242
        case MIG_STATE_ERROR:
243
            *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
244
            break;
245
        case MIG_STATE_CANCELLED:
246
            *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
247
            break;
248
        }
249
    }
250
}
251

    
252
/* shared migration helpers */
253

    
254
void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
255
{
256
    s->mon = mon;
257
    if (monitor_suspend(mon) == 0) {
258
        DPRINTF("suspending monitor\n");
259
    } else {
260
        monitor_printf(mon, "terminal does not allow synchronous "
261
                       "migration, continuing detached\n");
262
    }
263
}
264

    
265
void migrate_fd_error(FdMigrationState *s)
266
{
267
    DPRINTF("setting error state\n");
268
    s->state = MIG_STATE_ERROR;
269
    migrate_fd_cleanup(s);
270
}
271

    
272
int migrate_fd_cleanup(FdMigrationState *s)
273
{
274
    int ret = 0;
275

    
276
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
277

    
278
    if (s->file) {
279
        DPRINTF("closing file\n");
280
        if (qemu_fclose(s->file) != 0) {
281
            ret = -1;
282
        }
283
        s->file = NULL;
284
    }
285

    
286
    if (s->fd != -1)
287
        close(s->fd);
288

    
289
    /* Don't resume monitor until we've flushed all of the buffers */
290
    if (s->mon) {
291
        monitor_resume(s->mon);
292
    }
293

    
294
    s->fd = -1;
295

    
296
    return ret;
297
}
298

    
299
void migrate_fd_put_notify(void *opaque)
300
{
301
    FdMigrationState *s = opaque;
302

    
303
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
304
    qemu_file_put_notify(s->file);
305
}
306

    
307
ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
308
{
309
    FdMigrationState *s = opaque;
310
    ssize_t ret;
311

    
312
    do {
313
        ret = s->write(s, data, size);
314
    } while (ret == -1 && ((s->get_error(s)) == EINTR));
315

    
316
    if (ret == -1)
317
        ret = -(s->get_error(s));
318

    
319
    if (ret == -EAGAIN) {
320
        qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
321
    } else if (ret < 0) {
322
        if (s->mon) {
323
            monitor_resume(s->mon);
324
        }
325
        s->state = MIG_STATE_ERROR;
326
    }
327

    
328
    return ret;
329
}
330

    
331
void migrate_fd_connect(FdMigrationState *s)
332
{
333
    int ret;
334

    
335
    s->file = qemu_fopen_ops_buffered(s,
336
                                      s->bandwidth_limit,
337
                                      migrate_fd_put_buffer,
338
                                      migrate_fd_put_ready,
339
                                      migrate_fd_wait_for_unfreeze,
340
                                      migrate_fd_close);
341

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

    
354
void migrate_fd_put_ready(void *opaque)
355
{
356
    FdMigrationState *s = opaque;
357

    
358
    if (s->state != MIG_STATE_ACTIVE) {
359
        DPRINTF("put_ready returning because of non-active state\n");
360
        return;
361
    }
362

    
363
    DPRINTF("iterate\n");
364
    if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
365
        int state;
366
        int old_vm_running = vm_running;
367

    
368
        DPRINTF("done iterating\n");
369
        vm_stop(0);
370

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

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

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

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

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

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

    
409
    migrate_fd_cleanup(s);
410
}
411

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

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

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

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

    
434
    do {
435
        fd_set wfds;
436

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

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

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

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