Statistics
| Branch: | Revision:

root / migration.c @ 5667c493

History | View | Annotate | Download (12.2 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
void 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

    
109
void do_migrate_set_speed(Monitor *mon, const QDict *qdict)
110
{
111
    double d;
112
    FdMigrationState *s;
113

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

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

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

    
130
uint64_t migrate_max_downtime(void)
131
{
132
    return max_downtime;
133
}
134

    
135
void do_migrate_set_downtime(Monitor *mon, const QDict *qdict)
136
{
137
    char *ptr;
138
    double d;
139
    const char *value = qdict_get_str(qdict, "value");
140

    
141
    d = strtod(value, &ptr);
142
    if (!strcmp(ptr,"ms")) {
143
        d *= 1000000;
144
    } else if (!strcmp(ptr,"us")) {
145
        d *= 1000;
146
    } else if (!strcmp(ptr,"ns")) {
147
    } else {
148
        /* all else considered to be seconds */
149
        d *= 1000000000;
150
    }
151

    
152
    max_downtime = (uint64_t)d;
153
}
154

    
155
static void migrate_print_status(Monitor *mon, const char *name,
156
                                 const QDict *status_dict)
157
{
158
    QDict *qdict;
159

    
160
    qdict = qobject_to_qdict(qdict_get(status_dict, name));
161

    
162
    monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name,
163
                        qdict_get_int(qdict, "transferred") >> 10);
164
    monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name,
165
                        qdict_get_int(qdict, "remaining") >> 10);
166
    monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name,
167
                        qdict_get_int(qdict, "total") >> 10);
168
}
169

    
170
void do_info_migrate_print(Monitor *mon, const QObject *data)
171
{
172
    QDict *qdict;
173

    
174
    qdict = qobject_to_qdict(data);
175

    
176
    monitor_printf(mon, "Migration status: %s\n",
177
                   qdict_get_str(qdict, "status"));
178

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

    
183
    if (qdict_haskey(qdict, "disk")) {
184
        migrate_print_status(mon, "disk", qdict);
185
    }
186
}
187

    
188
static void migrate_put_status(QDict *qdict, const char *name,
189
                               uint64_t trans, uint64_t rem, uint64_t total)
190
{
191
    QObject *obj;
192

    
193
    obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", "
194
                               "'remaining': %" PRId64 ", "
195
                               "'total': %" PRId64 " }", trans, rem, total);
196
    assert(obj != NULL);
197

    
198
    qdict_put_obj(qdict, name, obj);
199
}
200

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

    
244
    if (s) {
245
        switch (s->get_status(s)) {
246
        case MIG_STATE_ACTIVE:
247
            qdict = qdict_new();
248
            qdict_put(qdict, "status", qstring_from_str("active"));
249

    
250
            migrate_put_status(qdict, "ram", ram_bytes_transferred(),
251
                               ram_bytes_remaining(), ram_bytes_total());
252

    
253
            if (blk_mig_active()) {
254
                migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(),
255
                                   blk_mig_bytes_remaining(),
256
                                   blk_mig_bytes_total());
257
            }
258

    
259
            *ret_data = QOBJECT(qdict);
260
            break;
261
        case MIG_STATE_COMPLETED:
262
            *ret_data = qobject_from_jsonf("{ 'status': 'completed' }");
263
            break;
264
        case MIG_STATE_ERROR:
265
            *ret_data = qobject_from_jsonf("{ 'status': 'failed' }");
266
            break;
267
        case MIG_STATE_CANCELLED:
268
            *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }");
269
            break;
270
        }
271
        assert(*ret_data != NULL);
272
    }
273
}
274

    
275
/* shared migration helpers */
276

    
277
void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon)
278
{
279
    s->mon = mon;
280
    if (monitor_suspend(mon) == 0) {
281
        dprintf("suspending monitor\n");
282
    } else {
283
        monitor_printf(mon, "terminal does not allow synchronous "
284
                       "migration, continuing detached\n");
285
    }
286
}
287

    
288
void migrate_fd_error(FdMigrationState *s)
289
{
290
    dprintf("setting error state\n");
291
    s->state = MIG_STATE_ERROR;
292
    migrate_fd_cleanup(s);
293
}
294

    
295
void migrate_fd_cleanup(FdMigrationState *s)
296
{
297
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
298

    
299
    if (s->file) {
300
        dprintf("closing file\n");
301
        qemu_fclose(s->file);
302
        s->file = NULL;
303
    }
304

    
305
    if (s->fd != -1)
306
        close(s->fd);
307

    
308
    /* Don't resume monitor until we've flushed all of the buffers */
309
    if (s->mon) {
310
        monitor_resume(s->mon);
311
    }
312

    
313
    s->fd = -1;
314
}
315

    
316
void migrate_fd_put_notify(void *opaque)
317
{
318
    FdMigrationState *s = opaque;
319

    
320
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
321
    qemu_file_put_notify(s->file);
322
}
323

    
324
ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size)
325
{
326
    FdMigrationState *s = opaque;
327
    ssize_t ret;
328

    
329
    do {
330
        ret = s->write(s, data, size);
331
    } while (ret == -1 && ((s->get_error(s)) == EINTR));
332

    
333
    if (ret == -1)
334
        ret = -(s->get_error(s));
335

    
336
    if (ret == -EAGAIN)
337
        qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s);
338

    
339
    return ret;
340
}
341

    
342
void migrate_fd_connect(FdMigrationState *s)
343
{
344
    int ret;
345

    
346
    s->file = qemu_fopen_ops_buffered(s,
347
                                      s->bandwidth_limit,
348
                                      migrate_fd_put_buffer,
349
                                      migrate_fd_put_ready,
350
                                      migrate_fd_wait_for_unfreeze,
351
                                      migrate_fd_close);
352

    
353
    dprintf("beginning savevm\n");
354
    ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk,
355
                                  s->mig_state.shared);
356
    if (ret < 0) {
357
        dprintf("failed, %d\n", ret);
358
        migrate_fd_error(s);
359
        return;
360
    }
361
    
362
    migrate_fd_put_ready(s);
363
}
364

    
365
void migrate_fd_put_ready(void *opaque)
366
{
367
    FdMigrationState *s = opaque;
368

    
369
    if (s->state != MIG_STATE_ACTIVE) {
370
        dprintf("put_ready returning because of non-active state\n");
371
        return;
372
    }
373

    
374
    dprintf("iterate\n");
375
    if (qemu_savevm_state_iterate(s->mon, s->file) == 1) {
376
        int state;
377
        int old_vm_running = vm_running;
378

    
379
        dprintf("done iterating\n");
380
        vm_stop(0);
381

    
382
        qemu_aio_flush();
383
        bdrv_flush_all();
384
        if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) {
385
            if (old_vm_running) {
386
                vm_start();
387
            }
388
            state = MIG_STATE_ERROR;
389
        } else {
390
            state = MIG_STATE_COMPLETED;
391
        }
392
        migrate_fd_cleanup(s);
393
        s->state = state;
394
    }
395
}
396

    
397
int migrate_fd_get_status(MigrationState *mig_state)
398
{
399
    FdMigrationState *s = migrate_to_fms(mig_state);
400
    return s->state;
401
}
402

    
403
void migrate_fd_cancel(MigrationState *mig_state)
404
{
405
    FdMigrationState *s = migrate_to_fms(mig_state);
406

    
407
    if (s->state != MIG_STATE_ACTIVE)
408
        return;
409

    
410
    dprintf("cancelling migration\n");
411

    
412
    s->state = MIG_STATE_CANCELLED;
413
    qemu_savevm_state_cancel(s->mon, s->file);
414

    
415
    migrate_fd_cleanup(s);
416
}
417

    
418
void migrate_fd_release(MigrationState *mig_state)
419
{
420
    FdMigrationState *s = migrate_to_fms(mig_state);
421

    
422
    dprintf("releasing state\n");
423
   
424
    if (s->state == MIG_STATE_ACTIVE) {
425
        s->state = MIG_STATE_CANCELLED;
426
        migrate_fd_cleanup(s);
427
    }
428
    free(s);
429
}
430

    
431
void migrate_fd_wait_for_unfreeze(void *opaque)
432
{
433
    FdMigrationState *s = opaque;
434
    int ret;
435

    
436
    dprintf("wait for unfreeze\n");
437
    if (s->state != MIG_STATE_ACTIVE)
438
        return;
439

    
440
    do {
441
        fd_set wfds;
442

    
443
        FD_ZERO(&wfds);
444
        FD_SET(s->fd, &wfds);
445

    
446
        ret = select(s->fd + 1, NULL, &wfds, NULL, NULL);
447
    } while (ret == -1 && (s->get_error(s)) == EINTR);
448
}
449

    
450
int migrate_fd_close(void *opaque)
451
{
452
    FdMigrationState *s = opaque;
453

    
454
    qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
455
    return s->close(s);
456
}